group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_1630_cpp
Expression Mining Consider an arithmetic expression built by combining single-digit positive integers with addition symbols + , multiplication symbols * , and parentheses ( ) , defined by the following grammar rules with the start symbol E . E ::= T | E '+' T T ::= F | T '*' F F ::= '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '(' E ')' When such an arithmetic expression is viewed as a string, its substring, that is, a contiguous sequence of characters within the string, may again form an arithmetic expression. Given an integer n and a string s representing an arithmetic expression, let us count the number of its substrings that can be read as arithmetic expressions with values computed equal to n . Input The input consists of multiple datasets, each in the following format. n s A dataset consists of two lines. In the first line, the target value n is given. n is an integer satisfying 1 ≤ n ≤ 10 9 . The string s given in the second line is an arithmetic expression conforming to the grammar defined above. The length of s does not exceed 2×10 6 . The nesting depth of the parentheses in the string is at most 1000. The end of the input is indicated by a line containing a single zero. The sum of the lengths of s in all the datasets does not exceed 5×10 6 . Output For each dataset, output in one line the number of substrings of s that conform to the above grammar and have the value n . The same sequence of characters appearing at different positions should be counted separately. Sample Input 3 (1+2)*3+3 2 1*1*1+1*1*1 587 1*(2*3*4)+5+((6+7*8))*(9) 0 Output for the Sample Input 4 9 2
[ { "submission_id": "aoj_1630_10665435", "code_snippet": "// C++ includes used for precompiling -*- C++ -*-\n// Copyright (C) 2003-2013 Free Software Foundation, Inc.\n//\n// This file is part of the GNU ISO C++ Library. This library is free\n// software; you can redistribute it and/or modify it under the\n// terms of the GNU General Public License as published by the\n// Free Software Foundation; either version 3, or (at your option)\n// any later version.\n// This library is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n// Under Section 7 of GPL version 3, you are granted additional\n// permissions described in the GCC Runtime Library Exception, version\n// 3.1, as published by the Free Software Foundation.\n// You should have received a copy of the GNU General Public License and\n// a copy of the GCC Runtime Library Exception along with this program;\n// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see\n// <http://w...content-available-to-author-only...u.org/licenses/>.\n/** @file stdc++.h\n * This is an implementation file for a precompiled header.\n */\n// 17.4.1.2 Headers\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\nusing mint = modint<998244353>;\nll calc(ll n, string s) {\n auto it = s.begin();\n ll ans = 0;\n function<ll()> rec = [&]() {\n // return evaluate value\n vector<int> vec{-2};\n while (it != s.end() && *it != ')') {\n if (*it == '+') {\n vec.push_back(-1);\n it++;\n } else if (*it == '*') {\n vec.push_back(-2);\n it++;\n } else if (isdigit(*it)) {\n vec.push_back(*it - '0');\n it++;\n } else if (*it == '(') {\n it++;\n vec.push_back(rec());\n it++;\n } else\n assert(false);\n }\n ll sum = 0; //<=n\n deque<vector<ll>> deq;\n ll prod = 1, eval = 0;\n vector<ll> cur;\n deque<ll> in_T;\n ll prod2 = 1;\n int len = 0;\n for (int i = 0; i < vec.size(); i += 2) {\n len++;\n if (vec[i] == -1) {\n eval += prod;\n sum += prod;\n for (int j = cur.size() - 2; j >= 0; j--) {\n cur[j] = min((ll)INF, cur[j] * cur[j + 1]);\n }\n reverse(all(cur));\n deq.push_back(cur);\n cur.clear();\n in_T.clear();\n prod2 = vec[i + 1];\n prod = vec[i + 1];\n cur.push_back(vec[i + 1]);\n in_T.push_back(vec[i + 1]);\n } else {\n prod = min((ll)INF, prod * vec[i + 1]);\n prod2 = prod2 * vec[i + 1];\n cur.push_back(vec[i + 1]);\n in_T.push_back(vec[i + 1]);\n }\n while (deq.size() && prod + sum > n) {\n sum -= deq.front().back();\n deq.front().pop_back();\n if (deq.front().empty())\n deq.pop_front();\n else\n sum += deq.front().back();\n len--;\n }\n // cout<<prod2<<endl;\n while (prod2 > n && in_T.size()) {\n prod2 /= in_T.front();\n in_T.pop_front();\n }\n if (deq.size())\n ans += len;\n else\n ans += in_T.size();\n }\n eval = min((ll)INF, eval + prod);\n // cout<<vec<<endl;\n // cout<<eval<<endl;\n return min((ll)INF, eval);\n };\n rec();\n return ans;\n}\n\nvoid solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n string s;\n cin >> s;\n // cerr<<calc(n,s)<<\" \"<<calc(n-1,s)<<endl;\n cout << calc(n, s) - calc(n - 1, s) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 74604, "score_of_the_acc": -0.0944, "final_rank": 8 }, { "submission_id": "aoj_1630_10641287", "code_snippet": "// C++ includes used for precompiling -*- C++ -*-\n// Copyright (C) 2003-2013 Free Software Foundation, Inc.\n//\n// This file is part of the GNU ISO C++ Library. This library is free\n// software; you can redistribute it and/or modify it under the\n// terms of the GNU General Public License as published by the\n// Free Software Foundation; either version 3, or (at your option)\n// any later version.\n// This library is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n// Under Section 7 of GPL version 3, you are granted additional\n// permissions described in the GCC Runtime Library Exception, version\n// 3.1, as published by the Free Software Foundation.\n// You should have received a copy of the GNU General Public License and\n// a copy of the GCC Runtime Library Exception along with this program;\n// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see\n// <http://w...content-available-to-author-only...u.org/licenses/>.\n/** @file stdc++.h\n * This is an implementation file for a precompiled header.\n */\n// 17.4.1.2 Headers\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\nusing mint = modint<998244353>;\nusing State = string::iterator;\nusing F = pair<ll, ll>;\nll calc(ll n, string s) {\n auto it = s.begin();\n ll ans = 0;\n function<ll()> rec = [&]() {\n // return evaluate value\n vector<int> vec{-2};\n while (it != s.end() && *it != ')') {\n if (*it == '+') {\n vec.push_back(-1);\n it++;\n } else if (*it == '*') {\n vec.push_back(-2);\n it++;\n } else if (isdigit(*it)) {\n vec.push_back(*it - '0');\n it++;\n } else if (*it == '(') {\n it++;\n vec.push_back(rec());\n it++;\n } else\n assert(false);\n }\n ll sum = 0; //<=n\n deque<vector<ll>> deq;\n ll prod = 1, eval = 0;\n vector<ll> cur;\n deque<ll> in_T;\n ll prod2 = 1;\n int len = 0;\n for (int i = 0; i < vec.size(); i += 2) {\n len++;\n if (vec[i] == -1) {\n eval += prod;\n sum += prod;\n for (int j = cur.size() - 2; j >= 0; j--) {\n cur[j] = min((ll)INF, cur[j] * cur[j + 1]);\n }\n reverse(all(cur));\n deq.push_back(cur);\n cur.clear();\n in_T.clear();\n prod2 = vec[i + 1];\n prod = vec[i + 1];\n cur.push_back(vec[i + 1]);\n in_T.push_back(vec[i + 1]);\n } else {\n prod = min((ll)INF, prod * vec[i + 1]);\n prod2 = prod2 * vec[i + 1];\n cur.push_back(vec[i + 1]);\n in_T.push_back(vec[i + 1]);\n }\n while (deq.size() && prod + sum > n) {\n sum -= deq.front().back();\n deq.front().pop_back();\n if (deq.front().empty())\n deq.pop_front();\n else\n sum += deq.front().back();\n len--;\n }\n // cout<<prod2<<endl;\n while (prod2 > n && in_T.size()) {\n prod2 /= in_T.front();\n in_T.pop_front();\n }\n if (deq.size())\n ans += len;\n else\n ans += in_T.size();\n }\n eval = min((ll)INF, eval + prod);\n // cout<<vec<<endl;\n // cout<<eval<<endl;\n return min((ll)INF, eval);\n };\n rec();\n return ans;\n}\n\nvoid solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n string s;\n cin >> s;\n // cerr<<calc(n,s)<<\" \"<<calc(n-1,s)<<endl;\n cout << calc(n, s) - calc(n - 1, s) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 74752, "score_of_the_acc": -0.0971, "final_rank": 9 }, { "submission_id": "aoj_1630_10641123", "code_snippet": "// C++ includes used for precompiling -*- C++ -*-\n// Copyright (C) 2003-2013 Free Software Foundation, Inc.\n//\n// This file is part of the GNU ISO C++ Library. This library is free\n// software; you can redistribute it and/or modify it under the\n// terms of the GNU General Public License as published by the\n// Free Software Foundation; either version 3, or (at your option)\n// any later version.\n// This library is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n// Under Section 7 of GPL version 3, you are granted additional\n// permissions described in the GCC Runtime Library Exception, version\n// 3.1, as published by the Free Software Foundation.\n// You should have received a copy of the GNU General Public License and\n// a copy of the GCC Runtime Library Exception along with this program;\n// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see\n// <http://w...content-available-to-author-only...u.org/licenses/>.\n/** @file stdc++.h\n * This is an implementation file for a precompiled header.\n */\n// 17.4.1.2 Headers\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\nusing mint = modint<998244353>;\nusing State=string::iterator;\nusing F=pair<ll,ll>;\nll calc(ll n,string s){\n auto it=s.begin();\n ll ans=0;\n function<ll()>rec=[&](){\n //return evaluate value\n vector<int>vec;\n while(it!=s.end()&&*it!=')'){\n //cout<<*it<<endl;\n if(*it=='+'){\n vec.push_back(-1);\n it++;\n }\n else if(*it=='*'){\n vec.push_back(-2);\n it++;\n }\n else if(isdigit(*it)){\n vec.push_back(*it-'0');\n it++;\n }\n else if(*it=='('){\n it++;\n vec.push_back(rec());\n it++;\n }\n else assert(false);\n }\n ll sum=0;//<=n\n deque<vector<ll>>deq;\n ll prod=vec[0],eval=0;\n if(prod<=n)ans+=1;\n vector<ll>cur{vec[0]};\n deque<ll>in_T{vec[0]};\n ll prod2=vec[0];\n int len=1;\n for(int i=1;i<vec.size();i+=2){\n len++;\n if(vec[i]==-1){\n eval+=prod;\n sum+=prod;\n for(int j=cur.size()-2;j>=0;j--){\n cur[j]=min((ll)INF,cur[j]*cur[j+1]);\n }\n reverse(all(cur));\n deq.push_back(cur);\n cur.clear();\n in_T.clear();\n prod2=vec[i+1];\n prod=vec[i+1];\n cur.push_back(vec[i+1]);\n in_T.push_back(vec[i+1]);\n }\n else{\n prod=min((ll)INF,prod*vec[i+1]);\n prod2=prod2*vec[i+1];\n cur.push_back(vec[i+1]);\n in_T.push_back(vec[i+1]);\n }\n while(deq.size()&&prod+sum>n){\n sum-=deq.front().back();\n deq.front().pop_back();\n if(deq.front().empty())deq.pop_front();\n else sum+=deq.front().back();\n len--;\n }\n //cout<<prod2<<endl;\n while(prod2>n&&in_T.size()){\n prod2/=in_T.front();\n in_T.pop_front();\n }\n if(deq.size()) ans+=len;\n else ans+=in_T.size();\n }\n eval=min((ll)INF,eval+prod);\n //cout<<vec<<endl;\n //cout<<eval<<endl;\n return min((ll)INF,eval);\n };\n rec();\n return ans;\n}\n\nvoid solve() {\n int n;\n cin>>n;\n if(n==0)exit(0);\n string s;\n cin>>s;\n //cerr<<calc(n,s)<<\" \"<<calc(n-1,s)<<endl;\n cout<<calc(n,s)-calc(n-1,s)<<endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 74516, "score_of_the_acc": -0.0943, "final_rank": 7 }, { "submission_id": "aoj_1630_10625498", "code_snippet": "#ifndef _DEBUG\n#define _GLIBCXX_DEBUG\n#endif\n#include <bits/stdc++.h>\nusing namespace std;\n#include <atcoder/all>\nusing namespace atcoder;\n// #include <boost/rational.hpp>\n// using namespace boost;\n// using rat = rational<long long int>;\nusing mint = modint998244353;\n// using mint = modint1000000007;\n// using mint = mint;\nusing ll = long long;\nusing ld = long double;\nusing ull = uint64_t;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\nusing vm = vector<mint>;\nusing vvm = vector<vm>;\nusing vvvm = vector<vvm>;\nusing vstr = vector<string>;\n#define v(T) vector<T>\n#define vv(T) vector<vector<T>>\n#define vvv(T) vector<vector<vector<T>>>\n#define vvvv(T) vector<vector<vector<vector<T>>>>\n\nistream &operator>>(istream &is, mint &a){ll tmp; is >> tmp; a = tmp; return is;}\nostream &operator<<(ostream &os, const mint &a){ os << a.val(); return os; }\nstring to_string(const __int128_t &a) { if (a == 0) return \"0\"; string s = \"\"; __int128_t num = a; bool is_negative = false; if (num < 0) { is_negative = true; num = -num; } while (num > 0) { s += '0' + (num % 10); num /= 10; } if (is_negative) s += '-'; reverse(s.begin(), s.end()); return s; }\nistream &operator>>(istream &is, __int128_t &a){ string s; is >> s; a = 0; for(char c : s) { if(isdigit(c)) {a = a*10 + (c - '0'); } } if(s[0]=='-'){ a *= -1; } return is; }\nostream &operator<<(ostream &os, const __int128_t &a){ os << to_string(a); return os; }\ntemplate<class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << \" \" << p.second; return os; }\ntemplate<class T> istream &operator>>(istream &is, vector<T> &vec){ for(T &e : vec){is >> e;} return is; }\ntemplate<class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for(int i = 0; i < (int)vec.size(); i++) { os << vec[i] << (i + 1 != (int)vec.size() ? \" \" : \"\"); } return os; }\n\ntemplate <class... T> constexpr auto min (T... a) { return min(initializer_list<common_type_t<T...>>{a...}); }\ntemplate <class... T> constexpr auto max (T... a) { return max(initializer_list<common_type_t<T...>>{a...}); }\ntemplate<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> T opmin(T x, T y) { return min(x, y); }\ntemplate<class T> T einf() { return numeric_limits<T>::max(); }\ntemplate<class T> T opmax(T x, T y) { return max(x, y); }\ntemplate<class T> T eminf() { return numeric_limits<T>::min(); }\ntemplate<class T> T opsum(T x, T y) { return x + y; }\ntemplate<class T> T ezero() { return (T)0; }\n// #define maxseg(T) segtree<T, [](T x, T y){return max(x, y);}, [](){return (T)(-(1LL << 60));}>\n// #define minseg(T) segtree<T, [](T x, T y){return min(x, y);}, [](){return (T)((1LL << 60));}>\n// #define sumseg(T) segtree<T, [](T x, T y){return x + y;}, [](){return (T)(0);}>\ntemplate<class T> using minseg = segtree<T, opmin<T>, einf<T>>;\ntemplate<class T> using maxseg = segtree<T, opmax<T>, eminf<T>>;\ntemplate<class T> using sumseg = segtree<T, opsum<T>, ezero<T>>;\n// template<class T> struct v : vector<T> { using vector<T> :: vector; };\n// template<class T> struct vv : vector<v<T>> { using vector<v<T>> :: vector; };\n// template<class T> struct vvv : vector<vv<T>> { using vector<vv<T>> :: vector; };\ntemplate<class T> inline bool chmin(T& a, T b) {if(a > b){a = b; return true;} else {return false;}};\ntemplate<class T> inline bool chmax(T& a, T b) {if(a < b){a = b; return true;} else {return false;}};\n#define rep(i,n) for(ll i = 0; i < (ll)(n); i++)\n#define repr(i,n) for(ll i = (ll)(n) - 1; i >= 0; i--)\n#define REP(i, l, r) for(ll i = (ll)l; i <= (ll)(r); i++)\n#define REPR(i, l, r) for(ll i = (ll)r; i >= (ll)(l); i--)\nconst ll inf = (1 << 30);\nconst ll INF = (1LL << 60);\nconst vector<pair<ll, ll>> DIJ = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};\nvoid out(){cout<<'\\n';}\ntemplate<class T, class... Ts> void out(const T& a, const Ts&... b){ cout<<a; (cout<<... << (cout << ' ', b)); cout << '\\n';}\nvoid outf(){cout<<endl;}\ntemplate<class T, class... Ts> void outf(const T& a, const Ts&... b){ cout<<a; (cout<<... << (cout << ' ', b)); cout << endl;}\ntemplate<class T, class U> void outp(pair<T, U> a){ out((a).first, (a).second); }\ntemplate<class T, class U> void outpf(pair<T, U> a){ outf((a).first, (a).second); }\ntemplate<class T> void outv(T a){rep(i, (a).size()){ cout << (a)[i] << \" \"; } cout << endl;}\ntemplate<class T> void outvL(T a){rep(i, (a).size()){out((a)[i]);} cout << flush; }\n// template<class T> void outvv(T a){rep(i, a.size()){ rep(j, a.at(i).size()){cout << a.at(i).at(j) << \" \"; } cout << endl; }}\n// template<class T> void outvp(T a){rep(i, a.size()){ out2(a.at(i).first, a.at(i).second); }}\nvoid setpre(int a){cout << fixed << setprecision(a);}\n#define outN out(\"No\")\n#define outY out(\"Yes\")\n#define outYN(flag) out(flag ? \"Yes\" : \"No\")\n#define dame(...) {outf(__VA_ARGS__);return 0;}\n\ntemplate<class T> void read(vector<T>& vec){ for(int i = 0; i < (int)vec.size(); i++) { cin >> vec[i]; } }\ntemplate<class... T> void read(T&... a){(cin >> ... >> a);}\n#define readll(...) ll __VA_ARGS__; read(__VA_ARGS__)\n#define readvll(a, n) vector<ll> a(n); read(a)\n#define readvt(type, a, n) vector<type> a(n); read(a)\n#define readvll2(a, b, n) vector<ll> a(n), b(n); for(int lopi = 0; lopi < (int)(n); lopi++) cin >> (a)[lopi] >> (b)[lopi]\n#define readvll3(a, b, c, n) vector<ll> a(n), b(n), c(n); for(int lopi = 0; lopi < (int)(n); lopi++) cin >> (a)[lopi] >> (b)[lopi] >> (c)[lopi]\n#define readstr(...) string __VA_ARGS__; read(__VA_ARGS__)\n#define readundirG(G, N, M) G = vvll(N); rep(lopi, M) {ll a, b; cin >> a >> b; G[a-1].push_back(b-1); G[b-1].push_back(a-1);}\n#define readdirG(G, N, M) G = vvll(N); rep(lopi, M) {ll a, b; cin >> a >> b; G[a-1].push_back(b-1);}\n#define readundirwghG(G, N, M) G = vv(pll)(N); rep(lopi, M) {ll a, b, c; cin >> a >> b >> c; G[a-1].emplace_back(b-1,c); G[b-1].emplace_back(a-1, c);}\n#define readdirwghG (G, N, M) G = vv(pll)(N); rep(lopi, M) {ll a, b, c; cin >> a >> b >> c; G[a-1].emplace_back(b-1, c);}\n\n#define All(a) (a).begin(), (a).end()\ntemplate<class T> inline void sortr(T& a){ sort((a).rbegin(), (a).rend()); }\ntemplate<class T> inline vector<int> argsort(T V, bool rev = false){vector<int> res(V.size()); iota(res.begin(), res.end(), 0); sort(res.begin(), res.end(), [&](int x, int y){if(!rev){return V[x] < V[y];}else{return V[x] > V[y];}}); return res;}\ntemplate<class T, class U> inline void sort_by_idx(T& V, vector<U>& I){assert(V.size() == I.size()); T tmpv = V; for(int loopi = 0; loopi < (int)I.size(); loopi++){V[loopi] = tmpv[I.at(loopi)];}}\ntemplate<class T, class U> inline void sortp(vector<T>& v1, vector<U>& v2, bool rev1 = false, int rev2 = false){assert(v1.size() == v2.size()); vector<int> I(v1.size()); iota(I.begin(), I.end(), 0); sort(I.begin(), I.end(), [&](const int x, const int y){if(v1[x] != v1[y]){return (bool)(rev1 ^ (v1[x] < v1[y]));}else{if(v2[x]==v2[y]){return false;} return (bool)(rev2 ^ (v2[x] < v2[y]));}}); sort_by_idx(v1, I); sort_by_idx(v2, I);}\ntemplate<class T> T POW(T x, ll n) {T ret = 1; while(n > 0){if(n & 1) ret *= x; x *= x; n >>= 1;} return ret;}\nll powll(ll x, ll n){ll ret = 1; while(n > 0){if(n & 1) ret *= x; x *= x; n >>= 1;} return ret;}\ninline ll divceil(ll x, ll y) { if(x >= 0) {return(x / y + (ll)(x % y != 0)); } else { return -((-x) / y); } }\ninline ll divfloor(ll x, ll y) { if(x >= 0) { return x/y; } else { return -((-x)/y + (ll)((-x) % y != 0)); } }\ninline bool inLR(ll x, ll L, ll R){ return (L <= x && x < R); }\ninline bool inRect(ll pos_x, ll pos_y, ll rect_H, ll rect_W, ll rect_h = 0, ll rect_w = 0){ return (rect_h <= pos_x && pos_x < rect_H && rect_w <= pos_y && pos_y < rect_W); }\n\ntemplate<class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v){e++;} return v;}\ntemplate<class T> vector<T> operator++(vector<T> &v, signed) {auto res=v; for(auto &e : v){e++;} return res;}\ntemplate<class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v){e--;} return v;}\ntemplate<class T> vector<T> operator--(vector<T> &v, signed) {auto res=v; for(auto &e : v){e--;} return res;}\ntemplate<class T> vector<T> operator+(const vector<T> &x, const vector<T> &y) { assert(x.size() == y.size()); vector<T> ret(x.size()); for(int i = 0; i < (int)x.size(); i++) {ret[i] = x[i] + y[i];} return ret; }\ntemplate<class T> vector<T> operator-(const vector<T> &x, const vector<T> &y) { assert(x.size() == y.size()); vector<T> ret(x.size()); for(int i = 0; i < (int)x.size(); i++) {ret[i] = x[i] - y[i];} return ret; } \n\ntemplate<class T, class U> pair<T, U> operator+(const pair<T, U> &x, const pair<T, U> &y) { return make_pair(x.first + y.first, x.second + y.second); }\ntemplate<class T, class U> pair<T, U> operator-(const pair<T, U> &x, const pair<T, U> &y) { return make_pair(x.first - y.first, x.second - y.second); }\ntemplate<class T, class U> void operator+=(pair<T, U> &x, pair<T, U> &y) { x = x + y; }\ntemplate<class T, class U> void operator-=(pair<T, U> &x, pair<T, U> &y) { x = x - y; }\n\ntemplate<class T> size_t HashCombine(const size_t seed,const T &v){ return seed^(std::hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2)); }\ntemplate<class T,class S> struct std::hash<std::pair<T,S>>{ size_t operator()(const std::pair<T,S> &keyval) const noexcept { return HashCombine(std::hash<T>()(keyval.first), keyval.second); } };\ntemplate<class T> struct std::hash<std::vector<T>>{ size_t operator()(const std::vector<T> &keyval) const noexcept { size_t s=0; for (auto&& v: keyval) s=HashCombine(s,v); return s; } };\ntemplate<int N> struct HashTupleCore{ template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ size_t s=HashTupleCore<N-1>()(keyval); return HashCombine(s,std::get<N-1>(keyval)); } };\ntemplate <> struct HashTupleCore<0>{ template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ return 0; } };\ntemplate<class... Args> struct std::hash<std::tuple<Args...>>{ size_t operator()(const tuple<Args...> &keyval) const noexcept { return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval); } };\n\nusing lll = __int128_t;\n\nint main()\n{\n std::cin.tie(nullptr), std::ios_base::sync_with_stdio(false);\n while(true)\n {\n readll(N);\n if(!N) break;\n readstr(S);\n ll M = S.size();\n vll f(M, -1);\n stack<ll> st;\n rep(i, M)\n {\n if(S[i] == '(')\n {\n st.push(i);\n }\n else if(S[i] == ')')\n {\n f[st.top()] = i;\n st.pop();\n }\n }\n ll ans = 0;\n // outf(f);\n function<string(ll, ll)> dfs = [&](ll l, ll r)\n {\n // outf(l, r);\n vstr V;\n ll i = l;\n while(i < r)\n {\n if(S[i] == '(')\n {\n string ns = dfs(i + 1, f[i]);\n // outf(ns);\n i = f[i] + 1;\n V.emplace_back(ns); \n }\n else\n {\n V.push_back(S.substr(i, 1));\n i++;\n }\n }\n lll sum = 0;\n ll li = 0, ri = 0;\n ll n = V.size();\n char op = '*';\n deque<pair<lll, queue<lll>>> Q;\n bool ninf = false;\n // outf(V);\n bool alln = false;\n ll rcnt1 = 0;\n while(li < n)\n {\n // outf(li, ri);\n chmax(ri, li);\n if(ri >= n) break;\n while(sum < N && ri < n)\n {\n if(V[ri] == \"+\")\n {\n op = '+';\n ri++;\n rcnt1 = 0;\n }\n else if(V[ri] == \"*\")\n {\n op = '*';\n ri++;\n }\n else\n {\n if(V[ri] == \"inf\")\n {\n ninf = true;\n Q.clear();\n sum = 0;\n li = ri + 2;\n ri = li;\n rcnt1 = 0;\n break;\n }\n else\n {\n lll ns = stoll(V[ri]);\n // outf(\"ns=\", ns);\n if(op == '+')\n {\n if(ns == 1) rcnt1++;\n else rcnt1 = 0;\n sum += ns;\n queue<lll> tmp;\n tmp.push(ns);\n Q.emplace_back(ns, tmp);\n ri++;\n }\n else\n {\n if(ns == 1) rcnt1++;\n else rcnt1 = 0;\n if(Q.empty())\n {\n sum += ns;\n queue<lll> tmp;\n tmp.push(ns);\n Q.emplace_back(ns, tmp);\n }\n else\n {\n sum -= Q.back().first;\n Q.back().first *= ns;\n Q.back().second.push(ns);\n sum += Q.back().first;\n }\n ri++;\n }\n }\n }\n }\n while(ri + 1 < n)\n {\n if(V[ri] == \"*\" && V[ri + 1] == \"1\")\n {\n op = '*';\n rcnt1++;\n if(Q.empty())\n {\n sum += 1;\n queue<lll> tmp;\n tmp.push(1);\n Q.emplace_back(1, tmp);\n }\n else\n {\n sum -= Q.back().first;\n Q.back().first *= 1;\n Q.back().second.push(1);\n sum += Q.back().first;\n }\n ri += 2;\n }\n else\n {\n break;\n }\n }\n // outf(li, ri, \"sum =\", sum, \"rcnt =\", rcnt1);\n if(li == 0 && ri == n && sum == N) alln = true;\n while(!Q.empty() && sum >= N)\n {\n if(sum >= N) ninf = true;\n if(sum == N)\n {\n if(N > 1) ans += rcnt1 + ((Q.back().first == 1) ? 0 : 1);\n else ans += rcnt1;\n }\n if(sum == 1 && Q.front().second.front() == 1) rcnt1--;\n sum -= Q.front().first;\n if(Q.front().second.size() == 1)\n {\n Q.pop_front();\n }\n else\n {\n // outf(Q.front().second.front());\n Q.front().first /= Q.front().second.front();\n Q.front().second.pop();\n sum += Q.front().first;\n }\n li += 2;\n }\n // outf(li, ri, ans);\n }\n string ret = to_string(sum);\n // if(!alln && !ninf && ret == \"0\") outf(li, ri, V);\n // outf(ans);\n if(alln) return to_string(N);\n else if(ninf ) return string{\"inf\"};\n else return ret;\n };\n string teststr = dfs(0, M);\n outf(ans);\n } \n}", "accuracy": 1, "time_ms": 890, "memory_kb": 811848, "score_of_the_acc": -1.2039, "final_rank": 18 }, { "submission_id": "aoj_1630_10623139", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll,ll>;\nusing bint = __int128_t;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define TT template<typename T>\nTT using vec = vector<T>;\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\ntemplate<class T> bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\ntemplate<class T> bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n__int128 parse(string &s) {\n __int128 ret = 0;\n for (size_t i = 0; i < s.length(); i++)\n if ('0' <= s[i] && s[i] <= '9')\n ret = 10 * ret + s[i] - '0';\n return ret;\n}\n\nstd::istream &operator>>(std::istream &dest, __int128_t &value) {\n string s;\n dest >> s;\n value = parse(s);\n return dest;\n}\n\nll N;\n\ntuple<map<ll,ll>,map<ll,ll>,ll> buildv(const vector<ll> &v){\n ll sm=1;\n for(ll c:v) sm=min(N+1,sm*c);\n map<ll,ll> st,ed;\n ll tmp=1;\n int m=v.size();\n rep(i,0,m){\n tmp*=v[i];\n if(tmp>N) break;\n ed[tmp]++;\n }\n tmp=1;\n rep(i,0,m){\n tmp*=v[m-1-i];\n if(tmp>N) break;\n st[tmp]++;\n }\n return {st,ed,sm};\n}\nusing state = string::iterator;\nll E(state& itr);\nvector<ll> T(state &itr);\nll F(state &itr);\n\nll F(state &itr){\n if(*itr=='('){\n itr++;\n ll res=E(itr);\n assert(*itr==')');\n itr++;\n return res;\n }else{\n return *(itr++)-'0';\n }\n}\nll ans=0;\nvector<ll> T(state &itr){\n vector<ll> Fs;\n Fs.push_back(F(itr));\n map<ll,ll> st;\n st[Fs[0]]++;\n if(st.count(N)) ans+=st[N];\n while(*itr=='*'){\n itr++;\n ll res=F(itr);\n if(res!=1){\n map<ll,ll> nst;\n for(auto[x,ad]:st){\n if(x*res>N) break;\n nst[x*res]+=ad;\n }\n swap(nst,st);\n }\n if(res<=N) st[res]++;\n if(st.count(N)) ans+=st[N];\n Fs.push_back(res);\n }\n return Fs;\n}\nll E(state &itr){\n auto v=T(itr);\n auto[start,end,sum]=buildv(v);\n ll tmp=0;\n while(*itr=='+'){\n itr++;\n auto[st,ed,sm]=buildv(T(itr));\n for(auto[x,ad]:ed){\n if(start.count(N-x-tmp)) ans+=start[N-x-tmp]*ad;\n }\n tmp+=sm;\n for(auto[x,ad]:st){\n start[x-tmp]+=ad;\n }\n };\n return min(N+1,sum+tmp);\n}\n\nint solve(){\n cin>>N;\n if(N==0) return 0;\n string s;\n cin>>s;\n state itr=s.begin();\n ans=0;\n E(itr);\n cout<<ans<<\"\\n\";\n return 1;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 70000, "score_of_the_acc": -0.2099, "final_rank": 13 }, { "submission_id": "aoj_1630_9835421", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nll ANS;\nint it;\nll N;\nstring S;\n\nll Expr();\nll Factor();\n\nvoid BugSearch(string _) {\n //cout << _ << \": \" << it << endl;\n}\n\nll Expr() {\n BugSearch(\"Expr\");\n vector<ll> V;\n vector<int> Op;\n V.push_back(Factor());\n while(S[it] == '+' || S[it] == '*') {\n if (S[it] == '+') Op.push_back(0);\n else Op.push_back(1);\n it++;\n V.push_back(Factor());\n }\n int M = V.size();\n int L = 0, R = 0, ONES = 0;\n deque<ll> Q;\n ll Cur;\n while(L < M) {\n if (R <= L+1) {\n if (V[L] == 1) ONES = 1;\n else ONES = 0;\n Cur = V[L];\n Q.clear();\n Q.push_front(Cur);\n R = L+1;\n }\n while (Cur < N && R < M) {\n if (Op[R-1] == 0) {\n if (V[R] == 1) ONES = 1;\n else ONES = 0;\n Cur += V[R];\n Q.push_back(V[R]);\n R++;\n }\n else {\n if (V[R] == 1) ONES++;\n else ONES = 0;\n Cur += Q.back() * (V[R] - 1);\n Q.back() *= V[R];\n R++;\n }\n }\n if (Cur == N) {\n if (R > 0 && V[R-1] != 1) ONES = 0;\n if (R < M && V[R] == 1 && R > 0 && Op[R-1] == 1) {\n while(R < M && V[R] == 1 && R > 0 && Op[R-1] == 1) {\n ONES++;\n R++;\n }\n }\n int Left;\n if (Q.back() == 1) Left = R - (ONES - 1);\n else Left = R - ONES;\n ANS += R - max(L + 2, Left) + 1;\n }\n if (L >= M-1) {\n Cur = 0;\n Q.clear();\n }\n else if (Op[L] == 0) {\n Cur -= V[L];\n Q.pop_front();\n }\n else {\n Cur -= Q.front();\n Cur += Q.front() / V[L];\n Q.front() /= V[L];\n }\n L++;\n chmin(ONES, R-L);\n }\n vector<ll> Term;\n Term.push_back(V[0]);\n rep(i,1,M) {\n if (Op[i-1] == 0) {\n Term.push_back(V[i]);\n }\n else {\n Term.back() *= V[i];\n chmin(Term.back(), (ll)inf);\n }\n }\n ll Ret = 0;\n rep(i,0,Term.size()) {\n Ret += Term[i];\n chmin(Ret, (ll)inf);\n }\n return Ret;\n}\n\nll Factor() {\n BugSearch(\"Factor\");\n if (S[it] == '(') {\n it++;\n ll Ret = Expr();\n it++;\n if (Ret == N) ANS++;\n return Ret;\n }\n else {\n ll Ret = S[it] - '0';\n it++;\n if (Ret == N) ANS++;\n return Ret;\n }\n}\n\nint main() {\nwhile(1) {\n cin >> N;\n if (N == 0) return 0;\n cin >> S;\n S += '$';\n ANS = 0;\n it = 0;\n Expr();\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 39632, "score_of_the_acc": -0.0204, "final_rank": 4 }, { "submission_id": "aoj_1630_9622932", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (upper_bound(ALL(A),x)-A.begin())\nstruct segtree{\n ll n;\n vector<ll>tree;\n segtree(ll N){\n n=1;\n while(n<N)n*=2;\n tree.assign(2*n,1);\n }\n void set(ll i,ll x){\n i+=n;\n tree[i]=x;\n while(i){\n tree[i/2]=min(tree[i]*tree[i^1],1000000100LL);\n i/=2;\n }\n }\n ll prod(ll l,ll r){\n assert(0<=l&&l<=r&&r<=n);\n l+=n;r+=n;\n ll res=1;\n while(l<r){\n if(l%2)res*=tree[l++];\n if(res>1000000000LL)break;\n if(r%2)res*=tree[--r];\n if(res>1000000000LL)break;\n l/=2;r/=2;\n }\n return res;\n }\n};\nint main(){\n while(1){\n ll x;string S;cin>>x>>S;\n ll N=S.size();\n if(!x)return 0;\n vector<int>par(N);\n stack<int>st;\n REP(i,N){\n if(S[i]=='(')st.emplace(i);\n if(S[i]==')')par[st.top()]=i,st.pop();\n }\n ll ans=0;\n function<ll(ll,ll)>dfs=[&](ll l,ll r){\n vector<ll>V;\n string op;\n FOR(i,l,r){\n if(S[i]=='(')V.emplace_back(dfs(i+1,par[i])),i=par[i]+1;\n else V.emplace_back(S[i]-'0'),i++;\n if(i==r)break;\n op+=S[i];\n }\n vector<vector<ll>>A;\n vector<ll>P;\n REP(i,V.size()){\n vector<ll>a={V[i]};\n ll pro=V[i];\n while(i<op.size()&&op[i]=='*')a.emplace_back(V[++i]),pro=min(x+10,pro*V[i]);\n A.emplace_back(a);\n P.emplace_back(pro);\n }\n vector<vector<ll>>S=A;\n vector<ll>B(P.size()+1);\n REP(i,P.size())B[i+1]=B[i]+P[i];\n REP(i,P.size())FOR(j,1,A[i].size())S[i][j]=min(x+10,S[i][j]*S[i][j-1]);\n REP(i,P.size()){\n if(B.back()-B[i]>=x){\n vector<ll>T=A[i];\n RREP(j,T.size()-1)T[j]=min(x+10,T[j+1]*T[j]);\n REP(j,T.size())if(T[j]+B.back()-B[i+1]>=x&&T[j]<x){\n ll t=LB(B,-T[j]+B[i+1]+x);\n ll val=B[t-1]-B[i+1]+T[j];\n ans+=UB(S[t-1],x-val)-LB(S[t-1],x-val);\n }\n }\n segtree seg(A[i].size());\n REP(j,A[i].size())seg.set(j,A[i][j]);\n REP(j,A[i].size()){\n ll l=j,r=A[i].size();\n while(r-l>1){\n ll m=(l+r)/2;\n if(seg.prod(j,m)<x)l=m;\n else r=m;\n }\n if(seg.prod(j,r)!=x)continue;\n ll p=l;\n r=A[i].size()+1;\n while(r-l>1){\n ll m=(l+r)/2;\n if(seg.prod(j,m)<=x)l=m;\n else r=m;\n }\n ans+=l-p;\n }\n }\n //cout<<l<<\" \"<<r<<\"->\"<<ans<<endl;\n return min(x+10,B.back());\n };\n dfs(0,N);\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 151336, "score_of_the_acc": -0.2537, "final_rank": 14 }, { "submission_id": "aoj_1630_9409614", "code_snippet": "#line 1 \"A.cpp\"\n#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ni64 solve(int X) {\n string s = in();\n s = \"(\" + s + \")\";\n\n i64 ans = 0;\n int i = 0;\n const i64 INF = 1e9 + 1;\n auto dfs = [&](auto self) -> i64 {\n assert(s[i] == '('); i++;\n vector<i64> num;\n vector<char> op = {'+'};\n while(s[i] != ')') {\n if(s[i] == '(') {\n num.push_back(self(self));\n } else if(std::isdigit(s[i])) {\n num.push_back(s[i] - '0'); i++;\n } else if(s[i] == '+' or s[i] == '*') {\n op.push_back(s[i]); i++;\n } else {\n assert(0);\n }\n }\n assert(s[i] == ')'); i++;\n\n // count\n const int m = op.size();\n op.push_back('+');\n\n if(X == 1) {\n vector<i64> vec = {0};\n for(int k : rep(m)) {\n if(num[k] == 1) {\n if(op[k] == '+') {\n vec.push_back(1);\n } else {\n vec.back() += 1;\n }\n } else {\n vec.push_back(0);\n }\n }\n for(i64 x : vec) ans += (x + 1) * x / 2;\n } else {\n vector<i64> one_cnt(m); {\n i64 cnt = 0;\n for(int k : rep(m)) {\n if(num[k] == 1 and op[k] == '*') {\n cnt++;\n } else {\n cnt = 0;\n }\n one_cnt[k] = cnt;\n }\n }\n\n deque<i64> dq;\n i64 value = 0;\n for(int L = 0, R = 0; L < m; L++) {\n while(R < m) {\n if(dq.empty()) {\n dq.push_back(num[R]);\n value += num[R];\n R++;\n } else {\n if(op[R] == '+') {\n if(value + num[R] <= X) {\n dq.push_back(num[R]);\n value += num[R];\n R++;\n } else break;\n } else {\n if(value - dq.back() + dq.back() * num[R] <= X) {\n value = value - dq.back() + dq.back() * num[R];\n dq.back() *= num[R];\n R++;\n } else break;\n }\n }\n }\n\n if(value == X) {\n ans += one_cnt[R - 1] + 1;\n }\n \n if(L + 1 == R) {\n value = 0;\n dq.pop_front();\n } else {\n if(op[L + 1] == '+') {\n value -= dq.front();\n dq.pop_front();\n } else {\n value = value - dq.front() + dq.front() / num[L];\n dq.front() /= num[L];\n }\n }\n }\n }\n\n // eval\n vector<i64> ts = {num[0]};\n for(int k : rep(1, m)) {\n if(op[k] == '+') {\n ts.push_back(num[k]);\n } else {\n ts.back() *= num[k];\n chmin(ts.back(), INF);\n }\n }\n i64 sum = 0;\n for(i64 t : ts) sum += t;\n return min(sum, INF);\n };\n dfs(dfs);\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 33828, "score_of_the_acc": -0.0008, "final_rank": 1 }, { "submission_id": "aoj_1630_9409612", "code_snippet": "#line 1 \"A.cpp\"\n#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ni64 solve(int X) {\n string s = in();\n s = \"(\" + s + \")\";\n\n i64 ans = 0;\n int i = 0;\n const i64 INF = 1e9 + 1;\n auto dfs = [&](auto self) -> i64 {\n assert(s[i] == '('); i++;\n vector<i64> num;\n vector<char> op = {'+'};\n while(s[i] != ')') {\n if(s[i] == '(') {\n num.push_back(self(self));\n } else if(std::isdigit(s[i])) {\n num.push_back(s[i] - '0'); i++;\n } else if(s[i] == '+' or s[i] == '*') {\n op.push_back(s[i]); i++;\n } else {\n assert(0);\n }\n }\n assert(s[i] == ')'); i++;\n\n // count\n const int m = op.size();\n op.push_back('+');\n\n if(X == 1) {\n vector<i64> vec = {0};\n for(int k : rep(m)) {\n if(num[k] == 1) {\n if(op[k] == '+') {\n vec.push_back(1);\n } else {\n vec.back() += 1;\n }\n } else {\n vec.push_back(0);\n }\n }\n for(i64 x : vec) ans += (x + 1) * x / 2;\n } else {\n vector<i64> one_cnt(m); {\n i64 cnt = 0;\n for(int k : rep(m)) {\n if(num[k] == 1 and op[k] == '*') {\n cnt++;\n } else {\n cnt = 0;\n }\n one_cnt[k] = cnt;\n }\n }\n\n deque<i64> dq;\n i64 value = 0;\n for(int L = 0, R = 0; L < m; L++) {\n while(R < m) {\n if(dq.empty()) {\n dq.push_back(num[R]);\n value += num[R];\n R++;\n } else {\n if(op[R] == '+') {\n if(value + num[R] <= X) {\n dq.push_back(num[R]);\n value += num[R];\n R++;\n } else break;\n } else {\n if(value - dq.back() + dq.back() * num[R] <= X) {\n value = value - dq.back() + dq.back() * num[R];\n dq.back() *= num[R];\n R++;\n } else break;\n }\n }\n }\n\n if(value == X) {\n ans += one_cnt[R - 1] + 1;\n }\n \n if(L + 1 == R) {\n value = 0;\n dq.pop_front();\n } else {\n if(op[L + 1] == '+') {\n value -= dq.front();\n dq.pop_front();\n } else {\n value = value - dq.front() + dq.front() / num[L];\n dq.front() /= num[L];\n }\n }\n }\n }\n\n // eval\n vector<i64> ts = {num[0]};\n for(int k : rep(1, m)) {\n if(op[k] == '+') {\n ts.push_back(num[k]);\n } else {\n ts.back() *= num[k];\n chmin(ts.back(), INF);\n }\n }\n i64 sum = 0;\n for(i64 t : ts) sum += t;\n return min(sum, INF);\n };\n dfs(dfs);\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 33840, "score_of_the_acc": -0.0008, "final_rank": 2 }, { "submission_id": "aoj_1630_9374325", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int LG_LENGTH = 22;\nconstexpr int MAX_DEPTH = 1001;\nconstexpr int INF = 1LL << 30;\n\ntemplate <auto op, auto e> struct dynamic_doubling {\n int sz;\n long long all_prod;\n vector<vector<int>> dest;\n vector<vector<int>> cost;\n dynamic_doubling() {\n sz = 0;\n all_prod = e();\n dest.push_back(vector<int>(LG_LENGTH, 0));\n cost.push_back(vector<int>(LG_LENGTH, INF));\n }\n void push_back(int v) {\n all_prod = op(all_prod, v);\n vector<int> dest_cur(LG_LENGTH, 0);\n vector<int> cost_cur(LG_LENGTH, INF);\n dest_cur[0] = sz;\n cost_cur[0] = v;\n for(int i = 0; i + 1 < LG_LENGTH; i++) {\n dest_cur[i + 1] = dest[dest_cur[i]][i];\n cost_cur[i + 1] = op(cost[dest_cur[i]][i], cost_cur[i]);\n }\n sz++;\n dest.push_back(dest_cur);\n cost.push_back(cost_cur);\n }\n pair<int, int> min_left(int v) {\n if(v <= e()) {\n return make_pair(sz, 0);\n }\n int l = sz;\n int cost_cur = e();\n for(int i = LG_LENGTH - 1; i >= 0; i--) {\n if(op(cost_cur, cost[l][i]) < v) {\n cost_cur = op(cost_cur, cost[l][i]);\n l = dest[l][i];\n }\n }\n return make_pair(l, cost_cur);\n }\n};\n\nint add_e() {\n return 0;\n}\nint add(int x, int y) {\n if(x <= INF - y) return x + y;\n return INF;\n}\n\nint mul_e() {\n return 1;\n}\nint mul(int x, int y) {\n if(x <= INF / y) return x * y;\n return INF;\n}\n\nbool solve() {\n int n;\n cin >> n;\n if(n == 0) return false;\n string s;\n cin >> s;\n long long ans = 0;\n vector<dynamic_doubling<add, add_e>> dbl_add;\n vector<vector<dynamic_doubling<mul, mul_e>>> dbl_mul;\n dbl_add.push_back(dynamic_doubling<add, add_e>());\n dbl_mul.push_back(vector<dynamic_doubling<mul, mul_e>>{dynamic_doubling<mul, mul_e>()});\n for(int i = 0, d = 0; i < int(s.size()); i++) {\n if(isdigit(s[i])) {\n dbl_mul[d].back().push_back(s[i] - '0');\n }else if(s[i] == '(') {\n d++;\n dbl_add.push_back(dynamic_doubling<add, add_e>());\n dbl_mul.push_back(vector<dynamic_doubling<mul, mul_e>>());\n dbl_mul[d].push_back(dynamic_doubling<mul, mul_e>());\n continue;\n }else if(s[i] == ')') {\n const long long eval_all = add(dbl_add[d].all_prod, dbl_mul[d].back().all_prod);\n d--;\n dbl_mul[d].back().push_back(eval_all);\n dbl_add.pop_back();\n dbl_mul.pop_back();\n }else if(s[i] == '+') {\n if(dbl_mul[d].back().sz > 0) {\n dbl_add[d].push_back(dbl_mul[d].back().all_prod);\n dbl_mul[d].push_back(dynamic_doubling<mul, mul_e>());\n }\n continue;\n }else if(s[i] == '*') {\n continue;\n }\n const int add_ap = dbl_add[d].all_prod;\n const int mul_ap = dbl_mul[d].back().all_prod;\n // cout << add_ap << \" \" << mul_ap << endl;\n if(add(add_ap, mul_ap) < n) {\n // do nothing\n }else if(mul_ap >= n) {\n auto [lm1, cm1] = dbl_mul[d].back().min_left(n + 1);\n auto [lm2, cm2] = dbl_mul[d].back().min_left(n);\n if(cm1 == n) ans += lm2 - lm1;\n }else {\n auto [la1, ca1] = dbl_add[d].min_left(n - mul_ap);\n auto [lm1, cm1] = dbl_mul[d][la1 - 1].min_left(n + 1 - ca1 - mul_ap);\n auto [lm2, cm2] = dbl_mul[d][la1 - 1].min_left(n - ca1 - mul_ap);\n // cout << n + 1 - ca1 - mul_ap << endl;\n // cout << la1 << \" \" << lm1 << \" \" << lm2 << endl;\n // cout << ca1 << \" \" << cm1 << \" \" << mul_ap << endl;\n if(add(cm1, add(ca1, mul_ap)) == n) ans += lm2 - lm1;\n }\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while(solve()) {}\n}", "accuracy": 1, "time_ms": 4170, "memory_kb": 805072, "score_of_the_acc": -1.9913, "final_rank": 19 }, { "submission_id": "aoj_1630_9374160", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define all(A) A.begin(),A.end()\nll eval(vll D) {\n ll res = 0;\n ll N = D.size();\n ll pr = 1;\n rep(i, N) {\n if (D[i] >= 0) {\n pr *= D[i];\n if (pr > 1e9)pr = 1e9 + 1;\n }\n else if (D[i] == -1) {\n res += pr;\n if (res > 1e9)res = 1e9 + 1;\n pr = 1;\n }\n }\n res += pr;\n return min(res, ll(1e9 + 1));\n}\n\n\n\nvector<pair<ll, ll>> compress(vll D) {\n ll N = D.size();\n vector<pair<ll, ll>> ND(N);\n rep(i, N) {\n ND[i].first = D[i];\n ND[i].second = 1;\n }\n reverse(all(ND));\n ll cnt = 0;\n for (ll i = 0; i < N; i += 2) {\n if (i==N-1||ND[i].first != 1 || (i != N - 1 && ND[i + 1].first == -1)) {\n ND[i].second += cnt;\n cnt = 0;\n }\n else cnt++;\n }\n reverse(all(ND));\n return ND;\n}\n\nll calc(vll D, ll T) {\n ll N = D.size();\n bool is_all_oneprod = 1;\n rep(i, N) {\n if (i % 2 == 0 && D[i] != 1)is_all_oneprod = 0;\n if (i % 2 == 1 && D[i] != -2)is_all_oneprod = 0;\n }\n if (is_all_oneprod) {\n if (T == 1) {\n return (N / 2 + 2) * (N / 2 + 1) / 2;\n }\n else return 0;\n }\n auto ND = compress(D);\n N = ND.size();\n deque<ll> P;\n deque<ll> index;\n ll res = 0;\n ll cnt = 0;\n ll R = 0;\n for (ll i = 0; i < N; i += 2) {\n while (res < T && R < N) {\n index.push_back(R);\n if (P.size() == 0 || ND[R - 1].first == -1) {\n P.push_back(ND[R].first);\n res += ND[R].first;\n }\n else {\n res -= P.back();\n P.back() *= ND[R].first;\n res += P.back();\n }\n R += 2;\n }\n if (res == T) {\n ll rid = index.back();\n cnt += ND[rid].second;\n }\n if (index.size() == 0)break;\n ll r = index.front();\n index.pop_front();\n if (index.size()==0||(r != N - 1 && ND[r + 1].first == -1)) {\n P.pop_front();\n res -= ND[r].first;\n }\n else {\n res -= P.front();\n //assert(0 <= r && r < N);\n P.front() /= ND[r].first;\n\n res += P.front();\n }\n }\n return cnt;\n}\n\nvoid solve(ll N) {\n string S;\n cin >> S;\n ll SN = S.size();\n vector<vll> D(SN + 1);\n //-1:+, -2:*\n\n ll an = 0;\n ll cnt = 0;\n rep(i, SN) {\n if (S[i] == '(')cnt++;\n else if (S[i] == ')') {\n an += calc(D[cnt], N);\n ll res = eval(D[cnt]);\n D[cnt] = {};\n D[cnt - 1].push_back(res);\n cnt--;\n }\n else if (S[i] == '+') {\n D[cnt].push_back(-1);\n }\n else if (S[i] == '*') {\n D[cnt].push_back(-2);\n }\n else {\n D[cnt].push_back(S[i] - '0');\n }\n }\n an += calc(D[0], N);\n cout << an << endl;\n}\n\nint main() {\n\n ll N;\n ll couutn = 0;\n while (cin >> N) {\n if (N == 0)return 0;\n couutn++;\n // if (couutn == 40)cout << \"#\" << N << endl;;\n solve(N);\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 150216, "score_of_the_acc": -0.1746, "final_rank": 12 }, { "submission_id": "aoj_1630_9374142", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define all(A) A.begin(),A.end()\nll eval(vll D) {\n ll res = 0;\n ll N = D.size();\n ll pr = 1;\n rep(i, N) {\n if (D[i] >= 0) {\n pr *= D[i];\n if (pr > 1e9)pr = 1e9+1;\n }\n else if (D[i] == -1) {\n res += pr;\n if (res > 1e9)res = 1e9+1;\n pr = 1;\n }\n }\n res += pr;\n return min(res, ll(1e9+1));\n}\n\n\n\nvector<pair<ll, ll>> compress(vll D) {\n ll N = D.size();\n vector<pair<ll, ll>> ND(N);\n rep(i, N) {\n ND[i].first = D[i];\n ND[i].second = 1;\n }\n reverse(all(ND));\n ll cnt = 0;\n for (ll i = 0; i < N; i += 2) {\n if (ND[i].first != 1 || (i != N - 1 && ND[i + 1].first == -1)) {\n ND[i].second += cnt;\n cnt = 0;\n }\n else cnt++;\n }\n reverse(all(ND));\n return ND;\n}\n\nll calc(vll D, ll T) {\n ll N = D.size();\n bool is_all_oneprod = 1;\n rep(i, N) {\n if (i % 2 == 0 && D[i] != 1)is_all_oneprod = 0;\n if (i % 2 == 1 && D[i] != -2)is_all_oneprod = 0;\n }\n if (is_all_oneprod) {\n if (T == 1) {\n return (N / 2 + 2) * (N / 2 + 1) / 2;\n }\n else return 0;\n }\n auto ND = compress(D);\n N = ND.size();\n deque<ll> P;\n deque<ll> index;\n ll res = 0;\n ll cnt = 0;\n ll R = 0;\n for (ll i = 0; i < N; i += 2) {\n while (res < T && R < N) {\n index.push_back(R);\n if (P.size() == 0 || ND[R - 1].first == -1) {\n P.push_back(ND[R].first);\n res += ND[R].first;\n }\n else {\n res -= P.back();\n P.back() *= ND[R].first;\n res += P.back();\n }\n R += 2;\n }\n if (res == T) {\n ll rid = index.back();\n cnt += ND[rid].second;\n }\n if(index.size()==0)break;\n ll r = index.front();\n index.pop_front();\n if (r != N - 1 && ND[r + 1].first == -1) {\n P.pop_front();\n res -= ND[r].first;\n }\n else {\n res -= P.front();\n assert(0<=r&&r<N);\n P.front() /= ND[r].first;\n \n res += P.front();\n }\n }\n return cnt;\n}\n\nvoid solve(ll N) {\n string S;\n cin >> S;\n ll SN = S.size();\n vector<vll> D(SN + 1);\n //-1:+, -2:*\n\n ll an = 0;\n ll cnt = 0;\n rep(i, SN) {\n if (S[i] == '(')cnt++;\n else if (S[i] == ')') {\n an += calc(D[cnt], N);\n ll res = eval(D[cnt]);\n D[cnt] = {};\n D[cnt - 1].push_back(res);\n cnt--;\n }\n else if (S[i] == '+') {\n D[cnt].push_back(-1);\n }\n else if (S[i] == '*') {\n D[cnt].push_back(-2);\n }\n else {\n D[cnt].push_back(S[i] - '0');\n }\n }\n an += calc(D[0], N);\n cout << an << endl;\n}\n\nint main() {\n\n ll N;\n while (cin >> N) {\n if (N == 0)return 0;\n solve(N);\n }\n}", "accuracy": 0.5, "time_ms": 160, "memory_kb": 150564, "score_of_the_acc": -0.1774, "final_rank": 20 }, { "submission_id": "aoj_1630_9323223", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cassert>\nusing i64 = long long;\n\ni64 ans = 0;\n\ni64 rec(int &i, i64 N, std::string S)\n{\n std::vector<std::vector<std::pair<i64, i64>>> v;\n v.push_back({});\n while (i < S.size())\n {\n if (S[i] == '(')\n {\n i += 1;\n i64 value = rec(i, N, S);\n assert(S[i] == ')');\n i += 1;\n if (value == 1 && v.back().size() > 0 && v.back().back().first == 1)\n {\n v.back().back().second += 1;\n }\n else\n {\n v.back().push_back({value, 1});\n }\n }\n else if (S[i] == ')')\n {\n break;\n }\n else if (S[i] == '+')\n {\n v.push_back({});\n i += 1;\n }\n else if (S[i] == '*')\n {\n i += 1;\n }\n else\n {\n int value = S[i] - '0';\n if (value == 1 && v.back().size() > 0 && v.back().back().first == 1)\n {\n v.back().back().second += 1;\n }\n else\n {\n v.back().push_back({value, 1});\n }\n i += 1;\n }\n }\n\n std::vector<i64> pi(v.size(), 0);\n i64 sum = 0;\n i64 ret = 0;\n std::vector<std::pair<int, int>> left;\n int li = 0;\n for (int x = 0; x < v.size(); x++)\n {\n i64 ret_pi = 1;\n for (int y = 0; y < v[x].size(); y++)\n {\n left.push_back({x, y});\n ret_pi *= v[x][y].first;\n ret_pi = std::min(N + 1, ret_pi);\n if (y == 0)\n {\n pi[x] += v[x][y].first;\n sum += pi[x];\n }\n else\n {\n sum -= pi[x];\n pi[x] *= v[x][y].first;\n sum += pi[x];\n }\n\n while (sum > N && li < left.size())\n {\n auto [i, j] = left[li];\n\n if (j + 1 == v[i].size())\n {\n sum -= pi[i];\n pi[i] = 0;\n }\n else\n {\n sum -= pi[i];\n pi[i] /= v[i][j].first;\n sum += pi[i];\n }\n li++;\n }\n if (sum == N && li < left.size())\n {\n if (li + 1 == left.size())\n {\n ans += v[x][y].second * (v[x][y].second + 1) / 2;\n }\n else\n {\n auto [i, j] = left[li];\n int add = 0;\n if (v[i][j].first == 1 && j + 1 < v[i].size())\n {\n add += 1;\n }\n ans += (add + v[i][j].second) * v[x][y].second;\n }\n }\n }\n ret = std::min(N + 1, ret + ret_pi);\n }\n return ret;\n}\nbool solve()\n{\n i64 N;\n std::cin >> N;\n if (N == 0)\n return false;\n std::string S;\n std::cin >> S;\n ans = 0;\n int i = 0;\n rec(i, N, S);\n std::cout << ans << std::endl;\n return true;\n}\n\nint main()\n{\n while (solve())\n ;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 90808, "score_of_the_acc": -0.1589, "final_rank": 11 }, { "submission_id": "aoj_1630_8534627", "code_snippet": "#include <bits/stdc++.h>\n// 03:05\nconstexpr long long INF = 1e18;\nconstexpr long long MAX = 1e9;\n\nstruct Data {\n std::vector<long long> digits;\n long long mul;\n};\n\nstruct Parser {\n using CopyIter = std::string::const_iterator;\n using Iter = CopyIter&;\n std::string line;\n std::vector<std::vector<Data>> ans;\n\n Parser(const std::string& s): line(s) {}\n\n void skip(Iter it, char c) {\n assert(*it == c);\n ++it;\n }\n\n std::vector<std::vector<Data>> parse() {\n auto it = line.cbegin();\n expr(it);\n return ans;\n }\n\n long long expr(Iter it) {\n std::vector<Data> data;\n long long sum = 0;\n data.push_back(term(it));\n sum += data.back().mul;\n if (sum > MAX) {\n sum = INF;\n }\n while (it != line.end() && *it == '+') {\n skip(it, '+');\n data.push_back(term(it));\n sum += data.back().mul;\n if (sum > MAX) {\n sum = INF;\n }\n }\n ans.push_back(data);\n return sum;\n }\n\n Data term(Iter it) {\n Data data;\n data.digits.push_back(factor(it));\n while (it != line.end() && *it == '*') {\n skip(it, '*');\n data.digits.push_back(factor(it));\n }\n data.mul = 1;\n for (auto val: data.digits) {\n if (val == INF) {\n data.mul = INF;\n break;\n }\n data.mul *= val;\n if (data.mul > MAX) {\n data.mul = INF;\n break;\n }\n }\n return data;\n }\n\n long long factor(Iter it) {\n if (*it == '(') {\n skip(it, '(');\n auto res = expr(it);\n skip(it, ')');\n return res;\n } else {\n return number(it);\n }\n }\n\n long long number(Iter it) {\n char c = *it;\n assert(std::isdigit(c));\n skip(it, c);\n return c - '0';\n }\n};\n\nlong long solve(long long n, const Data& data) {\n int r = 0;\n long long mul = 1;\n long long res = 0;\n std::vector<int> one(data.digits.size() + 1);\n for (int i = 0, now = 0; i <= (int)data.digits.size(); i++) {\n if (i < (int)data.digits.size() && data.digits[i] == 1) {\n now++;\n } else {\n one[i] = now;\n now = 0;\n }\n }\n for (int l = 0; l < (int)data.digits.size(); l++) {\n if (r < l) r = l;\n while (r < (int)data.digits.size()) {\n if (data.digits[r] == INF) break;\n if (mul * data.digits[r] > n) break;\n mul *= data.digits[r];\n r++;\n }\n assert(mul <= n);\n if (mul == n) {\n if (n != 1) {\n res += one[r] + 1;\n } else {\n res += r - l;\n }\n }\n if (l < r) {\n assert(mul % data.digits[l] == 0);\n mul /= data.digits[l];\n }\n }\n return res;\n}\n\nlong long solve(long long n, const std::vector<Data>& data) {\n long long ans = 0;\n for (const auto& d: data) {\n ans += solve(n, d);\n }\n\n std::vector<std::map<long long, int>> left(data.size()), right(data.size());\n for (int i = 0; i < (int)data.size(); i++) {\n const auto& d = data[i];\n {\n long long mul = 1;\n for (int j = 0; j < (int)d.digits.size(); j++) {\n if (d.digits[j] == INF) break;\n mul *= d.digits[j];\n if (mul > MAX) break;\n left[i][mul]++;\n }\n }\n {\n long long mul = 1;\n for (int j = (int)d.digits.size() - 1; j >= 0; j--) {\n if (d.digits[j] == INF) break;\n mul *= d.digits[j];\n if (mul > MAX) break;\n right[i][-mul]++;\n }\n }\n }\n\n int r = 0;\n long long sum = 0;\n for (int l = 0; l < (int)data.size(); l++) {\n if (r <= l) r = l + 1;\n for (const auto& [v, c]: right[l]) {\n while (r < (int)data.size()) {\n if (sum + -v + data[r].mul > n) break;\n sum += data[r].mul;\n r++;\n }\n //std::cerr << -v << ' ' << n << ' ' << sum << std::endl;\n assert(-v > n || sum + -v <= n);\n if (sum != 0 && -v + sum == n) {\n ans += c * left[r - 1][data[r - 1].mul];\n } else if (r < (int)data.size()) {\n long long diff = n - sum - -v;\n ans += c * left[r][diff];\n }\n }\n if (l + 1 < r) {\n sum -= data[l + 1].mul;\n }\n }\n return ans;\n}\n\nint main() {\n while (true) {\n int n;\n std::cin >> n;\n if (n == 0) break;\n std::string line;\n std::cin >> line;\n Parser parser(line);\n // std::cerr << \"start parse\" << std::endl;\n auto data = parser.parse();\n\n // for (const auto& d: data) {\n // std::cerr << \" ----------- \" << std::endl;\n // for (const auto& dd: d) {\n // std::cerr << dd.mul << std::endl;\n // }\n // }\n\n // std::cerr << \"end parse\" << std::endl;\n long long ans = 0;\n for (const auto& d: data) {\n ans += solve(n, d);\n }\n std::cout << ans << std::endl;\n }\n}", "accuracy": 1, "time_ms": 1260, "memory_kb": 414728, "score_of_the_acc": -0.7837, "final_rank": 17 }, { "submission_id": "aoj_1630_8294622", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tstring S;\n\t\tcin >> S;\n\t\tS = \"(\" + S + \")\";\n\t\tvector<int> num;\n\t\tvector<char> op;\n\t\tvector<int> st;\n\t\tlong long answer = 0;\n\t\tfor (int i = 0; i < S.size(); i++) {\n\t\t\tif (S[i] == '(') {\n\t\t\t\tst.push_back(num.size());\n\t\t\t}\n\t\t\tif (S[i] == ')') {\n\t\t\t\tint cnt = 0;\n\t\t\t\tfor (int j = st.back(); j < op.size(); j++) {\n\t\t\t\t\tif (op[j] == '+') {\n\t\t\t\t\t\tcnt += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tvector<long long> seq1(cnt + 1, 1);\n\t\t\t\tvector<long long> seq2(cnt + 1, 1);\n\t\t\t\tseq1[0] = num[st.back()];\n\t\t\t\tseq2[0] = num[st.back()];\n\t\t\t\tlong long val1 = num[st.back()];\n\t\t\t\tlong long val2 = num[st.back()];\n\t\t\t\tint lpos1 = st.back(), rcur1 = 0, lcur1 = 0;\n\t\t\t\tint lpos2 = st.back(), rcur2 = 0, lcur2 = 0;\n\t\t\t\tint ND = max(N - 1, 1);\n\t\t\t\tfor (int j = st.back(); j <= op.size(); j++) {\n\t\t\t\t\twhile (val1 > N && lpos1 <= j) {\n\t\t\t\t\t\tval1 -= seq1[lcur1];\n\t\t\t\t\t\tseq1[lcur1] /= num[lpos1];\n\t\t\t\t\t\tval1 += seq1[lcur1];\n\t\t\t\t\t\tif (lpos1 == op.size() || op[lpos1] == '+') {\n\t\t\t\t\t\t\tlcur1 += 1;\n\t\t\t\t\t\t\tval1 -= 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlpos1 += 1;\n\t\t\t\t\t}\n\t\t\t\t\twhile (val2 > ND && lpos2 <= j) {\n\t\t\t\t\t\tval2 -= seq2[lcur2];\n\t\t\t\t\t\tseq2[lcur2] /= num[lpos2];\n\t\t\t\t\t\tval2 += seq2[lcur2];\n\t\t\t\t\t\tif (lpos2 == op.size() || op[lpos2] == '+') {\n\t\t\t\t\t\t\tlcur2 += 1;\n\t\t\t\t\t\t\tval2 -= 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlpos2 += 1;\n\t\t\t\t\t}\n\t\t\t\t\tanswer += j - lpos1 + 1;\n\t\t\t\t\tif (N >= 2) {\n\t\t\t\t\t\tanswer -= j - lpos2 + 1;\n\t\t\t\t\t}\n\t\t\t\t\tif (j != op.size()) {\n\t\t\t\t\t\tif (op[j] == '+') {\n\t\t\t\t\t\t\trcur1 += 1;\n\t\t\t\t\t\t\trcur2 += 1;\n\t\t\t\t\t\t\tval1 += 1;\n\t\t\t\t\t\t\tval2 += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tval1 -= seq1[rcur1];\n\t\t\t\t\t\tval2 -= seq2[rcur2];\n\t\t\t\t\t\tseq1[rcur1] *= num[j + 1];\n\t\t\t\t\t\tseq2[rcur2] *= num[j + 1];\n\t\t\t\t\t\tval1 += seq1[rcur1];\n\t\t\t\t\t\tval2 += seq2[rcur2];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnum.resize(st.back());\n\t\t\t\top.resize(st.back());\n\t\t\t\tif (lpos1 == st.back()) {\n\t\t\t\t\tnum.push_back(val1);\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tnum.push_back(N + 1);\n\t\t\t\t}\n\t\t\t\tst.pop_back();\n\t\t\t}\n\t\t\tif (S[i] == '+' || S[i] == '*') {\n\t\t\t\top.push_back(S[i]);\n\t\t\t}\n\t\t\tif ('1' <= S[i] && S[i] <= '9') {\n\t\t\t\tnum.push_back(int(S[i] - '0'));\n\t\t\t}\n\t\t}\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 33200, "score_of_the_acc": -0.0024, "final_rank": 3 }, { "submission_id": "aoj_1630_7917267", "code_snippet": "#include <string.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cfloat>\n#include <chrono>\n#include <ciso646>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define REP3(i, l, r, s) \\\n for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \\\n i += rep3_s)\n#define REP2(i, l, r) REP3(i, l, r, 1)\n#define REP1(i, n) REP2(i, 0, n)\n#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)\n#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)\n\n#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define RREP3(i, l, r, s) \\\n for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \\\n i -= rrep3_s)\n#define RREP2(i, l, r) RREP3(i, l, r, 1)\n#define RREP1(i, n) RREP2(i, 0, n)\n#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)\n#define rrepin(i, l, r) \\\n for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)\n\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace rklib {\n\n// a <- max(a, b)\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// a <- min(a, b)\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// if a < 0: a <- b\n// else: a <- min(a, b)\ntemplate <class T>\nbool chmin_non_negative(T &a, const T &b) {\n if (a < 0 || a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// floor(num / den)\ntemplate <class T>\nT div_floor(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num >= 0 ? num / den : (num + 1) / den - 1;\n}\n\n// ceil(num / den)\ntemplate <class T>\nT div_ceil(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num <= 0 ? num / den : (num - 1) / den + 1;\n}\n\nnamespace internal {\n\ntemplate <class T>\nT remainder_count(T r, T b, T m) {\n return r / m * b + std::min(b, r % m);\n}\n\n} // namespace internal\n\n// Number of integer x s.t.\n// - x in [l, r)\n// - x mod m in [a, b)\ntemplate <class T>\nT remainder_count(T l, T r, T a, T b, T m) {\n assert(l >= 0);\n assert(m >= 1);\n\n if (l >= r || a >= b) return 0;\n if (m <= a || b < 0) return 0;\n chmax(a, T(0));\n chmin(b, m);\n\n auto res = internal::remainder_count(r, b, m);\n if (l >= 1) res -= internal::remainder_count(l, b, m);\n if (a >= 1) res -= internal::remainder_count(r, a, m);\n if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);\n\n return res;\n}\n\n} // namespace rklib\n\n\nusing namespace std;\nusing namespace rklib;\n\nusing lint = long long;\nusing pii = pair<int, int>;\nusing pll = pair<lint, lint>;\n\nconstexpr lint INF = 1e9 + 7;\n\nint n;\nstring s;\nint idx;\nlint ans;\n\nlint expr();\npair<lint, vector<lint>> term();\nlint factor();\n\nvoid solve(vector<vector<lint>> &a) {\n vector<pair<lint, int>> ps;\n vector<int> bucket;\n rep(i, a.size()) {\n for (int l = 0; l < int(a[i].size());) {\n if (a[i][l] != 1) {\n ps.emplace_back(a[i][l], 1);\n bucket.push_back(i);\n ++l;\n continue;\n }\n int r = l;\n while (r < int(a[i].size()) && a[i][r] == 1) ++r;\n ps.emplace_back(1, r - l);\n bucket.push_back(i);\n l = r;\n }\n }\n // for (auto p : ps) printf(\"(%lld, %d) \", p.first, p.second);\n // puts(\"\");\n // cout << \"bucket: \";\n // for (auto x : bucket) printf(\"%d \", x);\n // puts(\"\");\n\n deque<tuple<lint, int, int>> dq; // (val, num, bucket)\n int r = 0;\n lint tmp = 0;\n vector<lint> prods(bucket.size(), 0);\n vector<int> cnt(bucket.size(), 0);\n rep(l, ps.size()) {\n // extend\n while (r < int(ps.size()) && tmp < n) {\n auto [val, num] = ps[r];\n auto b = bucket[r];\n if (cnt[b] == 0) {\n prods[b] = val;\n tmp += val;\n // chmin(tmp, INF);\n ++cnt[b];\n } else {\n tmp -= prods[b];\n prods[b] *= val;\n tmp += prods[b];\n // chmin(tmp, INF);\n ++cnt[b];\n }\n dq.emplace_back(val, num, b);\n ++r;\n }\n if (!dq.empty() && r < int(ps.size()) && ps[r].first == 1 &&\n get<2>(dq.back()) == bucket[r]) {\n dq.emplace_back(1, ps[r].second, bucket[r]);\n ++cnt[bucket[r]];\n ++r;\n }\n\n if (tmp == n) {\n if (dq.size() == 1) {\n auto [val, num, b] = dq.front();\n // cout << val << \" \" << num << \" \" << b << endl;\n if (val == 1) {\n ans += lint(num) * (num + 1) / 2;\n } else {\n ++ans;\n }\n } else {\n auto [fv, fn, fb] = dq.front();\n auto [bv, bn, bb] = dq.back();\n // cout << tmp << endl;\n // cout << fv << \" \" << fn << \" \" << fb << endl;\n // cout << bv << \" \" << bn << \" \" << bb << endl;\n\n lint a = 1;\n if (fv == 1) a = fn;\n lint b = 1;\n if (bv == 1) b = (prods[bb] == 1 ? bn : bn + 1);\n ans += a * b;\n\n // cout << a << \" \" << b << endl;\n }\n }\n\n // cout << l << \" \" << r << \" \" << tmp << endl;\n\n // shrink\n auto [val, num] = ps[l];\n auto b = bucket[l];\n --cnt[b];\n if (cnt[b] == 0) {\n tmp -= prods[b];\n prods[b] = 0;\n } else {\n tmp -= prods[b];\n prods[b] /= val;\n tmp += prods[b];\n }\n dq.pop_front();\n }\n // cout << ans << endl;\n}\n\nlint expr() {\n vector<vector<lint>> a;\n lint res = 0;\n\n {\n auto [val, v] = term();\n res = val;\n a.push_back(v);\n }\n while (idx < int(s.size()) & s[idx] == '+') {\n ++idx;\n auto [val, v] = term();\n res = min(INF, res + val);\n a.push_back(v);\n }\n\n // cout << \"val: \" << res << endl;\n // for (auto v : a) {\n // for (auto x : v) cout << x << \" \";\n // cout << endl;\n // }\n\n solve(a);\n\n return res;\n}\npair<lint, vector<lint>> term() {\n lint res = 1;\n vector<lint> a;\n\n {\n auto val = factor();\n res = min(INF, res * val);\n a.push_back(val);\n }\n while (idx < int(s.size()) & s[idx] == '*') {\n ++idx;\n auto val = factor();\n res = min(INF, res * val);\n a.push_back(val);\n }\n\n return {res, a};\n}\nlint factor() {\n if (isdigit(s[idx])) {\n ++idx;\n return s[idx - 1] - '0';\n }\n ++idx;\n auto res = expr();\n ++idx;\n return res;\n}\n\nint main() {\n while (true) {\n cin >> n;\n if (n == 0) break;\n cin >> s;\n\n idx = 0;\n ans = 0;\n expr();\n\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 109220, "score_of_the_acc": -0.1462, "final_rank": 10 }, { "submission_id": "aoj_1630_7915828", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 30;\n#define rep(i, n) for(int i=0;i<n;i++)\n\nll solve(long long n, string s){\n int m = s.length();\n\n vector<int> lr(m, 0);\n iota(lr.begin(), lr.end(), 0);\n {\n stack<int> st;\n rep(i, m) {\n if(s[i] == '(') {\n st.push(i);\n } else if(s[i] == ')') {\n swap(lr[st.top()], lr[i]);\n st.pop();\n } \n }\n }\n\n ll ans = 0;\n\n auto calc2 = [&](vector<ll> &v) -> void {\n int k = v.size();\n assert(k & 1);\n vector<ll> ones(k + 2, 0);\n for(int i = k - 3; i >= 0; i -= 2) {\n if(v[i] == 1 and v[i + 1] == 1) {\n ones[i] = ones[i + 2] + 1;\n } \n }\n ll sum = v[0];\n deque<ll> dq;\n dq.push_back(v[0]);\n if(v[0] == n) ans ++;\n\n int j = 0;\n for(int i = 2; i < k; i += 2) {\n if(v[i - 1]) {\n sum -= dq.back();\n dq.back() *= v[i];\n sum += dq.back();\n } else {\n dq.push_back(v[i]);\n sum += v[i];\n }\n if(sum == n) {\n if(n == 1) {\n ans += ((ll)(i - j) >> 1LL) + 1;\n continue;\n }\n ans += ones[j] + 1;\n continue;\n } \n\n while(sum > n) {\n if(v[j + 1]) {\n assert(dq.front() % v[j] == 0);\n sum -= dq.front();\n dq.front() /= v[j];\n sum += dq.front();\n } else {\n sum -= dq.front();\n dq.pop_front();\n }\n j += 2;\n }\n if(sum == n) {\n if(n == 1) {\n ans += ((ll)(i - j) >> 1LL) + 1;\n continue;\n }\n ans += ones[j] + 1;\n }\n }\n };\n\n auto calc = [&](vector<ll> &v) -> void {\n vector<ll> tmp;\n int k = v.size();\n assert(k & 1);\n for(ll i = 0; i < k; i ++) {\n if(i & 1) {\n tmp.push_back(v[i]);\n } else {\n if(v[i] > n) {\n if(tmp.empty());\n else {\n tmp.resize(tmp.size() - 1);\n calc2(tmp);\n tmp.clear();\n }\n i ++;\n continue;\n } \n tmp.push_back(v[i]);\n }\n }\n if(!tmp.empty()) calc2(tmp);\n };\n\n auto dfs = [&](auto dfs, int le, int ri) -> ll { // 合計を返す\n vector<ll> v;\n ll sum = 0, now = 0;\n bool mode = 0; // 0 add, 1 mul\n for(int i = le; i < ri; i ++) {\n ll num = 0;\n if(s[i] == '(') {\n v.push_back(dfs(dfs, i + 1, lr[i]));\n num = v.back();\n } else if(s[i] == '+') {\n v.push_back(0);\n mode = 0;\n sum += now;\n if(sum > n) sum = INF;\n now = 0;\n continue;\n } else if(s[i] == '*') {\n v.push_back(1);\n mode = 1;\n continue;\n } else {\n num = s[i] - '0';\n v.push_back(num);\n }\n\n if(sum + now > n) {\n sum = INF;\n now = 0;\n i = lr[i];\n continue;\n }\n\n if(le == i) {\n now = num;\n } else if(mode) {\n now *= num;\n } else {\n now += num;\n }\n if(sum + now > n) {\n sum = INF;\n now = 0;\n }\n i = lr[i];\n }\n if(sum != INF) sum += now;\n if(sum > n) sum = INF; \n calc(v);\n return sum;\n };\n\n dfs(dfs, 0, m);\n return ans;\n}\nint main(){\n long long n;\n cin >> n;\n while(n != 0LL){\n string s;\n cin >> s;\n cout << solve(n, s) << endl;\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 81764, "score_of_the_acc": -0.0818, "final_rank": 6 }, { "submission_id": "aoj_1630_7915365", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nvoid solve(ll n, string s) {\n\tll ans = 0;\n\tint t = 0;\n\tauto dfs = [&](auto f) -> ll {\n\t\tll cur = 1;\n\t\tll all = 0;\n\t\tbool inf = false;\n\t\tbool all_inf = false;\n\t\tvector<ll> V;\n\t\twhile(t < s.size()) {\n\t\t\tif(s[t] >= '0' && s[t] <= '9') {\n\t\t\t\tV.push_back(s[t] - '0');\n\t\t\t\tcur *= s[t] - '0';\n\t\t\t\tif(cur > n) inf = true;\n\t\t\t} else if(s[t] == '(') {\n\t\t\t\tt++;\n\t\t\t\tll inner = f(f);\n\t\t\t\tif(inner < 0) {\n\t\t\t\t\tinf = true;\n\t\t\t\t}\n\t\t\t\tV.push_back(inner);\n\t\t\t\tcur *= inner;\n\t\t\t\tif(cur > n) inf = true;\n\t\t\t} else if(s[t] == '*') {\n\t\t\t\tV.push_back(0);\n\t\t\t} else if(s[t] == '+') {\n\t\t\t\tif(inf) all_inf = true;\n\t\t\t\tinf = false;\n\t\t\t\tall += cur;\n\t\t\t\tif(all > n) all_inf = true;\n\t\t\t\tcur = 1;\n\t\t\t\tV.push_back(1);\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt++;\n\t\t}\n\t\tall += cur;\n\t\tif(all > n) all_inf = true;\n\t\tif(inf) all_inf = true;\n\t\tcur = 1;\n\t\tint i = 0;\n\t\tint j = 0;\n\t\tdeque<ll> DI;\n\t\tdeque<ll> DJ;\n\t\tll curI = V[0];\n\t\tll curJ = V[0];\n\t\tDI.push_back(V[0]);\n\t\tDJ.push_back(V[0]);\n\t\tbool infI = false;\n\t\tbool infJ = false;\n\t\tif(V[0] == -1) {\n\t\t\tcurI = 0;\n\t\t\tcurJ = 0;\n\t\t\tDI.front() = 1;\n\t\t\tDJ.front() = 1;\n\t\t\tinfI = true;\n\t\t\tinfJ = true;\n\t\t}\n\t\tfor(int l = 0; l < V.size(); l += 2) {\n\t\t\tif(l > i) {\n\t\t\t\ti = l;\n\t\t\t\twhile(!DI.empty()) DI.pop_front();\n\t\t\t\tif(V[l] == -1) {\n\t\t\t\t\tinfI = true;\n\t\t\t\t\tDI.push_front(1);\n\t\t\t\t\tcurI = 0;\n\t\t\t\t} else {\n\t\t\t\t\tcurI = V[l];\n\t\t\t\t\tDI.push_back(V[l]);\n\t\t\t\t\tinfI = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(l > j) {\n\t\t\t\tj = l;\n\t\t\t\twhile(!DJ.empty()) DJ.pop_front();\n\t\t\t\tif(V[l] == -1) {\n\t\t\t\t\tinfJ = true;\n\t\t\t\t\tDJ.push_front(1);\n\t\t\t\t\tcurJ = 0;\n\t\t\t\t} else {\n\t\t\t\t\tcurJ = V[l];\n\t\t\t\t\tDJ.push_back(V[l]);\n\t\t\t\t\tinfJ = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile(!infI && curI < n && i + 1 < V.size()) {\n\t\t\t\tint op = V[i + 1];\t// 0:*, 1:+\n\t\t\t\tll a = V[i + 2];\n\t\t\t\tif(a == -1) {\n\t\t\t\t\tinfI = true;\n\t\t\t\t} else if(op == 1) {\n\t\t\t\t\tcurI += a;\n\t\t\t\t\tDI.push_back(a);\n\t\t\t\t} else {\n\t\t\t\t\tcurI -= DI.back();\n\t\t\t\t\tDI.back() *= a;\n\t\t\t\t\tcurI += DI.back();\n\t\t\t\t}\n\t\t\t\ti += 2;\n\t\t\t}\n\t\t\twhile(!infJ && curJ < n + 1\n\t\t\t\t && j + 1 < V.size()) {\n\t\t\t\tint op = V[j + 1];\n\t\t\t\tll a = V[j + 2];\n\t\t\t\tif(a == -1) {\n\t\t\t\t\tinfJ = true;\n\t\t\t\t} else if(op == 1) {\n\t\t\t\t\tcurJ += a;\n\t\t\t\t\tDJ.push_back(a);\n\t\t\t\t} else {\n\t\t\t\t\tcurJ -= DJ.back();\n\t\t\t\t\tDJ.back() *= a;\n\t\t\t\t\tcurJ += DJ.back();\n\t\t\t\t}\n\t\t\t\tj += 2;\n\t\t\t}\n\t\t\tif(!infI && curI < n && i < V.size()) i += 2;\n\t\t\tif(!infJ && curJ < n + 1 && j < V.size())\n\t\t\t\tj += 2;\n\n\t\t\tans += (j - i) / 2;\n\t\t\tif(l + 1 >= V.size()) break;\n\t\t\tint op = V[l + 1];\n\t\t\tint a = V[l];\n\t\t\tif(a == -1) {\n\t\t\t\tinfI = false;\n\t\t\t\tinfJ = false;\n\t\t\t} else if(op == 1) {\n\t\t\t\tint si = DI.size();\n\t\t\t\tcurI -= DI.front();\n\t\t\t\tDI.pop_front();\n\t\t\t\tcurJ -= DJ.front();\n\t\t\t\tDJ.pop_front();\n\t\t\t} else {\n\t\t\t\tcurI -= DI.front();\n\t\t\t\tDI.front() /= a;\n\t\t\t\tcurI += DI.front();\n\t\t\t\tcurJ -= DJ.front();\n\t\t\t\tDJ.front() /= a;\n\t\t\t\tcurJ += DJ.front();\n\t\t\t}\n\t\t}\n\t\tif(all_inf)\n\t\t\treturn -1;\n\t\telse\n\t\t\treturn all;\n\t};\n\tdfs(dfs);\n\tcout << ans << endl;\n}\nint main() {\n\tint n;\n\tcin >> n;\n\twhile(n != 0) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tsolve(n, s);\n\t\tcin >> n;\n\t}\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 42352, "score_of_the_acc": -0.0239, "final_rank": 5 }, { "submission_id": "aoj_1630_7914423", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while(1) {\n ll n; cin >> n;\n if(!n) break;\n string s; cin >> s;\n s = '(' + s + ')';\n int len = s.size();\n auto solve = [&](ll target) { // target以下が何通り\n if(target == 0) return 0LL;\n stack<vector<vector<int>>> stk; // カッコのレベルごとの積項の列\n ll ans = 0;\n for(int i = 0; i < len; i++) {\n if(s[i] == '(') {\n stk.emplace(vector<vector<int>>(1));\n } else if(s[i] == ')') {\n {\n // 1つの積項の中で完結\n for(auto& term : stk.top()) {\n int r = 0;\n ll cur = 1;\n term.push_back((ll)2e9); // 番兵\n for(int l = 0; l < (int)term.size()-1; cur /= term[l++]) { // [l,r)の積 <= target\n if(l > r) {\n r = l;\n cur = 1;\n }\n while(cur <= target) cur *= term[r++];\n ans += max(0, r - l - 1);\n }\n term.pop_back();\n }\n }\n stk.top().push_back(vector<int>(1, (ll)2e9)); // 番兵\n vector<vector<ll>> factor_prod_l; // 積項内の因数の左からの累積\n vector<vector<ll>> factor_prod_r; // 積項内の因数の右からの累積\n for(auto& term : stk.top()) {\n int m = term.size();\n factor_prod_l.emplace_back(vector<ll>(m+1));\n factor_prod_r.emplace_back(vector<ll>(m+1));\n factor_prod_l.back()[0] = factor_prod_r.back()[m] = 1;\n for(int fi = 0; fi < m; fi++) {\n factor_prod_l.back()[fi+1] = min((ll)2e9, factor_prod_l.back()[fi] * term[fi]);\n }\n for(int fi = m-1; fi >= 0; fi--) {\n factor_prod_r.back()[fi] = min((ll)2e9, factor_prod_r.back()[fi+1] * term[fi]);\n }\n }\n vector<int> factor_cnt_sum; // 積項内の因数の個数の累積和\n vector<ll> factor_prod_sum; // 積項の内の累積和\n factor_cnt_sum.emplace_back(0);\n factor_prod_sum.emplace_back(0);\n for(int ti = 0; ti < (int)stk.top().size(); ti++) {\n factor_cnt_sum.emplace_back(factor_cnt_sum.back() + stk.top()[ti].size());\n factor_prod_sum.emplace_back(factor_prod_sum.back() + factor_prod_l[ti].back());\n }\n stk.top().pop_back();\n {\n auto val = [&](int lti, int lfi, int rti, int rfi) {\n assert(lti < rti);\n return factor_prod_r[lti][lfi] + (factor_prod_sum[rti] - factor_prod_sum[lti+1]) + factor_prod_l[rti][rfi];\n };\n // 2つ以上の積項\n int rti = 1, rfi = 1;\n for(int ti = 0; ti < (int)stk.top().size(); ti++) {\n for(int fi = 0; fi < (int)stk.top()[ti].size(); fi++) {\n if(rti <= ti) {\n rti = ti+1;\n rfi = 1;\n }\n while(val(ti, fi, rti, rfi) <= target) {\n if(rfi == (int)stk.top()[rti].size()) {\n rti++;\n rfi = 1;\n } else {\n rfi++;\n }\n }\n ans += max(0, factor_cnt_sum[rti] + rfi - factor_cnt_sum[ti+1] - 1);\n }\n }\n }\n stk.pop();\n if(!stk.empty()) {\n factor_prod_sum.pop_back();\n // 式全体の値をカッコの外に置く\n stk.top().back().emplace_back(min((ll)2e9, factor_prod_sum.back()));\n }\n } else if(s[i] == '*');\n else if(s[i] == '+') {\n stk.top().emplace_back();\n } else {\n stk.top().back().emplace_back(s[i] - '0');\n }\n }\n return ans;\n };\n cout << solve(n) - solve(n-1) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 195040, "score_of_the_acc": -0.3899, "final_rank": 15 }, { "submission_id": "aoj_1630_7914413", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while(1) {\n ll n; cin >> n;\n if(!n) break;\n string s; cin >> s;\n s = '(' + s + ')';\n int len = s.size();\n auto solve = [&](ll target) { // target以下が何通り\n /* cerr << target << '\\n'; */\n if(target == 0) return 0LL;\n stack<vector<vector<int>>> stk; // カッコのレベルごとの積項の列\n ll ans = 0;\n for(int i = 0; i < len; i++) {\n /* cerr << i << ' ' << ans << endl; */\n if(s[i] == '(') {\n stk.emplace(vector<vector<int>>(1));\n } else if(s[i] == ')') {\n /* cerr << '!' << ans << '\\n'; */\n {\n // 1つの積項の中で完結\n for(auto& term : stk.top()) {\n /* cerr << term.size() << '\\n'; */\n int r = 0;\n ll cur = 1;\n term.push_back((ll)2e9); // 番兵\n for(int l = 0; l < (int)term.size()-1; cur /= term[l++]) { // [l,r)の積 <= target\n if(l > r) {\n r = l;\n cur = 1;\n }\n while(cur <= target) cur *= term[r++];\n /* cerr << l << ' ' << r << ' ' << cur << '\\n'; */\n ans += max(0, r - l - 1);\n }\n term.pop_back();\n }\n }\n /* cerr << '@' << ans << '\\n'; */\n stk.top().push_back(vector<int>(1, (ll)2e9)); // 番兵\n vector<vector<ll>> factor_prod_l; // 積項内の因数の左からの累積\n vector<vector<ll>> factor_prod_r; // 積項内の因数の右からの累積\n for(auto& term : stk.top()) {\n int m = term.size();\n factor_prod_l.emplace_back(vector<ll>(m+1));\n factor_prod_r.emplace_back(vector<ll>(m+1));\n factor_prod_l.back()[0] = factor_prod_r.back()[m] = 1;\n for(int fi = 0; fi < m; fi++) {\n factor_prod_l.back()[fi+1] = min((ll)2e9, factor_prod_l.back()[fi] * term[fi]);\n }\n for(int fi = m-1; fi >= 0; fi--) {\n factor_prod_r.back()[fi] = min((ll)2e9, factor_prod_r.back()[fi+1] * term[fi]);\n }\n }\n vector<int> factor_cnt_sum; // 積項内の因数の個数の累積和\n vector<ll> factor_prod_sum; // 積項の内の累積和\n factor_cnt_sum.emplace_back(0);\n factor_prod_sum.emplace_back(0);\n for(int ti = 0; ti < (int)stk.top().size(); ti++) {\n factor_cnt_sum.emplace_back(factor_cnt_sum.back() + stk.top()[ti].size());\n factor_prod_sum.emplace_back(factor_prod_sum.back() + factor_prod_l[ti].back());\n }\n stk.top().pop_back();\n {\n auto val = [&](int lti, int lfi, int rti, int rfi) {\n assert(lti < rti);\n return factor_prod_r[lti][lfi] + (factor_prod_sum[rti] - factor_prod_sum[lti+1]) + factor_prod_l[rti][rfi];\n };\n // 2つ以上の積項\n int rti = 1, rfi = 1;\n for(int ti = 0; ti < (int)stk.top().size(); ti++) {\n for(int fi = 0; fi < (int)stk.top()[ti].size(); fi++) {\n if(rti <= ti) {\n rti = ti+1;\n rfi = 1;\n }\n while(val(ti, fi, rti, rfi) <= target) {\n if(rfi == (int)stk.top()[rti].size()) {\n rti++;\n rfi = 1;\n } else {\n rfi++;\n }\n }\n ans += max(0, factor_cnt_sum[rti] + rfi - factor_cnt_sum[ti+1] - 1);\n }\n }\n }\n /* cerr << '#' << ans << '\\n'; */\n ll value = 0; // 式全体の値\n for(int ti = 0; ti < (int)stk.top().size(); ti++) {\n value = min((ll)2e9, value + factor_prod_l[ti].back());\n }\n /* cerr << \"val\" << value << '\\n'; */\n stk.pop();\n if(!stk.empty()) {\n stk.top().back().emplace_back(value);\n }\n } else if(s[i] == '*');\n else if(s[i] == '+') {\n stk.top().emplace_back();\n } else {\n stk.top().back().emplace_back(s[i] - '0');\n }\n }\n return ans;\n };\n cout << solve(n) - solve(n-1) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 193832, "score_of_the_acc": -0.3932, "final_rank": 16 } ]
aoj_1629_cpp
Equilateral Triangular Fence Ms. Misumi owns an orchard along a straight road. Recently, wild boars have been witnessed strolling around the orchard aiming at pears, and she plans to construct a fence around many of the pear trees. The orchard contains n pear trees, whose locations are given by the two-dimensional Euclidean coordinates ( x 1 , y 1 ),..., ( x n , y n ). For simplicity, we neglect the thickness of pear trees. Ms. Misumi's aesthetic tells that the fence has to form a equilateral triangle with one of its edges parallel to the road. Its opposite apex, of course, should be apart from the road. The coordinate system for the positions of the pear trees is chosen so that the road is expressed as y = 0, and the pear trees are located at y ≥ 1. Due to budget constraints, Ms. Misumi decided to allow at most k trees to be left outside of the fence. You are to find the shortest possible perimeter of the fence on this condition. The following figure shows the first dataset of the Sample Input. There are four pear trees at (−1,2), (0,1), (1,2), and (2,1). By excluding (−1,2) from the fence, we obtain the equilateral triangle with perimeter 6. Input The input consists of multiple datasets, each in the following format. n k x 1 y 1 ... x n y n Each of the datasets consists of n +2 lines. n in the first line is the integer representing the number of pear trees; it satisfies 3 ≤ n ≤ 10 000. k in the second line is the integer representing the number of pear trees that may be left outside of the fence; it satisfies 1 ≤ k ≤ min( n −2, 5 000). The following n lines have two integers each representing the x- and y- coordinates, in this order, of the locations of pear trees; it satisfies −10 000 ≤ x i ≤ 10 000, 1 ≤ y i ≤ 10 000. No two pear trees are at the same location, i.e., ( x i , y i )=( x j , y j ) only if i = j . The end of the input is indicated by a line containing a zero. The number of datasets is at most 100. Output For each dataset, output a single number that represents the shortest possible perimeter of the fence. The output must not contain an error greater than 10 −6 . Sample Input 4 1 0 1 1 2 -1 2 2 1 4 1 1 1 2 2 1 3 1 4 4 1 1 1 2 2 3 1 4 1 4 1 1 2 2 1 3 2 4 2 5 2 0 1 0 2 0 3 0 4 0 5 6 3 0 2 2 2 1 1 0 3 2 3 1 4 0 Output for the Sample Input 6.000000000000 6.928203230276 6.000000000000 7.732050807569 6.928203230276 6.000000000000
[ { "submission_id": "aoj_1629_10852996", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\nconst int N = 1e6 + 7;\ndouble sq3 = sqrt(3.0);\nstruct Node {\n int x, y;\n double dx, dy;\n} p[N], left[N], right[N], l[N], r[N];\nint n, k;\nbool cmp1(const Node &a, const Node &b) {\n return a.y<b.y || (a.y==b.y && a.x<b.x);\n}\nbool cmp2(const Node &a, const Node &b) {\n return a.dx>b.dx || (a.dx==b.dx && a.x<b.x);\n}\nbool cmp3(const Node &a, const Node &b) {\n return a.dy<b.dy || (a.dy==b.dy && a.x<b.x);\n}\ndouble check(int h, int k) {\n int cl = 0, cr = 0;\n double mn = 120000;\n for (int i = 1; i <= n; ++i) {\n if (left[i].y >= h) l[++cl] = left[i];\n if (right[i].y >= h) r[++cr] = right[i];\n }\n if (cl <= k) return mn;\n int rm = cl - k;//需要包含的点数\n //m 更新当前区域内包含的点数,j 为最右?点的下标\n //整个三?形从左向右移动\n for (int i = 1, m = rm, j = rm; i <= k+1;) {\n //若左移后的新的最左?点,之前没有加?,则 m+1\n if (l[i].dy > r[j].dy) ++m;\n while (m < rm) {\n if (j<cl && r[++j].dx>l[i].dx) continue;\n ++m;\n }\n if (j > cl) continue;\n\n if (r[j].dy < l[i].dy) mn = std::min(mn, (l[i].dy+l[i].dx-2*h)*sq3);\n else mn = std::min(mn, (r[j].dy+l[i].dx-2*h) * sq3);\n for (++i, --m; i<=k+1 && l[i].dx==l[i-1].dx; ++i, --m);\n }\n return mn;\n}\nint main() {\n while (~scanf(\"%d\", &n) && n) {\n\n scanf(\"%d\", &k); ++k;\n double mn = 120000;\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d%d\", &p[i].x, &p[i].y);\n p[i].dx = p[i].y - sq3 * p[i].x;\n p[i].dy = p[i].y + sq3 * p[i].x;\n left[i] = right[i] = p[i];\n }\n std::sort(p+1, p+n+1, cmp1);\n std::sort(left+1, left+n+1, cmp2);\n std::sort(right+1, right+n+1, cmp3);\n for (int i = 1; i <= k;) {\n double cir = check(p[i].y, k-i);\n if (cir < 120000) mn = std::min(mn, cir);\n for (++i; i<=k && p[i-1].y==p[i].y; ++i);\n }\n printf(\"%.12f\\n\", mn);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 13460, "score_of_the_acc": -1, "final_rank": 18 }, { "submission_id": "aoj_1629_10660680", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nstruct Matrix {\n vector<vector<T>> m;\n Matrix() = default;\n Matrix(int x) : m(vector(x, vector<T>(x, 0))) {}\n Matrix(const vector<vector<T>> &a) : m(a) {}\n vector<T> &operator[](int i) { return m[i]; }\n const vector<T> &operator[](int i) const { return m[i]; }\n static Matrix identity(int x) {\n Matrix res(x);\n for (int i = 0; i < x; i++) res[i][i] = 1;\n return res;\n }\n static Matrix zero(int x) { return Matrix(x); }\n\n Matrix operator+() const { return (*this); }\n Matrix operator-() const { return Matrix(this->m.size()) - (*this); }\n Matrix operator+(const Matrix &a) const {\n Matrix x = (*this);\n return x += a;\n }\n Matrix operator-(const Matrix &a) const {\n Matrix x = (*this);\n return x -= a;\n }\n Matrix operator*(const Matrix &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix operator*(const T &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix &operator+=(const Matrix &a) {\n int n = m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n m[i][j] += a[i][j];\n }\n }\n return *this;\n }\n Matrix &operator-=(const Matrix &a) {\n int n = m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n m[i][j] -= a[i][j];\n }\n }\n return *this;\n }\n Matrix &operator*=(const Matrix &a) {\n int n = m.size();\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int k = 0; k < n; k++) {\n for (int j = 0; j < n; j++) {\n res[i][j] += m[i][k] * a[k][j];\n }\n }\n }\n m = res.m;\n return *this;\n }\n Matrix &operator*=(const T &a) {\n int n = m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n m[i][j] *= a;\n }\n }\n return *this;\n }\n Matrix pow(ll b) const {\n Matrix x = *this, res = identity(x.m.size());\n while (b) {\n if (b & 1) {\n res *= x;\n }\n x *= x;\n b >>= 1;\n }\n return res;\n }\n T determinant() {\n int n = m.size();\n Matrix A(*this);\n T ret = 1;\n for (int i = 0; i < n; i++) {\n int pivot = -1;\n for (int j = i; j < n; j++) {\n if (A[j][i] != 0) pivot = j;\n }\n if (pivot == -1) return T(0);\n if (i != pivot) {\n ret *= -1;\n swap(A[i], A[pivot]);\n }\n ret *= A[i][i];\n T tmp = T(1) / A[i][i];\n for (int j = 0; j < n; j++) {\n A[i][j] *= tmp;\n }\n for (int j = i + 1; j < n; j++) {\n T a = A[j][i];\n for (int k = 0; k < n; k++) {\n A[j][k] -= A[i][k] * a;\n }\n }\n }\n return ret;\n }\n Matrix inverse() {\n assert(determinant() != 0);\n int n = m.size();\n Matrix ret = identity(n);\n Matrix A(*this);\n for (int i = 0; i < n; i++) {\n int pivot = -1;\n for (int j = i; j < n; j++) {\n if (A[j][i] != 0) pivot = j;\n }\n if (i != pivot) {\n swap(ret[i], ret[pivot]);\n swap(A[i], A[pivot]);\n }\n T tmp = T(1) / A[i][i];\n for (int j = 0; j < n; j++) {\n A[i][j] *= tmp;\n ret[i][j] *= tmp;\n }\n for (int j = 0; j < n; j++) {\n if (j == i) continue;\n T a = A[j][i];\n for (int k = 0; k < n; k++) {\n A[j][k] -= A[i][k] * a;\n ret[j][k] -= ret[i][k] * a;\n }\n }\n }\n return ret;\n }\n vector<T> characteristic_polynomial() {\n int n = m.size();\n if (n == 0) return {1};\n Matrix A(*this);\n for (int i = 1; i < n; i++) {\n int pivot = -1;\n for (int j = i; j < n; j++) {\n if (A[j][i - 1] != 0) pivot = j;\n }\n if (pivot == -1) continue;\n if (i != pivot) {\n swap(A[i], A[pivot]);\n for (int j = 0; j < n; j++) swap(A[j][i], A[j][pivot]);\n }\n T tmp = T(1) / A[i][i - 1];\n vector<T> c(n);\n for (int j = i + 1; j < n; j++) c[j] = tmp * A[j][i - 1];\n for (int j = i + 1; j < n; j++) {\n for (int k = i - 1; k < n; k++) {\n A[j][k] -= A[i][k] * c[j];\n }\n }\n for (int j = 0; j < n; j++) {\n for (int k = i + 1; k < n; k++) {\n A[j][i] += A[j][k] * c[k];\n }\n }\n }\n vector dp(n + 1, vector<T>(n + 1));\n dp[0][0] = 1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j <= i; j++) {\n dp[i + 1][j] -= dp[i][j] * A[i][i];\n dp[i + 1][j + 1] += dp[i][j];\n }\n T p = 1;\n for (int k = i + 1; k < n; k++) {\n p *= A[k][k - 1];\n for (int j = 0; j <= i; j++) dp[k + 1][j] -= dp[i][j] * p * A[i][k];\n }\n }\n return dp[n];\n }\n friend ostream &operator<<(ostream &os, const Matrix &a) {\n int n = a.m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n os << a[i][j];\n if (j != n - 1) os << \" \";\n }\n os << \"\\n\";\n }\n return os;\n }\n friend istream &operator>>(istream &is, Matrix &a) {\n int n = a.m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n is >> a[i][j];\n }\n }\n return is;\n }\n};\n\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct disjointsparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n disjointsparsetable() = default;\n disjointsparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len, e()));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n int shift = 1 << i;\n for (int j = 0; j < (int)v.size(); j += shift << 1) {\n int t = min(j + shift, (int)v.size());\n table[i][t - 1] = v[t - 1];\n for (int k = t - 2; k >= j; k--) table[i][k] = op(v[k], table[i][k + 1]);\n if (v.size() <= t) break;\n table[i][t] = v[t];\n int r = min(t + shift, (int)v.size());\n for (int k = t + 1; k < r; k++) table[i][k] = op(table[i][k - 1], v[k]);\n }\n }\n logtable.resize(1 << len);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n if (l == r) return e();\n if (l >= --r) return table[0][l];\n int len = logtable[l ^ r];\n return op(table[len][l], table[len][r]);\n };\n};\npair<int, int> lcatree_op(pair<int, int> a, pair<int, int> b) { return min(a, b); }\npair<int, int> lcatree_e() { return {1000000000, -1}; }\nstruct lca_tree {\n int n, size;\n vector<int> in, ord, depth;\n disjointsparsetable<pair<int, int>, lcatree_op, lcatree_e> st;\n lca_tree(vector<vector<int>> g, int root = 0) : n((int)g.size()), size(log2(n) + 2), in(n), depth(n, n) {\n depth[root] = 0;\n function<void(int, int)> dfs = [&](int v, int p) {\n in[v] = (int)ord.size();\n ord.push_back(v);\n for (int u : g[v]) {\n if (u == p) continue;\n if (depth[u] > depth[v] + 1) {\n depth[u] = depth[v] + 1;\n dfs(u, v);\n ord.push_back(v);\n }\n }\n };\n dfs(root, -1);\n vector<pair<int, int>> vec((int)ord.size());\n for (int i = 0; i < (int)ord.size(); i++) {\n vec[i] = make_pair(depth[ord[i]], ord[i]);\n }\n st = vec;\n }\n int lca(int u, int v) {\n if (in[u] > in[v]) swap(u, v);\n if (u == v) return u;\n return st.query(in[u], in[v]).second;\n }\n int dist(int u, int v) {\n int l = lca(u, v);\n return depth[u] + depth[v] - 2 * depth[l];\n }\n};\nstruct auxiliary_tree : lca_tree {\n vector<vector<int>> G;\n auxiliary_tree(vector<vector<int>> &g) : lca_tree(g), G(n) {}\n pair<int, vector<int>> query(vector<int> vs, bool decending = false) {\n assert(!vs.empty());\n sort(vs.begin(), vs.end(), [&](int a, int b) { return in[a] < in[b]; });\n int m = vs.size();\n stack<int> st;\n st.push(vs[0]);\n for (int i = 0; i < m - 1; i++) {\n int w = lca(vs[i], vs[i + 1]);\n if (w != vs[i]) {\n int l = st.top();\n st.pop();\n while (!st.empty() && depth[w] < depth[st.top()]) {\n if (!decending) G[l].push_back(st.top());\n G[st.top()].push_back(l);\n l = st.top();\n st.pop();\n }\n if (st.empty() || st.top() != w) {\n st.push(w);\n vs.push_back(w);\n }\n if (!decending) G[l].push_back(w);\n G[w].push_back(l);\n }\n st.push(vs[i + 1]);\n }\n while (st.size() > 1) {\n int x = st.top();\n st.pop();\n if (!decending) G[x].push_back(st.top());\n G[st.top()].push_back(x);\n }\n // {root,vertex_list}\n return make_pair(st.top(), vs);\n }\n void clear(vector<int> vs) {\n for (int v : vs) G[v].clear();\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\nstruct Mo {\n int n;\n vector<pair<int, int>> lr;\n\n explicit Mo(int n) : n(n) {}\n\n void add(int l, int r) { /* [x, y) */ lr.emplace_back(l, r); }\n\n template <typename AL, typename AR, typename EL, typename ER, typename O>\n void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out) {\n int q = (int)lr.size();\n int bs = max<int>(1, 1.0 * n / max<double>(1.0, sqrt(q * 2.0 / 3.0)));\n vector<int> ord(q);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n int ablock = lr[a].first / bs, bblock = lr[b].first / bs;\n if (ablock != bblock) return ablock < bblock;\n return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second;\n });\n int l = 0, r = 0;\n for (auto idx : ord) {\n while (l > lr[idx].first) add_left(--l);\n while (r < lr[idx].second) add_right(r++);\n while (l < lr[idx].first) erase_left(l++);\n while (r > lr[idx].second) erase_right(--r);\n out(idx);\n }\n }\n\n template <typename A, typename E, typename O>\n void build(const A &add, const E &erase, const O &out) {\n build(add, add, erase, erase, out);\n }\n};\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct dual_segtree {\n int sz = 1, log = 0;\n vector<S> lz;\n dual_segtree() = default;\n dual_segtree(int n) : dual_segtree(vector<S>(n, e())) {}\n dual_segtree(vector<S> a) {\n int n = a.size();\n while (sz < n) {\n sz <<= 1;\n log++;\n }\n lz.assign(sz << 1, e());\n for (int i = 0; i < n; i++) lz[i + sz] = a[i];\n }\n void push(int k) {\n int b = __builtin_ctz(k);\n for (int d = log; d > b; d--) {\n lz[k >> d << 1] = op(lz[k >> d << 1], lz[k >> d]);\n lz[k >> d << 1 | 1] = op(lz[k >> d << 1 | 1], lz[k >> d]);\n lz[k >> d] = e();\n }\n }\n void apply(int l, int r, S x) {\n l += sz, r += sz;\n push(l);\n push(r);\n while (l < r) {\n if (l & 1) {\n lz[l] = op(lz[l], x);\n l++;\n }\n if (r & 1) {\n r--;\n lz[r] = op(lz[r], x);\n }\n l >>= 1, r >>= 1;\n }\n }\n S get(int k) {\n k += sz;\n S res = e();\n while (k) {\n res = op(res, lz[k]);\n k >>= 1;\n }\n return res;\n }\n};\n\nstruct LowLink {\n vector<vector<int>> g;\n vector<int> ord, low, out;\n vector<bool> used;\n vector<pair<int, int>> bridge;\n vector<pair<int, int>> articulation;\n int unions;\n LowLink(vector<vector<int>> g) : g(g) {\n int n = (int)g.size();\n ord.resize(n);\n low.resize(n);\n out.resize(n);\n used.resize(n);\n unions = 0;\n int t = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n dfs(i, t, -1);\n unions++;\n }\n }\n }\n void dfs(int v, int &t, int par) {\n used[v] = true;\n ord[v] = t++, low[v] = ord[v];\n int cnt = 0;\n bool par_back = false;\n for (int to : g[v]) {\n if (!used[to]) {\n dfs(to, t, v);\n low[v] = min(low[v], low[to]);\n if (ord[v] < low[to]) bridge.push_back(minmax(v, to));\n if (ord[v] <= low[to]) cnt++;\n } else if (to != par || par_back) {\n low[v] = min(low[v], ord[to]);\n } else\n par_back = true;\n }\n if (par != -1) cnt++;\n if (cnt >= 2) articulation.push_back({v, cnt});\n out[v] = t;\n }\n};\nvoid solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n int k;\n cin >> k;\n vector<tuple<int, int, int>> ps(n);\n rep(i, n) {\n int x, y;\n cin >> x >> y;\n ps[i] = {y, x, i};\n }\n sort(all(ps), [&](auto p, auto q) { return get<0>(p) + sqrt(3) * get<1>(p) < get<0>(q) + sqrt(3) * get<1>(q); });\n vector<pair<int, int>> point(n);\n rep(i, n) {\n get<2>(ps[i]) = i;\n point[i] = make_pair(get<0>(ps[i]), get<1>(ps[i]));\n }\n sort(all(ps));\n vector<int> Ys;\n rep(lb, k + 1) Ys.push_back(get<0>(ps[lb]));\n sort(all(ps), [&](auto p, auto q) { return -get<0>(p) + sqrt(3) * get<1>(p) > -get<0>(q) + sqrt(3) * get<1>(q); });\n double ans = INF;\n for (auto Y : Ys) {\n vector<bool> exist(n);\n int mx = n - 1; // n-k th\n int cnt = 0;\n for (auto [y, x, i] : ps) {\n if (y < Y) continue;\n if (mx >= i) {\n exist[i] = true;\n cnt++;\n }\n while (cnt >= n - k) {\n if(!exist[mx]){\n mx--;\n }\n else if (cnt>n-k) {\n exist[mx] = false;\n cnt--;\n }else break;\n }\n if (cnt == n - k) {\n auto [y2, x2] = point[mx];\n double xl = x + (Y - y) / sqrt(3);\n double xr = x2 + (y2 - Y) / sqrt(3);\n chmin(ans, 3 * (xr - xl));\n }\n }\n }\n printf(\"%.12lf\\n\", ans);\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 1570, "memory_kb": 3768, "score_of_the_acc": -0.0482, "final_rank": 1 }, { "submission_id": "aoj_1629_10445143", "code_snippet": "// AOJ 1629 Equilateral Triangular Fence\n// 2025.5.3\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst double SQ3 = sqrt(3.0);\n\nconst int MAXN = 1<<18;\ndouble X[MAXN], Y[MAXN], Lval[MAXN], Rval[MAXN];\nint idxL[MAXN], idxR[MAXN];\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while(true) {\n int N, K;\n cin >> N;\n if (N == 0) break;\n cin >> K;\n int req = N - K;\n\n for(int i = 0; i < N; i++){\n cin >> X[i] >> Y[i];\n double x = X[i], y = Y[i];\n Lval[i] = SQ3 * x - y;\n Rval[i] = SQ3 * x + y;\n idxL[i] = idxR[i] = i;\n }\n\n sort(idxL, idxL + N, [&](int a, int b){\n return Lval[a] < Lval[b];\n });\n sort(idxR, idxR + N, [&](int a, int b){\n return Rval[a] < Rval[b];\n });\n\n vector<double> ys(Y, Y + N);\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n double ans = 1e18;\n for(double y0 : ys){\n int cur = 0, cnt = 0;\n for(int j = 0; j < N; j++){\n int iL = idxL[j];\n double border = Lval[iL];\n\n while(cur < N && cnt < req){\n int iR = idxR[cur++];\n if(Y[iR] >= y0 && Lval[iR] >= border) cnt++;\n }\n if(cnt < req) break;\n\n double cl = (border + y0) / SQ3;\n double cr = (Rval[idxR[cur-1]] - y0) / SQ3;\n ans = min(ans, 3.0 * (cr - cl));\n if(Y[iL] >= y0 && Rval[iL] <= Rval[idxR[cur-1]]) cnt--;\n }\n }\n printf(\"%.7lf\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1520, "memory_kb": 11864, "score_of_the_acc": -0.8643, "final_rank": 17 }, { "submission_id": "aoj_1629_10445130", "code_snippet": "// AOJ 1629 Equilateral Triangular Fence\n// 2025.5.3\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst double SQ3 = sqrt(3.0);\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while(true){\n int N;\n cin >> N;\n if(N == 0) break;\n int K;\n cin >> K;\n int req = N - K;\n\n vector<double> X(N), Y(N);\n for(int i = 0; i < N; i++) cin >> X[i] >> Y[i];\n\n vector<pair<double,int>> L, R;\n L.reserve(N);\n R.reserve(N);\n for(int i = 0; i < N; i++){\n L.emplace_back(SQ3 * X[i] - Y[i], i);\n R.emplace_back(SQ3 * X[i] + Y[i], i);\n }\n sort(L.begin(), L.end());\n sort(R.begin(), R.end());\n\n vector<double> ys = Y;\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n\n double ans = 1e18;\n for(double y0 : ys){\n int cur = 0, cnt = 0;\n for(int j = 0; j < N; j++){\n double border = L[j].first;\n\n while(cur < N && cnt < req){\n int idx = R[cur].second;\n if(Y[idx] >= y0 && (SQ3*X[idx] - Y[idx]) >= border) cnt++;\n cur++;\n }\n if(cnt < req) break;\n\n double cl = (border + y0) / SQ3;\n double cr = (R[cur-1].first - y0) / SQ3;\n ans = min(ans, 3.0 * (cr - cl));\n\n int idx2 = L[j].second;\n if(Y[idx2] >= y0 && (SQ3*X[idx2] + Y[idx2]) <= R[cur-1].first) cnt--;\n }\n }\n printf(\"%.7lf\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1540, "memory_kb": 3920, "score_of_the_acc": -0.0604, "final_rank": 2 }, { "submission_id": "aoj_1629_9814217", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n double sq3 = sqrt(3.0);\nwhile(1) {\n int N, K;\n cin >> N >> K;\n if (N == 0) return 0;\n vector<pair<double,double>> P(N);\n rep(i,0,N) cin >> P[i].first >> P[i].second;\n sort(ALL(P),[](pair<double,double> P1, pair<double,double> P2){return P1.second < P2.second;});\n auto CalcL = [&](int i) -> double {\n return P[i].first - P[i].second/sq3;\n };\n auto CalcR = [&](int i) -> double {\n return P[i].first + P[i].second/sq3;\n };\n vector<pair<double,int>> XL, XR;\n rep(i,0,N) XL.push_back({CalcL(i),i}), XR.push_back({CalcR(i),i});\n sort(ALL(XL)), sort(ALL(XR));\n double ANS = INF;\n rep(i,0,K+1) {\n int L = 0, R = 0, COUNT = 0;\n while(L < N) {\n while(R < N && COUNT < N-K) {\n R++;\n if (XR[R-1].second >= i && CalcL(XR[R-1].second) >= XL[L].first) COUNT++;\n }\n if (R == N && COUNT < N-K) break;\n chmin(ANS, (XR[R-1].first-P[i].second/sq3)-(XL[L].first+P[i].second/sq3));\n if (XL[L].second >= i && CalcR(XL[L].second) <= XR[R-1].first) COUNT--;\n L++;\n }\n }\n printf(\"%.12f\\n\", ANS*3.0);\n}\n}", "accuracy": 1, "time_ms": 1700, "memory_kb": 4172, "score_of_the_acc": -0.1028, "final_rank": 4 }, { "submission_id": "aoj_1629_9474176", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<vvvi> vvvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nint main(){\n while(1){\n ll N,K;cin>>N>>K;\n if(!N)return 0;\n vector<pi>A(N);cin>>A;\n sort(ALL(A),[](pi a,pi b){return a.S>b.S;});\n auto f=[](pi a,pi b){\n ll x=a.F-b.F,y=b.S-a.S;\n if(x*y<=0)return x>y;\n if(x<0)return 3*x*x<y*y;\n return 3*x*x>y*y;\n };\n auto g=[](pi a,pi b){\n ll x=a.F-b.F,y=a.S-b.S;\n if(x*y<=0)return x<y;\n if(x<0)return 3*x*x>y*y;\n return 3*x*x<y*y;\n };\n auto B=A;\n sort(ALL(B),f);\n vi tr(N);\n REP(i,N)REP(j,N)if(A[i]==B[j])tr[j]=i;\n ld ans=1e18;\n REP(i,N){\n ll l=0,r=0,cnt=0;\n vb used(N);\n REP(j,N){\n ld fy=(ld)0.5*(A[i].S+B[j].S+(ld)sqrt(3)*(B[j].F-A[i].F));\n ll y=fy+1e-9;\n while(l<N&&A[l].S>y){\n if(used[l])cnt--,used[l]=0;\n l++;\n }\n chmax(r,l);\n while(r<N&&(cnt<N-K)){\n if((!f(A[r],B[j]))&&(!g(A[r],A[i])))cnt++,used[r]=1;\n r++;\n }\n if(cnt<N-K)break;\n if((!f(A[i],B[j]))&&(!g(B[j],A[i])))chmin(ans,(fy-min({A[r-1].S,A[i].S,B[j].S}))*2*sqrt(3));\n if(used[tr[j]]){\n used[tr[j]]=0;cnt--;\n }\n }\n }\n printf(\"%.20Lf\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5970, "memory_kb": 3660, "score_of_the_acc": -0.4994, "final_rank": 11 }, { "submission_id": "aoj_1629_9408541", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<iomanip>\nusing namespace std;\nconst double sqrt3=1.7320508075688772;\nconst double inv_sqrt3=double(1)/sqrt3;\nint N,K,X[10101],Y[10101];\nint cand_y[10101];\nint L[10101],R[10101];\npair<double,int>Ls[10101],Rs[10101];\ntuple<double,int,bool>cand_x[20202];\nbool checked[10101];\nbool solve(){\n cin>>N;\n if(N==0)return false;\n cin>>K;\n for(int i=0;i<N;i++)\n {\n cin>>X[i]>>Y[i];\n Ls[i].first=X[i]-Y[i]*inv_sqrt3;\n Rs[i].first=X[i]+Y[i]*inv_sqrt3;\n Ls[i].second=Rs[i].second=i;\n cand_y[i]=Y[i];\n }\n sort(Ls,Ls+N);\n sort(Rs,Rs+N);\n sort(cand_y,cand_y+N);\n for(int i=0;i<=cand_y[N-1];i++)checked[i]=false;\n double ans=1e150;\n for(int k=0;k<N;k++)\n {\n int t=cand_y[k];\n if(checked[t])continue;\n checked[t]=true;\n int i=0,j=0;\n int M=0;\n while(i<N||j<N)\n {\n // insert R\n if(i==N)\n {\n auto[x,y]=Rs[j];\n j++;\n if(Y[y]<t)continue;\n cand_x[M++]=make_tuple(x-t*inv_sqrt3,y,false);\n }\n // insert L\n else if(j==N)\n {\n auto[x,y]=Ls[i];\n i++;\n if(Y[y]<t)continue;\n cand_x[M++]=make_tuple(x+t*inv_sqrt3,y,true);\n }\n else\n {\n auto[xl,yl]=Ls[i];\n auto[xr,yr]=Rs[j];\n // insert L\n if(xl+t*inv_sqrt3<xr-t*inv_sqrt3)\n {\n i++;\n if(Y[yl]<t)continue;\n cand_x[M++]=make_tuple(xl+t*inv_sqrt3,yl,true);\n }\n // insert R\n else\n {\n j++;\n if(Y[yr]<t)continue;\n cand_x[M++]=make_tuple(xr-t*inv_sqrt3,yr,false);\n }\n }\n }\n if(M<2*N-2*K)break;\n for(int i=0;i<M;i++)\n {\n auto[x,idx,is_left]=cand_x[i];\n if(is_left)L[idx]=i;\n else R[idx]=i;\n }\n int r=0,cnt=0;\n for(int l=0;l<M;l++)\n {\n while(r<M&&cnt<N-K)\n {\n auto[x,idx,is_left]=cand_x[r];\n if(!is_left&&l<=L[idx])cnt++;\n r++;\n }\n double d=get<0>(cand_x[r-1])-get<0>(cand_x[l]);\n if(cnt>=N-K)ans=min(ans,3*d);\n if(get<2>(cand_x[l])&&R[get<1>(cand_x[l])]<r)cnt--;\n }\n }\n cout<<fixed<<setprecision(16)<<ans<<'\\n';\n return true;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 6730, "memory_kb": 4304, "score_of_the_acc": -0.6446, "final_rank": 14 }, { "submission_id": "aoj_1629_9407960", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<iomanip>\nusing namespace std;\nconst double sqrt3=sqrtl(double(3.0));\nconst double inv_sqrt3=double(1)/sqrt3;\nbool solve(){\n int N;\n cin>>N;\n if(N==0)return false;\n int K;\n cin>>K;\n vector<int>X(N),Y(N);\n for(int i=0;i<N;i++)cin>>X[i]>>Y[i];\n vector<pair<double,int>>Ls(N),Rs(N);\n vector<int>cand_y(N);\n for(int i=0;i<N;i++)\n {\n Ls[i].first=X[i]-Y[i]*inv_sqrt3;\n Rs[i].first=X[i]+Y[i]*inv_sqrt3;\n Ls[i].second=Rs[i].second=i;\n cand_y[i]=Y[i];\n }\n sort(Ls.begin(),Ls.end());\n sort(Rs.begin(),Rs.end());\n sort(cand_y.begin(),cand_y.end());\n cand_y.erase(unique(cand_y.begin(),cand_y.end()),cand_y.end());\n double ans=1e150;\n for(int t:cand_y)\n {\n vector<tuple<double,int,bool>>cand_x; // (x, i, is_L)\n cand_x.reserve(2*N);\n int i=0,j=0;\n while(i<N||j<N)\n {\n // insert R\n if(i==N)\n {\n auto[x,y]=Rs[j];\n j++;\n if(Y[y]<t)continue;\n cand_x.push_back(make_tuple(x-t*inv_sqrt3,y,false));\n }\n // insert L\n else if(j==N)\n {\n auto[x,y]=Ls[i];\n i++;\n if(Y[y]<t)continue;\n cand_x.push_back(make_tuple(x+t*inv_sqrt3,y,true));\n }\n else\n {\n auto[xl,yl]=Ls[i];\n auto[xr,yr]=Rs[j];\n // insert L\n if(xl+t*inv_sqrt3<xr-t*inv_sqrt3)\n {\n i++;\n if(Y[yl]<t)continue;\n cand_x.push_back(make_tuple(xl+t*inv_sqrt3,yl,true));\n }\n // insert R\n else\n {\n j++;\n if(Y[yr]<t)continue;\n cand_x.push_back(make_tuple(xr-t*inv_sqrt3,yr,false));\n }\n }\n }\n vector<int>L(N,-1),R(N,-1);\n for(int i=0;i<cand_x.size();i++)\n {\n auto[x,idx,is_left]=cand_x[i];\n if(is_left)L[idx]=i;\n else R[idx]=i;\n }\n // for(auto[x,f]:cand_x)cout<<fixed<<setprecision(3)<<x<<' ';cout<<endl;\n int r=0,cnt=0;\n for(int l=0;l<cand_x.size();l++)\n {\n while(r<cand_x.size()&&cnt<N-K)\n {\n auto[x,idx,is_left]=cand_x[r];\n if(!is_left&&l<=L[idx])cnt++;\n r++;\n }\n double d=get<0>(cand_x[r-1])-get<0>(cand_x[l]);\n if(cnt>=N-K)ans=min(ans,3*d);\n if(get<2>(cand_x[l])&&R[get<1>(cand_x[l])]<r)cnt--;\n }\n }\n cout<<fixed<<setprecision(16)<<ans<<'\\n';\n return true;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 10790, "memory_kb": 4180, "score_of_the_acc": -1.0584, "final_rank": 19 }, { "submission_id": "aoj_1629_9322967", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\n\ntemplate <class T, T (*ide)(), T (*ope)(T, T)>\nstruct segment_tree\n{\n int n;\n vector<T> node;\n\n segment_tree() {}\n segment_tree(const vector<T> &init)\n {\n n = 1;\n while (n < init.size())\n n *= 2;\n node.resize(2 * n, ide());\n for (int i = 0; i < init.size(); i++)\n node[i + n] = init[i];\n for (int i = n; i-- > 1;)\n node[i] = ope(node[i * 2], node[i * 2 + 1]);\n } ///\n\n void update(int i, T x)\n {\n i += n;\n node[i] = x;\n while (i > 1)\n {\n i >>= 1;\n node[i] = ope(node[i * 2], node[i * 2 + 1]);\n }\n }\n\n T sum(int l, int r) const\n {\n T lx = ide();\n T rx = ide();\n l += n;\n r += n;\n while (l < r)\n {\n if (l & 1)\n {\n lx = ope(lx, node[l++]);\n }\n if (r & 1)\n {\n rx = ope(node[--r], rx);\n }\n l >>= 1;\n r >>= 1;\n }\n return ope(lx, rx);\n }\n\n const T &at(int i) const\n {\n return node[i + n];\n } ///\n\n template <class F>\n int subtree_down_first(int i, T lx, F isok) const\n {\n while (i < n)\n {\n T next = ope(lx, node[i * 2]);\n if (isok(next))\n i = i * 2;\n else\n {\n lx = next;\n i = i * 2 + 1;\n }\n }\n return i - n + 1;\n }\n\n template <class F>\n int find_first(int l, F isok) const\n {\n if (isok(ide()))\n {\n return l;\n }\n if (l == 0)\n {\n if (isok(node[1]))\n return subtree_down_first(1, ide(), isok);\n return -1;\n }\n T lx = ide();\n int r = n << 1;\n l += n;\n while (l < r)\n {\n if (l & 1)\n {\n T next = ope(lx, node[l]);\n if (isok(next))\n return subtree_down_first(l, lx, isok);\n lx = next;\n l++;\n }\n l >>= 1;\n r >>= 1;\n }\n return -1;\n } ///\n};\n\nint op(int x, int y) { return x + y; }\nint ide() { return 0; }\nusing Seg = segment_tree<int, ide, op>;\n\nbool solve()\n{\n int N;\n std::cin >> N;\n if (N == 0)\n return false;\n int K;\n std::cin >> K;\n std::vector<double> X(N), Y(N);\n std::vector<double> Ys;\n for (int i = 0; i < N; i++)\n {\n std::cin >> X[i] >> Y[i];\n Ys.push_back(Y[i]);\n }\n std::sort(Ys.begin(), Ys.end());\n Ys.erase(std::unique(Ys.begin(), Ys.end()), Ys.end());\n\n double ans = 1e18;\n\n std::vector<std::tuple<double, int>> in, out;\n for (int i = 0; i < N; i++)\n {\n double h = Y[i] - 0;\n in.push_back({X[i] - h / std::sqrt(3), i});\n out.push_back({X[i] + h / std::sqrt(3), i});\n }\n std::sort(in.begin(), in.end());\n std::sort(out.begin(), out.end());\n std::vector<int> oi(N, -1);\n for (int i = 0; i < out.size(); i++)\n {\n oi[std::get<1>(out[i])] = i;\n }\n\n for (int b = 0; b < Ys.size(); b++)\n {\n int baseline = Ys[b];\n std::vector<int> init(out.size(), 0);\n for (int i = 0; i < N; i++)\n {\n if (Y[i] < baseline)\n continue;\n init[oi[i]] = 1;\n }\n Seg seg(init);\n for (auto [x, i] : in)\n {\n if (Y[i] < baseline)\n continue;\n int R = seg.find_first(0, [&](int s)\n { return s >= N - K; });\n if (R == -1)\n break;\n int j = std::get<1>(out[R - 1]);\n double r = X[j] + (Y[j] - baseline) / std::sqrt(3);\n double l = X[i] - (Y[i] - baseline) / std::sqrt(3);\n ans = std::min(ans, 3.0 * (r - l));\n seg.update(oi[i], 0);\n }\n }\n std::cout << ans << std::endl;\n return true;\n}\n\nint main()\n{\n std::cout << std::setprecision(10) << std::fixed;\n while (solve())\n ;\n}", "accuracy": 1, "time_ms": 5940, "memory_kb": 4236, "score_of_the_acc": -0.5547, "final_rank": 13 }, { "submission_id": "aoj_1629_9259938", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#pragma GCC target(\"sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native\")\n\nusing ll = long long;\nusing ld = long double;\n\nmt19937 engine(42);\n\nbool is_end = false;\n\nconst ld root3 = sqrtl((ld)3.0);\n\ninline tuple<ld, ld, ld> convert_xy_to_pqr(ld x, ld y)\n{\n return {y, root3 * x - y, -root3 * x - y};\n}\n\ninline pair<ld, ld> convert_pqr_to_xy(ld p, ld q, ld r)\n{\n return {(p + q) / root3, p};\n}\n\ninline ld get_triangle_length(ld p, ld q, ld r)\n{\n ld x0 = (p + q) / root3;\n ld x1 = - (p + r) / root3;\n return 3 * (x1 - x0);\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nvoid solve()\n{\n int N, K; cin >> N >> K;\n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n vector<tuple<ld, ld, ld>> tree(N);\n vector<vector<pair<ld, int>>> ord(3, vector<pair<ld, int>>(N));\n for (uint i = 0; i < N; ++i)\n {\n ld x, y; cin >> x >> y;\n ld ran = (ld)engine() / UINT_MAX;\n y += ran * 1e-11;\n auto [p, q, r] = tree[i] = convert_xy_to_pqr(x, y);\n \n ord[0][i] = {p, i};\n ord[1][i] = {q, i};\n ord[2][i] = {r, i};\n }\n for (uint j = 0; j < 3; ++j)\n {\n sort(ord[j].begin(), ord[j].end());\n }\n \n ld res = 1e18;\n vector<bool> isin(N, true);\n int outer = 0;\n for (uint i0 = 0; i0 <= K; ++i0)\n {\n auto [p, id0] = ord[0][i0];\n uint i2 = K;\n outer = 0;\n for (int j = 0; j <= K; ++j)\n {\n auto [r0, id0] = ord[0][j];\n auto [r1, id1] = ord[1][j];\n auto [r2, id2] = ord[1][j];\n isin[id0] = isin[id1] = isin[id2] = true;\n }\n \n for (uint j = 0; j < i0; ++j)\n {\n auto [r0, id0] = ord[0][j];\n isin[id0] = false, outer++;\n }\n for (uint j = 0; j < K; ++j)\n {\n auto [r, id2] = ord[2][j];\n if (isin[id2]) isin[id2] = false, outer++;\n }\n // cout << i0 << \" \" << outer << endl;\n \n for (uint i1 = 0; i1 <= K; ++i1)\n {\n auto [q, id1] = ord[1][i1];\n \n while (outer > K && i2 > 0)\n {\n --i2;\n auto [r, id2] = ord[2][i2];\n auto [p2, q2, r2] = tree[id2];\n if (p2 >= p && q2 >= q) isin[id2] = true, outer--;\n }\n \n if (outer > K) break;\n \n chmin(res, get_triangle_length(p, q, ord[2][i2].first));\n \n if (isin[id1]) isin[id1] = false, outer++;\n }\n \n // if (isin[id0]) isin[id0] = false, outer++;\n }\n \n cout << fixed << setprecision(20);\n cout << res << endl;\n \n return;\n}\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 2820, "memory_kb": 5096, "score_of_the_acc": -0.3142, "final_rank": 10 }, { "submission_id": "aoj_1629_9206632", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define ll long long\n\nusing D=double;\nD s3=1.7320508075688772;\n\nvoid solve(){\n int n,k;\n cin>>n;\n if(n==0)exit(0);\n cin>>k;\n int rem=n-k;\n vector<pair<int,int>>xy;\n vector<pair<D,int>>order;\n vector<pair<int,D>>iy;\n for(int i=0;i<n;i++){\n int x,y;\n cin>>x>>y;\n iy.push_back({i,y});\n order.push_back({D(x)-D(y)/s3,i});\n xy.push_back({x,y});\n }\n sort(order.begin(),order.end());\n sort(iy.begin(),iy.end(),[](pair<int,D>A,pair<int,D>B){return A.second>B.second;});\n D ans=1<<30;\n vector<int>ok(n,0);\n for(int i=0;i<n;i++){\n priority_queue<D,vector<D>>pq;\n int idx=iy[i].first;\n D y_pos=iy[i].second;\n ok[idx]=1;\n for(int j=n-1;j>=0;j--){\n int idx2=order[j].second;\n if(ok[idx2]){\n auto [x,y]=xy[idx2];\n D L=D(x)-D(y-y_pos)/s3;\n D R=D(x)+D(y-y_pos)/s3;\n pq.push(R);\n if(pq.size()>rem)pq.pop();\n if(pq.size()==rem)ans=min(ans,pq.top()-L);\n }\n }\n }\n cout<<fixed<<setprecision(15)<<ans*3<<'\\n';\n}\n\nint main(){\n while(1)solve();\n}", "accuracy": 1, "time_ms": 7840, "memory_kb": 4180, "score_of_the_acc": -0.7486, "final_rank": 16 }, { "submission_id": "aoj_1629_8323931", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n\twhile (true) {\n\t\t// step #1. input\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tint K;\n\t\tcin >> K;\n\t\tvector<int> X(N), Y(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> X[i] >> Y[i];\n\t\t}\n\n\t\t// step #2. preparation\n\t\tconst double SQRT3 = sqrt(3);\n\t\tK = N - K;\n\t\tvector<int> comps = Y;\n\t\tsort(comps.begin(), comps.end());\n\t\tcomps.erase(unique(comps.begin(), comps.end()), comps.end());\n\t\tvector<double> z1(N), z2(N);\n\t\tvector<int> p1(N), p2(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tz1[i] = X[i] * SQRT3 - Y[i];\n\t\t\tz2[i] = X[i] * SQRT3 + Y[i];\n\t\t\tp1[i] = i;\n\t\t\tp2[i] = i;\n\t\t}\n\t\tsort(p1.begin(), p1.end(), [&](int va, int vb) { return z1[va] < z1[vb]; });\n\t\tsort(p2.begin(), p2.end(), [&](int va, int vb) { return z2[va] < z2[vb]; });\n\t\tvector<int> inv1(N), inv2(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tinv1[p1[i]] = i;\n\t\t\tinv2[p2[i]] = i;\n\t\t}\n\t\t\n\t\t// step #3. search\n\t\tdouble ans = 1.0e+99;\n\t\tfor (int border : comps) {\n\t\t\tint cnt = 0, pos = -1;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\twhile (pos != N - 1 && cnt < K) {\n\t\t\t\t\tpos += 1;\n\t\t\t\t\tif (Y[p2[pos]] >= border && inv1[p2[pos]] >= i) {\n\t\t\t\t\t\tcnt += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (cnt >= K) {\n\t\t\t\t\tdouble v1 = z1[p1[i]];\n\t\t\t\t\tdouble v2 = z2[p2[pos]];\n\t\t\t\t\tdouble x1 = (v1 + border) / SQRT3;\n\t\t\t\t\tdouble x2 = (v2 - border) / SQRT3;\n\t\t\t\t\tans = min(ans, (x2 - x1) * 3);\n\t\t\t\t}\n\t\t\t\tif (Y[p1[i]] >= border && inv2[p1[i]] <= pos) {\n\t\t\t\t\tcnt -= 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// step #4. output\n\t\tcout.precision(15);\n\t\tcout << fixed << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2210, "memory_kb": 3772, "score_of_the_acc": -0.1158, "final_rank": 5 }, { "submission_id": "aoj_1629_8323457", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint N;\nint K;\nint X[1 << 18], Y[1 << 18];\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tcin >> N; if (N == 0) break;\n\t\tcin >> K;\n\t\tfor (int i = 1; i <= N; i++) cin >> X[i] >> Y[i];\n\n\t\t// Step 2. Sorting\n\t\tdouble SQRT3 = sqrt(3.0);\n\t\tvector<pair<double, int>> L;\n\t\tvector<pair<double, int>> R;\n\t\tfor (int i = 1; i <= N; i++) L.push_back(make_pair(SQRT3 * X[i] - 1.0 * Y[i], i));\n\t\tfor (int i = 1; i <= N; i++) R.push_back(make_pair(SQRT3 * X[i] + 1.0 * Y[i], i));\n\t\tsort(L.begin(), L.end());\n\t\tsort(R.begin(), R.end());\n\n\t\t// Step 3. Enumerate Zahyou\n\t\tvector<int> Zahyou;\n\t\tfor (int i = 1; i <= N; i++) Zahyou.push_back(Y[i]);\n\t\tsort(Zahyou.begin(), Zahyou.end());\n\t\tZahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());\n\n\t\t// Step 4. Brute Force\n\t\tdouble Answer = 1.0e12;\n\t\tfor (int pts : Zahyou) {\n\t\t\tint cur = 0, cnt = 0;\n\t\t\tfor (int j = 0; j < L.size(); j++) {\n\t\t\t\tdouble Border = L[j].first;\n\n\t\t\t\t// Shakutorization\n\t\t\t\twhile (cur < R.size() && cnt < N - K) {\n\t\t\t\t\tint idx = R[cur].second;\n\t\t\t\t\tif (Y[idx] >= pts && SQRT3 * X[idx] - 1.0 * Y[idx] >= Border) cnt += 1;\n\t\t\t\t\tcur += 1;\n\t\t\t\t}\n\t\t\t\tif (cnt < N - K) break;\n\n\t\t\t\t// Get Answer\n\t\t\t\tdouble cl = (L[j].first + 1.0 * pts) / SQRT3;\n\t\t\t\tdouble cr = (R[cur - 1].first - 1.0 * pts) / SQRT3;\n\t\t\t\tAnswer = min(Answer, 3.0 * (cr - cl));\n\n\t\t\t\t// Refresh\n\t\t\t\tint idx = L[j].second;\n\t\t\t\tif (Y[idx] >= pts && SQRT3 * X[idx] + 1.0 * Y[idx] <= R[cur - 1].first) cnt -= 1;\n\t\t\t}\n\t\t}\n\n\t\t// Step 5. Output\n\t\tprintf(\"%.12lf\\n\", Answer);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1620, "memory_kb": 3904, "score_of_the_acc": -0.0672, "final_rank": 3 }, { "submission_id": "aoj_1629_7915379", "code_snippet": "#include <string.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cfloat>\n#include <chrono>\n#include <ciso646>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define REP3(i, l, r, s) \\\n for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \\\n i += rep3_s)\n#define REP2(i, l, r) REP3(i, l, r, 1)\n#define REP1(i, n) REP2(i, 0, n)\n#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)\n#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)\n\n#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define RREP3(i, l, r, s) \\\n for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \\\n i -= rrep3_s)\n#define RREP2(i, l, r) RREP3(i, l, r, 1)\n#define RREP1(i, n) RREP2(i, 0, n)\n#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)\n#define rrepin(i, l, r) \\\n for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)\n\n#define fi first\n#define se second\n\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace rklib {\n\n// a <- max(a, b)\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// a <- min(a, b)\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// if a < 0: a <- b\n// else: a <- min(a, b)\ntemplate <class T>\nbool chmin_non_negative(T &a, const T &b) {\n if (a < 0 || a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// floor(num / den)\ntemplate <class T>\nT div_floor(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num >= 0 ? num / den : (num + 1) / den - 1;\n}\n\n// ceil(num / den)\ntemplate <class T>\nT div_ceil(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num <= 0 ? num / den : (num - 1) / den + 1;\n}\n\nnamespace internal {\n\ntemplate <class T>\nT remainder_count(T r, T b, T m) {\n return r / m * b + std::min(b, r % m);\n}\n\n} // namespace internal\n\n// Number of integer x s.t.\n// - x in [l, r)\n// - x mod m in [a, b)\ntemplate <class T>\nT remainder_count(T l, T r, T a, T b, T m) {\n assert(l >= 0);\n assert(m >= 1);\n\n if (l >= r || a >= b) return 0;\n if (m <= a || b < 0) return 0;\n chmax(a, T(0));\n chmin(b, m);\n\n auto res = internal::remainder_count(r, b, m);\n if (l >= 1) res -= internal::remainder_count(l, b, m);\n if (a >= 1) res -= internal::remainder_count(r, a, m);\n if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);\n\n return res;\n}\n\n} // namespace rklib\n\n\nusing namespace std;\nusing namespace rklib;\n\nusing lint = long long;\nusing pii = pair<int, int>;\nusing pll = pair<lint, lint>;\n\nint main() {\n while (true) {\n int n, K;\n cin >> n;\n if (n == 0) break;\n cin >> K;\n vector<double> x(n), y(n);\n rep(i, n) cin >> x[i] >> y[i];\n\n vector<int> lps(n), rps(n);\n iota(lps.begin(), lps.end(), 0);\n iota(rps.begin(), rps.end(), 0);\n sort(lps.begin(), lps.end(), [&](const int &i, const int &j) {\n return x[i] * sqrt(3) - y[i] < x[j] * sqrt(3) - y[j];\n });\n sort(rps.begin(), rps.end(), [&](const int &i, const int &j) {\n return x[i] * sqrt(3) + y[i] < x[j] * sqrt(3) + y[j];\n });\n\n double ans = 1e9;\n for (auto ty : y) {\n vector<bool> used(n, false);\n int cnt = 0;\n int r = 0;\n\n for (auto l_id : lps) {\n while (r < n && cnt < n - K) {\n int r_id = rps[r];\n if (!used[r_id] && y[r_id] >= ty) {\n used[r_id] = true;\n ++cnt;\n }\n ++r;\n }\n if (cnt >= n - K) {\n double lx = x[l_id] + (ty - y[l_id]) / sqrt(3);\n int r_id = rps[r - 1];\n double rx = x[r_id] + (y[r_id] - ty) / sqrt(3);\n chmin(ans, (rx - lx) * 3);\n } else {\n break;\n }\n\n if (used[l_id]) {\n --cnt;\n }\n used[l_id] = true;\n }\n }\n printf(\"%.10lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 2870, "memory_kb": 3656, "score_of_the_acc": -0.1733, "final_rank": 6 }, { "submission_id": "aoj_1629_7802509", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i,s,n) for(int i = int(s); i < int(n); i++)\n#define rrep(i,s,n) for(int i = int(n) - 1; i >= s; i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\n\ntemplate<class T>\nbool chmin(T &a, T b) {\n if(a <= b) return false;\n a = b;\n return true;\n}\ntemplate<class T>\nbool chmax(T &a, T b) {\n if(a >= b) return false;\n a = b;\n return true;\n}\n\nusing namespace std;\n\nconst ld dinf = 2e9;\nconst ld r3 = ld(1)/sqrtl(3);\n\nint main() {\n cout << fixed << setprecision(15);\n while (true){\n int n; cin >> n;\n if (n == 0) break;\n int k; cin >> k;\n vector<int> xs(n), ys(n);\n rep(i,0,n) cin >> xs[i] >> ys[i];\n vector<int> idl(n); iota(all(idl),0);\n sort(all(idl),[&](int i, int j){\n int ai = xs[i], bi = -ys[i];\n int aj = xs[j], bj = -ys[j];\n int lhs = bi-bj, rhs = aj-ai;\n if (lhs < 0 && rhs > 0) return true;\n if (lhs > 0 && rhs < 0) return false;\n if (lhs >= 0 && rhs >= 0){\n return lhs*lhs < 3*rhs*rhs;\n }\n return 3*rhs*rhs < lhs*lhs;\n });\n vector<int> idr(n); iota(all(idr),0);\n sort(all(idr),[&](int i, int j){\n int ai = xs[i], bi = +ys[i];\n int aj = xs[j], bj = +ys[j];\n int lhs = bi-bj, rhs = aj-ai;\n if (lhs < 0 && rhs > 0) return true;\n if (lhs > 0 && rhs < 0) return false;\n if (lhs >= 0 && rhs >= 0){\n return lhs*lhs < 3*rhs*rhs;\n }\n return 3*rhs*rhs < lhs*lhs;\n });\n vector<int> vl(n), vr(n);\n rep(i,0,n){\n vl[idl[i]] = i;\n vr[idr[i]] = i;\n }\n ld ans = dinf;\n for (int y : ys){\n int cnt = 0;\n int r = 0;\n rep(i,0,n){\n while (r < n && cnt < n-k){\n int id = idr[r];\n if (ys[id] >= y && vl[id] >= i) cnt++;\n r++;\n }\n if (cnt >= n-k){\n int il = idl[i], ir = idr[r-1];\n ld cur = (xs[ir] + (ys[ir]-y)*r3) - (xs[il] - (ys[il]-y)*r3);\n chmin(ans,cur);\n }\n int id = idl[i];\n if (ys[id] >= y && vr[id] < r) cnt--;\n }\n }\n cout << 3*ans << endl;\n }\n}", "accuracy": 1, "time_ms": 3640, "memory_kb": 3604, "score_of_the_acc": -0.2489, "final_rank": 8 }, { "submission_id": "aoj_1629_7073175", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconst double sqrt3 = 1.0/sqrt(3);\n\nstruct point{\n int x,y;\n point() {}\n point(int x,int y): x(x), y(y) {}\n};\n\nvector<pair<double,int>> lv,rv;\nbool ld[10000], rd[10000];\nbool done[10000];\n\ndouble solve(int n){\n int k; cin >> k;\n vector<point> p(n);\n vector<int> p_idx(n);\n lv.resize(n);\n rv.resize(n);\n for(int i=0;i<n;i++){\n cin >> p[i].x >> p[i].y;\n p_idx[i] = i;\n done[i] = false;\n lv[i] = {-p[i].y*sqrt3+p[i].x,i}; // y*sqrt3\n rv[i] = {p[i].y*sqrt3+p[i].x,i}; // -y*sqrt3\n }\n sort(lv.begin(), lv.end());\n sort(rv.begin(), rv.end());\n lv.push_back({1e9,-1});\n rv.push_back({1e9,-1});\n sort(p_idx.begin(), p_idx.end(), [&](auto i,auto j){\n return p[i].y < p[j].y;\n });\n int need = n-k;\n double res = 1e9;\n for(int i=0;i<=k;){\n int y = p[p_idx[i]].y;\n\n for(int j=0;j<n;j++){\n if(!done[j]) ld[j] = rd[j] = false;\n }\n\n int r1 = 0;\n int r2 = 0;\n int cnt = 0;\n const double lad = sqrt3*y;\n const double rad = -sqrt3*y;\n for(int l1=0;l1<n;l1++){\n while(r2<n and cnt < need){\n if(lv[r1].first+lad <= rv[r2].first+rad){\n int id = lv[r1].second;\n if(!done[id]){\n ld[id] = true;\n if(rd[id]) cnt++;\n }\n r1++;\n }\n else{\n int id = rv[r2].second;\n if(!done[id]){\n rd[id] = true;\n if(ld[id])cnt++;\n }\n r2++;\n }\n }\n if(cnt == need){\n res = min(res, rv[r2-1].first-lv[l1].first-lad+rad);\n }\n else break;\n ld[lv[l1].second] = false;\n if(rd[lv[l1].second]){\n rd[lv[l1].second] = false;\n cnt--;\n }\n }\n\n while(i<n and p[p_idx[i]].y == y){\n done[p_idx[i]] = true;\n i++;\n }\n }\n return res*3;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n printf(\"%.9f\\n\",solve(n));\n }\n}", "accuracy": 1, "time_ms": 4010, "memory_kb": 3772, "score_of_the_acc": -0.3049, "final_rank": 9 }, { "submission_id": "aoj_1629_6791031", "code_snippet": "#include <bits/stdc++.h>\n\nint ri() {\n\tint n;\n\tscanf(\"%d\", &n);\n\treturn n;\n}\n\nstd::mt19937 rnd;\nint rnd_int(int l, int r) {\n\treturn std::uniform_int_distribution<>(l, r)(rnd);\n}\n\nbool solve() {\n\tint n = ri();\n\tif (n == 0) return false;\n\tint k = ri();\n\tstruct Point {\n\t\tint a;\n\t\tdouble b;\n\t\tdouble c;\n\t};\n\tstd::vector<Point> points;\n\t// std::set<std::pair<int, int> > used;\n\tfor (int i = 0; i < n; i++) {\n\t\t/*\n\t\tint x, y;\n\t\tdo {\n\t\t\tx = rnd_int(-10000, 10000);\n\t\t\ty = rnd_int(-10000, 10000);\n\t\t} while (!used.insert({x, y}).second);*/\n\t\tint x = ri();\n\t\tint y = ri();\n\t\tpoints.push_back({y, (double) y / 2 - sqrt(3) * x / 2, (double) y / 2 + sqrt(3) * x / 2});\n\t}\n\tclock_t r0 = clock();\n\tstd::sort(points.begin(), points.end(), [] (auto &i, auto &j) { return i.a > j.a; });\n\tstd::map<double, double> bc;\n\tstd::map<double, double> cb;\n\tint added = 0;\n\tdouble res = 1000000000;\n\tfor (auto pt : points) {\n\t\tassert(!bc.count(pt.b));\n\t\tassert(!cb.count(pt.c));\n\t\tbc[pt.b] = pt.c;\n\t\tcb[pt.c] = pt.b;\n\t\tadded++;\n\t\tif (added >= n - k) {\n\t\t\tauto itr1 = bc.begin();\n\t\t\tauto itr2 = cb.rbegin();\n\t\t\t// assert(itr2 != cb.rend());\n\t\t\tdouble b_limit = -1000000000;\n\t\t\tdouble c_limit = itr2->first;\n\t\t\tint cur = 0;\n\t\t\twhile (itr1 != bc.end() && cur < n - k) {\n\t\t\t\tcur++;\n\t\t\t\tb_limit = itr1->first;\n\t\t\t\titr1++;\n\t\t\t}\n\t\t\tauto shakutori = [&] () {\n\t\t\t\twhile (cur >= n - k && itr2 != cb.rend() && (cur > n - k || itr2->second > b_limit)) {\n\t\t\t\t\tif (itr2->second <= b_limit) cur--;\n\t\t\t\t\titr2++;\n\t\t\t\t\t// assert(itr2 != cb.rend());\n\t\t\t\t\tc_limit = itr2->first;\n\t\t\t\t}\n\t\t\t};\n\t\t\tshakutori();\n\t\t\t// assert(itr1 != bc.begin());\n\t\t\tdouble min = b_limit + c_limit;\n\t\t\twhile (itr1 != bc.end()) {\n\t\t\t\tif (itr1->second <= c_limit) cur++;\n\t\t\t\tb_limit = itr1->first;\n\t\t\t\titr1++;\n\t\t\t\tshakutori();\n\t\t\t\tmin = std::min(min, b_limit + c_limit);\n\t\t\t}\n\t\t\tres = std::min(res, min - pt.a);\n\t\t}\n\t}\n\tclock_t r1 = clock();\n\tprintf(\"%.11f\\n\", res * sqrt(3) * 2);\n\t// fprintf(stderr, \"%.2f ms\\n\", (double)(r1 - r0) / CLOCKS_PER_SEC * 1000);\n\treturn true;\n}\nint main() {\n\twhile (1) {\n\t\tbool cont = solve();\n\t\tif (!cont) break;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6470, "memory_kb": 4828, "score_of_the_acc": -0.6704, "final_rank": 15 }, { "submission_id": "aoj_1629_6790903", "code_snippet": "#include <bits/stdc++.h>\n\nint ri() {\n\tint n;\n\tscanf(\"%d\", &n);\n\treturn n;\n}\n\nstd::mt19937 rnd;\nint rnd_int(int l, int r) {\n\treturn std::uniform_int_distribution<>(l, r)(rnd);\n}\n\nbool solve() {\n\tint n = ri();\n\tif (n == 0) return false;\n\tint k = ri();\n\tstruct Point {\n\t\tint a;\n\t\tdouble b;\n\t\tdouble c;\n\t};\n\tstd::vector<Point> points;\n\t// std::set<std::pair<int, int> > used;\n\tfor (int i = 0; i < n; i++) {\n\t\t/*\n\t\tint x, y;\n\t\tdo {\n\t\t\tx = rnd_int(-10000, 10000);\n\t\t\ty = rnd_int(-10000, 10000);\n\t\t} while (!used.insert({x, y}).second);*/\n\t\tint x = ri();\n\t\tint y = ri();\n\t\tpoints.push_back({y, (double) y / 2 - sqrt(3) * x / 2, (double) y / 2 + sqrt(3) * x / 2});\n\t}\n\tclock_t r0 = clock();\n\tstd::sort(points.begin(), points.end(), [] (auto &i, auto &j) { return i.a > j.a; });\n\tstd::map<double, double> bc;\n\tstd::map<double, double> cb;\n\tint added = 0;\n\tdouble res = 1000000000;\n\tfor (auto pt : points) {\n\t\tbc[pt.b] = pt.c;\n\t\tcb[pt.c] = pt.b;\n\t\tadded++;\n\t\tif (added >= n - k) {\n\t\t\tauto itr1 = bc.begin();\n\t\t\tauto itr2 = cb.rbegin();\n\t\t\t// assert(itr2 != cb.rend());\n\t\t\tdouble b_limit = -1000000000;\n\t\t\tdouble c_limit = itr2->first;\n\t\t\tint cur = 0;\n\t\t\twhile (itr1 != bc.end() && cur < n - k) {\n\t\t\t\tcur++;\n\t\t\t\tb_limit = itr1->first;\n\t\t\t\titr1++;\n\t\t\t}\n\t\t\t// assert(itr1 != bc.begin());\n\t\t\tdouble min = b_limit + c_limit;\n\t\t\twhile (itr1 != bc.end()) {\n\t\t\t\tif (itr1->second <= c_limit) cur++;\n\t\t\t\tb_limit = itr1->first;\n\t\t\t\titr1++;\n\t\t\t\twhile (cur >= n - k && itr2 != cb.rend() && (cur > n - k || itr2->second > b_limit)) {\n\t\t\t\t\tif (itr2->second <= b_limit) cur--;\n\t\t\t\t\titr2++;\n\t\t\t\t\t// assert(itr2 != cb.rend());\n\t\t\t\t\tc_limit = itr2->first;\n\t\t\t\t}\n\t\t\t\tmin = std::min(min, b_limit + c_limit);\n\t\t\t}\n\t\t\tres = std::min(res, min - pt.a);\n\t\t}\n\t}\n\tclock_t r1 = clock();\n\tprintf(\"%.11f\\n\", res * sqrt(3) * 2);\n\t// fprintf(stderr, \"%.2f ms\\n\", (double)(r1 - r0) / CLOCKS_PER_SEC * 1000);\n\treturn true;\n}\nint main() {\n\twhile (1) {\n\t\tbool cont = solve();\n\t\tif (!cont) break;\n\t}\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 6450, "memory_kb": 4832, "score_of_the_acc": -0.6687, "final_rank": 20 }, { "submission_id": "aoj_1629_6039974", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nstruct point\n{\n double x,y;\n};\n\nvoid chmin(double &x, double y)\n{\n if(x>y) x=y;\n}\n\nconstexpr double sq3=sqrt(3);\n\nvoid solve()\n{\n int n,k; cin>>n;\n if(!n) exit(0);\n cin>>k;\n k=n-k;\n vector<point> pnt(n);\n\n for(point &e:pnt)\n {\n cin>>e.x>>e.y;\n }\n sort(begin(pnt), end(pnt),\n [](point p1, point p2)\n {\n if(p1.y==p2.y)\n {\n return p1.x>p2.x;\n }\n return p1.y>p2.y;\n });\n\n struct left_comp\n {\n bool operator()(point p1, point p2)\n {\n return p2.y-sq3*p2.x>p1.y-sq3*p1.x;\n }\n };\n auto lpnt=pnt;\n sort(begin(lpnt),end(lpnt), left_comp());\n\n struct right_comp\n {\n bool operator()(point p1, point p2)\n {\n return p2.y+sq3*p2.x>p1.y+sq3*p1.x;\n }\n };\n auto rpnt=pnt;\n\n sort(begin(rpnt), end(rpnt), right_comp());\n\n double ans=1e5;\n\n for(auto i=begin(pnt),j=i; i!=end(pnt); i=j)\n {\n const double nowy=i->y;\n vector<point> clf,crg;\n for(auto &p: lpnt)\n {\n if(p.y>=nowy) clf.emplace_back(p);\n }\n // for(auto &p: rpnt)\n // {\n // if(p.y>=nowy) crg.emplace_back(p);\n // }\n // auto itr=end(crg);\n priority_queue<point,vector<point>,right_comp> que;\n\n for(const auto &p: clf)\n {\n // cerr << \"p: \" << p.x << \" \" << p.y << endl;\n\n if(que.size()<k || right_comp()(p,que.top()))\n {\n que.emplace(p);\n }\n if(que.size()>k) que.pop();\n if(que.size()==k) // update\n {\n const double lx=p.x-(p.y-nowy)/sq3;\n auto tp=que.top();\n const double rx=tp.x+(tp.y-nowy)/sq3;\n // cerr << nowy << endl;\n // cerr << \"p: \" << p.x << \" \" << p.y << endl;\n // cerr << \"tp: \" << tp.x << \" \" << tp.y << endl;\n chmin(ans, rx-lx);\n }\n }\n\n while(j!=end(pnt) && i->y==j->y) ++j;\n }\n\n cout << fixed << setprecision(10) << ans*3 << \"\\n\";\n}\n\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(0);\n while(true) solve();\n}", "accuracy": 1, "time_ms": 5670, "memory_kb": 4240, "score_of_the_acc": -0.5267, "final_rank": 12 }, { "submission_id": "aoj_1629_6026511", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n\twhile(1){\n\t\tint n; cin >> n;\n\t\tif(!n) break;\n\t\tint k; cin >> k;\n\t\tvector<pii> p(n);\n\t\trep(i,n) cin >> p[i].first >> p[i].second;\n\t\tsort(all(p),[](pii a, pii b){\n\t\t\tif(sqrt(3)*a.first+a.second == sqrt(3)*b.first+b.second){\n\t\t\t\treturn a.first < b.first;\n\t\t\t}\n\t\t\treturn sqrt(3)*a.first+a.second <= sqrt(3)*b.first+b.second;}\n\t\t);\n\t\tauto L = [](pii a, pii b) -> bool {\n\t\t\treturn -sqrt(3)*a.first+a.second <= -sqrt(3)*b.first+b.second + eps;\n\t\t};\n\t\tauto R = [](pii a, pii b) -> bool {\n\t\t\treturn sqrt(3)*a.first+a.second <= sqrt(3)*b.first+b.second + eps;\n\t\t};\n\t\tvector<int> v(10101,0);\n\t\tdouble ans = inf;\n\t\trep(i,n){\n\t\t\tint now = 0;\n\t\t\tint bottom = 1;\n\t\t\tint cnt = 0;\n\t\t\trep(j,n){\n\t\t\t\tif(now <= j && R(p[now],p[j])){\n\t\t\t\t\tif(L(p[now],p[i]) && p[now].second >= bottom){\n\t\t\t\t\t\tv[p[now].second]++;\n\t\t\t\t\t\tcnt++;\n\t\t\t\t\t}\n\t\t\t\t\tnow++;\n\t\t\t\t}\n\t\t\t\tdouble C = -sqrt(3)*p[i].first + p[i].second;\n\t\t\t\tdouble D = sqrt(3)*p[j].first + p[j].second;\n\t\t\t\twhile(cnt >= n-k){\n\t\t\t\t\tdouble x1 = (bottom-C)/sqrt(3);\n\t\t\t\t\tdouble x2 = (D-bottom)/sqrt(3);\n\t\t\t\t\tchmin(ans,x2-x1);\n\t\t\t\t\tif(cnt - v[bottom] < n-k) break;\n\t\t\t\t\tcnt -= v[bottom];\n\t\t\t\t\tv[bottom] = 0;\n\t\t\t\t\tbottom++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tREP(i,bottom,10100) v[i] = 0;\n\t\t}\n\t\tprintf(\"%.10f\\n\",ans*3);\n\t}\n}", "accuracy": 1, "time_ms": 3190, "memory_kb": 3660, "score_of_the_acc": -0.2074, "final_rank": 7 } ]
aoj_1632_cpp
Scores of Final Examination I am a junior high school teacher. The final examination has just finished, and I have all the students' scores of all the subjects. I want to know the highest total score among the students, but it is not an easy task as the student scores are listed separately for each subject. I would like to ask you, an excellent programmer, to help me by writing a program that finds the total score of a student with the highest total score. Input The input consists of multiple datasets, each in the following format. n m p 1,1 p 1,2 … p 1, n p 2,1 p 2,2 … p 2, n … p m ,1 p m ,2 … p m,n The first line of a dataset has two integers n and m . n is the number of students (1 ≤ n ≤ 1000). m is the number of subjects (1 ≤ m ≤ 50). Each of the following m lines gives n students' scores of a subject. p j,k is an integer representing the k -th student's score of the subject j (1 ≤ j ≤ m and 1 ≤ k ≤ n ). It satisfies 0 ≤ p j,k ≤ 1000. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each dataset, output the total score of a student with the highest total score. The total score s k of the student k is defined by s k = p 1, k + … + p m,k . Sample Input 5 2 10 20 30 40 50 15 25 35 45 55 6 3 10 20 30 15 25 35 21 34 11 52 20 18 31 15 42 10 21 19 4 2 0 0 0 0 0 0 0 0 0 0 Output for the Sample Input 105 83 0
[ { "submission_id": "aoj_1632_10882956", "code_snippet": "#include <iostream>\n#include <vector>\n\ntemplate <typename T>\nclass matrix: public std::vector< std::vector<T> >\n{\npublic:\n\tmatrix(int m = 0, int n = 0, const T& v = T()):\n\t\tstd::vector< std::vector<T> >(m, std::vector<T>(n, v)) {}\n\tint rows() const {return this->size();}\n\tint cols() const {return rows() == 0 ? 0 : this->at(0).size();}\n};\n\ntemplate <typename T>\nstd::istream& operator>>(std::istream& is, matrix<T>& M)\n{\n\tfor (int i = 0; i < M.rows(); ++i)\n\t\tfor (int j = 0; j < M.cols(); ++j)\n\t\t\tis >> M[i][j];\n\treturn is;\n}\n\nint main()\n{\n\tint m, n;\n\twhile (std::cin >> n >> m && !(m == 0 && n == 0))\n\t{\n\t\tmatrix<int> M(m, n);\n\t\tstd::cin >> M;\n\t\tint maxs = 0;\n\t\tstd::vector<int> s(n);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t{\n\t\t\tfor (int j = 0; j < m; ++j)\n\t\t\t\ts[i] += M[j][i];\n\t\t\tif (maxs < s[i])\n\t\t\t\tmaxs = s[i]; \n\t\t}\n\t\tstd::cout << maxs << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3416, "score_of_the_acc": -0.4436, "final_rank": 9 }, { "submission_id": "aoj_1632_10834785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int n, m;\n cin >> n >> m;\n if(!n) break;\n\n vector<int> total_score(n);\n\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n int score;\n cin >> score;\n total_score[j] += score;\n }\n }\n sort(total_score.rbegin(), total_score.rend());\n\n cout << total_score[0] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3448, "score_of_the_acc": -0.5038, "final_rank": 12 }, { "submission_id": "aoj_1632_10681261", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n\nint solve(){\n\tint n,m;\n\tcin>>n>>m;\n\tif(n+m==0){\n\t\treturn 0;\n\t}\n\tvector<int> score(n,0);\n\tint a;\n\tfor(int i=0;i<m;i++){\n\t\tfor(int j=0;j<n;j++){\n\t\t\tcin>>a;\n\t\t\tscore.at(j)+=a;\n\t\t}\n\t}\n\tsort(score.begin(),score.end());\n\tcout<<score.at(n-1)<<endl;\n\treturn 1;\n}\n\n#undef int\nint main(){\n\t#define int long long\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.3759, "final_rank": 8 }, { "submission_id": "aoj_1632_10659679", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nusing int128 = __int128;\nusing State = string::const_iterator;\nclass ParseError {};\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define reps(i, l, r) for(ll i = (l); i < (r); i++)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define endl \"\\n\";\nconst ll INF = LLONG_MAX / 4;\nconst ld inf = numeric_limits<long double>::max() / (ld)4;\nconst ll mod1 = 1000000007;\nconst ll mod2 = 998244353;\nconst ld pi = 3.1415926535897;\nconst ll Hash = (1ll << 61) - 1;\nll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};\nll dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};\ntemplate <typename T>\nbool chmax(T &a, const T& b) {\n\tif (a < b) { a = b; return true; }\n\treturn false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T& b) {\n\tif (a > b) { a = b; return true; }\n\treturn false;\n}\nbool solve() {\n\tll N, M; cin >> N >> M;\n\tif (N == 0 && M == 0) return false;\n\tvector<vector<ll>>P(M, vector<ll>(N));\n\trep(i, M) rep(j, N) cin >> P[i][j];\n\tll ans = 0;\n\trep(i, N) {\n\t\tll sum = 0;\n\t\trep(j, M) {\n\t\t\tsum += P[j][i];\n\t\t}\n\t\tchmax(ans, sum);\n\t}\n\tcout << ans << endl;\n\treturn true;\n}\nint main() {\n\tbool check = true;\n\twhile (check) {\n\t\tcheck = solve();\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3556, "score_of_the_acc": -0.7068, "final_rank": 16 }, { "submission_id": "aoj_1632_10658714", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n\n\nint main(){\n for(int k=0;k<100;k++){\n int n=0,m=0;\n cin>>n>>m;\n\n vector<vector<int>> p(m,vector<int>(n));\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n cin>>p[i][j];\n }\n }\n\n if(!(1<=n and n<=1000)){\n return 0;\n }else if(!(1<=m and m<=50)){\n return 0;\n }else{\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(!(0<=p[i][j] and p[i][j]<=1000)){\n return 0;\n }\n }\n }\n\n vector<int> ans(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n ans[i]+=p[j][i];\n }\n }\n\n sort(ans.begin(),ans.end());\n cout<<ans[n-1]<<endl;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -1, "final_rank": 20 }, { "submission_id": "aoj_1632_10658342", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)\n#define rever(vec) reverse(vec.begin(), vec.end())\n#define sor(vec) sort(vec.begin(), vec.end())\n#define erauni(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())\n#define vvec(vec,n,s) vector<vector<long long>> vec((ll)(n),vector<long long>((ll)(s)))\n#define vvecstr(vec,n,s) vector<vector<string>> vec((ll)(n),vector<string>((ll)(s)))\n#define vpush(vec,n) vec.push_back((ll)(n))\n#define ll long long\n#define INF (int)2e+9\n#define co(ans) cout << ans << endl\n#define cos(ans) cout << ans << \" \"\n#define con cout << endl\n#define Yes cout << \"Yes\" << endl\n#define No cout << \"No\" << endl\nvoid solve(){\n int n,m;\n cin >> n >> m;\n \n if(n==0 && m == 0){\n exit(0);\n } \n\n vector<int> vec(n);\n rep(i,m){\n rep(j,n){\n int p; cin >> p;\n vec[j] += p;\n\n }\n }\n int ans = 0;\n rep(i,n){\n ans = max(ans,vec[i]);\n }\n cout << ans << endl;\n \n}\n\nint main(){\n while(true){\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.2857, "final_rank": 3 }, { "submission_id": "aoj_1632_10658327", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x\n#define debug_1(x1) std::cerr << #x1 << \": \" << x1 << std::endl\n#define debug_2(x1, x2) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << std::endl\n#define debug_3(x1, x2, x3) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << \", \" << #x3 << \": \" << x3 << std::endl\n#define debug_4(x1, x2, x3, x4) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << \", \" << #x3 << \": \" << x3 << \", \" << #x4 << \": \" << x4 << std::endl\n#define debug_5(x1, x2, x3, x4, x5) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << \", \" << #x3 << \": \" << x3 << \", \" << #x4 << \": \" << x4 << \", \" << #x5 << \": \" << x5 << std::endl\n#ifdef _DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))(__VA_ARGS__)\n#define debug_ary(a) \\\n for (int i = 0; i < int(a.size()); i++) { \\\n std::cerr << #a << \"[\" << setw(2) << i << \"]:\" << (a)[i] << std::endl; \\\n }\n#define debug_sleep(a) sleep(a);\n#define DBK std::cerr << \"DBK\" << std::endl;\n#else\n#define debug(...)\n#define debug_ary(a)\n#define debug_sleep(a)\n#define DBK\n#endif\n\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repn(i, a, n) for (int i = a; i < (int)(n); i++)\n#define rrep(i, n) for (int i = n - 1; 0 <= i; i--)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\ntemplate <class T>\nconstexpr T inf() { return ::numeric_limits<T>::max(); }\nusing ll = long long;\n// max(int)=2*10^9\n// max(long long)=9*10^18\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n ll ans = 0;\n vector<vector<int>> a(m, vector<int>(n));\n\n rep(i, m) {\n rep(j, n) {\n cin >> a[i][j];\n }\n }\n\n rep(i, n) {\n ll num = 0;\n rep(j, m) {\n // debug(j, i, a[j][i]);\n num += a[j][i];\n }\n // debug(ans, num);\n ans = max(ans, num);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3588, "score_of_the_acc": -0.7669, "final_rank": 19 }, { "submission_id": "aoj_1632_10658298", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nconst double pi = 3.141592653589793238;\nconst int INF = 2147483647;\nconst ll INFLL = 1LL << 62;\n#define all(a) (a).begin(), (a).end()\n#define allr(a) (a).rbegin(), (a).rend()\n#define rep(i, r, n) for (int i = r; i < (int)n; i++)\n#define pb push_back\n\nvoid solve(int n, int m)\n{\n int ans = 0;\n vector<int> a(n);\n rep(i, 0, m){\n rep(j, 0, n){\n int aa;\n cin >> aa;\n a[j] += aa;\n }\n }\n\n rep(i, 0, n){\n if(ans < a[i]) ans = a[i];\n\n }\n cout << ans << endl;\n return;\n}\n\nint main()\n{\n int n, m;\n while (1)\n {\n cin >> n >> m;\n if (n == 0 && m == 0)\n {\n return 0;\n }\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.7594, "final_rank": 17 }, { "submission_id": "aoj_1632_10649165", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n exit(0);\n }\n vector<vector<int>> P(M, vector<int>(N));\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < N; j++) {\n cin >> P[i][j];\n }\n }\n int ans = 0;\n for (int i = 0; i < N; i++) {\n int sum = 0;\n for (int j = 0; j < M; j++) {\n sum += P[j][i];\n }\n ans = max(ans, sum);\n }\n cout << ans << endl;\n}\n\nint main() {\n while (1) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3532, "score_of_the_acc": -0.6617, "final_rank": 15 }, { "submission_id": "aoj_1632_10595649", "code_snippet": "#include <algorithm>\n#include <array>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\ntemplate <typename T>\nbool ChMax(T& max, T val) {\n\treturn (val > max) ? (max = val, true) : false;\n}\n\nbool Solve(void) {\n\tint n, m;\n\tcin >> n >> m;\n\tif (n == 0) return false;\n\tvector<int> sum(n);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n int p;\n cin >> p;\n sum[j] += p;\n }\n }\n\n int max = 0;\n for (int i : sum) ChMax(max, i);\n\n cout << max << endl;\n\n\treturn true;\n}\n\nint main(void) {\n\twhile (Solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3180, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1632_10595646", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nint main() {\n int n, m;\n while (cin >> n >> m) {\n if (n == 0 && m == 0) break;\n vector<int> ans(n, 0);\n vector<vector<int>> a(m, vector<int>(n));\n for(int i = 0; i < m; i++) {\n for(int j = 0; j < n; j++) {\n cin >> a[i][j];\n ans[j] += a[i][j];\n }\n }\n sort(ans.rbegin(), ans.rend());\n cout << ans[0] << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3528, "score_of_the_acc": -0.6541, "final_rank": 14 }, { "submission_id": "aoj_1632_10588401", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n vector<int> MAX(0);\n while(true){\n\n int n, m;\n \n\n cin >> n >> m;\n if(n == 0 && m == 0){\n break;\n }\n\n vector<vector<int>> score(m, vector<int>(n));\n vector<int> total(n, 0);\n for(int i=0; i<m; i++){\n \n for(int j=0; j<n; j++){\n cin >> score.at(i).at(j);\n \n }\n\n }\n\n for(int j=0; j<n; j++){\n for(int i=0; i<m; i++){\n total.at(j)+=score.at(i).at(j);\n }\n }\n \n sort(total.begin(), total.end());\n \n MAX.push_back(total.at(n-1));\n\n \n }\n for(int x=0; x<MAX.size(); x++){\n cout << MAX.at(x) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.4737, "final_rank": 10 }, { "submission_id": "aoj_1632_10588317", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\ntypedef long long ll;\n\nint main() {\n vector<int> ans;\n while(true) {\n int n, m;\n cin >> n >> m;\n\n if (n == 0 && m == 0) break;\n\n vector<int> sum(n, 0);\n int score;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n cin >> score;\n sum[j] += score;\n }\n }\n\n sort(sum.begin(), sum.end());\n ans.push_back(sum[sum.size()-1]);\n }\n\n rep(i, ans.size()) {\n cout << ans[i] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.3684, "final_rank": 7 }, { "submission_id": "aoj_1632_10588295", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n, m;\n\n while (true) {\n cin >> n >> m;\n if (n == 0 && m == 0) return 0;\n\n vector<int> score(n);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n int p;\n cin >> p;\n score[j] += p;\n }\n }\n\n int maxSum = 0;\n for (int i = 0; i < n; i++) {\n maxSum = max(maxSum, score[i]);\n }\n\n cout << maxSum << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3448, "score_of_the_acc": -0.5038, "final_rank": 12 }, { "submission_id": "aoj_1632_10588224", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)\n\nint main() {\n while (1)\n {\n int n,m,ans;\n cin >> n >> m;\n if(n==0&&m==0){\n break;\n }\n\n vector<int> po(n);\n rep(i,0,n){\n cin>> po.at(i);\n }\n rep(i,0,m-1){\n rep(j,0,n){\n int p;\n cin>>p;\n po.at(j)+=p;\n }\n }\n\n sort(po.begin(),po.end());\n ans=po.at(n-1);\n cout << ans << endl;\n \n }\n return (0);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3360, "score_of_the_acc": -0.3383, "final_rank": 4 }, { "submission_id": "aoj_1632_10588200", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while(true){\n int N, M;\n cin >> N >> M;\n if (N == 0 and M == 0) break;\n int Ans = -1;\n int Sum;\n vector<int> Score(N+1, 0);\n\n for (int i = 1; i <= M; i++) {\n for (int j = 1; j <= N; j++) {\n int Ten;\n cin >> Ten;\n Score.at(j) = Score.at(j) + Ten;\n }\n }\n\n for (int i = 1; i <= N; i++) Ans = max(Ans, Score.at(i));\n\n cout << Ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3364, "score_of_the_acc": -0.3459, "final_rank": 6 }, { "submission_id": "aoj_1632_10588198", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,m,n) for(int i=m;i<n;i++)\n#define REP(i,m,n) for(int i=m;i<=n;i++)\nusing namespace std;\nusing ll = long long;\nint main(){\n rep(l,0,100){\n int n,m;\n cin>>n>>m;\n if(n==0&&m==0)return 0;\n vector<int> p(n,0);\n rep(i,0,m)rep(j,0,n){\n int temp;\n cin>>temp;\n p[j]+=temp;\n }\n int ans=*max_element(p.begin(),p.end());\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3360, "score_of_the_acc": -0.3383, "final_rank": 4 }, { "submission_id": "aoj_1632_10588103", "code_snippet": "#include <bits/stdc++.h>\n#if __has_include(<atcoder/all>)\n #include <atcoder/all>\nusing namespace atcoder;\nusing mint = modint998244353;\n#endif\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for (ll i = 0; i < (n); ++i)\n#define rep1(i,s,n) for (ll i = s; i < n; i++)\nint main() {\n while(1){\n ll n,m;\n cin >> n >> m;\n if(n==0&&m==0){\n break;\n }\n vector<vector<ll>> p(m,vector<ll>(n));\n ll ans=0,check=0;\n rep(i,m){\n rep(j,n){\n cin >> p.at(i).at(j);\n }\n }\n rep(i,n){\n rep(j,m){\n check+=p.at(j).at(i);\n }\n ans=max(ans,check);\n check=0;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3328, "score_of_the_acc": -0.2782, "final_rank": 2 }, { "submission_id": "aoj_1632_10588049", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int n,m;\n vector<int> result;\n while(true){\n cin >> n >> m;\n if(n==0 && m==0){break;}\n vector<vector<int>> data(m,vector<int>(n));\n for(int i=0;i<m;i++){\n for(int &x:data.at(i)){\n cin >> x;\n }\n }\n int k = 0;\n for(int i=0;i<n;i++){\n int A = 0;\n for(int j=0;j<m;j++){\n A = A + data.at(j).at(i);\n }\n k = max(k,A);\n }\n result.push_back(k);\n }\n for(int x:result){\n cout << x << endl;\n } \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.7594, "final_rank": 17 }, { "submission_id": "aoj_1632_10587988", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define fr(i,n) for(int i=0; i<(n); i++)\n\nint main() {\n vector<int> ans;\n while(true){\n int n,m,mx=0;\n cin >> n >> m;\n if(n==0&m==0) break;\n\n vector<vector<int>> v(m,vector<int> (n));\n fr(i,m){\n fr(j,n){\n cin >> v[i][j];\n }\n }\n fr(i,n){\n int tmp=0;\n fr(j,m){\n tmp += v[j][i];\n }\n mx = max(mx,tmp);\n }\n ans.push_back(mx);\n }\n fr(i,ans.size()) cout << ans[i] << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.4737, "final_rank": 10 } ]
aoj_1631_cpp
For Programming Excellence A countless number of skills are required to be an excellent programmer. Different skills have different importance degrees, and the total programming competence is measured by the sum of products of levels and importance degrees of his/her skills. In this summer season, you are planning to attend a summer programming school. The school offers courses for many of such skills. Attending a course for a skill, your level of the skill will be improved in proportion to the tuition paid, one level per one yen of tuition, however, each skill has its upper limit of the level and spending more money will never improve the skill level further. Skills are not independent: For taking a course for a skill, except for the most basic course, you have to have at least a certain level of its prerequisite skill. You want to realize the highest possible programming competence measure within your limited budget for tuition fees. Input The input consists of no more than 100 datasets, each in the following format. n k h 1 ... h n s 1 ... s n p 2 ... p n l 2 ... l n The first line has two integers, n , the number of different skills between 2 and 100, inclusive, and k , the budget amount available between 1 and 10 5 , inclusive. In what follows, skills are numbered 1 through n . The second line has n integers h 1 ... h n , in which h i is the maximum level of the skill i , between 1 and 10 5 , inclusive. The third line has n integers s 1 ... s n , in which s i is the importance degree of the skill i , between 1 and 10 9 , inclusive. The fourth line has n −1 integers p 2 ... p n , in which p i is the prerequisite skill of the skill i , between 1 and i −1, inclusive. The skill 1 has no prerequisites. The fifth line has n −1 integers l 2 ... l n , in which l i is the least level of prerequisite skill p i required to learn the skill i , between 1 and h p i , inclusive. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing one integer, which is the highest programming competence measure achievable, that is, the maximum sum of the products of levels and importance degrees of the skills, within the given tuition budget, starting with level zero for all the skills. You do not have to use up all the budget. Sample Input 3 10 5 4 3 1 2 3 1 2 5 2 5 40 10 10 10 10 8 1 2 3 4 5 1 1 2 3 10 10 10 10 5 10 2 2 2 5 2 2 2 2 5 2 1 1 2 2 2 1 2 1 0 0 Output for the Sample Input 18 108 35
[ { "submission_id": "aoj_1631_10853852", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\nconst ll BIG_NUM = 2e9;\nconst ll HUGE_NUM = 99999999999999999;\nconst ll MOD = 1e9+7;\nconst double EPS = 1e-9;\n\nusing namespace std;\n\nconst int SIZE = 100005;\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\t\treturn pre_need_level < arg.pre_need_level;\n\t}\n\n\tint index;\n\tll limit,value,pre_node,pre_need_level;\n};\n\nstruct Data{\n\tvoid set(ll arg_index,ll arg_diff){\n\t\tindex = arg_index;\n\t\tdiff = arg_diff;\n\t}\n\tll index,diff;\n};\n\n\nll V,K;\nll dp[105][SIZE];\nll first[SIZE],work[SIZE];\nInfo info[SIZE];\nData DEQ[SIZE];\nvector<Info> G[SIZE];\n\nvoid calc_dp(ll *base,ll *result,ll budget,ll value, ll limit){\n\tll head = 0,tail = 0;\n\tfor(int i = 0; i <= budget; i++){\n\t\tll diff = base[i]-i*value;\n\t\twhile(head < tail && DEQ[tail-1].diff <= diff)tail--;\n\t\tDEQ[tail++].set(i,diff);\n\t\tresult[i] = DEQ[head].diff+i*value;\n\t\tif(i-DEQ[head].index == limit)head++;\n\t}\n}\n\nvoid recursive(int node_id,ll budget,ll *table){\n\tcalc_dp(table,dp[node_id],budget,info[node_id].value,info[node_id].limit);\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\tint child = G[node_id][i].index;\n\t\tif(info[child].pre_need_level > budget)continue;\n\t\t\n recursive(child,budget-info[child].pre_need_level,table);\n\t\ttable = dp[child];\n\t\t\n calc_dp(dp[child],work,budget,info[node_id].value,info[node_id].limit-info[child].pre_need_level);\n\t\t\n ll add = info[child].pre_need_level*info[node_id].value;\n\t\tfor(int k = 0; k+info[child].pre_need_level <= budget; k++){\n\t\t\tdp[node_id][k+info[child].pre_need_level] = max(dp[node_id][k+info[child].pre_need_level],work[k]+add);\n\t\t}\n\t}\n}\n\nvoid solve(){\n\tfor(int i = 0; i < V; i++){\n\t\tscanf(\"%lld\",&info[i].limit);\n\t\tinfo[i].index = i;\n\t}\n\tfor(int i = 0; i < V; i++) scanf(\"%lld\",&info[i].value);\n\tfor(int i = 1; i < V; i++){\n\t\tscanf(\"%lld\",&info[i].pre_node);\n\t\tinfo[i].pre_node--;\n\t}\n\tfor(int i = 1; i < V; i++){\n\t\tscanf(\"%lld\",&info[i].pre_need_level);\n\t\tG[info[i].pre_node].push_back(info[i]);\n\t}\n\tfor(int i = 0; i < V; i++){\n\t\tif(G[i].size() == 0)continue;\n\t\tsort(G[i].begin(),G[i].end());\n\t}\n\tfor(int i = 0; i < V; i++)\n\t\tfor(int k = 0; k <= K; k++)\n\t\t\tdp[i][k] = 0;\n\tfor(int i = 0; i <= K; i++)\tfirst[i] = 0;\n\trecursive(0,K,first);\n\tprintf(\"%lld\\n\", dp[0][K]);\n}\n\nint main(){\n\twhile(1){\n\t\tscanf(\"%lld %lld\",&V,&K);\n\t\tif(V == 0 && K == 0)break;\n\t\tfor(int i = 0; i < V; i++) G[i].clear();\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3370, "memory_kb": 89624, "score_of_the_acc": -0.102, "final_rank": 4 }, { "submission_id": "aoj_1631_10706738", "code_snippet": "#include <algorithm>\n#include <cstdio>\n#include <vector>\nusing namespace std;\nconst int N = 301;\nconst long long maxn = 1e18;\nint n, m;\nlong long f[N][100001], w[N][100001], tmp[100001];\npair<int, long long> d[100001];\nvector<int>g[N];\nint r[N], l[N], s[N];\nvoid pre_tmp(){\n for (int i = 0; i <= m; i ++) tmp[i] = -maxn;\n}\nvoid work(long long*f, int v, int s) {\n if (!s) return;\n int l = 1;\n int r = 1;\n d[1] = {0, f[0]};\n for (int i = 1; i <= m; i ++) {\n while (l <= r && i - d[l].first > s) l ++;\n d[++r] = {i, f[i] - 1ll * i * v};\n while (r - l + 1 >= 2 && d[r].second >= d[r - 1].second) {\n d[r - 1] = d[r];\n r --;\n }\n f[i] = d[l].second + 1ll * v * i;\n }\n}\nvoid dfs(int x) {\n for (auto u:g[x]) {\n pre_tmp();\n for (int i = 0; i + l[u] <= m; i ++)\n tmp[l[u] + i] = f[x][i] + 1ll * l[u] * s[x];\n work(tmp, s[x], r[x] - l[u]);\n for (int i = 0; i <= m; i ++)\n f[u][i] = max(tmp[i], w[x][i]);\n dfs(u);\n for (int i = 0; i <= m; i ++)\n w[x][i] = max(w[x][i], f[u][i]);\n }\n work(f[x], s[x], r[x]);\n for (int i = 0; i <= m; i ++)\n f[x][i] = max(f[x][i], w[x][i]);\n}\nint main() {\n while (1) {\n scanf(\"%d %d\", &n, &m);\n if (!n && !m) break;\n for (int i = 1; i <= n; i ++)\n scanf(\"%d\", &r[i]);\n for (int i = 1; i <= n; i ++)\n scanf(\"%d\", &s[i]);\n for (int i = 2; i <= n ;i ++){\n int x;\n scanf(\"%d\", &x);\n g[x].push_back(i);\n }\n for (int i = 2; i <= n; i ++)\n scanf(\"%d\", &l[i]);\n for (int i = 1; i <= n; i ++) {\n sort(g[i].begin(), g[i].end(), [](int x, int y) { return l[x] > l[y]; });\n for (int j = 0; j <= m; j ++)\n f[i][j] = w[i][j] = -maxn;\n }\n f[1][0] = 0;\n dfs(1);\n long long mx = 0;\n for (int i = 0; i <= m; i ++)mx= max(mx, f[1][i]);\n printf(\"%lld\\n\", mx);\n for (int i = 1; i <= n; i ++)\n g[i].clear();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6470, "memory_kb": 164784, "score_of_the_acc": -1.6759, "final_rank": 18 }, { "submission_id": "aoj_1631_10662280", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nusing mint = modint<998244353>;\nvector<ll>dp[100];\nvoid solve() {\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) exit(0);\n vector<int> h(n), s(n), p(n), l(n);\n rep(i, n) {\n cin >> h[i];\n chmin(h[i], k);\n }\n cin >> s;\n vector<vector<int>> g(n);\n for (int i = 1; i < n; i++) {\n cin >> p[i];\n p[i]--;\n g[p[i]].push_back(i);\n }\n for (int i = 1; i < n; i++) cin >> l[i];\n auto apply = [&](ll value, int cap, vector<ll> &DP) {\n deque<pair<int, ll>> deq;\n rep(i, k + 1) {\n while (deq.size() && deq.back().second <= DP[i] - i * value) deq.pop_back();\n deq.push_back({i, DP[i] - i * value});\n DP[i] = deq.front().second + i * value;\n if (deq.front().first <= i - cap) deq.pop_front();\n }\n //return dp;\n };\n vector<ll> pre(k + 1);\n function<void(int)> dfs = [&](int v) {\n sort(all(g[v]), [&](int u1, int u2) { return l[u1] < l[u2]; });\n dp[v]=pre;\n apply(s[v],h[v],dp[v]);\n for (int u : g[v]) {\n dfs(u);\n apply(s[v], h[v] - l[u], dp[u]);\n rep(i, k + 1 - l[u]) chmax(dp[v][i + l[u]], dp[u][i] + 1LL * s[v] * l[u]);\n }\n pre=dp[v];\n };\n dfs(0);\n cout << *max_element(all(dp[0])) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 7480, "memory_kb": 84332, "score_of_the_acc": -0.914, "final_rank": 17 }, { "submission_id": "aoj_1631_10662278", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nusing mint = modint<998244353>;\nvector<ll>dp[100];\ndeque<pair<int, ll>> deq;\nvoid solve() {\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) exit(0);\n vector<int> h(n), s(n), p(n), l(n);\n rep(i, n) {\n cin >> h[i];\n chmin(h[i], k);\n }\n cin >> s;\n vector<vector<int>> g(n);\n for (int i = 1; i < n; i++) {\n cin >> p[i];\n p[i]--;\n g[p[i]].push_back(i);\n }\n for (int i = 1; i < n; i++) cin >> l[i];\n auto apply = [&](ll value, int cap, vector<ll> &DP) {\n rep(i, k + 1) {\n while (deq.size() && deq.back().second <= DP[i] - i * value) deq.pop_back();\n deq.push_back({i, DP[i] - i * value});\n DP[i] = deq.front().second + i * value;\n if (deq.front().first <= i - cap) deq.pop_front();\n }\n deq.clear();\n //return dp;\n };\n vector<ll> pre(k + 1);\n function<void(int)> dfs = [&](int v) {\n sort(all(g[v]), [&](int u1, int u2) { return l[u1] < l[u2]; });\n dp[v]=pre;\n apply(s[v],h[v],dp[v]);\n for (int u : g[v]) {\n dfs(u);\n apply(s[v], h[v] - l[u], dp[u]);\n rep(i, k + 1 - l[u]) chmax(dp[v][i + l[u]], dp[u][i] + 1LL * s[v] * l[u]);\n }\n pre=dp[v];\n };\n dfs(0);\n cout << *max_element(all(dp[0])) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 6500, "memory_kb": 85476, "score_of_the_acc": -0.719, "final_rank": 14 }, { "submission_id": "aoj_1631_8253392", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing i64 = std::int64_t;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nconst i64 INF = std::numeric_limits<i64>::max() / 4;\n\ni64 dp[101][100010];\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n int n,k;\n while(std::cin >> n >> k, !(n == 0 && k == 0)) {\n std::vector g(n, std::vector<int>());\n std::vector<int> h(n);\n std::vector<i64> s(n);\n rep(i,0,n) {\n std::cin >> h[i];\n chmin(h[i], k);\n }\n rep(i,0,n) std::cin >> s[i];\n rep(i,1,n) {\n int p;\n std::cin >> p;\n p--;\n g[p].emplace_back(i);\n }\n std::vector<int> l(n);\n rep(i,1,n) std::cin >> l[i];\n std::vector<int> dfs_order, nxt(n);\n auto dfs1 = [&](auto &&self, int v, int nv) -> void {\n dfs_order.emplace_back(v);\n nxt[v] = nv;\n std::sort(all(g[v]), [&](int a, int b) { return l[a] > l[b]; });\n rep(i,0,g[v].size()) {\n self(self, g[v][i], (i == (int)g[v].size() - 1) ? nv : g[v][i+1]);\n }\n };\n dfs1(dfs1, 0, n);\n std::deque<int> d;\n auto f = [&](int u, int v, int l, int r) -> void {\n d.clear();\n auto tmp = dp[u];\n auto nxt = dp[v];\n rep(i,l,k+1) {\n while(!d.empty() && tmp[d.back()] - d.back() * s[u] < tmp[i - l] - (i - l) * s[u]) {\n d.pop_back();\n }\n d.push_back(i - l);\n chmax(nxt[i], tmp[d.front()] - d.front() * s[u] + i * s[u]);\n if(d.front() == i - r) d.pop_front();\n }\n };\n rep(i,0,n+1) rep(j,0,k+1) dp[i][j] = -INF;\n dp[0][0] = 0;\n for(auto u: dfs_order) {\n for(auto v: g[u]) {\n f(u, v, l[v], h[u]);\n }\n f(u, nxt[u], 0, h[u]);\n }\n i64 ans = -1;\n rep(i,0,k+1) chmax(ans, dp[n][i]);\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 4840, "memory_kb": 82556, "score_of_the_acc": -0.3296, "final_rank": 8 }, { "submission_id": "aoj_1631_8253375", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing i64 = std::int64_t;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nstruct simple_deque {\n int l = 0, r = 0;\n int d[100100];\n simple_deque() = default;\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nconst i64 INF = std::numeric_limits<i64>::max() / 4;\n\ni64 dp[101][100010];\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n int n,k;\n while(std::cin >> n >> k, !(n == 0 && k == 0)) {\n std::vector g(n, std::vector<int>());\n std::vector<int> h(n);\n std::vector<i64> s(n);\n rep(i,0,n) {\n std::cin >> h[i];\n chmin(h[i], k);\n }\n rep(i,0,n) std::cin >> s[i];\n rep(i,1,n) {\n int p;\n std::cin >> p;\n p--;\n g[p].emplace_back(i);\n }\n std::vector<int> l(n);\n rep(i,1,n) std::cin >> l[i];\n std::vector<int> dfs_order, nxt(n);\n auto dfs1 = [&](auto &&self, int v, int nv) -> void {\n dfs_order.emplace_back(v);\n nxt[v] = nv;\n std::sort(all(g[v]), [&](int a, int b) { return l[a] > l[b]; });\n rep(i,0,g[v].size()) {\n self(self, g[v][i], (i == (int)g[v].size() - 1) ? nv : g[v][i+1]);\n }\n };\n dfs1(dfs1, 0, n);\n std::deque<int> d;\n auto f = [&](int u, int v, int l, int r) -> void {\n d.clear();\n auto tmp = dp[u];\n auto nxt = dp[v];\n rep(i,l,k+1) {\n while(!d.empty() && tmp[d.back()] - d.back() * s[u] < tmp[i - l] - (i - l) * s[u]) {\n d.pop_back();\n }\n d.push_back(i - l);\n chmax(nxt[i], tmp[d.front()] - d.front() * s[u] + i * s[u]);\n if(d.front() == i - r) d.pop_front();\n }\n };\n rep(i,0,n+1) rep(j,0,k+1) dp[i][j] = -INF;\n dp[0][0] = 0;\n for(auto u: dfs_order) {\n for(auto v: g[u]) {\n f(u, v, l[v], h[u]);\n }\n f(u, nxt[u], 0, h[u]);\n }\n i64 ans = -1;\n rep(i,0,k+1) chmax(ans, dp[n][i]);\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 4800, "memory_kb": 82596, "score_of_the_acc": -0.3215, "final_rank": 7 }, { "submission_id": "aoj_1631_8253350", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing i64 = std::int64_t;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nstruct simple_deque {\n int l = 0, r = 0;\n int d[100100];\n simple_deque() = default;\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nconst i64 INF = std::numeric_limits<i64>::max() / 4;\n\ni64 dp[101][100010];\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n int n,k;\n while(std::cin >> n >> k, !(n == 0 && k == 0)) {\n std::vector g(n, std::vector<int>());\n std::vector<int> h(n);\n std::vector<i64> s(n);\n rep(i,0,n) {\n std::cin >> h[i];\n chmin(h[i], k);\n }\n rep(i,0,n) std::cin >> s[i];\n rep(i,1,n) {\n int p;\n std::cin >> p;\n p--;\n g[p].emplace_back(i);\n }\n std::vector<int> l(n);\n rep(i,1,n) std::cin >> l[i];\n std::vector<int> dfs_order, nxt(n);\n auto dfs1 = [&](auto &&self, int v, int nv) -> void {\n dfs_order.emplace_back(v);\n nxt[v] = nv;\n std::sort(all(g[v]), [&](int a, int b) { return l[a] > l[b]; });\n rep(i,0,g[v].size()) {\n self(self, g[v][i], (i == (int)g[v].size() - 1) ? nv : g[v][i+1]);\n }\n };\n dfs1(dfs1, 0, n);\n simple_deque d;\n auto f = [&](int u, int v, int l, int r) -> void {\n d.clear();\n auto tmp = dp[u];\n auto nxt = dp[v];\n rep(i,l,k+1) {\n while(!d.empty() && tmp[d.back()] - d.back() * s[u] < tmp[i - l] - (i - l) * s[u]) {\n d.pop_back();\n }\n d.push_back(i - l);\n chmax(nxt[i], tmp[d.front()] - d.front() * s[u] + i * s[u]);\n if(d.front() == i - r) d.pop_front();\n }\n };\n rep(i,0,n+1) rep(j,0,k+1) dp[i][j] = -INF;\n dp[0][0] = 0;\n for(auto u: dfs_order) {\n for(auto v: g[u]) {\n f(u, v, l[v], h[u]);\n }\n f(u, nxt[u], 0, h[u]);\n }\n i64 ans = -1;\n rep(i,0,k+1) chmax(ans, dp[n][i]);\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 3850, "memory_kb": 82704, "score_of_the_acc": -0.1203, "final_rank": 5 }, { "submission_id": "aoj_1631_7158795", "code_snippet": "#ifdef noimi\n#include <stdc++.h>\n#else\n#include <bits/stdc++.h>\n#endif\n\n#define ll long long\n\n#define REP0(n) for(ll iiii = 0; iiii < (n); i++)\n#define REP1(i, n) for(ll i = 0; i < (n); i++)\n#define REP2(i, a, b) for(ll i = (a); i < (b); i++)\n#define overload3(a, b, c, name, ...) name\n#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n\n#define vi vector<int>\nusing namespace std;\n\ntemplate <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"{\";\n rep(i, v.size()) {\n if(i) os << \", \";\n os << v[i];\n }\n return os << \"}\";\n}\nstruct S {\n int l, r;\n ll v, d;\n};\n\nostream &operator<<(ostream &os, S s) { return os << \"(\" << s.l << \", \" << s.r << \", \" << s.v << \", \" << s.d << \")\"; }\n\n// deque\nstruct DEQUE {\n int l, r;\n int d[100100];\n DEQUE() : l(0), r(0) {}\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nll dp[101][100010];\n\nint main() {\n DEQUE d;\n while(true) {\n int n, k;\n cin >> n >> k;\n if(!n) exit(0);\n\n vi h(n);\n vector<ll> s(n);\n vector<vi> g(n);\n rep(i, n) cin >> h[i], h[i] = min(h[i], k);\n rep(i, n) cin >> s[i];\n\n rep(i, 1, n) {\n int x;\n cin >> x;\n x--;\n g[x].emplace_back(i);\n }\n vi l(n);\n rep(i, 1, n) cin >> l[i];\n\n constexpr ll inf = 1e18;\n rep(i, n + 1) rep(j, k + 1) dp[i][j] = -inf;\n\n vector<int> idx, nxt(n);\n {\n auto dfs = [&](auto &&f, int x, int nv) -> void {\n idx.emplace_back(x);\n sort(begin(g[x]), end(g[x]), [&](int i, int j) { return l[i] > l[j]; });\n rep(i, g[x].size()) { f(f, g[x][i], (i == g[x].size() - 1 ? nv : g[x][i + 1])); }\n nxt[x] = nv;\n };\n dfs(dfs, 0, n);\n }\n\n dp[0][0] = 0;\n\n auto f = [&](ll *tmp, ll *nxt, ll s, ll L, ll R) {\n // rep(i, k + 1) tmp[i] -= i * s;\n d.clear();\n rep(i, L, k + 1) {\n while(!d.empty() and tmp[d.back()] - d.back() * s < tmp[i - L] - (i - L) * s) d.pop_back();\n d.push_back(i - L);\n chmax(nxt[i], tmp[d.front()] - d.front() * s + i * s);\n if(d.front() == i - R) d.pop_front();\n }\n };\n\n rep(i, n) {\n int x = idx[i];\n for(auto e : g[x]) {\n int L = l[e], R = h[x];\n f(dp[x], dp[e], s[x], L, R);\n }\n int e = idx[i + 1];\n f(dp[x], dp[nxt[x]], s[x], 0, h[x]);\n }\n\n ll ma = 0;\n rep(j, k + 1) chmax(ma, dp[n][j]);\n cout << ma << endl;\n }\n}", "accuracy": 1, "time_ms": 3300, "memory_kb": 82456, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1631_7158794", "code_snippet": "#ifdef noimi\n#include <stdc++.h>\n#else\n#include <bits/stdc++.h>\n#endif\n\n#define ll long long\n\n#define REP0(n) for(ll iiii = 0; iiii < (n); i++)\n#define REP1(i, n) for(ll i = 0; i < (n); i++)\n#define REP2(i, a, b) for(ll i = (a); i < (b); i++)\n#define overload3(a, b, c, name, ...) name\n#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n\n#define vi vector<int>\nusing namespace std;\n\ntemplate <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"{\";\n rep(i, v.size()) {\n if(i) os << \", \";\n os << v[i];\n }\n return os << \"}\";\n}\nstruct S {\n int l, r;\n ll v, d;\n};\n\nostream &operator<<(ostream &os, S s) { return os << \"(\" << s.l << \", \" << s.r << \", \" << s.v << \", \" << s.d << \")\"; }\n\n// deque\nstruct DEQUE {\n int l, r;\n int d[100100];\n DEQUE() : l(0), r(0) {}\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nll dp[101][100010];\n\nint main() {\n DEQUE d;\n while(true) {\n int n, k;\n cin >> n >> k;\n if(!n) exit(0);\n\n vi h(n), par(n);\n vector<ll> s(n);\n vector<vi> g(n);\n rep(i, n) cin >> h[i], h[i] = min(h[i], k);\n rep(i, n) cin >> s[i];\n\n rep(i, 1, n) {\n int x;\n cin >> x;\n x--;\n g[x].emplace_back(i);\n par[i] = x;\n }\n vi l(n);\n rep(i, 1, n) cin >> l[i];\n\n constexpr ll inf = 1e18;\n rep(i, n + 1) rep(j, k + 1) dp[i][j] = -inf;\n // vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -inf));\n\n vector<int> idx, nxt(n);\n {\n auto dfs = [&](auto &&f, int x, int nv) -> void {\n idx.emplace_back(x);\n sort(begin(g[x]), end(g[x]), [&](int i, int j) { return l[i] > l[j]; });\n rep(i, g[x].size()) { f(f, g[x][i], (i == g[x].size() - 1 ? nv : g[x][i + 1])); }\n nxt[x] = nv;\n };\n dfs(dfs, 0, n);\n }\n\n dp[0][0] = 0;\n idx.emplace_back(n);\n par.emplace_back(-1);\n\n auto f = [&](ll *tmp, ll *nxt, ll s, ll L, ll R) {\n // rep(i, k + 1) tmp[i] -= i * s;\n d.clear();\n rep(i, L, k + 1) {\n while(!d.empty() and tmp[d.back()] - d.back() * s < tmp[i - L] - (i - L) * s) d.pop_back();\n d.push_back(i - L);\n chmax(nxt[i], tmp[d.front()] - d.front() * s + i * s);\n if(d.front() == i - R) d.pop_front();\n }\n };\n\n rep(i, n) {\n int x = idx[i];\n for(auto e : g[x]) {\n int L = l[e], R = h[x];\n f(dp[x], dp[e], s[x], L, R);\n }\n int e = idx[i + 1];\n f(dp[x], dp[nxt[x]], s[x], 0, h[x]);\n }\n\n ll ma = 0;\n rep(j, k + 1) chmax(ma, dp[n][j]);\n cout << ma << endl;\n }\n}", "accuracy": 1, "time_ms": 3300, "memory_kb": 82676, "score_of_the_acc": -0.0027, "final_rank": 2 }, { "submission_id": "aoj_1631_7158792", "code_snippet": "#ifdef noimi\n#include <stdc++.h>\n#else\n#include <bits/stdc++.h>\n#endif\n\n#define ll long long\n\n#define REP0(n) for(ll iiii = 0; iiii < (n); i++)\n#define REP1(i, n) for(ll i = 0; i < (n); i++)\n#define REP2(i, a, b) for(ll i = (a); i < (b); i++)\n#define overload3(a, b, c, name, ...) name\n#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n\n#define vi vector<int>\nusing namespace std;\n\ntemplate <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"{\";\n rep(i, v.size()) {\n if(i) os << \", \";\n os << v[i];\n }\n return os << \"}\";\n}\nstruct S {\n int l, r;\n ll v, d;\n};\n\nostream &operator<<(ostream &os, S s) { return os << \"(\" << s.l << \", \" << s.r << \", \" << s.v << \", \" << s.d << \")\"; }\n\n// deque\nstruct DEQUE {\n int l, r;\n int d[100100];\n DEQUE() : l(0), r(0) {}\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nint main() {\n DEQUE d;\n while(true) {\n int n, k;\n cin >> n >> k;\n if(!n) exit(0);\n\n vi h(n), par(n);\n vector<ll> s(n);\n vector<vi> g(n);\n rep(i, n) cin >> h[i], h[i] = min(h[i], k);\n rep(i, n) cin >> s[i];\n\n rep(i, 1, n) {\n int x;\n cin >> x;\n x--;\n g[x].emplace_back(i);\n par[i] = x;\n }\n vi l(n);\n rep(i, 1, n) cin >> l[i];\n\n constexpr ll inf = 1e18;\n vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -inf));\n\n vector<int> idx, nxt(n);\n {\n auto dfs = [&](auto &&f, int x, int nv) -> void {\n idx.emplace_back(x);\n sort(begin(g[x]), end(g[x]), [&](int i, int j) { return l[i] > l[j]; });\n rep(i, g[x].size()) { f(f, g[x][i], (i == g[x].size() - 1 ? nv : g[x][i + 1])); }\n nxt[x] = nv;\n };\n dfs(dfs, 0, n);\n }\n\n dp[0][0] = 0;\n idx.emplace_back(n);\n par.emplace_back(-1);\n\n auto f = [&](vector<ll> &tmp, vector<ll> &nxt, ll s, ll L, ll R) {\n // rep(i, k + 1) tmp[i] -= i * s;\n d.clear();\n rep(i, L, k + 1) {\n while(!d.empty() and tmp[d.back()] - d.back() * s < tmp[i - L] - (i - L) * s) d.pop_back();\n d.push_back(i - L);\n chmax(nxt[i], tmp[d.front()] - d.front() * s + i * s);\n if(d.front() == i - R) d.pop_front();\n }\n };\n\n vector<ll> DP(k + 1, -inf);\n DP[0] = 0;\n\n rep(i, n) {\n int x = idx[i];\n for(auto e : g[x]) {\n int L = l[e], R = h[x];\n f(dp[x], dp[e], s[x], L, R);\n }\n int e = idx[i + 1];\n f(dp[x], dp[nxt[x]], s[x], 0, h[x]);\n }\n\n ll ma = 0;\n rep(j, k + 1) chmax(ma, dp[n][j]);\n cout << ma << endl;\n }\n}", "accuracy": 1, "time_ms": 4620, "memory_kb": 83812, "score_of_the_acc": -0.2979, "final_rank": 6 }, { "submission_id": "aoj_1631_7158791", "code_snippet": "#ifdef noimi\n#include <stdc++.h>\n#else\n#include <bits/stdc++.h>\n#endif\n\n#define ll long long\n\n#define REP0(n) for(ll iiii = 0; iiii < (n); i++)\n#define REP1(i, n) for(ll i = 0; i < (n); i++)\n#define REP2(i, a, b) for(ll i = (a); i < (b); i++)\n#define overload3(a, b, c, name, ...) name\n#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n\n#define vi vector<int>\nusing namespace std;\n\ntemplate <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"{\";\n rep(i, v.size()) {\n if(i) os << \", \";\n os << v[i];\n }\n return os << \"}\";\n}\nstruct S {\n int l, r;\n ll v, d;\n};\n\nostream &operator<<(ostream &os, S s) { return os << \"(\" << s.l << \", \" << s.r << \", \" << s.v << \", \" << s.d << \")\"; }\n\n// deque\nstruct DEQUE {\n int l, r;\n int d[100100];\n DEQUE() : l(0), r(0) {}\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nint main() {\n DEQUE d;\n while(true) {\n int n, k;\n cin >> n >> k;\n if(!n) exit(0);\n\n vi h(n), par(n);\n vector<ll> s(n);\n vector<vi> g(n);\n rep(i, n) cin >> h[i], h[i] = min(h[i], k);\n rep(i, n) cin >> s[i];\n\n rep(i, 1, n) {\n int x;\n cin >> x;\n x--;\n g[x].emplace_back(i);\n par[i] = x;\n }\n vi l(n);\n rep(i, 1, n) cin >> l[i];\n\n constexpr ll inf = 1e18;\n vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -inf));\n\n vector<int> idx, nxt(n);\n {\n auto dfs = [&](auto &&f, int x, int nv) -> void {\n idx.emplace_back(x);\n sort(begin(g[x]), end(g[x]), [&](int i, int j) { return l[i] > l[j]; });\n rep(i, g[x].size()) { f(f, g[x][i], (i == g[x].size() - 1 ? nv : g[x][i + 1])); }\n nxt[x] = nv;\n };\n dfs(dfs, 0, n);\n }\n\n dp[0][0] = 0;\n idx.emplace_back(n);\n par.emplace_back(-1);\n\n auto f = [&](vector<ll> tmp, vector<ll> &nxt, ll s, ll L, ll R) {\n rep(i, k + 1) tmp[i] -= i * s;\n d.clear();\n rep(i, L, k + 1) {\n while(!d.empty() and tmp[d.back()] < tmp[i - L]) d.pop_back();\n d.push_back(i - L);\n chmax(nxt[i], tmp[d.front()] + i * s);\n if(d.front() == i - R) d.pop_front();\n }\n };\n\n vector<ll> DP(k + 1, -inf);\n DP[0] = 0;\n\n rep(i, n) {\n int x = idx[i];\n for(auto e : g[x]) {\n int L = l[e], R = h[x];\n f(dp[x], dp[e], s[x], L, R);\n }\n int e = idx[i + 1];\n f(dp[x], dp[nxt[x]], s[x], 0, h[x]);\n }\n\n ll ma = 0;\n rep(j, k + 1) chmax(ma, dp[n][j]);\n cout << ma << endl;\n }\n}", "accuracy": 1, "time_ms": 5420, "memory_kb": 84748, "score_of_the_acc": -0.4799, "final_rank": 10 }, { "submission_id": "aoj_1631_7158789", "code_snippet": "#ifdef noimi\n#include <stdc++.h>\n#else\n#include <bits/stdc++.h>\n#endif\n\n#define ll long long\n\n#define REP0(n) for(ll iiii = 0; iiii < (n); i++)\n#define REP1(i, n) for(ll i = 0; i < (n); i++)\n#define REP2(i, a, b) for(ll i = (a); i < (b); i++)\n#define overload3(a, b, c, name, ...) name\n#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n\n#define vi vector<int>\nusing namespace std;\n\ntemplate <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"{\";\n rep(i, v.size()) {\n if(i) os << \", \";\n os << v[i];\n }\n return os << \"}\";\n}\nstruct S {\n int l, r;\n ll v, d;\n};\n\nostream &operator<<(ostream &os, S s) { return os << \"(\" << s.l << \", \" << s.r << \", \" << s.v << \", \" << s.d << \")\"; }\n\n// deque\nstruct DEQUE {\n int l, r;\n int d[100100];\n DEQUE() : l(0), r(0) {}\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nint main() {\n DEQUE d;\n while(true) {\n int n, k;\n cin >> n >> k;\n if(!n) exit(0);\n\n vi h(n), par(n);\n vector<ll> s(n);\n vector<vi> g(n);\n rep(i, n) cin >> h[i], h[i] = min(h[i], k);\n rep(i, n) cin >> s[i];\n\n rep(i, 1, n) {\n int x;\n cin >> x;\n x--;\n g[x].emplace_back(i);\n par[i] = x;\n }\n vi l(n);\n rep(i, 1, n) cin >> l[i];\n\n constexpr ll inf = 1e18;\n vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -inf));\n\n vector<int> idx, nxt(n);\n {\n auto dfs = [&](auto &&f, int x, int nv) -> void {\n idx.emplace_back(x);\n sort(begin(g[x]), end(g[x]), [&](int i, int j) { return l[i] > l[j]; });\n rep(i, g[x].size()) { f(f, g[x][i], (i == g[x].size() - 1 ? nv : g[x][i + 1])); }\n nxt[x] = nv;\n };\n dfs(dfs, 0, n);\n }\n\n dp[0][0] = 0;\n idx.emplace_back(n);\n par.emplace_back(-1);\n\n auto f = [&](vector<ll> tmp, vector<ll> &nxt, ll s, ll L, ll R) {\n rep(i, k + 1) tmp[i] -= i * s;\n d.clear();\n rep(i, L, k + 1) {\n while(!d.empty() and tmp[d.back()] < tmp[i - L]) d.pop_back();\n d.push_back(i - L);\n chmax(nxt[i], tmp[d.front()] + i * s);\n if(d.front() == i - R) d.pop_front();\n }\n };\n\n vector<ll> DP(k + 1, -inf);\n DP[0] = 0;\n\n rep(i, n) {\n int x = idx[i];\n for(auto e : g[x]) {\n int L = l[e], R = h[x];\n f(dp[x], dp[e], s[x], L, R);\n }\n int e = idx[i + 1];\n f(dp[x], dp[nxt[x]], s[x], 0, h[x]);\n }\n\n ll ma = 0;\n rep(i, n + 1) rep(j, k + 1) chmax(ma, dp[i][j] + (i == n ? 0 : min<int>(h[i], k - j) * s[i]));\n cout << ma << endl;\n }\n}", "accuracy": 1, "time_ms": 6070, "memory_kb": 84784, "score_of_the_acc": -0.6189, "final_rank": 12 }, { "submission_id": "aoj_1631_7158787", "code_snippet": "#ifdef noimi\n#include <stdc++.h>\n#else\n#include <bits/stdc++.h>\n#endif\n\n#define ll long long\n\n#define REP0(n) for(ll iiii = 0; iiii < (n); i++)\n#define REP1(i, n) for(ll i = 0; i < (n); i++)\n#define REP2(i, a, b) for(ll i = (a); i < (b); i++)\n#define overload3(a, b, c, name, ...) name\n#define rep(...) overload3(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n\n#define vi vector<int>\nusing namespace std;\n\ntemplate <typename T, typename S> bool chmax(T &x, const S &y) { return (x < y ? x = y, true : false); }\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"{\";\n rep(i, v.size()) {\n if(i) os << \", \";\n os << v[i];\n }\n return os << \"}\";\n}\nstruct S {\n int l, r;\n ll v, d;\n};\n\nostream &operator<<(ostream &os, S s) { return os << \"(\" << s.l << \", \" << s.r << \", \" << s.v << \", \" << s.d << \")\"; }\n\n// deque\nstruct DEQUE {\n int l, r;\n int d[100100];\n DEQUE() : l(0), r(0) {}\n bool empty() { return l == r; }\n void push_back(int x) { d[r++] = x; }\n void pop_back() { r--; }\n void pop_front() { l++; }\n void clear() { l = r = 0; }\n int back() { return d[r - 1]; }\n int front() { return d[l]; }\n};\n\nint main() {\n DEQUE d;\n while(true) {\n int n, k;\n cin >> n >> k;\n if(!n) exit(0);\n\n vi h(n), par(n);\n vector<ll> s(n);\n vector<vi> g(n);\n rep(i, n) cin >> h[i], h[i] = min(h[i], k);\n rep(i, n) cin >> s[i];\n\n rep(i, 1, n) {\n int x;\n cin >> x;\n x--;\n g[x].emplace_back(i);\n par[i] = x;\n }\n vi l(n);\n rep(i, 1, n) cin >> l[i];\n\n constexpr ll inf = 1e18;\n vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -inf));\n\n vector<int> idx, nxt(n);\n {\n auto dfs = [&](auto &&f, int x, int nv) -> void {\n idx.emplace_back(x);\n sort(begin(g[x]), end(g[x]), [&](int i, int j) { return l[i] > l[j]; });\n rep(i, g[x].size()) { f(f, g[x][i], (i == g[x].size() - 1 ? nv : g[x][i + 1])); }\n nxt[x] = nv;\n };\n dfs(dfs, 0, n);\n }\n\n dp[0][0] = 0;\n idx.emplace_back(n);\n par.emplace_back(-1);\n\n auto f = [&](vector<ll> tmp, vector<ll> &nxt, ll s, ll L, ll R) {\n rep(i, k + 1) tmp[i] -= i * s;\n d.clear();\n // deque<int> d;\n rep(i, k + 1) {\n if(i - L >= 0) {\n while(!d.empty() and tmp[d.back()] < tmp[i - L]) d.pop_back();\n d.push_back(i - L);\n }\n if(!d.empty()) chmax(nxt[i], tmp[d.front()] + i * s);\n if(!d.empty() and d.front() == i - R) d.pop_front();\n }\n // rep(i, k + 1) { rep(j, i - R, i - L + 1) if(0 <= j) chmax(nxt[i], tmp[j] + s * i); }\n };\n\n vector<ll> DP(k + 1, -inf);\n DP[0] = 0;\n\n rep(i, n) {\n int x = idx[i];\n for(auto e : g[x]) {\n int L = l[e], R = h[x];\n f(dp[x], dp[e], s[x], L, R);\n }\n int e = idx[i + 1];\n f(dp[x], dp[nxt[x]], s[x], 0, h[x]);\n }\n\n ll ma = 0;\n rep(i, n + 1) rep(j, k + 1) chmax(ma, dp[i][j] + (i == n ? 0 : min<int>(h[i], k - j) * s[i]));\n cout << ma << endl;\n }\n}", "accuracy": 1, "time_ms": 6370, "memory_kb": 84820, "score_of_the_acc": -0.6833, "final_rank": 13 }, { "submission_id": "aoj_1631_6791109", "code_snippet": "#include <queue>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nconst long long INF = (1LL << 61);\n\nclass segment {\npublic:\n\t// segment y = a(x-l) + b (l <= x <= r)\n\tlong long a, b; int l, r;\n\tsegment() : a(0LL), b(0LL), l(0), r(0) {};\n\tsegment(long long a_, long long b_, int l_, int r_) : a(a_), b(b_), l(l_), r(r_) {};\n};\n\nvector<segment> get_polyline(int K, const vector<long long>& v) {\n\tvector<segment> res;\n\tint pre = 0;\n\tfor (int i = 0; i <= K + 1; i++) {\n\t\tif (i >= 2 && (i == K + 1 || v[i] - v[i - 1] != v[i - 1] - v[i - 2])) {\n\t\t\tif (v[i - 1] != -INF && v[pre] != -INF) {\n\t\t\t\tres.push_back(segment(v[i - 1] - v[i - 2], v[pre], pre, i - 1));\n\t\t\t}\n\t\t\tpre = i;\n\t\t}\n\t\tif (v[i] != -INF && (i == 0 || v[i - 1] == -INF) && (i == K || v[i + 1] == -INF)) {\n\t\t\tres.push_back(segment(0, v[i], i, i));\n\t\t}\n\t}\n\treturn res;\n}\n\nvector<long long> max_convolve(int K, const vector<long long>& v1, const vector<long long>& v2) {\n\tvector<segment> p1 = get_polyline(K, v1);\n\tvector<segment> p2 = get_polyline(K, v2);\n\tvector<long long> res(K + 1, -INF);\n\tfor (segment s1 : p1) {\n\t\tfor (segment s2 : p2) {\n\t\t\tfor (int i = s1.l + s2.l; i <= s1.r + s2.r && i <= K; i++) {\n\t\t\t\tint d = i - (s1.l + s2.l);\n\t\t\t\tlong long x = s1.b + s2.b;\n\t\t\t\tif (s1.a > s2.a) {\n\t\t\t\t\tx += (d <= s1.r - s1.l ? s1.a * d : s1.a * (s1.r - s1.l) + s2.a * (d - (s1.r - s1.l)));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tx += (d <= s2.r - s2.l ? s2.a * d : s2.a * (s2.r - s2.l) + s1.a * (d - (s2.r - s2.l)));\n\t\t\t\t}\n\t\t\t\tres[i] = max(res[i], x);\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\n\nvector<long long> apply_skill(int K, int H, long long S, int L, const vector<long long>& v) {\n\tif (L > H) {\n\t\treturn vector<long long>(K + 1, -INF);\n\t}\n\tvector<long long> res(K + 1, -INF);\n\tdeque<pair<long long, int> > que;\n\tfor (int i = 0; i <= K; i++) {\n\t\tif (i >= L && v[i - L] != -INF) {\n\t\t\tlong long vs = v[i - L] - S * (i - L);\n\t\t\twhile (!que.empty() && que.back().first <= vs) {\n\t\t\t\tque.pop_back();\n\t\t\t}\n\t\t\tque.push_back(make_pair(vs, i - L));\n\t\t}\n\t\tif (!que.empty() && que.front().second == i - (H + 1)) {\n\t\t\tque.pop_front();\n\t\t}\n\t\tif (!que.empty()) {\n\t\t\tres[i] = max(res[i], que.front().first + S * i);\n\t\t}\n\t}\n\treturn res;\n}\n\nlong long solve(int N, int K, const vector<int>& H, const vector<long long>& S, const vector<int>& P, const vector<int>& L) {\n\tvector<vector<int> > G(N);\n\tfor (int i = 1; i < N; i++) {\n\t\tG[P[i]].push_back(i);\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tif (!G[i].empty()) {\n\t\t\tsort(G[i].begin(), G[i].end(), [&](int va, int vb) {\n\t\t\t\treturn L[va] < L[vb];\n\t\t\t});\n\t\t}\n\t}\n\tvector<vector<long long> > dp(N, vector<long long>(K + 1, -INF));\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tvector<long long> seq(K + 1, -INF);\n\t\tseq[0] = 0;\n\t\tfor (int j = 0; j < int(G[i].size()); j++) {\n\t\t\tseq = max_convolve(K, seq, dp[G[i][j]]);\n\t\t\tvector<long long> res = apply_skill(K, H[i], S[i], L[G[i][j]], seq);\n\t\t\tfor (int k = 0; k <= K; k++) {\n\t\t\t\tdp[i][k] = max(dp[i][k], res[k]);\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j <= K && j <= H[i]; j++) {\n\t\t\tdp[i][j] = max(dp[i][j], S[i] * j);\n\t\t}\n\t}\n\tlong long answer = *max_element(dp[0].begin(), dp[0].end());\n\treturn answer;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N, K;\n\t\tcin >> N >> K;\n\t\tif (N == 0 && K == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<long long> S(N);\n\t\tvector<int> H(N), P(N, -1), L(N, -1);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> H[i];\n\t\t}\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> S[i];\n\t\t}\n\t\tfor (int i = 1; i < N; i++) {\n\t\t\tcin >> P[i];\n\t\t\tP[i] -= 1;\n\t\t}\n\t\tfor (int i = 1; i < N; i++) {\n\t\t\tcin >> L[i];\n\t\t}\n\t\tlong long answer = solve(N, K, H, S, P, L);\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7340, "memory_kb": 84552, "score_of_the_acc": -0.8869, "final_rank": 16 }, { "submission_id": "aoj_1631_5968647", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c.end()\n#define si(x) int(x.size())\n#define pb push_back\n#define eb emplace_back\n#define fs first\n#define sc second\ntemplate<class T> using V = vector<T>;\ntemplate<class T> using VV = vector<vector<T>>;\ntemplate<class T,class U> bool chmax(T& x, U y){\n\tif(x<y){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T,class U> bool chmin(T& x, U y){\n\tif(y<x){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}\ntemplate<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}\ntemplate<class T>\nV<T> Vec(size_t a) {\n return V<T>(a);\n}\ntemplate<class T, class... Ts>\nauto Vec(size_t a, Ts... ts) {\n return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));\n}\ntemplate<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){\n\treturn o<<\"(\"<<p.fs<<\",\"<<p.sc<<\")\";\n}\ntemplate<class T> ostream& operator<<(ostream& o,const vector<T> &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o<<v<<\",\";\n\to<<\"}\";\n\treturn o;\n}\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\nvoid dmpr(ostream& os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" ~ \";\n\tdmpr(os,args...);\n}\n#define shows(...) cerr << \"LINE\" << __LINE__ << \" : \";dmpr(cerr,##__VA_ARGS__)\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) void(0)\n#define dump(x) void(0)\n#define shows(...) void(0)\n#endif\n\ntemplate<class D> D divFloor(D a, D b){\n\treturn a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);\n}\ntemplate<class D> D divCeil(D a, D b) {\n\treturn a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);\n}\n\nnamespace SlideMax{\n\tusing D = ll;\n\tconst int X = 1000000;\n\tD a[X];\n\tint idx[X];\n\tint L,R,I,num_del;\n\tvoid clear(){\n\t\tL = R = I = num_del = 0;\n\t}\n\tvoid add(D v){\n\t\twhile(L<R && v>a[R-1]) R--;\t\t\t// D-D comparison v<a[R-1]\n\t\ta[R] = v;\n\t\tidx[R] = I;\n\t\tR++,I++;\n\t}\n\tvoid del(){\n\t\tif(L<R && idx[L] == num_del) L++;\n\t\tnum_del++;\n\t}\n\tD getmax(){\n\t\tassert(L<R);\n\t\treturn a[L];\n\t}\n\tint getargmax(){\n\t\tassert(L<R);\n\t\treturn idx[L];\n\t}\n}\nconst ll inf = TEN(18);\nstruct Item{\n\t// 重さw, 価値p の物体が n個\n\t// n個全部使い切ると下の頂点を使えるようになる\n\tint w; ll p; int n;\n\tfriend ostream& operator<<(ostream &o,const Item& x){ return o<<\"(\"<<x.w<<\",\"<<x.p<<\" * \" << x.n << \")\";}\n};\nvoid slide(V<ll>& f, int w, ll p, int n){\n\tint W = si(f)-1;\n\trep(r,w){\n\t\tSlideMax::clear();\n\t\tif(r > W) break;\n\t\tfor(int i=0,x=r;x<=W;i++,x+=w){\n\t\t\tSlideMax::add(f[x]-i*p);\n\t\t\tf[x] = SlideMax::getmax() + i*p;\n\t\t\tif(f[x] < -inf/2) f[x] = -inf;\n\t\t\tif(i >= n) SlideMax::del();\n\t\t}\n\t}\n}\nV<ll> treeKnapsack(VV<int> G, V<Item> items, int W){\n\tassert(si(G) == si(items));\n\tauto rec = [&](auto& self, int v, int p, V<ll>& dp_init)->void{\n\t\tV<ll> dp(W+1,-inf);\n\t\tauto& f = items[v];\n\t\tfor(ll i=0; i+ll(f.w)*f.n<=W; i++){\n\t\t\tif(dp_init[i] != -inf) dp[i+f.w*f.n] = dp_init[i] + f.p * f.n;\n\t\t}\n\t\tfor(int u: G[v]) if(u != p){\n\t\t\tself(self,u,v,dp);\n\t\t}\n\t\tslide(dp_init,f.w,f.p,f.n);\t// <= n\n\t\trep(i,W+1) chmax(dp[i], dp_init[i]);\n\t\tdp_init = dp;\n\t};\n\tV<ll> dp(W+1,-inf); dp[0] = 0;\n\trec(rec,0,-1,dp); return dp;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\twhile(true){\n\t\tint N,W; cin >> N >> W;\n\t\tif(N == 0) break;\n\t\tV<int> h(N); rep(i,N) cin >> h[i];\n\t\tV<ll> p(N); rep(i,N) cin >> p[i];\n\t\tVV<int> child(N);\n\t\trep1(i,N-1){\n\t\t\tint x; cin >> x; x--;\n\t\t\tchild[x].pb(i);\n\t\t}\n\t\tV<int> need(N);\n\t\trep1(i,N-1) cin >> need[i];\n\n\t\tVV<int> G;\n\t\tV<Item> items;\n\t\tint I = 0;\n\t\tV<int> par(N,-1);\n\t\tV<int> base(N);\n\t\trep(v,N){\n\t\t\tsort(all(child[v]),[&](int l,int r){return need[l]<need[r];});\n\t\t\tint K = si(child[v]);\n\t\t\trep(k,K+1){\n\t\t\t\tint id = I++;\n\t\t\t\tint n = (k == K ? h[v] : need[child[v][k]]) - (k == 0 ? 0 : need[child[v][k-1]]);\n\t\t\t\titems.pb({1,p[v],n});\n\t\t\t\tG.pb({});\n\t\t\t\tif(k != K){\n\t\t\t\t\tpar[child[v][k]] = id;\n\t\t\t\t}\n\t\t\t\tif(k != 0){\n\t\t\t\t\tG[id-1].pb(id);\n\t\t\t\t}\n\t\t\t\tif(k == 0){\n\t\t\t\t\tbase[v] = id;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep1(v,N-1) G[par[v]].pb(base[v]);\n\t\tauto f = treeKnapsack(G,items,W);\n\t\tcout << *max_element(all(f)) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 7000, "memory_kb": 86564, "score_of_the_acc": -0.8388, "final_rank": 15 }, { "submission_id": "aoj_1631_5968639", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c.end()\n#define si(x) int(x.size())\n#define pb push_back\n#define eb emplace_back\n#define fs first\n#define sc second\ntemplate<class T> using V = vector<T>;\ntemplate<class T> using VV = vector<vector<T>>;\ntemplate<class T,class U> bool chmax(T& x, U y){\n\tif(x<y){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T,class U> bool chmin(T& x, U y){\n\tif(y<x){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}\ntemplate<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}\ntemplate<class T>\nV<T> Vec(size_t a) {\n return V<T>(a);\n}\ntemplate<class T, class... Ts>\nauto Vec(size_t a, Ts... ts) {\n return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));\n}\ntemplate<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){\n\treturn o<<\"(\"<<p.fs<<\",\"<<p.sc<<\")\";\n}\ntemplate<class T> ostream& operator<<(ostream& o,const vector<T> &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o<<v<<\",\";\n\to<<\"}\";\n\treturn o;\n}\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\nvoid dmpr(ostream& os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" ~ \";\n\tdmpr(os,args...);\n}\n#define shows(...) cerr << \"LINE\" << __LINE__ << \" : \";dmpr(cerr,##__VA_ARGS__)\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) void(0)\n#define dump(x) void(0)\n#define shows(...) void(0)\n#endif\n\ntemplate<class D> D divFloor(D a, D b){\n\treturn a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);\n}\ntemplate<class D> D divCeil(D a, D b) {\n\treturn a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);\n}\n\nnamespace SlideMax{\n\tusing D = ll;\n\tconst int X = 1000000;\n\tD a[X];\n\tint idx[X];\n\tint L,R,I,num_del;\n\tvoid clear(){\n\t\tL = R = I = num_del = 0;\n\t}\n\tvoid add(D v){\n\t\twhile(L<R && v>a[R-1]) R--;\t\t\t// D-D comparison v<a[R-1]\n\t\ta[R] = v;\n\t\tidx[R] = I;\n\t\tR++,I++;\n\t}\n\tvoid del(){\n\t\tif(L<R && idx[L] == num_del) L++;\n\t\tnum_del++;\n\t}\n\tD getmax(){\n\t\tassert(L<R);\n\t\treturn a[L];\n\t}\n\tint getargmax(){\n\t\tassert(L<R);\n\t\treturn idx[L];\n\t}\n}\nconst ll inf = TEN(18);\nstruct Item{\n\t// 重さw, 価値p の物体が n個\n\t// n個全部使い切ると下の頂点を使えるようになる\n\tint w; ll p; int n;\n\tfriend ostream& operator<<(ostream &o,const Item& x){ return o<<\"(\"<<x.w<<\",\"<<x.p<<\" * \" << x.n << \")\";}\n};\nvoid slide(V<ll>& f, int w, ll p, int n){\n\tint W = si(f)-1;\n\trep(r,w){\n\t\tSlideMax::clear();\n\t\tif(r > W) break;\n\t\tfor(int i=0,x=r;x<=W;i++,x+=w){\n\t\t\tSlideMax::add(f[x]-i*p);\n\t\t\tf[x] = SlideMax::getmax() + i*p;\n\t\t\tif(f[x] < -inf/2) f[x] = -inf;\n\t\t\tif(i >= n) SlideMax::del();\n\t\t}\n\t}\n}\nV<ll> treeKnapsack(VV<int> G, V<Item> items, int W){\n\tassert(si(G) == si(items));\n\tauto rec = [&](auto& self, int v, int p, V<ll> dp_init) -> V<ll>{\n\t\tV<ll> dp(W+1,-inf);\n\t\tauto& f = items[v];\n\t\tfor(ll i=0; i+ll(f.w)*f.n<=W; i++){\n\t\t\tif(dp_init[i] != -inf) dp[i+f.w*f.n] = dp_init[i] + f.p * f.n;\n\t\t}\n\t\tfor(int u: G[v]) if(u != p){\n\t\t\tdp = self(self,u,v,dp);\n\t\t}\n\t\tslide(dp_init,f.w,f.p,f.n);\t// <= n\n\t\trep(i,W+1) chmax(dp[i], dp_init[i]);\n\t\treturn dp;\n\t};\n\tV<ll> dp(W+1,-inf); dp[0] = 0;\n\treturn rec(rec,0,-1,dp);\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\twhile(true){\n\t\tint N,W; cin >> N >> W;\n\t\tif(N == 0) break;\n\t\tV<int> h(N); rep(i,N) cin >> h[i];\n\t\tV<ll> p(N); rep(i,N) cin >> p[i];\n\t\tVV<int> child(N);\n\t\trep1(i,N-1){\n\t\t\tint x; cin >> x; x--;\n\t\t\tchild[x].pb(i);\n\t\t}\n\t\tV<int> need(N);\n\t\trep1(i,N-1) cin >> need[i];\n\n\t\tVV<int> G;\n\t\tV<Item> items;\n\t\tint I = 0;\n\t\tV<int> par(N,-1);\n\t\tV<int> base(N);\n\t\trep(v,N){\n\t\t\tsort(all(child[v]),[&](int l,int r){return need[l]<need[r];});\n\t\t\tint K = si(child[v]);\n\t\t\trep(k,K+1){\n\t\t\t\tint id = I++;\n\t\t\t\tint n = (k == K ? h[v] : need[child[v][k]]) - (k == 0 ? 0 : need[child[v][k-1]]);\n\t\t\t\titems.pb({1,p[v],n});\n\t\t\t\tG.pb({});\n\t\t\t\tif(k != K){\n\t\t\t\t\tpar[child[v][k]] = id;\n\t\t\t\t}\n\t\t\t\tif(k != 0){\n\t\t\t\t\tG[id-1].pb(id);\n\t\t\t\t}\n\t\t\t\tif(k == 0){\n\t\t\t\t\tbase[v] = id;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep1(v,N-1) G[par[v]].pb(base[v]);\n\t\tauto f = treeKnapsack(G,items,W);\n\t\tcout << *max_element(all(f)) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 7840, "memory_kb": 164676, "score_of_the_acc": -1.9667, "final_rank": 20 }, { "submission_id": "aoj_1631_5857064", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcountll\n\n#define INF 1e16\n#define mod 1000000007\n\nvector<int> h,l,s;\nvector<int> g[300];\nvector<ll> dp[301];\nvector<ll> update;\n\nvoid knapsackDP(ll v,ll lim,ll clim,const vector<ll>& pre,vector<ll>& ret){\n deque<ll> deq,deqv;\n rep(i,clim+1){\n ll val=pre[i]-i*v;\n while(deq.size()&&deqv.back()<=val){\n deq.pop_back(); deqv.pop_back();\n }\n deq.push_back(i);\n deqv.push_back(val);\n ret[i]=deqv.front()+i*v;\n if(deq.front()==i-lim){\n deq.pop_front();\n deqv.pop_front();\n }\n }\n}\n\nvoid dfs(int v,int clim,vector<ll>& ret){\n knapsackDP(s[v],h[v],clim,ret,dp[v]);\n for(int nv : g[v]){\n dfs(nv,clim-l[nv],ret);\n ret=dp[nv];\n knapsackDP(s[v],h[v]-l[nv],clim,ret,update);\n rep(i,clim+1-l[nv])maxch(dp[v][i+l[nv]],update[i]+(ll)l[nv]*s[v]);\n }\n}\n\nint main(){\n while(1){\n int N,C;\n scanf(\"%d%d\",&N,&C);\n if(N==0)break;\n rep(i,N){\n g[i].clear();\n dp[i].resize(C+1,0);\n }\n h.resize(N,0); l.resize(N,0); s.resize(N,0); update.resize(C+1,0);\n rep(i,N)scanf(\"%d\",&h[i]);\n rep(i,N)scanf(\"%d\",&s[i]);\n rep(i,N-1){\n int p;\n scanf(\"%d\",&p); p--;\n g[p].push_back(i+1);\n }\n rep(i,N-1)scanf(\"%d\",&l[i+1]);\n rep(i,N){\n sort(all(g[i]),[&](const int& a,const int& b){ return l[a] < l[b]; });\n }\n vector<ll> ret(C+1,0);\n dfs(0,C,ret);\n cout<<dp[0][C]<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 5670, "memory_kb": 84644, "score_of_the_acc": -0.5319, "final_rank": 11 }, { "submission_id": "aoj_1631_5127311", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\n#include<queue>\n#include<stack>\n#include<map>\n#include<set>\n#include<algorithm>\n#include<functional>\n#include<cstdio>\n#include<cstdlib>\n#include<cmath>\n#include<cassert>\n#include<ctime>\nusing namespace std;\n\n#define mind(a,b) (a>b?b:a)\n#define maxd(a,b) (a>b?a:b)\n#define absd(x) (x<0?-(x):x)\n#define pow2(x) ((x)*(x))\n#define rep(i,n) for(int i=0; i<n; ++i)\n#define repr(i,n) for(int i=n-1; i>=0; --i)\n#define repl(i,s,n) for(int i=s; i<=n; ++i)\n#define replr(i,s,n) for(int i=n; i>=s; --i)\n#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)\n#define repe(e,obj) for(auto e : obj)\n\n#define SP << \" \" <<\n#define COL << \" : \" <<\n#define COM << \", \" <<\n#define ARR << \" -> \" <<\n#define PNT(STR) cout << STR << endl\n#define POS(X,Y) \"(\" << X << \", \" << Y << \")\"\n#define DEB(A) \" (\" << #A << \") \" << A\n#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << \" \"; cout << endl\n#define ALL(V) (V).begin(), (V).end()\n#define INF 1000000007\n#define INFLL 1000000000000000007LL\n#define EPS 1e-9\n\ntypedef unsigned int uint;\ntypedef unsigned long ulong;\ntypedef unsigned long long ull;\ntypedef long long ll;\ntypedef long double ld;\n#define P_TYPE int\ntypedef pair<P_TYPE, P_TYPE> P;\ntypedef pair<P, P_TYPE> PI;\ntypedef pair<P_TYPE, P> IP;\ntypedef pair<P, P> PP;\ntypedef priority_queue<P, vector<P>, greater<P> > pvqueue;\n\n#define N 102\n#define K 100008\n\nll dp[N][K];\nll ss[N];\nint hs[N], ls[N];\nvector<int> g[N];\nll ps[K], qs[K];\n\nint n, k;\n\nvoid calc(int v, int w, int mi, int ma) {\n ll *p0 = dp[v], *p1 = dp[w];\n int d = (ma - mi);\n ll sv = ss[v];\n int s = 0, t = 0;\n rep(i, k-mi+1) {\n ll v0 = p0[i] + sv*(mi - i);\n while(s < t && qs[t-1] <= v0) --t;\n ps[t] = i; qs[t] = v0;\n ++t;\n\n p1[i+mi] = max(p1[i+mi], qs[s] + sv*i);\n\n if(s < t && ps[s] <= i-d) ++s;\n }\n}\n\nstack<int> st;\nvoid dfs(int v) {\n while(!st.empty()) {\n int u = st.top(); st.pop();\n calc(u, v, 0, hs[u]);\n }\n sort(ALL(g[v]), [](int i, int j)->bool{ return ls[i] > ls[j];});\n for(int w : g[v]) {\n calc(v, w, ls[w], hs[v]);\n dfs(w);\n }\n st.push(v);\n}\n\nint main() {\n while(cin >> n >> k && n && k) {\n rep(i, n) cin >> hs[i];\n rep(i, n) cin >> ss[i];\n rep(i, n) g[i].clear();\n rep(i, n-1) {\n int j; cin >> j;\n g[j-1].push_back(i+1);\n }\n rep(i, n-1) cin >> ls[i+1];\n rep(i, n+1) rep(j, k+1) dp[i][j] = -INFLL;\n dp[0][0] = 0;\n\n dfs(0);\n while(!st.empty()) {\n int u = st.top(); st.pop();\n calc(u, n, 0, hs[u]);\n }\n ll ans = 0;\n rep(i, k+1) ans = max(ans, dp[n][i]);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3410, "memory_kb": 83608, "score_of_the_acc": -0.0374, "final_rank": 3 }, { "submission_id": "aoj_1631_4966282", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define loop(i, a, b) for (int i = (a); i < (b); i++)\n#define pb push_back\n#define eb emplace_back\n#define all(v) (v).begin(), (v).end()\n#define fi first\n#define se second\n\nusing vint = vector<int>;\nusing pint = pair<int, int>;\nusing vpint = vector<pint>;\n\ntemplate <typename A, typename B>\ninline void chmin(A& a, B b) {\n if (a > b) a = b;\n}\ntemplate <typename A, typename B>\ninline void chmax(A& a, B b) {\n if (a < b) a = b;\n}\n\ntemplate <class A, class B>\nostream& operator<<(ostream& ost, const pair<A, B>& p) {\n ost << \"{\" << p.first << \",\" << p.second << \"}\";\n return ost;\n}\n\ntemplate <class T>\nostream& operator<<(ostream& ost, const vector<T>& v) {\n ost << \"{\";\n for (int i = 0; i < v.size(); i++) {\n if (i) ost << \",\";\n ost << v[i];\n }\n ost << \"}\";\n return ost;\n}\n\ninline int topbit(unsigned long long x) {\n return x ? 63 - __builtin_clzll(x) : -1;\n}\n\ninline int popcount(unsigned long long x) {\n return __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x) {\n return __builtin_parity(x);\n}\n\ntemplate <typename F>\nclass\n FixPoint : private F {\n public:\n explicit constexpr FixPoint(F&& f) noexcept\n : F{std::forward<F>(f)} {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n};\n\nnamespace {\ntemplate <typename F>\ninline constexpr decltype(auto)\nmakeFixPoint(F&& f) noexcept {\n return FixPoint<F>{std::forward<F>(f)};\n}\n} // namespace\n\nconst int INF = 1001001001001001001;\n\nsigned main() {\n auto solve = [](int N, int K) {\n vector<int> H(N), S(N);\n rep(i, N) scanf(\"%lld\", &H[i]);\n rep(i, N) scanf(\"%lld\", &S[i]);\n\n vector<int> P(N), L(N);\n loop(i, 1, N) scanf(\"%lld\", &P[i]), P[i]--;\n loop(i, 1, N) scanf(\"%lld\", &L[i]);\n\n vector<vpint> G(N);\n loop(i, 1, N) G[P[i]].eb(L[i], i);\n\n /*\n Xを初期配列として,vのitemをl個以上使う場合のナップサックをやる\n */\n auto calc = [&](const vint& X, int l, int v) {\n vint Z(K + 1, -INF);\n deque<pint> deq;\n for (int i = l; i <= K; i++) {\n int tmp = X[i - l] - (i - l) * S[v];\n while (deq.size() && deq.back().fi <= tmp) deq.pop_back();\n deq.emplace_back(tmp, i - l);\n Z[i] = deq.front().fi + i * S[v];\n if (deq.front().se == i - H[v]) deq.pop_front();\n }\n return Z;\n };\n\n auto dfs = makeFixPoint([&](auto dfs, int v, int p, vint& X) -> void {\n sort(all(G[v]));\n reverse(all(G[v]));\n vint Y(K + 1, -INF);\n\n for (auto& e : G[v]) {\n auto [l, u] = e;\n auto Z = calc(X, l, v);\n rep(i, K + 1) chmax(Y[i], Z[i]);\n dfs(u, v, Y);\n }\n X = calc(X, 0, v);\n rep(i, K + 1) chmax(X[i], Y[i]);\n });\n\n vint X(K + 1, -INF);\n X[0] = 0;\n dfs(0, -1, X);\n printf(\"%lld\\n\", *max_element(all(X)));\n };\n\n int N, K;\n while (scanf(\"%lld%lld\", &N, &K), N || K) solve(N, K);\n return 0;\n}", "accuracy": 1, "time_ms": 7990, "memory_kb": 161944, "score_of_the_acc": -1.9655, "final_rank": 19 }, { "submission_id": "aoj_1631_4259620", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 100005\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn pre_need_level < arg.pre_need_level;\n\t}\n\n\tint index;\n\tll limit,value,pre_node,pre_need_level;\n};\n\nstruct Data{\n\n\tvoid set(ll arg_index,ll arg_diff){\n\t\tindex = arg_index;\n\t\tdiff = arg_diff;\n\t}\n\tll index,diff;\n};\n\n\nll V,K;\nll dp[105][SIZE];\nll first[SIZE],work[SIZE];\nInfo info[SIZE];\nData DEQ[SIZE];\nvector<Info> G[SIZE];\n\n\nvoid calc_dp(ll *base,ll *result,ll budget,ll value, ll limit){\n\n\tll head = 0,tail = 0;\n\n\tfor(int i = 0; i <= budget; i++){\n\n\t\tll diff = base[i]-i*value;\n\n\t\twhile(head < tail && DEQ[tail-1].diff <= diff)tail--;\n\n\t\tDEQ[tail++].set(i,diff);\n\n\t\tresult[i] = DEQ[head].diff+i*value;\n\n\t\tif(i-DEQ[head].index == limit)head++;\n\t}\n}\n\n\nvoid recursive(int node_id,ll budget,ll *table){\n\n\tcalc_dp(table,dp[node_id],budget,info[node_id].value,info[node_id].limit);\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\n\t\tint child = G[node_id][i].index;\n\n\t\tif(info[child].pre_need_level > budget)continue;\n\n\t\trecursive(child,budget-info[child].pre_need_level,table);\n\n\t\ttable = dp[child];\n\n\t\tcalc_dp(dp[child],work,budget,info[node_id].value,info[node_id].limit-info[child].pre_need_level);\n\n\t\tll add = info[child].pre_need_level*info[node_id].value;\n\n\t\tfor(int k = 0; k+info[child].pre_need_level <= budget; k++){\n\n\t\t\tdp[node_id][k+info[child].pre_need_level] = max(dp[node_id][k+info[child].pre_need_level],work[k]+add);\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < V; i++){\n\n\t\tscanf(\"%lld\",&info[i].limit);\n\t\tinfo[i].index = i;\n\t}\n\tfor(int i = 0; i < V; i++){\n\n\t\tscanf(\"%lld\",&info[i].value);\n\t}\n\tfor(int i = 1; i < V; i++){\n\n\t\tscanf(\"%lld\",&info[i].pre_node);\n\t\tinfo[i].pre_node--;\n\t}\n\tfor(int i = 1; i < V; i++){\n\n\t\tscanf(\"%lld\",&info[i].pre_need_level);\n\t\tG[info[i].pre_node].push_back(info[i]);\n\t}\n\n\tfor(int i = 0; i < V; i++){\n\t\tif(G[i].size() == 0)continue;\n\n\t\tsort(G[i].begin(),G[i].end());\n\t}\n\n\tfor(int i = 0; i < V; i++){\n\t\tfor(int k = 0; k <= K; k++){\n\t\t\tdp[i][k] = 0;\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= K; i++){\n\n\t\tfirst[i] = 0;\n\t}\n\n\trecursive(0,K,first);\n\n\tprintf(\"%lld\\n\", dp[0][K]);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%lld %lld\",&V,&K);\n\t\tif(V == 0 && K == 0)break;\n\n\t\tfor(int i = 0; i < V; i++)G[i].clear();\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5020, "memory_kb": 86868, "score_of_the_acc": -0.4203, "final_rank": 9 } ]
aoj_1633_cpp
On-Screen Keyboard You are to input a string with an OSK (on-screen keyboard). A remote control with five buttons, four arrows and an OK (Fig. B-1), is used for the OSK. Find the minimum number of button presses required to input a given string with the given OSK. Fig. B-1 Remote control Fig. B-2 An on-screen keyboard Character to input Move of highlighted cells Button presses I →,→,→,→,→,→,→,→,OK (9 presses) C ←,←,←,←,←,←,OK (7 presses) P ↓,→,→,→,→,OK (6 presses) C ↑,←,←,←,←,OK (6 presses) Fig. B-3 The minimum steps to input “ ICPC ” with the OSK in Fig. B-2 The OSK has cells arranged in a grid, each of which has a character in it or is empty. No two of the cells have the same character. One of the cells of the OSK is highlighted, and pressing the OK button will input the character in that cell, if the cell is not empty. Initially, the cell at the top-left corner is highlighted. Pressing one of the arrow buttons will change the highlighted cell to one of the adjacent cells in the direction of the arrow. When the highlighted cell is on an edge of the OSK, pushing the arrow button with the direction to go out of the edge will have no effect. For example, using the OSK with its arrangement shown in Fig. B-2, a string “ ICPC ” can be input with 28 button presses as shown in Fig. B-3, which is the minimum number of presses. Characters in cells of the OSKs are any of a lowercase letter (‘ a ’, ‘ b ’, ..., ‘ z ’), an uppercase letter (‘ A ’, ‘ B ’, ..., ‘ Z ’), a digit (‘ 0 ’, ‘ 1 ’, ..., ‘ 9 ’), a comma (‘ , ’), a hyphen (‘ - ’), a dot (‘ . ’), a slash (‘ / ’), a colon (‘ : ’), a semicolon (‘ ; ’), or an at sign (‘ @ ’). Input The input consists of at most 100 datasets, each in the following format. h w r 1 ... r h s The two integers h and w in the first line are the height and the width of the OSK, respectively. They are separated by a space, and satisfy 1 ≤ h ≤ 50 and 1 ≤ w ≤ 50. Each of the next h lines gives a row of the OSK. The i -th row, r i is a string of length w . The characters in the string corresponds to the characters in the cells of the i -th row of the OSK or an underscore (‘ _ ’) indicating an empty cell, from left to right. The given OSK satisfies the conditions stated above. The next line is a string s to be input. Its length is between 1 and 1000, inclusive. All the characters in s appear in the given OSK. Note that s does not contain underscores. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing an integer indicating the minimum number of button presses required to input the given string with the given OSK. Sample Input 3 9 ABCDEFGHI JKLMNOPQR STUVWXYZ_ ICPC 5 11 ___________ ____A______ ________M__ ___________ _C_________ ACM 4 21 1_2_3_4_5_6_7_8_9_0_- QqWwEeRrTtYyUuIiOoPp@ AaSsDdFfGgHhJjKkLl;_: ZzXxCcVvBbNnMm,_._/__ ICPC2019,AsiaYokohamaRegiona ...(truncated)
[ { "submission_id": "aoj_1633_10897969", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e5+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nconst int dr[] = {-1,0,1,0}, dc[] = {0,1,0,-1};\nvoid setIO(string);\nvoid solve(){\n \n int n,m;\n cin>>n>>m;\n while(n != 0) {\n map<char, int> save;\n vector<vector<ll>> dist(n*m, vector<ll>(n*m,INF));\n for(int i = 0; i < n; i++) {\n string s;\n cin>>s;\n for(int j = 0; j < m; j++) {\n if(s[j] == '_') continue;\n save[s[j]] = m * i + j;\n }\n }\n\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n queue<pi> Q;\n int now = m * i + j;\n dist[now][now] = 0;\n Q.push(make_pair(i,j));\n while(!Q.empty()) {\n int ur = Q.front().first, uc = Q.front().second;\n //cerr << ur << \" I:J \" << i << \" : \" << j << \" \" << uc << nl;\n Q.pop();\n for(int k = 0; k < 4; k++) {\n int vr = ur + dr[k] , vc = uc + dc[k]; \n if(vr < 0 || vc < 0 || vr >= n || vc >= m || dist[now][m * vr + vc] != INF) continue;\n dist[now][m * vr + vc] = dist[now][m * ur + uc] + 1; \n Q.push(make_pair(vr,vc));\n }\n }\n }\n }\n string s; cin>>s;\n ll ans = s.size(); \n int cur = 0;\n for(auto &x : s) {\n ans += dist[cur][save[x]];\n cur = save[x];\n }\n cout << ans << nl;\n cin>>n>>m;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 51988, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1633_10846502", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int h, w;\n cin >> h >> w;\n if(!h) break;\n\n vector<string> keyboard(h);\n for(auto& i:keyboard) cin >> i;\n\n string target;\n cin >> target;\n\n int ans = 0;\n pair<int,int> locate = {0,0}; //{縦, 横}\n\n for(auto i:target){\n pair<int,int> goal; //{縦, 横}\n for(int j=0;j<h;j++){\n for(int k=0;k<w;k++){\n if(keyboard[j][k] == i){\n goal = {j,k};\n }\n }\n }\n ans += abs(locate.first-goal.first) + abs(locate.second-goal.second)+1;\n locate = goal;\n }\n\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3468, "score_of_the_acc": -0.0111, "final_rank": 13 }, { "submission_id": "aoj_1633_10681347", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n\nint solve(){\n\tint h,w;\n\tcin>>h>>w;\n\tif(h+w==0){\n\t\treturn 0;\n\t}\n\tvector<vector<char>> board(h,vector<char>(w,'.'));\n\tfor(int i=0;i<h;i++){\n\t\tfor(int j=0;j<w;j++){\n\t\t\tcin>>board.at(i).at(j);\n\t\t}\n\t}\n\tstring s;\n\tcin>>s;\n\tint x=0,y=0;\n\tint ans=0;\n\tfor(int k=0;k<s.size();k++){\n\t\tfor(int i=0;i<h;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tif(board.at(i).at(j)==s.at(k)){\n\t\t\t\t\tans+=abs(i-x)+abs(j-y);\n\t\t\t\t\tx=i,y=j;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n\tcout<<ans+s.size()<<endl;\n\treturn 1;\n}\n\n#undef int\nint main(){\n\t#define int long long\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3372, "score_of_the_acc": -0.0092, "final_rank": 11 }, { "submission_id": "aoj_1633_10673586", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Init {\n Init() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << setprecision(13);\n }\n} init;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing VLL = vector<ll>;\nusing VVLL = vector<VLL>;\nusing PLL = pair<ll, ll>;\nusing VS = vector<string>;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n#define FOR(i, a, b) for (int i = int(a); i < int(b); ++i)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) x.rbegin(), x.rend()\n\nconst double PI = 3.141592653589793238;\nconst int INF = 1073741823;\nconst ll INFLL = 1LL << 60;\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++)\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (T& in : v)\n is >> in;\n return is;\n}\nvoid print() {\n cout << '\\n';\n}\ntemplate <typename T>\nvoid print(const T& t) {\n cout << t << '\\n';\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head& head, const Tail&... tail) {\n cout << head << ' ';\n print(tail...);\n}\ntemplate <typename T1, typename T2>\ninline bool chmax(T1& a, T2 b) {\n bool compare = a < b;\n if (compare)\n a = b;\n return compare;\n}\ntemplate <typename T1, typename T2>\ninline bool chmin(T1& a, T2 b) {\n bool compare = a > b;\n if (compare)\n a = b;\n return compare;\n}\n\nint main() {\n while (true) {\n int h, w;\n string s;\n cin >> h >> w;\n if (h == 0 && w == 0)\n return 0;\n vector<vector<char>> r(h, vector<char>(w));\n for (int i = 0; i < h; i++)\n for (int j = 0; j < w; j++)\n cin >> r.at(i).at(j);\n cin >> s;\n\n long long ans = 0;\n int now_y = 0, now_x = 0;\n for (char c : s) { // cが入力しようとしている文字\n\n long long min_dist = 1e5;\n int min_y = 0, min_x = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (r.at(i).at(j) != c)\n continue;\n int dist = abs(i - now_y) + abs(j - now_x) + 1;\n if (min_dist > dist) {\n min_dist = dist;\n min_y = i;\n min_x = j;\n }\n }\n }\n ans += min_dist;\n now_y = min_y;\n now_x = min_x;\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3432, "score_of_the_acc": -0.0187, "final_rank": 14 }, { "submission_id": "aoj_1633_10673363", "code_snippet": "#include<bits/extc++.h>\n#define rep(i,n) for(int i=0; i< (n); i++)\n#define all(a) begin(a),end(a)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 998244353;\nconst int inf = (1<<30);\nconst ll INF = (1ull<<62);\nconst int dx[8] = {1,0,-1,0,1,1,-1,-1};\nconst int dy[8] = {0,1,0,-1,1,-1,1,-1};\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\n\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout.setf(ios_base::fixed);\n cout.precision(12);\n while (true){\n int h, w;\n cin >> h >> w;\n if (h == 0) break;\n vector<string> r(h);\n for (int i = 0; i < h; i++){\n cin >> r[i];\n }\n map<pair<char, char>, int> mp;\n for (int i = 0; i < h; i++){\n for (int j = 0; j < w; j++){\n for (int k = 0; k < h; k++){\n for (int l = 0; l < w; l++){\n mp[make_pair(r[i][j], r[k][l])] = abs(i - k) + abs(j - l);\n }\n }\n }\n }\n string s;\n cin >> s;\n int ans = s.size();\n for (int i = 0; i < h; i++){\n for (int j = 0; j < w; j++){\n if (r[i][j] == s[0]){\n ans += i + j;\n }\n }\n }\n for (int i = 0; i < (int) s.size() - 1; i++){\n ans += mp[make_pair(s[i], s[i + 1])];\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3656, "score_of_the_acc": -0.8828, "final_rank": 19 }, { "submission_id": "aoj_1633_10673346", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\nusing lint = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int h, w;\n while (1) {\n in(h, w);\n if (h == 0 and w == 0) break;\n unsigned char b = '_';\n vector a(h, vector(w, b));\n in(a);\n\n vector d(256, vector(256, -1));\n if (a[0][0] == '_') a[0][0] = '%';\n rep(y1, h) rep(x1, w) rep(y2, h) rep(x2, w) {\n if (a[y1][x1] == '_' or a[y2][x2] == '_') continue;\n unsigned char c1 = a[y1][x1];\n unsigned char c2 = a[y2][x2];\n d[c1 - '\\0'][c2 - '\\0'] = abs(y1 - y2) + abs(x1 - x2) + 1;\n d[c2 - '\\0'][c1 - '\\0'] = abs(y1 - y2) + abs(x1 - x2) + 1;\n }\n string s;\n in(s);\n\n int n = s.size();\n unsigned char cur = a[0][0];\n lint res = 0;\n rep(i, n) {\n int nex = s[i] - '\\0';\n res += d[cur][nex];\n cur = nex;\n }\n out(res);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3968, "score_of_the_acc": -0.0462, "final_rank": 15 }, { "submission_id": "aoj_1633_10658368", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x\n#define debug_1(x1) std::cerr << #x1 << \": \" << x1 << std::endl\n#define debug_2(x1, x2) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << std::endl\n#define debug_3(x1, x2, x3) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << \", \" << #x3 << \": \" << x3 << std::endl\n#define debug_4(x1, x2, x3, x4) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << \", \" << #x3 << \": \" << x3 << \", \" << #x4 << \": \" << x4 << std::endl\n#define debug_5(x1, x2, x3, x4, x5) std::cerr << #x1 << \": \" << x1 << \", \" << #x2 << \": \" << x2 << \", \" << #x3 << \": \" << x3 << \", \" << #x4 << \": \" << x4 << \", \" << #x5 << \": \" << x5 << std::endl\n#ifdef _DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))(__VA_ARGS__)\n#define debug_ary(a) \\\n for (int i = 0; i < int(a.size()); i++) { \\\n std::cerr << #a << \"[\" << setw(2) << i << \"]:\" << (a)[i] << std::endl; \\\n }\n#define debug_sleep(a) sleep(a);\n#define DBK std::cerr << \"DBK\" << std::endl;\n#else\n#define debug(...)\n#define debug_ary(a)\n#define debug_sleep(a)\n#define DBK\n#endif\n\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repn(i, a, n) for (int i = a; i < (int)(n); i++)\n#define rrep(i, n) for (int i = n - 1; 0 <= i; i--)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\ntemplate <class T>\nconstexpr T inf() { return ::numeric_limits<T>::max(); }\nusing ll = long long;\n// max(int)=2*10^9\n// max(long long)=9*10^18\n\nint main() {\n while (true) {\n int h, w;\n cin >> h >> w;\n ll ans = 0;\n string str = \"\";\n // 初期の座標\n ll x = 0, y = 0;\n vector<vector<char>> key(h, vector<char>(w));\n if (h == 0 && w == 0) {\n break;\n }\n rep(i, h) {\n rep(j, w) {\n cin >> key[i][j];\n }\n }\n cin >> str;\n bool flag = false;\n rep(i, (ll)str.size()) {\n rep(j, h) {\n rep(k, w) {\n if (str[i] == key[j][k]) {\n ans += abs(x - j) + abs(y - k) + 1;\n x = j;\n y = k;\n flag = true;\n break;\n }\n }\n if (flag == true) {\n break;\n }\n }\n flag = false;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3360, "score_of_the_acc": -0.0007, "final_rank": 1 }, { "submission_id": "aoj_1633_10588808", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<string>\n#include<queue>\nusing namespace std;\n\n#define rep(i, a, b) for(int i = a; i < (b); ++i)\n#define all(x) begin(x), end(x)\n#define sz(x) (int)(x).size()\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\nbool solve() {\n\tint H, W; cin >> H >> W;\n\tif (H == 0)return 0;\n\n\tvector<string>R(H);\n\trep(i, 0, H)cin >> R[i];\n\tstring S; cin >> S;\n\t\n\tconst int inf = 100100100;\n\tvector<vector<int>> dp(H, vector<int>(W, inf));\n\tdp[0][0] = 0;\n\n\tfor (auto tar : S) {\n\n\t\tvector<vector<int>> old(H, vector<int>(W, inf));\n\t\tswap(old, dp);\n\t\tqueue<pii>q;\n\t\trep(i, 0, H)rep(j, 0, W) if (old[i][j] < inf) {\n\t\t\tq.emplace(i, j);\n\t\t\tdp[i][j] = old[i][j];\n\t\t}\n\t\twhile (!q.empty()) {\n\t\t\tint i = q.front().first;\n\t\t\tint j = q.front().second;\n\t\t\tq.pop();\n\t\t\tconst int di[] = { 0,0,-1,1 };\n\t\t\tconst int dj[] = { -1,1,0,0 };\n\t\t\trep(d, 0, 4) {\n\t\t\t\tint ni = i + di[d];\n\t\t\t\tint nj = j + dj[d];\n\t\t\t\tif (ni < 0 || ni >= H || nj < 0 || nj >= W)continue;\n\t\t\t\tif (dp[ni][nj] > dp[i][j] + 1) {\n\t\t\t\t\tdp[ni][nj] = dp[i][j] + 1;\n\t\t\t\t\tq.emplace(ni, nj);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(i, 0, H)rep(j, 0, W) \n\t\t\tif (R[i][j] != tar) dp[i][j] = inf;\n\n\t}\n\n\tint ans = inf;\n\trep(i, 0, H)rep(j, 0, W)ans = min(ans, dp[i][j]);\n\tcout << ans+sz(S) << endl;\n\treturn 1;\n}\nint main() {\n\twhile(solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 3584, "score_of_the_acc": -0.3524, "final_rank": 18 }, { "submission_id": "aoj_1633_10588399", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\ntypedef long long ll;\n\nint main() {\n vector<int> ans;\n while(true) {\n int h, w;\n cin >> h >> w;\n\n if (h == 0 && w == 0) break;\n\n\n vector<vector<char>> key(h, vector<char>(w));\n\n rep(i, h) {\n rep(j, w) {\n cin >> key[i][j];\n }\n }\n\n string s;\n cin >> s;\n\n\n int step = 0;\n ll sum = 0; \n pair<int, int> p(0, 0);\n rep(k, s.size()) {\n bool found_char_for_s_k = false;\n rep(i, h) {\n rep(j, w) {\n if (key[i][j] == s[k]) {\n step = abs(p.first - i) + abs(p.second - j);\n p.first = i;\n p.second = j;\n sum += step+1;\n found_char_for_s_k = true;\n break; \n }\n }\n if (found_char_for_s_k) {\n break;\n }\n }\n }\n\n ans.push_back(sum);\n }\n\n rep(i, ans.size()) {\n cout << ans[i] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": -0.0009, "final_rank": 2 }, { "submission_id": "aoj_1633_10588345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nint main(){\n while(1){\n int h,w;\n string s;\n cin>>h>>w;\n if(h==0&&w==0) return 0;\n vector<vector<char>> key(h,vector<char>(w));\n rep(i,h){\n rep(j,w){\n cin>>key[i][j];\n }\n }\n cin>>s;\n int curX=0,curY=0;\n int ans=0;\n rep(i,s.size()){\n bool done=false;\n queue<int> A;\n vector<vector<int>> dist(h,vector<int>(w,-1));\n A.push(curX);A.push(curY);\n dist[curX][curY]=0;\n while(!done){\n int x = A.front();A.pop();\n int y = A.front();A.pop();\n if(key[x][y]==s[i]){\n done=true;\n ans+=dist[x][y]+1;\n curX=x;\n curY=y;\n break;\n }\n int dt=dist[x][y];\n if(x-1>=0){\n if(dist[x-1][y]<0) {\n A.push(x-1);A.push(y);dist[x-1][y]=dt+1;\n }\n }\n if(x+1<h){\n if(dist[x+1][y]<0){\n A.push(x+1);A.push(y);dist[x+1][y]=dt+1;\n }\n }\n if(y-1>=0){\n if(dist[x][y-1]<0){\n A.push(x);A.push(y-1);dist[x][y-1]=dt+1;\n }\n }\n if(y+1<w){\n if(dist[x][y+1]<0){\n A.push(x);A.push(y+1);dist[x][y+1]=dt+1;\n }\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3444, "score_of_the_acc": -0.1429, "final_rank": 16 }, { "submission_id": "aoj_1633_10588341", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n vector<int> result;\n int h,w;\n string s;\n while(true){\n cin >> h >> w;\n if(h == 0 && w == 0){break;}\n vector<string> keybord(h);\n for(int i=0;i<h;i++){cin >> keybord.at(i);}\n cin >> s;\n pair<int,int> iti(0,0);\n int kyori = 0;\n int A = 0;\n int B = 0;\n for(int u=0;u<s.size();u++){\n char k = s.at(u);\n for(int i=0;i<h;i++){\n int P = 0;\n for(int j=0;j<w;j++){\n if(k == keybord.at(i).at(j)){\n A = abs(iti.first - i);\n B = abs(iti.second - j);\n kyori = kyori + A + B + 1;\n iti.first = i;\n iti.second = j;\n P = 1;\n break;\n }\n }\n if(P == 1){break;}\n }\n }\n result.push_back(kyori);\n }\n for(int x:result){\n cout << x << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3392, "score_of_the_acc": -0.0013, "final_rank": 6 }, { "submission_id": "aoj_1633_10588338", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int h, w;\n vector<int> result;\n while (true) {\n cin >> h >> w;\n if (h == 0 && w == 0) break;\n\n vector<vector<char>> keybord(h, vector<char>(w));\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n char c;\n cin >> c;\n keybord[i][j] = c;\n }\n }\n\n string str;\n cin >> str;\n\n int ans = 0;\n int posI = 0;\n int posJ = 0;\n for (char const& c : str) {\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (c == keybord[i][j]) {\n ans += abs(posI - i) + abs(posJ - j) + 1;\n posI = i;\n posJ = j;\n i = h;\n break;\n }\n }\n }\n }\n\n result.push_back(ans);\n }\n\n for (int const& a : result) {\n cout << a << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": -0.0009, "final_rank": 2 }, { "submission_id": "aoj_1633_10588331", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)\n\nint main() {\n while(1){\n int n,m;\n cin>> n >>m;\n if(n==0&&m==0){\n break;\n }\n vector<vector<char>> key(n,vector<char>(m));\n rep(i,0,n){\n rep(j,0,m){\n cin>> key[i][j];\n }\n }\n string s;\n cin >> s;\n int x=0,y=0,ans=0;\n rep(i,0,s.size()){\n int flag=0;\n rep(j,0,n){\n rep(k,0,m){\n if(key[j][k]==s.at(i)){\n ans+=abs(k-x);\n ans+=abs(j-y);\n ans++;\n x=k;\n y=j;\n flag++;\n break;\n }\n if(flag==1){\n break;\n }\n }\n }\n }\n cout << ans << endl;\n\n }\n\n return (0);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.0012, "final_rank": 4 }, { "submission_id": "aoj_1633_10588316", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while(true){\n int h, w;\n cin >> h >> w;\n if (h == 0 and w == 0) break;\n int Ans = 0;\n vector<vector<char>> Key(h, vector<char>(w));\n\n for (int i = 0; i < h; i++) {\n string r;\n cin >> r;\n for (int j = 0; j < w; j++) {\n Key.at(i).at(j) = r[j];\n }\n }\n\n string S;\n cin >> S;\n\n int x = 0;\n int y = 0;\n\n for (int i = 0; i < S.length(); i++) {\n for (int j = 0; j < h; j++) {\n for (int k = 0; k < w; k++) {\n if (S[i] == Key[j][k]){\n Ans = Ans + abs(j-y) + abs(k-x) + 1;\n x = k;\n y = j;\n j = h;\n k = w;\n }\n }\n }\n //Ans = Ans + 1;\n }\n\n cout << Ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.0012, "final_rank": 4 }, { "submission_id": "aoj_1633_10588300", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,m,n) for(int i=m;i<n;i++)\n#define REP(i,m,n) for(int i=m;i<=n;i++)\nusing namespace std;\nusing ll = long long;\nint main(){\n rep(l,0,100){\n int h,w;\n cin>>h>>w;\n if(h==0&&w==0)return 0;\n char r[h][w];\n rep(i,0,h)rep(j,0,w)cin>>r[i][j];\n int x=0;\n int y=0;\n string s;\n cin>>s;\n int ans=0;\n rep(k,0,s.size()){\n rep(i,0,h)rep(j,0,w){\n if(r[i][j]==s[k]){\n ans+=abs(x-i)+abs(y-j)+1;\n x=i;y=j;\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3328, "score_of_the_acc": -0.0083, "final_rank": 10 }, { "submission_id": "aoj_1633_10561922", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while (true) {\n int h, w;\n cin >> h >> w;\n if (h == 0 && w == 0) {\n break;\n }\n vector<string> r(h);\n for (int i = 0; i < h; i++) {\n cin >> r[i];\n }\n string s;\n cin >> s;\n\n int cx = 0;\n int cy = 0;\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n int tx = -1;\n int ty = -1;\n for (int a = 0; a < h; a++) {\n for (int b = 0; b < w; b++) {\n if (r[a][b] == s[i]) {\n tx = a;\n ty = b;\n break;\n }\n }\n }\n\n ans += abs(cx - tx) + abs(cy - ty) + 1;\n cx = tx;\n cy = ty;\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0026, "final_rank": 7 }, { "submission_id": "aoj_1633_10561918", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nint dx[] = {0, 1, 0, -1};\nint dy[] = {-1, 0, 1, 0};\n\nvector<vector<int>> bfs(int sx, int sy, int h, int w) {\n queue<pair<int, int>> q;\n vector<vector<int>> dist(h, vector<int>(w, -1));\n q.emplace(sx, sy);\n dist[sx][sy] = 0;\n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n for (int dir = 0; dir < 4; dir++) {\n int nx = x + dx[dir];\n int ny = y + dy[dir];\n if (nx < 0 || nx >= h || ny < 0 || ny >= w) {\n continue;\n }\n\n if (dist[nx][ny] == -1) {\n dist[nx][ny] = dist[x][y] + 1;\n q.emplace(nx, ny);\n }\n }\n }\n\n return dist;\n}\n\nint main() {\n while (true) {\n int h, w;\n cin >> h >> w;\n if (h == 0 && w == 0) {\n break;\n }\n vector<string> r(h);\n for (int i = 0; i < h; i++) {\n cin >> r[i];\n }\n string s;\n cin >> s;\n\n int cx = 0;\n int cy = 0;\n int ans = 0;\n for (int i = 0; i < s.size(); i++) {\n auto d = bfs(cx, cy, h, w);\n\n int tx = -1;\n int ty = -1;\n for (int a = 0; a < h; a++) {\n for (int b = 0; b < w; b++) {\n if (r[a][b] == s[i]) {\n tx = a;\n ty = b;\n break;\n }\n }\n }\n\n ans += d[tx][ty]+1;\n cx = tx;\n cy = ty;\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3584, "score_of_the_acc": -0.3028, "final_rank": 17 }, { "submission_id": "aoj_1633_10505839", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nint main(){\n ll h,w;\n string s;\n ll count = 0;\n ll pre = 0;\n ll dx,dy;\n\n while(1){\n cin >> h >> w;\n if(h == 0 && w == 0)break;\n vector<char> key(h * 50 + w);\n\n for(ll i = 0; i < h; i++){\n for(ll t = 0; t < w; t++){\n cin >> key.at(i * 50 + t);\n }\n }\n\n cin >> s;\n\n for(ll i = 0; i < s.size(); i++){\n for(ll t = 0; t < h * 50 + w; t++){\n if(s.at(i) == key.at(t)){\n dx = ll(t / 50) - ll(pre / 50);\n dy = ll(t % 50) - ll(pre % 50);\n\n if(dx < 0)dx *= -1;\n if(dy < 0)dy *= -1;\n\n count += dx + dy + 1;\n\n pre = t;\n break;\n }\n }\n\n //cout << dx << \" \" << dy << '\\n';\n }\n \n cout << count << '\\n';\n count = 0;\n pre = 0;\n\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0026, "final_rank": 7 }, { "submission_id": "aoj_1633_10505714", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<(n);i++)//0-indexed\n#define REP(i,j,n) for(int i=j;i<(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define sort(a) sort(all(a))\n#define reverse(a) reverse(all(a))\n#define INF 1e9\n\nint dx[]={1,0,-1,0};\nint dy[]={0,1,0,-1};\n\nint main(){\n while(1){\n ll h,w;\n cin >> h >> w;\n if(h==0&&w==0)break;\n vector<vector<char>> s(h,vector<char>(w));\n rep(i,h){\n rep(j,w){\n cin>>s.at(i).at(j);\n }\n }\n string t;\n cin>>t;\n ll cnt=0;\n pair<ll, ll> now=make_pair(0,0);\n rep(i,t.length()){\n rep(j,h){\n rep(k,w){\n if(s.at(j).at(k)==t.at(i)){\n cnt+=(abs(j-now.first)+abs(k-now.second)+1);\n now.first=j;\n now.second=k;\n }\n }\n }\n }\n cout<<cnt<<endl;\n}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3376, "score_of_the_acc": -0.0093, "final_rank": 12 }, { "submission_id": "aoj_1633_10505593", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\n\nvoid solve(ll H,ll W) {\n vector<string> A(H);\n rep(i,H){\n cin>>A[i];\n }\n string S;\n cin>>S;\n ll ci=0,cj=0;\n ll cnt=0;\n for (auto si:S){\n bool flag=false;\n rep(i,H){\n rep(j,W){\n if (si==A[i][j]){\n cnt+=abs(i-ci)+abs(j-cj)+1;\n ci=i;\n cj=j;\n flag=true; \n break;\n }\n }\n if (flag)break;\n }\n }\n cout<<cnt<<endl;\n return;\n}\n\n\nint main() {\n ll H,W;\n while (true){\n cin>>H>>W;\n if (H==0){\n break;\n }\n solve(H,W);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0026, "final_rank": 7 } ]
aoj_1638_cpp
Let's Move Tiles! You have a framed square board forming a grid of square cells. Each of the cells is either empty or with a square tile fitting in the cell engraved with a Roman character. You can tilt the board to one of four directions, away from you, toward you, to the left, or to the right, making all the tiles slide up, down, left, or right, respectively, until they are squeezed to an edge frame of the board. The figure below shows an example of how the positions of the tiles change by tilting the board toward you. Let the characters U , D , L , and R represent the operations of tilting the board away from you, toward you, to the left, and to the right, respectively. Further, let strings consisting of these characters represent the sequences of corresponding operations. For example, the string DRU means a sequence of tilting toward you first, then to the right, and, finally, away from you. This will move tiles down first, then to the right, and finally, up. To deal with very long operational sequences, we introduce a notation for repeated sequences. For a non-empty sequence seq , the notation ( seq ) k means that the sequence seq is repeated k times. Here, k is an integer. For example, (LR)3 means the same operational sequence as LRLRLR . This notation for repetition can be nested. For example, ((URD)3L)2R means URDURDURDLURDURDURDLR . Your task is to write a program that, given an initial positions of tiles on the board and an operational sequence, computes the tile positions after the given operations are finished. Input The input consists of at most 100 datasets, each in the following format. n s 11 ... s 1 n ... s n 1 ... s nn seq In the first line, n is the number of cells in one row (and also in one column) of the board (2 ≤ n ≤ 50). The following n lines contain the initial states of the board cells. s ij is a character indicating the initial state of the cell in the i -th row from the top and in the j -th column from the left (both starting with one). It is either ‘ . ’ (a period), meaning the cell is empty, or an uppercase letter ‘ A ’-‘ Z ’, meaning a tile engraved with that character is there. seq is a string that represents an operational sequence. The length of seq is between 1 and 1000, inclusive. It is guaranteed that seq denotes an operational sequence as described above. The numbers in seq denoting repetitions are between 2 and 10 18 , inclusive, and are without leading zeros. Furthermore, the length of the operational sequence after unrolling all the nested repetitions is guaranteed to be at most 10 18 . The end of the input is indicated by a line containing a zero. Output For each dataset, output the states of the board cells, after performing the given operational sequence starting from the given initial states. The output should be formatted in n lines, in the same format as the initial board cell states are given in the input. Sample Input 4 ..E. .AD. B... ..C. D 4 ..E. .AD. B... ..C. DR 3 ... .A. BC. ((URD)3L)2R 5 ... ...(truncated)
[ { "submission_id": "aoj_1638_10865908", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define ALL(v) begin(v),end(v)\n#define REP(i,n) for(int i=0;i<(n);++i)\n#define RREP(i,n) for(int i=(n)-1;i>=0;--i)\n\ntypedef long long LL;\ntypedef vector<int> vint;\n\nconst int targets[4][9] = {\n\t{1, 1, 2, 2, 2, 1, 8, 8, 8},\n\t{3, 2, 2, 3, 4, 4, 4, 3, 2},\n\t{5, 5, 4, 4, 4, 5, 6, 6, 6},\n\t{7, 8, 8, 7, 6, 6, 6, 7, 8}\n};\n\nint getdir(char c){\n\tswitch(c){\n\t\tcase 'R': return 0;\n\t\tcase 'U': return 1;\n\t\tcase 'L': return 2;\n\t\tcase 'D': return 3;\n\t}\n\tabort();\n}\n\nstruct solver{\n\tint n;\n\tsolver(int n_) : n(n_) {}\n\tvector<vint> ids[9];\n\tvint trs[4];\n\tvint unit;\n\tconst char *p;\n\t\n\tvint mul(const vint &x, const vint &y){\n\t\tint sz = x.size();\n\t\tvint ret(sz);\n\t\tREP(i, sz){ ret[i] = y[x[i]]; }\n\t\treturn ret;\n\t}\n\t\n\tvint pow(vint x, LL y){\n\t\tvint a = unit;\n\t\twhile(y){\n\t\t\tif(y & 1){ a = mul(a, x); }\n\t\t\tx = mul(x, x);\n\t\t\ty >>= 1;\n\t\t}\n\t\treturn a;\n\t}\n\t\n\tvint parse(){\n\t\tvint a = unit;\n\t\tvint b;\n\t\twhile(1){\n\t\t\tif(*p == '('){\n\t\t\t\t++p;\n\t\t\t\tvint x = parse();\n\t\t\t\t++p;\n\t\t\t\tchar *endp;\n\t\t\t\tLL r = strtoll(p, &endp, 10);\n\t\t\t\tp = endp;\n\t\t\t\tb = pow(x, r);\n\t\t\t}\n\t\t\telse if(isalpha(*p)){\n\t\t\t\tint dir = getdir(*p);\n\t\t\t\t++p;\n\t\t\t\tb = trs[dir];\n\t\t\t}\n\t\t\telse{ break; }\n\t\t\ta = mul(a, b);\n\t\t}\n\t\treturn a;\n\t}\n\t\n\tvoid solve(){\n\t\tvector<string> in0(n);\n\t\tREP(i, n){ cin >> in0[i]; }\n\t\tstring op;\n\t\tcin >> op;\n\n\t\tint fstdir = -1;\n\t\tfor(char c : op){\n\t\t\tif(isalpha(c)){\n\t\t\t\tfstdir = getdir(c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tvector<string> in(n, string(n, '.'));\n\t\tif(fstdir == 0){\n\t\t\tREP(y, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(x, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[y][u--] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if(fstdir == 1){\n\t\t\tREP(x, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(y, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[u++][x] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if(fstdir == 2){\n\t\t\tREP(y, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(x, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[y][u++] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tREP(x, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(y, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[u--][x] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tREP(i, 9){\n\t\t\tids[i].assign(n, vint(n, -1));\n\t\t}\n\n\t\tint id = 0;\n\t\tREP(i, n)\n\t\tREP(j, n){\n\t\t\tif(in[i][j] != '.'){\n\t\t\t\tids[0][i][j] = id;\n\t\t\t\t++id;\n\t\t\t}\n\t\t}\n\t\tint pcnt = id;\n\n\t\tREP(i, 4){ trs[i].assign(9 * pcnt, -1); }\n\t\tunit.resize(9 * pcnt);\n\t\tiota(ALL(unit), 0);\n\n\t\tREP(from, 9){\n\t\t\tint to, dir;\n\n\t\t\tdir = 0;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(y, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(x, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][y][u];\n\t\t\t\t\t\t--u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdir = 1;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(x, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(y, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][u][x];\n\t\t\t\t\t\t++u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdir = 2;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(y, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(x, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][y][u];\n\t\t\t\t\t\t++u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdir = 3;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(x, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(y, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][u][x];\n\t\t\t\t\t\t--u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tp = op.c_str();\n\t\tvint res = parse();\n\n\t\tvector<char> val(9 * pcnt);\n\t\tREP(y, n)\n\t\tREP(x, n){\n\t\t\tint k = ids[0][y][x];\n\t\t\tif(k >= 0){\n\t\t\t\tval[res[k]] = in[y][x];\n\t\t\t}\n\t\t}\n\t\tvector<string> ans(n, string(n, '.'));\n\t\tREP(i, 9)\n\t\tREP(y, n)\n\t\tREP(x, n){\n\t\t\tint k = ids[i][y][x];\n\t\t\tif(k >= 0 && val[k] != 0){\n\t\t\t\tans[y][x] = val[k];\n\t\t\t}\n\t\t}\n\t\t\n\t\tREP(i, n){\n\t\t\tcout << ans[i] << '\\n';\n\t\t}\n\t}\n};\n\nint main(){\n\tint n;\n\twhile(cin >> n && n){\n\t\tsolver(n).solve();\n\t}\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 8032, "score_of_the_acc": -0.3933, "final_rank": 4 }, { "submission_id": "aoj_1638_10506737", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\n\nvoid solve(ll N) {\n vector<string> Grid(N);\n rep(i,N)cin>>Grid[i];\n string seq;\n cin>>seq;\n ll M=seq.size();\n auto slide=[&](vector<string> &G,char direction) -> void {\n if (direction=='L'){\n rep(i,N){\n ll nj=0;\n for (ll j=0;j<N;j++){\n if (G[i][j]!='.'){\n char s=G[i][j];\n G[i][j]='.';\n G[i][nj]=s;\n nj++;\n }\n }\n }\n }\n if (direction=='R'){\n rep(i,N){\n ll nj=N-1;\n for (ll j=N-1;j>=0;j--){\n if (G[i][j]!='.'){\n char s=G[i][j];\n G[i][j]='.';\n G[i][nj]=s;\n nj--;\n }\n }\n }\n }\n if (direction=='U'){\n rep(j,N){\n ll ni=0;\n for (ll i=0;i<N;i++){\n if (G[i][j]!='.'){\n char s=G[i][j];\n G[i][j]='.';\n G[ni][j]=s;\n ni++;\n }\n }\n }\n }\n if (direction=='D'){\n rep(j,N){\n ll ni=N-1;\n for (ll i=N-1;i>=0;i--){\n if (G[i][j]!='.'){\n char s=G[i][j];\n G[i][j]='.';\n G[ni][j]=s;\n ni--;\n }\n }\n }\n }\n return;\n };\n\n auto divmod=[&](ll a,ll b)->ll {\n a%=b;\n if (a<0)a+=b;\n return a;\n };\n\n ll glo_now=0;\n bool antei=false;\n char last_LR='#';\n char last_UD='#';\n ll pos;\n\n auto move_composition=[&](vector<ll> a,vector<ll> b) -> vector<ll> {\n vector<ll> c(4,0);\n rep(i,4){\n c[i]=a[i]+b[divmod(i+a[i],4)];\n }\n return c;\n };\n\n auto move_pow=[&](vector<ll> a,ll n) -> vector<ll> {\n vector<ll> b(4,0);\n while (n>0){\n if (n&1){\n b=move_composition(b,a);\n }\n a=move_composition(a,a);\n n>>=1;\n }\n return b;\n };\n\n auto read=[&] (auto self) -> vector<ll> {\n vector<ll> move(4,0);\n while (glo_now<M){\n if (seq[glo_now]==')'){\n glo_now++;\n ll repeat=0;\n while (glo_now<M){\n if ('0'<=seq[glo_now] and seq[glo_now]<='9'){\n repeat*=10;\n repeat+=seq[glo_now]-'0';\n glo_now++;\n }\n else{\n break;\n }\n }\n if (repeat==0)repeat=1;\n vector<ll> move2=move_pow(move,repeat-1);\n if (antei){\n pos+=move2[divmod(pos,4)];\n }\n move=move_composition(move2,move);\n break;\n }\n if (seq[glo_now]=='('){\n glo_now++;\n move=move_composition(move,self(self));\n }\n else{\n ll s=seq[glo_now];\n glo_now++;\n vector<ll> nxt_move;\n if (s=='L'){\n nxt_move={-1,+1,0,0};\n }\n if (s=='R'){\n nxt_move={0,0,-1,+1};\n }\n if (s=='U'){\n nxt_move={0,-1,+1,0};\n }\n if (s=='D'){\n nxt_move={+1,0,0,-1};\n }\n move=move_composition(move,nxt_move);\n if (antei){\n pos+=nxt_move[divmod(pos,4)];\n }\n else{\n if (s=='L' or s=='R'){\n if (last_UD=='#'){\n last_LR=s;\n }\n else{\n antei=true;\n slide(Grid,last_UD);\n slide(Grid,s);\n if (last_UD=='U'){\n if (s=='R'){\n pos=0;\n }\n else{\n slide(Grid,'D');\n slide(Grid,'R');\n slide(Grid,'U');\n pos=3;\n }\n }\n else{\n if (s=='R'){\n slide(Grid,'U');\n pos=1;\n }\n else{\n slide(Grid,'R');\n slide(Grid,'U');\n pos=2;\n }\n }\n }\n }\n else{\n if (last_LR=='#'){\n last_UD=s;\n }\n else{\n antei=true;\n slide(Grid,last_LR);\n slide(Grid,s);\n if (last_LR=='L'){\n if (s=='D'){\n slide(Grid,'R');\n slide(Grid,'U');\n pos=2;\n }\n else{\n slide(Grid,'D');\n slide(Grid,'R');\n slide(Grid,'U');\n pos=3;\n }\n }\n else{\n if (s=='D'){\n slide(Grid,'U');\n pos=1;\n }\n else{\n pos=0;\n }\n }\n }\n }\n }\n }\n }\n return move;\n };\n read(read);\n if (!antei){\n if (last_LR!='#'){\n slide(Grid,last_LR);\n }\n if (last_UD!='#'){\n slide(Grid,last_UD);\n }\n rep(i,N){\n cout<<Grid[i]<<endl;\n }\n return;\n }\n ll pd=divmod(pos,4);\n pos-=pd;\n ll cycle=pos/4;\n\n // 時計回りに回す\n auto slide_ord=[&](vector<vector<ll>> G) -> vector<vector<ll>> {\n // D\n rep(j,N){\n ll ni=N-1;\n for (ll i=N-1;i>=0;i--){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[ni][j]=s;\n ni--;\n }\n }\n }\n // L\n rep(i,N){\n ll nj=0;\n for (ll j=0;j<N;j++){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[i][nj]=s;\n nj++;\n }\n }\n }\n // U\n rep(j,N){\n ll ni=0;\n for (ll i=0;i<N;i++){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[ni][j]=s;\n ni++;\n }\n }\n }\n // R\n rep(i,N){\n ll nj=N-1;\n for (ll j=N-1;j>=0;j--){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[i][nj]=s;\n nj--;\n }\n }\n }\n return G;\n };\n\n // 反時計周りに回す\n auto slide_inv=[&](vector<vector<ll>> G) -> vector<vector<ll>> {\n // L\n rep(i,N){\n ll nj=0;\n for (ll j=0;j<N;j++){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[i][nj]=s;\n nj++;\n }\n }\n }\n // D\n rep(j,N){\n ll ni=N-1;\n for (ll i=N-1;i>=0;i--){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[ni][j]=s;\n ni--;\n }\n }\n }\n // R\n rep(i,N){\n ll nj=N-1;\n for (ll j=N-1;j>=0;j--){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[i][nj]=s;\n nj--;\n }\n }\n }\n // U\n rep(j,N){\n ll ni=0;\n for (ll i=0;i<N;i++){\n if (G[i][j]!=-1){\n ll s=G[i][j];\n G[i][j]=-1;\n G[ni][j]=s;\n ni++;\n }\n }\n }\n return G;\n };\n\n vector<vector<ll>> G(N,vector<ll>(N,-1));\n vector<pair<ll,ll>> G_pos;\n ll num=0;\n rep(i,N){\n rep(j,N){\n if (Grid[i][j]!='.'){\n G[i][j]=num;\n num++;\n G_pos.push_back({i,j});\n }\n }\n }\n \n\n auto mult=[&](vector<ll> P,vector<ll> Q) -> vector<ll> {\n ll s=P.size();\n vector<ll> R(s);\n rep(i,s){\n R[i]=Q.at(P.at(i));\n }\n return R;\n };\n\n auto doubling=[&](vector<ll> P,ll n) -> vector<ll> {\n ll s=P.size();\n vector<ll> R(s);\n rep(i,s){\n R[i]=i;\n }\n while (n>0){\n if (n&1){\n R=mult(R,P);\n }\n P=mult(P,P);\n n>>=1;\n }\n return R;\n };\n vector<ll> P(num);\n if (cycle>=0){\n vector<vector<ll>> NG=slide_ord(G);\n rep(i,N){\n rep(j,N){\n if (G[i][j]!=-1){\n P[NG[i][j]]=G[i][j];\n }\n }\n }\n P=doubling(P,cycle);\n }\n else{\n vector<vector<ll>> NG=slide_inv(G);\n rep(i,N){\n rep(j,N){\n if (G[i][j]!=-1){\n P[NG[i][j]]=G[i][j];\n }\n }\n }\n P=doubling(P,-cycle);\n }\n vector<string> new_grid(N,string(N,'.'));\n rep(t,num){\n ll i,j;\n tie(i,j)=G_pos[t];\n ll pt=P[t];\n ll pi,pj;\n tie(pi,pj)=G_pos[pt];\n new_grid[pi][pj]=Grid[i][j];\n }\n Grid=new_grid;\n string s=\"DLU\";\n rep(i,min(3LL,pd)){\n slide(Grid,s.at(i));\n }\n rep(i,N){\n cout<<Grid[i]<<endl;\n }\n return;\n}\n\n\nint main() {\n ll N;\n while (true){\n cin>>N;\n if (N==0)break;\n solve(N);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.009, "final_rank": 1 }, { "submission_id": "aoj_1638_8007000", "code_snippet": "#include<cmath>\n#include<algorithm>\n#include<iostream>\n#include<vector>\n#include<map>\n#include<set>\n#include<cassert>\n#include<stack>\nusing ll = long long;\nconst ll ILL=1e15;\ntemplate<class T>\nbool chmin(T &a,T b){\n\tif(a>b){\n\t\ta=b;\n\t\treturn true;\n\t}\n\treturn false;\n}\nusing namespace std;\n#define rep(i,a,b) for(int i=(int)a;i<(int)b;i++)\n\nvoid solve(int n){\n\tvector<vector<char>> p(n,vector<char>(n));\n\tint M=0;\n\tstring S;\n\tstring OP=\"UDLR\";\n\tint K=18;\n\tset<int> st={3,5,10,13,16};\n\tvector<vector<vector<int>>> ban(K,vector<vector<int>>(n,vector<int>(n,-1)));\n\tauto f=[&](vector<vector<int>> tmp,char c)->vector<vector<int>>{\n\t\tif(c=='U'){\n\t\t\trep(i,0,n) rep(j,0,n){\n\t\t\t\tif(tmp[i][j]==-1) continue;\n\t\t\t\tint x=i,y=j;\n\t\t\t\twhile(x!=0&&tmp[x-1][y]==-1) x--;\n\t\t\t\tswap(tmp[i][j],tmp[x][y]);\n\t\t\t}\n\t\t}\n\t\tif(c=='D'){\n\t\t\tfor(int i=n-1;i>=0;i--) rep(j,0,n){\n\t\t\t\tif(tmp[i][j]==-1) continue;\n\t\t\t\tint x=i,y=j;\n\t\t\t\twhile(x!=n-1&&tmp[x+1][y]==-1) x++;\n\t\t\t\tswap(tmp[i][j],tmp[x][y]);\n\t\t\t}\n\t\t}\n\t\tif(c=='L'){\n\t\t\trep(i,0,n) rep(j,0,n){\n\t\t\t\tif(tmp[i][j]==-1) continue;\n\t\t\t\tint x=i,y=j;\n\t\t\t\twhile(y!=0&&tmp[x][y-1]==-1) y--;\n\t\t\t\t//cout<<i<<\" \"<<j<<\" \"<<x<<\" \"<<y<<endl;\n\t\t\t\tswap(tmp[i][j],tmp[x][y]);\n\t\t\t}\n\t\t}\n\t\tif(c=='R'){\n\t\t\trep(i,0,n) for(int j=n-1;j>=0;j--){\n\t\t\t\tif(tmp[i][j]==-1) continue;\n\t\t\t\tint x=i,y=j;\n\t\t\t\twhile(y!=n-1&&tmp[x][y+1]==-1) y++;\n\t\t\t\tswap(tmp[i][j],tmp[x][y]);\n\t\t\t}\n\t\t}\n\t\treturn tmp;\n\t};\n\trep(i,0,n) rep(j,0,n){\n\t\tcin>>p[i][j];\n\t\tif(p[i][j]!='.'){\n\t\t\tban[4][i][j]=M;\n\t\t\tM++;\n\t\t}\n\t}\n\tcin>>S;\n\tif(M==0){\n\t\trep(i,0,n){\n\t\t\trep(j,0,n) cout<<p[i][j];\n\t\t\tcout<<\"\\n\";\n\t\t}\n\t\treturn;\n\t}\n\tvector<vector<int>> to_ban(K,vector<int>(4));\n\trep(i,0,K){\n\t\tto_ban[i][0]=i%3;\n\t\tto_ban[i][1]=to_ban[i][0]+6;\n\t\tto_ban[i][2]=3*(i/3);\n\t\tto_ban[i][3]=to_ban[i][2]+2;\n\t\tif(i==4){\n\t\t\tto_ban[i]={1,7,12,14};\n\t\t}\n\t\tif(i>=9){\n\t\t\trep(j,0,2) to_ban[i][j]+=9;\n\t\t}\n\t}\n\tvector<int> seen(K),order={4};\n\tseen[4]=1;\n\trep(i,0,(int)order.size()){\n\t\tint a=order[i];\n\t\trep(j,0,4){\n\t\t\tint b=to_ban[a][j];\n\t\t\tif(seen[b]==0){\n\t\t\t\tseen[b]=1;\n\t\t\t\torder.push_back(b);\n\t\t\t\tban[b]=f(ban[a],OP[j]);\n\t\t\t}\n\t\t}\n\t}\n\trep(i,0,K){\n\t\tif(st.count(i)) continue;\n\t\t/*cout<<i<<endl;\n\t\trep(j,0,n){\n\t\t\trep(k,0,n) cout<<ban[i][j][k]<<\" \";\n\t\t\tcout<<endl;\n\t\t}*/\n\t}\n\tvector<vector<vector<int>>> pos(K,vector<vector<int>>(4,vector<int>(M,-1)));\n\trep(i,0,K) rep(j,0,4){\n\t\tauto tmp=f(ban[i],OP[j]);\n\t\tint ind=to_ban[i][j];\n\t\trep(k,0,n) rep(l,0,n){\n\t\t\tif(tmp[k][l]!=-1){\n\t\t\t\tassert(ban[ind][k][l]!=-1);\n\t\t\t\tpos[i][j][tmp[k][l]]=ban[ind][k][l];\n\t\t\t}\n\t\t}\n\t}\n\tvector<int> q(K*M);\n\trep(i,0,K*M) q[i]=i;\n\tint X=0;\n\tvector<vector<int>> seni(4,q);\n\tmap<char,int> m;\n\trep(i,0,4) m[OP[i]]=i;\n\trep(i,0,4){\n\t\trep(j,0,K*M){\n\t\t\tint a=j/M;\n\t\t\tint b=j%M;\n\t\t\tif(pos[a][i][b]!=-1){\n\t\t\t\tseni[i][j]=M*to_ban[a][i]+pos[a][i][b];\n\t\t\t}\n\t\t}\n\t}\n\tauto merge=[&](vector<int> a,vector<int> b)->vector<int>{\n\t\tvector<int> res(K*M);\n\t\trep(i,0,K*M) res[i]=b[a[i]];\n\t\treturn res;\n\t};\n\tauto pow_merge=[&](vector<int> a,ll T)->vector<int>{\n\t\tauto e=q;\n\t\twhile(T){\n\t\t\tif(T&1){\n\t\t\t\te=merge(e,a);\n\t\t\t}\n\t\t\tT>>=1;\n\t\t\ta=merge(a,a);\n\t\t}\n\t\treturn e;\n\t};\n\tauto g=[&](auto self)->vector<int>{\n\t\tvector<int> res=q;\n\t\twhile(X<(int)S.size()){\n\t\t\tif(S[X]=='('){\n\t\t\t\tX++;\n\t\t\t\tauto tmp=self(self);\n\t\t\t\tll T=0;\n\t\t\t\twhile(X!=(int)S.size()&&'0'<=S[X]&&S[X]<='9'){\n\t\t\t\t\tT*=10ll;\n\t\t\t\t\tT+=(S[X]-'0');\n\t\t\t\t\tX++;\n\t\t\t\t}\n\t\t\t\tres=merge(res,pow_merge(tmp,T));\n\t\t\t}\n\t\t\telse if(S[X]==')'){\n\t\t\t\tX++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tres=merge(res,seni[m[S[X]]]);\n\t\t\t\tX++;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t};\n\tauto val=g(g);\n\tvector<vector<char>> ans(n,vector<char>(n,'.'));\n\tvector<pair<int,int>> Y(M);\n\tint a=val[4*M]/M;\n\t//cout<<a<<endl;\n\trep(i,0,n) rep(j,0,n){\n\t\tif(ban[a][i][j]!=-1) Y[ban[a][i][j]]={i,j};\n\t}\n\tvector<char> C;\n\trep(i,0,n) rep(j,0,n) if(p[i][j]!='.') C.push_back(p[i][j]);\n\trep(i,0,M){\n\t\tauto tmp=Y[val[4*M+i]%M];\n\t\tans[tmp.first][tmp.second]=C[i];\n\t}\n\trep(i,0,n){\n\t\trep(j,0,n) cout<<ans[i][j];\n\t\tcout<<\"\\n\";\n\t}\n\n}\n\nint main(){\n\twhile(true){\n\t\tint n;\n\t\tcin>>n;\n\t\tif(n) solve(n);\n\t\telse break;\n\t}\n}", "accuracy": 1, "time_ms": 1340, "memory_kb": 9020, "score_of_the_acc": -1.1487, "final_rank": 6 }, { "submission_id": "aoj_1638_6160126", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 10;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\n//-----------------------------------\n\nstruct Data {\n\tarray<int, 4> nex;\n\tarray<ll, 4> ad;\n};\nData operator*(Data a, Data b) {\n\tswap(a, b);\n\tData res;\n\trep(i, 4) {\n\t\tres.nex[i] = a.nex[b.nex[i]];\n\t\tres.ad[i] = a.ad[b.nex[i]] + b.ad[i];\n\t}\n\treturn res;\n}\nData e = {\n\t{0,1,2,3},\n\t{0,0,0,0}\n};\n//UDLR\nstring udlr = \"UDLR\";\nbool isdir(char c) {\n\treturn c == 'U' || c == 'D' || c == 'L' || c == 'R';\n}\nData ori[4] = {\n{\n\t{1,1,2,2},\n\t{1,0,0,-1}\n},\n{\n\t{0,0,3,3},\n\t{0,-1,1,0}\n},\n{\n\t{0,1,1,0},\n\t{0,0,-1,1}\n},\n{\n\t{3,2,2,3},\n\t{-1,1,0,0}\n}\n};\nData operator^(Data a, ll n) {\n\tData res = e;\n\twhile (n > 0) {\n\t\tif (n & 1)res = res * a;\n\t\ta = a * a; n >>= 1;\n\t}\n\treturn res;\n}\n\nvector<string> move(vector<string> s, int dir,char x='.') {\n\tint n = s.size();\n\tif (dir == 0) {\n\t\trep(j, n) {\n\t\t\tstring cur;\n\t\t\trep(i, n) {\n\t\t\t\tif (s[i][j] != x) {\n\t\t\t\t\tcur.push_back(s[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(i, cur.size())s[i][j] = cur[i];\n\t\t\tRep(i, cur.size(), n)s[i][j] = x;\n\t\t}\n\t}\n\telse if (dir == 1) {\n\t\trep(j, n) {\n\t\t\tstring cur;\n\t\t\trep(i, n) {\n\t\t\t\tif (s[n - 1 - i][j] != x) {\n\t\t\t\t\tcur.push_back(s[n-1-i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(i, cur.size())s[n - 1 - i][j] = cur[i];\n\t\t\tRep(i, cur.size(), n)s[n - 1 - i][j] = x;\n\t\t}\n\t}\n\telse if (dir == 2) {\n\t\trep(i, n) {\n\t\t\tstring cur;\n\t\t\trep(j, n) {\n\t\t\t\tif (s[i][j] != x) {\n\t\t\t\t\tcur.push_back(s[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(j, cur.size())s[i][j] = cur[j];\n\t\t\tRep(j, cur.size(), n)s[i][j] = x;\n\t\t}\n\t}\n\telse {\n\t\trep(i, n) {\n\t\t\tstring cur;\n\t\t\trep(j, n) {\n\t\t\t\tif (s[i][n-1-j] != x) {\n\t\t\t\t\tcur.push_back(s[i][n-1-j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(j, cur.size())s[i][n-1-j] = cur[j];\n\t\t\tRep(j, cur.size(), n)s[i][n-1-j] = x;\n\t\t}\n\t}\n\treturn s;\n}\nint dirjudge(int d1, int d2) {\n\tif (d1 >= 2)swap(d1, d2);\n\tif (d1 == 1 && d2 == 2)return 0;\n\telse if (d1 == 0 && d2 == 2)return 1;\n\telse if (d1 == 0 && d2 == 3)return 2;\n\telse return 3;\n}\nvector<int> merge(vector<int> a, vector<int> b) {\n\tvector<int> res(a.size());\n\trep(i, res.size())res[i] = a[b[i]];\n\treturn res;\n}\n\nint n;\nvoid solve() {\n\tvector<string> s(n);\n\trep(i, n)cin >> s[i];\n\tstring str; cin >> str;\n\n\tvector<Data> st;\n\tData nw = e;\n\trep(i, str.size()) {\n\t\tif (str[i] == '(') {\n\t\t\tst.push_back(nw);\n\t\t\tnw = e;\n\t\t}\n\t\telse if (str[i] == ')') {\n\t\t\ti++;\n\t\t\tint le = i;\n\t\t\twhile (i < str.size() && '0' <= str[i] && str[i] <= '9')i++;\n\t\t\tll num = stoll(str.substr(le, i - le));\n\t\t\tnw = nw ^ num;\n\t\t\tnw = st.back() * nw;\n\t\t\tst.pop_back();\n\t\t\ti--;\n\t\t}\n\t\telse {\n\t\t\tnw = nw * ori[udlr.find(str[i])];\n\t\t}\n\t}\n\tassert(st.empty());\n\tbool exi[2] = {};\n\tvector<int> memo;\n\trep(i, str.size()) {\n\t\tif (str[i] == 'U' || str[i] == 'D' || str[i] == 'L' || str[i] == 'R') {\n\t\t\tint id = 0;\n\t\t\tif (str[i] == 'L' || str[i] == 'R') {\n\t\t\t\tid = 1;\n\t\t\t}\n\t\t\tif (exi[id])continue;\n\t\t\texi[id] = true;\n\t\t\tmemo.push_back(i);\n\t\t}\n\t}\n\tauto getlas=[&](int loc)->char {\n\t\tfor (int i = loc - 1;; i--) {\n\t\t\tif (isdir(str[i]))return str[i];\n\t\t}\n\t\treturn '?';\n\t};\n\tif (memo.size() == 1) {\n\t\tchar z = getlas(str.size());\n\t\ts = move(s, udlr.find(z), '.');\n\t}\n\telse {\n\t\tint d2 = udlr.find(str[memo.back()]);\n\t\tint d1 = udlr.find(getlas(memo.back()));\n\t\ts = move(s, d1); s = move(s, d2);\n\t\tvector<int> vh, vw;\n\t\trep(i, n) {\n\t\t\tint cnt = 0;\n\t\t\trep(j, n)if (s[i][j] != '.')cnt++;\n\t\t\tvh.push_back(cnt);\n\t\t}\n\t\trep(j, n) {\n\t\t\tint cnt = 0;\n\t\t\trep(i, n)if (s[i][j] != '.')cnt++;\n\t\t\tvw.push_back(cnt);\n\t\t}\n\t\tsort(all(vh));\n\t\tsort(all(vw));\n\t\tint cur = dirjudge(d1, d2);\n\t\tll num = nw.ad[cur];\n\t\t//cout << \"? \" << num <<\" \"<<cur<< \"\\n\";\n\t\tvector<int> ori(n* n * 4);\n\t\tif (num >= 0) {\n\t\t\t//clockwise\n\t\t\trep(d, 4) {\n\t\t\t\trep(i, n)rep(j, n) {\n\t\t\t\t\tint id = i * n + j + d * n * n;\n\t\t\t\t\tint ni=i, nj=j, nd=d;\n\t\t\t\t\tnd = (d + 1) % 4;\n\t\t\t\t\tif (d == 0) {\n\t\t\t\t\t\tint cnum = vw[n - 1 - j];\n\t\t\t\t\t\tif (i >= n - cnum) {\n\t\t\t\t\t\t\tni = i - (n - cnum);\n\t\t\t\t\t\t\tnj = j;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (d == 1) {\n\t\t\t\t\t\tint rnum = vh[n - 1 - i];\n\t\t\t\t\t\tif (j < rnum) {\n\t\t\t\t\t\t\tni = i;\n\t\t\t\t\t\t\tnj = n - rnum + j;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (d == 2) {\n\t\t\t\t\t\tint cnum = vw[j];\n\t\t\t\t\t\tif (i < cnum) {\n\t\t\t\t\t\t\tni = n - cnum + i;\n\t\t\t\t\t\t\tnj = j;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if(d==3){\n\t\t\t\t\t\tint rnum = vh[i];\n\t\t\t\t\t\tif (j >= n - rnum) {\n\t\t\t\t\t\t\tni = i;\n\t\t\t\t\t\t\tnj = j - (n - rnum);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tori[id] = ni * n + nj + nd * n * n;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\t//counter clockwise\n\t\t\trep(d, 4) {\n\t\t\t\trep(i, n)rep(j, n) {\n\t\t\t\t\tint id = i * n + j + d * n * n;\n\t\t\t\t\tint ni = i, nj = j, nd = d;\n\t\t\t\t\tnd = (d + 3) % 4;\n\t\t\t\t\tif (d == 0) {\n\t\t\t\t\t\tint rnum = vh[i];\n\t\t\t\t\t\tif (j < rnum) {\n\t\t\t\t\t\t\tni = i;\n\t\t\t\t\t\t\tnj = n - rnum + j;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (d == 1) {\n\t\t\t\t\t\tint cnum = vw[n - 1 - j];\n\t\t\t\t\t\tif (i < cnum) {\n\t\t\t\t\t\t\tni = n - cnum + i;\n\t\t\t\t\t\t\tnj = j;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (d == 2) {\n\t\t\t\t\t\tint rnum = vh[n-1-i];\n\t\t\t\t\t\tif (j >= n - rnum) {\n\t\t\t\t\t\t\tni = i;\n\t\t\t\t\t\t\tnj = j - (n - rnum);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (d == 3) {\n\t\t\t\t\t\tint cnum = vw[j];\n\t\t\t\t\t\tif (i >= n - cnum) {\n\t\t\t\t\t\t\tni = i - (n - cnum);\n\t\t\t\t\t\t\tnj = j;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tori[id] = ni * n + nj + nd * n * n;\n\t\t\t\t}\n\t\t\t}\n\n\n\t\t\tnum *= -1;\n\t\t}\n\t\tvector<int> tra(n* n * 4);\n\t\trep(i, n* n * 4)tra[i] = i;\n\t\twhile (num > 0) {\n\t\t\tif (num & 1) {\n\t\t\t\ttra = merge(tra, ori);\n\t\t\t}\n\t\t\tori = merge(ori, ori); num >>= 1;\n\t\t}\n\t\tvector<string> cop = s;\n\t\trep(i, n)rep(j, n)s[i][j] = '.';\n\t\trep(i, n)rep(j, n) {\n\t\t\tif (cop[i][j] != '.') {\n\t\t\t\tint id = i * n + j + cur * n * n;\n\t\t\t\tint to = tra[id];\n\t\t\t\tto %= n * n;\n\t\t\t\ts[to / n][to % n] = cop[i][j];\n\t\t\t}\n\t\t}\n\t}\n\trep(i, n)cout << s[i] << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\twhile(cin>>n,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3644, "score_of_the_acc": -0.0147, "final_rank": 2 }, { "submission_id": "aoj_1638_6055620", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define NUM 13\n#define MAX 60\n#define SIZE 55\n#define SIZE2 1005\n\nenum Type{\n\tClock,\n\tRev,\n\tNot,\n};\n\nType type_array[3] = {Clock,Rev,Not};\n\nenum DIR{\n\tU,\n\tR,\n\tD,\n\tL,\n\tUndef,\n};\n\nenum DIR2{\n\tUR,\n\tDR,\n\tDL,\n\tUL,\n\t//■■■■URとRUは同じでない■■■■\n\tRU,\n\tRD,\n\tLD,\n\tLU,\n\t//■■■■URとRUは同じでない■■■■\n\tU2,\n\tR2,\n\tD2,\n\tL2,\n\tUndef2,\n};\n\nbool DEBUG = true;\n\nstring DEB[NUM] = {\"UR\",\"DR\",\"DL\",\"UL\",\"RU\",\"RD\",\"LD\",\"LU\",\"U2\",\"R2\",\"D2\",\"L2\",\"Undef2\"};\nDIR dir_array[5] = {U,R,D,L,Undef},convert[NUM] = {Undef,Undef,Undef,Undef,Undef,Undef,Undef,Undef,U,R,D,L,Undef};\nDIR2 swap_dir2[8] = {Undef2,Undef2,Undef2,Undef2,UR,DR,DL,UL};\nDIR2 dir2_array[NUM] = {UR,DR,DL,UL,RU,RD,LD,LU,U2,R2,D2,L2,Undef2};\n//■時計回りの移動\nDIR cw_move[8][4] = {{D,L,U,R}, //UR発\n\t\t\t\t{L,U,R,D}, //DR発\n\t\t\t\t{U,R,D,L}, //DL発\n\t\t\t\t{R,D,L,U}, //UL発\n\n\t\t\t\t{D,L,U,R}, //RU発\n\t\t\t\t{L,U,R,D}, //RD発\n\t\t\t\t{U,R,D,L}, //LD発\n\t\t\t\t{R,D,L,U} //LU発\n\n\t\t\t\t};\n\n//■反時計回りの移動\nDIR rev_move[8][4] = {{L,D,R,U}, //UR発\n\t\t\t\t\t {U,L,D,R}, //DR発\n\t\t\t\t\t {R,U,L,D}, //DL発\n\t\t\t\t\t {D,R,U,L}, //UL発\n\n\t\t\t\t\t {L,D,R,U}, //RU発\n\t\t\t\t\t {U,L,D,R}, //RD発\n\t\t\t\t\t {R,U,L,D}, //LD発\n\t\t\t\t\t {D,R,U,L} //LU\n};\n\n\nstruct LOC{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Trans{\n\tTrans(int arg_pre_row,int arg_pre_col,int arg_now_row,int arg_now_col){\n\n\t\tpre_row = arg_pre_row;\n\t\tpre_col = arg_pre_col;\n\t\tnow_row = arg_now_row;\n\t\tnow_col = arg_now_col;\n\t}\n\tint pre_row,pre_col,now_row,now_col;\n};\n\n\n//文字列の回転の大きさ\nstruct Roll{\n\n\tll num_roll;\n\tDIR2 base_dir; //■■■最初に寄った角と、その直前の動きでタイルの塊の形が決まる(たとえばR→UとU→Rは異なる)■■■\n};\n\nint N,LEN;\nint close_pos[SIZE2]; //開き括弧に対応する閉じ括弧の位置\nint MAP[SIZE][SIZE];\nint base_corn[NUM][SIZE][SIZE];\nchar line[SIZE2],input[SIZE][SIZE];\nll POW[MAX+1];\nLOC just_move[2][MAX+1][NUM][SIZE][SIZE]; //just_move[時計/反時計][2べき][最初に寄った角][行][列] = (遷移先行、遷移先列) ■余りの出ない回転\nLOC MOVE[NUM][MAX+1][SIZE][SIZE];\nint pow_corn[NUM][MAX+1];\nRoll roll_pow[NUM][MAX+1]; //ダブリング用テーブル\n\n//余りが出た時の、寄る角の遷移表\nDIR2 cw_next[4][4] = {{Undef2,DR,DL,UL},//UR\n\t\t\t\t\t {Undef2,DL,UL,UR}, //DR\n\t\t\t\t\t {Undef2,UL,UR,DR}, //DL\n\t\t\t\t\t {Undef2,UR,DR,DL}, //UL\n\t\t\t\t\t };\n\nDIR2 rev_next[4][4] = {{Undef2,UL,DL,DR},//UR\n\t\t\t\t\t {Undef2,UR,UL,DL}, //DR\n\t\t\t\t\t {Undef2,DR,UR,UL}, //DL\n\t\t\t\t\t {Undef2,DL,DR,UR}, //UL\n\t\t\t\t\t };\n//U,R,D,Lに移動した時の,次の角、\nDIR2 corner_next[4][4] = {{UR,UR,DR,UL}, //UR\n\t\t \t \t {UR,DR,DR,DL}, //DR\n\t\t\t\t\t\t {UL,DR,DL,DL}, //DL\n\t\t\t\t\t\t {UL,UR,DL,UL} //UL\n\t\t\t\t \t \t };\n\nbool is_corner(DIR2 dir2){\n\n\treturn (dir2 == UR || dir2 == DR || dir2 == DL || dir2 == UL || dir2 == RU || dir2 == RD || dir2 == LD || dir2 == LU);\n}\n\n\nbool is_special(DIR2 dir2){\n\n\treturn (dir2 == RU || dir2 == RD || dir2 == LD || dir2 == LU);\n}\n\nvoid init_table(int work[SIZE][SIZE]){\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\twork[row][col] = -1;\n\t\t}\n\t}\n}\n\nvoid copy_table(int from[SIZE][SIZE],int to[SIZE][SIZE]){\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tto[row][col] = from[row][col];\n\t\t}\n\t}\n}\n\n//遷移元(行,列)と遷移先(行,列)の対応情報取得\nvector<Trans> calc_trans(int from[SIZE][SIZE],int to[SIZE][SIZE]){\n\n\tvector<Trans> ret;\n\tLOC base[N*N];\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(from[row][col] == -1)continue;\n\n\t\t\tbase[from[row][col]].set(row,col); //ある数字が、元々どこにいたか\n\t\t}\n\t}\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(to[row][col] == -1)continue;\n\n\t\t\tint tmp = to[row][col];\n\t\t\tint pre_row = base[tmp].row;\n\t\t\tint pre_col = base[tmp].col;\n\n\t\t\tret.push_back(Trans(pre_row,pre_col,row,col));\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid calc_move(DIR dir,int from[SIZE][SIZE],int to[SIZE][SIZE]){\n\n\tinit_table(to);\n\n\tif(dir == U){ //上詰め\n\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int row = 0; row < N; row++){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int row = 0; row < vec.size(); row++){\n\n\t\t\t\tto[row][col] = vec[row];\n\t\t\t}\n\t\t}\n\n\t}else if(dir == R){ //右詰め\n\n\t\tfor(int row = 0; row < N; row++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int col = N-1; col >= 0; col--){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tto[row][(N-1)-i] = vec[i];\n\t\t\t}\n\t\t}\n\n\t}else if(dir == D){ //下詰め\n\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int row = N-1; row >= 0; row--){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tto[(N-1)-i][col] = vec[i];\n\t\t\t}\n\t\t}\n\n\t}else{ //dir == L //左詰め\n\n\t\tfor(int row = 0; row < N; row++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int col = 0; col < N; col++){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int col = 0; col < vec.size(); col++){\n\n\t\t\t\tto[row][col] = vec[col];\n\t\t\t}\n\t\t}\n\t}\n}\n\n\n//遷移先テーブル作り(ダブリング)\nvoid init(){\n\n\tint from[SIZE][SIZE],to[SIZE][SIZE],work[SIZE][SIZE];\n\tint TMP[SIZE][SIZE],WORK[SIZE][SIZE];\n\n\tinit_table(from);\n\n\tint IND = 0;\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(input[row][col] == '.'){\n\n\t\t\t\tfrom[row][col] = -1;\n\t\t\t}else{\n\n\t\t\t\tfrom[row][col] = IND++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//Undef\n\tcopy_table(from,base_corn[Undef2]);\n\n\tint calc_IND[4] = {8,9,10,11};\n\n\t//上下左右\n\tfor(int i = 0; i < 4; i++){\n\n\t\tcopy_table(from,work);\n\t\tcalc_move(dir_array[i],work,to);\n\n\t\tcopy_table(to,base_corn[calc_IND[i]]);\n\t}\n\n\tDIR make_map[8][4] = {{U,R},{D,R},{D,L},{U,L},{R,U},{R,D},{L,D},{L,U}};\n\n\t//■■[初めて寄った角,そこへ至る動き]で位置を区別する■■\n\tfor(int q = 0; q < 8; q++){\n\n\t\t//■まずは角へ移動\n\t\tcopy_table(from,work);\n\t\tfor(int k = 0; k < 2; k++){\n\t\t\tcalc_move(make_map[q][k],work,to);\n\t\t\tcopy_table(to,work);\n\t\t}\n\n\t\tcopy_table(work,base_corn[q]);\n\n\n\t\t//■現在の状態から、時計、反時計に一周する\n\t\tDIR2 now_dir = dir2_array[q];\n\t\tif(is_special(now_dir)){\n\n\t\t\tnow_dir = swap_dir2[now_dir];\n\t\t}\n\n\t\t//時計\n\t\t{\n\t\t\tcopy_table(work,TMP);\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tcalc_move(cw_move[now_dir][i],TMP,WORK);\n\t\t\t\tcopy_table(WORK,TMP);\n\t\t\t}\n\n\t\t\tvector<Trans> ret = calc_trans(work,TMP);\n\n\t\t\tfor(int k = 0; k < ret.size(); k++){\n\n\t\t\t\tjust_move[Clock][0][q][ret[k].pre_row][ret[k].pre_col].set(ret[k].now_row,ret[k].now_col);\n\t\t\t}\n\n\t\t}\n\n\t\t//反時計\n\t\t{\n\t\t\tcopy_table(work,TMP);\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tcalc_move(rev_move[now_dir][i],TMP,WORK);\n\t\t\t\tcopy_table(WORK,TMP);\n\t\t\t}\n\n\t\t\tvector<Trans> ret = calc_trans(work,TMP);\n\n\t\t\tfor(int k = 0; k < ret.size(); k++){\n\n\t\t\t\tjust_move[Rev][0][q][ret[k].pre_row][ret[k].pre_col].set(ret[k].now_row,ret[k].now_col);\n\t\t\t}\n\t\t}\n\t}\n\n\t//■ダブリング計算(角にいる場合のみ)\n\tfor(int d = 1; d <= MAX; d++){\n\t\tfor(int i = 0; i < 8; i++){\n\t\t\tfor(int k = 0; k < 2; k++){\n\t\t\t\tType type = type_array[k];\n\n\t\t\t\tfor(int row = 0; row < N; row++){\n\t\t\t\t\tfor(int col = 0; col < N; col++){\n\n\t\t\t\t\t\tint mid_row = just_move[type][d-1][i][row][col].row;\n\t\t\t\t\t\tint mid_col = just_move[type][d-1][i][row][col].col;\n\n\t\t\t\t\t\tint to_row = just_move[type][d-1][i][mid_row][mid_col].row;\n\t\t\t\t\t\tint to_col = just_move[type][d-1][i][mid_row][mid_col].col;\n\n\t\t\t\t\t\tjust_move[type][d][i][row][col].set(to_row,to_col);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nbool isNUM(char ch){\n\n\treturn ch >= '0' && ch <= '9';\n}\n\nbool is_ud(vector<char> vec){\n\n\treturn vec.size() > 0 && (vec.back() == 'U' || vec.back() == 'D');\n}\n\nbool is_lr(vector<char> vec){\n\n\treturn vec.size() > 0 && (vec.back() == 'L' || vec.back() == 'R');\n}\n\n//■出発位置ごとに、回転数と余り、移動先を計算する\nvector<Roll> calc_normal(int head,int tail){\n\n\tvector<Roll> ret;\n\n\tfor(int a = 0; a < 13; a++){\n\n\t\tDIR2 now_dir = dir2_array[a];\n\t\tDIR2 base_dir = dir2_array[a];\n\n\t\tif(is_special(base_dir)){\n\n\t\t\tnow_dir = swap_dir2[now_dir];\n\t\t}\n\n\t\tvector<char> vec;\n\n\t\tll num_roll = 0;\n\n\t\tfor(int i = head; i <= tail; i++){\n\n\t\t\tif(!is_corner(base_dir)){ //まだ角に寄ってない\n\n\t\t\t\tif(now_dir == Undef2){ //■■初期状態\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = U2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = R2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = D2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = L2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else if(now_dir == U2){\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = UR;\n\t\t\t\t\t\tbase_dir = UR;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = D2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = UL;\n\t\t\t\t\t\tbase_dir = UL;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else if(now_dir == R2){\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = UR; //■■■■■■■■■■■■■■now_dirはUR,RUをまとめて、base_dirはURとURを区別するので注意■■■■■■■■■■■■■■\n\t\t\t\t\t\tbase_dir = RU; //■■以下同様に注意■■\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = DR;\n\t\t\t\t\t\tbase_dir = RD;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = L2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else if(now_dir == D2){\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = U2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = DR;\n\t\t\t\t\t\tbase_dir = DR;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = DL;\n\t\t\t\t\t\tbase_dir = DL;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else{ //now_dir == L2\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = UL;\n\t\t\t\t\t\tbase_dir = LU;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = R2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = DL;\n\t\t\t\t\t\tbase_dir = LD;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{ //■角に寄っている\n\n\t\t\t\t//意味のない動きをはじく\n\t\t\t\tif(now_dir == UR){\n\n\t\t\t\t\tif(line[i] == 'U' || line[i] == 'R')continue;\n\n\t\t\t\t}else if(now_dir == DR){\n\n\t\t\t\t\tif(line[i] == 'D' || line[i] == 'R')continue;\n\n\t\t\t\t}else if(now_dir == DL){\n\n\t\t\t\t\tif(line[i] == 'D' || line[i] == 'L')continue;\n\n\t\t\t\t}else if(now_dir == UL){\n\n\t\t\t\t\tif(line[i] == 'U' || line[i] == 'L')continue;\n\t\t\t\t}\n\n\t\t\t\tif(line[i] == 'L' || line[i] == 'R'){\n\n\t\t\t\t\tif(is_lr(vec)){\n\n\t\t\t\t\t\t//■反対動で、前回の移動が無効化\n\t\t\t\t\t\tvec.pop_back();\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tvec.push_back(line[i]);\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tif(is_ud(vec)){\n\n\t\t\t\t\t\t//■反対動で、前回の移動が無効化\n\t\t\t\t\t\tvec.pop_back();\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tvec.push_back(line[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tDIR tmp_dir;\n\t\t\t\tswitch(line[i]){\n\t\t\t\tcase 'U':\n\t\t\t\t\ttmp_dir = U;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'R':\n\t\t\t\t\ttmp_dir = R;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'D':\n\t\t\t\t\ttmp_dir = D;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'L':\n\t\t\t\t\ttmp_dir = L;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\t//■現在寄っている角(基準角とは異なる→基準角は最初に寄った角)\n\t\t\t\tnow_dir = corner_next[now_dir][tmp_dir];\n\n\t\t\t\tif(vec.size() == 4){\n\n\t\t\t\t\tif(vec[0] == 'U'){\n\n\t\t\t\t\t\tif(vec[1] == 'R'){\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\n\t\t\t\t\t\t}else{ //vec[1] == 'L'\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}else if(vec[0] == 'D'){\n\n\t\t\t\t\t\tif(vec[1] == 'R'){\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\n\t\t\t\t\t\t}else{ //vec[1] == 'L'\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}else if(vec[0] == 'R'){\n\n\t\t\t\t\t\tif(vec[1] == 'U'){\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\n\t\t\t\t\t\t}else{ //vec[1] == 'D'\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}else{ //vec[0] == 'L'\n\n\t\t\t\t\t\tif(vec[1] == 'U'){\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tvec.clear();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tRoll roll;\n\t\troll.base_dir = base_dir;\n\n\t\tif(!is_corner(base_dir)){ //角に寄らない\n\n\t\t\troll.base_dir = now_dir; //■■注意■■\n\n\t\t}else{ //角に寄る\n\n\t\t\t//■余りの処理\n\t\t\tif(vec.size() > 0){\n\n\t\t\t\tll s = vec.size();\n\n\t\t\t\tDIR2 t_dir = base_dir;\n\n\t\t\t\tif(is_special(t_dir)){\n\n\t\t\t\t\tt_dir = swap_dir2[t_dir];\n\t\t\t\t}\n\n\t\t\t\tswitch(t_dir){\n\t\t\t\tcase UR:\n\t\t\t\t\tif(vec[0] == 'D'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase DR:\n\t\t\t\t\tif(vec[0] == 'L'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase DL:\n\t\t\t\t\tif(vec[0] == 'U'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase UL:\n\t\t\t\t\tif(vec[0] == 'R'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\troll.num_roll = num_roll;\n\t\tret.push_back(roll);\n\t}\n\treturn ret;\n}\n\nvoid move_table(Type type,DIR2 base_dir,int table[SIZE][SIZE],ll digit){\n\n\tint work[SIZE][SIZE];\n\tinit_table(work);\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(table[row][col] == -1)continue;\n\n\t\t\tint to_row = just_move[type][digit][base_dir][row][col].row;\n\t\t\tint to_col = just_move[type][digit][base_dir][row][col].col;\n\n\t\t\twork[to_row][to_col] = table[row][col];\n\t\t}\n\t}\n\n\tcopy_table(work,table);\n}\n\n\n\n//base_dirを出発点として、num_roll回転した時、現在どの角にいるか\nDIR2 calc_corner(DIR2 base_dir,ll num_roll){\n\n\tDIR2 tmp_c = base_dir;\n\tif(is_special(base_dir)){\n\n\t\ttmp_c = swap_dir2[base_dir];\n\t}\n\n\tif(abs(num_roll)%4 == 0){\n\n\t\treturn tmp_c;\n\t}\n\n\tll mod = abs(num_roll)%4;\n\n\tif(num_roll > 0){\n\n\t\treturn cw_next[tmp_c][mod];\n\n\t}else{\n\n\t\treturn rev_next[tmp_c][mod];\n\t}\n}\n\n//■実際にテーブルを回転させる\nvoid move_func(int table[SIZE][SIZE],Roll tmp_roll){\n\n\tint work[SIZE][SIZE];\n\n\t//角に到達していない場合\n\tif(!is_corner(tmp_roll.base_dir)){\n\n\t\tcalc_move(convert[tmp_roll.base_dir],table,work);\n\t\tcopy_table(work,table);\n\t\treturn;\n\t}\n\n\t//まずは角まで移動させる\n\tswitch(tmp_roll.base_dir){\n\tcase UR:\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase DR:\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase DL:\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase UL:\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase RU:\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase RD:\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase LD:\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase LU:\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\t}\n\n\tll mult = abs(tmp_roll.num_roll)/4;\n\tll mod = abs(tmp_roll.num_roll)%4;\n\n\tType type = Not;\n\tif(tmp_roll.num_roll > 0){\n\n\t\ttype = Clock;\n\t}else{\n\n\t\ttype = Rev;\n\t}\n\n\t//周回させる\n\tif(mult != 0){\n\n\t\tfor(int d = MAX; d >= 0; d--){\n\t\t\tif(POW[d] > mult)continue;\n\n\t\t\tmove_table(type,tmp_roll.base_dir,table,d);\n\t\t\tmult -= POW[d];\n\t\t}\n\t}\n\n\t//余りの処理\n\tif(mod != 0){\n\n\t\tmod = abs(mod);\n\t\tDIR2 now = tmp_roll.base_dir;\n\t\tif(is_special(now)){\n\n\t\t\tnow = swap_dir2[now];\n\t\t}\n\n\t\tfor(int i = 0; i < mod; i++){\n\n\t\t\tswitch(now){\n\t\t\tcase UR:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(D,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(L,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase DR:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(L,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(U,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase DL:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(U,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(R,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase UL:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(R,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(D,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(type == Clock){\n\n\t\t\t\tnow = cw_next[now][1];\n\n\t\t\t}else{\n\n\t\t\t\tnow = rev_next[now][1];\n\t\t\t}\n\t\t}\n\t}\n}\n\nvector<Roll> calc_E(int head,int tail){\n\n\tint left = head;\n\n\tDIR2 BASE_DIR[NUM];\n\tll NUM_ROLL[NUM];\n\n\tfor(int i = 0; i < 13; i++){\n\n\t\tBASE_DIR[i] = dir2_array[i]; //■■元々位置に合った塊の、現在の基準角\n\t\tNUM_ROLL[i] = 0;\n\t}\n\n\twhile(left <= tail){\n\n\t\tif(line[left] == '('){ //■倍化処理あり\n\n\t\t\tvector<Roll> vec = calc_E(left+1,close_pos[left]-1);\n\n\t\t\tint ind = close_pos[left]+1;\n\t\t\tll tmp_p = 0;\n\n\t\t\twhile(ind <= tail && isNUM(line[ind])){\n\n\t\t\t\ttmp_p = 10*tmp_p + (line[ind]-'0');\n\t\t\t\tind++;\n\t\t\t}\n\n\n\t\t\tll maxi = MAX;\n\t\t\twhile(POW[maxi] > tmp_p){\n\n\t\t\t\tmaxi--;\n\t\t\t}\n\n\t\t\t//ダブリングテーブル初期化\n\t\t\tfor(int d = 0; d <= maxi; d++){\n\t\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\t\troll_pow[i][d].base_dir = Undef2;\n\t\t\t\t\troll_pow[i][d].num_roll = 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/*■■■■■倍化処理■■■■■*/\n\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\troll_pow[i][0].base_dir = vec[i].base_dir;\n\t\t\t\troll_pow[i][0].num_roll = vec[i].num_roll;\n\t\t\t}\n\n\t\t\t//■ダブリング\n\t\t\tfor(int d = 1; d <= maxi; d++){\n\t\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\t\tif(is_corner(roll_pow[i][d-1].base_dir)){ //既に角に寄っている場合\n\n\t\t\t\t\t\tDIR2 tmp_c = roll_pow[i][d-1].base_dir;\n\t\t\t\t\t\tDIR2 next_c = calc_corner(tmp_c,roll_pow[i][d-1].num_roll);\n\n\t\t\t\t\t\troll_pow[i][d].base_dir = tmp_c; //■一度角に着いたら、以後ずっとそこを基準にする\n\t\t\t\t\t\troll_pow[i][d].num_roll = roll_pow[i][d-1].num_roll+roll_pow[next_c][d-1].num_roll;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tDIR2 mid = roll_pow[i][d-1].base_dir; //まずはmidへ\n\t\t\t\t\t\tDIR2 tmp = roll_pow[mid][d-1].base_dir;\n\n\t\t\t\t\t\troll_pow[i][d].base_dir = tmp;\n\n\t\t\t\t\t\tif(is_corner(tmp)){ //初めて角に着く\n\n\t\t\t\t\t\t\troll_pow[i][d].num_roll = roll_pow[mid][d-1].num_roll;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\tDIR2 base_dir = BASE_DIR[i]; //■■呼び出し元の位置iの基準角\n\t\t\t\tDIR2 now_dir;\n\t\t\t\tll num_roll = NUM_ROLL[i];\n\n\t\t\t\tif(is_corner(base_dir)){\n\n\t\t\t\t\tnow_dir = calc_corner(base_dir,num_roll); //■位置iの塊が、現在寄っている角\n\t\t\t\t}\n\n\t\t\t\tll rest = tmp_p;\n\t\t\t\tfor(int d = maxi; d >= 0; d--){\n\t\t\t\t\tif(POW[d] > rest)continue;\n\n\t\t\t\t\tif(is_corner(base_dir)){ //基準角が決まっている\n\n\t\t\t\t\t\tnum_roll += roll_pow[now_dir][d].num_roll;\n\t\t\t\t\t\tnow_dir = calc_corner(base_dir,num_roll);\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tbase_dir = roll_pow[base_dir][d].base_dir; //次の基準角\n\t\t\t\t\t\tif(is_corner(base_dir)){ //新しく基準角が決まる\n\n\t\t\t\t\t\t\tnum_roll += roll_pow[base_dir][d].num_roll;\n\t\t\t\t\t\t\tnow_dir = calc_corner(base_dir,num_roll);\n\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\trest -= POW[d];\n\t\t\t\t}\n\n\t\t\t\tBASE_DIR[i] = base_dir;\n\t\t\t\tNUM_ROLL[i] = num_roll;\n\t\t\t}\n\n\t\t\tleft = ind;\n\n\t\t}else{ //■倍化処理なし\n\n\t\t\tint right = left;\n\t\t\twhile(right <= tail && line[right] != '(')right++;\n\t\t\tright--;\n\n\t\t\t//この間に括弧なし\n\t\t\tvector<Roll> vec = calc_normal(left,right);\n\n\t\t\tfor(int i = 0; i < 13; i++){\n\t\t\t\tif(is_corner(BASE_DIR[i])){\n\n\t\t\t\t\tDIR2 now_dir = calc_corner(BASE_DIR[i],NUM_ROLL[i]);\n\t\t\t\t\tNUM_ROLL[i] += vec[now_dir].num_roll; //既に角にいる場合は、回転数だけ足す\n\n\t\t\t\t}else{\n\n\t\t\t\t\tDIR2 base_dir = vec[BASE_DIR[i]].base_dir;\n\t\t\t\t\tif(is_corner(base_dir)){\n\n\t\t\t\t\t\tNUM_ROLL[i] += vec[BASE_DIR[i]].num_roll;\n\t\t\t\t\t}\n\t\t\t\t\tBASE_DIR[i] = base_dir;\n\t\t\t\t}\n\t\t\t}\n\t\t\tleft = right+1;\n\t\t}\n\t}\n\n\tvector<Roll> ret;\n\n\tfor(int i = 0; i < 13; i++){\n\t\tRoll tmp_roll;\n\t\ttmp_roll.base_dir = BASE_DIR[i];\n\t\ttmp_roll.num_roll = NUM_ROLL[i];\n\n\t\tret.push_back(tmp_roll);\n\t}\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tfor(int row = 0; row < N; row++){\n\n\t\tscanf(\"%s\",input[row]);\n\t}\n\n\tscanf(\"%s\",line);\n\n\tinit();\n\n\tfor(LEN = 0; line[LEN] != '\\0'; LEN++);\n\n\t//括弧の対応を前計算\n\tstack<int> ST;\n\tfor(int i = 0; i < LEN; i++){\n\t\tif(line[i] == '('){\n\n\t\t\tST.push(i);\n\n\t\t}else if(line[i] == ')'){\n\n\t\t\tclose_pos[ST.top()] = i;\n\t\t\tST.pop();\n\t\t}\n\t}\n\n\tinit_table(MAP);\n\n\tmap<int,pair<int,int>> MEMO;\n\n\tint IND = 0;\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(input[row][col] == '.'){\n\n\t\t\t\tMAP[row][col] = -1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tMAP[row][col] = IND;\n\t\t\tMEMO[IND] = make_pair(row,col);\n\t\t\tIND++;\n\t\t}\n\t}\n\n\n\tvector<Roll> ret = calc_E(0,LEN-1);\n\n\tmove_func(MAP,ret[Undef2]);\n\n\tchar ANS[SIZE][SIZE];\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tANS[row][col] = '.';\n\t\t}\n\t}\n\n\t//回転処理\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(MAP[row][col] == -1)continue;\n\n\t\t\tint tmp_num = MAP[row][col];\n\t\t\tint pre_row = MEMO[tmp_num].first;\n\t\t\tint pre_col = MEMO[tmp_num].second;\n\n\t\t\tANS[row][col] = input[pre_row][pre_col];\n\n\t\t}\n\t}\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tprintf(\"%c\",ANS[row][col]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= MAX; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 41360, "score_of_the_acc": -1.1955, "final_rank": 8 }, { "submission_id": "aoj_1638_6055611", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define NUM 13\n#define MAX 60\n#define SIZE 55\n#define SIZE2 1005\n\nenum Type{\n\tClock,\n\tRev,\n\tNot,\n};\n\nType type_array[3] = {Clock,Rev,Not};\n\nenum DIR{\n\tU,\n\tR,\n\tD,\n\tL,\n\tUndef,\n};\n\nenum DIR2{\n\tUR,\n\tDR,\n\tDL,\n\tUL,\n\t//■■■■URとRUは同じでない■■■■\n\tRU,\n\tRD,\n\tLD,\n\tLU,\n\t//■■■■URとRUは同じでない■■■■\n\tU2,\n\tR2,\n\tD2,\n\tL2,\n\tUndef2,\n};\n\nbool DEBUG = true;\n\nstring DEB[NUM] = {\"UR\",\"DR\",\"DL\",\"UL\",\"RU\",\"RD\",\"LD\",\"LU\",\"U2\",\"R2\",\"D2\",\"L2\",\"Undef2\"};\nDIR dir_array[5] = {U,R,D,L,Undef},convert[NUM] = {Undef,Undef,Undef,Undef,Undef,Undef,Undef,Undef,U,R,D,L,Undef};\nDIR2 swap_dir2[8] = {Undef2,Undef2,Undef2,Undef2,UR,DR,DL,UL};\nDIR2 dir2_array[NUM] = {UR,DR,DL,UL,RU,RD,LD,LU,U2,R2,D2,L2,Undef2};\n//■時計回りの移動\nDIR cw_move[8][4] = {{D,L,U,R}, //UR発\n\t\t\t\t{L,U,R,D}, //DR発\n\t\t\t\t{U,R,D,L}, //DL発\n\t\t\t\t{R,D,L,U}, //UL発\n\n\t\t\t\t{D,L,U,R}, //RU発\n\t\t\t\t{L,U,R,D}, //RD発\n\t\t\t\t{U,R,D,L}, //LD発\n\t\t\t\t{R,D,L,U} //LU発\n\n\t\t\t\t};\n\n//■反時計回りの移動\nDIR rev_move[8][4] = {{L,D,R,U}, //UR発\n\t\t\t\t\t {U,L,D,R}, //DR発\n\t\t\t\t\t {R,U,L,D}, //DL発\n\t\t\t\t\t {D,R,U,L}, //UL発\n\n\t\t\t\t\t {L,D,R,U}, //RU発\n\t\t\t\t\t {U,L,D,R}, //RD発\n\t\t\t\t\t {R,U,L,D}, //LD発\n\t\t\t\t\t {D,R,U,L} //LU\n};\n\n\nstruct LOC{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Trans{\n\tTrans(int arg_pre_row,int arg_pre_col,int arg_now_row,int arg_now_col){\n\n\t\tpre_row = arg_pre_row;\n\t\tpre_col = arg_pre_col;\n\t\tnow_row = arg_now_row;\n\t\tnow_col = arg_now_col;\n\t}\n\tint pre_row,pre_col,now_row,now_col;\n};\n\n\n//文字列の回転の大きさ\nstruct Roll{\n\n\tll num_roll;\n\tDIR2 base_dir; //■■■最初に寄った角と、その直前の動きでタイルの塊の形が決まる■■■\n};\n\nint N,LEN;\nint close_pos[SIZE2]; //開き括弧に対応する閉じ括弧の位置\nint MAP[SIZE][SIZE];\nint base_corn[NUM][SIZE][SIZE];\nchar line[SIZE2],input[SIZE][SIZE];\nll POW[MAX+1];\nLOC just_move[2][MAX+1][NUM][SIZE][SIZE]; //just_move[時計/反時計][2べき][最初に寄った角][行][列] = (遷移先行、遷移先列) ■余りの出ない回転\nLOC MOVE[NUM][MAX+1][SIZE][SIZE];\nint pow_corn[NUM][MAX+1];\nRoll roll_pow[NUM][MAX+1]; //ダブリング用テーブル\n\n//余りが出た時の、寄る角の遷移表\nDIR2 cw_next[4][4] = {{Undef2,DR,DL,UL},//UR\n\t\t\t\t\t {Undef2,DL,UL,UR}, //DR\n\t\t\t\t\t {Undef2,UL,UR,DR}, //DL\n\t\t\t\t\t {Undef2,UR,DR,DL}, //UL\n\t\t\t\t\t };\n\nDIR2 rev_next[4][4] = {{Undef2,UL,DL,DR},//UR\n\t\t\t\t\t {Undef2,UR,UL,DL}, //DR\n\t\t\t\t\t {Undef2,DR,UR,UL}, //DL\n\t\t\t\t\t {Undef2,DL,DR,UR}, //UL\n\t\t\t\t\t };\n//U,R,D,Lに移動した時の,次の角、\nDIR2 corner_next[4][4] = {{UR,UR,DR,UL}, //UR\n\t\t \t \t {UR,DR,DR,DL}, //DR\n\t\t\t\t\t\t {UL,DR,DL,DL}, //DL\n\t\t\t\t\t\t {UL,UR,DL,UL} //UL\n\t\t\t\t \t \t };\n\n\nvoid outPut(int table[SIZE][SIZE]){\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tprintf(\" %d\",table[row][col]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\n\nbool is_corner(DIR2 dir2){\n\n\treturn (dir2 == UR || dir2 == DR || dir2 == DL || dir2 == UL || dir2 == RU || dir2 == RD || dir2 == LD || dir2 == LU);\n}\n\n\nbool is_special(DIR2 dir2){\n\n\treturn (dir2 == RU || dir2 == RD || dir2 == LD || dir2 == LU);\n}\n\nvoid init_table(int work[SIZE][SIZE]){\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\twork[row][col] = -1;\n\t\t}\n\t}\n}\n\nvoid copy_table(int from[SIZE][SIZE],int to[SIZE][SIZE]){\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tto[row][col] = from[row][col];\n\t\t}\n\t}\n}\n\n//遷移元(行,列)と遷移先(行,列)の対応情報取得\nvector<Trans> calc_trans(int from[SIZE][SIZE],int to[SIZE][SIZE]){\n\n\tvector<Trans> ret;\n\tLOC base[N*N];\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(from[row][col] == -1)continue;\n\n\t\t\tbase[from[row][col]].set(row,col); //ある数字が、元々どこにいたか\n\t\t}\n\t}\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(to[row][col] == -1)continue;\n\n\t\t\tint tmp = to[row][col];\n\t\t\tint pre_row = base[tmp].row;\n\t\t\tint pre_col = base[tmp].col;\n\n\t\t\tret.push_back(Trans(pre_row,pre_col,row,col));\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid calc_move(DIR dir,int from[SIZE][SIZE],int to[SIZE][SIZE]){\n\n\tinit_table(to);\n\n\tif(dir == U){ //上詰め\n\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int row = 0; row < N; row++){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int row = 0; row < vec.size(); row++){\n\n\t\t\t\tto[row][col] = vec[row];\n\t\t\t}\n\t\t}\n\n\t}else if(dir == R){ //右詰め\n\n\t\tfor(int row = 0; row < N; row++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int col = N-1; col >= 0; col--){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tto[row][(N-1)-i] = vec[i];\n\t\t\t}\n\t\t}\n\n\t}else if(dir == D){ //下詰め\n\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int row = N-1; row >= 0; row--){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tto[(N-1)-i][col] = vec[i];\n\t\t\t}\n\t\t}\n\n\t}else{ //dir == L //左詰め\n\n\t\tfor(int row = 0; row < N; row++){\n\t\t\tvector<int> vec;\n\t\t\tfor(int col = 0; col < N; col++){\n\t\t\t\tif(from[row][col] != -1){\n\n\t\t\t\t\tvec.push_back(from[row][col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int col = 0; col < vec.size(); col++){\n\n\t\t\t\tto[row][col] = vec[col];\n\t\t\t}\n\t\t}\n\t}\n}\n\n\n//遷移先テーブル作り(ダブリング)\nvoid init(){\n\n\tint from[SIZE][SIZE],to[SIZE][SIZE],work[SIZE][SIZE];\n\tint TMP[SIZE][SIZE],WORK[SIZE][SIZE];\n\n\tinit_table(from);\n\n\tint IND = 0;\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(input[row][col] == '.'){\n\n\t\t\t\tfrom[row][col] = -1;\n\t\t\t}else{\n\n\t\t\t\tfrom[row][col] = IND++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//Undef\n\tcopy_table(from,base_corn[Undef2]);\n\n\tint calc_IND[4] = {8,9,10,11};\n\n\t//上下左右\n\tfor(int i = 0; i < 4; i++){\n\n\t\tcopy_table(from,work);\n\t\tcalc_move(dir_array[i],work,to);\n\n\t\tcopy_table(to,base_corn[calc_IND[i]]);\n\t}\n\n\tDIR make_map[8][4] = {{U,R},{D,R},{D,L},{U,L},{R,U},{R,D},{L,D},{L,U}};\n\n\t//■■[初めて寄った角,そこへ至る動き]で位置を区別する■■\n\tfor(int q = 0; q < 8; q++){\n\n\t\t//■まずは角へ移動\n\t\tcopy_table(from,work);\n\t\tfor(int k = 0; k < 2; k++){\n\t\t\tcalc_move(make_map[q][k],work,to);\n\t\t\tcopy_table(to,work);\n\t\t}\n\n\t\tcopy_table(work,base_corn[q]);\n\n\n\t\t//■現在の状態から、時計、反時計に一周する\n\t\tDIR2 now_dir = dir2_array[q];\n\t\tif(is_special(now_dir)){\n\n\t\t\tnow_dir = swap_dir2[now_dir];\n\t\t}\n\n\t\t//時計\n\t\t{\n\t\t\tcopy_table(work,TMP);\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tcalc_move(cw_move[now_dir][i],TMP,WORK);\n\t\t\t\tcopy_table(WORK,TMP);\n\t\t\t}\n\n\t\t\tvector<Trans> ret = calc_trans(work,TMP);\n\n\t\t\tfor(int k = 0; k < ret.size(); k++){\n\n\t\t\t\tjust_move[Clock][0][q][ret[k].pre_row][ret[k].pre_col].set(ret[k].now_row,ret[k].now_col);\n\t\t\t}\n\n\t\t}\n\n\t\t//反時計\n\t\t{\n\t\t\tcopy_table(work,TMP);\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tcalc_move(rev_move[now_dir][i],TMP,WORK);\n\t\t\t\tcopy_table(WORK,TMP);\n\t\t\t}\n\n\t\t\tvector<Trans> ret = calc_trans(work,TMP);\n\n\t\t\tfor(int k = 0; k < ret.size(); k++){\n\n\t\t\t\tjust_move[Rev][0][q][ret[k].pre_row][ret[k].pre_col].set(ret[k].now_row,ret[k].now_col);\n\t\t\t}\n\t\t}\n\t}\n\n\t//■ダブリング計算(角にいる場合のみ)\n\tfor(int d = 1; d <= MAX; d++){\n\t\tfor(int i = 0; i < 8; i++){\n\t\t\tfor(int k = 0; k < 2; k++){\n\t\t\t\tType type = type_array[k];\n\n\t\t\t\tfor(int row = 0; row < N; row++){\n\t\t\t\t\tfor(int col = 0; col < N; col++){\n\n\t\t\t\t\t\tint mid_row = just_move[type][d-1][i][row][col].row;\n\t\t\t\t\t\tint mid_col = just_move[type][d-1][i][row][col].col;\n\n\t\t\t\t\t\tint to_row = just_move[type][d-1][i][mid_row][mid_col].row;\n\t\t\t\t\t\tint to_col = just_move[type][d-1][i][mid_row][mid_col].col;\n\n\t\t\t\t\t\tjust_move[type][d][i][row][col].set(to_row,to_col);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nbool isNUM(char ch){\n\n\treturn ch >= '0' && ch <= '9';\n}\n\nbool is_ud(vector<char> vec){\n\n\treturn vec.size() > 0 && (vec.back() == 'U' || vec.back() == 'D');\n}\n\nbool is_lr(vector<char> vec){\n\n\treturn vec.size() > 0 && (vec.back() == 'L' || vec.back() == 'R');\n}\n\n//■出発位置ごとに、回転数と余り、移動先を計算する\nvector<Roll> calc_normal(int head,int tail){\n\n\tvector<Roll> ret;\n\n\tfor(int a = 0; a < 13; a++){\n\n\t\tDIR2 now_dir = dir2_array[a];\n\t\tDIR2 base_dir = dir2_array[a];\n\n\t\tif(is_special(base_dir)){\n\n\t\t\tnow_dir = swap_dir2[now_dir];\n\t\t}\n\n\t\tDIR2 first_base = base_dir;\n\t\tDIR2 first_now = now_dir;\n\n\t\tvector<char> vec;\n\n\t\tll num_roll = 0;\n\n\t\tfor(int i = head; i <= tail; i++){\n\n\t\t\tif(!is_corner(base_dir)){ //まだ角に寄ってない\n\n\t\t\t\tif(now_dir == Undef2){ //■■初期状態\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = U2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = R2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = D2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = L2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else if(now_dir == U2){\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = UR;\n\t\t\t\t\t\tbase_dir = UR;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = D2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = UL;\n\t\t\t\t\t\tbase_dir = UL;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else if(now_dir == R2){\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = UR; //■■■■■■■■■■■■■■now_dirはUR,RUをまとめて、base_dirはURとURを区別するので注意■■■■■■■■■■■■■■\n\t\t\t\t\t\tbase_dir = RU; //■■以下同様に注意■■\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = DR;\n\t\t\t\t\t\tbase_dir = RD;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = L2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else if(now_dir == D2){\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = U2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = DR;\n\t\t\t\t\t\tbase_dir = DR;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\tnow_dir = DL;\n\t\t\t\t\t\tbase_dir = DL;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t}else{ //now_dir == L2\n\n\t\t\t\t\tswitch(line[i]){\n\t\t\t\t\tcase 'U':\n\t\t\t\t\t\tnow_dir = UL;\n\t\t\t\t\t\tbase_dir = LU;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'R':\n\t\t\t\t\t\tnow_dir = R2;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'D':\n\t\t\t\t\t\tnow_dir = DL;\n\t\t\t\t\t\tbase_dir = LD;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'L':\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{ //■角に寄っている\n\n\t\t\t\t//意味のない動きをはじく\n\t\t\t\tif(now_dir == UR){\n\n\t\t\t\t\tif(line[i] == 'U' || line[i] == 'R')continue;\n\n\t\t\t\t}else if(now_dir == DR){\n\n\t\t\t\t\tif(line[i] == 'D' || line[i] == 'R')continue;\n\n\t\t\t\t}else if(now_dir == DL){\n\n\t\t\t\t\tif(line[i] == 'D' || line[i] == 'L')continue;\n\n\t\t\t\t}else if(now_dir == UL){\n\n\t\t\t\t\tif(line[i] == 'U' || line[i] == 'L')continue;\n\t\t\t\t}\n\n\t\t\t\tif(line[i] == 'L' || line[i] == 'R'){\n\n\t\t\t\t\tif(is_lr(vec)){\n\n\t\t\t\t\t\t//■反対動で、前回の移動が無効化\n\t\t\t\t\t\tvec.pop_back();\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tvec.push_back(line[i]);\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tif(is_ud(vec)){\n\n\t\t\t\t\t\t//■反対動で、前回の移動が無効化\n\t\t\t\t\t\tvec.pop_back();\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tvec.push_back(line[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tDIR tmp_dir;\n\t\t\t\tswitch(line[i]){\n\t\t\t\tcase 'U':\n\t\t\t\t\ttmp_dir = U;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'R':\n\t\t\t\t\ttmp_dir = R;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'D':\n\t\t\t\t\ttmp_dir = D;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'L':\n\t\t\t\t\ttmp_dir = L;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\t//■現在寄っている角(基準角とは異なる→基準角は最初に寄った角)\n\t\t\t\tnow_dir = corner_next[now_dir][tmp_dir];\n\n\t\t\t\tif(vec.size() == 4){\n\n\t\t\t\t\tif(vec[0] == 'U'){\n\n\t\t\t\t\t\tif(vec[1] == 'R'){\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\n\t\t\t\t\t\t}else{ //vec[1] == 'L'\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}else if(vec[0] == 'D'){\n\n\t\t\t\t\t\tif(vec[1] == 'R'){\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\n\t\t\t\t\t\t}else{ //vec[1] == 'L'\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}else if(vec[0] == 'R'){\n\n\t\t\t\t\t\tif(vec[1] == 'U'){\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\n\t\t\t\t\t\t}else{ //vec[1] == 'D'\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}else{ //vec[0] == 'L'\n\n\t\t\t\t\t\tif(vec[1] == 'U'){\n\n\t\t\t\t\t\t\tnum_roll += 4;\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tnum_roll -= 4;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tvec.clear();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tRoll roll;\n\t\troll.base_dir = base_dir;\n\n\t\tif(!is_corner(base_dir)){ //角に寄らない\n\n\t\t\troll.base_dir = now_dir; //■■注意■■\n\n\t\t}else{ //角に寄る\n\n\t\t\t//■余りの処理\n\t\t\tif(vec.size() > 0){\n\n\t\t\t\tll s = vec.size();\n\n\t\t\t\tDIR2 t_dir = base_dir;\n\n\t\t\t\tif(is_special(t_dir)){\n\n\t\t\t\t\tt_dir = swap_dir2[t_dir];\n\t\t\t\t}\n\n\t\t\t\tswitch(t_dir){\n\t\t\t\tcase UR:\n\t\t\t\t\tif(vec[0] == 'D'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase DR:\n\t\t\t\t\tif(vec[0] == 'L'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase DL:\n\t\t\t\t\tif(vec[0] == 'U'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase UL:\n\t\t\t\t\tif(vec[0] == 'R'){\n\n\t\t\t\t\t\tnum_roll += s;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tnum_roll -= s;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\troll.num_roll = num_roll;\n\t\tret.push_back(roll);\n\t}\n\treturn ret;\n}\n\nvoid move_table(Type type,DIR2 base_dir,int table[SIZE][SIZE],ll digit){\n\n\tint work[SIZE][SIZE];\n\tinit_table(work);\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(table[row][col] == -1)continue;\n\n\t\t\tint to_row = just_move[type][digit][base_dir][row][col].row;\n\t\t\tint to_col = just_move[type][digit][base_dir][row][col].col;\n\n\t\t\twork[to_row][to_col] = table[row][col];\n\t\t}\n\t}\n\n\tcopy_table(work,table);\n}\n\n\n\n//base_dirを出発点として、num_roll回転した時、現在どの角にいるか\nDIR2 calc_corner(DIR2 base_dir,ll num_roll){\n\n\tDIR2 tmp_c = base_dir;\n\tif(is_special(base_dir)){\n\n\t\ttmp_c = swap_dir2[base_dir];\n\t}\n\n\tif(abs(num_roll)%4 == 0){\n\n\t\treturn tmp_c;\n\t}\n\n\tll mod = abs(num_roll)%4;\n\n\t//printf(\"now:%s mod:%lld\\n\",DEB[now].c_str(),mod);\n\n\tif(num_roll > 0){\n\n\t\treturn cw_next[tmp_c][mod];\n\n\t}else{\n\n\t\treturn rev_next[tmp_c][mod];\n\t}\n}\n\n//■実際にテーブルを回転させる\nvoid move_func(int table[SIZE][SIZE],Roll tmp_roll){\n\n\tint work[SIZE][SIZE];\n\n\t//角に到達していない場合\n\tif(!is_corner(tmp_roll.base_dir)){\n\n\t\t//printf(\"base_dir:%d\\n\",tmp_roll.base_dir);\n\n\t\tcalc_move(convert[tmp_roll.base_dir],table,work);\n\t\tcopy_table(work,table);\n\t\treturn;\n\t}\n\n\t//まずは角まで移動させる\n\tswitch(tmp_roll.base_dir){\n\tcase UR:\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase DR:\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase DL:\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase UL:\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase RU:\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase RD:\n\t\tcalc_move(R,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase LD:\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(D,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\tcase LU:\n\t\tcalc_move(L,table,work);\n\t\tcopy_table(work,table);\n\t\tcalc_move(U,table,work);\n\t\tcopy_table(work,table);\n\t\tbreak;\n\t}\n\n\tll mult = abs(tmp_roll.num_roll)/4;\n\tll mod = abs(tmp_roll.num_roll)%4;\n\n\t//printf(\"mult:%lld mod:%lld\\n\",mult,mod);\n\n\tType type = Not;\n\tif(tmp_roll.num_roll > 0){\n\n\t\ttype = Clock;\n\t}else{\n\n\t\ttype = Rev;\n\t}\n\n\t//周回させる\n\tif(mult != 0){\n\n\t\tfor(int d = MAX; d >= 0; d--){\n\t\t\tif(POW[d] > mult)continue;\n\n\t\t\tmove_table(type,tmp_roll.base_dir,table,d);\n\t\t\tmult -= POW[d];\n\t\t}\n\t}\n\n\t//余りの処理\n\tif(mod != 0){\n\n\t\tmod = abs(mod);\n\t\tDIR2 now = tmp_roll.base_dir;\n\t\tif(is_special(now)){\n\n\t\t\tnow = swap_dir2[now];\n\t\t}\n\n\t\tfor(int i = 0; i < mod; i++){\n\n\t\t\tswitch(now){\n\t\t\tcase UR:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(D,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(L,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase DR:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(L,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(U,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase DL:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(U,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(R,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase UL:\n\t\t\t\tif(type == Clock){\n\n\t\t\t\t\tcalc_move(R,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_move(D,table,work);\n\t\t\t\t\tcopy_table(work,table);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(type == Clock){\n\n\t\t\t\tnow = cw_next[now][1];\n\n\t\t\t}else{\n\n\t\t\t\tnow = rev_next[now][1];\n\t\t\t}\n\t\t}\n\t}\n}\n\nvector<Roll> calc_E(int head,int tail){\n\n\tint left = head;\n\n\tDIR2 BASE_DIR[NUM];\n\tll NUM_ROLL[NUM];\n\n\tfor(int i = 0; i < 13; i++){\n\n\t\tBASE_DIR[i] = dir2_array[i]; //■■元々位置に合った塊の、現在の基準角\n\t\tNUM_ROLL[i] = 0;\n\t}\n\n\twhile(left <= tail){\n\n\t\tif(line[left] == '('){ //■倍化処理あり\n\n\t\t\tvector<Roll> vec = calc_E(left+1,close_pos[left]-1);\n\n\t\t\tint ind = close_pos[left]+1;\n\t\t\tll tmp_p = 0;\n\n\t\t\twhile(ind <= tail && isNUM(line[ind])){\n\n\t\t\t\ttmp_p = 10*tmp_p + (line[ind]-'0');\n\t\t\t\tind++;\n\t\t\t}\n\n\n\t\t\tll maxi = MAX;\n\t\t\twhile(POW[maxi] > tmp_p){\n\n\t\t\t\tmaxi--;\n\t\t\t}\n\n\t\t\t//ダブリングテーブル初期化\n\t\t\tfor(int d = 0; d <= maxi; d++){\n\t\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\t\troll_pow[i][d].base_dir = Undef2;\n\t\t\t\t\troll_pow[i][d].num_roll = 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/*■■■■■倍化処理■■■■■*/\n\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\troll_pow[i][0].base_dir = vec[i].base_dir;\n\t\t\t\troll_pow[i][0].num_roll = vec[i].num_roll;\n\t\t\t}\n\n\t\t\t//■ダブリング\n\t\t\tfor(int d = 1; d <= maxi; d++){\n\t\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\t\tif(is_corner(roll_pow[i][d-1].base_dir)){ //既に角に寄っている場合\n\n\t\t\t\t\t\tDIR2 tmp_c = roll_pow[i][d-1].base_dir;\n\t\t\t\t\t\tDIR2 next_c = calc_corner(tmp_c,roll_pow[i][d-1].num_roll);\n\n\t\t\t\t\t\troll_pow[i][d].base_dir = tmp_c; //■一度角に着いたら、以後ずっとそこを基準にする\n\t\t\t\t\t\troll_pow[i][d].num_roll = roll_pow[i][d-1].num_roll+roll_pow[next_c][d-1].num_roll;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tDIR2 mid = roll_pow[i][d-1].base_dir; //まずはmidへ\n\t\t\t\t\t\tDIR2 tmp = roll_pow[mid][d-1].base_dir;\n\n\t\t\t\t\t\troll_pow[i][d].base_dir = tmp;\n\n\t\t\t\t\t\tif(is_corner(tmp)){ //初めて角に着く\n\n\t\t\t\t\t\t\troll_pow[i][d].num_roll = roll_pow[mid][d-1].num_roll;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\t\tDIR2 base_dir = BASE_DIR[i]; //■■呼び出し元の位置iの基準角\n\t\t\t\tDIR2 now_dir;\n\t\t\t\tll num_roll = NUM_ROLL[i];\n\n\t\t\t\tif(is_corner(base_dir)){\n\n\t\t\t\t\tnow_dir = calc_corner(base_dir,num_roll); //■位置iの塊が、現在寄っている角\n\t\t\t\t}\n\n\t\t\t\tll rest = tmp_p;\n\t\t\t\tfor(int d = maxi; d >= 0; d--){\n\t\t\t\t\tif(POW[d] > rest)continue;\n\n\t\t\t\t\tif(is_corner(base_dir)){ //基準角が決まっている\n\n\t\t\t\t\t\tnum_roll += roll_pow[now_dir][d].num_roll;\n\t\t\t\t\t\tnow_dir = calc_corner(base_dir,num_roll);\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tbase_dir = roll_pow[base_dir][d].base_dir; //次の基準角\n\t\t\t\t\t\tif(is_corner(base_dir)){ //新しく基準角が決まる\n\n\t\t\t\t\t\t\tnum_roll += roll_pow[base_dir][d].num_roll;\n\t\t\t\t\t\t\tnow_dir = calc_corner(base_dir,num_roll);\n\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\trest -= POW[d];\n\t\t\t\t}\n\n\t\t\t\tBASE_DIR[i] = base_dir;\n\t\t\t\tNUM_ROLL[i] = num_roll;\n\t\t\t}\n\n\t\t\tleft = ind;\n\n\t\t}else{ //■倍化処理なし\n\n\t\t\tint right = left;\n\t\t\twhile(right <= tail && line[right] != '(')right++;\n\t\t\tright--;\n\n\t\t\t//この間に括弧なし\n\t\t\tvector<Roll> vec = calc_normal(left,right);\n\n\t\t\tfor(int i = 0; i < 13; i++){\n\t\t\t\tif(is_corner(BASE_DIR[i])){\n\n\t\t\t\t\tDIR2 now_dir = calc_corner(BASE_DIR[i],NUM_ROLL[i]);\n\t\t\t\t\tNUM_ROLL[i] += vec[now_dir].num_roll; //既に角にいる場合は、回転数だけ足す\n\n\t\t\t\t}else{\n\n\t\t\t\t\tDIR2 base_dir = vec[BASE_DIR[i]].base_dir;\n\t\t\t\t\tif(is_corner(base_dir)){\n\n\t\t\t\t\t\tNUM_ROLL[i] += vec[BASE_DIR[i]].num_roll;\n\t\t\t\t\t}\n\t\t\t\t\tBASE_DIR[i] = base_dir;\n\t\t\t\t}\n\t\t\t}\n\t\t\tleft = right+1;\n\t\t}\n\t}\n\n\tvector<Roll> ret;\n\n\tfor(int i = 0; i < 13; i++){\n\t\tRoll tmp_roll;\n\t\ttmp_roll.base_dir = BASE_DIR[i];\n\t\ttmp_roll.num_roll = NUM_ROLL[i];\n\n\t\tret.push_back(tmp_roll);\n\t}\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tfor(int row = 0; row < N; row++){\n\n\t\tscanf(\"%s\",input[row]);\n\t}\n\n\tscanf(\"%s\",line);\n\n\tinit();\n\n\n /*if(DEBUG){\n\t\tfor(int i = 0; i < 13; i++){\n\n\t\t\tprintf(\"\\n\\n%d:\\n\",i);\n\t\t\tfor(int row = 0; row < N; row++){\n\t\t\t\tfor(int col = 0; col < N; col++){\n\n\t\t\t\t\tprintf(\" %d\",base_corn[i][row][col]);\n\t\t\t\t}\n\t\t\t\tprintf(\"\\n\");\n\t\t\t}\n\t\t};\n\t\treturn;\n\t}*/\n\n\n\n\tfor(LEN = 0; line[LEN] != '\\0'; LEN++);\n\n\t//括弧の対応を前計算\n\tstack<int> ST;\n\tfor(int i = 0; i < LEN; i++){\n\t\tif(line[i] == '('){\n\n\t\t\tST.push(i);\n\n\t\t}else if(line[i] == ')'){\n\n\t\t\tclose_pos[ST.top()] = i;\n\t\t\tST.pop();\n\t\t}\n\t}\n\n\tinit_table(MAP);\n\n\tmap<int,pair<int,int>> MEMO;\n\n\tint IND = 0;\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(input[row][col] == '.'){\n\n\t\t\t\tMAP[row][col] = -1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tMAP[row][col] = IND;\n\t\t\tMEMO[IND] = make_pair(row,col);\n\t\t\tIND++;\n\t\t}\n\t}\n\n\n\tvector<Roll> ret = calc_E(0,LEN-1);\n\n\t/*printf(\"ret[Undef2].base_dir:%d num_roll:%lld\\n\",ret[Undef2].base_dir,ret[Undef2].num_roll);\n\n\tprintf(\"move_func前のMAP\\n\");\n\toutPut(MAP);\n*/\n\tmove_func(MAP,ret[Undef2]);\n/*\n\n\tif(DEBUG){\n\t\tprintf(\"■最終遷移表■\\n\");\n\t\tfor(int row = 0; row < N; row++){\n\t\t\tfor(int col = 0; col < N; col++){\n\n\t\t\t\tprintf(\" %d\",MAP[row][col]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\n\t\t}\n\t}\n*/\n\n\tchar ANS[SIZE][SIZE];\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tANS[row][col] = '.';\n\t\t}\n\t}\n\n\t//回転処理\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\t\t\tif(MAP[row][col] == -1)continue;\n\n\t\t\tint tmp_num = MAP[row][col];\n\t\t\tint pre_row = MEMO[tmp_num].first;\n\t\t\tint pre_col = MEMO[tmp_num].second;\n\n\t\t\tANS[row][col] = input[pre_row][pre_col];\n\n\t\t}\n\t}\n\n\tfor(int row = 0; row < N; row++){\n\t\tfor(int col = 0; col < N; col++){\n\n\t\t\tprintf(\"%c\",ANS[row][col]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= MAX; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 41352, "score_of_the_acc": -1.1953, "final_rank": 7 }, { "submission_id": "aoj_1638_4979093", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\n// 方向 0:D 1:R 2:U 3:L\n// 状態 0:LU 1:LD 2:RD 3:RU\n// 右回りを正とする\n\nint Get[4][4]={\n {1,3,0,0},\n {1,2,0,1},\n {2,2,3,1},\n {2,3,3,0}\n};\n\nint cnt_rotate[4][4]={\n {0,-1,0,0},\n {0,0,0,0},\n {0,0,0,0},\n {0,0,0,1}\n};\n\nmap<char,int> mp; // 方向→数字\n\nvoid init(vector<vector<int>> &v){\n int n=v[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n v[i][j]=0;\n }\n }\n}\n\nvector<vector<int>> Down(vector<vector<int>> a){\n vector<vector<int>> b=a;\n init(b);\n int n=a[0].size();\n for(int i=0;i<n;i++){\n int cur=n-1;\n for(int j=n-1;j>=0;j--){\n if(a[j][i]){\n b[cur][i]=a[j][i];\n cur--;\n }\n }\n }\n return b;\n}\n\nvector<vector<int>> Right(vector<vector<int>> a){\n vector<vector<int>> b=a;\n init(b);\n int n=a[0].size();\n for(int i=0;i<n;i++){\n int cur=n-1;\n for(int j=n-1;j>=0;j--){\n if(a[i][j]){\n b[i][cur]=a[i][j];\n cur--;\n }\n }\n }\n return b;\n}\n\nvector<vector<int>> Up(vector<vector<int>> a){\n vector<vector<int>> b=a;\n init(b);\n int n=a[0].size();\n for(int i=0;i<n;i++){\n int cur=0;\n for(int j=0;j<n;j++){\n if(a[j][i]){\n b[cur][i]=a[j][i];\n cur++;\n }\n }\n }\n return b;\n}\n\nvector<vector<int>> Left(vector<vector<int>> a){\n vector<vector<int>> b=a;\n init(b);\n int n=a[0].size();\n for(int i=0;i<n;i++){\n int cur=0;\n for(int j=0;j<n;j++){\n if(a[i][j]){\n b[i][cur]=a[i][j];\n cur++;\n }\n }\n }\n return b;\n}\n\nvoid print_res(vector<vector<int>> s,vector<char> v){\n int n=s[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n printf(\"%c\",v[s[i][j]]);\n }\n printf(\"\\n\");\n }\n}\n\nvoid solve2(vector<vector<int>> s,string t,vector<char> v){\n reverse(t.begin(), t.end());\n char dir;\n for(char c:t){\n if(c=='R' or c=='L' or c=='D' or c=='U'){\n dir=c; break;\n }\n }\n if(dir=='R')s=Right(s);\n if(dir=='L')s=Left(s);\n if(dir=='U')s=Up(s);\n if(dir=='D')s=Down(s);\n print_res(s,v);\n}\n\nint id=0;\n\nvector<pair<int,ll>> solve(string t){\n vector<pair<int,ll>> res(4);\n for(int i=0;i<4;i++){\n res[i]=make_pair(i,0LL);\n }\n for(;id<t.size();id++){\n if(t[id]=='('){\n id++;\n auto R=solve(t);\n id++;\n ll num=0;\n while(id<t.size() and '0'<=t[id] and t[id]<='9'){\n num=num*10+t[id]-'0';\n id++;\n }\n id--;\n // ここで掛け算処理を書く\n vector<vector<pair<int,ll>>> mul(61,vector<pair<int,ll>>(4));\n for(int i=0;i<4;i++){\n mul[0][i]=R[i];\n }\n for(int i=1;i<61;i++){\n for(int j=0;j<4;j++){\n mul[i][j].first=mul[i-1][mul[i-1][j].first].first;\n mul[i][j].second=mul[i-1][j].second+mul[i-1][mul[i-1][j].first].second;\n }\n }\n for(int i=60;i>=0;i--){\n if((1LL<<i)&num){\n for(int j=0;j<4;j++){\n int cur=res[j].first;\n res[j].first=mul[i][cur].first;\n res[j].second+=mul[i][cur].second;\n }\n }\n }\n }\n else if(t[id]==')'){\n return res;\n }\n else{\n int op=mp[t[id]];\n for(int i=0;i<4;i++){\n int cur=res[i].first;\n res[i].first=Get[cur][op];\n res[i].second+=cnt_rotate[cur][op];\n }\n }\n }\n return res;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n mp['D']=0,mp['R']=1,mp['U']=2,mp['L']=3;\n while(cin >> n,n){\n vector<string> S(n);\n vector<vector<int>> s(n,vector<int>(n));\n for(int i=0;i<n;i++){\n cin >> S[i];\n }\n string t; cin >> t;\n vector<char> V={'.'};\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(S[i][j]!='.'){\n s[i][j]=(int)V.size();\n V.push_back(S[i][j]);\n }\n else s[i][j]=0;\n }\n }\n if(V.size()==1){\n print_res(s,V); continue;\n }\n bool flag1=true; // UD only\n bool flag2=true; // RL only\n for(char c:t){\n if(c=='R' or c=='L')flag1=false;\n if(c=='U' or c=='D')flag2=false;\n }\n if(flag1^flag2){\n solve2(s,t,V); continue;\n }\n vector<char> dir;\n for(char c:t){\n if(c=='R' or c=='L' or c=='U' or c=='D')dir.push_back(c);\n }\n int cur;\n for(int i=0;i+1<dir.size();i++){\n if(dir[i]=='R' or dir[i]=='L'){\n if(dir[i+1]=='U' or dir[i+1]=='D'){\n if(dir[i]=='L'){\n s=Left(s);\n if(dir[i+1]=='U'){cur=0;s=Up(s);}\n else{cur=1;s=Down(s);}\n }\n else{\n s=Right(s);\n if(dir[i+1]=='D'){cur=2;s=Down(s);}\n else{cur=3;s=Up(s);}\n }\n break;\n }\n }\n else{\n if(dir[i+1]=='R' or dir[i+1]=='L'){\n if(dir[i+1]=='L'){\n if(dir[i]=='U'){cur=0;s=Up(s);}\n else{cur=1;s=Down(s);}\n s=Left(s);\n }\n else{\n if(dir[i]=='D'){cur=2;s=Down(s);}\n else{cur=3;s=Up(s);}\n s=Right(s);\n }\n }\n }\n }\n id=0;\n auto Op=solve(t);\n auto op=Op[cur];\n // cout << op.first << \" \" << op.second << endl;\n if(cur==3){\n s=Down(s); s=Left(s); s=Up(s);\n }\n else if(cur==2){\n s=Left(s); s=Up(s);\n }\n else if(cur==1){\n s=Up(s);\n }\n auto ss=s;\n ss=Down(ss); ss=Right(ss); ss=Up(ss); ss=Left(ss);\n set<int> used;\n map<int,pair<int,int>> pos1,pos2;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n pos1[s[i][j]]=make_pair(i,j);\n pos2[ss[i][j]]=make_pair(i,j);\n }\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(s[i][j]==0)continue;\n if(used.find(s[i][j])!=used.end())continue;\n int now=s[i][j];\n vector<int> num;\n vector<pair<int,int>> num_pos;\n pair<int,int> cur_pos=make_pair(i,j);\n while(used.find(now)==used.end()){\n used.insert(now);\n num.push_back(now);\n num_pos.push_back(cur_pos);\n pair<int,int> nx_pos=pos2[now];\n int nx=s[nx_pos.first][nx_pos.second];\n now=nx; cur_pos=nx_pos;\n }\n int len=num.size();\n ll cycle=op.second;\n cycle%=len;\n if(cycle<0)cycle+=len;\n for(int k=0;k<len;k++){\n int kid=(k+cycle)%len;\n s[num_pos[kid].first][num_pos[kid].second]=num[k];\n }\n }\n }\n if(op.first==1){\n s=Down(s);\n }\n else if(op.first==2){\n s=Down(s); s=Right(s);\n }\n else if(op.first==3){\n s=Down(s); s=Right(s); s=Up(s);\n }\n print_res(s,V);\n // printf(\"Unsolved\\n\");\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3372, "score_of_the_acc": -0.0226, "final_rank": 3 }, { "submission_id": "aoj_1638_3771791", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstdio>\n#include <vector>\n#include <numeric>\n#include <cstdlib>\n#include <cctype>\nusing namespace std;\n\n#define ALL(v) begin(v),end(v)\n#define REP(i,n) for(int i=0;i<(n);++i)\n#define RREP(i,n) for(int i=(n)-1;i>=0;--i)\n\ntypedef long long LL;\ntypedef vector<int> vint;\n\nconst int targets[4][9] = {\n\t{1, 1, 2, 2, 2, 1, 8, 8, 8},\n\t{3, 2, 2, 3, 4, 4, 4, 3, 2},\n\t{5, 5, 4, 4, 4, 5, 6, 6, 6},\n\t{7, 8, 8, 7, 6, 6, 6, 7, 8}\n};\n\nint getdir(char c){\n\tswitch(c){\n\t\tcase 'R': return 0;\n\t\tcase 'U': return 1;\n\t\tcase 'L': return 2;\n\t\tcase 'D': return 3;\n\t}\n\tabort();\n}\n\nstruct solver{\n\tint n;\n\tsolver(int n_) : n(n_) {}\n\tvector<vint> ids[9];\n\tvint trs[4];\n\tvint unit;\n\tconst char *p;\n\t\n\tvint mul(const vint &x, const vint &y){\n\t\tint sz = x.size();\n\t\tvint ret(sz);\n\t\tREP(i, sz){ ret[i] = y[x[i]]; }\n\t\treturn ret;\n\t}\n\t\n\tvint pow(vint x, LL y){\n\t\tvint a = unit;\n\t\twhile(y){\n\t\t\tif(y & 1){ a = mul(a, x); }\n\t\t\tx = mul(x, x);\n\t\t\ty >>= 1;\n\t\t}\n\t\treturn a;\n\t}\n\t\n\tvint parse(){\n\t\tvint a = unit;\n\t\tvint b;\n\t\twhile(1){\n\t\t\tif(*p == '('){\n\t\t\t\t++p;\n\t\t\t\tvint x = parse();\n\t\t\t\t++p;\n\t\t\t\tchar *endp;\n\t\t\t\tLL r = strtoll(p, &endp, 10);\n\t\t\t\tp = endp;\n\t\t\t\tb = pow(x, r);\n\t\t\t}\n\t\t\telse if(isalpha(*p)){\n\t\t\t\tint dir = getdir(*p);\n\t\t\t\t++p;\n\t\t\t\tb = trs[dir];\n\t\t\t}\n\t\t\telse{ break; }\n\t\t\ta = mul(a, b);\n\t\t}\n\t\treturn a;\n\t}\n\t\n\tvoid solve(){\n\t\tvector<string> in0(n);\n\t\tREP(i, n){ cin >> in0[i]; }\n\t\tstring op;\n\t\tcin >> op;\n\n\t\tint fstdir = -1;\n\t\tfor(char c : op){\n\t\t\tif(isalpha(c)){\n\t\t\t\tfstdir = getdir(c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tvector<string> in(n, string(n, '.'));\n\t\tif(fstdir == 0){\n\t\t\tREP(y, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(x, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[y][u--] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if(fstdir == 1){\n\t\t\tREP(x, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(y, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[u++][x] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if(fstdir == 2){\n\t\t\tREP(y, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(x, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[y][u++] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tREP(x, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(y, n){\n\t\t\t\t\tif(in0[y][x] != '.'){\n\t\t\t\t\t\tin[u--][x] = in0[y][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tREP(i, 9){\n\t\t\tids[i].assign(n, vint(n, -1));\n\t\t}\n\n\t\tint id = 0;\n\t\tREP(i, n)\n\t\tREP(j, n){\n\t\t\tif(in[i][j] != '.'){\n\t\t\t\tids[0][i][j] = id;\n\t\t\t\t++id;\n\t\t\t}\n\t\t}\n\t\tint pcnt = id;\n\n\t\tREP(i, 4){ trs[i].assign(9 * pcnt, -1); }\n\t\tunit.resize(9 * pcnt);\n\t\tiota(ALL(unit), 0);\n\n\t\tREP(from, 9){\n\t\t\tint to, dir;\n\n\t\t\tdir = 0;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(y, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(x, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][y][u];\n\t\t\t\t\t\t--u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdir = 1;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(x, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(y, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][u][x];\n\t\t\t\t\t\t++u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdir = 2;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(y, n){\n\t\t\t\tint u = 0;\n\t\t\t\tREP(x, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][y][u];\n\t\t\t\t\t\t++u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdir = 3;\n\t\t\tto = targets[dir][from];\n\t\t\tid = to * pcnt;\n\t\t\tREP(x, n){\n\t\t\t\tint u = n - 1;\n\t\t\t\tRREP(y, n){\n\t\t\t\t\tif(ids[from][y][x] >= 0){\n\t\t\t\t\t\tint &r = ids[to][u][x];\n\t\t\t\t\t\t--u;\n\t\t\t\t\t\tif(r == -1){ r = id++; }\n\t\t\t\t\t\ttrs[dir][ids[from][y][x]] = r;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tp = op.c_str();\n\t\tvint res = parse();\n\n\t\tvector<char> val(9 * pcnt);\n\t\tREP(y, n)\n\t\tREP(x, n){\n\t\t\tint k = ids[0][y][x];\n\t\t\tif(k >= 0){\n\t\t\t\tval[res[k]] = in[y][x];\n\t\t\t}\n\t\t}\n\t\tvector<string> ans(n, string(n, '.'));\n\t\tREP(i, 9)\n\t\tREP(y, n)\n\t\tREP(x, n){\n\t\t\tint k = ids[i][y][x];\n\t\t\tif(k >= 0 && val[k] != 0){\n\t\t\t\tans[y][x] = val[k];\n\t\t\t}\n\t\t}\n\t\t\n\t\tREP(i, n){\n\t\t\tcout << ans[i] << '\\n';\n\t\t}\n\t}\n};\n\nint main(){\n\tint n;\n\twhile(cin >> n && n){\n\t\tsolver(n).solve();\n\t}\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 7796, "score_of_the_acc": -0.4247, "final_rank": 5 } ]
aoj_1635_cpp
Tally Counters A number of tally counters are placed in a row. Pushing the button on a counter will increment the displayed value by one, or, when the value is already the maximum, it goes down to one. All the counters are of the same model with the same maximum value. Fig. D-1 Tally Counters Starting from the values initially displayed on each of the counters, you want to change all the displayed values to target values specified for each. As you don't want the hassle, however, of pushing buttons of many counters one be one, you devised a special tool. Using the tool, you can push buttons of one or more adjacent counters, one push for each, in a single operation. You can choose an arbitrary number of counters at any position in each operation, as far as they are consecutively lined up. How many operations are required at least to change the displayed values on counters to the target values? Input The input consists of multiple datasets, each in the following format. n m a 1 a 2 ... a n b 1 b 2 ... b n Each dataset consists of 3 lines. The first line contains n (1 ≤ n ≤ 1000) and m (1 ≤ m ≤ 10000), the number of counters and the maximum value displayed on counters, respectively. The second line contains the initial values on counters, a i (1 ≤ a i ≤ m ), separated by spaces. The third line contains the target values on counters, b i (1 ≤ b i ≤ m ), separated by spaces. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each dataset, print in a line the minimum number of operations required to make all of the counters display the target values. Sample Input 4 5 2 3 1 4 2 5 5 2 3 100 1 10 100 1 10 100 5 10000 4971 7482 1238 8523 1823 3287 9013 9812 297 1809 0 0 Output for the Sample Input 4 0 14731
[ { "submission_id": "aoj_1635_10898275", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e3+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nconst int dr[] = {-1,0,1,0}, dc[] = {0,1,0,-1};\nvoid setIO(string);\nint n,m;\nll a[N], b[N];\nll dp[N][N];\nvoid solve(){\n \n cin>>n>>m;\n while(n != 0 || m != 0) {\n for(int i = 0; i < n; i++) cin>>a[i];\n for(int i = 0; i < n; i++) cin>>b[i];\n for(int i = 0; i < n; i++) {\n a[i] = (b[i] - a[i] + m) % m;\n }\n\n memset(dp, INF, sizeof dp);\n for(int i = 0; i <= n; i++) {\n dp[0][i] = a[0] + i * m; \n }\n for(int i = 1; i < n; i++) {\n // x <= y\n // find min suffix of dp[i-1][k]\n // x > y\n // find min prefix of dp[i-1][k]\n vector<ll> pre(n+1), suff(n+1);\n pre[0] = dp[i-1][0];\n for(int k = 1; k <= n; k++) {\n pre[k] = min(pre[k-1], dp[i-1][k] - k * m);\n }\n\n suff[n] = dp[i-1][n];\n for(int k = n - 1; k >= 0; k--) {\n suff[k] = min(suff[k+1], dp[i-1][k]);\n }\n\n for(int j = 0; j <= n; j++) {\n //cerr << dp[i-1][j] << \" \";\n ll x = a[i] + j * m;\n int l = 0, r = n+1;\n while(l < r) {\n int mid = l + (r - l) / 2;\n ll y = a[i-1] + mid * m;\n if(y >= x) {\n r = mid;\n } else {\n l = mid + 1;\n }\n }\n if(l <= n) dp[i][j] = min(dp[i][j], suff[l]);\n if(l > 0) dp[i][j] = min(dp[i][j], pre[l-1] + x - a[i-1]);\n // O(n^3)\n /*for(int k = 0; k <= n; k++) {\n ll y = a[i-1] + k * m;\n if(x <= y) {\n dp[i][j] = min(dp[i][j], dp[i-1][k]);\n } else {\n dp[i][j] = min(dp[i][j], dp[i-1][k] + x - y);\n }\n }*/\n }\n //cerr << nl;\n }\n ll ans = LINF;\n for(int i = 0; i <= n; i++) {\n ans = min(ans, dp[n-1][i]);\n }\n cout << ans << nl;\n cin>>n>>m;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 1650, "memory_kb": 11228, "score_of_the_acc": -1.4963, "final_rank": 20 }, { "submission_id": "aoj_1635_10898269", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e3+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nconst int dr[] = {-1,0,1,0}, dc[] = {0,1,0,-1};\nvoid setIO(string);\nint n,m;\nll a[N], b[N];\nll dp[N][N];\nvoid solve(){\n \n cin>>n>>m;\n while(n != 0 || m != 0) {\n for(int i = 0; i < n; i++) cin>>a[i];\n for(int i = 0; i < n; i++) cin>>b[i];\n for(int i = 0; i < n; i++) {\n a[i] = (b[i] - a[i] + m) % m;\n }\n\n memset(dp, INF, sizeof dp);\n for(int i = 0; i <= n; i++) {\n dp[0][i] = a[0] + i * m; \n }\n for(int i = 1; i < n; i++) {\n // x <= y\n // find min prefix of dp[i-1][k]\n // x > y\n // find min suffix of dp[i-1][k]\n vector<ll> pre(n+1), suff(n+1);\n pre[0] = dp[i-1][0];\n for(int k = 1; k <= n; k++) {\n pre[k] = min(pre[k-1], dp[i-1][k] - k * m);\n }\n\n suff[n] = dp[i-1][n];\n for(int k = n - 1; k >= 0; k--) {\n suff[k] = min(suff[k+1], dp[i-1][k]);\n }\n\n for(int j = 0; j <= n; j++) {\n //cerr << dp[i-1][j] << \" \";\n ll x = a[i] + j * m;\n int l = 0, r = n+1;\n while(l < r) {\n int mid = l + (r - l) / 2;\n ll y = a[i-1] + mid * m;\n if(y >= x) {\n r = mid;\n } else {\n l = mid + 1;\n }\n }\n if(l <= n) dp[i][j] = min(dp[i][j], suff[l]);\n if(l > 0) dp[i][j] = min(dp[i][j], pre[l-1] + x - a[i-1]);\n /*for(int k = 0; k <= n; k++) {\n ll y = a[i-1] + k * m;\n if(x <= y) {\n dp[i][j] = min(dp[i][j], dp[i-1][k]);\n } else {\n dp[i][j] = min(dp[i][j], dp[i-1][k] + x - y);\n }\n }*/\n }\n //cerr << nl;\n }\n ll ans = LINF;\n for(int i = 0; i <= n; i++) {\n ans = min(ans, dp[n-1][i]);\n }\n cout << ans << nl;\n cin>>n>>m;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 1650, "memory_kb": 11164, "score_of_the_acc": -1.4923, "final_rank": 19 }, { "submission_id": "aoj_1635_10898216", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = (ll)4e18;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n\n vector<int> a(n), b(n), d(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n for (int i = 0; i < n; i++) cin >> b[i];\n for (int i = 0; i < n; i++) {\n d[i] = (b[i] - a[i]) % m;\n if (d[i] < 0) d[i] += m;\n }\n\n int K = n; // safe bound for multiples\n vector<ll> dp_prev(K+1), dp_cur(K+1);\n\n // base\n for (int k = 0; k <= K; k++) {\n dp_prev[k] = (ll)d[0] + (ll)k * m;\n }\n\n // DP loop\n for (int i = 1; i < n; i++) {\n // prefix min of dp_prev[j] - j*m\n vector<ll> pref(K+1);\n ll cur = INF;\n for (int j = 0; j <= K; j++) {\n cur = min(cur, dp_prev[j] - (ll)j * m);\n pref[j] = cur;\n }\n // suffix min of dp_prev[j]\n vector<ll> suff(K+2);\n cur = INF;\n for (int j = K; j >= 0; j--) {\n cur = min(cur, dp_prev[j]);\n suff[j] = cur;\n }\n\n // compute dp_cur\n for (int k = 0; k <= K; k++) {\n ll Ti = (ll)d[i] + (ll)k * m;\n ll numer = Ti - (ll)d[i-1];\n ll j0;\n if (numer <= 0) j0 = 0;\n else j0 = (numer + m - 1) / m; // ceil(numer/m)\n\n if (j0 < 0) j0 = 0;\n if (j0 > K+1) j0 = K+1;\n\n ll A = (j0 <= K ? suff[j0] : INF);\n ll B = (j0-1 >= 0 ? pref[j0-1] + (Ti - (ll)d[i-1]) : INF);\n dp_cur[k] = min(A, B);\n }\n dp_prev.swap(dp_cur);\n }\n\n ll ans = *min_element(dp_prev.begin(), dp_prev.end());\n cout << ans << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 3460, "score_of_the_acc": -0.286, "final_rank": 12 }, { "submission_id": "aoj_1635_10851360", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int maxn = 1e3 + 5;\nconst int inf = 0x3f3f3f3f;\nint n, m;\nint a[maxn], dp[maxn][maxn];\nint main() {\n while(scanf(\"%d%d\", &n, &m) != EOF) {\n if(n == 0 && m == 0) break;\n for(int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n }\n for(int i = 0; i < n; ++i) {\n int x; scanf(\"%d\", &x);\n a[i] = x - a[i];\n if(a[i] < 0) a[i] += m;\n // printf(\"%d \", a[i]);\n }\n // puts(\"\");\n for(int i = 0; i < n; ++i) {\n dp[0][i] = a[0] + i*m;\n }\n for(int i = 1; i < n; ++i) {\n int dt = a[i] - a[i - 1];\n for(int j = 0; j < n; ++j) {\n if(dt < 0) {\n dp[i][j] = dp[i - 1][j];\n if(j > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + m + dt);\n } else {\n dp[i][j] = dp[i - 1][j] + dt;\n if(j < n - 1) dp[i][j] = min(dp[i][j], dp[i - 1][j + 1]);\n }\n }\n }\n int ans = inf;\n for(int i = 0; i < n; ++i) {\n ans = min(ans, dp[n - 1][i]);\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 7428, "score_of_the_acc": -0.2745, "final_rank": 11 }, { "submission_id": "aoj_1635_10682001", "code_snippet": "// # pragma GCC target(\"avx2\")\n// # pragma GCC optimize(\"O3\")\n// # pragma GCC optimize(\"unroll-loops\")\n// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\n// #include<atcoder/modint>\n// using namespace atcoder;\n// using mint = modint998244353;\n// const ll MOD = 998244353;\n// // using mint = modint;\n// // const ll MOD = 998244353;\n// // using mint = modint1000000007;\n// // const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n// #define rep(i, n) for (int i = 0; i < (n); i++)\n#define rep2(i, a, b) for (int i = (a); i < (b); i++)\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nconst long long INF = 1000000000000000005LL;\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multiple_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\nvoid solve(ll n, ll m) {\n vector<long long> a(n); for(int i = 0; i < n; i++) cin >> a[i]; \n vector<long long> b(n); for(int i = 0; i < n; i++) cin >> b[i]; \n vl c(n);\n rep(i, 0, n) {\n if(b[i] >= a[i]) {\n c[i] = b[i] - a[i];\n }\n else c[i] = m + (b[i] - a[i]);\n }\n\n vl dp(n + 1, llINF);\n rep(k, 0, n + 1) {\n dp[k] = m * k + c[0];\n }\n\n rep(i, 0, n - 1) {\n vl nextdp(n + 1, llINF);\n\n vector<P> arr;\n rep(k, 0, n + 1) {\n arr.emplace_back(P({m * k + c[i], dp[k] - (m * k + c[i])}));\n }\n\n ll curmin = llINF, id = 0;\n rep(k2, 0, n + 1) {\n while(id <= n && arr[id].first <= m * k2 + c[i + 1]) {\n chmin(curmin, arr[id].second);\n id++;\n }\n if(curmin == llINF) continue;\n\n chmin(nextdp[k2], curmin + m * k2 + c[i + 1]);\n }\n\n vector<P> arr2;\n rep(k, 0, n + 1) {\n arr2.emplace_back(P({m * k + c[i], dp[k]}));\n }\n \n curmin = llINF, id = n;\n\n drep(k2, 0, n + 1) {\n while(id >= 0 && arr2[id].first > m * k2 + c[i + 1]) {\n chmin(curmin, arr2[id].second);\n id--;\n }\n if(curmin == llINF) continue;\n\n chmin(nextdp[k2], curmin);\n }\n\n swap(nextdp, dp);\n }\n\n cout << *min_element(all(dp)) << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while(true) {\n ll n, m;\n cin >> n >> m;\n if(n == 0 && m == 0) return 0;\n solve(n, m);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 3584, "score_of_the_acc": -0.4097, "final_rank": 14 }, { "submission_id": "aoj_1635_10658611", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nconst double pi = 3.141592653589793238;\nconst int INF = 2147483647;\nconst ll INFLL = 1LL << 62;\n#define all(a) (a).begin(), (a).end()\n#define allr(a) (a).rbegin(), (a).rend()\n#define rep(i, r, n) for (int i = r; i < (int)n; i++)\n#define pb push_back\n\nint n,m;\nll a[1005][1005],ans;\nint s[1005],o;\n\nint main()\n{\n while(1){\n scanf(\"%d%d\",&n,&m);\n ans=INFLL;\n if(n+m==0){\n return 0;\n }\n for(int i=1;i<=n;++i)\n scanf(\"%d\",&s[i]);\n for(int i=1;i<=n;++i){\n scanf(\"%d\",&o);\n s[i]=o-s[i];\n if(s[i]<0)s[i]+=m;\n }\n for(int i=1;i<=n;++i)\n for(int j=0;j<=n+1;++j)\n a[i][j]=INFLL;\n for(int j=0;j<=n;++j){\n a[1][j]=s[1]+j*m;\n if(n==1)ans=min(ans,a[1][j]);\n }\n for(int i=2;i<=n;++i)\n for(int j=0;j<=n+1;++j){\n if(s[i]>s[i-1])a[i][j]=min(a[i][j],a[i-1][j]+s[i]-s[i-1]);\n if(s[i]<=s[i-1])a[i][j]=min(a[i][j],a[i-1][j]);\n if(j!=0)a[i][j]=min(a[i][j],a[i-1][j-1]+s[i]+m-s[i-1]);\n if(j!=n+1)a[i][j]=min(a[i][j],a[i-1][j+1]);\n if(i==n)ans=min(ans,a[i][j]);\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 11408, "score_of_the_acc": -0.5504, "final_rank": 15 }, { "submission_id": "aoj_1635_10645527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return false;\n vector<int> A(N), B(N);\n for (int i = 0; i < N; i++) cin >> A[i];\n for (int i = 0; i < N; i++) cin >> B[i];\n vector<int> D(N);\n for (int i = 0; i < N; i++) D[i] = B[i] - A[i] + (A[i] > B[i] ? M : 0);\n const long long inf = 1LL << 60;\n vector<long long> dp(N + 1, inf);\n dp[0] = 0;\n for (int i = 0; i < N; i++) {\n vector<long long> ndp(N + 1, inf);\n vector<long long> L(N + 1, inf), R(N + 1, inf);\n for (int j = 0; j <= N; j++) {\n L[j]= dp[j] - j * M - (i ? D[i - 1] : 0);\n if (j > 0) {\n L[j] = min(L[j], L[j - 1]);\n }\n }\n for (int j = N; j >= 0; j--) {\n R[j] = dp[j];\n if (j < N) {\n R[j] = min(R[j], R[j + 1]);\n }\n }\n for (int j = 0, k = 0; j <= N; j++) {\n while (k <= N && k * M + (i ? D[i - 1] : 0) <= j * M + D[i]) k++;\n auto L_val = (k > 0 ? L[k - 1] : inf) + j * M + D[i];\n auto R_val = (k <= N ? R[k] : inf);\n ndp[j] = min(L_val, R_val);\n }\n dp = ndp;\n }\n cout << *min_element(dp.begin(), dp.end()) << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3584, "score_of_the_acc": -0.1963, "final_rank": 9 }, { "submission_id": "aoj_1635_10637104", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG // 配列外参照のエラー\n// #endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vvvvi = vector<vector<vector<vector<int>>>>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vvvl = vector<vector<vector<ll>>>;\nusing vvvvl = vector<vector<vector<vector<ll>>>>;\n\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint INF32 = 2e9;\nll INF64 = 9e18;\n#define endl '\\n';\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\ninline void Yes(bool f);\n\n///////////////////ここから//////////////////////\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0) {\n return false;\n }\n vi A(N);\n vi B(N);\n for (int i = 0; i < N; i++) {\n cin >> A[i];\n }\n for (int i = 0; i < N; i++) {\n cin >> B[i];\n }\n vi C(N);\n for (int i = 0; i < N; i++) {\n C[i] = (B[i] + M - A[i]) % M;\n }\n\n vvl dp = vvl(N + 1, vl(N + 1, INF64));\n dp[0][0] = 0;\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (dp[i][j] == INF64) {\n continue;\n }\n int pre_hight = j * M;\n if (i > 0) {\n pre_hight += C[i - 1];\n }\n for (int d = -1; d < 2; d++) {\n int nj = j + d;\n if (nj < 0) {\n continue;\n }\n int hight = C[i] + nj * M;\n dp[i + 1][nj] =\n min(dp[i + 1][nj], dp[i][j]+ (ll)max(0, hight - pre_hight));\n }\n }\n }\n ll ans = INF64;\n for (int j = 0; j < N + 1; j++) {\n ans=min(ans,dp[N][j]);\n }\n print(ans);\n\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve())\n ;\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 11364, "score_of_the_acc": -0.5903, "final_rank": 16 }, { "submission_id": "aoj_1635_10593225", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int N,M;cin>>N>>M;\n if (N==0) return 0;\n vi A(N),B(N);\n rep(i,0,N)cin>>A[i];\n rep(i,0,N)cin>>B[i];\n \n //カウンタごとの最小押し回数\n vi C(N);\n rep(i,0,N)C[i]=(B[i]-A[i]+M)%M;\n\n //dp_i = 区間閉じ待ちがc+i*m個ある\n vi dp(N+1,-1);\n rep(i,0,N+1)dp[i]=C[0]+i*M;\n\n rep(i,1,N){\n vi old(N+1,N*M+1111);\n swap(old,dp);\n \n //なるべく区間を大きくする\n rep(j,0,N+1){\n rep(d,-1,2){\n int pj=j+d;\n if(pj<0||pj>N)continue;\n dp[j]=min(dp[j],old[pj]+max(0,(C[i]+j*M)-(C[i-1]+pj*M)));\n }\n }\n\n }\n cout<<*min_element(all(dp))<<endl;\n return 1;\n}\n\n//O(N^2)な気がするんですけど O(NM)ではなく", "accuracy": 1, "time_ms": 180, "memory_kb": 3712, "score_of_the_acc": -0.1251, "final_rank": 8 }, { "submission_id": "aoj_1635_10593224", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int N,M;cin>>N>>M;\n if (N==0) return 0;\n vi A(N),B(N);\n rep(i,0,N)cin>>A[i];\n rep(i,0,N)cin>>B[i];\n \n //カウンタごとの最小押し回数\n vi C(N);\n rep(i,0,N)C[i]=(B[i]-A[i]+M)%M;\n\n //dp_i = 区間閉じ待ちがc+i*m個ある\n vi dp(N+1,-1);\n rep(i,0,N+1)dp[i]=C[0]+i*M;\n\n rep(i,1,N){\n vi old(N+1,N*M+1111);\n swap(old,dp);\n \n //なるべく区間を大きくする\n rep(j,0,N+1){\n rep(d,-1,2){\n int pj=j+d;\n if(pj<0||pj>N)continue;\n dp[j]=min(dp[j],old[pj]+max(0,(C[i]+j*M)-(C[i-1]+pj*M)));\n }\n }\n\n }\n cout<<*min_element(all(dp))<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3584, "score_of_the_acc": -0.1171, "final_rank": 7 }, { "submission_id": "aoj_1635_10593222", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int N,M;cin>>N>>M;\n if (N==0) return 0;\n vi A(N),B(N);\n rep(i,0,N)cin>>A[i];\n rep(i,0,N)cin>>B[i];\n \n //カウンタごとの最小押し回数\n vi C(N);\n rep(i,0,N)C[i]=(B[i]-A[i]+M)%M;\n\n //dp_i = 区間閉じ待ちがc+i*m個ある\n vi dp(N+1,-1);\n rep(i,0,N+1)dp[i]=C[0]+i*M;\n\n rep(i,1,N){\n vi old(N+1,N*M+1111);\n swap(old,dp);\n \n //なるべく区間を大きくする\n rep(j,0,N+1){\n if(j-1>=0)dp[j]=min(dp[j],old[j-1]+max(0,(C[i]+j*M)-(C[i-1]+(j-1)*M)));\n dp[j]=min(dp[j],old[j]+max(0,(C[i]+j*M)-(C[i-1]+j*M)));\n if(j+1<=N)dp[j]=min(dp[j],old[j+1]);\n }\n\n }\n cout<<*min_element(all(dp))<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3584, "score_of_the_acc": -0.0561, "final_rank": 4 }, { "submission_id": "aoj_1635_10554792", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(a) (a).begin(), (a).end()\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)\n#define range(i, l, r) for (int i = (int)(l); i < (int)(r); i++)\nusing ll = long long;\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n for (int i = 0; i < v.size(); i++) {\n os << v[i] << (i == (v.size() - 1) ? \"\" : \", \");\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nostream& operator<<(ostream& os, const pair<T1, T2> p) {\n os << \"{\" << p.first << \", \" << p.second << \"}\";\n return os;\n}\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) break;\n\n vector<int> a(n);\n vector<int> b(n);\n rep(i, n) cin >> a[i];\n rep(i, n) cin >> b[i];\n\n vector<int> c(n + 1);\n rep(i, n) { c[i] = (b[i] - a[i] + m) % m; }\n c[n] = -c[n - 1];\n rrep(i, n - 2) { c[i + 1] = c[i + 1] - c[i]; }\n\n priority_queue<int> hq{};\n hq.push(-c[0]);\n range(i, 1, n) {\n auto ci = hq.top();\n if (-ci + m < c[i]) {\n hq.pop();\n hq.push(ci - m);\n c[i] -= m;\n }\n hq.push(-c[i]);\n }\n\n int ans = 0;\n while (!hq.empty()) {\n ans += max(0, -hq.top());\n hq.pop();\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1635_10550121", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return false;\n vector<int> A(N), B(N);\n for (int i = 0; i < N; i++) cin >> A[i];\n for (int i = 0; i < N; i++) cin >> B[i];\n vector<int> D(N);\n for (int i = 0; i < N; i++) D[i] = B[i] - A[i] + (A[i] > B[i] ? M : 0);\n const long long inf = 1LL << 60;\n vector<long long> dp(N + 1, inf);\n dp[0] = 0;\n for (int i = 0; i < N; i++) {\n vector<long long> ndp(N + 1, inf);\n vector<long long> L(N + 1, inf), R(N + 1, inf);\n for (int j = 0; j <= N; j++) {\n L[j]= dp[j] - j * M - (i ? D[i - 1] : 0);\n if (j > 0) {\n L[j] = min(L[j], L[j - 1]);\n }\n }\n for (int j = N; j >= 0; j--) {\n R[j] = dp[j];\n if (j < N) {\n R[j] = min(R[j], R[j + 1]);\n }\n }\n for (int j = 0, k = 0; j <= N; j++) {\n while (k <= N && k * M + (i ? D[i - 1] : 0) <= j * M + D[i]) k++;\n auto L_val = (k > 0 ? L[k - 1] : inf) + j * M + D[i];\n auto R_val = (k <= N ? R[k] : inf);\n ndp[j] = min(L_val, R_val);\n }\n dp = ndp;\n }\n cout << *min_element(dp.begin(), dp.end()) << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3712, "score_of_the_acc": -0.2044, "final_rank": 10 }, { "submission_id": "aoj_1635_10521792", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n\t\nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nvoid solve(ll n,ll m){\n\tvll a(n),b(n);cin >> a >> b;\n\ta--;b--;//mod用\n\tvector<vector<ll>> dp(n+1,vector<ll>(n+1,INF));// p周で合わせる\n\n\t// vvll smldp(n+1,vll(n+1,INF));// 各iについて[0,j]での最小\n\tvvll bigdp(n+1,vll(n+1,INF));//各iについて[j,n]での最小\n\tdp[0][0] = 0;\n\t// repn(i,n){\n\t// \tsmldp[0][i] = 0;\n\t// }\n\tvll base(n);\n\trep(i,n){\n\t\tif(a[i] <= b[i]){\n\t\t\tbase[i] = b[i] - a[i];\n\t\t}else{\n\t\t\tbase[i] = b[i] - a[i] + m;\n\t\t}\n\t}\n\n\trep(i,n){\n\t\tll last = INF;\n\t\trepn(j,n){// j周で\n\t\t\tll num = m * j +base[i];\n\t\t\tif(num < 0){\n\t\t\t\tlast += m;\n\t\t\t\tchmin(last,dp[i][j]);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t//j周より大から\n\t\t\tif(j != n){\n\t\t\t\tchmin(dp[i+1][j],bigdp[i][j+1]);\n\t\t\t}\n\t\t\t//ちょうどj周で\n\t\t\tif(i == 0 ){\n\t\t\t\tchmin(dp[i+1][j],dp[i][j] + num);\n\t\t\t}else if(m * j + base[i-1] < num){\n\t\t\t\tchmin(dp[i+1][j],dp[i][j]+num - (m * j + base[i-1]));\n\t\t\t}else{\n\t\t\t\tchmin(dp[i+1][j],dp[i][j]);\n\t\t\t}\n\t\t\t//j周未満から\n\t\t\tif(j != 0 && i != 0){\n\t\t\t\tchmin(dp[i+1][j],last+num - (m * (j-1) + base[i-1]));\n\t\t\t}else if(j != 0){\n\t\t\t\tchmin(dp[i+1][j],last+num);\n\t\t\t}\n\t\t\tlast += m;\n\t\t\tchmin(last,dp[i][j]);\n\t\t}\n\t\t//bigの更新\n\t\trrep(j,n,0){\n\t\t\tif(j == n){\n\t\t\t\tbigdp[i+1][n] = dp[i+1][n];\n\t\t\t}else{\n\t\t\t\tbigdp[i+1][j] = min(dp[i+1][j],bigdp[i+1][j+1]);\n\t\t\t}\n\t\t}\n\n\t}\n\tcout << *min_element(all(dp[n])) << endl;\n\n}\n \nint main(){\n\tios::sync_with_stdio(false);cin.tie(nullptr);\n\twhile(true){\n\t\tLL(n,m);\n\t\tif(n == 0 && m == 0)break;\n\t\tsolve(n,m);\n\t}\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 19200, "score_of_the_acc": -1.2073, "final_rank": 18 }, { "submission_id": "aoj_1635_10505843", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define REPR(i, n) for (int i = n; i >= 0; i--)\n#define FOR(i, m, n) for (ll i = m; i < n; i++)\n#define FORR(i, m, n) for (ll i = m; i >= n; i--)\n#define REPO(i, n) for (ll i = 1; i <= n; i++)\n#define ll long long\n#define INF (ll)1ll << 60\n#define MINF (-1 * INF)\n#define ALL(n) n.begin(), n.end()\n#define MOD (ll)998244353\n#define P pair<ll, ll>\n\n\nint main(){\n while(1){\n ll n, m;\n cin >> n >> m;\n if(n == 0 and m == 0){\n return 0;\n }\n vector<ll> a(n) , b(n);\n vector<vector<ll>> dp(n + 1, vector<ll>(n + 2, INF));\n REP(i, n)cin >> a[i];\n REP(i, n) cin >> b[i];\n dp[0][0] = 0;\n REP(i, n){\n if(a[i] > b[i]) b[i] += m;\n //if(i == 0){\n if(i == 0){\n dp[i + 1][0] = min({dp[i + 1][0], dp[i][0] + b[i] - a[i]});\n continue;\n }\n REP(j, n + 1){\n ll now = b[i] - a[i];\n ll bf = -1;\n //else {\n bf = b[i - 1] - a[i - 1];\n dp[i + 1][j] = min({dp[i + 1][j], dp[i][j] + max(now + m * (j) - (bf + m * j), 0ll)});\n if(j + 1 < n + 2)dp[i + 1][j + 1] = min({dp[i + 1][j + 1], dp[i][j] + max(now + m * (j + 1) - (bf + m * j), 0ll)});\n if(j - 1 >= 0) dp[i + 1][j - 1] =min({dp[i + 1][j - 1], dp[i][j] + max(now + m * (j - 1) - (bf + m * j), 0ll)});\n //}\n //}\n //cout << i <<\" \" << j <<\" \" << dp[i][j] << endl;\n }\n }\n ll ans = INF;\n REP(i, n + 2){\n ans = min(ans , dp[n][i]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 10940, "score_of_the_acc": -0.6306, "final_rank": 17 }, { "submission_id": "aoj_1635_10505805", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,l,r) for(int i=(l);i<(r);i++)\n#define all(v) begin(v),end(v)\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<ll>;\nint main(){\n int N,M;\n while(1){\n cin>>N>>M;\n if(N==0&&M==0)return 0;\n vi A(N),B(N);\n for(int &i:A)cin>>i;\n for(int &i:B)cin>>i;\n rep(i,0,N){\n B[i]-=A[i];\n if(B[i]<0)B[i]+=M;\n }\n vl dp(N+1);\n rep(i,0,N+1){\n dp[i]=B[0]+i*M;\n }\n // rep(i,0,N+1)cerr<<`\n rep(i,1,N){\n vl ndp(N+1,1e18);\n ll m=1e18;\n if(B[i]>B[i-1]){\n int t=B[i]-B[i-1];\n rep(ri,0,N+1){\n int j=N-ri;\n ndp[j]=min(ndp[j],m);\n ndp[j]=min(ndp[j],dp[j]+t);\n m=min(m,dp[j]);\n }\n }else{\n int t=B[i]-B[i-1]+M;\n rep(ri,0,N+1){\n int j=N-ri;\n m=min(m,dp[j]);\n ndp[j]=min(ndp[j],m);\n if(j-1>=0)ndp[j]=min(ndp[j],dp[j-1]+t);\n }\n }\n ndp[0]=min(ndp[0],m+B[i]);\n rep(j,0,N){\n ndp[j+1]=min(ndp[j+1],ndp[j]+M);\n }\n rep(i,0,N+1)dp[i]=ndp[i];\n // rep(i,0,N+1)cerr<<dp[i]<<' ';\n // cerr<<endl;\n }\n cout<<*min_element(all(dp))<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3456, "score_of_the_acc": -0.0602, "final_rank": 6 }, { "submission_id": "aoj_1635_10505766", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, a, b) for(ll i = a; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n\nint main(){\n while(true){\n ll n,m;\n cin>>n>>m;\n if(n==0)break;\n vector<ll> a(n);\n vector<ll> b(n);\n vector<ll> c={0};\n rep(i,0,n){\n cin>>a[i];\n }\n rep(i,0,n){\n cin>>b[i];\n }\n rep(i,0,n){\n c.push_back((b[i]-a[i]+m)%m);\n }\n vector<ll> z(1100,2000000000000000000);\n z[0]=0;\n rep(i,0,n){\n ll d=(c[i+1]-c[i]+m)%m;\n vector<ll> nz(1100,2000000000000000000);\n rep(j,0,1050){\n if(c[i+1]>=c[i]){\n nz[j]=min(nz[j],z[j]+d);\n if(j!=0)nz[j-1]=min(nz[j-1],z[j]);\n }\n else{\n nz[j+1]=min(nz[j+1],z[j]+d);\n nz[j]=min(nz[j],z[j]);\n }\n }\n z=nz;\n }\n ll ans=1000000000000000000;\n rep(i,0,1100){\n ans=min(ans,z[i]);\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3584, "score_of_the_acc": -0.0561, "final_rank": 4 }, { "submission_id": "aoj_1635_10505749", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\n\nvoid solve(ll N,ll M) {\n vector<ll> A(N);\n vector<ll> B(N);\n rep(i,N){\n cin>>A[i];\n }\n rep(i,N){\n cin>>B[i];\n }\n vector<ll> C(N);\n rep(i,N){\n ll r=B[i]-A[i];\n r%=M;\n r+=M;\n r%=M;\n C[i]=r;\n }\n vector<ll> dp(N+1,1e9);\n dp[0]=0;\n ll bef=0;\n rep(i,N){\n ll nxt=C[i];\n vector<ll> ndp(N+1,1e9);\n rep(j,N+1){\n if (j>0){\n chmin(ndp[j-1],dp[j]+abs((j*M+bef)-((j-1)*M+nxt)));\n }\n if (true){\n chmin(ndp[j],dp[j]+abs((j*M+bef)-(j*M+nxt)));\n }\n if (j<N){\n chmin(ndp[j+1],dp[j]+abs((j*M+bef)-((j+1)*M+nxt)));\n }\n }\n swap(dp,ndp);\n bef=nxt;\n }\n ll ans=1e9;\n rep(i,N+1){\n chmin(ans,dp[i]+abs(i*M+bef));\n }\n ans/=2;\n cout<<ans<<endl;\n return;\n}\n\n\nint main() {\n ll N,M;\n while (true){\n cin>>N>>M;\n if (N==0){\n break;\n }\n solve(N,M);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3712, "score_of_the_acc": -0.052, "final_rank": 3 }, { "submission_id": "aoj_1635_10216927", "code_snippet": "// AOJ #1635\n// Tally Counters 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint n, m;\n\nstatic unsigned dp[1010][1010];\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while (true) {\n cin >> n >> m;\n if (n == 0) break;\n\n vector<int> A(n), B(n);\n for (int i = 0; i < n; ++i) cin >> A[i];\n for (int i = 0; i < n; ++i) {\n cin >> B[i];\n A[i] = (B[i] - A[i] + m) % m;\n }\n\n memset(dp, 0xFF, sizeof(dp));\n\n for (int j = 0; j < n; ++j) \n dp[0][j] = A[0] + (unsigned)j * (unsigned)m;\n\n for (int i = 1; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n {\n unsigned cost = (unsigned)std::max(0, A[i] - A[i - 1]);\n if (dp[i - 1][j] != 0xFFFFFFFF) {\n unsigned val = dp[i - 1][j] + cost;\n dp[i][j] = std::min(dp[i][j], val);\n }\n }\n {\n if (j > 0 && dp[i - 1][j - 1] != 0xFFFFFFFF) {\n unsigned cost = (unsigned)A[i] + (unsigned)(m - A[i - 1]);\n unsigned val = dp[i - 1][j - 1] + cost;\n dp[i][j] = min(dp[i][j], val);\n }\n }\n {\n if (j + 1 < n && dp[i - 1][j + 1] != 0xFFFFFFFF) {\n dp[i][j] = min(dp[i][j], dp[i - 1][j + 1]);\n }\n }\n }\n }\n int best = INT_MAX;\n for (int j = 0; j < n; ++j) {\n if (dp[n - 1][j] < 0xFFFFFFFF) {\n best = min(best, (int)dp[n - 1][j]);\n }\n }\n cout << best << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7392, "score_of_the_acc": -0.3332, "final_rank": 13 }, { "submission_id": "aoj_1635_10039466", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid chmax(int &a, int b){\n if(a<b)a=b;\n}\n\nint main(){\n while(1){\n int n,m;\n cin>>n>>m;\n if(n==0){\n return 0;\n }\n vector<int> a(n);\n vector<int> b(n);\n for(int i=0;i<n;i++)cin>>a[i];\n for(int i=0;i<n;i++)cin>>b[i];\n for(int i=0;i<n;i++){\n if(a[i]>b[i])a[i]=m+b[i]-a[i];\n else a[i]=b[i]-a[i];\n }\n vector<int> d(n);\n vector<int> rwa(n+1);\n d[0] = a[0];\n for(int i=1;i<n;i++){d[i]=a[i]-a[i-1]; if(d[i]<0)d[i]+=m;}\n for(int i=1;i<=n;i++)rwa[i]=rwa[i-1]+d[i-1];\n //二乗が通る!!\n priority_queue<int> pq;\n int ans=0;\n int now=0;\n for(int i=0;i<n;i++){\n if(i==0){ans+=d[0];now=ans;}\n else{\n int neg = m-d[i];\n if(neg==m)neg=0;\n pq.push(neg);\n now-=neg;\n if(now<0){\n int v = pq.top();\n if(v==0)v=m;\n else v=m-v;\n now+=m;\n ans+=v;\n pq.pop();\n }\n }\n }\n cout<<ans<<endl;\n\n \n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3492, "score_of_the_acc": -0.0076, "final_rank": 2 } ]
aoj_1634_cpp
Balance Scale You, an experimental chemist, have a balance scale and a kit of weights for measuring weights of powder chemicals. For work efficiency, a single use of the balance scale should be enough for measurement of each amount. You can use any number of weights at a time, placing them either on the balance plate opposite to the chemical or on the same plate with the chemical. For example, if you have two weights of 2 and 9 units, you can measure out not only 2 and 9 units of the chemical, but also 11 units by placing both on the plate opposite to the chemical (Fig. C-1 left), and 7 units by placing one of them on the plate with the chemical (Fig. C-1 right). These are the only amounts that can be measured out efficiently. Fig. C-1 Measuring 11 and 7 units of chemical You have at hand a list of amounts of chemicals to measure today. The weight kit already at hand, however, may not be enough to efficiently measure all the amounts in the measurement list. If not, you can purchase one single new weight to supplement the kit, but, as heavier weights are more expensive, you'd like to do with the lightest possible. Note that, although weights of arbitrary positive masses are in the market, none with negative masses can be found. Input The input consists of at most 100 datasets, each in the following format. n m a 1 a 2 ... a n w 1 w 2 ... w m The first line of a dataset has n and m , the number of amounts in the measurement list and the number of weights in the weight kit at hand, respectively. They are integers separated by a space satisfying 1 ≤ n ≤ 100 and 1 ≤ m ≤ 10. The next line has the n amounts in the measurement list, a 1 through a n , separated by spaces. Each of a i is an integer satisfying 1 ≤ a i ≤ 10 9 , and a i ≠ a j holds for i ≠ j . The third and final line of a dataset has the list of the masses of the m weights at hand, w 1 through w m , separated by spaces. Each of w j is an integer, satisfying 1 ≤ w j ≤ 10 8 . Two or more weights may have the same mass. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing an integer specified as follows. If all the amounts in the measurement list can be measured out without any additional weights, 0 . If adding one more weight will make all the amounts in the measurement list measurable, the mass of the lightest among such weights. The weight added may be heavier than 10 8 units. If adding one more weight is never enough to measure out all the amounts in the measurement list, -1 . Sample Input 4 2 9 2 7 11 2 9 6 2 7 3 6 12 16 9 2 9 5 2 7 3 6 12 17 2 9 7 5 15 21 33 48 51 75 111 36 54 57 93 113 0 0 Output for the Sample Input 0 5 -1 5
[ { "submission_id": "aoj_1634_10898057", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e5+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nconst int dr[] = {-1,0,1,0}, dc[] = {0,1,0,-1};\nvoid setIO(string);\nint n,m;\nll a[N], b[N];\nvoid recur(int i, ll sum, unordered_set<ll>& sums) {\n if(i == m) {\n if(sum >= 0) sums.insert(sum);\n return;\n }\n recur(i+1, sum, sums);\n recur(i+1, sum + b[i], sums);\n recur(i+1, sum - b[i], sums);\n}\nvoid solve(){\n \n cin>>n>>m;\n while(n != 0 || m != 0) {\n for(int i = 0; i < n; i++) {\n cin>>a[i];\n }\n for(int j = 0; j < m; j++) {\n cin>>b[j];\n }\n unordered_set<ll> sums;\n recur(0,0ll,sums);\n\n unordered_set<ll> save;\n int need = 0;\n for(int i = 0; i < n; i++) {\n if(sums.count(a[i])) continue;\n if(!save.empty()) continue;\n need++;\n for(auto &x : sums) {\n save.insert(llabs(x - a[i])); \n save.insert(x + a[i]);\n }\n }\n\n if(need == 0) {\n cout << 0;\n } else {\n ll weight = LLONG_MAX;\n //save.insert(0ll);\n for(auto &x : save) {\n bool ok = true;\n for(int i = 0; i < n; i++) {\n if(sums.count(a[i]) || sums.count(a[i] + x) || sums.count(llabs(a[i] - x))) continue;\n ok = false;\n }\n if(ok) {\n weight = min(weight, x);\n }\n }\n cout << (weight == LLONG_MAX ? -1 : weight);\n }\n cout << nl;\n cin>>n>>m;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 2220, "memory_kb": 7652, "score_of_the_acc": -0.2835, "final_rank": 8 }, { "submission_id": "aoj_1634_10851500", "code_snippet": "#include <bits/stdc++.h>\n#define zpw \\\n ios::sync_with_stdio(false); \\\n cin.tie(NULL); \\\n cout.tie(NULL)\n\nusing namespace std;\n\ntypedef long long ll;\n\nint a[100 + 10];\nint w[100 + 10];\nvector<int> vt;\nvector<int> st[110];\n\nint main() {\n zpw;\n int n, m;\n // freopen(\"in.txt\", \"r\", stdin);\n while (cin >> n >> m, (n || m)) {\n vt.clear();\n for (int i = 0; i < 110; i++) {\n st[i].clear();\n }\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n }\n for (int i = 0; i < m; i++) {\n cin >> w[i];\n }\n int tot = pow(3, m);\n int we[tot + 10];\n for (int i = 0; i < tot; i++) {\n int t = i;\n int num = 0;\n for (int j = 0; j < m; j++) {\n if (t % 3 == 1) {\n num += w[j];\n } else if (t % 3 == 2) {\n num -= w[j];\n }\n t /= 3;\n }\n we[i] = num;\n }\n for (int i = 0; i < n; i++) {\n int flag = 0;\n for (int j = 0; j < tot; j++) {\n if (a[i] == abs(we[j])) {\n flag = 1;\n break;\n }\n }\n if (!flag) {\n vt.push_back(i);\n st[i].push_back(a[i]);\n }\n }\n if (vt.empty()) {\n cout << 0 << endl;\n continue;\n }\n for (int i = 0; i < vt.size(); i++) {\n int u = vt[i];\n for (int j = 0; j < tot; j++) {\n st[u].push_back(abs(we[j] - a[u]));\n }\n sort(st[u].begin(), st[u].end());\n int num = unique(st[u].begin(), st[u].end()) - st[u].begin();\n st[u].resize(num);\n }\n int s = vt[0];\n int ans = -1;\n for (int i = 0; i < st[s].size(); i++) {\n int flag = 1;\n for (int j = 1; j < vt.size(); j++) {\n int u = vt[j];\n vector<int>::iterator it =\n lower_bound(st[u].begin(), st[u].end(), st[s][i]);\n if (it == st[u].end() || *it != st[s][i]) {\n flag = 0;\n break;\n }\n }\n if (flag) {\n ans = st[s][i];\n break;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1560, "memory_kb": 24148, "score_of_the_acc": -0.2656, "final_rank": 7 }, { "submission_id": "aoj_1634_10684537", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(int n, int m) {\n vector<ll> a(n), w(m);\n ll wsm = 0;\n ll amn = 1ll << 60;\n for (auto &i: a) {\n cin >> i;\n amn = min(amn, i);\n }\n for (auto &i: w) {\n cin >> i;\n wsm += i;\n }\n vector<ll> st;\n st.push_back(0);\n for (int i = 0; i < m; i++) {\n wsm -= w[i];\n vector<ll> nst;\n for (auto j: st) {\n nst.push_back(j);\n nst.push_back(j + w[i]);\n nst.push_back(j - w[i]);\n }\n sort(nst.begin(), nst.end());\n nst.erase(unique(nst.begin(), nst.end()), nst.end());\n swap(st, nst);\n }\n vector<ll> vote;\n int ok = 0;\n bool first = true;\n for (const auto &i: a) {\n if (*lower_bound(st.begin(), st.end(), i) == i) {\n ok++;\n continue;\n }\n if (first) {\n first = false;\n for (const auto &j: st) {\n vote.push_back(abs(i - j));\n }\n sort(vote.begin(), vote.end());\n vote.erase(unique(vote.begin(), vote.end()), vote.end());\n }\n else {\n vector<ll> nvote;\n for (const auto &j: st) {\n nvote.push_back(abs(i - j));\n }\n sort(nvote.begin(), nvote.end());\n nvote.erase(unique(nvote.begin(), nvote.end()), nvote.end());\n vector<ll> tmp;\n for (const auto &j: vote) {\n if (*lower_bound(nvote.begin(), nvote.end(), j) == j) tmp.push_back(j);\n }\n swap(vote, tmp);\n }\n }\n if (ok == n) cout << \"0\\n\";\n else {\n for (const auto &pa: vote) {\n cout << pa << \"\\n\";\n return;\n }\n cout << \"-1\\n\";\n }\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) return 0;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 5272, "score_of_the_acc": -0.1033, "final_rank": 6 }, { "submission_id": "aoj_1634_10674319", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Init {\n Init() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << setprecision(13);\n }\n} init;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing VLL = vector<ll>;\nusing VVLL = vector<VLL>;\nusing PLL = pair<ll, ll>;\nusing VS = vector<string>;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n#define FOR(i, a, b) for (int i = int(a); i < int(b); ++i)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) x.rbegin(), x.rend()\n\nconst double PI = 3.141592653589793238;\nconst int INF = 1073741823;\nconst ll INFLL = 1LL << 60;\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++)\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (T& in : v)\n is >> in;\n return is;\n}\nvoid print() {\n cout << '\\n';\n}\ntemplate <typename T>\nvoid print(const T& t) {\n cout << t << '\\n';\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head& head, const Tail&... tail) {\n cout << head << ' ';\n print(tail...);\n}\ntemplate <typename T1, typename T2>\ninline bool chmax(T1& a, T2 b) {\n bool compare = a < b;\n if (compare)\n a = b;\n return compare;\n}\ntemplate <typename T1, typename T2>\ninline bool chmin(T1& a, T2 b) {\n bool compare = a > b;\n if (compare)\n a = b;\n return compare;\n}\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0)\n return 0;\n\n VLL a(n);\n VLL w(m);\n cin >> a >> w;\n\n set<ll> hakareru;\n for (auto i : w)\n hakareru.insert(i);\n for (int i = 0; i < (1 << m); i++) {\n for (int j = 0; j < (1 << m); j++) {\n if ((i & j) != 0)\n continue;\n ll weight = 0;\n rep(z, m) {\n if (i & (1 << z))\n weight += w[z];\n if (j & (1 << z))\n weight -= w[z];\n }\n if (weight <= 0)\n continue;\n\n hakareru.insert(weight);\n if (weight == 48) {\n print(i, j);\n }\n }\n }\n set<ll> hakarenai;\n rep(i, n) {\n if (!hakareru.count(a[i])) {\n hakarenai.insert(a[i]);\n }\n }\n if (hakarenai.empty()) {\n print(0);\n continue;\n }\n\n vector<set<ll>> e(hakarenai.size());\n {\n int i = 0;\n for (auto c : hakarenai) {\n for (auto d : hakareru) {\n e[i].insert(c + d);\n e[i].insert(abs(c - d));\n e[i].insert(c);\n }\n i++;\n }\n }\n\n if (hakarenai.size() == 1) {\n for (auto hh : e[0]) {\n print(hh);\n break;\n }\n continue;\n }\n bool flag = false;\n for (auto x : e[0]) {\n FOR(i, 1, hakarenai.size()) {\n if (!e[i].count(x)) {\n break;\n }\n if (i == hakarenai.size() - 1) {\n flag = true;\n print(x);\n break;\n }\n }\n if (flag)\n break;\n }\n if (!flag) {\n print(-1);\n }\n }\n}", "accuracy": 1, "time_ms": 5200, "memory_kb": 234448, "score_of_the_acc": -1.5831, "final_rank": 18 }, { "submission_id": "aoj_1634_10671618", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(ll n, ll m){\n vector<ll> a(n),b(m);\n for (int i = 0; i < n; i++)cin >> a[i];\n for (int i = 0; i < m; i++)cin >> b[i];\n\n set<ll> c;\n for (int i = 0; i < m; i++){\n set<ll> d;\n d.insert(b[i]);\n for (auto p: c){\n d.insert(p);\n d.insert(abs(p-b[i]));\n d.insert(abs(p+b[i]));\n }\n swap(c,d);\n }\n\n vector<ll> no;\n for (int i = 0; i < n; i++)if (!c.count(a[i]))no.push_back(a[i]);\n if (no.size() == 0){\n cout << 0 << endl;\n return;\n }\n\n vector<set<ll>> diff(no.size());\n for (int i = 0; i < no.size(); i++){\n for (auto p: c){\n diff[i].insert(no[i]);\n diff[i].insert(p+no[i]);\n diff[i].insert(abs(p-no[i]));\n }\n }\n int idx = 0;\n for (int i = 1; i < no.size(); i++)if (diff[idx].size() > diff[i].size())idx = i;\n\n for (auto p: diff[idx]){\n bool jud = true;\n for (int i = 0; i < no.size(); i++){\n if (i == idx)continue;\n\n if (!diff[i].count(p)){\n jud = false;\n break;\n }\n }\n if (jud){\n cout << p << endl;\n return;\n }\n }\n cout << -1 << endl;\n}\n\nint main(){\n ll n,m;cin >> n >> m;\n while(n){\n solve(n,m);\n cin >> n >> m;\n }\n}", "accuracy": 1, "time_ms": 5150, "memory_kb": 234512, "score_of_the_acc": -1.577, "final_rank": 17 }, { "submission_id": "aoj_1634_10658329", "code_snippet": "# include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nconst double pi = acos(-1);\ntemplate<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }\ntemplate<class T>constexpr T hinf() { return inf<T>() / 2; }\ntemplate <typename T_char>T_char TL(T_char cX) { return tolower(cX); }\ntemplate <typename T_char>T_char TU(T_char cX) { return toupper(cX); }\ntemplate<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }\ntemplate<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }\nint popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }\nint d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }\nint d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }\nll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };\nll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };\nll MOD(ll x, ll m){return (x%m+m)%m; }\nll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }\ntemplate<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;\n# define all(qpqpq) (qpqpq).begin(),(qpqpq).end()\n# define UNIQUE(wpwpw) (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())\n# define LOWER(epepe) transform(all((epepe)),(epepe).begin(),TL<char>)\n# define UPPER(rprpr) transform(all((rprpr)),(rprpr).begin(),TU<char>)\n# define rep(i,upupu) for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)\n# define reps(i,opopo) for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)\n# define len(x) ((ll)(x).size())\n# define bit(n) (1LL << (n))\n# define pb push_back\n# define eb emplace_back\n# define exists(c, e) ((c).find(e) != (c).end())\n\nstruct INIT{\n\tINIT(){\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(0);\n\t\tcout << fixed << setprecision(20);\n\t}\n}INIT;\n\nnamespace mmrz {\n\tvoid solve();\n}\n\nint main(){\n\tmmrz::solve();\n}\n#define debug(...) (static_cast<void>(0))\n\nusing namespace mmrz;\n\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing ull = unsigned long long;\nusing lll = __int128;\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\n// [BEGIN] template の include\n\nint SOLVE(){\n\tint n, m;\n\tcin >> n >> m;\n\tif(n == 0 && m == 0)return 1;\n\tvector<int> a(n), w(m);\n\tfor(auto &x : a)cin >> x;\n\tfor(auto &x : w)cin >> x;\n\t\n\tvector<int> valid_weights;\n\tauto dfs = [&](auto f, int idx, int c) -> void {\n\t\tif(idx == m){\n\t\t\tvalid_weights.eb(c);\n\t\t\treturn;\n\t\t}\n\t\tfor(int d = -1;d <= 1;d++){\n\t\t\tc += w[idx]*d;\n\t\t\tf(f, idx+1, c);\n\t\t\tc -= w[idx]*d;\n\t\t}\n\t};\n\tdfs(dfs, 0, 0);\n\tsort(all(valid_weights));\n\tUNIQUE(valid_weights);\n\t\n\t{\n\t\tvector<int> na;\n\t\tfor(auto &x : a){\n\t\t\tauto it = lower_bound(all(valid_weights), x);\n\t\t\tif(it != valid_weights.end() && *it == x)continue;\n\t\t\tna.eb(x);\n\t\t}\n\t\ta = na;\n\t}\n\tif(a.empty()){\n\t\tcout << \"0\\n\";\n\t\treturn 0;\n\t}\n\tint ans = hinf<int>();\n\tfor(auto x : valid_weights){\n\t\tint cand = abs(a[0]-x);\n\t\tbool flg = true;\n\t\tfor(auto sa : a){\n\t\t\tauto it_l = lower_bound(all(valid_weights), abs(sa-cand));\n\t\t\tif(it_l != valid_weights.end() && *it_l == abs(sa-cand))continue;\n\t\t\tauto it_r = lower_bound(all(valid_weights), sa+cand);\n\t\t\tif(it_r != valid_weights.end() && *it_r == sa+cand)continue;\n\t\t\tflg = false;\n\t\t\tbreak;\n\t\t}\n\t\tif(flg)chmin(ans, cand);\n\t}\n\n\tcout << (ans == hinf<int>() ? -1 : ans) << '\\n';\n\treturn 0;\n}\n\nvoid mmrz::solve(){\n\twhile(!SOLVE());\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3820, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1634_10630780", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\n// Linear Algebra ////////////////////////////////////////////////\nconst double Rad2Deg = 180.0 / M_PI;\nconst double Deg2Rad = M_PI / 180.0;\n//////////////////////////////////////////////////////////////////\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n\n\n\n\nint solve() {\n\tll n, m; cin >> n >> m;\n\tif (n == 0 && m == 0) return 1;\n\tvll a(n), w(m);\n\tcin >> a;\n\tcin >> w;\n\n\tset<ll> needs;\n\trep(i, n) needs.insert(a[i]);\n\n\tset<ll> can_weights;\n\tauto dfs = [&](auto self, ll l, ll r, ll i) -> void {\n\t\tcan_weights.insert(abs(l - r));\n\t\tneeds.erase(abs(l - r));\n\t\tif (m <= i) return;\n\n\t\tself(self, l, r, i + 1);\n\t\tself(self, l + w[i], r, i + 1);\n\t\tself(self, l, r + w[i], i + 1);\n\t};\n\tdfs(dfs, 0, 0, 0);\n\n\tif (needs.size() == 0) {\n\t\tcout << 0 << \"\\n\";\n\t\treturn 0;\n\t}\n\n\tset<ll> ok;\n\tauto it = needs.begin();\n\tfor (ll weight : can_weights) {\n\t\tok.insert(abs(*it - weight));\n\t\tok.insert(*it + weight);\n\t}\n\tit++;\n\tfor (;it != needs.end(); it++) {\n\t\tset<ll> oldok;\n\t\tswap(ok, oldok);\n\t\tfor (ll weight : can_weights) {\n\t\t\tll diff = abs(*it - weight);\n\t\t\tll sum = *it + weight;\n\t\t\tif (oldok.count(diff)) ok.insert(diff);\n\t\t\tif (oldok.count(sum)) ok.insert(sum);\n\t\t}\n\t}\n\n\tif (ok.size() == 0) {\n\t\tcout << -1 << \"\\n\";\n\t}\n\telse {\n\t\tcout << *ok.begin() << \"\\n\";\n\t}\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 9416, "score_of_the_acc": -0.0893, "final_rank": 4 }, { "submission_id": "aoj_1634_10615862", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n, m;\nvector<ll> a, w;\nvoid input() {\n cin >> n >> m;\n a.resize(n);\n w.resize(m);\n for(auto &x : a)\n cin >> x;\n for(auto &x : w)\n cin >> x;\n}\nvoid solve() {\n unordered_set<ll> sh;\n ll san = 1;\n for(int i = 0; i < m; i++) {\n san *= 3;\n }\n for(int i = 0; i < san; i++) {\n ll sum = 0;\n ll t = 1;\n for(int j = 0; j < m; j++) {\n if((i / t) % 3 == 1) {\n sum += w[j];\n } else if((i / t) % 3 == 2) {\n sum -= w[j];\n }\n t *= 3;\n }\n sh.insert(sum);\n }\n\n for(int i = 0; i < n; i++) {\n if(sh.count(a[i]) == 0) {\n ll ans = 1e18;\n for(auto &x : sh) {\n ll add = a[i] - x;\n if(add < 0)\n continue;\n bool ok = true;\n for(int j = 0; j < n; j++) {\n if(sh.count(a[j]) == 0 && sh.count(a[j] - add) == 0 &&\n sh.count(a[j] + add) == 0)\n ok = false;\n }\n if(ok) {\n ans = min(add,ans);\n }\n }\n for(auto &x : sh) {\n ll add = x - a[i];\n if(add < 0)\n continue;\n bool ok = true;\n for(int j = 0; j < n; j++) {\n if(sh.count(a[j]) == 0 && sh.count(a[j] - add) == 0 &&\n sh.count(a[j] + add) == 0)\n ok = false;\n }\n if(ok) {\n ans = min(add,ans);\n }\n }\n\n cout << (ans == 1e18 ? -1 : ans) << endl;\n return;\n }\n }\n\n cout << 0 << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2280, "memory_kb": 6148, "score_of_the_acc": -0.2851, "final_rank": 9 }, { "submission_id": "aoj_1634_10615858", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n, m;\nvector<ll> a, w;\nvoid input() {\n cin >> n >> m;\n a.resize(n);\n w.resize(m);\n for(auto &x : a)\n cin >> x;\n for(auto &x : w)\n cin >> x;\n}\nvoid solve() {\n set<ll> sh;\n ll san = 1;\n for(int i = 0; i < m; i++) {\n san *= 3;\n }\n for(int i = 0; i < san; i++) {\n ll sum = 0;\n ll t = 1;\n for(int j = 0; j < m; j++) {\n if((i / t) % 3 == 1) {\n sum += w[j];\n } else if((i / t) % 3 == 2) {\n sum -= w[j];\n }\n t *= 3;\n }\n sh.insert(sum);\n }\n\n for(int i = 0; i < n; i++) {\n if(sh.count(a[i]) == 0) {\n ll ans = 1e18;\n for(auto &x : sh) {\n ll add = a[i] - x;\n if(add < 0)\n continue;\n bool ok = true;\n for(int j = 0; j < n; j++) {\n if(sh.count(a[j]) == 0 && sh.count(a[j] - add) == 0 &&\n sh.count(a[j] + add) == 0)\n ok = false;\n }\n if(ok) {\n ans = min(add,ans);\n }\n }\n for(auto &x : sh) {\n ll add = x - a[i];\n if(add < 0)\n continue;\n bool ok = true;\n for(int j = 0; j < n; j++) {\n if(sh.count(a[j]) == 0 && sh.count(a[j] - add) == 0 &&\n sh.count(a[j] + add) == 0)\n ok = false;\n }\n if(ok) {\n ans = min(add,ans);\n }\n }\n\n cout << (ans == 1e18 ? -1 : ans) << endl;\n return;\n }\n }\n\n cout << 0 << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 7290, "memory_kb": 6116, "score_of_the_acc": -0.9272, "final_rank": 12 }, { "submission_id": "aoj_1634_10596808", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define fr(i,n) for(long long i=0; i<(n); i++)\nusing ll = long long;\n\nint main() {\n vector<ll> ans;\n while(true){\n ll n,m;\n cin >> n >> m;\n if(n==0) break;\n vector<set<ll>> buy(n);\n unordered_set<ll> possible;\n vector<ll> a(n),w(m),inpossible;\n fr(i,n) cin >> a[i];\n fr(i,m) cin >> w[i];\n possible.insert(0);\n fr(i,m){\n auto itr = possible.begin();\n queue<ll> q;\n fr(j,possible.size()){\n q.push(*itr+w[i]);\n q.push(abs(*itr-w[i]));\n itr++;\n }\n while(!q.empty()){\n possible.insert(q.front());\n q.pop();\n }\n }\n fr(i,n){\n if(possible.count(a[i])) continue;\n inpossible.push_back(i);\n auto itr = possible.begin();\n fr(j,possible.size()){\n buy[i].insert(a[i]+*itr);\n buy[i].insert(abs(a[i]-*itr));\n itr++;\n }\n }\n if(inpossible.size() == 0) ans.push_back(0);\n else{\n set<ll> common = buy[inpossible[0]];\n for(ll i=1; i<inpossible.size(); i++){\n set<ll> tmp;\n set_intersection(common.begin(),common.end(),buy[inpossible[i]].begin(),buy[inpossible[i]].end(),inserter(tmp, tmp.begin()));\n common = tmp;\n }\n if(common.size()==0) ans.push_back(-1);\n else ans.push_back(*common.begin());\n }\n }\n fr(i,ans.size()) cout << ans[i] << endl;\n}", "accuracy": 1, "time_ms": 6680, "memory_kb": 237052, "score_of_the_acc": -1.7834, "final_rank": 19 }, { "submission_id": "aoj_1634_10592968", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N==0) return 0;\n vector<ll>A(N),W(M);\n for(auto &a:A)cin>>a;\n for(auto &w:W)cin>>w;\n \n //追加しなくてもいい薬品量を求める\n set<ll>S;\n S.insert(0);\n rep(i,0,M){\n set<ll>nS;\n for(auto s:S){\n nS.insert(s+W[i]); \n nS.insert(s-W[i]); \n nS.insert(s);\n }\n S=nS;\n }\n\n //Aからそのままでいいものを削除\n for(int i=N-1;i>=0;i--){\n if(S.count(A[i])){\n A.erase(A.begin()+i);\n }\n }\n\n //0個になったら\n if(sz(A)==0){\n cout<<0<<endl;\n return 1;\n }\n\n //すべてが取れるようなものがあるか?\n //候補としてAの先頭要素に追加して量れるようになる分銅の量\n vector<ll>candidates;\n for(auto s:S){\n candidates.push_back(abs(A[0]-s));\n }\n\n \n //最小から見ていきいい感じのとる\n sort(all(candidates));\n for(auto c:candidates){\n bool f=true;\n for(auto a:A){\n if(!(S.count(a-c) || S.count(a+c))){\n f=false;\n break;\n }\n }\n if(f){\n cout<<c<<endl;\n return 1;\n }\n } \n cout<<-1<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 9344, "score_of_the_acc": -0.03, "final_rank": 3 }, { "submission_id": "aoj_1634_10588382", "code_snippet": "//~~~~~~~~~~\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\n#if !defined(ONLINE_JUDGE)\n#define _GLIBCXX_DEBUG\n#endif\n\n#define rrep(i, s, n) for (ll i = s; i < (n); i++)\n#define rep(i, n) rrep(i, 0, n)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n\n//~~~~~~~~~~\n\ntemplate <class ...C>\nvoid in(C &...v) { (cin >> ... >> v); }\n\ntemplate <class C>\nvoid out_single(const C& v) { cout << v; }\n\nvoid out_single(const char& v) { cout << v; }\n\ntemplate <class ...C>\nvoid out(C&&...v) { (out_single(v), ...); }\n\ntemplate <class C>\nvoid err_single(const C& v) { cerr << v; }\n\nvoid err_single(const char& v) { cerr << v; }\n\ntemplate <class ...C>\nvoid err(C&&...v) { (err_single(v), ...); }\n\n//~~~~~~~~~~\n\ntemplate <class C>\nbool chmax(C &a, const C b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class C>\nbool chmin(C &a, const C b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class C1, class C2>\nistream& operator>>(istream& is, pair<C1, C2>& p) {\n return is >> p.first >> p.second;\n}\n\nbool is_grid(int h, int w, int y, int x) {\n return (0 <= y && y < h && 0 <= x && x < w);\n}\n\n//~~~~~~~~~~\n\nll llpow(ll x, ll y) {\n ll res = 1;\n while (y--) res *= x;\n return res;\n}\n\n//~~~~~~~~~~\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while (true) {\n int n, m; in(n, m);\n if (n == 0 && m == 0) break;\n\n vector<int> A(n), B(m);\n rep (i, n) in(A[i]);\n rep (i, m) in(B[i]);\n\n // 現時点で作れる数の全列挙\n set<int> cur_canmake;\n rep (bit, llpow(3, m)) { // 59049回\n int left_ = 0, right_ = 0;\n rep (i, m) {\n if (bit / llpow(3, i) % 3 == 0) {\n left_ += B[i];\n } else if (bit / llpow(3, i) % 3 == 1) {\n right_ += B[i];\n }\n }\n\n cur_canmake.insert(abs(left_ - right_));\n }\n\n // // この分銅があれば作れるよ!リスト\n // // もしそのidx番目のsetに0があるなら既に作れる!\n // vector<set<int>> G(n);\n // set<int> test;\n // rep (idx, n) { // 100回\n // for (int i : cur_canmake) { // 59049回\n // G[idx].insert(abs(A[idx] - i));\n // G[idx].insert(A[idx] + i);\n // test.insert(abs(A[idx] - i));\n // test.insert(A[idx] + i);\n // }\n // }\n\n // // res を探す\n // // 全部に0があったら? -> 範囲for分の最初で落とせる\n // // 全部探してもなかったら? -> そのままres出力\n // int res = -1;\n // bool ok = false;\n // for (int i : test) { // 約100000回\n // rep (j, n) { // 100回\n // if (!G[j].count(0) && !G[j].count(i)) break;\n\n // if (j == n - 1) {\n // res = i;\n // ok = true;\n // break;\n // }\n // }\n // if (ok) break;\n // }\n\n // この分銅があれば作れるよ!リスト\n // もしそのidx番目のsetに0があるなら既に作れる!\n vector<set<int>> G(n);\n rep (idx, n) { // 100回\n for (int i : cur_canmake) { // 59049回\n G[idx].insert(abs(A[idx] - i));\n G[idx].insert(A[idx] + i);\n }\n }\n\n // res を探す\n // 全部に0があったら? -> 範囲for分の最初で落とせる\n // 全部探してもなかったら? -> そのままres出力\n int res = -1;\n bool ok = false;\n rep (i, n) {\n for (int j : G[i]) {\n rep (k, n) {\n if (i == k) continue;\n\n if (!G[k].count(0) && !G[k].count(j)) {\n break;\n }\n\n if (k == n - 1) {\n res = j;\n ok = true;\n break;\n }\n }\n if (ok) break;\n }\n if (ok) break;\n }\n\n out(res, '\\n');\n\n /*\n 6 2\n 7 3 6 12 16 9\n 2 9\n\n 3 6 7 9 12 16\n 2 7 9 11\n\n - 1 4 5 7 10 14\n + 5 8 9 11 14 18\n - 4 1 0 2 5 9\n - 6 3 2 0 3 7\n - 8 5 4 2 1 5\n \n */\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 7930, "memory_kb": 250972, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1634_10588195", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#ifdef LOCAL\n #include \"settings/debug.cpp\"\n#else\n #define Debug(...) void(0)\n#endif\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nusing ull = unsigned long long;\n\ninline void solve() {\n int n, m;\n cin >> n >> m;\n if (n + m == 0) exit(0);\n vector<ll> a(n);\n rep(i, n) cin >> a[i];\n vector<ll> w(m);\n rep(i, m) cin >> w[i];\n\n set<ll> cand;\n function<void(int, ll)> dfs = [&](int i, ll sum) {\n if (i == m) {\n cand.insert(sum);\n return;\n }\n dfs(i + 1, sum);\n dfs(i + 1, sum + w[i]);\n dfs(i + 1, sum - w[i]);\n };\n dfs(0, 0);\n\n {\n bool ok = true;\n for (auto x : a) {\n if (!cand.contains(x)) ok = false;\n }\n if (ok) {\n cout << 0 << '\\n';\n return;\n }\n }\n\n set<ll> need;\n bool flg = false;\n for (auto x : a) {\n if (cand.contains(x)) continue;\n if (!flg) {\n flg = true;\n for (auto y : cand) {\n need.insert(x + y);\n need.insert(x - y);\n need.insert(y - x);\n }\n }\n else {\n set<ll> new_need;\n for (auto y : need) {\n if (cand.contains(x + y) || cand.contains(x - y) || cand.contains(y - x)) new_need.insert(y);\n }\n swap(need, new_need);\n }\n }\n if (need.empty()) {\n cout << -1 << '\\n';\n return;\n }\n ll ans = 1e18;\n for (auto x : need) {\n if (x <= 0) continue;\n ans = min(ans, x);\n }\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n while (true) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 12672, "score_of_the_acc": -0.0948, "final_rank": 5 }, { "submission_id": "aoj_1634_10583490", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(int i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(int i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\ntemplate<class T, size_t n, size_t idx = 0>\nauto make_vec(const size_t (&d)[n], const T& init) noexcept {\n if constexpr (idx < n) return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else return init;\n}\ntemplate<class T, size_t n>\nauto make_vec(const size_t (&d)[n]) noexcept {\n return make_vec(d, T{});\n}\n/**\n * Read a vector from input. Set start to 1 if you want it to be 1-indexed.\n */\ntemplate<typename T>\nvector<T> read_vector(int N, int start = 0) {\n vector<T> v(start + N);\n for (int i = start; i < (int)v.size(); i++) {\n std::cin >> v[i];\n }\n return v;\n}\n\nvoid solve(int &n, int &m) {\n vector<int> a(n), w(m);\n rep(i, 0, n) cin >> a[i];\n rep(i, 0, m) cin >> w[i];\n\n set<int> s;\n s.insert(0);\n rep(i, 0, m) {\n set<int> nexts;\n for(auto curw : s) {\n rep(c, -1, 2) {\n nexts.insert(curw + c * w[i]); \n }\n }\n swap(nexts, s);\n }\n\n bool canbalanced = true;\n rep(i, 0, n) {\n if(!s.count(a[i])) canbalanced = false;\n }\n\n if(canbalanced) {\n cout << 0 << endl;\n return ;\n }\n\n\n int ans = intINF;\n rep(i, 0, n) {\n if(s.count(a[i])) continue;\n\n for(auto allw : s) {\n int addw = abs(allw - a[i]);\n if(addw < 0) continue;\n\n bool isok = true;\n rep(j, 0, n) {\n bool isfind = false;\n for(auto cc : {-1, 0, 1}) {\n if(s.count(a[j] + cc * addw)) isfind = true;\n }\n\n if(!isfind) {\n isok = false;\n break;\n }\n }\n\n if(isok) chmin(ans, addw);\n }\n }\n\n cout << (ans == intINF ? -1 : ans) << endl;\n}\n\nint main() {\n int n, m;\n while(true) {\n cin >> n >> m;\n if(n == 0 && m == 0) return 0;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 4670, "memory_kb": 6984, "score_of_the_acc": -0.5949, "final_rank": 10 }, { "submission_id": "aoj_1634_10557206", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid dfs1(int m, long long sum, vector<long long> &W, int M, bool &f) {\n if (m == M) {\n if (sum == 0) {\n f = true;\n }\n return;\n }\n dfs1(m + 1, sum + W[m], W, M, f);\n dfs1(m + 1, sum, W, M, f);\n dfs1(m + 1, sum - W[m], W, M, f);\n}\n\nvoid dfs2(int m, long long sum, vector<long long> &W, int M,\n map<long long, int> &add) {\n if (m == M) {\n add[abs(sum)] = 1;\n return;\n }\n dfs2(m + 1, sum + W[m], W, M, add);\n dfs2(m + 1, sum, W, M, add);\n dfs2(m + 1, sum - W[m], W, M, add);\n}\n\nvoid dfs3(int m, long long sum, vector<long long> &W, int M,\n set<long long> &add) {\n if (m == M) {\n add.insert(abs(sum));\n return;\n }\n dfs3(m + 1, sum + W[m], W, M, add);\n dfs3(m + 1, sum, W, M, add);\n dfs3(m + 1, sum - W[m], W, M, add);\n}\n\nvoid solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n exit(0);\n }\n vector<long long> A(N), W(M);\n for (int i = 0; i < N; i++) {\n cin >> A[i];\n }\n for (int i = 0; i < M; i++) {\n cin >> W[i];\n }\n vector<int> a;\n for (int i = 0; i < N; i++) {\n bool flag = false;\n dfs1(0, A[i], W, M, flag);\n if (flag == false) {\n a.push_back(A[i]);\n }\n }\n if (a.size() == 0) {\n cout << 0 << endl;\n } else {\n map<long long, int> add;\n dfs2(0, a[0], W, M, add);\n for (int i = 1; i < a.size(); i++) {\n set<long long> addset;\n dfs3(0, a[i], W, M, addset);\n for (long long x : addset) {\n if (add.find(x) != add.end()) {\n add[x]++;\n }\n }\n }\n for (auto x : add) {\n if (x.second == a.size()) {\n cout << x.first << endl;\n return;\n }\n }\n cout << -1 << endl;\n }\n}\n\nint main() {\n while (1) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6560, "memory_kb": 10112, "score_of_the_acc": -0.8498, "final_rank": 11 }, { "submission_id": "aoj_1634_10555381", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n break;\n }\n\n vector<int> A(N), W(M);\n for (int i = 0; i < N; i++) {\n cin >> A[i];\n }\n for (int i = 0; i < M; i++) {\n cin >> W[i];\n }\n\n vector<vector<int>> weight(1 << M);\n for (int bit = 0; bit < 1 << M; bit++) {\n vector<int> S;\n for (int i = 0; i < M; i++) {\n if (!(bit & (1 << i))) {\n S.push_back(W[i]);\n }\n }\n\n for (int s = 0; s < 1 << S.size(); s++) {\n int wei = 0;\n for (int i = 0; i < S.size(); i++) {\n if (s & (1 << i)) {\n wei += S[i];\n }\n }\n weight[bit].push_back(wei);\n }\n weight[bit].erase(unique(weight[bit].begin(), weight[bit].end()), weight[bit].end());\n ranges::sort(weight[bit]);\n }\n\n map<int, int> cnt_mp;\n int ng_cnt = 0;\n for (int i = 0; i < N; i++) {\n bool ok = false; // 計測できるか\n vector<int> add; // 計測できない場合、どの重さを追加すればいいか\n\n for (int bit = 0; bit < (1 << M); bit++) {\n int right_w = 0;\n for (int j = 0; j < M; j++) {\n if (bit & (1 << j)) {\n right_w += W[j];\n }\n }\n\n for (int weight_i : weight[bit]) {\n if (right_w == (A[i] + weight_i)) {\n ok = true;\n } else if (right_w > (A[i] + weight_i)) {\n add.push_back(right_w - (A[i] + weight_i));\n } else {\n add.push_back((A[i] + weight_i) - right_w);\n }\n }\n }\n\n if (!ok) {\n ng_cnt++;\n ranges::sort(add);\n add.erase(unique(add.begin(), add.end()), add.end());\n for (int vi : add) {\n cnt_mp[vi]++;\n }\n }\n }\n\n if (ng_cnt == 0) {\n cout << 0 << endl;\n } else {\n int ans = -1;\n for (auto [wei, cnt] : cnt_mp) {\n if (cnt == ng_cnt) {\n ans = wei;\n break;\n }\n }\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 5150, "memory_kb": 113536, "score_of_the_acc": -1.0875, "final_rank": 15 }, { "submission_id": "aoj_1634_10555368", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nconstexpr int INF = 2e9;\n\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n break;\n }\n\n vector<int> A(N), W(M);\n for (int i = 0; i < N; i++) {\n cin >> A[i];\n }\n for (int i = 0; i < M; i++) {\n cin >> W[i];\n }\n\n vector<vector<int>> weight(1 << M);\n for (int bit = 0; bit < 1 << M; bit++) {\n vector<int> S;\n for (int i = 0; i < M; i++) {\n if (!(bit & (1 << i))) {\n S.push_back(W[i]);\n }\n }\n\n for (int s = 0; s < 1 << S.size(); s++) {\n int wei = 0;\n for (int i = 0; i < S.size(); i++) {\n if (s & (1 << i)) {\n wei += S[i];\n }\n }\n weight[bit].push_back(wei);\n }\n weight[bit].erase(unique(weight[bit].begin(), weight[bit].end()), weight[bit].end());\n ranges::sort(weight[bit]);\n }\n\n map<int, int> cnt_mp;\n int ng_cnt = 0;\n for (int i = 0; i < N; i++) {\n bool ok = false; // 計測できるか\n vector<int> add; // 計測できない場合、どの重さを追加すればいいか\n\n for (int bit = 0; bit < (1 << M); bit++) {\n int right_w = 0;\n for (int j = 0; j < M; j++) {\n if (bit & (1 << j)) {\n right_w += W[j];\n }\n }\n\n for (int weight_i : weight[bit]) {\n if (right_w == (A[i] + weight_i)) {\n // printf(\"ok:(%d,%d,%d)\\n\", right_w, A[i], weight_i);\n ok = true;\n } else if (right_w > (A[i] + weight_i)) {\n // printf(\"%d - (%d + %d) = %d\\n\", right_w, A[i], weight_i, right_w - (A[i] + weight_i));\n add.push_back(right_w - (A[i] + weight_i));\n } else {\n // printf(\"(%d + %d) - %d = %d\\n\", A[i], weight_i, right_w, (A[i] + weight_i) - right_w);\n add.push_back((A[i] + weight_i) - right_w);\n }\n }\n }\n\n // cout << \"i=\" << i << endl;\n // cout << \"A[i]=\" << A[i] << endl;\n if (!ok) {\n ng_cnt++;\n // cout << \"ng\" << endl;\n\n ranges::sort(add);\n add.erase(unique(add.begin(), add.end()), add.end());\n for (int vi : add) {\n // cout << vi << \" \";\n cnt_mp[vi]++;\n }\n // cout << endl;\n }\n // cout << \"--\" << endl;\n }\n\n\n if (ng_cnt == 0) {\n cout << 0 << endl;\n } else {\n // cout << \"ng_cnt: \" << ng_cnt << endl;\n int ans = -1;\n for (auto [wei, cnt] : cnt_mp) {\n // cout << wei << \" \" << cnt << endl;\n if (cnt == ng_cnt) {\n ans = wei;\n break;\n }\n }\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 5140, "memory_kb": 113536, "score_of_the_acc": -1.0862, "final_rank": 14 }, { "submission_id": "aoj_1634_10550117", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include <atcoder/all>\nusing namespace atcoder;\n\nusing ll = long long;\nusing mint = modint998244353;\n\nint main() {\n while(true) {\n int N, M;\n cin >> N >> M;\n if(N == 0 && M == 0) break;\n vector<ll> A(N), W(M);\n for(int i = 0; i < N; ++i) {\n cin >> A[i];\n }\n for(int i = 0; i < M; ++i) {\n cin >> W[i];\n }\n vector<bool> ok(N);\n unordered_set<ll> st;\n vector cand(N, unordered_set<ll>());\n for(int s = 0; s < 1 << M; ++s) {\n for(int t = (1 << M); t > 0; ) {\n t = (t - 1) & s;\n ll a = 0, b = 0;\n for(int i = 0; i < M; ++i) {\n if((t >> i) & 1) a += W[i];\n else if((s >> i) & 1) b += W[i];\n }\n st.insert(abs(a - b));\n }\n }\n for(int i = 0; i < N; i++) {\n for(ll d : st) {\n cand[i].insert(d + A[i]);\n cand[i].insert(abs(d - A[i]));\n }\n }\n vector<int> lack;\n for(int i = 0; i < N; ++i) {\n if(!cand[i].count(0)) {\n lack.push_back(i);\n // cout << i + 1 << endl;\n // cout << cand[i].count(1) << endl;\n }\n }\n if(lack.empty()) {\n cout << 0 << endl;\n continue;\n }\n ll INF = 1LL << 60;\n ll ans = 1LL << 60;\n for(ll x : cand[lack[0]]) {\n // cout << x << endl;\n bool ok = true;\n for(int i : lack) {\n if(!cand[i].count(x)) ok = false;\n if(!ok) break;\n }\n if(ok) ans = min(ans, x);\n }\n cout << (ans == INF ? -1 : ans) << endl;\n }\n}", "accuracy": 1, "time_ms": 3350, "memory_kb": 228000, "score_of_the_acc": -1.3199, "final_rank": 16 }, { "submission_id": "aoj_1634_10521802", "code_snippet": "#line 1 \"C.cpp\"\n#include <iostream>\n#include <vector>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 1000001000;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\n#line 1 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n\n\n\n#ifdef LOCAL\n#define dbg(x) std::cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << std::endl\n#else\n#define dbg(x) true\n#endif\n\n#line 12 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n#include <utility>\n#include <set>\n\n\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {\n os << '(' << p.first << \", \" << p.second << ')';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << '[';\n for (int i=0; i<int(vec.size()); i++) {\n os << vec[i];\n if (i != int(vec.size())-1) os << \", \";\n }\n os << ']';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& st) {\n os << '{';\n auto itr = st.begin();\n while (itr != st.end()) {\n os << *itr;\n itr = next(itr);\n if (itr != st.end()) os << \", \";\n }\n os << '}';\n return os;\n}\n\n\n\n#line 14 \"C.cpp\"\n\nvoid solve(int N, int M) {\n // 1 <= N <= 100\n // 1 <= M <= 10\n\n vector<int>A(N);\n for (int i=0; i<N; i++) cin >> A[i];\n vector<int>W(M); for (int i=0; i<M; i++) cin >> W[i];\n\n\n set<ll>V; // 計れる重さの集合\n V.insert(0);\n for (int w : W) {\n set<ll> V2;\n for (ll v: V) {\n V2.insert(v+w);\n V2.insert(v-w);\n }\n for (ll v: V2) V.insert(v);\n }\n\n vector<ll> A2;\n for (int a: A) {\n if (!V.contains(a))A2.push_back(a);\n }\n\n if (A2.empty()) {\n cout << 0 << endl;\n return;\n }\n\n set<ll> Beta;\n {\n int a = A2[0];\n for (ll v: V) {\n Beta.insert(a-v);\n Beta.insert(v-a);\n }\n }\n\n\n for (int a : A2) {\n set<ll> Alphas;\n for (ll v: V) {\n Alphas.insert(a-v);\n Alphas.insert(v-a);\n }\n // Beta *= Alpha\n // \\foarll b \\in Beta, b \\in Alpha\n vector<ll> Gamma; // { x | x \\in Beta, x \\notin \n for (ll b: Beta) if (!Alphas.contains(b)) Gamma.push_back(b);\n for(ll g: Gamma) Beta.erase(g);\n }\n\n auto itr = Beta.lower_bound(0);\n if (itr != Beta.end()) {\n cout << *itr << endl;\n }\n else cout << -1 << endl;\n\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n while (true) {\n int n, m; cin >> n >> m;\n if (n == 0 && m == 0) break;\n solve(n,m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7470, "memory_kb": 19204, "score_of_the_acc": -1.0033, "final_rank": 13 }, { "submission_id": "aoj_1634_10505909", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> bool chmin(T& a, T b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool chmax(T& a, T b) { return a < b ? a = b, true : false; }\ntemplate<typename T>\nconcept Iterable = requires(T t) { std::begin(t); std::end(t); };\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nostream& operator<<(ostream& os, const T& container) { for (auto& element : container) os << element << ' '; return os; }\ntemplate<typename R>\n\trequires ranges::range<R> && (!is_same_v<decay_t<R>, string>) && (!is_same_v<decay_t<R>, const char*>)\nostream& operator<<(ostream& os, R&& range) { for (auto& element : range)os << element << ' '; return os; }\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nistream& operator>>(std::istream& is, T& container) { for (auto& e : container)is >> e; return is; }\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\ntemplate<class T = ll> struct Edge {\n\tint to;\n\tT weight;\n\tbool operator==(Edge e) { return this->to == e.to and this->weight == e.weight; }\n\tbool operator<(Edge e) { return this->to == e.to ? this->weight < e.weight : this->to < e.to; }\n};\n#ifdef _DEBUG\n#define SHOW(n) {const auto& _ret = n; cerr << #n << \": \" << _ret << endl;}\n#define MSG(x) cerr << x << endl;\n#else\n#define SHOW(n)\n#define MSG(x)\n#endif\n\n//AtCoder Library \n//#include <atcoder/all>\n//using namespace atcoder;\n//using mint = modint998244353;\n////using mint = modint1000000007;\n////using mint1 = dynamic_modint<0>;\n////using mint = modint;\n////mint::set_mod();\n//istream& operator>>(istream& is, mint& x) { ll r; is >> r; x = r; return is; }\n//ostream& operator<<(ostream& os, mint& x) { os << x.val(); return os; }\n\n//boost\n//#include <boost/multiprecision/cpp_int.hpp>\n//using namespace boost::multiprecision;\n//using l3 = int128_t;\n\n\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile(true) {\n\t\tint n, m;\n\t\tcin >> n >> m;\n\t\tif (n == 0 and m == 0)return 0;\n\n\t\tvector<ll> a(n), w(m);\n\t\tcin >> a >> w;\n\n\t\tset<ll> b;\n\t\tauto dfs = [&](auto&& f, int i, ll v)->void {\n\t\t\tif (i == m) {\n\t\t\t\tif (v >= 0)b.insert(v);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tf(f, i + 1, v);\n\t\t\tf(f, i + 1, v + w[i]);\n\t\t\tf(f, i + 1, v - w[i]);\n\t\t\t};\n\n\t\tdfs(dfs, 0, 0);\n\n\t\tvector<ll> memo;\n\t\tfor (int i = 0; i < a.size(); ++i) {\n\t\t\tif (not b.contains(a[i])) {\n\t\t\t\tmemo.push_back(i);\n\t\t\t}\n\t\t}\n\n\t\tif (memo.size() == 0) {\n\t\t\tcout << 0 << endl;\n\t\t\tcontinue;\n\t\t}\n\n\t\tll v = a[memo[0]];\n\n\t\t//cout << \"b:\";\n\t\t//for (auto x : b) {\n\t\t//\tcout << x << \" \";\n\t\t//}\n\t\t//cout << endl;\n\n\t\t//SHOW(v);\n\n\t\tvector<ll> res;\n\t\tfor (auto x : b) {\n\t\t\tll d = abs(x - v);\n\t\t\tbool ok = true;\n\t\t\tfor (int i = 0; i < a.size(); ++i) {\n\t\t\t\tif (!b.contains(a[i]) and !b.contains(abs(a[i] - d)) and !b.contains(a[i] + d)) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (ok)res.push_back(d);\n\n\t\t\td = x + v;\n\t\t\tok = true;\n\t\t\tfor (int i = 0; i < a.size(); ++i) {\n\t\t\t\t//SHOW(a[i]);\n\t\t\t\tif (!b.contains(a[i]) and !b.contains(abs(a[i] - d)) and !b.contains(a[i] + d)) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\n\t\t\tif (ok)res.push_back(d);\n\t\t}\n\n\t\tif (res.size() == 0)cout << -1 << endl;\n\t\telse cout << ranges::min(res) << endl;\n\n\t}\n\n\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 5376, "score_of_the_acc": -0.0294, "final_rank": 2 } ]
aoj_1636_cpp
Cube Surface Puzzle Given a set of six pieces, “Cube Surface Puzzle” is to construct a hollow cube with filled surface. Pieces of a puzzle is made of a number of small unit cubes laid grid-aligned on a plane. For a puzzle constructing a cube of its side length n , unit cubes are on either of the following two areas. Core (blue): A square area with its side length n −2. Unit cubes fill up this area. Fringe (red): The area of width 1 unit forming the outer fringe of the core. Each unit square in this area may be empty or with a unit cube on it. Each piece is connected with faces of its unit cubes. Pieces can be arbitrarily rotated and either side of the pieces can be inside or outside of the constructed cube. The unit cubes on the core area should come in the centers of the faces of the constructed cube. Consider that we have six pieces in Fig. E-1 (The first dataset of Sample Input). Then, we can construct a cube as shown in Fig. E-2. Fig. E-1 Pieces from the first dataset of Sample Input Fig. E-2 Constructing a cube Mr. Hadrian Hex has collected a number of cube surface puzzles. One day, those pieces were mixed together and he cannot find yet from which six pieces he can construct a cube. Your task is to write a program to help Mr. Hex, which judges whether we can construct a cube for a given set of pieces. Input The input consists of at most 200 datasets, each in the following format. n x 1,1 x 1,2 … x 1, n x 2,1 x 2,2 … x 2, n … x 6 n ,1 x 6 n ,2 … x 6 n , n The first line contains an integer n denoting the length of one side of the cube to be constructed (3 ≤ n ≤ 9, n is odd). The following 6 n lines give the six pieces. Each piece is described in n lines. Each of the lines corresponds to one grid row and each of the characters in the line, either ‘X’ or ‘.’, indicates whether or not a unit cube is on the corresponding unit square: ‘X’ means a unit cube is on the column and ‘.’ means none is there. The core area of each piece is centered in the data for the piece. The end of the input is indicated by a line containing a zero. Output For each dataset, output “ Yes ” if we can construct a cube, or “ No ” if we cannot. Sample Input 5 ..XX. .XXX. XXXXX XXXXX X.... ....X XXXXX .XXX. .XXX. ..... ..XXX XXXX. .XXXX .XXXX ...X. ...X. .XXXX XXXX. XXXX. .X.X. XXX.X .XXXX XXXXX .XXXX .XXXX XX... .XXXX XXXXX XXXXX XX... 5 ..XX. .XXX. XXXXX XXXX. X.... ....X XXXXX .XXX. .XXX. ..... .XXXX XXXX. .XXXX .XXXX ...X. ...X. .XXXX XXXX. XXXX. .X.X. XXX.X .XXXX XXXXX .XXXX .XXXX XX... XXXXX XXXXX .XXXX XX... 0 Output for the Sample Input Yes No
[ { "submission_id": "aoj_1636_10851082", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\n\nstruct jie{\n char s[10][10][10];\n}mp[7];\nchar a[15][15][15];\nint n;\nint flag,us[15];\nvoid dfs(int m){\n if(m>6){\n flag=1;\n return;\n }\n for(int k=1;k<=6;k++){\n if(us[k]==1) continue;\n us[k]=1;\n for(int i=1;i<=8;i++){\n if(k==1||k==3){\n int y=1,f=1;\n if(k==3) y=n;\n for(int x=1;x<=n;x++){\n for(int z=1;z<=n;z++){\n if(mp[m].s[i][x][z]=='X'&&a[x][y][z]=='X') f=0;\n }\n }\n if(f==0) continue;\n for(int x=1;x<=n;x++){\n for(int z=1;z<=n;z++){\n if(mp[m].s[i][x][z]=='X') a[x][y][z]='X';\n }\n }\n dfs(m+1);\n for(int x=1;x<=n;x++){\n for(int z=1;z<=n;z++){\n if(mp[m].s[i][x][z]=='X') a[x][y][z]='.';\n }\n }\n if(flag==1) return;\n }\n\n if(k==2||k==4){\n int x=n,f=1;\n if(k==4) x=1;\n for(int y=1;y<=n;y++){\n for(int z=1;z<=n;z++){\n if(mp[m].s[i][y][z]=='X'&&a[x][y][z]=='X') f=0;\n }\n }\n if(f==0) continue;\n for(int y=1;y<=n;y++){\n for(int z=1;z<=n;z++){\n if(mp[m].s[i][y][z]=='X') a[x][y][z]='X';\n }\n }\n dfs(m+1);\n for(int y=1;y<=n;y++){\n for(int z=1;z<=n;z++){\n if(mp[m].s[i][y][z]=='X') a[x][y][z]='.';\n }\n }\n if(flag==1) return;\n }\n\n if(k==5||k==6){\n int z=1,f=1;\n if(k==6) z=n;\n for(int x=1;x<=n;x++){\n for(int y=1;y<=n;y++){\n if(mp[m].s[i][x][y]=='X'&&a[x][y][z]=='X') f=0;\n }\n }\n if(f==0) continue;\n for(int x=1;x<=n;x++){\n for(int y=1;y<=n;y++){\n if(mp[m].s[i][x][y]=='X') a[x][y][z]='X';\n }\n }\n dfs(m+1);\n for(int x=1;x<=n;x++){\n for(int y=1;y<=n;y++){\n if(mp[m].s[i][x][y]=='X') a[x][y][z]='.';\n }\n }\n if(flag==1) return;\n }\n }\n us[k]=0;\n }\n}\nint main()\n{\n for(int i=1;i<=10;i++){\n for(int j=1;j<=10;j++){\n for(int k=1;k<=10;k++) a[i][j][k]='.';\n }\n }\n while(~scanf(\"%d\",&n)&&n){\n int sum=0;\n memset(us,0,sizeof(us));\n for(int k=1;k<=6;k++){\n for(int i=1;i<=n;i++){\n scanf(\"%s\",mp[k].s[1][i]+1);\n for(int j=1;j<=n;j++) if(mp[k].s[1][i][j]=='X') sum++;\n }\n for(int kk=2;kk<=4;kk++){\n for(int j=1;j<=n;j++){\n for(int i=1;i<=n;i++){\n mp[k].s[kk][j][i]=mp[k].s[kk-1][n-i+1][j];\n }\n }\n }\n\n for(int kk=5;kk<=8;kk++){\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n mp[k].s[kk][i][j]=mp[k].s[kk-4][i][n-j+1];\n }\n }\n }\n }\n flag=0;\n //printf(\"%d\\n\",sum);\n if(sum==n*n*2+(n-2)*n*2+(n-2)*(n-2)*2){\n dfs(1);\n }\n if(flag==0) printf(\"No\\n\");\n else printf(\"Yes\\n\");\n }\n return 0;\n}\n/*\n3\n.X.\nXXX\n.X.\n.X.\nXXX\n.X.\n...\n.X.\n...\n...\n.X.\n...\nXXX\n.X.\nXXX\nXXX\n.X.\nXXX\n\n*/", "accuracy": 1, "time_ms": 290, "memory_kb": 3396, "score_of_the_acc": -0.3661, "final_rank": 4 }, { "submission_id": "aoj_1636_10669311", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\n\n// #include \"titan_cpplib/others/print.cpp\"\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n\n using S = vector<vector<int>>;\n vector<S> X(6);\n rep(i, 6) {\n S x(n, vector<int>(n, 0));\n rep(j, n) {\n string s; cin >> s;\n rep(k, n) {\n x[j][k] = s[k] == 'X' ? 1 : 0;\n }\n }\n X[i] = x;\n }\n\n int cnt = 0;\n rep(idx, 6) {\n set<pair<int, int>> seen;\n rep(i, n) rep(j, n) if (i == 0 || j == 0 || i == n-1 || j == n-1) {\n seen.insert({i, j});\n }\n for (auto [i, j] : seen) {\n if (X[idx][i][j]) cnt++;\n }\n }\n if (cnt != n*12-2*8) {\n cout << \"No\" << endl;\n return true;\n }\n\n vector<vector<vector<int>>> G(n, vector<vector<int>>(n, vector<int>(n, 0)));\n\n auto rotate = [&] (S &R) -> void {\n auto P = R;\n rep(i, n) R[i][n-1] = P[0][i];\n rep(i, n) R[n-1][n-1-i] = P[i][n-1];\n rep(i, n) R[i][0] = P[n-1][i];\n rep(i, n) R[0][n-1-i] = P[i][0];\n };\n\n auto flip = [&] (S &R) -> void {\n rep(j, n/2) swap(R[0][j], R[0][n-1-j]);\n rep(j, n/2) swap(R[n-1][j], R[n-1][n-1-j]);\n for (int i = 1; i < n-1; ++i) swap(R[i][0], R[i][n-1]);\n };\n\n vector<vector<tuple<int, int, int>>> pos(6);\n rep(i, n) rep(j, n) {\n if (i == 0 || j == 0 || i == n-1 || j == n-1) {\n pos[0].push_back({0, i, j});\n pos[1].push_back({n-1, i, j});\n pos[2].push_back({i, 0, j});\n pos[3].push_back({i, n-1, j});\n pos[4].push_back({i, j, 0});\n pos[5].push_back({i, j, n-1});\n }\n }\n\n vector<tuple<int, int, int>> check;\n rep(i, n) rep(j, n) rep(k, n) {\n int x = i == 0 || i == n-1;\n int y = j == 0 || j == n-1;\n int z = k == 0 || k == n-1;\n if (x+y+z >= 2) {\n check.push_back({i, j, k});\n }\n }\n\n auto dfs = [&] (auto &&dfs, int idx, int seen) -> bool {\n // idx番目の正方形を割り当てる\n\n if (idx == 6) {\n for (const auto &[x, y, z] : check) {\n if (G[x][y][z] != 1) {\n return false;\n }\n }\n return true;\n }\n\n for (int i = 1; i < 6; ++i) if ((seen >> i & 1) == 0) {\n // 面iに割り当てる\n int nxt = seen | (1 << i);\n rep(j, 4) {\n rotate(X[idx]);\n rep(k, 2) {\n flip(X[idx]);\n\n bool ok = true;\n for (const auto &[x, y, z] : pos[i]) {\n if (i == 0 || i == 1) G[x][y][z] += X[idx][y][z];\n else if (i == 2 || i == 3) G[x][y][z] += X[idx][x][z];\n else if (i == 4 || i == 5) G[x][y][z] += X[idx][x][y];\n ok &= G[x][y][z] <= 1;\n }\n\n if (ok && dfs(dfs, idx+1, nxt)) {\n return true;\n }\n\n for (const auto &[x, y, z] : pos[i]) {\n if (i == 0 || i == 1) G[x][y][z] -= X[idx][y][z];\n else if (i == 2 || i == 3) G[x][y][z] -= X[idx][x][z];\n else if (i == 4 || i == 5) G[x][y][z] -= X[idx][x][y];\n }\n }\n }\n }\n\n return false;\n };\n\n for (const auto &[x, y, z] : pos[0]) {\n G[x][y][z] += X[0][y][z];\n }\n\n if (dfs(dfs, 1, 1)) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n\n return true;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(15);\n while (true) {\n if (!solve()) {\n break;\n }\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3712, "score_of_the_acc": -1.0074, "final_rank": 18 }, { "submission_id": "aoj_1636_10668432", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\n\n// #include \"titan_cpplib/others/print.cpp\"\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n\n using S = vector<vector<int>>;\n vector<S> X(6);\n rep(i, 6) {\n S x(n, vector<int>(n, 0));\n rep(j, n) {\n string s; cin >> s;\n rep(k, n) {\n x[j][k] = s[k] == 'X' ? 1 : 0;\n }\n }\n X[i] = x;\n }\n\n vector<vector<vector<int>>> G(n, vector<vector<int>>(n, vector<int>(n, 0)));\n\n auto rotate = [&] (S &R) -> void {\n auto P = R;\n rep(i, n) R[i][n-1] = P[0][i];\n rep(i, n) R[n-1][n-1-i] = P[i][n-1];\n rep(i, n) R[i][0] = P[n-1][i];\n rep(i, n) R[0][n-1-i] = P[i][0];\n };\n\n auto flip = [&] (S &R) -> void {\n rep(j, n/2) swap(R[0][j], R[0][n-1-j]);\n rep(j, n/2) swap(R[n-1][j], R[n-1][n-1-j]);\n for (int i = 1; i < n-1; ++i) swap(R[i][0], R[i][n-1]);\n };\n\n vector<vector<tuple<int, int, int>>> pos(6);\n rep(i, n) rep(j, n) {\n if (i == 0 || j == 0 || i == n-1 || j == n-1) {\n pos[0].push_back({0, i, j});\n pos[1].push_back({n-1, i, j});\n pos[2].push_back({i, 0, j});\n pos[3].push_back({i, n-1, j});\n pos[4].push_back({i, j, 0});\n pos[5].push_back({i, j, n-1});\n }\n }\n\n vector<tuple<int, int, int>> check;\n rep(i, n) rep(j, n) rep(k, n) {\n int x = i == 0 || i == n-1;\n int y = j == 0 || j == n-1;\n int z = k == 0 || k == n-1;\n if (x+y+z >= 2) {\n check.push_back({i, j, k});\n }\n }\n\n auto dfs = [&] (auto &&dfs, int idx, int seen) -> bool {\n // idx番目の正方形を割り当てる\n\n if (idx == 6) {\n for (auto [x, y, z] : check) {\n if (G[x][y][z] != 1) {\n return false;\n }\n }\n return true;\n }\n\n for (int i = 1; i < 6; ++i) if ((seen >> i & 1) == 0) {\n // 面iに割り当てる\n int nxt = seen | (1 << i);\n rep(j, 4) {\n rotate(X[idx]);\n rep(k, 2) {\n flip(X[idx]);\n\n bool ok = true;\n for (auto [x, y, z] : pos[i]) {\n if (i == 0 || i == 1) G[x][y][z] += X[idx][y][z];\n else if (i == 2 || i == 3) G[x][y][z] += X[idx][x][z];\n else if (i == 4 || i == 5) G[x][y][z] += X[idx][x][y];\n ok &= G[x][y][z] <= 1;\n }\n\n if (ok && dfs(dfs, idx+1, nxt)) {\n return true;\n }\n\n for (auto [x, y, z] : pos[i]) {\n if (i == 0 || i == 1) G[x][y][z] -= X[idx][y][z];\n else if (i == 2 || i == 3) G[x][y][z] -= X[idx][x][z];\n else if (i == 4 || i == 5) G[x][y][z] -= X[idx][x][y];\n }\n }\n }\n }\n\n return false;\n };\n\n for (auto [x, y, z] : pos[0]) {\n G[x][y][z] += X[0][y][z];\n }\n\n if (dfs(dfs, 1, 1)) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n\n return true;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(15);\n while (true) {\n if (!solve()) {\n break;\n }\n }\n}", "accuracy": 1, "time_ms": 740, "memory_kb": 3584, "score_of_the_acc": -0.8342, "final_rank": 16 }, { "submission_id": "aoj_1636_10593721", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nvoid cw(vector<string>&s){\n auto t=s;\n rep(i,0,sz(s)){\n rep(j,0,sz(s[0])){\n s[i][j]=t[sz(s)-1-j][i]; \n }\n }\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n\n vector S(6,vector<string>(n,\"\"));\n rep(i,0,6){\n rep(j,0,n)cin>>S[i][j];\n }\n vector cnt(n,vector(n,vi(n,0)));\n //3次元配列にピース数を足す\n auto apply=[&](const vector<string>&s,int bx,int by,int bz,int d,int c){\n bool f=0;\n int x,y,z;\n x=y=z=0;\n //d 0 xbase 1 ybase 2 zbase\n rep(i,0,n){\n if(d==0){x=bx,y=i, z=0;}\n if(d==1){x=0, y=by,z=i;}\n if(d==2){x=0, y=i, z=bz;}\n if(s[i][0]=='X'){\n cnt[x][y][z]+=c;\n if(cnt[x][y][z]>1)f=1;\n }\n if(d==0){z=n-1;}\n if(d==1){x=n-1;}\n if(d==2){x=n-1;}\n if(s[i][n-1]=='X'){\n cnt[x][y][z]+=c;\n if(cnt[x][y][z]>1)f=1;\n } \n }\n rep(j,1,n-1){\n if(d==0){x=bx,y=0, z=j;}\n if(d==1){x=j, y=by,z=0;}\n if(d==2){x=j, y=0, z=bz;}\n if(s[0][j]=='X'){\n cnt[x][y][z]+=c;\n if(cnt[x][y][z]>1)f=1;\n }\n if(d==0){y=n-1;}\n if(d==1){z=n-1;}\n if(d==2){y=n-1;}\n if(s[n-1][j]=='X'){\n cnt[x][y][z]+=c;\n if(cnt[x][y][z]>1)f=1;\n }\n }\n return f;\n };\n vi used(6);\n apply(S[0],0,-1,-1,0,1);\n used[0]=1;\n \n auto dfs=[&](auto dfs,int i){\n if(i==6) {\n int csum=0;\n rep(i,0,n)rep(j,0,n)rep(k,0,n){\n csum+=cnt[i][j][k];\n }\n if(csum==n*n*n-(n-2)*(n-2)*(n-2)-6*(n-2)*(n-2))return 1;\n return 0;\n }\n int bx,by,bz,d;\n bx=by=bz=d=0;\n if(i==1)bx=n-1,d=0;\n if(i==2)by=0,d=1;\n if(i==3)by=n-1,d=1;\n if(i==4)bz=0,d=2;\n if(i==5)bz=n-1,d=2;\n rep(j,1,6){\n auto T=S[j];\n if(!used[j]){\n used[j]=1;\n rep(k,0,2){\n rep(l,0,4){\n if(!apply(T,bx,by,bz,d,1)){\n if(dfs(dfs,i+1))return 1;\n }\n apply(T,bx,by,bz,d,-1);\n //回転\n cw(T);\n }\n \n // //裏返す\n reverse(all(T));\n }\n used[j]=0;\n }\n }\n return 0;\n };\n if(dfs(dfs,1)){\n cout<<\"Yes\"<<endl;\n }else{\n cout<<\"No\"<<endl;\n }\n return 1;\n}", "accuracy": 1, "time_ms": 990, "memory_kb": 3456, "score_of_the_acc": -0.5975, "final_rank": 12 }, { "submission_id": "aoj_1636_10589725", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\n#define rep2(i, m, n) for (int i = (m); i < (n); ++i)\n#define rep(i, n) rep2(i, 0, n)\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define INF 1001001001001001001ll\n#define inf (int)1001001000\n\nll gcd(ll x, ll y) {\tif (x == 0) return y;\treturn gcd(y%x, x);} \nll lcm(ll x, ll y) { __int128_t xx,yy; xx=x; yy=y; __int128_t ans=xx * yy / gcd(x, y); ll ans2=ans; return ans2; }\ntemplate<typename T>\nT POW(T x, ll n){T ret=1;\twhile(n>0){\t\tif(n&1) ret=ret*x;\t\tx=x*x;\t\tn>>=1;\t}\treturn ret;}\ntemplate<typename T>\nT modpow(T a, ll n, T p) {\tif(n==0) return (T)1; if (n == 1) return a % p; if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p; T t = modpow(a, n / 2, p); return (t * t) % p;}\ntemplate<typename T>\nT modinv(T a, T m) {\tif(m==0)return (T)1;\tT b = m, u = 1, v = 0;\twhile (b) {\t\tT t = a / b;\t\ta -= t * b; swap(a, b);\t\tu -= t * v; swap(u, v);\t}\tu %= m;\tif (u < 0) u += m;\treturn u;}\n/*\nconst int MAXCOMB=510000;\nstd::vector<mint> fac(MAXCOMB), finv(MAXCOMB), inv(MAXCOMB);\nvoid COMinit() {fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;for (int i = 2; i < MAXCOMB; i++) {fac[i] = fac[i - 1] * i;inv[i] = mint(0) - inv[mint::mod() % i] * (mint::mod() / i);finv[i] = finv[i - 1] * inv[i];}}\nmint COM(int n, int k) {if (n < k) return 0;if (n < 0 || k < 0) return 0;return fac[n] * finv[k] * finv[n - k];}\n*/\ntemplate <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false));}\ntemplate <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false));}\n\nint SOLVEFIN = 0;\n\nvoid solve(){\n int n;\n cin>>n;\n if(n==0){\n SOLVEFIN=1;\n return;\n }\n vector<vector<vector<string>>> grid(6,vector<vector<string>>(8,vector<string>(n)));\n rep(i,6){\n rep(j,n){\n cin>>grid[i][0][j];\n }\n }\n vector<vector<vector<int>>> cube(n,vector<vector<int>>(n,vector<int>(n,0)));\n rep(i,6){\n rep(j,3){\n rep(k,n){\n rep(l,n){\n grid[i][j+1][k][l]=grid[i][j][l][n-1-k];\n }\n }\n }\n }\n rep(i,6){\n rep(k,n){\n rep(l,n){\n grid[i][4][k][l]=grid[i][0][k][n-1-l];\n }\n }\n }\n rep(i,6){\n rep(j,3){\n rep(k,n){\n rep(l,n){\n grid[i][j+5][k][l]=grid[i][j+4][l][n-1-k];\n }\n }\n }\n }\n int cnt = 0;\n rep(i,6){\n rep(j,n){\n rep(k,n){\n //cout<<grid[i][0][j][k];\n if((j==0 || j==n-1 || k==0 || k==n-1) && grid[i][0][j][k]=='X')cnt++;\n }\n //cout<<endl;\n }\n }\n //cout<<cnt<<endl;\n if(cnt!=n*n*n-(n-2)*(n-2)*(n-2)-(n-2)*(n-2)*6){\n cout<<\"No\"<<endl;\n return;\n }\n\n vector<pair<int,int>> indpr;\n rep(i,n){\n rep(j,n){\n if((i==0 || i==n-1 )||(j==0 || j==n-1))indpr.push_back({i,j});\n }\n }\n\n int pos = 0;\n int maxdir = 0;\n int key = 1;\n vector<int> ord(n); //組み上げる順番\n\n auto dfs = [&](auto &&dfs, int dir) -> void{\n if(dir==6){\n pos = 1;\n return;\n }\n if(pos)return;\n // if(dir==5 && key){\n // rep(i,n){\n // rep(j,n){\n // rep(k,n){\n // cout<<cube[i][j][k];\n // }\n // cout<<endl;\n // }\n // cout<<endl;\n // }\n // key=0;\n // cout<<ord[dir]<<endl;\n // }\n\n int nowgrid = ord[dir];\n for(int j=0;j<8;j++){\n // if(dir==0 && (j!=0 && j!=4))continue;\n vector<vector<int>> allcor;\n int nowpos = 1;\n for(auto pr : indpr){\n if(grid[nowgrid][j][pr.first][pr.second]=='.')continue;\n vector<int> cor(3,0);\n if(dir==0){\n cor[0]=pr.first;\n cor[1]=pr.second;\n }\n else if(dir==1){\n cor[0] = n-1;\n cor[1]=pr.first;\n cor[2]=pr.second;\n }\n else if(dir==2){\n cor[1] = n-1;\n cor[0]=pr.first;\n cor[2]=pr.second;\n }\n else if(dir==3){\n cor[1]=pr.first;\n cor[2]=pr.second;\n }\n else if(dir==4){\n cor[0]=pr.first;\n cor[2]=pr.second;\n }else{ \n cor[2] = n-1;\n cor[0]=pr.first;\n cor[1]=pr.second;\n }\n allcor.push_back(cor);\n if(cube[cor[0]][cor[1]][cor[2]]){\n nowpos = 0;\n break;\n }\n }\n if(nowpos ==0)continue;\n for(vector<int> cor:allcor){\n cube[cor[0]][cor[1]][cor[2]]=dir+1;\n }\n dfs(dfs,dir+1);\n for(vector<int> cor:allcor){\n cube[cor[0]][cor[1]][cor[2]]=0;\n }\n }\n };\n\n ord = {0,1,2,3,4,5};\n dfs(dfs,0);\n ord = {0,1,3,2,4,5};\n dfs(dfs,0);\n ord = {0,1,3,4,2,5};\n dfs(dfs,0);\n\n ord = {0,1,2,3,5,4};\n dfs(dfs,0);\n ord = {0,1,3,2,5,4};\n dfs(dfs,0);\n ord = {0,1,2,5,3,4};\n dfs(dfs,0);\n\n ord = {0,1,2,4,5,3};\n dfs(dfs,0);\n ord = {0,1,4,2,5,3};\n dfs(dfs,0);\n ord = {0,1,2,5,4,3};\n dfs(dfs,0);\n\n ord = {0,1,3,4,5,2};\n dfs(dfs,0);\n ord = {0,1,3,5,4,2};\n dfs(dfs,0);\n ord = {0,1,5,3,4,2};\n dfs(dfs,0);\n\n ord = {0,5,2,3,4,1};\n dfs(dfs,0);\n ord = {0,5,3,2,4,1};\n dfs(dfs,0);\n ord = {0,5,2,4,3,1};\n dfs(dfs,0);\n //cout<<maxdir<<endl;\n\n\n\n cout<< (pos? \"Yes\" : \"No\")<<endl;\n\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\twhile(SOLVEFIN == 0) solve();\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3328, "score_of_the_acc": -0.2119, "final_rank": 2 }, { "submission_id": "aoj_1636_10522551", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\n// const ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n\t\nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nvoid solve(ll n){\n\tvector<vvll> g(6,vvll(n,vll(n)));\n\trep(i,6){\n\t\trep(j,n){\n\t\t\tStr(s);\n\t\t\trep(k,n)if(s[k] == 'X')g[i][j][k] = 1;\n\t\t}\n\t}\n\t\n\tvector ng(6,vector(4,vector(2,vector(n,vector<ll>(n)))));\n\t\n\tauto rot = [&](vvll &now){\n\t\tvvll ret(n,vll(n));\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\tret[j][n-1-i] = now[i][j];\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t};\n\tauto rev = [&](vvll &now){\n\t\tvvll ret(n,vll(n));\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\tret[i][n-1-j] = now[i][j];\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t};\n\trep(i,6){\n\t\tvvll now = g[i];\n\t\trep(j,4){\n\t\t\tng[i][j][0] = now;\n\t\t\tnow = rot(now);\n\t\t}\n\t\tnow = rev(now);\n\t\trep(j,4){\n\t\t\tng[i][j][1] = now;\n\t\t\tnow = rot(now);\n\t\t}\n\t}\n\n\t// //dbg\n\t// rep(i,4){\n\t// \trep(j,2){\n\t// \t\tprint(i,j);\n\t// \t\trep(k,n){\n\t// \t\t\trep(l,n){\n\t// \t\t\t\tcout << ng[0][i][j][k][l];\n\t// \t\t\t}\n\t// \t\t\tcout << endl;\n\t// \t\t}\n\t// \t}\n\t// }\n\t\n\tvll ord(5);\n\tiota(all(ord),0);\n\t//固定面\n\t\n\tbool answer = false;\n\n\n\t//面情報\n\n\tvector mid(6,vpll(4));\n\n\tmid[0] = {{0,1},{1,5},{4,5},{0,4}};\n\tmid[1] = {{3,2},{2,1},{0,1},{3,0}};\n\tmid[2] = {{1,2},{2,6},{5,6},{1,5}};\n\tmid[3] = {{6,7},{7,4},{5,4},{6,5}};\n\tmid[4] = {{3,0},{0,4},{7,4},{3,7}};\n\tmid[5] = {{2,3},{3,7},{6,7},{2,6}};\n\t\n\n\tmap<pll,vll> mp;\n\trep(i,6){\n\t\tfor(auto [u,v]:mid[i]){\n\t\t\tmp[{min(u,v),max(u,v)}] = vll(n);\n\t\t}\n\t}\n\tvll corner(8);\n\t\t\n\tll checknum = 0;\n\n\tvll rotstate(6);\n\n\tvll revstate(6);\n\n\tauto check = [&](){\n\t\t// cerr << rotstate << endl;\n\t\t// cerr << revstate << endl;\n\t\t// cerr << ord << endl;\n\t\tchecknum++;\n\t\teach(p,mp){\n\t\t\treps(i,1,n-1){\n\t\t\t\tif(p.second[i] == 0)return ;\n\t\t\t}\n\t\t}\n\t\teach(p,corner){\n\t\t\tif(p == 0)return ;\n\t\t}\n\t\tanswer = true;\n\t};\n\n\tauto puts = [&](ll sid, ll id,ll ro ,ll re) -> bool {\n\t\t//置く\n\t\t//おけないなら戻す \n\t\t//おけたらtrue\n\n\t\t// おけるか\n\t\tvll l(4),r(4),rv(4);\n\t\trep(i,4){\n\t\t\tl[i] = mid[sid][i].first;\n\t\t\tr[i] = mid[sid][i].second;\n\t\t\trv[i] = l[i] < r[i];\n\t\t}\n\t\tvpll pr(4);\n\t\trep(i,4){\n\t\t\tpr[i] ={min(l[i],r[i]),max(l[i],r[i])};\n\t\t}\n\n\t\treps(i,1,n-1){\n\t\t\t//上\n\t\t\tif(ng[id][ro][re][0][i] && mp[pr[0]][rv[0]?i:n-1-i]) return false;\n\t\t\t//右\n\t\t\tif(ng[id][ro][re][i][n-1] && mp[pr[1]][rv[1]?i:n-1-i]) return false;\n\t\t\t//下\n\t\t\tif(ng[id][ro][re][n-1][i] && mp[pr[2]][rv[2]?i:n-1-i])return false;\n\t\t\t//左\n\t\t\tif(ng[id][ro][re][i][0] && mp[pr[3]][rv[3]?i:n-1-i])return false;\n\t\t}\n\t\t//角\n\t\tif(ng[id][ro][re][0][0] && corner[l[0]])return false;//左上\n\t\tif(ng[id][ro][re][0][n-1] && corner[l[1]])return false;//右上\n\t\tif(ng[id][ro][re][n-1][n-1] && corner[r[2]])return false;//右下\n\t\tif(ng[id][ro][re][n-1][0] && corner[r[3]])return false;//左下\n\t\t//置く\n\t\treps(i,1,n-1){\n\t\t\t//上\n\t\t\tif(ng[id][ro][re][0][i]) mp[pr[0]][rv[0]?i:n-1-i] = 1;\n\t\t\t//右\n\t\t\tif(ng[id][ro][re][i][n-1]) mp[pr[1]][rv[1]?i:n-1-i] = 1;\n\t\t\t//下\n\t\t\tif(ng[id][ro][re][n-1][i]) mp[pr[2]][rv[2]?i:n-1-i] = 1;\n\t\t\t//左\n\t\t\tif(ng[id][ro][re][i][0]) mp[pr[3]][rv[3]?i:n-1-i] = 1;\n\t\t}\n\t\tif(ng[id][ro][re][0][0])corner[l[0]] = 1;//左上\n\t\tif(ng[id][ro][re][0][n-1])corner[l[1]] = 1;//右上\n\t\tif(ng[id][ro][re][n-1][n-1])corner[r[2]] = 1;//右下\n\t\tif(ng[id][ro][re][n-1][0])corner[r[3]] = 1;//左下\n\n\t\treturn true;\n\t};\n\n\tauto erase = [&](ll sid,ll id ,ll ro,ll re){\n\t\t//消す\n\t\t//置けている前提\n\t\t//消す\n\t\t// おけるか\n\t\tvll l(4),r(4),rv(4);\n\t\trep(i,4){\n\t\t\tl[i] = mid[sid][i].first;\n\t\t\tr[i] = mid[sid][i].second;\n\t\t\trv[i] = l[i] < r[i];\n\t\t}\n\t\tvpll pr(4);\n\t\trep(i,4){\n\t\t\tpr[i] ={min(l[i],r[i]),max(l[i],r[i])};\n\t\t}\n\t\t//消す\n\t\treps(i,1,n-1){\n\t\t\t//上\n\t\t\tif(ng[id][ro][re][0][i]){assert(mp[pr[0]][rv[0]?i:n-1-i]);mp[pr[0]][rv[0]?i:n-1-i] = 0;} \n\t\t\t//右\n\t\t\tif(ng[id][ro][re][i][n-1]){ assert(mp[pr[1]][rv[1]?i:n-1-i]); mp[pr[1]][rv[1]?i:n-1-i] = 0;}\n\t\t\t//下\n\t\t\tif(ng[id][ro][re][n-1][i]){assert(mp[pr[2]][rv[2]?i:n-1-i]); mp[pr[2]][rv[2]?i:n-1-i] = 0;}\n\t\t\t//左\n\t\t\tif(ng[id][ro][re][i][0]){assert(mp[pr[3]][rv[3]?i:n-1-i]); mp[pr[3]][rv[3]?i:n-1-i] = 0;}\n\t\t}\n\t\tif(ng[id][ro][re][0][0]){assert(corner[l[0]]); corner[l[0]] = 0;}//左上\n\t\tif(ng[id][ro][re][0][n-1]){assert(corner[l[1]]);corner[l[1]] = 0;}//右上\n\t\tif(ng[id][ro][re][n-1][n-1]){assert(corner[r[2]]); corner[r[2]] = 0;}//右下\n\t\tif(ng[id][ro][re][n-1][0]){assert(corner[r[3]]); corner[r[3]] = 0;}//左下\n\t};\n\n\n\tauto dfs = [&](auto dfs, ll v)->void{\n\t\tif(answer) return ;\n\t\tif(v == 4){\n\t\t\t// cerr << \"here\" << endl;\n\t\t\trep(i,4){\n\t\t\t\trep(j,2){\n\t\t\t\t\trotstate[ord[v]] = i;\n\t\t\t\t\trevstate[ord[v]] = j;\n\t\t\t\t\t//置く\n\t\t\t\t\tbool ret = puts(v,ord[v],i,j);\n\t\t\t\t\tif(!ret)continue;\n\t\t\t\t\t//判定\n\t\t\t\t\tcheck();\n\t\t\t\t\t//外す\n\t\t\t\t\terase(v,ord[v],i,j);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ;\n\t\t}\n\t\trep(i,4){\n\t\t\trep(j,2){\n\t\t\t\trotstate[ord[v]] = i;\n\t\t\t\trevstate[ord[v]] = j;\n\t\t\t\t//置く\n\t\t\t\tbool ret = puts(v,ord[v],i,j);\n\t\t\t\tif(!ret){continue;}\n\t\t\t\t//次\n\t\t\t\tdfs(dfs,v+1);\n\t\t\t\t//外す\n\t\t\t\terase(v,ord[v],i,j);\n\t\t\t}\n\t\t}\n\t};\n\t\n\tdo{\n\t\tif(answer)break;\n\t\trep(z,2){\t\t\n\t\t\trevstate[5] = z;\n\t\t\t//置く\n\t\t\tbool ret = puts(5,5,0,z);\n\t\t\tif(!ret)continue;\n\t\t\tdfs(dfs,0);\n\t\t\t//消す\n\t\t\terase(5,5,0,z);\n\t\t}\n\t}while(next_permutation(all(ord)));\n\n\t// cerr << checknum << endl;\n\n\tYes(answer);\n\t\n}\n \nint main(){\n\tios::sync_with_stdio(false);cin.tie(nullptr);\n\twhile(true){\n\t\tLL(n);\n\t\tif(n == 0)break;\n\t\tsolve(n);\n\t}\n\n}", "accuracy": 1, "time_ms": 2760, "memory_kb": 3712, "score_of_the_acc": -1.4056, "final_rank": 20 }, { "submission_id": "aoj_1636_10506192", "code_snippet": "#include <bits/extc++.h>\n\nusing ll = long long;\n\nusing pii = std::pair<int, int>;\n\nusing tiii = std::tuple<int, int, int>;\n\ntemplate <typename T>\nbool chmax(T& x, const T& v) {\n if (x < v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\n\nbool solve() {\n int N;\n std::cin >> N;\n if (N == 0) {\n return true;\n }\n std::array<std::vector<std::string>, 6> X;\n for (auto& xs : X) {\n xs = std::vector<std::string>(N);\n for (auto& x : xs) {\n std::cin >> x;\n }\n }\n\n auto rot = [&](int i, pii p) -> pii {\n while (i--) {\n p = {p.second, N - 1 - p.first};\n }\n\n return p;\n };\n\n auto flip = [&](bool f, pii p) -> pii {\n if (f) {\n p = {p.second, p.first};\n }\n\n return p;\n };\n\n std::array<int, 6> indices = {0, 1, 2, 3, 4, 5};\n\n auto map = [&](int i, pii p) -> tiii {\n int t = 0;\n\n if (i & 1) {\n t = N - 1;\n }\n\n auto [x, y] = p;\n\n if ((i >> 1) == 0) {\n return {x, y, t};\n } else if ((i >> 1) == 1) {\n return {y, t, x};\n } else {\n return {t, x, y};\n }\n };\n\n do {\n if (indices[0] != 0) break;\n\n std::vector table(N, std::vector(N, std::vector(N, -1)));\n\n auto rec = [&](auto rec, int i) -> bool {\n if (i == 6) {\n for (int k = 0; k < N; k++) {\n for (int x = 0; x < N; x += N - 1) {\n for (int y = 0; y < N; y += N - 1) {\n if (table[k][x][y] == -1) {\n return false;\n }\n if (table[y][k][x] == -1) {\n return false;\n }\n if (table[x][y][k] == -1) {\n return false;\n }\n }\n }\n }\n\n return true;\n }\n\n for (int s = 0; s < (i > 0 ? 8 : 1); s++) {\n bool isok = true;\n\n for (int aaa = 0; aaa < N - 1; aaa++) {\n for (int bbb = 0; bbb < 4; bbb++) {\n auto [x, y] = rot(bbb, {aaa, 0});\n\n if (X[indices[i]][x][y] == '.') continue;\n\n auto [a, b, c] = map(i, flip(s & 1, rot(s >> 1, {x, y})));\n\n if (table[a][b][c] >= 0) {\n isok = false;\n goto escape;\n }\n\n table[a][b][c] = i;\n }\n }\n\n if (isok) {\n if (rec(rec, i + 1)) {\n return true;\n }\n }\n\n escape:\n\n for (int k = 0; k < N; k++) {\n for (int x = 0; x < N; x += N - 1) {\n for (int y = 0; y < N; y += N - 1) {\n if (table[k][x][y] == i) table[k][x][y] = -1;\n if (table[x][k][y] == i) table[x][k][y] = -1;\n if (table[y][x][k] == i) table[y][x][k] = -1;\n }\n }\n }\n }\n\n return false;\n };\n\n if (rec(rec, 0)) {\n std::cout << \"Yes\\n\";\n return false;\n }\n\n for (int s0 = 0; s0 < 0; s0++) {\n for (int s1 = 0; s1 < 8; s1++) {\n for (int s2 = 0; s2 < 8; s2++) {\n for (int s3 = 0; s3 < 8; s3++) {\n for (int s4 = 0; s4 < 8; s4++) {\n for (int s5 = 0; s5 < 8; s5++) {\n std::array<int, 6> state = {s0, s1, s2, s3, s4, s5};\n\n bool isok = true;\n\n for (int i = 0; i < 6; i++) {\n for (int aaa = 0; aaa < N - 1; aaa++) {\n for (int bbb = 0; bbb < 4; bbb++) {\n auto [x, y] = rot(bbb, {aaa, 0});\n\n if (X[indices[i]][x][y] == '.') continue;\n\n auto [a, b, c] = map(\n i, flip(state[i] & 1, rot(state[i] >> 1, {x, y})));\n\n if (table[a][b][c]) {\n isok = false;\n goto escape;\n }\n\n table[a][b][c] = true;\n }\n }\n }\n\n for (int i = 0; i < N; i++) {\n for (int x = 0; x < N; x += N - 1) {\n for (int y = 0; y < N; y += N - 1) {\n if (!table[i][x][y]) {\n isok = false;\n goto escape;\n }\n if (!table[y][i][x]) {\n isok = false;\n goto escape;\n }\n if (!table[x][y][i]) {\n isok = false;\n goto escape;\n }\n }\n }\n }\n\n escape:\n\n if (isok) {\n std::cout << \"Yes\\n\";\n return false;\n }\n\n for (int i = 0; i < N; i++) {\n for (int x = 0; x < N; x += N - 1) {\n for (int y = 0; y < N; y += N - 1) {\n table[i][x][y] = table[y][i][x] = table[x][y][i] = false;\n }\n }\n }\n }\n }\n }\n }\n }\n }\n\n } while (std::next_permutation(indices.begin(), indices.end()));\n\n std::cout << \"No\\n\";\n\n return false;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 640, "memory_kb": 3456, "score_of_the_acc": -0.5459, "final_rank": 8 }, { "submission_id": "aoj_1636_10506144", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,l,r) for(int i=(l);i<(r);i++)\n#define all(v) begin(v),end(v)\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<ll>;\nint main(){\n int N;\n while(1){\n cin>>N;\n if(N==0)return 0;\n using edge= array<int,4>;\n auto rotate=[&](edge a) -> edge {\n return edge{a[1],a[2],a[3],a[0]};\n };\n auto reverseEdge=[&](int a) -> int {\n int ret=0;\n rep(i,0,N)ret|=(a>>i&1)<<(N-1-i);\n return ret;\n };\n auto flip=[&](edge a) -> edge {\n edge ret{0,0,0,0};\n ret[0]=reverseEdge(a[0]);\n ret[1]=reverseEdge(a[3]);\n ret[2]=reverseEdge(a[2]);\n ret[3]=reverseEdge(a[1]);\n return ret;\n };\n\n auto edgeCover=[&](int a,int b) -> bool {\n int c=((1<<(N-1))-2);\n if(a&b)return false;\n if((c&(a|b))!=c)return false;\n return true;\n };\n vector<edge> A(6);\n rep(i,0,6){\n vector<string> S(N);\n rep(j,0,N){\n cin>>S[j];\n }\n edge e{0,0,0,0};\n rep(i,0,N){\n if(S[0][i]=='X')e[0]|=1<<(N-1-i);\n if(S[i][N-1]=='X')e[1]|=1<<(N-1-i);\n if(S[N-1][i]=='X')e[2]|=1<<i;\n if(S[i][0]=='X')e[3]|=1<<i;\n }\n A[i]=e;\n }\n\n auto output=[&](int v){\n rep(i,0,N)cerr<<(v>>(N-1-i)&1);\n cerr<<'/';\n };\n // cerr<<edgeCover(A[0][0],reverseEdge(A[1][0]))<<endl;\n // output(A[0][0]);\n // output(A[0][1]);\n // output(A[0][2]);\n // output(A[0][3]);\n // cerr<<endl;\n // output(rotate(A[0])[0]);\n // output(rotate(A[0])[1]);\n // output(rotate(A[0])[2]);\n // output(rotate(A[0])[3]);\n // cerr<<endl;\n vi P(6);\n iota(all(P),0);\n bool ok=0;\n vector<edge> v;\n auto dfs=[&](auto f,int n) -> bool {\n if(n==6){\n bool now=1;\n if((v[0][0]&1)+(v[1][1]&1)+(v[3][3]&1)!=1)now=0;\n if((v[1][0]&1)+(v[3][0]&1)+(v[4][3]&1)!=1)now=0;\n if((v[1][3]&1)+(v[4][0]&1)+(v[5][3]&1)!=1)now=0;\n if((v[5][0]&1)+(v[1][2]&1)+(v[0][3]&1)!=1)now=0;\n\n if((v[2][0]&1)+(v[0][1]&1)+(v[3][2]&1)!=1)now=0;\n if((v[2][1]&1)+(v[3][1]&1)+(v[4][2]&1)!=1)now=0;\n if((v[2][2]&1)+(v[4][1]&1)+(v[5][2]&1)!=1)now=0;\n if((v[2][3]&1)+(v[5][1]&1)+(v[0][2]&1)!=1)now=0;\n \n if(!edgeCover(v[0][0],reverseEdge(v[1][2])))now=0;\n if(!edgeCover(v[0][2],reverseEdge(v[2][0])))now=0;\n \n if(!edgeCover(v[0][1],reverseEdge(v[3][3])))now=0;\n if(!edgeCover(v[3][0],reverseEdge(v[1][1])))now=0;\n if(!edgeCover(v[3][2],reverseEdge(v[2][1])))now=0;\n \n if(!edgeCover(v[3][1],reverseEdge(v[4][3])))now=0;\n if(!edgeCover(v[4][0],reverseEdge(v[1][0])))now=0;\n if(!edgeCover(v[4][2],reverseEdge(v[2][2])))now=0;\n \n if(!edgeCover(v[4][1],reverseEdge(v[5][3])))now=0;\n if(!edgeCover(v[5][0],reverseEdge(v[1][3])))now=0;\n if(!edgeCover(v[5][1],reverseEdge(v[0][3])))now=0;\n if(!edgeCover(v[5][2],reverseEdge(v[2][3])))now=0;\n return now;\n }\n bool ret=0;\n rep(x,0,8){\n edge e=A[P[n]];\n if(x&4)e=flip(e);\n rep(i,0,x%4)e=rotate(e);\n v.push_back(e);\n bool now=1;\n if(n==1)if(!edgeCover(v[0][0],reverseEdge(v[1][2])))now=0;\n if(n==2)if(!edgeCover(v[0][2],reverseEdge(v[2][0])))now=0;\n\n if(n==3){\n if(!edgeCover(v[0][1],reverseEdge(v[3][3])))now=0;\n if(!edgeCover(v[3][0],reverseEdge(v[1][1])))now=0;\n if(!edgeCover(v[3][2],reverseEdge(v[2][1])))now=0;\n }\n \n if(n==4){\n if(!edgeCover(v[3][1],reverseEdge(v[4][3])))now=0;\n if(!edgeCover(v[4][0],reverseEdge(v[1][0])))now=0;\n if(!edgeCover(v[4][2],reverseEdge(v[2][2])))now=0;\n }\n if(n==5){\n if(!edgeCover(v[4][1],reverseEdge(v[5][3])))now=0;\n if(!edgeCover(v[5][0],reverseEdge(v[1][3])))now=0;\n if(!edgeCover(v[5][1],reverseEdge(v[0][3])))now=0;\n if(!edgeCover(v[5][2],reverseEdge(v[2][3])))now=0;\n }\n\n if(now&&f(f,n+1))ret=1;\n v.pop_back();\n }\n return ret;\n };\n v.push_back(A[0]);\n do{ \n if(dfs(dfs,1))ok=1;\n }while(next_permutation(P.begin()+1,P.end()));\n cout<<(ok?\"Yes\\n\":\"No\\n\");\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3456, "score_of_the_acc": -0.4574, "final_rank": 6 }, { "submission_id": "aoj_1636_10506066", "code_snippet": "#line 2 \"/Users/noya2/Desktop/Noya2_library/template/template.hpp\"\nusing namespace std;\n\n#include<bits/stdc++.h>\n#line 1 \"/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp\"\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\n} // namespace noya2\n#line 1 \"/Users/noya2/Desktop/Noya2_library/template/const.hpp\"\nnamespace noya2{\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n} // namespace noya2\n#line 2 \"/Users/noya2/Desktop/Noya2_library/template/utils.hpp\"\n\n#line 6 \"/Users/noya2/Desktop/Noya2_library/template/utils.hpp\"\n\nnamespace noya2{\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << std::min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(std::abs(a),std::abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(std::vector<T> &v){\n std::sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n#line 8 \"/Users/noya2/Desktop/Noya2_library/template/template.hpp\"\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\n\n#line 2 \"c.cpp\"\n\nint rev(int n, int x){\n int y = 0;\n rep(i,n){\n y |= ((x >> (n-1-i) & 1) << i);\n }\n return y;\n}\n\nstruct face {\n int n;\n array<int,4> e;\n void input(int _n){\n n = _n;\n vector<string> a(n); in(a);\n e = {};\n rep(i,n){\n if (a[0][i] == 'X'){\n e[0] |= (1 << i);\n }\n }\n rep(i,n){\n if (a[i][n-1] == 'X'){\n e[1] |= (1 << i);\n }\n }\n rep(i,n){\n if (a[n-1][n-1-i] == 'X'){\n e[2] |= (1 << i);\n }\n }\n rep(i,n){\n if (a[n-1-i][0] == 'X'){\n e[3] |= (1 << i);\n }\n }\n }\n void rot(){\n swap(e[0],e[1]);\n swap(e[1],e[2]);\n swap(e[2],e[3]);\n }\n void flip(){\n rep(i,4){\n e[i] = rev(n,e[i]);\n }\n swap(e[1],e[3]);\n }\n};\n\nvoid solve1(int n){\n vector<face> f(6);\n rep(i,6){\n f[i].input(n);\n }\n int msk = (1<<(n-1))-2;\n auto test = f;\n auto check = [&](int x, int y){\n if ((x&msk)&(y&msk)) return false;\n return ((x&msk)|(y&msk)) == msk;\n };\n auto edge = [&](int i, int di, int j, int dj, bool revd = false){\n if (revd) check(test[i].e[di], test[j].e[dj]);\n return check(test[i].e[di], rev(n,test[j].e[dj]));\n };\n auto vertex = [&](int i, int di, int j, int dj, int k, int dk){\n return (test[i].e[di]&1) + (test[j].e[dj]&1) + (test[k].e[dk]&1) == 1;\n };\n int call = 0;\n auto valid = [&](){\n call++;\n // if (!edge(0,0,4,0)) return false;\n // if (!edge(0,1,3,0)) return false;\n // if (!edge(0,3,5,0)) return false;\n // if (!edge(1,3,5,1)) return false;\n // if (!edge(2,3,5,2)) return false;\n // if (!edge(2,2,4,2)) return false;\n // if (!edge(2,1,3,2)) return false;\n // if (!edge(0,2,1,0,1)) return false;\n // if (!edge(1,2,2,0,1)) return false;\n // if (!edge(1,1,3,3,1)) return false;\n // if (!edge(3,1,4,3,1)) return false;\n // if (!edge(4,1,5,3,1)) return false;\n if (!vertex(0,2,1,1,3,0)) return false;\n if (!vertex(0,1,3,1,4,0)) return false;\n if (!vertex(0,0,4,1,5,0)) return false;\n if (!vertex(1,3,2,0,5,2)) return false;\n if (!vertex(2,3,4,2,5,3)) return false;\n if (!vertex(3,2,4,3,2,2)) return false;\n if (!vertex(0,3,1,0,5,1)) return false;\n if (!vertex(1,2,2,1,3,3)) return false;\n return true;\n };\n auto brunchok = [&](int i){\n if (i == 0) return true;\n if (i == 1){\n if (!edge(0,2,1,0,1)) return false;\n return true;\n }\n if (i == 2){\n if (!edge(1,2,2,0,1)) return false;\n return true;\n }\n if (i == 3){\n if (!edge(0,1,3,0)) return false;\n if (!edge(2,1,3,2)) return false;\n if (!edge(1,1,3,3,1)) return false;\n return true;\n }\n if (i == 4){\n if (!edge(0,0,4,0)) return false;\n if (!edge(2,2,4,2)) return false;\n if (!edge(3,1,4,3,1)) return false;\n return true;\n }\n if (i == 5){\n if (!edge(0,3,5,0)) return false;\n if (!edge(1,3,5,1)) return false;\n if (!edge(2,3,5,2)) return false;\n if (!edge(4,1,5,3,1)) return false;\n return true;\n }\n return false;\n };\n bool ans = false;\n auto dfs = [&](auto sfs, int step) -> void {\n if (ans) return ;\n if (step == 12){\n if (valid()){\n ans = true;\n }\n return ;\n }\n int i = step/2;\n if (step % 2 == 0){\n rep(tt,2){\n sfs(sfs,step+1);\n test[i].flip();\n }\n }\n else {\n rep(tt,4){\n if (brunchok(i)){\n sfs(sfs,step+1);\n }\n test[i].rot();\n }\n }\n };\n repp(i,1,6){\n int j = (i == 1 ? 2 : 1);\n test[0] = f[0];\n test[1] = f[j];\n test[2] = f[i];\n vector<int> p;\n rep(k,6){\n if (k == 0 || k == i || k == j) continue;\n p.emplace_back(k);\n }\n assert(p.size() == 3u);\n do {\n rep(k,3){\n test[3+k] = f[p[k]];\n }\n dfs(dfs,0);\n }while(next_permutation(all(p)));\n }\n yn(ans);\n // out(call);\n}\n\nvoid solve(){\n int n;\n while (true){\n cin >> n;\n if (n == 0) return ;\n solve1(n);\n }\n}\n\nint main(){\n int t = 1; //in(t);\n while (t--) { solve(); }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3584, "score_of_the_acc": -0.7324, "final_rank": 15 }, { "submission_id": "aoj_1636_10505961", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, a, b) for(ll i = a; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n#pragma optimize GCC(\"Ofast\")\n\nint main(){\n while(true) {\n int n;\n cin >> n;\n if(n ==0 )break;\n int sum = 0;\n\n vector<vector<string>> b(6, vector<string>(n));\n rep(i, 0, 6){\n rep(j, 0, n) {\n cin >> b[i][j];\n rep(k, 0, n) sum += (b[i][j][k]=='X'?1:0);\n }\n }\n\n if(sum != n*n*2 + (n-2) * (n-1) * 4){\n cout << \"No\" << endl;\n continue;\n }\n\n vector<int> iot(5);\n iota(all(iot), 1);\n\n auto rot = [&](vector<string> f, int p) -> vector<string> {\n if(p >= 4) {\n p -= 4;\n rep(i, 0, n) reverse(all(f[i]));\n }\n rep(i, 0, p) {\n vector<string> nx(n, string(n, '.'));\n rep(j, 0, n){\n rep(k, 0, n){\n nx[k][n-1-j] = f[j][k];\n }\n }\n nx.swap(f);\n }\n return f;\n };\n bool ans = false;\n\n vector rott(6, vector(8, vector<string>()));\n rep(i, 0, 6){\n rep(j ,0, 8){\n rott[i][j] = rot(b[i], j);\n }\n }\n\n \n do {\n vector<string> f0 = b[0];\n rep(i1, 0, 8) {\n bool res = true;\n rep(i, 0, n){\n if(f0[0][i] == 'X' && rott[iot[0]][i1][n-1][i] == 'X') res = false;\n }\n if(!res) continue;\n rep(i2, 0, 8) {\n res = true;\n rep(i, 0, n){\n if(f0[i][n-1] == 'X' && rott[iot[1]][i2][i][0] == 'X') res = false;\n if(rott[iot[0]][i1][i][n-1] == 'X' && rott[iot[1]][i2][0][n-1-i] == 'X') res = false;\n }\n if(!res) continue;\n rep(i3, 0, 8) {\n res = true;\n rep(i, 0, n){\n if(f0[n-1][i] == 'X' && rott[iot[2]][i3][0][i] == 'X') res = false;\n if(rott[iot[1]][i2][n-1][i] == 'X' && rott[iot[2]][i3][i][n-1] == 'X') res = false;\n }\n if(!res) continue;\n rep(i4, 0, 8){\n res = true;\n rep(i, 0, n){\n if(f0[i][0] == 'X' && rott[iot[3]][i4][i][n-1] == 'X') res = false;\n if(rott[iot[2]][i3][i][0] == 'X' && rott[iot[3]][i4][n-1][n-1-i] == 'X') res = false;\n if(rott[iot[3]][i4][0][i] == 'X' && rott[iot[0]][i1][i][0] == 'X') res = false;\n }\n if(!res) continue;\n rep(i5, 0, 8){\n res = true;\n rep(i, 0, n){\n if(rott[iot[4]][i5][0][i] == 'X' && rott[iot[0]][i1][0][i] == 'X') res = false;\n if(rott[iot[4]][i5][n-1][i] == 'X' && rott[iot[2]][i3][n-1][i] == 'X') res = false;\n if(rott[iot[4]][i5][i][0] == 'X' && rott[iot[3]][i4][i][0] == 'X') res = false;\n if(rott[iot[4]][i5][i][n-1] == 'X' && rott[iot[1]][i2][i][n-1] == 'X') res = false;\n }\n if(!res) continue;\n ans = true;\n }\n if(ans) break;\n }\n if(ans) break;\n }\n if(ans) break;\n }\n if(ans) break;\n }\n if(ans) break;\n } while(next_permutation(all(iot)));\n\n rep(i, 0, n) reverse(all(b[0][i]));\n sort(all(iot));\n do {\n vector<string> f0 = b[0];\n rep(i1, 0, 8) {\n bool res = true;\n rep(i, 0, n){\n if(f0[0][i] == 'X' && rott[iot[0]][i1][n-1][i] == 'X') res = false;\n }\n if(!res) continue;\n rep(i2, 0, 8) {\n res = true;\n rep(i, 0, n){\n if(f0[i][n-1] == 'X' && rott[iot[1]][i2][i][0] == 'X') res = false;\n if(rott[iot[0]][i1][i][n-1] == 'X' && rott[iot[1]][i2][0][n-1-i] == 'X') res = false;\n }\n if(!res) continue;\n rep(i3, 0, 8) {\n res = true;\n rep(i, 0, n){\n if(f0[n-1][i] == 'X' && rott[iot[2]][i3][0][i] == 'X') res = false;\n if(rott[iot[1]][i2][n-1][i] == 'X' && rott[iot[2]][i3][i][n-1] == 'X') res = false;\n }\n if(!res) continue;\n rep(i4, 0, 8){\n res = true;\n rep(i, 0, n){\n if(f0[i][0] == 'X' && rott[iot[3]][i4][i][n-1] == 'X') res = false;\n if(rott[iot[2]][i3][i][0] == 'X' && rott[iot[3]][i4][n-1][n-1-i] == 'X') res = false;\n if(rott[iot[3]][i4][0][i] == 'X' && rott[iot[0]][i1][i][0] == 'X') res = false;\n }\n if(!res) continue;\n rep(i5, 0, 8){\n res = true;\n rep(i, 0, n){\n if(rott[iot[4]][i5][0][i] == 'X' && rott[iot[0]][i1][0][i] == 'X') res = false;\n if(rott[iot[4]][i5][n-1][i] == 'X' && rott[iot[2]][i3][n-1][i] == 'X') res = false;\n if(rott[iot[4]][i5][i][0] == 'X' && rott[iot[3]][i4][i][0] == 'X') res = false;\n if(rott[iot[4]][i5][i][n-1] == 'X' && rott[iot[1]][i2][i][n-1] == 'X') res = false;\n }\n if(!res) continue;\n ans = true;\n }\n if(ans) break;\n }\n if(ans) break;\n }\n if(ans) break;\n }\n if(ans) break;\n }\n if(ans) break;\n } while(next_permutation(all(iot)));\n\n if(ans) {\n cout << \"Yes\" << endl;\n }else{\n cout << \"No\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3584, "score_of_the_acc": -0.728, "final_rank": 14 }, { "submission_id": "aoj_1636_10340448", "code_snippet": "// AOJ #1636 Cube Surface Puzzle\n// 2025.4.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvector<string> rotatePiece(const vector<string>& S) {\n int N = S.size();\n vector<string> R(N, string(N, ' '));\n for (int i = 0; i < N; i++){\n for (int j = 0; j < N; j++) R[j][N - 1 - i] = S[i][j];\n }\n return R;\n}\n\nvector<string> flipPiece(const vector<string>& S) {\n vector<string> F = S;\n for(auto &row : F) reverse(row.begin(), row.end());\n return F;\n}\n\nbool edgeMatch(const string &A, const string &B) {\n int L = A.size();\n for (int i = 0; i < L; i++) if(A[i]=='X' && B[i]=='X') return false;\n for (int i = 1; i < L - 1; i++) if(A[i]=='.' && B[i]=='.') return false;\n return true;\n}\n\nbool OK(char a, char b, char c) { return (a + b + c) == ('.' + '.' + 'X'); }\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while(true) {\n int N;\n cin >> N;\n if(N == 0) break;\n\n vector<vector<string>> S(6, vector<string>(N));\n for (int i = 0; i < 6; i++) for (int j = 0; j < N; j++) cin >> S[i][j];\n\n vector<vector<vector<string>>> T(6, vector<vector<string>>(8));\n for (int i = 0; i < 6; i++){\n T[i][0] = S[i];\n for (int j = 1; j < 4; j++) T[i][j] = rotatePiece(T[i][j - 1]);\n for (int j = 4; j < 8; j++) T[i][j] = flipPiece(T[i][j - 4]);\n }\n\n vector<vector<vector<string>>> X(6, vector<vector<string>>(8, vector<string>(8, \"\")));\n for (int i = 0; i < 6; i++){\n for (int o = 0; o < 8; o++){\n X[i][o][0] = T[i][o][0];\n X[i][o][1] = \"\";\n for (int k = 0; k < N; k++) X[i][o][1] += T[i][o][k][N - 1];\n X[i][o][2] = \"\";\n for (int k = 0; k < N; k++) X[i][o][2] += T[i][o][N - 1][N - 1 - k];\n X[i][o][3] = \"\";\n for (int k = 0; k < N; k++) X[i][o][3] += T[i][o][N - 1 - k][0];\n for (int k = 0; k < 4; k++){\n X[i][o][k + 4] = X[i][o][k];\n reverse(X[i][o][k + 4].begin(), X[i][o][k + 4].end());\n }\n }\n }\n\n bool found = false;\n for (int m0 = 0; m0 < 8 && !found; m0++){\n for (int d = 1; d < 6 && !found; d++){\n for (int md = 0; md < 8 && !found; md++){\n int l = 1; if(l == d) l++;\n for (int ml = 0; ml < 8 && !found; ml++){\n if(!edgeMatch(X[0][m0][7], X[l][ml][0])) continue;\n if(!edgeMatch(X[l][ml][6], X[d][md][7])) continue;\n vector<int> B;\n for (int k = 1; k < 6; k++) if(k != d && k != l) B.push_back(k);\n for (int iB = 0; iB < (int)B.size() && !found; iB++){\n int b = B[iB];\n for (int mb = 0; mb < 8 && !found; mb++){\n if(!edgeMatch(X[b][mb][0], X[0][m0][0])) continue;\n if(!edgeMatch(X[b][mb][7], X[l][ml][7])) continue;\n if(!edgeMatch(X[b][mb][6], X[d][md][0])) continue;\n for (int iB2 = 0; iB2 < (int)B.size() && !found; iB2++){\n if(B[iB2] == b) continue;\n int r = B[iB2];\n for (int mr = 0; mr < 8 && !found; mr++){\n if(!edgeMatch(X[b][mb][5], X[r][mr][3])) continue;\n if(!edgeMatch(X[0][m0][1], X[r][mr][0])) continue;\n if(!edgeMatch(X[r][mr][6], X[d][md][1])) continue;\n int f = B[0] ^ B[1] ^ B[2] ^ b ^ r;\n for (int mf = 0; mf < 8 && !found; mf++){\n if(!edgeMatch(X[f][mf][0], X[0][m0][6])) continue;\n if(!edgeMatch(X[f][mf][1], X[r][mr][1])) continue;\n if(!edgeMatch(X[f][mf][2], X[d][md][2])) continue;\n if(!edgeMatch(X[f][mf][3], X[l][ml][5])) continue;\n if(!OK(T[0][m0][0][0], T[l][ml][0][0], T[b][mb][0][0])) continue;\n if(!OK(T[0][m0][0][N-1], T[b][mb][0][N-1], T[r][mr][0][0])) continue;\n if(!OK(T[0][m0][N-1][0], T[l][ml][0][N-1], T[f][mf][0][0])) continue;\n if(!OK(T[0][m0][N-1][N-1], T[r][mr][0][N-1], T[f][mf][0][N-1])) continue;\n if(!OK(T[d][md][0][0], T[l][ml][N-1][0], T[b][mb][N-1][0])) continue;\n if(!OK(T[d][md][0][N-1], T[b][mb][N-1][N-1], T[r][mr][N-1][0])) continue;\n if(!OK(T[d][md][N-1][0], T[l][ml][N-1][N-1], T[f][mf][N-1][0])) continue;\n if(!OK(T[d][md][N-1][N-1], T[f][mf][N-1][N-1], T[r][mr][N-1][N-1])) continue;\n\n found = true;\n break;\n }\n }\n }\n }\n }\n }\n }\n }\n }\n cout << (found ? \"Yes\" : \"No\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3536, "score_of_the_acc": -0.6254, "final_rank": 13 }, { "submission_id": "aoj_1636_9679441", "code_snippet": "#if !defined(__clang__) && defined(__GNUC__)\n#include <bits/stdc++.h>\n#pragma GCC optimize(\"O3,unroll-loops\")\n#ifdef ONLINE_JUDGE\n#pragma GCC target(\"avx2,fma,popcnt,bmi,bmi2\")\n#endif\n#elif defined(__clang__)\n#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <deque>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <vector>\n#else\n#error \"We don't know this compiler lol\"\n#endif\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\nstd::ostream &operator<<(std::ostream &os, const modint998244353 &a) {\n return os << a.val();\n}\nstd::ostream &operator<<(std::ostream &os, const modint1000000007 &a) {\n return os << a.val();\n}\nstd::istream &operator>>(std::istream &is, modint998244353 &a) {\n long long t;\n is >> t;\n a = t;\n return is;\n}\nstd::istream &operator>>(std::istream &is, modint1000000007 &a) {\n long long t;\n is >> t;\n a = t;\n return is;\n}\n#endif\nusing namespace std;\n// # Type Aliases\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\ntemplate <class T>\nusing V = vector<T>;\ntemplate <class T, class U>\nusing P = pair<T, U>;\ntemplate <class T>\nusing max_heap = priority_queue<T>;\ntemplate <class T>\nusing min_heap = priority_queue<T, vector<T>, greater<T>>;\n// # Repeat Macros\n#define OVERLOADER(_1, _2, _3, NAME, ...) NAME\n#define REP2(i, a, b) for (ll i = (a); i < (ll)(b); ++i)\n#define REP(i, n) REP2(i, 0, n)\n#define rep(...) OVERLOADER(__VA_ARGS__, REP2, REP)(__VA_ARGS__)\n#define repd(i, a, b) for (ll i = (ll)(b) - 1; i >= (ll)(a); --i)\n// # Abbreviation Macros\n#define pb push_back\n#define eb emplace_back\n#define ALL(a) begin(a), end(a)\n#define RALL(a) a.rbegin(), a.rend()\n// # Fast IO and IO Settings\nstruct IO_Setting {\n IO_Setting() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(3);\n }\n} io_setting;\n// # Input Overloads\ntemplate <class T, class U>\nistream &operator>>(istream &is, P<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nistream &operator>>(istream &is, V<T> &v) {\n for (T &e : v) is >> e;\n return is;\n}\n// # Output Overloads\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const P<T, U> &p) {\n return os << p.first << ' ' << p.second;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, const V<T> &v) {\n int ss = v.size();\n rep(ii, ss) { os << v[ii] << (ii + 1 == ss ? \"\" : \" \"); }\n return os;\n}\n// # Function Definition\ntemplate <class T>\nT sq(T x) {\n return x * x;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\nll max(ll lhs, ll rhs) { return std::max<ll>(lhs, rhs); }\nll min(ll lhs, ll rhs) { return std::min<ll>(lhs, rhs); }\nll ceil_div(ll a, ll b) { return a / b + ((a ^ b) >= 0 && a % b); }\nll floor_div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate <class Container>\nContainer reversed(const Container &v) {\n return Container(v.rbegin(), v.rend());\n}\ntemplate <class Container>\nContainer sorted(const Container &v, bool rev = false) {\n Container r = v;\n rev ? sort(r.rbegin(), r.rend()) : sort(r.begin(), r.end());\n return r;\n}\ntemplate <class T>\nvoid uniqify(V<T> &v) {\n v.erase(unique(ALL(v)), v.end());\n}\ntemplate <class T>\nV<T> compress(V<T> &v) {\n V<T> vals = v;\n sort(ALL(vals));\n uniqify(vals);\n rep(i, v.size()) v[i] = lower_bound(ALL(vals), v[i]) - vals.begin();\n return vals;\n}\ntemplate <class T>\nV<T> sum_array(const V<T> &v) {\n int n = v.size();\n V<T> sum(n + 1);\n rep(i, n) sum[i + 1] = sum[i] + v[i];\n return sum;\n}\ntemplate <class T>\ninline void drop(T &&x) {\n cout << x << endl;\n exit(0);\n}\ninline void yesno(bool f) { cout << (f ? \"Yes\" : \"No\") << '\\n'; }\n// # Constant Definition\nconstexpr char ENDL = '\\n';\nconstexpr ll INF = (1LL << 30) - 1; // 問題毎にfit\nconstexpr ll INFLL = (1LL << 62) - 1; // 問題毎にfit\nconst ld PI = acos(static_cast<long double>(-1));\n// constexpr int dy[] = {0, 1, 0, -1};\n// constexpr int dx[] = {1, 0, -1, 0};\n// constexpr int dy[]={0, 1, 1, 1, 0, -1, -1, -1};\n// constexpr int dx[]={1, 1, 0, -1, -1, -1, 0, 1};\n// # Debug Macro\n#ifdef LOCAL\n// #include \"CP-library/debug_print.hpp\"\n// #define debug(...) debug_impl(__LINE__, __VA_ARGS__)\n// #define debug_impl(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)\n#include \"cpp-dump/cpp-dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\n#if __has_include(<atcoder/modint>)\n#include <atcoder/modint>\nnamespace cpp_dump::_detail {\ntemplate <int m>\ninline std::string export_var(const atcoder::static_modint<m> &mint,\n const std::string &indent,\n std::size_t last_line_length,\n std::size_t current_depth, bool fail_on_newline,\n const export_command &command) {\n return export_var(mint.val(), indent, last_line_length, current_depth,\n fail_on_newline, command);\n}\ntemplate <int m>\ninline std::string export_var(const atcoder::dynamic_modint<m> &mint,\n const std::string &indent,\n std::size_t last_line_length,\n std::size_t current_depth, bool fail_on_newline,\n const export_command &command) {\n return export_var(mint.val(), indent, last_line_length, current_depth,\n fail_on_newline, command);\n}\n} // namespace cpp_dump::_detail\n#endif // __has_include(<atcoder/modint>)\nnamespace cp = cpp_dump;\nCPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);\nCPP_DUMP_SET_OPTION_GLOBAL(log_label_func, cp::log_label::line());\n// CPP_DUMP_DEFINE_EXPORT_OBJECT(Type, properties..., methods...);\n#define INLOCAL(statements) \\\n do { \\\n statements \\\n } while (false)\n#else\n#define debug(...)\n#define dump(...)\n#define INLOCAL(statements)\n#endif\n// using mint = modint1000000007;\n// using mint = modint998244353;\n\nconstexpr ll B = 9;\nll n;\n\nstruct Piece {\n bool grid[B][B];\n Piece() : grid() {}\n\n Piece flip() {\n Piece ret;\n rep(i, n) {\n rep(j, n) { ret.grid[i][j] = grid[i][n - j - 1]; }\n }\n return ret;\n }\n Piece clockwise_rotated() {\n Piece ret;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n ret.grid[i][j] = grid[n - j - 1][i];\n }\n }\n return ret;\n }\n};\nPiece pieces[6];\nPiece transformed[6][8];\n\nconstexpr int PLAINS = 6;\n// 絶対*(n-1)してください\nconstexpr int ofx[] = {0, 0, 0, 0, 1, 0};\nconstexpr int ofy[] = {0, 0, 0, 1, 0, 0};\nconstexpr int ofz[] = {0, 0, 0, 0, 0, 1};\n\nconstexpr int dix[] = {1, 1, 0, 1, 0, 1};\nconstexpr int diy[] = {0, 0, 1, 0, 1, 0};\nconstexpr int diz[] = {0, 0, 0, 0, 0, 0};\nconstexpr int djx[] = {0, 0, 0, 0, 0, 0};\nconstexpr int djy[] = {1, 0, 0, 0, 0, 1};\nconstexpr int djz[] = {0, 1, 1, 1, 1, 0};\n\nstruct Puzzle {\n int invalid_count;\n int filled_count;\n int cube_cnt[B][B][B];\n\n Puzzle() : invalid_count(), filled_count(), cube_cnt() {}\n\n void place(int plain, const Piece &piece) {\n const int pofx = ofx[plain] * (n - 1);\n const int pofy = ofy[plain] * (n - 1);\n const int pofz = ofz[plain] * (n - 1);\n rep(i, n) {\n rep(j, n) {\n int &target = cube_cnt[pofx + i * dix[plain] + j * djx[plain]]\n [pofy + i * diy[plain] + j * djy[plain]]\n [pofz + i * diz[plain] + j * djz[plain]];\n int prev = target;\n target += piece.grid[i][j];\n if (prev == 0 && target == 1) filled_count++;\n if (prev == 1 && target == 2) invalid_count++;\n }\n }\n }\n void unplace(int plain, const Piece &piece) {\n const int pofx = ofx[plain] * (n - 1);\n const int pofy = ofy[plain] * (n - 1);\n const int pofz = ofz[plain] * (n - 1);\n rep(i, n) {\n rep(j, n) {\n int &target = cube_cnt[pofx + i * dix[plain] + j * djx[plain]]\n [pofy + i * diy[plain] + j * djy[plain]]\n [pofz + i * diz[plain] + j * djz[plain]];\n int prev = target;\n target -= piece.grid[i][j];\n if (prev == 1 && target == 0) filled_count--;\n if (prev == 2 && target == 1) invalid_count--;\n }\n }\n }\n bool is_valid() { return invalid_count == 0; }\n bool is_satisfied() {\n return filled_count == n * n * n - (n - 2) * (n - 2) * (n - 2);\n }\n};\n\n#ifdef LOCAL\nCPP_DUMP_DEFINE_EXPORT_OBJECT(Piece, grid);\nCPP_DUMP_DEFINE_EXPORT_OBJECT(Puzzle, invalid_count, filled_count, cube_cnt);\n#endif\n\nbool solve() {\n Puzzle puzzle;\n // input\n rep(i, 6) {\n rep(j, n) {\n rep(k, n) {\n char c;\n cin >> c;\n pieces[i].grid[j][k] = (c == 'X');\n }\n }\n }\n // dump(pieces | cp::boolnum());\n\n // piece5をおく\n puzzle.place(5, pieces[5]);\n dump(puzzle);\n // puzzle.unplace(5, pieces[5]);\n // dump(puzzle);\n // return false; // debug\n\n // make transformed\n rep(i, 5) {\n transformed[i][0] = pieces[i];\n rep(j, 1, 4) {\n transformed[i][j] = transformed[i][j - 1].clockwise_rotated();\n }\n transformed[i][4] = transformed[i][3].clockwise_rotated().flip();\n rep(j, 5, 8) {\n transformed[i][j] = transformed[i][j - 1].clockwise_rotated();\n }\n // dump(i, transformed[i] | cp::boolnum());\n }\n\n vector<int> perm(5);\n iota(ALL(perm), 0);\n\n do {\n bool ok = false;\n // dfs\n auto dfs = [&](auto &f, int placed) -> void {\n if (placed == 5) {\n if (puzzle.is_satisfied()) {\n ok = true;\n }\n return;\n }\n rep(j, 8) {\n puzzle.place(placed, transformed[perm[placed]][j]);\n if (puzzle.is_valid()) {\n f(f, placed + 1);\n }\n puzzle.unplace(placed, transformed[perm[placed]][j]);\n }\n };\n dfs(dfs, 0);\n // if (!(puzzle.filled_count == 0 && puzzle.invalid_count == 0)) {\n // dump(ok, perm, puzzle);\n // assert(false);\n // }\n // assert(puzzle.filled_count == 0 && puzzle.invalid_count == 0);\n if (ok) return true;\n } while (next_permutation(ALL(perm)));\n return false;\n}\n\nint main() {\n while (true) {\n cin >> n;\n if (n == 0) break;\n cout << (solve() ? \"Yes\" : \"No\") << ENDL;\n }\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 3480, "score_of_the_acc": -0.5942, "final_rank": 11 }, { "submission_id": "aoj_1636_9465493", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nvector<string>rot(vector<string>S){\n auto T=S;\n REP(i,sz(S))REP(j,sz(S))T[i][j]=S[sz(S)-1-j][i];\n return T;\n}\nvector<string>rev(vector<string>S){\n auto T=S;\n REP(i,sz(T))reverse(ALL(T[i]));\n return T;\n}\nbool f(vector<string>&F,vector<string>&B,vector<string>&L,vector<string>&R,vector<string>&U,vector<string>&D){\n ll N=sz(F);\n REP(i,N){\n if(U[0][i]=='X'&&B[0][i]=='X')return 0;\n if(U[i][0]=='X'&&L[0][i]=='X')return 0;\n if(U[N-1][i]=='X'&&F[0][i]=='X')return 0;\n if(U[i][N-1]=='X'&&R[0][N-1-i]=='X')return 0;\n if(F[i][0]=='X'&&L[i][N-1]=='X')return 0;\n if(F[i][N-1]=='X'&&R[i][0]=='X')return 0;\n if(B[i][0]=='X'&&L[i][0]=='X')return 0;\n if(B[i][N-1]=='X'&&R[i][N-1]=='X')return 0;\n if(D[0][i]=='X'&&B[N-1][i]=='X')return 0;\n if(D[N-1][i]=='X'&&F[N-1][i]=='X')return 0;\n if(D[i][0]=='X'&&L[N-1][i]=='X')return 0;\n if(D[i][N-1]=='X'&&R[N-1][N-1-i]=='X')return 0;\n }\n return 1;\n};\nint main(){\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n vector<vector<string>>S(6,vector<string>(N));cin>>S;\n ll cnt=0;\n REP(i,6)REP(j,N)REP(k,N)if(S[i][j][k]=='X')cnt++;\n if(cnt!=N*N*N-(N-2)*(N-2)*(N-2)){cout<<\"No\"<<endl;continue;}\n vector<vector<vector<string>>>T(6,vector<vector<string>>(8,vector<string>(N)));\n REP(i,6){\n T[i][0]=S[i];\n FOR(j,1,4)T[i][j]=rot(T[i][j-1]);\n T[i][4]=rev(S[i]);\n FOR(j,5,8)T[i][j]=rot(T[i][j-1]);\n }\n bool ok=0;\n for(auto&S0:T[0])REP(_,5){\n for(auto&S1:T[1]){\n for(auto&S2:T[2]){\n for(auto&S3:T[3]){\n for(auto&S4:T[4]){\n for(auto&S5:T[5]){\n if(f(S0,S1,S2,S3,S4,S5))ok=1;\n if(f(S0,S1,S2,S3,S5,S4))ok=1;\n if(f(S0,S1,S2,S4,S3,S5))ok=1;\n if(f(S0,S1,S2,S4,S5,S3))ok=1;\n if(f(S0,S1,S2,S5,S3,S4))ok=1;\n if(f(S0,S1,S2,S5,S4,S3))ok=1;\n }\n }\n }\n }\n }\n rotate(S.begin()+1,S.begin()+2,S.end());\n rotate(T.begin()+1,T.begin()+2,T.end());\n }\n cout<<(ok?\"Yes\":\"No\")<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6790, "memory_kb": 3416, "score_of_the_acc": -1.3675, "final_rank": 19 }, { "submission_id": "aoj_1636_9452795", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\n\nint N;\nint MAX;\nint PV[6][6][8] = {}; //piece i wo surface j ni muki k de haru\nint PE[6][6][8][12] = {}; //piece i wo surface j ni muki k de haru\n\nconst int SV[6][4] = {{0,1,3,2},{0,1,4,5},{1,2,5,6},{3,2,7,6},{0,3,4,7},{4,5,7,6}};\nconst int SE[6][4] = {{0,3,2,1},{0,4,8,5},{1,5,9,6},{2,7,10,6},{3,4,11,7},{8,11,10,9}};\n\nvector<vector<pair<int,int>>> Share(6);\n\nvector<vector<bool>> Rotate(vector<vector<bool>> B) {\n vector<vector<bool>> Ret(N,vector<bool>(N));\n rep(i,0,N) {\n rep(j,0,N) {\n Ret[N-1-j][i] = B[i][j];\n }\n }\n return Ret;\n}\n\nvector<vector<bool>> Flip(vector<vector<bool>> B) {\n vector<vector<bool>> Ret(N,vector<bool>(N));\n rep(i,0,N) {\n rep(j,0,N) {\n Ret[N-1-i][j] = B[i][j];\n }\n }\n return Ret;\n}\n\nbool DFS(int Level, vector<bool>& used, vector<pair<int,int>>& Cur, int Ver) { //Cur[i] : surface i ni piece first muki second\n if (Level == 6) {\n if (Ver == 255) return true;\n else return false;\n }\n rep(i,0,6) {\n if (used[i]) continue;\n rep(j,0,8) {\n bool check = true;\n if ((Ver & PV[i][Level][j]) != 0) check = false;\n for (pair<int,int> k : Share[Level]) {\n if ((PE[i][Level][j][k.first] ^ PE[Cur[k.second].first][k.second][Cur[k.second].second][k.first]) != MAX) check = false;\n }\n if (!check) continue;\n used[i] = true;\n Cur.push_back({i,j});\n if (DFS(Level+1,used,Cur,Ver|PV[i][Level][j])) return true;\n used[i] = false;\n Cur.pop_back();\n }\n }\n return false;\n}\n\nint main() {\nShare[0] = {}, Share[1] = {{0,0}}, Share[2] = {{1,0},{5,1}}, Share[3] = {{2,0},{6,2}}, Share[4] = {{3,0},{4,1},{7,3}}, Share[5] = {{8,1},{9,2},{10,3},{11,4}};\nwhile(1) {\n cin >> N;\n if (N == 0) return 0;\n rep(i,0,6) rep(j,0,6) rep(k,0,8) PV[i][j][k] = 0;\n rep(i,0,6) rep(j,0,6) rep(k,0,8) rep(l,0,12) PE[i][j][k][l] = 0;\n MAX = (1<<(N-2)) - 1;\n rep(i,0,6) {\n vector<vector<bool>> Piece(N,vector<bool>(N));\n rep(j,0,N) {\n string S;\n cin >> S;\n rep(k,0,N) {\n if (S[k] == '.') Piece[j][k] = 0;\n else Piece[j][k] = 1;\n }\n }\n rep(j,0,4) {\n rep(k,0,6) {\n if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);\n if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);\n if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);\n if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);\n rep(l,1,N-1) {\n if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));\n if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));\n if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));\n if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));\n }\n }\n Piece = Rotate(Piece);\n }\n Piece = Flip(Piece);\n rep(j,4,8) {\n rep(k,0,6) {\n if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);\n if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);\n if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);\n if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);\n rep(l,1,N-1) {\n if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));\n if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));\n if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));\n if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));\n }\n }\n Piece = Rotate(Piece);\n }\n }\n vector<bool> used(6,false);\n vector<pair<int,int>> Cur = {};\n bool ANS = DFS(0,used,Cur,0);\n cout << (ANS ? \"Yes\" : \"No\") << endl;\n}\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3424, "score_of_the_acc": -0.423, "final_rank": 5 }, { "submission_id": "aoj_1636_9452792", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\n\nint N;\nint MAX;\nint PV[6][6][8] = {}; //piece i wo surface j ni muki k de haru\nint PE[6][6][8][12] = {}; //piece i wo surface j ni muki k de haru\n\nconst int SV[6][4] = {{0,1,3,2},{0,1,4,5},{1,2,5,6},{3,2,7,6},{0,3,4,7},{4,5,7,6}};\nconst int SE[6][4] = {{0,3,2,1},{0,4,8,5},{1,5,9,6},{2,7,10,6},{3,4,11,7},{8,11,10,9}};\n\nvector<vector<pair<int,int>>> Share(6);\n\nvector<vector<bool>> Rotate(vector<vector<bool>> B) {\n vector<vector<bool>> Ret(N,vector<bool>(N));\n rep(i,0,N) {\n rep(j,0,N) {\n Ret[N-1-j][i] = B[i][j];\n }\n }\n return Ret;\n}\n\nvector<vector<bool>> Flip(vector<vector<bool>> B) {\n vector<vector<bool>> Ret(N,vector<bool>(N));\n rep(i,0,N) {\n rep(j,0,N) {\n Ret[N-1-i][j] = B[i][j];\n }\n }\n return Ret;\n}\n\nbool DFS(int Level, vector<bool>& used, vector<pair<int,int>>& Cur, int Ver) { //Cur[i] : surface i ni piece first muki second\n if (Level == 6) {\n if (Ver == 255) return true;\n else return false;\n }\n rep(i,0,6) {\n if (used[i]) continue;\n rep(j,0,8) {\n bool check = true;\n if ((Ver & PV[i][Level][j]) != 0) check = false;\n for (pair<int,int> k : Share[Level]) {\n if ((PE[i][Level][j][k.first] ^ PE[Cur[k.second].first][k.second][Cur[k.second].second][k.first]) != MAX) check = false;\n }\n if (!check) continue;\n used[i] = true;\n Cur.push_back({i,j});\n if (DFS(Level+1,used,Cur,Ver|PV[i][Level][j])) return true;\n used[i] = false;\n Cur.pop_back();\n }\n }\n return false;\n}\n\nint main() {\nShare[0] = {}, Share[1] = {{0,0}}, Share[2] = {{1,0},{5,1}}, Share[3] = {{2,0},{6,2}}, Share[4] = {{3,0},{4,1},{7,3}}, Share[5] = {{8,1},{9,2},{10,3},{11,4}};\nwhile(1) {\n cin >> N;\n if (N == 0) return 0;\n rep(i,0,6) rep(j,0,6) rep(k,0,8) PV[i][j][k] = 0;\n rep(i,0,6) rep(j,0,6) rep(k,0,8) rep(l,0,12) PE[i][j][k][l] = 0;\n MAX = (1<<(N-2)) - 1;\n rep(i,0,6) {\n vector<vector<bool>> Piece(N,vector<bool>(N));\n rep(j,0,N) {\n string S;\n cin >> S;\n rep(k,0,N) {\n if (S[k] == '.') Piece[j][k] = 0;\n else Piece[j][k] = 1;\n }\n }\n rep(j,0,4) {\n rep(k,0,6) {\n if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);\n if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);\n if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);\n if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);\n rep(l,1,N-1) {\n if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));\n if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));\n if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));\n if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));\n }\n }\n Piece = Rotate(Piece);\n }\n Piece = Flip(Piece);\n rep(j,4,8) {\n rep(k,0,6) {\n if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);\n if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);\n if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);\n if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);\n rep(l,1,N-1) {\n if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));\n if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));\n if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));\n if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));\n }\n }\n Piece = Rotate(Piece);\n }\n }\n vector<bool> used(6,false);\n vector<pair<int,int>> Cur = {};\n used[0] = true;\n Cur.push_back({0,0});\n bool ANS = DFS(1,used,Cur,PV[0][0][0]);\n cout << (ANS ? \"Yes\" : \"No\") << endl;\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3516, "score_of_the_acc": -0.5812, "final_rank": 9 }, { "submission_id": "aoj_1636_9408511", "code_snippet": "#if 1\n// clang-format off\n#include <bits/stdc++.h>\n\nusing namespace std;\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing lf = long double;\nusing pll = pair<ll, ll>;\n#define vec vector\ntemplate <class T> using v = vector<T>;\ntemplate <class T> using vv = v<v<T>>;\ntemplate <class T> using vvv = v<vv<T>>;\nusing vl = v<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vvv<ll>;\nusing vpl = v<pll>;\nusing vs = v<string>;\nusing vb = v<bool>;\nusing vvb = v<vb>;\nusing vvvb = v<vvb>;\ntemplate<class T> using PQ = priority_queue<T, v<T>, greater<T>>;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }\n\n#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)\n#define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++)\n#define rrep(i, N) for (ll i = N - 1; i >= 0; i--)\n#define rrep1(i, N) for (ll i = N; i > 0; i--)\n#define fore(i, a) for (auto &i : a)\n#define fs first\n#define sc second\n#define eb emplace_back\n#define pb push_back\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define UNIQUE(x) (x).erase(unique((x).begin(), (x).end()), (x).end());\n#define YES(x) cout << ((x) ? \"YES\\n\" : \"NO\\n\");\n#define Yes(x) cout << ((x) ? \"Yes\\n\" : \"No\\n\");\n#define yes(x) cout << ((x) ? \"yes\\n\" : \"no\\n\");\ntemplate <class T, class U> void chmin(T &t, const U &u) { if (t > u) t = u; }\ntemplate <class T, class U> void chmax(T &t, const U &u) { if (t < u) t = u; }\ntemplate <class T> T min(const v<T> &lis) { return *min_element(all(lis)); }\ntemplate <class T> T max(v<T> &lis) { return *max_element(all(lis)); }\nconst int inf = (1 << 30);\nconst ll infl = (1LL << 60);\nconst ll mod93 = 998244353;\nconst ll mod17 = 1000000007;\nint popcnt(uint x) { return __builtin_popcount(x); }\nint popcnt(ull x) { return __builtin_popcountll(x); }\n// 桁数\nint bsr(uint x) { return 31 - __builtin_clz(x); }\nint bsr(ull x) { return 63 - __builtin_clzll(x); }\n// 2で割れる回数\nint bsf(uint x) { return __builtin_ctz(x); }\nint bsf(ull x) { return __builtin_ctzll(x); }\n\ntemplate <class T, class S> istream &operator>>(istream &is, pair<T, S> &x) { return is >> x.first >> x.second; }\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &x) { return os << x.first << \" \" << x.second; }\ntemplate <class T> istream &operator>>(istream &is, vector<T> &x) { for (auto &y : x) is >> y; return is; }\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &x) {\n for (size_t i = 0, size = x.size(); i < size; i++)\n os << x[i] << (i == size - 1 ? \"\" : \" \");\n return os;\n}\n\nll rand_int(ll l, ll r) { // [l, r]\n static random_device rd;\n static mt19937 gen(rd());\n return uniform_int_distribution<ll>(l, r)(gen);\n}\n\n// #include <boost/multiprecision/cpp_int.hpp>\n// using cpp_int = boost::multiprecision::cpp_int;\n\n// clang-format on\n#endif\n\n// #define _GLIBCXX_DEBUG\n\nconstexpr int N_MAX = 9;\nint n, m;\nusing Piece = array<array<char, N_MAX>, N_MAX>;\nPiece x[6];\nPiece y[6][2][4];\nPiece Piece_rotate(Piece p) {\n Piece ret;\n rep(i, n) rep(j, n) { ret[j][n - 1 - i] = p[i][j]; }\n return ret;\n}\nPiece Piece_reverse(Piece p) {\n Piece ret;\n rep(i, n) rep(j, n) ret[i][j] = p[j][i];\n return ret;\n}\nvoid print(const Piece &p) {\n rep(i, n) rep(j, n) cout << p[i][j] << \" \\n\"[j + 1 == n];\n}\nusing State = array<array<array<char, N_MAX>, N_MAX>, N_MAX>;\n\nvoid print(const State &s) {\n rep(i, n) {\n cout << \"i: \" << i << \"\\n\";\n rep(j, n) {\n rep(k, n) { cout << s[k][i][j] << \" \\n\"[k + 1 == n]; }\n }\n }\n}\nState e() {\n State ret;\n rep(i, n) rep(j, n) rep(k, n) ret[i][j][k] = '.';\n return ret;\n}\n\npair<bool, State> put(const State &s, const Piece &p, int i) {\n array<array<int, 9>, 6> d = {\n array<int, 9>{ 0, 0, 0, 1, 0, 0, 0, 1, 0},\n array<int, 9>{ 0, 0, 0, 1, 0, 0, 0, 0, 1},\n array<int, 9>{n - 1, 0, 0, 0, 1, 0, 0, 0, 1},\n array<int, 9>{n - 1, n - 1, 0, -1, 0, 0, 0, 0, 1},\n array<int, 9>{ 0, n - 1, 0, 0, -1, 0, 0, 0, 1},\n array<int, 9>{ 0, 0, n - 1, 1, 0, 0, 0, 1, 0},\n };\n State ret = s;\n int sx = d[i][0];\n int sy = d[i][1];\n int sz = d[i][2];\n // cout << \"i, sx, sy, sz: \" << i << \" \" << sx << \" \" << sy << \" \" << sz <<\n // endl;\n rep(j, n) {\n rep(k, n) {\n int x = sx + j * d[i][3] + k * d[i][6];\n int y = sy + j * d[i][4] + k * d[i][7];\n int z = sz + j * d[i][5] + k * d[i][8];\n // cout << \"i, x, y, z: \" << i << \" \" << x << \" \" << y << \" \" << z <<\n // endl;\n if (ret[x][y][z] == 'X' && p[j][k] == 'X') {\n return {true, ret};\n }\n if (ret[x][y][z] == '.' && p[j][k] == 'X')\n ret[x][y][z] = p[j][k];\n }\n }\n // cout << \"i: \" << i << \"\\n\";\n // cout << \"s\\n\";\n // print(s);\n // cout << \"p\\n\";\n // print(p);\n // cout << \"ret\\n\";\n // print(ret);\n return {false, ret};\n}\n\nbool ans = false;\nvoid dfs(int i, bitset<6> used, State s) {\n if (i == 0) {\n State ns = put(s, y[0][0][0], 0).sc;\n dfs(1, 1, ns);\n return;\n }\n if (i == 6) {\n ans = true;\n // print(s);\n return;\n }\n rep(j, 6) {\n if (used[j]) continue;\n rep(reverse_num, 2) rep(rotate_num, 4) {\n if (ans) return;\n auto [ng, ns] = put(s, y[j][reverse_num][rotate_num], i);\n if (ng) continue;\n dfs(i + 1, used | bitset<6>(1 << j), ns);\n }\n }\n}\nvoid solve() {\n m = n * n * n;\n m -= (n - 2) * (n - 2) * (n - 2);\n int cnt = 0;\n rep(i, 6) {\n rep(j, n) {\n rep(k, n) {\n cin >> x[i][j][k];\n cnt += x[i][j][k] == 'X';\n }\n }\n y[i][0][0] = x[i];\n rep(j, 3) y[i][0][j + 1] = Piece_rotate(y[i][0][j]);\n rep(j, 4) y[i][1][j] = Piece_reverse(y[i][0][j]);\n }\n if (cnt != m) {\n Yes(false);\n return;\n }\n\n ans = false;\n State tmp = e();\n dfs(0, bitset<6>(0), tmp);\n Yes(ans);\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n\n while (true) {\n cin >> n;\n if (n == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3336, "score_of_the_acc": -0.2025, "final_rank": 1 }, { "submission_id": "aoj_1636_9406954", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool is_end = false;\n\nint N;\nusing cover_t = vector<vector<vector< vector<array<int, 3>> >>>;\nusing blocks_t = vector<vector<vector< int >>>;\n\nvoid rotate(vector<vector<int>> &vec)\n{\n vector<vector<int>> tmp = vec;\n for (int i = 0; i < N; ++i)\n {\n for (int j = 0; j < N; ++j)\n {\n vec[i][j] = tmp[j][N-1-i];\n }\n }\n return;\n}\n\nvoid rever(vector<vector<int>> &vec)\n{\n vector<vector<int>> tmp = vec;\n for (int i = 0; i < N; ++i)\n {\n for (int j = 0; j < N; ++j)\n {\n vec[i][j] = tmp[N-1-i][j];\n }\n }\n return;\n}\n\nvoid deb(blocks_t &blocks, cover_t &cover)\n{\n for (int z = 0; z < N; ++z)\n {\n for (int x = 0; x < N; ++x)\n {\n for (int y = 0; y < N; ++y)\n {\n int sum = 0;\n for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];\n cout << sum << \" \";\n }\n cout << endl;\n }\n }\n cout << \"-------\" << endl;\n \n return;\n}\n\nbool check_all(blocks_t &blocks, cover_t &cover)\n{\n for (int x = 0; x < N; ++x)\n {\n for (int y = 0; y < N; ++y)\n {\n for (int z = 0; z < N; ++z)\n {\n bool isin = (1 <= min({x, y, z}) && max({x, y, z}) < N-1);\n int sum = 0;\n for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];\n if (isin && sum != 0) return false;\n if (!isin && sum != 1) return false;\n }\n }\n }\n \n return true;\n}\n\nbool check_mid(blocks_t &blocks, cover_t &cover)\n{\n // low\n {\n int z = 0;\n for (int x = 0; x < N; ++x)\n {\n for (int y = 0; y < N; ++y)\n {\n int sum = 0;\n for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n }\n }\n }\n // middle\n for (int z = 1; z < N-1; ++z)\n {\n {\n int sum = 0;\n for (int x = 0; x < N; ++x)\n {\n sum = 0;\n for (auto [id, px, py] : cover[x][0][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n \n sum = 0;\n for (auto [id, px, py] : cover[x][N-1][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n }\n for (int y = 0; y < N; ++y)\n {\n sum = 0;\n for (auto [id, px, py] : cover[0][y][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n \n sum = 0;\n for (auto [id, px, py] : cover[N-1][y][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n }\n }\n }\n \n return true;\n}\n\nbool check_bottom(int depth, blocks_t &blocks, cover_t &cover)\n{\n if (depth == 5) return check_mid(blocks, cover);\n \n bool ret = true;\n if (2 <= depth)\n {\n int x = 1, y = 0, z = 0;\n for (x = 1; x < N-1; ++x)\n {\n int sum = 0;\n for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n }\n }\n if (3 <= depth)\n {\n int x = N-1, y = 0, z = 0;\n for (y = 0; y < N-1; ++y)\n {\n int sum = 0;\n for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n }\n }\n if (4 <= depth)\n {\n int x = N-1, y = N-1, z = 0;\n for (x = N-1; x > 0; --x)\n {\n int sum = 0;\n for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];\n if (sum != 1) return false;\n }\n }\n \n return true;\n}\n\nmt19937 engine(42);\n\nbool dfs(int depth, blocks_t &blocks, cover_t &cover)\n{\n if (depth == 6)\n {\n return check_all(blocks, cover);\n }\n \n if (!check_bottom(depth, blocks, cover)) return false;\n \n bool ret = false;\n for (int rev = 0; rev < 2; ++rev)\n {\n for (int ro = 0; ro < 4; ++ro)\n {\n ret |= dfs(depth + 1, blocks, cover);\n if (ret) return true;\n \n rotate(blocks[depth]);\n }\n rever(blocks[depth]);\n }\n \n return ret;\n}\n\n\n\nvoid solve()\n{\n cin >> N;\n if (N == 0)\n {\n is_end = true;\n return;\n }\n vector blocks(6, vector(N, vector<int>(N, 0)));\n int Xsum = 0;\n for (int i = 0; i < 6; ++i)\n {\n for (int j = 0; j < N; ++j)\n {\n string S; cin >> S;\n for (int k = 0; k < N; ++k)\n {\n if (S[k] == 'X') blocks[i][k][N-1-j] = 1, Xsum++;\n }\n }\n }\n \n vector cover(N, vector(N, vector(N, vector<array<int, 3>>())));\n vector< array<array<int, 3>, 3> > diff;\n {\n // (sx, sy, sz), (dx, dy, dz), (dx, dy, dz)\n array<int, 3> ar0, ar1, ar2;\n array< array<int, 3>, 3> arr;\n \n ar0 = {0, 0, 0}, ar1 = {1, 0, 0}, ar2 = {0, 1, 0};\n diff.push_back({ar0, ar1, ar2});\n ar0 = {0, 0, 0}, ar1 = {1, 0, 0}, ar2 = {0, 0, 1};\n diff.push_back({ar0, ar1, ar2});\n ar0 = {N-1, 0, 0}, ar1 = {0, 1, 0}, ar2 = {0, 0, 1};\n diff.push_back({ar0, ar1, ar2});\n ar0 = {N-1, N-1, 0}, ar1 = {-1, 0, 0}, ar2 = {0, 0, 1};\n diff.push_back({ar0, ar1, ar2});\n ar0 = {0, N-1, 0}, ar1 = {0, -1, 0}, ar2 = {0, 0, 1};\n diff.push_back({ar0, ar1, ar2});\n ar0 = {0, 0, N-1}, ar1 = {1, 0, 0}, ar2 = {0, 1, 0};\n diff.push_back({ar0, ar1, ar2});\n }\n vector<array<int, 3>> outer;\n for (int i = 0; i < 6; ++i)\n {\n auto [s, dx, dy] = diff[i];\n for (int x = 0; x < N; ++x)\n {\n for (int y = 0; y < N; ++y)\n {\n int nx = s[0] + dx[0] * x + dy[0] * y;\n int ny = s[1] + dx[1] * x + dy[1] * y;\n int nz = s[2] + dx[2] * x + dy[2] * y;\n assert(0 <= min({nx, ny, nz}) && max({nx, ny, nz}) < N);\n cover[nx][ny][nz].push_back({i, x, y});\n outer.push_back({nx, ny, nz});\n }\n }\n }\n \n // for (int x = 0; x < N; ++x)\n // {\n // for (int y = 0; y < N; ++y)\n // {\n // for (int z = 0; z < N; ++z)\n // {\n // if (N == 3)\n // {\n // cout << x << \" \" << y << \" \" << z << \" \" << cover[x][y][z].size() << endl;\n // for (auto [i, px, py] : cover[x][y][z])\n // {\n // cout << i << \" \" << px << \" \" << py << endl;\n // }\n // cout << \"--------\" << endl;\n // }\n // }\n // }\n // }\n sort(outer.begin(), outer.end());\n outer.erase(unique(outer.begin(), outer.end()), outer.end());\n if (Xsum != (int)outer.size())\n {\n cout << \"No\" << endl;\n return;\n }\n \n vector<int> P(6);\n iota(P.begin(), P.end(), 0);\n \n bool res = false;\n do\n {\n if (P[0] != 0) break;\n vector<vector<vector<int>>> copy = blocks;\n for (int i = 0; i < 6; ++i)\n {\n copy[i] = blocks[P[i]];\n }\n \n for (int rev = 0; rev < 2; ++rev)\n {\n res |= dfs(1, copy, cover);\n rever(copy[0]);\n }\n if (res) break;\n \n } while (next_permutation(P.begin(), P.end()));\n \n cout << (res ? \"Yes\" : \"No\") << endl;\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3376, "score_of_the_acc": -0.2968, "final_rank": 3 }, { "submission_id": "aoj_1636_9390464", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, n) for(int i = a; i < n; i++)\n#define rrep(i, a, n) for(int i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nint main(){\n\n const int m = 6;\n auto calc_triple = [&](vector<pair<int, int>> p, int num, int width) -> vector<tuple<int, int, int>> {\n vector<tuple<int, int, int>> res(p.size());\n int n = width;\n rep(i, 0, p.size()){\n auto [r, c] = p[i];\n if(num == 0){\n // x = 0\n res[i] = {0, r, c};\n }else if(num == 1){\n // x = n-1\n res[i] = {n-1, r, c};\n }else if(num == 2){\n // y = 0\n res[i] = {r, 0, c};\n }else if(num == 3){\n // y = n-1\n res[i] = {r, n-1, c};\n }else if(num == 4){\n // z = 0\n res[i] = {n-1-c, r, 0};\n }else if(num == 5){\n // z = n-1\n res[i] = {n-1-c, r, n-1};\n }\n }\n return res;\n };\n while(true){\n int n; cin >> n;\n if(n == 0) break;\n\n vector<vector<pair<int, int>>> p(m);\n rep(i, 0, m){\n rep(j, 0, n){\n string s; cin >> s;\n rep(k, 0, n){\n if(s[k] == 'X'){\n p[i].push_back({n-1-j, k});\n }\n }\n }\n }\n\n // vector<vector<vector<tuple<int, int, int>>>> t(m, vector<vector<tuple<int, int, int>>>(m));\n // rep(i, 0, m){\n // rep(j, 0, m){\n // t[i][j] = calc_triple(p[i], j, n);\n // }\n // }\n\n vector<int> q(m);\n iota(q.begin(), q.end(), 0);\n \n\n bool ans = false;\n vector<vector<vector<bool>>> ok(n, vector<vector<bool>>(n, vector<bool>(n)));\n auto f = [&](auto self, int pos, vector<int> q_) -> bool {\n if(pos == m){\n bool flag = true;\n rep(i, 0, n){\n rep(j, 0, n){\n if(!ok[i][j][0]) flag = false;\n if(!ok[i][j][n-1]) flag = false;\n if(!ok[i][0][j]) flag = false;\n if(!ok[i][n-1][j]) flag = false;\n if(!ok[0][i][j]) flag = false;\n if(!ok[n-1][i][j]) flag = false;\n if(!flag) break;\n }\n if(!flag) break;\n }\n return flag;\n }\n bool res = false;\n rep(_, 0, 2){\n rep(__, 0, 4){\n bool flag = true;\n vector<tuple<int, int, int>> t = calc_triple(p[pos], q[pos], n);\n rep(i, 0, t.size()){\n auto [x, y, z] = t[i];\n if(ok[x][y][z]){\n flag = false;\n break;\n }\n }\n if(flag){\n rep(i, 0, t.size()){\n auto [x, y, z] = t[i];\n ok[x][y][z] = 1;\n }\n res |= self(self, pos+1, q_);\n if(res) return true;\n rep(i, 0, t.size()){\n auto [x, y, z] = t[i];\n ok[x][y][z] = 0;\n }\n }\n rep(i, 0, p[pos].size()){\n p[pos][i] = {n-1-p[pos][i].second, p[pos][i].first};\n }\n }\n rep(i, 0, p[pos].size()){\n p[pos][i] = {n-1-p[pos][i].first, p[pos][i].second};\n }\n }\n return res;\n };\n do{\n if(q[0] != 0) break;\n\n ans |= f(f, 0, q);\n if(ans) break;\n }while(next_permutation(q.begin(), q.end()));\n if(!ans) cout << \"No\" << endl;\n else cout << \"Yes\" << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 6120, "memory_kb": 3244, "score_of_the_acc": -0.9012, "final_rank": 17 }, { "submission_id": "aoj_1636_9379126", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\n#define rep(i, n) for (long long i = 0; i < (n); i++)\n#define rep1(i, n) for (long long i = 1; i <= (n); i++)\n#define all(v) v.begin(), v.end()\n#define decimal(n) cout << fixed << setprecision(n);\nusing namespace std;\n// using namespace atcoder;\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; \n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>;\nusing vll = vl;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>; using vvc = vector<vc>;\nconst ll MAX = 2000100; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, 0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {0, 1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\n\nll n;\nvector<P> ijpairs;\n\nvoid rotate(vs &piece) {\n vs next(n);\n rep(i, n) {\n next[i] = string(n, 'X');\n }\n for(auto [i, j] : ijpairs) {\n next[j][n - 1 - i] = piece[i][j];\n }\n swap(piece, next);\n}\n\nvoid flip(vs &piece) {\n vs next(n);\n rep(i, n) {\n next[i] = string(n, 'X');\n }\n for(auto [i, j] : ijpairs) {\n next[n - 1 - i][j] = piece[i][j];\n }\n swap(piece, next);\n}\n\nbool pieceset(vs &piece, vector<vs> &cube, ll x, ll y, ll z) {\n if(z != -1) {\n ll k = z;\n for(auto [i, j] : ijpairs) {\n if(piece[i][j] == 'X') {\n if(cube[i][j][k] == 'X') {\n return false;\n }\n cube[i][j][k] = piece[i][j];\n }\n }\n }\n else if(x != -1) {\n ll i = x;\n for(auto [j, k] : ijpairs) {\n if(piece[j][k] == 'X') {\n if(cube[i][j][k] == 'X') {\n return false;\n }\n cube[i][j][k] = piece[j][k];\n }\n }\n }\n else {\n ll j = y;\n for(auto [i, k] : ijpairs) {\n if(piece[i][k] == 'X') {\n if(cube[i][j][k] == 'X') {\n return false;\n }\n cube[i][j][k] = piece[i][k];\n }\n }\n }\n return true;\n}\n\nbool solve() {\n ijpairs.clear();\n rep(i, n) {\n rep(j, n) {\n if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n ijpairs.emplace_back(make_pair(i, j));\n }\n }\n }\n\n \n // ピースのうけとりと単位立方体の数カウント\n ll PIECECOUNT = 6;\n vector<vector<string>> masterpieces(PIECECOUNT);\n ll cnt = 0;\n rep(pid, PIECECOUNT) {\n masterpieces[pid].resize(n);\n rep(i, n) cin >> masterpieces[pid][i];\n rep(i, n) {\n rep(j, n) {\n cnt += (masterpieces[pid][i][j] == 'X');\n }\n }\n }\n if(cnt != 2 * n * n + 4 * (n - 2) * (n - 1)) {\n return false;\n }\n\n rep(baseflip, 2) {\n // キューブの初期化\n vector<vs> mastercube(n, vs(n));\n rep(i, n) {\n rep(j, n) {\n mastercube[i][j] = string(n, '.');\n }\n }\n\n vector<vector<ll>> corbase = {{-1, 0, -1}, {-1, -1, 0}, {n - 1, -1, -1}, {-1, n - 1, -1}, {-1, -1, n - 1}, {0, -1, -1}};\n if(baseflip) flip(masterpieces.back());\n pieceset(masterpieces.back(), mastercube, 0, -1, -1);\n\n vl piece_order(PIECECOUNT - 1);\n iota(all(piece_order), 0LL);\n\n do {\n // ピースを一つずつ当てはめていく\n for(ll statebit = 0; statebit < (1LL << (3 * (PIECECOUNT - 1))); statebit++) {\n vector<vs> cube = mastercube;\n bool flag = true;\n rep(piece_id, PIECECOUNT - 1) {\n auto piece = masterpieces[piece_order[piece_id]];\n ll shiftbit = 3 * (PIECECOUNT - 2 - piece_id);\n\n // 回転\n ll nowstate = (statebit >> shiftbit); \n ll rottime = nowstate & 3;\n rep(t, rottime) {\n rotate(piece);\n }\n\n // 反転\n if(nowstate & 4) {\n flip(piece);\n }\n\n // セットして確認\n ll x = corbase[piece_id][0];\n ll y = corbase[piece_id][1];\n ll z = corbase[piece_id][2];\n if(!pieceset(piece, cube, x, y, z)) {\n flag = false;\n statebit = ((statebit >> shiftbit) << shiftbit) + ((1LL << shiftbit) - 1);\n break;\n }\n }\n if(flag) {\n return true;\n }\n }\n } while(next_permutation(all(piece_order)));\n }\n return false;\n}\n\nint main() {\n while(true) {\n cin >> n;\n if(n == 0) break;\n cout << (solve() ? \"Yes\" : \"No\") << endl;\n }\n}", "accuracy": 1, "time_ms": 1630, "memory_kb": 3352, "score_of_the_acc": -0.4697, "final_rank": 7 }, { "submission_id": "aoj_1636_9379121", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\n#define rep(i, n) for (long long i = 0; i < (n); i++)\n#define rep1(i, n) for (long long i = 1; i <= (n); i++)\n#define all(v) v.begin(), v.end()\n#define decimal(n) cout << fixed << setprecision(n);\nusing namespace std;\n// using namespace atcoder;\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; \n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>;\nusing vll = vl;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>; using vvc = vector<vc>;\nconst ll MAX = 2000100; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, 0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {0, 1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\n\nll n;\nvector<P> ijpairs;\n\nvoid rotate(vs &piece) {\n vs next(n);\n rep(i, n) {\n next[i] = string(n, 'X');\n }\n for(auto [i, j] : ijpairs) {\n next[j][n - 1 - i] = piece[i][j];\n }\n swap(piece, next);\n}\n\nvoid flip(vs &piece) {\n vs next(n);\n rep(i, n) {\n next[i] = string(n, 'X');\n }\n for(auto [i, j] : ijpairs) {\n next[n - 1 - i][j] = piece[i][j];\n }\n swap(piece, next);\n}\n\nbool pieceset(vs &piece, vector<vs> &cube, ll x, ll y, ll z) {\n if(z != -1) {\n ll k = z;\n for(auto [i, j] : ijpairs) {\n if(piece[i][j] == 'X') {\n if(cube[i][j][k] == 'X') {\n return false;\n }\n cube[i][j][k] = piece[i][j];\n }\n }\n }\n else if(x != -1) {\n ll i = x;\n for(auto [j, k] : ijpairs) {\n if(piece[j][k] == 'X') {\n if(cube[i][j][k] == 'X') {\n return false;\n }\n cube[i][j][k] = piece[j][k];\n }\n }\n }\n else {\n ll j = y;\n for(auto [i, k] : ijpairs) {\n if(piece[i][k] == 'X') {\n if(cube[i][j][k] == 'X') {\n return false;\n }\n cube[i][j][k] = piece[i][k];\n }\n }\n }\n return true;\n}\n\nbool solve() {\n ijpairs.clear();\n rep(i, n) {\n rep(j, n) {\n if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {\n ijpairs.emplace_back(make_pair(i, j));\n }\n }\n }\n\n \n // ピースのうけとりと単位立方体の数カウント\n ll PIECECOUNT = 6;\n vector<vector<string>> masterpieces(PIECECOUNT);\n ll cnt = 0;\n rep(pid, PIECECOUNT) {\n masterpieces[pid].resize(n);\n rep(i, n) cin >> masterpieces[pid][i];\n rep(i, n) {\n rep(j, n) {\n cnt += (masterpieces[pid][i][j] == 'X');\n }\n }\n }\n if(cnt != 2 * n * n + 4 * (n - 2) * (n - 1)) {\n return false;\n }\n\n // キューブの初期化\n vector<vs> mastercube(n, vs(n));\n rep(i, n) {\n rep(j, n) {\n mastercube[i][j] = string(n, '.');\n }\n }\n\n vector<vector<ll>> corbase = {{-1, 0, -1}, {-1, -1, 0}, {n - 1, -1, -1}, {-1, n - 1, -1}, {-1, -1, n - 1}, {0, -1, -1}};\n pieceset(masterpieces.back(), mastercube, 0, -1, -1);\n\n vl piece_order(PIECECOUNT - 1);\n iota(all(piece_order), 0LL);\n\n do {\n // ピースを一つずつ当てはめていく\n for(ll statebit = 0; statebit < (1LL << (3 * (PIECECOUNT - 1))); statebit++) {\n vector<vs> cube = mastercube;\n bool flag = true;\n rep(piece_id, PIECECOUNT - 1) {\n auto piece = masterpieces[piece_order[piece_id]];\n ll shiftbit = 3 * (PIECECOUNT - 2 - piece_id);\n\n // 回転\n ll nowstate = (statebit >> shiftbit); \n ll rottime = nowstate & 3;\n rep(t, rottime) {\n rotate(piece);\n }\n\n // 反転\n if(nowstate & 4) {\n flip(piece);\n }\n\n // セットして確認\n ll x = corbase[piece_id][0];\n ll y = corbase[piece_id][1];\n ll z = corbase[piece_id][2];\n if(!pieceset(piece, cube, x, y, z)) {\n flag = false;\n statebit = ((statebit >> shiftbit) << shiftbit) + ((1LL << shiftbit) - 1);\n break;\n }\n }\n if(flag) {\n return true;\n }\n }\n } while(next_permutation(all(piece_order)));\n\n // ベースのピースをflipしてもう一回\n rep(i, n) {\n rep(j, n) {\n mastercube[i][j] = string(n, '.');\n }\n }\n flip(masterpieces.back());\n pieceset(masterpieces.back(), mastercube, 0, -1, -1);\n\n\n iota(all(piece_order), 0LL);\n\n do {\n // ピースを一つずつ当てはめていく\n for(ll statebit = 0; statebit < (1LL << (3 * (PIECECOUNT - 1))); statebit++) {\n vector<vs> cube = mastercube;\n bool flag = true;\n rep(piece_id, PIECECOUNT - 1) {\n auto piece = masterpieces[piece_order[piece_id]];\n ll shiftbit = 3 * (PIECECOUNT - 2 - piece_id);\n\n // 回転\n ll nowstate = (statebit >> shiftbit); \n ll rottime = nowstate & 3;\n rep(t, rottime) {\n rotate(piece);\n }\n\n // 反転\n if(nowstate & 4) {\n flip(piece);\n }\n\n // セットして確認\n ll x = corbase[piece_id][0];\n ll y = corbase[piece_id][1];\n ll z = corbase[piece_id][2];\n if(!pieceset(piece, cube, x, y, z)) {\n flag = false;\n statebit = ((statebit >> shiftbit) << shiftbit) + ((1LL << shiftbit) - 1);\n break;\n }\n }\n if(flag) {\n return true;\n }\n }\n } while(next_permutation(all(piece_order)));\n\n return false;\n}\n\nint main() {\n while(true) {\n cin >> n;\n if(n == 0) break;\n cout << (solve() ? \"Yes\" : \"No\") << endl;\n }\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 3408, "score_of_the_acc": -0.5835, "final_rank": 10 } ]
aoj_1639_cpp
Addition on Convex Polygons Mr. Convexman likes convex polygons very much. During his study on convex polygons, he has come up with the following elegant addition on convex polygons. The sum of two points in the xy -plane, ( x 1 , y 1 ) and ( x 2 , y 2 ) is defined as ( x 1 + x 2 , y 1 + y 2 ). A polygon is treated as the set of all the points on its boundary and in its inside. Here, the sum of two polygons S 1 and S 2 , S 1 + S 2 , is defined as the set of all the points v 1 + v 2 satisfying v 1 ∈ S 1 and v 2 ∈ S 2 . Sums of two convex polygons, thus defined, are always convex polygons! The multiplication of a non-negative integer and a polygon is defined inductively by 0 S = {(0, 0)} and, for a positive integer k , kS = ( k - 1) S + S . In this problem, line segments and points are also considered as convex polygons. Mr. Convexman made many convex polygons based on two convex polygons P and Q to study properties of the addition, but one day, he threw away by mistake the piece of paper on which he had written down the vertex positions of the two polygons and the calculation process. What remains at hand are only the vertex positions of two convex polygons R and S that are represented as linear combinations of P and Q with non-negative integer coefficients: R = aP + bQ , and S = cP + dQ . Fortunately, he remembers that the coordinates of all vertices of P and Q are integers and the coefficients a , b , c and d satisfy ad - bc = 1. Mr. Convexman has requested you, a programmer and his friend, to make a program to recover P and Q from R and S , that is, he wants you to, for given convex polygons R and S , compute non-negative integers a , b , c , d ( ad - bc = 1) and two convex polygons P and Q with vertices on integer points satisfying the above equation. The equation may have many solutions. Make a program to compute the minimum possible sum of the areas of P and Q satisfying the equation. Input The input consists of at most 40 datasets, each in the following format. n m x 1 y 1 ... x n y n x' 1 y' 1 ... x' m y' m n is the number of vertices of the convex polygon R , and m is that of S . n and m are integers and satisfy 3 ≤ n ≤ 1000 and 3 ≤ m ≤ 1000. ( x i , y i ) represents the coordinates of the i -th vertex of the convex polygon R and ( x' i , y' i ) represents that of S . x i , y i , x' i and y' i are integers between −10 6 and 10 6 , inclusive. The vertices of each convex polygon are given in counterclockwise order. Three different vertices of one convex polygon do not lie on a single line. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing an integer representing the double of the sum of the areas of P and Q . Note that the areas are always integers when doubled because the coordinates of each vertex of P and Q are integers. Sample Input 5 3 0 0 2 0 2 1 1 2 0 2 0 0 1 0 0 1 4 4 0 0 5 0 5 5 0 5 0 0 2 0 2 2 0 2 3 3 0 0 1 0 0 1 0 1 0 0 1 1 3 3 0 0 1 0 1 1 0 0 1 0 1 1 4 4 0 ...(truncated)
[ { "submission_id": "aoj_1639_10849688", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\ntypedef long long LL;\ntypedef pair<LL,LL> pll;\n\npll add(const pll &a, const pll &b){\n\treturn pll(a.first + b.first, a.second + b.second);\n}\n\npll sub(const pll &a, const pll &b){\n\treturn pll(a.first - b.first, a.second - b.second);\n}\n\npll mul(LL t, const pll &a){\n\treturn pll(a.first * t, a.second * t);\n}\n\nLL cross(const pll &a, const pll &b){\n\treturn a.first * b.second - b.first * a.second;\n}\n\nLL div(const pll &a, const pll &b){\n\tif(b.first != 0){ return a.first / b.first; }\n\treturn a.second / b.second;\n}\n\nvector<pll> input(int n){\n\tvector<pll> pts(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tcin >> pts[i].first >> pts[i].second;\n\t}\n\trotate(pts.begin(), min_element(pts.begin(), pts.end()), pts.end());\n\n\tvector<pll> vec(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tvec[i] = sub(pts[i + 1], pts[i]);\n\t}\n\tvec[n - 1] = sub(pts[0], pts[n - 1]);\n\treturn vec;\n}\n\nLL area(const vector<pll> &v){\n\tint n = v.size();\n\tpll p;\n\tLL s = 0;\n\tfor(int i = 0; i < n; ++i){\n\t\tpll q = add(p, v[i]);\n\t\ts += cross(p, q);\n\t\tp = q;\n\t}\n\treturn s;\n}\n\nint main(){\n\tint n, m;\n\twhile(cin >> n >> m && n){\n\t\tvector<pll> v1 = input(n);\n\t\tvector<pll> v2 = input(m);\n\t\tif(area(v1) > area(v2)){ v1.swap(v2); }\n\t\tvector<int> idxs;\n\t\twhile(true){\n\t\t\tint s1 = v1.size(), s2 = v2.size();\n\t\t\tidxs.assign(s1, -1);\n\t\t\tint j = 0;\n\t\t\tLL u = 1LL << 50;\n\t\t\tfor(int i = 0; i < s1; ++i){\n\t\t\t\tfor(; j < s2; ++j){\n\t\t\t\t\tif(cross(v1[i], v2[j]) == 0){ break; }\n\t\t\t\t}\n\t\t\t\tif(j >= s2){\n\t\t\t\t\tu = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tidxs[i] = j;\n\t\t\t\tLL t = div(v2[j], v1[i]);\n\t\t\t\tu = min(t, u);\n\t\t\t\t++j;\n\t\t\t}\n\t\t\tif(u <= 0){ break; }\n\t\t\tfor(int i = 0; i < s1; ++i){\n\t\t\t\tint k = idxs[i];\n\t\t\t\tv2[k] = sub(v2[k], mul(u, v1[i]));\n\t\t\t}\n\t\t\tv2.erase(remove(v2.begin(), v2.end(), pll()), v2.end());\n\t\t\tv1.swap(v2);\n\t\t}\n\t\tLL a1 = area(v1);\n\t\tLL a2 = area(v2);\n\t\tcout << (a1 + a2) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3400, "score_of_the_acc": -0.0018, "final_rank": 3 }, { "submission_id": "aoj_1639_10532574", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n#define rep2(i, j, k) for(ll i=ll(j);i<ll(k);i++)\n#define rep(i, j) rep2(i,0,j)\n#define rrep2(i, j, k) for(ll i=ll(j)-1;i>=ll(k);i--)\n#define rrep(i, j) rrep2(i,j,0)\n#define eb emplace_back\n#define all(a) a.begin(),a.end()\n#define SZ(a) ll(a.size())\nusing ll = long long;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing P = pair<ll, ll>;\nusing vp = vector<P>;\nusing vvp = vector<vp>;\n\ntemplate<class T>\nbool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }\n\ntemplate<class T>\nbool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }\n\nconst ll inf = LLONG_MAX / 4;\n\npair<P, ll> decomp(ll x, ll y) {\n assert(x or y);\n ll g = gcd(abs(x), abs(y));\n return {P(x/g, y/g), g};\n}\n\nll det(P a, P b) {\n return a.first * b.second - a.second * b.first;\n}\n\nbool cmp(P a, P b) {\n assert(a.first or a.second);\n assert(b.first or b.second);\n int ax = (a.second > 0 or (!a.second and a.first > 0));\n int bx = (b.second > 0 or (!b.second and b.first > 0));\n if(ax != bx) return ax < bx;\n return det(a, b) > 0;\n}\n\nP add(P a, P b) {\n return {a.first + b.first, a.second + b.second};\n}\n\nvector<vector<pair<P, P>>> path(P f) {\n assert(f.first >= 0 and f.second >= 0 and (f.first or f.second));\n assert(gcd(f.first, f.second) == 1);\n if(!f.first or !f.second) return {};\n P l = {0, 1}, r = {1, 0};\n vector<vector<pair<P, P>>> res;\n res.eb();\n res.back().eb(l, r);\n P m = add(l, r);\n while(m != f) {\n res.eb();\n if(cmp(m, f)) { // m > f\n do {\n r = m;\n m = add(l, r);\n res.back().eb(l, r);\n } while(cmp(m, f));\n } else { // m < f\n do {\n l = m;\n m = add(l, r);\n res.back().eb(l, r);\n } while(cmp(f, m));\n }\n }\n return res;\n}\n\nvp vecs;\nvl r, s;\n\nll area(const vl &v) {\n assert(SZ(v) == SZ(vecs));\n ll res = 0;\n ll x = 0, y = 0;\n rep(i, SZ(v)) {\n ll nx = x + vecs[i].first * v[i];\n ll ny = y + vecs[i].second * v[i];\n res += det(P(x, y), P(nx, ny));\n x = nx;\n y = ny;\n }\n assert(!x and !y);\n return res;\n}\n\nvoid solve(int n, int m) {\n vecs.clear();\n map<P, P> cnt;\n rep(t, 2) {\n vl x(n+1), y(n+1);\n rep(i, n) cin >> x[i] >> y[i];\n x[n] = x[0];\n y[n] = y[0];\n rep(i, n) {\n auto [p, c] = decomp(x[i+1]-x[i], y[i+1]-y[i]);\n vecs.eb(p);\n if(!t) cnt[p].first += c;\n else cnt[p].second += c;\n }\n swap(n, m);\n }\n sort(all(vecs), cmp);\n vecs.erase(unique(all(vecs)), vecs.end());\n int sz = SZ(vecs);\n r.assign(sz, 0);\n s.assign(sz, 0);\n vp f(sz);\n rep(i, sz) {\n r[i] = cnt[vecs[i]].first;\n s[i] = cnt[vecs[i]].second;\n f[i] = decomp(r[i], s[i]).first;\n }\n sort(f.rbegin(), f.rend(), cmp);\n if(f[0] == f.back()) {\n vl v(sz);\n rep(i, sz) v[i] = r[i] / f[0].first;\n cout << area(v) << '\\n';\n return;\n }\n ll ans = inf;\n auto calc = [&](pair<P, P> p) -> ll {\n if(cmp(p.first, f[0])) return inf;\n if(cmp(f.back(), p.second)) return inf;\n auto [b, d] = p.first;\n auto [a, c] = p.second;\n assert(a * d - b * c == 1);\n vl P(sz), Q(sz);\n rep(i, sz) {\n P[i] = d * r[i] - b * s[i];\n Q[i] = a * s[i] - c * r[i];\n assert(P[i] >= 0 and Q[i] >= 0);\n }\n return area(P) + area(Q);\n };\n chmin(ans, calc({P(0, 1), P(1, 0)}));\n for(const auto &v : path(f[0])) {\n assert(SZ(v) >= 1);\n rep(i, SZ(v)) chmin(ans, calc(v[i]));\n }\n for(const auto &v : path(f.back())) {\n assert(SZ(v) >= 1);\n rep(i, SZ(v)) chmin(ans, calc(v[i]));\n }\n if(det(f[0], f.back()) == -1) chmin(ans, calc({f[0], f.back()}));\n assert(ans != inf);\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n, m;\n while(cin >> n >> m, n) solve(n, m);\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 29048, "score_of_the_acc": -0.2588, "final_rank": 7 }, { "submission_id": "aoj_1639_9685751", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<int> Vint;\ntypedef std::vector<bool> Vbool;\ntypedef std::vector<ld> Vld;\nconst ll INF = 1e17;\nconst int LEN = 1e5 + 1;\nconst ld TOL = 1e-7;\nconst ll MOD = 1'000'000'007;\nint sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\nbool zero(const ld& x) { return !sign(x); }\nll sq(int x) { return (ll)x * x; }\nll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }\nint gcd(int a, int b) { return !b ? a : gcd(b, a % b); }\n\nint T, N, M;\nstruct Pos {\n\tint x, y, i;\n\tPos(int X = 0, int Y = 0, int I = 0) : x(X), y(Y), i(I) {}\n\tbool operator == (const Pos& p) const { return x == p.x && y == p.y; }\n\tbool operator != (const Pos& p) const { return x != p.x || y != p.y; }\n\t//bool operator < (const Pos& p) const { return x == p.x ? y < p.y : x < p.x; }\n\tbool operator < (const Pos& p) const { return x == p.x ? y == p.y ? i > p.i : y < p.y : x < p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const int& n) const { return { x * n, y * n }; }\n\tPos operator / (const int& n) const { return { x / n, y / n }; }\n\tll operator * (const Pos& p) const { return (ll)x * p.x + (ll)y * p.y; }\n\tll operator / (const Pos& p) const { return (ll)x * p.y - (ll)y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const int& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const int& scale) { x /= scale; y /= scale; return *this; }\n\tll Euc() const { return (ll)x * x + (ll)y * y; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nll cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nll cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nll dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nll dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ll ret = cross(d1, d2, d3); return ret < 0 ? -1 : !!ret; }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { ll ret = cross(d1, d2, d3, d4); return ret < 0 ? -1 : !!ret; }\nbool same_dir(const Pos& p, const Pos& q) { return p * q >= 0 && p / q == 0; }\nll area(const Polygon& H) {\n\tll ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nvoid norm(Polygon& H) {\n\tll A = area(H);\n\tif (A < 0) std::reverse(H.begin(), H.end());\n\treturn;\n}\nvoid fit(Polygon& H) {\n\tauto p = std::min_element(H.begin(), H.end());\n\tstd::rotate(H.begin(), p, H.end());\n\treturn;\n}\nbool convex(Polygon& H) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i0 = (i - 1 + sz) % sz, i1 = i, i2 = (i + 1) % sz;\n\t\tif (ccw(H[i0], H[i1], H[i2]) <= 0) return 0;\n\t}\n\treturn 1;\n}\nbool same_polygon(Polygon& R, Polygon& S) {\n\tfit(R); fit(S);\n\tint sz = R.size();\n\tif (sz != S.size()) return 0;\n\tPos v = R[0] - S[0];\n\tfor (Pos& p : S) p += v;\n\tfor (int i = 0; i < sz; i++) if (R[i] != S[i]) return 0;\n\treturn 1;\n}\nPolygon graham_scan(Polygon& C) {\n\tPolygon H;\n\tif (C.size() < 3) {\n\t\tstd::sort(C.begin(), C.end());\n\t\treturn C;\n\t}\n\tstd::swap(C[0], *min_element(C.begin(), C.end()));\n\tstd::sort(C.begin() + 1, C.end(), [&](const Pos& p, const Pos& q) -> bool {\n\t\tint ret = ccw(C[0], p, q);\n\t\tif (!ret) return (C[0] - p).Euc() < (C[0] - q).Euc();\n\t\treturn ret > 0;\n\t\t}\n\t);\n\tC.erase(unique(C.begin(), C.end()), C.end());\n\tint sz = C.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\twhile (H.size() >= 2 && ccw(H[H.size() - 2], H.back(), C[i]) <= 0)\n\t\t\tH.pop_back();\n\t\tH.push_back(C[i]);\n\t}\n\treturn H;\n}\nbool minkowski_difference(Polygon& R, Polygon& S) {\n\tll ar = area(R);\n\tll as = area(S);\n\tif (ar == as) {\n\t\tif (same_polygon(R, S)) S.clear();\n\t\treturn 0;\n\t}\n\tif (ar > as) std::swap(R, S);\n\tif (R.size() > S.size()) return 0;\n\tint sz = R.size();\n\tVbool F(sz, 0);\n\tPolygon V;\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos v = R[(i + 1) % sz] - R[i];\n\t\tint gcd_ = gcd(std::abs(v.x), std::abs(v.y));\n\t\tv /= gcd_;\n\t\tv.i = i;\n\t\tV.push_back(v);\n\t}\n\tsz = S.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos v = S[(i + 1) % sz] - S[i];\n\t\tint gcd_ = gcd(std::abs(v.x), std::abs(v.y));\n\t\tv /= gcd_;\n\t\tv.i = ~i;\n\t\tV.push_back(v);\n\t}\n\tint r = -1, s = -1;\n\tstd::sort(V.begin(), V.end());\n\tsz = V.size();\n\tfor (int i = 0; i < sz - 1; i++) {\n\t\tif (V[i] == V[i + 1]) {\n\t\t\tassert(V[i].i >= 0); assert(V[i + 1].i < 0);\n\t\t\tif (!~r) r = V[i].i, s = ~V[i + 1].i;\n\t\t\tF[V[i].i] = 1;\n\t\t}\n\t}\n\tif (!~r) return 0;\n\tsz = F.size();\n\tfor (int i = 0; i < sz; i++) if (!F[i]) return 0;\n\n\tPolygon H;\n\tH.push_back(Pos(0, 0));\n\tint n = R.size(), m = S.size();\n\tfor (int _ = 0; _ < n + m; _++) {\n\t\tint r1 = (r + 1) % n;\n\t\tint s1 = (s + 1) % m;\n\t\tPos vr = R[r1] - R[r];\n\t\tPos sr = S[s1] - S[s];\n\t\tPos cur = H.back();\n\t\tassert(vr.Euc()); assert(sr.Euc());\n\t\tif (same_dir(vr, sr)) {\n\t\t\tif (vr.Euc() > sr.Euc()) return 0;\n\t\t\tr = (r + 1) % n;\n\t\t\ts = (s + 1) % m;\n\t\t\tif (vr.Euc() == sr.Euc()) continue;\n\t\t\tcur += sr - vr;\n\t\t}\n\t\telse {\n\t\t\ts = (s + 1) % m;\n\t\t\tcur += sr;\n\t\t}\n\t\tH.push_back(cur);\n\t\tif (H.back() == H[0]) break;\n\t}\n\n\tif (H.back() != H[0]) return 0;\n\tif (H.back() == H[0]) H.pop_back();\n\tS = graham_scan(H);\n\treturn 1;\n}\nbool query() {\n\tstd::cin >> N >> M;\n\tif (!N && !M) return 0;\n\tPolygon R(N), S(M);\n\tfor (Pos& p : R) std::cin >> p;\n\tfor (Pos& p : S) std::cin >> p;\n\tnorm(R), norm(S);\n\tassert(convex(R));\n\tassert(convex(S));\n\twhile (minkowski_difference(R, S));\n\tstd::cout << area(R) + area(S) << \"\\n\";\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\t//freopen(\"../../../input_data/H_addition/H3\", \"r\", stdin);\n\t//freopen(\"../../../input_data/H_addition/H_ret.txt\", \"w\", stdout);\n\twhile (query());\n\treturn;\n}\nint main() { solve(); return 0; }//boj17584 Addition on Convex Polygons", "accuracy": 1, "time_ms": 1060, "memory_kb": 3404, "score_of_the_acc": -0.9831, "final_rank": 8 }, { "submission_id": "aoj_1639_9685747", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<int> Vint;\ntypedef std::vector<bool> Vbool;\ntypedef std::vector<ld> Vld;\nconst ll INF = 1e17;\nconst int LEN = 1e5 + 1;\nconst ld TOL = 1e-7;\nconst ll MOD = 1'000'000'007;\nint sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\nbool zero(const ld& x) { return !sign(x); }\nll sq(int x) { return (ll)x * x; }\nll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }\nint gcd(int a, int b) { return !b ? a : gcd(b, a % b); }\n\nint T, N, M;\nstruct Pos {\n\tint x, y, i;\n\tPos(int X = 0, int Y = 0, int I = 0) : x(X), y(Y), i(I) {}\n\tbool operator == (const Pos& p) const { return x == p.x && y == p.y; }\n\tbool operator != (const Pos& p) const { return x != p.x || y != p.y; }\n\t//bool operator < (const Pos& p) const { return x == p.x ? y < p.y : x < p.x; }\n\tbool operator < (const Pos& p) const { return x == p.x ? y == p.y ? i > p.i : y < p.y : x < p.x; }\n\tbool operator <= (const Pos& p) const { return x == p.x ? y <= p.y : x <= p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const int& n) const { return { x * n, y * n }; }\n\tPos operator / (const int& n) const { return { x / n, y / n }; }\n\tll operator * (const Pos& p) const { return (ll)x * p.x + (ll)y * p.y; }\n\tll operator / (const Pos& p) const { return (ll)x * p.y - (ll)y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const int& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const int& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tll xy() const { return (ll)x * y; }\n\tll Euc() const { return (ll)x * x + (ll)y * y; }\n\tint Man() const { return std::abs(x) + std::abs(y); }\n\tld mag() const { return hypot(x, y); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return y > 0 || y == 0 && x >= 0; }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nll cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nll cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nll dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nll dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ll ret = cross(d1, d2, d3); return ret < 0 ? -1 : !!ret; }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { ll ret = cross(d1, d2, d3, d4); return ret < 0 ? -1 : !!ret; }\nbool same_dir(const Pos& p, const Pos& q) { return p * q >= 0 && p / q == 0; }\nll area(const Polygon& H) {\n\tll ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nvoid norm(Polygon& H) {\n\tll A = area(H);\n\tif (A < 0) std::reverse(H.begin(), H.end());\n\treturn;\n}\nvoid fit(Polygon& H) {\n\tauto p = std::min_element(H.begin(), H.end());\n\tstd::rotate(H.begin(), p, H.end());\n\treturn;\n}\nbool convex(Polygon& H) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i0 = (i - 1 + sz) % sz, i1 = i, i2 = (i + 1) % sz;\n\t\tif (ccw(H[i0], H[i1], H[i2]) <= 0) return 0;\n\t}\n\treturn 1;\n}\nbool same_polygon(Polygon& R, Polygon& S) {\n\tfit(R); fit(S);\n\tint sz = R.size();\n\tif (sz != S.size()) return 0;\n\tPos v = R[0] - S[0];\n\tfor (Pos& p : S) p += v;\n\tfor (int i = 0; i < sz; i++) if (R[i] != S[i]) return 0;\n\treturn 1;\n}\nPolygon graham_scan(Polygon& C) {\n\tPolygon H;\n\tif (C.size() < 3) {\n\t\tstd::sort(C.begin(), C.end());\n\t\treturn C;\n\t}\n\tstd::swap(C[0], *min_element(C.begin(), C.end()));\n\tstd::sort(C.begin() + 1, C.end(), [&](const Pos& p, const Pos& q) -> bool {\n\t\tint ret = ccw(C[0], p, q);\n\t\tif (!ret) return (C[0] - p).Euc() < (C[0] - q).Euc();\n\t\treturn ret > 0;\n\t\t}\n\t);\n\tC.erase(unique(C.begin(), C.end()), C.end());\n\tint sz = C.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\twhile (H.size() >= 2 && ccw(H[H.size() - 2], H.back(), C[i]) <= 0)\n\t\t\tH.pop_back();\n\t\tH.push_back(C[i]);\n\t}\n\treturn H;\n}\nbool minkovski_difference(Polygon& R, Polygon& S) {\n\tll ar = area(R);\n\tll as = area(S);\n\tif (ar == as) {\n\t\tif (same_polygon(R, S)) { S.clear(); return 0; }\n\t\treturn 0;\n\t}\n\tif (ar > as) std::swap(R, S);\n\tif (R.size() > S.size()) return 0;\n\tint sz = R.size();\n\tVbool F(sz, 0);\n\tPolygon V;\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos v = R[(i + 1) % sz] - R[i];\n\t\tint gcd_ = gcd(std::abs(v.x), std::abs(v.y));\n\t\tv.x /= gcd_; v.y /= gcd_;\n\t\tv.i = i;\n\t\tV.push_back(v);\n\t}\n\tsz = S.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos v = S[(i + 1) % sz] - S[i];\n\t\tint gcd_ = gcd(std::abs(v.x), std::abs(v.y));\n\t\tv.x /= gcd_; v.y /= gcd_;\n\t\tv.i = ~i;\n\t\tV.push_back(v);\n\t}\n\tint r = -1, s = -1;\n\tstd::sort(V.begin(), V.end());\n\tsz = V.size();\n\tfor (int i = 0; i < sz - 1; i++) {\n\t\tif (V[i] == V[i + 1]) {\n\t\t\tassert(V[i].i >= 0); assert(V[i + 1].i < 0);\n\t\t\tif (!~r) r = V[i].i, s = ~V[i + 1].i;\n\t\t\tF[V[i].i] = 1;\n\t\t}\n\t}\n\tif (!~r) return 0;\n\tsz = F.size();\n\tfor (int i = 0; i < sz; i++) if (!F[i]) return 0;\n\n\tPolygon H;\n\tH.push_back(Pos(0, 0));\n\tint n = R.size(), m = S.size();\n\tfor (int _ = 0; _ < n + m; _++) {\n\t\tint r1 = (r + 1) % n;\n\t\tint s1 = (s + 1) % m;\n\t\tPos vr = R[r1] - R[r];\n\t\tPos sr = S[s1] - S[s];\n\t\tPos cur = H.back();\n\t\tassert(vr.Euc()); assert(sr.Euc());\n\t\tif (same_dir(vr, sr)) {\n\t\t\tif (vr.Euc() > sr.Euc()) return 0;\n\t\t\tr = (r + 1) % n;\n\t\t\ts = (s + 1) % m;\n\t\t\tif (vr.Euc() == sr.Euc()) continue;\n\t\t\tcur += sr - vr;\n\t\t}\n\t\telse {\n\t\t\ts = (s + 1) % m;\n\t\t\tcur += sr;\n\t\t}\n\t\tH.push_back(cur);\n\t\tif (H.back() == H[0]) break;\n\t}\n\n\tif (H.back() != H[0]) return 0;\n\tif (H.back() == H[0]) H.pop_back();\n\tS = graham_scan(H);\n\treturn 1;\n}\nbool query() {\n\tstd::cin >> N >> M;\n\tif (!N && !M) return 0;\n\tPolygon R(N), S(M);\n\tfor (Pos& p : R) std::cin >> p;\n\tfor (Pos& p : S) std::cin >> p;\n\tnorm(R), norm(S);\n\tassert(convex(R));\n\tassert(convex(S));\n\twhile (minkovski_difference(R, S));\n\tstd::cout << area(R) + area(S) << \"\\n\";\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\t//freopen(\"../../../input_data/H_addition/H3\", \"r\", stdin);\n\t//freopen(\"../../../input_data/H_addition/H_ret.txt\", \"w\", stdout);\n\twhile (query());\n\treturn;\n}\nint main() { solve(); return 0; }//boj17584 Addition on Convex Polygons", "accuracy": 1, "time_ms": 1060, "memory_kb": 3408, "score_of_the_acc": -0.9832, "final_rank": 9 }, { "submission_id": "aoj_1639_9685744", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <random>\n#include <array>\n#include <tuple>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<int> Vint;\ntypedef std::vector<bool> Vbool;\ntypedef std::vector<ld> Vld;\nconst ll INF = 1e17;\nconst int LEN = 1e5 + 1;\nconst ld TOL = 1e-7;\nconst ll MOD = 1'000'000'007;\nint sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\nbool zero(const ld& x) { return !sign(x); }\nll sq(int x) { return (ll)x * x; }\nll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }\nint gcd(int a, int b) { return !b ? a : gcd(b, a % b); }\n\nint T, N, M;\nstruct Pos {\n\tint x, y, i;\n\tPos(int X = 0, int Y = 0, int I = 0) : x(X), y(Y), i(I) {}\n\tbool operator == (const Pos& p) const { return x == p.x && y == p.y; }\n\tbool operator != (const Pos& p) const { return x != p.x || y != p.y; }\n\t//bool operator < (const Pos& p) const { return x == p.x ? y < p.y : x < p.x; }\n\tbool operator < (const Pos& p) const { return x == p.x ? y == p.y ? i > p.i : y < p.y : x < p.x; }\n\tbool operator <= (const Pos& p) const { return x == p.x ? y <= p.y : x <= p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const int& n) const { return { x * n, y * n }; }\n\tPos operator / (const int& n) const { return { x / n, y / n }; }\n\tll operator * (const Pos& p) const { return (ll)x * p.x + (ll)y * p.y; }\n\tll operator / (const Pos& p) const { return (ll)x * p.y - (ll)y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const int& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const int& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tll xy() const { return (ll)x * y; }\n\tll Euc() const { return (ll)x * x + (ll)y * y; }\n\tint Man() const { return std::abs(x) + std::abs(y); }\n\tld mag() const { return hypot(x, y); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return y > 0 || y == 0 && x >= 0; }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nll cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nll cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nll dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nll dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ll ret = cross(d1, d2, d3); return ret < 0 ? -1 : !!ret; }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { ll ret = cross(d1, d2, d3, d4); return ret < 0 ? -1 : !!ret; }\nbool same_dir(const Pos& p, const Pos& q) { return p * q >= 0 && p / q == 0; }\nll area(const Polygon& H) {\n\tll ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nvoid norm(Polygon& H) {\n\tll A = area(H);\n\tif (A < 0) std::reverse(H.begin(), H.end());\n\treturn;\n}\nvoid fit(Polygon& H) {\n\tauto p = std::min_element(H.begin(), H.end());\n\tstd::rotate(H.begin(), p, H.end());\n\treturn;\n}\nbool convex(Polygon& H) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i0 = (i - 1 + sz) % sz, i1 = i, i2 = (i + 1) % sz;\n\t\tif (ccw(H[i0], H[i1], H[i2]) <= 0) return 0;\n\t}\n\treturn 1;\n}\nbool same_polygon(Polygon& R, Polygon& S) {\n\tfit(R); fit(S);\n\tint sz = R.size();\n\tif (sz != S.size()) return 0;\n\tPos v = R[0] - S[0];\n\tfor (Pos& p : S) p += v;\n\tfor (int i = 0; i < sz; i++) if (R[i] != S[i]) return 0;\n\treturn 1;\n}\nPolygon graham_scan(Polygon& C) {\n\tPolygon H;\n\tif (C.size() < 3) {\n\t\tstd::sort(C.begin(), C.end());\n\t\treturn C;\n\t}\n\tstd::swap(C[0], *min_element(C.begin(), C.end()));\n\tstd::sort(C.begin() + 1, C.end(), [&](const Pos& p, const Pos& q) -> bool {\n\t\tint ret = ccw(C[0], p, q);\n\t\tif (!ret) return (C[0] - p).Euc() < (C[0] - q).Euc();\n\t\treturn ret > 0;\n\t\t}\n\t);\n\tC.erase(unique(C.begin(), C.end()), C.end());\n\tint sz = C.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\twhile (H.size() >= 2 && ccw(H[H.size() - 2], H.back(), C[i]) <= 0)\n\t\t\tH.pop_back();\n\t\tH.push_back(C[i]);\n\t}\n\treturn H;\n}\nbool minkovski_difference(Polygon& R, Polygon& S) {\n\tif (same_polygon(R, S)) { S.clear(); return 0; }\n\tll ar = area(R);\n\tll as = area(S);\n\tif (ar == as) return 0;\n\tif (ar > as) std::swap(R, S);\n\tif (R.size() > S.size()) return 0;\n\tint sz = R.size();\n\tVbool F(sz, 0);\n\tPolygon V;\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos v = R[(i + 1) % sz] - R[i];\n\t\tint gcd_ = gcd(std::abs(v.x), std::abs(v.y));\n\t\tv.x /= gcd_; v.y /= gcd_;\n\t\tv.i = i;\n\t\tV.push_back(v);\n\t}\n\tsz = S.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos v = S[(i + 1) % sz] - S[i];\n\t\tint gcd_ = gcd(std::abs(v.x), std::abs(v.y));\n\t\tv.x /= gcd_; v.y /= gcd_;\n\t\tv.i = ~i;\n\t\tV.push_back(v);\n\t}\n\tint r = -1, s = -1;\n\tstd::sort(V.begin(), V.end());\n\tsz = V.size();\n\tfor (int i = 0; i < sz - 1; i++) {\n\t\tif (V[i] == V[i + 1]) {\n\t\t\tassert(V[i].i >= 0); assert(V[i + 1].i < 0);\n\t\t\tif (!~r) r = V[i].i, s = ~V[i + 1].i;\n\t\t\tF[V[i].i] = 1;\n\t\t}\n\t}\n\tif (!~r) return 0;\n\tsz = F.size();\n\tfor (int i = 0; i < sz; i++) if (!F[i]) return 0;\n\n\tPolygon H;\n\tH.push_back(Pos(0, 0));\n\tint n = R.size(), m = S.size();\n\tfor (int _ = 0; _ < n + m; _++) {\n\t\tint r1 = (r + 1) % n;\n\t\tint s1 = (s + 1) % m;\n\t\tPos vr = R[r1] - R[r];\n\t\tPos sr = S[s1] - S[s];\n\t\tPos cur = H.back();\n\t\tassert(vr.Euc()); assert(sr.Euc());\n\t\tif (same_dir(vr, sr)) {\n\t\t\tif (vr.Euc() > sr.Euc()) return 0;\n\t\t\tr = (r + 1) % n;\n\t\t\ts = (s + 1) % m;\n\t\t\tif (vr.Euc() == sr.Euc()) continue;\n\t\t\tcur += sr - vr;\n\t\t}\n\t\telse {\n\t\t\ts = (s + 1) % m;\n\t\t\tcur += sr;\n\t\t}\n\t\tH.push_back(cur);\n\t\tif (H.back() == H[0]) break;\n\t}\n\n\tif (H.back() != H[0]) return 0;\n\tif (H.back() == H[0]) H.pop_back();\n\tS = graham_scan(H);\n\t//std::cout << \"DEBUG:: \" << area(R) << \" \" << area(S) << \"\\n\";\n\treturn 1;\n}\nbool query() {\n\tstd::cin >> N >> M;\n\tif (!N && !M) return 0;\n\tPolygon R(N), S(M);\n\tfor (Pos& p : R) std::cin >> p;\n\tfor (Pos& p : S) std::cin >> p;\n\tnorm(R), norm(S);\n\tassert(convex(R));\n\tassert(convex(S));\n\t//std::cout << \"DEBUG:: \" << area(R) << \" \" << area(S) << \"\\n\";\n\twhile (minkovski_difference(R, S));\n\tstd::cout << area(R) + area(S) << \"\\n\";\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\t//freopen(\"../../../input_data/H_addition/H3\", \"r\", stdin);\n\t//freopen(\"../../../input_data/H_addition/H_ret.txt\", \"w\", stdout);\n\twhile (query());\n\treturn;\n}\nint main() { solve(); return 0; }//boj17584 Addition on Convex Polygons", "accuracy": 1, "time_ms": 1080, "memory_kb": 3496, "score_of_the_acc": -1.0025, "final_rank": 10 }, { "submission_id": "aoj_1639_7140930", "code_snippet": "#line 1 \"library/my_template.hpp\"\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing pi = pair<ll, ll>;\nusing vi = vector<ll>;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i128 = __int128;\n\ntemplate <class T>\nusing vc = vector<T>;\ntemplate <class T>\nusing vvc = vector<vc<T>>;\ntemplate <class T>\nusing vvvc = vector<vvc<T>>;\ntemplate <class T>\nusing vvvvc = vector<vvvc<T>>;\ntemplate <class T>\nusing vvvvvc = vector<vvvvc<T>>;\ntemplate <class T>\nusing pq = priority_queue<T>;\ntemplate <class T>\nusing pqg = priority_queue<T, vector<T>, greater<T>>;\n\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) \\\n vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) \\\n vector<vector<vector<type>>> name( \\\n h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name( \\\n a, vector<vector<vector<type>>>( \\\n b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n\n// https://trap.jp/post/1224/\n#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)\n#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)\n#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)\n#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))\n#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)\n#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)\n#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)\n#define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c))\n#define overload4(a, b, c, d, e, ...) e\n#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)\n#define FOR_R(...) \\\n overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)\n\n#define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s))\n#define all(x) x.begin(), x.end()\n#define len(x) ll(x.size())\n#define elif else if\n\n#define eb emplace_back\n#define mp make_pair\n#define mt make_tuple\n#define fi first\n#define se second\n\n#define stoi stoll\n\ntemplate <typename T, typename U>\nT SUM(const vector<U> &A) {\n T sum = 0;\n for (auto &&a: A) sum += a;\n return sum;\n}\n\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate <typename T>\nT pick(deque<T> &que) {\n T a = que.front();\n que.pop_front();\n return a;\n}\n\ntemplate <typename T>\nT pick(pq<T> &que) {\n T a = que.top();\n que.pop();\n return a;\n}\n\ntemplate <typename T>\nT pick(pqg<T> &que) {\n assert(que.size());\n T a = que.top();\n que.pop();\n return a;\n}\n\ntemplate <typename T>\nT pick(vc<T> &que) {\n assert(que.size());\n T a = que.back();\n que.pop_back();\n return a;\n}\n\ntemplate <typename T, typename U>\nT ceil(T x, U y) {\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\n\ntemplate <typename T, typename U>\nT floor(T x, U y) {\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <typename T, typename U>\npair<T, T> divmod(T x, U y) {\n T q = floor(x, y);\n return {q, x - q * y};\n}\n\ntemplate <typename F>\nll binary_search(F check, ll ok, ll ng) {\n assert(check(ok));\n while (abs(ok - ng) > 1) {\n auto x = (ng + ok) / 2;\n tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));\n }\n return ok;\n}\n\ntemplate <typename F>\ndouble binary_search_real(F check, double ok, double ng, int iter = 100) {\n FOR(iter) {\n double x = (ok + ng) / 2;\n tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));\n }\n return (ok + ng) / 2;\n}\n\ntemplate <class T, class S>\ninline bool chmax(T &a, const S &b) {\n return (a < b ? a = b, 1 : 0);\n}\ntemplate <class T, class S>\ninline bool chmin(T &a, const S &b) {\n return (a > b ? a = b, 1 : 0);\n}\n\nvc<int> s_to_vi(const string &S, char first_char) {\n vc<int> A(S.size());\n FOR(i, S.size()) { A[i] = S[i] - first_char; }\n return A;\n}\n\ntemplate <typename T, typename U>\nvector<T> cumsum(vector<U> &A, int off = 1) {\n int N = A.size();\n vector<T> B(N + 1);\n FOR(i, N) { B[i + 1] = B[i] + A[i]; }\n if (off == 0) B.erase(B.begin());\n return B;\n}\n\ntemplate <typename CNT, typename T>\nvc<CNT> bincount(const vc<T> &A, int size) {\n vc<CNT> C(size);\n for (auto &&x: A) { ++C[x]; }\n return C;\n}\n\n// stable\ntemplate <typename T>\nvector<int> argsort(const vector<T> &A) {\n vector<int> ids(A.size());\n iota(all(ids), 0);\n sort(all(ids),\n [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); });\n return ids;\n}\n\n// A[I[0]], A[I[1]], ...\ntemplate <typename T>\nvc<T> rearrange(const vc<T> &A, const vc<int> &I) {\n int n = len(I);\n vc<T> B(n);\n FOR(i, n) B[i] = A[I[i]];\n return B;\n}\n#line 1 \"library/other/io2.hpp\"\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n long double __VA_ARGS__; \\\n IN(__VA_ARGS__)\n\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n read(name)\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n read(name)\n\nvoid read(int &a) { cin >> a; }\nvoid read(long long &a) { cin >> a; }\nvoid read(char &a) { cin >> a; }\nvoid read(double &a) { cin >> a; }\nvoid read(long double &a) { cin >> a; }\nvoid read(string &a) { cin >> a; }\ntemplate <class T, class S> void read(pair<T, S> &p) { read(p.first), read(p.second); }\ntemplate <class T> void read(vector<T> &a) {for(auto &i : a) read(i);}\ntemplate <class T> void read(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n read(head);\n IN(tail...);\n}\n\ntemplate <typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& A) {\n os << A.fi << \" \" << A.se;\n return os;\n}\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& A) {\n for (size_t i = 0; i < A.size(); i++) {\n if(i) os << \" \";\n os << A[i];\n }\n return os;\n}\n\nvoid print() {\n cout << \"\\n\";\n cout.flush();\n}\n\ntemplate <class Head, class... Tail>\nvoid print(Head&& head, Tail&&... tail) {\n cout << head;\n if (sizeof...(Tail)) cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid YES(bool t = 1) { print(t ? \"YES\" : \"NO\"); }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { print(t ? \"Yes\" : \"No\"); }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { print(t ? \"yes\" : \"no\"); }\nvoid no(bool t = 1) { yes(!t); }\n#line 2 \"library/geo/base.hpp\"\ntemplate <typename T>\nstruct Point {\n T x, y;\n\n Point() = default;\n\n template <typename A, typename B>\n Point(A x, B y) : x(x), y(y) {}\n\n template <typename A, typename B>\n Point(pair<A, B> p) : x(p.fi), y(p.se) {}\n\n Point operator+(Point p) const { return {x + p.x, y + p.y}; }\n Point operator-(Point p) const { return {x - p.x, y - p.y}; }\n bool operator==(Point p) const { return x == p.x && y == p.y; }\n Point operator-() const { return {-x, -y}; }\n\n bool operator<(Point p) const {\n if (x != p.x) return x < p.x;\n return y < p.y;\n }\n\n T dot(Point other) { return x * other.x + y * other.y; }\n T det(Point other) { return x * other.y - y * other.x; }\n};\n\ntemplate <typename REAL, typename T>\nREAL dist(Point<T> A, Point<T> B) {\n A -= B;\n T p = A.dot(A);\n return sqrt(REAL(p));\n}\n\ntemplate <typename T>\nstruct Line {\n T a, b, c;\n\n Line(T a, T b, T c) : a(a), b(b), c(c) {}\n Line(Point<T> A, Point<T> B) {\n a = A.y - B.y;\n b = B.x - A.x;\n c = A.x * B.y - A.y * B.x;\n }\n Line(T x1, T y1, T x2, T y2) : Line(Point<T>(x1, y1), Point<T>(x2, y2)) {}\n\n template <typename U>\n U eval(Point<U> P) {\n return a * P.x + b * P.y + c;\n }\n\n template <typename U>\n T eval(U x, U y) {\n return a * x + b * y + c;\n }\n\n bool is_parallel(Line other) { return a * other.b - b * other.a == 0; }\n\n bool is_orthogonal(Line other) { return a * other.a + b * other.b == 0; }\n};\n\ntemplate <typename T>\nstruct Segment {\n Point<T> A, B;\n\n Segment(Point<T> A, Point<T> B) : A(A), B(B) {}\n Segment(T x1, T y1, T x2, T y2)\n : Segment(Point<T>(x1, y1), Point<T>(x2, y2)) {}\n\n Line<T> to_Line() { return Line(A, B); }\n};\n\ntemplate <typename T>\nstruct Circle {\n Point<T> O;\n T r;\n Circle(Point<T> O, T r) : O(O), r(r) {}\n Circle(T x, T y, T r) : O(Point<T>(x, y)), r(r) {}\n};\n\ntemplate <typename T>\nstruct Polygon {\n vc<Point<T>> points;\n T a;\n\n template <typename A, typename B>\n Polygon(vc<pair<A, B>> pairs) {\n for (auto&& [a, b]: pairs) points.eb(Point<T>(a, b));\n build();\n }\n Polygon(vc<Point<T>> points) : points(points) { build(); }\n\n int size() { return len(points); }\n\n template <typename REAL>\n REAL area() {\n return a * 0.5;\n }\n\n template <enable_if_t<is_integral<T>::value, int> = 0>\n T area_2() {\n return a;\n }\n\n bool is_convex() {\n FOR(j, len(points)) {\n int i = (j == 0 ? len(points) - 1 : j - 1);\n int k = (j == len(points) - 1 ? 0 : j + 1);\n if ((points[j] - points[i]).det(points[k] - points[j]) < 0) return false;\n }\n return true;\n }\n\nprivate:\n void build() {\n a = 0;\n FOR(i, len(points)) {\n int j = (i + 1 == len(points) ? 0 : i + 1);\n a += points[i].det(points[j]);\n }\n if (a < 0) {\n a = -a;\n reverse(all(points));\n }\n }\n};\n#line 2 \"library/geo/angle_sort.hpp\"\n\n// 偏角ソートに対する argsort\ntemplate <typename T>\nvector<int> angle_argsort(vector<Point<T>>& P) {\n vector<int> lower, origin, upper;\n const Point<T> O = {0, 0};\n FOR(i, len(P)) {\n if (P[i] == O) origin.eb(i);\n elif ((P[i].y < 0) || (P[i].y == 0 && P[i].x > 0)) lower.eb(i);\n else upper.eb(i);\n }\n sort(all(lower), [&](auto& i, auto& j) { return P[i].det(P[j]) > 0; });\n sort(all(upper), [&](auto& i, auto& j) { return P[i].det(P[j]) > 0; });\n auto& I = lower;\n I.insert(I.end(), all(origin));\n I.insert(I.end(), all(upper));\n return I;\n}\n\n// 偏角ソートに対する argsort\ntemplate <typename T>\nvector<int> angle_argsort(vector<pair<T, T>>& P) {\n vc<Point<T>> tmp(len(P));\n FOR(i, len(P)) tmp[i] = Point<T>(P[i]);\n return angle_argsort<T>(tmp);\n}\n\n// inplace に偏角ソートする\n// index が欲しい場合は angle_argsort\ntemplate <typename T>\nvoid angle_sort(vector<Point<T>>& P) {\n auto I = angle_argsort<T>(P);\n P = rearrange(P, I);\n}\n\n// inplace に偏角ソートする\n// index が欲しい場合は angle_argsort\ntemplate <typename T>\nvoid angle_sort(vector<pair<T, T>>& P) {\n auto I = angle_argsort<T>(P);\n P = rearrange(P, I);\n}\n#line 4 \"main.cpp\"\n\nvoid solve() {\n ll N, M;\n while (cin >> N >> M) {\n if (N == 0 && M == 0) break;\n VEC(pi, AB, N);\n VEC(pi, CD, M);\n AB.eb(AB[0]);\n CD.eb(CD[0]);\n\n map<pi, pi> CNT;\n FOR(i, N) {\n auto [a1, b1] = AB[i];\n auto [a2, b2] = AB[i + 1];\n ll a = a2 - a1, b = b2 - b1;\n ll g = gcd(a, b);\n pi v = {a / g, b / g};\n // ベクトル v が g 個\n CNT[v].fi += g;\n }\n FOR(i, M) {\n auto [a1, b1] = CD[i];\n auto [a2, b2] = CD[i + 1];\n ll a = a2 - a1, b = b2 - b1;\n ll g = gcd(a, b);\n pi v = {a / g, b / g};\n // ベクトル v が g 個\n CNT[v].se += g;\n }\n vc<pi> vecs;\n vi P, Q;\n for (auto&& [v, pq]: CNT) {\n vecs.eb(v);\n auto [p, q] = pq;\n P.eb(p);\n Q.eb(q);\n }\n auto I = angle_argsort(vecs);\n vecs = rearrange(vecs, I);\n P = rearrange(P, I);\n Q = rearrange(Q, I);\n\n while (1) {\n bool upd = 0;\n FOR(2) {\n swap(P, Q);\n const ll INF = 1LL << 60;\n ll k = INF;\n FOR(i, len(vecs)) {\n if (Q[i] == 0) continue;\n chmin(k, P[i] / Q[i]);\n }\n if (k > 0 && k < INF) upd = 1;\n FOR(i, len(vecs)) { P[i] -= k * Q[i]; }\n }\n if (!upd) break;\n }\n\n auto calc = [&](vi A) -> ll {\n ll area = 0;\n ll x = 0, y = 0;\n FOR(i, len(vecs)) {\n auto [dx, dy] = vecs[i];\n dx *= A[i];\n dy *= A[i];\n ll x1 = x + dx, y1 = y + dy;\n area += x * y1 - y * x1;\n x = x1, y = y1;\n }\n return area;\n };\n ll ANS = calc(P) + calc(Q);\n print(ANS);\n }\n}\n\nsigned main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << setprecision(15);\n\n solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3592, "score_of_the_acc": -0.0032, "final_rank": 6 }, { "submission_id": "aoj_1639_7140905", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i, n) for(ll i = 0; i < (n); i++)\nusing P = vector<pair<ll, ll>>;\n\nconstexpr int type(ll x, ll y) {\n if(x == 0 and y == 0) return 0;\n if(y < 0 or (y == 0 and x > 0)) return -1;\n return 1;\n}\n\nbool arg_cmp(pair<ll, ll> x, pair<ll, ll> y) {\n int L = type(x.first, x.second), R = type(y.first, y.second);\n if(L != R) return L < R;\n ll X = x.first * y.second, Y = y.first * x.second;\n if(X != Y) return X > Y;\n return x.first < y.first;\n}\n\nlong long solve(P &x, P &y, int second = 0) {\n bool flag = true;\n int j = 0;\n P ny;\n if(empty(x)) flag = false;\n for(auto &[a, b] : x) {\n while(j < y.size()) {\n auto &[c, d] = y[j];\n if(a * d == b * c and abs(a + b) <= abs(c + d)) { break; }\n ny.emplace_back(c, d);\n j++;\n }\n if(j == y.size()) {\n flag = false;\n break;\n }\n if(y[j].first - a or y[j].second - b) ny.emplace_back(y[j].first - a, y[j].second - b);\n j++;\n }\n while(j < y.size()) { ny.emplace_back(y[j++]); }\n if(flag)\n return solve(x, ny);\n else if(!second) { return solve(y, x, true); }\n\n ll res = 0;\n auto calc = [&](const P &x) {\n pair<ll, ll> p{};\n rep(i, x.size()) {\n pair<ll, ll> np(p.first + x[i].first, p.second + x[i].second);\n res += p.first * np.second - p.second * np.first;\n p = np;\n }\n };\n calc(x);\n calc(y);\n return res;\n}\n\nsigned main() {\n while(true) {\n int n, m;\n cin >> n >> m;\n if(!n) exit(0);\n auto get = [](int n) {\n P r(n);\n rep(i, n) cin >> r[i].first >> r[i].second;\n P res(n);\n rep(i, n) res[i] = pair(r[(i + 1) % n].first - r[i].first, r[(i + 1) % n].second - r[i].second);\n\n int k = min_element(begin(res), end(res), arg_cmp) - begin(res);\n rotate(begin(res), begin(res) + k, end(res));\n return pair(r, res);\n };\n auto [r, x] = get(n);\n auto [s, y] = get(m);\n\n ll res = solve(x, y);\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 143856, "score_of_the_acc": -1.1869, "final_rank": 14 }, { "submission_id": "aoj_1639_7140903", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define ll long long\nusing P = vector<pair<ll, ll>>;\n\nconstexpr int type(ll x, ll y) {\n if(x == 0 and y == 0) return 0;\n if(y < 0 or (y == 0 and x > 0)) return -1;\n return 1;\n}\n\nbool arg_cmp(pair<ll, ll> x, pair<ll, ll> y) {\n int L = type(x.first, x.second), R = type(y.first, y.second);\n if(L != R) return L < R;\n long long X = x.first * y.second, Y = y.first * x.second;\n if(X != Y) return X > Y;\n return x.first < y.first;\n}\n\nlong long solve(P &x, P &y, int second = 0) {\n bool flag = true;\n int j = 0;\n P ny;\n if(empty(x)) flag = false;\n for(auto &[a, b] : x) {\n while(j < y.size()) {\n auto &[c, d] = y[j];\n if(a * d == b * c and abs(a + b) <= abs(c + d)) { break; }\n ny.emplace_back(c, d);\n j++;\n }\n if(j == y.size()) {\n flag = false;\n break;\n }\n if(y[j].first - a or y[j].second - b) ny.emplace_back(y[j].first - a, y[j].second - b);\n j++;\n }\n while(j < y.size()) { ny.emplace_back(y[j++]); }\n if(flag)\n return solve(x, ny);\n else if(!second) { return solve(y, x, true); }\n\n ll res = 0;\n auto calc = [&](const P &x) {\n pair<ll, ll> p{};\n rep(i, x.size()) {\n pair<ll, ll> np(p.first + x[i].first, p.second + x[i].second);\n res += p.first * np.second - p.second * np.first;\n p = np;\n }\n };\n calc(x);\n calc(y);\n return res;\n}\n\nsigned main() {\n while(true) {\n int n, m;\n cin >> n >> m;\n if(!n) exit(0);\n auto get = [](int n) {\n P r(n);\n rep(i, n) cin >> r[i].first >> r[i].second;\n P res(n);\n rep(i, n) res[i] = pair(r[(i + 1) % n].first - r[i].first, r[(i + 1) % n].second - r[i].second);\n\n int k = min_element(begin(res), end(res), arg_cmp) - begin(res);\n rotate(begin(res), begin(res) + k, end(res));\n return pair(r, res);\n };\n auto [r, x] = get(n);\n auto [s, y] = get(m);\n\n int res = solve(x, y);\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 143744, "score_of_the_acc": -1.1861, "final_rank": 13 }, { "submission_id": "aoj_1639_7140899", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define rep(i, n) for(int i = 0; i < (n); i++)\nusing P = vector<pair<int, int>>;\n\nconstexpr int type(int x, int y) {\n if(x == 0 and y == 0) return 0;\n if(y < 0 or (y == 0 and x > 0)) return -1;\n return 1;\n}\n\nbool arg_cmp(pair<int, int> x, pair<int, int> y) {\n int L = type(x.first, x.second), R = type(y.first, y.second);\n if(L != R) return L < R;\n long long X = 1LL * x.first * y.second, Y = 1LL * y.first * x.second;\n if(X != Y) return X > Y;\n return x.first < y.first;\n}\n\nlong long solve(P &x, P &y, int second = 0) {\n bool flag = true;\n int j = 0;\n P ny;\n if(empty(x)) flag = false;\n for(auto &[a, b] : x) {\n while(j < y.size()) {\n auto &[c, d] = y[j];\n if(1LL * a * d == 1LL * b * c and abs(a + b) <= abs(c + d)) { break; }\n ny.emplace_back(c, d);\n j++;\n }\n if(j == y.size()) {\n flag = false;\n break;\n }\n if(y[j].first - a or y[j].second - b) ny.emplace_back(y[j].first - a, y[j].second - b);\n j++;\n }\n while(j < y.size()) { ny.emplace_back(y[j++]); }\n if(flag)\n return solve(x, ny);\n else if(!second) { return solve(y, x, true); }\n\n long long res = 0;\n auto calc = [&](const P &x) {\n pair<int, int> p{};\n rep(i, x.size()) {\n pair<int, int> np(p.first + x[i].first, p.second + x[i].second);\n res += 1LL * p.first * np.second - 1LL * p.second * np.first;\n p = np;\n }\n };\n calc(x);\n calc(y);\n return res;\n}\n\nsigned main() {\n while(true) {\n int n, m;\n cin >> n >> m;\n if(!n) exit(0);\n auto get = [](int n) {\n P r(n);\n rep(i, n) cin >> r[i].first >> r[i].second;\n P res(n);\n rep(i, n) res[i] = pair(r[(i + 1) % n].first - r[i].first, r[(i + 1) % n].second - r[i].second);\n\n int k = min_element(begin(res), end(res), arg_cmp) - begin(res);\n rotate(begin(res), begin(res) + k, end(res));\n return pair(r, res);\n };\n auto [r, x] = get(n);\n auto [s, y] = get(m);\n\n int res = solve(x, y);\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 143484, "score_of_the_acc": -1.1749, "final_rank": 12 }, { "submission_id": "aoj_1639_7140893", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define rep(i, n) for(int i = 0; i < (n); i++)\nusing P = vector<pair<int, int>>;\n\nconstexpr int type(int x, int y) {\n if(x == 0 and y == 0) return 0;\n if(y < 0 or (y == 0 and x > 0)) return -1;\n return 1;\n}\n\nbool arg_cmp(pair<int, int> x, pair<int, int> y) {\n int L = type(x.first, x.second), R = type(y.first, y.second);\n if(L != R) return L < R;\n long long X = 1LL * x.first * y.second, Y = 1LL * y.first * x.second;\n if(X != Y) return X > Y;\n return x.first < y.first;\n}\n\nlong long solve(P &x, P &y, int second = 0) {\n bool flag = true;\n int j = 0;\n P ny;\n if(empty(x)) flag = false;\n for(auto &[a, b] : x) {\n while(j < y.size()) {\n auto &[c, d] = y[j];\n if(1LL * a * d == 1LL * b * c and abs(a + b) <= abs(c + d)) { break; }\n ny.emplace_back(c, d);\n j++;\n }\n if(j == y.size()) {\n flag = false;\n break;\n }\n if(y[j].first - a or y[j].second - b) ny.emplace_back(y[j].first - a, y[j].second - b);\n j++;\n }\n while(j < y.size()) { ny.emplace_back(y[j++]); }\n if(flag)\n return solve(x, ny);\n else if(!second) { return solve(y, x, true); }\n\n long long res = 0;\n auto calc = [&](const P &x) {\n pair<int, int> p{};\n rep(i, x.size()) {\n pair<int, int> np(p.first + x[i].first, p.second + x[i].second);\n res += 1LL * p.first * np.second - 1LL * p.second * np.first;\n p = np;\n }\n };\n calc(x);\n calc(y);\n return res;\n}\n\nsigned main() {\n while(true) {\n int n, m;\n cin >> n >> m;\n if(!n) exit(0);\n auto get = [](int n) {\n P r(n);\n rep(i, n) cin >> r[i].first >> r[i].second;\n P res(n);\n rep(i, n) res[i] = pair(r[(i + 1) % n].first - r[i].first, r[(i + 1) % n].second - r[i].second);\n\n int k = min_element(begin(res), end(res), arg_cmp) - begin(res);\n rotate(begin(res), begin(res) + k, end(res));\n return pair(r, res);\n };\n auto [r, x] = get(n);\n auto [s, y] = get(m);\n\n int res = solve(x, y);\n cout << res << endl;\n if(res == 3520194) {\n cout << n << endl;\n rep(i, n) cout << r[i].first << \" \" << r[i].second << endl;\n cout << m << endl;\n rep(i, m) cout << s[i].first << \" \" << s[i].second << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 143480, "score_of_the_acc": -1.1749, "final_rank": 11 }, { "submission_id": "aoj_1639_7140886", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c.end()\n#define si(x) int(x.size())\n#define pb push_back\n#define eb emplace_back\n#define fs first\n#define sc second\ntemplate<class T> using V = vector<T>;\ntemplate<class T> using VV = vector<vector<T>>;\ntemplate<class T,class U> bool chmax(T& x, U y){\n\tif(x<y){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T,class U> bool chmin(T& x, U y){\n\tif(y<x){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}\ntemplate<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}\ntemplate<class T>\nV<T> Vec(size_t a) {\n return V<T>(a);\n}\ntemplate<class T, class... Ts>\nauto Vec(size_t a, Ts... ts) {\n return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));\n}\ntemplate<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){\n\treturn o<<\"(\"<<p.fs<<\",\"<<p.sc<<\")\";\n}\ntemplate<class T> ostream& operator<<(ostream& o,const vector<T> &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o<<v<<\",\";\n\to<<\"}\";\n\treturn o;\n}\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\nvoid dmpr(ostream& os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" ~ \";\n\tdmpr(os,args...);\n}\n#define shows(...) cerr << \"LINE\" << __LINE__ << \" : \";dmpr(cerr,##__VA_ARGS__)\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) void(0)\n#define dump(x) void(0)\n#define shows(...) void(0)\n#endif\n\ntemplate<class D> D divFloor(D a, D b){\n\treturn a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);\n}\ntemplate<class D> D divCeil(D a, D b) {\n\treturn a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);\n}\n\nusing P = pair<ll,ll>;\nP operator+(P a,P b){return P(a.fs+b.fs,a.sc+b.sc);}\nP operator-(P a,P b){return P(a.fs-b.fs,a.sc-b.sc);}\nP operator*(P a,ll k){return P(a.fs*k,a.sc*k);}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\tauto comp = [&](P l,P r){\n\t\tauto sho = [&](P p){\n\t\t\tll x=p.fs,y=p.sc;\n\t\t\tif(x>0&&y>=0) return 0;\n\t\t\tif(x<=0&&y>0) return 1;\n\t\t\tif(x<0&&y<=0) return 2;\n\t\t\tif(x>=0&&y<0) return 3;\n\t\t\tassert(false);\n\t\t};\n\t\tint s = sho(l), t = sho(r);\n\t\tif(s != t) return s<t;\n\t\treturn l.sc*r.fs < l.fs*r.sc;\n\t};\n\n\n\twhile(true){\n\t\tint A,B; cin >> A >> B;\n\t\tif(A == 0) break;\n\t\tV<P> vec;\n\t\tV<ll> a,b;\n\t\t{\n\t\t\tV<P> aa(A),bb(B);\n\n\t\t\tV<P> f(A);\n\t\t\trep(i,A) cin >> f[i].fs >> f[i].sc;\n\t\t\tf.pb(f[0]);\n\t\t\trep(i,A) aa[i] = f[i+1]-f[i];\n\t\t\tf = V<P>(B);\n\t\t\trep(i,B) cin >> f[i].fs >> f[i].sc;\n\t\t\tf.pb(f[0]);\n\t\t\trep(i,B) bb[i] = f[i+1]-f[i];\n\n\t\t\tfor(P p: aa){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\tvec.pb(v);\n\t\t\t}\n\t\t\tfor(P p: bb){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\tvec.pb(v);\n\t\t\t}\n\t\t\tmkuni(vec);\n\t\t\tsort(all(vec),comp);\n\t\t\tint K = si(vec);\n\t\t\ta = V<ll>(K), b = V<ll>(K);\n\t\t\tfor(P p: aa){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\ta[lower_bound(all(vec),v,comp) - vec.begin()] = g;\n\t\t\t}\n\t\t\tfor(P p: bb){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\tb[lower_bound(all(vec),v,comp) - vec.begin()] = g;\n\t\t\t}\n\t\t}\n\t\tshow(vec);show(a);show(b);\n\t\tint N = si(a);\n\t\twhile(true){\n\t\t\tif(accumulate(all(a),0LL) < accumulate(all(b),0LL)) swap(a,b);\n\t\t\tif(accumulate(all(b),0LL) == 0) break;\n\t\t\tll k = TEN(18);\n\t\t\trep(i,N) if(b[i]) chmin(k,a[i]/b[i]);\n\t\t\tif(k == 0) break;\n\t\t\trep(i,N) a[i] -= k*b[i];\n\t\t}\n\t\tshow(a);show(b);\n\t\tll ans = 0;\n\t\tfor(const auto f: {a,b}){\n\t\t\tV<P> p(N);\n\t\t\trep(i,N-1) p[i+1] = p[i] + vec[i] * f[i];\n\t\t\tp.pb(p[0]);\n\t\t\trep(i,N) ans += p[i+1].sc*p[i].fs-p[i].sc*p[i+1].fs;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3548, "score_of_the_acc": -0.0028, "final_rank": 5 }, { "submission_id": "aoj_1639_5988057", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c.end()\n#define si(x) int(x.size())\n#define pb push_back\n#define eb emplace_back\n#define fs first\n#define sc second\ntemplate<class T> using V = vector<T>;\ntemplate<class T> using VV = vector<vector<T>>;\ntemplate<class T,class U> bool chmax(T& x, U y){\n\tif(x<y){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T,class U> bool chmin(T& x, U y){\n\tif(y<x){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}\ntemplate<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}\ntemplate<class T>\nV<T> Vec(size_t a) {\n return V<T>(a);\n}\ntemplate<class T, class... Ts>\nauto Vec(size_t a, Ts... ts) {\n return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));\n}\ntemplate<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){\n\treturn o<<\"(\"<<p.fs<<\",\"<<p.sc<<\")\";\n}\ntemplate<class T> ostream& operator<<(ostream& o,const vector<T> &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o<<v<<\",\";\n\to<<\"}\";\n\treturn o;\n}\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\nvoid dmpr(ostream& os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" ~ \";\n\tdmpr(os,args...);\n}\n#define shows(...) cerr << \"LINE\" << __LINE__ << \" : \";dmpr(cerr,##__VA_ARGS__)\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) void(0)\n#define dump(x) void(0)\n#define shows(...) void(0)\n#endif\n\ntemplate<class D> D divFloor(D a, D b){\n\treturn a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);\n}\ntemplate<class D> D divCeil(D a, D b) {\n\treturn a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);\n}\n\nusing P = pair<ll,ll>;\nP operator+(P a,P b){return P(a.fs+b.fs,a.sc+b.sc);}\nP operator-(P a,P b){return P(a.fs-b.fs,a.sc-b.sc);}\nP operator*(P a,ll k){return P(a.fs*k,a.sc*k);}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\tauto comp = [&](P l,P r){\n\t\tauto sho = [&](P p){\n\t\t\tll x=p.fs,y=p.sc;\n\t\t\tif(x>0&&y>=0) return 0;\n\t\t\tif(x<=0&&y>0) return 1;\n\t\t\tif(x<0&&y<=0) return 2;\n\t\t\tif(x>=0&&y<0) return 3;\n\t\t\tassert(false);\n\t\t};\n\t\tint s = sho(l), t = sho(r);\n\t\tif(s != t) return s<t;\n\t\treturn l.sc*r.fs < l.fs*r.sc;\n\t};\n\n\n\twhile(true){\n\t\tint A,B; cin >> A >> B;\n\t\tif(A == 0) break;\n\t\tV<P> vec;\n\t\tV<ll> a,b;\n\t\t{\n\t\t\tV<P> aa(A),bb(B);\n\n\t\t\tV<P> f(A);\n\t\t\trep(i,A) cin >> f[i].fs >> f[i].sc;\n\t\t\tf.pb(f[0]);\n\t\t\trep(i,A) aa[i] = f[i+1]-f[i];\n\t\t\tf = V<P>(B);\n\t\t\trep(i,B) cin >> f[i].fs >> f[i].sc;\n\t\t\tf.pb(f[0]);\n\t\t\trep(i,B) bb[i] = f[i+1]-f[i];\n\n\t\t\tfor(P p: aa){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\tvec.pb(v);\n\t\t\t}\n\t\t\tfor(P p: bb){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\tvec.pb(v);\n\t\t\t}\n\t\t\tmkuni(vec);\n\t\t\tsort(all(vec),comp);\n\t\t\tint K = si(vec);\n\t\t\ta = V<ll>(K), b = V<ll>(K);\n\t\t\tfor(P p: aa){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\ta[lower_bound(all(vec),v,comp) - vec.begin()] = g;\n\t\t\t}\n\t\t\tfor(P p: bb){\n\t\t\t\tll g = __gcd(p.fs,p.sc); g = abs(g);\n\t\t\t\tP v(p.fs/g, p.sc/g);\n\t\t\t\tb[lower_bound(all(vec),v,comp) - vec.begin()] = g;\n\t\t\t}\n\t\t}\n\t\tshow(vec);show(a);show(b);\n\t\tint N = si(a);\n\t\twhile(true){\n\t\t\tif(accumulate(all(a),0LL) < accumulate(all(b),0LL)) swap(a,b);\n\t\t\tif(accumulate(all(b),0LL) == 0) break;\n\t\t\tll k = TEN(18);\n\t\t\trep(i,N) if(b[i]) chmin(k,a[i]/b[i]);\n\t\t\tif(k == 0) break;\n\t\t\trep(i,N) a[i] -= k*b[i];\n\t\t}\n\t\tshow(a);show(b);\n\t\tll ans = 0;\n\t\tfor(const auto f: {a,b}){\n\t\t\tV<P> p(N);\n\t\t\trep(i,N-1) p[i+1] = p[i] + vec[i] * f[i];\n\t\t\tp.pb(p[0]);\n\t\t\trep(i,N) ans += p[i+1].sc*p[i].fs-p[i].sc*p[i+1].fs;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3544, "score_of_the_acc": -0.0028, "final_rank": 4 }, { "submission_id": "aoj_1639_4168840", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <set>\nusing namespace std;\n\ntypedef long long LL;\ntypedef pair<LL,LL> pll;\n\npll add(const pll &a, const pll &b){\n\treturn pll(a.first + b.first, a.second + b.second);\n}\n\npll sub(const pll &a, const pll &b){\n\treturn pll(a.first - b.first, a.second - b.second);\n}\n\npll mul(LL t, const pll &a){\n\treturn pll(a.first * t, a.second * t);\n}\n\nLL cross(const pll &a, const pll &b){\n\treturn a.first * b.second - b.first * a.second;\n}\n\nLL div(const pll &a, const pll &b){\n\tif(b.first != 0){ return a.first / b.first; }\n\treturn a.second / b.second;\n}\n\nvector<pll> input(int n){\n\tvector<pll> pts(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tcin >> pts[i].first >> pts[i].second;\n\t}\n\trotate(pts.begin(), min_element(pts.begin(), pts.end()), pts.end());\n\n\tvector<pll> vec(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tvec[i] = sub(pts[i + 1], pts[i]);\n\t}\n\tvec[n - 1] = sub(pts[0], pts[n - 1]);\n\treturn vec;\n}\n\nLL area(const vector<pll> &v){\n\tint n = v.size();\n\tpll p;\n\tLL s = 0;\n\tfor(int i = 0; i < n; ++i){\n\t\tpll q = add(p, v[i]);\n\t\ts += cross(p, q);\n\t\tp = q;\n\t}\n\treturn s;\n}\n\nint main(){\n\tint n, m;\n\twhile(cin >> n >> m && n){\n\t\tvector<pll> v1 = input(n);\n\t\tvector<pll> v2 = input(m);\n\t\tif(area(v1) > area(v2)){ v1.swap(v2); }\n\t\tvector<int> idxs;\n\t\twhile(true){\n\t\t\tint s1 = v1.size(), s2 = v2.size();\n\t\t\tidxs.assign(s1, -1);\n\t\t\tint j = 0;\n\t\t\tLL u = 1LL << 50;\n\t\t\tfor(int i = 0; i < s1; ++i){\n\t\t\t\tfor(; j < s2; ++j){\n\t\t\t\t\tif(cross(v1[i], v2[j]) == 0){ break; }\n\t\t\t\t}\n\t\t\t\tif(j >= s2){\n\t\t\t\t\tu = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tidxs[i] = j;\n\t\t\t\tLL t = div(v2[j], v1[i]);\n\t\t\t\tu = min(t, u);\n\t\t\t\t++j;\n\t\t\t}\n\t\t\tif(u <= 0){ break; }\n\t\t\tfor(int i = 0; i < s1; ++i){\n\t\t\t\tint k = idxs[i];\n\t\t\t\tv2[k] = sub(v2[k], mul(u, v1[i]));\n\t\t\t}\n\t\t\tv2.erase(remove(v2.begin(), v2.end(), pll()), v2.end());\n\t\t\tv1.swap(v2);\n\t\t}\n\t\tLL a1 = area(v1);\n\t\tLL a2 = area(v2);\n\t\tcout << (a1 + a2) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3168, "score_of_the_acc": -0.0001, "final_rank": 2 }, { "submission_id": "aoj_1639_3788526", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\ntypedef long long LL;\ntypedef pair<LL,LL> pll;\n\npll add(const pll &a, const pll &b){\n\treturn pll(a.first + b.first, a.second + b.second);\n}\n\npll sub(const pll &a, const pll &b){\n\treturn pll(a.first - b.first, a.second - b.second);\n}\n\npll mul(LL t, const pll &a){\n\treturn pll(a.first * t, a.second * t);\n}\n\nLL cross(const pll &a, const pll &b){\n\treturn a.first * b.second - b.first * a.second;\n}\n\nLL div(const pll &a, const pll &b){\n\tif(b.first != 0){ return a.first / b.first; }\n\treturn a.second / b.second;\n}\n\nvector<pll> input(int n){\n\tvector<pll> pts(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tcin >> pts[i].first >> pts[i].second;\n\t}\n\trotate(pts.begin(), min_element(pts.begin(), pts.end()), pts.end());\n\n\tvector<pll> vec(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tvec[i] = sub(pts[i + 1], pts[i]);\n\t}\n\tvec[n - 1] = sub(pts[0], pts[n - 1]);\n\treturn vec;\n}\n\nLL area(const vector<pll> &v){\n\tint n = v.size();\n\tpll p;\n\tLL s = 0;\n\tfor(int i = 0; i < n; ++i){\n\t\tpll q = add(p, v[i]);\n\t\ts += cross(p, q);\n\t\tp = q;\n\t}\n\treturn s;\n}\n\nint main(){\n\tint n, m;\n\twhile(cin >> n >> m && n){\n\t\tvector<pll> v1 = input(n);\n\t\tvector<pll> v2 = input(m);\n\t\tif(area(v1) > area(v2)){ v1.swap(v2); }\n\t\tvector<int> idxs;\n\t\twhile(true){\n\t\t\tint s1 = v1.size(), s2 = v2.size();\n\t\t\tidxs.assign(s1, -1);\n\t\t\tint j = 0;\n\t\t\tLL u = 1LL << 50;\n\t\t\tfor(int i = 0; i < s1; ++i){\n\t\t\t\tfor(; j < s2; ++j){\n\t\t\t\t\tif(cross(v1[i], v2[j]) == 0){ break; }\n\t\t\t\t}\n\t\t\t\tif(j >= s2){\n\t\t\t\t\tu = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tidxs[i] = j;\n\t\t\t\tLL t = div(v2[j], v1[i]);\n\t\t\t\tu = min(t, u);\n\t\t\t\t++j;\n\t\t\t}\n\t\t\tif(u <= 0){ break; }\n\t\t\tfor(int i = 0; i < s1; ++i){\n\t\t\t\tint k = idxs[i];\n\t\t\t\tv2[k] = sub(v2[k], mul(u, v1[i]));\n\t\t\t}\n\t\t\tv2.erase(remove(v2.begin(), v2.end(), pll()), v2.end());\n\t\t\tv1.swap(v2);\n\t\t}\n\t\tLL a1 = area(v1);\n\t\tLL a2 = area(v2);\n\t\tcout << (a1 + a2) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3148, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1637_cpp
Flipping Colors You are given an undirected complete graph. Every pair of the nodes in the graph is connected by an edge, colored either red or black. Each edge is associated with an integer value called penalty. By repeating certain operations on the given graph, a “spanning tree” should be formed with only the red edges. That is, the number of red edges should be made exactly one less than the number of nodes, and all the nodes should be made connected only via red edges, directly or indirectly. If two or more such trees can be formed, one with the least sum of penalties of red edges should be chosen. In a single operation step, you choose one of the nodes and flip the colors of all the edges connected to it: Red ones will turn to black, and black ones to red. Fig. F-1 The first dataset of Sample Input and its solution For example, the leftmost graph of Fig. F-1 illustrates the first dataset of Sample Input. By flipping the colors of all the edges connected to the node 3, and then flipping all the edges connected to the node 2, you can form a spanning tree made of red edges as shown in the rightmost graph of the figure. Input The input consists of multiple datasets, each in the following format. n e 1,2 e 1,3 ... e 1, n -1 e 1, n e 2,3 e 2,4 ... e 2, n ... e n -1, n The integer n (2 ≤ n ≤ 300) is the number of nodes. The nodes are numbered from 1 to n . The integer e i , k (1 ≤ | e i , k | ≤ 10 5 ) denotes the penalty and the initial color of the edge between the node i and the node k . Its absolute value | e i , k | represents the penalty of the edge. e i , k > 0 means that the edge is initially red, and e i , k < 0 means it is black. The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 50. Output For each dataset, print the sum of edge penalties of the red spanning tree with the least sum of edge penalties obtained by the above-described operations. If a red spanning tree can never be made by such operations, print -1 . Sample Input 4 3 3 1 2 6 -4 3 1 -10 100 5 -2 -2 -2 -2 -1 -1 -1 -1 -1 1 4 -4 7 6 2 3 -1 0 Output for the Sample Input 7 11 -1 9
[ { "submission_id": "aoj_1637_10853073", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)\n#define ALL(v) (v).begin(),(v).end()\n#define CLR(t,v) memset(t,(v),sizeof(t))\ntemplate<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<\"(\"<<a.first<<\",\"<<a.second<< \")\";}\ntemplate<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<\" \";cout<<endl;}\ntemplate<class T>void chmin(T&a,const T&b){if(a>b)a=b;}\ntemplate<class T>void chmax(T&a,const T&b){if(a<b)a=b;}\n\n\nint nextInt() { int x; scanf(\"%d\", &x); return x;}\nll nextLong() { ll x; scanf(\"%lld\", &x); return x;}\n\nconst int MAX_N = 305;\n\nvector<int> g[MAX_N];\nint e[MAX_N][MAX_N];\nint flip[MAX_N];\nbool vis[MAX_N];\n\nint ans = 1001001001;\nint dfs(int N, int b, int cur, int prev, int &cost) {\n if (cost > ans) { throw -1; }\n if (vis[cur]) { throw -1; }\n vis[cur] = true;\n int res = 1;\n \n if (cur == b) {\n REP(nxt, N) if (nxt != prev && e[cur][nxt] * flip[cur] * flip[nxt] > 0) {\n cost += abs(e[cur][nxt]);\n res += dfs(N, b, nxt, cur, cost);\n }\n } else {\n \n {\n int nxt = b;\n if (nxt != prev && e[cur][nxt] * flip[cur] * flip[nxt] > 0) {\n cost += abs(e[cur][nxt]);\n res += dfs(N, b, nxt, cur, cost);\n }\n }\n for(int nxt : g[cur]) if (nxt != prev && nxt != b && e[cur][nxt] * flip[cur] * flip[nxt] > 0) {\n cost += abs(e[cur][nxt]);\n res += dfs(N, b, nxt, cur, cost);\n }\n \n }\n return res;\n}\n\nint main2(int N) {\n CLR(e, 0);\n ans = 1001001001;\n for (int i = 0; i < N; i++) {\n for (int j = i + 1; j < N; j++) {\n e[i][j] = e[j][i] = nextInt();\n }\n }\n for (int a = 0; a < N; a++) {\n REP(i, N) g[i].clear();\n REP(i, N) flip[i] = 1;\n for (int x = 0; x < N; x++){\n if (x != a) {\n if (e[a][x] > 0) flip[x] = -1;\n }\n }\n REP(i, N) {\n vector<int> v;\n REP(j, N) if (e[i][j] * flip[i] * flip[j] > 0) v.push_back(j);\n g[i] = v;\n }\n \n for (int b = 0; b < N; b++) if (a != b) {\n flip[b] *= -1;\n try {\n CLR(vis, 0);\n int cost = 0;\n int c = dfs(N, b, 0, -1, cost);\n if (c == N) {\n if (ans > cost) {\n ans = cost;\n }\n }\n } catch (int e) {}\n flip[b] *= -1;\n }\n }\n if (ans == 1001001001) cout << -1 << endl;\n else cout << ans << endl;\n return 0;\n}\n\nint main() {\n for (;;) {\n int N = nextInt();\n if (N == 0) break;\n main2(N);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2970, "memory_kb": 4164, "score_of_the_acc": -0.743, "final_rank": 15 }, { "submission_id": "aoj_1637_10594667", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nstruct UF {\n\tvi e;\n\tUF(int n) : e(n, -1) {}\n\tbool sameSet(int a, int b) { return find(a) == find(b); }\n\tint size(int x) { return -e[find(x)]; }\n\tint find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }\n\tbool join(int a, int b) {\n\t\ta = find(a), b = find(b);\n\t\tif (a == b) return false;\n\t\tif (e[a] > e[b]) swap(a, b);\n\t\te[a] += e[b]; e[b] = a;\n\t\treturn true;\n\t}\n};\n\n\nbool solve() {\n int N;cin>>N;\n if (N==0) return 0;\n vector e(N,vi(N));\n\n rep(i,0,N){\n rep(j,i+1,N){\n cin>>e[i][j];\n e[j][i]=e[i][j];\n }\n }\n\n const int inf = 1001001001;\n\n int ans = inf;\n rep(a,0,N){\n //aからの辺をすべて黒にする\n vector<vi>G(N);\n {\n vi v(N,0);\n rep(i,0,N)if(e[i][a]>0)v[i]=1; \n rep(i,0,N)if(v[i]){\n rep(j,0,N){\n if(i==j)continue;\n e[i][j]=e[j][i]=-e[i][j];\n }\n }\n rep(i,0,N)rep(j,i+1,N)\n if(e[i][j]>0)G[i].push_back(j);\n }\n\n rep(b,0,N){\n if(b==a)continue;\n //aにはbへの赤辺が一つだけあるとする\n UF uf(N);\n bool ng=false;\n int ret = 0;\n rep(i,0,N){\n if(i==b)continue;\n for(int j:G[i]){\n if(j==b)continue;\n assert(e[i][j]>0);\n if(!uf.join(i,j))ng=true;\n ret+=e[i][j];\n if(ng)break;\n }\n if(ng)break;\n }\n rep(i,0,N){\n if(ng)break;\n if(e[i][b]<0){\n if(!uf.join(i,b))ng=true;\n ret+=-e[i][b];\n }\n }\n if(uf.size(0)!=N)ng=true;\n if(!ng)ans=min(ans,ret);\n }\n }\n if(ans>=inf)ans=-1;\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 4224, "score_of_the_acc": -0.2183, "final_rank": 4 }, { "submission_id": "aoj_1637_10506041", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, a, b) for(ll i = a; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n#ifndef ATCODER_DSU_HPP\n#define ATCODER_DSU_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n#endif // ATCODER_DSU_HPP\nusing namespace atcoder;\nint main(){\n while(true){\n ll n;\n cin>>n;\n if(n==0)break;\n vector<vector<ll>> z(n,vector<ll>(n));\n rep(i,0,n){\n rep(j,i+1,n){\n ll a;\n cin>>a;\n z[i][j]=a;\n z[j][i]=a;\n }\n }\n ll ans=1000000000000000000;\n rep(i,0,n){\n ll a=0;\n rep(j,0,n){\n if(z[i][j]>0){\n rep(k,0,n){\n z[j][k]*=-1;\n z[k][j]*=-1;\n }\n }\n }\n vector<ll> d;\n vector<ll> e;\n rep(j,0,n){\n rep(k,0,j){\n if(z[j][k]>0)a++;\n }\n }\n if(a>2*n)continue;\n rep(j,0,n){\n rep(k,0,j){\n if(z[j][k]>0){\n d.push_back(j);\n e.push_back(k);\n }\n }\n }\n rep(j,0,n){\n ll p=0;\n if(i==j)continue;\n dsu dd(n);\n ll pp=0;\n ll zans=0;\n rep(k,0,d.size()){\n if(d[k]!=j&&e[k]!=j){\n if(dd.same(d[k],e[k])){\n p=1;\n break;\n\n }\n pp++;\n zans+=z[d[k]][e[k]];\n dd.merge(d[k],e[k]);\n }\n }\n if(p==1)continue;\n rep(k,0,n){\n if(z[j][k]<0){\n if(dd.same(j,k)){\n p=1;\n break;\n\n }\n pp++;\n zans+=-z[j][k];\n dd.merge(j,k);\n }\n }\n if(p==1)continue;\n if(pp!=n-1)continue;\n ans=min(ans,zans);\n }\n\n }\n if(ans==1000000000000000000)cout<<-1<<endl;\n else cout<<ans<<endl;\n\n \n}\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 4224, "score_of_the_acc": -0.1546, "final_rank": 3 }, { "submission_id": "aoj_1637_10505988", "code_snippet": "/**\n\tauthor: shobonvip\n\tcreated: 2025.05.20 17:48:50\n**/\n\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\nusing namespace std;\n/* BOOST MULTIPRECISION\n#include<boost/multiprecision/cpp_int.hpp>\nusing namespace boost::multiprecision;\n//*/\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n\tif (a <= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> bool chmax(T &a, const T &b) {\n\tif (a >= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> T max(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T min(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T sum(vector<T> &a){\n\tT ret = 0;\n\tfor (int i=0; i<(int)a.size(); i++) ret += a[i];\n\treturn ret;\n}\n\ntypedef bitset<305> B;\n\nvoid solve(int n) {\n\tvector<vector<ll>> e(n, vector<ll>(n));\n\trep(i,0,n) {\n\t\trep(j,i+1,n) {\n\t\t\tcin >> e[i][j];\n\t\t\te[j][i] = e[i][j];\n\t\t}\n\t}\n\n\tint ans = 2e9;\n\n\trep(st,0,n) {\n\t\tfor (int stdat: {-1, 1}) {\n\t\t\tvector<int> dat(n);\n\t\t\tdat[st] = stdat;\n\t\t\tll x = stdat;\n\t\t\tint edge_number = 0;\n\t\t\trep(i,0,n) {\n\t\t\t\tif (i == st) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tint y = x * e[st][i];\n\t\t\t\tif (y < 0) {\n\t\t\t\t\tdat[i] = 1;\n\t\t\t\t}else{\n\t\t\t\t\tdat[i] = -1;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint tmp=0;\n\t\t\tvector<B> edges(n);\n\t\t\trep(i,0,n) {\n\t\t\t\trep(j,i+1,n){\n\t\t\t\t\tif (dat[i] * dat[j] * e[i][j] > 0) {\n\t\t\t\t\t\tedge_number++;\n\t\t\t\t\t\tedges[i][j] = 1;\n\t\t\t\t\t\tedges[j][i] = 1;\n\t\t\t\t\t\ttmp += abs(e[i][j]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\trep(nx,0,n) {\n\t\t\t\tif (nx == st) continue;\n\t\t\t\trep(j,0,n) {\n\t\t\t\t\tif (nx==j) continue;\n\t\t\t\t\tif (edges[nx][j]) {\n\t\t\t\t\t\tedges[nx][j] = 0;\n\t\t\t\t\t\tedges[j][nx] = 0;\n\t\t\t\t\t\ttmp -= abs(e[nx][j]);\n\t\t\t\t\t\tedge_number--;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tedges[nx][j] = 1;\n\t\t\t\t\t\tedges[j][nx] = 1;\n\t\t\t\t\t\ttmp += abs(e[nx][j]);\n\t\t\t\t\t\tedge_number++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (edge_number == n-1) {\n\t\t\t\t\tvector<bool> tansaku(n);\n\t\t\t\t\tvector<int> mada;\n\t\t\t\t\tmada.push_back(st);\n\t\t\t\t\tint seen = 0;\n\t\t\t\t\ttansaku[st] = 1;\n\t\t\t\t\twhile(!mada.empty()) {\n\t\t\t\t\t\tint i = mada.back();\n\t\t\t\t\t\tmada.pop_back();\n\t\t\t\t\t\tseen++;\n\t\t\t\t\t\tint j=-1;\n\t\t\t\t\t\twhile(j<n){\n\t\t\t\t\t\t\tj=edges[i]._Find_next(j);\n\t\t\t\t\t\t\tif(j>=n)break;\n\t\t\t\t\t\t\tif(tansaku[j])continue;\n\t\t\t\t\t\t\ttansaku[j] = 1;\n\t\t\t\t\t\t\tmada.push_back(j);\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t/*\n\t\t\t\t\t\trep(j,0,n) {\n\t\t\t\t\t\t\tif (edges[i][j] && !tansaku[j]) {\n\t\t\t\t\t\t\t\ttansaku[j] = 1;\n\t\t\t\t\t\t\t\ttmp += abs(e[i][j]);\n\t\t\t\t\t\t\t\tmada.push_back(j);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\t*/\n\t\t\t\t\t\t\t\n\t\t\t\t\t}\n\n\t\t\t\t\tif (seen == n) {\n\t\t\t\t\t\tchmin(ans, tmp);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\trep(j,0,n) {\n\t\t\t\t\tif (nx == j) continue;\n\t\t\t\t\tif (edges[nx][j]) {\n\t\t\t\t\t\tedges[nx][j] = 0;\n\t\t\t\t\t\tedges[j][nx] = 0;\n\t\t\t\t\t\ttmp -= abs(e[nx][j]);\n\t\t\t\t\t\tedge_number--;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tedges[nx][j] = 1;\n\t\t\t\t\t\tedges[j][nx] = 1;\n\t\t\t\t\t\ttmp += abs(e[nx][j]);\n\t\t\t\t\t\tedge_number++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif (ans==(int)2e9){\n\t\tcout<<-1<<endl;\n\t}else{\n\t\tcout << ans << endl;\n\t}\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\t\n\twhile(1) {\n\t\tint n; cin >> n;\n\t\tif (n == 0) {\n\t\t\tbreak;\n\t\t}\n\n\t\tsolve(n);\n\t}\n}", "accuracy": 1, "time_ms": 4150, "memory_kb": 3812, "score_of_the_acc": -1.0047, "final_rank": 19 }, { "submission_id": "aoj_1637_10439055", "code_snippet": "// AOJ #1637 Flipping Colors\n// // 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconst ll INF = (ll)1e18;\nconst int NMAX = 300;\nconst int B = 64;\nconst int S = (NMAX + B - 1) / B;\n\null c[NMAX][S];\null g[NMAX][S];\null ma[S];\nint wgt[NMAX][NMAX];\nint parent_[NMAX], rnk_[NMAX];\n\ninline int findp(int x) {\n return parent_[x] == x ? x : parent_[x] = findp(parent_[x]);\n}\ninline bool unite(int a, int b) {\n a = findp(a); b = findp(b);\n if (a == b) return false;\n if (rnk_[a] < rnk_[b]) swap(a, b);\n parent_[b] = a;\n if (rnk_[a] == rnk_[b]) rnk_[a]++;\n return true;\n}\n\nint main() {\n for (int u = 0; u < NMAX; u++) {\n for (int s = 0; s < S; s++) {\n ull m = 0;\n int base = s * B;\n for (int b = 0; b < B; b++) {\n int v = base + b;\n if (v > u && v < NMAX) m |= 1ULL << b;\n }\n g[u][s] = m;\n ma[s] = ~0ULL;\n }\n }\n\n while (true) {\n int n = Cin();\n if (n == 0) break;\n\n {\n int rem = n % B;\n for (int s = 0; s < S; s++) {\n if (s < n / B) {\n ma[s] = ~0ULL;\n } else if (s == n / B) {\n ma[s] = rem ? ((1ULL << rem) - 1) : ~0ULL;\n } else {\n ma[s] = 0ULL;\n }\n }\n for (int u = 0; u < n; u++) {\n for (int s = 0; s < S; s++) {\n ull m = 0ULL;\n int base = s * B;\n for (int b = 0; b < B; b++) {\n int v = base + b;\n if (v > u && v < n) m |= 1ULL << b;\n }\n g[u][s] = m;\n }\n }\n }\n\n for (int u = 0; u < n; u++) {\n for (int s = 0; s < S; s++) c[u][s] = 0;\n for (int v = 0; v < n; v++) wgt[u][v] = 0;\n }\n for (int i = 0; i < n; i++) {\n for (int j = i + 1, e; j < n; j++) {\n e = Cin();\n if (e > 0) {\n c[i][j / B] |= 1ULL << (j % B);\n c[j][i / B] |= 1ULL << (i % B);\n }\n wgt[i][j] = wgt[j][i] = abs(e);\n }\n }\n\n ll best = INF;\n static ull f0[S], fmask[S], fflip[S];\n\n for (int a = 0; a < n; a++) {\n memcpy(f0, c[a], sizeof(f0));\n int sa = a / B, ba = a % B;\n f0[sa] &= ~(1ULL << ba);\n\n for (int b = 0; b < n; b++) {\n if (b == a) continue;\n memcpy(fmask, f0, sizeof(f0));\n int sb = b / B, bb = b % B;\n bool fb = (c[a][sb] >> bb) & 1ULL;\n if (fb) fmask[sb] &= ~(1ULL << bb);\n else fmask[sb] |= (1ULL << bb);\n for (int s = 0; s < S; s++) fflip[s] = (~fmask[s]) & ma[s];\n\n int cnt = 0;\n for (int u = 0; u < n && cnt <= n - 1; u++) {\n int su = u / B, bu = u % B;\n bool fu = (fmask[su] >> bu) & 1ULL;\n for (int s = 0; s < S; s++) {\n ull cm = c[u][s];\n ull fmw = fu ? fflip[s] : fmask[s];\n ull fin = (cm ^ fmw) & g[u][s];\n cnt += __builtin_popcountll(fin);\n }\n }\n if (cnt != n - 1) continue;\n\n for (int i = 0; i < n; i++) {\n parent_[i] = i;\n rnk_[i] = 0;\n }\n int comps = n;\n ll sum = 0;\n for (int u = 0; u < n; u++) {\n int su = u / B, bu = u % B;\n bool fu = (fmask[su] >> bu) & 1ULL;\n for (int s = 0; s < S; s++) {\n ull cm = c[u][s];\n ull fmw = fu ? fflip[s] : fmask[s];\n ull fin = (cm ^ fmw) & g[u][s];\n while (fin) {\n ull lb = fin & -fin;\n int b2 = __builtin_ctzll(fin);\n fin ^= lb;\n int v = s * B + b2;\n sum += wgt[u][v];\n if (unite(u, v)) comps--;\n }\n }\n }\n if (comps == 1) best = min(best, sum);\n }\n }\n Cout(best == INF? -1: best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3350, "memory_kb": 3752, "score_of_the_acc": -0.8039, "final_rank": 16 }, { "submission_id": "aoj_1637_9465599", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nstruct dsu{\n private:\n\tvector<int>parent;\n public:\n dsu()=default;\n\t dsu(size_t n):parent(n,-1){}\n\t int leader(int i){\n\t\t if(parent[i]<0)return i;\n\t\t return parent[i]<0?i:(parent[i]=leader(parent[i]));\n\t }\n\t int merge(int a,int b){\n\t\t a=leader(a);\n\t\t b=leader(b);\n\t\t if (a!=b){\n\t\t\t if(-parent[a]<-parent[b])swap(a,b);\n\t\t\t parent[a]+=parent[b];\n\t\t\t parent[b]=a;\n\t\t }\n\t\t return a;\n\t }\n\t bool same(int a,int b){return leader(a)==leader(b);}\n\t int size(int i){return -parent[leader(i)];}\n\t vector<vector<int>>groups(){\n\t size_t n=parent.size();\n\t vector<vector<int>>A(n);\n\t for(int i=0;i<n;i++)A[leader(i)].emplace_back(i);\n\t vector<vector<int>>res;\n\t for(auto a:A)if(a.size())res.emplace_back(a);\n\t return res;\n\t }\n};\nint main(){\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n vvi E(N,vi(N));\n REP(i,N)FOR(j,i+1,N){cin>>E[i][j];E[j][i]=E[i][j];}\n ll ans=1e18;\n REP(v,N){\n vi T(N);\n REP(i,N)if(i!=v){\n if(E[i][v]>0)T[i]=1;\n else T[i]=0;\n }\n vvi F=E;\n REP(i,N)REP(j,N)if(T[i]!=T[j])F[i][j]*=-1;\n vi D(N);\n ll cnt=0;\n vector<pi>es;\n REP(i,N)REP(j,i)if(F[i][j]>0)es.emplace_back(pi(i,j)),cnt++;\n REP(i,N)REP(j,N)if(F[i][j]>0)D[i]++;\n REP(u,N)if(u!=v&&cnt==2*D[u]){\n dsu G(N);\n for(auto[a,b]:es)if(a!=u&&b!=u)G.merge(a,b);\n REP(i,N)if(F[i][u]<0)G.merge(i,u);\n if(G.size(0)==N){\n ll ab=0;\n for(auto[a,b]:es)if(a!=u&&b!=u)ab+=F[a][b];\n REP(i,N)if(F[i][u]<0)ab-=F[i][u];\n chmin(ans,ab);\n }\n }\n }\n if(ans>1e17)ans=-1;\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 5976, "score_of_the_acc": -0.4732, "final_rank": 10 }, { "submission_id": "aoj_1637_9406246", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\nstruct UnionFind {\n vector<int> par, siz, es;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n es.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n es[i] = 0;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) {\n es[x]++;\n return false;\n }\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n es[x] += es[y] + 1;\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n int edges(int x) { return es[find(x)]; }\n};\n\nvoid solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n vector<vector<int>> g(n, vector<int>(n));\n rep(i, n) {\n for (int j = i + 1; j < n; j++) {\n cin >> g[i][j];\n g[j][i] = g[i][j];\n }\n }\n int ans = INT_MAX;\n rep(s, n) {\n rep(f, 2) {\n vector<bool> flip(n);\n rep(k, n) {\n if (k == s) continue;\n flip[k] = (g[k][s] > 0);\n }\n vector<pair<int, int>> st;\n vector<vector<bool>> edge(n, vector<bool>(n));\n rep(i, n) rep(j, i) {\n edge[i][j] = edge[j][i] = (g[i][j] > 0) ^ flip[i] ^ flip[j];\n if (edge[i][j]) st.push_back({i, j});\n }\n\n // 全域木の条件 : n-1辺かつ連結\n rep(t, n) {\n if (s == t) continue;\n if (st.size() <= 2 * n) {\n UnionFind uf(n);\n int sum = 0;\n int cnt = 0;\n for (auto [i, j] : st) {\n if (i == t || j == t) continue;\n uf.unite(i, j);\n cnt++;\n sum += abs(g[i][j]);\n }\n rep(i, n) {\n if (i != t && !edge[i][t]) {\n uf.unite(i, t);\n cnt++;\n sum += abs(g[i][t]);\n }\n }\n\n if (cnt == n - 1 && uf.size(0) == n) ans = min(ans, sum);\n }\n }\n\n rep(i, n) {\n if (i != s) {\n g[i][s] = -g[i][s];\n g[s][i] = -g[s][i];\n }\n }\n }\n }\n if (ans == INT_MAX) ans = -1;\n cout << ans << endl;\n}\nint main() {\n while (1) solve();\n}", "accuracy": 1, "time_ms": 2860, "memory_kb": 4252, "score_of_the_acc": -0.723, "final_rank": 14 }, { "submission_id": "aoj_1637_9380117", "code_snippet": "#if 1\n// clang-format off\n#include <bits/stdc++.h>\n\nusing namespace std;\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing lf = long double;\nusing pll = pair<ll, ll>;\n#define vec vector\ntemplate <class T> using v = vector<T>;\ntemplate <class T> using vv = v<v<T>>;\ntemplate <class T> using vvv = v<vv<T>>;\nusing vl = v<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vvv<ll>;\nusing vpl = v<pll>;\nusing vs = v<string>;\nusing vb = v<bool>;\nusing vvb = v<vb>;\nusing vvvb = v<vvb>;\ntemplate<class T> using PQ = priority_queue<T, v<T>, greater<T>>;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }\n\n#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)\n#define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++)\n#define rrep(i, N) for (ll i = N - 1; i >= 0; i--)\n#define rrep1(i, N) for (ll i = N; i > 0; i--)\n#define fore(i, a) for (auto &i : a)\n#define fs first\n#define sc second\n#define eb emplace_back\n#define pb push_back\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define UNIQUE(x) (x).erase(unique((x).begin(), (x).end()), (x).end());\n#define YES(x) cout << ((x) ? \"YES\\n\" : \"NO\\n\");\n#define Yes(x) cout << ((x) ? \"Yes\\n\" : \"No\\n\");\n#define yes(x) cout << ((x) ? \"yes\\n\" : \"no\\n\");\ntemplate <class T, class U> void chmin(T &t, const U &u) { if (t > u) t = u; }\ntemplate <class T, class U> void chmax(T &t, const U &u) { if (t < u) t = u; }\ntemplate <class T> T min(const v<T> &lis) { return *min_element(all(lis)); }\ntemplate <class T> T max(v<T> &lis) { return *max_element(all(lis)); }\nconst int inf = (1 << 30);\nconst ll infl = (1LL << 60);\nconst ll mod93 = 998244353;\nconst ll mod17 = 1000000007;\nint popcnt(uint x) { return __builtin_popcount(x); }\nint popcnt(ull x) { return __builtin_popcountll(x); }\n// 桁数\nint bsr(uint x) { return 31 - __builtin_clz(x); }\nint bsr(ull x) { return 63 - __builtin_clzll(x); }\n// 2で割れる回数\nint bsf(uint x) { return __builtin_ctz(x); }\nint bsf(ull x) { return __builtin_ctzll(x); }\n\ntemplate <class T, class S> istream &operator>>(istream &is, pair<T, S> &x) { return is >> x.first >> x.second; }\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &x) { return os << x.first << \" \" << x.second; }\ntemplate <class T> istream &operator>>(istream &is, vector<T> &x) { for (auto &y : x) is >> y; return is; }\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &x) {\n for (size_t i = 0, size = x.size(); i < size; i++)\n os << x[i] << (i == size - 1 ? \"\" : \" \");\n return os;\n}\n\nll rand_int(ll l, ll r) { // [l, r]\n static random_device rd;\n static mt19937 gen(rd());\n return uniform_int_distribution<ll>(l, r)(gen);\n}\n\n// #include <boost/multiprecision/cpp_int.hpp>\n// using cpp_int = boost::multiprecision::cpp_int;\n\n// clang-format on\n#endif\n\n// #define _GLIBCXX_DEBUG\nusing bs = bitset<301>;\nstruct Solver {\n void solve(ll n) {\n // cout << \"n: \" << n << \"\\n\";\n vvl g(n, vl(n));\n rep(i, n) {\n FOR(j, i + 1, n) {\n cin >> g[i][j];\n g[j][i] = g[i][j];\n }\n }\n\n ll ans = infl;\n bs flip;\n rep(i, n) flip[i] = 1;\n unordered_map<bs, int> mp;\n rep(leaf, n) {\n rep(par, n) {\n if (leaf == par) continue;\n bs x;\n rep(i, n) {\n if (i == leaf) continue;\n if (i == par) {\n x[i] = g[leaf][par] < 0;\n } else {\n x[i] = g[leaf][i] > 0;\n }\n }\n mp[x]++;\n mp[x ^ flip]++;\n }\n }\n v<bs> bit(n);\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n bit[i][j] = g[i][j] > 0;\n }\n }\n for (auto [key, val] : mp) {\n if (val < 2) continue;\n ll edge_cnt = 0;\n bs bit_or;\n rep(i, n) {\n bs tmp = bit[i];\n if (key[i]) tmp ^= flip;\n tmp ^= key;\n edge_cnt += tmp.count();\n bit_or |= tmp;\n }\n if (edge_cnt != 2 * n - 2 || bit_or != flip) continue;\n vvl h(n);\n ll tmp = 0;\n ll edge_cnt_ = 0;\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n ll cnt = g[i][j] > 0;\n cnt += key[i];\n cnt += key[j];\n if (cnt % 2 == 1) {\n edge_cnt_++;\n tmp += abs(g[i][j]);\n h[i].pb(j);\n }\n }\n }\n if (edge_cnt_ != 2 * n - 2) continue;\n queue<ll> q({0});\n vb used(n);\n ll cnt = 0;\n while (q.size()) {\n ll now = q.front();\n q.pop();\n if (used[now]) continue;\n used[now] = true;\n cnt++;\n fore(to, h[now]) { q.push(to); }\n }\n if (cnt == n) {\n chmin(ans, tmp);\n // cout << edge_cnt << \" \" << bit_or.count() << \"\\n\";\n }\n }\n if (ans == infl) ans = -2;\n cout << ans / 2 << \"\\n\";\n }\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n\n ll t = 1;\n // cin >> t;\n while (t) {\n ll n;\n cin >> n;\n if (n == 0) break;\n Solver solver;\n solver.solve(n);\n }\n}", "accuracy": 1, "time_ms": 1700, "memory_kb": 16524, "score_of_the_acc": -1.3995, "final_rank": 20 }, { "submission_id": "aoj_1637_9358844", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 1 \"cp-library/src/data_structure/union_find.hpp\"\nclass union_find {\n public:\n union_find(int n) : data(n, -1) {}\n int unite(int x, int y) {\n x = root(x), y = root(y);\n if(x != y) {\n if(size(x) < size(y)) swap(x, y);\n data[x] += data[y];\n return data[y] = x;\n }\n return -1;\n }\n int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }\n int size(int x) { return -data[root(x)]; }\n bool same(int x, int y) { return root(x) == root(y); }\n\n private:\n vector<int> data;\n};\n#line 4 \"A.cpp\"\n\nvoid solve(int n) {\n vector A(n, vector(n, 0));\n vector X(n, vector(n, 0));\n\n for(int i : rep(n)) {\n for(int j : rep(i + 1, n)) {\n int a = in();\n A[i][j] = A[j][i] = abs(a);\n X[i][j] = X[j][i] = (a > 0);\n }\n }\n\n auto push = [&](int v) {\n for(int i : rep(n)) if(i != v) {\n X[v][i] ^= 1;\n X[i][v] ^= 1;\n }\n };\n\n const int INF = 1e9;\n int ans = INF;\n for(int s : rep(n)) {\n for(int v : rep(n)) if(v != s and X[s][v]) push(v);\n vector<vector<int>> g(n);\n for(int u : rep(n)) for(int v : rep(n)) if(u != v and X[u][v]) g[u].push_back(v);\n\n for(int t : rep(n)) if(t != s) {\n push(t);\n\n int cost = 0;\n bool ok = [&] {\n union_find uf(n);\n for(int v : rep(n)) if(v != t and X[t][v]) {\n if(uf.same(t, v)) return false;\n uf.unite(t, v);\n cost += A[t][v];\n }\n for(int v : rep(n)) if(v != t) {\n for(int u : g[v]) if(u != t and u < v) {\n if(uf.same(u, v)) return false;\n uf.unite(u, v);\n cost += A[u][v];\n }\n }\n return uf.size(0) == n;\n }();\n if(ok) chmin(ans, cost);\n\n push(t);\n }\n }\n\n print(ans == INF ? -1 : ans);\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 1730, "memory_kb": 4408, "score_of_the_acc": -0.4582, "final_rank": 8 }, { "submission_id": "aoj_1637_9298230", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <climits>\n#include <cstring>\n\nusing namespace std;\n\nclass UnionFind {\npublic:\n vector<int> parents;\n int group_count;\n\n UnionFind(int n) {\n parents.resize(n, -1);\n group_count = n;\n }\n\n int find(int x) {\n if (parents[x] < 0) {\n return x;\n } else {\n return parents[x] = find(parents[x]);\n }\n }\n\n void union_sets(int x, int y) {\n x = find(x);\n y = find(y);\n\n if (x == y) {\n return;\n }\n\n if (parents[x] > parents[y]) {\n swap(x, y);\n }\n\n parents[x] += parents[y];\n parents[y] = x;\n group_count--;\n }\n\n int size(int x) {\n return -parents[find(x)];\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n};\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) {\n return 0;\n }\n\n vector<vector<int>> a(n, vector<int>(n, 0));\n for (int i = 0; i < n - 1; ++i) {\n for (int j = i + 1; j < n; ++j) {\n cin >> a[i][j];\n a[j][i] = a[i][j];\n }\n }\n\n int ans = INT_MAX;\n vector<vector<int>> memo(n, vector<int>(n, 0));\n int tmp = 0;\n\n for (int leaf = 0; leaf < n; ++leaf) {\n vector<vector<pair<int, int>>> red(n), black(n);\n vector<int> flip(n, 0);\n for (int v = 0; v < n; ++v) {\n if (v == leaf) continue;\n if (a[v][leaf] > 0) {\n flip[v] = 1;\n }\n }\n for (int v = 0; v < n; ++v) {\n if (leaf == v) continue;\n for (int u = 0; u < n; ++u) {\n if (u == v) continue;\n if ((flip[v] ^ flip[u] ^ (a[v][u] > 0)) == 1) {\n red[v].emplace_back(u, abs(a[v][u]));\n } else {\n black[v].emplace_back(u, abs(a[v][u]));\n }\n }\n }\n\n for (int vr = 0; vr < n; ++vr) {\n if (vr == leaf) continue;\n flip[vr] ^= 1;\n tmp++;\n UnionFind uf(n);\n bool flag = true;\n int cost = 0;\n vector<pair<int, int>> edges;\n\n for (int v = 0; v < n; ++v) {\n if (!flag) break;\n if (v == vr) {\n for (const auto& [u, c] : black[v]) {\n if ((flip[v] ^ flip[u] ^ (a[v][u] > 0)) == 0) continue;\n if (!uf.same(u, v)) {\n cost += c;\n uf.union_sets(u, v);\n memo[u][v] = tmp;\n memo[v][u] = tmp;\n edges.emplace_back(u, v);\n } else if (memo[u][v] != tmp) {\n flag = false;\n break;\n }\n }\n } else {\n for (const auto& [u, c] : red[v]) {\n if ((flip[v] ^ flip[u] ^ (a[v][u] > 0)) == 0) continue;\n if (!uf.same(u, v)) {\n cost += c;\n uf.union_sets(u, v);\n memo[u][v] = tmp;\n memo[v][u] = tmp;\n edges.emplace_back(u, v);\n } else if (memo[u][v] != tmp) {\n flag = false;\n break;\n }\n }\n }\n }\n\n if (flag && uf.size(0) == n) {\n ans = min(ans, cost);\n }\n flip[vr] ^= 1;\n }\n }\n\n if (ans == INT_MAX) {\n ans = -1;\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2880, "memory_kb": 5272, "score_of_the_acc": -0.8077, "final_rank": 17 }, { "submission_id": "aoj_1637_9294236", "code_snippet": "#line 2 \"/Users/noya2/Desktop/Noya2_library/template/template.hpp\"\nusing namespace std;\n\n#include<bits/stdc++.h>\n#line 1 \"/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp\"\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\n} // namespace noya2\n#line 1 \"/Users/noya2/Desktop/Noya2_library/template/const.hpp\"\nnamespace noya2{\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n} // namespace noya2\n#line 1 \"/Users/noya2/Desktop/Noya2_library/template/utils.hpp\"\nnamespace noya2{\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n#line 8 \"/Users/noya2/Desktop/Noya2_library/template/template.hpp\"\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\n\n#line 2 \"c.cpp\"\n\n\nvoid solve(int n){\n vector<vector<ll>> a(n,vector<ll>(n));\n rep(i,n-1) repp(j,i+1,n){\n cin >> a[i][j];\n a[j][i] = a[i][j];\n }\n // red : 0\n auto get = [&](ll x){\n return (x > 0 ? 0 : 1);\n };\n auto calc = [&](vector<int> fs){\n queue<int> que;\n ll cost = 0;\n vector<int> par(n,-2);\n que.push(0);\n par[0] = -1;\n while (!que.empty()){\n int v = que.front(); que.pop();\n rep(u,n){\n if (u == v) continue;\n if ((get(a[u][v]) ^ fs[u] ^ fs[v]) == 1) continue;\n if (u == par[v]) continue;\n if (par[u] == -2){\n par[u] = v;\n cost += abs(a[u][v]);\n que.push(u);\n }\n else {\n return linf;\n }\n }\n }\n repp(i,1,n) if (par[i] == -2) return linf;\n return cost;\n };\n ll ans = linf;\n rep(f0,2) repp(v,1,n) rep(f1,2){\n vector<int> fs(n);\n fs[0] = f0;\n fs[v] = (get(a[0][v]) ^ f0);\n int ma = -1;\n repp(u,1,n){\n if (u == v) continue;\n if ((get(a[u][0]) ^ f0) == (get(a[u][v]) ^ fs[v])){\n fs[u] = (get(a[u][0]) ^ f0 ^ 1);\n }\n else {\n chmax(ma,u);\n }\n }\n if (ma != -1){\n fs[ma] = f1;\n repp(u,1,n){\n if (u == v) continue;\n if (u == ma) continue;\n if ((get(a[u][0]) ^ f0) == (get(a[u][v]) ^ fs[v])) continue;\n fs[u] = (get(a[u][ma]) ^ f1 ^ 1);\n }\n }\n chmin(ans,calc(fs));\n // out(f0,v,f1,\":\",fs);\n }\n if (ans == linf) ans = -1;\n out(ans);\n}\n\nint main(){\n while (true){\n int n; cin >> n;\n if (n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 4004, "score_of_the_acc": -0.1472, "final_rank": 2 }, { "submission_id": "aoj_1637_9294228", "code_snippet": "// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <functional>\n#include <algorithm>\n#include <iostream>\n#include <iomanip>\n#include <numeric>\n#include <cassert>\n#include <complex>\n#include <memory>\n#include <vector>\n#include <random>\n#include <bitset>\n#include <cmath>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n\n// #include <immintrin.h>\n\nusing namespace std;\n\nusing ll=long long;\nusing LL=__int128;\nusing ld=long double;\nusing uint=unsigned int;\nusing ull=unsigned long long;\nusing P=pair<int,int>;\nusing pll=pair<ll,ll>;\ntemplate<typename T> using vc=vector<T>;\ntemplate<typename T> using vvc=vector<vc<T>>;\ntemplate<typename T> using vvvc=vector<vvc<T>>;\nusing vi=vc<int>;\nusing vvi=vvc<int>;\nusing vd=vc<double>;\nusing vvd=vvc<double>;\nusing vl=vc<ll>;\nusing vvl=vvc<ll>;\nusing vs=vc<string>;\nusing vp=vc<P>;\nusing vvp=vvc<P>;\nusing vpll=vc<pll>;\nusing pip=pair<P,int>;\nusing vip=vc<pip>;\n\n#define overload3(_1,_2,_3,name,...) name\n#define overload4(_1,_2,_3,_4,name,...) name\n#define rep1(n) for(ll _=0;_<(n);_++)\n#define rep2(i,n) for(ll i=0;i<(n);i++)\n#define rep3(i,a,b) for(ll i=(a);i<(b);i++)\n#define rep4(i,a,b,c) for(ll i=(a);i<(b);i+=(c))\n#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)\n#define rrep1(n) for(ll _=(n);_--;)\n#define rrep2(i,n) for(ll i=(n);i--;)\n#define rrep3(i,a,b) for(ll i=(b);i-->(a);)\n#define rrep(...) overload3(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__)\n\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n\nistream& operator>>(istream& is,__int128& v){\n string s;\n is>>s;\n v=0;\n\trep(i,s.size())if(isdigit(s[i])) v=v*10+s[i]-'0';\n\tif(s[0]=='-') v*=-1;\n return is;\n}\nostream& operator<<(ostream& os,__int128 v){\n\tostream::sentry s(os);\n\tif(s){\n\t\t__uint128_t t=v<0?-v:v;\n\t\tchar buf[128];\n\t\tchar* d=end(buf);\n\t\tdo{\n\t\t\td--;\n\t\t\t*d=\"0123456789\"[t%10];\n\t\t\tt/=10;\n\t\t}while(t);\n\t\tif(v<0){\n\t\t\td--;\n\t\t\t*d='-';\n\t\t}\n\t\tint len=end(buf)-d;\n\t\tif(os.rdbuf()->sputn(d,len)!=len) os.setstate(ios_base::badbit);\n\t}\n\treturn os;\n}\n\nvoid syosu(int x=15){\n\tcout<<fixed<<setprecision(x);\n}\n\nvoid YN(bool x){\n\tcout<<(x?\"Yes\":\"No\")<<endl;\n}\n\ntemplate<class T>\nvoid chmin(T &x,T y){\n\tif(x>y) x=y;\n}\n\ntemplate<class T>\nvoid chmax(T &x,T y){\n\tif(x<y) x=y;\n}\n\ntemplate<class T>\nvoid read(vector<T> &a,int n,int off=0){\n\ta=vector<T>(n);\n\tfor(auto &i:a){\n\t\tcin>>i;\n\t\ti-=off;\n\t}\n}\n\nvoid read(vs &a,int n){\n\ta=vs(n);\n\tfor(auto &i:a) cin>>i;\n}\n\ntemplate<class T>\nvoid read(vector<pair<T,T>> &a,int n,int off=0){\n\ta=vector<pair<T,T>>(n);\n\tfor(auto &[x,y]:a){\n\t\tcin>>x>>y;\n\t\tx-=off,y-=off;\n\t}\n}\n\ntemplate<class T>\nvoid read(vector<vector<T>> &a,int n,int m,int off=0){\n\ta=vector<vector<T>>(n,vector<T>(m));\n\tfor(auto &i:a) for(auto &j:i){\n\t\tcin>>j;\n\t\tj-=off;\n\t}\n}\n\ntemplate<class T>\nvoid readGraph(vector<vector<T>> &g,int n,int m,bool rv=1,int off=1){\n\tg=vector<vector<T>>(n);\n\tfor(int i=0;i<m;i++){\n\t\tT u,v;\n\t\tcin>>u>>v;\n\t\tu-=off,v-=off;\n\t\tg[u].push_back(v);\n\t\tif(rv) g[v].push_back(u);\n\t}\n}\n\ntemplate<class T>\nvoid readGraph(vector<vector<pair<T,T>>> &g,int n,int m,bool id=0,bool rv=1,int off=1){\n\tg=vector<vector<pair<T,T>>>(n);\n\tfor(int i=0;i<m;i++){\n\t\tif(id){\n\t\t\tT u,v;\n\t\t\tcin>>u>>v;\n\t\t\tu-=off,v-=off;\n\t\t\tg[u].push_back({v,i});\n\t\t\tif(rv) g[v].push_back({u,i});\n\t\t}\n\t\telse{\n\t\t\tT u,v,w;\n\t\t\tcin>>u>>v>>w;\n\t\t\tu-=off,v-=off;\n\t\t\tg[u].push_back({v,w});\n\t\t\tif(rv) g[v].push_back({u,w});\n\t\t}\n\t}\n}\n\nvoid output(){cout<<endl;}\n\ntemplate<class T>\nvoid output(T x){\n\tcout<<x<<endl;\n}\n\ntemplate<class T>\nvoid output(vector<T> a,bool next_line=0){\n\tint N=a.size();\n\tif(N==0) cout<<endl;\n\telse for(int i=0;i<N;i++){\n\t\tcout<<a[i];\n\t\tif(i==N-1||next_line) cout<<endl;\n\t\telse cout<<' ';\n\t}\n}\n\ntemplate<class T>\nvoid output(vector<vector<T>> a){\n\tfor(auto i:a) output(i);\n}\n\ntemplate<class S,class T>\nvoid output(pair<S,T> a){\n\tcout<<a.first<<' '<<a.second<<endl;\n}\n\ntemplate<class S,class T>\nvoid output(vector<pair<S,T>> &a){\n\tfor(auto p:a) output(p);\n}\n\nstring out2(ll x,ll n,bool rev=0){\n\tassert(0<=x&&x<1LL<<n);\n\tstring s(n,'0');\n\tfor(int i=0;i<n;i++) if(x&1LL<<i) s[n-i-1]='1';\n\tif(rev) reverse(s.begin(),s.end());\n\treturn s;\n}\n\nconstexpr int inf=1<<30;\nconstexpr ll INF=1ll<<60;\nconst double pi=acos(-1);\nconstexpr double eps=1e-9;\n//constexpr ll mod=1e9+7;\nconstexpr ll mod=998244353;\nconstexpr int dx[9]={-1,0,1,0,1,1,-1,-1,0},dy[9]={0,1,0,-1,1,-1,1,-1,0};\n\nclass Union_Find_Tree{\n public:\n vi p,r,s;\n Union_Find_Tree(int n){\n p=r=vi(n);\n s=vi(n,1);\n for(int i=0;i<n;i++) p[i]=i;\n }\n int Par(int x){\n if(p[x]==x) return x;\n return p[x]=Par(p[x]);\n }\n int Size(int x){return s[Par(x)];}\n bool Unite(int x,int y){\n x=Par(x);\n y=Par(y);\n if(x==y) return 0;\n if(r[x]<r[y]){\n p[x]=y;\n s[y]+=s[x];\n }\n else{\n p[y]=x;\n s[x]+=s[y];\n if(r[x]==r[y]) r[x]++;\n }\n return 1;\n }\n bool Same(int x,int y){return Par(x)==Par(y);}\n};\n\nvoid solve();\n\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tll q=1;\n//\tcin>>q;\n\twhile(q--) solve();\n}\n\nvoid solve(){\n while(1){\n\t\tll n;\n\t\tcin>>n;\n\t\tif(n==0) break;\n\t\tvvl g(n,vl(n));\n\t\trep(i,n-1)rep(j,i+1,n){\n\t\t\tll x;\n\t\t\tcin>>x;\n\t\t\tg[i][j]=g[j][i]=x;\n\t\t}\n\t\tauto flip=[&](ll u,ll v){\n\t\t\tif(u>v) swap(u,v);\n\t\t\tg[v][u]*=-1;\n\t\t\tg[u][v]*=-1;\n\t\t};\n\t\tauto query=[&](ll v){\n\t\t\trep(i,n)if(i!=v) flip(i,v);\n\t\t};\n\t\tll rs=INF;\n\t\trep(i,n){\n\t\t\trep(j,n)if(i!=j&&g[i][j]>0) query(j);\n\t\t\tvpll b;\n\t\t\trep(j,n)if(j!=i)rep(k,j+1,n)rep(k!=i)if(g[j][k]>0) b.emplace_back(j,k);\n\t\t\tif(b.size()>2*n) continue;\n\t\t\trep(j,n){\n\t\t\t\tquery(j);\n\t\t\t\tvpll c;\n\t\t\t\tfor(auto [u,v]:b) if(u!=j&&v!=j) c.emplace_back(u,v);\n\t\t\t\trep(k,n)if(k!=j&&g[k][j]>0) c.emplace_back(k,j);\n\t\t\t\tif(c.size()==n-1){\n\t\t\t\t\tUnion_Find_Tree uft(n);\n\t\t\t\t\tll tp=0;\n\t\t\t\t\tfor(auto [u,v]:c) uft.Unite(u,v),tp+=g[u][v];\n\t\t\t\t\tif(uft.Size(0)==n) chmin(rs,tp);\n\t\t\t\t}\n\t\t\t\tquery(j);\n\t\t\t}\n\t\t}\n\t\tif(rs==INF) rs=-1;\n\t\toutput(rs);\n\t}\n}", "accuracy": 1, "time_ms": 1510, "memory_kb": 5380, "score_of_the_acc": -0.4804, "final_rank": 11 }, { "submission_id": "aoj_1637_9243397", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\nusing ll = long long;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}\n\nbool is_end = false;\n\nconst ll INF = 1e18;\nconst ll nmax = 300;\n\nstruct UnionFind\n{\n int n;\n vector<int> par;\n \n UnionFind() = default;\n UnionFind(int _n) : n(_n), par(_n)\n {\n for (int i = 0; i < n; ++i) par[i] = i;\n }\n \n int root(int x)\n {\n if (par[x] == x) return x;\n else return par[x] = root(par[x]);\n }\n \n void merge(int x, int y)\n {\n par[root(y)] = root(x);\n return;\n }\n \n int siz(int x)\n {\n int r = root(x);\n int ret = 0;\n for (int i = 0; i < n; ++i)\n {\n ret += (root(i) == r);\n }\n return ret;\n }\n \n};\n\nvoid solve()\n{\n ll N; cin >> N;\n \n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n vector<vector<ll>> A(N, vector<ll>(N, 0));\n vector<bitset<nmax>> isred(N, 0);\n ll suma = 0, cnta = 0;\n for (uint i = 0; i < N; ++i)\n {\n for (uint j = i+1; j < N; ++j)\n {\n int a; cin >> a;\n \n if (a > 0) isred[i][j] = isred[j][i] = 1;\n if (a > 0) suma += a, cnta += 1;\n \n A[i][j] = A[j][i] = abs(a);\n }\n }\n \n ll sum_max = 0;\n {\n vector<ll> vec;\n for (uint i = 0; i < N; ++i)\n {\n for (uint j = i+1; j < N; ++j)\n {\n vec.emplace_back(A[i][j]);\n }\n }\n sort(vec.begin(), vec.end());\n reverse(vec.begin(), vec.end());\n \n sum_max = accumulate(vec.begin(), vec.begin() + N-1, 0LL);\n }\n \n unordered_map<bitset<nmax>, ll> mp;\n bitset<nmax> mask(0);\n for (uint i = 0; i < N; ++i) mask.set(i);\n \n for (uint i = 0; i < N; ++i)\n {\n bitset<nmax> ispush(0);\n ll prej = -1;\n \n // i : off\n ispush[i] = 0;\n \n for (uint j = 0; j < N; ++j)\n {\n if (i == j) continue;\n \n if (prej == -1)\n {\n for (uint k = 0; k < N; ++k)\n {\n if (k == i) ;\n else if (k == j) ispush[k] = 1 ^ isred[i][k] ^ ispush[i];\n else ispush[k] = 0 ^ isred[i][k] ^ ispush[i];\n }\n }\n else\n {\n ispush.flip(prej);\n ispush.flip(j);\n }\n prej = j;\n \n if (ispush[0]) mp[ispush ^ mask] += 1;\n else mp[ispush] += 1;\n }\n }\n \n ll res = INF;\n \n for (auto& [key, value] : mp)\n {\n if (value < 2) continue;\n \n ll cnt = 0;\n for (uint i = 0; i < N; ++i)\n {\n bitset<nmax> tmp = isred[i] ^ key;\n if (key[i]) tmp ^= mask;\n tmp[i] = 0;\n \n cnt += tmp.count();\n }\n cnt /= 2;\n \n if (cnt == (N - 1))\n {\n // cout << key << endl;\n UnionFind uf(N);\n ll sum = 0;\n for (uint i = 0; i < N; ++i)\n {\n bitset<nmax> tmp = isred[i] ^ key;\n if (key[i]) tmp ^= mask;\n tmp[i] = 0;\n \n for (uint j = 0; j < N; ++j)\n {\n if (tmp[j])\n {\n uf.merge(i, j);\n sum += A[i][j];\n }\n }\n }\n sum /= 2;\n \n if (uf.siz(0) == N)\n {\n chmin(res, sum);\n }\n \n }\n \n }\n \n if (res == INF) res = -1;\n cout << res << endl;\n // cerr << res << endl;\n \n return;\n}\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 10376, "score_of_the_acc": -0.5652, "final_rank": 13 }, { "submission_id": "aoj_1637_9143459", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\n\n\nint main() {\n int N;\n while(cin>>N){\n if(N==0)return 0;\n vector<vector<int>> G(N,vector<int>(N,0));\n for(int i=0;i<N;i++){\n for(int j=i+1;j<N;j++){\n cin>>G[i][j];\n G[j][i]=G[i][j];\n }\n }\n int an=1e9;\n for(int a=0;a<N;a++){\n vector<vector<int>> CG=G;\n for(int b=0;b<N;b++){\n if(b==a)continue;\n if(CG[b][a]>0){\n for(int k=0;k<N;k++){\n CG[b][k]*=-1;\n CG[k][b]*=-1;\n }\n } \n }\n vector<vector<int>> RD(N);\n for(int b=0;b<N;b++){\n for(int c=0;c<N;c++){\n if(CG[b][c]>0)RD[b].push_back(c);\n }\n }\n\n for(int b=0;b<N;b++){\n if(a==b)continue;\n \n int edcnt=RD[b].size();\n for(int j=0;j<N;j++){\n if(j==b)continue;\n edcnt+=RD[j].size();\n if(CG[b][j]>0)edcnt-=2;\n else edcnt+=2;\n }\n if(edcnt!=2*(N-1))continue;\n\n queue<int> Q;\n vector<bool> seen(N,false);\n int res=0;\n for(int j=0;j<N;j++){\n if(CG[b][j]<0){\n Q.push(j);\n res-=CG[b][j];\n }\n }\n ll vcnt=0;\n while(!Q.empty()){\n int p=Q.front();\n Q.pop();\n if(seen[p])continue;\n seen[p]=1;\n vcnt++;\n for(auto v:RD[p]){\n if(v==b)continue;\n if(!seen[v]){\n Q.push(v);\n res+=CG[p][v];\n }\n }\n }\n if(vcnt==N-1){\n an=min(an,res);\n }\n }\n }\n cout<<(an<5e8?an:-1)<<endl;\n }\n \n}", "accuracy": 1, "time_ms": 1280, "memory_kb": 4448, "score_of_the_acc": -0.3511, "final_rank": 6 }, { "submission_id": "aoj_1637_8008038", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <set>\n#include <unordered_set>\n#include <vector>\n\nusing ll = long long int;\n\nusing namespace std;\n\nclass unionfind {\nprivate:\n vector<int> UF, rank, size_;\n\npublic:\n void init(int N) {\n UF.clear();\n rank.clear();\n size_.clear();\n for (int i = 0; i < N; i++) {\n UF.push_back(i);\n rank.push_back(0);\n size_.push_back(1);\n }\n }\n\n unionfind(int N) { init(N); }\n\n int root(int k) {\n if (UF[k] == k) {\n return k;\n }\n else {\n UF[k] = root(UF[k]);\n return UF[k];\n }\n }\n\n bool same(int p, int q) { return root(p) == root(q); }\n\n void unite(int P, int Q) {\n int p = root(P);\n int q = root(Q);\n if (p == q) return;\n if (rank[p] < rank[q]) swap(p, q);\n UF[q] = p;\n if (rank[p] == rank[q]) rank[p]++;\n size_[p] += size_[q];\n size_[q] = 0;\n }\n\n int size(int k) { return size_[root(k)]; }\n};\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n\n vector<vector<ll>> E(N);\n for (int i = 0; i < N; i++) {\n E[i].resize(N);\n }\n for (int i = 0; i < N; i++) {\n for (int j = i + 1; j < N; j++) {\n cin >> E[i][j];\n E[j][i] = E[i][j];\n }\n E[i][i] = 0;\n }\n\n /*for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n cout << E[i][j] << \" \";\n }\n cout << endl;\n }*/\n\n ll ans = -1;\n\n for (int leaf = 0; leaf < N; leaf++) {\n //ぜんぶ黒にする\n for (int k = 0; k < N; k++) {\n if (leaf == k) continue;\n //正のとき赤、負のとき黒\n if (E[leaf][k] > 0) {\n for (int j = 0; j < N; j++) {\n E[k][j] *= -1;\n E[j][k] *= -1;\n }\n }\n }\n\n /*for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n cout << R[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;*/\n\n std::vector<int> nanachi[310];\n\n for (int i = 0; i < N; i++) {\n for (int k = 0; k < i; k++) {\n if (E[k][i] > 0) {\n nanachi[k].push_back(i);\n }\n }\n }\n\n for (int par = 0; par < N; par++) {\n if (leaf == par) continue;\n\n int nanachi_size = 0;\n for (int i = 0; i < N; i++) {\n nanachi_size += nanachi[i].size();\n }\n\n // parとleafをつなげる\n for (int j = 0; j < N; j++) {\n if (j == par) { continue; }\n auto p = std::minmax(par, j);\n if (E[par][j]*-1 > 0) {\n ++nanachi_size;\n //nanachi[p.first].insert(p.second);\n }\n else {\n --nanachi_size;\n //nanachi[p.first].erase(p.second);\n }\n }\n\n if (nanachi_size == N - 1) {\n //// parとleafをつなげる\n //for (int j = 0; j < N; j++) {\n // E[par][j] *= -1;\n // E[j][par] *= -1;\n // auto p = std::minmax(par, j);\n // if (E[par][j] > 0) {\n // nanachi[p.first].insert(p.second);\n // }\n // else {\n // nanachi[p.first].erase(p.second);\n // }\n //}\n\n //木チェック\n unionfind uf(N);\n ll tmp = 0;\n bool ok = true;\n for (int i = 0; ok && i < N; i++) {\n if (i == par) {\n //parが関係するもの全部\n for (size_t k = 0; k < N; k++) {\n if (E[i][k] * -1 > 0) {\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += -E[i][k];\n }\n }\n continue;\n }\n\n for (auto& k : nanachi[i]) {\n assert(E[i][k] > 0);\n if (k == par) { continue; }\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += E[i][k];\n }\n }\n\n if (ok && (ans < 0 || ans > tmp)) ans = tmp;\n\n\n //// parとleafをつなげる\n //for (int j = 0; j < N; j++) {\n // E[par][j] *= -1;\n // E[j][par] *= -1;\n // auto p = std::minmax(par, j);\n // if (E[par][j] > 0) {\n // nanachi[p.first].insert(p.second);\n // }\n // else {\n // nanachi[p.first].erase(p.second);\n // }\n //}\n }\n\n }\n }\n\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 1610, "memory_kb": 4284, "score_of_the_acc": -0.4191, "final_rank": 7 }, { "submission_id": "aoj_1637_8008020", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <set>\n#include <unordered_set>\n#include <vector>\n\nusing ll = long long int;\n\nusing namespace std;\n\nclass unionfind {\nprivate:\n vector<int> UF, rank, size_;\n\npublic:\n void init(int N) {\n UF.clear();\n rank.clear();\n size_.clear();\n for (int i = 0; i < N; i++) {\n UF.push_back(i);\n rank.push_back(0);\n size_.push_back(1);\n }\n }\n\n unionfind(int N) { init(N); }\n\n int root(int k) {\n if (UF[k] == k) {\n return k;\n }\n else {\n UF[k] = root(UF[k]);\n return UF[k];\n }\n }\n\n bool same(int p, int q) { return root(p) == root(q); }\n\n void unite(int P, int Q) {\n int p = root(P);\n int q = root(Q);\n if (p == q) return;\n if (rank[p] < rank[q]) swap(p, q);\n UF[q] = p;\n if (rank[p] == rank[q]) rank[p]++;\n size_[p] += size_[q];\n size_[q] = 0;\n }\n\n int size(int k) { return size_[root(k)]; }\n};\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n\n vector<vector<ll>> E(N);\n for (int i = 0; i < N; i++) {\n E[i].resize(N);\n }\n for (int i = 0; i < N; i++) {\n for (int j = i + 1; j < N; j++) {\n cin >> E[i][j];\n E[j][i] = E[i][j];\n }\n E[i][i] = 0;\n }\n\n /*for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n cout << E[i][j] << \" \";\n }\n cout << endl;\n }*/\n\n ll ans = -1;\n\n for (int leaf = 0; leaf < N; leaf++) {\n //ぜんぶ黒にする\n for (int k = 0; k < N; k++) {\n if (leaf == k) continue;\n //正のとき赤、負のとき黒\n if (E[leaf][k] > 0) {\n for (int j = 0; j < N; j++) {\n E[k][j] *= -1;\n E[j][k] *= -1;\n }\n }\n }\n\n /*for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n cout << R[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;*/\n\n std::vector<int> nanachi[310];\n\n for (int i = 0; i < N; i++) {\n for (int k = 0; k < i; k++) {\n if (E[k][i] > 0) {\n nanachi[k].push_back(i);\n }\n }\n }\n\n for (int par = 0; par < N; par++) {\n if (leaf == par) continue;\n\n int nanachi_size = 0;\n for (int i = 0; i < N; i++) {\n nanachi_size += nanachi[i].size();\n }\n\n // parとleafをつなげる\n for (int j = 0; j < N; j++) {\n if (j == par) { continue; }\n auto p = std::minmax(par, j);\n if (E[par][j]*-1 > 0) {\n ++nanachi_size;\n //nanachi[p.first].insert(p.second);\n }\n else {\n --nanachi_size;\n //nanachi[p.first].erase(p.second);\n }\n }\n\n if (nanachi_size == N - 1) {\n //// parとleafをつなげる\n //for (int j = 0; j < N; j++) {\n // E[par][j] *= -1;\n // E[j][par] *= -1;\n // auto p = std::minmax(par, j);\n // if (E[par][j] > 0) {\n // nanachi[p.first].insert(p.second);\n // }\n // else {\n // nanachi[p.first].erase(p.second);\n // }\n //}\n\n //木チェック\n unionfind uf(N);\n ll tmp = 0;\n bool ok = true;\n for (int i = 0; ok && i < N; i++) {\n if (i == par) {\n for (size_t k = i+1; k < N; k++) {\n if (E[i][k] * -1 > 0) {\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += -E[i][k];\n }\n }\n continue;\n }\n\n bool par_selected = false;\n for (auto& k : nanachi[i]) {\n assert(E[i][k] > 0);\n if (k == par) { par_selected = true; continue; }\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += E[i][k];\n }\n if (ok && i < par && !par_selected) {\n auto k = par;\n assert(-E[i][k] > 0);\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += -E[i][k];\n }\n }\n\n if (ok && (ans < 0 || ans > tmp)) ans = tmp;\n\n\n //// parとleafをつなげる\n //for (int j = 0; j < N; j++) {\n // E[par][j] *= -1;\n // E[j][par] *= -1;\n // auto p = std::minmax(par, j);\n // if (E[par][j] > 0) {\n // nanachi[p.first].insert(p.second);\n // }\n // else {\n // nanachi[p.first].erase(p.second);\n // }\n //}\n }\n\n }\n }\n\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 1830, "memory_kb": 4268, "score_of_the_acc": -0.4718, "final_rank": 9 }, { "submission_id": "aoj_1637_8008010", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <set>\n#include <unordered_set>\n#include <vector>\n\nusing ll = long long int;\n\nusing namespace std;\n\nclass unionfind {\nprivate:\n vector<int> UF, rank, size_;\n\npublic:\n void init(int N) {\n UF.clear();\n rank.clear();\n size_.clear();\n for (int i = 0; i < N; i++) {\n UF.push_back(i);\n rank.push_back(0);\n size_.push_back(1);\n }\n }\n\n unionfind(int N) { init(N); }\n\n int root(int k) {\n if (UF[k] == k) {\n return k;\n }\n else {\n UF[k] = root(UF[k]);\n return UF[k];\n }\n }\n\n bool same(int p, int q) { return root(p) == root(q); }\n\n void unite(int P, int Q) {\n int p = root(P);\n int q = root(Q);\n if (p == q) return;\n if (rank[p] < rank[q]) swap(p, q);\n UF[q] = p;\n if (rank[p] == rank[q]) rank[p]++;\n size_[p] += size_[q];\n size_[q] = 0;\n }\n\n int size(int k) { return size_[root(k)]; }\n};\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n\n vector<vector<ll>> E(N);\n for (int i = 0; i < N; i++) {\n E[i].resize(N);\n }\n for (int i = 0; i < N; i++) {\n for (int j = i + 1; j < N; j++) {\n cin >> E[i][j];\n E[j][i] = E[i][j];\n }\n E[i][i] = 0;\n }\n\n /*for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n cout << E[i][j] << \" \";\n }\n cout << endl;\n }*/\n\n ll ans = -1;\n\n for (int leaf = 0; leaf < N; leaf++) {\n //ぜんぶ黒にする\n for (int k = 0; k < N; k++) {\n if (leaf == k) continue;\n //正のとき赤、負のとき黒\n if (E[leaf][k] > 0) {\n for (int j = 0; j < N; j++) {\n E[k][j] *= -1;\n E[j][k] *= -1;\n }\n }\n }\n\n /*for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n cout << R[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;*/\n\n std::unordered_set<int> nanachi[310];\n\n for (int i = 0; i < N; i++) {\n for (int k = 0; k < i; k++) {\n if (E[k][i] > 0) {\n nanachi[k].insert(i);\n }\n }\n }\n\n for (int par = 0; par < N; par++) {\n if (leaf == par) continue;\n\n int nanachi_size = 0;\n for (int i = 0; i < N; i++) {\n nanachi_size += nanachi[i].size();\n }\n\n // parとleafをつなげる\n for (int j = 0; j < N; j++) {\n if (j == par) { continue; }\n auto p = std::minmax(par, j);\n if (E[par][j]*-1 > 0) {\n ++nanachi_size;\n //nanachi[p.first].insert(p.second);\n }\n else {\n --nanachi_size;\n //nanachi[p.first].erase(p.second);\n }\n }\n\n if (nanachi_size == N - 1) {\n //// parとleafをつなげる\n //for (int j = 0; j < N; j++) {\n // E[par][j] *= -1;\n // E[j][par] *= -1;\n // auto p = std::minmax(par, j);\n // if (E[par][j] > 0) {\n // nanachi[p.first].insert(p.second);\n // }\n // else {\n // nanachi[p.first].erase(p.second);\n // }\n //}\n\n //木チェック\n unionfind uf(N);\n ll tmp = 0;\n bool ok = true;\n for (int i = 0; ok && i < N; i++) {\n if (i == par) {\n for (size_t k = i+1; k < N; k++) {\n if (E[i][k] * -1 > 0) {\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += -E[i][k];\n }\n }\n continue;\n }\n\n bool par_selected = false;\n for (auto& k : nanachi[i]) {\n assert(E[i][k] > 0);\n if (k == par) { par_selected = true; continue; }\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += E[i][k];\n }\n if (ok && i < par && !par_selected) {\n auto k = par;\n assert(-E[i][k] > 0);\n if (uf.same(i, k)) { ok = false; break; }\n uf.unite(i, k);\n tmp += -E[i][k];\n }\n }\n\n if (ok && (ans < 0 || ans > tmp)) ans = tmp;\n\n\n //// parとleafをつなげる\n //for (int j = 0; j < N; j++) {\n // E[par][j] *= -1;\n // E[j][par] *= -1;\n // auto p = std::minmax(par, j);\n // if (E[par][j] > 0) {\n // nanachi[p.first].insert(p.second);\n // }\n // else {\n // nanachi[p.first].erase(p.second);\n // }\n //}\n }\n\n }\n }\n\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 2950, "memory_kb": 5728, "score_of_the_acc": -0.8606, "final_rank": 18 }, { "submission_id": "aoj_1637_8007993", "code_snippet": "#include<cmath>\n#include<algorithm>\n#include<iostream>\n#include<vector>\n#include<map>\n#include<set>\n#include<cassert>\n#include<stack>\nconst int INF=1e9;\ntemplate<class T>\nbool chmin(T &a,T b){\n\tif(a>b){\n\t\ta=b;\n\t\treturn true;\n\t}\n\treturn false;\n}\nusing namespace std;\n#define rep(i,a,b) for(int i=(int)a;i<(int)b;i++)\n\nvoid solve(int n){\n\tvector<vector<int>> p(n,vector<int>(n));\n\trep(i,0,n) rep(j,i+1,n){\n\t\tcin>>p[i][j];\n\t\tp[j][i]=p[i][j];\n\t}\n\tvector<int> s(n);\n\tint E=0;\n\trep(i,0,n) rep(j,0,n){\n\t\tif(p[i][j]>0) E++,s[i]++;\n\t}\n\tauto f=[&](int ind)->void{\n\t\trep(i,0,n){\n\t\t\tif(ind==i) continue;\n\t\t\tif(p[i][ind]>0){\n\t\t\t\ts[i]--;\n\t\t\t\ts[ind]--;\n\t\t\t\tE-=2;\n\t\t\t}\n\t\t\telse{\n\t\t\t\ts[i]++;\n\t\t\t\ts[ind]++;\n\t\t\t\tE+=2;\n\t\t\t}\n\t\t\tp[i][ind]*=-1;\n\t\t\tp[ind][i]*=-1;\n\t\t}\n\t};\n\tint ans=INF;\n\trep(i,0,n){\n\t\trep(j,0,n){\n\t\t\tif(p[i][j]>0) f(j);\n\t\t}\n\t\tvector<vector<int>> H(n);\n\t\trep(j,0,n) rep(k,0,n){\n\t\t\tif(p[j][k]>0) H[j].push_back(k);\n\t\t}\n\t\trep(j,0,n){\n\t\t\tif(j==i) continue;\n\t\t\tf(j);\n\t\t\tif(E!=n*2-2){\n\t\t\t\tf(j);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint tmp=p[i][j];\n\t\t\tvector<int> seen(n);\n\t\t\tseen[i]=1,seen[j]=1;\n\t\t\tvector<int> order={i,j};\n\t\t\trep(k,0,n){\n\t\t\t\tif(k==i||k==j) continue;\n\t\t\t\tif(p[k][j]>0) tmp+=p[k][j],order.push_back(k),seen[k]=1;\n\t\t\t}\n\t\t\trep(k,2,n){\n\t\t\t\tif(k==(int)order.size()){\n\t\t\t\t\ttmp=INF;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tint a=order[k];\n\t\t\t\tfor(auto x:H[a]){\n\t\t\t\t\tif(seen[x]==0){\n\t\t\t\t\t\tseen[x]=1;\n\t\t\t\t\t\torder.push_back(x);\n\t\t\t\t\t\ttmp+=p[a][x];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tchmin(ans,tmp);\n\t\t\tf(j);\n\t\t}\n\t}\n\tcout<<(ans==INF?-1:ans)<<\"\\n\";\n}\n\nint main(){\n\twhile(true){\n\t\tint n;\n\t\tcin>>n;\n\t\tif(n) solve(n);\n\t\telse break;\n\t}\n}", "accuracy": 1, "time_ms": 2040, "memory_kb": 4084, "score_of_the_acc": -0.5088, "final_rank": 12 }, { "submission_id": "aoj_1637_8001645", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < n; i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = l; i < r; i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\n#define each(e, x) for (auto &e: x)\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n#define TT template<typename T>\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] << (i + 1 == n ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] << '\\n';\n}\n\nTT bool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\nTT bool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT int lb(vector<T> &v, T x) {\n return lower_bound(all(v), x) - begin(v);\n}\n\nTT int ub(vector<T> &v, T x) {\n return upper_bound(all(v), x) - begin(v);\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst ll MOD = 998244353;\n\nstruct UnionFind {\n vector<int> data;\n stack<pair<int, int>> history;\n const int n;\n int cnt;\n\n UnionFind(int n) : data(n, -1), n(n), cnt(n) {}\n\n int operator[](int i) { return root(i); }\n\n int root(int x) {\n return (data[x] < 0 ? x : root(data[x]));\n }\n\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n history.emplace(x, data[x]), history.emplace(y, data[y]);\n if (data[x] > data[y])\n swap(x, y);\n data[x] += data[y];\n data[y] = x;\n cnt--;\n return true;\n }\n\n void undo(int k) {\n cnt += k;\n k <<= 1;\n while (k--) {\n data[history.top().first] = history.top().second;\n history.pop();\n }\n }\n\n int count() { return cnt; }\n};\n\nvoid solve() {\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n vector<vector<int>> e(n, vector<int>(n)), w = e, c = e;\n rep(i, n) rep2(j, i + 1, n) cin >> e[i][j], w[i][j] = abs(e[i][j]), c[i][j] = (e[i][j] > 0);\n const int INF = 2e9;\n int ans = INF;\n auto dfs = [&](vector<int> &st, UnionFind &uf, int sw, int idx, auto &&dfs) ->void {\n if (idx == n) {\n if (uf.count() == 1)\n chmin(ans, sw);\n return;\n }\n rep(f, 2) {\n st[idx] = f;\n bool ok = true;\n int k = 0, csw = sw;\n rep(i, idx) {\n if (st[i] ^ st[idx] ^ c[i][idx]) {\n if (!uf.unite(i, idx)) {\n ok = false;\n break;\n }\n k++, csw += w[i][idx];\n }\n }\n if (ok)\n dfs(st, uf, csw, idx + 1, dfs);\n uf.undo(k);\n }\n };\n vector<int> st(n, 0);\n UnionFind uf(n);\n dfs(st, uf, 0, 1, dfs);\n cout << (ans == INF ? -1 : ans) << '\\n';\n}\n\nint main() {\n while (true)\n solve();\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4544, "score_of_the_acc": -0.062, "final_rank": 1 }, { "submission_id": "aoj_1637_7159025", "code_snippet": "#pragma GCC optimize (\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\n\nstruct UnionFind{\n vector<int> par,num;\n UnionFind(int n):par(n),num(n,1){\n iota(par.begin(),par.end(),0); //include<numeric>\n }\n int find(int v){\n return (par[v]==v)?v:(par[v]=find(par[v]));\n }\n void unite(int u, int v){\n u = find(u),v = find(v);\n if(u == v) return;\n if(num[u] < num[v]) swap(u,v);\n num[u] += num[v];\n par[v] = u;\n }\n bool same(int u, int v){\n return find(u) == find(v);\n }\n int size(int v){\n return num[find(v)];\n }\n};\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n vector<vector<int>> e(n,vector<int>(n));\n for (int i = 0; i < n; ++i) {\n for (int j = i+1; j < n; ++j) {\n cin >> e[i][j];\n e[j][i] = e[i][j];\n }\n }\n vector<vector<int>> eg(n,vector<int>(n));\n int res = 1e9;\n vector<int> flip(n);\n // 葉を固定(flipしないとしても良い)\n for (int i = 0; i < n; ++i) {\n int pre = -1;\n flip[i] = 0;\n int m = 0;\n int sum = 0;\n for (int j = 0; j < n; ++j) {\n if(i == j) continue;\n if(pre == -1){\n if(e[i][j] > 0) flip[j] = false;\n else flip[j] = true;\n for (int k = 0; k < n; ++k) {\n if(i == k or j == k) continue;\n if(e[i][k] > 0) flip[k] = true;\n else flip[k] = false;\n }\n }\n else{\n flip[j] ^= 1;\n flip[pre] ^= 1;\n }\n if(pre == -1){\n for (int k = 0; k < n; ++k) {\n for (int l = k+1; l < n; ++l) {\n int f = (flip[k]^flip[l]);\n if(!f and e[k][l] > 0){\n eg[k][l] = true; m++;\n sum += e[k][l];\n }\n else if(f and e[k][l] < 0){\n eg[k][l] = true; m++;\n sum -= e[k][l];\n }\n else{\n eg[k][l] = false;\n }\n }\n }\n }\n else{\n auto update = [&](int x,int y)->void{\n if(x > y) swap(x, y);\n if(eg[x][y]){\n m--; eg[x][y] = false;\n sum -= abs(e[x][y]);\n }\n else{\n m++; eg[x][y] = true;\n sum += abs(e[x][y]);\n }\n };\n for (int k = 0; k < n; ++k) {\n if(k == pre) continue;\n update(pre, k);\n }\n for (int k = 0; k < n; ++k) {\n if(k == j) continue;\n update(j, k);\n }\n }\n if(m == n-1 and sum < res){\n UnionFind uf(n);\n for (int k = 0; k < n; ++k) {\n for (int l = k+1; l < n; ++l) {\n if(eg[k][l]){\n uf.unite(k, l);\n }\n }\n }\n if(uf.size(0) == n) res = min(res, sum);\n }\n pre = j;\n }\n }\n\n if(res == 1e9) res = -1;\n cout << res << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 3932, "score_of_the_acc": -0.296, "final_rank": 5 } ]
aoj_1646_cpp
Avoiding Three Cs You are a member of ICPC Company, and planning to place seats in the new office. Your task is to place as many seats as possible in the room while satisfying the conditions for avoiding three Cs . You decided to solve the problem through simulation on a grid. In the simulation, the room is expressed by a rectangular grid. Each cell in the grid is either empty where you can place a seat, or a wall cell where you cannot place a seat. You cannot place two or more seats in the same empty cell to keep the distance between seats. Hereinafter, a route starting from the cell at the northwest corner, repeating moves to the east or to the south, finally reaching the cell at the southeast corner, is called a ventilation route. There may exist many ventilation routes. For any cell with a seat placed, it must be on at least one ventilation route. Moreover, the number of seats on any ventilation route, called the seat passing number, must not exceed the given maximum. Your task is to find seat placements in the room that have as many seats as possible. Input The input consists of multiple datasets. Each dataset is represented in the following format. n m k a 1,1 ... a 1, m ... a n, 1 ... a n,m n and m are the height and width of the given grid. n and m are positive integers not exceeding 20. k is the maximum allowed seat passing number. k is a positive integer not exceeding n + m − 1. Each of the following n lines contains m characters, either ' . ' or ' # ', denoting the cells of the grid. If a i,j equals ' . ', the corresponding cell is empty. If it equals ' # ', the cell is a wall. The cell at row 1 and column 1 is at the northwest corner of the room, and the cell at row n and column m is at the southeast corner. The end of the input is indicated by a line containing three zeros. The number of datasets does not exceed 100. Output For each of the datasets, output the following n + 1 lines. The first line should contain the maximum possible number of seats that can be placed. The second and subsequent lines should contain one of the placements with the maximum number of seats. The output of the placements should be in the same format as given in the input, except that cells with a seat placed should be represented as ' @ ' instead of ' . '. If there are two or more placements with the maximum possible number of seats, output one with the first in the dictionary order in the ASCII codes when all the lines of their representations are concatenated. Note that ' # ' < ' . ' < ' @ ' holds in ASCII codes. Sample Input 3 3 3 ... .#. ... 3 3 1 ... .#. ... 6 11 1 ........... .....#..... .....#..... .....#..... .....#..... ........... 7 7 3 ....... .##.##. .#...#. .#...#. .#...#. .##.##. ....... 0 0 0 Output for the Sample Input 6 .@@ @#@ @@. 2 ... .#@ .@. 10 ..........@ ....@#...@. ...@.#..@.. ..@..#.@... .@...#@.... @... ...(truncated)
[ { "submission_id": "aoj_1646_9417671", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n mcf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap, cost});\n g[to].push_back(_edge{from, from_id, 0, -cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{\n pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,\n };\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result(m);\n for (int i = 0; i < m; i++) {\n result[i] = get_edge(i);\n }\n return result;\n }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n std::vector<Cost> dual(_n, 0), dist(_n);\n std::vector<int> pv(_n), pe(_n);\n std::vector<bool> vis(_n);\n auto dual_ref = [&]() {\n std::fill(dist.begin(), dist.end(),\n std::numeric_limits<Cost>::max());\n std::fill(pv.begin(), pv.end(), -1);\n std::fill(pe.begin(), pe.end(), -1);\n std::fill(vis.begin(), vis.end(), false);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::priority_queue<Q> que;\n dist[s] = 0;\n que.push(Q{0, s});\n while (!que.empty()) {\n int v = que.top().to;\n que.pop();\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n for (int i = 0; i < int(g[v].size()); i++) {\n auto e = g[v][i];\n if (vis[e.to] || !e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual[e.to] + dual[v];\n if (dist[e.to] - dist[v] > cost) {\n dist[e.to] = dist[v] + cost;\n pv[e.to] = v;\n pe[e.to] = i;\n que.push(Q{dist[e.to], e.to});\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])\n // = - shortest(s, t) + dual[t] + shortest(s, v)\n // = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C\n dual[v] -= dist[t] - dist[v];\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result;\n result.push_back({flow, cost});\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = pv[v]) {\n c = std::min(c, g[pv[v]][pe[v]].cap);\n }\n for (int v = t; v != s; v = pv[v]) {\n auto& e = g[pv[v]][pe[v]];\n e.cap -= c;\n g[v][e.rev].cap += c;\n }\n Cost d = -dual[s];\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n\n private:\n int _n;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n\n\n// なにこれ\n// 全然正当性がよく分からない\n\nvoid solve(int n, int m,int k){\n\tvector<string> a(n);\n\trep(i,0,n) cin >> a[i];\n\t\n\trep(i,0,n){\n\t\trep(j,0,m){\n\t\t\tif (a[i][j] == '.') a[i][j] = '?';\n\t\t}\n\t}\n\n\tauto check = [&]() -> int {\n\t\tint siz = n*m;\n\t\tatcoder::mcf_graph<int,int> mcf(2*siz+2);\n\t\tmcf.add_edge(2*siz, 0, siz, 0);\n\t\tmcf.add_edge(2*siz-1, 2*siz+1, siz, 0);\n\t\t\n\t\trep(i,0,n){\n\t\t\trep(j,0,m){\n\t\t\t\tif (a[i][j] == '?' || a[i][j] == '@'){\n\t\t\t\t\tmcf.add_edge(i*m+j, i*m+j+siz, 1, 0);\n\t\t\t\t}\n\t\t\t\tmcf.add_edge(i*m+j, i*m+j+siz, siz, 1);\n\t\t\t}\n\t\t}\n\n\t\trep(i,0,n-1){\n\t\t\trep(j,0,m){\n\t\t\t\tif (a[i][j] == '#' || a[i+1][j] == '#') continue;\n\t\t\t\tmcf.add_edge(i*m+j+siz, (i+1)*m+j, siz, 0);\n\t\t\t}\n\t\t}\n\t\t\n\t\trep(i,0,n){\n\t\t\trep(j,0,m-1){\n\t\t\t\tif (a[i][j] == '#' || a[i][j+1] == '#') continue;\n\t\t\t\tmcf.add_edge(i*m+j+siz, i*m+j+1, siz, 0);\n\t\t\t}\n\t\t}\n\n\t\tvector<pair<int,int>> sl = mcf.slope(siz*2, siz*2+1);\n\t\tint ret = 0;\n\t\trep(i,0,(int)sl.size()-1){\n\t\t\tint now_cost = sl[i].second;\n\t\t\tint nxt_cost = sl[i+1].second;\n\t\t\tint now_cap = sl[i].first;\n\t\t\tint nxt_cap = sl[i+1].first;\n\n\t\t\tint increase = n+m-1 - (nxt_cost - now_cost) / (nxt_cap - now_cap);\n\n\t\t\trep(x,0,nxt_cap - now_cap){\n\t\t\t\tret += min(k, increase);\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t};\n\n\tvector<vector<int>> dp(n, vector<int>(m));\n\tint max_seat = check();\n\n\trep(i,0,n){\n\t\trep(j,0,m){\n\t\t\tif (a[i][j] == '#') continue;\n\t\t\t\n\t\t\tif (i-1 >= 0) dp[i][j] = max(dp[i][j], dp[i-1][j]);\n\t\t\tif (j-1 >= 0) dp[i][j] = max(dp[i][j], dp[i][j-1]);\n\t\t\t\n\t\t\tif (dp[i][j] == k){\n\t\t\t\ta[i][j] = '.';\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\n\t\t\ta[i][j] = '.';\n\t\t\tif (check() != max_seat){\n\t\t\t\ta[i][j] = '@';\n\t\t\t\tdp[i][j] += 1;\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << max_seat << '\\n';\n\trep(i,0,n){\n\t\tcout << a[i] << '\\n';\n\t}\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\t\n\twhile(true){\n\t\tint n, m, k; cin >> n >> m >> k;\n\t\tif (n == 0) return 0;\n\t\tsolve(n, m, k);\n\t}\n}", "accuracy": 1, "time_ms": 6430, "memory_kb": 3572, "score_of_the_acc": -0.7898, "final_rank": 2 }, { "submission_id": "aoj_1646_6067051", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n\n\n#define MAX 60\n#define NUM 805\n#define SIZE 25\n\n\n\n//辺を表す構造体{行先、容量、コスト、逆辺のインデックス}\nstruct Edge{\n\tEdge(int arg_to,int arg_capacity,int arg_cost,int arg_rev_index){\n\t\tto = arg_to;\n\t\tcapacity = arg_capacity;\n\t\tcost = arg_cost;\n\t\trev_index = arg_rev_index;\n\t}\n\n\tint to,cost,capacity,rev_index;\n};\n\n\nint H,W,K;\nchar input[SIZE][SIZE],ANS[SIZE][SIZE];\n\n\nint V; //頂点数\nvector<Edge> G[NUM]; //グラフの隣接リスト表現\nint h[NUM]; //ポテンシャル\nint dist[NUM]; //最短距離\nint pre_node[NUM],pre_edge[NUM]; //直前の頂点と辺\nint IN[405],OUT[405],dp[SIZE][SIZE];\n\n\nvoid outPut(){\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tprintf(\"%c\",ANS[row][col]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\nint getIndex(int row,int col){\n\n\treturn row*W + col;\n}\n\nbool is_R(int row,int col){\n\n\treturn col+1 < W && input[row][col+1] != '#';\n}\n\nbool is_L(int row,int col){\n\n\treturn col-1 >= 0 && input[row][col-1] != '#';\n}\n\n\nbool is_U(int row,int col){\n\n\treturn row-1 >= 0 && input[row-1][col] != '#';\n}\n\nbool is_D(int row,int col){\n\n\treturn row+1 <= H-1 && input[row+1][col] != '#';\n}\n\n\n\n\ntypedef pair<int,int> P; //firstは最短距離、secondは頂点の番号\n\n\n\n//fromからtoへ向かう容量capacity,コストcostの辺をグラフに追加する\nvoid add_edge(int from,int to,int capacity,int cost){\n\tG[from].push_back(Edge(to,capacity,cost,G[to].size()));\n\tG[to].push_back(Edge(from,0,-cost,G[from].size()-1));\n}\n\nint min_cost_flow(int source,int sink,int flow){\n\n\tint ret = 0;\n\tfor(int i = 0; i < V; i++)h[i] = 0; //ポテンシャルを0にする\n\n\twhile(flow > 0){\n\t\t//ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P,vector<P>,greater<P>> Q;\n\n\t\tfor(int i = 0; i < V; i++)dist[i] = BIG_NUM;\n\n\t\tdist[source] = 0;\n\t\tQ.push(P(0,source));\n\n\t\twhile(!Q.empty()){\n\t\t\tP p = Q.top();\n\t\t\tQ.pop();\n\t\t\tint node_id = p.second;\n\t\t\tif(dist[node_id] < p.first)continue; //最短でなければSKIP\n\t\t\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\t\t\tEdge &e = G[node_id][i];\n\t\t\t\tif(e.capacity > 0 && dist[e.to] > dist[node_id]+e.cost+h[node_id]-h[e.to]){\n\t\t\t\t\tdist[e.to] = dist[node_id]+e.cost+h[node_id]-h[e.to];\n\t\t\t\t\tpre_node[e.to] = node_id;\n\t\t\t\t\tpre_edge[e.to] = i;\n\t\t\t\t\tQ.push(P(dist[e.to],e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(dist[sink] == BIG_NUM){\n\t\t\t//これ以上流せない\n\t\t\treturn ret;\n\t\t}\n\n\t\tfor(int node_id = 0; node_id < V; node_id++)h[node_id] += dist[node_id];\n\n\t\t//source-sink間最短路に沿って目いっぱい流す\n\t\tint tmp_flow = flow;\n\t\tfor(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\t\t\ttmp_flow = min(tmp_flow,G[pre_node[node_id]][pre_edge[node_id]].capacity);\n\t\t}\n\t\tflow -= tmp_flow;\n\n\t\tint mult = min(K,(H+W-1)-h[sink]); //■Kの制約\n\n\t\tret += tmp_flow*mult;\n\n\t\tfor(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\t\t\tEdge &e = G[pre_node[node_id]][pre_edge[node_id]];\n\t\t\te.capacity -= tmp_flow;\n\t\t\tG[node_id][e.rev_index].capacity += tmp_flow;\n\t\t}\n\t}\n\treturn ret;\n}\n\n\n\nint calc_maxi(int source,int sink,int not_row,int not_col){\n\n\n\tfor(int i = 0; i < NUM; i++){\n\n\t\tG[i].clear();\n\t}\n\n\tint CAP = H*W;\n\n\tadd_edge(source,IN[getIndex(0,0)],CAP,0);\n\tadd_edge(OUT[getIndex(H-1,W-1)],sink,CAP,0);\n\n\tfor(ll row = 0; row < H; row++){\n\t\tfor(ll col = 0; col < W; col++){\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tint id = getIndex(row,col);\n\n\t\t\tif((row == not_row && col == not_col) || (ANS[row][col] == '-')){ //■置かない\n\n\t\t\t\tadd_edge(IN[id],OUT[id],CAP,1);\n\n\t\t\t}else{ //■置く\n\n\t\t\t\tadd_edge(IN[id],OUT[id],1,0);\n\t\t\t\tadd_edge(IN[id],OUT[id],CAP-1,1);\n\t\t\t}\n\n\t\t\tif(is_R(row,col)){\n\t\t\t\tint adj = getIndex(row,col+1);\n\t\t\t\tadd_edge(OUT[id],IN[adj],CAP,0);\n\t\t\t}\n\t\t\tif(is_D(row,col)){\n\t\t\t\tint adj = getIndex(row+1,col);\n\t\t\t\tadd_edge(OUT[id],IN[adj],CAP,0);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn min_cost_flow(source,sink,CAP);\n}\n\n\nvoid func(){\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",input[row]);\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tANS[row][col] = input[row][col];\n\t\t}\n\t}\n\n\tif(input[0][0] == '#' || input[H-1][W-1] == '#'){\n\n\t\tprintf(\"0\\n\");\n\t\toutPut();\n\t\treturn;\n\t}\n\n\tint index = 0;\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tll id = getIndex(row,col);\n\t\t\tIN[id] = index++;\n\t\t\tOUT[id] = index++;\n\t\t}\n\t}\n\n\tint source = index,sink = index+1;\n\tV = index+2;\n\n\t//最大の設置個数を求める\n\tint maxi = calc_maxi(source,sink,-1,-1);\n\tprintf(\"%d\\n\",maxi);\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tdp[row][col] = 0;\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tif(row-1 >= 0){\n\n\t\t\t\tdp[row][col] = max(dp[row][col],dp[row-1][col]);\n\t\t\t}\n\t\t\tif(col-1 >= 0){\n\n\t\t\t\tdp[row][col] = max(dp[row][col],dp[row][col-1]);\n\t\t\t}\n\n\t\t\tif(dp[row][col] == K){ //もう置けない\n\n\t\t\t\tANS[row][col] = '-';\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tll tmp = calc_maxi(source,sink,row,col);\n\n\t\t\tif(tmp != maxi){ //■置かないと最大値を実現できない\n\n\t\t\t\tANS[row][col] = '@';\n\t\t\t\tdp[row][col]++;\n\n\t\t\t}else{ //■置かなくても最大値を実現できる\n\n\t\t\t\tANS[row][col] = '-'; //■\"@\"をなるべく後ろに置く\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(ANS[row][col] == '-'){\n\n\t\t\t\tANS[row][col] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\toutPut();\n\tfflush(stdout);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&H,&W,&K);\n\t\tif(H == 0 && W == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7630, "memory_kb": 3672, "score_of_the_acc": -1.918, "final_rank": 6 }, { "submission_id": "aoj_1646_6067038", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n\n\n#define MAX 60\n#define NUM 805\n#define SIZE 25\n\n\n\n//辺を表す構造体{行先、容量、コスト、逆辺のインデックス}\nstruct Edge{\n\tEdge(int arg_to,int arg_capacity,int arg_cost,int arg_rev_index){\n\t\tto = arg_to;\n\t\tcapacity = arg_capacity;\n\t\tcost = arg_cost;\n\t\trev_index = arg_rev_index;\n\t}\n\n\tint to,cost,capacity,rev_index;\n};\n\n\nint H,W,K;\nchar input[SIZE][SIZE],ANS[SIZE][SIZE];\n\n\nint V; //頂点数\nvector<Edge> G[NUM]; //グラフの隣接リスト表現\nint h[NUM]; //ポテンシャル\nint dist[NUM]; //最短距離\nint pre_node[NUM],pre_edge[NUM]; //直前の頂点と辺\nint IN[405],OUT[405],dp[SIZE][SIZE];\n\n\nvoid outPut(){\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tprintf(\"%c\",ANS[row][col]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\nint getIndex(int row,int col){\n\n\treturn row*W + col;\n}\n\nbool is_R(int row,int col){\n\n\treturn col+1 < W && input[row][col+1] != '#';\n}\n\nbool is_L(int row,int col){\n\n\treturn col-1 >= 0 && input[row][col-1] != '#';\n}\n\n\nbool is_U(int row,int col){\n\n\treturn row-1 >= 0 && input[row-1][col] != '#';\n}\n\nbool is_D(int row,int col){\n\n\treturn row+1 <= H-1 && input[row+1][col] != '#';\n}\n\n\n\n\ntypedef pair<int,int> P; //firstは最短距離、secondは頂点の番号\n\n\n\n//fromからtoへ向かう容量capacity,コストcostの辺をグラフに追加する\nvoid add_edge(int from,int to,int capacity,int cost){\n\tG[from].push_back(Edge(to,capacity,cost,G[to].size()));\n\tG[to].push_back(Edge(from,0,-cost,G[from].size()-1));\n}\n\nint min_cost_flow(int source,int sink,int flow){\n\n\tint ret = 0;\n\tfor(int i = 0; i < V; i++)h[i] = 0; //ポテンシャルを0にする\n\n\twhile(flow > 0){\n\t\t//ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P,vector<P>,greater<P>> Q;\n\n\t\tfor(int i = 0; i < V; i++)dist[i] = BIG_NUM;\n\n\t\tdist[source] = 0;\n\t\tQ.push(P(0,source));\n\n\t\twhile(!Q.empty()){\n\t\t\tP p = Q.top();\n\t\t\tQ.pop();\n\t\t\tint node_id = p.second;\n\t\t\tif(dist[node_id] < p.first)continue; //最短でなければSKIP\n\t\t\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\t\t\tEdge &e = G[node_id][i];\n\t\t\t\tif(e.capacity > 0 && dist[e.to] > dist[node_id]+e.cost+h[node_id]-h[e.to]){\n\t\t\t\t\tdist[e.to] = dist[node_id]+e.cost+h[node_id]-h[e.to];\n\t\t\t\t\tpre_node[e.to] = node_id;\n\t\t\t\t\tpre_edge[e.to] = i;\n\t\t\t\t\tQ.push(P(dist[e.to],e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(dist[sink] == BIG_NUM){\n\t\t\t//これ以上流せない\n\t\t\treturn ret;\n\t\t}\n\n\t\tfor(int node_id = 0; node_id < V; node_id++)h[node_id] += dist[node_id];\n\n\t\t//source-sink間最短路に沿って目いっぱい流す\n\t\tint tmp_flow = flow;\n\t\tfor(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\t\t\ttmp_flow = min(tmp_flow,G[pre_node[node_id]][pre_edge[node_id]].capacity);\n\t\t}\n\t\tflow -= tmp_flow;\n\n\t\tint mult = min(K,(H+W-1)-h[sink]); //■Kの制約\n\n\t\tret += tmp_flow*mult;\n\n\t\tfor(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\t\t\tEdge &e = G[pre_node[node_id]][pre_edge[node_id]];\n\t\t\te.capacity -= tmp_flow;\n\t\t\tG[node_id][e.rev_index].capacity += tmp_flow;\n\t\t}\n\t}\n\treturn ret;\n}\n\n\n\nint calc_maxi(int source,int sink,int not_row,int not_col){\n\n\n\tfor(int i = 0; i < NUM; i++){\n\n\t\tG[i].clear();\n\t}\n\n\tint CAP = H*W; //■1回の流れでは、なるべくコストを小さくするので、集められるだけの@が集まる\n\n\tadd_edge(source,IN[getIndex(0,0)],CAP,0);\n\tadd_edge(OUT[getIndex(H-1,W-1)],sink,CAP,0);\n\n\tfor(ll row = 0; row < H; row++){\n\t\tfor(ll col = 0; col < W; col++){\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tint id = getIndex(row,col);\n\n\t\t\tif((row == not_row && col == not_col) || (ANS[row][col] == '-')){ //■置かない\n\n\t\t\t\tadd_edge(IN[id],OUT[id],CAP,1);\n\n\t\t\t}else{ //■置く\n\n\t\t\t\tadd_edge(IN[id],OUT[id],1,0);\n\t\t\t\tadd_edge(IN[id],OUT[id],CAP-1,1);\n\t\t\t}\n\n\t\t\tif(is_R(row,col)){\n\t\t\t\tint adj = getIndex(row,col+1);\n\t\t\t\tadd_edge(OUT[id],IN[adj],CAP,0);\n\t\t\t}\n\t\t\tif(is_D(row,col)){\n\t\t\t\tint adj = getIndex(row+1,col);\n\t\t\t\tadd_edge(OUT[id],IN[adj],CAP,0);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn min_cost_flow(source,sink,CAP);\n}\n\n\nvoid func(){\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",input[row]);\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tANS[row][col] = input[row][col];\n\t\t}\n\t}\n\n\tif(input[0][0] == '#' || input[H-1][W-1] == '#'){\n\n\t\tprintf(\"0\\n\");\n\t\toutPut();\n\t\treturn;\n\t}\n\n\tint index = 0;\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tll id = getIndex(row,col);\n\t\t\tIN[id] = index++;\n\t\t\tOUT[id] = index++;\n\t\t}\n\t}\n\n\tint source = index,sink = index+1;\n\tV = index+2;\n\n\t//最大の設置個数を求める\n\tint maxi = calc_maxi(source,sink,-1,-1);\n\tprintf(\"%d\\n\",maxi);\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tdp[row][col] = 0;\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tif(row-1 >= 0){\n\n\t\t\t\tdp[row][col] = max(dp[row][col],dp[row-1][col]);\n\t\t\t}\n\t\t\tif(col-1 >= 0){\n\n\t\t\t\tdp[row][col] = max(dp[row][col],dp[row][col-1]);\n\t\t\t}\n\n\t\t\tif(dp[row][col] == K){ //もう置けない\n\n\t\t\t\tANS[row][col] = '-';\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tll tmp = calc_maxi(source,sink,row,col);\n\n\t\t\tif(tmp != maxi){ //■置かないと最大値を実現できない\n\n\t\t\t\tANS[row][col] = '@';\n\t\t\t\tdp[row][col]++;\n\n\t\t\t}else{ //■置かなくても最大値を実現できる\n\n\t\t\t\tANS[row][col] = '-'; //■\"@\"をなるべく後ろに置く\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(ANS[row][col] == '-'){\n\n\t\t\t\tANS[row][col] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\toutPut();\n\tfflush(stdout);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&H,&W,&K);\n\t\tif(H == 0 && W == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7630, "memory_kb": 3588, "score_of_the_acc": -1.1938, "final_rank": 5 }, { "submission_id": "aoj_1646_6067036", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n\n\n#define MAX 60\n#define NUM 805\n#define SIZE 25\n\n\n\n//辺を表す構造体{行先、容量、コスト、逆辺のインデックス}\nstruct Edge{\n\tEdge(int arg_to,int arg_capacity,int arg_cost,int arg_rev_index){\n\t\tto = arg_to;\n\t\tcapacity = arg_capacity;\n\t\tcost = arg_cost;\n\t\trev_index = arg_rev_index;\n\t}\n\n\tint to,cost,capacity,rev_index;\n};\n\n\nint H,W,K;\nchar input[SIZE][SIZE],ANS[SIZE][SIZE];\n\n\nint V; //頂点数\nvector<Edge> G[NUM]; //グラフの隣接リスト表現\nint h[NUM]; //ポテンシャル\nint dist[NUM]; //最短距離\nint pre_node[NUM],pre_edge[NUM]; //直前の頂点と辺\nint IN[405],OUT[405],dp[SIZE][SIZE];\n\n\nvoid outPut(){\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tprintf(\"%c\",ANS[row][col]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\nint getIndex(int row,int col){\n\n\treturn row*W + col;\n}\n\nbool is_R(int row,int col){\n\n\treturn col+1 < W && input[row][col+1] != '#';\n}\n\nbool is_L(int row,int col){\n\n\treturn col-1 >= 0 && input[row][col-1] != '#';\n}\n\n\nbool is_U(int row,int col){\n\n\treturn row-1 >= 0 && input[row-1][col] != '#';\n}\n\nbool is_D(int row,int col){\n\n\treturn row+1 <= H-1 && input[row+1][col] != '#';\n}\n\n\n\n\ntypedef pair<ll,int> P; //firstは最短距離、secondは頂点の番号\n\n\n\n//fromからtoへ向かう容量capacity,コストcostの辺をグラフに追加する\nvoid add_edge(int from,int to,int capacity,int cost){\n\tG[from].push_back(Edge(to,capacity,cost,G[to].size()));\n\tG[to].push_back(Edge(from,0,-cost,G[from].size()-1));\n}\n\nint min_cost_flow(int source,int sink,int flow){\n\n\tint ret = 0;\n\tfor(int i = 0; i < V; i++)h[i] = 0; //ポテンシャルを0にする\n\n\twhile(flow > 0){\n\t\t//ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P,vector<P>,greater<P>> Q;\n\n\t\tfor(int i = 0; i < V; i++)dist[i] = BIG_NUM;\n\n\t\tdist[source] = 0;\n\t\tQ.push(P(0,source));\n\n\t\twhile(!Q.empty()){\n\t\t\tP p = Q.top();\n\t\t\tQ.pop();\n\t\t\tint node_id = p.second;\n\t\t\tif(dist[node_id] < p.first)continue; //最短でなければSKIP\n\t\t\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\t\t\tEdge &e = G[node_id][i];\n\t\t\t\tif(e.capacity > 0 && dist[e.to] > dist[node_id]+e.cost+h[node_id]-h[e.to]){\n\t\t\t\t\tdist[e.to] = dist[node_id]+e.cost+h[node_id]-h[e.to];\n\t\t\t\t\tpre_node[e.to] = node_id;\n\t\t\t\t\tpre_edge[e.to] = i;\n\t\t\t\t\tQ.push(P(dist[e.to],e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(dist[sink] == BIG_NUM){\n\t\t\t//これ以上流せない\n\t\t\treturn ret;\n\t\t}\n\n\t\tfor(int node_id = 0; node_id < V; node_id++)h[node_id] += dist[node_id];\n\n\t\t//source-sink間最短路に沿って目いっぱい流す\n\t\tint tmp_flow = flow;\n\t\tfor(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\t\t\ttmp_flow = min(tmp_flow,G[pre_node[node_id]][pre_edge[node_id]].capacity);\n\t\t}\n\t\tflow -= tmp_flow;\n\n\t\t//printf(\"tmp_flow:%lld\\n\",tmp_flow);\n\n\t\tint mult = min(K,(H+W-1)-h[sink]);\n\t\t//printf(\"mult:%lld\\n\",mult);\n\n\t\tret += tmp_flow*mult;\n\n\t\t//printf(\"ret:%lld\\n\",ret);\n\n\t\tfor(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\t\t\tEdge &e = G[pre_node[node_id]][pre_edge[node_id]];\n\t\t\te.capacity -= tmp_flow;\n\t\t\tG[node_id][e.rev_index].capacity += tmp_flow;\n\t\t}\n\t}\n\treturn ret;\n}\n\n\n\nint calc_maxi(int source,int sink,int not_row,int not_col){\n\n\n\tfor(int i = 0; i < NUM; i++){\n\n\t\tG[i].clear();\n\t}\n\n\tint CAP = H*W; //■1回の流れでは、なるべくコストを小さくするので、集められるだけの@が集まる\n\n\tadd_edge(source,IN[getIndex(0,0)],CAP,0);\n\tadd_edge(OUT[getIndex(H-1,W-1)],sink,CAP,0);\n\n\tfor(ll row = 0; row < H; row++){\n\t\tfor(ll col = 0; col < W; col++){\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tint id = getIndex(row,col);\n\n\t\t\tif((row == not_row && col == not_col) || (ANS[row][col] == '-')){ //■置かない\n\n\t\t\t\tadd_edge(IN[id],OUT[id],CAP,1);\n\n\t\t\t}else{ //■置く\n\n\t\t\t\tadd_edge(IN[id],OUT[id],1,0);\n\t\t\t\tadd_edge(IN[id],OUT[id],CAP-1,1);\n\t\t\t}\n\n\t\t\tif(is_R(row,col)){\n\t\t\t\tint adj = getIndex(row,col+1);\n\t\t\t\tadd_edge(OUT[id],IN[adj],CAP,0);\n\t\t\t}\n\t\t\tif(is_D(row,col)){\n\t\t\t\tint adj = getIndex(row+1,col);\n\t\t\t\tadd_edge(OUT[id],IN[adj],CAP,0);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn min_cost_flow(source,sink,CAP);\n}\n\n\nvoid func(){\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",input[row]);\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tANS[row][col] = input[row][col];\n\t\t}\n\t}\n\n\tif(input[0][0] == '#' || input[H-1][W-1] == '#'){\n\n\t\tprintf(\"0\\n\");\n\t\toutPut();\n\t\treturn;\n\t}\n\n\tint index = 0;\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tll id = getIndex(row,col);\n\t\t\tIN[id] = index++;\n\t\t\tOUT[id] = index++;\n\t\t}\n\t}\n\n\tint source = index,sink = index+1;\n\tV = index+2;\n\n\t//最大の設置個数を求める\n\tint maxi = calc_maxi(source,sink,-1,-1);\n\tprintf(\"%d\\n\",maxi);\n\t//return;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tdp[row][col] = 0;\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(input[row][col] == '#')continue;\n\n\t\t\tif(row-1 >= 0){\n\n\t\t\t\tdp[row][col] = max(dp[row][col],dp[row-1][col]);\n\t\t\t}\n\t\t\tif(col-1 >= 0){\n\n\t\t\t\tdp[row][col] = max(dp[row][col],dp[row][col-1]);\n\t\t\t}\n\n\t\t\tif(dp[row][col] == K){ //もう置けない\n\n\t\t\t\tANS[row][col] = '-';\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tll tmp = calc_maxi(source,sink,row,col);\n\t\t\t//printf(\"row:%lld col:%lld tmp:%lld\\n\",row,col,tmp);\n\n\t\t\tif(tmp != maxi){ //■置かないと最大値を実現できない\n\n\t\t\t\tANS[row][col] = '@';\n\t\t\t\tdp[row][col]++;\n\n\t\t\t}else{ //■置かなくても最大値を実現できる\n\n\t\t\t\tANS[row][col] = '-'; //■\"@\"をなるべく後ろに置く\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(ANS[row][col] == '-'){\n\n\t\t\t\tANS[row][col] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\toutPut();\n\tfflush(stdout);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&H,&W,&K);\n\t\tif(H == 0 && W == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 8000, "memory_kb": 3556, "score_of_the_acc": -1, "final_rank": 4 }, { "submission_id": "aoj_1646_5645383", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({from, to, cap, 0, cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}});\n elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}});\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n } else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n } else {\n que.push_back(Q{dist_to, e.to});\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = {{Cap(0), Cost(0)}};\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\nint main() {\n int N, M, K;\n while (cin >> N >> M >> K, N) {\n vector<string> A(N);\n for (auto&& a : A) {\n cin >> a;\n }\n auto f = [&](int x, int y) { return x * M + y; };\n\n auto calc = [&](int xx, int yy) {\n mcf_graph<int, int> G(N * M * 2);\n for (int x = 0; x < N; x++) {\n for (int y = 0; y < M; y++) {\n G.add_edge(f(x, y), f(x, y) + N * M, ((A[x][y] == '.' || A[x][y] == '@') && !(x == xx && y == yy)) ? 1 : 0, 0);\n }\n }\n for (int x = 0; x < N; x++) {\n for (int y = 0; y < M; y++) {\n G.add_edge(f(x, y), f(x, y) + N * M, N + M, 1);\n }\n }\n for (int x = 0; x < N; x++) {\n for (int y = 0; y < M; y++) {\n if (A[x][y] == '#')\n continue;\n if (x < N - 1 && A[x + 1][y] != '#')\n G.add_edge(f(x, y) + N * M, f(x + 1, y), N + M, 0);\n if (y < M - 1 && A[x][y + 1] != '#')\n G.add_edge(f(x, y) + N * M, f(x, y + 1), N + M, 0);\n }\n }\n auto Slope = G.slope(0, 2 * N * M - 1);\n vector<int> costs = {0};\n for (int i = 0; i < (int)Slope.size() - 1; i++) {\n auto [cap_now, cost_now] = Slope[i];\n auto [cap_nxt, cost_nxt] = Slope[i + 1];\n int cost = (cost_nxt - cost_now) / (cap_nxt - cap_now);\n int score = N + M - 1 - cost;\n if (score == 0)\n break;\n for (int j = cap_now + 1; j <= cap_nxt; j++) {\n costs.emplace_back(score);\n }\n }\n int ret = 0;\n for (auto&& x : costs) {\n ret += min(K, x);\n }\n return ret;\n };\n\n int ans = calc(-1, -1);\n\n int cnt[30][30] = {};\n for (int xx = 0; xx < N; xx++) {\n for (int yy = 0; yy < M; yy++) {\n if (A[xx][yy] == '#')\n continue;\n if (cnt[xx][yy] < K && calc(xx, yy) != ans) {\n A[xx][yy] = '@';\n cnt[xx][yy]++;\n } else\n A[xx][yy] = '*';\n cnt[xx + 1][yy] = max(cnt[xx + 1][yy], cnt[xx][yy]);\n cnt[xx][yy + 1] = max(cnt[xx][yy + 1], cnt[xx][yy]);\n }\n }\n for (auto&& a : A) {\n for (auto&& c : a) {\n if (c == '*')\n c = '.';\n }\n }\n\n cout << ans << endl;\n for (auto&& a : A) {\n cout << a << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 3490, "memory_kb": 3628, "score_of_the_acc": -0.6207, "final_rank": 1 }, { "submission_id": "aoj_1646_5645341", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({from, to, cap, 0, cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}});\n elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}});\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n } else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n } else {\n que.push_back(Q{dist_to, e.to});\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = {{Cap(0), Cost(0)}};\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\nint main() {\n int N, M, K;\n while (cin >> N >> M >> K, N) {\n vector<string> A(N);\n for (auto&& a : A) {\n cin >> a;\n }\n auto f = [&](int x, int y) { return x * M + y; };\n\n auto calc = [&](int xx, int yy) {\n mcf_graph<int, int> G(N * M * 2);\n for (int x = 0; x < N; x++) {\n for (int y = 0; y < M; y++) {\n G.add_edge(f(x, y), f(x, y) + N * M, ((A[x][y] == '.' || A[x][y] == '@') && !(x == xx && y == yy)) ? 1 : 0, 1);\n }\n }\n for (int x = 0; x < N; x++) {\n for (int y = 0; y < M; y++) {\n G.add_edge(f(x, y), f(x, y) + N * M, N + M, 100);\n }\n }\n for (int x = 0; x < N; x++) {\n for (int y = 0; y < M; y++) {\n if (A[x][y] == '#')\n continue;\n if (x < N - 1 && A[x + 1][y] != '#')\n G.add_edge(f(x, y) + N * M, f(x + 1, y), N + M, 0);\n if (y < M - 1 && A[x][y + 1] != '#')\n G.add_edge(f(x, y) + N * M, f(x, y + 1), N + M, 0);\n }\n }\n auto Slope = G.slope(0, 2 * N * M - 1);\n vector<int> costs = {0};\n for (int i = 0; i < (int)Slope.size() - 1; i++) {\n auto [cap_now, cost_now] = Slope[i];\n auto [cap_nxt, cost_nxt] = Slope[i + 1];\n int cost = (cost_nxt - cost_now) / (cap_nxt - cap_now);\n if (cost % 100 == 0)\n break;\n for (int j = cap_now + 1; j <= cap_nxt; j++) {\n costs.emplace_back(cost % 100);\n }\n }\n int ret = 0;\n for (auto&& x : costs) {\n ret += min(K, x);\n }\n return ret;\n };\n\n int ans = calc(-1, -1);\n\n for (int xx = 0; xx < N; xx++) {\n for (int yy = 0; yy < M; yy++) {\n if (A[xx][yy] == '#')\n continue;\n if (calc(xx, yy) != ans)\n A[xx][yy] = '@';\n else\n A[xx][yy] = '*';\n }\n }\n for (auto&& a : A) {\n for (auto&& c : a) {\n if (c == '*')\n c = '.';\n }\n }\n\n cout << ans << endl;\n for (auto&& a : A) {\n cout << a << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 4260, "memory_kb": 3628, "score_of_the_acc": -0.7914, "final_rank": 3 } ]
aoj_1643_cpp
Icpcan Alphabet Figure D-1 A picture of two expressions engraved on a stone wall. Research on the civilization of old kingdom of Icpca has been carried out at the Investigation Center for Prehistoric Civilization (ICPC). Their recent excavation found two expressions engraved on a stone wall at the ruins of Icpca. The expressions are composed of n distinct Icpca letters a 1 , ..., a n , symbols corresponding to inequality signs ('<' and '>') and parentheses (' ( ' and ' ) '). In what follows, they are represented by capital Roman letters and ordinary symbols for description ease. Expressions conform to the following grammar rules with the start symbol E . E ::= F | '(' E '<' E ')' | '(' E '>' E ')' F ::= a 1 | a 2 | ... | a n Analyses unifying multifarious knowledge on the old kingdom and its civilization revealed the following evaluation rules. A total order relation is defined on the set of letters a 1 , ..., a n . When an expression consists only of a single letter, the value of the expression is that very letter. For two expressions P and Q with values a i and a j , respectively, the value of the expression ( P < Q ) is one of the letters a i or a j that precedes in the total order. If the two letters are the same, the value is that letter. Similarly, for two expressions P and Q with values a i and a j , respectively, the value of the expression ( P > Q ) is one of the letters a i or a j that comes later in the total order. If the two letters are the same, the value is that letter. It was also found that the values of the two expressions discovered must be equal. How many different total orders, among n ! possible total orders, make the values of the two expressions equal? Input The input consists of multiple datasets, each in the following format. n a 1 a 2 ... a n S T Each dataset consists of four lines. The first line contains n (1 ≤ n ≤ 16), the number of different capital Roman letters corresponding to Icpcan characters. The second line contains n distinct capital Roman letters without any spaces. The third and the fourth lines contain two discovered expressions S and T , respectively. Both S and T conform to the grammar given above and neither of them has more than 100 characters. The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 50. Output For each dataset, output a single line containing the number of different total orders making the values of the two expressions equal. Sample Input 3 CIP ((I<C)>(P<C)) (P>C) 2 AB (A<B) (A>B) 3 ANY ((A<N)<Y) ((N<Y)<((A<A)<N)) 6 ANSWER A ((E>(A>((E<W)>(N<S))))>(A<W)) 0 Output for the Sample Input 1 0 6 300
[ { "submission_id": "aoj_1643_10881117", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\n// clang-format off\n\nint n;\nstring a;\nstring S, T;\n\nchar val(const string& P, int& r, char tchar, int mask) {\n ++r;\n if (P[r-1]==tchar) return 1;\n int idx=P[r-1]-'A';\n if ((mask>>idx)&1) {\n return 2; // greater other char\n } else {\n return 0; // smaller\n }\n}\n\nchar expr(const string& P, int& r, char tchar, int mask) {\n if (P[r] == '(') {\n ++r; // (\n int c1 = expr(P, r, tchar, mask);\n char op = P[r];\n ++r; // < or >\n int c2 = expr(P, r, tchar, mask);\n ++r; // )\n assert(op=='<'||op=='>');\n if (op == '<') {\n return min(c1, c2);\n } else {\n return max(c1, c2);\n }\n } else {\n int v = val(P, r, tchar, mask);\n return v;\n }\n}\n\nint main(void) {\n \n while (cin>>n, n>0) {\n cin>>a>>S>>T;\n\n ll ans = 0;\n vector<ll> fact(n+1,1);\n for(int i=0;i<n;++i) fact[i+1]=fact[i]*(i+1);\n for(int i=0;i<n;++i) {\n for(int s=0; s<(1L<<n); ++s) {\n if ((s>>i)&1) continue;\n int mask=0;\n for (int j=0;j<n;++j) if ((s>>j)&1) mask|=1L<<(a[j]-'A');\n int r1=0, r2=0;\n int e1=expr(S, r1, a[i], mask);\n int e2=expr(T, r2, a[i], mask);\n if (e1 == 1 && e2 == 1) {\n int l=popcount<unsigned int>(s);\n int r=n-1-l;\n ans+=fact[l]*fact[r];\n }\n }\n }\n cout << ans <<endl;\n }\n\n}", "accuracy": 1, "time_ms": 2140, "memory_kb": 3456, "score_of_the_acc": -0.4632, "final_rank": 13 }, { "submission_id": "aoj_1643_10676338", "code_snippet": "#line 1 \"library/Template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T, typename S = T> S SUM(const vector<T> &a) {\n return accumulate(ALL(a), S(0));\n}\ntemplate <typename S, typename T = S> S POW(S a, T b) {\n S ret = 1, base = a;\n for (;;) {\n if (b & 1)\n ret *= base;\n b >>= 1;\n if (b == 0)\n break;\n base *= base;\n }\n return ret;\n}\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define debug 1\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define debug 0\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n#line 2 \"library/Utility/fastio.hpp\"\n#include <unistd.h>\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n#line 2 \"library/Utility/random.hpp\"\n\nnamespace Random {\nmt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count());\nusing u64 = unsigned long long;\nu64 get() {\n return randgen();\n}\ntemplate <typename T> T get(T L) { // [0,L]\n return get() % (L + 1);\n}\ntemplate <typename T> T get(T L, T R) { // [L,R]\n return get(R - L) + L;\n}\ndouble uniform() {\n return double(get(1000000000)) / 1000000000;\n}\nstring str(int n) {\n string ret;\n rep(i, 0, n) ret += get('a', 'z');\n return ret;\n}\ntemplate <typename Iter> void shuffle(Iter first, Iter last) {\n if (first == last)\n return;\n int len = 1;\n for (auto it = first + 1; it != last; it++) {\n len++;\n int j = get(0, len - 1);\n if (j != len - 1)\n iter_swap(it, first + j);\n }\n}\ntemplate <typename T> vector<T> select(int n, T L, T R) { // [L,R]\n if (n * 2 >= R - L + 1) {\n vector<T> ret(R - L + 1);\n iota(ALL(ret), L);\n shuffle(ALL(ret));\n ret.resize(n);\n return ret;\n } else {\n unordered_set<T> used;\n vector<T> ret;\n while (SZ(used) < n) {\n T x = get(L, R);\n if (!used.count(x)) {\n used.insert(x);\n ret.push_back(x);\n }\n }\n return ret;\n }\n}\n\nvoid relabel(int n, vector<pair<int, int>> &es) {\n shuffle(ALL(es));\n vector<int> ord(n);\n iota(ALL(ord), 0);\n shuffle(ALL(ord));\n for (auto &[u, v] : es)\n u = ord[u], v = ord[v];\n}\ntemplate <bool directed, bool multi, bool self>\nvector<pair<int, int>> genGraph(int n, int m) {\n vector<pair<int, int>> cand, es;\n rep(u, 0, n) rep(v, 0, n) {\n if (!self and u == v)\n continue;\n if (!directed and u > v)\n continue;\n cand.push_back({u, v});\n }\n if (m == -1)\n m = get(SZ(cand));\n // chmin(m, SZ(cand));\n vector<int> ord;\n if (multi)\n rep(_, 0, m) ord.push_back(get(SZ(cand) - 1));\n else {\n ord = select(m, 0, SZ(cand) - 1);\n }\n for (auto &i : ord)\n es.push_back(cand[i]);\n relabel(n, es);\n return es;\n}\nvector<pair<int, int>> genTree(int n) {\n vector<pair<int, int>> es;\n rep(i, 1, n) es.push_back({get(i - 1), i});\n relabel(n, es);\n return es;\n}\n}; // namespace Random\n\n/**\n * @brief Random\n */\n#line 4 \"sol.cpp\"\n\nint main() {\n for (;;) {\n int n;\n read(n);\n if (n == 0)\n break;\n string var, S, T;\n read(var, S, T);\n\n if (n == 1) {\n print(1);\n continue;\n }\n sort(ALL(var));\n ll ret = 0;\n rep(x, 0, n) {\n string rem;\n rep(j, 0, n) if (x != j) rem += var[j];\n rep(mask, 0, 1 << (n - 1)) {\n string zen, kou;\n rep(i, 0, n - 1) {\n if (mask >> i & 1)\n zen += rem[i];\n else\n kou += rem[i];\n }\n // 0:zen,1:s[x],2:kou\n auto dfs = [&](auto &dfs, int &i, string &s) -> int {\n if (s[i] != '(') {\n int ret = -1;\n if (zen.find(s[i]) != string::npos)\n ret = 0;\n else if (s[i] == var[x])\n ret = 1;\n else\n ret = 2;\n i++;\n return ret;\n }\n\n i++; // (\n int X = dfs(dfs, i, s);\n char c = s[i];\n i++;\n int Y = dfs(dfs, i, s);\n i++; // )\n\n int ret;\n if (c == '<')\n ret = min(X, Y);\n else\n ret = max(X, Y);\n // show(s, i, ret);\n return ret;\n };\n int init = 0;\n int X = dfs(dfs, init, S);\n init = 0;\n int Y = dfs(dfs, init, T);\n // show(X, Y, var[x], zen, kou);\n if (X == 1 and Y == 1) {\n ll add = 1;\n rep(x, 1, SZ(zen) + 1) add *= x;\n rep(x, 1, SZ(kou) + 1) add *= x;\n ret += add;\n }\n }\n }\n print(ret);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2890, "memory_kb": 3420, "score_of_the_acc": -0.6278, "final_rank": 15 }, { "submission_id": "aoj_1643_10674492", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\nusing lint = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nchar solve(string s, const vector<int> &o) {\n stack<char> st;\n int siz = s.size();\n\n rep(i, siz) {\n char c = s[i];\n if (c == ')') {\n char a, b;\n char op;\n b = st.top();\n st.pop();\n op = st.top();\n st.pop();\n a = st.top();\n st.pop();\n st.pop();\n\n int aid = a - 'A';\n int bid = b - 'A';\n char res;\n\n if (op == '<') {\n if (o[aid] < o[bid]) {\n res = a;\n } else {\n res = b;\n }\n } else {\n if (o[aid] < o[bid]) {\n res = b;\n } else {\n res = a;\n }\n }\n\n st.push(res);\n } else {\n st.push(c);\n }\n }\n return st.top();\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n;\n\n vector<long long> fac(100);\n\n fac[0] = 1;\n fac[1] = 1;\n for (lint i = 2; i < 30; i++) {\n fac[i] = fac[i - 1] * i;\n }\n\n while (1) {\n in(n);\n if (n == 0) break;\n\n vector<char> cv(26);\n string a;\n in(a);\n sort(all(a));\n char cur = 'A';\n\n rep(i, a.size()) {\n cv[a[i] - 'A'] = cur;\n cur++;\n }\n\n string s, t;\n in(s, t);\n\n rep(i, s.size()) {\n if ('A' <= s[i] and s[i] <= 'Z') {\n s[i] = cv[s[i] - 'A'];\n }\n }\n rep(i, t.size()) {\n if ('A' <= t[i] and t[i] <= 'Z') {\n t[i] = cv[t[i] - 'A'];\n }\n }\n\n rep(i, a.size()) {\n a[i] = cv[a[i] - 'A'];\n }\n\n vector<int> ord(n, -2);\n lint ans = 0;\n for (auto c : a) {\n ord[c - 'A'] = 0;\n int cur = c - 'A';\n rep(bit, 1 << n) {\n int cnt_0 = 0, cnt_1 = 0;\n\n rep(i, n) {\n if (i == cur) {\n continue;\n }\n if (bit & (1 << i)) {\n ord[i] = 1;\n cnt_1++;\n } else {\n ord[i] = -1;\n cnt_0++;\n }\n }\n char res_s = solve(s, ord);\n char res_t = solve(t, ord);\n if (res_s == c and res_t == c) {\n ans += fac[cnt_0] * fac[cnt_1];\n }\n }\n }\n out(ans / 2);\n }\n}", "accuracy": 1, "time_ms": 3800, "memory_kb": 3456, "score_of_the_acc": -0.8289, "final_rank": 18 }, { "submission_id": "aoj_1643_10666312", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nchar EVAL(char lf, char op, char rg) {\n if(op == '<') {\n if(lf == '-' || rg == '-') return '-';\n if(lf == '0' || rg == '0') return '0';\n if(lf == '+' || rg == '+') return '+';\n }else {\n if(lf == '+' || rg == '+') return '+';\n if(lf == '0' || rg == '0') return '0';\n if(lf == '-' || rg == '-') return '-';\n }\n return '*';\n}\n\nchar EXPR(string &S, int &i) {\n if(S[i] == '(') {\n i += 1;\n char lf = EXPR(S, i);\n char op = S[i];\n i += 1;\n char rg = EXPR(S, i);\n i += 1;\n return EVAL(lf, op, rg);\n }else {\n char ret = S[i];\n i += 1;\n return ret;\n }\n}\n\nstring FORMAT(string S, string A, string X) {\n for(char &c : S) {\n if(isupper(c)) {\n c = X[find(A.begin(), A.end(), c) - A.begin()];\n }\n }\n return S;\n}\n\nbool solve() {\n int N;\n cin >> N;\n if(N == 0) return false;\n string A, S, T;\n cin >> A >> S >> T;\n string X(N, '*');\n vector<string> v;\n auto dfs = [&](auto dfs, int i, int j) -> void {\n if(i == N) {\n // cout << X << endl;\n v.push_back(X);\n }else {\n X[i] = '-';\n dfs(dfs, i + 1, j);\n X[i] = '+';\n dfs(dfs, i + 1, j);\n if(j == 0) {\n X[i] = '0';\n dfs(dfs, i + 1, j + 1);\n }\n }\n };\n dfs(dfs, 0, 0);\n long long ans = 0;\n for(string x : v) {\n int i = 0, j = 0;\n string s = FORMAT(S, A, x);\n string t = FORMAT(T, A, x);\n if(EXPR(s, i) == '0' && EXPR(t, j) == '0') {\n long long f = 1;\n int cn = count(x.begin(), x.end(), '-');\n int cp = count(x.begin(), x.end(), '+');\n for(int k = 1; k <= cn; k++) f *= k;\n for(int k = 1; k <= cp; k++) f *= k;\n ans += f;\n }\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 4580, "memory_kb": 56660, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1643_10665898", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct dat {\n int L, R;\n char C;\n};\n\nvector<dat> make_tree(const string &S) {\n vector<dat> T;\n auto rec = [&] (auto rec, int &i) -> int {\n int p = T.size();\n T.push_back({-1, -1, '?'});\n if (S[i] != '(' && S[i] != ')') {\n T[p] = dat{-1, -1, S[i]};\n } else {\n int l = rec(rec, ++i);\n assert(S[i] == '<' || S[i] == '>');\n char c = S[i];\n int r = rec(rec, ++i);\n T[p] = dat{l, r, c};\n }\n i++;\n return p;\n };\n int i = 0;\n rec(rec, i);\n return T;\n}\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n string A, S, T;\n cin >> A >> S >> T;\n vector<long long> fac(N + 1, 1);\n for (int i = 1; i <= N; i++) fac[i] = i * fac[i - 1];\n auto treeS = make_tree(S);\n auto treeT = make_tree(T);\n long long ans = 0;\n vector<int> val(256, 0);\n auto f = [&] (auto f, const vector<dat> &T, int u) -> int {\n char c = T[u].C;\n if (c != '<' && c != '>') return val[c];\n auto l = f(f, T, T[u].L), r = f(f, T, T[u].R);\n return c == '<' ? min(l, r) : max(l, r);\n };\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < 1 << N; i++) {\n int l = 0, r = 0;\n for (int j = 0; j < N; j++) {\n if (j == k) {\n val[A[j]] = 0;\n } else if (i >> j & 1) {\n val[A[j]] = -1;\n l++;\n } else {\n val[A[j]] = 1;\n r++;\n }\n }\n if (f(f, treeS, 0) == 0 && f(f, treeT, 0) == 0) {\n ans += fac[l] * fac[r];\n }\n }\n }\n cout << ans / 2 << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 1370, "memory_kb": 3584, "score_of_the_acc": -0.296, "final_rank": 10 }, { "submission_id": "aoj_1643_10645092", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\n\ntypedef string::const_iterator State;\n\nclass ParseError {};\n\nll expression(State &begin);\nmap<char,ll> cmp;\nstruct node{\n\tchar ch; // charがいいときもある\n\tvector<ll> to;\n};\n\nvector<node>nodes;\n\n//四則演算の式を構文解析し評価結果を返す。\nll expression(State &begin){\n if(isupper(*begin)){\n ll ret = nodes.size();\n nodes.push_back({*begin,{}});\n begin++;\n return ret;\n }\n // (\n assert(*begin == '(');\n begin++;\n ll before = expression(begin);\n ll ret = sz(nodes);\n nodes.push_back({*begin,{}});\n begin++;\n ll after = expression(begin);\n\n nodes[ret].to.push_back(before);\n nodes[ret].to.push_back(after);\n nodes[before].to.push_back(ret);\n nodes[after].to.push_back(ret);\n\n assert(*begin == ')');\n begin++;\n\n return ret;\n}\n\n\nvoid solve(ll n){\n Str(s);\n cmp.clear();\n rep(i,n){\n cmp[s[i]] = i;\n }\n\n auto retable = [&](string &t) -> vvll {\n nodes.clear();\n State begin = t.begin();\n ll root = expression(begin);\n\n //各文字がトップになるときの\n vector<vector<ll>> able(n);\n rep(z,n){\n vector<vector<ll>> dp(sz(nodes),vll(1<<n));// -1 s[z] より小さいのが上がってきた、0:同じ,1:大きい\n vector<ll> found(sz(nodes),0);//n頂点\n auto dfs = [&](auto dfs, ll v)->void{\n found[v] = 1;\n if(isupper(nodes[v].ch)){\n if(nodes[v].ch == s[z]){\n rep(i,1 << n){\n dp[v][i] = 0;\n }\n }else{\n ll id = cmp[nodes[v].ch];\n rep(i,1 << n){\n if(i >> id &1){\n dp[v][i] = 1;\n }else{\n dp[v][i] = -1;\n }\n }\n }\n return ;\n }\n //gでグラフを受け取っている\n vll vs;\n for(auto &p:nodes[v].to){\n if(found[p] == 0){\n vs.push_back(p);\n dfs(dfs,p);\n }\n }\n //更新\n if(nodes[v].ch == '<'){\n //小さいほうが残る\n rep(i,1 << n){\n dp[v][i] = min(dp[vs[0]][i],dp[vs[1]][i]);\n }\n }else{\n rep(i,1 << n){\n dp[v][i] = max(dp[vs[0]][i],dp[vs[1]][i]);\n }\n }\n };\n dfs(dfs,root);\n rep(i,1 << n){\n if(dp[root][i] == 0){\n able[z].push_back(i);\n }\n }\n }\n return able;\n };\n Str(t);\n vvll ablet = retable(t);\n\n Str(u);\n vvll ableu_tmp = retable(u);\n //後ろはsetにする\n vector<set<ll>> ableu(n);\n rep(i,n){\n each(p,ableu_tmp[i]){\n ableu[i].insert(p);\n }\n }\n ll ans = 0;\n vll kai(n+1);\n kai[0] = 1;\n repsn(i,1,n){\n kai[i] = kai[i-1]*i;\n }\n\n rep(i,n){\n each(p,ablet[i]){\n if((p >> i & 1)&&ableu[i].contains(p)){\n //\n ll ucnt = popcnt(p);\n ll dcnt = n - ucnt;\n ucnt--;\n ans += kai[ucnt] * kai[dcnt]; \n }\n }\n }\n cout << ans << endl;\n\n}\n\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n);\n if(n == 0)break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 3110, "memory_kb": 31620, "score_of_the_acc": -1.2059, "final_rank": 19 }, { "submission_id": "aoj_1643_10636846", "code_snippet": "#include <bits/stdc++.h>\n# pragma GCC target(\"avx2\")\n# pragma GCC optimize(\"O3\")\n# pragma GCC optimize(\"unroll-loops\")\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (ll i=0; i<(n); i++)\ntemplate<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true;} return false; }\ntemplate<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true;} return false; }\nconst ll INFL = (1LL << 61);\n\nint main() {\n vector<ll> fact(17, 1);\n rep(i, 16) fact[i+1] = fact[i] * (i+1);\n while (true) {\n ll n; cin >> n;\n if (n == 0) break;\n string d; cin >> d;\n string s,t; cin >> s >> t;\n // cout << d << \" \" << s << \" \" << t << \"\\n\";\n struct node {\n ll val;\n ll l,r;\n };\n vector<ll> pos(26, -1);\n rep(i, n) pos[d[i] - 'A'] = i;\n auto parse_e = [&] (auto self, ll start, const string &s, vector<node> &nodes) -> pair<ll, ll> {\n ll p = nodes.size();\n if ('A' <= s[start] && s[start] <= 'Z') {\n nodes.emplace_back(pos[s[start] - 'A'], -1, -1);\n return {start + 1, p};\n }\n assert(s[start] == '(');\n nodes.emplace_back(0, -1, -1);\n auto [e, pn] = self(self, start + 1, s, nodes);\n nodes[p].l = pn;\n if (s[e] == '<') nodes[p].val = -1;\n else nodes[p].val = -2;\n auto [en, pnn] = self(self, e + 1, s, nodes);\n nodes[p].r = pnn;\n return {en + 1, p};\n };\n vector<node> nodes;\n vector<node> nodet;\n parse_e(parse_e, 0, s, nodes);\n parse_e(parse_e, 0, t, nodet);\n // for (auto node : nodet) {\n // cout << node.val << \" \" << node.l << \" \" << node.r << \"\\n\";\n // }\n // cout << \"---\\n\";\n ll ans = 0;\n rep(i, n) {\n rep(bit, (1LL << n)) {\n // cout << i << \" \" << bit << \"\\n\";\n if ((bit >> i) & 1) continue;\n auto dfs = [&] (auto self, ll p, const vector<node> &nodes) -> ll {\n if (nodes[p].l == -1) {\n ll x = nodes[p].val;\n if (x == i) return 0;\n if ((bit >> x) & 1) return -INFL;\n return INFL;\n }\n ll lv = self(self, nodes[p].l, nodes);\n ll rv = self(self, nodes[p].r, nodes);\n // cout << \"dfs: \" << lv << \" \" << rv << \" \" << nodes[p].val << \"\\n\";\n if (nodes[p].val == -1) return min(lv, rv);\n return max(lv, rv);\n };\n ll sv = dfs(dfs, 0, nodes);\n ll tv = dfs(dfs, 0, nodet);\n if (sv == 0 && tv == 0) {\n // cout << i << \" \" << bit << '\\n';\n ll p = __builtin_popcountll(bit);\n ans += fact[p] * fact[n-1-p];\n }\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 3456, "score_of_the_acc": -0.1813, "final_rank": 7 }, { "submission_id": "aoj_1643_10636739", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <bitset>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0; i<ll(n); i++)\ntemplate<class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }\n\nll solve(int N, string S, string A, string B){\n using Bitset = bitset<65536>;\n vector<Bitset> buf(N);\n rep(i,N) rep(j,1<<N) if(j&(1<<i)) buf[i].set(j);\n auto expr = [&](auto& expr, string &s, ll& p) -> Bitset {\n if(s[p] == '('){\n p++;\n auto fa = expr(expr, s, p);\n char d = s[p++];\n auto fb = expr(expr, s, p);\n p++;\n if(d == '<') return fa & fb; else return fa | fb;\n }\n return buf[S.find(s[p++])];\n };\n auto al_expr = [&](string &s) -> Bitset {\n ll p = 0;\n return expr(expr, s, p);\n };\n auto fA = al_expr(A);\n auto fB = al_expr(B);\n vector<ll> fact(N+1, 1); rep(i,N) fact[i+1] = fact[i] * (i+1);\n ll ans = 0;\n rep(a,N){\n rep(f,1<<N) if(!(f&(1<<a))){\n ll pc = 0; rep(i,N) pc += (f >> i) & 1;\n if(!fA.test(f) && !fB.test(f) && fA.test(f|(1<<a)) && fB.test(f|(1<<a))){\n ans += fact[pc] * fact[N-1-pc];\n }\n }\n }\n return ans;\n}\n\nvoid testcase(){\n ll N; cin >> N; if(N == 0) exit(0);\n string S, A, B; cin >> S >> A >> B;\n ll ans = solve(N, S, A, B);\n cout << ans << \"\\n\";\n}\n\nint main(){\n cin.tie(0)->sync_with_stdio(0);\n while(1) testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4004, "score_of_the_acc": -0.0176, "final_rank": 1 }, { "submission_id": "aoj_1643_10611553", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,l,r) for(ll i=l;i<r;i++)\n#define all(v) begin(v),end(v)\n#define sz(v) ((int)v.size())\nvoid solve(int, string, string, string);\nint main(){\n while(1){\n int n;cin >> n;\n \n if(n==0)break;\n string a, s, t;cin >> a >> s >> t;\n solve(n, a, s, t);\n }\n}\nvoid solve(int n, string a, string s, string t){\n \n\n map<char, int> mp;\n rep(i, 0, n) mp[a[i]] = i;\n ll ans = 0;\n vector<ll> fact(n + 1, 1);\n rep(i, 0, n)fact[i+1] = fact[i] * (i + 1);\n rep(x, 0, n) {\n rep(bit, 0, 1 << n) {\n if (bit >> x & 1) continue;\n auto F = [&](string &S, int &i) -> int {\n char X = S[i];\n i++;\n if (mp[X] == x) return 0;\n else if (bit >> mp[X] & 1) return 1;\n else return -1;\n };\n \n auto E = [&](auto self, string &S, int &i) -> int {\n if (S[i] == '(') {\n i++;\n auto l = self(self, S, i);\n char X = S[i];\n i++;\n auto r = self(self, S, i);\n i++;\n if (X == '<') {\n return min(l, r);\n }\n else {\n return max(l, r);\n }\n }\n else {\n return F(S, i);\n }\n };\n int i = 0;\n auto res1 = E(E, s, i);\n // cerr << i << \" \" ;\n i = 0;\n\n auto res2 = E(E, t, i);\n // cerr << i << \" \";\n int pc = 0;\n // cerr << x << \" \" << bit << \" \" << res1 << \" \" << res2 << endl;\n\n rep(i, 0, n) if (bit >> i & 1) pc++;\n if (res1 == 0 and res2 == 0) {\n ans += fact[pc] * fact[n-1-pc];\n }\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 3320, "memory_kb": 3456, "score_of_the_acc": -0.7231, "final_rank": 16 }, { "submission_id": "aoj_1643_10611422", "code_snippet": "#include <bits/extc++.h>\n\nusing ll = long long;\n\nint parse(const std::string& s, int& pos, int target, int islarge) {\n if (s[pos] == '(') {\n pos++;\n int l = parse(s, pos, target, islarge);\n char op = s[pos++];\n int r = parse(s, pos, target, islarge);\n assert(s[pos++] == ')');\n if (op == '<') {\n return std::min(l, r);\n } else {\n return std::max(l, r);\n }\n } else {\n assert(isalpha(s[pos]));\n int n = s[pos++] - 'A';\n if (n == target) {\n return 0;\n } else {\n return ((1 << n) & islarge) ? 1 : -1;\n }\n }\n}\n\nll factorial(int n) {\n if (n <= 1) return 1;\n\n ll ans = 1;\n\n for (int i = 2; i <= n; i++) {\n ans *= i;\n }\n\n return ans;\n}\n\nbool solve() {\n int N;\n std::cin >> N;\n\n if (N == 0) {\n return false;\n }\n\n std::string A, S, T;\n std::cin >> A >> S >> T;\n\n // std::vector<char> A(A2.begin(), A2.end());\n\n std::sort(A.begin(), A.end());\n\n for (char& c : S) {\n if (isalpha(c)) {\n c = 'A' + (std::lower_bound(A.begin(), A.end(), c) - A.begin());\n }\n }\n\n for (char& c : T) {\n if (isalpha(c)) {\n c = 'A' + (std::lower_bound(A.begin(), A.end(), c) - A.begin());\n }\n }\n\n ll ans = 0;\n\n // std::cerr << A << '\\n';\n\n for (int i = 0; i < N; i++) {\n int mask = (1 << N) - 1 - (1 << i);\n for (int p = 0; p < (1 << N); p += 1 + (1 << i)) {\n p &= mask;\n int pos = 0;\n int x = parse(S, pos, i, p);\n pos = 0;\n int y = parse(T, pos, i, p);\n if (x == 0 && y == 0) {\n int a = __builtin_popcount(p);\n int b = N - a - 1;\n ans += factorial(a) * factorial(b);\n\n // std::cerr << A[i] << \" \" << std::bitset<16>(p) << \" \" << a << \" \" <<\n // b << '\\n';\n }\n }\n }\n\n std::cout << ans << '\\n';\n\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 2040, "memory_kb": 3584, "score_of_the_acc": -0.4436, "final_rank": 12 }, { "submission_id": "aoj_1643_10611126", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nusing sitr = string::iterator;\nvector<int> v(20,0);\nvector<int> rc(26,-1);\nint F(sitr &itr){\n return v[rc[*(itr++)-'A']];\n}\nint E(sitr &itr){\n if(*itr=='('){\n itr++;\n int l=E(itr);\n char c=*itr;\n assert(c=='<'||c=='>');\n itr++;\n int r=E(itr);\n assert(*itr==')');\n itr++;\n if(c=='<') return max(l,r);\n else return min(l,r);\n }else{\n return F(itr);\n }\n assert(0);\n}\n\nint Solve(){\n int n;\n cin>>n;\n if(n==0) return 0;\n string a,s,t;\n cin>>a>>s>>t;\n rep(i,0,n) rc[a[i]-'A']=i;\n\n ll ans=0;\n vector<ll> fac(20,1);\n rep(i,1,20) fac[i]=fac[i-1]*i;\n for(int st=0;st<n;st++){\n rep(bit,0,1<<(n-1)){\n rep(i,0,st){\n if((bit>>i)&1) v[i]=1;\n else v[i]=-1;\n }\n v[st]=0;\n rep(i,st+1,n){\n if((bit>>(i-1))&1) v[i]=1;\n else v[i]=-1;\n }\n {\n auto itr=s.begin();\n int l=E(itr);\n if(l!=0) continue;\n }\n {\n auto itr=t.begin();\n int r=E(itr);\n if(r!=0) continue;\n }\n int ct=__builtin_popcount(bit);\n ans+=fac[ct]*fac[n-1-ct];\n }\n }\n cout<<ans<<\"\\n\";\n return 1;\n}\n\nint main(){\n while(Solve());\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 3456, "score_of_the_acc": -0.1923, "final_rank": 8 }, { "submission_id": "aoj_1643_10611006", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\nstring r;\nint itr;\n\nint dfs(){\n assert(itr < ssize(r));\n if (r[itr] != '('){\n int ret = r[itr] - '0';\n itr++;\n return ret;\n }\n assert(r[itr] == '(');\n itr++;\n int lhs = dfs();\n char op = r[itr];\n itr++;\n int rhs = dfs();\n assert(itr < ssize(r));\n assert(r[itr] == ')');\n itr++;\n if (op == '<'){\n return min(lhs,rhs);\n }\n else {\n return max(lhs,rhs);\n }\n}\n\nint parse(string s){\n itr = 0;\n r = s;\n int ret = dfs();\n assert(itr == ssize(r));\n return ret;\n}\n\nvoid solve(int n){\n string a; in(a);\n string s, t; in(s,t);\n vector<bool> ok(1<<n);\n vector<char> w(26);\n rep(sub,1<<n){\n rep(i,n){\n w[a[i]-'A'] = NUM[sub >> i & 1];\n }\n string ns = \"\";\n for (auto c : s){\n if ('A' <= c && c <= 'Z'){\n ns += w[c - 'A'];\n }\n else {\n ns += c;\n }\n }\n string nt = \"\";\n for (auto c : t){\n if ('A' <= c && c <= 'Z'){\n nt += w[c - 'A'];\n }\n else {\n nt += c;\n }\n }\n ok[sub] = (parse(ns) == parse(nt));\n }\n assert(ok[0]);\n vector<ll> dp(1<<n);\n dp[0] = 1;\n repp(sub,1,1<<n){\n if (!ok[sub]) continue;\n rep(i,n){\n if (sub >> i & 1){\n dp[sub] += dp[sub^(1<<i)];\n }\n }\n }\n // out(ok);\n // out(dp);\n out(dp.back());\n}\n\nint main(){\n while (true){\n int n; in(n);\n if (n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 4204, "score_of_the_acc": -0.1359, "final_rank": 6 }, { "submission_id": "aoj_1643_10610651", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define all(p) p.begin(),p.end()\n\nvoid solve(int N){\n string S;\n cin >> S;\n const int M = 26;\n vector<int> ind(M, -1);\n rep(i, 0, N) ind[S[i] - 'A'] = i;\n vector dp(2, vector<int>(1 << N));\n using F = bitset<1 << 16>;\n rep(rp, 0, 2){\n string X;\n cin >> X;\n int pos = 0;\n auto f = [&](auto self) -> F {\n if (X[pos] != '('){\n F base;\n rep(i, 0, 1 << N){\n if (i & (1 << ind[X[pos] - 'A'])) base[i] = true;\n }\n pos++;\n return base;\n }\n pos++;\n auto L = self(self);\n auto a = X[pos++];\n auto R = self(self);\n pos++;\n if (a == '<'){\n L = (L & R);\n }\n else{\n L = (L | R);\n }\n return L;\n };\n auto tmp = f(f);\n rep(i, 0, 1 << N) dp[rp][i] = tmp[i];\n }\n vector<ll> fact(N + 1, 1);\n rep(i, 0, N) fact[i + 1] = fact[i] * (i + 1);\n ll ans = 0;\n rep(i, 0, 1 << N){\n int a = 0;\n rep(j, 0, N) if (i & (1 << j)) a++;\n a--;\n rep(j, 0, N) if (i & (1 << j)){\n if (dp[0][i] & dp[1][i] & !dp[0][i - (1 << j)] & !dp[1][i - (1 << j)]){\n ans += fact[a] * fact[N - 1 - a];\n }\n }\n }\n cout << ans << \"\\n\";\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int N;\n while (cin >> N, N) solve(N);\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4420, "score_of_the_acc": -0.0276, "final_rank": 2 }, { "submission_id": "aoj_1643_10587832", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nusing ll = long long;\n\nclass IcpcanAlphabet {\npublic:\n explicit IcpcanAlphabet(const int max_n) : fact(max_n + 1), cmp(26) {\n fact[0] = 1;\n for (int i = 1; i <= max_n; ++i) fact[i] = i * fact[i - 1];\n }\n\n ll GetNumTotalOrderEqualExp(const std::string &A, const std::string &S, const std::string &T) {\n const int N = A.size();\n ll num = 0;\n for (int i = 0; i < N; ++i) { // S と T の値が A_i となる全順序の数を求める\n cmp[index(A[i])] = 0;\n\n const unsigned int univ = ((1 << N) - 1) ^ (1 << i); // univ = {0, 1, ..., N - 1} / {i}\n unsigned int sub = univ;\n while (true) { // univ の部分集合を全列挙する:部分集合 sub := {x \\in univ | x < i} とする\n int num_less = 0;\n for (int j = 0; j < N; ++j) {\n if (sub >> j & 1) {\n ++num_less;\n cmp[index(A[j])] = -1;\n }\n else if (i != j) {\n cmp[index(A[j])] = 1;\n }\n }\n\n auto it_s = S.begin(), it_t = T.begin();\n if (Evaluate(it_s) == 0 && Evaluate(it_t) == 0)\n num += fact[num_less] * fact[N - num_less - 1];\n\n if (sub == 0) break; // 部分集合が空の場合も計算できるように\n sub = (sub - 1) & univ;\n }\n }\n\n return num;\n }\n\nprivate:\n std::vector<ll> fact;\n // 固定された a \\in A に対して a' \\in A の cmp の値を a との大小関係で定める\n // a == a' なら cmp[index(a')] = 0\n // a < a' なら cmp[index(a')] = 1\n // a > a' なら cmp[index(a')] = -1\n std::vector<int> cmp;\n\n unsigned index(const char c) const { return static_cast<unsigned>(c - 'A'); }\n\n int Evaluate(std::string::const_iterator& it) {\n // E ::= F | '(' E '<' E ')' | '(' E '>' E ')'\n if (*it == '(') {\n int lhs = Evaluate(++it);\n char op = *it;\n int rhs = Evaluate(++it);\n ++it; // skip ')'\n return (op == '<') ? std::min(lhs, rhs) : std::max(lhs, rhs);\n }\n else {\n return cmp[index(*it++)];\n }\n }\n};\n\nint main() {\n const int MAX_N = 16;\n IcpcanAlphabet solver(MAX_N);\n\n int n;\n while (std::cin >> n, n) {\n std::string a, s, t;\n std::cin >> a >> s >> t;\n std::cout << solver.GetNumTotalOrderEqualExp(a, s, t) << std::endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 640, "memory_kb": 3584, "score_of_the_acc": -0.1352, "final_rank": 5 }, { "submission_id": "aoj_1643_10578817", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ALL(v) (v).begin(), (v).end()\nusing ll = long long;\n\nll fac[20];\n\nvoid solve(int N, string &A, string &S, string &T) {\n map<char,int> idx;\n for (int i = 0; i < N; i++) idx[A[i]] = i;\n vector<pair<int,int>> B[2];\n B[0].resize((int)S.size(), make_pair(-1, -1));\n B[1].resize((int)T.size(), make_pair(-1, -1));\n auto make_block = [&](int t) {\n auto X = (t == 0 ? S : T);\n stack<pair<int,int>> st;\n for (int i = 0; i < (int)X.size(); i++) {\n if ('A' <= X[i] && X[i] <= 'Z') {\n B[t][i] = make_pair(i, i);\n continue;\n }\n if (X[i] == ')') {\n int m = st.top().second; st.pop();\n int l = st.top().second; st.pop();\n assert(X[l] == '(' && (X[m] == '<' || X[m] == '>'));\n B[t][l] = make_pair(m, i);\n }\n else {\n st.emplace(X[i], i);\n }\n }\n };\n make_block(0);\n make_block(1);\n ll ans = 0;\n for (auto c : A) {\n for (int bit = 0; bit < (1 << N); bit++) {\n if ((bit >> idx[c]) & 1) continue;\n int pcnt = __builtin_popcount(bit);\n auto calc = [&](int t) {\n auto X = (t == 0 ? S : T);\n auto comp = [&](auto &comp, int l, int m, int r) {\n if (l == m && m == r) {\n return (X[l] == c) ? 0 : (((bit >> idx[X[l]]) & 1) ? 1 : -1);\n }\n int lhs = comp(comp, l+1, B[t][l+1].first, B[t][l+1].second);\n int rhs = comp(comp, m+1, B[t][m+1].first, B[t][m+1].second);\n return (X[m] == '>' ? max(lhs, rhs) : min(lhs, rhs));\n };\n return (comp(comp, 0, B[t][0].first, B[t][0].second) == 0);\n };\n if (calc(0) && calc(1)) {\n ans += fac[pcnt] * fac[N-1-pcnt];\n }\n }\n }\n cout << ans << \"\\n\";\n return;\n}\n\nint main() {\n fac[0] = 1;\n for (int i = 1; i < 20; i++) fac[i] = fac[i-1] * i;\n while (true) {\n int N; cin >> N;\n if (N == 0) break;\n string A, S, T;\n cin >> A >> S >> T;\n solve(N, A, S, T);\n }\n}", "accuracy": 1, "time_ms": 1470, "memory_kb": 3584, "score_of_the_acc": -0.3181, "final_rank": 11 }, { "submission_id": "aoj_1643_10547936", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#ifdef _DEBUG\n#define show(x) \\\n\tcerr << #x << \" : \"; \\\n\tshowVal(x)\ntemplate <typename T>\nvoid showVal(const T &a) {\n\tcerr << a << endl;\n}\ntemplate <typename T, typename U>\nvoid showVal(const pair<T, U> &a) {\n\tcerr << a.first << \" \" << a.second << endl;\n}\ntemplate <typename T>\nvoid showVal(const vector<T> &a) {\n\tfor (const T &v : a) cerr << v << \" \";\n\tcerr << endl;\n}\ntemplate <typename T, typename U>\nvoid showVal(const vector<pair<T, U>> &a) {\n\tcerr << endl;\n\tfor (const pair<T, U> &v : a) cerr << v.first << \" \" << v.second << endl;\n}\ntemplate <typename T, typename U>\nvoid showVal(const map<T, U> &a) {\n\tcerr << endl;\n\tfor (const auto &v : a) cerr << \"[\" << v.first << \"] \" << v.second << endl;\n}\ntemplate <typename T>\nvoid showVal(const vector<vector<T>> &a) {\n\tcerr << endl;\n\tfor (const vector<T> &v : a) showVal(v);\n}\n#else\n#define show(x)\n#endif\nint main() {\n\tvector<ll> fact(17);\n\tfact[0] = fact[1] = 1;\n\tfor (int i = 1; i < 16; i++) {\n\t\tfact[i + 1] = fact[i] * (i + 1);\n\t}\n\twhile (1) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tstring c;\n\t\tstring s, t;\n\t\tcin >> c >> s >> t;\n\t\tunordered_map<char, int> toi;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\ttoi[c[i]] = i;\n\t\t}\n\t\tll ans = 0;\n\n\t\tvector<map<pair<int, int>, int>> tmpmidi(2);\n\n\t\tfor (int v = 0; v < 2; v++) {\n\t\t\tstack<pair<int, int>> st;\n\t\t\tint sz = s.size();\n\t\t\tfor (int i = 0; i < sz; i++) {\n\t\t\t\tif (s[i] == '(') {\n\t\t\t\t\tst.push({i, 0});\n\t\t\t\t} else if (s[i] == ')') {\n\t\t\t\t\tauto [l, m] = st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\ttmpmidi[v][{l, i}] = m;\n\t\t\t\t} else if (s[i] == '<' || s[i] == '>') {\n\t\t\t\t\tst.top().second = i;\n\t\t\t\t}\n\t\t\t}\n\t\t\tswap(s, t);\n\t\t}\n\n\t\tfor (int x = 0; x < n; x++) {\n\t\t\t// show(x);\n\t\t\t// x fixed\n\t\t\tfor (int bit = 0; bit < (1 << (n - 1)); bit++) {\n\t\t\t\tvector<int> mg(n, 1);\n\t\t\t\t// 0 : < x, 1: = x, 2 : > x\n\t\t\t\t{\n\t\t\t\t\tint cur = 0;\n\t\t\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\t\t\tif (i == x) continue;\n\t\t\t\t\t\tmg[i] = ((bit >> cur) & 1) * 2;\n\t\t\t\t\t\tcur++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// show(mg);\n\n\t\t\t\tauto cmp = [&](int lhs, int rhs, char op) -> int {\n\t\t\t\t\t// show(op);\n\t\t\t\t\tif (op == '<') {\n\t\t\t\t\t\treturn min(lhs, rhs);\n\t\t\t\t\t} else if (op == '>') {\n\t\t\t\t\t\treturn max(lhs, rhs);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tassert(false);\n\t\t\t\t\t\treturn -1;\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tvector<int> vals;\n\n\t\t\t\tfor (int v = 0; v < 2; v++) {\n\t\t\t\t\tint sz = s.size();\n\t\t\t\t\t// for (auto [p, m] : midi) {\n\t\t\t\t\t// \tshow(p);\n\t\t\t\t\t// \tshow(m);\n\t\t\t\t\t// }\n\t\t\t\t\tauto val = [&](auto self, int l, int r) -> int {\n\t\t\t\t\t\t// value of [l,r)\n\t\t\t\t\t\tif (l + 1 == r) {\n\t\t\t\t\t\t\treturn mg[toi[s[l]]];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tint m = tmpmidi[v][{l, r - 1}];\n\t\t\t\t\t\t// show(m);\n\t\t\t\t\t\tint lhs = self(self, l + 1, m);\n\t\t\t\t\t\tint rhs = self(self, m + 1, r - 1);\n\t\t\t\t\t\t// show(m);\n\t\t\t\t\t\t// show(s[m]);\n\t\t\t\t\t\treturn cmp(lhs, rhs, s[m]);\n\t\t\t\t\t};\n\t\t\t\t\t// show(val(val, 2, 3));\n\t\t\t\t\t// show(val(val, 1, 6));\n\t\t\t\t\tvals.push_back(val(val, 0, sz));\n\t\t\t\t\tswap(s, t);\n\t\t\t\t}\n\t\t\t\tif (vals == vector<int>{1, 1}) {\n\t\t\t\t\tvector<int> cnt(3);\n\t\t\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\t\t\tcnt[mg[i]]++;\n\t\t\t\t\t}\n\t\t\t\t\tans += fact[cnt[0]] * fact[cnt[2]];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3500, "memory_kb": 3456, "score_of_the_acc": -0.7628, "final_rank": 17 }, { "submission_id": "aoj_1643_10486095", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nbool solve() {\n int n; cin >> n;\n if(n == 0) return false;\n string a; cin >> a;\n string s, t; cin >> s >> t;\n auto get_closing = [](const string& s) {\n int l =ssize(s);\n vector<pair<int, int>> ret(l);\n stack<int> stk;\n for(int i = 0; i < l; i++) {\n if(s[i] == '(') stk.push(i);\n else if(s[i] == '<' || s[i] == '>') ret[stk.top()].first = i;\n else if(s[i] == ')') {\n ret[stk.top()].second = i;\n stk.pop();\n }\n }\n return ret;\n };\n auto process = [&](auto&& self, const string& s, const vector<pair<int, int>>& closing, const vector<int>& order, int l, int r) -> int {\n if(s[l] != '(') {\n assert(l+1 == r);\n int f = ranges::find(a, s[l]) - a.begin();\n return order[f];\n }\n int x = self(self, s, closing, order, l+1, closing[l].first);\n int y = self(self, s, closing, order, closing[l].first+1, closing[l].second);\n if(s[closing[l].first] == '<') return min(x, y);\n if(s[closing[l].first] == '>') return max(x, y);\n assert(0);\n };\n vector<ll> factorial(n+1);\n factorial[0] = 1;\n for(int i = 1; i <= n; i++) factorial[i] = factorial[i-1] * i;\n ll ans = 0;\n auto closing_s = get_closing(s);\n auto closing_t = get_closing(t);\n for(int ret = 0; ret < n; ret++) {\n for(int st = 0; st < 1<<n; st++) {\n if(st >> ret & 1) continue;\n vector<int> order(n);\n for(int i = 0; i < n; i++) {\n if(i == ret) continue;\n if(st >> i & 1) order[i] = 1;\n else order[i] = -1;\n }\n if(process(process, s, closing_s, order, 0, ssize(s)) != 0) continue;\n if(process(process, t, closing_t, order, 0, ssize(t)) != 0) continue;\n ans += factorial[ranges::count(order, 1)] * factorial[ranges::count(order, -1)];\n }\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 3584, "score_of_the_acc": -0.2013, "final_rank": 9 }, { "submission_id": "aoj_1643_10440850", "code_snippet": "// AOJ #1643 Icpcan Alphabet\n// // 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nstring Cins() {\n static char temp[105];\n char *s = temp;\n do *s = gc();\n while (*s++ > ' ');\n *(s-1) = 0;\n string input(temp);\n return input;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconst int MAXN = 16;\nconst int MAXM = MAXN - 1;\nconst int MAXJ = 1 << MAXM;\n\nvector<ll> fact;\n\nstruct Node {\n char op;\n int c[2], p, let;\n ull mask;\n Node():op(0),p(-1),let(-1),mask(0){ c[0]=c[1]=-1; }\n};\n\nint parseExpr(const string &s, int &pos, vector<Node> &t, const vector<int> &id){\n if (s[pos]=='(') {\n pos++;\n int L = parseExpr(s,pos,t,id);\n char op = s[pos++];\n int R = parseExpr(s,pos,t,id);\n pos++;\n int u = t.size();\n t.emplace_back();\n t[u].op = op;\n t[u].c[0] = L; t[u].c[1] = R;\n t[L].p = u; t[R].p = u;\n return u;\n } else {\n int idx = id[s[pos++]-'A'];\n int u = t.size();\n t.emplace_back();\n t[u].op = 'L';\n t[u].let = idx;\n t[u].mask = 1ull<<idx;\n return u;\n }\n}\n\nvoid build_mask(vector<Node> &t, int u){\n if (t[u].op == 'L') return;\n build_mask(t,t[u].c[0]);\n build_mask(t,t[u].c[1]);\n t[u].mask = t[t[u].c[0]].mask | t[t[u].c[1]].mask;\n}\n\nint main(){\n fact.resize(MAXN+1);\n fact[0]=1;\n for(int i=1;i<=MAXN;i++) fact[i]=fact[i-1]*i;\n\n while(true){\n int n = Cin();\n if(n == 0) break;\n string s = Cins(), S = Cins(), T = Cins();\n vector<int> id(26,-1);\n for(int i=0;i<n;i++) id[s[i]-'A'] = i;\n\n vector<Node> a, b;\n int p1 = 0, p2 = 0;\n int r1 = parseExpr(S,p1,a,id);\n int r2 = parseExpr(T,p2,b,id);\n build_mask(a, r1);\n build_mask(b, r2);\n\n ull Um = a[r1].mask | b[r2].mask;\n vector<int> U;\n for(int i=0;i<n;i++) if(Um>>i & 1) U.push_back(i);\n int Usz = U.size();\n\n ull globalF = fact[n] / fact[Usz];\n ull total = 0;\n\n static bitset<MAXJ> pat[MAXM];\n static bitset<MAXJ> LSs[256], EqS[256], GTs[256];\n static bitset<MAXJ> LSt[256], EqT[256], GTt[256];\n static ll w[MAXN+1];\n bitset<MAXJ> dom;\n\n for(int c: U){\n bool inS=false, inT=false;\n for(auto &nd:a) if(nd.op=='L' && nd.let==c){ inS=true; break; }\n for(auto &nd:b) if(nd.op=='L' && nd.let==c){ inT=true; break; }\n if(!inS||!inT) continue;\n\n vector<int> U2; U2.reserve(Usz-1);\n for(int y:U) if(y!=c) U2.push_back(y);\n int m = Usz-1, D = 1<<m;\n int id2[MAXN]; fill(begin(id2), end(id2), -1);\n for(int i2=0;i2<m;i2++) id2[U2[i2]] = i2;\n\n dom.reset();\n for(int j=0;j<D;j++) dom.set(j);\n\n for(int pos=0;pos<m;pos++){\n pat[pos].reset();\n int step = 1<<(pos+1), half = 1<<pos;\n for(int j0=half;j0<D;j0+=step){\n int ed = min(j0+half,D);\n for(int k=j0;k<ed;k++) pat[pos].set(k);\n }\n }\n\n int sa = a.size();\n for(int i=0;i<sa;i++){\n LSs[i].reset(); EqS[i].reset(); GTs[i].reset();\n if(a[i].op=='L'){\n if(a[i].let==c) EqS[i]=dom;\n else {\n GTs[i]=pat[id2[a[i].let]];\n LSs[i]=dom ^ GTs[i];\n }\n } else {\n int l=a[i].c[0], r=a[i].c[1];\n if(a[i].op=='<'){\n LSs[i]=LSs[l]|LSs[r];\n GTs[i]=GTs[l]&GTs[r];\n EqS[i]=(EqS[l]&(EqS[r]|GTs[r]))|(EqS[r]&(EqS[l]|GTs[l]));\n } else {\n GTs[i]=GTs[l]|GTs[r];\n LSs[i]=LSs[l]&LSs[r];\n EqS[i]=(EqS[l]&~GTs[r])|(EqS[r]&~GTs[l]);\n }\n }\n }\n\n int sb = b.size();\n for(int i=0;i<sb;i++){\n LSt[i].reset(); EqT[i].reset(); GTt[i].reset();\n if(b[i].op=='L'){\n if(b[i].let==c) EqT[i]=dom;\n else {\n GTt[i]=pat[id2[b[i].let]];\n LSt[i]=dom ^ GTt[i];\n }\n } else {\n int l=b[i].c[0], r=b[i].c[1];\n if(b[i].op=='<'){\n LSt[i]=LSt[l]|LSt[r];\n GTt[i]=GTt[l]&GTt[r];\n EqT[i]=(EqT[l]&(EqT[r]|GTt[r]))|(EqT[r]&(EqT[l]|GTt[l]));\n } else {\n GTt[i]=GTt[l]|GTt[r];\n LSt[i]=LSt[l]&LSt[r];\n EqT[i]=(EqT[l]&~GTt[r])|(EqT[r]&~GTt[l]);\n }\n }\n }\n\n for(int k=0;k<=m;k++) w[k] = fact[k]*fact[m-k];\n\n ull sum_c = 0;\n for(int j=0;j<D;j++){\n if(EqS[r1].test(j) && EqT[r2].test(j))\n sum_c += w[__builtin_popcount((unsigned)j)];\n }\n total += sum_c;\n }\n Cout(total * globalF);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 8036, "score_of_the_acc": -0.0867, "final_rank": 4 }, { "submission_id": "aoj_1643_10440820", "code_snippet": "// AOJ #1643 Icpcan Alphabet\n// // 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nstring Cins() {\n static char temp[105];\n char *s = temp;\n do *s = gc();\n while (*s++ > ' ');\n *(s-1) = 0;\n string input(temp);\n return input;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconstexpr int MAXN = 16;\nconstexpr int MAXM = MAXN - 1;\nconstexpr int MAXJ = 1 << MAXM;\n\nvector<ll> fact;\n\nstruct Node {\n char op;\n int c[2], p;\n int let;\n ull mask;\n Node():op(0),p(-1),let(-1),mask(0){ c[0]=c[1]=-1; }\n};\n\nint parseExpr(const string &s, int &pos, vector<Node> &t, const vector<int> &id){\n if(s[pos]=='('){\n pos++;\n int L = parseExpr(s,pos,t,id);\n char op = s[pos++];\n int R = parseExpr(s,pos,t,id);\n pos++;\n int u = t.size();\n t.emplace_back();\n t[u].op = op;\n t[u].c[0] = L; t[u].c[1] = R;\n t[L].p = u; t[R].p = u;\n return u;\n } else {\n int idx = id[s[pos++]-'A'];\n int u = t.size();\n t.emplace_back();\n t[u].op = 'L';\n t[u].let = idx;\n t[u].mask = 1ull<<idx;\n return u;\n }\n}\n\nvoid build_mask(vector<Node> &t, int u){\n if(t[u].op != 'L'){\n build_mask(t, t[u].c[0]);\n build_mask(t, t[u].c[1]);\n t[u].mask = t[t[u].c[0]].mask | t[t[u].c[1]].mask;\n }\n}\n\nint main(){\n fact.resize(MAXN+1);\n fact[0] = 1;\n for(int i=1;i<=MAXN;i++) fact[i] = fact[i-1]*i;\n\n while(true){\n int n = Cin();\n if(n == 0) break;\n string chs = Cins(), S = Cins(), T = Cins();\n vector<int> id(26, -1);\n for(int i=0;i<n;i++) id[chs[i]-'A'] = i;\n\n vector<Node> a, b;\n int p1=0, p2=0;\n int r1 = parseExpr(S, p1, a, id);\n int r2 = parseExpr(T, p2, b, id);\n build_mask(a, r1);\n build_mask(b, r2);\n\n ull Um = a[r1].mask | b[r2].mask;\n vector<int> U;\n for(int i=0;i<n;i++) if(Um>>i & 1) U.push_back(i);\n int Usz = U.size();\n\n ll globalF = fact[n] / fact[Usz];\n __int128 total = 0;\n static bitset<MAXJ> LSs[256], EqS[256], GTs[256];\n static bitset<MAXJ> LSt[256], EqT[256], GTt[256];\n static bitset<MAXJ> pat[MAXM];\n bitset<MAXJ> dom;\n\n for(int c: U){\n bool inS=false, inT=false;\n for(auto &nd: a) if(nd.op=='L' && nd.let==c){ inS=true; break; }\n for(auto &nd: b) if(nd.op=='L' && nd.let==c){ inT=true; break; }\n if(!inS || !inT) continue;\n\n vector<int> U2; U2.reserve(Usz-1);\n for(int y: U) if(y!=c) U2.push_back(y);\n int m = Usz - 1;\n array<int,MAXN> id2;\n id2.fill(-1);\n for(int i2=0;i2<m;i2++) id2[U2[i2]] = i2;\n\n int D = 1<<m;\n dom.reset();\n for(int j=0;j<D;j++) dom.set(j);\n\n for(int pos=0;pos<m;pos++){\n pat[pos].reset();\n int step = 1<<(pos+1);\n int half = 1<<pos;\n for(int j0=half;j0<D;j0+=step){\n int end = min(j0+half, D);\n for(int k=j0;k<end;k++) pat[pos].set(k);\n }\n }\n\n int szA = a.size();\n for(int i=0;i<szA;i++){\n LSs[i].reset();\n EqS[i].reset();\n GTs[i].reset();\n if(a[i].op=='L'){\n int y = a[i].let;\n if(y == c) EqS[i] = dom;\n else {\n auto &g = GTs[i];\n g = pat[ id2[y] ];\n LSs[i] = dom ^ g;\n }\n } else {\n int l = a[i].c[0], r = a[i].c[1];\n if(a[i].op=='<'){\n LSs[i] = LSs[l] | LSs[r];\n GTs[i] = GTs[l] & GTs[r];\n EqS[i] = (EqS[l] & (EqS[r] | GTs[r])) | (EqS[r] & (EqS[l] | GTs[l]));\n } else {\n GTs[i] = GTs[l] | GTs[r];\n LSs[i] = LSs[l] & LSs[r];\n EqS[i] = (EqS[l] & ~GTs[r]) | (EqS[r] & ~GTs[l]);\n }\n }\n }\n\n int szB = b.size();\n for(int i=0;i<szB;i++){\n LSt[i].reset();\n EqT[i].reset();\n GTt[i].reset();\n if(b[i].op=='L'){\n int y = b[i].let;\n if(y == c) EqT[i] = dom;\n else {\n auto &g = GTt[i];\n g = pat[ id2[y] ];\n LSt[i] = dom ^ g;\n }\n } else {\n int l = b[i].c[0], r = b[i].c[1];\n if(b[i].op=='<'){\n LSt[i] = LSt[l] | LSt[r];\n GTt[i] = GTt[l] & GTt[r];\n EqT[i] = (EqT[l] & (EqT[r] | GTt[r])) | (EqT[r] & (EqT[l] | GTt[l]));\n } else {\n GTt[i] = GTt[l] | GTt[r];\n LSt[i] = LSt[l] & LSt[r];\n EqT[i] = (EqT[l] & ~GTt[r]) | (EqT[r] & ~GTt[l]);\n }\n }\n }\n\n static ll w[MAXN+1];\n for(int k=0;k<=m;k++) w[k] = fact[k] * fact[m-k];\n\n ll sum_c = 0;\n for(int j=0;j<D;j++){\n if(EqS[r1].test(j) && EqT[r2].test(j)){\n sum_c += w[__builtin_popcount((unsigned)j)];\n }\n }\n total += sum_c;\n }\n ll ans = (ll)(total * globalF);\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 7964, "score_of_the_acc": -0.0853, "final_rank": 3 }, { "submission_id": "aoj_1643_10340290", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nchar eval(string &s) {\n if((int)s.size() == 1) return s[0];\n auto dfs = [&](auto dfs, int i) -> pair<char, int> {\n char a;\n if(s[i + 1] == '(') {\n auto [c, j] = dfs(dfs, i + 1);\n a = c;\n i = j + 1;\n } else {\n a = s[i + 1];\n i += 2;\n }\n char x = s[i];\n char b;\n if(s[i + 1] == '(') {\n auto [c, j] = dfs(dfs, i + 1);\n b = c;\n i = j + 1;\n } else {\n b = s[i + 1];\n i += 2;\n }\n if(x == '>') return {max(a, b), i};\n return {min(a, b), i};\n };\n return dfs(dfs, 0).first;\n}\n\nint mp[256];\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n vector<ll> fac(17, 1);\n for(ll i = 2; i <= 16; i ++) fac[i] = fac[i - 1] * i;\n int n; cin >> n;\n while(n) {\n string cs; cin >> cs;\n string S, T;\n cin >> S >> T;\n ll ans = 0;\n rep(i, n) {\n int cnt = 0;\n rep(j, n) if(i != j) {\n mp[cs[j]] = cnt ++;\n }\n rep(bit, 1 << cnt) {\n string s = \"\", t = \"\";\n foa(e, S) {\n if('A' <= e and e <= 'Z') {\n if(e == cs[i]) s += '1'; \n else if(bit >> mp[e] & 1) s += '0';\n else s += '2';\n } else s += e;\n }\n foa(e, T) {\n if('A' <= e and e <= 'Z') {\n if(e == cs[i]) t += '1'; \n else if(bit >> mp[e] & 1) t += '0';\n else t += '2';\n } else t += e;\n }\n char c = eval(s);\n if(c == '1' and c == eval(t)) {\n int num = __builtin_popcount(bit);\n ans += fac[num] * fac[cnt - num];\n }\n }\n }\n cout << ans << endl;\n cin >> n;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2720, "memory_kb": 3584, "score_of_the_acc": -0.5934, "final_rank": 14 } ]
aoj_1642_cpp
Luggage Mr. Yokohama is estimating the delivery fare for a number of luggage pieces. The fare for a piece is determined by its width w , depth d and height h , each rounded up to an integer. The fare is proportional to the sum of them. The list of the luggage pieces is at hand, but the list has the product w × d × h for each piece, instead of the sum w + d + h by mistake. This information is not enough to calculate the exact delivery fare. Mr. Yokohama therefore decided to estimate the minimum possible delivery fare. To this end, given the listed product p for each of the luggage pieces, the minimum possible sum s = w + d + h satisfying p = w × d × h should be found. You are requested to help Mr. Yokohama by writing a program that, when given the product p , computes the minimum sum s. Input The input consists of multiple datasets. Each of the datasets has one line containing an integer p (0 < p < 10 15 ). The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 300. Output For each dataset, output a single line containing an integer s . s should be the minimum possible sum w + d + h of three positive integers, w, d, and h, satisfying p = w × d × h. Sample Input 1 2 6 8 729 47045881 12137858827249 562949953421312 986387345919360 999730024299271 999998765536093 0 Output for the Sample Input 3 4 6 6 27 1083 6967887 262144 298633 299973 999998765536095
[ { "submission_id": "aoj_1642_10849119", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N=4e7+3;\nbool isComposite[N];\nlong long primes[N], sz;\nvoid sieve() { // save the all primes till N in linear time\n isComposite[0] = isComposite[1] = true;\n for (int i = 2; i < N; ++i) {\n if (!isComposite[i]) primes[sz++] = i;\n for (int j = 0; j < sz && primes[j] * i < N; ++j) {\n isComposite[primes[j] * i] = true;\n if (i % primes[j] == 0) break;\n }\n }\n}\nint FirstPrimeIndexBiggerThan_1e5;\nvector<long long> HowManyPrimesBiggerThan_1e5(long long n){// given n and return the primes factor bigger than 1e5\n vector<long long>ret;\n for (int i = FirstPrimeIndexBiggerThan_1e5; i <sz&&primes[i]*primes[i]<=n ; ++i) {\n while(n%primes[i]==0)\n ret.push_back(primes[i]),n/=primes[i];\n }\n return ret;\n}\nvoid solve(long long n) {\n vector<long long>PrimesBiggerThan_1e5= HowManyPrimesBiggerThan_1e5(n);\n long long best=n+2;//the best save the best answer till now\n // of course \"1 1 n\" which his summation n+2 is the worst answer\n if(PrimesBiggerThan_1e5.size()==2) {\n best = PrimesBiggerThan_1e5[0] + PrimesBiggerThan_1e5[1] +\n n / (PrimesBiggerThan_1e5[0] * PrimesBiggerThan_1e5[1]);\n }\n else if(PrimesBiggerThan_1e5.size()==1){\n n/=PrimesBiggerThan_1e5[0];\n for (int i= 1; i<=min(n,(long long)1e5) ; ++i) {\n if(n%i==0)\n best=min(best,PrimesBiggerThan_1e5[0]+i+n/i);\n }\n }\n else{\n vector<long long>factor;\n for (int i = 1; i<=min(n,(long long)1e5) ; ++i) {\n if(n%i==0){\n factor.push_back(i);\n for (int j = 0; j <factor.size()&&i*factor[j]<=n ; ++j) {\n if(n%(i*factor[j])==0)\n best=min(best,i+factor[j]+n/(i*factor[j]));\n }\n }\n }\n }\n cout<<best<<'\\n';\n\n}\nint main() {\n sieve();\n FirstPrimeIndexBiggerThan_1e5= upper_bound(primes,primes+sz,1e5)-primes;\n long long n;\n while (cin>>n,n)\n solve(n);\n}", "accuracy": 1, "time_ms": 4760, "memory_kb": 64296, "score_of_the_acc": -0.9117, "final_rank": 17 }, { "submission_id": "aoj_1642_10675645", "code_snippet": "#line 1 \"2020C.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2020C.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(x)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst ll inf=1ll<<60;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\n// https://algo-method.com/tasks/553/editorial\n// Miller-Rabin 素数判定法\ntemplate<class T> T pow_mod(T A, T N, T M) {\n T res = 1 % M;\n A %= M;\n while (N) {\n if (N & 1) res = (res * A) % M;\n A = (A * A) % M;\n N >>= 1;\n }\n return res;\n}\n\nbool is_prime(long long N) {\n if (N <= 1) return false;\n if (N == 2 || N == 3) return true;\n if (N % 2 == 0) return false;\n vector<long long> A = {2, 325, 9375, 28178, 450775,\n 9780504, 1795265022};\n long long s = 0, d = N - 1;\n while (d % 2 == 0) {\n ++s;\n d >>= 1;\n }\n for (auto a : A) {\n if (a % N == 0) return true;\n long long t, x = pow_mod<__int128_t>(a, d, N);\n if (x != 1) {\n for (t = 0; t < s; ++t) {\n if (x == N - 1) break;\n x = __int128_t(x) * x % N;\n }\n if (t == s) return false;\n }\n }\n return true;\n}\n\n// Pollard のロー法\nlong long gcd(long long A, long long B) {\n A = abs(A), B = abs(B);\n if (B == 0) return A;\n else return gcd(B, A % B);\n}\n \nlong long pollard(long long N) {\n if (N % 2 == 0) return 2;\n if (is_prime(N)) return N;\n\n long long step = 0;\n auto f = [&](long long x) -> long long {\n return (__int128_t(x) * x + step) % N;\n };\n while (true) {\n ++step;\n long long x = step, y = f(x);\n while (true) {\n long long p = gcd(y - x + N, N);\n if (p == 0 || p == N) break;\n if (p != 1) return p;\n x = f(x);\n y = f(f(y));\n }\n }\n}\n\nvector<long long> prime_factorize(long long N) {\n if (N == 1) return {};\n long long p = pollard(N);\n if (p == N) return {p};\n vector<long long> left = prime_factorize(p);\n vector<long long> right = prime_factorize(N / p);\n left.insert(left.end(), right.begin(), right.end());\n sort(left.begin(), left.end());\n return left;\n}\n\n\nint solve(){\n ll p; cin >> p;\n if(p==0) return 1;\n auto a = prime_factorize(p);\n \n auto uniqued_a = a;\n uniqued_a.erase(unique(all(uniqued_a)), uniqued_a.end());\n\n map<ll,ll> mp;\n for(auto x: a) mp[x]++;\n dbg(a);\n dbg(mp);\n vll divisors;\n\n auto dfs = [&](auto self, ll cur, int i) {\n if(i==uniqued_a.size()) {\n divisors.emplace_back(cur);\n return;\n }\n\n ll muler = 1;\n rep(c, mp[uniqued_a[i]]+1) {\n cur *= muler;\n self(self,cur,i+1);\n cur /= muler;\n muler *= uniqued_a[i];\n }\n return;\n };\n\n dfs(dfs,1,0);\n sort(all(divisors));\n\n dbg(divisors);\n\n ll ans = inf;\nfor (auto a : divisors) {\n if (a * a * a > p) break;\n ll left = p / a;\n ll root = sqrtl((long double)left);\n auto it = upper_bound(divisors.begin(), divisors.end(), root);\n while (it != divisors.begin()) {\n --it;\n ll d = *it;\n if (d < a) break;\n if (left % d == 0) {\n ll h = left / d;\n ans = min(ans, a + d + h);\n break;\n }\n }\n}\n cout << ans << '\\n';\n return 0;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3836, "score_of_the_acc": -0.0017, "final_rank": 1 }, { "submission_id": "aoj_1642_10670482", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n// エラトステネスの篩\nstruct Eratosthenes {\n // テーブル\n vector<bool> isprime;\n\n // 整数 i を割り切る最小の素数\n vector<ll> minfactor;\n\n vector<ll> mobius;\n\n // コンストラクタで篩を回す\n Eratosthenes(ll N)\n : isprime(N + 1, true), minfactor(N + 1, -1), mobius(N + 1, 1) {\n // 1 は予めふるい落としておく\n isprime[1] = false;\n minfactor[1] = 1;\n\n // 篩\n for (ll p = 2; p <= N; ++p) {\n // すでに合成数であるものはスキップする\n if (!isprime[p]) continue;\n\n // p についての情報更新\n minfactor[p] = p;\n mobius[p] = -1;\n\n // p 以外の p の倍数から素数ラベルを剥奪\n for (ll q = p * 2; q <= N; q += p) {\n // q は合成数なのでふるい落とす\n isprime[q] = false;\n\n // q は p で割り切れる旨を更新\n if (minfactor[q] == -1) minfactor[q] = p;\n if ((q / p) % p == 0)\n mobius[q] = 0;\n else\n mobius[q] = -mobius[q];\n }\n }\n }\n\n // 高速素因数分解\n // pair (素因子, 指数) の vector を返す\n vector<pair<ll, ll>> factorize(ll n) {\n vector<pair<ll, ll>> res;\n while (n > 1) {\n ll p = minfactor[n];\n ll exp = 0;\n\n // n で割り切れる限り割る\n while (minfactor[n] == p) {\n n /= p;\n ++exp;\n }\n res.emplace_back(p, exp);\n }\n return res;\n }\n vector<ll> divisors(ll n) {\n vector<ll> res({1});\n auto pf = factorize(n);\n for (auto p : pf) {\n ll s = (ll)res.size();\n for (ll i = 0; i < s; i++) {\n ll v = 1;\n for (ll j = 0; j < p.second; j++) {\n v *= p.first;\n res.push_back(res[i] * v);\n }\n }\n }\n return res;\n }\n};\nint main() {\n vector<char> er(34000000, 1);\n er[0] = 0;\n er[1] = 1;\n vector<ll> primes;\n for (ll i = 2; i < er.size(); i++) {\n if (er[i]) {\n primes.push_back(i);\n for (ll j = i * 2; j < er.size(); j += i) {\n er[j] = 0;\n }\n }\n }\n while (1) {\n ll p;\n cin >> p;\n if (p == 0) break;\n\n using a2 = array<ll, 2>;\n vector<a2> cnt;\n ll n = p;\n for (auto x : primes) {\n if (x * x > n) break;\n ll c = 0;\n while (n % x == 0) {\n n /= x;\n c++;\n }\n if (c) cnt.push_back({x, c});\n }\n if (n > 1) cnt.push_back({n, 1});\n\n vector<ll> div;\n\n auto make = [&](auto self, ll now, ll depth) -> void {\n if (depth == cnt.size()) {\n div.push_back(now);\n } else {\n for (int i = 0; i <= cnt[depth][1]; i++) {\n self(self, now, depth + 1);\n now *= cnt[depth][0];\n }\n }\n };\n\n make(make, 1, 0);\n\n sort(div.begin(), div.end());\n unordered_set<ll> sh;\n for (auto x : div) sh.insert(x);\n\n ll ans = p + 2;\n for (int i = 0; i < div.size(); i++) {\n if (div[i] * div[i] * div[i] > p) break;\n\n ll n = p / div[i];\n\n ll sq = sqrt(n);\n auto itr = lower_bound(div.begin(), div.end(), sq);\n while (itr != div.end()) {\n if (n / (*itr) < div[i]) break;\n if (n % (*itr) == 0 && sh.count(n / (*itr))) {\n ans = min(ans, div[i] + (*itr) + n / (*itr));\n }\n itr++;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 4190, "memory_kb": 54336, "score_of_the_acc": -0.7892, "final_rank": 15 }, { "submission_id": "aoj_1642_10666221", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n# pragma GCC target(\"avx2\")\n# pragma GCC optimize(\"O3\")\n# pragma GCC optimize(\"unroll-loops\")\n\nusing ll = long long;\n\nint isqrt(ll N) {\n ll ok = 0, ng = 1e8;\n while(ok + 1 < ng) {\n ll x = (ok + ng) / 2;\n if(x * x <= N) ok = x;\n else ng = x;\n }\n return ok;\n}\n\nint icbrt(ll N) {\n ll ok = 0, ng = 2e5;\n while(ok + 1 < ng) {\n ll x = (ok + ng) / 2;\n if(x * x * x <= N) ok = x;\n else ng = x;\n }\n return ok;\n}\n\nbool solve() {\n ll N;\n cin >> N;\n if(N == 0) return false;\n const int X = icbrt(N);\n ll ans = N + 2;\n for(int x = X, c = 0; x >= 1 && c < 30; --x) {\n if(N % x == 0) {\n const int Y = isqrt(N / x);\n for(ll y = Y; y >= x; --y) {\n if((N / x) % y == 0) {\n c++;\n ans = min(ans, x + y + (N / x / y));\n break;\n }\n }\n }\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 7400, "memory_kb": 3456, "score_of_the_acc": -1, "final_rank": 18 }, { "submission_id": "aoj_1642_10666158", "code_snippet": "#include <bits/stdc++.h>\n#line 2 \"prime/fast-factorize.hpp\"\n\n#include <cstdint>\n#include <numeric>\n#include <vector>\nusing namespace std;\n\n#line 2 \"internal/internal-math.hpp\"\n\n#line 2 \"internal/internal-type-traits.hpp\"\n\n#include <type_traits>\nusing namespace std;\n\nnamespace internal {\ntemplate <typename T>\nusing is_broadly_integral =\n typename conditional_t<is_integral_v<T> || is_same_v<T, __int128_t> ||\n is_same_v<T, __uint128_t>,\n true_type, false_type>::type;\n\ntemplate <typename T>\nusing is_broadly_signed =\n typename conditional_t<is_signed_v<T> || is_same_v<T, __int128_t>,\n true_type, false_type>::type;\n\ntemplate <typename T>\nusing is_broadly_unsigned =\n typename conditional_t<is_unsigned_v<T> || is_same_v<T, __uint128_t>,\n true_type, false_type>::type;\n\n#define ENABLE_VALUE(x) \\\n template <typename T> \\\n constexpr bool x##_v = x<T>::value;\n\nENABLE_VALUE(is_broadly_integral);\nENABLE_VALUE(is_broadly_signed);\nENABLE_VALUE(is_broadly_unsigned);\n#undef ENABLE_VALUE\n\n#define ENABLE_HAS_TYPE(var) \\\n template <class, class = void> \\\n struct has_##var : false_type {}; \\\n template <class T> \\\n struct has_##var<T, void_t<typename T::var>> : true_type {}; \\\n template <class T> \\\n constexpr auto has_##var##_v = has_##var<T>::value;\n\n#define ENABLE_HAS_VAR(var) \\\n template <class, class = void> \\\n struct has_##var : false_type {}; \\\n template <class T> \\\n struct has_##var<T, void_t<decltype(T::var)>> : true_type {}; \\\n template <class T> \\\n constexpr auto has_##var##_v = has_##var<T>::value;\n\n} // namespace internal\n#line 4 \"internal/internal-math.hpp\"\n\nnamespace internal {\n\n#include <cassert>\n#include <utility>\n#line 10 \"internal/internal-math.hpp\"\nusing namespace std;\n\n// a mod p\ntemplate <typename T>\nT safe_mod(T a, T p) {\n a %= p;\n if constexpr (is_broadly_signed_v<T>) {\n if (a < 0) a += p;\n }\n return a;\n}\n\n// 返り値:pair(g, x)\n// s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\ntemplate <typename T>\npair<T, T> inv_gcd(T a, T p) {\n static_assert(is_broadly_signed_v<T>);\n a = safe_mod(a, p);\n if (a == 0) return {p, 0};\n T b = p, x = 1, y = 0;\n while (a != 0) {\n T q = b / a;\n swap(a, b %= a);\n swap(x, y -= q * x);\n }\n if (y < 0) y += p / b;\n return {b, y};\n}\n\n// 返り値 : a^{-1} mod p\n// gcd(a, p) != 1 が必要\ntemplate <typename T>\nT inv(T a, T p) {\n static_assert(is_broadly_signed_v<T>);\n a = safe_mod(a, p);\n T b = p, x = 1, y = 0;\n while (a != 0) {\n T q = b / a;\n swap(a, b %= a);\n swap(x, y -= q * x);\n }\n assert(b == 1);\n return y < 0 ? y + p : y;\n}\n\n// T : 底の型\n// U : T*T がオーバーフローしない かつ 指数の型\ntemplate <typename T, typename U>\nT modpow(T a, U n, T p) {\n a = safe_mod(a, p);\n T ret = 1 % p;\n while (n != 0) {\n if (n % 2 == 1) ret = U(ret) * a % p;\n a = U(a) * a % p;\n n /= 2;\n }\n return ret;\n}\n\n// 返り値 : pair(rem, mod)\n// 解なしのときは {0, 0} を返す\ntemplate <typename T>\npair<T, T> crt(const vector<T>& r, const vector<T>& m) {\n static_assert(is_broadly_signed_v<T>);\n assert(r.size() == m.size());\n int n = int(r.size());\n T r0 = 0, m0 = 1;\n for (int i = 0; i < n; i++) {\n assert(1 <= m[i]);\n T r1 = safe_mod(r[i], m[i]), m1 = m[i];\n if (m0 < m1) swap(r0, r1), swap(m0, m1);\n if (m0 % m1 == 0) {\n if (r0 % m1 != r1) return {0, 0};\n continue;\n }\n auto [g, im] = inv_gcd(m0, m1);\n T u1 = m1 / g;\n if ((r1 - r0) % g) return {0, 0};\n T x = (r1 - r0) / g % u1 * im % u1;\n r0 += x * m0;\n m0 *= u1;\n if (r0 < 0) r0 += m0;\n }\n return {r0, m0};\n}\n\n} // namespace internal\n#line 2 \"misc/rng.hpp\"\n\n#line 2 \"internal/internal-seed.hpp\"\n\n#include <chrono>\nusing namespace std;\n\nnamespace internal {\nunsigned long long non_deterministic_seed() {\n unsigned long long m =\n chrono::duration_cast<chrono::nanoseconds>(\n chrono::high_resolution_clock::now().time_since_epoch())\n .count();\n m ^= 9845834732710364265uLL;\n m ^= m << 24, m ^= m >> 31, m ^= m << 35;\n return m;\n}\nunsigned long long deterministic_seed() { return 88172645463325252UL; }\n\n// 64 bit の seed 値を生成 (手元では seed 固定)\n// 連続で呼び出すと同じ値が何度も返ってくるので注意\n// #define RANDOMIZED_SEED するとシードがランダムになる\nunsigned long long seed() {\n#if defined(NyaanLocal) && !defined(RANDOMIZED_SEED)\n return deterministic_seed();\n#else\n return non_deterministic_seed();\n#endif\n}\n\n} // namespace internal\n#line 4 \"misc/rng.hpp\"\n\nnamespace my_rand {\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n// [0, 2^64 - 1)\nu64 rng() {\n static u64 _x = internal::seed();\n return _x ^= _x << 7, _x ^= _x >> 9;\n}\n\n// [l, r]\ni64 rng(i64 l, i64 r) {\n assert(l <= r);\n return l + rng() % u64(r - l + 1);\n}\n\n// [l, r)\ni64 randint(i64 l, i64 r) {\n assert(l < r);\n return l + rng() % u64(r - l);\n}\n\n// choose n numbers from [l, r) without overlapping\nvector<i64> randset(i64 l, i64 r, i64 n) {\n assert(l <= r && n <= r - l);\n unordered_set<i64> s;\n for (i64 i = n; i; --i) {\n i64 m = randint(l, r + 1 - i);\n if (s.find(m) != s.end()) m = r - i;\n s.insert(m);\n }\n vector<i64> ret;\n for (auto& x : s) ret.push_back(x);\n sort(begin(ret), end(ret));\n return ret;\n}\n\n// [0.0, 1.0)\ndouble rnd() { return rng() * 5.42101086242752217004e-20; }\n// [l, r)\ndouble rnd(double l, double r) {\n assert(l < r);\n return l + rnd() * (r - l);\n}\n\ntemplate <typename T>\nvoid randshf(vector<T>& v) {\n int n = v.size();\n for (int i = 1; i < n; i++) swap(v[i], v[randint(0, i + 1)]);\n}\n\n} // namespace my_rand\n\nusing my_rand::randint;\nusing my_rand::randset;\nusing my_rand::randshf;\nusing my_rand::rnd;\nusing my_rand::rng;\n#line 2 \"modint/arbitrary-montgomery-modint.hpp\"\n\n#include <iostream>\nusing namespace std;\n\ntemplate <typename Int, typename UInt, typename Long, typename ULong, int id>\nstruct ArbitraryLazyMontgomeryModIntBase {\n using mint = ArbitraryLazyMontgomeryModIntBase;\n\n inline static UInt mod;\n inline static UInt r;\n inline static UInt n2;\n static constexpr int bit_length = sizeof(UInt) * 8;\n\n static UInt get_r() {\n UInt ret = mod;\n while (mod * ret != 1) ret *= UInt(2) - mod * ret;\n return ret;\n }\n static void set_mod(UInt m) {\n assert(m < (UInt(1u) << (bit_length - 2)));\n assert((m & 1) == 1);\n mod = m, n2 = -ULong(m) % m, r = get_r();\n }\n UInt a;\n\n ArbitraryLazyMontgomeryModIntBase() : a(0) {}\n ArbitraryLazyMontgomeryModIntBase(const Long &b)\n : a(reduce(ULong(b % mod + mod) * n2)){};\n\n static UInt reduce(const ULong &b) {\n return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length;\n }\n\n mint &operator+=(const mint &b) {\n if (Int(a += b.a - 2 * mod) < 0) a += 2 * mod;\n return *this;\n }\n mint &operator-=(const mint &b) {\n if (Int(a -= b.a) < 0) a += 2 * mod;\n return *this;\n }\n mint &operator*=(const mint &b) {\n a = reduce(ULong(a) * b.a);\n return *this;\n }\n mint &operator/=(const mint &b) {\n *this *= b.inverse();\n return *this;\n }\n\n mint operator+(const mint &b) const { return mint(*this) += b; }\n mint operator-(const mint &b) const { return mint(*this) -= b; }\n mint operator*(const mint &b) const { return mint(*this) *= b; }\n mint operator/(const mint &b) const { return mint(*this) /= b; }\n\n bool operator==(const mint &b) const {\n return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);\n }\n bool operator!=(const mint &b) const {\n return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);\n }\n mint operator-() const { return mint(0) - mint(*this); }\n mint operator+() const { return mint(*this); }\n\n mint pow(ULong n) const {\n mint ret(1), mul(*this);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul, n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const mint &b) {\n return os << b.get();\n }\n\n friend istream &operator>>(istream &is, mint &b) {\n Long t;\n is >> t;\n b = ArbitraryLazyMontgomeryModIntBase(t);\n return (is);\n }\n\n mint inverse() const {\n Int x = get(), y = get_mod(), u = 1, v = 0;\n while (y > 0) {\n Int t = x / y;\n swap(x -= t * y, y);\n swap(u -= t * v, v);\n }\n return mint{u};\n }\n\n UInt get() const {\n UInt ret = reduce(a);\n return ret >= mod ? ret - mod : ret;\n }\n\n static UInt get_mod() { return mod; }\n};\n\n// id に適当な乱数を割り当てて使う\ntemplate <int id>\nusing ArbitraryLazyMontgomeryModInt =\n ArbitraryLazyMontgomeryModIntBase<int, unsigned int, long long,\n unsigned long long, id>;\ntemplate <int id>\nusing ArbitraryLazyMontgomeryModInt64bit =\n ArbitraryLazyMontgomeryModIntBase<long long, unsigned long long, __int128_t,\n __uint128_t, id>;\n#line 2 \"prime/miller-rabin.hpp\"\n\n#line 4 \"prime/miller-rabin.hpp\"\nusing namespace std;\n\n#line 8 \"prime/miller-rabin.hpp\"\n\nnamespace fast_factorize {\n\ntemplate <typename T, typename U>\nbool miller_rabin(const T& n, vector<T> ws) {\n if (n <= 2) return n == 2;\n if (n % 2 == 0) return false;\n\n T d = n - 1;\n while (d % 2 == 0) d /= 2;\n U e = 1, rev = n - 1;\n for (T w : ws) {\n if (w % n == 0) continue;\n T t = d;\n U y = internal::modpow<T, U>(w, t, n);\n while (t != n - 1 && y != e && y != rev) y = y * y % n, t *= 2;\n if (y != rev && t % 2 == 0) return false;\n }\n return true;\n}\n\nbool miller_rabin_u64(unsigned long long n) {\n return miller_rabin<unsigned long long, __uint128_t>(\n n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});\n}\n\ntemplate <typename mint>\nbool miller_rabin(unsigned long long n, vector<unsigned long long> ws) {\n if (n <= 2) return n == 2;\n if (n % 2 == 0) return false;\n\n if (mint::get_mod() != n) mint::set_mod(n);\n unsigned long long d = n - 1;\n while (~d & 1) d >>= 1;\n mint e = 1, rev = n - 1;\n for (unsigned long long w : ws) {\n if (w % n == 0) continue;\n unsigned long long t = d;\n mint y = mint(w).pow(t);\n while (t != n - 1 && y != e && y != rev) y *= y, t *= 2;\n if (y != rev && t % 2 == 0) return false;\n }\n return true;\n}\n\nbool is_prime(unsigned long long n) {\n using mint32 = ArbitraryLazyMontgomeryModInt<96229631>;\n using mint64 = ArbitraryLazyMontgomeryModInt64bit<622196072>;\n\n if (n <= 2) return n == 2;\n if (n % 2 == 0) return false;\n if (n < (1uLL << 30)) {\n return miller_rabin<mint32>(n, {2, 7, 61});\n } else if (n < (1uLL << 62)) {\n return miller_rabin<mint64>(\n n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});\n } else {\n return miller_rabin_u64(n);\n }\n}\n\n} // namespace fast_factorize\n\nusing fast_factorize::is_prime;\n\n/**\n * @brief Miller-Rabin primality test\n */\n#line 12 \"prime/fast-factorize.hpp\"\n\nnamespace fast_factorize {\nusing u64 = uint64_t;\n\ntemplate <typename mint, typename T>\nT pollard_rho(T n) {\n if (~n & 1) return 2;\n if (is_prime(n)) return n;\n if (mint::get_mod() != n) mint::set_mod(n);\n mint R, one = 1;\n auto f = [&](mint x) { return x * x + R; };\n auto rnd_ = [&]() { return rng() % (n - 2) + 2; };\n while (1) {\n mint x, y, ys, q = one;\n R = rnd_(), y = rnd_();\n T g = 1;\n constexpr int m = 128;\n for (int r = 1; g == 1; r <<= 1) {\n x = y;\n for (int i = 0; i < r; ++i) y = f(y);\n for (int k = 0; g == 1 && k < r; k += m) {\n ys = y;\n for (int i = 0; i < m && i < r - k; ++i) q *= x - (y = f(y));\n g = gcd(q.get(), n);\n }\n }\n if (g == n) do\n g = gcd((x - (ys = f(ys))).get(), n);\n while (g == 1);\n if (g != n) return g;\n }\n exit(1);\n}\n\nusing i64 = long long;\n\nvector<i64> inner_factorize(u64 n) {\n using mint32 = ArbitraryLazyMontgomeryModInt<452288976>;\n using mint64 = ArbitraryLazyMontgomeryModInt64bit<401243123>;\n\n if (n <= 1) return {};\n u64 p;\n if (n <= (1LL << 30)) {\n p = pollard_rho<mint32, uint32_t>(n);\n } else if (n <= (1LL << 62)) {\n p = pollard_rho<mint64, uint64_t>(n);\n } else {\n exit(1);\n }\n if (p == n) return {i64(p)};\n auto l = inner_factorize(p);\n auto r = inner_factorize(n / p);\n copy(begin(r), end(r), back_inserter(l));\n return l;\n}\n\nvector<i64> factorize(u64 n) {\n auto ret = inner_factorize(n);\n sort(begin(ret), end(ret));\n return ret;\n}\n\nmap<i64, i64> factor_count(u64 n) {\n map<i64, i64> mp;\n for (auto &x : factorize(n)) mp[x]++;\n return mp;\n}\n\nvector<i64> divisors(u64 n) {\n if (n == 0) return {};\n vector<pair<i64, i64>> v;\n for (auto &p : factorize(n)) {\n if (v.empty() || v.back().first != p) {\n v.emplace_back(p, 1);\n } else {\n v.back().second++;\n }\n }\n vector<i64> ret;\n auto f = [&](auto rc, int i, i64 x) -> void {\n if (i == (int)v.size()) {\n ret.push_back(x);\n return;\n }\n rc(rc, i + 1, x);\n for (int j = 0; j < v[i].second; j++) rc(rc, i + 1, x *= v[i].first);\n };\n f(f, 0, 1);\n sort(begin(ret), end(ret));\n return ret;\n}\n\n} // namespace fast_factorize\n\nusing fast_factorize::divisors;\nusing fast_factorize::factor_count;\nusing fast_factorize::factorize;\n\n/**\n * @brief 高速素因数分解(Miller Rabin/Pollard's Rho)\n * @docs docs/prime/fast-factorize.md\n */\nusing namespace std;\nusing ll=long long;\nusing ull=unsigned long long;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)\nconstexpr ll ma=10'000'000'000'000'000LL;\nll N;ll res;\nsigned main() {\n cin.tie(0)->sync_with_stdio(0);\n cout<<fixed<<setprecision(16);\n for (;cin>>N,N;) {\n vector<ll>d=factorize(N);\n vector<pair<ll,int>>d0;\n for (const ll i:d) {\n if (d0.empty()||d0.back().first!=i)d0.emplace_back(i,1);\n else ++d0.back().second;\n }\n vector<vector<ll>>d1(d0.size());\n rep(i,d0.size()) {\n d1[i].resize(d0[i].second+1);\n d1[i][0]=1;\n rep(j,d0[i].second)d1[i][j+1]=d1[i][j]*d0[i].first;\n }\n res=ma;\n {\n auto f=[&](auto self,const int p,const ll l,const ll r)->void {\n if (p==(int)d0.size()) {\n if (l+r+N/l/r<res)res=l+r+N/l/r;\n return;\n }\n rep(i,d0[p].second+1)rep(j,d0[p].second-i+1) {\n self(self,p+1,l*d1[p][i],r*d1[p][j]);\n }\n return;\n };\n f(f,0,(ll)1,(ll)1);\n }\n cout<<res<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 3640, "memory_kb": 3584, "score_of_the_acc": -0.4869, "final_rank": 7 }, { "submission_id": "aoj_1642_10665809", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct montgomery_modint {\n using int64 = uint64_t;\n using int128 = __uint128_t;\n using modint = montgomery_modint;\n montgomery_modint() : x(0) {}\n montgomery_modint(long long v) : x(reduce((int128(v) + MOD) * R)) {}\n static void set_mod(long long _m) {\n MOD = _m;\n R = -int128(MOD) % MOD;\n INV = get_inv_mod(); \n }\n static long long mod() { return MOD; }\n long long val() const {\n int64 res = reduce(x);\n return res >= MOD ? res - MOD : res;\n }\n modint& operator+=(const modint &r) {\n x += r.x;\n if (x >= (MOD << 1)) x -= (MOD << 1);\n return *this;\n }\n modint& operator-=(const modint &r) {\n x += (MOD << 1) - r.x;\n if (x >= (MOD << 1)) x -= (MOD << 1);\n return *this;\n }\n modint& operator*=(const modint &r) {\n x = reduce(int128(x) * r.x);\n return *this;\n }\n modint& operator/=(const modint &r) {\n *this *= r.inv();\n return *this;\n }\n friend modint operator+(const modint &a, const modint &b) {\n return modint(a) += b;\n }\n friend modint operator-(const modint &a, const modint &b) {\n return modint(a) -= b;\n }\n friend modint operator*(const modint &a, const modint &b) {\n return modint(a) *= b;\n }\n friend modint operator/(const modint &a, const modint &b) {\n return modint(a) /= b;\n }\n friend bool operator==(const modint &a, const modint &b) {\n return a.val() == b.val();\n }\n friend bool operator!=(const modint &a, const modint &b) {\n return a.val() != b.val();\n }\n modint operator+() const { return *this; }\n modint operator-() const { return modint() - *this; }\n modint inv() const { return pow(MOD - 2); }\n modint pow(int128 k) const {\n modint a = *this;\n modint res = 1;\n while (k > 0) {\n if (k & 1) res *= a;\n a *= a;\n k >>= 1;\n }\n return res;\n }\nprivate:\n int64 x;\n static int64 MOD, INV, R;\n static int64 get_inv_mod() {\n int64 res = MOD;\n for (int t = 0; t < 5; t++) res *= 2 - MOD * res;\n return res;\n }\n static int64 reduce(const int128 &v) {\n return (v + int128(int64(v) * int64(-INV)) * MOD) >> 64;\n }\n};\ntypename montgomery_modint::int64\nmontgomery_modint::MOD, montgomery_modint::INV, montgomery_modint::R;\n\nbool miller_rabin(long long m, const vector<long long> ps) {\n using mint = montgomery_modint;\n mint::set_mod(m);\n long long u = 0, v = m - 1;\n while ((v & 1) == 0) u++, v >>= 1;\n for (long long p : ps) {\n if (m <= p) return true;\n mint x = mint(p).pow(v);\n if (x != 1) {\n long long w;\n for (w = 0; w < u; w++) {\n if (x == m - 1) break;\n x *= x;\n }\n if (u == w) return false;\n }\n }\n return true;\n}\n\nbool miller_rabin_small(long long m) {\n return miller_rabin(m, {2, 7, 61});\n}\n\nbool miller_rabin_large(long long m) {\n return miller_rabin(m, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});\n}\n\nbool is_prime(long long m) {\n if (m <= 1) return false;\n if (m == 2) return true;\n if (m % 2 == 0) return false;\n return m < 4759123141LL ? miller_rabin_small(m) : miller_rabin_large(m);\n}\n\ntemplate<typename T>\nT rho(T n) {\n for (int p : {2, 3, 5, 7}) {\n if (n % p == 0) return p;\n }\n auto randint_64 = [&](T low, T hi) -> T {\n random_device seed;\n mt19937_64 rng(seed());\n uniform_int_distribution<T> dist(low, hi);\n return dist(rng);\n };\n using mint = montgomery_modint;\n mint::set_mod(n);\n while (true) {\n mint u = randint_64(2, n - 1);\n mint v = u;\n mint c = randint_64(1, n - 1);\n T d = 1;\n while (d == 1) {\n u = u * u + c;\n v = v * v + c;\n v = v * v + c;\n d = gcd((u - v).val(), n);\n }\n if (d < n) return d;\n }\n return -1;\n}\n\ntemplate<typename T>\nvector<T> prime_factor(T n) {\n if (n <= 1) return {};\n if (is_prime(n)) return {n};\n vector<T> res;\n T d = rho(n);\n auto a = prime_factor(d);\n auto b = prime_factor(n / d);\n merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));\n res.erase(unique(res.begin(), res.end()), res.end());\n return res;\n}\n\nvector<pair<long long, int>> factorise(long long n) {\n auto ps = prime_factor(n);\n vector<pair<long long, int>> res;\n res.reserve(ps.size());\n for (auto p : ps) {\n res.emplace_back(p, 0);\n while (n % p == 0) {\n res.back().second++;\n n /= p;\n }\n }\n return res;\n}\n\ntemplate<typename T>\nvector<T> enumerate_divisors(T n) {\n auto f = factorise(n);\n int m = f.size();\n vector<T> res;\n auto rec = [&](const auto &rec, int i, T x) -> void {\n if (i == m) {\n res.push_back(x);\n return;\n }\n auto [p, k] = f[i];\n for (int j = 0; j <= k; j++) {\n rec(rec, i + 1, x);\n x *= p;\n }\n };\n rec(rec, 0, 1);\n sort(res.begin(), res.end());\n return res;\n}\n\nbool solve() {\n long long P;\n cin >> P;\n if (P == 0) return false;\n auto D = enumerate_divisors(P);\n int N = D.size();\n long long ans = 1LL << 60;\n for (int i = 0; i < N && D[i] * D[i] * D[i] <= P; i++) {\n for (int j = i; j < N && D[i] * D[j] * D[j] <= P; j++) {\n if (P % (D[i] * D[j]) == 0) {\n ans = min(ans, D[i] + D[j] + P / (D[i] * D[j]));\n }\n }\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 1680, "memory_kb": 3804, "score_of_the_acc": -0.2201, "final_rank": 3 }, { "submission_id": "aoj_1642_10649662", "code_snippet": "// #pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace atcoder;\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing vecint = std::vector<int>;\nusing vecll = std::vector<long long>;\nusing vecstr = std::vector<string>;\nusing vecbool = std::vector<bool>;\nusing vecdou = std::vector<double>;\nusing vecpl = std::vector<pair<ll,ll>>;\nusing vec2d = std::vector<vecll>;\nusing vec2di = std::vector<vecint>;\nusing vec2dd = std::vector<vecdou>;\nusing vec2db = std::vector<vecbool>;\nusing pl = pair<long long,long long>;\nusing mint998 = modint998244353;\nusing mint107 = modint1000000007;\nusing mint = modint;\nusing vecmint = std::vector<mint>;\nusing vecmint998 = std::vector<mint998>;\nusing vecmint107 = std::vector<mint107>;\nusing vec2dmint = std::vector<vecmint>; \nusing vec2dmint998 = std::vector<vecmint998>;\nusing vec2dmint107 = std::vector<vecmint107>;\n#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)\n#define rep1(i,n) for (ll i = 1; i <= (ll)(n); i++)\n#define REP(i,l,r) for (ll i = (ll)(l); i < (ll)(r); i++)\n#define rrep(i,n) for (ll i = (ll)(n)-1; i >= 0; i--)\n#define rrep1(i,n) for (ll i = (ll)(n); i > 0; i--)\n#define RREP(i,l,r) for (ll i = (ll)(r)-1; i >= (ll)(l); i--)\n#define all(a) (a).begin(), (a).end()\n#define INF ((1LL<<62)-(1LL<<31))\n#define inr(a,x,b) ((a) <= (x) && (x) < (b))\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvoid ynout(bool x,string Tru=\"Yes\",string Wro=\"No\"){\n if(x){\n cout << Tru << '\\n';\n }else{\n cout << Wro << '\\n';\n }\n}\nll power(ll a,ll b,ll mod=INF){\n long long x=1,y=a%mod;\n while(b>0){\n if(b&1ll){\n x=(x*y)%mod;\n }\n y=(y*y)%mod;\n b>>=1;\n }\n return x%mod;\n}\nll Pdist2(pair<ll,ll> a,pair<ll,ll> b){\n return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second);\n}\ndouble Pdist(pair<ll,ll> a,pair<ll,ll> b){\n return sqrt(Pdist2(a,b));\n}\nll PdistM(pair<ll,ll> a,pair<ll,ll> b){\n return abs(a.first-b.first)+abs(a.second-b.second);\n}\nll gcd(ll a,ll b){\n if(b==0){\n return a;\n }else{\n return gcd(b,a%b);\n }\n}\nll lcm(ll a,ll b){\n return a/gcd(a,b)*b;\n}\nvector<ll> prime;\nbool seen[40000009];\nbool solve(){\n ll p;\n cin>>p;\n if(p == 0)return 1;\n vector<ll> soinsuu;\n for(auto v : prime){\n if(p % v == 0)soinsuu.emplace_back(v);\n }\n vector<ll> insuu = {1};\n ll P = p;\n for(auto v : soinsuu){\n vector<ll> t = {1};\n ll c = 1;\n while(P % v == 0){\n P /= v;\n c *= v;\n t.emplace_back(c);\n }\n vector<ll> r;\n for(auto x : insuu){\n for(auto y : t){\n r.emplace_back(x*y);\n }\n }\n insuu = r;\n }\n insuu.emplace_back(2e9);\n sort(all(insuu));\n ll ans = 2e18;\n for(int i=1; i<=1e5; i++){\n if(p % i != 0)continue;\n ll p1 = p / i;\n ll l = 0;\n ll r = insuu.size()-1;\n while(r-l > 1){\n ll m = (l+r)/2;\n if(p1 < insuu[m]*insuu[m]){\n r = m;\n }\n else l = m;\n }\n while(p1 % insuu[l] != 0)l--;\n if(l >= 0)chmin(ans,i+insuu[l]+p1/insuu[l]);\n }\n cout << ans << endl;\n return 0;\n}\nint main() {\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n \n for(int i=2; i<=4e7; i++){\n if(seen[i])continue;\n prime.emplace_back(i);\n for(int j=2; i*j<=4e7; j++){\n seen[i*j] = 1;\n }\n }\n // cout << prime.size() << endl;\n while(1){\n if(solve())return 0;\n }\n}", "accuracy": 1, "time_ms": 2200, "memory_kb": 76304, "score_of_the_acc": -0.6157, "final_rank": 14 }, { "submission_id": "aoj_1642_10611550", "code_snippet": "// https://codeforces.com/blog/entry/96344\n//#pragma GCC optimize(\"O3,unroll-loops\")\n//#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n \n#include <bits/stdc++.h>\n \nusing namespace std;\n \nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\n \n#if __cplusplus >= 202002L\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n#endif\n \ntemplate<typename T>\nbool chmax(T& a, const T& b) {\n bool res = a < b;\n a = max(a, b);\n return res;\n}\ntemplate<typename T>\nbool chmin(T& a, const T& b){\n bool res = a > b;\n a = min(a, b);\n return res;\n}\n \ntypedef vector<long long> vl;\ntypedef pair<ll,ll> pll;\ntypedef vector<pair<ll, ll>> vll;\ntypedef vector<int> vi;\ntypedef vector<pair<int,int>> vii;\ntypedef pair<int,int> pii;\n \nconst int inf = 1000000009;\nconst ll linf = 4000000000000000009;\n \n \n// https://trap.jp/post/1224/\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n \nvoid print(){\n cout << '\\n';\n}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n \ntemplate <typename T>\nT floor(T a, T b) {\n return a / b - (a % b && (a ^ b) < 0);\n}\ntemplate <typename T>\nT ceil(T x, T y) {\n return floor(x + y - 1, y);\n}\n \nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n \ntemplate<typename T, typename U>\nT SUM(const vector<U> &A){\n T ret = 0;\n for (auto &&a: A) ret += a;\n return ret;\n}\n \n#define rep1(a) for(int i = 0; i < a; i++)\n#define rep2(i, a) for(int i = 0; i < a; i++)\n#define rep3(i, a, b) for(int i = a; i < b; i++)\n#define rep4(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n \n#define rrep(i, a, b) for(int i = a; i >= b; i--)\n \n#define eb emplace_back\n#define mp make_pair\n#define mt make_tuple\n#define fi first\n#define se second\n#define all(v) v.begin(), v.end()\n//---------------------------------\n \n\nvi primes, sPrime(40000000);\nll p;\n\nvll factorize(ll n){\n vll ret;\n for(ll i = 0; i < primes.size() && primes[i] * primes[i] <= n; i++){\n int cnt = 0;\n while(n % primes[i] == 0){\n cnt++;\n n /= primes[i];\n }\n if(cnt > 0) ret.push_back(mp(primes[i], cnt));\n }\n return ret;\n}\n\nvoid divs(ll n, ll j, vll &pr, vl &d){\n if(n > (ll)sqrt(p) + 10) return;\n if(j == pr.size()){\n d.push_back(n);\n return;\n }\n for(int i = 0; i <= pr[j].se; i++){\n divs(n, j+1, pr, d);\n n *= pr[j].fi;\n }\n}\n\nvoid solve(){\n vll pr = factorize(p);\n vector<ll> di;\n divs(1, 0, pr, di);\n ll ans = p + 2;\n //cout << ans << endl;\n //for(auto v: di) cout << v << endl;\n rep(i, di.size()) rep(j, i, di.size()){\n ll s = di[i] + di[j];\n ll pro = di[i] * di[j];\n if(p % pro == 0){\n s += p / pro;\n chmin(ans, s);\n }\n }\n\n cout << ans << endl;\n\n}\n \n \nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n \n for (int i = 2; (ll)i * i <= 100000000000000LL; ++i) {\n if (!sPrime[i]) {\n sPrime[i] = i;\n primes.push_back(i);\n }\n for (int j = 0; j < primes.size() && primes[j] <= sPrime[j]; ++j) {\n if ((ll)i * primes[j] > 40000000) break;\n sPrime[primes[j] * i] = primes[j];\n }\n }\n\n //cout << \"BUUB\" << endl;\n cin >> p;\n while(p > 0){\n solve();\n cin >> p;\n }\n}", "accuracy": 1, "time_ms": 5250, "memory_kb": 226868, "score_of_the_acc": -1.7063, "final_rank": 20 }, { "submission_id": "aoj_1642_10611513", "code_snippet": "#include <bits/extc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define REPR(i, n) for (int i = n; i >= 0; i--)\n#define FOR(i, j, n) for (int i = j; i < n; i++)\n#define ll long long\n#define P pair<ll, ll>\n#define ALL(s) (s).begin(), (s).end()\nbool IsPrime(ll n) {\n if (n <= 1) return false;\n for (ll i = 2; i <= sqrt(n); i++) {\n if (n % i == 0) return false;\n }\n return true;\n}\nvector<bool> ok(40000001, true);\nvector<ll> res;\nvoid era(ll n) {\n ok[0] = false;\n ok[1] = false;\n for (ll i = 2; i <= sqrt(n); ++i) {\n if (IsPrime(i)) {\n for (ll j = i * i; j <= n; j += i) ok[j] = false;\n }\n }\n ///*\n FOR(i, 2, n + 1) if (ok[i]) res.push_back(i);\n\n // return ok;\n}\n\nvoid solve(ll i, ll a, vector<P> &r, vector<ll> &t) {\n if (i >= (ll)r.size()) {\n t.push_back(a);\n return;\n }\n ll now = 1;\n REP(kk, r[i].second + 1) {\n solve(i + 1, a * now, r, t);\n now *= r[i].first;\n }\n}\n\nint main() {\n era(40000000);\n while (1) {\n ll n;\n cin >> n;\n if (n == 0) return 0;\n vector<P> r;\n ll nn = n;\n for (auto &x : res) {\n ll now = 0;\n if (x * x > n) break;\n while (nn % x == 0) {\n nn /= x;\n now++;\n }\n if (now == 0) continue;\n r.push_back(P(x, now));\n }\n if (nn > 1) {\n r.push_back(P(nn, 1));\n }\n sort(ALL(r));\n vector<ll> t;\n solve(0, 1, r, t);\n sort(ALL(t));\n ll t1 = 0, t2 = 0;\n ll sq = sqrt(n);\n REP(i, (ll)t.size()) {\n if (t[i] <= sq) {\n t2 = i;\n if (t[i] * t[i] <= n / t[i]) {\n t1 = i;\n }\n }\n }\n ll ans = 1ll << 60;\n REPR(i, t2) {\n REPR(j, t1) {\n if (n % (t[i] * t[j]) == 0) {\n ans = min(ans, t[i] + t[j] + n / (t[i] * t[j]));\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 6320, "memory_kb": 42892, "score_of_the_acc": -1.029, "final_rank": 19 }, { "submission_id": "aoj_1642_10610733", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\n// https://nyaannyaan.github.io/library/prime/fast-factorize.hpp\n\n#line 2 \"prime/fast-factorize.hpp\"\n\n#include <cstdint>\n#include <numeric>\n#include <vector>\nusing namespace std;\n\n#line 2 \"internal/internal-math.hpp\"\n\n#line 2 \"internal/internal-type-traits.hpp\"\n\n#include <type_traits>\nusing namespace std;\n\nnamespace internal {\ntemplate <typename T>\nusing is_broadly_integral =\n typename conditional_t<is_integral_v<T> || is_same_v<T, __int128_t> ||\n is_same_v<T, __uint128_t>,\n true_type, false_type>::type;\n\ntemplate <typename T>\nusing is_broadly_signed =\n typename conditional_t<is_signed_v<T> || is_same_v<T, __int128_t>,\n true_type, false_type>::type;\n\ntemplate <typename T>\nusing is_broadly_unsigned =\n typename conditional_t<is_unsigned_v<T> || is_same_v<T, __uint128_t>,\n true_type, false_type>::type;\n\n#define ENABLE_VALUE(x) \\\n template <typename T> \\\n constexpr bool x##_v = x<T>::value;\n\nENABLE_VALUE(is_broadly_integral);\nENABLE_VALUE(is_broadly_signed);\nENABLE_VALUE(is_broadly_unsigned);\n#undef ENABLE_VALUE\n\n#define ENABLE_HAS_TYPE(var) \\\n template <class, class = void> \\\n struct has_##var : false_type {}; \\\n template <class T> \\\n struct has_##var<T, void_t<typename T::var>> : true_type {}; \\\n template <class T> \\\n constexpr auto has_##var##_v = has_##var<T>::value;\n\n#define ENABLE_HAS_VAR(var) \\\n template <class, class = void> \\\n struct has_##var : false_type {}; \\\n template <class T> \\\n struct has_##var<T, void_t<decltype(T::var)>> : true_type {}; \\\n template <class T> \\\n constexpr auto has_##var##_v = has_##var<T>::value;\n\n} // namespace internal\n#line 4 \"internal/internal-math.hpp\"\n\nnamespace internal {\n\n#include <cassert>\n#include <utility>\n#line 10 \"internal/internal-math.hpp\"\nusing namespace std;\n\n// a mod p\ntemplate <typename T>\nT safe_mod(T a, T p) {\n a %= p;\n if constexpr (is_broadly_signed_v<T>) {\n if (a < 0) a += p;\n }\n return a;\n}\n\n// 返り値:pair(g, x)\n// s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\ntemplate <typename T>\npair<T, T> inv_gcd(T a, T p) {\n static_assert(is_broadly_signed_v<T>);\n a = safe_mod(a, p);\n if (a == 0) return {p, 0};\n T b = p, x = 1, y = 0;\n while (a != 0) {\n T q = b / a;\n swap(a, b %= a);\n swap(x, y -= q * x);\n }\n if (y < 0) y += p / b;\n return {b, y};\n}\n\n// 返り値 : a^{-1} mod p\n// gcd(a, p) != 1 が必要\ntemplate <typename T>\nT inv(T a, T p) {\n static_assert(is_broadly_signed_v<T>);\n a = safe_mod(a, p);\n T b = p, x = 1, y = 0;\n while (a != 0) {\n T q = b / a;\n swap(a, b %= a);\n swap(x, y -= q * x);\n }\n assert(b == 1);\n return y < 0 ? y + p : y;\n}\n\n// T : 底の型\n// U : T*T がオーバーフローしない かつ 指数の型\ntemplate <typename T, typename U>\nT modpow(T a, U n, T p) {\n a = safe_mod(a, p);\n T ret = 1 % p;\n while (n != 0) {\n if (n % 2 == 1) ret = U(ret) * a % p;\n a = U(a) * a % p;\n n /= 2;\n }\n return ret;\n}\n\n// 返り値 : pair(rem, mod)\n// 解なしのときは {0, 0} を返す\ntemplate <typename T>\npair<T, T> crt(const vector<T>& r, const vector<T>& m) {\n static_assert(is_broadly_signed_v<T>);\n assert(r.size() == m.size());\n int n = int(r.size());\n T r0 = 0, m0 = 1;\n for (int i = 0; i < n; i++) {\n assert(1 <= m[i]);\n T r1 = safe_mod(r[i], m[i]), m1 = m[i];\n if (m0 < m1) swap(r0, r1), swap(m0, m1);\n if (m0 % m1 == 0) {\n if (r0 % m1 != r1) return {0, 0};\n continue;\n }\n auto [g, im] = inv_gcd(m0, m1);\n T u1 = m1 / g;\n if ((r1 - r0) % g) return {0, 0};\n T x = (r1 - r0) / g % u1 * im % u1;\n r0 += x * m0;\n m0 *= u1;\n if (r0 < 0) r0 += m0;\n }\n return {r0, m0};\n}\n\n} // namespace internal\n#line 2 \"misc/rng.hpp\"\n\n#line 2 \"internal/internal-seed.hpp\"\n\n#include <chrono>\nusing namespace std;\n\nnamespace internal {\nunsigned long long non_deterministic_seed() {\n unsigned long long m =\n chrono::duration_cast<chrono::nanoseconds>(\n chrono::high_resolution_clock::now().time_since_epoch())\n .count();\n m ^= 9845834732710364265uLL;\n m ^= m << 24, m ^= m >> 31, m ^= m << 35;\n return m;\n}\nunsigned long long deterministic_seed() { return 88172645463325252UL; }\n\n// 64 bit の seed 値を生成 (手元では seed 固定)\n// 連続で呼び出すと同じ値が何度も返ってくるので注意\n// #define RANDOMIZED_SEED するとシードがランダムになる\nunsigned long long seed() {\n#if defined(NyaanLocal) && !defined(RANDOMIZED_SEED)\n return deterministic_seed();\n#else\n return non_deterministic_seed();\n#endif\n}\n\n} // namespace internal\n#line 4 \"misc/rng.hpp\"\n\nnamespace my_rand {\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n// [0, 2^64 - 1)\nu64 rng() {\n static u64 _x = internal::seed();\n return _x ^= _x << 7, _x ^= _x >> 9;\n}\n\n// [l, r]\ni64 rng(i64 l, i64 r) {\n assert(l <= r);\n return l + rng() % u64(r - l + 1);\n}\n\n// [l, r)\ni64 randint(i64 l, i64 r) {\n assert(l < r);\n return l + rng() % u64(r - l);\n}\n\n// choose n numbers from [l, r) without overlapping\nvector<i64> randset(i64 l, i64 r, i64 n) {\n assert(l <= r && n <= r - l);\n unordered_set<i64> s;\n for (i64 i = n; i; --i) {\n i64 m = randint(l, r + 1 - i);\n if (s.find(m) != s.end()) m = r - i;\n s.insert(m);\n }\n vector<i64> ret;\n for (auto& x : s) ret.push_back(x);\n sort(begin(ret), end(ret));\n return ret;\n}\n\n// [0.0, 1.0)\ndouble rnd() { return rng() * 5.42101086242752217004e-20; }\n// [l, r)\ndouble rnd(double l, double r) {\n assert(l < r);\n return l + rnd() * (r - l);\n}\n\ntemplate <typename T>\nvoid randshf(vector<T>& v) {\n int n = v.size();\n for (int i = 1; i < n; i++) swap(v[i], v[randint(0, i + 1)]);\n}\n\n} // namespace my_rand\n\nusing my_rand::randint;\nusing my_rand::randset;\nusing my_rand::randshf;\nusing my_rand::rnd;\nusing my_rand::rng;\n#line 2 \"modint/arbitrary-montgomery-modint.hpp\"\n\n#include <iostream>\nusing namespace std;\n\ntemplate <typename Int, typename UInt, typename Long, typename ULong, int id>\nstruct ArbitraryLazyMontgomeryModIntBase {\n using mint = ArbitraryLazyMontgomeryModIntBase;\n\n inline static UInt mod;\n inline static UInt r;\n inline static UInt n2;\n static constexpr int bit_length = sizeof(UInt) * 8;\n\n static UInt get_r() {\n UInt ret = mod;\n while (mod * ret != 1) ret *= UInt(2) - mod * ret;\n return ret;\n }\n static void set_mod(UInt m) {\n assert(m < (UInt(1u) << (bit_length - 2)));\n assert((m & 1) == 1);\n mod = m, n2 = -ULong(m) % m, r = get_r();\n }\n UInt a;\n\n ArbitraryLazyMontgomeryModIntBase() : a(0) {}\n ArbitraryLazyMontgomeryModIntBase(const Long &b)\n : a(reduce(ULong(b % mod + mod) * n2)){};\n\n static UInt reduce(const ULong &b) {\n return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length;\n }\n\n mint &operator+=(const mint &b) {\n if (Int(a += b.a - 2 * mod) < 0) a += 2 * mod;\n return *this;\n }\n mint &operator-=(const mint &b) {\n if (Int(a -= b.a) < 0) a += 2 * mod;\n return *this;\n }\n mint &operator*=(const mint &b) {\n a = reduce(ULong(a) * b.a);\n return *this;\n }\n mint &operator/=(const mint &b) {\n *this *= b.inverse();\n return *this;\n }\n\n mint operator+(const mint &b) const { return mint(*this) += b; }\n mint operator-(const mint &b) const { return mint(*this) -= b; }\n mint operator*(const mint &b) const { return mint(*this) *= b; }\n mint operator/(const mint &b) const { return mint(*this) /= b; }\n\n bool operator==(const mint &b) const {\n return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);\n }\n bool operator!=(const mint &b) const {\n return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);\n }\n mint operator-() const { return mint(0) - mint(*this); }\n mint operator+() const { return mint(*this); }\n\n mint pow(ULong n) const {\n mint ret(1), mul(*this);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul, n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const mint &b) {\n return os << b.get();\n }\n\n friend istream &operator>>(istream &is, mint &b) {\n Long t;\n is >> t;\n b = ArbitraryLazyMontgomeryModIntBase(t);\n return (is);\n }\n\n mint inverse() const {\n Int x = get(), y = get_mod(), u = 1, v = 0;\n while (y > 0) {\n Int t = x / y;\n swap(x -= t * y, y);\n swap(u -= t * v, v);\n }\n return mint{u};\n }\n\n UInt get() const {\n UInt ret = reduce(a);\n return ret >= mod ? ret - mod : ret;\n }\n\n static UInt get_mod() { return mod; }\n};\n\n// id に適当な乱数を割り当てて使う\ntemplate <int id>\nusing ArbitraryLazyMontgomeryModInt =\n ArbitraryLazyMontgomeryModIntBase<int, unsigned int, long long,\n unsigned long long, id>;\ntemplate <int id>\nusing ArbitraryLazyMontgomeryModInt64bit =\n ArbitraryLazyMontgomeryModIntBase<long long, unsigned long long, __int128_t,\n __uint128_t, id>;\n#line 2 \"prime/miller-rabin.hpp\"\n\n#line 4 \"prime/miller-rabin.hpp\"\nusing namespace std;\n\n#line 8 \"prime/miller-rabin.hpp\"\n\nnamespace fast_factorize {\n\ntemplate <typename T, typename U>\nbool miller_rabin(const T& n, vector<T> ws) {\n if (n <= 2) return n == 2;\n if (n % 2 == 0) return false;\n\n T d = n - 1;\n while (d % 2 == 0) d /= 2;\n U e = 1, rev = n - 1;\n for (T w : ws) {\n if (w % n == 0) continue;\n T t = d;\n U y = internal::modpow<T, U>(w, t, n);\n while (t != n - 1 && y != e && y != rev) y = y * y % n, t *= 2;\n if (y != rev && t % 2 == 0) return false;\n }\n return true;\n}\n\nbool miller_rabin_u64(unsigned long long n) {\n return miller_rabin<unsigned long long, __uint128_t>(\n n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});\n}\n\ntemplate <typename mint>\nbool miller_rabin(unsigned long long n, vector<unsigned long long> ws) {\n if (n <= 2) return n == 2;\n if (n % 2 == 0) return false;\n\n if (mint::get_mod() != n) mint::set_mod(n);\n unsigned long long d = n - 1;\n while (~d & 1) d >>= 1;\n mint e = 1, rev = n - 1;\n for (unsigned long long w : ws) {\n if (w % n == 0) continue;\n unsigned long long t = d;\n mint y = mint(w).pow(t);\n while (t != n - 1 && y != e && y != rev) y *= y, t *= 2;\n if (y != rev && t % 2 == 0) return false;\n }\n return true;\n}\n\nbool is_prime(unsigned long long n) {\n using mint32 = ArbitraryLazyMontgomeryModInt<96229631>;\n using mint64 = ArbitraryLazyMontgomeryModInt64bit<622196072>;\n\n if (n <= 2) return n == 2;\n if (n % 2 == 0) return false;\n if (n < (1uLL << 30)) {\n return miller_rabin<mint32>(n, {2, 7, 61});\n } else if (n < (1uLL << 62)) {\n return miller_rabin<mint64>(\n n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});\n } else {\n return miller_rabin_u64(n);\n }\n}\n\n} // namespace fast_factorize\n\nusing fast_factorize::is_prime;\n\n/**\n * @brief Miller-Rabin primality test\n */\n#line 12 \"prime/fast-factorize.hpp\"\n\nnamespace fast_factorize {\nusing u64 = uint64_t;\n\ntemplate <typename mint, typename T>\nT pollard_rho(T n) {\n if (~n & 1) return 2;\n if (is_prime(n)) return n;\n if (mint::get_mod() != n) mint::set_mod(n);\n mint R, one = 1;\n auto f = [&](mint x) { return x * x + R; };\n auto rnd_ = [&]() { return rng() % (n - 2) + 2; };\n while (1) {\n mint x, y, ys, q = one;\n R = rnd_(), y = rnd_();\n T g = 1;\n constexpr int m = 128;\n for (int r = 1; g == 1; r <<= 1) {\n x = y;\n for (int i = 0; i < r; ++i) y = f(y);\n for (int k = 0; g == 1 && k < r; k += m) {\n ys = y;\n for (int i = 0; i < m && i < r - k; ++i) q *= x - (y = f(y));\n g = gcd(q.get(), n);\n }\n }\n if (g == n) do\n g = gcd((x - (ys = f(ys))).get(), n);\n while (g == 1);\n if (g != n) return g;\n }\n exit(1);\n}\n\nusing i64 = long long;\n\nvector<i64> inner_factorize(u64 n) {\n using mint32 = ArbitraryLazyMontgomeryModInt<452288976>;\n using mint64 = ArbitraryLazyMontgomeryModInt64bit<401243123>;\n\n if (n <= 1) return {};\n u64 p;\n if (n <= (1LL << 30)) {\n p = pollard_rho<mint32, uint32_t>(n);\n } else if (n <= (1LL << 62)) {\n p = pollard_rho<mint64, uint64_t>(n);\n } else {\n exit(1);\n }\n if (p == n) return {i64(p)};\n auto l = inner_factorize(p);\n auto r = inner_factorize(n / p);\n copy(begin(r), end(r), back_inserter(l));\n return l;\n}\n\nvector<i64> factorize(u64 n) {\n auto ret = inner_factorize(n);\n sort(begin(ret), end(ret));\n return ret;\n}\n\nmap<i64, i64> factor_count(u64 n) {\n map<i64, i64> mp;\n for (auto &x : factorize(n)) mp[x]++;\n return mp;\n}\n\nvector<i64> divisors(u64 n) {\n if (n == 0) return {};\n vector<pair<i64, i64>> v;\n for (auto &p : factorize(n)) {\n if (v.empty() || v.back().first != p) {\n v.emplace_back(p, 1);\n } else {\n v.back().second++;\n }\n }\n vector<i64> ret;\n auto f = [&](auto rc, int i, i64 x) -> void {\n if (i == (int)v.size()) {\n ret.push_back(x);\n return;\n }\n rc(rc, i + 1, x);\n for (int j = 0; j < v[i].second; j++) rc(rc, i + 1, x *= v[i].first);\n };\n f(f, 0, 1);\n sort(begin(ret), end(ret));\n return ret;\n}\n\n} // namespace fast_factorize\n\nusing fast_factorize::divisors;\nusing fast_factorize::factor_count;\nusing fast_factorize::factorize;\n\n/**\n * @brief 高速素因数分解(Miller Rabin/Pollard's Rho)\n * @docs docs/prime/fast-factorize.md\n */\n\nconst ll INF=1LL<<60;\n\nvoid Solve(ll P){\n vector<ll>D=divisors(P);\n ll ans=INF;\n int L=D.size();\n for(int i=0;i<L;i++){\n ll w=D[i];\n if(w*w*w>P)break;\n ll Q=P/w;\n for(int j=i;j<L;j++){\n ll d=D[j];\n if(d*d>Q)break;\n if(Q%d!=0)continue;\n ll n=Q/d;\n chmin(ans,w+d+n);\n }\n }\n cout<<ans<<endl;\n}\n\nint main(){\n while(1){\n ll P;\n cin>>P;\n if(P==0)return 0;\n Solve(P);\n }\n}", "accuracy": 1, "time_ms": 1630, "memory_kb": 3940, "score_of_the_acc": -0.2139, "final_rank": 2 }, { "submission_id": "aoj_1642_10610538", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\n\nnamespace fast_factorize {\n\n/*\n See : https://judge.yosupo.jp/submission/189742\n*/\n\n// ---- gcd ----\n\nuint64_t gcd_stein_impl( uint64_t x, uint64_t y ) {\n if( x == y ) { return x; }\n const uint64_t a = y - x;\n const uint64_t b = x - y;\n const int n = __builtin_ctzll( b );\n const uint64_t s = x < y ? a : b;\n const uint64_t t = x < y ? x : y;\n return gcd_stein_impl( s >> n, t );\n}\n\nuint64_t gcd_stein( uint64_t x, uint64_t y ) {\n if( x == 0 ) { return y; }\n if( y == 0 ) { return x; }\n const int n = __builtin_ctzll( x );\n const int m = __builtin_ctzll( y );\n return gcd_stein_impl( x >> n, y >> m ) << ( n < m ? n : m );\n}\n\n// ---- is_prime ----\n\nuint64_t mod_pow( uint64_t x, uint64_t y, uint64_t mod ) {\n uint64_t ret = 1;\n uint64_t acc = x;\n for( ; y; y >>= 1 ) {\n if( y & 1 ) {\n ret = __uint128_t(ret) * acc % mod;\n }\n acc = __uint128_t(acc) * acc % mod;\n }\n return ret;\n}\n\nbool miller_rabin( uint64_t n, const std::initializer_list<uint64_t>& as ) {\n return std::all_of( as.begin(), as.end(), [n]( uint64_t a ) {\n if( n <= a ) { return true; }\n\n int e = __builtin_ctzll( n - 1 );\n uint64_t z = mod_pow( a, ( n - 1 ) >> e, n );\n if( z == 1 || z == n - 1 ) { return true; }\n\n while( --e ) {\n z = __uint128_t(z) * z % n;\n if( z == 1 ) { return false; }\n if( z == n - 1 ) { return true; }\n }\n\n return false;\n });\n}\n\nbool is_prime( uint64_t n ) {\n if( n == 2 ) { return true; }\n if( n % 2 == 0 ) { return false; }\n if( n < 4759123141 ) { return miller_rabin( n, { 2, 7, 61 } ); }\n return miller_rabin( n, { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 } );\n}\n\n// ---- Montgomery ----\n\nclass Montgomery {\n uint64_t mod;\n uint64_t R;\npublic:\n Montgomery( uint64_t n ) : mod(n), R(n) {\n for( size_t i = 0; i < 5; ++i ) {\n R *= 2 - mod * R;\n }\n }\n\n uint64_t fma( uint64_t a, uint64_t b, uint64_t c ) const {\n const __uint128_t d = __uint128_t(a) * b;\n const uint64_t e = c + mod + ( d >> 64 );\n const uint64_t f = uint64_t(d) * R;\n const uint64_t g = ( __uint128_t(f) * mod ) >> 64;\n return e - g;\n }\n\n uint64_t mul( uint64_t a, uint64_t b ) const {\n return fma( a, b, 0 );\n }\n};\n\n// ---- Pollard's rho algorithm ----\n\nuint64_t pollard_rho( uint64_t n ) {\n if( n % 2 == 0 ) { return 2; }\n const Montgomery m( n );\n\n constexpr uint64_t C1 = 1;\n constexpr uint64_t C2 = 2;\n constexpr uint64_t M = 512;\n\n uint64_t Z1 = 1;\n uint64_t Z2 = 2;\nretry:\n uint64_t z1 = Z1;\n uint64_t z2 = Z2;\n for( size_t k = M; ; k *= 2 ) {\n const uint64_t x1 = z1 + n;\n const uint64_t x2 = z2 + n;\n for( size_t j = 0; j < k; j += M ) {\n const uint64_t y1 = z1;\n const uint64_t y2 = z2;\n\n uint64_t q1 = 1;\n uint64_t q2 = 2;\n z1 = m.fma( z1, z1, C1 );\n z2 = m.fma( z2, z2, C2 );\n for( size_t i = 0; i < M; ++i ) {\n const uint64_t t1 = x1 - z1;\n const uint64_t t2 = x2 - z2;\n z1 = m.fma( z1, z1, C1 );\n z2 = m.fma( z2, z2, C2 );\n q1 = m.mul( q1, t1 );\n q2 = m.mul( q2, t2 );\n }\n q1 = m.mul( q1, x1 - z1 );\n q2 = m.mul( q2, x2 - z2 );\n\n const uint64_t q3 = m.mul( q1, q2 );\n const uint64_t g3 = gcd_stein( n, q3 );\n if( g3 == 1 ) { continue; }\n if( g3 != n ) { return g3; }\n\n const uint64_t g1 = gcd_stein( n, q1 );\n const uint64_t g2 = gcd_stein( n, q2 );\n\n const uint64_t C = g1 != 1 ? C1 : C2;\n const uint64_t x = g1 != 1 ? x1 : x2;\n uint64_t z = g1 != 1 ? y1 : y2;\n uint64_t g = g1 != 1 ? g1 : g2;\n\n if( g == n ) {\n do {\n z = m.fma( z, z, C );\n g = gcd_stein( n, x - z );\n } while( g == 1 );\n }\n if( g != n ) {\n return g;\n }\n\n Z1 += 2;\n Z2 += 2;\n goto retry;\n }\n }\n}\n\nvoid factorize_impl( uint64_t n, std::vector<uint64_t>& ret ) {\n if( n <= 1 ) { return; }\n if( is_prime( n ) ) { ret.push_back( n ); return; }\n\n const uint64_t p = pollard_rho( n );\n\n factorize_impl( p, ret );\n factorize_impl( n / p, ret );\n}\n\nstd::vector<uint64_t> factorize( uint64_t n ) {\n std::vector<uint64_t> ret;\n factorize_impl( n, ret );\n std::sort( ret.begin(), ret.end() );\n return ret;\n}\n\n} // namespace fast_factorize\n\nnamespace noya2 {\n\nstd::vector<std::pair<long long, int>> factorize(long long n){\n std::vector<std::pair<long long, int>> ans;\n auto ps = fast_factorize::factorize(n);\n int sz = ps.size();\n for (int l = 0, r = 0; l < sz; l = r){\n while (r < sz && ps[l] == ps[r]) r++;\n ans.emplace_back(ps[l], r-l);\n }\n return ans;\n}\n\nstd::vector<long long> divisors(long long n){\n auto ps = fast_factorize::factorize(n);\n int sz = ps.size();\n std::vector<long long> ans = {1};\n for (int l = 0, r = 0; l < sz; l = r){\n while (r < sz && ps[l] == ps[r]) r++;\n int e = r - l;\n int len = ans.size();\n ans.reserve(len*(e+1));\n long long mul = ps[l];\n while (true){\n for (int i = 0; i < len; i++){\n ans.emplace_back(ans[i]*mul);\n }\n if (--e == 0) break;\n mul *= ps[l];\n }\n }\n return ans;\n}\n\nstd::vector<long long> divisors(const std::vector<std::pair<long long, int>> &pes){\n std::vector<long long> ans = {1};\n for (auto [p, e] : pes){\n int len = ans.size();\n ans.reserve(len*(e+1));\n long long mul = p;\n while (true){\n for (int i = 0; i < len; i++){\n ans.emplace_back(ans[i]*mul);\n }\n if (--e == 0) break;\n mul *= p;\n }\n }\n return ans;\n}\n\nbool is_prime(long long n){\n if (n <= 1) return false;\n return fast_factorize::is_prime(n);\n}\n\n} // namespace noya2\n\nvoid solve(ll p){\n auto ds = divisors(p);\n sort(all(ds));\n ll ans = p+2;\n int sz = ds.size();\n ll lim = 100100;\n rep(i,sz){\n if (ds[i] >= lim) break;\n repp(j,i,sz){\n if (ds[i] > p/ds[j] + 1) break;\n if (ds[i]*ds[j] > p/ds[j] + 1) break;\n if (p % (ds[i] * ds[j]) != 0) continue;\n chmin(ans,ds[i]+ds[j]+p/(ds[i]*ds[j]));\n }\n }\n out(ans);\n}\n\nint main(){\n while(true){\n ll p; in(p);\n if (p == 0) break;\n solve(p);\n }\n}", "accuracy": 1, "time_ms": 2430, "memory_kb": 3940, "score_of_the_acc": -0.3232, "final_rank": 5 }, { "submission_id": "aoj_1642_10586313", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconstexpr ll INF = 4e18;\nvector<ll> primes;\nvector<ll> eratosthenes(ll n) {\n vector<bool> is_prime(n + 1, true);\n is_prime[0] = false;\n is_prime[1] = false;\n for (ll i = 2; i * i <= n; i++) {\n if (is_prime[i]) {\n for (ll j = 2 * i; j <= n; j += i) {\n is_prime[j] = false;\n }\n }\n }\n vector<ll> p;\n for (int i = 0; i <= n; i++) {\n if (is_prime[i]) {\n p.push_back(i);\n }\n }\n return p;\n}\n\nvector<ll> divisors(ll n) {\n vector<ll> divs = {1};\n for (auto p : primes) {\n if (p * p > n) {\n break;\n }\n if (n % p != 0) {\n continue;\n }\n\n int cnt = 0;\n while (n % p == 0) {\n n /= p;\n cnt++;\n }\n int siz = divs.size();\n for (int i = 0; i < siz; i++) {\n ll prod = 1;\n for (int c = 0; c < cnt; c++) {\n prod *= p;\n divs.push_back(divs[i] * prod);\n }\n }\n }\n\n if (n > 1) {\n int siz = divs.size();\n for (int i = 0; i < siz; i++) {\n divs.push_back(divs[i] * n);\n }\n }\n\n ranges::sort(divs);\n return divs;\n}\n\nvoid solve() {\n ll p;\n cin >> p;\n if (p == 0) {\n exit(0);\n }\n\n auto divs = divisors(p);\n ll ans = INF;\n for (ll d1 : divs) {\n if (d1 * d1 * d1 > p) {\n break;\n }\n ll rem = p / d1;\n for (ll d2 : divs) {\n if (d1 * d2 * d2 > p) {\n break;\n }\n if (rem % d2 == 0) {\n ll d3 = rem / d2;\n ll sum = d1 + d2 + d3;\n ans = min(ans, sum);\n }\n }\n }\n cout << ans << endl;\n}\n\nint main() {\n primes = eratosthenes(static_cast<ll>(sqrt(1e15)));\n while (true) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 3000, "memory_kb": 25484, "score_of_the_acc": -0.4975, "final_rank": 10 }, { "submission_id": "aoj_1642_10586272", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconstexpr ll INF = 4e18;\nvector<ll> primes;\nvector<ll> eratosthenes(ll n) {\n vector<bool> is_prime(n + 1, true);\n is_prime[0] = false;\n is_prime[1] = false;\n for (ll i = 2; i * i <= n; i++) {\n if (is_prime[i]) {\n for (ll j = 2 * i; j <= n; j += i) {\n is_prime[j] = false;\n }\n }\n }\n vector<ll> p;\n for (int i = 0; i <= n; i++) {\n if (is_prime[i]) {\n p.push_back(i);\n }\n }\n return p;\n}\n\nvector<ll> divisors(ll n) {\n vector<ll> divs = {1};\n for (auto p : primes) {\n if (p * p > n) {\n break;\n }\n if (n % p != 0) {\n continue;\n }\n\n int cnt = 0;\n while (n % p == 0) {\n n /= p;\n cnt++;\n }\n int siz = divs.size();\n for (int i = 0; i < siz; i++) {\n ll prod = 1;\n for (int c = 0; c < cnt; c++) {\n prod *= p;\n divs.push_back(divs[i] * prod);\n }\n }\n }\n\n if (n > 1) {\n int siz = divs.size();\n for (int i = 0; i < siz; i++) {\n divs.push_back(divs[i] * n);\n }\n }\n\n ranges::sort(divs);\n return divs;\n}\n\nvoid solve() {\n ll p;\n cin >> p;\n if (p == 0) {\n exit(0);\n }\n\n auto divs = divisors(p);\n // ll lim1 = (ll) pow(p, 1.0 / 3.0) + 4;\n ll ans = INF;\n for (ll d1 : divs) {\n ll x = p / d1;\n if (d1 * d1 * d1 > p) {\n break;\n }\n for (ll d2 : divs) {\n if (d1 * d2 * d2 > p) {\n break;\n }\n\n if (x % d2 != 0) {\n continue;\n }\n ll d3 = x / d2;\n ll res = d1 + d2 + d3;\n ans = min(ans, res);\n }\n }\n cout << ans << endl;\n}\n\nint main() {\n primes = eratosthenes(static_cast<ll>(sqrt(1e15)));\n while (true) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 2990, "memory_kb": 25352, "score_of_the_acc": -0.4955, "final_rank": 8 }, { "submission_id": "aoj_1642_10586228", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconstexpr ll INF = 4e18;\nvector<ll> primes;\nvector<ll> eratosthenes(ll n) {\n vector<bool> is_prime(n + 1, true);\n is_prime[0] = false;\n is_prime[1] = false;\n for (ll i = 2; i * i <= n; i++) {\n if (is_prime[i]) {\n for (ll j = 2 * i; j <= n; j += i) {\n is_prime[j] = false;\n }\n }\n }\n vector<ll> p;\n for (int i = 0; i <= n; i++) {\n if (is_prime[i]) {\n p.push_back(i);\n }\n }\n return p;\n}\n\nvector<ll> divisors(ll n) {\n vector<ll> divs = {1};\n for (auto p : primes) {\n if (p * p > n) {\n break;\n }\n if (n % p != 0) {\n continue;\n }\n\n int cnt = 0;\n while (n % p == 0) {\n n /= p;\n cnt++;\n }\n int siz = divs.size();\n for (int i = 0; i < siz; i++) {\n ll prod = 1;\n for (int c = 0; c < cnt; c++) {\n prod *= p;\n divs.push_back(divs[i] * prod);\n }\n }\n }\n\n if (n > 1) {\n int siz = divs.size();\n for (int i = 0; i < siz; i++) {\n divs.push_back(divs[i] * n);\n }\n }\n\n ranges::sort(divs);\n return divs;\n}\n\nvoid solve() {\n ll p;\n cin >> p;\n if (p == 0) {\n exit(0);\n }\n\n auto divs = divisors(p);\n ll lim1 = (ll) pow(p, 1.0 / 3.0) + 4;\n ll ans = INF;\n for (int i = 0; i < divs.size() && divs[i] < lim1; i++) {\n ll f1 = divs[i];\n ll rest = p / f1;\n ll lim2 = (ll) sqrt(rest) + 4;\n for (int j = 0; j < divs.size() && divs[j] < lim2; j++) {\n ll f2 = divs[j];\n if (rest % f2 == 0) {\n ll f3 = rest / f2;\n ll sum = f1 + f2 + f3;\n ans = min(ans, sum);\n }\n }\n }\n cout << ans << endl;\n}\n\nint main() {\n primes = eratosthenes(static_cast<ll>(sqrt(1e15)));\n while (true) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 3000, "memory_kb": 25356, "score_of_the_acc": -0.4969, "final_rank": 9 }, { "submission_id": "aoj_1642_10586124", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nconst ll llINF = (long long)4e18 + 22000020;\n\ntemplate<class T, class U>\nbool chmin(T &a, const U &b) {\n return ((a > b) ? (a = b, true) : false);\n}\n\nvector<ll> primes;\n\nvector<ll> eratosthenes(ll n) {\n vector<bool> isPrime(n + 1, true);\n isPrime[0] = false;\n isPrime[1] = false;\n\n for (ll i = 2; i * i <= n; i++) {\n if (isPrime[i]) {\n ll prod = i * 2;\n while (prod <= n) {\n isPrime[prod] = false;\n prod += i;\n }\n }\n }\n\n vector<ll> ret;\n for (int i = 0; i < isPrime.size(); i++) {\n if (isPrime[i]) ret.emplace_back(i);\n }\n return ret;\n}\n\nvector<ll> enum_divisors(ll n) {\n vector<ll> ret = {1};\n for (auto pr : primes) {\n if (pr * pr > n) break;\n if (n % pr != 0) continue;\n int cnt = 0;\n while (n % pr == 0) {\n n /= pr;\n cnt++;\n }\n int sz = ret.size();\n for (int i = 0; i < sz; i++) {\n ll prod = 1;\n for (int c = 0; c < cnt; c++) {\n prod *= pr;\n ret.emplace_back(ret[i] * prod);\n }\n }\n }\n\n if (n > 1) {\n int sz = ret.size();\n for (int i = 0; i < sz; i++) {\n ret.emplace_back(ret[i] * n);\n }\n }\n\n sort(ret.begin(), ret.end());\n return ret;\n}\n\n\nvoid solve() {\n ll p;\n cin >> p;\n if (p == 0) exit(0);\n auto divs = enum_divisors(p);\n\n ll limit1 = (ll)pow(p, 1.0 / 3.0) + 4;\n\n ll ans = llINF;\n for (int i = 0; i < divs.size() && divs[i] < limit1; i++) {\n ll f1 = divs[i];\n ll rest = p / f1;\n ll limit2 = (ll)sqrt(rest) + 4;\n for (int j = 0; j < divs.size() && divs[j] < limit2; j++) {\n ll f2 = divs[j];\n if (rest % f2 == 0) {\n ll f3 = rest / f2;\n ll sum = f1 + f2 + f3;\n chmin(ans, sum);\n }\n }\n }\n\n cout << ans << endl;\n}\n\nint main() {\n primes = move(eratosthenes((ll)sqrt(1e15) + 10));\n while (true) solve();\n \n return 0;\n}", "accuracy": 1, "time_ms": 3030, "memory_kb": 25484, "score_of_the_acc": -0.5016, "final_rank": 11 }, { "submission_id": "aoj_1642_10586121", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nconst ll llINF = (long long)4e18 + 22000020;\n\ntemplate<class T, class U>\nbool chmin(T &a, const U &b) {\n return ((a > b) ? (a = b, true) : false);\n}\n\nvector<ll> primes;\n\nvector<ll> eratosthenes(ll n) {\n vector<bool> isPrime(n + 1, true);\n isPrime[0] = false;\n isPrime[1] = false;\n\n for (ll i = 2; i * i <= n; i++) {\n if (isPrime[i]) {\n ll prod = i * 2;\n while (prod <= n) {\n isPrime[prod] = false;\n prod += i;\n }\n }\n }\n\n vector<ll> ret;\n for (int i = 0; i < isPrime.size(); i++) {\n if (isPrime[i]) ret.emplace_back(i);\n }\n return ret;\n}\n\nvector<ll> enum_divisors(ll n) {\n vector<ll> ret = {1};\n for (auto pr : primes) {\n if (pr * pr > n) break;\n if (n % pr != 0) continue;\n int cnt = 0;\n while (n % pr == 0) {\n n /= pr;\n cnt++;\n }\n int sz = ret.size();\n for (int i = 0; i < sz; i++) {\n ll prod = 1;\n for (int c = 0; c < cnt; c++) {\n prod *= pr;\n ret.emplace_back(ret[i] * prod);\n }\n }\n }\n\n if (n > 1) {\n int sz = ret.size();\n for (int i = 0; i < sz; i++) {\n ret.emplace_back(ret[i] * n);\n }\n }\n\n sort(ret.begin(), ret.end());\n return ret;\n}\n\n\nvoid solve() {\n ll p;\n cin >> p;\n if (p == 0) exit(0);\n auto divs = enum_divisors(p);\n\n ll limit1 = (ll)pow(p, 1.0 / 3.0) + 4;\n\n ll ans = llINF;\n for (int i = 0; i < divs.size() && divs[i] < limit1; i++) {\n ll f1 = divs[i];\n ll rest = p / f1;\n ll limit2 = (ll)sqrt(rest) + 4;\n for (int j = 0; j < divs.size() && divs[j] < limit2; j++) {\n ll f2 = divs[j];\n if (rest % f2 == 0) {\n ll f3 = rest / f2;\n ll sum = f1 + f2 + f3;\n chmin(ans, sum);\n }\n }\n }\n\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n primes = move(eratosthenes((ll)sqrt(1e15) + 10));\n while (true) solve();\n \n return 0;\n}", "accuracy": 1, "time_ms": 3060, "memory_kb": 25508, "score_of_the_acc": -0.5058, "final_rank": 12 }, { "submission_id": "aoj_1642_10586086", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nconst ll INF = 4e18;\n\ntemplate <class T, class U>\nbool chmin(T& a, const U& b) {\n return ((a > b) ? (a = b, true) : false);\n}\n\nvector<ll> primes;\n\nvector<ll> eratosthenes(ll n) {\n vector<bool> isPrime(n + 1, true);\n isPrime[0] = false;\n isPrime[1] = false;\n\n for (ll i = 2; i * i <= n; i++) {\n if (isPrime[i]) {\n ll prod = i * 2;\n while (prod <= n) {\n isPrime[prod] = false;\n prod += i;\n }\n }\n }\n\n vector<ll> ret;\n for (int i = 0; i < isPrime.size(); i++) {\n if (isPrime[i]) {\n ret.emplace_back(i);\n }\n }\n return ret;\n}\n\nvector<ll> enum_divisors(ll n) {\n vector<ll> ret = {1};\n for (auto pr : primes) {\n if (pr * pr > n) {\n break;\n }\n if (n % pr != 0) {\n continue;\n }\n int cnt = 0;\n while (n % pr == 0) {\n n /= pr;\n cnt++;\n }\n int sz = ret.size();\n for (int i = 0; i < sz; i++) {\n ll prod = 1;\n for (int c = 0; c < cnt; c++) {\n prod *= pr;\n ret.emplace_back(ret[i] * prod);\n }\n }\n }\n\n if (n > 1) {\n int sz = ret.size();\n for (int i = 0; i < sz; i++) {\n ret.emplace_back(ret[i] * n);\n }\n }\n\n sort(ret.begin(), ret.end());\n return ret;\n}\n\n\nvoid solve() {\n ll p;\n cin >> p;\n if (p == 0) {\n exit(0);\n }\n auto divs = enum_divisors(p);\n\n ll limit1 = (ll) pow(p, 1.0 / 3.0) + 4;\n\n ll ans = INF;\n for (int i = 0; i < divs.size() && divs[i] < limit1; i++) {\n ll f1 = divs[i];\n ll rest = p / f1;\n ll limit2 = (ll) sqrt(rest) + 4;\n for (int j = 0; j < divs.size() && divs[j] < limit2; j++) {\n ll f2 = divs[j];\n if (rest % f2 == 0) {\n ll f3 = rest / f2;\n ll sum = f1 + f2 + f3;\n chmin(ans, sum);\n }\n }\n }\n\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n primes = move(eratosthenes((ll) sqrt(1e15) + 10));\n while (true) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 3060, "memory_kb": 25508, "score_of_the_acc": -0.5058, "final_rank": 12 }, { "submission_id": "aoj_1642_10586008", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing u64 = uint64_t;\n\n// --- factorize --------------------------------------------------------------\nvector<pair<u64, uint32_t>> factorize(u64 n) {\n vector<pair<u64, uint32_t>> res;\n\n for (u64 p : {2, 3}) {\n uint32_t c = 0;\n while (n % p == 0) {\n ++c;\n n /= p;\n }\n if (c) {\n res.emplace_back(p, c);\n }\n }\n\n u64 k = 5, add = 2;\n while (k * k <= n) {\n uint32_t c = 0;\n while (n % k == 0) {\n n /= k;\n ++c;\n }\n if (c) {\n res.emplace_back(k, c);\n }\n k += add;\n add ^= 6; // 2 → 4 → 2 → 4 …\n }\n\n if (n > 1) {\n res.emplace_back(n, 1);\n }\n return res;\n}\n\n// --- helpers ----------------------------------------------------------------\ninline u64 pow_u64(u64 x, unsigned e) {\n u64 r = 1;\n while (e--) {\n r *= x;\n }\n return r;\n}\n\n// --- divisor ----------------------------------------------------------------\nvector<u64> divisor(u64 n) {\n auto f = factorize(n);\n\n size_t cap = 1;\n for (auto [_, c] : f) {\n cap *= (c + 1);\n }\n\n vector<u64> res;\n res.reserve(cap);\n res.push_back(1);\n\n for (auto [p, c] : f) {\n size_t len = res.size();\n u64 mul = 1;\n for (uint32_t i = 1; i <= c; ++i) {\n mul *= p;\n for (size_t j = 0; j < len; ++j) {\n res.push_back(res[j] * mul);\n }\n }\n sort(res.begin(), res.end());\n }\n return res;\n}\n\n// --- solve ------------------------------------------------------------------\nvoid solve(u64 n) {\n auto d = divisor(n);\n u64 ans = n + 2;\n\n size_t x = 0;\n while (x < d.size() && d[x] * d[x] <= n) {\n ++x;\n }\n\n for (u64 a : d) {\n if (pow_u64(a, 3) > n) {\n break;\n }\n u64 m = n / a;\n\n while (x && pow_u64(d[x - 1], 2) > m) {\n --x;\n }\n\n size_t y = x;\n while (m % d[y - 1] != 0) {\n --y;\n }\n\n ans = min(ans, a + d[y - 1] + m / d[y - 1]);\n }\n cout << ans << '\\n';\n}\n\n// --- main -------------------------------------------------------------------\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n u64 n;\n while (cin >> n) {\n if (n) {\n solve(n);\n }\n }\n}", "accuracy": 1, "time_ms": 2400, "memory_kb": 3684, "score_of_the_acc": -0.318, "final_rank": 4 }, { "submission_id": "aoj_1642_10582825", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n// --- Miller–Rabin for 64bit ---\nll mod_mul(ll a, ll b, ll m) {\n __int128 t = (__int128)a * b;\n return (ll)(t % m);\n}\nll mod_pow(ll a, ll e, ll m) {\n ll r = 1;\n while (e) {\n if (e & 1) r = mod_mul(r, a, m);\n a = mod_mul(a, a, m);\n e >>= 1;\n }\n return r;\n}\nbool isPrime(ll n) {\n if (n < 2) return false;\n for (ll p : {2,3,5,7,11,13,17,19,23,29,31,37}) {\n if (n%p==0) return n==p;\n }\n ll d = n-1, s = 0;\n while ((d&1)==0) { d>>=1; ++s; }\n for (ll a : {2,325,9375,28178,450775,9780504,1795265022}) {\n if (a % n == 0) continue;\n ll x = mod_pow(a, d, n);\n if (x==1 || x==n-1) continue;\n bool comp = true;\n for (int r=1; r<s; ++r) {\n x = mod_mul(x, x, n);\n if (x == n-1) { comp = false; break; }\n }\n if (comp) return false;\n }\n return true;\n}\n\n// --- Pollard's Rho ---\nmt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());\nll pollards_rho(ll n) {\n if (n%2==0) return 2;\n if (n%3==0) return 3;\n ll c = uniform_int_distribution<ll>(1, n-1)(rng);\n ll x = uniform_int_distribution<ll>(0, n-1)(rng);\n ll y = x;\n ll d = 1;\n auto f = [&](ll v){ return (mod_mul(v,v,n) + c) % n; };\n while (d==1) {\n x = f(x);\n y = f(f(y));\n d = gcd<ll>(llabs(x-y), n);\n if (d==n) return pollards_rho(n);\n }\n return d;\n}\n\nvoid factor(ll n, vector<ll> &out) {\n if (n==1) return;\n if (isPrime(n)) {\n out.push_back(n);\n } else {\n ll d = pollards_rho(n);\n factor(d, out);\n factor(n/d, out);\n }\n}\n\n// 再帰的に約数を生成\nvoid gen_divs(int i, ll cur,\n const vector<pair<ll,int>> &fac,\n vector<ll> &divs) {\n if (i == (int)fac.size()) {\n divs.push_back(cur);\n return;\n }\n ll p = fac[i].first;\n int e = fac[i].second;\n for (int k = 0; k <= e; ++k) {\n gen_divs(i+1, cur, fac, divs);\n cur *= p;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n ll p;\n if (!(cin >> p)) break;\n if (p == 0) break;\n\n // 素因数分解\n vector<ll> tmp;\n factor(p, tmp);\n sort(tmp.begin(), tmp.end());\n vector<pair<ll,int>> fac;\n for (ll x : tmp) {\n if (fac.empty() || fac.back().first != x)\n fac.emplace_back(x, 1);\n else\n fac.back().second++;\n }\n\n // 全約数列挙\n vector<ll> divs;\n gen_divs(0, 1, fac, divs);\n sort(divs.begin(), divs.end());\n\n ll ans = LLONG_MAX;\n int D = divs.size();\n\n // w <= d <= h となるよう二重ループ\n for (int i = 0; i < D; ++i) {\n ll w = divs[i];\n ll q = p / w;\n for (int j = i; j < D; ++j) {\n ll d = divs[j];\n if (d * d > q) break; // d > sqrt(q) は h<d になるので意味なし\n if (q % d) continue;\n ll h = q / d;\n if (h < d) continue; // w<=d<=h を維持\n ans = min(ans, w + d + h);\n }\n }\n\n cout << ans << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3540, "memory_kb": 3648, "score_of_the_acc": -0.4735, "final_rank": 6 }, { "submission_id": "aoj_1642_10582096", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing lint = long long;\n\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\n__int128_t mod_pow(__int128_t a, long long n, long long m) {\n __int128_t res = 1;\n a %= m;\n while (n) {\n if (n & 1) res = (res * a) % m;\n a = (a * a) % m;\n n >>= 1;\n }\n return res;\n}\n\nconstexpr long long MR[] = {2, 7, 61};\nconstexpr long long MRl[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n\nbool Miller_Rabin(long long n) {\n long long s = 0;\n long long d = n - 1;\n while ((d & 1) == 0) {\n s++;\n d >>= 1;\n }\n for (int i = 0; i < 3; i++) {\n if (n <= MR[i]) return true;\n __int128_t x = mod_pow(MR[i], d, n);\n if (x != 1) {\n bool ok = false;\n for (int t = 0; t < s; t++) {\n if (x == n - 1) {\n ok = true;\n break;\n }\n x = x * x % n;\n }\n if (!ok) return false;\n }\n }\n return true;\n}\n\nbool Miller_Rabinl(long long n) {\n long long s = 0;\n long long d = n - 1;\n while ((d & 1) == 0) {\n s++;\n d >>= 1;\n }\n for (int i = 0; i < 7; i++) {\n if (n <= MRl[i]) return true;\n __int128_t x = mod_pow(MRl[i], d, n);\n if (x != 1) {\n bool ok = false;\n for (int t = 0; t < s; t++) {\n if (x == n - 1) {\n ok = true;\n break;\n }\n x = x * x % n;\n }\n if (!ok) return false;\n }\n }\n return true;\n}\n\nbool brute_force(long long n) {\n for (int i = 2; i * i <= n; i++) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\nbool is_prime(long long n) {\n if (n == 2) return true;\n if ((n & 1) == 0 or n < 2) return false;\n if (n < 1000) return brute_force(n);\n if (n < 4759123141LL) {\n return Miller_Rabin(n);\n }\n return Miller_Rabinl(n);\n}\n\nlong long find_prime_factor(long long n) {\n if ((n & 1) == 0) return 2;\n long long m = int64_t(powl(n, 0.125)) + 1;\n for (int i = 1; i < n; i++) {\n long long y = 0;\n long long g = 1;\n long long q = 1;\n long long r = 1;\n long long k = 0;\n long long ys = 0;\n long long x = 0;\n while (g == 1) {\n x = y;\n while (k < 3ll * r / 4) {\n y = (__int128_t(y) * y + i) % n;\n k++;\n }\n while (k < r and g == 1) {\n ys = y;\n for (int j = 0; j < min(m, r - k); j++) {\n y = (__int128_t(y) * y + i) % n;\n q = (__int128_t(q) * abs(x - y)) % n;\n }\n g = gcd(q, n);\n k += m;\n }\n k = r;\n r <<= 1;\n }\n if (g == n) {\n g = 1;\n y = ys;\n while (g == 1) {\n y = (__int128_t(y) * y + i) % n;\n g = gcd(abs(x - y), n);\n }\n }\n if (g == n) continue;\n if (is_prime(g)) return g;\n if (is_prime(n / g)) return n / g;\n return find_prime_factor(g);\n }\n return -1;\n}\n\nvector<long long> factorize(long long n, bool set = false) {\n vector<long long> res;\n while (!is_prime(n) and n > 1) {\n long long p = find_prime_factor(n);\n if (set) res.emplace_back(p);\n while (n % p == 0) {\n n /= p;\n if (!set) res.emplace_back(p);\n }\n }\n if (n > 1) {\n res.emplace_back(n);\n }\n sort(res.begin(), res.end());\n return res;\n}\n\nvector<long long> enumerate_divisors(long long n, bool sorted_result = false) {\n vector<long long> res = {1};\n long long before = -1;\n long long mul;\n int siz_before = 1;\n for (const long long p : factorize(n)) {\n mul = (p == before ? mul * p : p);\n int siz = (p == before ? siz_before : int(res.size()));\n for (int i = 0; i < siz; i++) {\n res.emplace_back(res[i] * mul);\n }\n before = p;\n siz_before = siz;\n }\n if (sorted_result) sort(res.begin(), res.end());\n return res;\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n long long p;\n while (1) {\n long long res = LLONG_MAX;\n in(p);\n if (p == 0) break;\n for (const long long w : enumerate_divisors(p)) {\n long long dh = p / w;\n for (const long long d : enumerate_divisors(dh)) {\n long long h = dh / d;\n chmin(res, w + d + h);\n }\n }\n\n out(res);\n }\n}", "accuracy": 1, "time_ms": 6240, "memory_kb": 4204, "score_of_the_acc": -0.8449, "final_rank": 16 } ]
aoj_1644_cpp
Keima You are given a set of points aligned in a grid of N rows and M columns. Each of the points is named P i , j with its row number i (1 ≤ i ≤ N ) and its column number j (1 ≤ j ≤ M ). Each point is colored either white or black. Your task is to compute the number of subsets T of white points satisfying the following conditions: ∀ i ∈ {3, 4,..., N }, ∀ j ∈ {2, 3,..., M }, P i , j ∈ T ⇒ P i -2, j -1 ∉ T , ∀ i ∈ {3, 4,..., N }, ∀ j ∈ {1, 2,..., M −1}, P i , j ∈ T ⇒ P i -2, j +1 ∉ T . For example, in the figure below, if you include the point marked with a star to the set T , you can include neither of the two crossed points. If you are familiar with the game of shogi (Japanese chess), you may find similarities between the possible moves of a keima with the relative positions of the star point and crossed points. Because the answer can be huge, output the remainder divided by 10 9 +7. Input The input consists of multiple datasets, each in the following format. N M a 1,1 ... a 1, M ... a N, 1 ... a N,M The first line of each dataset contains two integers N (2 ≤ N ≤ 60) and M (2 ≤ M ≤ 60) representing the numbers of rows and columns of the grid, respectively. Each of the following N lines contains M characters. The j -th character of the i -th line, a i,j , represents the color of the point P i, j , where '.' denotes white and '#' denotes black. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each dataset, output one line containing the number of subsets satisfying the conditions modulo 10 9 +7. Sample Input 3 2 .# ## #. 3 3 .#. #.# ..# 6 5 .#..# ...#. #.... ..#.# #.... ..#.. 0 0 Output for the Sample Input 3 20 103320
[ { "submission_id": "aoj_1644_10666042", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<int MOD>\nstruct modint {\n modint() : x(0) {}\n modint(long long v) {\n long long y = v % m();\n if (y < 0) y += m();\n x = (unsigned int)(y);\n }\n static modint raw(int v) {\n modint a;\n a.x = v;\n return a;\n }\n static constexpr int mod() { return m(); }\n unsigned int val() const { return x; }\n modint& operator++() {\n x++;\n if (x == m()) x = 0;\n return *this;\n }\n modint& operator--() {\n if (x == 0) x = m();\n x--;\n return *this;\n }\n modint operator++(int) {\n modint res = *this;\n ++*this;\n return res;\n }\n modint operator--(int) {\n modint res = *this;\n --*this;\n return res;\n }\n modint& operator+=(const modint &r) {\n x += r.x;\n if (x >= m()) x -= m();\n return *this;\n }\n modint& operator-=(const modint &r) {\n x -= r.x;\n if (x >= m()) x += m();\n return *this;\n }\n modint& operator*=(const modint &r) {\n unsigned long long y = x;\n y *= r.x;\n x = (unsigned int)(y % m());\n return *this;\n }\n modint &operator/=(const modint &r) {\n return *this = *this * r.inv();\n }\n friend modint operator+(const modint &a, const modint &b) {\n return modint(a) += b;\n }\n friend modint operator-(const modint &a, const modint &b) {\n return modint(a) -= b;\n }\n friend modint operator*(const modint &a, const modint &b) {\n return modint(a) *= b;\n }\n friend modint operator/(const modint &a, const modint &b) {\n return modint(a) /= b;\n }\n friend bool operator==(const modint &a, const modint &b) {\n return a.x == b.x;\n }\n friend bool operator!=(const modint &a, const modint &b) {\n return a.x != b.x;\n }\n modint operator+() const { return *this; }\n modint operator-() const { return modint() - *this; }\n modint pow(long long k) const {\n assert(k >= 0);\n modint a = *this;\n modint res = 1;\n while (k > 0) {\n if (k & 1) res *= a;\n a *= a;\n k >>= 1;\n }\n return res;\n }\n modint inv() const {\n long long a = x, b = m(), u = 1, v = 0;\n while (b > 0) {\n long long t = a / b;\n a -= t * b;\n swap(a, b);\n u -= t * v;\n swap(u, v);\n }\n return modint(u);\n }\nprivate:\n unsigned int x;\n static constexpr unsigned int m() { return MOD; }\n};\nusing mint = modint<int(1e9 + 7)>;\n\nmint f(vector<string> S) {\n // cout << \"case :\\n\";\n // for (auto s : S) cout << s << endl;\n int H = S.size(), W = S[0].size();\n int N = (H + 1) / 2, M = H / 2;\n vector<vector<int>> nxt1(1 << N), nxt2(1 << M);\n for (int i = 0; i < 1 << N; i++) {\n auto rec = [&] (auto rec, int j, int k) -> void {\n if (j == M) {\n nxt1[i].push_back(k);\n return;\n }\n rec(rec, j + 1, k);\n if (!(i >> j & 1) && !(i >> (j + 1) & 1)) {\n rec(rec, j + 1, k ^ (1 << j));\n }\n };\n rec(rec, 0, 0);\n }\n for (int j = 0; j < 1 << M; j++) {\n auto rec = [&] (auto rec, int i, int k) -> void {\n if (i == N) {\n nxt2[j].push_back(k);\n return;\n }\n rec(rec, i + 1, k);\n if (!(j >> i & 1) && !(j >> (i ? i - 1 : i) & 1)) {\n rec(rec, i + 1, k ^ (1 << i));\n }\n };\n rec(rec, 0, 0);\n }\n // cout << \"nxt1 :\\n\";\n // for (int i = 0; i < 1 << N; i++) {\n // cout << i << \" :\";\n // for (int j : nxt1[i]) cout << ' ' << j;\n // cout << endl;\n // }\n // cout << \"nxt2 :\\n\";\n // for (int i = 0; i < 1 << M; i++) {\n // cout << i << \" :\";\n // for (int j : nxt2[i]) cout << ' ' << j;\n // cout << endl;\n // }\n vector<mint> dp1(1 << N), dp2(1 << M);\n dp1[0] = dp2[0] = 1;\n for (int j = 0; j < W; j++) {\n vector<mint> ep1(1 << N), ep2(1 << M);\n int m1 = 0, m2 = 0;\n for (int i = 0; i < H; i++) {\n if (S[i][j] == '.') {\n (i % 2 == 0 ? m1 : m2) ^= 1 << (i / 2);\n }\n }\n for (int j = 0; j < 1 << N; j++) {\n for (int k : nxt1[j]) {\n if ((k & m2) == k) ep2[k] += dp1[j];\n }\n }\n for (int j = 0; j < 1 << M; j++) {\n for (int k : nxt2[j]) {\n if ((k & m1) == k) ep1[k] += dp2[j];\n }\n }\n dp1 = ep1, dp2 = ep2;\n // cout << \"dp1 :\";\n // for (auto x : dp1) cout << ' ' << x.val();\n // cout << endl;\n // cout << \"dp2 :\";\n // for (auto x : dp2) cout << ' ' << x.val();\n // cout << endl;\n }\n auto sum = [] (vector<mint> dp) -> mint {\n mint res = 0;\n for (auto x : dp) res += x;\n return res;\n };\n return sum(dp1) * sum(dp2);\n}\n\nbool solve() {\n int H, W;\n cin >> H >> W;\n if (H == 0 && W == 0) return false;\n vector<string> S, T;\n while (H--) {\n string s;\n cin >> s;\n S.push_back(s);\n swap(S, T);\n }\n cout << (f(S) * f(T)).val() << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 4080, "memory_kb": 23600, "score_of_the_acc": -0.9497, "final_rank": 18 }, { "submission_id": "aoj_1644_10611410", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &a){\n\tif (a.empty()) return os;\n\tos << a.front();\n\tfor (auto e : a | views::drop(1)){\n\t\tos << ' ' << e;\n\t}\n\treturn os;\n}\nvoid dump(auto... vs) {\n\t((cout << vs << ' '), ...) << endl;\n}\n\n\nvoid solve(ll N,ll M) {\n\tconst ll Mod=1000000007;\n\tvector<string> S(N);\n\trep(i,N)cin>>S[N-i-1];\n\tauto zeta=[&](vector<ll> &A,ll n){\n\t\trep(i,n){\n\t\t\trep(bi,1<<n){\n\t\t\t\tif (bi>>i&1)continue;\n\t\t\t\tll nbi=bi^(1LL<<i);\n\t\t\t\tA[bi]+=A[nbi];\n\t\t\t}\n\t\t}\n\t\trep(i,1<<n){\n\t\t\tA[i]%=Mod;\n\t\t}\n\t\treturn;\n\t};\n\tll ans=1;\n\trep(st,4){\n\t\tvector<ll> dp(1,1);\n\t\trep(j,M){\n\t\t\tll b=0;\n\t\t\tll len=0;\n\t\t\tll low=st+2*j;\n\t\t\tlow%=4;\n\t\t\trep(i,N){\n\t\t\t\tif (low+4*i>=N)break;\n\t\t\t\tif (S[low+4*i][j]=='.')b|=1<<i;\n\t\t\t\tlen++;\n\t\t\t}\n\t\t\tvector<ll> ndp(1<<len,0);\n\t\t\t// dump(ndp);\n\t\t\t// dump(b);\n\t\t\trep(bi,(ll)dp.size()){\n\t\t\t\t// dump(bi);\n\t\t\t\t// dump(ndp);\n\t\t\t\tif (low<2){\n\t\t\t\t\tndp[(~bi)&(~(bi<<1))&b]+=dp[bi];\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tndp[(~bi)&(~(bi>>1))&b]+=dp[bi];\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(i,1<<len)ndp[i]%=Mod;\n\t\t\tzeta(ndp,len);\n\t\t\trep(i,1<<len){\n\t\t\t\tif ((i&b)!=i){\n\t\t\t\t\tndp[i]=0;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// if (st==2){\n\t\t\t// \tdump(ndp);\n\t\t\t// }\n\t\t\tswap(dp,ndp);\n\t\t}\n\t\tll sum=0;\n\t\tfor (ll v:dp){\n\t\t\tsum+=v;\n\t\t\tsum%=Mod;\n\t\t}\n\t\tans*=sum;\n\t\tans%=Mod;\n\t}\n\tans%=Mod;\n\tcout<<ans<<endl;\n return;\n}\n\n\nint main() {\n while (true){\n\t\tll N,M;\n\t\tcin>>N>>M;\n\t\tif (N==0)break;\n solve(N,M);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 3932, "score_of_the_acc": -0.0092, "final_rank": 1 }, { "submission_id": "aoj_1644_10604071", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG // 配列外参照のエラー\n// #endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 多倍長整数\n// #if __has_include(<boost/multiprecision/cpp_int.hpp>)\n// #include <boost/multiprecision/cpp_int.hpp>\n// using lll =boost::multiprecision::int128_t;\n// #endif\n\n// atcoder関連\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\nusing mint = modint998244353;\nusing MINT = modint1000000007;\n#endif\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint inf = 2e9;\nll INF = 9e18;\n#define endl '\\n';\n\n// 新規マクロ\n#define all(a) (a).begin(), (a).end()\n#define rep(i, N, limit) for (int i = (int)N; i < (int)limit; i++)\n\n///////////////////// vector関連\ntemplate <class T, size_t n, size_t idx = 0>\nauto make_vec(const size_t (&d)[n], const T &init) noexcept {\n if constexpr (idx < n)\n return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else\n return init;\n}\n\ntemplate <class T, size_t n> auto make_vec(const size_t (&d)[n]) noexcept {\n return make_vec(d, T{});\n}\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\n////////////////////////////\ninline string input();\ninline ll I();\ninline pll II();\n\n/////////////////////出力関連\ninline void Yes(bool f);\n\nll solve3(vector<ll> &B, int B_size, int o) {\n int W = B.size();\n vector<vector<ll>> dp(W + 1, vector<ll>(1 << B_size, 0));\n\n auto add = [&](int i, int j, ll val) {\n dp[i][j] += val;\n dp[i][j] %= MOD;\n };\n\n dp[0][0] = 1;\n int j_max = (1 << B_size) - 1;\n for (int i = 0; i < W; i++) {\n for (int j = 0; j <= j_max; j++) {\n ll val = dp[i][j];\n int k = (j << 1);\n if ((o + i) % 2==0) {\n k= ( j>>1) ;\n }\n\n int ok = j_max & ( j_max ^ (j | k | B[i]));\n int l = ok;\n while (l > 0) {\n add(i + 1, l, val);\n l = (l - 1) & ok;\n }\n add(i + 1, 0, val);\n }\n }\n ll ans = 0;\n for (int i = 0; i <= j_max; i++) {\n ans += dp[W][i];\n ans%=MOD;\n }\n return ans;\n}\n\nll solve2(vector<string> &F) {\n int h = F.size(); // 定義されているHとは異なる(Hの半分)\n int W = F[0].size();\n\n vector<ll> B1, B2;\n int B1_size = (h + 1) / 2, B2_size = (h + 1) / 2;\n for (int j = 0; j < W; j++) {\n ll b1 = 0LL, b2 = 0LL;\n for (int i = 0; i < h; i++) {\n ll k = i / 2;\n ll f = ll(F[i][j] == '#');\n if ((i + j) % 2 == 0) {\n b1 += (1LL << k) * f;\n } else {\n b2 += (1LL << k) * f;\n }\n\n if (i == h - 1) {\n if (j % 2 && h % 2) {\n b1+=(1LL<<k);\n }\n if (j % 2 == 0 && h % 2) {\n b2+=(1LL<<k);\n }\n }\n }\n int p1 = 1, p2 = 1;\n // if (h == 1) {\n // if (j % 2) {\n // p1=0;\n // } else {\n // p2=0;\n // }\n // }\n if (p1)B1.push_back(b1);\n if (p2)B2.push_back(b2);\n }\n\n return solve3(B1, B1_size, 1) * solve3(B2, B2_size, 0) % MOD;\n}\n\nbool solve() {\n int H, W;\n cin >> H >> W;\n if (H == 0) {\n return false;\n }\n vector<string> F1, F2;\n for (int i = 0; i < H; i++) {\n string S;\n cin >> S;\n if (i % 2 == 0) {\n F1.push_back(S);\n } else {\n F2.push_back(S);\n }\n }\n\n print(solve2(F1) * solve2(F2) % MOD);\n\n return true;\n}\n\n///////////////////ここから//////////////////////\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve())\n ;\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n/////////////////////初期化///////////////////////\n\ninline string input() {\n string S;\n cin >> S;\n return S;\n}\ninline ll I() {\n ll ret;\n cin >> ret;\n return ret;\n}\ninline pll II() {\n pll ret;\n cin >> ret.first >> ret.second;\n return ret;\n}\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 2330, "memory_kb": 19456, "score_of_the_acc": -0.5944, "final_rank": 12 }, { "submission_id": "aoj_1644_10592617", "code_snippet": "// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#include <atcoder/all> \nusing namespace atcoder;\n\n// 数値型\nusing mint = modint1000000007;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing Pll = pair<ll, ll>;\nusing Pli = pair<ll, int>;\nusing Pil = pair<int, ll>;\n\n// vector関連\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\ntemplate<typename T>\nusing vc = vector<T>;\ntemplate<typename T>\nusing vvc = vector<vc<T>>;\ntemplate<typename T>\nusing vvvc = vector<vvc<T>>;\ntemplate<typename T>\nusing vvvvc = vector<vvvc<T>>;\n\n// priority_queue\ntemplate<typename T>\nusing pq = priority_queue<T>;\ntemplate<typename T>\nusing pqg = priority_queue<T, vc<T>, greater<T>>;\n\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(int i = a; i < (int)(b); i++)\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define MIN(vec) *min_element(all(vec))\n#define MAX(vec) *max_element(all(vec))\n#define next_perm(vec) (vec).begin(), (vec).end()\n#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())\n#define el \"\\n\"\n#define Yes cout << \"Yes\" << el\n#define No cout << \"No\" << el\n#define YES cout << \"YES\" << el\n#define NO cout << \"NO\" << el\n#define EPS 1e-8\n#define Equal(a, b) (fabs((a)-(b)) < EPS) \n#ifdef ONLINE_JUDGE\n#define dbg(x) (void)0\n#else\n#define dbg(x) cerr << #x << \"=\" << x << el \n#endif\n// 定数\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\nconstexpr int DX8[] = {1, 0, -1, 0, 1, 1, -1, -1};\nconstexpr int DY8[] = {0, 1, 0, -1, 1, -1, 1, -1};\n\ntemplate<typename T1, typename T2>\nostream &operator<< (ostream &os, pair<T1, T2> p) {\n os << \"{\" << p.first << \",\" << p.second << \"}\";\n return os;\n}\ntemplate<typename T>\nostream &operator<< (ostream &os, vc<T> &vec) {\n int sz = vec.size();\n rep(i, sz){\n os << vec[i] << (i==sz-1?\"\":\" \");\n }\n return os;\n}\n\nostream &operator<< (ostream &os, mint &a) {\n os << a.val();\n return os;\n}\n\nistream &operator>> (istream &is, mint &a) {\n ll x;\n is >> x;\n a = x;\n return is;\n}\ntemplate<typename T1, typename T2>\nistream &operator>> (istream &is, pair<T1, T2> &p) {\n is >> p.first >> p.second;\n return is;\n}\ntemplate<typename T>\nistream &operator>> (istream &is, vc<T> &vec) {\n int sz = vec.size();\n rep(i, sz) { is >> vec[i]; }\n return is;\n}\n/// @brief aとbの最大値をaに格納。更新があったかbool値を返す\n/// @tparam T1 \n/// @tparam T2 \n/// @param a \n/// @param b \n/// @return bool\ntemplate<typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b){\n bool ret = a<b;\n if(ret) a = b;\n return ret;\n}\n\n/// @brief aとbの最小値をaに格納。更新があったかbool値を返す\n/// @tparam T1 \n/// @tparam T2 \n/// @param a \n/// @param b \n/// @return bool\ntemplate<typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b){\n bool ret = a>b;\n if(ret) {a = b;}\n return ret;\n}\n\ninline void YesNo(bool flag){\n if(flag) {Yes;}\n else {No;}\n return;\n}\n\ninline void YESNO(bool flag){\n if(flag) {YES;}\n else {NO;}\n return;\n}\n\ninline bool outof(ll x, ll xlim){\n return (x<0 || x>=xlim);\n}\n\ntemplate<typename T>\ninline T sqnorm(T x, T y){\n return x*x+y*y;\n}\n\n/// @brief char->int\n/// @param c \n/// @return int\ninline int ctoi(char c){\n return c-'0';\n}\n\n/// @brief xを素因数分解\n/// @param x \n/// @return vector<Pli>, 素因数の昇順に {p, cnt}\nvector<Pli> prime_fact(ll x){\n vector<Pli> ret;\n for(ll i=2; i*i<=x; i++){\n if(x%i == 0){\n ret.emplace_back(i, 0);\n while(x%i == 0){\n ret.back().second++;\n x /= i;\n }\n }\n }\n if(x != 1) ret.emplace_back(x, 1);\n return ret;\n}\n\n/// @brief xの約数列挙\n/// @param x \n/// @return vll, 約数の昇順\nvll divisor_enum(ll x){\n vector<ll> ret;\n for(ll i=1; i*i<=x; i++){\n if(x%i == 0){\n ret.push_back(x/i);\n ret.push_back(i);\n }\n }\n sort(all(ret));\n UNIQUE(ret);\n return ret;\n}\n\n/// @brief 繰り返し二乗法。\n/// @tparam T \n/// @param x \n/// @param k \n/// @param op \n/// @param e \n/// @return \ntemplate<typename T>\nT pow_t(T x, ll k, T (*op)(T, T), T (*e)()){\n T ret = e();\n while(k){\n if(k&1) ret *= x;\n x *= x;\n k >>= 1;\n }\n return ret;\n}\n\nll powll(ll x, ll k){\n return pow_t<ll>(x, k, [](ll a, ll b) -> ll{return a*b;}, []() -> ll{return 1;});\n}\n\ninline int pop_cnt(ll x) { return __builtin_popcountll(x); }\ninline int top_bit(ll x) { return (x==0?-1:63-__builtin_clzll(x));}\n\nvoid main2();\n\nint main(){\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n main2();\n}\n\nmint f(vc<string> s, int n, int m){\n vc<mint> dp1((1<<(n/2+1)), 0), dp2((1<<(n/2+1)), 0);\n dp1[0] = 1;\n dp2[0] = 1;\n rep(j, m){\n rep(i, n){\n vc<mint> ndp((1<<(n/2+1)), 0);\n if(s[i][j] == '#'){\n rep(S, 1<<(n/2+1)){\n if((i+j)%2 == 0){\n ndp[(S>>1)] += dp1[S];\n }\n else{\n ndp[(S>>1)] += dp2[S];\n }\n }\n }\n else rep(S, 1<<(n/2+1)){\n if((i+j)%2 == 0){\n if(j%2 == 0){\n ndp[S>>1] += dp1[S];\n // if(i != 0 && j+1 != m && s[i-1][j+1] == '#') continue;\n if(i != 0 && (S&1) > 0) continue;\n if((i != n-1 && (S&2) > 0)) continue;\n ndp[(S>>1)|(1<<(n/2))] += dp1[S];\n }\n else{\n ndp[S>>1] += dp1[S];\n // if(i != 0 && j+1 != m && s[i-1][j+1] == '#') continue;\n if(i != 0 && (S>>(1-n%2)&1) > 0) continue;\n if(i != n-1 && (S>>(2-n%2)&1) > 0) continue;\n ndp[(S>>1)|(1<<(n/2))] += dp1[S];\n }\n }\n else{\n if(j%2 == 0){\n ndp[S>>1] += dp2[S];\n // if(i != 0 && j+1 != m && s[i-1][j+1] == '#') continue;\n if(i != 0 && (S>>(1-n%2)&1) > 0) continue;\n if(i != n-1 && (S>>(2-n%2)&1) > 0) continue;\n ndp[(S>>1)|(1<<(n/2))] += dp2[S];\n }\n else{\n ndp[S>>1] += dp2[S];\n // if(i != 0 && j+1 != m && s[i-1][j+1] == '#') continue;\n if(i != 0 && (S&1) > 0) continue;\n if((i != n-1 && (S&2) > 0)) continue;\n ndp[(S>>1)|(1<<(n/2))] += dp2[S];\n }\n }\n }\n if((i+j)%2 == 0) swap(ndp, dp1);\n else swap(ndp, dp2);\n }\n }\n mint ans1 = 0, ans2 = 0;\n rep(i, 1<<(n/2+1)) ans1 += dp1[i], ans2 += dp2[i];\n return ans1*ans2;\n}\nbool solve(){\n int n,m;\n cin >> n >> m;\n if(n == 0) return false;\n vc<string> s(n);\n cin >> s;\n\n // split\n vc<string> s_odd(n/2), s_even((n+1)/2);\n rep(i, n){\n if(i%2 == 0) s_even[i/2] = s[i];\n else s_odd[i/2] = s[i];\n }\n mint ans = f(s_odd, n/2, m)*f(s_even, (n+1)/2, m);\n // cout << f(s_odd, n/2, m).val() << el;\n cout << ans << el;\n return true;\n}\nvoid main2(){\n while(solve()){\n ;\n }\n}", "accuracy": 1, "time_ms": 2700, "memory_kb": 4476, "score_of_the_acc": -0.3645, "final_rank": 8 }, { "submission_id": "aoj_1644_10509082", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nbool solve(){\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) return false;\n vector<string> a(n);\n for(int i = 0; i < n; i++) cin >> a[i];\n constexpr ll MOD = 1e9+7;\n ll ans = 1;\n for(int i0 = 0; i0 < 4; i0++) {\n vector<int> is;\n for(int i = i0; i < n; i += 4) {\n is.push_back(i);\n }\n vector<ll> dp(1 << ssize(is));\n int valid = 0;\n for(int ii = 0; ii < ssize(is); ii++) {\n if(a[is[ii]][0] == '.') valid |= 1 << ii;\n }\n for(int sii = valid;; sii = (sii-1) & valid) {\n dp[sii] = 1;\n if(sii == 0) break;\n }\n for(int j = 1; j < m; j++) {\n vector<int> is;\n for(int i = (i0+j*2) % 4; i < n; i += 4) {\n is.push_back(i);\n }\n vector<ll> ndp(1 << ssize(is));\n for(int sii = 0; sii < ssize(dp); sii++) {\n int valid = 0;\n for(int nii = 0; nii < ssize(is); nii++) {\n if(a[is[nii]][j] == '.') valid |= 1 << nii;\n }\n valid &= ~sii;\n if((i0/2 + j) % 2 == 1) {\n valid &= ~(sii >> 1);\n } else {\n valid &= ~(sii << 1);\n }\n for(int nsii = valid;; nsii = (nsii-1) & valid) {\n if((ndp[nsii] += dp[sii]) >= MOD) ndp[nsii] -= MOD;\n if(nsii == 0) break;\n }\n }\n dp.swap(ndp);\n }\n ll tmp = 0;\n for(int sii = 0; sii < ssize(dp); sii++) {\n if((tmp += dp[sii]) >= MOD) tmp -= MOD;\n }\n ans *= tmp;\n ans %= MOD;\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 2160, "memory_kb": 4188, "score_of_the_acc": -0.2739, "final_rank": 4 }, { "submission_id": "aoj_1644_10390429", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep2(i, m, n) for (int i = (m); i < (n); ++i)\n#define rep(i, n) rep2(i, 0, n)\n#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)\n#define drep(i, n) drep2(i, n, 0)\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)\n#define REV(i, a, b) for (int i = (a); i >= (b); --i)\n#define CLR(a, b) memset((a), (b), sizeof(a))\n#define DUMP(x) cout << #x << \" = \" << (x) << endl;\n#define INF 1001001001001001001ll\n#define inf (int)1001001000\n#define MOD 998244353\n#define MOD1 1000000007\n#define PI 3.14159265358979\n#define Dval 1e-12\n#define fcout cout << fixed << setprecision(12)\n#define MP make_pair\n#define PB push_back\n#define fi first\n#define se second\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n\nusing ll = long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vl = vector<long long>;\nusing vs = vector<string>;\nusing vd = vector<double>;\nusing vld = vector<long double>;\nusing vc = vector<char>;\nusing vb = vector<bool>;\nusing vpii = vector<pair<int, int>>;\nusing vpil = vector<pair<int, long long>>;\nusing vpll = vector<pair<long long, long long>>;\nusing vvi = vector<vector<int>>;\nusing vvl = vector<vector<long long>>;\nusing vvd = vector<vector<double>>;\nusing vvld = vector<vector<long double>>;\nusing vvc = vector<vector<char>>;\nusing vvb = vector<vector<bool>>;\nusing vvpii = vector<vector<pair<int,int>>>;\nusing vvpll = vector<vector<pair<long long,long long>>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vvvl = vector<vector<vector<long long>>>;\nusing pii = pair<int, int>;\nusing pll = pair<long long, long long>;\n\nll gcd(ll x, ll y) {\tif (x == 0) return y;\treturn gcd(y%x, x);} \ntemplate<typename T>\nT POW(T x, ll n){T ret=1;\twhile(n>0){\t\tif(n&1) ret=ret*x;\t\tx=x*x;\t\tn>>=1;\t}\treturn ret;}\ntemplate<typename T>\nT modpow(T a, ll n, T p) {\tif(n==0) return (T)1; if (n == 1) return a % p; if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p; T t = modpow(a, n / 2, p); return (t * t) % p;}\ntemplate<typename T>\nT modinv(T a, T m) {\tif(m==0)return (T)1;\tT b = m, u = 1, v = 0;\twhile (b) {\t\tT t = a / b;\t\ta -= t * b; swap(a, b);\t\tu -= t * v; swap(u, v);\t}\tu %= m;\tif (u < 0) u += m;\treturn u;}\ntemplate<typename T>\nT REM(T a, T b){ return (a % b + b) % b;}\ntemplate<typename T>\nT QUO(T a, T b){ return (a - REM(a, b)) / b;}\n/* \nconst int MAXCOMB=510000;\nll MODCOMB = 998244353;\nll fac[MAXCOMB], finv[MAXCOMB], inv[MAXCOMB]; \nvoid COMinit() {\tfac[0] = fac[1] = 1;\tfinv[0] = finv[1] = 1;\tinv[1] = 1;\tfor (int i = 2; i < MAXCOMB; i++) {\t\tfac[i] = fac[i - 1] * i % MODCOMB;\t\tinv[i] = MODCOMB - inv[MODCOMB%i] * (MODCOMB / i) % MODCOMB;\t\tfinv[i] = finv[i - 1] * inv[i] % MODCOMB;\t}}\nll COM(ll n, ll k) {\tif (n < k) return 0;\tif (n < 0 || k < 0) return 0;\treturn fac[n] * (finv[k] * finv[n - k] % MODCOMB) % MODCOMB;}\nll com(ll n,ll m){ if(n<m || n<=0 ||m<0){\t\treturn 0;\t}\tif( m==0 || n==m){\t\treturn 1;\t}\tll k=1;\tfor(ll i=1;i<=m;i++){ k*=(n-i+1); \t k%=MODCOMB;\t k*=modinv(i,MODCOMB);\t k%=MODCOMB;\t}\treturn k;}\n*/\n/*\nconst int MAXCOMB=510000;\nstd::vector<mint> fac(MAXCOMB), finv(MAXCOMB), inv(MAXCOMB);\nvoid COMinit() {fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;for (int i = 2; i < MAXCOMB; i++) {fac[i] = fac[i - 1] * i;inv[i] = mint(0) - inv[mint::mod() % i] * (mint::mod() / i);finv[i] = finv[i - 1] * inv[i];}}\nmint COM(int n, int k) {if (n < k) return 0;if (n < 0 || k < 0) return 0;return fac[n] * finv[k] * finv[n - k];}\n*/\ntemplate <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false));}\ntemplate <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false));}\ntemplate <class T> T BS(vector<T> &vec, T key) { auto itr = lower_bound(vec.begin(), vec.end(), key); return distance(vec.begin(), itr); }\ntemplate<class T> pair<T,T> RangeBS(vector<T> &vec, T lowv, T highv){auto itr_l = lower_bound(vec.begin(), vec.end(), lowv); auto itr_r = upper_bound(vec.begin(), vec.end(), highv); return make_pair(distance(vec.begin(), itr_l), distance(vec.begin(), itr_r)-1);}\nvoid fail() { cout << \"-1\\n\"; exit(0); } void no() { cout << \"No\\n\"; exit(0); } void yes() { cout << \"Yes\\n\"; exit(0); }\ntemplate<class T> void er(T a) { cout << a << '\\n'; exit(0); }\nint dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1};\nbool range_in(int i, int j, int h, int w){ if(i<0 || j<0 || i>=h || j>=w) return false; return true;} \nint bitcount(int n){n=(n&0x55555555)+(n>>1&0x55555555); n=(n&0x33333333)+(n>>2&0x33333333); n=(n&0x0f0f0f0f)+(n>>4&0x0f0f0f0f); n=(n&0x00ff00ff)+(n>>8&0x00ff00ff); n=(n&0x0000ffff)+(n>>16&0x0000ffff); return n;}\n\ntemplate<typename T>\nstruct Edge{\n int from, to, index;\n T cost;\n Edge(int _to) : from(-1), to(_to), index(-1), cost(0) {}\n Edge(int _to, T _cost) : from(-1), to(_to), index(-1), cost(_cost) {}\n Edge(int _from, int _to, int _index) : from(_from), to(_to), index(_index), cost(0) {}\n Edge(int _from, int _to, int _index, T _cost) \n : from(_from), to(_to), index(_index), cost(_cost) {}\n bool operator<(const Edge<T>& other) const {\n return cost < other.cost; \n }\n Edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n};\nusing Graph = vector<vector<int>>; \ntemplate <typename T>\nusing WGraph = vector<vector<Edge<T>>>; \n\n\ntemplate <uint32_t mod>\nstruct LazyMontgomeryModInt {\n using mint = LazyMontgomeryModInt;\n using i32 = int32_t;\n using u32 = uint32_t;\n using u64 = uint64_t;\n\n static constexpr u32 get_r() {\n u32 ret = mod;\n for (i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret;\n return ret;\n }\n\n static constexpr u32 r = get_r();\n static constexpr u32 n2 = -u64(mod) % mod;\n static_assert(mod < (1 << 30), \"invalid, mod >= 2 ^ 30\");\n static_assert((mod & 1) == 1, \"invalid, mod % 2 == 0\");\n static_assert(r * mod == 1, \"this code has bugs.\");\n\n u32 a;\n\n constexpr LazyMontgomeryModInt() : a(0) {}\n constexpr LazyMontgomeryModInt(const int64_t &b)\n : a(reduce(u64(b % mod + mod) * n2)){};\n\n static constexpr u32 reduce(const u64 &b) {\n return (b + u64(u32(b) * u32(-r)) * mod) >> 32;\n }\n\n constexpr mint &operator+=(const mint &b) {\n if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod;\n return *this;\n }\n\n constexpr mint &operator-=(const mint &b) {\n if (i32(a -= b.a) < 0) a += 2 * mod;\n return *this;\n }\n\n constexpr mint &operator*=(const mint &b) {\n a = reduce(u64(a) * b.a);\n return *this;\n }\n\n constexpr mint &operator/=(const mint &b) {\n *this *= b.inverse();\n return *this;\n }\n\n constexpr mint operator+(const mint &b) const { return mint(*this) += b; }\n constexpr mint operator-(const mint &b) const { return mint(*this) -= b; }\n constexpr mint operator*(const mint &b) const { return mint(*this) *= b; }\n constexpr mint operator/(const mint &b) const { return mint(*this) /= b; }\n constexpr bool operator==(const mint &b) const {\n return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);\n }\n constexpr bool operator!=(const mint &b) const {\n return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);\n }\n constexpr mint operator-() const { return mint() - mint(*this); }\n constexpr mint operator+() const { return mint(*this); }\n\n constexpr mint pow(u64 n) const {\n mint ret(1), mul(*this);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n constexpr mint inverse() const {\n int x = get(), y = mod, u = 1, v = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, u -= t * v;\n tmp = x, x = y, y = tmp;\n tmp = u, u = v, v = tmp;\n }\n return mint{u};\n }\n\n friend ostream &operator<<(ostream &os, const mint &b) {\n return os << b.get();\n }\n\n friend istream &operator>>(istream &is, mint &b) {\n int64_t t;\n is >> t;\n b = LazyMontgomeryModInt<mod>(t);\n return (is);\n }\n\n constexpr u32 get() const {\n u32 ret = reduce(a);\n return ret >= mod ? ret - mod : ret;\n }\n\n static constexpr u32 get_mod() { return mod; }\n};\n\ntemplate <typename mint>\nstruct NTT {\n static constexpr uint32_t get_pr() {\n uint32_t _mod = mint::get_mod();\n using u64 = uint64_t;\n u64 ds[32] = {};\n int idx = 0;\n u64 m = _mod - 1;\n for (u64 i = 2; i * i <= m; ++i) {\n if (m % i == 0) {\n ds[idx++] = i;\n while (m % i == 0) m /= i;\n }\n }\n if (m != 1) ds[idx++] = m;\n\n uint32_t _pr = 2;\n while (1) {\n int flg = 1;\n for (int i = 0; i < idx; ++i) {\n u64 a = _pr, b = (_mod - 1) / ds[i], r = 1;\n while (b) {\n if (b & 1) r = r * a % _mod;\n a = a * a % _mod;\n b >>= 1;\n }\n if (r == 1) {\n flg = 0;\n break;\n }\n }\n if (flg == 1) break;\n ++_pr;\n }\n return _pr;\n };\n\n static constexpr uint32_t mod = mint::get_mod();\n static constexpr uint32_t pr = get_pr();\n static constexpr int level = __builtin_ctzll(mod - 1);\n mint dw[level], dy[level];\n\n void setwy(int k) {\n mint w[level], y[level];\n w[k - 1] = mint(pr).pow((mod - 1) / (1 << k));\n y[k - 1] = w[k - 1].inverse();\n for (int i = k - 2; i > 0; --i)\n w[i] = w[i + 1] * w[i + 1], y[i] = y[i + 1] * y[i + 1];\n dw[1] = w[1], dy[1] = y[1], dw[2] = w[2], dy[2] = y[2];\n for (int i = 3; i < k; ++i) {\n dw[i] = dw[i - 1] * y[i - 2] * w[i];\n dy[i] = dy[i - 1] * w[i - 2] * y[i];\n }\n }\n\n NTT() { setwy(level); }\n\n void fft4(vector<mint> &a, int k) {\n if ((int)a.size() <= 1) return;\n if (k == 1) {\n mint a1 = a[1];\n a[1] = a[0] - a[1];\n a[0] = a[0] + a1;\n return;\n }\n if (k & 1) {\n int v = 1 << (k - 1);\n for (int j = 0; j < v; ++j) {\n mint ajv = a[j + v];\n a[j + v] = a[j] - ajv;\n a[j] += ajv;\n }\n }\n int u = 1 << (2 + (k & 1));\n int v = 1 << (k - 2 - (k & 1));\n mint one = mint(1);\n mint imag = dw[1];\n while (v) {\n // jh = 0\n {\n int j0 = 0;\n int j1 = v;\n int j2 = j1 + v;\n int j3 = j2 + v;\n for (; j0 < v; ++j0, ++j1, ++j2, ++j3) {\n mint t0 = a[j0], t1 = a[j1], t2 = a[j2], t3 = a[j3];\n mint t0p2 = t0 + t2, t1p3 = t1 + t3;\n mint t0m2 = t0 - t2, t1m3 = (t1 - t3) * imag;\n a[j0] = t0p2 + t1p3, a[j1] = t0p2 - t1p3;\n a[j2] = t0m2 + t1m3, a[j3] = t0m2 - t1m3;\n }\n }\n // jh >= 1\n mint ww = one, xx = one * dw[2], wx = one;\n for (int jh = 4; jh < u;) {\n ww = xx * xx, wx = ww * xx;\n int j0 = jh * v;\n int je = j0 + v;\n int j2 = je + v;\n for (; j0 < je; ++j0, ++j2) {\n mint t0 = a[j0], t1 = a[j0 + v] * xx, t2 = a[j2] * ww,\n t3 = a[j2 + v] * wx;\n mint t0p2 = t0 + t2, t1p3 = t1 + t3;\n mint t0m2 = t0 - t2, t1m3 = (t1 - t3) * imag;\n a[j0] = t0p2 + t1p3, a[j0 + v] = t0p2 - t1p3;\n a[j2] = t0m2 + t1m3, a[j2 + v] = t0m2 - t1m3;\n }\n xx *= dw[__builtin_ctzll((jh += 4))];\n }\n u <<= 2;\n v >>= 2;\n }\n }\n\n void ifft4(vector<mint> &a, int k) {\n if ((int)a.size() <= 1) return;\n if (k == 1) {\n mint a1 = a[1];\n a[1] = a[0] - a[1];\n a[0] = a[0] + a1;\n return;\n }\n int u = 1 << (k - 2);\n int v = 1;\n mint one = mint(1);\n mint imag = dy[1];\n while (u) {\n // jh = 0\n {\n int j0 = 0;\n int j1 = v;\n int j2 = v + v;\n int j3 = j2 + v;\n for (; j0 < v; ++j0, ++j1, ++j2, ++j3) {\n mint t0 = a[j0], t1 = a[j1], t2 = a[j2], t3 = a[j3];\n mint t0p1 = t0 + t1, t2p3 = t2 + t3;\n mint t0m1 = t0 - t1, t2m3 = (t2 - t3) * imag;\n a[j0] = t0p1 + t2p3, a[j2] = t0p1 - t2p3;\n a[j1] = t0m1 + t2m3, a[j3] = t0m1 - t2m3;\n }\n }\n // jh >= 1\n mint ww = one, xx = one * dy[2], yy = one;\n u <<= 2;\n for (int jh = 4; jh < u;) {\n ww = xx * xx, yy = xx * imag;\n int j0 = jh * v;\n int je = j0 + v;\n int j2 = je + v;\n for (; j0 < je; ++j0, ++j2) {\n mint t0 = a[j0], t1 = a[j0 + v], t2 = a[j2], t3 = a[j2 + v];\n mint t0p1 = t0 + t1, t2p3 = t2 + t3;\n mint t0m1 = (t0 - t1) * xx, t2m3 = (t2 - t3) * yy;\n a[j0] = t0p1 + t2p3, a[j2] = (t0p1 - t2p3) * ww;\n a[j0 + v] = t0m1 + t2m3, a[j2 + v] = (t0m1 - t2m3) * ww;\n }\n xx *= dy[__builtin_ctzll(jh += 4)];\n }\n u >>= 4;\n v <<= 2;\n }\n if (k & 1) {\n u = 1 << (k - 1);\n for (int j = 0; j < u; ++j) {\n mint ajv = a[j] - a[j + u];\n a[j] += a[j + u];\n a[j + u] = ajv;\n }\n }\n }\n\n void ntt(vector<mint> &a) {\n if ((int)a.size() <= 1) return;\n fft4(a, __builtin_ctz(a.size()));\n }\n\n void intt(vector<mint> &a) {\n if ((int)a.size() <= 1) return;\n ifft4(a, __builtin_ctz(a.size()));\n mint iv = mint(a.size()).inverse();\n for (auto &x : a) x *= iv;\n }\n\n vector<mint> multiply(const vector<mint> &a, const vector<mint> &b) {\n int l = a.size() + b.size() - 1;\n if (min<int>(a.size(), b.size()) <= 40) {\n vector<mint> s(l);\n for (int i = 0; i < (int)a.size(); ++i)\n for (int j = 0; j < (int)b.size(); ++j) s[i + j] += a[i] * b[j];\n return s;\n }\n int k = 2, M = 4;\n while (M < l) M <<= 1, ++k;\n setwy(k);\n vector<mint> s(M);\n for (int i = 0; i < (int)a.size(); ++i) s[i] = a[i];\n fft4(s, k);\n if (a.size() == b.size() && a == b) {\n for (int i = 0; i < M; ++i) s[i] *= s[i];\n } else {\n vector<mint> t(M);\n for (int i = 0; i < (int)b.size(); ++i) t[i] = b[i];\n fft4(t, k);\n for (int i = 0; i < M; ++i) s[i] *= t[i];\n }\n ifft4(s, k);\n s.resize(l);\n mint invm = mint(M).inverse();\n for (int i = 0; i < l; ++i) s[i] *= invm;\n return s;\n }\n\n void ntt_doubling(vector<mint> &a) {\n int M = (int)a.size();\n auto b = a;\n intt(b);\n mint r = 1, zeta = mint(pr).pow((mint::get_mod() - 1) / (M << 1));\n for (int i = 0; i < M; i++) b[i] *= r, r *= zeta;\n ntt(b);\n copy(begin(b), end(b), back_inserter(a));\n }\n};\n\ntemplate <typename Int, typename UInt, typename Long, typename ULong, int id>\nstruct ArbitraryLazyMontgomeryModIntBase {\n using mint = ArbitraryLazyMontgomeryModIntBase;\n\n inline static UInt mod;\n inline static UInt r;\n inline static UInt n2;\n static constexpr int bit_length = sizeof(UInt) * 8;\n\n static UInt get_r() {\n UInt ret = mod;\n while (mod * ret != 1) ret *= UInt(2) - mod * ret;\n return ret;\n }\n static void set_mod(UInt m) {\n assert(m < (UInt(1u) << (bit_length - 2)));\n assert((m & 1) == 1);\n mod = m, n2 = -ULong(m) % m, r = get_r();\n }\n UInt a;\n\n ArbitraryLazyMontgomeryModIntBase() : a(0) {}\n ArbitraryLazyMontgomeryModIntBase(const Long &b)\n : a(reduce(ULong(b % mod + mod) * n2)){};\n\n static UInt reduce(const ULong &b) {\n return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length;\n }\n\n mint &operator+=(const mint &b) {\n if (Int(a += b.a - 2 * mod) < 0) a += 2 * mod;\n return *this;\n }\n mint &operator-=(const mint &b) {\n if (Int(a -= b.a) < 0) a += 2 * mod;\n return *this;\n }\n mint &operator*=(const mint &b) {\n a = reduce(ULong(a) * b.a);\n return *this;\n }\n mint &operator/=(const mint &b) {\n *this *= b.inverse();\n return *this;\n }\n\n mint operator+(const mint &b) const { return mint(*this) += b; }\n mint operator-(const mint &b) const { return mint(*this) -= b; }\n mint operator*(const mint &b) const { return mint(*this) *= b; }\n mint operator/(const mint &b) const { return mint(*this) /= b; }\n\n bool operator==(const mint &b) const {\n return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);\n }\n bool operator!=(const mint &b) const {\n return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);\n }\n mint operator-() const { return mint(0) - mint(*this); }\n mint operator+() const { return mint(*this); }\n\n mint pow(ULong n) const {\n mint ret(1), mul(*this);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul, n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const mint &b) {\n return os << b.get();\n }\n\n friend istream &operator>>(istream &is, mint &b) {\n Long t;\n is >> t;\n b = ArbitraryLazyMontgomeryModIntBase(t);\n return (is);\n }\n\n mint inverse() const {\n Int x = get(), y = get_mod(), u = 1, v = 0;\n while (y > 0) {\n Int t = x / y;\n swap(x -= t * y, y);\n swap(u -= t * v, v);\n }\n return mint{u};\n }\n\n UInt get() const {\n UInt ret = reduce(a);\n return ret >= mod ? ret - mod : ret;\n }\n\n static UInt get_mod() { return mod; }\n};\n\n// id に適当な乱数を割り当てて使う\ntemplate <int id>\nusing ArbitraryLazyMontgomeryModInt =\n ArbitraryLazyMontgomeryModIntBase<int, unsigned int, long long,\n unsigned long long, id>;\ntemplate <int id>\nusing ArbitraryLazyMontgomeryModInt64bit =\n ArbitraryLazyMontgomeryModIntBase<long long, unsigned long long, __int128_t,\n __uint128_t, id>;\n\nnamespace ArbitraryNTT {\nusing i64 = int64_t;\nusing u128 = __uint128_t;\nconstexpr int32_t m0 = 167772161;\nconstexpr int32_t m1 = 469762049;\nconstexpr int32_t m2 = 754974721;\nusing mint0 = LazyMontgomeryModInt<m0>;\nusing mint1 = LazyMontgomeryModInt<m1>;\nusing mint2 = LazyMontgomeryModInt<m2>;\nconstexpr int r01 = mint1(m0).inverse().get();\nconstexpr int r02 = mint2(m0).inverse().get();\nconstexpr int r12 = mint2(m1).inverse().get();\nconstexpr int r02r12 = i64(r02) * r12 % m2;\nconstexpr i64 w1 = m0;\nconstexpr i64 w2 = i64(m0) * m1;\n\ntemplate <typename T, typename submint>\nvector<submint> mul(const vector<T> &a, const vector<T> &b) {\n static NTT<submint> ntt;\n vector<submint> s(a.size()), t(b.size());\n for (int i = 0; i < (int)a.size(); ++i) s[i] = i64(a[i] % submint::get_mod());\n for (int i = 0; i < (int)b.size(); ++i) t[i] = i64(b[i] % submint::get_mod());\n return ntt.multiply(s, t);\n}\n\ntemplate <typename T>\nvector<int> multiply(const vector<T> &s, const vector<T> &t, int mod) {\n auto d0 = mul<T, mint0>(s, t);\n auto d1 = mul<T, mint1>(s, t);\n auto d2 = mul<T, mint2>(s, t);\n int n = d0.size();\n vector<int> ret(n);\n const int W1 = w1 % mod;\n const int W2 = w2 % mod;\n for (int i = 0; i < n; i++) {\n int n1 = d1[i].get(), n2 = d2[i].get(), a = d0[i].get();\n int b = i64(n1 + m1 - a) * r01 % m1;\n int c = (i64(n2 + m2 - a) * r02r12 + i64(m2 - b) * r12) % m2;\n ret[i] = (i64(a) + i64(b) * W1 + i64(c) * W2) % mod;\n }\n return ret;\n}\n\ntemplate <typename mint>\nvector<mint> multiply(const vector<mint> &a, const vector<mint> &b) {\n if (a.size() == 0 && b.size() == 0) return {};\n if (min<int>(a.size(), b.size()) < 128) {\n vector<mint> ret(a.size() + b.size() - 1);\n for (int i = 0; i < (int)a.size(); ++i)\n for (int j = 0; j < (int)b.size(); ++j) ret[i + j] += a[i] * b[j];\n return ret;\n }\n vector<int> s(a.size()), t(b.size());\n for (int i = 0; i < (int)a.size(); ++i) s[i] = a[i].get();\n for (int i = 0; i < (int)b.size(); ++i) t[i] = b[i].get();\n vector<int> u = multiply<int>(s, t, mint::get_mod());\n vector<mint> ret(u.size());\n for (int i = 0; i < (int)u.size(); ++i) ret[i] = mint(u[i]);\n return ret;\n}\n\ntemplate <typename T>\nvector<u128> multiply_u128(const vector<T> &s, const vector<T> &t) {\n if (s.size() == 0 && t.size() == 0) return {};\n if (min<int>(s.size(), t.size()) < 128) {\n vector<u128> ret(s.size() + t.size() - 1);\n for (int i = 0; i < (int)s.size(); ++i)\n for (int j = 0; j < (int)t.size(); ++j) ret[i + j] += i64(s[i]) * t[j];\n return ret;\n }\n auto d0 = mul<T, mint0>(s, t);\n auto d1 = mul<T, mint1>(s, t);\n auto d2 = mul<T, mint2>(s, t);\n int n = d0.size();\n vector<u128> ret(n);\n for (int i = 0; i < n; i++) {\n i64 n1 = d1[i].get(), n2 = d2[i].get();\n i64 a = d0[i].get();\n i64 b = (n1 + m1 - a) * r01 % m1;\n i64 c = ((n2 + m2 - a) * r02r12 + (m2 - b) * r12) % m2;\n ret[i] = a + b * w1 + u128(c) * w2;\n }\n return ret;\n}\n} // namespace ArbitraryNTT\n\n\ntemplate <typename mint>\nstruct FormalPowerSeries : vector<mint> {\n using vector<mint>::vector;\n using FPS = FormalPowerSeries;\n\n FPS &operator+=(const FPS &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i];\n return *this;\n }\n\n FPS &operator+=(const mint &r) {\n if (this->empty()) this->resize(1);\n (*this)[0] += r;\n return *this;\n }\n\n FPS &operator-=(const FPS &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i];\n return *this;\n }\n\n FPS &operator-=(const mint &r) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= r;\n return *this;\n }\n\n FPS &operator*=(const mint &v) {\n for (int k = 0; k < (int)this->size(); k++) (*this)[k] *= v;\n return *this;\n }\n\n FPS &operator/=(const FPS &r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int n = this->size() - r.size() + 1;\n if ((int)r.size() <= 64) {\n FPS f(*this), g(r);\n g.shrink();\n mint coeff = g.back().inverse();\n for (auto &x : g) x *= coeff;\n int deg = (int)f.size() - (int)g.size() + 1;\n int gs = g.size();\n FPS quo(deg);\n for (int i = deg - 1; i >= 0; i--) {\n quo[i] = f[i + gs - 1];\n for (int j = 0; j < gs; j++) f[i + j] -= quo[i] * g[j];\n }\n *this = quo * coeff;\n this->resize(n, mint(0));\n return *this;\n }\n return *this = ((*this).rev().pre(n) * r.rev().inv(n)).pre(n).rev();\n }\n\n FPS &operator%=(const FPS &r) {\n *this -= *this / r * r;\n shrink();\n return *this;\n }\n\n FPS operator+(const FPS &r) const { return FPS(*this) += r; }\n FPS operator+(const mint &v) const { return FPS(*this) += v; }\n FPS operator-(const FPS &r) const { return FPS(*this) -= r; }\n FPS operator-(const mint &v) const { return FPS(*this) -= v; }\n FPS operator*(const FPS &r) const { return FPS(*this) *= r; }\n FPS operator*(const mint &v) const { return FPS(*this) *= v; }\n FPS operator/(const FPS &r) const { return FPS(*this) /= r; }\n FPS operator%(const FPS &r) const { return FPS(*this) %= r; }\n FPS operator-() const {\n FPS ret(this->size());\n for (int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i];\n return ret;\n }\n\n void shrink() {\n while (this->size() && this->back() == mint(0)) this->pop_back();\n }\n\n FPS rev() const {\n FPS ret(*this);\n reverse(begin(ret), end(ret));\n return ret;\n }\n\n FPS dot(FPS r) const {\n FPS ret(min(this->size(), r.size()));\n for (int i = 0; i < (int)ret.size(); i++) ret[i] = (*this)[i] * r[i];\n return ret;\n }\n\n // 前 sz 項を取ってくる。sz に足りない項は 0 埋めする\n FPS pre(int sz) const {\n FPS ret(begin(*this), begin(*this) + min((int)this->size(), sz));\n if ((int)ret.size() < sz) ret.resize(sz);\n return ret;\n }\n\n FPS operator>>(int sz) const {\n if ((int)this->size() <= sz) return {};\n FPS ret(*this);\n ret.erase(ret.begin(), ret.begin() + sz);\n return ret;\n }\n\n FPS operator<<(int sz) const {\n FPS ret(*this);\n ret.insert(ret.begin(), sz, mint(0));\n return ret;\n }\n\n FPS diff() const {\n const int n = (int)this->size();\n FPS ret(max(0, n - 1));\n mint one(1), coeff(1);\n for (int i = 1; i < n; i++) {\n ret[i - 1] = (*this)[i] * coeff;\n coeff += one;\n }\n return ret;\n }\n\n FPS integral() const {\n const int n = (int)this->size();\n FPS ret(n + 1);\n ret[0] = mint(0);\n if (n > 0) ret[1] = mint(1);\n auto mod = mint::get_mod();\n for (int i = 2; i <= n; i++) ret[i] = (-ret[mod % i]) * (mod / i);\n for (int i = 0; i < n; i++) ret[i + 1] *= (*this)[i];\n return ret;\n }\n\n mint eval(mint x) const {\n mint r = 0, w = 1;\n for (auto &v : *this) r += w * v, w *= x;\n return r;\n }\n\n FPS log(int deg = -1) const {\n assert(!(*this).empty() && (*this)[0] == mint(1));\n if (deg == -1) deg = (int)this->size();\n return (this->diff() * this->inv(deg)).pre(deg - 1).integral();\n }\n\n FPS pow(int64_t k, int deg = -1) const {\n const int n = (int)this->size();\n if (deg == -1) deg = n;\n if (k == 0) {\n FPS ret(deg);\n if (deg) ret[0] = 1;\n return ret;\n }\n for (int i = 0; i < n; i++) {\n if ((*this)[i] != mint(0)) {\n mint rev = mint(1) / (*this)[i];\n FPS ret = (((*this * rev) >> i).log(deg) * k).exp(deg);\n ret *= (*this)[i].pow(k);\n ret = (ret << (i * k)).pre(deg);\n if ((int)ret.size() < deg) ret.resize(deg, mint(0));\n return ret;\n }\n if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));\n }\n return FPS(deg, mint(0));\n }\n\n static void *ntt_ptr;\n static void set_fft();\n FPS &operator*=(const FPS &r);\n void ntt();\n void intt();\n void ntt_doubling();\n static int ntt_pr();\n FPS inv(int deg = -1) const;\n FPS exp(int deg = -1) const;\n};\ntemplate <typename mint>\nvoid *FormalPowerSeries<mint>::ntt_ptr = nullptr;\n\ntemplate <typename mint>\nvoid FormalPowerSeries<mint>::set_fft() {\n ntt_ptr = nullptr;\n}\n\ntemplate <typename mint>\nvoid FormalPowerSeries<mint>::ntt() {\n exit(1);\n}\n\ntemplate <typename mint>\nvoid FormalPowerSeries<mint>::intt() {\n exit(1);\n}\n\ntemplate <typename mint>\nvoid FormalPowerSeries<mint>::ntt_doubling() {\n exit(1);\n}\n\ntemplate <typename mint>\nint FormalPowerSeries<mint>::ntt_pr() {\n exit(1);\n}\n\ntemplate <typename mint>\nFormalPowerSeries<mint>& FormalPowerSeries<mint>::operator*=(\n const FormalPowerSeries<mint>& r) {\n if (this->empty() || r.empty()) {\n this->clear();\n return *this;\n }\n auto ret = ArbitraryNTT::multiply(*this, r);\n return *this = FormalPowerSeries<mint>(ret.begin(), ret.end());\n}\n\ntemplate <typename mint>\nFormalPowerSeries<mint> FormalPowerSeries<mint>::inv(int deg) const {\n assert((*this)[0] != mint(0));\n if (deg == -1) deg = (*this).size();\n FormalPowerSeries<mint> ret({mint(1) / (*this)[0]});\n for (int i = 1; i < deg; i <<= 1)\n ret = (ret + ret - ret * ret * (*this).pre(i << 1)).pre(i << 1);\n return ret.pre(deg);\n}\n\ntemplate <typename mint>\nFormalPowerSeries<mint> FormalPowerSeries<mint>::exp(int deg) const {\n assert((*this).size() == 0 || (*this)[0] == mint(0));\n if (deg == -1) deg = (int)this->size();\n FormalPowerSeries<mint> ret({mint(1)});\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret * (pre(i << 1) + mint(1) - ret.log(i << 1))).pre(i << 1);\n }\n return ret.pre(deg);\n}\n\nint SOLVEFIN = 0;\n\nusing mint = LazyMontgomeryModInt<1000000007>;\nusing fps = FormalPowerSeries<mint>;\n\nint n,m;\npii prev1(pii x){\n return {x.fi-2,x.se-1};\n}\npii prev2(pii x){\n return {x.fi+2,x.se-1};\n}\nbool range_in(pii x){\n if(x.fi>=0 && x.fi<n && x.se>=0 && x.se<m) return true;\n else return false;\n}\n\ntemplate <typename T>\nvoid zt_des(vector<T> &A, int n) {\n int An = A.size();\n for(int i=0;i<n;i++){ //各次元に対しての更新\n for(int j=0;j<An;j++){ //各要素を見る\n if(j & (1LL<<i)) A[j] += A[j^(1LL<<i)];\n }\n } \n return;\n}\n\nvoid solve(){\n //縦に見ると15マス!!!\n cin>>n>>m;\n if(n==0){\n SOLVEFIN = 1;\n return;\n }\n vs gr(n);\n rep(i,n)cin>>gr[i];\n mint ans = 1;\n \n for(int k=0;k<=3;k++){\n int prevcount = 0;\n vector<vector<mint>> dp(m,vector<mint>(POW((int)2,15)));\n for(int j=0;j<m;j++){\n int count = 0;\n int s = k;\n if(j%2==1) s = (k+2)%4; \n int now = s;\n while(now<n){\n count++;\n now+=4;\n }\n \n vector<mint> prevdp;\n if(j) {prevdp = dp[j-1];\n zt_des(prevdp ,prevcount);}\n\n //ありうる状態を列挙\n \n for(int st=0;st<(1<<count);st++){\n int imp = 0;\n for(int t=0;t<count;t++){\n if(gr[s+4*t][j]=='#' && ((st>>t)&1) ){imp=1;break;}\n }\n if(imp)continue;\n if(j==0){dp[0][st]=1;continue;}\n int offset = 0;\n unsigned int maxstate = POW(2,prevcount)-1;\n if(range_in(prev1({s,j})))offset= 1;\n //cout<<k<<\" \"<<j<<\" \"<<st<<\" \"<<offset<<\" \"<<s<<endl;\n for(int t=0;t<count;t++){\n if(((st>>t)&1) && (t+offset)<prevcount){\n if((maxstate>>(t+offset))&1) maxstate = ~(~maxstate | (1<<(t+offset)));\n }\n if(((st>>t)&1) && (t+offset-1)<prevcount && (t+offset-1)>=0){\n if((maxstate>>(t+offset-1))&1)maxstate = ~(~maxstate | (1<<(t+offset-1)));\n }\n }\n //if(st==2 && k==0 && j==2)cout<<maxstate<<\" \"<<prevcount<<\" \"<<count<<\"A\"<<endl;\n \n dp[j][st] = prevdp[maxstate];\n\n }\n\n prevcount = count;\n }\n mint res = 0;\n // rep(o,m){\n // rep(p,(1<<prevcount)){\n // cout<<k<<\" \"<<\" \"<<o<<\" \"<<p<<\" \"<<dp[o][p]<<endl;\n // }\n // }\n for(int i=0;i<(1<<15);i++){\n res += dp[m-1][i];\n }\n ans*=res;\n //cout<<dp[0][0]<<\" \"<<dp[0][1]<<endl;\n }\n cout<<ans<<endl;\n \n\n \n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\twhile(SOLVEFIN == 0) solve();\n}", "accuracy": 1, "time_ms": 2470, "memory_kb": 10932, "score_of_the_acc": -0.4525, "final_rank": 9 }, { "submission_id": "aoj_1644_10367009", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\ntemplate<int MOD> struct Modint {\n long long val;\n constexpr Modint(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; }\n constexpr int mod() const { return MOD; }\n constexpr long long value() const { return val; }\n constexpr Modint operator - () const noexcept { return val ? MOD - val : 0; }\n constexpr Modint operator + (const Modint& r) const noexcept { return Modint(*this) += r; }\n constexpr Modint operator - (const Modint& r) const noexcept { return Modint(*this) -= r; }\n constexpr Modint operator * (const Modint& r) const noexcept { return Modint(*this) *= r; }\n constexpr Modint operator / (const Modint& r) const noexcept { return Modint(*this) /= r; }\n constexpr Modint& operator += (const Modint& r) noexcept {\n val += r.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n constexpr Modint& operator -= (const Modint& r) noexcept {\n val -= r.val;\n if (val < 0) val += MOD;\n return *this;\n }\n constexpr Modint& operator *= (const Modint& r) noexcept {\n val = val * r.val % MOD;\n return *this;\n }\n constexpr Modint& operator /= (const Modint& r) noexcept {\n long long a = r.val, b = MOD, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n val = val * u % MOD;\n if (val < 0) val += MOD;\n return *this;\n }\n constexpr bool operator == (const Modint& r) const noexcept { return this->val == r.val; }\n constexpr bool operator != (const Modint& r) const noexcept { return this->val != r.val; }\n friend constexpr istream& operator >> (istream& is, Modint<MOD>& x) noexcept {\n is >> x.val;\n x.val %= MOD;\n if (x.val < 0) x.val += MOD;\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Modint<MOD>& x) noexcept {\n return os << x.val;\n }\n constexpr Modint<MOD> pow(long long n) noexcept {\n if (n == 0) return 1;\n if (n < 0) return this->pow(-n).inv();\n Modint<MOD> ret = pow(n >> 1);\n ret *= ret;\n if (n & 1) ret *= *this;\n return ret;\n }\n constexpr Modint<MOD> inv() const noexcept {\n long long a = this->val, b = MOD, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Modint<MOD>(u);\n }\n};\n\nconst int MOD = MOD7;\nusing mint = Modint<MOD>;\n\nmint solve(vector<string> s, int m, int f) {\n int n = s.size();\n \n \n vector<pair<int, int>> vec;\n \n for(int j = 0; j < m; j ++) {\n for(int i = (j & 1 ? f^1 : f); i < n; i += 2) {\n vec.push_back({i, j});\n }\n }\n int sz = vec.size();\n int num = (n + 3) / 2;\n num = min(num, sz);\n int mask = 1 << num;\n vector<mint> dp(mask, mint(0));\n rep(bit, mask) {\n bool ng = 0;\n rep(i, num) if(bit >> i & 1) {\n if(s[vec[i].first][vec[i].second] == '#') {\n ng = 1;\n break;\n }\n for(int j = i + 1; j < num; j ++) {\n if(abs(vec[j].first - vec[i].first) == 1 and abs(vec[j].second - vec[i].second) == 1 and (bit >> j & 1)) {\n ng = 1;\n i = num;\n break;\n }\n }\n }\n if(!ng) {\n dp[bit] = 1;\n }\n }\n \n for(int i = num; i < sz; i ++) {\n vector<mint> nx(mask, 0);\n rep(bit, mask) {\n nx[bit >> 1] += dp[bit];\n if(s[vec[i].first][vec[i].second] == '#') continue;\n bool ng = 0;\n for(int j = 0; j < 3; j ++) if(bit >> j & 1) {\n if(j >= num) break;\n int id = i - num + j;\n if(abs(vec[i].first - vec[id].first) == 1 and abs(vec[i].second - vec[id].second) == 1) {\n ng = 1;\n break;\n }\n }\n if(!ng) nx[(bit >> 1) | (1 << (num - 1))] += dp[bit];\n }\n swap(nx, dp);\n }\n mint ans = accumulate(all(dp), mint(0));\n return ans;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m; cin >> n >> m;\n while(n) {\n vector<string> s, t;\n rep(i, n) {\n string in; cin >> in;\n if(i & 1) s.pb(in);\n else t.pb(in);\n }\n cout << solve(t, m, 0) * solve(s, m, 0) * solve(t, m, 1) * solve(s, m, 1) << endl; \n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3270, "memory_kb": 4608, "score_of_the_acc": -0.4568, "final_rank": 10 }, { "submission_id": "aoj_1644_10065208", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\nconst ll mod = 1000000007;\n// const ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nstruct modint {\n int Mod = mod;\n int val;\n using mint = modint;\n modint() {};\n modint(ll v) {\n if (0 <= v && v < Mod) val = v;\n else{\n v %= Mod;\n if (v < 0) v += Mod;\n val = v;\n }\n }\n mint operator++(int){\n val++;\n if (val == Mod) val = 0;\n return *this;\n }\n mint operator--(int){\n if (val == 0) val = Mod;\n val--;\n return *this;\n }\n mint& operator+=(const mint& v){\n val += v.val;\n if (val >= Mod) val -= Mod;\n return *this;\n }\n mint& operator-=(const mint& v){\n val -= v.val;\n if (val < 0) val += Mod;\n return *this;\n }\n mint& operator*=(const mint& v){\n val = (int)(1ll * val * v.val % Mod);\n return *this;\n }\n modint inverse() const {\n int a = val, b = mod, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b), swap(u -= t * v, v);\n }\n return modint(u);\n }\n mint pow(ll n) const {\n mint a = *this, ret = 1;\n while (n > 0){\n if (n & 1) ret *= a;\n a *= a;\n n >>= 1;\n }\n return ret;\n }\n mint& operator>>=(ll k){\n *this *= mint(2).pow(k).pow(Mod - 2);\n return *this;\n }\n mint& operator<<=(ll k){\n *this *= mint(2).pow(k);\n return *this;\n}\n mint operator/=(const mint& v){\n *this *= v.inverse();\n return *this;\n }\n mint operator-() const { return mint(-val);}\n friend mint operator+(const mint& u, const mint& v){return mint(u) += v;}\n friend mint operator-(const mint& u, const mint& v){return mint(u) -= v;}\n friend mint operator*(const mint& u, const mint& v){return mint(u) *= v;}\n friend mint operator/(const mint& u, const mint& v){return mint(u) /= v;}\n friend bool operator==(const mint& u, const mint& v){return u.val == v.val;}\n friend bool operator!=(const mint& u, const mint& v){return u.val != v.val;}\n friend bool operator>(const mint& u, const mint& v){return u.val > v.val;}\n friend bool operator<(const mint& u, const mint& v){return u.val < v.val;}\n friend mint operator>>(mint& u, const ll k){return u >>= k;}\n friend mint operator<<(mint& u, const ll k){return u <<= k;}\n friend ostream& operator<<(ostream& stream, mint& v){\n stream << v.val;\n return stream;\n }\n};\n\nusing mint = modint;\n\nmint dp[2][1 << 18];\n\nmint f(vc<string> &S){\n int nw = 0, nxt = 1;\n int H = len(S), W = len(S[0]);\n rep(j, 1 << (W + 1)) dp[nw][j] = 0;\n dp[0][0] = 1;\n rep(i, H * W){\n int x = i / W, y = i % W;\n rep(j, 1 << (W + 1)) dp[nxt][j] = 0;\n rep(j, 1 << (W + 1)) dp[nxt][j >> 1] += dp[nw][j];\n if (S[x][y] == '.'){\n rep(j, 1 << (W + 1)){\n bool flag = true;\n if (j >> 1 & 1) flag = false;\n if (x % 2 && y > 0 && j & 1) flag = false;\n if (x % 2 == 0 && y < W - 1 && j >> 2 & 1) flag = false;\n if (flag) dp[nxt][(j >> 1) | (1 << W)] += dp[nw][j];\n }\n }\n swap(nw, nxt);\n }\n mint ans = 0;\n rep(j, 1 << (W + 1)) ans += dp[nw][j];\n return ans;\n}\n\nvoid solve(int N, int M){\n vc<string> A(N); rep(i, N) cin >> A[i];\n mint ans = 1;\n rep(k, 4){\n vc<string> B(M, string(N, '#'));\n rep(j, M){\n for (int i = k - (j % 2) * 2, ni = 0; i < N; i += 4, ni++) if (inside(i, j, N, M)){\n B[j][ni] = A[i][j];\n }\n }\n while (len(B[0]) >= 2){\n bool flag = true;\n rep(i, M) if (B[i].back() == '.'){\n flag = false;\n break;\n }\n if (!flag) break;\n rep(i, M) B[i].pop_back();\n }\n ans *= f(B);\n }\n cout << ans << endl;\n}\n\nint main(){\n while (true){\n int N, M; cin >> N >> M;\n if (N == 0) break;\n solve(N, M);\n }\n}", "accuracy": 1, "time_ms": 3080, "memory_kb": 7444, "score_of_the_acc": -0.4814, "final_rank": 11 }, { "submission_id": "aoj_1644_9662932", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n#define MOD 1000000007\n// #define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\ntemplate< int mod >\nstruct ModInt {\n int x;\n\n ModInt() : x(0) {}\n\n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n\n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n\n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n\n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n\n ModInt operator-() const { return ModInt(-x); }\n\n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n\n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n\n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n\n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n\n bool operator==(const ModInt &p) const { return x == p.x; }\n\n bool operator!=(const ModInt &p) const { return x != p.x; }\n\n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n\n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n\n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n\n static int get_mod() { return mod; }\n};\n\nusing modint = ModInt< MOD >;\nusing mvi = vector<modint>;\nusing mvvi = vector<mvi>;\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m;cin >> n >> m;\n if(!n)break;\n vector<string> a(n);\n cin >> a;\n modint res = 0;\n auto c2 = [&](vi &od,vi &ev)->modint{\n int N = max(sz(od),sz(ev));\n mvi dp(1ll << N);\n dp[0] = 1;\n rep(j,m){\n mvi DP(1ll << N);\n if(!(j & 1)){\n for(int bit = 0;bit < 1ll << sz(ev);bit++){\n bool is = true;\n rep(i,sz(ev))if((bit >> i & 1) && a[ev[i]][j] == '#'){\n is = false;break;\n }\n if(!is)continue;\n int msk = (1ll << sz(od))-1;\n rep(i,sz(ev))if(bit >> i & 1){\n int k = (ev[i]+2)/2;k = k/2;\n if(k < sz(od))msk = msk & (~(1ll << k));\n if(ev[i] < 2)continue;\n k = (ev[i]-2)/2;k = k/2;\n if(k < sz(od))msk = msk & (~(1ll << k));\n }\n for(int S = msk;S > 0;S = (S-1) & msk){\n DP[bit] += dp[S];\n }\n DP[bit] += dp[0];\n }\n }else{\n for(int bit = 0;bit < 1ll << sz(od);bit++){\n bool is = true;\n rep(i,sz(od))if((bit >> i & 1) && a[od[i]][j] == '#'){\n is = false;break;\n }\n if(!is)continue;\n int msk = (1ll << sz(ev))-1;\n rep(i,sz(od))if(bit >> i & 1){\n int k = (od[i]+2)/2;k = k/2;\n if(k < sz(ev))msk = msk & (~(1ll << k));\n if(od[i] < 2)continue;\n k = (od[i]-2)/2;k = k/2;\n if(k < sz(ev))msk = msk & (~(1ll << k));\n }\n for(int S = msk;S > 0;S = (S-1) & msk){\n DP[bit] += dp[S];\n }\n DP[bit] += dp[0];\n }\n }\n swap(dp,DP);\n }\n modint ret = 0;\n rep(i,1ll << N)ret += dp[i];\n return ret;\n };\n auto calc = [&](vi &p)->modint{\n int N = sz(p);\n vi ev,od;\n rep(i,N){\n if(!(i & 1))ev.emplace_back(p[i]);\n else od.emplace_back(p[i]);\n }\n modint ret = c2(od,ev);\n ret = ret*c2(ev,od);\n return ret;\n };\n \n vi p((n+1)/2);\n for(int i = 0;i < n;i+=2)p[i/2] = i;\n res = calc(p);\n // cout << res << \" a\\n\";\n p.resize(n/2);\n for(int i = 1;i < n;i+=2)p[i/2] = i;\n res = res*calc(p);\n // cout << calc(p) << \" a\\n\";\n cout << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 5680, "memory_kb": 3456, "score_of_the_acc": -0.8142, "final_rank": 15 }, { "submission_id": "aoj_1644_9462042", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\n//using mint=modint<998244353>;\nusing mint=modint<1000000007>;\nvvi LS(1<<15),SL(1<<15);\nvoid init(){\n REP(i,1<<15){\n ll s=(1<<15)-1;\n REP(j,15)if((i>>j)%2){\n s-=s&(1<<j);\n if(j)s-=s&(1<<(j-1));\n }\n for(ll j=s;j;j=(j-1)&s)LS[i].emplace_back(j);\n LS[i].emplace_back(0);\n reverse(ALL(LS[i]));\n }\n REP(i,1<<15){\n ll s=(1<<15)-1;\n REP(j,15)if((i>>j)%2){\n s-=s&(1<<j);\n if(j+1<15)s-=s&(1<<(j+1));\n }\n for(ll j=s;j;j=(j-1)&s)SL[i].emplace_back(j);\n SL[i].emplace_back(0);\n reverse(ALL(SL[i]));\n }\n}\nmint f(vector<string>S,ll H,ll W){\n mint ans1=0,ans2=0;\n {\n //even part\n vi A(W);\n REP(i,W)REP(j,H)if((i+j)%2==0&&S[j][i]=='.')A[i]+=1<<(j/2);\n vector<vector<mint>>DP(W,vector<mint>(1<<((H+1)/2)));\n for(ll i=A[0];i;i=(i-1)&A[0])DP[0][i]=1;DP[0][0]=1;\n REP(i,W-1)REP(j,1<<((H+1-i%2)/2)){\n if(i%2){\n //SL\n for(auto k:SL[j])if((k&A[i+1])==k)DP[i+1][k]+=DP[i][j];\n }\n else{\n //LS\n for(auto k:LS[j])if((k&A[i+1])==k)DP[i+1][k]+=DP[i][j];\n }\n }\n REP(i,sz(DP[W-1]))ans1+=DP[W-1][i];\n }\n {\n //odd part\n vi A(W);\n REP(i,W)REP(j,H)if((i+j)%2==1&&S[j][i]=='.')A[i]+=1<<(j/2);\n vector<vector<mint>>DP(W,vector<mint>(1<<((H+1)/2)));\n for(ll i=A[0];i;i=(i-1)&A[0])DP[0][i]=1;DP[0][0]=1;\n REP(i,W-1)REP(j,1<<((H+i%2)/2)){\n if(i%2==0){\n //SL\n for(auto k:SL[j])if((k&A[i+1])==k)DP[i+1][k]+=DP[i][j];\n }\n else{\n //LS\n for(auto k:LS[j])if((k&A[i+1])==k)DP[i+1][k]+=DP[i][j];\n }\n }\n REP(i,sz(DP[W-1]))ans2+=DP[W-1][i];\n }\n return ans1*ans2;\n}\nint main(){\n init();\n while(1){\n ll H,W;cin>>H>>W;\n if(!H)return 0;\n vector<string>S(H);cin>>S;\n vector<string>S1,S2;\n REP(i,H)(i%2?S2:S1).emplace_back(S[i]);\n mint ans=f(S1,sz(S1),W)*f(S2,sz(S2),W);\n cout<<ans.val()<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4330, "memory_kb": 55440, "score_of_the_acc": -1.6016, "final_rank": 20 }, { "submission_id": "aoj_1644_9429548", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nusing Fp = fp<1000000007>;\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector X(N, vector<bool>(M));\n rep(i,0,N) {\n rep(j,0,M) {\n char C;\n cin >> C;\n if (C == '.') X[i][j] = true;\n else X[i][j] = false;\n }\n }\n Fp ANS = 1;\n rep(_,0,4) {\n vector<vector<int>> V(M);\n vector<int> W(M), B(M,0);\n rep(j,0,M) {\n rep(i,0,N) {\n if ((i+j*2)%4==_) V[j].push_back(i), B[j]+=(1<<(i/4))*(int)(X[i][j]);\n }\n W[j]=V[j].size();\n }\n vector<Fp> DP(1<<W[0],0);\n rep(i,0,1<<W[0]) {\n if ((i&B[0])==i) DP[i]=1;\n }\n rep(j,1,M) {\n vector<Fp> DP2(1<<W[j],0);\n rep(k,0,1<<W[j-1]) {\n vector<bool> Flag(W[j]);\n rep(l,0,W[j]) Flag[l] = X[V[j][l]][j];\n rep(l,0,W[j-1]) {\n if ((k & (1<<l)) != 0) {\n int Now = V[j-1][l];\n if (Now-2 >= 0) {\n Flag[(Now-2)/4] = false;\n }\n if (Now+2 < N) {\n Flag[(Now+2)/4] = false;\n }\n }\n }\n int XX = 0;\n rep(l,0,W[j]) if (Flag[l]) XX += (1<<l);\n DP2[XX] += DP[k];\n }\n rep(l,0,W[j]) {\n rep(m,0,1<<W[j]) {\n if ((m & (1<<l)) == 0) DP2[m] += DP2[m | (1<<l)];\n }\n }\n DP.resize(1<<W[j]);\n rep(k,0,1<<W[j]) DP[k] = DP2[k];\n }\n Fp COUNT = 0;\n rep(__,0,1<<W[M-1]) COUNT += DP[__];\n ANS *= COUNT;\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 6860, "memory_kb": 3700, "score_of_the_acc": -1.0047, "final_rank": 19 }, { "submission_id": "aoj_1644_9393676", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n#define rep2(i,m,n) for(ll i=ll(m);i<ll(n);i++)\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vvvl = vector<vvl>;\nusing pl = pair<ll,ll>;\nusing vpl = vector<pl>;\n#define pb push_back\n\nconst long double EPS = 0.0000000001;\nconst ll INF = 1000000000000000000;\nconst double pi = std::acos(-1.0);\n\n__int128 read_int128(){ //__int128を入力する\n string S;\n cin >> S;\n int N = S.size();\n int st = 0;\n bool minus = false;\n if(S[0] == '-'){\n minus = true;\n st = 1;\n }\n __int128 res = 0;\n rep2(i,st,N) res = res*10+int(S[i]-'0');\n if(minus) res *= -1;\n return res;\n}\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { //これでcoutで__int128を出力できるように\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\nvoid Yes(){ cout << \"Yes\" << endl; } //文字列\"Yes\"を標準出力\nvoid No(){ cout << \"No\" << endl; } //文字列\"No\"を標準出力\n\nvoid Sort(vl &A){ sort(A.begin(),A.end()); } //long longの配列を昇順ソート(破壊的)\nvoid rSort(vl &A){ sort(A.rbegin(),A.rend()); } //long longの配列を降順ソート(破壊的)\nvl Sorted(vl A){ //long longの配列を昇順ソートしたものを返す(非破壊的)\n vl res = A;\n Sort(res);\n return res;\n}\nvl rSorted(vl A){ //long longの配列を降順ソートしたものを返す(非破壊的)\n vl res = A;\n rSort(res);\n return res;\n}\n\nstruct cumulative_sum1d{ //1次元累積和\n int N;\n vl S;\n cumulative_sum1d(int n,vl A):N(),S(n+1,0){\n N = n;\n rep(i,N) S[i+1] = S[i]+A[i];\n }\n \n ll sum(int l,int r){ //区間[l,r]の和を出力(0<=l<=r<=N-1)\n return S[r+1]-S[l];\n }\n};\n\ntemplate<class T> bool chmin(T& a,T b){\n if(a > b){\n a = b;\n return true;\n }\n else return false;\n}\n\ntemplate<class T> bool chmax(T& a,T b){\n if(a < b){\n a = b;\n return true;\n }\n else return false;\n}\n\n/*\n辺の貼り方\nxi=0ならコストAi:s->iにAi\nxi=1ならコストBi:i->tにBi\n正負によってはポテンシャル付ける\n\nxi=1かつxj=0のときコストC:i->jにC\nxi=1かつxj=1のとき利得P:s->iにP、i->jにP、-Pのポテンシャル\nxi=0かつxj=0のとき利得P:i->jにP、j->tにP、-Pのポテンシャル\n\nコストがxi=0,xj=0でA、xi=0,xj=1でB、xi=1,xj=0でC、xi=1,xj=1でDで、B+C >= A+Dとする。\ni->tにB-A、j->tにD-B、i->jにB+C-A-D、Aのポテンシャル\n\n*/\n\ntemplate< typename flow_t >\nstruct Dinic {\n const flow_t INF;\n\n struct edge {\n int to;\n flow_t cap;\n int rev;\n bool isrev;\n int idx;\n };\n\n vector< vector< edge > > graph;\n vector< int > min_cost, iter;\n\n Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}\n\n void add_edge(int from, int to, flow_t cap, int idx = -1) {\n graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});\n graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});\n }\n\n bool bfs(int s, int t) {\n min_cost.assign(graph.size(), -1);\n queue< int > que;\n min_cost[s] = 0;\n que.push(s);\n while(!que.empty() && min_cost[t] == -1) {\n int p = que.front();\n que.pop();\n for(auto &e : graph[p]) {\n if(e.cap > 0 && min_cost[e.to] == -1) {\n min_cost[e.to] = min_cost[p] + 1;\n que.push(e.to);\n }\n }\n }\n return min_cost[t] != -1;\n }\n\n flow_t dfs(int idx, const int t, flow_t flow) {\n if(idx == t) return flow;\n for(int &i = iter[idx]; i < graph[idx].size(); i++) {\n edge &e = graph[idx][i];\n if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {\n flow_t d = dfs(e.to, t, min(flow, e.cap));\n if(d > 0) {\n e.cap -= d;\n graph[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n\n flow_t max_flow(int s, int t) {\n flow_t flow = 0;\n while(bfs(s, t)) {\n iter.assign(graph.size(), 0);\n flow_t f = 0;\n while((f = dfs(s, t, INF)) > 0) flow += f;\n }\n return flow;\n }\n\n void output() {\n for(int i = 0; i < graph.size(); i++) {\n for(auto &e : graph[i]) {\n if(e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << e.cap + rev_e.cap << \")\" << endl;\n }\n }\n }\n};\n\nconst long long int MOD = 1000000007;\n\nlong long int modpow(long long int a,long long int n,long long int mod){\n long long int res = 1;\n while(n>0){\n if(n&1) res = res*a%mod;\n a = a*a%mod;\n n >>= 1;\n }\n return res;\n}\n\nconst long long int MAX = 510000; //最大\n\nlong long int fac[MAX], finv[MAX], inv[MAX];\n\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for(int i=2;i<MAX;i++){\n fac[i] = fac[i-1]*i%MOD;\n inv[i] = MOD-inv[MOD%i]*(MOD/i)%MOD;\n finv[i] = finv[i-1]*inv[i]%MOD;\n }\n}\n\nlong long int COM(long long int n,long long int k){\n if(n<k) return 0;\n if(n<0||k<0) return 0;\n return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;\n}\n\nstruct UnionFind{ //グラフの連結性を管理\n vector<int> par,siz;\n UnionFind(int n):par(n,-1),siz(n,1){}\n \n int root(int x){ //頂点xが属する連結成分の代表点を求める\n if(par[x] == -1) return x;\n else return par[x] = root(par[x]);\n }\n bool issame(int x,int y){ //頂点x,yが同じ連結成分に属しているか\n if(root(x) == root(y)) return true;\n else return false;\n }\n bool unite(int x,int y){ //頂点x,yを辺で結ぶ\n x = root(x);\n y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) swap(x,y);\n par[y] = x;\n siz[x] += siz[y];\n return true;\n }\n int size(int x){ //頂点xが属する連結成分の大きさを求める\n return siz[root(x)];\n }\n};\n\n/*遅延セグメントツリー\nX:セグメントに入れる値の型\nM:作用させるものの型\nfx:セグメントどうしの結合\nfa:XにMを作用させるとき\nfm:Mが追加されたとき\nex:Xの単位元\nem:Mの単位元\n\nRSQだったらこうする\nusing X = long long;\nusing M = long long;\nauto fx = [](X x1, X x2) -> X { return x1 + x2; };\nauto fa = [](X x, M m) -> X { return x + m; };\nauto fm = [](M m1, M m2) -> M { return m1 + m2; };\nlong long ex = 0;\nlong long em = 0;\nSegTreeLazy<X, M> rsq(n,fx,fa,fm,ex,em);\n*/\ntemplate <typename X, typename M>\nstruct SegTreeLazy {\n using FX = function<X(X,X)>;\n using FA = function<X(X,M)>;\n using FM = function<M(M,M)>;\n int n;\n FX fx;\n FA fa;\n FM fm;\n const X ex;\n const M em;\n vector<X> dat;\n vector<M> lazy;\n SegTreeLazy(int n_,FX fx_,FA fa_, FM fm_, X ex_, M em_)\n : n(), fx(fx_), fa(fa_), fm(fm_), ex(ex_), em(em_), dat(n_ * 4, ex), lazy(n_ * 4, em) {\n int x = 1;\n while(n_ > x) x *= 2;\n n = x;\n }\n void set(int i, X x) { dat[i+n-1] = x; }\n void build(){\n for(int k=n-2;k>=0;k--) dat[k] = fx(dat[2*k+1],dat[2*k+2]);\n }\n void eval(int k){\n if(lazy[k] == em) return;\n if(k < n-1){\n lazy[k*2+1] = fm(lazy[k*2+1],lazy[k]);\n lazy[k*2+2] = fm(lazy[k*2+2],lazy[k]);\n }\n dat[k] = fa(dat[k],lazy[k]);\n lazy[k] = em;\n }\n void update(int a,int b,M x,int k,int l,int r){\n eval(k);\n if(a <= l && r <= b){\n lazy[k] = fm(lazy[k],x);\n eval(k);\n } \n else if(a < r && l < b){\n update(a,b,x,k*2+1,l,(l+r)/2);\n update(a,b,x,k*2+2,(l+r)/2,r);\n dat[k] = fx(dat[k*2+1],dat[k*2+2]);\n }\n }\n void update(int a,int b,M x) { update(a,b,x,0,0,n); }\n X query_sub(int a,int b,int k,int l,int r) {\n eval(k);\n if(r <= a || b <= l){\n return ex;\n } \n else if(a <= l && r <= b){\n return dat[k];\n } \n else{\n X vl = query_sub(a,b,k*2+1,l,(l+r)/2);\n X vr = query_sub(a,b,k*2+2,(l+r)/2,r);\n return fx(vl, vr);\n }\n }\n X query(int a,int b){ return query_sub(a,b,0,0,n); }\n};\n\nvoid dft_for_fft(vector<complex<long double>> &func, int inverse){\n int len = func.size();\n if(len == 1) return;\n vector<complex<long double>> A,B;\n rep(i,len/2){\n A.push_back(func[2*i]);\n B.push_back(func[2*i+1]);\n }\n dft_for_fft(A,inverse);\n dft_for_fft(B,inverse);\n complex<long double> now = 1,zeta = polar((long double)1.0,inverse*2.0*pi/(long double)len);\n rep(i,len){\n func[i] = A[i%(len/2)]+now*B[i%(len/2)];\n now *= zeta;\n }\n}\n\ntemplate<typename T>\nvector<long double> multiply(vector<T> f, vector<T> g){\n int len = 1;\n while(len < f.size()+g.size()) len *= 2;\n vector<complex<long double>> nf(len),ng(len);\n rep(i,len){\n if(i < f.size()) nf[i] = f[i];\n if(i < g.size()) ng[i] = g[i];\n }\n dft_for_fft(nf,1);\n dft_for_fft(ng,1);\n rep(i,len) nf[i] *= ng[i];\n dft_for_fft(nf,-1);\n vector<long double> res;\n rep(i,len) res.push_back(nf[i].real()/len);\n return res;\n}\n\nll depth_cnt(ll N,ll u,int d){\n vector<ll> res(61,0);\n res[0] = 1;\n ll l = 2LL*u,r=2LL*u+1;\n rep2(i,1,61){\n if(l > N) break;\n else if(r <= N) res[i] = r-l+1;\n else{\n res[i] = N-l+1;\n break;\n }\n l *= 2LL;\n r *= 2LL;\n r++;\n }\n if(d < 0 || d > 60) return 0;\n return res[d];\n}\n\ntemplate< typename flow_t, typename cost_t >\nstruct PrimalDual {\n const cost_t INF;\n\n struct edge {\n int to;\n flow_t cap;\n cost_t cost;\n int rev;\n bool isrev;\n };\n vector< vector< edge > > graph;\n vector< cost_t > potential, min_cost;\n vector< int > prevv, preve;\n\n PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}\n\n void add_edge(int from, int to, flow_t cap, cost_t cost) {\n graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});\n graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});\n }\n\n cost_t min_cost_flow(int s, int t, flow_t f) {\n int V = (int) graph.size();\n cost_t ret = 0;\n using Pi = pair< cost_t, int >;\n priority_queue< Pi, vector< Pi >, greater< Pi > > que;\n potential.assign(V, 0);\n preve.assign(V, -1);\n prevv.assign(V, -1);\n\n while(f > 0) {\n min_cost.assign(V, INF);\n que.emplace(0, s);\n min_cost[s] = 0;\n while(!que.empty()) {\n Pi p = que.top();\n que.pop();\n if(min_cost[p.second] < p.first) continue;\n for(int i = 0; i < graph[p.second].size(); i++) {\n edge &e = graph[p.second][i];\n cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];\n if(e.cap > 0 && min_cost[e.to] > nextCost) {\n min_cost[e.to] = nextCost;\n prevv[e.to] = p.second, preve[e.to] = i;\n que.emplace(min_cost[e.to], e.to);\n }\n }\n }\n if(min_cost[t] == INF) return INF;\n for(int v = 0; v < V; v++) potential[v] += min_cost[v];\n flow_t addflow = f;\n for(int v = t; v != s; v = prevv[v]) {\n addflow = min(addflow, graph[prevv[v]][preve[v]].cap);\n }\n f -= addflow;\n ret += addflow * potential[t];\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = graph[prevv[v]][preve[v]];\n e.cap -= addflow;\n graph[v][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n\n void output() {\n for(int i = 0; i < graph.size(); i++) {\n for(auto &e : graph[i]) {\n if(e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << rev_e.cap + e.cap << \")\" << endl;\n }\n }\n }\n};\n\ntemplate <class F>\nvector<ll> monotone_maxima(F &f, int h, int w) {\n vector<ll> ret(h);\n auto sol = [&](auto &&self, const int l_i, const int r_i, const int l_j, const int r_j) -> void {\n const int m_i = (l_i + r_i) / 2;\n int max_j = l_j;\n ll max_val = -INF;\n for (int j = l_j; j <= r_j; ++j) {\n const ll v = f(m_i, j);\n if (v > max_val) {\n max_j = j;\n max_val = v;\n }\n }\n ret[m_i] = max_val;\n\n if (l_i <= m_i - 1) {\n self(self, l_i, m_i - 1, l_j, max_j);\n }\n if (m_i + 1 <= r_i) {\n self(self, m_i + 1, r_i, max_j, r_j);\n }\n };\n sol(sol, 0, h - 1, 0, w - 1);\n return ret;\n}\n\nvector<ll> max_plus_convolution(const vector<ll> &a, const vector<ll> &b) {\n const int n = (int)a.size(), m = (int)b.size();\n auto f = [&](int i, int j) {\n if (i < j or i - j >= m) {\n return -INF;\n }\n return a[j] + b[i - j];\n };\n\n return monotone_maxima(f, n + m - 1, n);\n}\n \nvector<vector<long long int>> mat_mul(vector<vector<long long int>> &A,vector<vector<long long int>> &B){\n vector<vector<long long int>> C(A.size(),vector<long long int>(B[0].size()));\n for(int i=0;i<(int)A.size();i++) for(int k=0;k<(int)B.size();k++) for(int j=0;j<(int)B[0].size();j++) C[i][j] = (C[i][j]+A[i][k]*B[k][j])%MOD;\n return C;\n}\n \nvector<vector<long long int>> mat_pow(vector<vector<long long int>> &A,long long int n){\n ll N = n;\n vector<vector<long long int>> B(A.size(),vector<long long int>(A.size()));\n for(int i=0;i<(int)A.size();i++) B[i][i] = 1;\n while(N > 0){\n if(N & 1) B = mat_mul(B,A);\n A = mat_mul(A,A);\n N >>= 1;\n }\n return B;\n}\n\nvoid MergeSort(vl &a,ll left, ll right){\n if(right-left == 1) return;\n int mid = (left+right)/2;\n MergeSort(a,left,mid);\n MergeSort(a,mid,right);\n vl buf;\n for(int i=left;i<mid;i++) buf.push_back(a[i]);\n for(int i=right-1;i>=mid;i--) buf.push_back(a[i]);\n int index_left = 0;\n int index_right = (int)buf.size()-1;\n for(int i=left;i<right-1;i++){\n if(buf[index_left] <= buf[index_right]){\n a[i] = buf[index_left];\n index_left++;\n }\n else{\n a[i] = buf[index_right];\n index_right--;\n }\n }\n a[right-1] = buf[index_left];\n}\n\ntemplate<class COST> struct TwoVariableSubmodularOpt {\n // edge class\n struct Edge {\n // core members\n int rev, from, to;\n COST cap, icap, flow;\n \n // constructor\n Edge(int r, int f, int t, COST c)\n : rev(r), from(f), to(t), cap(c), icap(c), flow(0) {}\n void reset() { cap = icap, flow = 0; }\n \n // debug\n friend ostream& operator << (ostream& s, const Edge& E) {\n return s << E.from << \"->\" << E.to << '(' << E.flow << '/' << E.icap << ')';\n }\n };\n \n // constructor\n TwoVariableSubmodularOpt() : N(2), S(0), T(0), OFFSET(0) {}\n TwoVariableSubmodularOpt(int n, COST inf = 0)\n : N(n), S(n), T(n + 1), OFFSET(0), INF(inf), list(n + 2) {}\n void init(int n, COST inf = 0) {\n N = n, S = n, T = n + 1;\n OFFSET = 0, INF = inf;\n list.assign(N + 2, Edge());\n pos.clear();\n }\n friend ostream& operator << (ostream& s, const TwoVariableSubmodularOpt &G) {\n const auto &edges = G.get_edges();\n for (const auto &e : edges) s << e << endl;\n return s;\n }\n\n // add 1-Variable submodular functioin\n void add_single_cost(int xi, COST false_cost, COST true_cost) {\n assert(0 <= xi && xi < N);\n if (false_cost >= true_cost) {\n OFFSET += true_cost;\n add_edge(S, xi, false_cost - true_cost);\n } else {\n OFFSET += false_cost;\n add_edge(xi, T, true_cost - false_cost);\n }\n }\n \n // add \"project selection\" constraint (xi = T, xj = F is penalty C)\n void add_psp_penalty(int xi, int xj, COST cost) {\n assert(0 <= xi && xi < N);\n assert(0 <= xj && xj < N);\n assert(cost >= 0);\n add_edge(xi, xj, cost);\n }\n \n // add general 2-variable submodular function\n // (xi, xj) = (F, F): A, (F, T): B\n // (xi, xj) = (T, F): C, (T, T): D\n void add_submodular_function(int xi, int xj, COST A, COST B, COST C, COST D) {\n assert(0 <= xi && xi < N);\n assert(0 <= xj && xj < N);\n assert(B + C >= A + D); // assure submodular function\n OFFSET += A;\n add_single_cost(xi, 0, D - B);\n add_single_cost(xj, 0, B - A);\n add_psp_penalty(xi, xj, B + C - A - D);\n }\n \n // add all True profit\n void add_all_true_profit(const vector<int> &xs, COST profit) {\n assert(profit >= 0);\n int slack = (int)list.size();\n list.resize(slack + 1);\n OFFSET -= profit;\n add_edge(S, slack, profit);\n for (auto xi : xs) {\n assert(xi >= 0 && xi < N);\n add_edge(slack, xi, INF);\n }\n }\n \n // solve\n COST solve() {\n return dinic() + OFFSET;\n }\n vector<bool> reconstruct() {\n vector<bool> res(N, false), seen(list.size(), false);\n queue<int> que;\n seen[S] = true;\n que.push(S);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (const auto &e : list[v]) {\n if (e.cap && !seen[e.to]) {\n if (e.to < N) res[e.to] = true;\n seen[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return res;\n }\n \nprivate:\n // inner data\n int N, S, T;\n COST OFFSET, INF;\n vector<vector<Edge>> list;\n vector<pair<int,int>> pos;\n \n // add edge\n Edge &get_rev_edge(const Edge &e) {\n if (e.from != e.to) return list[e.to][e.rev];\n else return list[e.to][e.rev + 1];\n }\n Edge &get_edge(int i) {\n return list[pos[i].first][pos[i].second];\n }\n const Edge &get_edge(int i) const {\n return list[pos[i].first][pos[i].second];\n }\n vector<Edge> get_edges() const {\n vector<Edge> edges;\n for (int i = 0; i < (int)pos.size(); ++i) {\n edges.push_back(get_edge(i));\n }\n return edges;\n }\n void add_edge(int from, int to, COST cap) {\n if (!cap) return;\n pos.emplace_back(from, (int)list[from].size());\n list[from].push_back(Edge((int)list[to].size(), from, to, cap));\n list[to].push_back(Edge((int)list[from].size() - 1, to, from, 0));\n }\n \n // Dinic's algorithm\n COST dinic(COST limit_flow) {\n COST current_flow = 0;\n vector<int> level((int)list.size(), -1), iter((int)list.size(), 0);\n \n // Dinic BFS\n auto bfs = [&]() -> void {\n level.assign((int)list.size(), -1);\n level[S] = 0;\n queue<int> que;\n que.push(S);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (const Edge &e : list[v]) {\n if (level[e.to] < 0 && e.cap > 0) {\n level[e.to] = level[v] + 1;\n if (e.to == T) return;\n que.push(e.to);\n }\n }\n }\n };\n \n // Dinic DFS\n auto dfs = [&](auto self, int v, COST up_flow) {\n if (v == T) return up_flow;\n COST res_flow = 0;\n for (int &i = iter[v]; i < (int)list[v].size(); ++i) {\n Edge &e = list[v][i], &re = get_rev_edge(e);\n if (level[v] >= level[e.to] || e.cap == 0) continue;\n COST flow = self(self, e.to, min(up_flow - res_flow, e.cap));\n if (flow <= 0) continue;\n res_flow += flow;\n e.cap -= flow, e.flow += flow;\n re.cap += flow, re.flow -= flow;\n if (res_flow == up_flow) break;\n }\n return res_flow;\n };\n \n // flow\n while (current_flow < limit_flow) {\n bfs();\n if (level[T] < 0) break;\n iter.assign((int)iter.size(), 0);\n while (current_flow < limit_flow) {\n COST flow = dfs(dfs, S, limit_flow - current_flow);\n if (!flow) break;\n current_flow += flow;\n }\n }\n return current_flow;\n };\n COST dinic() {\n return dinic(numeric_limits<COST>::max());\n }\n};\n\nll calc(vvvl G,vector<vector<char>> A,int n,int M){\n vvl dp(M,vl(1<<n,0));\n rep(i,1<<n){\n bool ok = true;\n //iあえて塗らない集合\n rep(j,n){\n if((i & (1<<j)) > 0 && A[j][0] == '#'){\n ok = false;\n break;\n }\n }\n if(ok) dp[0][i] = 1;\n }\n rep(i,n){\n int bit = 1<<i;\n rep(j,1<<n){\n if((j & bit) == 0){\n dp[0][j] += dp[0][j | bit];\n dp[0][j] %= MOD;\n }\n }\n }\n rep2(i,1,M){\n rep(j,1<<n){\n bool ok = true;\n rep(k,n){\n if((j & (1<<k)) > 0 && A[k][i] == '#'){\n ok = false;\n break;\n }\n }\n if(!ok) continue;\n ll t = 0;\n rep(k,n){\n if((j & (1<<k)) == 0 && A[k][i] == '.'){\n rep(l,G[k][i].size()){\n t |= 1<<G[k][i][l];\n }\n }\n }\n //tは塗っちゃいけない集合\n dp[i][j] = dp[i-1][t];\n }\n if(i != M-1){\n rep(j,n){\n int bit = 1<<j;\n rep(k,1<<n){\n if((k & bit) == 0){\n dp[i][k] += dp[i][k | bit];\n dp[i][k] %= MOD;\n }\n }\n }\n }\n }\n ll res = 0;\n rep(i,1<<n) res += dp[M-1][i];\n res %= MOD;\n return res;\n}\n\nint main(){\n for(;;){\n int N,M;\n cin >> N >> M;\n if(N == 0) break;\n vector<string> a(N);\n rep(i,N) cin >> a[i];\n int n = (N+3)/4;\n ll ans = 1;\n rep(i,4){\n vector<vector<char>> A(n,vector<char>(M,'#'));\n rep(j,n){\n rep(k,M){\n if(k%2 == 0){\n int inow = i+4*j;\n if(inow < N) A[j][k] = a[inow][k];\n }\n else if(i<2){\n int inow = 2+i+4*j;\n if(inow < N) A[j][k] = a[inow][k];\n }\n else{\n int inow = -2+i+4*j;\n if(inow < N) A[j][k] = a[inow][k];\n }\n }\n }\n vvvl G(n,vvl(M));\n rep2(j,1,M){\n rep(k,n){\n if(j%2 == 1){\n if(i < 2){\n int nowi = 2+i+4*k;\n if(nowi < 0 || nowi >= N) continue;\n if(a[nowi][j] == '#') continue;\n if(nowi-2 >= 0 && a[nowi-2][j-1] == '.') G[k][j].push_back(k);\n if(nowi+2 < N && k+1 < n && a[nowi+2][j-1] == '.') G[k][j].push_back(k+1);\n }\n else{\n int nowi = -2+i+4*k;\n if(nowi < 0 || nowi >= N) continue;\n if(a[nowi][j] == '#') continue;\n if(nowi+2 < N && a[nowi+2][j-1] == '.') G[k][j].push_back(k);\n if(nowi-2 >= 0 && k >= 1 && a[nowi-2][j-1] == '.') G[k][j].push_back(k-1);\n }\n }\n else{\n if(i >= 2){\n int nowi = i+4*k;\n if(nowi < 0 || nowi >= N) continue;\n if(a[nowi][j] == '#') continue;\n if(nowi-2 >= 0 && a[nowi-2][j-1] == '.') G[k][j].push_back(k);\n if(nowi+2 < N && k+1 < n && a[nowi+2][j-1] == '.') G[k][j].push_back(k+1);\n }\n else{\n int nowi = i+4*k;\n if(nowi < 0 || nowi >= N) continue;\n if(a[nowi][j] == '#') continue;\n if(nowi+2 < N && a[nowi+2][j-1] == '.') G[k][j].push_back(k);\n if(nowi-2 >= 0 && k >= 1 && a[nowi-2][j-1] == '.') G[k][j].push_back(k-1);\n }\n }\n }\n }\n ll p = calc(G,A,n,M);\n ans *= p;\n ans %= MOD;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 3100, "memory_kb": 18760, "score_of_the_acc": -0.7023, "final_rank": 13 }, { "submission_id": "aoj_1644_9369422", "code_snippet": "#include <utility>\n\nnamespace atcoder {\n\nnamespace internal {\n\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n unsigned int umod() const { return _m; }\n\n unsigned int mul(unsigned int a, unsigned int b) const {\n\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <algorithm>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n}\n\nint bsf(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n public:\n segtree() : segtree(0) {}\n segtree(int n) : segtree(std::vector<S>(n, e())) {}\n segtree(const std::vector<S>& v) : _n(int(v.size())) {\n log = internal::ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n\n} // namespace atcoder\n\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <math.h>\n#include <iomanip>\n#include <functional>\n#include<unordered_map>\n#include <random>\n#include<bitset>\nusing namespace std; \nusing namespace atcoder;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n//#define endl '\\n'\n#define all(x) (x).begin(),(x).end()\n#define arr(x) (x).rbegin(),(x).rend()\ntypedef long long ll;\ntypedef unsigned long long ull;\nconst ll inf=4e18; \nusing graph = vector<vector<int> > ;\nusing vi=vector<int>;\nusing P= pair<ll,ll>; \nusing vvi=vector<vi>;\nusing vvvi=vector<vvi>;\nusing vll=vector<ll>; \nusing vvll=vector<vll>;\nusing vp=vector<P>;\nusing vvp=vector<vp>;\nusing vd=vector<double>;\nusing vvd =vector<vd>;\n//using vs=vector<string>;\n//string T=\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n//string S=\"abcdefghijklmnopqrstuvwxyz\";\n//g++ main.cpp -std=c++17 -I . \n//cout <<setprecision(20);\n//cout << fixed << setprecision(10);\n//cin.tie(0); ios::sync_with_stdio(false);\n\nconst double PI = acos(-1);\nint vx[]={0,1,0,-1,-1,1,1,-1},vy[]={1,0,-1,0,1,1,-1,-1};\nvoid putsYes(bool f){cout << (f?\"Yes\":\"No\") << endl;}\nvoid putsYES(bool f){cout << (f?\"YES\":\"NO\") << endl;}\nvoid putsFirst(bool f){cout << (f?\"First\":\"Second\") << endl;}\nvoid debug(int test){cout << \"TEST\" << \" \" << test << endl;}\nll pow_pow(ll x,ll n,ll mod){\n if(n==0) return 1; \n x%=mod;\n ll res=pow_pow(x*x%mod,n/2,mod);\n if(n&1)res=res*x%mod;\n return res;\n}\nll gcdll(ll x,ll y){\n if(y==0)return x;\n return gcdll(y,x%y);\n}\nll INFF=1000100100100100100;\nll lcmll(ll x,ll y){\n ll X=(x/gcdll(x,y)),Y=y;\n if(X<=INFF/Y)return X*Y;\n else return INFF;\n}\ntemplate<class T> bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n else return false;\n}\ntemplate<class T> bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n else return false;\n}\nstruct edge{\n int to; ll cost;int t;\n edge(int to,ll cost,int t) : to(to),cost(cost),t(t){}\n};\nusing ve=vector<edge>;\nusing vve=vector<ve>;\nusing mint = modint;\n//using mint = modint1000000007;\nusing vm=vector<mint>;\nusing vvm=vector<vm>;\nusing vvvm=vector<vvm>;\nint mod = 1e9+9;\n//int mod=1e9+7;\nconstexpr int MAX =10;\nll fact[MAX],finv[MAX],inv[MAX];\nvoid initcomb(){\n fact[0]=fact[1]=1;\n finv[0]=finv[1]=1;\n inv[1]=1;\n for(int i=2;i<MAX;i++){\n fact[i]=fact[i-1]*i%mod;\n inv[i]=mod-inv[mod%i]*(mod/i)%mod;\n finv[i]=finv[i-1]*inv[i]%mod;\n }\n}\nll comb(ll n,ll k){\n if(n<k) return 0;\n if(n<0||k<0) return 0;\n return fact[n]*(finv[k]*finv[n-k]%mod)%mod;\n}\n\nstruct UnionFind { \n vector<int> par, siz;\n UnionFind(int n) : par(n, -1) , siz(n, 1) { } \n int root(int x) { \n if (par[x] == -1) return x;\n else return par[x] = root(par[x]);\n }\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false; \n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n return true;\n }\n int size(int x) {\n return siz[root(x)];\n }\n};\nstruct Compress{\n vll a;\n int cnt;\n Compress() :cnt(0) {}\n void add(ll x){a.push_back(x);}\n void init(){\n sort(all(a));\n a.erase(unique(all(a)),a.end());\n cnt=a.size();\n }\n ll to(ll x){\n int index=lower_bound(all(a),x)-a.begin();\n return index;\n } //変換先\n ll from(int x){return a[x];}//逆引き\n int size(){return cnt;}\n};\nvll anss;\nint EXIT=0;\nvoid solve(int test){\n int n,m; cin >> n >> m;\n int h=n,w=m;\n if(n==0 && m==0){\n EXIT=1;\n return;\n }\n vector<string> s(n);\n rep(i,n)cin >> s[i];\n mint ans=1;\n UnionFind uf(n*m);\n rep(i,n)rep(j,m){\n if(i-2>=0 && j-1>=0){\n uf.unite(i*w+j,(i-2)*w+j-1);\n }\n if(i-2>=0 && j+1<m){\n uf.unite(i*w+j,(i-2)*w+j+1);\n }\n }\n vvi id(n,vi(m));\n vvi seen(n,vi(m));\n rep(sx,n)rep(sy,m){\n if(seen[sx][sy])continue;\n vm dp;\n int presi=0;\n rep(j,m){\n int si=0;\n vi vs;\n rep(i,n){\n if(uf.issame(sx*w+sy,i*w+j)){\n id[i][j]=si++;\n seen[i][j]=1;\n vs.push_back(i);\n }\n }\n if(j==0){\n vm next(1<<si);\n rep(bit,1<<si){\n int ng=0;\n rep(k,si)if(bit>>k&1){\n int i=vs[k];\n if(s[i][j]=='#'){ng=1;break;}\n }\n if(!ng)next[bit]=1;\n }\n swap(next,dp);\n }\n else {\n rep(i,presi)for(int bit=dp.size()-1; bit>=0; bit--)if(bit>>i&1){\n dp[bit]+=dp[bit-(1<<i)];\n }\n vm next(1<<si);\n rep(bit,1<<si){\n int nbit=dp.size()-1;\n int ng=0;\n rep(k,si)if(bit>>k&1){\n int i=vs[k];\n if(s[i][j]=='#'){ng=1;break;}\n if(i-2>=0 && j-1>=0){\n nbit&=((1<<presi)-1-(1<<id[i-2][j-1]));\n }\n if(i+2<n && j-1>=0){\n nbit&=((1<<presi)-1-(1<<id[i+2][j-1]));\n }\n }\n if(!ng)next[bit]=dp[nbit];\n else next[bit]=0;\n }\n swap(next,dp);\n }\n presi=si;\n }\n mint tot=0;\n rep(bit,dp.size())tot+=dp[bit];\n ans*=tot;\n }\n cout << ans.val() << endl;\n}\n//g++ cf.cpp -std=c++17 -I .\nint main(){cin.tie(0);ios::sync_with_stdio(false);\n {\n mod=1e9+7;\n initcomb();\n modint::set_mod(mod);\n }\n for(int test=0;; test++){\n solve(test);\n if(EXIT)break;\n }\n for(auto ans:anss){\n cout<< ans << endl;\n }\n}", "accuracy": 1, "time_ms": 2080, "memory_kb": 3584, "score_of_the_acc": -0.2497, "final_rank": 3 }, { "submission_id": "aoj_1644_9368531", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1000000000;\nconstexpr int64_t llINF = 3000000000000000000;\nconstexpr double eps = 1e-10;\nconst double pi = acos(-1);\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nll modpow(ll a, ll b, ll m) {\n ll res = 1;\n while (b) {\n if (b & 1) {\n res *= a;\n res %= m;\n }\n a *= a;\n a %= m;\n b >>= 1;\n }\n return res;\n}\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nstruct Matrix {\n int n;\n vector<vector<T>> m;\n Matrix() : Matrix(0) {}\n Matrix(int x) : Matrix(vector<vector<T>>(x, vector<T>(x, 0))) {}\n Matrix(const vector<vector<T>> &a) {\n n = a.size();\n m = a;\n }\n vector<T> &operator[](int i) { return m[i]; }\n const vector<T> &operator[](int i) const { return m[i]; }\n static Matrix identity(int x) {\n Matrix res(x);\n for (int i = 0; i < x; i++) res[i][i] = 1;\n return res;\n }\n Matrix operator+(const Matrix &a) const {\n Matrix x = (*this);\n return x += a;\n }\n Matrix operator*(const Matrix &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix &operator+=(const Matrix &a) {\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n res[i][j] = m[i][j] + a[i][j];\n }\n }\n m = res.m;\n return *this;\n }\n Matrix &operator*=(const Matrix &a) {\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n res[i][j] += m[i][k] * a[k][j];\n }\n }\n }\n m = res.m;\n return *this;\n }\n Matrix pow(ll b) const {\n Matrix x = *this, res = identity(n);\n while (b) {\n if (b & 1) {\n res *= x;\n }\n x *= x;\n b >>= 1;\n }\n return res;\n }\n};\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct segtree {\n int siz = 1;\n vector<S> dat;\n segtree(int n) : segtree(vector<S>(n, e())) {}\n segtree(const vector<S> &a) {\n while (siz < a.size()) siz <<= 1;\n dat = vector<S>(siz << 1, e());\n for (int i = 0; i < a.size(); i++) dat[siz + i] = a[i];\n for (int i = siz - 1; i >= 1; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);\n }\n void set(int p, S x) {\n p += siz;\n dat[p] = x;\n while (p > 0) {\n p >>= 1;\n dat[p] = op(dat[2 * p], dat[2 * p + 1]);\n }\n }\n void add(int p, S x) { set(p, get(p) + x); }\n S get(int p) { return dat[p + siz]; }\n S prod(int l, int r) {\n S vl = e(), vr = e();\n l += siz, r += siz;\n while (l < r) {\n if (l & 1) vl = op(vl, dat[l++]);\n if (r & 1) vr = op(dat[--r], vr);\n l >>= 1, r >>= 1;\n }\n return op(vl, vr);\n }\n S all_prod() { return dat[1]; }\n};\n\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct sparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n sparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n for (int j = 0; j + (1 << i) <= (1 << len); j++) {\n table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);\n }\n }\n logtable.resize(v.size() + 1);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n assert(l <= r);\n if (l == r) return e();\n int len = logtable[r - l];\n return op(table[len][l], table[len][r - (1 << len)]);\n }\n};\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct disjointsparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n disjointsparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len, e()));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n int shift = 1 << i;\n for (int j = 0; j < (int)v.size(); j += shift << 1) {\n int t = min(j + shift, (int)v.size());\n table[i][t - 1] = v[t - 1];\n for (int k = t - 2; k >= j; k--) table[i][k] = op(v[k], table[i][k + 1]);\n if (v.size() <= t) break;\n table[i][t] = v[t];\n int r = min(t + shift, (int)v.size());\n for (int k = t + 1; k < r; k++) table[i][k] = op(table[i][k - 1], v[k]);\n }\n }\n logtable.resize(1 << len);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n if (l == r) return e();\n if (l >= --r) return table[0][l];\n int len = logtable[l ^ r];\n return op(table[len][l], table[len][r]);\n };\n};\nstruct lca_tree {\n int n, size;\n vector<vector<int>> par;\n vector<int> depth;\n lca_tree(vector<vector<int>> g, int root = 0) : n((int)g.size()), size(log2(n) + 2), par(size, vector<int>(n, -1)), depth(vector<int>(n, n)) {\n queue<int> que;\n depth[root] = 0;\n que.push(root);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n for (int i : g[p]) {\n if (depth[i] > depth[p] + 1) {\n depth[i] = depth[p] + 1;\n par[0][i] = p;\n que.push(i);\n }\n }\n }\n for (int k = 0; k < size - 1; k++) {\n for (int i = 0; i < n; i++) {\n if (par[k][i] == -1)\n par[k + 1][i] = -1;\n else\n par[k + 1][i] = par[k][par[k][i]];\n }\n }\n }\n int query(int u, int v) {\n if (depth[u] > depth[v]) swap(u, v);\n for (int k = size - 1; k >= 0; k--) {\n if (((depth[v] - depth[u]) >> k) & 1) v = par[k][v];\n if (u == v) return u;\n }\n for (int k = size - 1; k >= 0; k--) {\n if (par[k][u] != par[k][v]) {\n u = par[k][u];\n v = par[k][v];\n }\n }\n return par[0][u];\n }\n int dist(int u, int v) {\n int l = query(u, v);\n return depth[u] + depth[v] - 2 * depth[l];\n }\n};\nstruct UnionFind {\n vector<int> par, siz, es;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n es.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n es[i] = 0;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) {\n es[x]++;\n return false;\n }\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n es[x] += es[y] + 1;\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n int edges(int x) { return es[find(x)]; }\n};\nstruct Mo {\n int n;\n vector<pair<int, int>> lr;\n\n explicit Mo(int n) : n(n) {}\n\n void add(int l, int r) { /* [l, r) */ lr.emplace_back(l, r); }\n\n template <typename AL, typename AR, typename EL, typename ER, typename O>\n void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out) {\n int q = (int)lr.size();\n int bs = n / min<int>(n, sqrt(q));\n vector<int> ord(q);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n int ablock = lr[a].first / bs, bblock = lr[b].first / bs;\n if (ablock != bblock) return ablock < bblock;\n return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second;\n });\n int l = 0, r = 0;\n for (auto idx : ord) {\n while (l > lr[idx].first) add_left(--l);\n while (r < lr[idx].second) add_right(r++);\n while (l < lr[idx].first) erase_left(l++);\n while (r > lr[idx].second) erase_right(--r);\n out(idx);\n }\n }\n\n template <typename A, typename E, typename O>\n void build(const A &add, const E &erase, const O &out) {\n build(add, add, erase, erase, out);\n }\n};\ntemplate <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>\nstruct lazy_segtree {\n int _n, sz = 1;\n vector<S> dat;\n vector<F> lazy;\n lazy_segtree(vector<S> a) : _n(int(a.size())) {\n while (sz < _n) sz <<= 1;\n dat.resize(sz * 2, e());\n lazy.resize(sz * 2, id());\n rep(i, _n) dat[sz + i] = a[i];\n for (int i = sz - 1; i >= 1; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);\n }\n void eval(int k) {\n dat[k] = mapping(lazy[k], dat[k]);\n if (k < sz) {\n lazy[k * 2] = composition(lazy[k], lazy[k * 2]);\n lazy[k * 2 + 1] = composition(lazy[k], lazy[k * 2 + 1]);\n }\n lazy[k] = id();\n }\n void apply(int a, int b, F f, int k = 1, int l = 0, int r = -1) {\n eval(k);\n if (r == -1) r = sz;\n if (r <= a || b <= l) return;\n if (a <= l && r <= b) {\n lazy[k] = composition(f, lazy[k]);\n eval(k);\n return;\n }\n int m = (l + r) >> 1;\n apply(a, b, f, 2 * k, l, m);\n apply(a, b, f, 2 * k + 1, m, r);\n dat[k] = op(dat[2 * k], dat[2 * k + 1]);\n }\n S prod(int a, int b, int k = 1, int l = 0, int r = -1) {\n eval(k);\n if (r == -1) r = sz;\n if (r <= a || b <= l) return e();\n if (a <= l && r <= b) return dat[k];\n int m = (l + r) >> 1;\n S vl = prod(a, b, 2 * k, l, m);\n S vr = prod(a, b, 2 * k + 1, m, r);\n return op(vl, vr);\n }\n};\n\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct dual_segtree {\n int sz = 1, log = 0;\n vector<S> lz;\n dual_segtree(vector<S> a) {\n int n = a.size();\n while (sz < n) {\n sz <<= 1;\n log++;\n }\n lz.assign(sz << 1, e());\n for (int i = 0; i < n; i++) lz[i + sz] = a[i];\n }\n void push(int k) {\n int b = __builtin_ctz(k);\n for (int d = log; d > b; d--) {\n lz[k >> d << 1] = op(lz[k >> d << 1], lz[k >> d]);\n lz[k >> d << 1 | 1] = op(lz[k >> d << 1 | 1], lz[k >> d]);\n lz[k >> d] = e();\n }\n }\n void apply(int l, int r, S x) {\n l += sz, r += sz;\n push(l);\n push(r);\n while (l < r) {\n if (l & 1) {\n lz[l] = op(lz[l], x);\n l++;\n }\n if (r & 1) {\n r--;\n lz[r] = op(lz[r], x);\n }\n l >>= 1, r >>= 1;\n }\n }\n S get(int k) {\n k += sz;\n S res = e();\n while (k) {\n res = op(res, lz[k]);\n k >>= 1;\n }\n return res;\n }\n};\nll modinv(int x, int modulo) {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return (u >= 0 ? u % modulo : (modulo - (-u) % modulo) % modulo);\n}\nstruct auxiliary_tree : lca_tree {\n vector<int> in;\n vector<vector<int>> G;\n auxiliary_tree(vector<vector<int>> &g) : lca_tree(g), in((int)g.size()), G((int)g.size()) {\n int t = 0;\n dfs(0, -1, t, g);\n }\n void dfs(int v, int p, int &t, vector<vector<int>> &g) {\n in[v] = t++;\n for (auto u : g[v]) {\n if (u != p) dfs(u, v, t, g);\n }\n }\n using lca_tree::depth;\n pair<int, vector<int>> query(vector<int> vs, bool decending = false) {\n // decending: only parent to child\n assert(!vs.empty());\n sort(vs.begin(), vs.end(), [&](int a, int b) { return in[a] < in[b]; });\n int m = vs.size();\n stack<int> st;\n st.push(vs[0]);\n for (int i = 0; i < m - 1; i++) {\n int w = lca_tree::query(vs[i], vs[i + 1]);\n if (w != vs[i]) {\n int l = st.top();\n st.pop();\n while (!st.empty() && depth[w] < depth[st.top()]) {\n if (!decending) G[l].push_back(st.top());\n G[st.top()].push_back(l);\n l = st.top();\n st.pop();\n }\n if (st.empty() || st.top() != w) {\n st.push(w);\n vs.push_back(w);\n }\n if (!decending) G[l].push_back(w);\n G[w].push_back(l);\n }\n st.push(vs[i + 1]);\n }\n while (st.size() > 1) {\n int x = st.top();\n st.pop();\n if (!decending) G[x].push_back(st.top());\n G[st.top()].push_back(x);\n }\n // {root,vertex_list}\n return make_pair(st.top(), vs);\n }\n void clear(vector<int> vs) {\n for (int v : vs) G[v].clear();\n }\n};\n/*\n#include <atcoder/convolution>\ntemplate <int T>\ninline std::ostream &operator<<(std::ostream &os, const atcoder::static_modint<T> &p) {\n return os << p.val();\n}\ntemplate <int T>\ninline std::istream &operator>>(std::istream &is, atcoder::static_modint<T> &a) {\n int64_t t;\n is >> t;\n a = t;\n return is;\n}\n\ntemplate <typename mint>\nstruct FormalPowerSeries : vector<mint> {\n using vector<mint>::vector;\n using FPS = FormalPowerSeries;\n FPS &operator+=(const FPS &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i];\n return *this;\n }\n FPS &operator+=(const mint &r) {\n if (this->empty()) this->resize(1);\n (*this)[0] += r;\n return *this;\n }\n\n FPS &operator-=(const FPS &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i];\n return *this;\n }\n\n FPS &operator-=(const mint &r) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= r;\n return *this;\n }\n FPS &operator*=(const FPS &r) {\n if (this->empty() || r.empty()) {\n this->clear();\n return *this;\n }\n assert(mint::mod() == 998244353);\n vector<mint> prod = atcoder::convolution(*this, r);\n this->resize((int)prod.size());\n for (int i = 0; i < (int)this->size(); i++) (*this)[i] = prod[i];\n return *this;\n }\n FPS &operator*=(const mint &v) {\n for (int i = 0; i < (int)this->size(); i++) (*this)[i] *= v;\n return *this;\n }\n FPS &operator/=(const FPS &r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int n = this->size() - r.size() + 1;\n return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev();\n }\n FPS &operator%=(const FPS &r) {\n *this -= *this / r * r;\n shrink();\n return *this;\n }\n FPS operator+(const FPS &r) const { return FPS(*this) += r; }\n FPS operator+(const mint &v) const { return FPS(*this) += v; }\n FPS operator-(const FPS &r) const { return FPS(*this) -= r; }\n FPS operator-(const mint &v) const { return FPS(*this) -= v; }\n FPS operator*(const FPS &r) const { return FPS(*this) *= r; }\n FPS operator*(const mint &v) const { return FPS(*this) *= v; }\n FPS operator/(const FPS &r) const { return FPS(*this) /= r; }\n FPS operator%(const FPS &r) const { return FPS(*this) %= r; }\n FPS operator-() const {\n FPS ret(this->size());\n for (int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i];\n return ret;\n }\n void shrink() {\n while (this->size() && this->back() == mint(0)) this->pop_back();\n }\n FPS operator>>(int sz) const {\n if ((int)this->size() <= sz) return {};\n FPS ret(*this);\n ret.erase(ret.begin(), ret.begin() + sz);\n return ret;\n }\n FPS operator<<(int sz) const {\n FPS ret(*this);\n ret.insert(ret.begin(), sz, mint(0));\n return ret;\n }\n FPS pre(int sz) const { return FPS(begin(*this), begin(*this) + min((int)this->size(), sz)); }\n FPS rev() const {\n FPS ret(*this);\n reverse(begin(ret), end(ret));\n return ret;\n }\n FPS diff() const {\n const int n = this->size();\n FPS ret(max(0, n - 1));\n for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * mint(i);\n return ret;\n }\n FPS integral() const {\n const int n = this->size();\n FPS ret(n + 1);\n ret[0] = mint(0);\n if (n > 0) ret[1] = mint(1);\n auto mod = mint::mod();\n for (int i = 2; i <= n; i++) ret[i] = (-ret[mod % i] * (mod / i));\n for (int i = 0; i < n; i++) ret[i + 1] *= (*this)[i];\n return ret;\n }\n FPS inv(int deg = -1) const {\n assert(((*this)[0]) != mint(0));\n const int n = this->size();\n if (deg == -1) deg = n;\n FPS ret({mint(1) / (*this)[0]});\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1);\n }\n return ret.pre(deg);\n }\n FPS log(int deg = -1) {\n assert((*this)[0] == mint(1));\n if (deg == -1) deg = this->size();\n return (this->diff() * this->inv(deg)).pre(deg - 1).integral();\n }\n FPS exp(int deg = -1) const {\n assert((*this)[0] == mint(0));\n const int n = this->size();\n if (deg == -1) deg = n;\n FPS ret({mint(1)});\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret * (pre(i << 1) + mint(1) - ret.log(i << 1))).pre(i << 1);\n }\n return ret.pre(deg);\n }\n FPS pow(int64_t k, int deg = -1) const {\n const int n = this->size();\n if (deg == -1) deg = n;\n if (k == 0) {\n FPS ret(deg);\n if (deg) ret[0] = 1;\n return ret;\n }\n for (int i = 0; i < n; i++) {\n if ((*this)[i] != mint(0)) {\n mint rev = mint(1) / (*this)[i];\n FPS ret = (((*this * rev) >> i).log(deg) * k).exp(deg);\n ret *= (*this)[i].pow(k);\n ret = (ret << (i * k)).pre(deg);\n if ((int)ret.size() < deg) ret.resize(deg, mint(0));\n return ret;\n }\n if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));\n }\n return FPS(deg, mint(0));\n }\n FPS sqrt(int deg = -1) const {\n const int n = this->size();\n if (deg == -1) deg = n;\n if (n == 0) return FPS(deg, 0);\n if ((*this)[0] == mint(0)) {\n for (int i = 1; i < n; i++) {\n if ((*this)[i] != mint(0)) {\n if (i & 1) return {};\n if (deg - i / 2 <= 0) break;\n auto ret = (*this >> i).sqrt(deg - i / 2);\n if (ret.empty()) return {};\n ret = ret << (i / 2);\n if ((int)ret.size() < deg) ret.resize(deg, mint(0));\n return ret;\n }\n }\n return FPS(deg, 0);\n }\n int64_t sqr = mod_sqrt((*this)[0].val(), mint::mod());\n if (sqr == -1) return {};\n assert(sqr * sqr % mint::mod() == (*this)[0].val());\n FPS ret({mint(sqr)});\n mint inv2 = mint(2).inv();\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2;\n }\n return ret.pre(deg);\n }\n mint eval(mint x) const {\n mint r = 0, w = 1;\n for (auto &v : *this) r += w * v, w *= x;\n return r;\n }\n};\n// calculate f(x + a)\ntemplate <typename mint>\nFormalPowerSeries<mint> TaylorShift(FormalPowerSeries<mint> f, mint a, Binomial<mint> &bin) {\n int n = f.size();\n for (int i = 0; i < n; i++) f[i] *= bin.fact[i];\n f = f.rev();\n FormalPowerSeries<mint> g(n, mint(1));\n for (int i = 1; i < n; i++) g[i] = g[i - 1] * a * bin.inv[i];\n f = (f * g).pre(n);\n f = f.rev();\n for (int i = 0; i < n; i++) f[i] *= bin.factinv[i];\n return f;\n}\ntemplate <typename mint>\nFormalPowerSeries<mint> FPS_Product(vector<FormalPowerSeries<mint>> f) {\n int n = (int)f.size();\n if (n == 0) return {1};\n function<FormalPowerSeries<mint>(int, int)> calc = [&](int l, int r) {\n if (r - l == 1) return f[l];\n int m = (l + r) / 2;\n return calc(l, m) * calc(m, r);\n };\n return calc(0, n);\n}\n#include <atcoder/string>\nint string_period(string s) {\n int n = s.size();\n auto z = atcoder::z_algorithm(s);\n for (int i = 1; i < n; i++) {\n if (n % i == 0 && z[i] == n - i) return i;\n }\n return n;\n}\n*/\nstruct LowLink {\n vector<vector<int>> g;\n vector<int> ord, low, out;\n vector<bool> used;\n vector<pair<int, int>> bridge;\n vector<pair<int, int>> articulation;\n int unions;\n LowLink(vector<vector<int>> g) : g(g) {\n int n = (int)g.size();\n ord.resize(n);\n low.resize(n);\n out.resize(n);\n used.resize(n);\n unions = 0;\n int t = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n dfs(i, t, -1);\n unions++;\n }\n }\n }\n void dfs(int v, int &t, int par) {\n used[v] = true;\n ord[v] = t++, low[v] = ord[v];\n int cnt = 0;\n bool par_back = false;\n for (int to : g[v]) {\n if (!used[to]) {\n dfs(to, t, v);\n low[v] = min(low[v], low[to]);\n if (ord[v] < low[to]) bridge.push_back(minmax(v, to));\n if (ord[v] <= low[to]) cnt++;\n } else if (to != par || par_back) {\n low[v] = min(low[v], ord[to]);\n } else\n par_back = true;\n }\n if (par != -1) cnt++;\n if (cnt >= 2) articulation.push_back({v, cnt});\n out[v] = t;\n }\n};\nvoid solve(int n, int m) {\n using mint = modint<1000000007>;\n vector<string> s(n);\n rep(i, n) cin >> s[i];\n // ほうじょ?\n mint ans = 1;\n rep(d, 4) {\n vector<int> u, v;\n rep(i, n) {\n if (i % 4 == d) u.push_back(i);\n if ((i + 2) % 4 == d) v.push_back(i);\n }\n int k = v.size();\n vector<mint> dp(1 << k, 0);\n dp[0] = 1;\n rep(i, m) {\n auto ref = (i % 2 == 0 ? v : u);\n auto nxt = (i % 2 == 0 ? u : v);\n vector<mint> tmp(1 << (nxt.size()), 0);\n rep(S, dp.size()) {\n if (dp[S] == 0) continue;\n vector<bool> ng(n);\n rep(p, ref.size()) {\n if ((S >> p) & 1) {\n if (ref[p] + 2 < n) ng[ref[p] + 2] = true;\n if (ref[p] >= 2) ng[ref[p] - 2] = true;\n }\n }\n int T = 0;\n int t = 0;\n for (int j : nxt) {\n if (s[j][i] == '.' && !ng[j]) T |= 1 << t;\n t++;\n }\n tmp[T] += dp[S];\n }\n\n swap(dp, tmp);\n rep(j, nxt.size()) {\n rep(S, dp.size()) {\n if ((S >> j) & 1) {\n dp[S & ~(1 << j)] += dp[S];\n }\n }\n }\n }\n mint sum = 0;\n for (auto x : dp) sum += x;\n ans *= sum;\n }\n cout << ans << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m, k, x, y;\n ll p;\n string s, t;\n while (cin >> n >> m) {\n if (n == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 2490, "memory_kb": 3508, "score_of_the_acc": -0.3128, "final_rank": 5 }, { "submission_id": "aoj_1644_9351742", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1000000000;\nconstexpr int64_t llINF = 3000000000000000000;\nconstexpr double eps = 1e-10;\nconst double pi = acos(-1);\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nll modpow(ll a, ll b, ll m) {\n ll res = 1;\n while (b) {\n if (b & 1) {\n res *= a;\n res %= m;\n }\n a *= a;\n a %= m;\n b >>= 1;\n }\n return res;\n}\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nstruct Matrix {\n int n;\n vector<vector<T>> m;\n Matrix() : Matrix(0) {}\n Matrix(int x) : Matrix(vector<vector<T>>(x, vector<T>(x, 0))) {}\n Matrix(const vector<vector<T>> &a) {\n n = a.size();\n m = a;\n }\n vector<T> &operator[](int i) { return m[i]; }\n const vector<T> &operator[](int i) const { return m[i]; }\n static Matrix identity(int x) {\n Matrix res(x);\n for (int i = 0; i < x; i++) res[i][i] = 1;\n return res;\n }\n Matrix operator+(const Matrix &a) const {\n Matrix x = (*this);\n return x += a;\n }\n Matrix operator*(const Matrix &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix &operator+=(const Matrix &a) {\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n res[i][j] = m[i][j] + a[i][j];\n }\n }\n m = res.m;\n return *this;\n }\n Matrix &operator*=(const Matrix &a) {\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n res[i][j] += m[i][k] * a[k][j];\n }\n }\n }\n m = res.m;\n return *this;\n }\n Matrix pow(ll b) const {\n Matrix x = *this, res = identity(n);\n while (b) {\n if (b & 1) {\n res *= x;\n }\n x *= x;\n b >>= 1;\n }\n return res;\n }\n};\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct segtree {\n int siz = 1;\n vector<S> dat;\n segtree(int n) : segtree(vector<S>(n, e())) {}\n segtree(const vector<S> &a) {\n while (siz < a.size()) siz <<= 1;\n dat = vector<S>(siz << 1, e());\n for (int i = 0; i < a.size(); i++) dat[siz + i] = a[i];\n for (int i = siz - 1; i >= 1; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);\n }\n void set(int p, S x) {\n p += siz;\n dat[p] = x;\n while (p > 0) {\n p >>= 1;\n dat[p] = op(dat[2 * p], dat[2 * p + 1]);\n }\n }\n void add(int p, S x) { set(p, get(p) + x); }\n S get(int p) { return dat[p + siz]; }\n S prod(int l, int r) {\n S vl = e(), vr = e();\n l += siz, r += siz;\n while (l < r) {\n if (l & 1) vl = op(vl, dat[l++]);\n if (r & 1) vr = op(dat[--r], vr);\n l >>= 1, r >>= 1;\n }\n return op(vl, vr);\n }\n S all_prod() { return dat[1]; }\n};\n\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct sparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n sparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n for (int j = 0; j + (1 << i) <= (1 << len); j++) {\n table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);\n }\n }\n logtable.resize(v.size() + 1);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n assert(l <= r);\n if (l == r) return e();\n int len = logtable[r - l];\n return op(table[len][l], table[len][r - (1 << len)]);\n }\n};\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct disjointsparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n disjointsparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len, e()));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n int shift = 1 << i;\n for (int j = 0; j < (int)v.size(); j += shift << 1) {\n int t = min(j + shift, (int)v.size());\n table[i][t - 1] = v[t - 1];\n for (int k = t - 2; k >= j; k--) table[i][k] = op(v[k], table[i][k + 1]);\n if (v.size() <= t) break;\n table[i][t] = v[t];\n int r = min(t + shift, (int)v.size());\n for (int k = t + 1; k < r; k++) table[i][k] = op(table[i][k - 1], v[k]);\n }\n }\n logtable.resize(1 << len);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n if (l == r) return e();\n if (l >= --r) return table[0][l];\n int len = logtable[l ^ r];\n return op(table[len][l], table[len][r]);\n };\n};\nstruct lca_tree {\n int n, size;\n vector<vector<int>> par;\n vector<int> depth;\n lca_tree(vector<vector<int>> g, int root = 0) : n((int)g.size()), size(log2(n) + 2), par(size, vector<int>(n, -1)), depth(vector<int>(n, n)) {\n queue<int> que;\n depth[root] = 0;\n que.push(root);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n for (int i : g[p]) {\n if (depth[i] > depth[p] + 1) {\n depth[i] = depth[p] + 1;\n par[0][i] = p;\n que.push(i);\n }\n }\n }\n for (int k = 0; k < size - 1; k++) {\n for (int i = 0; i < n; i++) {\n if (par[k][i] == -1)\n par[k + 1][i] = -1;\n else\n par[k + 1][i] = par[k][par[k][i]];\n }\n }\n }\n int query(int u, int v) {\n if (depth[u] > depth[v]) swap(u, v);\n for (int k = size - 1; k >= 0; k--) {\n if (((depth[v] - depth[u]) >> k) & 1) v = par[k][v];\n if (u == v) return u;\n }\n for (int k = size - 1; k >= 0; k--) {\n if (par[k][u] != par[k][v]) {\n u = par[k][u];\n v = par[k][v];\n }\n }\n return par[0][u];\n }\n int dist(int u, int v) {\n int l = query(u, v);\n return depth[u] + depth[v] - 2 * depth[l];\n }\n};\nstruct UnionFind {\n vector<int> par, siz, es;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n es.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n es[i] = 0;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) {\n es[x]++;\n return false;\n }\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n es[x] += es[y] + 1;\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n int edges(int x) { return es[find(x)]; }\n};\nstruct Mo {\n int n;\n vector<pair<int, int>> lr;\n\n explicit Mo(int n) : n(n) {}\n\n void add(int l, int r) { /* [l, r) */ lr.emplace_back(l, r); }\n\n template <typename AL, typename AR, typename EL, typename ER, typename O>\n void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out) {\n int q = (int)lr.size();\n int bs = n / min<int>(n, sqrt(q));\n vector<int> ord(q);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n int ablock = lr[a].first / bs, bblock = lr[b].first / bs;\n if (ablock != bblock) return ablock < bblock;\n return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second;\n });\n int l = 0, r = 0;\n for (auto idx : ord) {\n while (l > lr[idx].first) add_left(--l);\n while (r < lr[idx].second) add_right(r++);\n while (l < lr[idx].first) erase_left(l++);\n while (r > lr[idx].second) erase_right(--r);\n out(idx);\n }\n }\n\n template <typename A, typename E, typename O>\n void build(const A &add, const E &erase, const O &out) {\n build(add, add, erase, erase, out);\n }\n};\ntemplate <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>\nstruct lazy_segtree {\n int _n, sz = 1;\n vector<S> dat;\n vector<F> lazy;\n lazy_segtree(vector<S> a) : _n(int(a.size())) {\n while (sz < _n) sz <<= 1;\n dat.resize(sz * 2, e());\n lazy.resize(sz * 2, id());\n rep(i, _n) dat[sz + i] = a[i];\n for (int i = sz - 1; i >= 1; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);\n }\n void eval(int k) {\n dat[k] = mapping(lazy[k], dat[k]);\n if (k < sz) {\n lazy[k * 2] = composition(lazy[k], lazy[k * 2]);\n lazy[k * 2 + 1] = composition(lazy[k], lazy[k * 2 + 1]);\n }\n lazy[k] = id();\n }\n void apply(int a, int b, F f, int k = 1, int l = 0, int r = -1) {\n eval(k);\n if (r == -1) r = sz;\n if (r <= a || b <= l) return;\n if (a <= l && r <= b) {\n lazy[k] = composition(f, lazy[k]);\n eval(k);\n return;\n }\n int m = (l + r) >> 1;\n apply(a, b, f, 2 * k, l, m);\n apply(a, b, f, 2 * k + 1, m, r);\n dat[k] = op(dat[2 * k], dat[2 * k + 1]);\n }\n S prod(int a, int b, int k = 1, int l = 0, int r = -1) {\n eval(k);\n if (r == -1) r = sz;\n if (r <= a || b <= l) return e();\n if (a <= l && r <= b) return dat[k];\n int m = (l + r) >> 1;\n S vl = prod(a, b, 2 * k, l, m);\n S vr = prod(a, b, 2 * k + 1, m, r);\n return op(vl, vr);\n }\n};\n\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct dual_segtree {\n int sz = 1, log = 0;\n vector<S> lz;\n dual_segtree(vector<S> a) {\n int n = a.size();\n while (sz < n) {\n sz <<= 1;\n log++;\n }\n lz.assign(sz << 1, e());\n for (int i = 0; i < n; i++) lz[i + sz] = a[i];\n }\n void push(int k) {\n int b = __builtin_ctz(k);\n for (int d = log; d > b; d--) {\n lz[k >> d << 1] = op(lz[k >> d << 1], lz[k >> d]);\n lz[k >> d << 1 | 1] = op(lz[k >> d << 1 | 1], lz[k >> d]);\n lz[k >> d] = e();\n }\n }\n void apply(int l, int r, S x) {\n l += sz, r += sz;\n push(l);\n push(r);\n while (l < r) {\n if (l & 1) {\n lz[l] = op(lz[l], x);\n l++;\n }\n if (r & 1) {\n r--;\n lz[r] = op(lz[r], x);\n }\n l >>= 1, r >>= 1;\n }\n }\n S get(int k) {\n k += sz;\n S res = e();\n while (k) {\n res = op(res, lz[k]);\n k >>= 1;\n }\n return res;\n }\n};\nll modinv(int x, int modulo) {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return (u >= 0 ? u % modulo : (modulo - (-u) % modulo) % modulo);\n}\nstruct auxiliary_tree : lca_tree {\n vector<int> in;\n vector<vector<int>> G;\n auxiliary_tree(vector<vector<int>> &g) : lca_tree(g), in((int)g.size()), G((int)g.size()) {\n int t = 0;\n dfs(0, -1, t, g);\n }\n void dfs(int v, int p, int &t, vector<vector<int>> &g) {\n in[v] = t++;\n for (auto u : g[v]) {\n if (u != p) dfs(u, v, t, g);\n }\n }\n using lca_tree::depth;\n pair<int, vector<int>> query(vector<int> vs, bool decending = false) {\n // decending: only parent to child\n assert(!vs.empty());\n sort(vs.begin(), vs.end(), [&](int a, int b) { return in[a] < in[b]; });\n int m = vs.size();\n stack<int> st;\n st.push(vs[0]);\n for (int i = 0; i < m - 1; i++) {\n int w = lca_tree::query(vs[i], vs[i + 1]);\n if (w != vs[i]) {\n int l = st.top();\n st.pop();\n while (!st.empty() && depth[w] < depth[st.top()]) {\n if (!decending) G[l].push_back(st.top());\n G[st.top()].push_back(l);\n l = st.top();\n st.pop();\n }\n if (st.empty() || st.top() != w) {\n st.push(w);\n vs.push_back(w);\n }\n if (!decending) G[l].push_back(w);\n G[w].push_back(l);\n }\n st.push(vs[i + 1]);\n }\n while (st.size() > 1) {\n int x = st.top();\n st.pop();\n if (!decending) G[x].push_back(st.top());\n G[st.top()].push_back(x);\n }\n // {root,vertex_list}\n return make_pair(st.top(), vs);\n }\n void clear(vector<int> vs) {\n for (int v : vs) G[v].clear();\n }\n};\n/*\n#include <atcoder/convolution>\ntemplate <int T>\ninline std::ostream &operator<<(std::ostream &os, const atcoder::static_modint<T> &p) {\n return os << p.val();\n}\ntemplate <int T>\ninline std::istream &operator>>(std::istream &is, atcoder::static_modint<T> &a) {\n int64_t t;\n is >> t;\n a = t;\n return is;\n}\n\ntemplate <typename mint>\nstruct FormalPowerSeries : vector<mint> {\n using vector<mint>::vector;\n using FPS = FormalPowerSeries;\n FPS &operator+=(const FPS &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i];\n return *this;\n }\n FPS &operator+=(const mint &r) {\n if (this->empty()) this->resize(1);\n (*this)[0] += r;\n return *this;\n }\n\n FPS &operator-=(const FPS &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i];\n return *this;\n }\n\n FPS &operator-=(const mint &r) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= r;\n return *this;\n }\n FPS &operator*=(const FPS &r) {\n if (this->empty() || r.empty()) {\n this->clear();\n return *this;\n }\n assert(mint::mod() == 998244353);\n vector<mint> prod = atcoder::convolution(*this, r);\n this->resize((int)prod.size());\n for (int i = 0; i < (int)this->size(); i++) (*this)[i] = prod[i];\n return *this;\n }\n FPS &operator*=(const mint &v) {\n for (int i = 0; i < (int)this->size(); i++) (*this)[i] *= v;\n return *this;\n }\n FPS &operator/=(const FPS &r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int n = this->size() - r.size() + 1;\n return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev();\n }\n FPS &operator%=(const FPS &r) {\n *this -= *this / r * r;\n shrink();\n return *this;\n }\n FPS operator+(const FPS &r) const { return FPS(*this) += r; }\n FPS operator+(const mint &v) const { return FPS(*this) += v; }\n FPS operator-(const FPS &r) const { return FPS(*this) -= r; }\n FPS operator-(const mint &v) const { return FPS(*this) -= v; }\n FPS operator*(const FPS &r) const { return FPS(*this) *= r; }\n FPS operator*(const mint &v) const { return FPS(*this) *= v; }\n FPS operator/(const FPS &r) const { return FPS(*this) /= r; }\n FPS operator%(const FPS &r) const { return FPS(*this) %= r; }\n FPS operator-() const {\n FPS ret(this->size());\n for (int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i];\n return ret;\n }\n void shrink() {\n while (this->size() && this->back() == mint(0)) this->pop_back();\n }\n FPS operator>>(int sz) const {\n if ((int)this->size() <= sz) return {};\n FPS ret(*this);\n ret.erase(ret.begin(), ret.begin() + sz);\n return ret;\n }\n FPS operator<<(int sz) const {\n FPS ret(*this);\n ret.insert(ret.begin(), sz, mint(0));\n return ret;\n }\n FPS pre(int sz) const { return FPS(begin(*this), begin(*this) + min((int)this->size(), sz)); }\n FPS rev() const {\n FPS ret(*this);\n reverse(begin(ret), end(ret));\n return ret;\n }\n FPS diff() const {\n const int n = this->size();\n FPS ret(max(0, n - 1));\n for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * mint(i);\n return ret;\n }\n FPS integral() const {\n const int n = this->size();\n FPS ret(n + 1);\n ret[0] = mint(0);\n if (n > 0) ret[1] = mint(1);\n auto mod = mint::mod();\n for (int i = 2; i <= n; i++) ret[i] = (-ret[mod % i] * (mod / i));\n for (int i = 0; i < n; i++) ret[i + 1] *= (*this)[i];\n return ret;\n }\n FPS inv(int deg = -1) const {\n assert(((*this)[0]) != mint(0));\n const int n = this->size();\n if (deg == -1) deg = n;\n FPS ret({mint(1) / (*this)[0]});\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1);\n }\n return ret.pre(deg);\n }\n FPS log(int deg = -1) {\n assert((*this)[0] == mint(1));\n if (deg == -1) deg = this->size();\n return (this->diff() * this->inv(deg)).pre(deg - 1).integral();\n }\n FPS exp(int deg = -1) const {\n assert((*this)[0] == mint(0));\n const int n = this->size();\n if (deg == -1) deg = n;\n FPS ret({mint(1)});\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret * (pre(i << 1) + mint(1) - ret.log(i << 1))).pre(i << 1);\n }\n return ret.pre(deg);\n }\n FPS pow(int64_t k, int deg = -1) const {\n const int n = this->size();\n if (deg == -1) deg = n;\n if (k == 0) {\n FPS ret(deg);\n if (deg) ret[0] = 1;\n return ret;\n }\n for (int i = 0; i < n; i++) {\n if ((*this)[i] != mint(0)) {\n mint rev = mint(1) / (*this)[i];\n FPS ret = (((*this * rev) >> i).log(deg) * k).exp(deg);\n ret *= (*this)[i].pow(k);\n ret = (ret << (i * k)).pre(deg);\n if ((int)ret.size() < deg) ret.resize(deg, mint(0));\n return ret;\n }\n if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));\n }\n return FPS(deg, mint(0));\n }\n FPS sqrt(int deg = -1) const {\n const int n = this->size();\n if (deg == -1) deg = n;\n if (n == 0) return FPS(deg, 0);\n if ((*this)[0] == mint(0)) {\n for (int i = 1; i < n; i++) {\n if ((*this)[i] != mint(0)) {\n if (i & 1) return {};\n if (deg - i / 2 <= 0) break;\n auto ret = (*this >> i).sqrt(deg - i / 2);\n if (ret.empty()) return {};\n ret = ret << (i / 2);\n if ((int)ret.size() < deg) ret.resize(deg, mint(0));\n return ret;\n }\n }\n return FPS(deg, 0);\n }\n int64_t sqr = mod_sqrt((*this)[0].val(), mint::mod());\n if (sqr == -1) return {};\n assert(sqr * sqr % mint::mod() == (*this)[0].val());\n FPS ret({mint(sqr)});\n mint inv2 = mint(2).inv();\n for (int i = 1; i < deg; i <<= 1) {\n ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2;\n }\n return ret.pre(deg);\n }\n mint eval(mint x) const {\n mint r = 0, w = 1;\n for (auto &v : *this) r += w * v, w *= x;\n return r;\n }\n};\n// calculate f(x + a)\ntemplate <typename mint>\nFormalPowerSeries<mint> TaylorShift(FormalPowerSeries<mint> f, mint a, Binomial<mint> &bin) {\n int n = f.size();\n for (int i = 0; i < n; i++) f[i] *= bin.fact[i];\n f = f.rev();\n FormalPowerSeries<mint> g(n, mint(1));\n for (int i = 1; i < n; i++) g[i] = g[i - 1] * a * bin.inv[i];\n f = (f * g).pre(n);\n f = f.rev();\n for (int i = 0; i < n; i++) f[i] *= bin.factinv[i];\n return f;\n}\ntemplate <typename mint>\nFormalPowerSeries<mint> FPS_Product(vector<FormalPowerSeries<mint>> f) {\n int n = (int)f.size();\n if (n == 0) return {1};\n function<FormalPowerSeries<mint>(int, int)> calc = [&](int l, int r) {\n if (r - l == 1) return f[l];\n int m = (l + r) / 2;\n return calc(l, m) * calc(m, r);\n };\n return calc(0, n);\n}\n#include <atcoder/string>\nint string_period(string s) {\n int n = s.size();\n auto z = atcoder::z_algorithm(s);\n for (int i = 1; i < n; i++) {\n if (n % i == 0 && z[i] == n - i) return i;\n }\n return n;\n}\n*/\nstruct LowLink {\n vector<vector<int>> g;\n vector<int> ord, low, out;\n vector<bool> used;\n vector<pair<int, int>> bridge;\n vector<pair<int, int>> articulation;\n int unions;\n LowLink(vector<vector<int>> g) : g(g) {\n int n = (int)g.size();\n ord.resize(n);\n low.resize(n);\n out.resize(n);\n used.resize(n);\n unions = 0;\n int t = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n dfs(i, t, -1);\n unions++;\n }\n }\n }\n void dfs(int v, int &t, int par) {\n used[v] = true;\n ord[v] = t++, low[v] = ord[v];\n int cnt = 0;\n bool par_back = false;\n for (int to : g[v]) {\n if (!used[to]) {\n dfs(to, t, v);\n low[v] = min(low[v], low[to]);\n if (ord[v] < low[to]) bridge.push_back(minmax(v, to));\n if (ord[v] <= low[to]) cnt++;\n } else if (to != par || par_back) {\n low[v] = min(low[v], ord[to]);\n } else\n par_back = true;\n }\n if (par != -1) cnt++;\n if (cnt >= 2) articulation.push_back({v, cnt});\n out[v] = t;\n }\n};\nvoid solve(int n, int m) {\n using mint = modint<1000000007>;\n vector<string> s(n);\n rep(i, n) cin >> s[i];\n // ほうじょ?\n mint ans = 1;\n rep(d, 4) {\n vector<int> u, v;\n rep(i, n) {\n if (i % 4 == d) u.push_back(i);\n if ((i + 2) % 4 == d) v.push_back(i);\n }\n int k = v.size();\n vector<mint> dp(1 << k, 0);\n dp[0] = 1;\n rep(i, m) {\n auto ref = (i % 2 == 0 ? v : u);\n auto nxt = (i % 2 == 0 ? u : v);\n vector<mint> tmp(1 << (nxt.size()), 0);\n rep(S, dp.size()) {\n if (dp[S] == 0) continue;\n vector<bool> ng(n);\n rep(p, ref.size()) {\n if ((S >> p) & 1) {\n if (ref[p] + 2 < n) ng[ref[p] + 2] = true;\n if (ref[p] >= 2) ng[ref[p] - 2] = true;\n }\n }\n int T = 0;\n int t = 0;\n for (int j : nxt) {\n if (s[j][i] == '.' && !ng[j]) T |= 1 << t;\n t++;\n }\n tmp[T] += dp[S];\n }\n\n swap(dp, tmp);\n rep(j, nxt.size()) {\n rep(S, dp.size()) {\n if ((S >> j) & 1) {\n dp[S & ~(1 << j)] += dp[S];\n }\n }\n }\n }\n mint sum = 0;\n for (auto x : dp) sum += x;\n ans *= sum;\n }\n cout << ans << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m, k, x, y;\n ll p;\n string s, t;\n while (cin >> n >> m) {\n if (n == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 2500, "memory_kb": 3492, "score_of_the_acc": -0.3141, "final_rank": 6 }, { "submission_id": "aoj_1644_9335328", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (long long i=0;i<n;i++)\n#define loop(i,m,n) for(long long i=m;i<=n;i++)\n#define range(value,range) for(const auto &value : range)\n#define ll long long\n#define vl vector<long long>\n#define vvl vector<vector<long long>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define inf 4000000000000000000LL\n#define mod 1000000007LL\nbool solve(){\n\tll h,w;\n\tcin>>h>>w;\n\tif(h==0)return false;\n\n\tvector<string> a(h);\n\trep(i,h)cin>>a[i];\n\n\tvector<string> s(((h+3)/4)*4,string(w,'#'));\n\trep(i,h)rep(j,w)s[i][j]=a[i][j];\n\th=((h+3)/4)*4;\n\n\tll ans=1;\n\t//4分割\n\trep(z,4){\n\t\t//模様の取り出し、向き合わせ(下2つのグループは上下反転)&(偶数列は上下反転)\n\t\tvvl moyou(w,vl(h/4));\n\t\trep(i,w){\n\t\t\trep(j,h/4){\n\t\t\t\tif(s[j*4+(z+(i%2)*2)%4][i]=='.')moyou[i][j]=1;\n\t\t\t\telse moyou[i][j]=0;\n\t\t\t}\n\t\t\t//この時反転\n\t\t\tif((i%2==1&&z<=1)||(i%2==0&&z>=2))reverse(moyou[i].begin(),moyou[i].end());\n\t\t}\n\n\t\t//上下交互に&を取ったビット列を生成後、3^N DP\n\t\tvvl dp(w+1,vl(1<<(h/4),0));\n\t\t\n\t\tdp[0][0]=1;\n\n\t\t//渡すDP\n\t\trep(i,w){\n\t\t\trep(j,1<<(h/4)){\n\t\t\t\t//dp[i][j]が0ならcontinue\n\t\t\t\tif(dp[i][j]==0)continue;\n\n\t\t\t\t//tmpはjのビットを配列として反転したもの\n\t\t\t\tll tmp=0;\n\t\t\t\trep(k,h/4)if((1<<k)&j)tmp+=1<<((h/4-1)-k);\n\n\t\t\t\t//bitは今、遷移先として、bitを1にできるものを全て1にした物。\n\t\t\t\tll bit=0;\n\t\t\t\tif(tmp%2==0&&moyou[i][0]==1)bit++;\n\t\t\t\tloop(k,1,(h/4-1))if(((1<<k)&tmp)==0&&((1<<(k-1))&tmp)==0&&moyou[i][k]==1)bit+=1<<k;\n\n\t\t\t\t//bitの部分列全てに、dp[i][j]を足す。3^N DP\n\t\t\t\tll b=bit;\n\t\t\t\twhile(1){\n\t\t\t\t\tdp[i+1][b]+=dp[i][j];\n\t\t\t\t\tdp[i+1][b]%=mod;\n\t\t\t\t\tif(b==0)break;\n\t\t\t\t\tb=((b-1)&bit);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tll mul=0;\n\t\trep(j,1<<(h/4))mul+=dp[w][j];\n\t\tmul%=mod;\n\t\tans*=mul,ans%=mod;\n\n\t\t//vvdbg(dp);\n\t\t//cout<<endl;\n\t}\n\tcout<<ans<<endl;\n\treturn true;\n}\n\nint main(){\n\twhile(solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3180, "memory_kb": 18908, "score_of_the_acc": -0.7177, "final_rank": 14 }, { "submission_id": "aoj_1644_9313527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nvoid chmax(T& p, T q) { if (p < q)p = q; };\n\n\n\nconst ll mod = 1000000007;\n\nvoid Zetasuptrans(vll& A) {\n ll N = A.size();\n for (int i = 1; i < N; i *= 2) {\n for (int j = 0; j < N; j++) {\n if (!(j & i))A[j] += A[i | j];\n A[j] %= mod;\n if (A[j] < 0)A[j] += mod;\n }\n }\n}\n\nll N, M;\nvoid solve(ll N, ll M) {\n vector<string> S(N);\n rep(i, N)cin >> S[i];\n while (N % 4 != 0) {\n S.push_back(\"\");\n rep(i, M)S.back().push_back('#');\n N++;\n }\n\n ll an = 1;\n ll QN = N / 4;\n rep(p, 4) {\n vll DP(1 << QN, 0);\n rep(bit, 1 << QN) {\n bool OK = 1;\n rep(q, QN) {\n if (bit & (1 << q)) {\n if (S[q * 4 + p][0] == '#')OK = 0;\n }\n }\n if (OK)DP[bit] = 1;\n }\n rep(i, M - 1) {\n vll NDP(1 << QN, 0);\n bool m = (i % 2 == 0) ^ (p < 2);\n //1:右下, 0:右上\n rep(bit, 1 << QN) {\n ll is_OK = (1 << QN) - 1;\n rep(q, QN) {\n if (bit & (1 << q)) {\n is_OK &= ((1 << QN) - 1) - (1 << q);\n if ((!m) && (q != 0)) {\n is_OK &= (((1 << QN) - 1) - (1 << (q - 1)));\n }\n else if (m && (q != QN - 1)) {\n is_OK &= (((1 << QN) - 1) - (1 << (q + 1)));\n }\n }\n }\n NDP[is_OK]=(NDP[is_OK]+DP[bit])%mod;\n }\n Zetasuptrans(NDP);\n\n rep(bit, 1 << QN) {\n bool OK = 1;\n rep(q, QN) {\n if (bit & (1 << q)) {\n if (i % 2 == 1) {\n if (S[q * 4 + p][i + 1] == '#')OK = 0;\n }\n else if (p < 2) {\n if (S[q * 4 + p + 2][i + 1] == '#')OK = 0;\n }\n else {\n if (S[q * 4 + p - 2][i + 1] == '#')OK = 0;\n }\n }\n }\n if (OK)DP[bit] = NDP[bit] % mod;\n else DP[bit] = 0;\n }\n }\n\n ll res = 0;\n rep(bit, 1 << QN){\n res += DP[bit];\n res%=mod;\n }\n an *= ((res) % mod);\n an %= mod;\n }\n if (an < 0)an += mod;\n cout << an << endl;\n\n\n\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n while (cin >> N >> M) {\n if (N == 0)return 0;\n solve(N, M);\n }\n\n}", "accuracy": 1, "time_ms": 5690, "memory_kb": 3648, "score_of_the_acc": -0.8194, "final_rank": 16 }, { "submission_id": "aoj_1644_9122432", "code_snippet": "#include <bits/stdc++.h>\n\nconstexpr long long MOD{1000000007};\n\nlong long mult(long long a, long long b) {\n return (a * b) % MOD;\n}\n\nlong long add(long long a, long long b) {\n a += b;\n if (a >= MOD) a -= MOD;\n return a;\n}\n\nlong long calc2(std::vector<std::vector<int>> A) {\n std::vector<long long> dp((1 << A[0].size()), 1);\n for (int i{1} ; i < (int)A.size() ; i++) {\n // dpをゼータ変換\n for (int j{} ; j < (int)A[i - 1].size() ; j++) {\n for (int bit{} ; bit < (int)(1 << A[i - 1].size()) ; bit++) {\n if (bit & (1 << j)) {\n dp[bit] = add(dp[bit], dp[bit ^ (1 << j)]);\n }\n }\n }\n std::vector<long long> next((1 << A[i].size()));\n std::map<int, int> map;\n for (int j{} ; j < (int)A[i - 1].size() ; j++) {\n map[A[i - 1][j]] = j;\n }\n for (int bit{} ; bit < (int)(1 << A[i].size()) ; bit++) {\n std::vector<bool> ok(A[i - 1].size(), true);\n for (int j{} ; j < (int)A[i].size() ; j++) {\n if (!(bit & (1 << j))) continue;\n {\n auto it{map.find(A[i][j] - 1)};\n if (it != map.end()){\n ok[it->second] = false;\n }\n }\n {\n auto it{map.find(A[i][j] + 1)};\n if (it != map.end()){\n ok[it->second] = false;\n }\n }\n }\n int maximal{};\n for (int j{} ; j < (int)A[i - 1].size() ; j++) {\n if (ok[j]) maximal |= (1 << j);\n }\n next[bit] = dp[maximal];\n }\n dp = std::move(next);\n }\n long long res{};\n for (auto x : dp) res = add(res, x);\n return res;\n}\n\n\nlong long calc(std::vector<std::string> S) {\n int M{(int)S[0].size()};\n std::vector<std::vector<int>> A(M), B(M);\n for (int i{} ; i < (int)S.size() ; i++) {\n for (int j{} ; j < (int)S[i].size() ; j++) {\n if (S[i][j] == '.') {\n if ((i + j) % 2) {\n A[j].push_back(i);\n }\n else {\n B[j].push_back(i);\n }\n }\n }\n }\n long long res{mult(calc2(A), calc2(B))};\n return res;\n}\n\nbool solve() {\n int N, M;\n std::cin >> N >> M;\n if (N == 0 and M == 0) {\n return false;\n }\n std::vector<std::string> S(N);\n for (auto& s : S) {\n std::cin >> s;\n }\n std::vector<std::string> odd, even;\n for (int i{} ; i < N ; i++) {\n if (i % 2) odd.push_back(S[i]);\n else even.push_back(S[i]);\n }\n long long ans{mult(calc(odd), calc(even))};\n std::cout << ans << '\\n';\n return true;\n}\n\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 2550, "memory_kb": 4028, "score_of_the_acc": -0.3323, "final_rank": 7 }, { "submission_id": "aoj_1644_9072398", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\nconst i64 MOD = 1e9+7;\n\ni64 solve2(vector<string> S) {\n const int H = S.size();\n if(H == 0) return 1;\n const int W = S[0].size();\n\n vector ID(H, vector(W, int(-1)));\n vector<vector<int>> T(W);\n for(int j : rep(W)) for(int i : rep(H)) {\n if(S[i][j] == '.') {\n ID[i][j] = T[j].size();\n T[j].push_back(i);\n }\n }\n\n vector dp(1 << T[0].size(), i64(1));\n for(int j : rep(W - 1)) {\n const int nj = j + 1;\n vector nt(1 << T[nj].size(), i64(0));\n for(int set : rep(1 << T[j].size())) {\n int nt_set = (1 << T[nj].size()) - 1;\n for(int k : rep(T[j].size())) if(set & (1 << k)) {\n const int i = T[j][k];\n for(const int ni : {i - 1, i + 1}) {\n if(0 <= ni and ni < H and S[ni][nj] == '.') {\n if(nt_set & (1 << ID[ni][nj])) nt_set ^= (1 << ID[ni][nj]);\n }\n }\n }\n nt[nt_set] += dp[set];\n nt[nt_set] %= MOD;\n }\n\n for(int k : rep(T[nj].size())) {\n for(int set : rep(1 << T[nj].size())) {\n if(not(set & (1 << k))) {\n nt[set] += nt[set | (1 << k)];\n nt[set] %= MOD;\n }\n }\n }\n\n dp = move(nt);\n }\n\n i64 ans = 0;\n for(int set : rep(1 << T[W - 1].size())) {\n ans = (ans + dp[set]) % MOD;\n }\n return ans;\n}\n\ni64 solve1(vector<string> S) {\n const int H = S.size();\n if(H == 0) return 1;\n const int W = S[0].size();\n\n vector<string> T, U;\n for(int i : rep(H)) {\n string t = \"\", u = \"\";\n for(int j : rep(W)) {\n if((i + j) % 2 == 0) t += S[i][j], u += '#';\n if((i + j) % 2 == 1) u += S[i][j], t += '#';\n }\n\n T.push_back(t);\n U.push_back(u);\n }\n return solve2(T) * solve2(U) % MOD;\n}\n\ni64 solve(int H, int W) {\n vector<string> S = in(H);\n vector<string> T, U;\n for(int i : rep(H)) {\n if(i % 2 == 0) T.push_back(S[i]);\n if(i % 2 == 1) U.push_back(S[i]);\n }\n return solve1(T) * solve1(U) % MOD;\n}\n\nint main() {\n while(true) {\n int H = in(), W = in();\n if(make_pair(H, W) == make_pair(0, 0)) return 0;\n print(solve(H, W));\n }\n}", "accuracy": 1, "time_ms": 1700, "memory_kb": 3992, "score_of_the_acc": -0.1977, "final_rank": 2 }, { "submission_id": "aoj_1644_7971231", "code_snippet": "#include <cassert>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\nusing i64 = long long;\n\n#define rep(i, n) for (int i = 0; i < n; i++)\n\nconst i64 mod = 1'000'000'007;\n\nvoid zeta(vector<i64>& a) {\n int n = a.size();\n int c = 0;\n int tmp = n;\n while (tmp) {\n c++;\n tmp >>= 1;\n }\n for (int j = c - 1; j >= 0; j--) {\n rep(i, n) {\n if (i >> j & 1) {\n a[i] = (a[i ^ (1 << j)] + a[i]) % mod;\n }\n }\n }\n}\nchar a[60][60];\nchar s[60][60];\n\nvoid solve(int h, int w) {\n rep(i, h) rep(j, w) cin >> a[i][j];\n rep(i, h) rep(j, w) s[j][i] = a[i][j];\n swap(h, w);\n int b = (w + 3) / 4;\n i64 ans = 1;\n rep(k, 4) {\n vector dp(h, vector<i64>(1 << b));\n rep(i, 1 << b) {\n int f = 1;\n rep(j, b) {\n if (i >> j & 1) {\n if (j * 4 + k >= w || s[0][j * 4 + k] == '#') f = 0;\n }\n }\n dp[0][i] = f;\n }\n zeta(dp[0]);\n int now = k, nx = (k + 2) % 4;\n for (int i = 1; i < h; i++) {\n rep(nxt, 1 << b) {\n int g = 1;\n rep(j, b) {\n if (nxt >> j & 1) {\n if (j * 4 + nx >= w || s[i][j * 4 + nx] == '#') g = 0;\n }\n }\n if (!g) {\n continue;\n }\n int bit = 0;\n vector<int> fl(b, 1);\n if (now >= 2) {\n rep(j, b) {\n auto xj = j * 4 + nx;\n if (xj >= w || s[i][xj] == '#') continue;\n if (xj + 2 < w) {\n if (nxt >> j & 1) fl[j] = 0;\n }\n if (xj - 2 >= 0) {\n assert(j >= 1);\n if (nxt >> j & 1) fl[j - 1] = 0;\n }\n }\n } else {\n rep(j, b) {\n auto xj = j * 4 + nx;\n if (xj >= w || s[i][xj] == '#') continue;\n if (xj - 2 >= 0) {\n if (nxt >> j & 1) fl[j] = 0;\n }\n if (xj + 2 < w) {\n assert(j + 1 < b);\n if (nxt >> j & 1) fl[j + 1] = 0;\n }\n }\n }\n rep(j, b) bit += (1 << j) * fl[j];\n // cout << \"nxt: \" << nxt << endl;\n // cout << dp[i - 1][bit] << \"\\n\";\n dp[i][nxt] = (dp[i - 1][bit] + dp[i][nxt]) % mod;\n }\n zeta(dp[i]);\n swap(now, nx);\n }\n // cout << dp[h - 1][(1 << b) - 1] << endl;\n ans = (ans * dp[h - 1][(1 << b) - 1]) % mod;\n }\n cout << ans << endl;\n}\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) return 0;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 4670, "memory_kb": 18760, "score_of_the_acc": -0.9495, "final_rank": 17 } ]
aoj_1645_cpp
Evacuation Site The council of your home city decided to build a public evacuation site to prepare for natural disasters. The evacuation site is desired to be built at a location reachable from as many locations across the city as possible, even when many road segments are closed due to a natural disaster. Given the road network of the city, please find the best candidate locations. The road network is specified as an undirected graph. The nodes of the graph correspond to various locations in the city area, and the edges are two-way road segments connecting the locations. Each road segment is assigned a positive integer representing the assumed strength against disasters. A road segment with assumed strength s is expected to be available on a disaster of level below s , but will be closed on a disaster of level above or equal to s . Adequacy of a location as an evacuation site is assessed by the following ordering. Let us define r s ( v ) as the number of locations that is directly or indirectly connected to the location v via road segments available even on the level- s disaster. We define the location u is more suitable than the location v as an evacuation site if and only if there exists some k ≥ 1 satisfying r 1 ( u ) = r 1 ( v ), r 2 ( u ) = r 2 ( v ), ... r k -1 ( u ) = r k -1 ( v ), and r k ( u ) > r k ( v ). Please find the best locations based on the above criterion. If two or more locations are rated equally as the best, enumerate all of them. Input The input consists of multiple datasets, each in the following format. n m a 1 b 1 s 1 ... a m b m s m n is a positive integer less than or equal to 10 5 , representing the number of locations. m is a non-negative integer less than or equal to 10 5 , representing the number of road segments. The locations in the city are given ID numbers 1 through n . The i -th segment connects the locations a i and b i , and its assumed strength is s i . Each of a i , b i , and s i is an integer and they satisfy 1 ≤ a i < b i ≤ n and 1 ≤ s i ≤ 10 5 . When i ≠ j , either a i ≠ a j or b i ≠ b j holds. The given graph is connected. That is, there is at least one path between every pair of locations. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 50. Output For each dataset, output the list of the ID numbers of all of the locations most suitable with the criterion described above separated by a space character, in the increasing order, in a line. Sample Input 3 3 1 2 1 1 3 2 2 3 3 3 3 1 2 3 1 3 3 2 3 3 5 5 1 2 5 2 3 1 3 4 2 3 5 1 4 5 2 0 0 Output for the Sample Input 2 3 1 2 3 3 4 5
[ { "submission_id": "aoj_1645_10672181", "code_snippet": "#line 1 \"library/Template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T, typename S = T> S SUM(const vector<T> &a) {\n return accumulate(ALL(a), S(0));\n}\ntemplate <typename S, typename T = S> S POW(S a, T b) {\n S ret = 1, base = a;\n for (;;) {\n if (b & 1)\n ret *= base;\n b >>= 1;\n if (b == 0)\n break;\n base *= base;\n }\n return ret;\n}\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define debug 1\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define debug 0\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n#line 2 \"library/Utility/fastio.hpp\"\n#include <unistd.h>\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n#line 2 \"library/Utility/random.hpp\"\n\nnamespace Random {\nmt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count());\nusing u64 = unsigned long long;\nu64 get() {\n return randgen();\n}\ntemplate <typename T> T get(T L) { // [0,L]\n return get() % (L + 1);\n}\ntemplate <typename T> T get(T L, T R) { // [L,R]\n return get(R - L) + L;\n}\ndouble uniform() {\n return double(get(1000000000)) / 1000000000;\n}\nstring str(int n) {\n string ret;\n rep(i, 0, n) ret += get('a', 'z');\n return ret;\n}\ntemplate <typename Iter> void shuffle(Iter first, Iter last) {\n if (first == last)\n return;\n int len = 1;\n for (auto it = first + 1; it != last; it++) {\n len++;\n int j = get(0, len - 1);\n if (j != len - 1)\n iter_swap(it, first + j);\n }\n}\ntemplate <typename T> vector<T> select(int n, T L, T R) { // [L,R]\n if (n * 2 >= R - L + 1) {\n vector<T> ret(R - L + 1);\n iota(ALL(ret), L);\n shuffle(ALL(ret));\n ret.resize(n);\n return ret;\n } else {\n unordered_set<T> used;\n vector<T> ret;\n while (SZ(used) < n) {\n T x = get(L, R);\n if (!used.count(x)) {\n used.insert(x);\n ret.push_back(x);\n }\n }\n return ret;\n }\n}\n\nvoid relabel(int n, vector<pair<int, int>> &es) {\n shuffle(ALL(es));\n vector<int> ord(n);\n iota(ALL(ord), 0);\n shuffle(ALL(ord));\n for (auto &[u, v] : es)\n u = ord[u], v = ord[v];\n}\ntemplate <bool directed, bool multi, bool self>\nvector<pair<int, int>> genGraph(int n, int m) {\n vector<pair<int, int>> cand, es;\n rep(u, 0, n) rep(v, 0, n) {\n if (!self and u == v)\n continue;\n if (!directed and u > v)\n continue;\n cand.push_back({u, v});\n }\n if (m == -1)\n m = get(SZ(cand));\n // chmin(m, SZ(cand));\n vector<int> ord;\n if (multi)\n rep(_, 0, m) ord.push_back(get(SZ(cand) - 1));\n else {\n ord = select(m, 0, SZ(cand) - 1);\n }\n for (auto &i : ord)\n es.push_back(cand[i]);\n relabel(n, es);\n return es;\n}\nvector<pair<int, int>> genTree(int n) {\n vector<pair<int, int>> es;\n rep(i, 1, n) es.push_back({get(i - 1), i});\n relabel(n, es);\n return es;\n}\n}; // namespace Random\n\n/**\n * @brief Random\n */\n#line 4 \"sol.cpp\"\n\n#line 2 \"library/DataStructure/unionfind.hpp\"\n\nstruct UnionFind{\n vector<int> par; int n;\n UnionFind(){}\n UnionFind(int _n):par(_n,-1),n(_n){}\n int root(int x){return par[x]<0?x:par[x]=root(par[x]);}\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n bool unite(int x,int y){\n x=root(x),y=root(y); if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n par[y]+=par[x]; par[x]=y; n--; return true;\n }\n};\n\n/**\n * @brief Union Find\n */\n#line 6 \"sol.cpp\"\n\nint main() {\n for (;;) {\n int n, m;\n read(n, m);\n if (n == 0)\n break;\n if (n == 1) {\n print(1);\n continue;\n }\n\n using P = pair<int, int>;\n using T = array<int, 3>;\n vector es(101010, vector<P>());\n rep(_, 0, m) {\n int x, y, w;\n read(x, y, w);\n x--, y--;\n es[w].push_back({x, y});\n }\n UnionFind uni(n);\n map<P, vector<P>> tree;\n vector<int> pre(n, 101010);\n map<P, int> sz;\n P root = {101010, 0};\n rrep(w, 1, 100001) {\n map<int, vector<int>> mp;\n for (auto &[u, v] : es[w]) {\n if (!uni.same(u, v)) {\n if (!mp.count(uni.root(u))) {\n mp[uni.root(u)].push_back(uni.root(u));\n }\n if (!mp.count(uni.root(v))) {\n mp[uni.root(v)].push_back(uni.root(v));\n }\n int other = uni.root(u) ^ uni.root(v);\n uni.unite(u, v);\n int rt = uni.root(u);\n other ^= rt;\n if (SZ(mp[rt]) < SZ(mp[other])) {\n swap(mp[rt], mp[other]);\n }\n for (auto &x : mp[other])\n mp[rt].push_back(x);\n mp.erase(other);\n }\n }\n for (auto &[rt, vs] : mp) {\n for (auto &x : vs) {\n tree[{w, rt}].push_back({pre[x], x});\n }\n pre[rt] = w;\n root = {w, rt};\n sz[{w, rt}] = uni.size(rt);\n }\n }\n // show(tree);\n\n vector<int> ret;\n vector cand(101010, vector<P>());\n cand[root.first].push_back(root);\n set<pair<int, P>> st;\n st.insert({sz[root], root});\n rep(w, 1, 101010) {\n for (auto &v : cand[w]) {\n st.erase({sz[v], v});\n for (auto &to : tree[v]) {\n st.insert({sz[to], to});\n }\n }\n if (SZ(st) == 0)\n continue;\n int mx = (*st.rbegin()).first;\n for (auto &v : cand[w])\n for (auto &to : tree[v])\n if (mx == sz[to]) {\n if (to.first == 101010) {\n ret.push_back(to.second + 1);\n } else {\n cand[to.first].push_back(to);\n }\n }\n while (SZ(st) > 0) {\n if ((*st.begin()).first < mx)\n st.erase(st.begin());\n else\n break;\n }\n }\n\n sort(ALL(ret));\n print(ret);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 38548, "score_of_the_acc": -1.2991, "final_rank": 19 }, { "submission_id": "aoj_1645_10612476", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define vi vector<int>\n#define vl vector<ll>\n#define ov4(a, b, c, d, name, ...) name\n#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for(ll i = (a)-1; i >= (b); i--)\n#define fore(e, v) for(auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T, typename S> bool chmin(T& a, const S& b) { return a > b ? a = b, 1 : 0; }\ntemplate<typename T, typename S> bool chmax(T& a, const S& b) { return a < b ? a = b, 1 : 0; }\n \nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n \n#define i128 __int128_t\n \nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\nvoid debug(auto ...vs) {\n ((cerr << vs << \" \"), ...) << endl;\n}\n\nstruct UnionFind {\n int n;\n vi par;\n stack<pii> history;\n\n UnionFind(int n) : n(n), par(n, -1) {}\n\n int leader(int x) {\n if (par[x] < 0) return x;\n return leader(par[x]);\n }\n\n pii merge(int x, int y) {\n x = leader(x);\n y = leader(y);\n if (par[x] > par[y]) {\n swap(x, y);\n }\n \n history.emplace(x, par[x]);\n history.emplace(y, par[y]);\n if (x == y) {\n return {-1, -1};\n }\n\n par[x] += par[y];\n par[y] = x;\n return {x, y};\n }\n int size(int x) {\n return -par[leader(x)];\n }\n pii rollback() {\n auto [y, py] = history.top();history.pop();\n par[y] = py;\n auto [x, px] = history.top();history.pop();\n par[x] = px;\n \n return {x, y};\n }\n};\n\n\nvoid solve(int n, int m) {\n\tconst int B = 1e5+10;\n vector<vector<pii>> E(B);\n rep(i, m) {\n int a, b, s;cin >> a >> b >> s;\n a--, b--;\n E[s].emplace_back(a, b);\n }\n UnionFind uf(n);\n vector<set<int>> se(B);\n per(t, B, 0) {\n for(auto [a, b] : E[t]) {\n se[t].insert(uf.leader(a));\n se[t].insert(uf.leader(b));\n }\n for(auto [a, b] : E[t]) {\n auto [x, y] = uf.merge(a, b);\n }\n }\n vi ok(n, 0);ok[uf.leader(0)] = n;\n int last = n, cnt = 1;\n rep(t, B) {\n vector<pii> sp;\n int mx = -1, c = 0;\n map<int, int> mp;\n for(auto x: se[t]) {\n if (ok[uf.leader(x)] ) {\n mp[x] = uf.leader(x);\n }\n }\n for (auto [x, y] : mp) {\n if (ok[y]) {\n ok[y] = 0;\n cnt--;\n }\n }\n rep(sz(E[t])) {\n auto [x, y] = uf.rollback();\n }\n \n for(auto [x, y] : mp) {\n chmax(mx, uf.size(x));\n }\n\n if (cnt) {\n mx = last;\n }\n else {\n last = mx;\n }\n \n for(auto [x, y] : mp) {\n if (uf.size(x) == mx) {\n ok[x] = 1;\n cnt++;\n }\n }\n \n } \n vi res;\n rep(i, n) {\n if (ok[i]) res.emplace_back(i);\n }\n rep(i, sz(res)) {\n if (i) cout << \" \";\n cout << res[i] + 1;\n }\n cout << endl;\n\n}\n\nint main() {\n // int T;cin >> T;\n\t// int T = 1;\n while(true) {\n int n, m;cin >>n >> m;\n if (n == 0) break;\n solve(n, m);\n }\n \n}", "accuracy": 1, "time_ms": 150, "memory_kb": 25484, "score_of_the_acc": -0.6498, "final_rank": 10 }, { "submission_id": "aoj_1645_10611571", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define all(p) p.begin(),p.end()\n\nstruct UF {\n int n;\n vector<int> wei, par;\n UF (int n_){\n n = n_;\n wei.resize(n, 1);\n par.resize(n);\n rep(i, 0, n) par[i] = i;\n }\n int root(int a){\n if (par[a] == a) return a;\n return par[a] = root(par[a]);\n }\n int same(int a, int b){\n a = root(a);\n b = root(b);\n return a == b;\n }\n int unite(int a, int b){\n a = root(a), b = root(b);\n if (a == b) return 0;\n if (a < b) swap(a, b);\n wei[a] += wei[b];\n par[b] = a;\n return 1;\n }\n};\n\n\n\nvoid solve(int N, int M){\n UF T(N);\n const int L = 100'000;\n vector<vector<pair<int, int>>> E(L);\n rep(i, 0, M){\n int a, b, c;\n cin >> a >> b >> c;\n a--, b--, c--;\n E[c].push_back({a, b});\n }\n vector<vector<pair<int, pair<int, int>>>> G(N * 2);\n vector<int> pos(N);\n vector<int> sz(N + N);\n rep(i, 0, N) pos[i] = i, sz[i] = 1;\n int root = N - 1;\n for (int s = L - 1; s >= 0; s--){\n vector<int> seen;\n for (auto [a, b] : E[s]){\n if (T.same(a, b)) continue;\n seen.push_back(T.root(a));\n seen.push_back(T.root(b));\n }\n sort(all(seen));\n seen.erase(unique(all(seen)), seen.end());\n for (auto [a, b] : E[s]){\n T.unite(a, b);\n }\n sort(all(seen), [&](int l, int r){\n return T.root(l) < T.root(r);\n });\n for (int l = 0, r = 0; l < (int)seen.size(); l = r){\n root++;\n while (r != (int)seen.size() && T.root(seen[l]) == T.root(seen[r])){\n int a = seen[r];\n sz[root] = T.wei[T.root(a)];\n G[root].push_back({pos[a], {s, sz[pos[a]]}});\n pos[a] = root;\n r++;\n }\n }\n }\n vector<int> ans = {root};\n /*rep(i, 0, root + 1){\n cout << i << \" : \" ;\n for (auto [a, b] : G[i]) cout << a << \" \";\n cout << endl;\n }*/\n while (true){\n sort(all(ans));\n if (ans.front() < N){\n while (ans.back() >= N) ans.pop_back();\n rep(i, 0, ans.size()){\n if (i) cout << \" \";\n cout << ans[i] + 1;\n }\n cout << \"\\n\";\n return;\n }\n vector<int> n_ans;\n pair<int, int> mx = {-1, -1};\n for (auto x : ans) for (auto [var, z] : G[x]){\n if (mx < z) mx = z, n_ans.clear();\n if (mx == z) n_ans.push_back(var);\n }\n swap(n_ans, ans);\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n void init();\n\n int N, M;\n while (cin >> N >> M, N) solve(N, M);\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 18388, "score_of_the_acc": -0.4528, "final_rank": 4 }, { "submission_id": "aoj_1645_10611520", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nstruct dsu {\n using vi = vector<int>; \n using vvi = vector<vector<int>>;\n vi par, sz, es;\n int cc;\n\n vector<array<int,4>> hist;\n\n dsu(int n) {\n par = vi(n);\n sz = vi(n, 1);\n es = vi(n, 0);\n cc = n;\n iota(all(par), 0);\n }\n \n int leader(int u) {\n if (par[u] != u) {\n return leader(par[u]);\n } \n return u;\n }\n \n bool same(int a, int b) {\n return leader(a) == leader(b);\n }\n \n bool merge(int a, int b) {\n a = leader(a), b = leader(b);\n if(sz[a] < sz[b]) swap(a, b);\n\n if(a == b) {\n ++es[a];\n return false;\n }\n else {\n cc--;\n hist.push_back({b,par[b],a,sz[a]});\n par[b] = leader(a);\n sz[a] += sz[b];\n return true;\n }\n }\n\n void rollback(){\n auto[b,pb,a,sa]=hist.back();\n hist.pop_back();\n par[b]=pb;\n sz[a]=sa;\n }\n //以下、必要な物を書く\n\n int size(int u) {\n return sz[leader(u)];\n }\n\n \n int componentcnt() {\n return cc;\n }\n \n int edgecnt(int u) {\n return es[leader(u)];\n }\n\n vvi groups() {\n int n = par.size();\n vvi ms(n);\n rep(v, 0, n) {\n ms[leader(v)].push_back(v);\n }\n\n vvi res;\n rep(i, 0, n) if(ms[i].size() > 0) {\n res.push_back(ms[i]);\n }\n\n return res;\n }\n};\nint Solve(){\n int n,m;\n cin>>n>>m;\n if(n==0) return 0;\n vector<vector<array<int,2>>> vv;\n {\n map<int,vector<array<int,2>>> mp;\n rep(i,0,m){\n int a,b,s;\n cin>>a>>b>>s;\n a--, b--;\n mp[s].push_back({a,b});\n }\n for(auto[s,v]:mp) vv.push_back(v);\n }\n m=vv.size();\n dsu uf(n);\n {\n vector<vector<array<int,2>>> nvv;\n for(int i=m-1;i>=0;i--){\n vector<array<int,2>> tmp;\n for(auto[x,y]:vv[i]) if(uf.merge(x,y)) tmp.push_back({x,y});\n reverse(tmp.begin(),tmp.end());\n if(!tmp.empty()) nvv.push_back(tmp);\n }\n reverse(nvv.begin(),nvv.end());\n swap(nvv,vv);\n }\n set<int> lds={uf.leader(0)};\n for(auto vp:vv){\n set<int> cld=lds;\n for(auto[x,y]:vp){\n if(cld.count(uf.leader(x))){\n uf.rollback();\n cld.insert(uf.leader(x));\n cld.insert(uf.leader(y));\n }else{\n uf.rollback();\n }\n }\n int mx=0;\n for(int d:cld) chmax(mx,uf.size(d));\n set<int> nst;\n for(int d:cld){\n if(mx==uf.size(d)) nst.insert(d);\n }\n lds=nst;\n }\n vector<int> ans;\n for(int x:lds) ans.push_back(x+1);\n rep(i,0,ans.size()) cout<<ans[i]<<\" \\n\"[i==ans.size()-1];\n return 1;\n}\n\nint main(){\n while(Solve());\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 22372, "score_of_the_acc": -0.5522, "final_rank": 6 }, { "submission_id": "aoj_1645_10611496", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\nvoid solve(int n, int m){\n vector<pii> uvs(m);\n vector<int> w(m);\n rep(i,m){\n int u, v; in(u,v); u--, v--;\n uvs[i] = {u,v};\n in(w[i]);\n }\n vector<int> ids(m); iota(all(ids),0);\n ranges::sort(ids, {}, [&](int i){\n return -w[i];\n });\n vector<int> top(n); iota(all(top),0);\n dsu d(n);\n int vid = n;\n vector<int> a(n*2);\n rep(i,n){\n a[i] = iinf;\n }\n vector<vector<int>> childs(n*2);\n vector<int> sz(n*2,1);\n for (int i : ids){\n auto [u, v] = uvs[i];\n if (d.same(u,v)) continue;\n u = d.leader(u);\n v = d.leader(v);\n int uv = d.merge(u,v);\n u = top[u];\n v = top[v];\n int nv = vid++;\n childs[nv].emplace_back(u);\n childs[nv].emplace_back(v);\n top[uv] = nv;\n a[nv] = w[i];\n sz[nv] = sz[u] + sz[v];\n // out(u,v,nv);\n }\n // out(sz);\n // out(a);\n using dat = array<int,3>;\n set<dat> st;\n st.insert({sz[vid-1],a[vid-1],vid-1});\n while (true){\n assert(!st.empty());\n int msz = (*st.rbegin())[0];\n while ((*st.begin())[0] < msz){\n st.erase(st.begin());\n }\n // out(msz);\n int iw = (*st.begin())[1];\n if (iw == iinf){\n vector<int> ans;\n for (auto [p, q, r] : st){\n ans.emplace_back(r+1);\n }\n out(ans);\n return ;\n }\n queue<int> que;\n while (!st.empty() && (*st.begin())[1] == iw){\n auto [p, q, r] = *st.begin();\n que.push(r);\n // out(\"que\",r);\n st.erase(st.begin());\n }\n while (!que.empty()){\n int v = que.front(); que.pop();\n // out(\"pop\",v);\n assert(v >= n);\n for (int c : childs[v]){\n // out(\"child\",v,c,a[c],iw);\n if (a[c] == iw){\n que.push(c);\n }\n else {\n st.insert({sz[c],a[c],c});\n }\n }\n }\n }\n}\n\nint main(){\n while (true){\n int n, m; in(n,m);\n if (n == 0) break;\n solve(n,m);\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 15072, "score_of_the_acc": -0.2718, "final_rank": 3 }, { "submission_id": "aoj_1645_10590190", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(a) (a).begin(), (a).end()\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)\n#define range(i, l, r) for (int i = (int)(l); i < (int)(r); i++)\nusing ll = long long;\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n for (int i = 0; i < v.size(); i++) {\n os << v[i] << (i == (v.size() - 1) ? \"\" : \", \");\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nostream& operator<<(ostream& os, const pair<T1, T2> p) {\n os << \"{\" << p.first << \", \" << p.second << \"}\";\n return os;\n}\n\nstruct RollbackUnionFind {\n vector<int> data;\n stack<pair<int, int>> history;\n int inner_snap;\n\n RollbackUnionFind(int sz) : inner_snap(0) { data.assign(sz, -1); }\n\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n history.emplace(x, data[x]);\n history.emplace(y, data[y]);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n return true;\n }\n\n int find(int k) {\n if (data[k] < 0) return k;\n return find(data[k]);\n }\n\n int same(int x, int y) { return find(x) == find(y); }\n\n int size(int k) { return (-data[find(k)]); }\n\n void undo() {\n data[history.top().first] = history.top().second;\n history.pop();\n data[history.top().first] = history.top().second;\n history.pop();\n }\n\n void snapshot() { inner_snap = int(history.size() >> 1); }\n\n int get_state() { return int(history.size() >> 1); }\n\n void rollback(int state = -1) {\n if (state == -1) state = inner_snap;\n state <<= 1;\n assert(state <= (int)history.size());\n while (state < (int)history.size()) undo();\n }\n};\n\n/**\n * @brief RollbackつきUnion Find\n * @docs docs/data-structure/rollback-union-find.md\n */\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) break;\n\n if (n == 1) {\n cout << 1 << endl;\n continue;\n }\n\n vector<array<int, 3>> e{};\n e.push_back({0, 1, 0});\n rep(_, m) {\n int a, b, s;\n cin >> a >> b >> s;\n a--;\n b--;\n e.push_back({s, a, b});\n }\n\n sort(all(e));\n RollbackUnionFind uf(n);\n rrep(i, e.size() - 1) { uf.unite(e[i][1], e[i][2]); }\n\n set<array<int, 2>> st{};\n st.insert({n, uf.find(0)});\n\n rep(i, e.size()) {\n if (i > 0 && e[i][0] != e[i - 1][0]) {\n while ((*st.begin())[0] != (*st.rbegin())[0])\n st.erase(st.begin());\n }\n auto [s, a, b] = e[i];\n int r = uf.find(a);\n int sz = uf.size(a);\n\n uf.undo();\n if (sz == uf.size(a)) continue;\n if (st.find({sz, r}) == st.end()) continue;\n st.erase({sz, r});\n st.insert({uf.size(a), uf.find(a)});\n st.insert({uf.size(b), uf.find(b)});\n }\n\n vector<int> ans{};\n auto it = st.begin();\n while (it != st.end()) {\n ans.push_back((*it)[1]);\n it++;\n }\n\n rep(i, ans.size()) {\n cout << ans[i] + 1 << (i == ans.size() - 1 ? \"\\n\" : \" \");\n }\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 6684, "score_of_the_acc": -0.1026, "final_rank": 1 }, { "submission_id": "aoj_1645_10483629", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nstruct UnionFind {\n vector<vector<int>> vers;\n vector<int> leader;\n \n vector<vector<pair<int, int>>> op;\n vector<vector<int>> minimas;\n\n UnionFind(int n) {\n leader.resize(n);\n vers.resize(n);\n op.resize(n);\n minimas.resize(n);\n iota(all(leader), 0);\n rep(i, n) {\n vers[i].pb(i);\n op[i].push_back({1 << 20, 1});\n minimas[i].pb(i);\n }\n }\n int judge(int x, int y, int t) {\n if(op[x].back().first == t) op[x].pop_back();\n if(op[y].back().first == t) op[y].pop_back();\n int sz1 = op[x].size();\n int sz2 = op[y].size();\n int sz = min(sz1, sz2);\n for(int i = 1; i <= sz; i ++) {\n auto [t1, s1] = op[x][sz1 - i];\n auto [t2, s2] = op[y][sz2 - i];\n if(s1 == s2) {\n if(t1 > t2) {\n return -1;\n } else if(t1 < t2) {\n return 1;\n } \n } else {\n if(s1 < s2) {\n return 1;\n } else {\n return -1;\n }\n }\n }\n return 0;\n }\n void connect(int x, int y, int s) {\n x = leader[x]; y = leader[y];\n if (x == y) return;\n if(vers[x].size() < vers[y].size()) swap(x, y);\n int flag = judge(x, y, s);\n if(flag == -1) { \n ;\n } else if(flag == 1) {\n op[x] = op[y];\n minimas[x] = minimas[y];\n } else {\n if(minimas[x].size() < minimas[y].size()) {\n foa(e, minimas[x]) {\n minimas[y].pb(e);\n }\n minimas[x] = minimas[y];\n } else {\n foa(e, minimas[y]) {\n minimas[x].pb(e);\n }\n minimas[y].clear();\n }\n }\n foa(e, vers[y]) {\n leader[e] = x;\n vers[x].pb(e);\n }\n op[x].push_back({s, (int)vers[x].size()});\n return;\n }\n};\n\nint main() {\n int n, m; \n cin >> n >> m;\n while(n) {\n vector<tuple<int, int, int>> edges;\n rep(i, m) {\n int x, y, s; cin >> x >> y >> s;\n x --; y --;\n edges.push_back({s, x, y});\n }\n sort(all(edges));\n reverse(all(edges));\n UnionFind uf(n);\n for(auto [s, x, y] : edges) {\n uf.connect(x, y, s);\n }\n auto ans = uf.minimas[uf.leader[0]];\n sort(all(ans));\n int sz = ans.size();\n rep(i, sz) {\n if(i) cout << \" \";\n cout << ans[i] + 1;\n }\n cout << endl;\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 23540, "score_of_the_acc": -0.6572, "final_rank": 12 }, { "submission_id": "aoj_1645_10483609", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nstruct UnionFind {\n vector<vector<int>> vers;\n vector<int> leader;\n \n vector<vector<pair<int, int>>> op;\n vector<vector<int>> minimas;\n\n UnionFind(int n) {\n leader.resize(n);\n vers.resize(n);\n op.resize(n);\n minimas.resize(n);\n iota(all(leader), 0);\n rep(i, n) {\n vers[i].pb(i);\n op[i].push_back({1 << 20, 1});\n minimas[i].pb(i);\n }\n }\n int judge(int x, int y, int t) {\n if(op[x].back().first == t) op[x].pop_back();\n if(op[y].back().first == t) op[y].pop_back();\n int sz1 = op[x].size();\n int sz2 = op[y].size();\n int sz = min(sz1, sz2);\n for(int i = 1; i <= sz; i ++) {\n auto [t1, s1] = op[x][sz1 - i];\n auto [t2, s2] = op[y][sz2 - i];\n if(s1 == s2) {\n if(t1 > t2) {\n return -1;\n } else if(t1 < t2) {\n return 1;\n } \n } else {\n if(s1 < s2) {\n return 1;\n } else {\n return -1;\n }\n }\n }\n return 0;\n }\n void connect(int x, int y, int s) {\n x = leader[x]; y = leader[y];\n if (x == y) return;\n if(vers[x].size() < vers[y].size()) swap(x, y);\n int flag = judge(x, y, s);\n if(flag == -1) { \n ;\n } else if(flag == 1) {\n op[x] = op[y];\n minimas[x] = minimas[y];\n } else {\n if(minimas[x].size() < minimas[y].size()) {\n foa(e, minimas[x]) {\n minimas[y].pb(x);\n }\n minimas[x] = minimas[y];\n } else {\n foa(e, minimas[y]) {\n minimas[x].pb(e);\n }\n minimas[y].clear();\n }\n }\n foa(e, vers[y]) {\n leader[e] = x;\n vers[x].pb(e);\n }\n op[x].push_back({s, (int)vers[x].size()});\n return;\n }\n};\n\nint main() {\n int n, m; \n cin >> n >> m;\n while(n) {\n vector<tuple<int, int, int>> edges;\n rep(i, m) {\n int x, y, s; cin >> x >> y >> s;\n x --; y --;\n edges.push_back({s, x, y});\n }\n sort(all(edges));\n reverse(all(edges));\n UnionFind uf(n);\n for(auto [s, x, y] : edges) {\n uf.connect(x, y, s);\n }\n auto ans = uf.minimas[uf.leader[0]];\n sort(all(ans));\n int sz = ans.size();\n rep(i, sz) {\n if(i) cout << \" \";\n cout << ans[i] + 1;\n }\n cout << endl;\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 23536, "score_of_the_acc": -0.6485, "final_rank": 9 }, { "submission_id": "aoj_1645_10483426", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nstruct UnionFind {\n vector<vector<int>> vers;\n vector<int> leader;\n \n vector<vector<pair<int, int>>> op;\n vector<vector<int>> minimas;\n\n UnionFind(int n) {\n leader.resize(n);\n vers.resize(n);\n op.resize(n);\n minimas.resize(n);\n iota(all(leader), 0);\n rep(i, n) {\n vers[i].pb(i);\n op[i].push_back({1 << 20, 1});\n minimas[i].pb(i);\n }\n }\n int judge(int x, int y, int t) {\n if(op[x].back().first == t) op[x].pop_back();\n if(op[y].back().first == t) op[y].pop_back();\n int sz1 = op[x].size();\n int sz2 = op[y].size();\n int sz = min(sz1, sz2);\n for(int i = 1; i <= sz; i ++) {\n auto [t1, s1] = op[x][sz1 - i];\n auto [t2, s2] = op[y][sz2 - i];\n if(s1 == s2) {\n if(t1 > t2) {\n return -1;\n } else if(t1 < t2) {\n return 1;\n } \n } else {\n if(s1 < s2) {\n return 1;\n } else {\n return -1;\n }\n }\n }\n return 0;\n }\n void connect(int x, int y, int s) {\n x = leader[x]; y = leader[y];\n if (x == y) return;\n if(vers[x].size() < vers[y].size()) swap(x, y);\n int flag = judge(x, y, s);\n if(flag == -1) { \n ;\n } else if(flag == 1) {\n op[x] = (op[y]);\n minimas[x] = (minimas[y]);\n } else {\n if(minimas[x].size() < minimas[y].size()) {\n foa(e, minimas[x]) {\n minimas[y].pb(x);\n }\n minimas[x] = (minimas[y]);\n } else {\n foa(e, minimas[y]) {\n minimas[x].pb(e);\n }\n minimas[y].clear();\n }\n }\n foa(e, vers[y]) {\n leader[e] = x;\n vers[x].pb(e);\n }\n op[x].push_back({s, (int)vers[x].size()});\n return;\n }\n};\n\nint main() {\n int n, m; \n cin >> n >> m;\n while(n) {\n vector<tuple<int, int, int>> edges;\n rep(i, m) {\n int x, y, s; cin >> x >> y >> s;\n x --; y --;\n edges.push_back({s, x, y});\n }\n sort(all(edges));\n reverse(all(edges));\n UnionFind uf(n);\n for(auto [s, x, y] : edges) {\n uf.connect(x, y, s);\n }\n auto ans = uf.minimas[uf.leader[0]];\n sort(all(ans));\n int sz = ans.size();\n rep(i, sz) {\n if(i) cout << \" \";\n cout << ans[i] + 1;\n }\n cout << endl;\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 23228, "score_of_the_acc": -0.6987, "final_rank": 14 }, { "submission_id": "aoj_1645_10483405", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nstruct UnionFind {\n vector<vector<int>> vers;\n vector<int> leader;\n \n vector<vector<pair<int, int>>> op;\n vector<vector<int>> minimas;\n\n UnionFind(int n) {\n leader.resize(n);\n vers.resize(n);\n op.resize(n);\n minimas.resize(n);\n iota(all(leader), 0);\n rep(i, n) {\n vers[i].pb(i);\n op[i].push_back({1 << 20, 1});\n minimas[i].pb(i);\n }\n }\n int judge(int x, int y, int t) {\n if(op[x].back().first == t) op[x].pop_back();\n if(op[y].back().first == t) op[y].pop_back();\n int sz1 = op[x].size();\n int sz2 = op[y].size();\n int sz = min(sz1, sz2);\n for(int i = 1; i <= sz; i ++) {\n auto [t1, s1] = op[x][sz1 - i];\n auto [t2, s2] = op[y][sz2 - i];\n if(s1 == s2) {\n if(t1 > t2) {\n return -1;\n } else if(t1 < t2) {\n return 1;\n } \n } else {\n if(s1 < s2) {\n return 1;\n } else {\n return -1;\n }\n }\n }\n return 0;\n }\n void connect(int x, int y, int s) {\n x = leader[x]; y = leader[y];\n if (x == y) return;\n if(vers[x].size() < vers[y].size()) swap(x, y);\n int flag = judge(x, y, s);\n if(flag == -1) { \n ;\n } else if(flag == 1) {\n op[x] = move(op[y]);\n minimas[x] = move(minimas[y]);\n } else {\n if(minimas[x].size() < minimas[y].size()) {\n foa(e, minimas[x]) {\n minimas[y].pb(x);\n }\n minimas[x] = move(minimas[y]);\n } else {\n foa(e, minimas[y]) {\n minimas[x].pb(e);\n }\n minimas[y].clear();\n }\n }\n foa(e, vers[y]) {\n leader[e] = x;\n vers[x].pb(e);\n }\n op[x].push_back({s, (int)vers[x].size()});\n return;\n }\n};\n\nint main() {\n int n, m; \n cin >> n >> m;\n while(n) {\n vector<tuple<int, int, int>> edges;\n rep(i, m) {\n int x, y, s; cin >> x >> y >> s;\n x --; y --;\n edges.push_back({s, x, y});\n }\n sort(all(edges));\n reverse(all(edges));\n UnionFind uf(n);\n for(auto [s, x, y] : edges) {\n uf.connect(x, y, s);\n }\n auto ans = uf.minimas[uf.leader[0]];\n sort(all(ans));\n int sz = ans.size();\n rep(i, sz) {\n if(i) cout << \" \";\n cout << ans[i] + 1;\n }\n cout << endl;\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 23528, "score_of_the_acc": -0.6568, "final_rank": 11 }, { "submission_id": "aoj_1645_10483373", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nstruct UnionFind {\n vector<vector<int>> vers;\n vector<int> leader;\n \n vector<vector<pair<int, int>>> op;\n vector<int> op_leader;\n\n vector<vector<int>> minimas;\n vector<int> minimas_leader;\n\n UnionFind(int n) {\n leader.resize(n);\n vers.resize(n);\n op.resize(n);\n op_leader.resize(n);\n minimas.resize(n);\n minimas_leader.resize(n);\n iota(all(leader), 0);\n iota(all(op_leader), 0);\n iota(all(minimas_leader), 0);\n rep(i, n) {\n vers[i].pb(i);\n op[i].push_back({1 << 20, 1});\n minimas[i].pb(i);\n }\n }\n int judge(int x, int y, int t) {\n x = op_leader[x];\n y = op_leader[y];\n if(op[x].back().first == t) op[x].pop_back();\n if(op[y].back().first == t) op[y].pop_back();\n int sz1 = op[x].size();\n int sz2 = op[y].size();\n int sz = min(sz1, sz2);\n for(int i = 1; i <= sz; i ++) {\n auto [t1, s1] = op[x][sz1 - i];\n auto [t2, s2] = op[y][sz2 - i];\n if(s1 == s2) {\n if(t1 > t2) {\n return -1;\n } else if(t1 < t2) {\n return 1;\n } \n } else {\n if(s1 < s2) {\n return 1;\n } else {\n return -1;\n }\n }\n }\n return 0;\n }\n void connect(int x, int y, int s) {\n x = leader[x]; y = leader[y];\n if (x == y) return;\n if(vers[x].size() < vers[y].size()) swap(x, y);\n int flag = judge(x, y, s);\n if(flag == -1) { \n ;\n } else if(flag == 1) {\n op_leader[x] = op_leader[y];\n minimas_leader[x] = minimas_leader[y];\n } else {\n int x0 = minimas_leader[x], y0 = minimas_leader[y];\n if(minimas[x0].size() < minimas[y0].size()) {\n foa(e, minimas[x0]) minimas[y0].pb(e);\n minimas_leader[x] = y0;\n } else {\n foa(e, minimas[y0]) minimas[x0].pb(e);\n minimas_leader[y] = x0;\n }\n }\n foa(e, vers[y]) {\n leader[e] = x;\n vers[x].pb(e);\n }\n op[op_leader[x]].push_back({s, (int)vers[x].size()});\n return;\n }\n vector<int> get_minima(int x) {\n x = leader[x];\n x = minimas_leader[x];\n return minimas[x];\n }\n};\n\nint main() {\n int n, m; \n cin >> n >> m;\n while(n) {\n vector<tuple<int, int, int>> edges;\n rep(i, m) {\n int x, y, s; cin >> x >> y >> s;\n x --; y --;\n edges.push_back({s, x, y});\n }\n sort(all(edges));\n reverse(all(edges));\n UnionFind uf(n);\n for(auto [s, x, y] : edges) {\n uf.connect(x, y, s);\n }\n assert((int)uf.vers[uf.leader[0]].size() == n);\n auto ans = uf.get_minima(0);\n sort(all(ans));\n int sz = ans.size();\n rep(i, sz) {\n if(i) cout << \" \";\n cout << ans[i] + 1;\n }\n cout << endl;\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 24088, "score_of_the_acc": -0.7342, "final_rank": 15 }, { "submission_id": "aoj_1645_10482912", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nusing i64 = long long;\n#define rep(i,n) for(i64 i=0; i<i64(n); i++)\n\nstruct Dsu {\n vector<i64> A;\n Dsu(i64 n = 0) : A(n,-1) {}\n i64 leader(i64 a){\n if(A[a] < 0) return a;\n return A[a] = leader(A[a]);\n }\n void merge(i64 u, i64 v){\n u = leader(u);\n v = leader(v);\n if(-A[u] < -A[v]) swap(u, v);\n A[u] += A[v];\n A[v] = u;\n }\n};\n\nvoid testcase(){\n i64 N, M; cin >> N >> M;\n if(N == 0) exit(0);\n vector<pair<i64,pair<i64,i64>>> joins;\n rep(i,M){\n i64 a,b,s; cin >> a >> b >> s; a--; b--;\n joins.push_back({ s, {a,b} });\n }\n sort(joins.rbegin(), joins.rend());\n vector<i64> sz(N,1), st(N,1000000000), id(N);\n vector<vector<i64>> ch(N);\n rep(i,N) id[i] = i;\n auto dsu = Dsu(N);\n for(auto [s,uv] : joins){\n auto [u,v] = uv;\n if(dsu.leader(u) == dsu.leader(v)) continue;\n i64 x = id[dsu.leader(u)];\n i64 y = id[dsu.leader(v)];\n dsu.merge(u,v);\n if(st[y] == s) swap(x,y);\n if(st[y] == s){\n if(ch[x].size() < ch[y].size()) swap(x,y);\n for(auto a : ch[y]) ch[x].push_back(a);\n if(y < N) ch[x].push_back(y);\n ch[y] = {};\n sz[x] += sz[y];\n id[dsu.leader(u)] = x;\n } else if(st[x] == s){\n ch[x].push_back(y);\n sz[x] += sz[y];\n id[dsu.leader(u)] = x;\n } else {\n i64 z = sz.size();\n sz.push_back(sz[x] + sz[y]);\n st.push_back(s);\n ch.push_back({ x,y });\n id[dsu.leader(u)] = z;\n }\n }\n vector<i64> cand;\n cand.push_back(id[dsu.leader(0)]);\n while(cand[0] >= N){\n i64 s = -1, z = 0;\n for(i64 a : cand) for(auto b : ch[a]){\n if(z < sz[b] || (z == sz[b] && s < st[b])){\n s = st[b]; z = sz[b];\n }\n }\n vector<i64> nxcand;\n for(i64 a : cand) for(auto b : ch[a]){\n if(s == st[b] && z == sz[b]) nxcand.push_back(b);\n }\n swap(cand, nxcand);\n }\n sort(cand.begin(), cand.end());\n rep(i,cand.size()){\n if(i) cout << \" \";\n cout << (1 + cand[i]);\n } cout << \"\\n\";\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true) testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 20500, "score_of_the_acc": -0.4678, "final_rank": 5 }, { "submission_id": "aoj_1645_10404853", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep2(i, n) for (ll i = 0; i < (n); i++)\n#define rep3(i, a, b) for (ll i = (a); i < (b); i++)\n#define overload(a, b, c, d, ...) d\n#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)\n#define all(a) begin(a), end(a)\nbool chmin(auto& a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, auto b) { return a < b ? a = b, 1 : 0; }\n\nstruct UnionFind {\n int n;\n vector<int> a;\n UnionFind(int n): n(n), a(n, -1) {}\n int find(int x) {\n if (a[x] < 0) return x;\n return a[x] = find(a[x]);\n }\n bool merge(int l, int r) {\n l = find(l);\n r = find(r);\n if (l == r) return false;\n if (a[l] < a[r]) swap(l, r);\n a[r] += a[l];\n a[l] = r;\n return true;\n }\n int size(int i) {\n return -a[find(i)];\n }\n};\n\nint solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0) return 1;\n vector<tuple<int, int, int>> edges;\n rep(i, m) {\n int a, b, s;\n cin >> a >> b >> s;\n a--; b--;\n edges.emplace_back(a, b, s);\n }\n sort(all(edges), [&](auto lhs, auto rhs) {\n return get<2>(lhs) > get<2>(rhs);\n });\n // size, root\n using P = pair<int, int>;\n // Big, lhs, rhs\n vector<tuple<P, P, P>> info;\n {\n UnionFind uf(n);\n vector<tuple<int, int, int>> temp;\n for (auto [u, v, s]: edges) {\n int l = uf.find(u);\n int r = uf.find(v);\n int ls = uf.size(l);\n int rs = uf.size(r);\n if (uf.merge(u, v)) {\n temp.emplace_back(u, v, s);\n int m = uf.find(u);\n info.emplace_back(P{uf.size(m), m}, P{ls, l}, P{rs, r});\n }\n }\n edges = temp;\n }\n reverse(all(info));\n reverse(all(edges));\n // (size, root)\n set<P> S;\n if (edges.empty()) {\n S.emplace(1, 0);\n } else {\n S.emplace(n, get<0>(info[0]).second);\n }\n for (int L = 0, R = 0; L + 1 < n; L = R) {\n while (R + 1 < n && get<2>(edges[L]) == get<2>(edges[R])) R++;\n rep(i, L, R) {\n auto [root, lhs, rhs] = info[i];\n if (!S.contains(root)) continue;\n S.erase(root);\n S.insert(lhs);\n S.insert(rhs);\n }\n while (true) {\n auto sit = S.begin();\n auto eit = prev(S.end());\n if (sit->first != eit->first) {\n S.erase(sit);\n } else {\n break;\n }\n }\n }\n\n vector<int> ans;\n for (auto [s, u]: S) ans.push_back(u);\n rep(i, ssize(ans)) cout << ans[i] + 1 << \" \\n\"[i + 1 == ssize(ans)];\n\n return 0;\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n while (!solve());\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 13472, "score_of_the_acc": -0.213, "final_rank": 2 }, { "submission_id": "aoj_1645_9561729", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\nint MAXS = 100010;\n\nstruct Edge {\n int a, b;\n int sza, szb;\n};\n\nvoid BFS(int X, int H, vector<vector<pair<int,int>>>& G, vector<bool>& ANS) {\n int N = ANS.size();\n queue<int> Q;\n Q.push(X);\n ANS[X] = false;\n while(!Q.empty()) {\n int P = Q.front();\n Q.pop();\n for (pair<int,int> p : G[P]) {\n auto [NP,s] = p;\n if (s <= H) continue;\n if (ANS[NP]) {\n ANS[NP] = false;\n Q.push(NP);\n }\n }\n } \n return;\n}\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<vector<pair<int,int>>> G(N);\n vector<vector<pair<int,int>>> OldS(MAXS);\n vector<vector<Edge>> S(MAXS);\n rep(i,0,M) {\n int a, b, s;\n cin >> a >> b >> s;\n a--, b--;\n OldS[s].push_back({a,b});\n }\n dsu UF(N);\n rrep(i,0,MAXS) {\n for (pair<int,int> p : OldS[i]) {\n auto [a,b] = p;\n if (UF.same(a,b)) continue;\n G[a].push_back({b,i});\n G[b].push_back({a,i});\n int sza = UF.size(a), szb = UF.size(b);\n S[i].push_back({a,b,sza,szb});\n UF.merge(a,b);\n }\n }\n vector<bool> ANS(N,true);\n multiset<int> ST;\n ST.insert(N);\n rep(i,0,MAXS) reverse(ALL(S[i]));\n rep(i,0,MAXS) {\n if (S[i].empty()) continue;\n for (Edge E : S[i]) {\n if (!ANS[E.a] || !ANS[E.b]) continue;\n ST.erase(ST.find(E.sza+E.szb));\n ST.insert(E.sza);\n ST.insert(E.szb);\n }\n int MAX = *ST.rbegin();\n for (Edge E : S[i]) {\n if (E.sza < MAX) BFS(E.a,i,G,ANS);\n if (E.szb < MAX) BFS(E.b,i,G,ANS);\n }\n while(*ST.begin() < MAX) ST.erase(ST.begin());\n }\n vector<int> ANS2;\n rep(i,0,N) {\n if (ANS[i]) ANS2.push_back(i);\n }\n rep(i,0,ANS2.size()) {\n cout << ANS2[i] + 1 << (i + 1 == ANS2.size() ? '\\n' : ' ');\n }\n}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 20000, "score_of_the_acc": -0.6145, "final_rank": 7 }, { "submission_id": "aoj_1645_9548686", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\n// https://ei1333.github.io/luzhiled/snippets/structure/union-find.html\nstruct UnionFindUndo{\n vector<int> par;\n stack<pair<int,int>> history;\n UnionFindUndo(int n):par(n,-1){}\n // O(logN)\n\n bool unite(int x,int y){\n x = root(x),y = root(y);\n history.emplace(x, par[x]);\n history.emplace(y, par[y]);\n if(x == y) return false;\n if(par[x] > par[y]) swap(x, y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n bool find(int x,int y){\n return root(x) == root(y);\n }\n int root(int x){\n return par[x] < 0 ? x : root(par[x]);\n }\n int size(int x){\n return -par[root(x)];\n }\n void undo(){\n par[history.top().first] = history.top().second;\n history.pop();\n par[history.top().first] = history.top().second;\n history.pop();\n }\n // ある時点を記録する。その時点よりさかのぼることができなくなる・\n void snapshot(){\n while(history.size())history.pop();\n }\n // 記録した時点にさかのぼる。\n void rollback(){\n while(history.size())undo();\n }\n};\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m;cin >> n >> m;\n if(n == 0 && m == 0)break;\n vector<vector<P>> v(100010);\n vvi vv(n),w(n);\n rep(i,m){\n int a,b,c;cin >> a >> b >> c;\n a--;b--;\n v[c].emplace_back(a,b);\n vv[a].emplace_back(b);\n vv[b].emplace_back(a);\n w[a].emplace_back(c);\n w[b].emplace_back(c);\n }\n vi is(n,1);\n UnionFindUndo uf(n);\n for(int i = 100000;i >= 0;i--){\n rep(j,sz(v[i])){\n auto [a,b] = v[i][j];\n uf.unite(a,b);\n }\n }\n auto ch = [&](int k,int i)->void{\n queue<int> que;que.push(k);\n is[k] = 0;\n while(!que.empty()){\n int ov = que.front();que.pop();\n rep(j,sz(vv[ov])){\n int nv = vv[ov][j],c = w[ov][j];\n if(c <= i)continue;\n if(!is[nv])continue;\n is[nv] = 0;\n que.push(nv);\n }\n }\n };\n multiset<int> st;\n st.insert(n);\n rep(i,100010){\n rep(j,sz(v[i])){\n auto [a,b] = v[i][sz(v[i])-1-j];\n int k = uf.size(a);\n uf.undo();\n if(!is[a])continue;\n if(uf.find(a,b))continue;\n st.erase(st.find(k));\n st.insert(uf.size(a));\n st.insert(uf.size(b));\n }\n int mx = *st.rbegin();\n while(1){\n int k = *st.begin();\n if(k < mx)st.erase(st.begin());\n else break;\n }\n rep(j,sz(v[i])){\n auto [a,b] = v[i][j];\n if(uf.size(a) < mx && is[a]){\n ch(a,i);\n }\n if(uf.size(b) < mx && is[b]){\n ch(b,i);\n }\n }\n }\n vi res;\n rep(i,n)if(is[i])res.emplace_back(i+1);\n cout << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 22308, "score_of_the_acc": -0.6356, "final_rank": 8 }, { "submission_id": "aoj_1645_9462077", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\n#include<bits/stdc++.h>\nusing namespace std;\nstruct dsu{\n private:\n\tvector<int>parent;\n public:\n dsu()=default;\n\t dsu(size_t n):parent(n,-1){}\n\t int leader(int i){\n\t\t if(parent[i]<0)return i;\n\t\t return parent[i]<0?i:(parent[i]=leader(parent[i]));\n\t }\n\t int merge(int a,int b){\n\t\t a=leader(a);\n\t\t b=leader(b);\n\t\t if (a!=b){\n\t\t\t if(-parent[a]<-parent[b])swap(a,b);\n\t\t\t parent[a]+=parent[b];\n\t\t\t parent[b]=a;\n\t\t }\n\t\t return a;\n\t }\n\t bool same(int a,int b){return leader(a)==leader(b);}\n\t int size(int i){return -parent[leader(i)];}\n\t vector<vector<int>>groups(){\n\t size_t n=parent.size();\n\t vector<vector<int>>A(n);\n\t for(int i=0;i<n;i++)A[leader(i)].emplace_back(i);\n\t vector<vector<int>>res;\n\t for(auto a:A)if(a.size())res.emplace_back(a);\n\t return res;\n\t }\n};\nint main(){\n while(1){\n int N,M;cin>>N>>M;\n if(!N)return 0;\n vector<vector<pi>>E(1e5);\n REP(i,M){\n ll a,b,s;cin>>a>>b>>s;a--;b--;s--;\n E[s].emplace_back(pi(a,b));\n }\n set<pi>S;\n REP(i,N)S.insert(pi(1,i));\n vector<vector<pair<pi,pair<pi,pi>>>>T(1e5);\n dsu D(N);\n RREP(i,1e5){\n for(auto[a,b]:E[i])if(!D.same(a,b)){\n a=D.leader(a);b=D.leader(b);\n auto p=pi(D.size(a),a);\n auto q=pi(D.size(b),b);\n S.erase(p);S.erase(q);\n ll c=D.merge(a,b);\n T[i].emplace_back(make_pair(pi(D.size(c),c),make_pair(p,q)));\n S.insert(pi(D.size(c),c));\n }\n }\n REP(i,1e5){\n reverse(ALL(T[i]));\n for(auto[p,q]:T[i]){\n if(S.count(p)){\n S.erase(p);\n S.insert(q.F);\n S.insert(q.S);\n }\n }\n ll f=(*prev(S.end())).F;\n while((*S.begin()).F!=f){S.erase(S.begin());}\n }\n vi ans;\n for(auto p:S)ans.emplace_back(p.S+1);\n sort(ALL(ans));\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 17920, "score_of_the_acc": -0.6689, "final_rank": 13 }, { "submission_id": "aoj_1645_9378543", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nclass union_find {\n public:\n union_find(int n) : data(n, -1) {}\n int unite(int x, int y) {\n x = root(x), y = root(y);\n if(x != y) {\n // if(size(x) < size(y)) swap(x, y);\n data[x] += data[y];\n return data[y] = x;\n }\n return -1;\n }\n int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }\n int size(int x) { return -data[root(x)]; }\n bool same(int x, int y) { return root(x) == root(y); }\n\n private:\n vector<int> data;\n};\n\n\nvector<int> solve(int n, int m) {\n map<int, vector<pair<int,int>>> edges;\n for(int _ : rep(m)) {\n int a = in(), b = in(), s = in(); a--, b--;\n edges[-s].push_back({a, b});\n }\n\n struct D {\n vector<int> vertex;\n vector<int> weight;\n vector<int> size;\n D() {}\n };\n auto cmp = [&](const D& L, const D& R) -> int {\n const int nL = L.weight.size();\n const int nR = R.weight.size();\n int pL = nL - 1;\n int pR = nR - 1;\n while(0 <= pL and 0 <= pR) {\n if(L.size[pL] == R.size[pR]) {\n if(L.weight[pL] == R.weight[pR]) {\n pL--, pR--;\n } else {\n return L.weight[pL] > R.weight[pR] ? -1 : +1;\n }\n } else {\n return L.size[pL] > R.size[pR] ? -1 : +1;\n }\n }\n if(pL == -1 and pR == -1) {\n return 0;\n } else {\n return pR == -1 ? -1 : +1;\n }\n };\n \n union_find uf(n);\n vector< D > data(n);\n for(int x : rep(n)) data[x].vertex.push_back(x);\n for(const auto &[s_, es] : edges) {\n const int s = - s_;\n set<int> cand;\n for(const auto &[u, v] : es) {\n int ru = uf.root(u);\n int rv = uf.root(v);\n if(uf.same(ru, rv)) continue;\n int p = uf.unite(ru, rv);\n cand.insert(p);\n const int c = cmp(data[ru], data[rv]);\n if(c == 0) {\n assert(ru == p || rv == p);\n int q = (ru ^ rv ^ p);\n if(data[p].vertex.size() < data[q].vertex.size()) swap(data[p].vertex, data[q].vertex);\n for(int x : data[q].vertex) data[p].vertex.push_back(x);\n } else {\n int q = (c == -1 ? ru : rv);\n swap(data[p].vertex, data[q].vertex);\n swap(data[p].weight, data[q].weight);\n swap(data[p].size , data[q].size );\n }\n }\n \n for(int x : cand) if(uf.root(x) == x) {\n data[x].weight.push_back(s);\n data[x].size.push_back(uf.size(x));\n }\n }\n vector<int> ans = data[uf.root(0)].vertex;\n sort(ans);\n ans++;\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(), m = in();\n if(make_pair(n, m) == make_pair(0, 0)) return 0;\n print(solve(n, m));\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 27172, "score_of_the_acc": -0.7541, "final_rank": 16 }, { "submission_id": "aoj_1645_9369951", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, n) for(int i = a; i < n; i++)\n#define rrep(i, a, n) for(int i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n\n// using mint = modint1000000007;\n// using mint = modint998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n// ここから\nclass RollbackUnionFind {\n vector<ll> parent;\n stack<pair<int, int>> history;\n int inner_snap;\n\n\npublic:\n RollbackUnionFind(ll n_ = 1): inner_snap(0){ parent.assign(n_, -1); }\n\n inline int find(int x){\n // 経路圧縮を行わない\n return (parent[x] < 0? x:find(parent[x]));\n }\n\n inline bool same(int x, int y){\n return find(x) == find(y);\n }\n\n inline bool unite(int x, int y){\n x = find(x), y = find(y);\n history.emplace(x, parent[x]);\n history.emplace(y, parent[y]);\n if(x == y) return false;\n if(parent[x] > parent[y]) swap(x, y);\n parent[x] += parent[y];\n parent[y] = x;\n return true;\n }\n\n // rollback処理(stackの上2つを使って戻す)\n inline void undo(){\n parent[history.top().first] = history.top().second;\n history.pop();\n parent[history.top().first] = history.top().second;\n history.pop();\n }\n\n void snapshot() { inner_snap = int(history.size() >> 1); }\n\n int get_state() { return int(history.size() >> 1); }\n\n void rollback(int state = -1) {\n if (state == -1) state = inner_snap;\n state <<= 1;\n assert(state <= (int)history.size());\n while (state < (int)history.size()) undo();\n }\n\n inline int size(int x){\n return (-parent[find(x)]);\n }\n\n inline int operator[](int x){\n return find(x);\n }\n};\n\nint main(){\n while(true){\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) break;\n\n vector<vector<pair<int, int>>> edge(100005);\n vector<vector<bool>> connect(100005);\n vector<vector<pair<int, int>>> g(n);\n rep(i, 0, m){\n int a, b, s; cin >> a >> b >> s;\n a--, b--;\n edge[s].push_back({a, b});\n g[a].push_back({b, s});\n g[b].push_back({a, s});\n }\n RollbackUnionFind uf(n);\n rrep(i, 100000, 1){\n for(auto [u, v]: edge[i]){\n connect[i].push_back(!uf.same(u, v));\n uf.unite(u, v);\n }\n reverse(edge[i].begin(), edge[i].end());\n reverse(connect[i].begin(), connect[i].end());\n }\n set<pair<int, int>> c;\n c.insert({uf.size(0), uf[0]});\n vector<bool> ng(n, false);\n rep(i, 1, 100001){\n rep(j, 0, edge[i].size()){\n auto [u, v] = edge[i][j];\n // cout << u << ' ' << v << ' ' << (connect[i][j] ? \"connect\": \"no\") << endl;\n int sz = uf.size(u), root = uf[u];\n uf.undo();\n if(!connect[i][j]) continue;\n int pu = uf[u], pv = uf[v];\n if(!ng[u] && !ng[v] && pu != pv){\n c.erase(c.find({sz, root}));\n c.insert({uf.size(u), pu});\n c.insert({uf.size(v), pv});\n }\n }\n\n int mx = (*c.rbegin()).first;\n if(mx == 1) break;\n while((*c.begin()).first < mx){\n auto [s, r] = (*c.begin());\n c.erase(c.begin());\n queue<int> que;\n que.push(r);\n ng[r] = 1;\n while(!que.empty()){\n int q = que.front(); que.pop();\n for(auto [nq, cost]: g[q]){\n if(cost <= i) continue;\n if(ng[nq]) continue;\n ng[nq] = 1;\n que.push(nq);\n }\n }\n }\n }\n\n vector<int> ans;\n rep(i, 0, n){\n if(ng[i]) continue;\n ans.push_back(i+1);\n }\n cout << ans[0];\n rep(i, 1, ans.size()){\n cout << ' ' << ans[i];\n }\n cout << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 23644, "score_of_the_acc": -0.7545, "final_rank": 17 }, { "submission_id": "aoj_1645_9369948", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, n) for(int i = a; i < n; i++)\n#define rrep(i, a, n) for(int i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n\n// using mint = modint1000000007;\n// using mint = modint998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n// ここから\nclass RollbackUnionFind {\n vector<ll> parent;\n stack<pair<int, int>> history;\n int inner_snap;\n\n\npublic:\n RollbackUnionFind(ll n_ = 1): inner_snap(0){ parent.assign(n_, -1); }\n\n inline int find(int x){\n // 経路圧縮を行わない\n return (parent[x] < 0? x:find(parent[x]));\n }\n\n inline bool same(int x, int y){\n return find(x) == find(y);\n }\n\n inline bool unite(int x, int y){\n x = find(x), y = find(y);\n history.emplace(x, parent[x]);\n history.emplace(y, parent[y]);\n if(x == y) return false;\n if(parent[x] > parent[y]) swap(x, y);\n parent[x] += parent[y];\n parent[y] = x;\n return true;\n }\n\n // rollback処理(stackの上2つを使って戻す)\n inline void undo(){\n parent[history.top().first] = history.top().second;\n history.pop();\n parent[history.top().first] = history.top().second;\n history.pop();\n }\n\n void snapshot() { inner_snap = int(history.size() >> 1); }\n\n int get_state() { return int(history.size() >> 1); }\n\n void rollback(int state = -1) {\n if (state == -1) state = inner_snap;\n state <<= 1;\n assert(state <= (int)history.size());\n while (state < (int)history.size()) undo();\n }\n\n inline int size(int x){\n return (-parent[find(x)]);\n }\n\n inline int operator[](int x){\n return find(x);\n }\n};\n\nint main(){\n while(true){\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) break;\n\n vector<vector<pair<int, int>>> edge(100005);\n vector<vector<bool>> connect(100005);\n vector<vector<pair<int, int>>> g(n);\n rep(i, 0, m){\n int a, b, s; cin >> a >> b >> s;\n a--, b--;\n edge[s].push_back({a, b});\n g[a].push_back({b, s});\n g[b].push_back({a, s});\n }\n RollbackUnionFind uf(n);\n rrep(i, 100000, 1){\n for(auto [u, v]: edge[i]){\n connect[i].push_back(!uf.same(u, v));\n uf.unite(u, v);\n }\n reverse(edge[i].begin(), edge[i].end());\n reverse(connect[i].begin(), connect[i].end());\n }\n set<pair<int, int>> c;\n c.insert({uf.size(0), uf[0]});\n vector<bool> ng(n, false);\n rep(i, 1, 100001){\n rep(j, 0, edge[i].size()){\n auto [u, v] = edge[i][j];\n // cout << u << ' ' << v << ' ' << (connect[i][j] ? \"connect\": \"no\") << endl;\n int sz = uf.size(u), root = uf[u];\n uf.undo();\n if(!connect[i][j]) continue;\n // cout << u << ' ' << v << ' ' << uf[u] << ' ' << uf[v] << endl;\n if(!ng[u] && !ng[v] && uf[u] != uf[v]){\n c.erase(c.find({sz, root}));\n c.insert({uf.size(u), uf[u]});\n c.insert({uf.size(v), uf[v]});\n }\n }\n\n int mx = (*c.rbegin()).first;\n if(mx == 1) break;\n while((*c.begin()).first < mx){\n auto [s, r] = (*c.begin());\n c.erase(c.begin());\n queue<int> que;\n que.push(r);\n ng[r] = 1;\n while(!que.empty()){\n int q = que.front(); que.pop();\n for(auto [nq, cost]: g[q]){\n if(cost <= i) continue;\n if(ng[nq]) continue;\n ng[nq] = 1;\n que.push(nq);\n }\n }\n }\n }\n\n vector<int> ans;\n rep(i, 0, n){\n if(ng[i]) continue;\n ans.push_back(i+1);\n }\n cout << ans[0];\n rep(i, 1, ans.size()){\n cout << ' ' << ans[i];\n }\n cout << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 23880, "score_of_the_acc": -0.7704, "final_rank": 18 }, { "submission_id": "aoj_1645_9369938", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, n) for(int i = a; i < n; i++)\n#define rrep(i, a, n) for(int i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n\n// using mint = modint1000000007;\n// using mint = modint998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n// ここから\nclass RollbackUnionFind {\n vector<ll> parent;\n stack<pair<int, int>> history;\n int inner_snap;\n\n\npublic:\n RollbackUnionFind(ll n_ = 1): inner_snap(0){ parent.assign(n_, -1); }\n\n inline int find(int x){\n // 経路圧縮を行わない\n return (parent[x] < 0? x:find(parent[x]));\n }\n\n inline bool same(int x, int y){\n return find(x) == find(y);\n }\n\n inline bool unite(int x, int y){\n x = find(x), y = find(y);\n history.emplace(x, parent[x]);\n history.emplace(y, parent[y]);\n if(x == y) return false;\n if(parent[x] > parent[y]) swap(x, y);\n parent[x] += parent[y];\n parent[y] = x;\n return true;\n }\n\n // rollback処理(stackの上2つを使って戻す)\n inline void undo(){\n parent[history.top().first] = history.top().second;\n history.pop();\n parent[history.top().first] = history.top().second;\n history.pop();\n }\n\n void snapshot() { inner_snap = int(history.size() >> 1); }\n\n int get_state() { return int(history.size() >> 1); }\n\n void rollback(int state = -1) {\n if (state == -1) state = inner_snap;\n state <<= 1;\n assert(state <= (int)history.size());\n while (state < (int)history.size()) undo();\n }\n\n inline int size(int x){\n return (-parent[find(x)]);\n }\n\n inline int operator[](int x){\n return find(x);\n }\n};\n\nint main(){\n while(true){\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) break;\n\n vector<vector<pair<int, int>>> edge(100005);\n vector<vector<bool>> connect(100005);\n vector<vector<pair<int, int>>> g(n);\n rep(i, 0, m){\n int a, b, s; cin >> a >> b >> s;\n a--, b--;\n edge[s].push_back({a, b});\n g[a].push_back({b, s});\n g[b].push_back({a, s});\n }\n RollbackUnionFind uf(n);\n rrep(i, 100000, 1){\n for(auto [u, v]: edge[i]){\n connect[i].push_back(!uf.same(u, v));\n uf.unite(u, v);\n }\n reverse(edge[i].begin(), edge[i].end());\n reverse(connect[i].begin(), connect[i].end());\n }\n set<pair<int, int>> c;\n c.insert({uf.size(0), uf[0]});\n vector<bool> ng(n, false);\n rep(i, 1, 100001){\n rep(j, 0, edge[i].size()){\n auto [u, v] = edge[i][j];\n // cout << u << ' ' << v << ' ' << (connect[i][j] ? \"connect\": \"no\") << endl;\n int sz = uf.size(u), root = uf[u];\n uf.undo();\n if(!connect[i][j]) continue;\n // cout << u << ' ' << v << ' ' << uf[u] << ' ' << uf[v] << endl;\n if(!ng[u] && !ng[v] && uf[u] != uf[v]){\n c.erase(c.find({sz, root}));\n c.insert({uf.size(u), uf[u]});\n c.insert({uf.size(v), uf[v]});\n }\n }\n\n int mx = (*c.rbegin()).first;\n if(mx == 1) break;\n set<pair<int, int>> c_ = c;\n for(auto [s, r]: c){\n if(s == mx) break;\n queue<int> que;\n que.push(r);\n ng[r] = 1;\n while(!que.empty()){\n int q = que.front(); que.pop();\n for(auto [nq, cost]: g[q]){\n if(cost <= i) continue;\n if(ng[nq]) continue;\n ng[nq] = 1;\n que.push(nq);\n }\n }\n c_.erase(c_.begin());\n }\n swap(c_, c);\n }\n\n vector<int> ans;\n rep(i, 0, n){\n if(ng[i]) continue;\n ans.push_back(i+1);\n }\n cout << ans[0];\n rep(i, 1, ans.size()){\n cout << ' ' << ans[i];\n }\n cout << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1250, "memory_kb": 23808, "score_of_the_acc": -1.5374, "final_rank": 20 } ]
aoj_1649_cpp
Hundred-Cell Calculation Puzzles The hundred-cell calculation is a kind of calculation training. In hundred-cell calculation, a sheet with ten by ten empty cells is given. Numbers, 0 through 9, are written on the top of the ten columns in an arbitrary order. 0 through 9 are also written at the left of the ten rows in an arbitrary order. You are to fill the empty cells with the sums of the top and left numbers, as an example shown in the figure below. You may think of more generalized drills. Such a drill has different numbers of the columns and rows, say w × h. The numbers to be written on the top and the left can be any integers. Hideo is designing a puzzle based on the generalized hundred-cell calculation. A sheet is given in which some of answer cells are filled with numbers, but numbers on the top and the left are omitted. The puzzle is to find these omitted numbers consistent with the filled numbers. Hideo decided to construct puzzles by preparing sheets filled with all the correct sums and then removing some of these sums. This guarantees that the puzzle has at least one solution. The existence of the solution, however, is not enough; it should be unique. Hideo has found through many trials that, when the leftmost of the numbers on the top is fixed to be 0, puzzles with w × h cells may have a unique solution when w + h − 1 sums are kept. He, however, could not figure out which cells to keep for a single unique solution. Your task is to help Hideo by writing a program that judges solution uniqueness for each of the puzzle candidates he has made. Input The input consists of at most 100 datasets, each in the following format. w h x 1 y 1 n 1 ... x k y k n k w and h in the first line are the numbers of answer cells in one row and in one column, respectively (2 ≤ w ≤ 100 and 2 ≤ h ≤ 100). There remain k numbers ( k = w + h − 1) in the answer cells. The k lines starting from the second show that the number n i is in the cell x i -th from the left and y i -th from the top. x i , y i , n i are integers satisfying 1 ≤ x i ≤ w , 1 ≤ y i ≤ h , and −100 ≤ n i ≤ 100. Here, x = 1 means the leftmost and y = 1 means the topmost. Either of x i ≠ x j or y i ≠ y j holds for different i and j . The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output YES if the puzzle has a unique solution, and NO otherwise, in a line. Sample Input 2 2 1 1 10 1 2 1 2 2 5 3 3 1 1 1 1 2 2 2 2 3 2 3 4 3 3 5 3 3 1 1 1 1 3 3 2 2 3 3 1 3 3 3 5 3 2 1 1 8 1 2 7 2 1 7 3 2 5 6 6 1 1 -2 1 4 4 2 2 2 3 3 3 3 4 8 4 2 -4 4 6 1 5 5 4 5 6 5 6 1 -7 6 3 -6 6 6 1 2 3 1 4 0 2 1 -3 2 3 -1 3 1 0 3 2 6 4 6 0 5 4 -5 5 5 -10 6 3 -6 6 5 -10 6 6 1 5 -2 2 5 -1 3 5 -7 4 1 5 4 2 -1 4 3 4 4 4 3 4 5 -1 4 6 2 5 5 -6 6 5 0 2 6 1 1 -2 1 2 -1 1 3 -3 1 4 -2 1 5 -5 1 6 -2 2 4 0 0 0 Output for the Sample Input YES YES NO YES NO NO YES YES
[ { "submission_id": "aoj_1649_10643103", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define ld long double\nusing namespace std;\n\n\nll detA(vector<vector<ld>> M, ll n){\n ll t = 1;\n ll det = 1;\n for (int i = 1; i < n; i++){\n if (M[i][i] == 0){\n for (int j = i; j < n; j++){\n if (M[j][i] != 0){\n swap(M[j],M[i]);\n break;\n }\n }\n }\n ld first = M[i][i];\n if (first == 0){\n return 0;\n }\n for (int j = i+1; j < n; j++){\n if (M[j][i] != 0){\n ld t = M[j][i];\n for (ll k = i; k < n; k++){\n M[j][k] -= (t/first)*M[i][k];\n }\n }\n }\n }\n for (ll l = 0; l < n; l++){\n det *= M[l][l];\n }\n return det;\n}\n\n\nint main(){\n while (true){\n ll H,W;\n cin >> W >> H;\n if (H == 0 and W == 0){\n break;\n }\n else{\n vector<vector<ld>> A(H+W,vector<ld>(H+W,0));\n A[0][0] = 1;\n for (ll i = 1; i < H+W; i++){\n ll x,y,z;\n cin >> x >> y >> z;\n A[i][x-1] = 1;\n A[i][W+y-1] = 1;\n }\n \n bool a;\n\n if (detA(A,H+W) == 0){\n a = false;\n }\n else{\n a = true;\n }\n\n \n \n vector<string> T {\"NO\", \"YES\"};\n cout << T[a] << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4500, "score_of_the_acc": -0.4466, "final_rank": 17 }, { "submission_id": "aoj_1649_10643002", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/modint>\n#include <deque>\n#include <atcoder/scc>\nusing namespace std;\nusing namespace atcoder;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll=vector<vector<ll>>;\nusing Graph=vvll;\nusing Edgegraph=vector<vector<pair<ll,ll>>>;\nusing vch=vector<char>;\nusing vvch=vector<vector<char>>;\nusing P=pair<ll,ll>;\nusing vP=vector<P>;\nusing tup=tuple<ll,ll,ll>;\nusing vbl=vector<bool>;\nusing vvbl=vector<vbl>;\nusing mint = atcoder::modint998244353;\nconst int infint = 1073741823;\nconst ll inf = 1LL << 60;\ntemplate <class T> inline bool chmax(T& a,T b){if (a<b){a=b;return 1;}return 0;}\ntemplate <class T> inline bool chmin(T& a,T b){if (a>b){a=b;return 1;}return 0;}\n#define rep(i,x,lim) for(ll i = (x);i < (ll)(lim);i++)\n#define rep2(j,x,lim) for(int j = (x);j < (int)(lim);j++)\n\nset<ll> Hseen;\nset<ll> Wseen;\n\nvoid bfs(Graph WG,Graph HG,P p){\n ll w=p.first;\n ll h=p.second;\n Hseen.insert(h);\n Wseen.insert(w);\n for(ll next:WG[w]){\n if(!Hseen.count(next)) bfs(WG,HG,make_pair(w,next));\n }\n for(ll next:HG[h]){\n if(!Wseen.count(next)) bfs(WG,HG,make_pair(next,h));\n }\n}\nint main(){\n vbl ans;\n while(1){\n ll W,H;\n cin >> W >> H;\n ll lastx,lasty;\n if(W==0&&H==0) break;\n Graph WG(101);\n Graph HG(101);\n rep(i,0,W+H-1){\n ll x,y,n;\n cin >> x >> y >> n;\n x--;y--;\n WG[x].push_back(y);\n HG[y].push_back(x);\n lastx=x;\n lasty=y;\n }\n bfs(WG,HG,make_pair(lastx,lasty));\n if(Hseen.size()==H&&Wseen.size()==W) ans.push_back(true);\n else ans.push_back(false);\n Hseen.clear();\n Wseen.clear();\n }\n rep(i,0,ans.size()){\n if(ans[i]) cout << \"YES\" << '\\n';\n else cout << \"NO\" << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6096, "score_of_the_acc": -1.0125, "final_rank": 19 }, { "submission_id": "aoj_1649_10592920", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/dsu>\nusing namespace std;\nusing namespace atcoder;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n int T = 1;\n // cin >> T;\n while(solve());\n return 0;\n}\n\nbool solve() {\n int W,H;cin>>W>>H;\n if(W==0)return 0;\n vector ex(H,vector<bool>(W));\n for(int i=0;i<W+H-1;i++){\n int x,y,n;cin>>x>>y>>n;\n x--,y--;\n ex[y][x]=true;\n }\n\n queue<pii>q;\n rep(i,0,H)if(ex[i][0])q.emplace(i,0);\n\n vector<bool>rok(H),cok(W);\n cok[0]=true;\n\n while(!q.empty()){\n auto [r,c]=q.front();\n cok[c]=rok[r]=true;\n ex[r][c]=false;\n q.pop();\n\n rep(i,0,H)if(ex[i][c])q.emplace(i,c);\n rep(i,0,W)if(ex[r][i])q.emplace(r,i);\n }\n\n int ok_sum=0;\n rep(i,0,H)ok_sum+=rok[i];\n rep(i,0,W)ok_sum+=cok[i];\n\n cout<<((ok_sum==W+H)?\"YES\":\"NO\")<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3712, "score_of_the_acc": -0.1921, "final_rank": 13 }, { "submission_id": "aoj_1649_10555530", "code_snippet": "//#pragma GCC target(\"avx2\")\n//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pli = pair<ll,int>;\n#define AMARI 998244353\n//#define AMARI 1000000007\n#define el '\\n'\n#define El '\\n'\n#define YESNO(x) ((x) ? \"Yes\" : \"No\")\n#define YES YESNO(true)\n#define NO YESNO(false)\n#define REV_PRIORITY_QUEUE(tp) priority_queue<tp,vector<tp>,greater<tp>>\n#define EXIT_ANS(x) {cout << (x) << '\\n'; return;}\ntemplate <typename T> void inline SORT(vector<T> &v){sort(v.begin(),v.end()); return;}\ntemplate <typename T> void inline VEC_UNIQ(vector<T> &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}\ntemplate <typename T> T inline MAX(vector<T> &v){return *max_element(v.begin(),v.end());}\ntemplate <typename T> T inline MIN(vector<T> &v){return *min_element(v.begin(),v.end());}\ntemplate <typename T> T inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}\ntemplate <typename T> void inline DEC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;}\ntemplate <typename T> void inline INC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;}\nvoid inline TEST(void){cerr << \"TEST\" << endl; return;}\ntemplate <typename T> bool inline chmin(T &x,T y){\n if(x > y){\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T> bool inline chmax(T &x,T y){\n if(x < y){\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T = long long> vector<T> inline get_vec(int n){\n vector<T> ans(n);\n for(int i = 0; i < n; i++)cin >> ans[i];\n return ans;\n}\ntemplate <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){\n int n = (int)vec.size();\n for(int i = 0; i < n; i++){\n cout << vec[i];\n if(kaigyou || i == n - 1)cout << '\\n';\n else cout << ' ';\n }\n if(!n)cout << '\\n';\n return;\n}\ntemplate <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){\n int n = (int)vec.size();\n for(int i = 0; i < n; i++){\n cerr << vec[i];\n if(kaigyou || i == n - 1)cerr << '\\n';\n else cerr << ' ';\n }\n if(!n)cerr << '\\n';\n return;\n}\nvector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false){\n if(m == -1)m = n - 1;\n vector<vector<int>> g(n);\n while(m--){\n int u,v;\n cin >> u >> v;\n u--; v--;\n g[u].push_back(v);\n if(!direct)g[v].push_back(u);\n }\n return g;\n}\n\nvector<int> make_inv(vector<int> p){\n int n = (int)p.size();\n vector<int> ans(n);\n for(int i = 0; i < n; i++)ans[p[i]] = i;\n return ans;\n}\n\n#define MULTI_TEST_CASE false\nvoid solve(void){\n ll w,h;\n cin >> w >> h;\n if(w == 0 && h == 0){exit(0);}\n vector<ll> a(w,-1);\n vector<ll> b(h,-1);\n a[0] = 0;\n vector<vector<ll>> v(h,vector<ll>(w,-1));\n for(int i=0;i<w+h-1;i++){\n ll x,y,z;\n cin >> y >> x >> z;\n x--;\n y--;\n v[x][y] = 0;\n }\n //return;\n for(int i=0;i < 2*(h+w);i++){\n for(int j=0;j<h;j++){\n for(int k=0;k<w;k++){\n if(a[k] != -1 && b[j] != -1){\n v[j][k] = 0;\n }\n if(v[j][k] != -1){\n if(a[k] != -1){\n b[j] = 0;\n }\n if(b[j] != -1){\n a[k] = 0;\n }\n }\n }\n }\n }\n\n ll ans = 0;\n for(int i=0;i<h;i++){\n if(b[i] != -1){ans++;}\n }\n for(int i=0;i<w;i++){\n if(a[i] != -1){ans++;}\n }\n cout << ((ans == w+h)?\"YES\":\"NO\") << endl;\n return;\n}\n\nvoid calc(void){\n return;\n}\n\nsigned main(void){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n calc();\n int t = 1;\n if(MULTI_TEST_CASE)cin >> t;\n while(1){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3584, "score_of_the_acc": -0.1852, "final_rank": 12 }, { "submission_id": "aoj_1649_10390033", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\n\nvoid solve(ll h,ll w,vpll a){\n vll x(h,-1);\n vll y(w,-1);\n y[0] = 0;\n vvll check(h,vll(w,0));\n for(auto [y,x]:a){\n check[x-1][y-1] = 1;\n }\n rep(l,h+w){\n rep(i,h){\n rep(j,w){\n if(x[i] == 0 && y[j] == 0){\n check[i][j] = 1;\n }\n if(check[i][j] && x[i] == 0){\n y[j] = 0;\n }\n if(check[i][j] && y[j] == 0){\n x[i] = 0;\n }\n }\n }\n }\n rep(i,h){\n rep(j,w){\n if(check[i][j] == 0){\n print(\"NO\");\n return;\n }\n }\n }\n print(\"YES\");\n}\n\nint main(){\n while(1){\n LL(w,h);\n if(w == 0){break;}\n vpll a;\n rep(i,h+w-1){\n LL(p,q,r);\n a.push_back({p,q});\n }\n solve(h,w,a);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3584, "score_of_the_acc": -0.154, "final_rank": 11 }, { "submission_id": "aoj_1649_9419161", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef unsigned long long ull;\ntypedef long long ll;\n\nint main(void){\n vector<string> ans;\n while(1){\n int h,w;cin>>w>>h;\n if(h==0)break;\n vector<int> x(w,999),y(h,999);\n x[0]=0;\n vector<vector<int>> g(h,vector<int>(w,999));\n for(int k=0;k<w+h-1;k++){\n int t,s;cin>>t>>s;\n t--;s--;\n cin>>g[s][t];\n if(t==0){\n y[s]=g[s][t];\n }\n }\n for(int p=0;p<100;p++){\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(g[i][j]!=999&&y[i]!=999){\n x[j]=g[i][j]-y[i];\n }\n if(g[i][j]!=999&&x[j]!=999){\n y[i]=g[i][j]-x[j];\n }\n if(y[i]!=999&&x[j]!=999){\n g[i][j]=y[i]+x[j];\n }\n }\n }\n }\n bool f=true;\n for(int i=0;i<h;i++){\n if(y[i]==999){\n f=false;\n break;\n }\n }\n for(int i=0;i<w;i++){\n if(x[i]==999){\n f=false;\n break;\n }\n }\n if(f){\n ans.push_back(\"YES\");\n }else{\n ans.push_back(\"NO\");\n }\n }\n for(string x:ans)cout<<x<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3412, "score_of_the_acc": -0.0881, "final_rank": 9 }, { "submission_id": "aoj_1649_9418120", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while (true) {\n int w, h;\n cin >> w >> h;\n if (w == 0 && h == 0) {\n break;\n }\n\n vector<vector<int>> grid(h, vector<int>(w, 10000));\n vector<int> top(w, 10000);\n vector<int> left(h, 10000);\n\n for (int i = 0; i < w + h - 1; ++i) {\n int x, y, n;\n cin >> x >> y >> n;\n grid[y-1][x-1] = n;\n }\n\n top[0] = 0;\n queue<pair<int, int>> q;\n q.push({0, 0}); // second 0 == top, 1 == left\n\n while (!q.empty()) {\n auto v = q.front();\n q.pop();\n if (v.second == 0) {\n for (int k = 0; k < h; ++k) {\n if (grid[k][v.first] != 10000 && left[k] == 10000) {\n left[k] = grid[k][v.first] - v.first;\n q.push({k, 1});\n }\n }\n } else {\n for (int k = 0; k < w; ++k) {\n if (grid[v.first][k] != 10000 && top[k] == 10000) {\n top[k] = grid[v.first][k] - v.first;\n q.push({k, 0});\n }\n }\n }\n\n for (int y = 0; y < h; ++y) {\n for (int x = 0; x < w; ++x) {\n if (left[y] != 10000 && top[x] != 10000) {\n grid[y][x] = left[y] + top[x];\n }\n }\n }\n }\n\n string ans = \"YES\";\n for (int y = 0; y < h; ++y) {\n if (left[y] == 10000) {\n ans = \"NO\";\n break;\n }\n }\n\n for (int x = 0; x < w; ++x) {\n if (top[x] == 10000) {\n ans = \"NO\";\n break;\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3404, "score_of_the_acc": -0.0791, "final_rank": 6 }, { "submission_id": "aoj_1649_9417837", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nconst int initial = -101;\n\n\n\nint main(){\n while(1){\n int w,h;\n cin >> w >> h;\n if(w==0&&h==0)break;\n vector <int> yoko(w,0),tate(h,0);\n vector <vector <int> > map(h,vector<int>(w,initial));\n yoko[0] = 1;\n for(int k=0;k<h+w-1;k++){\n int xi,yi,ni;\n cin >> xi >> yi >> ni;\n for(int i=0;i<w;i++){\n for(int j=0;j<h;j++){\n if(xi == i+1 && yi == j+1){\n map[j][i] = ni;\n }\n }\n }\n }\n queue<int> q;\n q.push(1);\n while(1){\n if(q.empty())break;\n else if(q.front() > 0){\n int tmp = q.front();\n tmp -= 1;\n q.pop();\n for(int i=0;i<h;i++){\n if(map[i][tmp] != initial){\n map[i][tmp] = initial;\n tate[i] = 1;\n q.push(-i-1);\n }\n } \n } \n else if(q.front() < 0){\n int tmp = -q.front();\n tmp -= 1;\n q.pop();\n for(int i=0;i<w;i++){\n if(map[tmp][i]!= initial){\n map[tmp][i] = initial;\n yoko[i] = 1;\n q.push(i+1);\n }\n }\n }\n \n }\n \n int flag = 0;\n for(int i=0;i<h;i++){\n if(tate[i] == 0){\n flag = 1;\n break;\n }\n }\n if(flag == 0){\n for(int i=0;i<w;i++){\n if(yoko[i] == 0){\n flag = 1;\n break;\n }\n }\n }\n if(flag == 1)cout << \"NO\" << endl;\n else cout << \"YES\" << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3212, "score_of_the_acc": -0.0125, "final_rank": 1 }, { "submission_id": "aoj_1649_9408423", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while(1){\n int h,w; cin>>w>>h;\n if(h == 0) break;\n vector<vector<int>> t(h,vector<int>(w,0));\n for(int i = 0; i<h+w-1; i++){\n int x,y,n; cin>>x>>y>>n; x--;y--;\n t[y][x] = 1;\n }\n\n\n for(int k = 0; k<w+5; k++){\n bool f = 1;\n for(int j = 0; j<h-1; j++){\n for(int j2 = j+1; j2<h; j2++){\n bool c = 0;\n for(int i = 0; i<w; i++){\n if(t[j][i] == 1 && t[j2][i] == 1){\n c = 1;\n break;\n }\n }\n if(c == 0) continue;\n for(int i = 0; i<w; i++){\n if(t[j][i] == 1 || t[j2][i] == 1){\n t[j][i] = 1;\n t[j2][i] = 1;\n }\n }\n }}\n for(int i = 0; i<h; i++){\n for(int j = 0; j<w; j++){\n if(t[i][j] == 0){\n f = 0;\n break;\n }\n }\n }\n if(f) break;\n }\n bool ans = 1;\n bool tate= 0, yoko = 0;\n for(int i = 0; i<h; i++){\n for(int j = 0; j<w; j++){\n if(t[i][j] == 0){\n /*if(tate == 0 && i < h-1 && t[i+1][j] == 0){\n tate = 1;\n for(int i2 = 0; i2<h; i2++) t[i2][j] = 1;\n continue;\n }else if(yoko == 0 && j < w-1 && t[i][j+1] == 0){\n yoko = 1;\n for(int j2 = 0; j2<w; j2++) t[i][j2] = 1;\n continue;\n }*/\n ans = 0;\n //break;\n }\n // cout<<t[i][j]<<\" \";\n }\n // cout<<endl;\n }\n \n cout<<(ans ? \"YES\" : \"NO\")<<endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3420, "score_of_the_acc": -0.3034, "final_rank": 15 }, { "submission_id": "aoj_1649_9384211", "code_snippet": "// clang-format off\n#include <bits/stdc++.h>\n\n// #pragma once\n\nusing namespace std;\n\nusing ll = int64_t;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing V = vector<ll>;\nusing M = vector<vector<ll>>;\n\n#define rep(i, n) for (ll i = 0; i < static_cast<ll>(n); ++i)\n#define rep_rev(i, n) for (ll i = n - 1; i >= 0; --i)\n#define rep2(i, a, b) for (ll i = a; i < static_cast<ll>(b); ++i)\n#define rep_rev2(i, a, b) for (ll i = b - 1; i >= a; --i)\n#define all(v) (v).begin(), (v).end()\n#define bit(n,k) (((n)>>(k))&1)\n#define isin(x, l, r) (l <= (x) && (x) < r)\n#define dx4 {-1, 0, 1, 0}\n#define dy4 {0, 1, 0, -1}\n#define dx8 {-1, -1, 0, 1, 1, 1, 0, -1}\n#define dy8 {0, 1, 1, 1, 0, -1, -1, -1}\n#define length(a) static_cast<ll>(a.size())\n#define get(x, i) get<i>(x)\n#define sign(x) ((x == 0) ? 0 : (x) / abs(x))\n#define ndigits(x) static_cast<ll>(floor(log10(x)) + 1)\n#define deg2rad(x) ((x) * M_PI / 180)\n#define rad2deg(x) ((x) * 180 / M_PI)\n#define log(x, y) (log(y) / log(x))\n#define scanvar(T, x) T x; cin >> x\n#define scanll(x) ll x; cin >> x\n#define scanv(T, v, n) vector<T> v; for (int i = 0; i < n; ++i) { T temp; cin >> temp; (v).push_back(temp); }\n#define scanvs(v) vector<char> v; do { scanvar(string, temp); for (char c : temp) {v.push_back(c); } } while (false)\n#define scanm(T, m, a, b) vector<vector<T>> m(a, vector<T>(b)); rep(i, a) { rep(j, b) { cin >> m[i][j]; } }\n#define scanms(m, a, b) vector<vector<char>> m(a, vector<char>(b)); rep(i,a) { scanvar(string, temp); rep(j, b) { m[i][j] = temp[j]; } }\n#define pl(x) cout << (x) << endl\n#define printld(x) printf(\"%.12Lf\\n\", x)\n#define printv(v) for (auto __x: v) {cout << __x << \" \";} cout << endl;\n#define printm(m) for (auto __v : m) { printv(__v); }\n\nll _pow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; }\ntemplate <class T1, class T2, class Func> void __map(vector<T1> v, vector<T2>& res, Func f) { int n = v.size(); rep(i, n) { res[i] = f(v[i]); } }\ntemplate <class T> void __unique(vector<T>& v) { auto p = unique(all(v)); v.erase(p, v.end()); }\ntemplate <class T> void __join(const vector<T> v, const string separator, string& res) { ll n = v.size(); rep(i, n - 1) { res += to_string(v[i]) + separator; } res += to_string(v[n - 1]); }\nvoid __joins(const vector<char> v, const string separator, string& res) { ll n = v.size(); rep(i, n - 1) { res += v[i] + separator; } if (n > 0) res += v[n - 1]; }\nvoid __to_string(ll x, ll num, string& res, ll pad = -1) { while (x >= num) { res = to_string(x % num) + res; x /= num; } if (x > 0) res = to_string(x) + res; ll d = pad - res.size(); rep(i, d) { res = '0' + res; } }\n\n// Debug\n#define DEBUG_MODE 1\n#define debug(x) if (DEBUG_MODE) { cout << \" \\033[35m\" << #x << \"\\033[m\" << \": \" << x << endl; }\n#define debugld(x) if (DEBUG_MODE) { cout << `\" \\033[35m`\" << #x << `\"\\033[m`\" << `\": `\"; printld(x);}\n#define debugv(v) if (DEBUG_MODE) { cout << \" \\033[35m\" << #v << \"\\033[m\" << \": \"; for (auto& __x : v) {cout << __x << \" \"; } cout << endl; }\n#define debugm(m) if (DEBUG_MODE) { cout << \" \\033[35m\" << #m << \"\\033[m\" << \":\\n\"; for (auto __v : m) { cout << \" \"; printv(__v); } cout << endl; }\n// clang-format on\n#if DEBUG_MODE\n\n#endif\n\nstruct Node {\n ll val = 0;\n bool visited = false;\n ll x = 0;\n ll y = 0;\n\n Node() {}\n Node(ll x, ll y) : x(x), y(y) {}\n\n void setVal(ll val) {\n this->val = val;\n this->visited = true;\n }\n\n void print() {\n debug(x);\n debug(y);\n debug(val);\n debug(visited);\n pl(\"\");\n }\n};\n\nvoid solve() {\n scanll(w);\n if (w == 0) exit(0);\n\n scanll(h);\n ll k = w + h - 1;\n scanm(ll, xyn, k, 3);\n\n vector<vector<Node>> m(h+1, vector<Node>(w+1));\n\n rep(i, h+1) {\n rep(j, w+1) {\n m[i][j] = Node(j, i);\n }\n }\n\n m[0][0].setVal(-1);\n\n rep(i, k) {\n ll x = xyn[i][0];\n ll y = xyn[i][1];\n ll n = xyn[i][2];\n m[y][x].setVal(n);\n }\n\n stack<Node*> s;\n m[0][1].setVal(0);\n s.push(&m[0][1]);\n\n auto cross = [&](ll x, ll y) {\n Node* left = &m[y][0];\n Node* upper = &m[0][x];\n Node* cur = &m[y][x];\n\n if (left == upper || left == cur || upper == cur) return;\n\n if (!left->visited && upper->visited && cur->visited) {\n left->setVal(cur->val - upper->val);\n s.push(left);\n }\n if (!upper->visited && left->visited && cur->visited) {\n upper->setVal(cur->val - left->val);\n s.push(upper);\n }\n if (!cur->visited && left->visited && upper->visited) {\n cur->setVal(left->val + upper->val);\n s.push(cur);\n }\n };\n\n while (!s.empty()) {\n Node* node = s.top();\n s.pop();\n\n ll x = node->x;\n ll y = node->y;\n\n rep(j, w+1) {\n if (j==x) continue;\n cross(j, y);\n }\n\n rep(i, h+1) {\n if (i==y) continue;\n cross(x, i);\n }\n }\n\n rep(i, h+1) {\n rep(j, w+1) {\n // m[i][j].print();\n if (!m[i][j].visited) {\n pl(\"NO\");\n return;\n }\n // cout << m[i][j].val << \" \";\n }\n // cout << endl;\n }\n\n pl(\"YES\");\n}\n\nint main() {\n while (true) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3800, "score_of_the_acc": -0.2601, "final_rank": 14 }, { "submission_id": "aoj_1649_9374843", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\nusing namespace std;\nusing namespace atcoder;\n\n#define all(v) v.begin(), v.end()\n#define ll long long\n#define ld long double\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<vvl> vvvl;\n\nint main()\n{\n while(true){\n ll w, h;\n cin >> w >> h;\n if(w == 0 && h == 0)return 0;\n ll k = w + h - 1;\n vl x(k), y(k), n(k);\n for (int i = 0; i < k;i++){\n cin >> x[i] >> y[i] >> n[i];\n }\n // in := 初期盤面\n vvl in(h, vl(w, 1e9));\n for (int i = 0; i < k;i++){\n in[y[i] - 1][x[i] - 1] = n[i];\n }\n bool ok = true;\n // 横方向のuf\n vl par(w);\n dsu uf1(w);\n for (int i = 0; i < w; i++)\n par[i] = i;\n for (int idx = 0; idx < h;idx++){\n for (int i = 0; i < w;i++){\n for (int j = i + 1; j < w;j++){\n if(in[idx][i] != 1e9 && in[idx][j] != 1e9){\n par[i] = par[j] = min(par[i], par[j]);\n uf1.merge(i, j);\n }\n }\n }\n }\n // for (int i = 0; i < w;i++){\n // if(par[i] != 0){\n // ok = false;\n // break;\n // }\n // }\n if(uf1.groups().size() != 1){\n ok = false;\n }\n // 縦方向\n vl par2(h);\n dsu uf2(h);\n for (int i = 0; i < h; i++)\n par2[i] = i;\n for (int idx = 0; idx < w; idx++){\n for (int i = 0; i < h;i++){\n for (int j = i + 1; j < h;j++){\n if(in[i][idx] != 1e9 && in[j][idx] != 1e9){\n par2[i] = par2[j] = min(par2[i], par2[j]);\n uf2.merge(i, j);\n }\n }\n }\n }\n // for (int i = 0; i < h;i++){\n // if(par2[i] != 0){\n // ok = false;\n // break;\n // }\n // }\n if(uf2.groups().size() != 1){\n ok = false;\n }\n if(ok)cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3432, "score_of_the_acc": -0.0825, "final_rank": 8 }, { "submission_id": "aoj_1649_9369993", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint checktate(vector<vector<int>> &board,vector<vector<int>> &clear,int h,int w){\n int change=0;\n for(int j=0;j<w;j++){\n for(int i=0;i<h;i++){\n if((clear.at(i).at(j)==2 || clear.at(i).at(j)==3) && board.at(i).at(j)!=-1000){\n for(int k=0;k<h;k++){\n if(clear.at(k).at(j)==0){\n clear.at(k).at(j)=1;\n change=1;\n }\n else if(clear.at(k).at(j)==2){\n clear.at(k).at(j)=3;\n change=1;\n }\n }\n }\n }\n }\n if(change){\n return 0;\n }\n return 1;\n}\n\nint checkyoko(vector<vector<int>> &board,vector<vector<int>> &clear,int h,int w){\n int change=0;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if((clear.at(i).at(j)==1 || clear.at(i).at(j)==3) && board.at(i).at(j)!=-1000){\n for(int k=0;k<w;k++){\n if(clear.at(i).at(k)==0){\n clear.at(i).at(k)=2;\n change=1;\n }\n else if(clear.at(i).at(k)==1){\n clear.at(i).at(k)=3;\n change=1;\n }\n \n }\n }\n }\n }\n if(change){\n return 0;\n }\n return 1;\n}\n\nint checkfull(vector<vector<int>> &clear,int h,int w){\n int ok=1;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(clear.at(i).at(j)!=3){\n ok=0;\n }\n }\n }\n return ok;\n}\n\nint main(){\n int w,h,a,b,c,ok;\n while(1){\n cin>>w>>h;\n if(w==0 && h==0){\n break;\n }\n vector<vector<int>> board(h,vector<int>(w,-1000));\n vector<vector<int>> clear(h,vector<int>(w,0));\n for(int i=0;i<w+h-1;i++){\n cin>>a>>b>>c;\n board.at(b-1).at(a-1)=c;\n }\n for(int i=0;i<h;i++){\n clear.at(i).at(0)=1;\n }\n int flag=2;\n while(flag>0){\n flag=2;\n flag-=checktate(board,clear,h,w);\n flag-=checkyoko(board,clear,h,w);\n }\n ok=checkfull(clear,h,w);\n if(ok){\n cout<<\"YES\"<<endl;\n }\n else{\n cout<<\"NO\"<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3460, "score_of_the_acc": -0.111, "final_rank": 10 }, { "submission_id": "aoj_1649_9347437", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nvoid solve(){\n while(true){\n int w,h;cin>>w>>h;\n if(max(w,h)==0)return;\n const ll k = w+h-1;\n vl x(k),y(k),n(k);rep(i,k)cin>>x[i]>>y[i]>>n[i];\n vvl A(h,vl(w,INF));\n rep(i,k){\n y[i]--;\n x[i]--;\n A[y[i]][x[i]] = n[i];\n }\n vl X(h,INF),Y(w,INF);\n Y[0] = 0;\n rep(hw,h*w+1){\n rep(i,h){\n rep(j,w){\n if(A[i][j]!=INF&&X[i]!=INF)Y[j]=A[i][j]-X[i];\n if(A[i][j]!=INF&&Y[j]!=INF)X[i]=A[i][j]-Y[j];\n }\n }\n }\n bool ok = true;\n rep(i,h)ok&=X[i]!=INF;\n rep(i,w)ok&=Y[i]!=INF;\n cout<<(ok?\"YES\":\"NO\")<<endl;\n }\n}", "accuracy": 1, "time_ms": 1610, "memory_kb": 3564, "score_of_the_acc": -1.1221, "final_rank": 20 }, { "submission_id": "aoj_1649_9344019", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for (ll i = 0; i < n; ++i)\n#define loop(i, a, b) for (ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\nconstexpr ll INF = 1001001001001001001LL;\nconstexpr int INF32 = 1001001001;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\n// Graph /////////////////////////////////////////////////////////\ntemplate <class T> struct Edge\n{\n int from;\n int to;\n T val;\n\n Edge() : from(-1), to(-1), val(1) {}\n Edge(const int &i) {\n from = -1, to = i;\n val = 1;\n }\n Edge(int from, int to) : from(from), to(to), val(1) {}\n Edge(int from, int to, T val) : from(from), to(to), val(val) {}\n bool operator==(const Edge &e) const {\n return from == e.from && to == e.to && val == e.val;\n }\n bool operator!=(const Edge &e) const {\n return from != e.from || to != e.to || val != e.val;\n }\n\n operator int() const { return to; }\n\n friend ostream &operator<<(ostream &os, const Edge &e) {\n os << e.from << \" -> \" << e.to << \" : \" << e.val;\n return os;\n }\n};\ntemplate <class T> using Graph = vector<vector<Edge<T>>>;\n//////////////////////////////////////////////////////////////////\n\n// Math //////////////////////////////////////////////////////////\nll pow(ll a, ll b) {\n ll res = 1;\n while (b > 0) {\n if (b & 1) res = res * a;\n a = a * a;\n b >>= 1;\n }\n return res;\n}\nll pow(ll a, ll b, ll mod) {\n if (b < 0) return pow(a, mod - 1 + b, mod);\n ll res = 1;\n while (b > 0) {\n if (b & 1) res = res * a % mod;\n a = a * a % mod;\n b >>= 1;\n }\n return res;\n}\n/**\n * @brief 法がmodのときのaの逆元を求める\n * @remark modが素数のときのみ有効\n * @remark\n * aとmodが互いに素でなければいけない(割り算の代わりに使う場合は、気にしなくても大丈夫)\n * @details a^(p-1) ≡ 1 mod p -[両辺にa^(-1)を掛ける]-> a^(p-2) ≡ a^(-1) mod p\n * @param a 逆元を求めたい値\n * @param mod 法\n * @return ll aの逆元\n */\nll inv(ll a, ll mod) {\n return pow(a, mod - 2, mod);\n}\n\nll nCrDP[67][67];\nll nCr(ll n, ll r) {\n if (nCrDP[n][r] != 0) return nCrDP[n][r];\n if (r == 0 || n == r) return 1;\n return nCrDP[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r);\n}\nll nHr(ll n, ll r) {\n return nCr(n + r - 1, r);\n}\n\nunordered_map<ll, vector<ll>> fact, invfact;\nll nCr(ll n, ll r, ll mod) {\n if (fact.count(mod) == 0 || fact[mod].size() <= max(n, r)) {\n const ll size = max(500000LL, max(n, r));\n fact[mod] = vector<ll>(size + 1, 0);\n invfact[mod] = vector<ll>(size + 1, 0);\n fact[mod][0] = 1;\n for (int i = 0; i < size; ++i)\n fact[mod][i + 1] = fact[mod][i] * (i + 1) % mod;\n invfact[mod][size] = inv(fact[mod][size], mod);\n for (int i = size - 1; i >= 0; --i)\n invfact[mod][i] = invfact[mod][i + 1] * (i + 1) % mod;\n }\n return fact[mod][n] * invfact[mod][r] % mod * invfact[mod][n - r] % mod;\n}\nll nHr(ll n, ll r, ll mod) {\n return nCr(n + r - 1, r, mod);\n}\n//////////////////////////////////////////////////////////////////\n\n// Linear Algebra ////////////////////////////////////////////////\nconst double Rad2Deg = 180.0 / M_PI;\nconst double Deg2Rad = M_PI / 180.0;\n//////////////////////////////////////////////////////////////////\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T> inline bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T> inline bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<vector<T>> &vv) {\n for (size_t i = 0; i < vv.size(); ++i) {\n os << vv[i];\n if (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\n\nstruct phash{\n inline size_t operator()(const pair<ll, ll> & p) const{\n const auto h1 = hash<ll>()(p.first);\n const auto h2 = hash<ll>()(p.second);\n return h1 + h2 * 12345;\n }\n};\n\n\n\nint solve() {\n ll w, h;\n cin >> w >> h;\n if (w == 0) return 1;\n ll n = w + h - 1;\n unordered_set<P, phash> cells;\n rep(i, n) {\n ll x, y, value;\n cin >> x >> y >> value;\n x--, y--;\n cells.insert({x, y});\n }\n\n vector<bool> knownW(w, false);\n vector<bool> knownH(h, false);\n knownW[0] = true;\n\n for (auto pos : cells) {\n if (pos.first == 0) { knownH[pos.second] = true; }\n }\n\n rep(i, w) {\n rep(x, w) {\n rep(y, h) {\n P pos = {x, y};\n if (cells.count(pos) && knownH[y]) {\n knownW[x] = true;\n break;\n }\n }\n if (knownW[x]) {\n rep(y, h) {\n P pos = {x, y};\n if (cells.count(pos)) { knownH[y] = true; }\n }\n }\n }\n\n /* rep(y, h) {\n rep(x, w) {\n P pos = {x, y};\n if (cells.count(pos) && knownW[x]) {\n knownH[y] = true;\n break;\n }\n }\n if (knownH[y]) {\n rep(x, w) {\n P pos = {x, y};\n if (cells.count(pos)) { knownW[x] = true; }\n }\n }\n } */\n }\n\n bool ok = true;\n rep(x, w) {\n if (!knownW[x]) {\n ok = false;\n break;\n }\n }\n rep(y, h) {\n if (!knownH[y]) {\n ok = false;\n break;\n }\n }\n\n cout << (ok ? \"YES\" : \"NO\") << endl;\n\n return 0;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (!solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 3380, "score_of_the_acc": -0.352, "final_rank": 16 }, { "submission_id": "aoj_1649_9327103", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool solve(){\n int H,W;\n cin>>H>>W;\n if(H==0&&W==0)return 0;\n int K=H+W;\n vector<vector<int>>MH(H),MW(W);\n vector<int>R(K-1,0),C(K-1,0),X(K-1,0);\n queue<int>q;\n for(int i=0;i<K-1;i++)\n {\n cin>>R[i]>>C[i]>>X[i];\n R[i]--,C[i]--;\n MH[R[i]].push_back(i);\n MW[C[i]].push_back(i);\n if(R[i]==0)q.push(i);\n }\n const int INF=1<<28;\n vector<int>ansH(H,INF),ansW(W,INF);\n ansH[0]=0;\n while(!q.empty())\n {\n int i=q.front();\n q.pop();\n int r=R[i],c=C[i];\n assert(ansH[r]!=INF||ansW[c]!=INF);\n if(ansH[r]!=INF)\n {\n if(ansW[c]!=INF&&ansW[c]!=X[i]-ansH[r])\n {\n cout<<\"NO\"<<\"\\n\";\n return 1;\n }\n ansW[c]=X[i]-ansH[r];\n }\n else\n {\n if(ansH[r]!=INF&&ansH[r]!=X[i]-ansW[c])\n {\n cout<<\"NO\"<<\"\\n\";\n return 1;\n }\n ansH[r]=X[i]-ansW[c];\n }\n for(int j:MH[r])if(j!=i)\n {\n if(ansH[R[j]]==INF||ansW[C[j]]==INF)q.push(j);\n }\n for(int j:MW[c])if(j!=i)\n {\n if(ansH[R[j]]==INF||ansW[C[j]]==INF)q.push(j);\n }\n }\n for(int i=0;i<K-1;i++)if(ansH[R[i]]+ansW[C[i]]!=X[i])\n {\n cout<<\"NO\"<<\"\\n\";\n return 1;\n }\n for(int i=0;i<H;i++)if(ansH[i]==INF)\n {\n cout<<\"NO\"<<\"\\n\";\n return 1;\n }\n for(int i=0;i<W;i++)if(ansW[i]==INF)\n {\n cout<<\"NO\"<<\"\\n\";\n return 1;\n }\n cout<<\"YES\"<<\"\\n\";\n return 1; \n}\n\n\nint main(){\n while(solve()){}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3288, "score_of_the_acc": -0.0264, "final_rank": 2 }, { "submission_id": "aoj_1649_9280044", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass UnionFind {\n private:\n vector<int> par; // 各元の親を表す配列\n vector<int> siz; // 素集合のサイズを表す\n public:\n // コンストラクタ\n UnionFind(int sz) : par(sz), siz(sz, 1) {\n for (int i = 0; i < sz; i++)\n par[i] = i; // 親は自分\n }\n int root(int x) { // 根の検索\n while (par[x] != x) {\n x = par[x] = par[par[x]]; // 経路圧縮 xの親の親をxの親とする\n }\n return x;\n }\n bool unite(int x, int y) { // 素集合の併合\n x = root(x), y = root(y);\n if (x == y)\n return false;\n if (siz[x] < siz[y]) { // マージテク\n swap(x, y);\n }\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n bool same(int x, int y) { // 連結判定\n return root(x) == root(y);\n }\n int size(int x) { // 素集合のサイズ\n return siz[root(x)];\n }\n};\n\nint w, h;\n\nint encode1(int a, int b) { return a * h + b; }\nint encode2(int a) { return h * w + a; }\nint encode3(int a) { return h * w + w + a; }\n\nvoid solve() {\n int x[210] = {}, y[210] = {}, n[210] = {};\n cin >> w >> h;\n if (w == 0 && h == 0) {\n exit(0);\n }\n UnionFind uf(200010);\n for (int i = 0; i < w + h - 1; i++) {\n cin >> x[i] >> y[i] >> n[i];\n swap(x[i], y[i]);\n x[i]--, y[i]--;\n uf.unite(encode3(x[i]), encode2(y[i]));\n }\n bool flag = true;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (!uf.same(encode2(j), encode3(i))) {\n flag = false;\n }\n }\n }\n if (flag) {\n cout << \"YES\" << endl;\n return;\n }\n cout << \"NO\" << endl;\n}\n\nint main() {\n while (1) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4672, "score_of_the_acc": -0.5187, "final_rank": 18 }, { "submission_id": "aoj_1649_9126468", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0; i< (n); i++)\n#define mkp(i,j) make_pair((i),(j))\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 998244353;\nconst int inf = (1<<30);\n\n\nint main(){\n\twhile(1){\n\t\tint w,h; cin>>w>>h;\n\t\tif(w == 0) break;\n\t\tvector<bitset<(size_t)(100)>> t(w); \n\t\tint k = w+h - 1;\n\t\trep(i,k){\n\t\t\tint x,y,n; cin>>x>>y>>n;\n\t\t\tx--;y--;\n\t\t\tt[x][y] = 1;\n\t\t}\n\t\trep(o,100){\n\t\tfor(int i = 0; i < w-1; i++){\n\t\tfor(int i2 = i+1; i< w; i++){\n\t\t\tbool c = 0;\n\t\t\tbitset<100> u;\n\t\t\tu = t[i]&t[i2];\n\t\t\trep(j,h) c |= u[j];\n\t\t\tif(c){\n\t\t\t\tt[i] |= t[i2];\n\t\t\t\tt[i2] |= t[i];\n\t\t\t}\n\t\t}}}\n\t\t\n\t\t/*for(int i = w-2; i >= 0; i--){\n\t\t\tbool c = 0;\n\t\t\tbitset<100> u;\n\t\t\tu = t[i]&t[i+1];\n\t\t\trep(j,h) c |= u[j];\n\t\t\tif(c){\n\t\t\t\tt[i] |= t[i+1];\n\t\t\t\tt[i+1] |= t[i];\n\t\t\t}\n\t\t}*/\n\t\tbool res = 1;\n\t\trep(i,w){\n\t\t\trep(j,h) res &= t[i][j];\n\t\t}\n\n\t\t/*rep(i,w){\n\t\t\tcout<<t[i]<<endl;;\n\t\t}*/\n\t\tcout<<(res ? \"YES\" : \"NO\")<<endl;\n\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3372, "score_of_the_acc": -0.0617, "final_rank": 4 }, { "submission_id": "aoj_1649_9084742", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst ll INF = 1e18;\n\nvoid solve() {\n\n while (1) {\n ll w,h;\n cin >> w >> h;\n if (w==0) break;\n\n vector<vector<ll>> table(h, vector<ll>(w,INF));\n vector<ll> hidari(h,INF);\n vector<ll> ue(w,INF);\n ue[0] = 0;\n\n for (ll i = 0; i < w+h-1; ++i) {\n ll x,y,n;\n cin >> y >> x >> n;\n --x,--y;\n table[x][y] = n;\n }\n\n while (true) {\n\n ll ct = 0;\n for (ll i = 0; i < h; ++i) {\n for (ll j = 0; j < w; ++j) {\n \n if (table[i][j] == INF && ue[j]!= INF && hidari[i] != INF) table[i][j] = hidari[i]+ue[j],ct++;\n if (table[i][j]!= INF && ue[j] == INF && hidari[i] !=INF) ue[j] = table[i][j] - hidari[i],ct++;\n if (table[i][j]!= INF && ue[j] != INF && hidari[i] ==INF) hidari[i] = table[i][j] - ue[j],ct++;\n }\n }\n\n if (ct==0) break;\n }\n\n bool ans = true;\n for (ll i = 0; i < h; ++i) {\n for (ll j = 0; j < w; ++j) {\n if (table[i][j] == INF) ans = false;\n }\n }\n\n\n for (auto m : hidari) if (m==INF) ans = false;\n for (auto m : ue) if (m==INF) ans = false;\n\n if (ans) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n\n }\n}\n\nint main() {\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.0763, "final_rank": 5 }, { "submission_id": "aoj_1649_8006873", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n\nint main() {\n while (true) {\n int w, h;\n cin >> w >> h;\n if (w + h == 0) break;\n constexpr int INF = 1e9;\n vector Grid(h, vector<int>(w, INF));\n rep(_, w + h - 1) {\n int x, y, n;\n cin >> x >> y >> n;\n --x, --y;\n Grid[y][x] = n;\n }\n vector<int> row(h, INF), col(w, INF);\n col[0] = 0;\n while (true) {\n bool updated = false;\n rep(i, h) rep(j, w) {\n if (row[i] != INF && Grid[i][j] != INF && col[j] == INF) {\n col[j] = Grid[i][j] - row[i];\n updated = true;\n }\n if (row[i] == INF && Grid[i][j] != INF && col[j] != INF) {\n row[i] = Grid[i][j] - col[j];\n updated = true;\n }\n }\n if (!updated) break;\n }\n bool ok = true;\n rep(i, h) ok &= (row[i] != INF);\n rep(i, w) ok &= (col[i] != INF);\n cout << (ok ? \"YES\" : \"NO\") << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": -0.0555, "final_rank": 3 }, { "submission_id": "aoj_1649_7903883", "code_snippet": "#include <bits/stdc++.h>\n\n// #include <atcoder/all>\ntypedef long long int ll;\n#define FOR(i, a, b) for (ll i = (a); i < (b); i++)\n#define REP(i, n) for (ll i = 0; i < signed(n); i++)\n#define EREP(i, n) for (ll i = 1; i <= signed(n); i++)\n#define ALL(a) (a).begin(), (a).end()\nusing namespace std;\n// using namespace atcoder;\n// #define EVEL 1\n#ifdef EVEL\n#define DEB(X) cout << #X << \":\" << X << \" \";\n#define TF(f) f ? cout << \"true \" : cout << \"false \";\n#define END cout << \"\\n\";\n#else\n#define DEB(X) ;\n#define TF(f) ;\n#define END ;\n#endif\nconst ll INF = 9e18;\ntypedef std::pair<ll, ll> P;\nstruct edge {\n ll to, cost;\n};\n#define VMAX 100000\ntemplate <class T>\ninline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\nint main(void) {\n ll N, H, W;\n // vector<ll> A(4);\n while (true) {\n N = 0;\n cin >> W >> H;\n if (W == 0 && H == 0) break;\n set<int> h, w;\n vector<P> A(W + H - 1);\n w.insert(1);\n REP(i, W + H - 1) {\n int x, y, n;\n cin >> x >> y >> n;\n A[i] = {x, y};\n if (x == 1) {\n h.insert(y);\n }\n }\n REP(loop, W + H - 1){\n REP(i, W + H - 1) {\n auto [x, y] = A[i];\n if (w.count(x)) {\n h.insert(y);\n }\n if (h.count(y)) {\n w.insert(x);\n }\n }\n }\n \n cout<<(w.size()==W&&h.size()==H?\"YES\":\"NO\")<<endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3368, "score_of_the_acc": -0.0791, "final_rank": 7 } ]
aoj_1647_cpp
Idealistic Canister The Ideally Compact Packaging Canisters (ICPC) corporation is a package manufacturer specialized in canisters (cylindrical containers), designing and manufacturing canisters satisfying clients' demands. The corporation has taken an order of a canister to pack two different products together. Both products have shapes of convex polygonal prisms with the same height. The inside of the canister should be precisely a cylinder whose height is equal to the height of the products. Products must be placed vertically in the canister, that is, the bottoms of the products must meet the inner bottom of the canister. Products can be placed at an arbitrary position and with an arbitrary direction in the canister, but they should not be laid upside-down nor stacked one on the other. Products and the inner surface of the canister may touch one another. The customer requests to make the canister as small as possible. Your task is to find the smallest possible diameter of the canister bottom that can contain the two products. Figure H-1 Example of two products packed in the minimum-sized canister. Input The input consists of at most 50 datasets, each in the following format. n x a ,1 y a ,1 ... x a , n y a , n m x b ,1 y b ,1 ... x b , m y b , m n (3 ≤ n ≤ 40) is the number of vertices of the bottom polygon of one of the products. The following n lines have two integers each, which are the x- and y- coordinates of the vertices of the bottom polygon. They are between −1000 and 1000, inclusive. The vertices are listed in a counter-clockwise order. The bottom polygon is guaranteed to be convex. Then comes the description of the bottom polygon of the other product in exactly the same manner. The end of the input is indicated by a line containing a zero. Output For each dataset, output the smallest possible diameter of the bottom circle of the canister that accommodates the two products together. The output must not contain an error greater than 10 −6 . Sample Input 3 8 0 7 7 0 6 4 0 0 5 0 5 5 0 5 3 0 0 2 2 0 2 3 3 1 5 1 5 3 5 0 0 3 0 3 1 1 3 0 3 5 0 0 3 0 3 1 1 3 0 3 9 0 0 9 4 13 13 9 22 6 24 -6 24 -9 22 -13 13 -9 4 4 1 0 0 5 -1 0 0 -5 0 Output for the Sample Input 10.625000000000 2.828427124746 5.656854249492 26.000000000000
[ { "submission_id": "aoj_1647_9994054", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass point2d {\npublic:\n double x, y;\n point2d() : x(0.0), y(0.0) {};\n point2d(double x_, double y_) : x(x_), y(y_) {};\n point2d& operator+=(const point2d& p) { x += p.x; y += p.y; return (*this); }\n point2d& operator-=(const point2d& p) { x -= p.x; y -= p.y; return (*this); }\n point2d& operator*=(double v) { x *= v; y *= v; return (*this); }\n point2d operator+(const point2d& p) const { return point2d(*this) += p; }\n point2d operator-(const point2d& p) const { return point2d(*this) -= p; }\n point2d operator*(double v) const { return point2d(*this) *= v; }\n double abs() const {\n return sqrt(x * x + y * y);\n }\n double dot(const point2d& p) const {\n return x * p.x + y * p.y;\n }\n};\n\nconst double pi = acos(-1.0);\n\n// Q. Can the polygon be placed in range x^2 + y^2 <= R, y >= YL ???\nbool check(int N, const vector<point2d>& P, double R, double YL, const vector<vector<vector<point2d> > >& anglist) {\n // Case #1. Two vertices in line y = YL\n for (int i = 0; i < N; i++) {\n double l = -1.0e+9, r = +1.0e+9;\n for (int j = 0; j < N; j++) {\n point2d subp = point2d(0, YL) + anglist[i][(i + 1) % N][j];\n if (subp.y >= R) {\n l = +1.0e+9;\n r = -1.0e+9;\n }\n else {\n double w = sqrt(R * R - subp.y * subp.y);\n l = max(l, -w - subp.x);\n r = min(r, +w - subp.x);\n }\n }\n if (l < r) {\n return true;\n }\n }\n // Case #2. Two vertices in circle's circumference\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (i == j || (P[j] - P[i]).abs() >= 2 * R) {\n continue;\n }\n double elen = (P[j] - P[i]).abs();\n double subh = sqrt(R * R - elen * elen / 4);\n double l1 = 0.0, r1 = 2 * pi;\n double l2 = 0.0, r2 = 2 * pi;\n bool valid = true;\n for (int k = 0; k < N; k++) {\n point2d subp = point2d(-elen / 2, -subh) + anglist[i][j][k];\n double d = subp.abs();\n if (d >= R + 1.0e-7 || (YL >= 0 && d <= YL)) {\n valid = false;\n break;\n }\n double phi = atan2(subp.y, subp.x);\n if (d > -YL) {\n double g = asin(YL / d);\n double cl = g - phi;\n double cr = (pi - g) - phi;\n while (cl < 0.0) cl += 2 * pi;\n while (cl >= 2 * pi) cl -= 2 * pi;\n while (cr < 0.0) cr += 2 * pi;\n while (cr >= 2 * pi) cr -= 2 * pi;\n if (cl <= cr) {\n l1 = max(l1, cl);\n r1 = min(r1, cr);\n }\n else {\n l2 = max(l2, cl);\n r2 = min(r2, cr);\n }\n if (!((l2 < r2 && l1 <= r1) || (l1 <= r2 && r2 <= r1) || (l1 <= l2 && l2 <= r1))) {\n valid = false;\n break;\n }\n }\n }\n if (valid) {\n return true;\n }\n }\n }\n return false;\n}\n\ndouble min_height(int N, const vector<point2d>& P, double R) {\n vector<vector<vector<point2d> > > anglist(N, vector<vector<point2d> >(N, vector<point2d>(N)));\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (i == j) {\n continue;\n }\n point2d evec = P[j] - P[i];\n double elen = evec.abs();\n for (int k = 0; k < N; k++) {\n double slen = (P[k] - P[i]).abs();\n double theta = (i != k ? acos(max(min(evec.dot(P[k] - P[i]) / (elen * slen), +1.0), -1.0)) : 0.0);\n if ((k - i + N) % N < (j - i + N) % N) {\n theta *= -1.0;\n }\n anglist[i][j][k] = point2d(cos(theta), sin(theta)) * slen;\n }\n }\n }\n double hl = 0.0, hr = 2.0 * R;\n while (hr - hl > 1.0e-7) {\n double h = (hl + hr) * 0.5;\n if (check(N, P, R, R - h, anglist) == true) {\n hr = h;\n }\n else {\n hl = h;\n }\n }\n return hr;\n}\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n if (N == 0) {\n break;\n }\n vector<point2d> P1(N);\n for (int i = 0; i < N; i++) {\n cin >> P1[i].x >> P1[i].y;\n }\n int M;\n cin >> M;\n vector<point2d> P2(M);\n for (int i = 0; i < M; i++) {\n cin >> P2[i].x >> P2[i].y;\n }\n double l = 0.0, r = 1.0e+4;\n while (r - l > 1.0e-7) {\n double m = (l + r) * 0.5;\n double res1 = min_height(N, P1, m);\n double res2 = min_height(M, P2, m);\n if (res1 + res2 <= 2 * m) {\n r = m;\n }\n else {\n l = m;\n }\n }\n cout.precision(15);\n cout << r * 2.0 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7370, "memory_kb": 5264, "score_of_the_acc": -1.2928, "final_rank": 18 }, { "submission_id": "aoj_1647_9994046", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nclass point2d {\npublic:\n\tdouble x, y;\n\tpoint2d() : x(0.0), y(0.0) {};\n\tpoint2d(double x_, double y_) : x(x_), y(y_) {};\n\tpoint2d& operator+=(const point2d& p) { x += p.x; y += p.y; return (*this); }\n\tpoint2d& operator-=(const point2d& p) { x -= p.x; y -= p.y; return (*this); }\n\tpoint2d& operator*=(double v) { x *= v; y *= v; return (*this); }\n\tpoint2d operator+(const point2d& p) const { return point2d(*this) += p; }\n\tpoint2d operator-(const point2d& p) const { return point2d(*this) -= p; }\n\tpoint2d operator*(double v) const { return point2d(*this) *= v; }\n\tdouble abs() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\tdouble dot(const point2d& p) const {\n\t\treturn x * p.x + y * p.y;\n\t}\n};\n\nconst double pi = acos(-1.0);\n\n// Q. Can the polygon be placed in range x^2 + y^2 <= R, y >= YL ???\nbool check(int N, const vector<point2d>& P, double R, double YL, const vector<vector<vector<point2d> > >& anglist) {\n\t// Case #1. Two vertices in line y = YL\n\tfor (int i = 0; i < N; i++) {\n\t\tdouble l = -1.0e+9, r = +1.0e+9;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tpoint2d subp = point2d(0, YL) + anglist[i][(i + 1) % N][j];\n\t\t\tif (subp.y >= R) {\n\t\t\t\tl = +1.0e+9;\n\t\t\t\tr = -1.0e+9;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdouble w = sqrt(R * R - subp.y * subp.y);\n\t\t\t\tl = max(l, -w - subp.x);\n\t\t\t\tr = min(r, +w - subp.x);\n\t\t\t}\n\t\t}\n\t\tif (l < r) {\n\t\t\treturn true;\n\t\t}\n\t}\n\t// Case #2. Two vertices in circle's circumference\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (i == j || (P[j] - P[i]).abs() >= 2 * R) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdouble elen = (P[j] - P[i]).abs();\n\t\t\tdouble subh = sqrt(R * R - elen * elen / 4);\n\t\t\tdouble l1 = 0.0, r1 = 2 * pi;\n\t\t\tdouble l2 = 0.0, r2 = 2 * pi;\n\t\t\tbool valid = true;\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tpoint2d subp = point2d(-elen / 2, -subh) + anglist[i][j][k];\n\t\t\t\tdouble d = subp.abs();\n\t\t\t\tif (d >= R + 1.0e-7 || (YL >= 0 && d <= YL)) {\n\t\t\t\t\tvalid = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tdouble phi = atan2(subp.y, subp.x);\n\t\t\t\tif (d > -YL) {\n\t\t\t\t\tdouble g = asin(YL / d);\n\t\t\t\t\tdouble cl = g - phi;\n\t\t\t\t\tdouble cr = (pi - g) - phi;\n\t\t\t\t\twhile (cl < 0.0) cl += 2 * pi;\n\t\t\t\t\twhile (cl >= 2 * pi) cl -= 2 * pi;\n\t\t\t\t\twhile (cr < 0.0) cr += 2 * pi;\n\t\t\t\t\twhile (cr >= 2 * pi) cr -= 2 * pi;\n\t\t\t\t\tif (cl <= cr) {\n\t\t\t\t\t\tl1 = max(l1, cl);\n\t\t\t\t\t\tr1 = min(r1, cr);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tl2 = max(l2, cl);\n\t\t\t\t\t\tr2 = min(r2, cr);\n\t\t\t\t\t}\n\t\t\t\t\tif (!((l2 < r2 && l1 <= r1) || (l1 <= r2 && r2 <= r1) || (l1 <= l2 && l2 <= r1))) {\n\t\t\t\t\t\tvalid = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (valid) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\ndouble min_height(int N, const vector<point2d>& P, double R) {\n\tvector<vector<vector<point2d> > > anglist(N, vector<vector<point2d> >(N, vector<point2d>(N)));\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (i == j) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tpoint2d evec = P[j] - P[i];\n\t\t\tdouble elen = evec.abs();\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tdouble slen = (P[k] - P[i]).abs();\n\t\t\t\tdouble theta = (i != k ? acos(max(min(evec.dot(P[k] - P[i]) / (elen * slen), +1.0), -1.0)) : 0.0);\n\t\t\t\tif ((k - i + N) % N < (j - i + N) % N) {\n\t\t\t\t\ttheta *= -1.0;\n\t\t\t\t}\n\t\t\t\tanglist[i][j][k] = point2d(cos(theta), sin(theta)) * slen;\n\t\t\t}\n\t\t}\n\t}\n\tdouble hl = 0.0, hr = 2.0 * R;\n\twhile (hr - hl > 1.0e-7) {\n\t\tdouble h = (hl + hr) * 0.5;\n\t\tif (check(N, P, R, R - h, anglist) == true) {\n\t\t\thr = h;\n\t\t}\n\t\telse {\n\t\t\thl = h;\n\t\t}\n\t}\n\treturn hr;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<point2d> P1(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> P1[i].x >> P1[i].y;\n\t\t}\n\t\tint M;\n\t\tcin >> M;\n\t\tvector<point2d> P2(M);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> P2[i].x >> P2[i].y;\n\t\t}\n\t\tdouble l = 0.0, r = 1.0e+4;\n\t\twhile (r - l > 1.0e-7) {\n\t\t\tdouble m = (l + r) * 0.5;\n\t\t\tdouble res1 = min_height(N, P1, m);\n\t\t\tdouble res2 = min_height(M, P2, m);\n\t\t\tif (res1 + res2 <= 2 * m) {\n\t\t\t\tr = m;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tl = m;\n\t\t\t}\n\t\t}\n\t\tcout.precision(15);\n\t\tcout << r * 2.0 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7390, "memory_kb": 5236, "score_of_the_acc": -1.2907, "final_rank": 17 }, { "submission_id": "aoj_1647_9853299", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 40;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& n) const { return { x * n, y * n }; }\n\tPos operator / (const ld& n) const { return { x / n, y / n }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, const int& i, Polygon B, const int& j, const ld& d) {\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) A.push_back(p + v);\n\treturn minimum_enclose_circle(A);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF, r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 3680, "memory_kb": 3880, "score_of_the_acc": -0.0805, "final_rank": 2 }, { "submission_id": "aoj_1647_9853295", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 40;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\t//bool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\t//bool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\t//bool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& n) const { return { x * n, y * n }; }\n\tPos operator / (const ld& n) const { return { x / n, y / n }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\t//Pos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\t//Pos& operator *= (const ld& n) { x *= scale; y *= n; return *this; }\n\t//Pos& operator /= (const ld& n) { x /= scale; y /= n; return *this; }\n\t//Pos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\t//bool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\t//bool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, const int& i, const Polygon& B, const int& j, const ld& d) {\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (const Pos& p : B) A.push_back(p + v);\n\treturn minimum_enclose_circle(A);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF, r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 3800, "memory_kb": 3856, "score_of_the_acc": -0.1031, "final_rank": 3 }, { "submission_id": "aoj_1647_9853293", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 40;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\t//bool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\t//bool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\t//bool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& n) const { return { x * n, y * n }; }\n\tPos operator / (const ld& n) const { return { x / n, y / n }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\t//Pos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\t//Pos& operator *= (const ld& n) { x *= scale; y *= n; return *this; }\n\t//Pos& operator /= (const ld& n) { x /= scale; y /= n; return *this; }\n\t//Pos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\t//bool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\t//bool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, const int& i, Polygon B, const int& j, const ld& d) {\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) A.push_back(p + v);\n\treturn minimum_enclose_circle(A);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF, r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 3670, "memory_kb": 3860, "score_of_the_acc": -0.0732, "final_rank": 1 }, { "submission_id": "aoj_1647_9853289", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 40;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\t//bool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\t//bool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\t//bool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& n) const { return { x * n, y * n }; }\n\tPos operator / (const ld& n) const { return { x / n, y / n }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\t//Pos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\t//Pos& operator *= (const ld& n) { x *= scale; y *= n; return *this; }\n\t//Pos& operator /= (const ld& n) { x /= scale; y /= n; return *this; }\n\t//Pos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\t//bool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\t//bool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF, r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 3760, "memory_kb": 3912, "score_of_the_acc": -0.1073, "final_rank": 4 }, { "submission_id": "aoj_1647_9853288", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 40;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\t//bool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\t//bool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\t//bool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& n) const { return { x * n, y * n }; }\n\tPos operator / (const ld& n) const { return { x / n, y / n }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\t//Pos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\t//Pos& operator *= (const ld& n) { x *= scale; y *= n; return *this; }\n\t//Pos& operator /= (const ld& n) { x /= scale; y /= n; return *this; }\n\t//Pos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\t//bool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\t//bool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF, r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\t//ans.push_back(ret);\n\tstd::cout << ret << \"\\n\";\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\t//for (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 3950, "memory_kb": 3852, "score_of_the_acc": -0.1376, "final_rank": 6 }, { "submission_id": "aoj_1647_9853277", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 3750, "memory_kb": 3928, "score_of_the_acc": -0.1088, "final_rank": 5 }, { "submission_id": "aoj_1647_9853179", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 30; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 4470, "memory_kb": 3860, "score_of_the_acc": -0.2628, "final_rank": 7 }, { "submission_id": "aoj_1647_9852216", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n};\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 50; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 7360, "memory_kb": 3960, "score_of_the_acc": -0.9721, "final_rank": 11 }, { "submission_id": "aoj_1647_9851987", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\n#include <array>\n#include <tuple>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator < (const Pos& r) const { return x == r.x ? y == r.y ? d < r.d : y < r.y : x < r.x; }\n\t//bool operator < (const Pos& r) const { return zero(x - r.x) ? zero(y - r.y) ? d < r.d : y < r.y : x < r.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tld xy() const { return x * y; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tbool close(const Pos& p) const { return zero((*this - p).Euc()); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n\tVec& operator *= (const ld& scalar) { vy *= scalar; vx *= scalar; return *this; }\n\tVec& operator /= (const ld& scalar) { vy /= scalar; vx /= scalar; return *this; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tbool operator < (const Line& l) const {\n\t\tbool f1 = Zero < s;\n\t\tbool f2 = Zero < l.s;\n\t\tif (f1 != f2) return f1;\n\t\tld CCW = s / l.s;\n\t\treturn zero(CCW) ? c * hypot(l.s.vy, l.s.vx) < l.c * hypot(s.vy, s.vx) : CCW > 0;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n\tLine operator + (const ld& scalar) const { return Line(s, c + hypot(s.vy, s.vx) * scalar); }\n\tLine operator - (const ld& scalar) const { return Line(s, c - hypot(s.vy, s.vx) * scalar); }\n\tLine operator * (const ld& scalar) const { return Line({ s.vy * scalar, s.vx * scalar }, c * scalar); }\n\tLine& operator += (const ld& scalar) { c += hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator -= (const ld& scalar) { c -= hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator *= (const ld& scalar) { s *= scalar, c *= scalar; return *this; }\n\tld dist(const Pos& p) const { return s.vy * p.x + s.vx * p.y; }\n\tld above(const Pos& p) const { return s.vy * p.x + s.vx * p.y - c; }\n\tfriend std::ostream& operator << (std::ostream& os, const Line& l) { os << l.s.vy << \" \" << l.s.vx << \" \" << l.c; return os; }\n};\nconst Line Xaxis = { { 0, -1 }, 0 };\nconst Line Yaxis = { { 1, 0 }, 0 };\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nld cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n\tCircle operator + (const Circle& C) const { return { {c.x + C.c.x, c.y + C.c.y}, r + C.r }; }\n\tCircle operator - (const Circle& C) const { return { {c.x - C.c.x, c.y - C.c.y}, r - C.r }; }\n\tld H(const ld& th) const { return sin(th) * c.x + cos(th) * c.y + r; }// coord trans | check right\n\tld A() const { return r * r * PI; }\n\tfriend std::istream& operator >> (std::istream& is, Circle& c) { is >> c.c.x >> c.c.y >> c.r; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Circle& c) { os << c.c.x << \" \" << c.c.y << \" \" << c.r; return os; }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\t//Pos v1 = v - u, v2 = w - v;\n\t//if (zero(v1 / v2)) return INVAL;\n\t//Pos m1 = (u + v) * .5, m2 = (v + w) * .5;\n\t//Pos p1 = m1 + ~v1, p2 = m2 + ~v2;\n\t//Pos c = intersection(m1, p1, m2, p2);\n\t//return Circle(c, (c - u).mag());\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 50; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 7350, "memory_kb": 3968, "score_of_the_acc": -0.9716, "final_rank": 10 }, { "submission_id": "aoj_1647_9851957", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\n#include <array>\n#include <tuple>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator < (const Pos& r) const { return x == r.x ? y == r.y ? d < r.d : y < r.y : x < r.x; }\n\t//bool operator < (const Pos& r) const { return zero(x - r.x) ? zero(y - r.y) ? d < r.d : y < r.y : x < r.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tld xy() const { return x * y; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tbool close(const Pos& p) const { return zero((*this - p).Euc()); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n\tVec& operator *= (const ld& scalar) { vy *= scalar; vx *= scalar; return *this; }\n\tVec& operator /= (const ld& scalar) { vy /= scalar; vx /= scalar; return *this; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tbool operator < (const Line& l) const {\n\t\tbool f1 = Zero < s;\n\t\tbool f2 = Zero < l.s;\n\t\tif (f1 != f2) return f1;\n\t\tld CCW = s / l.s;\n\t\treturn zero(CCW) ? c * hypot(l.s.vy, l.s.vx) < l.c * hypot(s.vy, s.vx) : CCW > 0;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n\tLine operator + (const ld& scalar) const { return Line(s, c + hypot(s.vy, s.vx) * scalar); }\n\tLine operator - (const ld& scalar) const { return Line(s, c - hypot(s.vy, s.vx) * scalar); }\n\tLine operator * (const ld& scalar) const { return Line({ s.vy * scalar, s.vx * scalar }, c * scalar); }\n\tLine& operator += (const ld& scalar) { c += hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator -= (const ld& scalar) { c -= hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator *= (const ld& scalar) { s *= scalar, c *= scalar; return *this; }\n\tld dist(const Pos& p) const { return s.vy * p.x + s.vx * p.y; }\n\tld above(const Pos& p) const { return s.vy * p.x + s.vx * p.y - c; }\n\tfriend std::ostream& operator << (std::ostream& os, const Line& l) { os << l.s.vy << \" \" << l.s.vx << \" \" << l.c; return os; }\n};\nconst Line Xaxis = { { 0, -1 }, 0 };\nconst Line Yaxis = { { 1, 0 }, 0 };\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nld cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tPos v = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n\tCircle operator + (const Circle& C) const { return { {c.x + C.c.x, c.y + C.c.y}, r + C.r }; }\n\tCircle operator - (const Circle& C) const { return { {c.x - C.c.x, c.y - C.c.y}, r - C.r }; }\n\tld H(const ld& th) const { return sin(th) * c.x + cos(th) * c.y + r; }// coord trans | check right\n\tld A() const { return r * r * PI; }\n\tfriend std::istream& operator >> (std::istream& is, Circle& c) { is >> c.c.x >> c.c.y >> c.r; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Circle& c) { os << c.c.x << \" \" << c.c.y << \" \" << c.r; return os; }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nCircle minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V);\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\tint cnt = 50; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1).r;\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2).r;\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld ternary_search(const Pos& p0, const Pos& p1, const Pos& p2, const Pos& c, const ld& r) {\n\tPos vec, v1, v2;\n\tauto dist = [&](const ld& t) -> ld {\n\t\tPos s2 = p1 + vec.rot(t);\n\t\tld d = cross(p1, s2, c) / (p1 - s2).mag();\n\t\treturn r - d;\n\t\t};\n\tvec = (p0 - p1);\n\tv1 = ~(p0 - p1), v2 = ~(p2 - p1);\n\tld s = 0, e = rad(v1, v2), m1, m2, d1, d2;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\td1 = dist(m1);\n\t\td2 = dist(m2);\n\t\tif (sign(d1 < d2)) s = m1;\n\t\telse e = m2;\n\t}\n\treturn (s + e) * .5;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) continue;\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\ttmp = std::min(tmp, r - (c - p1).mag());\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r), db = fit(B, r);\n\treturn sign(r + r - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\tret = std::min({ ret, r1, r2 }) * 2;\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231 Idealistic Canister", "accuracy": 1, "time_ms": 7310, "memory_kb": 3952, "score_of_the_acc": -0.9583, "final_rank": 9 }, { "submission_id": "aoj_1647_9851679", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\n#include <array>\n#include <tuple>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator < (const Pos& r) const { return x == r.x ? y == r.y ? d < r.d : y < r.y : x < r.x; }\n\t//bool operator < (const Pos& r) const { return zero(x - r.x) ? zero(y - r.y) ? d < r.d : y < r.y : x < r.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tld xy() const { return x * y; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tbool close(const Pos& p) const { return zero((*this - p).Euc()); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n\tVec& operator *= (const ld& scalar) { vy *= scalar; vx *= scalar; return *this; }\n\tVec& operator /= (const ld& scalar) { vy /= scalar; vx /= scalar; return *this; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tbool operator < (const Line& l) const {\n\t\tbool f1 = Zero < s;\n\t\tbool f2 = Zero < l.s;\n\t\tif (f1 != f2) return f1;\n\t\tld CCW = s / l.s;\n\t\treturn zero(CCW) ? c * hypot(l.s.vy, l.s.vx) < l.c * hypot(s.vy, s.vx) : CCW > 0;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n\tLine operator + (const ld& scalar) const { return Line(s, c + hypot(s.vy, s.vx) * scalar); }\n\tLine operator - (const ld& scalar) const { return Line(s, c - hypot(s.vy, s.vx) * scalar); }\n\tLine operator * (const ld& scalar) const { return Line({ s.vy * scalar, s.vx * scalar }, c * scalar); }\n\tLine& operator += (const ld& scalar) { c += hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator -= (const ld& scalar) { c -= hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator *= (const ld& scalar) { s *= scalar, c *= scalar; return *this; }\n\tld dist(const Pos& p) const { return s.vy * p.x + s.vx * p.y; }\n\tld above(const Pos& p) const { return s.vy * p.x + s.vx * p.y - c; }\n\tfriend std::ostream& operator << (std::ostream& os, const Line& l) { os << l.s.vy << \" \" << l.s.vx << \" \" << l.c; return os; }\n};\nconst Line Xaxis = { { 0, -1 }, 0 };\nconst Line Yaxis = { { 1, 0 }, 0 };\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nld cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t, Pos& v) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tv = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n\tCircle operator + (const Circle& C) const { return { {c.x + C.c.x, c.y + C.c.y}, r + C.r }; }\n\tCircle operator - (const Circle& C) const { return { {c.x - C.c.x, c.y - C.c.y}, r - C.r }; }\n\tld H(const ld& th) const { return sin(th) * c.x + cos(th) * c.y + r; }// coord trans | check right\n\tld A() const { return r * r * PI; }\n\tfriend std::istream& operator >> (std::istream& is, Circle& c) { is >> c.c.x >> c.c.y >> c.r; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Circle& c) { os << c.c.x << \" \" << c.c.y << \" \" << c.r; return os; }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nld minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\t//std::cout << v.mag() << \"\\n\";\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V).r;\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\t//std::cout << e << \"\\n\";\n\tint cnt = 50; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\t//std::cout << r1 << \" \" << r2 << \"\\n\";\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1);\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2);\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld ternary_search(const Pos& p0, const Pos& p1, const Pos& p2, const Pos& c, const ld& r) {\n\tPos vec, v1, v2;\n\tauto dist = [&](const ld& t) -> ld {\n\t\tPos s2 = p1 + vec.rot(t);\n\t\tld d = cross(p1, s2, c) / (p1 - s2).mag();\n\t\treturn r - d;\n\t\t};\n\tvec = (p0 - p1);\n\tv1 = ~(p0 - p1), v2 = ~(p2 - p1);\n\tld s = 0, e = rad(v1, v2), m1, m2, d1, d2;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\td1 = dist(m1);\n\t\td2 = dist(m2);\n\t\tif (sign(d1 < d2)) s = m1;\n\t\telse e = m2;\n\t}\n\treturn (s + e) * .5;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\t//bool f = 1;\n\t\t\t\t//for (int k = 0; k < sz; k++) {\n\t\t\t\t//\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t//\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t//\tif (sign((P[k] - c).mag() - r) > 0) { f = 0; break; }\n\t\t\t\t//}\n\t\t\t\t//if (!f) continue;\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) {\n\t\t\t\t\t\tassert(eq(r, (P[k] - c).mag()));\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\t//if (eq(r, (p1 - c).mag())) tmp = std::max({ tmp, dist(p0, p1, c1), dist(p1, p2, c) });\n\t\t\t\t\t//else if (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0)\n\t\t\t\t\t//\ttmp = std::max(tmp, ternary_search(p0, p1, p2, c, r));\n\t\t\t\t\t//else tmp = std::max({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\tld d = r - (c - p1).mag();\n\t\t\t\t\t\t//if (d < 10) {\n\t\t\t\t\t\t//\tstd::cout << \"r:: \" << r << \"\\n\";\n\t\t\t\t\t\t//\tstd::cout << \"p0 = \" << p0 << \" \";\n\t\t\t\t\t\t//\tstd::cout << \"p1 = \" << p1 << \" \";\n\t\t\t\t\t\t//\tstd::cout << \"p2 = \" << p2 << \" \";\n\t\t\t\t\t\t//\tstd::cout << \"c = \" << c << \"\\n\";\n\t\t\t\t\t\t//\tstd::cout << \"dist = \" << (c - p1).mag() << \"\\n\";\n\t\t\t\t\t\t//\tstd::cout << \"d:: \" << d << \"\\n\";\n\t\t\t\t\t\t//}\n\t\t\t\t\t\ttmp = std::min(tmp, d);\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\t//std::cout << \"ret:: \" << ret << \"\\n\";\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r);\n\tld db = fit(B, r);\n\tld D = r * 2;\n\t//std::cout << \"r:: \" << r << \" da:: \" << da << \" db:: \" << db << \"\\n\";\n\t//std::cout << \"r*2:: \" << r * 2 << \"da+db:: \" << (da + db) << \"\\n\";\n\treturn sign(D - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\t//std::cout << \"m:: \" << m << \"\\n\";\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPos v;\n\t\t\t//std::cout << \"FUCK::\\n\";\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t, v);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\t//std::cout << \"ra :: \" << ra << \"\\n\";\n\t//std::cout << \"rb :: \" << rb << \"\\n\";\n\t//std::cout << \"r1 :: \" << r1 << \"\\n\";\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\t//std::cout << \"r2 :: \" << r2 << \"\\n\";\n\tret = std::min({ ret, r1, r2 }) * 2;\n\t//std::cout << \"ret:: \" << ret << \"\\n\";\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231\n\n/*\n\n40\n904 -405\n906 -400\n395 -616\n-16 -989\n-10 -992\n0 -992\n27 -991\n50 -990\n103 -987\n122 -985\n130 -984\n151 -981\n163 -979\n228 -966\n253 -960\n260 -958\n312 -942\n324 -938\n369 -921\n392 -912\n408 -905\n442 -888\n467 -875\n500 -857\n524 -843\n527 -841\n570 -812\n604 -786\n618 -775\n658 -742\n687 -716\n715 -688\n731 -671\n772 -623\n797 -591\n812 -570\n843 -523\n876 -463\n878 -459\n899 -416\n40\n-923 -266\n-817 -489\n-724 -604\n-635 -711\n-626 -718\n-536 -780\n-352 -889\n-334 -897\n-205 -934\n102 -954\n270 -920\n357 -891\n399 -874\n494 -820\n513 -809\n629 -718\n672 -680\n756 -591\n934 -213\n944 -122\n954 -4\n955 30\n939 178\n918 261\n894 345\n865 405\n776 565\n709 644\n478 832\n382 877\n305 890\n202 900\n-729 407\n-755 393\n-841 335\n-932 234\n-954 50\n-956 18\n-956 16\n-946 -113\n0\n\n963.3270026199625\n\n36\n510 -524\n616 -398\n631 -375\n675 -289\n694 -250\n726 -115\n728 -73\n732 15\n732 95\n704 213\n690 261\n653 334\n592 434\n497 533\n452 547\n289 518\n66 478\n-394 388\n-638 339\n-677 250\n-713 163\n-724 130\n-735 48\n-735 -41\n-715 -164\n-678 -266\n-630 -386\n-511 -533\n-427 -599\n-400 -619\n-299 -672\n-131 -723\n-27 -738\n110 -722\n287 -678\n426 -598\n21\n722 157\n721 162\n693 258\n687 270\n665 260\n443 -142\n381 -622\n410 -618\n442 -595\n478 -567\n485 -561\n533 -513\n599 -429\n627 -392\n663 -326\n689 -271\n699 -246\n710 -214\n722 -162\n736 74\n734 90\n0\n\n5\n0 0\n3 0\n3 1\n1 3\n0 3\n5\n0 0\n3 0\n3 1\n1 3\n0 3\n0\n\n*/", "accuracy": 1, "time_ms": 7740, "memory_kb": 3968, "score_of_the_acc": -1.0641, "final_rank": 15 }, { "submission_id": "aoj_1647_9851675", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\n#include <array>\n#include <tuple>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tint i, d;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) { i = 0, d = 0; }\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\t//bool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator < (const Pos& r) const { return x == r.x ? y == r.y ? d < r.d : y < r.y : x < r.x; }\n\tbool operator < (const Pos& r) const { return zero(x - r.x) ? zero(y - r.y) ? d < r.d : y < r.y : x < r.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tld xy() const { return x * y; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tbool close(const Pos& p) const { return zero((*this - p).Euc()); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n} pos[LEN << 3]; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n\tVec& operator *= (const ld& scalar) { vy *= scalar; vx *= scalar; return *this; }\n\tVec& operator /= (const ld& scalar) { vy /= scalar; vx /= scalar; return *this; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tbool operator < (const Line& l) const {\n\t\tbool f1 = Zero < s;\n\t\tbool f2 = Zero < l.s;\n\t\tif (f1 != f2) return f1;\n\t\tld CCW = s / l.s;\n\t\treturn zero(CCW) ? c * hypot(l.s.vy, l.s.vx) < l.c * hypot(s.vy, s.vx) : CCW > 0;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n\tLine operator + (const ld& scalar) const { return Line(s, c + hypot(s.vy, s.vx) * scalar); }\n\tLine operator - (const ld& scalar) const { return Line(s, c - hypot(s.vy, s.vx) * scalar); }\n\tLine operator * (const ld& scalar) const { return Line({ s.vy * scalar, s.vx * scalar }, c * scalar); }\n\tLine& operator += (const ld& scalar) { c += hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator -= (const ld& scalar) { c -= hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator *= (const ld& scalar) { s *= scalar, c *= scalar; return *this; }\n\tld dist(const Pos& p) const { return s.vy * p.x + s.vx * p.y; }\n\tld above(const Pos& p) const { return s.vy * p.x + s.vx * p.y - c; }\n\tfriend std::ostream& operator << (std::ostream& os, const Line& l) { os << l.s.vy << \" \" << l.s.vx << \" \" << l.c; return os; }\n};\nconst Line Xaxis = { { 0, -1 }, 0 };\nconst Line Yaxis = { { 1, 0 }, 0 };\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nld cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = cross(d1, d2, d3);\n\treturn zero(ret) ? 0 : ret > 0 ? 1 : -1;\n}\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = dot(d1, d3, d2);\n\treturn !ccw(d1, d2, d3) && (ret > 0 || zero(ret));\n}\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = dot(d1, d3, d2);\n\treturn !ccw(d1, d2, d3) && ret > 0;\n}\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) {\n\tld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);\n\treturn (p1 * a2 + p2 * a1) / (a1 + a2);\n}\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t, Pos& v) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tv = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n\tCircle operator + (const Circle& C) const { return { {c.x + C.c.x, c.y + C.c.y}, r + C.r }; }\n\tCircle operator - (const Circle& C) const { return { {c.x - C.c.x, c.y - C.c.y}, r - C.r }; }\n\tld H(const ld& th) const { return sin(th) * c.x + cos(th) * c.y + r; }// coord trans | check right\n\tld A() const { return r * r * PI; }\n\tfriend std::istream& operator >> (std::istream& is, Circle& c) { is >> c.c.x >> c.c.y >> c.r; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Circle& c) { os << c.c.x << \" \" << c.c.y << \" \" << c.r; return os; }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nld minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\t//std::cout << v.mag() << \"\\n\";\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V).r;\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\t//std::cout << e << \"\\n\";\n\tint cnt = 50; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\t//std::cout << r1 << \" \" << r2 << \"\\n\";\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1);\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2);\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld ternary_search(const Pos& p0, const Pos& p1, const Pos& p2, const Pos& c, const ld& r) {\n\tPos vec, v1, v2;\n\tauto dist = [&](const ld& t) -> ld {\n\t\tPos s2 = p1 + vec.rot(t);\n\t\tld d = cross(p1, s2, c) / (p1 - s2).mag();\n\t\treturn r - d;\n\t\t};\n\tvec = (p0 - p1);\n\tv1 = ~(p0 - p1), v2 = ~(p2 - p1);\n\tld s = 0, e = rad(v1, v2), m1, m2, d1, d2;\n\tint cnt = 25; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\td1 = dist(m1);\n\t\td2 = dist(m2);\n\t\tif (sign(d1 < d2)) s = m1;\n\t\telse e = m2;\n\t}\n\treturn (s + e) * .5;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tbool f = 1;\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { f = 0; break; }\n\t\t\t\t}\n\t\t\t\tif (!f) continue;\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) {\n\t\t\t\t\t\tassert(eq(r, (P[k] - c).mag()));\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\t//if (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\t//if (eq(r, (p1 - c).mag())) tmp = std::max({ tmp, dist(p0, p1, c1), dist(p1, p2, c) });\n\t\t\t\t\t//else if (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0)\n\t\t\t\t\t//\ttmp = std::max(tmp, ternary_search(p0, p1, p2, c, r));\n\t\t\t\t\t//else tmp = std::max({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\tld d = r - (c - p1).mag();\n\t\t\t\t\t\t//if (d < 10) {\n\t\t\t\t\t\t//\tstd::cout << \"r:: \" << r << \"\\n\";\n\t\t\t\t\t\t//\tstd::cout << \"p0 = \" << p0 << \" \";\n\t\t\t\t\t\t//\tstd::cout << \"p1 = \" << p1 << \" \";\n\t\t\t\t\t\t//\tstd::cout << \"p2 = \" << p2 << \" \";\n\t\t\t\t\t\t//\tstd::cout << \"c = \" << c << \"\\n\";\n\t\t\t\t\t\t//\tstd::cout << \"dist = \" << (c - p1).mag() << \"\\n\";\n\t\t\t\t\t\t//\tstd::cout << \"d:: \" << d << \"\\n\";\n\t\t\t\t\t\t//}\n\t\t\t\t\t\ttmp = std::min(tmp, d);\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\t//std::cout << \"ret:: \" << ret << \"\\n\";\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r);\n\tld db = fit(B, r);\n\tld D = r * 2;\n\t//std::cout << \"r:: \" << r << \" da:: \" << da << \" db:: \" << db << \"\\n\";\n\t//std::cout << \"r*2:: \" << r * 2 << \"da+db:: \" << (da + db) << \"\\n\";\n\treturn sign(D - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\t//std::cout << \"m:: \" << m << \"\\n\";\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPos v;\n\t\t\t//std::cout << \"FUCK::\\n\";\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t, v);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\t//std::cout << \"ra :: \" << ra << \"\\n\";\n\t//std::cout << \"rb :: \" << rb << \"\\n\";\n\t//std::cout << \"r1 :: \" << r1 << \"\\n\";\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\t//std::cout << \"r2 :: \" << r2 << \"\\n\";\n\tret = std::min({ ret, r1, r2 }) * 2;\n\t//std::cout << \"ret:: \" << ret << \"\\n\";\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231\n\n/*\n\n40\n904 -405\n906 -400\n395 -616\n-16 -989\n-10 -992\n0 -992\n27 -991\n50 -990\n103 -987\n122 -985\n130 -984\n151 -981\n163 -979\n228 -966\n253 -960\n260 -958\n312 -942\n324 -938\n369 -921\n392 -912\n408 -905\n442 -888\n467 -875\n500 -857\n524 -843\n527 -841\n570 -812\n604 -786\n618 -775\n658 -742\n687 -716\n715 -688\n731 -671\n772 -623\n797 -591\n812 -570\n843 -523\n876 -463\n878 -459\n899 -416\n40\n-923 -266\n-817 -489\n-724 -604\n-635 -711\n-626 -718\n-536 -780\n-352 -889\n-334 -897\n-205 -934\n102 -954\n270 -920\n357 -891\n399 -874\n494 -820\n513 -809\n629 -718\n672 -680\n756 -591\n934 -213\n944 -122\n954 -4\n955 30\n939 178\n918 261\n894 345\n865 405\n776 565\n709 644\n478 832\n382 877\n305 890\n202 900\n-729 407\n-755 393\n-841 335\n-932 234\n-954 50\n-956 18\n-956 16\n-946 -113\n0\n\n963.3270026199625\n\n36\n510 -524\n616 -398\n631 -375\n675 -289\n694 -250\n726 -115\n728 -73\n732 15\n732 95\n704 213\n690 261\n653 334\n592 434\n497 533\n452 547\n289 518\n66 478\n-394 388\n-638 339\n-677 250\n-713 163\n-724 130\n-735 48\n-735 -41\n-715 -164\n-678 -266\n-630 -386\n-511 -533\n-427 -599\n-400 -619\n-299 -672\n-131 -723\n-27 -738\n110 -722\n287 -678\n426 -598\n21\n722 157\n721 162\n693 258\n687 270\n665 260\n443 -142\n381 -622\n410 -618\n442 -595\n478 -567\n485 -561\n533 -513\n599 -429\n627 -392\n663 -326\n689 -271\n699 -246\n710 -214\n722 -162\n736 74\n734 90\n0\n\n5\n0 0\n3 0\n3 1\n1 3\n0 3\n5\n0 0\n3 0\n3 1\n1 3\n0 3\n0\n\n*/", "accuracy": 1, "time_ms": 7430, "memory_kb": 4064, "score_of_the_acc": -1.014, "final_rank": 13 }, { "submission_id": "aoj_1647_9851672", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <random>\n#include <array>\n#include <tuple>\ntypedef long long ll;\n//typedef long double ld;\ntypedef double ld;\ntypedef std::pair<int, int> pi;\ntypedef std::vector<ld> Vld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-7;\nconst ld PI = acos(-1);\nconst int LEN = 1e3;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\ninline bool eq(const ld& x, const ld& y) { return zero(x - y); }\n\nVld ans;\nint N, M;\nstruct Pos {\n\tld x, y;\n\tint i, d;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) { i = 0, d = 0; }\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\t//bool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\t//bool operator < (const Pos& r) const { return x == r.x ? y == r.y ? d < r.d : y < r.y : x < r.x; }\n\tbool operator < (const Pos& r) const { return zero(x - r.x) ? zero(y - r.y) ? d < r.d : y < r.y : x < r.x; }\n\tbool operator <= (const Pos& p) const { return *this < p || *this == p; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tld xy() const { return x * y; }\n\tPos rot(ld the) const { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tbool close(const Pos& p) const { return zero((*this - p).Euc()); }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << \"(\" << p.x << \", \" << p.y << \")\"; return os; }\n} pos[LEN << 3]; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n\tVec& operator *= (const ld& scalar) { vy *= scalar; vx *= scalar; return *this; }\n\tVec& operator /= (const ld& scalar) { vy /= scalar; vx /= scalar; return *this; }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tLine(Vec V = Vec(0, 0), Pos p = Pos(0, 0)) : s(V) { c = s.vy * p.x + s.vx * p.y; }\n\tLine(Pos ps = Pos(0, 0), Pos pe = Pos(0, 0)) {\n\t\tld dy, dx;\n\t\tdy = pe.y - ps.y;\n\t\tdx = ps.x - pe.x;\n\t\ts = Vec(dy, dx);\n\t\tc = dy * ps.x + dx * ps.y;\n\t}\n\tbool operator < (const Line& l) const {\n\t\tbool f1 = Zero < s;\n\t\tbool f2 = Zero < l.s;\n\t\tif (f1 != f2) return f1;\n\t\tld CCW = s / l.s;\n\t\treturn zero(CCW) ? c * hypot(l.s.vy, l.s.vx) < l.c * hypot(s.vy, s.vx) : CCW > 0;\n\t}\n\tld operator / (const Line& l) const { return s / l.s; }\n\tld operator * (const Line& l) const { return s * l.s; }\n\tLine operator + (const ld& scalar) const { return Line(s, c + hypot(s.vy, s.vx) * scalar); }\n\tLine operator - (const ld& scalar) const { return Line(s, c - hypot(s.vy, s.vx) * scalar); }\n\tLine operator * (const ld& scalar) const { return Line({ s.vy * scalar, s.vx * scalar }, c * scalar); }\n\tLine& operator += (const ld& scalar) { c += hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator -= (const ld& scalar) { c -= hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator *= (const ld& scalar) { s *= scalar, c *= scalar; return *this; }\n\tld dist(const Pos& p) const { return s.vy * p.x + s.vx * p.y; }\n\tld above(const Pos& p) const { return s.vy * p.x + s.vx * p.y - c; }\n\tfriend std::ostream& operator << (std::ostream& os, const Line& l) { os << l.s.vy << \" \" << l.s.vx << \" \" << l.c; return os; }\n};\nconst Line Xaxis = { { 0, -1 }, 0 };\nconst Line Yaxis = { { 1, 0 }, 0 };\nLine L(const Pos& s, const Pos& e) { return Line(s, e); }\nLine rotate90(const Line& l, const Pos& p) { return Line(~l.s, p); }\nPos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nld cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = cross(d1, d2, d3);\n\treturn zero(ret) ? 0 : ret > 0 ? 1 : -1;\n}\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) * (d4 - d3); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = dot(d1, d3, d2);\n\treturn !ccw(d1, d2, d3) && (ret > 0 || zero(ret));\n}\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = dot(d1, d3, d2);\n\treturn !ccw(d1, d2, d3) && ret > 0;\n}\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) {\n\tld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);\n\treturn (p1 * a2 + p2 * a1) / (a1 + a2);\n}\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret;\n}\nbool norm(Polygon& H) {\n\tld a = area(H);\n\tif (zero(a)) return 1;\n\tif (a < 0) std::reverse(H.begin(), H.end());\n\treturn 0;\n}\nPolygon rotate_and_norm(Polygon B, const int& j0, const Polygon& A, const int& i0, const ld& t, Pos& v) {\n\tint sz = B.size();\n\tfor (int j = 0; j < sz; j++) B[j] = B[j].rot(t);\n\tv = A[i0] - B[j0];\n\tfor (int j = 0; j < sz; j++) B[j] += v;\n\treturn B;\n}\nstruct Circle {\n\tPos c;\n\tld r;\n\tCircle(Pos C = Pos(0, 0), ld R = 0) : c(C), r(R) {}\n\tbool operator == (const Circle& C) const { return c == C.c && std::abs(r - C.r) < TOL; }\n\tbool operator != (const Circle& C) const { return !(*this == C); }\n\tbool operator > (const Pos& p) const { return r > (c - p).mag(); }\n\tbool operator >= (const Pos& p) const { return r + TOL > (c - p).mag(); }\n\tbool operator < (const Pos& p) const { return r < (c - p).mag(); }\n\tCircle operator + (const Circle& C) const { return { {c.x + C.c.x, c.y + C.c.y}, r + C.r }; }\n\tCircle operator - (const Circle& C) const { return { {c.x - C.c.x, c.y - C.c.y}, r - C.r }; }\n\tld H(const ld& th) const { return sin(th) * c.x + cos(th) * c.y + r; }// coord trans | check right\n\tld A() const { return r * r * PI; }\n\tfriend std::istream& operator >> (std::istream& is, Circle& c) { is >> c.c.x >> c.c.y >> c.r; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Circle& c) { os << c.c.x << \" \" << c.c.y << \" \" << c.r; return os; }\n} INVAL = { { 0, 0 }, -1 };\nCircle enclose_circle(const Pos& u, const Pos& v) {\n\tPos c = (u + v) * .5;\n\treturn Circle(c, (c - u).mag());\n}\nCircle enclose_circle(const Pos& u, const Pos& v, const Pos& w) {\n\tLine l1 = rotate90(L(u, v), (u + v) * .5);\n\tLine l2 = rotate90(L(v, w), (v + w) * .5);\n\tif (zero(l1 / l2)) return { { 0, 0 }, -1 };\n\tPos c = intersection(l1, l2);\n\tld r = (c - u).mag();\n\treturn Circle(c, r);\n}\nCircle minimum_enclose_circle(std::vector<Pos> P) {\n\tshuffle(P.begin(), P.end(), std::mt19937(0x14004));\n\tCircle mec = INVAL;\n\tint sz = P.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tif (mec.r < -1 || mec < P[i]) {\n\t\t\tmec = Circle(P[i], 0);\n\t\t\tfor (int j = 0; j <= i; j++) {\n\t\t\t\tif (mec < P[j]) {\n\t\t\t\t\tCircle ans = enclose_circle(P[i], P[j]);\n\t\t\t\t\tif (zero(mec.r)) { mec = ans; continue; }\n\t\t\t\t\tCircle l = INVAL, r = INVAL;\n\t\t\t\t\tfor (int k = 0; k <= j; k++) {\n\t\t\t\t\t\tif (ans < P[k]) {\n\t\t\t\t\t\t\tint CCW = ccw(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tCircle c = enclose_circle(P[i], P[j], P[k]);\n\t\t\t\t\t\t\tif (c.r < 0) continue;\n\t\t\t\t\t\t\telse if (CCW > 0 && (l.r < 0 || cross(P[i], P[j], c.c) > cross(P[i], P[j], l.c))) l = c;\n\t\t\t\t\t\t\telse if (CCW < 0 && (r.r < 0 || cross(P[i], P[j], c.c) < cross(P[i], P[j], r.c))) r = c;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (l.r < 0 && r.r < 0) mec = ans;\n\t\t\t\t\telse if (l.r < 0) mec = r;\n\t\t\t\t\telse if (r.r < 0) mec = l;\n\t\t\t\t\telse mec = l.r < r.r ? l : r;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn mec;\n}\nld minimum_enclose_circle(Polygon A, int i, Polygon B, int j, ld d) {\n\tPolygon V;\n\tPos v = (A[(i + 1) % A.size()] - B[(j + 1) % B.size()]).unit() * d;\n\t//std::cout << v.mag() << \"\\n\";\n\tfor (Pos& p : B) p += v;\n\tfor (const Pos& p : A) V.push_back(p);\n\tfor (const Pos& p : B) V.push_back(p);\n\treturn minimum_enclose_circle(V).r;\n}\nld ternary_search(const Polygon& A, const int& i, const Polygon& B, const int& j) {\n\tint a = A.size(), b = B.size();\n\tconst Pos& pa = A[(i + 1) % a], & pb = B[(j + 1) % b];\n\tld s = 0, e = (pa - pb).mag(), m1, m2, r1 = 0, r2 = 0;\n\t//std::cout << e << \"\\n\";\n\tint cnt = 50; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\t//std::cout << r1 << \" \" << r2 << \"\\n\";\n\t\tr1 = minimum_enclose_circle(A, i, B, j, m1);\n\t\tr2 = minimum_enclose_circle(A, i, B, j, m2);\n\t\tif (r1 > r2) s = m1;\n\t\telse e = m2;\n\t}\n\treturn r2;\n}\nld ternary_search(const Pos& p0, const Pos& p1, const Pos& p2, const Pos& c, const ld& r) {\n\tPos vec, v1, v2;\n\tauto dist = [&](const ld& t) -> ld {\n\t\tPos s2 = p1 + vec.rot(t);\n\t\tld d = cross(p1, s2, c) / (p1 - s2).mag();\n\t\treturn r - d;\n\t\t};\n\tvec = (p0 - p1);\n\tv1 = ~(p0 - p1), v2 = ~(p2 - p1);\n\tld s = 0, e = rad(v1, v2), m1, m2, d1, d2;\n\tint cnt = 30; while (cnt--) {\n\t\tm1 = (s + s + e) / 3;\n\t\tm2 = (s + e + e) / 3;\n\t\td1 = dist(m1);\n\t\td2 = dist(m2);\n\t\tif (sign(d1 < d2)) s = m1;\n\t\telse e = m2;\n\t}\n\treturn (s + e) * .5;\n}\nld fit(const Polygon& P, const ld& r) {\n\tauto dist = [&](const Pos& s1, const Pos& s2, const Pos& c) -> ld {\n\t\tld d = cross(s1, s2, c) / (s1 - s2).mag();\n\t\treturn r + d;\n\t\t};\n\tint sz = P.size();\n\tld ret = INF;\n\tfor (int i = 0; i < sz; i++) {\n\t\tfor (int j = i + 1; j < sz; j++) {\n\t\t\tld tmp = INF;\n\t\t\tconst Pos& p = P[i], & q = P[j];\n\t\t\tPos mid = (p + q) * .5;\n\t\t\tPos vec = p - mid;\n\t\t\tld h = vec.mag();\n\t\t\tif (sign(h - r) > 0) continue;\n\t\t\tld w = sqrt(r * r - h * h);\n\t\t\tPos c1 = mid + ~vec.unit() * w;\n\t\t\tPos c2 = mid - ~vec.unit() * w;\n\t\t\tfor (const Pos& c : { c1, c2 }) {\n\t\t\t\tbool f = 1;\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (sign((P[k] - c).mag() - r) > 0) { f = 0; break; }\n\t\t\t\t}\n\t\t\t\tif (!f) continue;\n\t\t\t\tfor (int k = 0; k < sz; k++) {\n\t\t\t\t\tconst Pos& p0 = P[(k - 1 + sz) % sz], & p1 = P[k], & p2 = P[(k + 1) % sz];\n\t\t\t\t\tassert(ccw(p0, p1, p2) >= 0);\n\t\t\t\t\tif (k == i || k == j) {\n\t\t\t\t\t\tassert(eq(r, (P[k] - c).mag()));\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\t//if (sign((P[k] - c).mag() - r) > 0) { tmp = INF; break; }\n\t\t\t\t\t//if (eq(r, (p1 - c).mag())) tmp = std::max({ tmp, dist(p0, p1, c1), dist(p1, p2, c) });\n\t\t\t\t\t//else if (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0)\n\t\t\t\t\t//\ttmp = std::max(tmp, ternary_search(p0, p1, p2, c, r));\n\t\t\t\t\t//else tmp = std::max({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t\tif (ccw(p0, p1, c) < 0 && ccw(p1, p2, c) < 0 && dot(p0, p1, c) > 0 && dot(p2, p1, c) > 0) {\n\t\t\t\t\t\tld d = r - (c - p1).mag();\n\t\t\t\t\t\t//if (d < 10) {\n\t\t\t\t\t\t//\t//std::cout << \"r:: \" << r << \"\\n\";\n\t\t\t\t\t\t//\t//std::cout << \"p0 = \" << p0 << \" \";\n\t\t\t\t\t\t//\t//std::cout << \"p1 = \" << p1 << \" \";\n\t\t\t\t\t\t//\t//std::cout << \"p2 = \" << p2 << \" \";\n\t\t\t\t\t\t//\t//std::cout << \"c = \" << c << \"\\n\";\n\t\t\t\t\t\t//\t//std::cout << \"dist = \" << (c - p1).mag() << \"\\n\";\n\t\t\t\t\t\t//\t//std::cout << \"d:: \" << d << \"\\n\";\n\t\t\t\t\t\t//}\n\t\t\t\t\t\ttmp = std::min(tmp, d);\n\t\t\t\t\t}\n\t\t\t\t\telse tmp = std::min({ tmp, dist(p0, p1, c), dist(p1, p2, c) });\n\t\t\t\t}\n\n\n\t\t\t\tret = std::min(ret, tmp);\n\t\t\t}\n\t\t}\n\t}\n\t//std::cout << \"ret:: \" << ret << \"\\n\";\n\treturn ret;\n}\nbool fit(const Polygon& A, const Polygon& B, const ld& r) {\n\tld da = fit(A, r);\n\tld db = fit(B, r);\n\tld D = r * 2;\n\t//std::cout << \"r:: \" << r << \" da:: \" << da << \" db:: \" << db << \"\\n\";\n\t//std::cout << \"r*2:: \" << r * 2 << \"da+db:: \" << (da + db) << \"\\n\";\n\treturn sign(D - (da + db)) >= 0 ? 1 : 0;\n}\nld bi_search(const Polygon& A, const Polygon& B, ld s, ld e) {\n\tint cnt = 30; while (cnt--) {\n\t\tld m = (s + e) * .5;\n\t\t//std::cout << \"m:: \" << m << \"\\n\";\n\t\tif (fit(A, B, m)) e = m;\n\t\telse s = m;\n\t}\n\treturn (s + e) * .5;\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tPolygon A(N);\n\tfor (Pos& p : A) std::cin >> p;\n\tnorm(A);\n\tstd::cin >> M;\n\tPolygon B(M);\n\tfor (Pos& p : B) std::cin >> p;\n\tnorm(B);\n\tld ret = INF;\n\tld r1 = INF;\n\tld ra = minimum_enclose_circle(A).r, rb = minimum_enclose_circle(B).r;\n\tfor (int i = 0; i < N; i++) {\n\t\tPos& I0 = A[i], & I1 = A[(i + 1) % N];\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tPos& J0 = B[j], & J1 = B[(j + 1) % M];\n\t\t\tld t = rad(J0 - J1, I1 - I0);\n\t\t\tPos v;\n\t\t\t//std::cout << \"FUCK::\\n\";\n\t\t\tPolygon B2 = rotate_and_norm(B, j, A, i, t, v);\n\t\t\tr1 = std::min(r1, ternary_search(A, i, B2, j));\n\t\t}\n\t}\n\t//std::cout << \"ra :: \" << ra << \"\\n\";\n\t//std::cout << \"rb :: \" << rb << \"\\n\";\n\t//std::cout << \"r1 :: \" << r1 << \"\\n\";\n\tld r2 = bi_search(A, B, std::max(ra, rb), r1);\n\t//std::cout << \"r2 :: \" << r2 << \"\\n\";\n\tret = std::min({ ret, r1, r2 }) * 2;\n\t//std::cout << \"ret:: \" << ret << \"\\n\";\n\tans.push_back(ret);\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (const ld& ret : ans) std::cout << ret << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj20231\n\n/*\n\n40\n904 -405\n906 -400\n395 -616\n-16 -989\n-10 -992\n0 -992\n27 -991\n50 -990\n103 -987\n122 -985\n130 -984\n151 -981\n163 -979\n228 -966\n253 -960\n260 -958\n312 -942\n324 -938\n369 -921\n392 -912\n408 -905\n442 -888\n467 -875\n500 -857\n524 -843\n527 -841\n570 -812\n604 -786\n618 -775\n658 -742\n687 -716\n715 -688\n731 -671\n772 -623\n797 -591\n812 -570\n843 -523\n876 -463\n878 -459\n899 -416\n40\n-923 -266\n-817 -489\n-724 -604\n-635 -711\n-626 -718\n-536 -780\n-352 -889\n-334 -897\n-205 -934\n102 -954\n270 -920\n357 -891\n399 -874\n494 -820\n513 -809\n629 -718\n672 -680\n756 -591\n934 -213\n944 -122\n954 -4\n955 30\n939 178\n918 261\n894 345\n865 405\n776 565\n709 644\n478 832\n382 877\n305 890\n202 900\n-729 407\n-755 393\n-841 335\n-932 234\n-954 50\n-956 18\n-956 16\n-946 -113\n0\n\n963.3270026199625\n\n36\n510 -524\n616 -398\n631 -375\n675 -289\n694 -250\n726 -115\n728 -73\n732 15\n732 95\n704 213\n690 261\n653 334\n592 434\n497 533\n452 547\n289 518\n66 478\n-394 388\n-638 339\n-677 250\n-713 163\n-724 130\n-735 48\n-735 -41\n-715 -164\n-678 -266\n-630 -386\n-511 -533\n-427 -599\n-400 -619\n-299 -672\n-131 -723\n-27 -738\n110 -722\n287 -678\n426 -598\n21\n722 157\n721 162\n693 258\n687 270\n665 260\n443 -142\n381 -622\n410 -618\n442 -595\n478 -567\n485 -561\n533 -513\n599 -429\n627 -392\n663 -326\n689 -271\n699 -246\n710 -214\n722 -162\n736 74\n734 90\n0\n\n5\n0 0\n3 0\n3 1\n1 3\n0 3\n5\n0 0\n3 0\n3 1\n1 3\n0 3\n0\n\n*/", "accuracy": 1, "time_ms": 7440, "memory_kb": 4080, "score_of_the_acc": -1.0203, "final_rank": 14 }, { "submission_id": "aoj_1647_9408874", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\n#include <type_traits>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <iostream>\n#include <numeric>\n#include <cassert>\n\n\n\ntemplate<typename Float>\nconstexpr Float EPS = Float{1e-6};\n\ntemplate<typename Float>\nconstexpr int sign(const Float &x, Float eps = EPS<Float>){\n return (x < -eps ? -1 : x > eps ? 1 : 0);\n}\n\ntemplate<typename Float>\nstruct point {\n Float x, y;\n point () {}\n point (Float _x, Float _y) : x(_x), y(_y) {}\n point &operator+=(const point &r){\n x += r.x;\n y += r.y;\n return *this;\n }\n point &operator-=(const point &r){\n x -= r.x;\n y -= r.y;\n return *this;\n }\n point operator+() const {\n return *this;\n }\n point operator-() const {\n x = -x;\n y = -y;\n return *this;\n }\n point &operator*=(const Float &a){\n x *= a;\n y *= a;\n return *this;\n }\n\tpoint &operator/=(const Float &a){\n x /= a;\n y /= a;\n return *this;\n }\n friend point operator+(const point& lhs, const point& rhs){\n return point(lhs) += rhs;\n }\n friend point operator-(const point& lhs, const point& rhs){\n return point(lhs) -= rhs;\n }\n friend point operator*(const point& lhs, const Float& a){\n return point(lhs) *= a;\n }\n friend point operator*(const Float& a, const point& rhs){\n return point(rhs) *= a;\n }\n\tfriend point operator/(const point& lhs, const Float& a){\n return point(lhs) /= a;\n }\n friend std::ostream &operator<<(std::ostream &os, const point& p){\n return os << p.x << ' ' << p.y;\n }\n friend std::istream &operator>>(std::istream &is, point &a){\n Float _x, _y; is >> _x >> _y;\n a = point(_x, _y);\n return (is);\n }\n friend Float norm(const point &a) {\n return a.x*a.x + a.y*a.y;\n }\n friend Float dot(const point &a, const point &b){\n return a.x*b.x + a.y*b.y;\n }\n friend Float cross(const point &a, const point &b){\n return a.x*b.y - a.y*b.x;\n }\n\tfriend point rot90(const point &a){\n\t\treturn point(-a.y, a.x);\n\t}\n friend point rot(const point &a, const Float rad){\n using std::cos, std::sin;\n Float cosrad = cos(rad);\n Float sinrad = sin(rad);\n return point(a.x * cosrad - a.y * sinrad, a.x * sinrad + a.y * cosrad);\n }\n friend Float abs(const point &a){\n using std::sqrt;\n return sqrt(norm(a));\n }\n\n friend int quadrant_atan2(const point &a){\n // not origin point\n // ceil ( atan2(y,x) / (pi/2) )\n\t\tint signx = sign(a.x);\n\t\tint signy = sign(a.y);\n\t\tif (signx <= 0 && signy < 0) return -1;\n\t\tif (signx > 0 && signy <= 0) return 0;\n\t\tif (signx >= 0 && signy > 0) return 1;\n\t\tif (signx < 0 && signy >= 0) return 2;\n // origin point x == 0 && y == 0\n return 0;\n }\n\n friend int sign_cross(const point &a, const point &b){\n using std::abs;\n return sign(cross(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n }\n\n friend int sign_dot(const point &a, const point &b){\n using std::abs;\n return sign(dot(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n }\n\n\tfriend int ccw(const point &a, point b, point c){\n\t\tb -= a;\n\t\tc -= a;\n\t\tint signcr = sign_cross(b, c);\n\t\tif (signcr > 0){\n\t\t\t// ccw \n\t\t\t// c\n\t\t\t// a --> b \n\t\t\treturn 1;\n\t\t}\n\t\tif (signcr < 0){\n\t\t\t// cw\n\t\t\t// a --> b\n\t\t\t// c\n\t\t\treturn -1;\n\t\t}\n\t\tif (sign_dot(b, c) < 0){\n\t\t\t// c a --> b\n\t\t\treturn 2;\n\t\t}\n\t\tif (norm(b) < norm(c)){\n\t\t\t// a --> b c\n\t\t\treturn -2;\n\t\t}\n\t\t// a - b -> c\n\t\treturn 0;\n\t}\n friend bool has_common_point(const point &a, const point &b){\n return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0;\n }\n};\n\ntemplate<typename Float>\nstruct arg_less {\n constexpr bool operator()(const point<Float> &l, const point<Float> &r){\n using std::atan2;\n return atan2(l.y, l.x) < atan2(r.y, r.x);\n }\n};\n\ntemplate<typename Float>\nvoid arg_sort(std::vector<point<Float>> &a){\n\tstd::sort(a.begin(), a.end(), arg_less<Float>{});\n}\n\n\ntemplate<typename Point>\nstd::vector<int> upper_convex_hull_index(const std::vector<Point> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) ptr--;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[i] - a[c], a[c] - a[p]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<typename Float>\nstd::vector<int> lower_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) continue;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[c] - a[p], a[i] - a[c]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<typename Point>\nstd::vector<int> convex_hull_index(const std::vector<Point> &a){\n\tif (a.empty()) return {};\n\tauto upper = upper_convex_hull_index(a);\n\tauto lower = lower_convex_hull_index(a);\n\tif (upper.size() == 1u){\n\t\t// lower.size() == 1u\n\t\tif (a[upper.front()] == a[lower.front()]){\n\t\t\treturn {upper.front()};\n\t\t}\n\t\treturn {lower.front(), upper.front()};\n\t}\n\tif (a[upper.back()] == a[lower.back()]){\n\t\tlower.pop_back();\n\t}\n\tlower.insert(lower.end(), upper.rbegin(), upper.rend());\n\tif (a[upper.front()] == a[lower.front()]) lower.pop_back();\n\treturn lower;\n}\n\n\ntemplate<typename Float>\nstruct line {\n\tpoint<Float> end0, end1;\n line () {}\n\tline (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\t// assert(end0 != end1);\n\t}\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const line &a, const line &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const line &a, const line &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const line &a, const point<Float> &b){\n\t\treturn sign(cross(a.direction(), b - a.end0)) == 0;\n\t}\n\tfriend bool has_common_point(const line &a, const line &b){\n\t\treturn !is_parallel(a, b) || has_common_point(a, b.end0);\n\t}\n\tfriend point<Float> common_point(const line &a, const line &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tfriend point<Float> projection(const line &a, const point<Float> &b){\n\t\tauto dir = a.direction();\n\t\treturn a.end0 + dir * (dot(dir, b - a.end0) / norm(dir));\n\t}\n\tfriend point<Float> reflection(const line &a, const point<Float> &b){\n\t\tauto prj = projection(a, b);\n\t\treturn prj + prj - b;\n\t}\n};\n\n\ntemplate<typename Float>\nstruct segment {\n\tpoint<Float> end0, end1;\n segment () {}\n\tsegment (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\t// assert(end0 != end1);\n\t}\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const segment &a, const segment &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const segment &a, const segment &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const segment &a, const segment &b){\n\t\treturn ccw(a.end0, a.end1, b.end0) * ccw(a.end0, a.end1, b.end1) <= 0\n\t\t\t&& ccw(b.end0, b.end1, a.end0) * ccw(b.end0, b.end1, a.end1) <= 0;\n\t}\n\tfriend bool has_common_point(const segment &a, const point<Float> &b){\n\t\treturn ccw(a.end0, a.end1, b) == 0;\n\t}\n\tfriend point<Float> common_point(const segment &a, const segment &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\tif (has_common_point(a, b.end0)){\n\t\t\t\treturn b.end0;\n\t\t\t}\n\t\t\tif (has_common_point(a, b.end1)){\n\t\t\t\treturn b.end1;\n\t\t\t}\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tline<Float> as_line() const {\n\t\treturn line<Float>(end0, end1);\n\t}\n};\n\ntemplate<typename Float>\nstruct circle {\n point<Float> center;\n Float radius;\n circle () {}\n circle (const point<Float> &_center, const Float &_radius) : center(_center), radius(_radius) {\n assert(sign(radius) >= 0);\n }\n friend int num_of_common_tangents(const circle &a, const circle &b){\n if (has_common_point(a.center, b.center) && sign(a.radius - b.radius) == 0){\n return 5; // infty\n }\n Float d = abs(a.center - b.center);\n Float r1 = a.radius, r2 = b.radius;\n int sumsign = sign(d - (r1 + r2));\n if (sumsign >= 0) return sumsign + 3;\n using std::abs;\n int diffsign = sign(d - abs(r1 - r2));\n return diffsign + 1;\n }\n friend bool has_common_point(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n return cnt == 1 || cnt == 2 || cnt == 3 || cnt == 5;\n }\n friend bool has_common_point(const circle &c, const line<Float> &l){\n point<Float> p = reflection(l, c.center);\n Float d = abs(c.center, p);\n return sign(c.radius - d) >= 0;\n }\n friend bool has_common_point(const circle &c, const segment<Float> &s){\n // return std::ranges::any_of(common_points(c, s.as_line()), [&](auto p){\n // return has_common_point(s, p);\n // });\n return false;\n }\n friend bool has_common_point(const circle &c, const point<Float> &p){\n return sign(abs(p - c.center) - c.radius) == 0;\n }\n friend std::vector<point<Float>> common_points(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n assert(cnt != 5); // cannot be same circle\n if (cnt == 0 || cnt == 4){\n return {};\n }\n Float d = abs(a.center - b.center);\n Float x = (d*d + a.radius*a.radius - b.radius*b.radius) / (d + d);\n point<Float> p = a.center + (b.center - a.center) * x / d;\n if (cnt == 1 || cnt == 3 || sign(a.radius - x) <= 0){\n return {p};\n }\n point<Float> v = rot90(b.center - a.center);\n using std::sqrt;\n v *= sqrt(a.radius*a.radius - x*x) / abs(v);\n return {p + v, p - v};\n }\n friend std::vector<point<Float>> common_points(const circle &c, const line<Float> &l) {\n point<Float> p = reflection(l, c.center);\n Float d = abs(c.center, p);\n int cnt = sign(c.radius - d) + 1;\n if (cnt == 0){\n return {};\n }\n if (cnt == 1 || sign(c.radius - d) <= 0){\n return {p};\n }\n point<Float> v = l.direction();\n using std::sqrt;\n v *= sqrt(c.radius*c.radius - d*d) / abs(v);\n return {p + v, p - v};\n }\n};\n\n\ntemplate<>\nconstexpr ld EPS<ld> = 1e-8;\n\nusing pt = point<ld>;\nusing cr = circle<ld>;\n\nbool includes(const vector<pt> &a, ld r, ld ylow){\n int n = a.size();\n pt ga(0,0);\n rep(i,n) ga += a[i];\n ga /= n;\n rep(i,n) rep(j,n){\n if (i == j) continue;\n pt mid = (a[i] + a[j]) / 2;\n ld d = abs(a[i] - a[j]) / 2;\n if (sign(d - r) >= 0) continue;\n pt ctr = mid + rot90(mid - a[i]) * sqrt(r*r-d*d) / d;\n bool ok = true;\n rep(k,n){\n if (sign(abs(a[k] - ctr) - r) <= 0) continue;\n ok = false;\n break;\n }\n if (!ok) continue;\n rep(k,n){\n line<ld> l(a[k],a[k == n-1 ? 0 : k+1]);\n pt pr = projection(l, ctr);\n int dirg = ccw(l.end0, l.end1, ga);\n int dirc = ccw(l.end0, l.end1, ctr);\n ld cur = r;\n if (dirg == dirc){\n cur += abs(pr - ctr);\n }\n else {\n cur -= abs(pr - ctr);\n }\n if (sign(r - cur - ylow) >= 0){\n return true;\n }\n }\n rep(k,n){\n if (sign(abs(ctr - a[k])) == 0){\n if (sign(ylow) <= 0){\n return true;\n }\n continue;\n }\n line<ld> l(a[k], a[k] + rot90(a[k] - ctr));\n int dirg = ccw(l.end0, l.end1, ga);\n int dirc = ccw(l.end0, l.end1, ctr);\n if (dirg == dirc) continue;\n ld cur = r - abs(projection(l, ctr) - ctr);\n if (sign(r - cur - ylow) < 0) continue;\n bool okk = true;\n rep(kk,n){\n if (ccw(l.end0, l.end1, a[kk]) != dirc) continue;\n okk = false;\n break;\n }\n if (okk) return true;\n }\n }\n rep(i,n){\n pt dir = (a[i == n-1 ? 0 : i+1] - a[i]);\n dir /= abs(dir);\n ld xl = -1e9, xr = 1e9;\n rep(j,n){\n pt v = a[j] - a[i];\n ld h = abs(cross(v, dir));\n if (sign(ylow + h - r) >= 0){\n xl = 1;\n xr = -1;\n break;\n }\n ld geta = dot(v, dir);\n ld y = ylow + h;\n ld w = sqrt(r*r - y*y);\n chmax(xl,-w-geta);\n chmin(xr,+w-geta);\n }\n if (sign(xl - xr) <= 0){\n return true;\n }\n }\n return false;\n}\n\nld height(const vector<pt> &a, ld r){\n int n = a.size();\n vector<cr> cs(n);\n rep(i,n){\n cs[i] = cr(a[i],r);\n }\n auto check = [&](pt p){\n bool ok = true;\n rep(k,n){\n if (sign(abs(a[k] - p) - r) <= 0) continue;\n ok = false;\n break;\n }\n return ok;\n };\n bool inc = false;\n rep(i,n-1) repp(j,i+1,n){\n if (inc) break;\n for (auto p : common_points(cs[i], cs[j])){\n if (check(p)){\n inc = true;\n break;\n }\n }\n }\n rep(i,n){\n if (check(a[i])){\n inc = true;\n break;\n }\n }\n if (!inc){\n return r*10;\n }\n ld le = 0, ri = 2*r;\n rep(tt,60){\n ld md = (le + ri) / 2;\n if (includes(a, r, r - md)){\n ri = md;\n }\n else {\n le = md;\n }\n }\n return ri;\n}\n\nvoid solve(int n){\n vector<pt> a(n); in(a);\n int m; in(m);\n vector<pt> b(m); in(b);\n ld le = 0, ri = 1e9;\n rep(tt,60){\n ld md = (le + ri) / 2;\n ld ha = height(a, md);\n ld hb = height(b, md);\n if (ha + hb <= md + md){\n ri = md;\n }\n else {\n le = md;\n }\n }\n out(ri*2);\n}\n\nint main(){\n while (true){\n int n; in(n);\n if (n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 7890, "memory_kb": 3560, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_1647_6794725", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nclass point2d {\npublic:\n\tdouble x, y;\n\tpoint2d() : x(0.0), y(0.0) {};\n\tpoint2d(double x_, double y_) : x(x_), y(y_) {};\n\tpoint2d& operator+=(const point2d& p) { x += p.x; y += p.y; return (*this); }\n\tpoint2d& operator-=(const point2d& p) { x -= p.x; y -= p.y; return (*this); }\n\tpoint2d& operator*=(double v) { x *= v; y *= v; return (*this); }\n\tpoint2d operator+(const point2d& p) const { return point2d(*this) += p; }\n\tpoint2d operator-(const point2d& p) const { return point2d(*this) -= p; }\n\tpoint2d operator*(double v) const { return point2d(*this) *= v; }\n\tdouble abs() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\tdouble dot(const point2d& p) const {\n\t\treturn x * p.x + y * p.y;\n\t}\n};\n\nconst double pi = acos(-1.0);\n\n// Q. Can the polygon be placed in range x^2 + y^2 <= R, y >= YL ???\nbool check(int N, const vector<point2d>& P, double R, double YL, const vector<vector<vector<point2d> > >& anglist) {\n\t// Case #1. Two vertices in line y = YL\n\tfor (int i = 0; i < N; i++) {\n\t\tdouble l = -1.0e+9, r = +1.0e+9;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tpoint2d subp = point2d(0, YL) + anglist[i][(i + 1) % N][j];\n\t\t\tif (subp.y >= R) {\n\t\t\t\tl = +1.0e+9;\n\t\t\t\tr = -1.0e+9;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdouble w = sqrt(R * R - subp.y * subp.y);\n\t\t\t\tl = max(l, -w - subp.x);\n\t\t\t\tr = min(r, +w - subp.x);\n\t\t\t}\n\t\t}\n\t\tif (l < r) {\n\t\t\treturn true;\n\t\t}\n\t}\n\t// Case #2. Two vertices in circle's circumference\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (i == j || (P[j] - P[i]).abs() >= 2 * R) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdouble elen = (P[j] - P[i]).abs();\n\t\t\tdouble subh = sqrt(R * R - elen * elen / 4);\n\t\t\tdouble l1 = 0.0, r1 = 2 * pi;\n\t\t\tdouble l2 = 0.0, r2 = 2 * pi;\n\t\t\tbool valid = true;\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tpoint2d subp = point2d(-elen / 2, -subh) + anglist[i][j][k];\n\t\t\t\tdouble d = subp.abs();\n\t\t\t\tif (d >= R + 1.0e-7 || (YL >= 0 && d <= YL)) {\n\t\t\t\t\tvalid = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tdouble phi = atan2(subp.y, subp.x);\n\t\t\t\tif (d > -YL) {\n\t\t\t\t\tdouble g = asin(YL / d);\n\t\t\t\t\tdouble cl = g - phi;\n\t\t\t\t\tdouble cr = (pi - g) - phi;\n\t\t\t\t\twhile (cl < 0.0) cl += 2 * pi;\n\t\t\t\t\twhile (cl >= 2 * pi) cl -= 2 * pi;\n\t\t\t\t\twhile (cr < 0.0) cr += 2 * pi;\n\t\t\t\t\twhile (cr >= 2 * pi) cr -= 2 * pi;\n\t\t\t\t\tif (cl <= cr) {\n\t\t\t\t\t\tl1 = max(l1, cl);\n\t\t\t\t\t\tr1 = min(r1, cr);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tl2 = max(l2, cl);\n\t\t\t\t\t\tr2 = min(r2, cr);\n\t\t\t\t\t}\n\t\t\t\t\tif (!((l2 < r2 && l1 <= r1) || (l1 <= r2 && r2 <= r1) || (l1 <= l2 && l2 <= r1))) {\n\t\t\t\t\t\tvalid = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (valid) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\ndouble min_height(int N, const vector<point2d>& P, double R) {\n\tvector<vector<vector<point2d> > > anglist(N, vector<vector<point2d> >(N, vector<point2d>(N)));\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (i == j) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tpoint2d evec = P[j] - P[i];\n\t\t\tdouble elen = evec.abs();\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tdouble slen = (P[k] - P[i]).abs();\n\t\t\t\tdouble theta = (i != k ? acos(max(min(evec.dot(P[k] - P[i]) / (elen * slen), +1.0), -1.0)) : 0.0);\n\t\t\t\tif ((k - i + N) % N < (j - i + N) % N) {\n\t\t\t\t\ttheta *= -1.0;\n\t\t\t\t}\n\t\t\t\tanglist[i][j][k] = point2d(cos(theta), sin(theta)) * slen;\n\t\t\t}\n\t\t}\n\t}\n\tdouble hl = 0.0, hr = 2.0 * R;\n\twhile (hr - hl > 1.0e-7) {\n\t\tdouble h = (hl + hr) * 0.5;\n\t\tif (check(N, P, R, R - h, anglist) == true) {\n\t\t\thr = h;\n\t\t}\n\t\telse {\n\t\t\thl = h;\n\t\t}\n\t}\n\treturn hr;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<point2d> P1(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> P1[i].x >> P1[i].y;\n\t\t}\n\t\tint M;\n\t\tcin >> M;\n\t\tvector<point2d> P2(M);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> P2[i].x >> P2[i].y;\n\t\t}\n\t\tdouble l = 0.0, r = 1.0e+4;\n\t\twhile (r - l > 1.0e-7) {\n\t\t\tdouble m = (l + r) * 0.5;\n\t\t\tdouble res1 = min_height(N, P1, m);\n\t\t\tdouble res2 = min_height(M, P2, m);\n\t\t\tif (res1 + res2 <= 2 * m) {\n\t\t\t\tr = m;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tl = m;\n\t\t\t}\n\t\t}\n\t\tcout.precision(15);\n\t\tcout << r * 2.0 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7380, "memory_kb": 5228, "score_of_the_acc": -1.2864, "final_rank": 16 }, { "submission_id": "aoj_1647_6025527", "code_snippet": "//Let's join Kaede Takagaki Fan Club !!\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cassert>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n#include <cassert>\n#include <iomanip>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <complex>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\n#define int long long\n//#define L __int128\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n#define eps 1e-7\n#define INF 1000000000\n#define a first\n#define b second\n#define fi first\n#define sc second\n#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,x) for(int i=0;i<x;i++)\n#define repn(i,x) for(int i=1;i<=x;i++)\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n#define all(x) x.begin(),x.end()\n#define si(x) int(x.size())\n \n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n \ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n \ntemplate<class t> using vc=vector<t>;\n \ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.fi<<\",\"<<p.sc<<\"}\";\n}\n \ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n \ntemplate<class T>\nvoid g(T &a){\n\tcin >> a;\n}\ntemplate<class T>\nvoid o(const T &a,bool space=false){\n\tcout << a << (space?' ':'\\n');\n}\n//ios::sync_with_stdio(false);\n//const ll mod = 998244353;\nconst ll mod = 1000000007;\nmt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());\ntemplate<class T>\nvoid add(T&a,T b){\n\ta+=b;\n\tif(a >= mod) a-=mod;\n}\nll modpow(ll x,ll n){\n\tll res=1;\n\twhile(n>0){\n\t\tif(n&1) res=res*x%mod;\n\t\tx=x*x%mod;\n\t\tn>>=1;\n\t}\n\treturn res;\n}\n#define _sz 1\nll F[_sz],R[_sz];\nvoid make(){\n\tF[0] = 1;\n\tfor(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod;\n\tR[_sz-1] = modpow(F[_sz-1], mod-2);\n\tfor(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod;\n}\nll C(int a,int b){\n\tif(b < 0 || a < b) return 0;\n\treturn F[a]*R[b]%mod*R[a-b]%mod;\n}\n#define double long double\ntypedef complex<double> pt;\ntypedef pair<pt,pt> L;\ntypedef vector<P> poly;\nconst double EPS = 1e-15;\n#define x real()\n#define y imag()\nbool eq(double a,double b){\n return (-EPS < a-b && a-b < EPS);\n}\ndouble dot(pt a,pt b){\n\treturn (conj(a)*b).x;\n}\ndouble cross(pt a,pt b){\n\treturn (conj(a)*b).y;\n}\nint ccw(pt a,pt b,pt c){\n\tb -= a; c -= a;\n\tif(cross(b,c) > EPS) return 1; // counter clockwise\n\tif(cross(b,c) < -EPS) return -1; // clockwise\n\tif(dot(b,c) < -EPS) return 2; //c-a-b\n\tif(norm(b) < norm(c)) return -2; //a-b-c\n\treturn 0; //a-c-b\n}\nbool cmp(const pt& a,const pt& b){\n\tif(-EPS < a.x-b.x && a.x-b.x < EPS) return a.y < b.y;\n\telse return a.x < b.x;\n}\nvector<pt>convex_hull(vector<pt>ps)\n{\n\tsort(ps.begin(),ps.end(),cmp);\n\tint k=0,n = ps.size();\n\tvector<pt>qs(n*2);\n\tfor(int i=0;i<n;i++)\n\t{\n\t\twhile(k>1 && ccw(qs[k-2],qs[k-1],ps[i]) == -1) k--;\n\t\tqs[k++] = ps[i];\n\t}\n\tfor(int i=n-2,t=k;i>=0;i--)\n\t{\n\t\twhile(k>t && ccw(qs[k-2],qs[k-1],ps[i]) == -1) k--;\n\t\tqs[k++] = ps[i];\n\t}\n\tqs.resize(k-1);\n\treturn qs;\n}\n//平行?\nbool is_par(pt a,pt b,pt c,pt d){\n b -= a; d -= c;\n\tif(abs(b.y*d.x - b.x*d.y) < EPS){\n\t\treturn 1;\n\t}\n\treturn 0;\n}\n\n//直線同士\npt crossPoint(pt a,pt b,pt c,pt d){\n\tdouble A = cross(b-a,d-c);\n\tdouble B = cross(b-a,b-c);\n\tif( fabs(A) < EPS && fabs(B) < EPS ) return c;\n\telse if(fabs(A) >= EPS ) return c+B/A*(d-c);\n\telse return pt(1e9,1e9);\n}\n//線分同士の交点\nbool on_segment(pair<pt,pt> a,pt p){\n return eq(abs(a.first-a.second)-abs(a.first-p)-abs(a.second-p),0);\n}\n//円 & 円 の交点\nvector<pt> CCintersect (pair<pt,double> c, pair<pt,double> d) {\n vector<pt> ret;\n double dist = abs(c.first - d.first);\n double cr = c.second;\n double dr = d.second;\n \n if (dist > cr + dr) return ret;\n if (dist < abs(cr - dr)) return ret;\n \n double s = (cr + dr + dist) / 2.;\n double area = sqrt(s * (s - cr) * (s - dr) * (s - dist));\n double h = 2 * area / dist;\n \n pt v = d.first - c.first; v /= abs(v);\n pt m = c.first + sqrt(cr * cr - h * h) * v;\n pt n = v * pt(0, 1);\n \n ret.push_back(m + n * h);\n ret.push_back(m - n * h);\n return ret;\n}\n//線分a-b とc の距離\ndouble dist_lp(pt a,pt b,pt c){\n\tif(dot(a-b,c-b) <= 0.0) return abs(c-b);\n\tif(dot(b-a,c-a) <= 0.0) return abs(c-a);\n\treturn abs(cross(b-a,c-a)) / abs(b-a);\n}\n//線分が交わる?\nbool intersect(pt a,pt b,pt c,pt d){\n\treturn (ccw(a,b,c)*ccw(a,b,d) <= 0 && ccw(c,d,a)*ccw(c,d,b) <= 0);\n}\n//線分a-b と 線分c-d の距離\ndouble min_dist_ll(pt a,pt b,pt c,pt d){\n if(intersect(a,b,c,d) == true) return 0.0;\n double ret = 1e18;\n ret = min(ret,dist_lp(a,b,c));\n ret = min(ret,dist_lp(a,b,d));\n ret = min(ret,dist_lp(c,d,a));\n ret = min(ret,dist_lp(c,d,b));\n return ret;\n}\nbool contain_point(vector<pt>ps,pt p){\n\tdouble sum = 0;\n\t//arg no sum wo keisan\n\tfor(int i=0;i<ps.size();i++){\n\t\tif(on_segment(mp(ps[i],ps[ (i+1)%ps.size() ]),p)) return 1;\n\t\tsum += arg( (ps[ (i+1)%ps.size() ]-p) / (ps[i]-p) );\n\t}\n\treturn (abs(sum) > 1);\n}\nbool LCMP(const pt& a,const pt& b){\n\tif(eq(a.x,b.x)) return a.y < b.y;\n\telse return a.x < b.x;\n}\nbool contain_segment(vector<pt>ps,pt p,pt q){\n vector<pt> qs;\n qs.pb(p); qs.pb(q);\n \tfor(int i=0;i<ps.size();i++){\n \t\t//on-segment\n \t\tif(ccw(p,q,ps[i]) == 0) qs.pb(ps[i]);\n \t}\n for(int i=0;i<ps.size();i++){\n \tpt r = crossPoint(p,q,ps[i],ps[(i+1)%ps.size()]);\n \tif(r.x > 1e8) continue;\n \tif(ccw(p,q,r) == 0) qs.pb(r);\n }\n sort(qs.begin(),qs.end(),LCMP);\n for(int i=1;i<qs.size();i++){\n pt r = (qs[i] + qs[i-1]) / (double)2.0;\n if(!contain_point(ps, r)) return 0;\n }\n return 1;\n}\n//G -> counter-clockwise\nvector<pt> convex_cut(vector<pt> G, pair<pt,pt>l){\n vector<pt> ans;\n for(int i=0;i<(int)G.size();i++){\n pt A = G[i];\n pt B = G[(i+1)%G.size()];\n if(ccw(l.fi,l.sc,A) != -1) ans.pb(A);\n if(ccw(l.fi,l.sc,A) * ccw(l.fi,l.sc,B) < 0)\n ans.pb(crossPoint(A,B,l.fi,l.sc));\n }\n return ans;\n}\nvector<pair<pt,pt> >LL;\nvoid add(pt c1,double r1,pt c2,double r2){\n\t//2-circle tangent line\n\tc2-=c1; // later add c1\n\t//(0,0) and c2\n\t//uchigawa\n\tdouble IN = norm(c2)-(r1+r2)*(r1+r2);\n\tif(IN >= 0){\n double c3_x_1 = r1 / norm(c2) * (c2.x * (r1+r2) + c2.y * sqrt(norm(c2)-(r1+r2)*(r1+r2)) );\n double c3_y_1 = r1 / norm(c2) * (c2.y * (r1+r2) - c2.x * sqrt(norm(c2)-(r1+r2)*(r1+r2)) );\n double c3_x_2 = r1 / norm(c2) * (c2.x * (r1+r2) - c2.y * sqrt(norm(c2)-(r1+r2)*(r1+r2)) );\n double c3_y_2 = r1 / norm(c2) * (c2.y * (r1+r2) + c2.x * sqrt(norm(c2)-(r1+r2)*(r1+r2)) );\n pt T2 = pt(c3_x_1,c3_y_1); T2 *= pt(0.0,1.0); LL.pb(mp(pt(c3_x_1,c3_y_1)+c1,pt(c3_x_1,c3_y_1)+c1+T2));\n T2 = pt(c3_x_2,c3_y_2); T2 *= pt(0.0,1.0); LL.pb(mp(pt(c3_x_2,c3_y_2)+c1,pt(c3_x_2,c3_y_2)+c1+T2));\n }\n //sotogawa\n IN = norm(c2)-(r1-r2)*(r1-r2);\n if(IN >= 0){\n double c3_x_1 = r1 / norm(c2) * (c2.x * (r1-r2) + c2.y * sqrt(norm(c2)-(r1-r2)*(r1-r2)) );\n double c3_y_1 = r1 / norm(c2) * (c2.y * (r1-r2) - c2.x * sqrt(norm(c2)-(r1-r2)*(r1-r2)) );\n double c3_x_2 = r1 / norm(c2) * (c2.x * (r1-r2) - c2.y * sqrt(norm(c2)-(r1-r2)*(r1-r2)) );\n double c3_y_2 = r1 / norm(c2) * (c2.y * (r1-r2) + c2.x * sqrt(norm(c2)-(r1-r2)*(r1-r2)) );\n pt T2 = pt(c3_x_1,c3_y_1); T2 *= pt(0.0,1.0); LL.pb(mp(pt(c3_x_1,c3_y_1)+c1,pt(c3_x_1,c3_y_1)+c1+T2));\n T2 = pt(c3_x_2,c3_y_2); T2 *= pt(0.0,1.0); LL.pb(mp(pt(c3_x_2,c3_y_2)+c1,pt(c3_x_2,c3_y_2)+c1+T2));\n }\n}\npt project(pt po,pair<pt,pt> L){\n\tpt base = L.sc-L.fi;\n\tdouble r = dot(po-L.fi,base) / norm(base);\n\treturn L.fi+base*r;\n}\npt reflect(pt po,pair<pt,pt> L){\n\treturn po + (project(po,L)-po)*(double)2.;\n}\nvoid mergee(vector<pair<pt,pt> > &segments) {\n for(int i = 0; i < segments.size(); ) {\n const pair<pt,pt> &s1 = segments[i];\n bool ok = true;\n for(int j = i + 1; j < segments.size(); ++j) {\n const pair<pt,pt> &s2 = segments[j];\n \n if(intersect(s1.fi,s1.sc,s2.fi,s2.sc) && abs(cross(s1.fi-s1.sc,s2.fi-s2.sc)) <= EPS) {\n pt a = pt(1e9,1e9),b = pt(-1e9,-1e9);\n if(a.x > s1.fi.x) a=s1.fi; else if(abs(a.x-s1.fi.x)<=EPS && a.y > s1.fi.y) a = s1.fi;\n if(a.x > s1.sc.x) a=s1.sc; else if(abs(a.x-s1.sc.x)<=EPS && a.y > s1.sc.y) a = s1.sc;\n if(a.x > s2.fi.x) a=s2.fi; else if(abs(a.x-s2.fi.x)<=EPS && a.y > s2.fi.y) a = s2.fi;\n if(a.x > s2.sc.x) a=s2.sc; else if(abs(a.x-s2.sc.x)<=EPS && a.y > s2.sc.y) a = s2.sc;\n if(b.x < s1.fi.x) b=s1.fi; else if(abs(b.x-s1.fi.x)<=EPS && b.y < s1.fi.y) b = s1.fi;\n if(b.x < s1.sc.x) b=s1.sc; else if(abs(b.x-s1.sc.x)<=EPS && b.y < s1.sc.y) b = s1.sc;\n if(b.x < s2.fi.x) b=s2.fi; else if(abs(b.x-s2.fi.x)<=EPS && b.y < s2.fi.y) b = s2.fi;\n if(b.x < s2.sc.x) b=s2.sc; else if(abs(b.x-s2.sc.x)<=EPS && b.y < s2.sc.y) b = s2.sc;\n segments.push_back(mp(a, b));\n segments.erase(segments.begin() + j);\n segments.erase(segments.begin() + i);\n ok = false;\n break;\n }\n }\n if(ok) ++i;\n }\n}\nbool CMPP(const pair<int,double>&a,const pair<int,double>&b){\n\treturn a.sc < b.sc;\n}\nbool cmpp(const pt&a,const pt&b){\n\tif(abs(a.x-b.x)>EPS) return a.x < b.x;\n\telse return a.y<b.y;\n}\nvoid arrangement(vector<pair<pt,pt> > &segments, vector<pt> &points) {\n mergee(segments);\n const int n = segments.size();\n points.clear();\n \n for(int i = 0; i < n; ++i) {\n const pair<pt,pt> &s1 = segments[i];\n points.pb(s1.fi);\n points.pb(s1.sc);\n \n for(int j = i + 1; j < n; ++j) {\n const pair<pt,pt> &s2 = segments[j];\n if(intersect(s1.fi,s1.sc,s2.fi,s2.sc)) points.pb(crossPoint(s1.fi,s1.sc,s2.fi,s2.sc));\n }\n }\n \n sort(points.begin(),points.end(),cmpp);\n points.erase(unique(points.begin(),points.end()), points.end());\n \n const int V = points.size();\n vector<pair<int,double> >G[V];\n for(const pair<pt,pt> &s : segments) {\n vector<pair<double, int>> vs;\n for(int i = 0; i < V; ++i) {\n if(ccw(s.fi,s.sc,points[i]) == 0) vs.pb(mp(norm(s.fi - points[i]), i));\n }\n \n sort(vs.begin(),vs.end());\n for(int i = 1; i < vs.size(); ++i) {\n const int v = vs[i].second;\n const int u = vs[i - 1].second;\n G[v].pb(mp(u, arg(points[u] - points[v])));\n G[u].pb(mp(v, arg(points[v] - points[u])));\n }\n }\n for(int i=0;i<V;i++){\n \tsort(G[i].begin(),G[i].end(),CMPP);\n }\n //return G;\n}\n//面積\ndouble area(vc<pt>ax){\n int n = ax.size();\n\t/*\n\t//only for convex polygons!!\n\t//convert to counter-clockwise\n\tfor(int i=0;i<n;i++){\n \tint f = ccw(ax[(i+0)%n], ax[(i+1)%n], ax[(i+2)%n]);\n \tif(abs(f) == 1); else continue;\n \t\n \tif(f != 1){\n \t\tfor(int i=1;i<n;i++){\n \t\t\tif(i >= n-i) break;\n \t\t\tswap(ax[i], ax[n-i]);\n \t\t}\n \t}\n \tbreak;\n\t}\n\t*/\n\tdouble ans = 0.0;\n\tfor(int i=0;i<n;i++){\n\t\tans += cross(ax[i], ax[(i+1)%n]);\n\t}\n\treturn abs(ans) / 2.0;\n}\ndouble pi = acos(-1.0L);\npair<pt, pt>cross_CL(pair<pt, double>c, pair<pt, pt>l){\n\tpt mid = project(c.a, l);\n\tif(abs(mid - c.a) < eps){\n\t\t//cross center\n\t\tdouble d = arg(l.b - l.a);\n\t\tauto vec = polar(c.b, d);\n\t\treturn mp(c.a + vec, c.a - vec);\n\t}\n\tif(abs(mid - c.a) > c.b+eps){\n\t\t//not intersect\n\t\treturn mp(pt(1e18, 1e18), pt(1e18, 1e18));\n\t}\n\tif(abs(mid - c.a) > c.b-eps){\n\t\t//tangent\n\t\treturn mp(mid, mid);\n\t}\n\tdouble len = sqrt(c.b*c.b - abs(mid - c.a)*abs(mid - c.a));\n\tauto vec = mid - c.a;\n\tvec = vec / abs(vec) * len * polar(1.0L, pi/2);\n\treturn mp(mid + vec, mid - vec);\n}\n//複数テストケースなので積み上げる変数はローカルに!\n//置けないなら必ず初期化!\nint n, m;\nvoid solve(){\n\t//最初の入力は済んでいる\n\tvc<pt>a[2];\n\trep(i, n){\n\t\tdouble ax, ay; cin >> ax >> ay;\n\t\ta[0].pb(pt(ax, ay));\n\t}\n\tcin>>m;\n\trep(i, m){\n\t\tdouble ax, ay; cin >> ax >> ay;\n\t\ta[1].pb(pt(ax, ay));\n\t}\n\t//calculate minimum length of arc of circle radius = r\n\t//s.t. contain all points of p\n\tauto calc = [&](vc<pt>p, double r) -> double{\n\t\tvc<pt>cand;\n\t\tint n = p.size();\n\t\trep(i, n) rep(j, i){\n\t\t\tif(abs(p[i] - p[j]) > r * 2.0-eps) continue;\n\t\t\tauto mid = (p[i] + p[j]) / 2.0L;\n\t\t\tdouble len = sqrt(r*r - abs(p[i]-p[j]) * abs(p[i]-p[j]) / 4.0L);\n\t\t\tauto vec = p[i]-p[j];\n\t\t\tvec = vec / abs(vec) * len * polar(1.0L, pi/2);\n\t\t\tcand.pb(mid + vec);\n\t\t\tcand.pb(mid - vec);\n\t\t}\n\t\trep(i, n) rep(j, n){\n\t\t\tauto vec = p[j] - p[(j+1)%n];\n\t\t\tvec = vec / abs(vec) * r * polar(1.0L, pi/2);\n\t\t\tcand.pb(p[i] + vec);\n\t\t\tcand.pb(p[i] - vec);\n\t\t}\n\t\tdouble ret = 1e18;\n\t\tfor(auto c:cand){\n\t\t\tbool bad = 0;\n\t\t\trep(i, n){\n\t\t\t\tif(abs(c-p[i]) > r + eps) {\n\t\t\t\t\tbad = 1;\n\t\t\t\t\tgoto fail;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfail:;\n\t\t\tif(bad) continue;\n\t\t\trep(i, n){\n\t\t\t\tauto cs = cross_CL(mp(c, r), mp(p[i], p[(i+1)%n]));\n\t\t\t\tauto p1 = c + polar(r, arg(cs.a-cs.b)+pi/2.0L);\n\t\t\t\tauto p2 = c - polar(r, arg(cs.a-cs.b)+pi/2.0L);\n\t\t\t\tif(ccw(p[i], p[(i+1)%n], p1) == 1);\n\t\t\t\telse{\n\t\t\t\t\tassert(ccw(p[i], p[(i+1)%n], p2) == 1);\n\t\t\t\t\tswap(p1, p2);\n\t\t\t\t}\n\t\t\t\tif(ccw(cs.a, cs.b, p1) != 1){\n\t\t\t\t\tassert(ccw(cs.b, cs.a, p1) == 1);\n\t\t\t\t\tswap(cs.a, cs.b);\n\t\t\t\t}\n\t\t\t\tdouble p = arg((p1-c) / (cs.b-c));\n\t\t\t\tdouble q = arg((cs.a-c) / (p1-c));\n\t\t\t\tassert(p >= 0 and q >= 0 and abs(p-q) <= eps);\n\t\t\t\tchmin(ret, r * (p + q));\n\t\t\t}\n\t\t\trep(i, n){\n\t\t\t\tif(abs(c - p[i]) <= eps){\n\t\t\t\t\tchmin(ret, r * pi);\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tauto v = (c-p[i]) * polar(1.0L, pi/2);\n\t\t\t\t\tauto cs = cross_CL(mp(c, r), mp(p[i]-v, p[i]+v));\n\t\t\t\t\tif(abs(cs.a-cs.b) < eps){\n\t\t\t\t\t\tchmin(ret, r * pi * 2);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tbool w[2] = {}, bad = 0;\n\t\t\t\t\trep(j, n){\n\t\t\t\t\t\tif(i == j) continue;\n\t\t\t\t\t\tint f = ccw(cs.a, cs.b, p[j]);\n\t\t\t\t\t\tif(abs(f) != 1) {\n\t\t\t\t\t\t\tbad = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tw[f<0] = 1;\n\t\t\t\t\t}\n\t\t\t\t\tif(w[0] and w[1]) continue;\n\t\t\t\t\tif(bad) continue;\n\t\t\t\t\t\n\t\t\t\t\tif(w[1]) swap(cs.a, cs.b);\n\t\t\t\t\tauto p1 = c + polar(r, arg(cs.a-cs.b)+pi/2.0L);\n\t\t\t\t\tauto p2 = c - polar(r, arg(cs.a-cs.b)+pi/2.0L);\n\t\t\t\t\t\n\t\t\t\t\tif(ccw(cs.a, cs.b, p1) != 1){\n\t\t\t\t\t\tassert(ccw(cs.a, cs.b, p2) == 1);\n\t\t\t\t\t\tswap(p1, p2);\n\t\t\t\t\t}\n\t\t\t\t\tdouble p = arg((p1-c) / (cs.b-c));\n\t\t\t\t\tdouble q = arg((cs.a-c) / (p1-c));\n\t\t\t\t\tassert(p >= 0 and q >= 0 and abs(p-q) <= eps);\n\t\t\t\t\tchmin(ret, r * (p + q));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t};\n\tdouble lb = 0.0, ub = 2500.0;\n\trep(_, 100){\n\t\tdouble mid = (lb + ub) / 2;\n\t\tdouble flag = 0.0;\n\t\tflag += calc(a[0], mid);\n\t\tflag += calc(a[1], mid);\n\t\t\n\t\tif(flag <= mid * pi * 2.0L) ub = mid;\n\t\telse lb = mid;\n\t}\n\to(ub * 2.0L);\n}\nsigned main(){\n\t//cin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\twhile(true){\n\t\tcin>>n;\n\t\t//終了条件\n\t\tif(n==0) return 0;\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 4460, "memory_kb": 3968, "score_of_the_acc": -0.2868, "final_rank": 8 }, { "submission_id": "aoj_1647_6024605", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 50\n\nenum Type{\n\tA,\n\tB,\n};\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tvoid outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}\n\tPoint p[2];\n};\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nint N[2];\ndouble NUM = 50000;\nLine LINE[2][SIZE];\nvector<Line> vert_line[2][SIZE];\nPolygon input[2];\nPoint point[2][SIZE][SIZE][SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n\tvector<Point> ret;\n\n\tVector pr = project(l,c.center);\n\tVector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n\tdouble base;\n\n\tif(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n\t\tbase = 0;\n\t}else{\n\t\tbase = sqrt(c.r*c.r-norm(pr-c.center));\n\t}\n\n\tret.push_back(Point(pr+e*base));\n\tret.push_back(Point(pr-e*base));\n\n\treturn ret;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\n/*\n * IN 2\n * ON 1\n * OUT 0\n *\n */\nint contains(Polygon g,Point p){\n\tint n = g.size();\n\tbool x = false;\n\tfor(int i = 0; i < n; i++){\n\t\tPoint a = g[i]-p,b = g[(i+1)%n]-p;\n\t\tif(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1;\n\t\tif(a.y > b.y)swap(a,b);\n\t\tif(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;\n\t}\n\treturn (x ? 2:0);\n}\n\n\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n\nbool isOK(double R){\n\n\tdouble MINI[2] = {BIG_NUM,BIG_NUM};\n\n\tfor(int loop = 0; loop < 2; loop++){\n\n\t\tfor(int base = 0; base < N[loop]; base++){ //50*40*2 = 4000\n\n\t\t\tCircle C;\n\t\t\tC.center = input[loop][base];\n\t\t\tC.r = R;\n\n\t\t\t//円の中心候補\n\t\t\tvector<Point> vec;\n\n\t\t\tfor(int i = 0; i < vert_line[loop][base].size(); i++){\n\n\t\t\t\tif(getDistanceSP(vert_line[loop][base][i],C.center) +EPS < R){\n\t\t\t\t\tvector<Point> work = getCrossPoints(C,vert_line[loop][base][i]);\n\t\t\t\t\tfor(int c = 0; c < work.size(); c++){\n\n\t\t\t\t\t\tvec.push_back(work[c]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(vec.size() == 0)continue;\n\n\t\t\tsort(vec.begin(),vec.end());\n\t\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\n\t\t\tfor(int k = 0; k < vec.size(); k++){\n\n\t\t\t\t//自分自身が円に収まっているか\n\t\t\t\tbool FLG = true;\n\t\t\t\tfor(int a = 0; a < N[loop]; a++){ //4000*40 = 16万\n\n\t\t\t\t\tdouble tmp_dist = calc_dist(vec[k],input[loop][a]);\n\t\t\t\t\tif(tmp_dist > R+EPS){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!FLG){\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tbool is_IN = contains(input[loop],vec[k]);\n\n\t\t\t\t//ポリゴンの辺への最短距離を求める\n\t\t\t\tdouble mini = R,max_h;\n\t\t\t\tint ind = -1;\n\n\t\t\t\tfor(int a = 0; a < N[loop]; a++){\n\n\t\t\t\t\tdouble tmp_dist = getDistanceSP(LINE[loop][a],vec[k]);\n\t\t\t\t\tif(mini > tmp_dist+EPS){\n\n\t\t\t\t\t\tmini = tmp_dist;\n\t\t\t\t\t\tind = a;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(is_IN){ //円の中心が多角形の内部にある\n\n\t\t\t\t\tmax_h = R-mini; //円の断片の最大高さ\n\n\n\t\t\t\t}else{ //円の中心が多角形の外部にある\n\n\t\t\t\t\tmax_h = R+mini; //円の断片の最大高さ\n\t\t\t\t}\n\n\t\t\t\tMINI[loop] = min(MINI[loop],2*R-max_h);\t//ポリゴンを収めるのに必要な、円の断片の最小の高さ\n\n\t\t\t\t//baseを中心に図形を回転させる\n\t\t\t\tdouble LEFT = -M_PI,RIGHT = M_PI;\n\t\t\t\tdouble mid[3];\n\n\t\t\t\tfor(int J = 0; J < 45; J++){\n\n\t\t\t\t\tmid[1] = (2.0*LEFT+RIGHT)/3.0;\n\t\t\t\t\tmid[2] = (1.0*LEFT+2.0*RIGHT)/3.0;\n\n\t\t\t\t\tdouble dist1 = 2*R,dist2 = 2*R;\n\n\t\t\t\t\tfor(int c = 0; c < 2; c++){\n\n\t\t\t\t\t\tPolygon tmp_poly;\n\t\t\t\t\t\tbool all_IN = true;\n\t\t\t\t\t\tfor(int d = 0; d < N[loop]; d++){ //4000*50*40 = 800万\n\n\t\t\t\t\t\t\tPoint tmp_p = rotate(input[loop][base],input[loop][d],mid[c+1]);\n\t\t\t\t\t\t\tdouble work_dist = calc_dist(vec[k],tmp_p);\n\t\t\t\t\t\t\tif(work_dist > R+EPS){\n\n\t\t\t\t\t\t\t\tall_IN = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\ttmp_poly.push_back(tmp_p);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(!all_IN){\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tmini = R;\n\n\t\t\t\t\t\t//回転させた図形と、円の中心との距離を見る\n\t\t\t\t\t\tfor(int a = 0; a < N[loop]; a++){\n\n\t\t\t\t\t\t\tLine calc_line = Line(tmp_poly[a],tmp_poly[(a+1)%N[loop]]);\n\t\t\t\t\t\t\tdouble work_dist = getDistanceSP(calc_line,vec[k]);\n\n\t\t\t\t\t\t\tif(mini > work_dist+EPS){\n\n\t\t\t\t\t\t\t\tmini = work_dist;\n\t\t\t\t\t\t\t\tind = a;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tdouble tmp_h,tmp_d;\n\n\t\t\t\t\t\tif(contains(tmp_poly,vec[k])){\n\n\t\t\t\t\t\t\ttmp_h = R-mini; //円の断片の最大高さ\n\t\t\t\t\t\t\ttmp_d = 2*R-tmp_h;\n\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\ttmp_h = R+mini; //円の断片の最大高さ\n\t\t\t\t\t\t\ttmp_d = 2*R-tmp_h;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(c == 0){\n\n\t\t\t\t\t\t\tdist1 = tmp_d;\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tdist2 = tmp_d;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(fabs(dist1-2*R) < EPS && fabs(dist2-2*R) < EPS){\n\n\t\t\t\t\t\tLEFT = mid[1];\n\t\t\t\t\t\tRIGHT = mid[2];\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tMINI[loop] = min(MINI[loop],min(dist1,dist2));\n\n\t\t\t\t\tif(dist1 < dist2){\n\n\t\t\t\t\t\tRIGHT = mid[2];\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tLEFT = mid[1];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn MINI[A]+MINI[B] <= 2*R+EPS; //円の断片の高さの和が直径以下ならOK(凸多角形なので、ある直線より上に全ての点を配置させることができる)\n}\n\n\nvoid func(){\n\n\tinput[A].clear();\n\tinput[B].clear();\n\n\tfor(int i = 0; i < 2; i++){\n\t\tfor(int k = 0; k < SIZE; k++){\n\n\t\t\tvert_line[i][k].clear();\n\t\t}\n\t}\n\n\tdouble x,y;\n\n\tfor(int i = 0; i < N[A]; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tinput[A].push_back(Point(x,y));\n\t}\n\n\tscanf(\"%d\",&N[B]);\n\n\tfor(int i = 0; i < N[B]; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tinput[B].push_back(Point(x,y));\n\t}\n\n\tdouble maxi = 0;\n\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int i = 0; i < N[loop]-1; i++){\n\t\t\tfor(int k = i+1; k < N[loop]; k++){\n\n\t\t\t\tdouble tmp_dist = calc_dist(input[loop][i],input[loop][k]);\n\t\t\t\tmaxi = max(maxi,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int i = 0; i < N[loop]; i++){\n\n\t\t\tPoint a = input[loop][i];\n\t\t\tPoint b = input[loop][(i+1)%N[loop]];\n\n\t\t\tLINE[loop][i] = Line(a,b);\n\t\t}\n\t}\n\n\t//各基準点ごとに垂直二等分線を求める\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int base = 0; base < N[loop]; base++){\n\t\t\tfor(int adj = 0; adj < N[loop]; adj++){\n\t\t\t\tif(adj == base)continue;\n\n\t\t\t\tPoint p = input[loop][base];\n\t\t\t\tPoint q = input[loop][adj];\n\n\t\t\t\tPoint mid_p = (p+q)/2;\n\n\t\t\t\tLine tmp_line;\n\n\t\t\t\tif(fabs(p.x-q.x) < EPS){ //垂直\n\n\t\t\t\t\t//垂直二等分線\n\t\t\t\t\ttmp_line.p[0] = Point(mid_p.x-NUM,mid_p.y);\n\t\t\t\t\ttmp_line.p[1] = Point(mid_p.x+NUM,mid_p.y);\n\n\t\t\t\t}else if(fabs(p.y-q.y) < EPS){ //水平\n\n\t\t\t\t\ttmp_line.p[0] = Point(mid_p.x,mid_p.y+NUM);\n\t\t\t\t\ttmp_line.p[1] = Point(mid_p.x,mid_p.y-NUM);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tdouble tmp_slope = calc_slope(Line(p,q));\n\n\t\t\t\t\tif(p.x > q.x){\n\n\t\t\t\t\t\tswap(p,q);\n\t\t\t\t\t}\n\n\t\t\t\t\tdouble vert_slope = -1.0/tmp_slope;\n\n\t\t\t\t\ttmp_line.p[0] = Point(mid_p.x-NUM,mid_p.y-NUM*vert_slope);\n\t\t\t\t\ttmp_line.p[1] = Point(mid_p.x+NUM,mid_p.y+NUM*vert_slope);\n\t\t\t\t}\n\n\t\t\t\tvert_line[loop][base].push_back(tmp_line);\n\t\t\t}\n\t\t}\n\t}\n\n\n\t//半径を二分探索\n\tdouble left = maxi/2,right = 2001,mid = (left+right)/2;\n\tdouble ans = 2001;\n\tdouble pre = BIG_NUM;\n\n\tfor(int loop = 0; loop < 50; loop++){\n\n\t\tif(fabs(pre-mid) < 1e-8)break;\n\t\tpre = mid;\n\n\t\tif(isOK(mid)){\n\n\t\t\tans = mid;\n\t\t\tright = mid;\n\t\t}else{\n\n\t\t\tleft = mid;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tprintf(\"%.10lf\\n\",ans*2);\n}\n\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N[A]);\n\t\tif(N[A] == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7640, "memory_kb": 7656, "score_of_the_acc": -1.9408, "final_rank": 19 }, { "submission_id": "aoj_1647_6024596", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 50\n\nenum Type{\n\tA,\n\tB,\n};\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tvoid outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}\n\tPoint p[2];\n};\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nint N[2];\ndouble NUM = 50000;\nLine LINE[2][SIZE];\nvector<Line> vert_line[2][SIZE];\nPolygon input[2];\nPoint point[2][SIZE][SIZE][SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n\tvector<Point> ret;\n\n\tVector pr = project(l,c.center);\n\tVector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n\tdouble base;\n\n\tif(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n\t\tbase = 0;\n\t}else{\n\t\tbase = sqrt(c.r*c.r-norm(pr-c.center));\n\t}\n\n\tret.push_back(Point(pr+e*base));\n\tret.push_back(Point(pr-e*base));\n\n\treturn ret;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\n/*\n * IN 2\n * ON 1\n * OUT 0\n *\n */\nint contains(Polygon g,Point p){\n\tint n = g.size();\n\tbool x = false;\n\tfor(int i = 0; i < n; i++){\n\t\tPoint a = g[i]-p,b = g[(i+1)%n]-p;\n\t\tif(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1;\n\t\tif(a.y > b.y)swap(a,b);\n\t\tif(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;\n\t}\n\treturn (x ? 2:0);\n}\n\n\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n\n\nbool DEBUG = false;\n\nbool isOK(double R){\n\n\tdouble MINI[2] = {BIG_NUM,BIG_NUM};\n\n\tif(DEBUG){\n\t\tprintf(\"\\nR:%.6lf 直径:%.6lf\\n\",R,2*R);\n\t}\n\n\tfor(int loop = 0; loop < 2; loop++){\n\n\t\tif(DEBUG){\n\n\t\t\tprintf(\"\\n\\n■■図形:%d■■\\n\",loop);\n\t\t}\n\n\t\tfor(int base = 0; base < N[loop]; base++){ //50*40*2 = 4000\n\n\t\t\tCircle C;\n\t\t\tC.center = input[loop][base];\n\t\t\tC.r = R;\n\n\t\t\t//円の中心候補\n\t\t\tvector<Point> vec;\n\n\t\t\tfor(int i = 0; i < vert_line[loop][base].size(); i++){\n\n\t\t\t\tif(getDistanceSP(vert_line[loop][base][i],C.center) +EPS < R){\n\t\t\t\t\tvector<Point> work = getCrossPoints(C,vert_line[loop][base][i]);\n\t\t\t\t\tfor(int c = 0; c < work.size(); c++){\n\n\t\t\t\t\t\tvec.push_back(work[c]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(vec.size() == 0)continue;\n\n\t\t\tsort(vec.begin(),vec.end());\n\t\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\n\t\t\tfor(int k = 0; k < vec.size(); k++){\n\n\t\t\t\t//自分自身が円に収まっているか\n\t\t\t\tbool FLG = true;\n\t\t\t\tfor(int a = 0; a < N[loop]; a++){ //4000*40 = 16万\n\n\t\t\t\t\tdouble tmp_dist = calc_dist(vec[k],input[loop][a]);\n\t\t\t\t\tif(tmp_dist > R+EPS){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!FLG){\n\t\t\t\t\t//printf(\"収まってない\\n\");\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif(DEBUG){\n\t\t\t\t\tprintf(\"\\n中心候補:\");\n\t\t\t\t\tvec[k].debug();\n\t\t\t\t\tprintf(\"基準点:\");\n\t\t\t\t\tinput[loop][base].debug();\n\t\t\t\t\tfor(int i = 0; i < N[loop]; i++){\n\t\t\t\t\t\tprintf(\"点\");\n\t\t\t\t\t\tinput[loop][i].debug();\n\t\t\t\t\t\tprintf(\"中心との距離:%6lf\\n\",calc_dist(vec[k],input[loop][i]));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tbool is_IN = contains(input[loop],vec[k]);\n\n\t\t\t\t//ポリゴンの辺への最短距離を求める\n\t\t\t\tdouble mini = R,max_h;\n\t\t\t\tint ind = -1;\n\n\t\t\t\tfor(int a = 0; a < N[loop]; a++){\n\n\t\t\t\t\tdouble tmp_dist = getDistanceSP(LINE[loop][a],vec[k]);\n\n\t\t\t\t\t/*if(DEBUG){\n\t\t\t\t\t\tprintf(\"線分\\n\");\n\t\t\t\t\t\tLINE[loop][a].outPut();\n\t\t\t\t\t\tprintf(\"tmp_dist:%.3lf\\n\",tmp_dist);\n\t\t\t\t\t}*/\n\t\t\t\t\tif(mini > tmp_dist+EPS){\n\n\t\t\t\t\t\tmini = tmp_dist;\n\t\t\t\t\t\tind = a;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(is_IN){ //円の中心が多角形の内部にある\n\n\t\t\t\t\tif(DEBUG){\n\n\t\t\t\t\t\tprintf(\"多角形の中\\n\");\n\t\t\t\t\t}\n\n\t\t\t\t\tmax_h = R-mini; //円の断片の最大高さ\n\n\n\t\t\t\t}else{ //円の中心が多角形の外部にある\n\n\t\t\t\t\tif(DEBUG){\n\n\t\t\t\t\t\tprintf(\"多角形の外\\n\");\n\t\t\t\t\t}\n\n\t\t\t\t\tmax_h = R+mini; //円の断片の最大高さ\n\t\t\t\t}\n\n\t\t\t\tMINI[loop] = min(MINI[loop],2*R-max_h);\t//ポリゴンを収めるのに必要な、円の断片の最小の高さ\n\n\t\t\t\tif(DEBUG){\n\n\t\t\t\t\tprintf(\"線分%dとの距離が%.6lf max_h:%.6lf\\n\",ind,mini,max_h);\n\t\t\t\t\tprintf(\"高さ:%.3lf\\n\",2*R-max_h);\n\t\t\t\t}\n\n\t\t\t\t//baseを中心に図形を回転させる\n\t\t\t\tdouble LEFT = -M_PI,RIGHT = M_PI;\n\t\t\t\tdouble mid[3];\n\n\t\t\t\tfor(int J = 0; J < 45; J++){\n\n\t\t\t\t\tmid[1] = (2.0*LEFT+RIGHT)/3.0;\n\t\t\t\t\tmid[2] = (1.0*LEFT+2.0*RIGHT)/3.0;\n\n\t\t\t\t\tif(DEBUG){\n\t\t\t\t\tprintf(\"\\nmid[1]:%.6lf mid[2]:%.6lf\\n\",mid[1],mid[2]);\n\t\t\t\t\t}\n\n\t\t\t\t\tdouble dist1 = 2*R,dist2 = 2*R;\n\n\t\t\t\t\tfor(int c = 0; c < 2; c++){\n\n\t\t\t\t\t\tPolygon tmp_poly;\n\t\t\t\t\t\tbool all_IN = true;\n\t\t\t\t\t\tfor(int d = 0; d < N[loop]; d++){ //4000*50*40 = 800万\n\n\t\t\t\t\t\t\tPoint tmp_p = rotate(input[loop][base],input[loop][d],mid[c+1]);\n\t\t\t\t\t\t\tdouble work_dist = calc_dist(vec[k],tmp_p);\n\t\t\t\t\t\t\tif(work_dist > R+EPS){\n\n\t\t\t\t\t\t\t\tall_IN = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\ttmp_poly.push_back(tmp_p);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(!all_IN){\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(DEBUG){\n\t\t\t\t\t\tprintf(\"■■円に収まった c:%d■■\\n\",c);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tmini = R;\n\n\t\t\t\t\t\t//回転させた図形と、円の中心との距離を見る\n\t\t\t\t\t\tfor(int a = 0; a < N[loop]; a++){\n\n\t\t\t\t\t\t\tLine calc_line = Line(tmp_poly[a],tmp_poly[(a+1)%N[loop]]);\n\t\t\t\t\t\t\tdouble work_dist = getDistanceSP(calc_line,vec[k]);\n\n\t\t\t\t\t\t\t/*if(DEBUG){\n\t\t\t\t\t\t\t\tprintf(\"線分\\n\");\n\t\t\t\t\t\t\t\tcalc_line.outPut();\n\t\t\t\t\t\t\t\tprintf(\"tmp_dist:%.3lf\\n\",work_dist);\n\t\t\t\t\t\t\t}*/\n\t\t\t\t\t\t\tif(mini > work_dist+EPS){\n\n\t\t\t\t\t\t\t\tmini = work_dist;\n\t\t\t\t\t\t\t\tind = a;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tdouble tmp_h,tmp_d;\n\n\t\t\t\t\t\tif(contains(tmp_poly,vec[k])){\n\n\t\t\t\t\t\t\ttmp_h = R-mini; //円の断片の最大高さ\n\t\t\t\t\t\t\ttmp_d = 2*R-tmp_h;\n\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\ttmp_h = R+mini; //円の断片の最大高さ\n\t\t\t\t\t\t\ttmp_d = 2*R-tmp_h;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(c == 0){\n\n\t\t\t\t\t\t\tdist1 = tmp_d;\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tdist2 = tmp_d;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(fabs(dist1-2*R) < EPS && fabs(dist2-2*R) < EPS){\n\n\t\t\t\t\t\tLEFT = mid[1];\n\t\t\t\t\t\tRIGHT = mid[2];\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(DEBUG){\n\t\t\t\t\tprintf(\"dist1:%.6lf dist2:%.6lf\\n\",dist1,dist2);\n\t\t\t\t\t}\n\n\t\t\t\t\tMINI[loop] = min(MINI[loop],min(dist1,dist2));\n\n\t\t\t\t\tif(dist1 < dist2){\n\n\t\t\t\t\t\tRIGHT = mid[2];\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tLEFT = mid[1];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(DEBUG){\n\n\t\tprintf(\"R:%.6lf 2*R:%.6lf MINI[A]:%.6lf MINIT[B]:%.6lf 2*R-(A+B):%.6lf\\n\",R,2*R,MINI[A],MINI[B],2*R-(MINI[A]+MINI[B]));\n\n\t\tif(MINI[A]+MINI[B] <= 2*R+EPS){\n\n\t\t\tprintf(\"OK\\n\");\n\t\t}else{\n\n\t\t\tprintf(\"不可\\n\");\n\t\t}\n\t}\n\n\treturn MINI[A]+MINI[B] <= 2*R+EPS;\n\n}\n\n\nvoid func(){\n\n\tinput[A].clear();\n\tinput[B].clear();\n\n\tfor(int i = 0; i < 2; i++){\n\t\tfor(int k = 0; k < SIZE; k++){\n\n\t\t\tvert_line[i][k].clear();\n\t\t}\n\t}\n\n\tdouble x,y;\n\n\tfor(int i = 0; i < N[A]; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tinput[A].push_back(Point(x,y));\n\t}\n\n\tscanf(\"%d\",&N[B]);\n\n\tfor(int i = 0; i < N[B]; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tinput[B].push_back(Point(x,y));\n\t}\n\n\tdouble maxi = 0;\n\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int i = 0; i < N[loop]-1; i++){\n\t\t\tfor(int k = i+1; k < N[loop]; k++){\n\n\t\t\t\tdouble tmp_dist = calc_dist(input[loop][i],input[loop][k]);\n\t\t\t\tmaxi = max(maxi,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int i = 0; i < N[loop]; i++){\n\n\t\t\tPoint a = input[loop][i];\n\t\t\tPoint b = input[loop][(i+1)%N[loop]];\n\n\t\t\tLINE[loop][i] = Line(a,b);\n\t\t}\n\t}\n\n\tif(DEBUG){\n\tprintf(\"maxi:%.10lf\\n\",maxi);\n\t}\n\n\t//各基準点ごとに垂直二等分線を求める\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int base = 0; base < N[loop]; base++){\n\t\t\tfor(int adj = 0; adj < N[loop]; adj++){\n\t\t\t\tif(adj == base)continue;\n\n\t\t\t\tPoint p = input[loop][base];\n\t\t\t\tPoint q = input[loop][adj];\n\n\t\t\t\tPoint mid_p = (p+q)/2;\n\n\t\t\t\tLine tmp_line;\n\n\t\t\t\tif(fabs(p.x-q.x) < EPS){ //垂直\n\n\t\t\t\t\t//垂直二等分線\n\t\t\t\t\ttmp_line.p[0] = Point(mid_p.x-NUM,mid_p.y);\n\t\t\t\t\ttmp_line.p[1] = Point(mid_p.x+NUM,mid_p.y);\n\n\t\t\t\t}else if(fabs(p.y-q.y) < EPS){ //水平\n\n\t\t\t\t\ttmp_line.p[0] = Point(mid_p.x,mid_p.y+NUM);\n\t\t\t\t\ttmp_line.p[1] = Point(mid_p.x,mid_p.y-NUM);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tdouble tmp_slope = calc_slope(Line(p,q));\n\n\t\t\t\t\tif(p.x > q.x){\n\n\t\t\t\t\t\tswap(p,q);\n\t\t\t\t\t}\n\n\t\t\t\t\tdouble vert_slope = -1.0/tmp_slope;\n\n\t\t\t\t\ttmp_line.p[0] = Point(mid_p.x-NUM,mid_p.y-NUM*vert_slope);\n\t\t\t\t\ttmp_line.p[1] = Point(mid_p.x+NUM,mid_p.y+NUM*vert_slope);\n\t\t\t\t}\n\n\t\t\t\tvert_line[loop][base].push_back(tmp_line);\n\t\t\t}\n\t\t}\n\t}\n\n\n\tif(DEBUG){\n\t\tisOK(13);\n\t\treturn;\n\t}\n\n\t//半径を二分探索\n\tdouble left = maxi/2,right = 2001,mid = (left+right)/2;\n\tdouble ans = 2001;\n\tdouble pre = BIG_NUM;\n\n\tfor(int loop = 0; loop < 50; loop++){\n\n\t\tif(fabs(pre-mid) < 1e-8)break;\n\t\tpre = mid;\n\n\t\tif(isOK(mid)){\n\n\t\t\tans = mid;\n\t\t\tright = mid;\n\t\t}else{\n\n\t\t\tleft = mid;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tprintf(\"%.10lf\\n\",ans*2);\n}\n\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N[A]);\n\t\tif(N[A] == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7660, "memory_kb": 7640, "score_of_the_acc": -1.9416, "final_rank": 20 } ]
aoj_1650_cpp
Tree Transformation Puzzle Let's get started with a puzzle! First, you are given an arithmetic expression, which solely consists of integers, operator symbols + and − , and parentheses. (4−1)−(2+3)−(5+6) is an example of such an expression. As is well known, the structure of an arithmetic expression is naturally represented as a tree . For instance, the tree in Figure C-1 represents (4−1)−(2+3)−(5+6) . Figure C-1: A tree representation of (4−1)−(2+3)−(5+6) In the tree of the figure, each box is a leaf node labeled with an integer, and each circle is a non-leaf node labeled with an operator symbol + or − . The value of a leaf node labeled with an integer is that integer. The value of a node with an operator symbol + is the sum of the values of all of its child nodes. The value of a node with an operator symbol − is the value of its leftmost child node subtracted with the values of all the other child nodes. The value of the root node calculated in this manner is equal to the value of the given expression calculated with the usual rules of arithmetic, of course. For any trees representing arithmetic expressions given for this puzzle, the root nodes have three child nodes, and all the other non-leaf nodes have one parent and two child nodes. Therefore, every non-leaf node is directly connected to exactly three nodes. Let us think of transformation of the tree structure corresponding to the given arithmetic expression by applying either of the following operations on the entire tree or any of its subtrees arbitrarily many times. Swapping two child nodes of a non-leaf node. For the leftmost (or the rightmost) child node of the root, if it is a non-leaf, making it the new root of the tree and the old root node the rightmost (or the leftmost, respectively) child node of the new root. Unless explicitly stated above, these operations change neither the parent-child relationship between two nodes nor the left-to-right order among child nodes sharing the same parent. The new tree resulted from such operations represents a possibly different arithmetic expression having a possibly different value. The goal of this puzzle is to find the maximum possible value of such an expression through the operations described above applied arbitrarily many times. For example, by swapping the two child nodes of the leftmost non-leaf node in Figure C-1, which has an operator symbol − , you will have the tree in Figure C-2. Figure C-2: The result of swapping the two leftmost leaf nodes And then by swapping the leftmost and the rightmost child nodes of the root in Figure C-2, you will have the tree in Figure C-3. The value of the original arithmetic expression (4−1)−(2+3)−(5+6) is −13, and those of the expressions represented by the two trees in Figures C-2 and C-3 are −19 and 9, respectively. Figure C-3: The result of swapping the leftmost and the rightmost nodes of the root In addition, by making the leftmost child node of the root in Figure C-3 the new root, and the origin ...(truncated)
[ { "submission_id": "aoj_1650_10845127", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n//https://onlinejudge.u-aizu.ac.jp/services/ice/?problemId=1650\n//模擬解答を参考\n\nconst int INF = 1e9; //非常に大きな値\n\nstruct Node {\n char op; // '+', '-', or '@' (葉ノード)\n int val; // if op == '@'\n vector<int> children;\n};\n\nconst int MAX_NODE = 500005;\nconst int MAX_DEPTH = 105;\n\nchar expr[MAX_NODE]; // expression 入力された式を格納する文字列(例:\"(+ 1 (- 2 3))\")\nNode tree[MAX_NODE]; // 構文木\nint dp_max[MAX_NODE], dp_min[MAX_NODE]; // 各ノードにおける部分木の最大値・最小値を表す\nint ans_max[MAX_NODE], ans_min[MAX_NODE]; // 各ノードを「根」に持ってきたときの、全体の最大値・最小値\nint pre_max[MAX_NODE], pre_min[MAX_NODE]; // 親ノード側の情報を使って、自分を根にしたときに必要な補助データ\nvector<int> op_pos_by_depth[MAX_DEPTH]; // 各深さごとに、'+', '-' の演算子が出現する位置\nint node_cnt = 0; // ノード数を管理するカウンター\n\n// 葉ノード\nint make_leaf(int val) {\n ++node_cnt;\n tree[node_cnt].op = '@';\n tree[node_cnt].val = val;\n return node_cnt;\n}\n\n// 构造表达式\nint parse_expr(int l, int r, int depth);\n// l〜rの部分式を、優先順位が低い演算子(+や-)に基づいて解析する関数\n// depth:現在の括弧の深さ(V[depth] を使うために渡す)\n// 戻り値:tree[]配列に構築されたノードのインデックス\nint parse_factor(int l, int r, int depth);\n// 数字や括弧で囲まれた小さい式を処理する関数\n// 基本的に「葉ノード」になる数字か、もう一つの `parse_expr` を呼ぶ\n// 戻り値:tree[]配列に構築されたノードのインデックス\n\nint parse_expr(int l, int r, int depth) {\n vector<int> ops; //depth +-の位置\n auto& v = op_pos_by_depth[depth];\n int L = lower_bound(v.begin(), v.end(), l) - v.begin();\n int R = upper_bound(v.begin(), v.end(), r) - v.begin() - 1;\n\n for (int i = L; i <= R; i++) ops.push_back(v[i]);\n\n if (ops.empty()) return parse_factor(l, r, depth);\n\n if (depth == 0) {\n tree[0].op = expr[ops[0]];\n tree[0].children.push_back(parse_factor(l, ops[0] - 1, depth));\n tree[0].children.push_back(parse_factor(ops[0] + 1, ops[1] - 1, depth));\n tree[0].children.push_back(parse_factor(ops[1] + 1, r, depth));\n return 0;\n } else {\n ++node_cnt;\n int cur = node_cnt;\n tree[cur].op = expr[ops[0]];\n tree[cur].children.push_back(parse_factor(l, ops[0] - 1, depth));\n tree[cur].children.push_back(parse_factor(ops[0] + 1, r, depth));\n return cur;\n }\n}\n\nint parse_factor(int l, int r, int depth) {\n if (isdigit(expr[l])) return make_leaf(expr[l] - '0');\n return parse_expr(l + 1, r - 1, depth + 1);\n}\n\n\nvoid dfs(int node) {\n if (tree[node].op == '@') {\n dp_min[node] = dp_max[node] = tree[node].val;\n return;\n }\n\n for (int child : tree[node].children) dfs(child);\n\n if (tree[node].op == '+') {\n dp_min[node] = dp_max[node] = 0;\n for (int child : tree[node].children) {\n dp_min[node] += dp_min[child];\n dp_max[node] += dp_max[child];\n }\n } else { // '-'\n dp_max[node] = -INF;\n dp_min[node] = INF;\n\n for (int i = 0; i < tree[node].children.size(); i++) {\n int left = tree[node].children[i];\n int max_val = dp_max[left], min_val = dp_min[left];\n\n for (int j = 0; j < tree[node].children.size(); j++) {\n if (i == j) continue;\n int right = tree[node].children[j];\n max_val -= dp_min[right];\n min_val -= dp_max[right];\n }\n\n dp_max[node] = max(dp_max[node], max_val);\n dp_min[node] = min(dp_min[node], min_val);\n }\n }\n}\n\n\nvoid dfs2(int node, int parent) {\n if (node == 0) {\n ans_max[node] = dp_max[node];\n ans_min[node] = dp_min[node];\n } else {\n if (tree[node].op == '@') return;\n\n if (tree[parent].op == '+') {\n pre_max[node] = ans_max[parent] - dp_max[node];\n pre_min[node] = ans_min[parent] - dp_min[node];\n } else {\n if (parent == 0) {\n vector<int> rest;\n for (int c : tree[parent].children) if (c != node) rest.push_back(c);\n\n pre_max[node] = max(\n dp_max[rest[0]] - dp_min[rest[1]],\n dp_max[rest[1]] - dp_min[rest[0]]\n );\n pre_min[node] = min(\n dp_min[rest[0]] - dp_max[rest[1]],\n dp_min[rest[1]] - dp_max[rest[0]]\n );\n } else {\n int sibling; //兄弟\n for (int c : tree[parent].children) {\n if (c != node) {\n sibling = c;\n break;\n }\n }\n\n pre_max[node] = max(\n dp_max[sibling] - pre_min[parent],\n pre_max[parent] - dp_min[sibling]\n );\n pre_min[node] = min(\n dp_min[sibling] - pre_max[parent],\n pre_min[parent] - dp_max[sibling]\n );\n }\n }\n\n if (tree[node].op == '+') {\n ans_max[node] = dp_max[node] + pre_max[node];\n ans_min[node] = dp_min[node] + pre_min[node];\n } else {\n int a = tree[node].children[0];\n int b = tree[node].children[1];\n\n ans_max[node] = max({\n dp_max[a] - (dp_min[b] + pre_min[node]),\n dp_max[b] - (dp_min[a] + pre_min[node]),\n pre_max[node] - (dp_min[a] + dp_min[b])\n });\n\n ans_min[node] = min({\n dp_min[a] - (dp_max[b] + pre_max[node]),\n dp_min[b] - (dp_max[a] + pre_max[node]),\n pre_min[node] - (dp_max[a] + dp_max[b])\n });\n }\n }\n\n for (int child : tree[node].children) dfs2(child, node);\n}\n\nvoid solve() {\n //初期化\n for (int i = 0; i < MAX_DEPTH; i++) op_pos_by_depth[i].clear();\n for (int i = 0; i < MAX_NODE; i++) tree[i].children.clear();\n\n int len = strlen(expr);\n int depth = 0;\n for (int i = 0; i < len; i++) {\n if (expr[i] == '(') depth++;\n else if (expr[i] == ')') depth--;\n else if (expr[i] == '+' || expr[i] == '-') op_pos_by_depth[depth].push_back(i);\n }\n\n node_cnt = 0;\n parse_expr(0, len - 1, 0);\n\n for (int i = 0; i <= node_cnt; i++) {\n dp_min[i] = ans_min[i] = INF;\n dp_max[i] = ans_max[i] = -INF;\n }\n\n dfs(0);\n dfs2(0, -1);\n\n int res = -INF;\n for (int i = 0; i <= node_cnt; i++) {\n if (tree[i].op != '@') res = max(res, ans_max[i]);\n }\n\n printf(\"%d\\n\", res);\n}\n\nint main() {\n while (true) {\n scanf(\"%s\", expr);\n if (strcmp(expr, \"-1\") == 0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 33760, "score_of_the_acc": -0.2812, "final_rank": 11 }, { "submission_id": "aoj_1650_10684034", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1001001001;\n#ifdef _DEBUG\n#define show(x) \\\n\tcerr << #x << \" : \"; \\\n\tshowVal(x)\ntemplate <typename T>\nvoid showVal(const T &a) {\n\tcerr << a << endl;\n}\ntemplate <typename T, typename U>\nvoid showVal(const pair<T, U> &a) {\n\tcerr << a.first << \" \" << a.second << endl;\n}\ntemplate <typename T>\nvoid showVal(const vector<T> &a) {\n\tfor (const T &v : a) cerr << v << \" \";\n\tcerr << endl;\n}\ntemplate <typename T, typename U>\nvoid showVal(const vector<pair<T, U>> &a) {\n\tcerr << endl;\n\tfor (const pair<T, U> &v : a) cerr << v.first << \" \" << v.second << endl;\n}\ntemplate <typename T, typename U>\nvoid showVal(const map<T, U> &a) {\n\tcerr << endl;\n\tfor (const auto &v : a) cerr << \"[\" << v.first << \"] \" << v.second << endl;\n}\ntemplate <typename T>\nvoid showVal(const vector<vector<T>> &a) {\n\tcerr << endl;\n\tfor (const vector<T> &v : a) showVal(v);\n}\n#else\n#define show(x)\n#endif\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n\tif (b < a) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\nint main() {\n\twhile (1) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tif (s == \"-1\") break;\n\t\tint sz = s.size();\n\t\tvector<vector<int>> g(sz);\n\t\t{\n\t\t\tstack<pair<int, int>> st;\n\t\t\tvector<int> threei;\n\t\t\tmap<pair<int, int>, int> lrtom;\n\t\t\tfor (int i = 0; i < sz; i++) {\n\t\t\t\tif (s[i] == '(') {\n\t\t\t\t\tst.push({i, -1});\n\t\t\t\t} else if (s[i] == ')') {\n\t\t\t\t\tauto [l, m] = st.top();\n\t\t\t\t\tlrtom[{l, i}] = m;\n\t\t\t\t\tst.pop();\n\t\t\t\t} else if (s[i] == '-' || s[i] == '+') {\n\t\t\t\t\tif (st.size() == 0) {\n\t\t\t\t\t\tthreei.push_back(i);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tst.top().second = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(threei.size() == 2);\n\t\t\tauto dfs = [&](auto self, int l, int r, int par) {\n\t\t\t\tif (l == r) {\n\t\t\t\t\tg[par].push_back(l);\n\t\t\t\t\tg[l].push_back(par);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tint m = lrtom[{l, r}];\n\t\t\t\tg[m].push_back(par);\n\t\t\t\tg[par].push_back(m);\n\t\t\t\tself(self, l + 1, m - 1, m);\n\t\t\t\tself(self, m + 1, r - 1, m);\n\t\t\t\treturn;\n\t\t\t};\n\t\t\tint root = threei[0];\n\t\t\tdfs(dfs, 0, threei[0] - 1, root);\n\t\t\tdfs(dfs, threei[0] + 1, threei[1] - 1, root);\n\t\t\tdfs(dfs, threei[1] + 1, sz - 1, root);\n\t\t}\n\t\t// show(g);\n\n\t\tauto combminmax = [&](const vector<pair<int, int>> &vals, char op) {\n\t\t\t// show(vals);\n\t\t\t// show(op);\n\t\t\tint n = vals.size();\n\t\t\tvector<int> res;\n\t\t\tfor (int bit = 0; bit < (1 << n); bit++) {\n\t\t\t\tint tot = (bit & 1) ? vals[0].first : vals[0].second;\n\t\t\t\tfor (int i = 1; i < n; i++) {\n\t\t\t\t\tint tv = ((bit >> i) & 1) ? vals[i].first : vals[i].second;\n\t\t\t\t\tif (op == '+')\n\t\t\t\t\t\ttot += tv;\n\t\t\t\t\telse\n\t\t\t\t\t\ttot -= tv;\n\t\t\t\t}\n\t\t\t\tres.push_back(tot);\n\t\t\t}\n\t\t\tint mins = *min_element(res.begin(), res.end());\n\t\t\tint maxs = *max_element(res.begin(), res.end());\n\t\t\t// show(mins);\n\t\t\t// show(maxs);\n\t\t\treturn pair<int, int>{mins, maxs};\n\t\t};\n\n\t\tmap<pair<int, int>, pair<int, int>> memo;\n\t\tauto f = [&](auto self, int i, int j) -> pair<int, int> {\n\t\t\tif (memo.count({i, j})) return memo[{i, j}];\n\t\t\t// -> i ? -> j\n\t\t\tvector<pair<int, int>> vals;\n\t\t\tfor (auto v : g[i]) {\n\t\t\t\tif (v == j) continue;\n\t\t\t\tvals.push_back(self(self, v, i));\n\t\t\t}\n\t\t\tpair<int, int> res;\n\t\t\tif (vals.size() == 0) {\n\t\t\t\tres.first = s[i] - '0';\n\t\t\t\tres.second = s[i] - '0';\n\t\t\t} else {\n\t\t\t\tassert(vals.size() == 2);\n\t\t\t\tshow(i);\n\t\t\t\tshow(j);\n\t\t\t\tauto [mins1, maxs1] = combminmax(vals, s[i]);\n\t\t\t\treverse(vals.begin(), vals.end());\n\t\t\t\tauto [mins2, maxs2] = combminmax(vals, s[i]);\n\t\t\t\tres.first = min(mins1, mins2);\n\t\t\t\tres.second = max(maxs1, maxs2);\n\t\t\t}\n\t\t\tmemo[{i, j}] = res;\n\t\t\treturn res;\n\t\t};\n\t\tint ans = -INF;\n\t\tfor (int i = 0; i < sz; i++) {\n\t\t\tif (g[i].size() == 3) {\n\t\t\t\tshow(i);\n\t\t\t\tvector<int> sg(3);\n\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\tsg[j] = j;\n\t\t\t\t}\n\t\t\t\tdo {\n\t\t\t\t\tvector<pair<int, int>> vals;\n\t\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\t\tvals.push_back(f(f, g[i][sg[j]], i));\n\t\t\t\t\t}\n\t\t\t\t\tchmax(ans, combminmax(vals, s[i]).second);\n\t\t\t\t} while (next_permutation(sg.begin(), sg.end()));\n\t\t\t\tshow(ans);\n\t\t\t}\n\t\t}\n\t\t// for (auto [p, q] : memo) {\n\t\t// \tshow(p);\n\t\t// \tshow(q);\n\t\t// }\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 38120, "score_of_the_acc": -1.2827, "final_rank": 20 }, { "submission_id": "aoj_1650_10671464", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nstruct Node{\n vector<Node*>ns;\n char c;\n int idx;\n};\n\nbool solve() {\n string s;\n Node root;\n cin>>s;\n if (s==\"-1\") return 0;\n //構文解析\n vector<char>val;\n stack<Node>st;\n vector<vi>G;\n auto add_edge=[&](Node u,Node v)->void{\n while(sz(G)<=max(u.idx,v.idx))G.push_back({});\n G[u.idx].push_back(v.idx);\n G[v.idx].push_back(u.idx);\n };\n int n=0;\n rep(i,0,sz(s)){\n if(s[i]=='(')continue;\n if(s[i]==')'){\n assert(sz(st)>=3);\n Node n1=st.top();st.pop();\n Node n2=st.top();st.pop();\n Node n3=st.top();st.pop();\n add_edge(n1,n2);\n add_edge(n2,n3);\n st.push(n2);\n continue;\n }\n Node node;\n node.c=s[i];\n node.idx=n++;\n while(sz(val)<=n)val.push_back({});\n val[node.idx]=node.c;\n st.push(node);\n }\n assert(sz(st)==5);\n {\n Node n1=st.top();st.pop();\n Node n2=st.top();st.pop();\n Node n3=st.top();st.pop();\n Node n4=st.top();st.pop();\n Node n5=st.top();st.pop();\n root.c=n2.c;\n root.idx=n++;\n while(sz(val)<=n)val.push_back({});\n val[root.idx]=root.c;\n // cout<<root.c<<endl;\n add_edge(root,n1);\n add_edge(root,n3);\n add_edge(root,n5);\n }\n assert(sz(st)==0);\n\n \n //全方位木dp 最大と最小をもっておく\n vector<pii>dp(n);//max,min\n pii ini={-1e7,1e7};\n //木dp\n auto dfs1=[&](auto dfs1,int v,int p)->pii{\n pii ret=ini;\n vector<pii> chd_val;\n // cout<<v<<\"totunyuu\"<<endl;\n for(auto to:G[v]){\n if(to==p)continue;\n chd_val.push_back(dfs1(dfs1,to,v));\n }\n if(val[v]=='+'){\n ret=pii(0,0);\n for(auto [mx,mn]:chd_val)ret.first+=mx,ret.second+=mn;\n }else if(val[v]=='-'){\n //4~8通りしかない\n rep(i,0,sz(chd_val)){\n //iだけmin,他max\n int mnx=chd_val[i].second;\n //iだけmax,他min\n int mxn=chd_val[i].first;\n //iのところだけ+\n rep(j,0,sz(chd_val)){\n if(j==i)continue;\n mnx-=chd_val[j].first;\n mxn-=chd_val[j].second;\n }\n ret.first=max(ret.first,mxn);\n ret.second=min(ret.second,mnx);\n }\n }\n else ret=pii(val[v]-'0',val[v]-'0');\n dp[v]=ret;\n // cout<<\"頂点\"<<v<<\" \"<<val[v]<<endl;\n // cout<<dp[v].first<<\" \"<<dp[v].second<<endl;\n// cout<<v<<\" \"<<val[v]<<endl; cout<<dp[v].first<<\" \"<<dp[v].second<<endl;\n return ret;\n };\n dfs1(dfs1,root.idx,-1);\n // cout<<dp[0].first<<\" \"<<dp[0].second<<endl;\n // cout<<endl;\n int ans=dp[root.idx].first;\n\n auto dfs2=[&](auto dfs2,int v,int p,pii p_val)->pii{\n pii ret=ini;\n vector<pii> chd_val;\n for(auto to:G[v]){\n if(to==p)chd_val.push_back(p_val);\n else chd_val.push_back(dp[to]);\n }\n if(val[v]=='+'){\n ret=pii(0,0);\n for(auto [mx,mn]:chd_val)ret.first+=mx,ret.second+=mn;\n }else if(val[v]=='-'){\n //4~8通りしかない\n rep(i,0,sz(chd_val)){\n //iだけmin,他max\n int mnx=chd_val[i].second;\n //iだけmax,他min\n int mxn=chd_val[i].first;\n //iのところだけ+\n rep(j,0,sz(chd_val)){\n if(j==i)continue;\n mnx-=chd_val[j].first;\n mxn-=chd_val[j].second;\n }\n ret.first=max(ret.first,mxn);\n ret.second=min(ret.second,mnx);\n }\n }\n else ret=pii(val[v]-'0',val[v]-'0');\n if(!isdigit(val[v]))ans=max(ans,ret.first);\n\n if(v==root.idx)assert(dp[v]==ret);\n // dp[v]=ret;//kokonohamotomatta\n if(isdigit(val[v]))return ret;\n\n rep(rm,0,sz(G[v])){\n // cout<<v<<\"突入\"<<endl;\n chd_val.clear();\n ret=ini;\n for(auto to:G[v]){\n if(to==p)chd_val.push_back(p_val);\n else chd_val.push_back(dp[to]);\n }\n chd_val.erase(chd_val.begin()+rm);\n assert(sz(chd_val)==2);\n // for(auto [mx,mn]:chd_val)cout<<\"(\"<<mx<<\",\"<<mn<<\")\";\n // cout<<endl;\n if(val[v]=='+'){\n ret=pii(0,0);\n for(auto [mx,mn]:chd_val)ret.first+=mx,ret.second+=mn;\n }else if(val[v]=='-'){\n //4~8通りしかない\n rep(i,0,sz(chd_val)){\n //iだけmin,他max\n int mnx=chd_val[i].second;\n //iだけmax,他min\n int mxn=chd_val[i].first;\n //iのところだけ+\n rep(j,0,sz(chd_val)){\n if(j==i)continue;\n mnx-=chd_val[j].first;\n mxn-=chd_val[j].second;\n }\n ret.first=max(ret.first,mxn);\n ret.second=min(ret.second,mnx);\n }\n }\n\n if(G[v][rm]!=p)dfs2(dfs2,G[v][rm],v,ret);\n } \n\n // cout<<\"頂点\"<<v<<\" \"<<val[v]<<endl;\n // cout<<dp[v].first<<\" \"<<dp[v].second<<endl;\n\n return dp[v];\n };\n dfs2(dfs2,root.idx,-1,ini);\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 17560, "score_of_the_acc": -0.0351, "final_rank": 1 }, { "submission_id": "aoj_1650_10658829", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct node {\n char c;\n vector<bool> vtd = vector<bool>(3,false);\n vector<int> mx = vector<int>(3,0);\n vector<int> mn = vector<int>(3,0);\n int num;\n vector<int> to;\n};\n\nvoid solve(string s){\n vector<node> g(4);\n stack<int> st;\n st.push(3);st.push(0);st.push(2);st.push(0);st.push(1);\n \n g[0].to.push_back(1);g[0].to.push_back(2);g[0].to.push_back(3);\n g[1].to.push_back(0);g[2].to.push_back(0);g[3].to.push_back(0);\n\n for (auto p:s){\n if ('0' <= p && p <= '9'){\n int v = st.top();\n st.pop();\n g[v].num = p - '0';\n g[v].c = '.';\n }\n else if (p == '-' || p == '+'){\n int v = st.top();\n st.pop();\n g[v].c = p;\n }\n else if (p == '(') {\n int v = st.top();\n st.pop();\n node nd;\n g.push_back(nd);\n g.push_back(nd);\n st.push(g.size()-1);\n st.push(v);\n st.push(g.size()-2);\n g[v].to.push_back(g.size()-1);\n g[v].to.push_back(g.size()-2);\n g[g.size()-1].to.push_back(v);\n g[g.size()-2].to.push_back(v);\n }\n }\n\n\n int n = g.size();\n int ans = -1e9;\n vector<vector<int>> mx(n, vector<int>(3));\n vector<vector<int>> mn(n, vector<int>(3));\n vector<vector<bool>> vtd(n, vector<bool>(3,false));\n\n /*for (int i = 0; i < n; i++){\n cout << i << ' ' << g[i].c << ' ';\n if (g[i].c == '.')cout << g[i].num;\n cout << endl;\n for (auto a:g[i].to)cout << a << ' ';\n cout << endl;\n }*/\n\n\n auto f = [&](auto &f, int now, int from)->void{\n if (g[now].c == '.'){\n for (int i = 0; i < 3; i++){\n mx[now][i] = g[now].num;\n mn[now][i] = g[now].num;\n vtd[now][i] = true;\n }\n return;\n }\n\n int idx;\n int v1,v2;\n if (g[now].to[0] == from){\n v1 = g[now].to[1];\n v2 = g[now].to[2];\n idx = 0;\n }\n else if (g[now].to[1] == from){\n v1 = g[now].to[0];\n v2 = g[now].to[2];\n idx = 1;\n }\n else {\n v1 = g[now].to[0];\n v2 = g[now].to[1];\n idx = 2;\n }\n\n if (vtd[now][idx])return;\n\n\n\n\n int idx1,idx2;\n for (int i = 0; i < 3; i++){\n if (g[v1].to[i] == now)idx1 = i;\n if (g[v2].to[i] == now)idx2 = i;\n }\n \n f(f,v1,now);\n f(f,v2,now);\n\n\n int mx1,mx2,mn1,mn2;\n mx1 = mx[v1][idx1];\n mx2 = mx[v2][idx2];\n mn1 = mn[v1][idx1];\n mn2 = mn[v2][idx2];\n\n if (g[now].c == '+'){\n mx[now][idx] = mx1 + mx2;\n mn[now][idx] = mn1 + mn2;\n }\n else {\n mx[now][idx] = max(mx1-mn2, mx2-mn1);\n mn[now][idx] = min(mn1-mx2, mn2-mx1);\n }\n vtd[now][idx] = true;\n };\n\n for (int i = 0; i < n; i++){\n if (g[i].c == '.')continue;\n\n int i1,i2,i3,j1,j2,j3;\n i1 = g[i].to[0];\n f(f, i1, i);\n for (int j = 0; j < 3; j++){\n if (g[i1].to[j] == i)j1 = j;\n }\n\n i2 = g[i].to[1];\n f(f, i2, i);\n for (int j = 0; j < 3; j++){\n if (g[i2].to[j] == i)j2 = j;\n }\n\n i3 = g[i].to[2];\n f(f, i3, i);\n for (int j = 0; j < 3; j++){\n if (g[i3].to[j] == i)j3 = j;\n }\n\n if (g[i].c == '+')ans = max(ans, mx[i1][j1] + mx[i2][j2] + mx[i3][j3]);\n else {\n ans = max(ans, mx[i1][j1]-mn[i2][j2]-mn[i3][j3]);\n ans = max(ans, mx[i2][j2]-mn[i1][j1]-mn[i3][j3]);\n ans = max(ans, mx[i3][j3]-mn[i2][j2]-mn[i1][j1]);\n\n //cout << mx[i1][j1]-mn[i2][j2]-mn[i3][j3] << ' ' << mx[i2][j2]-mn[i1][j1]-mn[i3][j3] << ' ' << mx[i3][j3]-mn[i2][j2]-mn[i1][j1] << endl;\n }\n //cout << g[i].c << ' ' << i << endl;\n }\n cout << ans << endl;\n}\n\nint main(){\n string s;cin >> s;\n while(s != \"-1\"){\n solve(s);\n cin >> s;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 91524, "score_of_the_acc": -1.2286, "final_rank": 19 }, { "submission_id": "aoj_1650_10643390", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define vi vector<int>\n#define vl vector<ll>\n#define ov4(a, b, c, d, name, ...) name\n#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for(ll i = (a)-1; i >= (b); i--)\n#define fore(e, v) for(auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T, typename S> bool chmin(T& a, const S& b) { return a > b ? a = b, 1 : 0; }\ntemplate<typename T, typename S> bool chmax(T& a, const S& b) { return a < b ? a = b, 1 : 0; }\n \nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n \n#define i128 __int128_t\n \nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\nvoid debug(auto ...vs) {\n ((cerr << vs << \" \"), ...) << endl;\n}\n\n// digit = 1 | 2 | ...\n// root = f op f op f\n// f = (f op f) | digit\n\nvoid solve(string s) {\n int m = sz(s);\n \n int n = 0;\n vector<pii> E;\n vector<int> op;\n auto digit = [&](int &i) -> int {\n int x = n++;\n op.emplace_back(-1);\n // debug(x, sz(op));\n op[x] = s[i] - '0';\n i++;\n return x;\n };\n auto f = [&](auto self, int &i) -> int {\n if (s[i] == '(') {\n int x = n++;\n op.emplace_back(-1);\n i++;\n int v = self(self, i);\n E.emplace_back(x, v);\n op[x] = (s[i] == '+');\n i++;// op\n v = self(self, i);\n E.emplace_back(x, v);\n i++;// )\n return x;\n }\n else {\n return digit(i);\n }\n };\n\n auto root = [&](int &i) -> int {\n int x = n++;\n op.emplace_back(-1);\n \n int v = f(f, i);\n E.emplace_back(x, v);\n // debug(x, v);\n // L\n op[x] = (s[i] == '+');\n i++; // op\n v = f(f, i);\n E.emplace_back(x, v);\n // debug(x, v);\n i++; // op\n v = f(f, i);\n E.emplace_back(x, v);\n // debug(x, v);\n\n return x;\n };\n \n int pos = 0;\n root(pos);\n // cout << pos << endl;\n // cout << n << endl;\n vector<vi> g(n);\n for(auto [p, q] : E ) {\n g[p].emplace_back(q);\n g[q].emplace_back(p);\n // debug(p, q);\n }\n\n // rep(i, n) {\n // debug(sz(g[i]), op[i]);\n // }\n using S = array<int, 2>;\n map<pii, S> mp;\n auto dfs2 = [&](auto self, int v, int p) -> S {\n if (sz(g[v]) == 1) return {op[v], op[v]};\n if (mp.contains(pii{v, p})) return mp[pii{v, p}];\n S ans = {-INF, INF};\n vector<S> res;\n fore(u, g[v]) {\n if (u == p) continue;\n S a;\n a = self(self, u, v);\n res.emplace_back(a);\n }\n\n if (op[v]) {\n return mp[pii{v, p}] = {res[0][0] + res[1][0], res[0][1] + res[1][1]};\n }\n else {\n return mp[pii{v, p}] = {max(res[0][0] - res[1][1], res[1][0] - res[0][1]) , min(res[0][1] - res[1][0], res[1][1] - res[0][0])};\n }\n };\n \n auto dfs1 = [&](int v) -> int {\n vector<S> res;\n fore(u, g[v]) {\n res.emplace_back(dfs2(dfs2, u, v));\n }\n int ans = -INF;\n if (op[v]) {\n chmax(ans, res[0][0] + res[1][0] + res[2][0]);\n }\n else {\n chmax(ans, res[0][0] - res[1][1] - res[2][1]);\n chmax(ans, res[1][0] - res[2][1] - res[0][1]);\n chmax(ans, res[2][0] - res[0][1] - res[1][1]);\n }\n return ans;\n };\n\n // rep(i, n) {\n // if (sz(g[i]) == 1) {\n\n // }\n // }\n int ans = -INF;\n rep(i, n) {\n if (sz(g[i]) == 3) {\n chmax(ans, dfs1(i));\n }\n }\n cout << ans << endl;\n}\n\n\nint main() {\n\n // int T;cin >> T;\n\tint T = 1;\n while(true) {\n string s;cin >> s;\n if (s == \"-1\") break;\n solve(s);\n }\n \n}", "accuracy": 1, "time_ms": 90, "memory_kb": 29920, "score_of_the_acc": -0.3725, "final_rank": 13 }, { "submission_id": "aoj_1650_10643370", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define ov4(a, b, c, d, name, ...) name\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)\n#define fore(e, v) for (auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate <typename T, typename S>\nbool chmin(T& a, const S& b) {\n return a > b ? a = b, 1 : 0;\n}\ntemplate <typename T, typename S>\nbool chmax(T& a, const S& b) {\n return a < b ? a = b, 1 : 0;\n}\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n#define i128 __int128_t\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\nvoid solve(string);\nint main(){\n while(1){\n string S;\n cin>>S;\n if(S==\"-1\")break;\n solve(S);\n }\n}\n\nvoid solve(string VS){\n using vpii = vector<pii>;\n vector<vector<pii>> G;\n\n vector<char> GC;\n G.reserve(sz(VS));\n GC.reserve(sz(VS));\n\n // lから1フレーズ (vertex,right)\n auto lex=[&G,&GC](auto f,int l,const string& s,bool top=false) -> pair<int,int> {\n if(top){\n if(s[l]=='('){\n auto [v,r]=f(f,l+1,s);\n return pii{v,-1};\n }else{\n int v=sz(G);\n G.push_back(vpii());\n GC.push_back(s[l]);\n return pii{v,-1};\n }\n }\n int v1=-1,v2=-1;\n if(s[l]=='('){\n auto [v,r]=f(f,l+1,s);\n v1=v;\n l=r +1; // ')'\n }else{\n v1=sz(G);\n G.push_back(vpii());\n GC.push_back(s[l]);\n }\n l++;\n int vp=sz(GC);\n G.push_back(vpii());\n GC.push_back(s[l]);\n l++;\n if(s[l]=='('){\n auto [v,r]=f(f,l+1,s);\n v2=v;\n l=r+1; // ')'\n }else{\n v2=sz(G);\n G.push_back(vpii());\n GC.push_back(s[l]);\n }\n G[v1].push_back(pii{vp,sz(G[vp])});\n G[vp].push_back(pii{v1,sz(G[v1])-1});\n G[v2].push_back(pii{vp,sz(G[vp])});\n G[vp].push_back(pii{v2,sz(G[v2])-1});\n return pii{vp,l};\n };\n\n // 1\n string T1=\"\";\n int dep=0,l=0;\n for(;l<sz(VS);l++){\n if(VS[l]=='(')dep++;\n if(VS[l]==')')dep--;\n T1+=VS[l];\n if(dep==0)break;\n }\n auto [v1,uuuuuuu]=lex(lex,0,T1,true);\n l++;\n char cp=VS[l];\n l++;\n // 2\n string T2=\"\";\n for(;l<sz(VS);l++){\n if(VS[l]=='(')dep++;\n if(VS[l]==')')dep--;\n T2+=VS[l];\n if(dep==0)break;\n }\n auto [v2,uuuuuuuu]=lex(lex,0,T2,true);\n l+=2;\n // 3\n string T3=\"\";\n for(;l<sz(VS);l++){\n if(VS[l]=='(')dep++;\n if(VS[l]==')')dep--;\n T3+=VS[l];\n if(dep==0)break;\n }\n auto [v3,uuuuuu]=lex(lex,0,T3,true);\n int vp=sz(G);\n\n // cerr<<T1<<' '<<T2<<' '<<T3<<' '<<cp<<endl;\n G.push_back(vpii());\n GC.push_back(cp);\n G[vp].push_back(pii{v1,sz(G[v1])});\n G[v1].push_back(pii{vp,sz(G[vp])-1});\n G[vp].push_back(pii{v2,sz(G[v2])});\n G[v2].push_back(pii{vp,sz(G[vp])-1});\n G[vp].push_back(pii{v3,sz(G[v3])});\n G[v3].push_back(pii{vp,sz(G[vp])-1});\n\n int N=sz(G);\n // cerr<<\"N:\"<<N<<endl;\n\n // fore(i,GC)cerr<<i<<' ';\n // cerr<<endl;\n // rep(i,N){\n // cerr<<\"v:\"<<i<<endl;\n // fore(j,G[i])cerr<<j.first<<'/'<<j.second<<' ';\n // cerr<<endl;\n // }\n // cerr<<endl;\n \n\n\n // (max,min)\n vector dp(N,vector<pii>());\n rep(i,N){\n dp[i].resize(sz(G[i]));\n fore(j,dp[i]){\n j=pii{-1e9,1e9};\n }\n }\n\n auto dfs=[&](auto f,int v,int p) -> pii {\n auto &g=G[v];\n rep(i,sz(g)){\n if(g[i].first==p)continue;\n dp[v][i]=f(f,g[i].first,v);\n }\n if(GC[v]=='+'){\n array<int,3> a,b;\n rep(i,sz(g)){\n a[i]=dp[v][i].first;\n b[i]=dp[v][i].second;\n }\n pii ret{-1e9,1e9};\n rep(i,3){\n rep(j,i+1,3){\n if(a[i]==-1e9||a[j]==-1e9)continue;\n chmax(ret.first,a[i]+a[j]);\n chmin(ret.second,b[i]+b[j]);\n }\n }\n return ret;\n }else if(GC[v]=='-'){\n array<int,3> a,b;\n rep(i,sz(g)){\n a[i]=dp[v][i].first;\n b[i]=dp[v][i].second;\n }\n pii ret{-1e9,1e9};\n rep(i,3){\n rep(j,3){\n if(a[i]==-1e9||a[j]==-1e9||i==j)continue;\n chmax(ret.first,a[i]-b[j]);\n chmin(ret.second,b[i]-a[j]);\n }\n }\n return ret;\n }else{\n int x=GC[v]-'0';\n return pii{x,x};\n }\n };\n\n dfs(dfs,vp,-1);\n\n // rep(i,N){\n // cerr<<\"v:\"<<i<<endl;\n // fore(j,dp[i])cerr<<j.first<<'/'<<j.second<<' ';\n // cerr<<endl;\n // }\n // cerr<<endl;\n\n auto dfs2=[&](auto f,int v,int p) -> void {\n auto &g=G[v];\n // leaf\n if(sz(g)==1)return;\n \n rep(x,sz(g)){\n pii a,b;\n int c=0;\n rep(j,sz(g)){\n if(j!=x){\n if(c==0)a=dp[v][j],c++;\n else if(c==1) b=dp[v][j];\n }\n }\n if(GC[v]=='+'){\n dp[g[x].first][g[x].second]=pii{a.first+b.first,a.second+b.second};\n }else if(GC[v]=='-'){\n dp[g[x].first][g[x].second]=pii{\n max(a.first-b.second,b.first-a.second),\n min(a.second-b.first,b.second-a.first)\n };\n }else{\n assert(false);\n }\n }\n rep(i,sz(g)){\n if(g[i].first!=p)f(f,g[i].first,v);\n }\n return;\n };\n\n dfs2(dfs2,vp,-1);\n\n // rep(i,N){\n // cerr<<\"v:\"<<i<<endl;\n // fore(j,dp[i])cerr<<j.first<<'/'<<j.second<<' ';\n // cerr<<endl;\n // }\n\n int ans=-1e9;\n rep(i,N){\n if(GC[i]=='+'){\n int now=0;\n for(auto j:dp[i]){\n now+=j.first;\n }\n chmax(ans,now);\n }else if(GC[i]=='-'){\n int s=0,f=-1e9;\n for(auto j:dp[i]){\n s-=j.second;\n chmax(f,j.first+j.second);\n }\n chmax(ans,f+s);\n }\n }\n cout<<ans<<'\\n';\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 28812, "score_of_the_acc": -0.1862, "final_rank": 7 }, { "submission_id": "aoj_1650_10642810", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint solve(){\n string s;\n cin>>s;\n if(s==\"-1\") return 0;\n vector<vector<int>> vp;\n int cnt=0;\n vector<int> dat;\n {\n vector<pair<int,int>> g;\n auto f=[&](auto self,int &i) -> int {\n if(s[i]=='('){\n i++;\n int a = self(self,i);\n char c = s[i];\n assert(c=='+'||c=='-');\n i++;\n int b = self(self,i);\n assert(s[i]==')');\n i++;\n if(c=='+') dat.push_back(10);\n else dat.push_back(-10);\n g.push_back({cnt,a});\n g.push_back({cnt,b});\n }else{\n dat.push_back(s[i]-'0');\n i++;\n }\n return cnt++;\n };\n int i=0;\n char c;\n {\n int x = f(f,i);\n c=s[i];\n i++;\n int y = f(f,i);\n assert(s[i]==c);\n i++;\n int z = f(f,i);\n g.push_back({cnt,x});\n g.push_back({cnt,y});\n g.push_back({cnt,z});\n if(c=='+') dat.push_back(10);\n else dat.push_back(-10);\n cnt++;\n }\n vp.resize(cnt);\n for(auto[x,y]:g){\n vp[x].push_back(y);\n vp[y].push_back(x);\n }\n }\n auto checkv=[&](vector<ll> vmx,vector<ll> vmn) ->pair<ll,ll> {\n int m=vmx.size();\n ll tmx=0,tmn=0;\n rep(i,0,m){\n tmx+=vmx[i];\n tmn+=vmn[i];\n }\n ll mx=-1e18,mn=1e18;\n rep(i,0,m){\n chmax(mx,-(tmn-vmn[i])+vmx[i]);\n chmin(mn,-(tmx-vmx[i])+vmn[i]);\n }\n return {mx,mn};\n };\n vector<ll> mx(cnt),mn(cnt);\n {\n auto dfs=[&](auto self,int nw,int pr) ->void {\n if(abs(dat[nw])<10){\n mx[nw]=mn[nw]=dat[nw];\n return;\n }\n if(dat[nw]>0){\n ll tmx=0,tmn=0;\n for(int nx:vp[nw]){\n if(nx==pr) continue;\n self(self,nx,nw);\n tmx+=mx[nx];\n tmn+=mn[nx];\n }\n mx[nw]=tmx;\n mn[nw]=tmn;\n }else{\n vector<ll> vmx,vmn;\n for(int nx:vp[nw]){\n if(nx==pr) continue;\n self(self,nx,nw);\n vmx.push_back(mx[nx]);\n vmn.push_back(mn[nx]);\n } \n auto[tmx,tmn]=checkv(vmx,vmn);\n mx[nw]=tmx;\n mn[nw]=tmn;\n }\n };\n dfs(dfs,cnt-1,-1);\n }\n ll ans=-1e18;\n {\n ll bmx=0,bmn=0;\n auto dfs=[&](auto self,int nw,int pr){\n if(abs(dat[nw])<10) return;\n vector<ll> vmx,vmn;\n for(int nx:vp[nw]){\n if(nx==pr) continue;\n vmx.push_back(mx[nx]);\n vmn.push_back(mn[nx]);\n }\n if(pr!=-1){\n vmx.push_back(bmx);\n vmn.push_back(bmn);\n }\n if(dat[nw]>0){\n ll tmx=0;\n for(auto x:vmx) tmx+=x;\n chmax(ans,tmx);\n }else{\n assert(vmx.size()==3);\n auto[tmx,tmn]=checkv(vmx,vmn);\n chmax(ans,tmx);\n }\n if(dat[nw]>0){\n ll tmx=0,tmn=0;\n for(auto x:vmx) tmx+=x;\n for(auto x:vmn) tmn+=x;\n for(int nx:vp[nw]){\n if(nx==pr) continue;\n bmx=tmx-mx[nx];\n bmn=tmn-mn[nx];\n self(self,nx,nw);\n }\n }else{\n int ctt=0;\n for(int nx:vp[nw]){\n if(nx==pr) continue;\n auto cmv=vmx;\n cmv.erase(cmv.begin()+ctt);\n auto cnv=vmn;\n cnv.erase(cnv.begin()+ctt);\n assert(cmv.size()==2);\n auto[tmx,tmn]=checkv(cmv,cnv);\n bmx=tmx;\n bmn=tmn;\n self(self,nx,nw);\n ctt++;\n }\n }\n };\n dfs(dfs,cnt-1,-1);\n }\n cout<<ans<<\"\\n\";\n return 1;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 18656, "score_of_the_acc": -0.1069, "final_rank": 4 }, { "submission_id": "aoj_1650_10642761", "code_snippet": "#include <bits/extc++.h>\n\nusing ll = long long;\n\nusing pii = std::pair<int, int>;\n\ntemplate <class T>\nbool chmax(T& x, const T& v) {\n if (x < v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\ntemplate <class T>\nbool chmin(T& x, const T& v) {\n if (x > v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\n\nstruct tree;\n\nusing pointer = std::shared_ptr<tree>;\n\nstruct tree {\n char op;\n ll min, max;\n bool cached = false;\n bool rooted = false;\n\n std::vector<pointer> children;\n\n tree(char op, pointer l, pointer r) : op(op), children({l, r}) {}\n tree(char op, pointer l, pointer m, pointer r)\n : op(op), children({l, m, r}), rooted(true) {}\n\n tree(ll val) : min(val), max(val), cached(true) {}\n\n void calc() {\n if (cached) {\n return;\n }\n\n for (auto ch : children) {\n ch->calc();\n }\n\n if (op == '+') {\n max = min = 0;\n for (auto ch : children) {\n max += ch->max;\n min += ch->min;\n }\n } else {\n max = std::numeric_limits<ll>::min();\n min = std::numeric_limits<ll>::max();\n\n for (int i = 0; i < children.size(); i++) {\n ll tmpmax = children[i]->max, tmpmin = children[i]->min;\n\n for (int k = 0; k < children.size(); k++) {\n if (i == k) continue;\n\n tmpmax -= children[k]->min;\n tmpmin -= children[k]->max;\n }\n\n chmax(max, tmpmax);\n chmin(min, tmpmin);\n }\n }\n\n cached = true;\n }\n\n void force_calc() {\n cached = false;\n calc();\n }\n\n static pointer rotate_at(pointer root, int i) {\n pointer l = root->children[i];\n\n root->children.erase(root->children.begin() + i);\n\n root->force_calc();\n\n l->children.push_back(root);\n\n l->force_calc();\n\n l->rooted = true;\n\n return l;\n }\n};\n\nvoid debug_(pointer root, std::string& str) {\n if (root->children.size()) {\n str.push_back('(');\n\n for (int i = 0; i < root->children.size(); i++) {\n if (i > 0) {\n str.push_back(root->op);\n }\n debug_(root->children[i], str);\n }\n\n str.push_back(')');\n\n } else {\n str.push_back('0' + root->max);\n }\n}\n\nstd::string debug(pointer root) {\n std::string str;\n\n debug_(root, str);\n\n return str;\n}\n\npointer parse(const std::string& str, int& pos) {\n if (std::isalnum(str[pos])) {\n return std::make_shared<tree>(str[pos++] - '0');\n } else {\n if (str[pos++] != '(') {\n std::cerr << \"error! \" << str[pos - 1] << '\\n';\n };\n pointer l = parse(str, pos);\n char op = str[pos++];\n pointer r = parse(str, pos);\n assert(str[pos++] == ')');\n return std::make_shared<tree>(op, l, r);\n }\n}\n\npointer parse_root(const std::string& str, int& pos) {\n pointer l = parse(str, pos);\n char op = str[pos++];\n pointer m = parse(str, pos);\n assert(op == str[pos++]);\n pointer r = parse(str, pos);\n assert(pos == str.size());\n\n return std::make_shared<tree>(op, l, m, r);\n}\n\nll traverse(pointer root) {\n root->calc();\n\n ll res = root->max;\n\n // std::println(std::cerr, \"{}\", debug(root));\n\n // std::println(std::cerr, \"size of tree: {}\", root->children.size());\n\n while (true) {\n bool rotated = false;\n\n for (int i = 0; i < 3; i++) {\n if (root->children[i]->children.size() && !root->children[i]->rooted) {\n pointer tmp = tree::rotate_at(root, i);\n\n // assert(root->children.size() == 2);\n // assert(tmp->children.size() == 3);\n\n rotated = true;\n chmax(res, traverse(tmp));\n\n bool is_ok = false;\n\n for (int k = 0; k < 3; k++) {\n if (tmp->children[k] == root) {\n tree::rotate_at(tmp, k);\n is_ok = true;\n break;\n }\n }\n\n assert(is_ok);\n\n assert(root->children.size() == 3);\n\n break;\n }\n }\n\n if (!rotated) {\n break;\n }\n }\n\n return res;\n}\n\nbool solve() {\n std::string S;\n std::cin >> S;\n int pos = 0;\n\n if (S == \"-1\") {\n return false;\n }\n\n pointer root = parse_root(S, pos);\n\n std::cout << traverse(root) << '\\n';\n\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 31840, "score_of_the_acc": -0.2269, "final_rank": 10 }, { "submission_id": "aoj_1650_10642729", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n/* ATCODER\n#include<atcoder/all>\nusing namespace atcoder;\ntypedef modint998244353 mint;\n//*/\n\n/* BOOST MULTIPRECISION\n#include<boost/multiprecision/cpp_int.hpp>\nusing namespace boost::multiprecision;\n//*/\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) begin(v),end(v)\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n\tif (a <= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> bool chmax(T &a, const T &b) {\n\tif (a >= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> T max(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T min(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T sum(vector<T> &a){\n\tT ret = 0;\n\tfor (int i=0; i<(int)a.size(); i++) ret += a[i];\n\treturn ret;\n}\n\nstring s;\nvector<vector<int>> ikeru;\nvector<int> is_leaf;\nint piv = 0;\nvector<int> st;\n\nvoid pars();\nvoid childs();\n\nvoid pars() {\n\n st.push_back(0);\n ikeru.push_back(vector<int>(0));\n is_leaf.push_back(-1);\n\n childs();\n char c = s[piv];\n assert(c == '+' || c == '-');\n is_leaf[st.back()] = (c == '+' ? -1 : -2);\n piv++;\n\n childs();\n piv++;\n childs();\n\n st.pop_back();\n\n}\n\nvoid childs() {\n if ('0' <= s[piv] && s[piv] <= '9') {\n is_leaf.push_back(s[piv] - '0');\n ikeru.push_back(vector<int>(0));\n st.push_back((int)is_leaf.size() - 1);\n\n ikeru[st[(int)st.size()-1]].push_back(st[(int)st.size()-2]);\n ikeru[st[(int)st.size()-2]].push_back(st[(int)st.size()-1]);\n piv++;\n\n st.pop_back();\n return;\n }\n\n ikeru.push_back(vector<int>(0));\n is_leaf.push_back(-1);\n st.push_back((int)is_leaf.size() - 1);\n\n ikeru[st[(int)st.size()-1]].push_back(st[(int)st.size()-2]);\n ikeru[st[(int)st.size()-2]].push_back(st[(int)st.size()-1]);\n\n\n assert(s[piv] == '(');\n piv++;\n\n childs();\n\n char c = s[piv];\n assert(c == '+' || c == '-');\n is_leaf[st.back()] = (c == '+' ? -1 : -2);\n piv++;\n\n childs();\n\n assert(s[piv] == ')');\n piv++;\n\n st.pop_back();\n}\n\n\nvoid solve() {\n ikeru.clear();\n is_leaf.clear();\n piv=0;\n st.clear();\n // root = 0\n pars();\n\n /*\n rep(i,0,(int)is_leaf.size()) {\n cout << is_leaf[i] << ' ';\n }\n cout << endl;\n */\n\n\n map<pair<int,int>,pair<ll,ll>> mp;\n auto dfs = [&](auto self, int i, int p) -> pair<ll,ll> {\n if(mp.find(pair(p, i)) != mp.end()) {\n return mp[pair(p, i)];\n }\n ll maxval = -1e9, minval = 1e9;\n if (is_leaf[i] >= 0) {\n maxval = is_leaf[i];\n minval = is_leaf[i];\n }else{\n vector<pair<ll,ll>> values;\n for (int j: ikeru[i]) {\n if (j == p) continue;\n values.push_back(self(self, j, i));\n //cout << i << ' ' << j << ' ' << self(self,j,i).first << ' ' << self(self,j,i).second << endl;\n }\n\n if (is_leaf[i] == -1) {\n minval = 0;\n maxval = 0;\n rep(j,0,(int)values.size()) {\n minval += values[j].first;\n maxval += values[j].second;\n }\n }else {\n ll tmpmin = 0, tmpmax = 0;\n rep(j,0,(int)values.size()) {\n tmpmin -= values[j].second;\n tmpmax -= values[j].first;\n }\n rep(j,0,(int)values.size()) {\n chmin(minval, tmpmin + values[j].second + values[j].first);\n chmax(maxval, tmpmax + values[j].second + values[j].first);\n }\n }\n \n }\n mp[pair(p, i)] = pair(minval, maxval);\n return pair(minval, maxval);\n };\n\n int n = is_leaf.size();\n ll ans = -1e9;\n rep(st,0,n) {\n if (is_leaf[st] >= 0) continue;\n dfs(dfs, st, st);\n chmax(ans, mp[pair(st,st)].second);\n }\n cout << ans << '\\n';\n\n}\n\nint main() {\n\twhile(true){\n cin >> s;\n if (s == \"-1\") break;\n solve();\n\t}\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 41128, "score_of_the_acc": -0.6659, "final_rank": 16 }, { "submission_id": "aoj_1650_10630701", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <array>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0; i<ll(n); i++)\ntemplate<class A, class B> void chmax(A& a, const B& b){ if(a < b) a = b; }\ntemplate<class A, class B> void chmin(A& a, const B& b){ if(b < a) a = b; }\n\nvoid testcase(){\n string s; cin >> s;\n if(s == \"-1\") exit(0);\n vector<vector<ll>> adj;\n vector<ll> val;\n auto newNode = [&]() -> ll {\n ll res = adj.size();\n adj.push_back({});\n val.push_back(-1);\n return res;\n };\n auto addEdge = [&](ll u, ll v){\n adj[u].push_back(v);\n adj[v].push_back(u);\n };\n auto readNonRoot = [&](auto& readNonRoot, ll& p) -> ll {\n ll q = newNode();\n if(s[p] == '('){\n p++;\n auto a = readNonRoot(readNonRoot, p);\n val[q] = s[p++];\n auto b = readNonRoot(readNonRoot, p);\n addEdge(q,a);\n addEdge(q,b);\n p++;\n } else {\n val[q] = s[p++];\n }\n return q;\n };\n auto readRoot = [&]() -> ll {\n ll p = 0;\n ll q = newNode();\n auto a = readNonRoot(readNonRoot, p);\n val[q] = s[p++];\n auto b = readNonRoot(readNonRoot, p);\n s[p++];\n auto c = readNonRoot(readNonRoot, p);\n addEdge(q,a);\n addEdge(q,b);\n addEdge(q,c);\n return q;\n };\n readRoot();\n\n ll N = adj.size();\n vector<array<ll, 3>> ql(N), qr(N);\n rep(i,N) rep(j,3) ql[i][j] = 1ll << 60;\n rep(i,N) rep(j,3) qr[i][j] = -(1ll << 60);\n\n auto dfs = [&](auto& dfs, ll v, ll w) -> pair<ll, ll> {\n if(val[v] != '+' && val[v] != '-') return { val[v] - '0', val[v] - '0' };\n ll i = 0; while(adj[v][i] != w) i++;\n if(ql[v][i] != (1ll << 60)) return { ql[v][i], qr[v][i] };\n auto [a1, b1] = dfs(dfs, adj[v][(i+1)%3], v);\n auto [a2, b2] = dfs(dfs, adj[v][(i+2)%3], v);\n if(val[v] == '+'){\n ql[v][i] = a1 + a2;\n qr[v][i] = b1 + b2;\n } else {\n ql[v][i] = min(a1 - b2, a2 - b1);\n qr[v][i] = max(b1 - a2, b2 - a1);\n }\n return { ql[v][i], qr[v][i] };\n };\n\n ll ans = -(1ll << 60);\n rep(i,N) if(val[i] == '+' || val[i] == '-'){\n auto [a1, b1] = dfs(dfs, adj[i][0], i);\n auto [a2, b2] = dfs(dfs, adj[i][1], i);\n auto [a3, b3] = dfs(dfs, adj[i][2], i);\n if(val[i] == '+'){\n chmax(ans, b1 + b2 + b3);\n } else {\n chmax(ans, b1 - a2 - a3);\n chmax(ans, b2 - a1 - a3);\n chmax(ans, b3 - a2 - a1);\n }\n }\n cout << ans << \"\\n\";\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true) testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 28144, "score_of_the_acc": -0.1487, "final_rank": 5 }, { "submission_id": "aoj_1650_10624579", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <array>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0; i<ll(n); i++)\ntemplate<class A, class B> void chmax(A& a, const B& b){ if(a < b) a = b; }\ntemplate<class A, class B> void chmin(A& a, const B& b){ if(b < a) a = b; }\n\nvoid testcase(){\n string s; cin >> s;\n if(s == \"-1\") exit(0);\n vector<vector<ll>> adj;\n vector<ll> val;\n auto newNode = [&]() -> ll {\n ll res = adj.size();\n adj.push_back({});\n val.push_back(-1);\n return res;\n };\n auto addEdge = [&](ll u, ll v){\n adj[u].push_back(v);\n adj[v].push_back(u);\n };\n auto readNonRoot = [&](auto& readNonRoot, ll& p) -> ll {\n ll q = newNode();\n if(s[p] == '('){\n p++;\n auto a = readNonRoot(readNonRoot, p);\n val[q] = s[p++];\n auto b = readNonRoot(readNonRoot, p);\n addEdge(q,a);\n addEdge(q,b);\n p++;\n } else {\n val[q] = s[p++];\n }\n return q;\n };\n auto readRoot = [&]() -> ll {\n ll p = 0;\n ll q = newNode();\n auto a = readNonRoot(readNonRoot, p);\n val[q] = s[p++];\n auto b = readNonRoot(readNonRoot, p);\n s[p++];\n auto c = readNonRoot(readNonRoot, p);\n addEdge(q,a);\n addEdge(q,b);\n addEdge(q,c);\n return q;\n };\n readRoot();\n\n ll N = adj.size();\n vector<array<ll, 3>> ql(N), qr(N);\n rep(i,N) rep(j,3) ql[i][j] = 1ll << 60;\n rep(i,N) rep(j,3) qr[i][j] = -(1ll << 60);\n\n auto dfs = [&](auto& dfs, ll v, ll w) -> pair<ll, ll> {\n if(val[v] != '+' && val[v] != '-') return { val[v] - '0', val[v] - '0' };\n ll i = 0; while(adj[v][i] != w) i++;\n if(ql[v][i] != (1ll << 60)) return { ql[v][i], qr[v][i] };\n auto [a1, b1] = dfs(dfs, adj[v][(i+1)%3], v);\n auto [a2, b2] = dfs(dfs, adj[v][(i+2)%3], v);\n if(val[v] == '+'){\n ql[v][i] = a1 + a2;\n qr[v][i] = b1 + b2;\n } else {\n ql[v][i] = min(a1 - b2, a2 - b1);\n qr[v][i] = max(b1 - a2, b2 - a1);\n }\n return { ql[v][i], qr[v][i] };\n };\n\n ll ans = -(1ll << 60);\n rep(i,N) if(val[i] == '+' || val[i] == '-'){\n auto [a1, b1] = dfs(dfs, adj[i][0], i);\n auto [a2, b2] = dfs(dfs, adj[i][1], i);\n auto [a3, b3] = dfs(dfs, adj[i][2], i);\n if(val[i] == '+'){\n chmax(ans, b1 + b2 + b3);\n } else {\n chmax(ans, b1 - a2 - a3);\n chmax(ans, b2 - a1 - a3);\n chmax(ans, b3 - a2 - a1);\n }\n }\n cout << ans << \"\\n\";\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true) testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 28652, "score_of_the_acc": -0.1555, "final_rank": 6 }, { "submission_id": "aoj_1650_10603270", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\n// Linear Algebra ////////////////////////////////////////////////\nconst double Rad2Deg = 180.0 / M_PI;\nconst double Deg2Rad = M_PI / 180.0;\n//////////////////////////////////////////////////////////////////\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n\n\n\nstruct DP\n{\n\tll mx;\n\tll mn;\n\n DP(ll mx = -INF, ll mn = INF) : mx(mx), mn(mn) {}\n\n // 2つのDPをマージ(結合則が成り立つ必要がある)\n /* DP operator+(const DP &rhs) const { return DP(dp + rhs.dp, sum + rhs.sum); }\n DP operator-(const DP &rhs) const { return DP(dp - rhs.dp, sum - rhs.sum); } */\n // 親に辿ったときの値の更新\n // DP up() const { return DP(dp + sum, sum); }\n};\n\nDP calculate(vector<DP> children, char op) {\n\tDP res(-INF, INF);\n\n\tvll p;\n\trep(i, children.size()) {\n\t\tp.push_back(i);\n\t}\n\tdo {\n\t\trep(ismx, 1 << children.size()) {\n\t\t\tll ans;\n\t\t\tif (ismx & (1 << 0)) ans = children[p[0]].mx;\n\t\t\telse ans = children[p[0]].mn;\n\n\t\t\tloop(i, 1, children.size() - 1) {\n\t\t\t\tif (ismx & (1 << i)) {\n\t\t\t\t\tif (op == '+') ans += children[p[i]].mx;\n\t\t\t\t\telse ans -= children[p[i]].mx;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (op == '+') ans += children[p[i]].mn;\n\t\t\t\t\telse ans -= children[p[i]].mn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchmax(res.mx, ans);\n\t\t\tchmin(res.mn, ans);\n\t\t}\n\t}\n\twhile (next_permutation(all(p)));\n\treturn res;\n}\n\nint solve() {\n\tstring s;\n\tcin >> s;\n\tif (s == \"-1\") return 1;\n\n\tvector<vector<DP>> dp;\n\tvector<bool> isNode;\n\tvector<char> ops;\n\tvvll g;\n\tvector<PLL> edges;\n\tauto parse = [&](auto self, int& i, int idx) -> pair<ll, DP> {\n\t\tdp.push_back(vector<DP>());\n\t\tisNode.push_back(true);\n\t\tops.push_back('\\0');\n\t\tg.push_back(vll());\n\n\t\tll nextidx = idx + 1;\n\t\tif (s[i] == '(') {\n\t\t\tg[idx].push_back(nextidx);\n\t\t\tedges.push_back({idx, nextidx});\n\t\t\ti++;\n\t\t\tauto [id, val] = self(self, i, nextidx);\n\t\t\tdp[idx].push_back(val);\n\t\t\tnextidx = id + 1;\n\t\t}\n\t\telse {\n\t\t\tassert('0' <= s[i] && s[i] <= '9');\n\t\t\tg.push_back(vll());\n\t\t\tg[idx].push_back(nextidx);\n\t\t\tedges.push_back({idx, nextidx});\n\t\t\tll v = s[i] - '0';\n\t\t\tdp.push_back(vector<DP>());\n\t\t\tisNode.push_back(false);\n\t\t\tops.push_back('\\0');\n\t\t\tdp[idx].push_back(DP(v, v));\n\t\t\tnextidx++;\n\t\t}\n\n\t\ti++;\n\t\tassert(s[i] == '+' || s[i] == '-');\n\t\tops[idx] = s[i];\n\t\ti++;\n\n\t\tif (s[i] == '(') {\n\t\t\tg[idx].push_back(nextidx);\n\t\t\tedges.push_back({idx, nextidx});\n\t\t\ti++;\n\t\t\tauto [id, val] = self(self, i, nextidx);\n\t\t\tdp[idx].push_back(val);\n\t\t\tnextidx = id + 1;\n\t\t}\n\t\telse {\n\t\t\tassert('0' <= s[i] && s[i] <= '9');\n\t\t\tg.push_back(vll());\n\t\t\tg[idx].push_back(nextidx);\n\t\t\tedges.push_back({idx, nextidx});\n\t\t\tll v = s[i] - '0';\n\t\t\tdp.push_back(vector<DP>());\n\t\t\tisNode.push_back(false);\n\t\t\tops.push_back('\\0');\n\t\t\tdp[idx].push_back(DP(v, v));\n\t\t\tnextidx++;\n\t\t}\n\n\t\ti++;\n\t\tassert(s[i] == '+' || s[i] == '-' || s[i] == ')');\n\n\t\tif (s[i] == ')') {\n\t\t\tgoto calc;\n\t\t}\n\n\t\ti++;\n\t\tif (s[i] == '(') {\n\t\t\tg[idx].push_back(nextidx);\n\t\t\tedges.push_back({idx, nextidx});\n\t\t\ti++;\n\t\t\tauto [id, val] = self(self, i, nextidx);\n\t\t\tdp[idx].push_back(val);\n\t\t\tnextidx = id + 1;\n\t\t}\n\t\telse {\n\t\t\tassert('0' <= s[i] && s[i] <= '9');\n\t\t\tg.push_back(vll());\n\t\t\tg[idx].push_back(nextidx);\n\t\t\tedges.push_back({idx, nextidx});\n\t\t\tll v = s[i] - '0';\n\t\t\tdp.push_back(vector<DP>());\n\t\t\tisNode.push_back(false);\n\t\t\tops.push_back('\\0');\n\t\t\tdp[idx].push_back(DP(v, v));\n\t\t\tnextidx++;\n\t\t}\n\n\tcalc:\n\t\tDP res = calculate(dp[idx], ops[idx]);\n\t\treturn {nextidx - 1, res};\n\t};\n\n\tint i = 0;\n\tparse(parse, i, 0);\n\n\tfor (auto [u, v] : edges) {\n\t\tg[v].push_back(u);\n\t\tdp[v].push_back(DP());\n\t}\n\n\tll ans = -INF;\n auto bfs = [&](auto bfs, ll v, ll p, DP par) -> void {\n ll deg = g[v].size();\n rep(i, deg) if (g[v][i] == p) dp[v][i] = par;\n\n\t\tvector<DP> all;\n\t\trep(i, deg) {\n\t\t\tall.push_back(dp[v][i]);\n\t\t}\n\t\tchmax(ans, calculate(all, ops[v]).mx);\n\n rep(i, deg) {\n if (g[v][i] == p) continue;\n\t\t\tif (!isNode[g[v][i]]) continue;\n\n\t\t\tvector<DP> children;\n\t\t\trep(j, deg) {\n\t\t\t\tif (i == j) continue;\n\t\t\t\tchildren.push_back(dp[v][j]);\n\t\t\t}\n\t\t\tDP now = calculate(children, ops[v]);\n bfs(bfs, g[v][i], v, now);\n }\n };\n bfs(bfs, 0, -1, DP());\n\n\tcout << ans << \"\\n\";\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 37316, "score_of_the_acc": -0.5004, "final_rank": 14 }, { "submission_id": "aoj_1650_10602732", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)\n#define vl vector<long long>\n#define vvl vector<vector<long long>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vpdbg(a) rep(ii,a.size()){cout<<\"{\"<<a[ii].first<<\",\"<<a[ii].second<<\"} \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 4000000000000000000LL\n#define mod 998244353LL\n//#define mod 1000000007LL\n#define eps 0.000000001\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\n//#include<boost/multiprecision/cpp_int.hpp>\n//#define bbi boost::multiprecision::cpp_int\n//#include<atcoder/lazysegtree>\n\n\n//整数同士の累乗の計算をする。\nll power(ll A, ll B) {\n\tll result = 1;\n\tfor (ll i=0;i<B;i++){\n\t\tresult *= A;\n\t}\n\treturn result;\n}\n\n// nのk乗をmodで割った余りを計算\nll power_mod(ll n, ll k){\n\tlong long result = 1;\n\twhile (k > 0){\n\t\tif ((k&1) ==1)result=(result*n)%mod;\n\t\tn=n*n%mod;\n\t\tk >>= 1;\n\t}\n\treturn result;\n}\n\n\n//受け取った2次元文字の外側に、文字pをコーティングする。\nvector<string> pad(vector<string> &s,char p){\n\tll h=s.size();\n\tll w=s[0].size();\n\tvector<string> res(h+2,string(w+2,p));\n\trep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];\n\treturn res;\n}\n\n// Union-Find\nstruct UnionFind {\n\tvector<int> par, siz;\n\tUnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t// x を含むグループと y を含むグループとを併合する\n\tbool unite(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false; \n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\n\n\n//グリッド問題等用\nvl dx={1,0,-1,0};\nvl dy={0,1,0,-1};\n\nvvl g;\nstring s;\n\nvector<map<ll,pair<ll,ll>>> dp;\n\npair<ll,ll> dfs(ll node,ll before){\n\tif(dp[node].count(before))return dp[node][before];\n\tif(s[node]!='+'&&s[node]!='-'){\n\t\tdp[node][before]={s[node]-'0',s[node]-'0'};\n\t\treturn dp[node][before];\n\t}\n\n\tpair<ll,ll> ans={-inf,inf};\n\tvector<pair<ll,ll>> list;\n\trep(i,g[node].size()){\n\t\tif(g[node][i]==before)continue;\n\t\tlist.push_back(dfs(g[node][i],node));\n\t}\n\tsort(list.begin(),list.end());\n\n\tdo{\n\t\tpair<ll,ll> tmp=list[0];\n\t\tloop(i,1,list.size()-1){\n\t\t\tif(s[node]=='+'){\n\t\t\t\ttmp.first+=list[i].first;\n\t\t\t\ttmp.second+=list[i].second;\n\t\t\t}else{\n\t\t\t\ttmp.first-=list[i].second;\n\t\t\t\ttmp.second-=list[i].first;\n\t\t\t}\n\t\t}\n\t\tans.first=max(ans.first,tmp.first);\n\t\tans.second=min(ans.second,tmp.second);\n\t}while(next_permutation(list.begin(),list.end()));\n\tdp[node][before]=ans;\n\treturn dp[node][before];\n}\n\nll solve(){\n\tcin>>s;\n\tif(s==\"-1\"){return 1;}\n\tg=vvl(s.size());\n\tdp=vector<map<ll,pair<ll,ll>>>(s.size());\n\n\tvl st;\n\trep(i,s.size()){\n\t\tst.push_back(i);\n\t\tif(s[i]==')'){\n\t\t\tll par=st[st.size()-3];\n\t\t\tll chil1=st[st.size()-2];\n\t\t\tll chil2=st[st.size()-4];\n\t\t\tg[par].push_back(chil1);\n\t\t\tg[par].push_back(chil2);\n\t\t\tg[chil1].push_back(par);\n\t\t\tg[chil2].push_back(par);\n\t\t\tst.pop_back();\n\t\t\tst.pop_back();\n\t\t\tst.pop_back();\n\t\t\tst.pop_back();\n\t\t\tst.pop_back();\n\t\t\tst.push_back(par);\n\t\t}\n\t}\n\tll par=st[st.size()-4];\n\tll chil1=st[st.size()-1];\n\tll chil2=st[st.size()-3];\n\tll chil3=st[st.size()-5];\n\tg[par].push_back(chil1);\n\tg[par].push_back(chil2);\n\tg[par].push_back(chil3);\n\tg[chil1].push_back(par);\n\tg[chil2].push_back(par);\n\tg[chil3].push_back(par);\n\n\tll ans=-inf;\n\trep(i,s.size()){\n\t\tif(g[i].size()==3){\n\t\t\tans=max(ans,dfs(i,-1).first);\n\t\t}\n\t}\n\tcout<<ans<<endl;\n\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(solve()==0);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 64444, "score_of_the_acc": -0.8363, "final_rank": 17 }, { "submission_id": "aoj_1650_10601052", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 500005\n#define MAX_DEPTH 105\n\nstruct Node{\n\n\tint value; //葉なら値あり\n\tchar op; //■@:葉\n};\n\n\nint num_node;\nchar line[SIZE];\nvector<int> V[MAX_DEPTH],children[SIZE];\nint dp_min[SIZE],dp_max[SIZE],pre_min[SIZE],pre_max[SIZE];\nint ans_min[SIZE],ans_max[SIZE];\nNode nodes[SIZE];\n\n\nint calc_E(int left,int right,int depth);\nint calc_F(int left,int right,int depth);\nint calc_NUM(int left,int right,int depth);\n\n\n//ノード番号を返す\nint calc_E(int left,int right,int depth){\n\n\tvector<int> vec;\n\n\t//深さdepthの、プラスまたは-を探す\n\tint L = lower_bound(V[depth].begin(),V[depth].end(),left)-V[depth].begin();\n\tint R = upper_bound(V[depth].begin(),V[depth].end(),right)-V[depth].begin();\n\tR--;\n\n\tint ind;\n\n\tif(L == V[depth].size()){ //演算子なし\n\n\t\tind = calc_F(left,right,depth);\n\n\t}else{ //演算子あり\n\n\t\tfor(int i = L; i <= R; i++){\n\n\t\t\tvec.push_back(V[depth][i]); //インデックス\n\t\t}\n\n\t\tif(depth == 0){ //■ルート\n\n\t\t\tind = 0;\n\n\t\t\tnodes[0].op = line[vec[0]]; //■rootは+2個か-2個\n\n\t\t\tint a = calc_F(left,vec[0]-1,depth);\n\t\t\tint b = calc_F(vec[0]+1,vec[1]-1,depth);\n\t\t\tint c = calc_F(vec[1]+1,right,depth);\n\n\t\t\tchildren[0].push_back(a);\n\t\t\tchildren[0].push_back(b);\n\t\t\tchildren[0].push_back(c);\n\n\t\t}else{\n\n\t\t\t++num_node; //■■注意■■\n\n\t\t\tind = num_node;\n\t\t\tnodes[ind].op = line[vec[0]];\n\n\t\t\tint a = calc_F(left,vec[0]-1,depth);\n\t\t\tint b = calc_F(vec[0]+1,right,depth);\n\n\t\t\tchildren[ind].push_back(a);\n\t\t\tchildren[ind].push_back(b);\n\t\t}\n\t}\n\n\treturn ind;\n}\n\n\nint calc_F(int left,int right,int depth){\n\n\tif(line[left] >= '0' && line[left] <= '9'){\n\n\t\treturn calc_NUM(left,right,depth);\n\n\t}else{ // line[left] == '('\n\n\t\treturn calc_E(left+1,right-1,depth+1);\n\t}\n}\n\nint calc_NUM(int left,int right,int depth){\n\n\t++num_node;\n\n\tnodes[num_node].op = '@';\n\tnodes[num_node].value = line[left]-'0';\n\n\tint ret = num_node;\n\n\treturn ret;\n}\n\n//■葉方向の最大値、最小値を計算\nvoid dfs(int node){\n\n\tif(nodes[node].op == '@'){//葉\n\n\t\tdp_min[node] = nodes[node].value;\n\t\tdp_max[node] = nodes[node].value;\n\n\t\treturn;\n\t}\n\n\t//節\n\tvector<int> vec;\n\n\tfor(int i = 0; i < children[node].size(); i++){\n\n\t\tint child = children[node][i];\n\n\t\tdfs(child);\n\t\tvec.push_back(child);\n\t}\n\n\tif(nodes[node].op == '+'){\n\n\t\tint maxi = 0;\n\t\tint mini = 0;\n\n\t\tfor(int i = 0; i < children[node].size(); i++){\n\n\t\t\tint child = children[node][i];\n\n\t\t\tmaxi += dp_max[child]; //最大値は、各子の最大値の和\n\t\t\tmini += dp_min[child]; //最小値は、各子の最小値の和\n\t\t}\n\n\t\tdp_max[node] = maxi;\n\t\tdp_min[node] = mini;\n\n\t}else{ //nodes[node].op == '-'\n\n\t\tint maxi = -BIG_NUM;\n\t\tint mini = BIG_NUM;\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tint left = vec[i]; //左端のノード\n\n\t\t\tint tmp_maxi = dp_max[left];\n\t\t\tint tmp_mini = dp_min[left];\n\n\n\t\t\tfor(int k = 0; k < vec.size(); k++){\n\n\t\t\t\tif(k == i)continue;\n\n\t\t\t\ttmp_maxi -= dp_min[vec[k]];\n\t\t\t\ttmp_mini -= dp_max[vec[k]];\n\t\t\t}\n\n\t\t\tmaxi = max(maxi,tmp_maxi);\n\t\t\tmini = min(mini,tmp_mini);\n\t\t}\n\n\t\tdp_max[node] = maxi;\n\t\tdp_min[node] = mini;\n\t}\n}\n\nvoid dfs2(int node,int pre){\n\n\tif(node == 0){\n\n\t\tans_min[node] = dp_min[node];\n\t\tans_max[node] = dp_max[node];\n\n\t}else{\n\n\t\tif(nodes[node].op == '@')return; //葉\n\n\t\t/*■まずはpre_min,pre_maxを求める■*/\n\n\t\tif(nodes[pre].op == '+'){\n\n\t\t\tpre_min[node] = (ans_min[pre]-dp_min[node]);\n\t\t\tpre_max[node] = (ans_max[pre]-dp_max[node]);\n\n\t\t}else{ //nodes[pre].op == '-'\n\n\t\t\tvector<int> vec;\n\n\t\t\tif(pre == 0){ //■preがroot\n\n\t\t\t\t//■自分以外の2つの子を求める\n\t\t\t\tfor(int i = 0; i < children[pre].size(); i++){\n\n\t\t\t\t\tint child = children[pre][i];\n\t\t\t\t\tif(child == node)continue;\n\n\t\t\t\t\tvec.push_back(child);\n\t\t\t\t}\n\n\t\t\t\tint a = vec[0];\n\t\t\t\tint b = vec[1];\n\n\t\t\t\tpre_min[node] = min(dp_min[a]-dp_max[b],dp_min[b]-dp_max[a]);\n\t\t\t\tpre_max[node] = max(dp_max[a]-dp_min[b],dp_max[b]-dp_min[a]);\n\n\t\t\t}else{ //■preがrootでない\n\n\t\t\t\tint tmp_min = pre_min[pre];\n\t\t\t\tint tmp_max = pre_max[pre];\n\n\t\t\t\tint a;\n\t\t\t\tfor(int i = 0; i < children[pre].size(); i++){\n\n\t\t\t\t\tint child = children[pre][i];\n\t\t\t\t\tif(child != node){\n\n\t\t\t\t\t\ta = child;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tpre_min[node] = min(dp_min[a]-tmp_max,tmp_min-dp_max[a]);\n\t\t\t\tpre_max[node] = max(dp_max[a]-tmp_min,tmp_max-dp_min[a]);\n\t\t\t}\n\t\t}\n\n\t\t/*■ans_min,ans_maxを計算する■*/\n\n\t\tif(nodes[node].op == '+'){\n\n\t\t\tans_max[node] = dp_max[node]+pre_max[node];\n\t\t\tans_min[node] = dp_min[node]+pre_min[node];\n\n\t\t}else{ //nodes[node].op == '-'\n\n\t\t\tint a = children[node][0];\n\t\t\tint b = children[node][1];\n\n\t\t\tans_max[node] = dp_max[a]-(dp_min[b]+pre_min[node]);\n\t\t\tans_max[node] = max(ans_max[node],dp_max[b]-(dp_min[a]+pre_min[node]));\n\t\t\tans_max[node] = max(ans_max[node],pre_max[node]-(dp_min[a]+dp_min[b]));\n\n\n\t\t\tans_min[node] = dp_min[a]-(dp_max[b]+pre_max[node]);\n\t\t\tans_min[node] = min(ans_min[node],dp_min[b]-(dp_max[a]+pre_max[node]));\n\t\t\tans_min[node] = min(ans_min[node],pre_min[node]-(dp_max[a]+dp_max[b]));\n\t\t}\n\t}\n\n\tfor(int i = 0; i < children[node].size(); i++){\n\n\t\tint child = children[node][i];\n\t\tdfs2(child,node);\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < MAX_DEPTH; i++){\n\n\t\tV[i].clear();\n\t}\n\tfor(int i = 0; i < SIZE; i++){\n\n\t\tchildren[i].clear();\n\t}\n\n\tint len;\n\tfor(len = 0; line[len] != '\\0'; len++);\n\n\tint dep = 0;\n\n\t//各深さにおける演算子の位置を前計算\n\tfor(int i = 0; i < len; i++){\n\t\tif(line[i] == '('){\n\n\t\t\tdep++;\n\n\t\t}else if(line[i] == ')'){\n\n\t\t\tdep--;\n\n\t\t}else if(line[i] == '-' || line[i] == '+'){\n\n\t\t\tV[dep].push_back(i);\n\t\t}\n\t}\n\n\tnum_node = 0;\n\n\t//初期状態の木構造取得\n\tcalc_E(0,len-1,0);\n\n\tfor(int i = 0; i <= num_node; i++){\n\n\t\tdp_min[i] = BIG_NUM;\n\t\tdp_max[i] = -BIG_NUM;\n\n\t\tans_min[i] = BIG_NUM;\n\t\tans_max[i] = -BIG_NUM;\n\t}\n\n\tdfs(0);\n\tdfs2(0,-1);\n\n\tint ans = -BIG_NUM;\n\tfor(int i = 0; i <= num_node; i++){\n\t\tif(nodes[i].op == '@')continue;\n\t\tans = max(ans,ans_max[i]);\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%s\",line);\n\t\tif(line[0] == '-' && line[1] == '1' && line[2] == '\\0')break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 34492, "score_of_the_acc": -0.2911, "final_rank": 12 }, { "submission_id": "aoj_1650_10441011", "code_snippet": "// AOJ #1650 Tree Transformation Puzzle\n// // 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nstring Cins() {\n static char temp[500005];\n char *s = temp;\n do *s = gc();\n while (*s++ > ' ');\n *(s-1) = 0;\n string input(temp);\n return input;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nstring S;\nint p, n;\nvector<char> c;\nvector<ll> a;\nvector<vector<int>> ch, g;\nvector<vector<ll>> mn, mx;\n\nint idx(int u, int v){\n for(int i = 0; i < (int)g[u].size(); ++i) if(g[u][i] == v) return i;\n return -1;\n}\n\nint pr(){\n if(isdigit(S[p])){\n int u = n++;\n c.push_back('?');\n a.push_back(S[p++] - '0');\n ch.emplace_back();\n return u;\n }\n ++p;\n int l = pr();\n char o = S[p++];\n int r = pr();\n ++p;\n int u = n++;\n c.push_back(o);\n a.push_back(0);\n ch.push_back({l, r});\n return u;\n}\n\nvoid dfs1(int u, int p0){\n for(int v: g[u]) if(v != p0) dfs1(v, u);\n if(p0 != -1){\n int i = idx(u, p0);\n ll mnv, mxv;\n vector<pair<ll,ll>> t;\n for(int w: g[u]) if(w != p0){\n int j = idx(w, u);\n t.emplace_back(mn[w][j], mx[w][j]);\n }\n if(c[u] == '?') mnv = mxv = a[u];\n else if(c[u] == '+'){\n mnv = t[0].first + t[1].first;\n mxv = t[0].second + t[1].second;\n } else {\n ll a0 = t[0].first, A0 = t[0].second;\n ll b0 = t[1].first, B0 = t[1].second;\n mxv = max(A0 - b0, B0 - a0);\n mnv = min(a0 - B0, b0 - A0);\n }\n mn[u][i] = mnv;\n mx[u][i] = mxv;\n }\n}\n\nvoid dfs2(int u, int p0){\n for(int v: g[u]) if(v != p0){\n int i = idx(u, v);\n ll mnv, mxv;\n vector<pair<ll,ll>> t;\n for(int w: g[u]) if(w != v){\n int j = idx(w, u);\n t.emplace_back(mn[w][j], mx[w][j]);\n }\n if(c[u] == '?') mnv = mxv = a[u];\n else if(c[u] == '+'){\n mnv = t[0].first + t[1].first;\n mxv = t[0].second + t[1].second;\n } else {\n ll a0 = t[0].first, A0 = t[0].second;\n ll b0 = t[1].first, B0 = t[1].second;\n mxv = max(A0 - b0, B0 - a0);\n mnv = min(a0 - B0, b0 - A0);\n }\n mn[u][i] = mnv;\n mx[u][i] = mxv;\n dfs2(v, u);\n }\n}\n\nint main(){\n while(true){\n S = Cins();\n if(S == \"-1\") break;\n\n p = 0; n = 0;\n c.clear(); a.clear(); ch.clear();\n\n int u1 = pr();\n char o = S[p++];\n int u2 = pr();\n ++p;\n int u3 = pr();\n\n int rt = n++;\n c.push_back(o);\n a.push_back(0);\n ch.push_back({u1, u2, u3});\n\n g.assign(n, {});\n for(int i = 0; i < n; ++i){\n for(int v: ch[i]){\n g[i].push_back(v);\n g[v].push_back(i);\n }\n }\n mn.assign(n, {});\n mx.assign(n, {});\n for(int i = 0; i < n; ++i){\n mn[i].assign(g[i].size(), 0);\n mx[i].assign(g[i].size(), 0);\n }\n\n dfs1(rt, -1);\n dfs2(rt, -1);\n\n ll ans = LLONG_MIN;\n for(int i = 0; i < n; ++i){\n if(c[i] != '?' && g[i].size() == 3){\n vector<pair<ll,ll>> t;\n for(int v: g[i]){\n int j = idx(v, i);\n t.emplace_back(mn[v][j], mx[v][j]);\n }\n ll cur;\n if(c[i] == '+') cur = t[0].second + t[1].second + t[2].second;\n else {\n ll sumMin = t[0].first + t[1].first + t[2].first;\n ll best = LLONG_MIN;\n for(int k = 0; k < 3; ++k){\n ll oth = sumMin - t[k].first;\n best = max(best, t[k].second - oth);\n }\n cur = best;\n }\n ans = max(ans, cur);\n }\n }\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 47272, "score_of_the_acc": -0.577, "final_rank": 15 }, { "submission_id": "aoj_1650_10055691", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nstring s;\nvector<pair<char,vector<int>>> tree(0);\nvector<bool> visi(0);\nvector<int> vs(0);\nint now = 0;\nint df2 = 0;\nvector<pair<int,int>> dp(0);\nvector<pair<int,int>> rdp(0);\n\nint ans;\n\n\nvoid al(int l, int r, int root) {\n if (r-l == 1) {\n tree[now].first = s[l];\n tree[now].second.push_back(root);\n tree[root].second.push_back(now);\n now++;\n return;\n }\n\n int br = 0;\n for (int i = l+1; i < r-1; i++) {\n if (s[i] == '(') br++;\n if (s[i] == ')') br--;\n if (br == 0 && (s[i] == '+' || s[i] == '-')) {\n tree[now].first = s[i];\n tree[now].second.push_back(root);\n tree[root].second.push_back(now);\n int temp = now;\n now++;\n al(l+1,i,temp);\n al(i+1,r-1,temp);\n }\n }\n}\n\nvoid dfs2(int x) {\n vs[x] = df2;\n df2++;\n\n\n if (tree[x].first != '+' && tree[x].first != '-') return;\n\n\n for (auto p : tree[x].second) {\n if (vs[p] < 100000000) {\n vector<pair<int,int>> temp;\n for (auto pp : tree[p].second) {\n if (pp == x) continue;\n if (vs[pp] < vs[p]) {\n temp.push_back(rdp[p]);\n }\n else {\n temp.push_back(dp[pp]);\n }\n }\n assert(temp.size() >= 2);\n if (tree[p].first == '+') {\n rdp[x].first = temp[0].first+temp[1].first;\n rdp[x].second = temp[0].second+temp[1].second;\n }\n else if (tree[p].first == '-') {\n rdp[x].first = max(temp[0].first-temp[1].second,temp[1].first-temp[0].second);\n rdp[x].second = min(temp[0].second-temp[1].first,temp[1].second-temp[0].first);\n }\n }\n else {\n dfs2(p);\n }\n }\n\n if (x == 0) {\n return;\n }\n\n\n vector<pair<int,int>> temp;\n for (auto p : tree[x].second) {\n if (vs[x] < vs[p]) {\n temp.push_back(dp[p]);\n }\n else {\n temp.push_back(rdp[x]);\n }\n }\n\n assert(temp.size() >= 3);\n if (tree[x].first == '+') {\n ans = max(ans,temp[0].first+temp[1].first+temp[2].first);\n }\n else if (tree[x].first == '-') {\n ans = max(ans,max(temp[0].first-temp[1].second-temp[2].second,max(temp[1].first-temp[0].second-temp[2].second,temp[2].first-temp[0].second-temp[1].second)));\n }\n}\n\n\nvoid dfs(int x) {\n visi[x] = true;\n if (tree[x].first == '+') {\n dp[x].first = 0;\n dp[x].second = 0;\n for (auto p : tree[x].second) {\n if (!visi[p]) {\n dfs(p);\n dp[x].first += dp[p].first;\n dp[x].second += dp[p].second;\n }\n }\n }\n else if (tree[x].first == '-') {\n dp[x].first = 0;\n dp[x].second = 0;\n vector<pair<int,int>> temp;\n for (auto p : tree[x].second) {\n if (!visi[p]) {\n dfs(p);\n temp.push_back({dp[p].first,dp[p].second});\n }\n }\n assert(temp.size() >= 2);\n\n if (temp.size() == 2) {\n dp[x].first = max(temp[0].first-temp[1].second, temp[1].first-temp[0].second);\n dp[x].second = min(temp[0].second-temp[1].first, temp[1].second-temp[0].first);\n }\n else {\n dp[x].first = max(temp[0].first-temp[1].second-temp[2].second,max(temp[1].first-temp[0].second-temp[2].second,temp[2].first-temp[0].second-temp[1].second));\n dp[x].second = min(temp[0].second-temp[1].first-temp[2].first,min(temp[1].second-temp[0].first-temp[2].first,temp[2].second-temp[0].first-temp[1].first));\n }\n }\n else {\n dp[x].first = tree[x].first - '0';\n dp[x].second = dp[x].first;\n }\n}\n\n\nvoid solve() {\n now = 0;\n int n = s.length();\n tree.resize(n);\n tree.clear();\n int br = 0;\n int root1, root2 = 0;\n rep(i,n) {\n if (s[i] == '(') br++;\n if (s[i] == ')') br--;\n if ((s[i] == '+' || s[i] == '-') && br == 0) {\n root1 = root2;\n root2 = i;\n }\n }\n tree[now].first = s[root1];\n now++;\n al(0,root1,0);\n al(root1+1,root2,0);\n al(root2+1,n,0);\n\n // rep(i,now) {\n // cout << i << \" \" << tree[i].first << \": \";\n // for (auto x : tree[i].second) {\n // cout << x << ' ';\n // }\n // cout << endl;\n // }\n\n\n dp.resize(now);\n visi.resize(now);\n rep(i,now+1) {\n visi[i] = false;\n }\n\n dfs(0);\n\n // rep(i,now+1) {\n // cout << dp[i].first << ' ' << dp[i].second << endl;\n // }\n\n ans = dp[0].first;\n\n vs.resize(now);\n\n rep(i,now) {\n vs[i] = 1e9;\n }\n rdp.resize(now);\n df2 = 0;\n dfs2(0);\n\n cout << ans << endl;\n return;\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n while (cin >> s && s != \"-1\") {\n solve();\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 26904, "score_of_the_acc": -0.2177, "final_rank": 8 }, { "submission_id": "aoj_1650_10055517", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint N;\nvvi g;\nvi num;\n\nint id;\n\nvoid f(string A, int last){ // make_tree\n int THIS = id; id++;\n if (last != -1){\n g[last].push_back(THIS);\n }\n if (len(A) == 1){\n num[THIS] = A[0] - '0';\n return;\n }\n int p = 0;\n vc<string> child;\n char x;\n string nw = \"\";\n rep(i, len(A)){\n nw += A[i];\n if (A[i] == '(') p++;\n else if (A[i] == ')') p--;\n else if (p == 0 && !isdigit(A[i])){\n nw.pop_back();\n if (nw[0] == '(' && nw.back() == ')'){\n nw = nw.substr(1, len(nw) - 2);\n }\n x = A[i];\n f(nw, THIS);\n nw = \"\";\n }\n }\n if (nw[0] == '(' && nw.back() == ')'){\n nw = nw.substr(1, len(nw) - 2);\n }\n f(nw, THIS);\n if (x == '+') num[THIS] = -1;\n else if (x == '-') num[THIS] = -2;\n else{\n cout << \"No\" << endl;\n }\n}\n\nint ans;\n\nvc<pair<int, int>> dp0, dp1;\n\nvoid dfs0(int v){\n for (auto u : g[v]){\n dfs0(u);\n }\n if (v == 0){}\n else if (num[v] >= 0) dp0[v] = {num[v], num[v]};\n else if (num[v] == -1){\n dp0[v] = {0, 0};\n for (auto u : g[v]){\n dp0[v].first += dp0[u].first;\n dp0[v].second += dp0[u].second;\n }\n }else{\n dp0[v] = {0, 0};\n int ma = -inf, me = inf;\n for (auto u : g[v]){\n dp0[v].first -= dp0[u].second;\n ma = max(ma, dp0[u].first + dp0[u].second);\n dp0[v].second -= dp0[u].first;\n me = min(me, dp0[u].first + dp0[u].second);\n }\n dp0[v].first += ma;\n dp0[v].second += me;\n }\n}\n\nvoid dfs1(int v){\n if (v == 0){\n if (num[v] == -1){\n int nw = 0;\n for (auto u : g[v]) nw += dp0[u].first;\n ans = max(ans, nw);\n }else{\n int nw = 0, ma = -inf;\n for (auto u : g[v]){\n nw -= dp0[u].second;\n ma = max(ma, dp0[u].first + dp0[u].second);\n }\n ans = max(ans, nw + ma);\n }\n for (auto u : g[v]){\n if (num[v] == -1){\n dp1[u] = {0, 0};\n for (auto nu : g[v]) if (nu != u){\n dp1[u].first += dp0[nu].first;\n dp1[u].second += dp0[nu].second;\n }\n }else{\n dp1[u] = {0, 0};\n int ma = -inf, me = inf;\n for (auto nu : g[v]) if (nu != u){\n dp1[u].first -= dp0[nu].second;\n ma = max(ma, dp0[nu].first + dp0[nu].second);\n dp1[u].second -= dp0[nu].first;\n me = min(me, dp0[nu].first + dp0[nu].second);\n }\n dp1[u].first += ma;\n dp1[u].second += me;\n }\n dfs1(u);\n }\n }else if (len(g[v])){\n if (num[v] == -1){\n int nw = dp1[v].first;\n for (auto u : g[v]) nw += dp0[u].first;\n ans = max(ans, nw);\n }else{\n int nw = -dp1[v].second, ma = dp1[v].first + dp1[v].second;\n for (auto u : g[v]){\n nw -= dp0[u].second;\n ma = max(ma, dp0[u].first + dp0[u].second);\n }\n ans = max(ans, nw + ma);\n }\n for (auto u : g[v]){\n if (num[v] == -1){\n dp1[u] = dp1[v];\n for (auto nu : g[v]) if (nu != u){\n dp1[u].first += dp0[nu].first;\n dp1[u].second += dp0[nu].second;\n }\n }else{\n dp1[u] = {-dp1[v].second, -dp1[v].first};\n int ma = dp1[v].first + dp1[v].second, me = ma;\n for (auto nu : g[v]) if (nu != u){\n dp1[u].first -= dp0[nu].second;\n ma = max(ma, dp0[nu].first + dp0[nu].second);\n dp1[u].second -= dp0[nu].first;\n me = min(me, dp0[nu].first + dp0[nu].second);\n }\n dp1[u].first += ma;\n dp1[u].second += me;\n }\n dfs1(u);\n }\n }\n}\n\nvoid solve(string S){\n id = 0;\n N = 0;\n for (auto s : S) if (s != '(' && s != ')') N++;\n N--;\n g.clear();\n g.resize(N);\n num.resize(N);\n f(S, -1);\n\n // rep(i, N){\n // cout << i << \" : \";\n // for (auto x : g[i]) cout << x << \" \";\n // cout << endl;\n // }\n // debug(num);\n dp0.resize(N);\n dfs0(0);\n // rep(i, N){\n // cout << i << \" \" << dp0[i].first << \" \" << dp0[i].second << endl;\n // }\n ans = -inf;\n dp1.resize(N);\n dp1[0] = {0, 0};\n dfs1(0);\n // rep(i, N){\n // cout << i << \" \" << dp1[i].first << \" \" << dp1[i].second << endl;\n // }\n cout << ans << \"\\n\";\n}\n\nint main(){\n while (true){\n string S; cin >> S;\n if (S == \"-1\") break;\n solve(S);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 17076, "score_of_the_acc": -0.0857, "final_rank": 2 }, { "submission_id": "aoj_1650_9548665", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nvector<int> Root;\nvector<vector<int>> Child;\nvector<vector<int>> Ad;\nvector<int> Par;\nmap<pair<int,int>, pair<ll,ll>> ANS;\nstring S;\nint N;\n\npair<ll,ll> DFS(int X, int P) {\n if (ANS.count({X,P})) return ANS[{X,P}];\n if (Ad[X].size() == 1) {\n return ANS[{X,P}] = {(ll)(S[X]-'0'),(ll)(S[X]-'0')};\n }\n vector<pair<ll,ll>> Ret;\n rep(i,0,3) {\n if (Ad[X][i] == P) continue;\n Ret.push_back(DFS(Ad[X][i],X));\n }\n if (S[X] == '+') return ANS[{X,P}] = {Ret[0].first+Ret[1].first, Ret[0].second+Ret[1].second};\n return ANS[{X,P}] = {min(Ret[0].first-Ret[1].second,Ret[1].first-Ret[0].second),max(Ret[0].second-Ret[1].first,Ret[1].second-Ret[0].first)};\n}\n\nint MakeGraph(int L, int R, int P) {\n if (L==R) {\n Par[L] = P;\n return L;\n }\n int Dep = 0;\n int Ro = -1;\n rep(i,L,R+1) {\n if (S[i] == '(') Dep++;\n if (S[i] == ')') Dep--;\n if (Dep == 0 && (S[i] == '+' || S[i] == '-')) Ro = i;\n }\n if (Ro == -1) {\n return MakeGraph(L+1,R-1,P);\n }\n Child[Ro].push_back(MakeGraph(L,Ro-1,Ro));\n Child[Ro].push_back(MakeGraph(Ro+1,R,Ro));\n Par[Ro] = P;\n return Ro;\n}\n\nint main() {\nwhile(1) {\n cin >> S;\n if (S == \"-1\") return 0;\n N = S.size();\n Child.clear();\n Child.resize(N);\n Par.clear();\n Par.resize(N,-1);\n Ad.clear();\n Ad.resize(N);\n Root.clear();\n int Dep = 0;\n rep(i,0,N) {\n if (S[i] == '(') Dep++;\n if (S[i] == ')') Dep--;\n if (Dep == 0 && (S[i] == '+' || S[i] == '-')) Root.push_back(i);\n }\n Child[Root[0]].push_back(MakeGraph(0,Root[0]-1,Root[0]));\n Child[Root[0]].push_back(MakeGraph(Root[0]+1,Root[1]-1,Root[0]));\n Child[Root[0]].push_back(MakeGraph(Root[1]+1,N-1,Root[0]));\n rep(i,0,N) {\n if (Par[i] != -1) Ad[i].push_back(Par[i]);\n for(int C : Child[i]) Ad[i].push_back(C);\n }\n ANS.clear();\n ll MAX = -inf;\n rep(i,0,N) {\n if (Ad[i].size() == 3) {\n vector<pair<ll,ll>> Ret(3);\n rep(j,0,3) {\n Ret[j] = DFS(Ad[i][j], i);\n }\n vector<int> ID(3);\n iota(ALL(ID),0);\n do {\n if (S[i] == '+') chmax(MAX, Ret[ID[0]].second + Ret[ID[1]].second + Ret[ID[2]].second);\n if (S[i] == '-') chmax(MAX, Ret[ID[0]].second - Ret[ID[1]].first - Ret[ID[2]].first);\n } while(next_permutation(ALL(ID)));\n }\n }\n cout << MAX << endl;\n}\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 51952, "score_of_the_acc": -0.8685, "final_rank": 18 }, { "submission_id": "aoj_1650_9487559", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconstexpr ll INF = 1001001001001001001LL;\n\nint main() {\n while(1) {\n string s; cin >> s;\n if(s == \"-1\") break;\n vector<vector<int>> child;\n vector<int> par;\n vector<char> op;\n vector<ll> value;\n auto num = [&](int i, int p) -> int {\n int id = child.size();\n child.push_back({});\n par.push_back(p);\n op.push_back('_');\n value.push_back(s[i]-'0');\n return id;\n };\n auto exp = [&](auto&& self, int i, int p) -> pair<int, int> {\n int id = child.size();\n child.push_back({});\n par.push_back(p);\n op.push_back('_');\n value.push_back(-1);\n for(int j = 0; j < (p==-1 ? 3 : 2); j++) {\n if(j != 0) {\n op[id] = s[i++];\n }\n if(s[i] == '(') {\n auto [c, e] = self(self, i+1, id);\n child[id].push_back(c);\n i = e+1;\n } else {\n auto cid = num(i++, id);\n child[id].push_back(cid);\n }\n }\n return {id, i};\n };\n auto [_, e] = exp(exp, 0, -1);\n assert(_ == 0);\n assert(e == ssize(s));\n int n = child.size();\n vector<ll> dpmax(n, -INF), dpmin(n, INF);\n stack<int> stk;\n stk.push(~0);\n stk.push(0);\n while(!stk.empty()) {\n int u = stk.top(); stk.pop();\n if(u >= 0) {\n for(int v : child[u]) {\n if(v >= 0) {\n stk.push(~v);\n stk.push(v);\n }\n }\n } else {\n u = ~u;\n int nc = ssize(child[u]);\n if(nc == 0) {\n dpmax[u] = value[u];\n dpmin[u] = value[u];\n continue;\n }\n if(op[u] == '+') {\n dpmax[u] = 0, dpmin[u] = 0;\n for(int v : child[u]) {\n dpmax[u] += dpmax[v];\n dpmin[u] += dpmin[v];\n }\n } else if(op[u] == '-') {\n for(int c = 0; c < nc; c++) {\n int tmpmax = 0, tmpmin = 0;\n for(int i = 0; i < nc; i++) {\n int v = child[u][i];\n if(i == c) {\n tmpmax += dpmax[v];\n tmpmin += dpmin[v];\n } else {\n tmpmax -= dpmin[v];\n tmpmin -= dpmax[v];\n }\n }\n if(dpmax[u] < tmpmax) dpmax[u] = tmpmax;\n if(dpmin[u] > tmpmin) dpmin[u] = tmpmin;\n }\n } else assert(0);\n }\n }\n vector<ll> pardpmax(n, -INF), pardpmin(n, INF);\n stk.push(0);\n ll ans = -INF;\n while(!stk.empty()) {\n int u = stk.top(); stk.pop();\n if(value[u] != -1) continue;\n vector<ll> nearmax, nearmin;\n for(int c : child[u]) {\n nearmax.push_back(dpmax[c]);\n nearmin.push_back(dpmin[c]);\n }\n if(u != 0) {\n nearmax.push_back(pardpmax[u]);\n nearmin.push_back(pardpmin[u]);\n }\n if(op[u] == '+') {\n ll tmp = nearmax[0]+nearmax[1]+nearmax[2];\n if(ans < tmp) ans = tmp;\n for(int i = 0; i < ssize(child[u]); i++) {\n pardpmax[child[u][i]] = nearmax[(i+1)%3]+nearmax[(i+2)%3];\n pardpmin[child[u][i]] = nearmin[(i+1)%3]+nearmin[(i+2)%3];\n }\n } else if(op[u] == '-') {\n for(int i = 0; i < 3; i++) {\n ll tmp = nearmax[i] - nearmin[(i+1)%3] - nearmin[(i+2)%3];\n if(ans < tmp) ans = tmp;\n for(int i = 0; i < ssize(child[u]); i++) {\n pardpmax[child[u][i]] = max(nearmax[(i+1)%3]-nearmin[(i+2)%3], nearmax[(i+2)%3]-nearmin[(i+1)%3]);\n pardpmin[child[u][i]] = min(nearmin[(i+1)%3]-nearmax[(i+2)%3], nearmin[(i+2)%3]-nearmax[(i+1)%3]);\n }\n }\n } else assert(0);\n for(int c : child[u]) {\n stk.push(c);\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 21516, "score_of_the_acc": -0.0882, "final_rank": 3 }, { "submission_id": "aoj_1650_9458152", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nint main(){\n while(1){\n string S;cin>>S;\n if(S==\"-1\")return 0;\n ll N=sz(S);\n vvi E(N);\n vi P(N);\n ll r;\n {\n //構文解析パート\n stack<ll>st;\n vi right(N);\n REP(i,N){\n if(S[i]=='(')st.emplace(i);\n if(S[i]==')')right[st.top()]=i,st.pop();\n }\n ll t=1;\n if(S[0]=='(')t=right[0]+1;\n ll u=t+2;\n if(S[t+1]=='(')u=right[t+1]+1;\n function<ll(ll,ll)>dfs=[&](ll l,ll r){\n if(S[l]=='('){l++;r--;}\n if(r-l==1){return l;}\n ll t=l+1;\n if(S[l]=='(')t=right[l]+1;\n ll a=dfs(l,t),b=dfs(t+1,r);\n P[a]=P[b]=t;\n E[t].emplace_back(a);\n E[t].emplace_back(b);\n return t;\n };\n ll a=dfs(0,t),b=dfs(t+1,u),c=dfs(u+1,N);\n P[a]=P[b]=P[c]=t;\n E[t].emplace_back(a);\n E[t].emplace_back(b);\n E[t].emplace_back(c);\n r=t;\n }\n vi DP(N,1e18),EP(N,-1e18);\n function<void(ll)>dfs=[&](ll v){\n for(auto u:E[v])dfs(u);\n if(!sz(E[v]))DP[v]=EP[v]=S[v]-'0';\n else if(sz(E[v])==2){\n if(S[v]=='+'){\n DP[v]=DP[E[v][0]]+DP[E[v][1]];\n EP[v]=EP[E[v][0]]+EP[E[v][1]];\n }\n else{\n DP[v]=min(DP[E[v][0]]-EP[E[v][1]],-EP[E[v][0]]+DP[E[v][1]]);\n EP[v]=max(EP[E[v][0]]-DP[E[v][1]],-DP[E[v][0]]+EP[E[v][1]]);\n }\n }\n else{\n if(S[v]=='+'){\n DP[v]=DP[E[v][0]]+DP[E[v][1]]+DP[E[v][2]];\n EP[v]=EP[E[v][0]]+EP[E[v][1]]+EP[E[v][2]];\n }\n else{\n DP[v]=min({DP[E[v][0]]-EP[E[v][1]]-EP[E[v][2]],-EP[E[v][0]]+DP[E[v][1]]-EP[E[v][2]],-EP[E[v][0]]-EP[E[v][1]]+DP[E[v][2]]});\n EP[v]=max({EP[E[v][0]]-DP[E[v][1]]-DP[E[v][2]],-DP[E[v][0]]+EP[E[v][1]]-DP[E[v][2]],-DP[E[v][0]]-DP[E[v][1]]+EP[E[v][2]]});\n }\n }\n };\n dfs(r);\n vi FP(N,1e18),GP(N,-1e18);\n function<void(ll)>bfs=[&](ll v){\n if(!sz(E[v]))return;\n if(S[v]=='+'){\n FP[E[v][0]]=FP[v]+DP[E[v][1]];\n GP[E[v][0]]=GP[v]+EP[E[v][1]];\n FP[E[v][1]]=FP[v]+DP[E[v][0]];\n GP[E[v][1]]=GP[v]+EP[E[v][0]];\n }\n else{\n FP[E[v][0]]=min(FP[v]-EP[E[v][1]],-GP[v]+DP[E[v][1]]);\n GP[E[v][0]]=max(GP[v]-DP[E[v][1]],-FP[v]+EP[E[v][1]]);\n FP[E[v][1]]=min(FP[v]-EP[E[v][0]],-GP[v]+DP[E[v][0]]);\n GP[E[v][1]]=max(GP[v]-DP[E[v][0]],-FP[v]+EP[E[v][0]]);\n }\n bfs(E[v][0]);\n bfs(E[v][1]);\n };\n if(S[r]=='+'){\n FP[E[r][0]]=DP[E[r][1]]+DP[E[r][2]];\n GP[E[r][0]]=EP[E[r][1]]+EP[E[r][2]];\n FP[E[r][1]]=DP[E[r][0]]+DP[E[r][2]];\n GP[E[r][1]]=EP[E[r][0]]+EP[E[r][2]];\n FP[E[r][2]]=DP[E[r][0]]+DP[E[r][1]];\n GP[E[r][2]]=EP[E[r][0]]+EP[E[r][1]];\n }\n else{\n FP[E[r][0]]=min(DP[E[r][1]]-EP[E[r][2]],-EP[E[r][1]]+DP[E[r][2]]);\n GP[E[r][0]]=max(EP[E[r][1]]-DP[E[r][2]],-DP[E[r][1]]+EP[E[r][2]]);\n FP[E[r][1]]=min(DP[E[r][0]]-EP[E[r][2]],-EP[E[r][0]]+DP[E[r][2]]);\n GP[E[r][1]]=max(EP[E[r][0]]-DP[E[r][2]],-DP[E[r][0]]+EP[E[r][2]]);\n FP[E[r][2]]=min(DP[E[r][0]]-EP[E[r][1]],-EP[E[r][0]]+DP[E[r][1]]);\n GP[E[r][2]]=max(EP[E[r][0]]-DP[E[r][1]],-DP[E[r][0]]+EP[E[r][1]]);\n }\n bfs(E[r][0]);\n bfs(E[r][1]);\n bfs(E[r][2]);\n //REP(i,N)cout<<i<<\" \"<<FP[i]<<\" \"<<GP[i]<<endl;\n ll ans=EP[r];\n REP(i,N)if(i!=r&&sz(E[i])){\n if(S[i]=='+')chmax(ans,EP[i]+GP[i]);\n if(S[i]=='-'){\n chmax(ans,GP[i]-DP[i]);\n chmax(ans,FP[i]-(DP[E[i][0]]+DP[E[i][1]]));\n }\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 31476, "score_of_the_acc": -0.222, "final_rank": 9 } ]
aoj_1651_cpp
Handing out Balloons Each time an ICPC participant team has successfully solved a problem at the competition site, a balloon of a color specific to that problem is delivered to that team. You had thus prepared enough number of balloons of various colors for the case when all the teams solved all the problems, but many problems have been solved by few teams. You thus decided to give away the remaining balloons to the kids in the neighborhood. You are preparing poles, each tied down with one or more balloons of the same color. You are lining up these poles from left to right. Whenever a kid comes up, you give three balloons of different colors to the kid picking up one from each of the three leftmost poles. Poles that run out of the balloons are removed, and when the poles remaining are two or less, you finish giving balloons away. The number of kids you can give the balloons depends on the order of the poles. Find the maximum possible number of kids you can give the balloons by arranging the order of poles appropriately. The figure on the right depicts the consequences of different arrangements of five poles with 1, 2, 3, 4 and 5 balloons. When poles are lined up in the order of 1, 2, 3, 4 and 5 balloons, you can give to only three kids; when they are in the order of 5, 4, 3, 2 and 1 balloons, you can give to five kids. Input The input consists of multiple datasets, each in the following format. n b 1 b 2 … b n n is the number of poles. n is an integer between 3 and 50, inclusive. b 1 through b n are the numbers of the balloons on the poles. Each b i is a positive integer not exceeding 50. The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 50. Output For each dataset, output a single line containing the maximum number of kids you can give the balloons to. Sample Input 5 1 2 3 4 5 9 1 3 3 3 3 3 3 4 4 4 5 2 8 3 0 Output for the Sample Input 5 9 5
[ { "submission_id": "aoj_1651_10684431", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing B = bitset<2501>;\n\nvoid solve(int n) {\n vector<int> a(n);\n int sm = 0;\n for (auto &i: a) {\n cin >> i;\n sm += i;\n }\n vector<B> dp(2501);\n dp[0][0] = 1;\n for (int i = 0; i < n; i++) {\n for (int j = 2500; j >= 0; j--) {\n if (j + a[i] <= 2500)\n dp[j + a[i]] |= dp[j];\n dp[j] |= dp[j] << a[i];\n }\n }\n int ans = 0;\n for (int i = 0; i <= sm; i++) {\n for (int j = 0; j <= sm; j++) {\n if (dp[i][j]) {\n ans = max(ans, min(i, min(j, sm - i - j)));\n }\n }\n }\n cout << ans << \"\\n\";\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 4160, "score_of_the_acc": -0.0194, "final_rank": 2 }, { "submission_id": "aoj_1651_10674862", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ntemplate <typename T>\nbool chmin(T &x, T y) {\n return x > y ? x = y, true : false;\n}\n\ntemplate <typename T>\nbool chmax(T &x, T y) {\n return x < y ? x = y, true : false;\n}\n\n\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n const int M = 2500;\n vector<vector<bool>> dp(M + 1, vector<bool>(M + 1, false));\n dp[0][0] = true;\n int sum = 0;\n while (N--) {\n int a;\n cin >> a;\n sum += a;\n for (int i = M; i >= 0; i--) {\n for (int j = M; j >= 0; j--) {\n if (dp[i][j]) {\n if (i + a <= M) dp[i + a][j] = true;\n if (j + a <= M) dp[i][j + a] = true;\n }\n }\n }\n }\n int ans = 0;\n for (int a = 0; a <= M; a++) {\n for (int b = 0; b <= M; b++) {\n if (dp[a][b]) {\n ans = max(ans, min({a, b, sum - a - b}));\n }\n }\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 4950, "memory_kb": 4352, "score_of_the_acc": -0.6472, "final_rank": 14 }, { "submission_id": "aoj_1651_10671598", "code_snippet": "#line 1 \"2021D.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2021D.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(x)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\n//const int m = 1700;\n\n\nint solve(){\n int n; cin >> n;\n if(n==0)return 1;\n auto a = i64_vec_IN(n);\n int m = 0;\n rep(i,n) m += a[i];\n\n vector dp(m+1, vector(m+1, -1));\n vector ep(m+1,vector(m+1,-1));\n\n dp[0][0] = 0;\n rep(i,n) {\n rep(j,m+1) {\n rep(k,m+1) {\n if(dp[j][k] == -1) continue;\n \n chmax(ep[j+a[i]][k], dp[j][k]);\n chmax(ep[j][k+a[i]], dp[j][k]);\n chmax(ep[j][k], int(dp[j][k]+a[i]));\n }\n }\n dp=ep;\n fill(all(ep), vector(m+1,-1));\n }\n\n int ans = 0;\n rep(i,m+1) rep(j,m+1) {\n chmax(ans, min(int(i),min(int(j),dp[i][j])));\n }\n cout << ans << '\\n';\n return 0;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 740, "memory_kb": 42376, "score_of_the_acc": -0.5791, "final_rank": 11 }, { "submission_id": "aoj_1651_10671593", "code_snippet": "#line 1 \"2021D.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2021D.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(x)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nconst int m = 2500;\n\n\nvector dp(m+1, vector(m+1, -1));\nvector ep(m+1,vector(m+1,-1));\n\nint solve(){\n int n; cin >> n;\n if(n==0)return 1;\n auto a = i64_vec_IN(n);\n fill(all(dp), vector(m+1,-1));\n\n dp[0][0] = 0;\n rep(i,n) {\n rep(j,m+1) {\n rep(k,m+1) {\n if(dp[j][k] == -1) continue;\n \n chmax(ep[j+a[i]][k], dp[j][k]);\n chmax(ep[j][k+a[i]], dp[j][k]);\n chmax(ep[j][k], int(dp[j][k]+a[i]));\n }\n }\n dp=ep;\n fill(all(ep), vector(m+1,-1));\n }\n\n int ans = 0;\n rep(i,m+1) rep(j,m+1) {\n chmax(ans, min(int(i),min(int(j),dp[i][j])));\n }\n cout << ans << '\\n';\n return 0;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 6000, "memory_kb": 52608, "score_of_the_acc": -1.4131, "final_rank": 19 }, { "submission_id": "aoj_1651_10671524", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n vi b(n);\n rep(i,0,n)cin>>b[i];\n int sum=accumulate(all(b),0);\n const int m=50*50/3+10;\n auto dp(vector(m,vi(m,0)));\n dp[0][0]=true;\n rep(i,0,n){\n auto old(vector(m,vi(m,0)));\n swap(old,dp);\n rep(a,0,m)rep(c,0,m){\n if(a+b[i]<m)dp[a+b[i]][c]|=old[a][c];\n if(c+b[i]<m)dp[a][c+b[i]]|=old[a][c];\n dp[a][c]|=old[a][c];\n }\n }\n int ans=0;\n rep(a,0,m)rep(c,0,m)if(dp[a][c]){\n int d=sum-a-c;\n ans=max(ans,min({a,c,d}));\n }\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 8968, "score_of_the_acc": -0.2191, "final_rank": 6 }, { "submission_id": "aoj_1651_10667261", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < (ll)n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n\n\n\n\nint solve() {\n\tll n; cin >> n;\n\tif (n == 0) return 1;\n\n\tvll b(n); cin >> b;\n\tll sum = 0;\n\trep(i, n) sum += b[i];\n\n\tconst int SIZE = sum + 1;\n\tvvll dp(SIZE, vll(SIZE, INF));\n\tdp[0][0] = 0;\n\trep(i, n) {\n\t\tvvll old(SIZE, vll(SIZE, INF));\n\t\tswap(dp, old);\n\n\t\trep(j, SIZE) {\n\t\t\trep(k, SIZE) {\n\t\t\t\tif (0 <= j - b[i] && old[j - b[i]][k] != INF) dp[j][k] = old[j - b[i]][k];\n\t\t\t\tif (0 <= k - b[i] && old[j][k - b[i]] != INF) dp[j][k] = old[j][k - b[i]];\n\t\t\t\tif (old[j][k] != INF) {\n\t\t\t\t\tdp[j][k] = old[j][k] + b[i];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tll ans = 0;\n\tll mx = -INF;\n\tloop(i, 0, SIZE - 1) {\n\t\tloop(j, 0, SIZE - 1) {\n\t\t\tif (dp[i][j] == INF) continue;\n\t\t\tll tmp = min({i, j, dp[i][j]});\n\t\t\tif (chmax(mx, tmp)) {\n\t\t\t\tans = tmp;\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << ans << \"\\n\";\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2660, "memory_kb": 80892, "score_of_the_acc": -1.3347, "final_rank": 18 }, { "submission_id": "aoj_1651_10667177", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < (ll)n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n\n\n\n\nint solve() {\n\tll n; cin >> n;\n\tif (n == 0) return 1;\n\n\tvll b(n); cin >> b;\n\tll sum = 0;\n\trep(i, n) sum += b[i];\n\n\tconst int SIZE = sum + 1;\n\tvector<vector<bool>> dp(SIZE, vector<bool>(SIZE, false));\n\tdp[0][0] = true;\n\trep(i, n) {\n\t\tvector<vector<bool>> old(SIZE, vector<bool>(SIZE, false));\n\t\tswap(dp, old);\n\n\t\trep(j, SIZE) {\n\t\t\trep(k, SIZE) {\n\t\t\t\tif (0 <= j - b[i] && old[j - b[i]][k]) dp[j][k] = true;\n\t\t\t\tif (0 <= k - b[i] && old[j][k - b[i]]) dp[j][k] = true;\n\t\t\t\tif (old[j][k]) dp[j][k] = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tll ans = 0;\n\tll mx = -INF;\n\tloop(i, 0, SIZE - 1) {\n\t\tloop(j, 0, SIZE - 1) {\n\t\t\tif (!dp[i][j]) continue;\n\t\t\tll tmp = min({i, j, sum - (i + j)});\n\t\t\tif (chmax(mx, tmp)) {\n\t\t\t\tans = tmp;\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << ans << \"\\n\";\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2130, "memory_kb": 4348, "score_of_the_acc": -0.2712, "final_rank": 7 }, { "submission_id": "aoj_1651_10643932", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <fstream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n#include <climits>\n#include <cfloat>\n#include <cassert>\n#include <ctime>\n#include <cctype>\n#include <cwctype>\n#include <cstdint>\n#include <type_traits>\n#include <initializer_list>\n#include <utility>\n#include <bitset>\n#include <vector>\n#include <deque>\n#include <list>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <string>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <iterator>\n#include <tuple>\n#include <array>\n#include <new>\n#include <memory>\n#include <limits>\n#include <random>\n#include <exception>\n#include <stdexcept>\n#include <regex>\n#include <complex>\n#include <chrono>\n#include <future>\n#include <thread>\n#include <mutex>\n#include <atomic>\n#include <atcoder/all>\n// #include <boost/multiprecision/cpp_int.hpp>\nusing namespace std;\nusing namespace atcoder;\n// using mint = modint;\nusing mint = modint998244353;\n// using mint = modint1000000007;\n// using namespace boost::multiprecision;\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing T3 = tuple<int, int, int>;\nusing G = vector<vector<int>>;\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rep2(i, a, b) for (ll i = a; i < (b); ++i)\n#define rrep2(i, a, b) for (ll i = a-1; i >= (b); --i)\n#define rep3(i, a, b, c) for (ll i = a; i < (b); i+=c)\n#define rng(a) a.begin(),a.end()\n#define rrng(a) a.rbegin(),a.rend()\n#define popcount __builtin_popcount\n#define popcount_ll __builtin_popcountll\n#define fi first\n#define se second\n#define UNIQUE(v) sort(rng(v)), v.erase(unique(rng(v)), v.end())\n#define MIN(v) *min_element(rng(v))\n#define MAX(v) *max_element(rng(v))\n#define SUM(v) accumulate(rng(v),0LL)\n#define IN(v, x) (find(rng(v),x) != v.end())\ntemplate<class T> bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void printv(vector<T> &v){rep(i,v.size())cout<<v[i]<<\" \\n\"[i==v.size()-1];}\ntemplate<class T> void printvv(vector<vector<T>> &v){rep(i,v.size())rep(j,v[i].size())cout<<v[i][j]<<\" \\n\"[j==v[i].size()-1];cout<<endl;}\nconst ll dx[] = {-1, 0, 1, 0};\nconst ll dy[] = {0, 1, 0, -1};\nconst ll dxx[] = {-1, -1, 0, 1, 1, 1, 0, -1};\nconst ll dyy[] = {0, 1, 1, 1, 0, -1, -1, -1};\nconst ll LINF = 3001002003004005006ll;\nconst int INF = 1001001001;\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(1){\n int n; cin >> n;\n if (n == 0) return 0;\n vector<int> x(n);\n rep(i, n) cin >> x[i];\n vector<vector<bool>> dp(2510, vector<bool>(2510, false));\n dp[0][0] = true;\n rep(i, n){\n vector<vector<bool>> old(2510, vector<bool>(2510, false)); swap(old, dp);\n rep(a, 2501)rep(b, 2501){\n if (old[a][b] == false) continue;\n dp[a+x[i]][b] = true;\n dp[a][b+x[i]] = true;\n dp[a][b] = true;\n }\n }\n int ans = 0;\n int sum = SUM(x);\n rep(a, 2501)rep(b, 2501){\n if (dp[a][b] == false) continue;\n int c = sum-a-b; \n chmax(ans, min({(int)a, (int)b, c}));\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4520, "memory_kb": 5248, "score_of_the_acc": -0.6015, "final_rank": 13 }, { "submission_id": "aoj_1651_10643889", "code_snippet": "#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n#define fast std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr)\n#define eb emplace_back\n#define mp make_pair\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\nusing ll = long long;\nusing u64 = uint64_t;\nusing u128 = __uint128_t;\nusing ull = unsigned long long;\nconstexpr ll inf = 2e18;\nconstexpr ll mod = 998244353;\n#ifdef LOCAL\n#define debug(x) std::cerr << #x << \" = \" << (x) << std::endl;\n#else\n#define debug(x)\n#endif\nstatic void judge(bool c) {\n std::cout << (c ? \"Yes\" : \"No\") << '\\n';\n}\nll ppow(ll a, ll b, ll m) {\n ll res = 1;\n a %= m;\n while (b > 0) {\n if (b & 1) res = (res * a) % m;\n a = (a * a) % m;\n b >>= 1;\n }\n return res;\n}\n\n\nint n,b[50],dp[2501][2501];\nint main() {\n fast;\n while(1){\n cin >> n;\n if(n == 0){\n return 0;\n }\n int sum = 0;\n \n for(int i = 0; i < n; i++){\n cin >> b[i];\n sum += b[i];\n }\n for(int i = 0; i <= 2500; i++){\n for(int j = 0; j <= 2500; j++){\n dp[i][j] = 0;\n }\n }\n dp[0][0] = 1; \n for(int i = 0; i < n; i++){\n for(int j = 2500; j >= 0; j--){\n for(int k = 2500; k >= 0; k--){\n if(dp[j][k]){\n dp[j + b[i]][k] = 1;\n dp[j][k + b[i]] = 1;\n }\n }\n }\n }\n int ans = -1;\n for(int i = 0; i <= 2500; i++){\n for(int j = 0; j <= 2500; j++){\n if(dp[i][j]) ans = max(ans,min(i,min(j,sum - i - j)));\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 2940, "memory_kb": 27812, "score_of_the_acc": -0.6835, "final_rank": 15 }, { "submission_id": "aoj_1651_10643284", "code_snippet": "#include <bits/extc++.h>\n\n#include <atcoder/all>\n\nusing mint = atcoder::modint1000000007;\n\nusing ll = long long;\n\nusing pii = std::pair<int, int>;\nusing pil = std::pair<int, ll>;\n\ntemplate <class T>\nusing pq_rev = std::priority_queue<T, std::vector<T>, std::greater<>>;\n\ntemplate <class T>\nbool chmax(T& x, const T& v) {\n if (x < v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\ntemplate <class T>\nbool chmin(T& x, const T& v) {\n if (x > v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\n\nbool solve() {\n int N;\n std::cin >> N;\n\n if (N == 0) {\n return false;\n }\n\n std::vector<int> A(N);\n for (auto& a : A) {\n std::cin >> a;\n }\n\n int K = std::accumulate(A.begin(), A.end(), 0);\n\n std::vector dp(K + 1, std::vector(K + 1, false));\n\n dp[0][0] = true;\n\n for (int i = 0; i < N; i++) {\n auto next = dp;\n\n for (int x = 0; x <= K; x++) {\n for (int y = 0; y <= K; y++) {\n if (x + A[i] <= K) {\n next[x + A[i]][y] = next[x + A[i]][y] || dp[x][y];\n }\n if (y + A[i] <= K) {\n next[x][y + A[i]] = next[x][y + A[i]] || dp[x][y];\n }\n }\n }\n\n std::swap(next, dp);\n }\n\n int ans = 0;\n\n for (int x = 0; x <= K; x++) {\n for (int y = 0; y <= K; y++) {\n if (dp[x][y]) {\n chmax(ans, std::min({x, y, K - x - y}));\n }\n }\n }\n\n std::cout << ans << '\\n';\n\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 2820, "memory_kb": 4736, "score_of_the_acc": -0.3682, "final_rank": 8 }, { "submission_id": "aoj_1651_10642803", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &a){\n\tif (a.empty()) return os;\n\tos << a.front();\n\tfor (auto e : a | views::drop(1)){\n\t\tos << ' ' << e;\n\t}\n\treturn os;\n}\nvoid dump(auto... vs) {\n\t((cout << vs << ' '), ...) << endl;\n}\n\n\nvoid solve(ll N) {\n\tvector<ll> A(N);\n\trep(i,N)cin>>A[i];\n\tll sum=0;\n\trep(i,N){\n\t\tsum+=A[i];\n\t}\n\tll L=(sum/2)+20;\n\tvector<vector<bool>> dp(L,vector<bool> (L,false));\n\tdp[0][0]=true;\n\tll now=0;\n\trep(i,N){\n\t\tvector<vector<bool>> ndp(L,vector<bool> (L,false));\n\t\trep(j,L){\n\t\t\tfor (ll k=j;k<L;k++){\n\t\t\t\tif (!dp[j][k])continue;\n\t\t\t\tll l=now-j-k;\n\t\t\t\tif (l<k)break;\n\t\t\t\tndp[j][k]=true;\n\t\t\t\tll nk=k+A[i];\n\t\t\t\tif (nk<=l){\n\t\t\t\t\tndp[j][nk]=true;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tndp[j][l]=true;\n\t\t\t\t}\n\t\t\t\tll nj=j+A[i];\n\t\t\t\tif (nj<=k){\n\t\t\t\t\tndp[nj][k]=true;\n\t\t\t\t}\n\t\t\t\telse if (nj<=l){\n\t\t\t\t\tndp[k][nj]=true;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tndp[k][l]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tnow+=A[i];\n\t\tswap(dp,ndp);\n\t}\n\tll ans=0;\n\trep(i,L){\n\t\trep(j,L){\n\t\t\tll k=sum-i-j;\n\t\t\tif (dp[i][j]){\n\t\t\t\tchmax(ans,min(i,min(j,k)));\n\t\t\t}\n\t\t}\n\t}\n\tcout<<ans<<endl;\n return;\n}\n\n\nint main() {\n while (true){\n\t\tll N;\n\t\tcin>>N;\n\t\tif (N==0)break;\n solve(N);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3968, "score_of_the_acc": -0.0022, "final_rank": 1 }, { "submission_id": "aoj_1651_10642674", "code_snippet": "// MARK: - コード\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n\nconst ll MOD = 1000000007;\n// const ll MOD = 998244353;\nconst int dx[8] = {0, -1, 1, 0, -1, 1, -1, 1};\nconst int dy[8] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &v)\n{\n for (auto &e : v)\n is >> e;\n return is;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v)\n{\n for (auto &e : v)\n os << e << ' ';\n return os;\n}\n\n#define reps(i, l, r) for (std::decay_t<decltype(r)> i##_right = (r), i = (l); i < i##_right; i++)\n#define rep(i, n) reps(i, 0, n)\n\ntemplate <class T>\ninline bool chmax(T &a, T &b)\n{\n if (a < b)\n {\n a = b;\n return true;\n }\n else\n return false;\n}\ntemplate <class T>\ninline bool chmin(T &a, T &b)\n{\n if (a > b)\n {\n a = b;\n return true;\n }\n else\n return false;\n}\n\nint main()\n{\n while (true)\n {\n int N;\n cin >> N;\n if (N == 0)\n break;\n\n vector<int> A(N);\n rep(i, N) cin >> A[i];\n int sum = 0;\n rep(i, N) sum += A[i];\n\n vector<vector<bool>> DP(sum + 1, vector<bool>(sum + 1, false));\n DP[0][0] = true;\n rep(i, N)\n {\n for (int a = sum; a >= 0; a--)\n {\n for (int b = sum; b >= 0; b--)\n {\n if (DP[a][b])\n {\n DP[a + A[i]][b] = true;\n DP[a][b + A[i]] = true;\n }\n }\n }\n }\n int ans = 0;\n rep(a, sum + 1)\n {\n rep(b, sum + 1)\n {\n if (DP[a][b])\n {\n ans = max(ans, min(a, min(b, sum - a - b)));\n }\n }\n }\n cout << ans << endl;\n }\n}\n/*\nMARK: - メモ\n\n\n\n*/", "accuracy": 1, "time_ms": 900, "memory_kb": 3796, "score_of_the_acc": -0.1, "final_rank": 5 }, { "submission_id": "aoj_1651_10642517", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<int>B(N);\n rep(i,0,N)cin>>B[i];\n vector dp(1,vector<bool>(1));\n dp[0][0]=true;\n int sum=0;\n rep(i,0,N){\n sum+=B[i];\n vector nxt(sum+1,vector<bool>(sum+1));\n rep(j,0,sum-B[i]+1)rep(k,0,sum-B[i]+1){\n if(!dp[j][k])continue;\n nxt[j+B[i]][k]=true;\n nxt[j][k+B[i]]=true;\n nxt[j][k]=true;\n }\n swap(dp,nxt);\n }\n int ans=0;\n rep(i,0,sum+1)rep(j,0,sum+1){\n if(dp[i][j]){\n chmax(ans,min(i,min(j,sum-i-j)));\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 4864, "score_of_the_acc": -0.0499, "final_rank": 3 }, { "submission_id": "aoj_1651_10642448", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\n#define all(v) v.begin(), v.end()\n#define rall(a) a.rbegin(), a.rend()\n#define fi first\n#define se second\n#define endl \"\\n\"\n\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\n\ntemplate <typename T>\nusing vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます\ntemplate <typename T>\nusing vv = vc<vc<T>>;\n#define int long long\nusing ll = long long;\nll INF = 2e18;\n\n#ifdef ONLINE_JUDGE\n// ジャッジ環境ではデバッグマクロを無効化\n#define debug_x(x) \\\n do \\\n { \\\n } while (0)\n#define debug_vc(v) \\\n do \\\n { \\\n } while (0)\n#define debug_vv(v) \\\n do \\\n { \\\n } while (0)\n#else\n// ローカルではデバッグ出力\n#define debug_x(x) cout << #x << \" = \" << (x) << endl\n#define debug_vc(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) cout << v[i] << ' '; \\\n cout << endl; \\\n } // 一次元配列を出力する\n#define debug_vv(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) \\\n { \\\n rep(j, size(v[i])) { cout << v[i][j] << ' '; } \\\n cout << endl; \\\n } \\\n } // 二次元配列を出力する\n#endif\n\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = 1; i <= (n); ++i)\n#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define nfor(i, s, n) for (ll i = s; i < n; i++) // i=s,s+1...n-1 ノーマルfor\n#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる\n\nusing ld = long double;\nusing bl = bool;\n\ntemplate <class T>\nusing pq = priority_queue<T, vc<T>>; // 大きい順\ntemplate <class T>\nusing pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順\n\nusing vl = vc<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vv<vl>;\nusing vvvvl = vv<vvl>;\nusing vs = vc<string>;\nusing vvs = vv<string>;\nusing vb = vc<bl>;\nusing vvb = vv<bl>;\nusing vvvb = vv<vb>;\nusing vld = vc<ld>;\nusing vvld = vv<ld>;\nusing vvvld = vv<vld>;\nusing P = pair<ll, ll>;\nusing vP = vc<P>;\nusing vvP = vc<vP>;\nusing vvvP = vv<vP>;\n\n#define pb push_back\n\n#define YES cout << \"Yes\" << endl\n#define NO cout << \"No\" << endl\n#define YN \\\n { \\\n cout << \"Yes\" << endl; \\\n } \\\n else \\\n { \\\n cout << \"No\" << endl; \\\n } // if(a==b)YN;\n#define dame cout << -1 << endl\n\nstring dir = \"DRUL\"; // 第4象限の場合\nvl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};\nvl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};\n\nbool out_grid(ll i, ll j, ll h, ll w)\n{ // trueならcontinue\n return (!(0 <= i && i < h && 0 <= j && j < w));\n}\n\nll n;\nvl b;\n\nvoid gutyoku()\n{\n vl p(n);\n iota(all(p), 0);\n ll ans = -1;\n vl maxP;\n do\n {\n ll cnt = 0;\n vl tmp = b;\n rep(i, n - 2)\n {\n ll dec = min(min(tmp[p[i]], tmp[p[i + 1]]), tmp[p[i + 2]]);\n cnt += dec;\n rep(j, 3)\n {\n tmp[p[i + j]] -= dec;\n }\n if (tmp[p[i + 1]] > tmp[p[i + 2]])\n swap(tmp[p[i + 1]], tmp[p[i + 2]]);\n if (tmp[p[i]] > tmp[p[i + 1]])\n swap(tmp[p[i]], tmp[p[i + 1]]);\n if (tmp[p[i + 1]] > tmp[p[i + 2]])\n swap(tmp[p[i + 1]], tmp[p[i + 2]]);\n }\n if (cnt == 9)\n {\n cout << \"b:\" << endl;\n rep(i, n)\n {\n cout << b[p[i]] << ' ';\n }\n cout << endl;\n cout << \"mod:\" << endl;\n rep(i, n)\n {\n cout << b[p[i]] % 3 << ' ';\n }\n cout << endl;\n }\n } while (next_permutation(all(p)));\n // debug_x(ans);\n}\n\nvoid solve()\n{\n b.resize(n);\n ll sum = 0;\n rep(i, n)\n {\n cin >> b[i];\n sum += b[i];\n }\n // gutyoku();\n // dp[k][x][y] = \"{b1, ..., bk} を 3 分割して和が (x, y, (Σki=1 bi)-x-y) となるようにできるか否か\"\n vvvb dp(n + 1, vvb(sum + 1, vb(sum + 1)));\n dp[0][0][0] = true;\n rep(i, n)\n {\n rep(j, sum + 1)\n {\n rep(k, sum + 1)\n {\n if (dp[i][j][k])\n {\n dp[i + 1][j + b[i]][k] = true;\n dp[i + 1][j][k + b[i]] = true;\n dp[i + 1][j][k] = true;\n }\n }\n }\n }\n ll ans = 0;\n rep(j,sum+1){\n rep(k,sum+1){\n if(dp[n][j][k]){\n ans = max(ans,min(j,min(k,sum-(j+k))));\n }\n }\n }\n cout << ans << endl;\n return;\n}\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true)\n {\n cin >> n;\n if (n == 0)\n break;\n solve();\n }\n return 0;\n}\n\n/*\nshift+alt+Fで成形\nf2で変数名変更\n\nスニペット\nbasic\n m:atcoder以外用\n ma:ABC/ARC用\n mahc:AHC用\nmath\n floor:床関数\n ceil:天井関数\n comv:二項係数\n modpow: a^b mod m\n GCD:最大公約数\n extGCD:拡張ユークリッド互除法\n PFD:素因数分解\n isprime:エラトステネスの篩\n matrixpow:行列累乗\ndata_structure\n seg:セグメント木\ntechnic\n Compress:座標圧縮\n runlength:ランレングス圧縮\ngraph\n warshall:ワーシャルフロイド法\n dfs:深さ優先探索\n bfs:幅優先探索\n LCA:最近共通祖先\n*/", "accuracy": 1, "time_ms": 1300, "memory_kb": 37964, "score_of_the_acc": -0.5965, "final_rank": 12 }, { "submission_id": "aoj_1651_10624712", "code_snippet": "#line 2 \"/home/nixos/workspace/CompPro-Make/library/KowerKoint/stl-expansion.hpp\"\n#include <bits/stdc++.h>\n\ntemplate <typename T1, typename T2>\nstd::istream& operator>>(std::istream& is, std::pair<T1, T2>& p) {\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& is, std::array<T, N>& a) {\n for (size_t i = 0; i < N; ++i) {\n is >> a[i];\n }\n return is;\n}\ntemplate <typename T>\nstd::istream& operator>>(std::istream& is, std::vector<T>& v) {\n for (auto& e : v) is >> e;\n return is;\n}\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& os, const std::array<T, N>& a) {\n for (size_t i = 0; i < N; ++i) {\n os << a[i] << (i + 1 == a.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\n#line 3 \"/home/nixos/workspace/CompPro-Make/library/KowerKoint/base.hpp\"\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define END(...) { print(__VA_ARGS__); return; }\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing ull = unsigned long long;\nusing VUL = vector<ull>;\nusing VVUL = vector<VUL>;\nusing VVVUL = vector<VVUL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef DEBUG\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#else\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#endif\n\ntemplate<typename T>\nvector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {\n vector<vector<T>> res;\n vector<T> cur;\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(*it);\n }\n res.push_back(cur);\n return res;\n}\n\nvector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {\n vector<string> res;\n string cur = \"\";\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(*it);\n }\n res.push_back(cur);\n return res;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename It>\nauto rle(It begin, It end) {\n vector<pair<typename It::value_type, int>> res;\n if(begin == end) return res;\n auto pre = *begin;\n int num = 1;\n for(auto it = begin + 1; it != end; it++) {\n if(pre != *it) {\n res.emplace_back(pre, num);\n pre = *it;\n num = 1;\n } else num++;\n }\n res.emplace_back(pre, num);\n return res;\n}\n\ntemplate <typename It>\nvector<pair<typename It::value_type, int>> rle_sort(It begin, It end) {\n vector<typename It::value_type> cloned(begin, end);\n sort(ALL(cloned));\n auto e = rle(ALL(cloned));\n sort(ALL(e), [](const auto& l, const auto& r) { return l.second < r.second; });\n return e;\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n#line 2 \"main.cpp\"\n\nint simulate(const vector<int>& b){\n vector<int> v;\n v.push_back(b[0]);\n v.push_back(b[1]);\n v.push_back(b[2]);\n int ans = 0;\n int nxt = 3;\n while(ssize(v) == 3) {\n int mn = *ranges::min_element(v);\n ans += mn;\n vector<int> nv;\n for(int i = 0; i < 3; i++) {\n v[i] -= mn;\n if(v[i] > 0) nv.push_back(v[i]);\n }\n while(nxt < ssize(b) && ssize(nv) < 3) {\n nv.push_back(b[nxt++]);\n }\n v.swap(nv);\n }\n return ans;\n}\n\nint solve(const vector<int>& b) {\n int n = ssize(b);\n constexpr int SUM = 50*50;\n vector<bitset<SUM+1>> dp(SUM+1);\n dp[0][0] = 1;\n REP(i, n) {\n vector<bitset<SUM+1>> ndp(SUM+1);\n REP(j, SUM+1) {\n if(j+b[i] <= SUM) ndp[j+b[i]] |= dp[j];\n ndp[j] |= dp[j] << b[i];\n ndp[j] |= dp[j];\n }\n dp.swap(ndp);\n }\n int ans = 0;\n int sumb = accumulate(ALL(b), 0);\n REP(i, SUM+1) REP(j, SUM+1) {\n if(dp[i][j]) chmax(ans, min(min(i, j), sumb-i-j));\n }\n return ans;\n}\n\nvoid expr() {\n mt19937 mt(0);\n while(1) {\n cout << \"expr\" << endl;\n int n = mt() % 4 + 3;\n vector<int> b(n);\n for(int i = 0; i < n; i++) {\n b[i] = mt() % 10;\n }\n ranges::sort(b);\n int mx = 0;\n vector<int> best;\n do {\n if(chmax(mx, simulate(b))) {\n best = b;\n }\n } while(next_permutation(ALL(b)));\n ranges::sort(b | views::reverse);\n if(mx != solve(b)) {\n for(int i = 0; i < n; i++) {\n cout << ' ' << best[i];\n }\n cout << endl;\n cout << mx << endl;\n cout << solve(b) << endl;\n return;\n }\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n while(1) {\n int n; cin >> n;\n if(n==0) return 0;\n vector<int> b(n);\n for(int i = 0; i < n; i++) cin >> b[i];\n cout << solve(b) << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 4992, "score_of_the_acc": -0.0528, "final_rank": 4 }, { "submission_id": "aoj_1651_10607497", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int N;cin>>N;\n if (N==0) return 0;\n vi B(N);\n rep(i,0,N)cin>>B[i];\n const int M=50*(N+1);\n vector dp(M,vector<int>(M));\n dp[0][0]=true;\n int bsum=0;\n rep(bi,0,N){\n vector old(M,vector<int>(M));\n swap(old,dp);\n rep(i,0,M)rep(j,0,M){\n if(i+B[bi]<M)dp[i+B[bi]][j] |= old[i][j];\n if(j+B[bi]<M)dp[i][j+B[bi]] |= old[i][j];\n dp[i][j] |=old[i][j];\n }\n bsum+=B[bi];\n }\n int ans=0;\n rep(i,0,M)rep(j,0,M){\n if(dp[i][j]){\n ans=max(ans,min({i,j,bsum-i-j}));\n }\n }\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 7650, "memory_kb": 54364, "score_of_the_acc": -1.6559, "final_rank": 20 }, { "submission_id": "aoj_1651_10604321", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(int i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(int i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\n\n\nvoid solve(ll &n) {\n vector<int> b(n);\n rep(i, 0, n) cin >> b[i];\n\n int maxsum = n * 50;\n vector<vector<bool>> dp(maxsum + 1, vector<bool>(maxsum + 1));\n dp[0][0] = true;\n rep(i, 0, n) {\n vector<vector<bool>> ndp(maxsum + 1, vector<bool>(maxsum + 1));\n rep(sum1, 0, maxsum + 1) {\n rep(sum2, 0, maxsum + 1) {\n if(!dp[sum1][sum2]) continue;\n\n ndp[sum1 + b[i]][sum2] = true;\n ndp[sum1][sum2 + b[i]] = true;\n ndp[sum1][sum2] = true;\n }\n }\n\n swap(ndp, dp);\n }\n\n int ans = 0;\n int sumb = accumulate(all(b), 0);\n rep(sum1, 0, maxsum + 1) {\n rep(sum2, 0, maxsum + 1) {\n if(!dp[sum1][sum2]) continue;\n\n chmax(ans, min({sum1, sum2, sumb - (sum1 + sum2)}));\n }\n }\n\n cout << ans << endl;\n}\n\nint main() {\n ll n;\n while(true) {\n cin >> n;\n if(n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 3320, "memory_kb": 5012, "score_of_the_acc": -0.4384, "final_rank": 9 }, { "submission_id": "aoj_1651_10568200", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 1000001000;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\nvoid solve(int N) {\n vector<int>B(N);\n int MX = 0;\n for (int i=0; i<N; i++) {\n cin >> B[i];\n MX += B[i];\n }\n\n vector<vector<vector<bool>>>dp(N+1, vector(MX+1, vector(MX+1, false)));\n // dp[i][x][y] := [0,i)を見て、チーム1の総和がx,チーム2の総和がyとなるように和を分解することができるか\n\n dp[0][0][0] = true;\n\n for (int i=0; i<N; i++) {\n for (int x=0; x<=MX; x++) {\n for (int y=0; y<=MX; y++) {\n if (!dp[i][x][y]) continue;\n int b = B[i];\n // teamAにjoinさせる\n dp[i+1][x+b][y] = true;\n\n // teamBにjoinさせる\n dp[i+1][x][y+b] = true;\n \n // A,Bにjoinさせない\n dp[i+1][x][y] = true;\n }\n }\n }\n\n int ans = 0;\n\n for (int x=0; x<=MX; x++) {\n for (int y=0; y<=MX; y++) {\n int z = MX - x - y;\n if (dp[N][x][y]) chmax(ans, min(x,min(y,z)));\n }\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 38404, "score_of_the_acc": -0.5569, "final_rank": 10 }, { "submission_id": "aoj_1651_10555566", "code_snippet": "//#pragma GCC target(\"avx2\")\n//#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pli = pair<ll,int>;\n#define AMARI 998244353\n//#define AMARI 1000000007\n#define el '\\n'\n#define El '\\n'\n#define YESNO(x) ((x) ? \"Yes\" : \"No\")\n#define YES YESNO(true)\n#define NO YESNO(false)\n#define REV_PRIORITY_QUEUE(tp) priority_queue<tp,vector<tp>,greater<tp>>\n#define EXIT_ANS(x) {cout << (x) << '\\n'; return;}\ntemplate <typename T> void inline SORT(vector<T> &v){sort(v.begin(),v.end()); return;}\ntemplate <typename T> void inline VEC_UNIQ(vector<T> &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}\ntemplate <typename T> T inline MAX(vector<T> &v){return *max_element(v.begin(),v.end());}\ntemplate <typename T> T inline MIN(vector<T> &v){return *min_element(v.begin(),v.end());}\ntemplate <typename T> T inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}\ntemplate <typename T> void inline DEC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;}\ntemplate <typename T> void inline INC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;}\nvoid inline TEST(void){cerr << \"TEST\" << endl; return;}\ntemplate <typename T> bool inline chmin(T &x,T y){\n if(x > y){\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T> bool inline chmax(T &x,T y){\n if(x < y){\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T = long long> vector<T> inline get_vec(int n){\n vector<T> ans(n);\n for(int i = 0; i < n; i++)cin >> ans[i];\n return ans;\n}\ntemplate <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){\n int n = (int)vec.size();\n for(int i = 0; i < n; i++){\n cout << vec[i];\n if(kaigyou || i == n - 1)cout << '\\n';\n else cout << ' ';\n }\n if(!n)cout << '\\n';\n return;\n}\ntemplate <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){\n int n = (int)vec.size();\n for(int i = 0; i < n; i++){\n cerr << vec[i];\n if(kaigyou || i == n - 1)cerr << '\\n';\n else cerr << ' ';\n }\n if(!n)cerr << '\\n';\n return;\n}\nvector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false){\n if(m == -1)m = n - 1;\n vector<vector<int>> g(n);\n while(m--){\n int u,v;\n cin >> u >> v;\n u--; v--;\n g[u].push_back(v);\n if(!direct)g[v].push_back(u);\n }\n return g;\n}\n\nvector<int> make_inv(vector<int> p){\n int n = (int)p.size();\n vector<int> ans(n);\n for(int i = 0; i < n; i++)ans[p[i]] = i;\n return ans;\n}\n\n#define MULTI_TEST_CASE false\nvoid solve(void){\n ll n;\n cin >> n;\n if(n == 0){exit(0);}\n vector<ll> a(n);\n for(int i=0;i<n;i++){\n cin >> a[i];\n }\n ll m = 50;\n vector<vector<int>> dp(n*m+1,vector<int>(n * m + 1,0));\n dp[0][0] = 1;\n ll s = 0;\n for(int i=0;i<n;i++){\n s += a[i];\n vector<vector<int>> ndp(n*m+1,vector<int>(n * m + 1,0));\n for(int j=0;j<n*m+1;j++){\n for(int k=0;k<n*m+1;k++){\n if(dp[j][k] == 0){continue;}\n ndp[j][k] |= dp[j][k];\n if(j + a[i] <= n*m){\n ndp[j+a[i]][k] |= dp[j][k];\n }\n if(k + a[i] <= n*m){\n ndp[j][k+a[i]] |= dp[j][k];\n }\n }\n }\n swap(dp,ndp);\n }\n ll ans = 0;\n for(ll i=0;i<n*m+1;i++){\n for(ll j=0;j<n*m+1;j++){\n ll k =s - i - j;\n if(k < 0){continue;}\n if(dp[i][j]){\n ans = max(ans,min(i,min(j,k)));\n } \n }\n }\n cout << ans << endl;\n return;\n}\n\nvoid calc(void){\n return;\n}\n\nsigned main(void){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n calc();\n int t = 1;\n if(MULTI_TEST_CASE)cin >> t;\n while(1){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3470, "memory_kb": 52508, "score_of_the_acc": -1.0745, "final_rank": 17 }, { "submission_id": "aoj_1651_10406997", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace atcoder;\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\n\n\nint main(){\n while(1){\n LL(n);\n if(n == 0){break;}\n VLL(a,n);\n auto solve = [&](){\n ll z = 0;\n for(ll x:a){\n z += x;\n }\n vector<vector<bitset<2501>>> dp(n+1,vector<bitset<2501>>(2501));\n dp[0][0][0] = 1;\n rep(i,n){\n rep(j,n*50+1){\n rep(k,n*50+1){\n if(dp[i][j][k] == 0){continue;}\n if(j+a[i] < n*50+1){\n dp[i+1][j+a[i]][k] = 1;\n }\n if(k+a[i] < n*50+1){\n dp[i+1][j][k+a[i]] = 1;\n }\n dp[i+1][j][k] = 1;\n }\n }\n }\n ll ans = 0;\n rep(i,n*50+1){\n rep(j,n*50+1){\n if(dp[n][i][j] == 1){\n ans = max(ans,min(i,min(j,z-i-j)));\n }\n \n }\n }\n print(ans);\n };\n solve();\n }\n}", "accuracy": 1, "time_ms": 2640, "memory_kb": 44896, "score_of_the_acc": -0.8651, "final_rank": 16 } ]
aoj_1652_cpp
Time is Money The city of ICPC has made excessive pedestrian separation, making transportation in the city quite complicated. Citizens of the ICPC city thus have big concern on efficient transportation to save time. The residents are eager to reach their destinations as fast as possible by repeating taxi rides on driveways and walks on footpaths. Many cabstands are in the city. Footpaths and/or driveways directly connect some of the cabstand pairs allowing both-way traffic. Some pairs of cabstands may have no direct nor indirect paths. When you are on foot at a cabstand, you can either Walk a footpath, if any, to another cabstand, or Pick up a taxi, if any driveway connects the cabstand. When you are on a taxi, you can either Keep riding to another cabstand via a driveway, or Get out of the taxi. You can get out of a taxi only at cabstands. Transportation on a footpath or a driveway takes time defined for each of the roads. In addition, you have to wait one minute to pick up a taxi for the first time. The waiting time is doubled for each subsequent taxi picks, that is, two minutes for the second time, four minutes for the third time, and 2 99 minutes for the 100-th time. You are initially at one of the cabstands on foot. Find the shortest time possible to reach the destination cabstand. Input The input consists of multiple datasets, each in the following format. n p q a 1 b 1 c 1 … a p b p c p d 1 e 1 f 1 … d q e q f q Here, n , p , and q are the numbers of cabstands, footpaths, and driveways, respectively. n is an integer between 2 and 20000, inclusive. Cabstands are numbered 1 through n. p and q are positive integers not exceeding 20000. a i , b i , and c i describe the i- th footpath, meaning that it connects the cabstands numbered a i and b i , and it takes c i minutes to walk. d j , e j , and f j describe the j- th driveway, meaning that it connects the cabstands numbered d j and e j , and it takes a ride of f j minutes. a i , b i , d j , and e j are positive integers not exceeding n. c i and f j are positive integers not exceeding 10 6 . The end of the input is indicated by a line containing three zeros. The total of n, the total of p, and the total of q of all the datasets do not exceed 200000, respectively. Output For each dataset, output a single line containing the shortest time required in minutes for transporting from the cabstand number 1 to the cabstand number n modulo 10 9 +7. If such transportation is impossible, output −1 instead. Sample Input 5 4 2 1 2 1 2 3 100 3 4 1 4 5 100 2 3 10 4 5 10 4 1 1 1 2 100 2 3 100 4 3 3 1 2 10 2 3 1 3 4 10 2 1 2 3 2 2 4 3 2 0 0 0 Output for the Sample Input 25 -1 7
[ { "submission_id": "aoj_1652_10675436", "code_snippet": "#define _USE_MATH_DEFINES\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing H = pair<ll, ll>;\nusing P = pair<ll, H>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define all(a) (a).begin(), (a).end()\n#define fs first\n#define sc second\n#define xx first\n#define yy second.first\n#define zz second.second\n#define Q(i, j, k) mkp((i), mkp((j), (k)))\n#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)\n#define rep(i, n) rng(i, 0, (n))\n#define mkp make_pair\n#define vec vector\n#define pb emplace_back\n#define siz(a) int((a).size())\n#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())\n#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())\n#define ssp(i, n) (i==(ll)(n)-1?\"\\n\":\" \")\n#define ctoi(c) (int)((c)-'0')\n#define itoc(c) (char)((c)+'0')\n#define cyes printf(\"Yes\\n\")\n#define cno printf(\"No\\n\")\n#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)\n#define gcj printf(\"Case #%lld: \", __quetimes+1)\n#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()\n#define found(a, x) (a.find(x)!=a.end())\nconstexpr ll mod = (ll)1e9 + 7;\nconstexpr ll Mod = 998244353;\nconstexpr ld EPS = 1e-10;\nconstexpr ll inf = (ll)1e15;\nconstexpr int Inf = 1e9;\nconstexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T = ll>T read() { T u, k = scanf(\"%lld\", &u); return u; }\nstring reads() { string s; cin >> s; return s; }\nbool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }\nbool ina(int t, int l, int r) { return l <= t && t < r; }\nll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }\ntemplate<typename T>\nvoid fin(T x) { cout << x << endl; exit(0); }\n//--------------------------------------------------------------\nstruct st {\n int to;\n ll cst;\n bool type;\n};\nstruct ps {\n int p, t;\n ll c;\n bool ride;\n bool operator<(const ps& a) const {\n if ((t <= 40) != (a.t <= 40)) (t <= 40) < (a.t <= 40);\n if (t > 40 && a.t > 40) return t < a.t;\n return c < a.c;\n }\n bool operator>(const ps& a) const {\n if ((t <= 40) != (a.t <= 40)) (t <= 40) > (a.t <= 40);\n if (t > 40 && a.t > 40) return t > a.t;\n return c > a.c;\n }\n};\nsigned main() {\n int n, p, q;\n while (cin >> n >> p >> q && n) {\n vec<vec<st>>e(n);\n rep(i, p) {\n int a, b, c;cin >> a >> b >> c;\n a--;b--;\n e[a].pb(st{ b,c,false });\n e[b].pb(st{ a,c,false });\n }\n rep(i, q) {\n int a, b, c;cin >> a >> b >> c;\n a--;b--;\n e[a].pb(st{ b,c,true });\n e[b].pb(st{ a,c,true });\n }\n ll pow[50];\n pow[0] = 1;\n rep(i, 49) pow[i + 1] = pow[i] * 2;\n vec<vec<vl>>a(n, vec<vl>(50, vl(2, inf)));\n vec<vec<H>>b(n, vec<H>(2, H{ inf, inf }));\n a[0][0][0] = 0;\n priority_queue<ps, vec<ps>, greater<ps>>pq;\n pq.push(ps{ 0,0,0,0 });\n while (!pq.empty()) {\n auto [v, t, c, ride] = pq.top();\n pq.pop();\n if (t < 40) {\n if (a[v][t][ride] != c) continue;\n for (auto g : e[v]) {\n if (g.type) {\n if (a[g.to][t + !ride][1] > c + g.cst + (ride ? 0 : pow[t])) {\n a[g.to][t + !ride][1] = c + g.cst + (ride ? 0 : pow[t]);\n pq.push(ps{ g.to, t + !ride, a[g.to][t + !ride][1], 1 });\n }\n }\n else {\n if (a[g.to][t][0] > c + g.cst) {\n a[g.to][t][0] = c + g.cst;\n pq.push(ps{ g.to, t, a[g.to][t][0], 0 });\n }\n }\n }\n }\n else if (t == 40) {\n if (a[v][t][ride] != c) continue;\n for (auto g : e[v]) {\n if (g.type) {\n if (ride) {\n if (a[g.to][t][1] > c + g.cst) {\n a[g.to][t][1] = c + g.cst;\n pq.push(ps{ g.to, t, a[g.to][t][1], 1 });\n }\n }\n else {\n if (b[g.to][1] > H{ 41,c + g.cst }) {\n b[g.to][1] = H{ 41, c + g.cst };\n pq.push(ps{ g.to, 41, b[g.to][1].sc, 1 });\n }\n }\n }\n else {\n if (a[g.to][t][0] > c + g.cst) {\n a[g.to][t][0] = c + g.cst;\n pq.push(ps{ g.to, t, a[g.to][t][0], 0 });\n }\n }\n }\n }\n else {\n if (b[v][ride] != H{ t, c }) continue;\n for (auto g : e[v]) {\n if (g.type) {\n if (b[g.to][1] > H{ t + !ride, c + g.cst }) {\n b[g.to][1] = H{ t + !ride, c + g.cst };\n pq.push(ps{ g.to, t + !ride, b[g.to][1].sc, 1 });\n }\n }\n else {\n if (b[g.to][0] > H{ t, c + g.cst }) {\n b[g.to][0] = H{ t, c + g.cst };\n pq.push(ps{ g.to, t, b[g.to][0].sc, 0 });\n }\n }\n }\n }\n }\n ll ans = inf;\n rep(i, 50) rep(j, 2) chmin(ans, a[n - 1][i][j]);\n if (ans == inf) {\n H res = min(b[n - 1][0], b[n - 1][1]);\n if (res.fs == inf) ans = -1;\n else {\n ll tmp = 1;\n ans = 0;\n rep(i, res.fs) {\n if (i >= 40) (ans += tmp) %= 1000000007;\n (tmp *= 2) %= 1000000007;\n }// ;\n ans += res.sc;\n }\n }\n cout << ans % 1000000007 << endl;\n }\n}\n//564049465049187\n//1125899906842723\n\n//1126999418470499", "accuracy": 1, "time_ms": 1860, "memory_kb": 65888, "score_of_the_acc": -0.4222, "final_rank": 13 }, { "submission_id": "aoj_1652_10672214", "code_snippet": "// # pragma GCC target(\"avx2\")\n// # pragma GCC optimize(\"O3\")\n// # pragma GCC optimize(\"unroll-loops\")\n// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\n#include <atcoder/modint>\n#include <atcoder/math>\nusing namespace atcoder;\n// using mint = modint998244353;\n// const ll MOD = 998244353;\n// // using mint = modint;\n// // const ll MOD = 998244353;\nusing mint = modint1000000007;\nconst ll MOD = 1000000007;\nusing vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n// #define rep(i, n) for (int i = 0; i < (n); i++)\n#define rep2(i, a, b) for (int i = (a); i < (b); i++)\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing lll = __int128;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nconst long long INF = 1000000000000000005LL;\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multiple_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\nvoid dijkstra(long long start, vector<vector<pair<long long, long long>>> &G, vector<long long> &dist){\n // pair(cost,vertex)の順で入っていく\n priority_queue<pair<long long, long long>,vector<pair<long long, long long>>,greater<pair<long long, long long>>> q;\n\n dist.resize(int(G.size()));\n fill(all(dist), llINF);\n\n dist[start] = 0;\n q.push(make_pair(0, start));\n\n while(!q.empty()){\n auto [time, cur_v] = q.top();\n q.pop();\n if(dist[cur_v] != time) {\n continue;\n }\n for(auto p : G[cur_v]){\n long long next_v = p.first; long long cost = p.second;\n\n if(dist[next_v] > dist[cur_v] + cost){\n dist[next_v] = dist[cur_v] + cost;\n q.push(make_pair(dist[next_v], next_v));\n }\n }\n }\n}\n\nvoid dijkstra2(ll start, vector<vector<T>> &G, vector<P> &dist){\n priority_queue<pair<P, ll>,vector<pair<P, ll>>,greater<pair<P, ll>>> q; // pair(cost,vertex)の順で入っていく\n\n dist.resize(sz(G));\n rep(i, 0, sz(G)) dist[i] = {llINF, llINF};\n dist[start] = {0ll ,0ll};\n q.push(make_pair(dist[start], start));\n\n while(!q.empty()){\n auto [time, now] = q.top();\n q.pop();\n if(dist[now] != time) {\n continue;\n }\n for(auto p : G[now]){\n ll go = get<0>(p), taxicost = get<1>(p), timecost = get<2>(p);\n P next = {time.first + taxicost, time.second + timecost};\n\n if(dist[go] > next){\n dist[go] = next;\n q.push(make_pair(dist[go], go));\n }\n }\n }\n}\n\nvoid solve(ll n, ll p, ll q) {\n vl a(p), b(p), c(p);\n rep(i, 0, p) {\n cin >> a[i] >> b[i] >> c[i];\n a[i]--; b[i]--;\n }\n vl d(q), e(q), f(q);\n rep(i, 0, q) {\n cin >> d[i] >> e[i] >> f[i];\n d[i]--; e[i]--;\n }\n\n ll mt = 45;\n vector<vector<P>> G(2 * mt * n);\n rep(i, 0, n) {\n rep(j, 0, 2 * mt - 1) {\n G[i + j * n].emplace_back(P({i + j * n + n, 0ll}));\n }\n }\n rep(i, 0, mt) {\n rep(j, 0, p) {\n G[2 * i * n + a[j]].emplace_back(P({2 * i * n + b[j], c[j]}));\n G[2 * i * n + b[j]].emplace_back(P({2 * i * n + a[j], c[j]}));\n }\n }\n rep(i, 0, mt - 1) {\n rep(j, 0, q) {\n G[(2 * i + 1) * n + d[j]].emplace_back(P({(2 * i + 1) * n + e[j]}, f[j]));\n G[(2 * i + 1) * n + e[j]].emplace_back(P({(2 * i + 1) * n + d[j]}, f[j]));\n }\n }\n\n vl dist;\n dijkstra(0ll, G, dist);\n\n vl pena(mt);\n pena[0] = 0;\n rep(i, 0, mt - 1) {\n pena[i + 1] = pena[i] * 2 + 1;\n }\n\n ll ans = llINF;\n for(ll i = n - 1; i < 2 * mt * n; i += 2 * n){\n if(dist[i] == llINF) continue;\n chmin(ans, dist[i] + pena[i / (2 * n)]);\n }\n if(ans != llINF) ans %= MOD;\n\n vector<vector<T>> G2(2 * n);\n rep(i, 0, n) {\n G2[i].emplace_back(T({i + n, 1ll, 0ll}));\n G2[i + n].emplace_back(T({i, 0ll, 0ll}));\n }\n rep(i, 0, p) {\n G2[a[i]].emplace_back(T({b[i], 0ll, c[i]}));\n G2[b[i]].emplace_back(T({a[i], 0ll, c[i]}));\n }\n rep(i, 0, q) {\n G2[n + d[i]].emplace_back(T({n + e[i], 0ll, f[i]}));\n G2[n + e[i]].emplace_back(T({n + d[i], 0ll, f[i]}));\n }\n\n vector<P> dist2;\n dijkstra2(0, G2, dist2);\n\n if(dist2[n - 1].first != llINF && dist2[n - 1].first >= mt) {\n ans = dist2[n - 1].second + pow_mod(2ll, dist2[n - 1].first, MOD) - 1 + MOD;\n ans %= MOD;\n }\n\n if(ans == llINF) cout << -1 << endl;\n else cout << ans << endl;\n return ;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n while(true) {\n ll n, p, q;\n cin >> n >> p >> q;\n if(n == 0) return 0;\n solve(n, p, q);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 5500, "memory_kb": 215344, "score_of_the_acc": -1.8407, "final_rank": 20 }, { "submission_id": "aoj_1652_10672166", "code_snippet": "// # pragma GCC target(\"avx2\")\n// # pragma GCC optimize(\"O3\")\n// # pragma GCC optimize(\"unroll-loops\")\n// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\n#include <atcoder/modint>\n#include <atcoder/math>\nusing namespace atcoder;\n// using mint = modint998244353;\n// const ll MOD = 998244353;\n// // using mint = modint;\n// // const ll MOD = 998244353;\nusing mint = modint1000000007;\nconst ll MOD = 1000000007;\nusing vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n// #define rep(i, n) for (int i = 0; i < (n); i++)\n#define rep2(i, a, b) for (int i = (a); i < (b); i++)\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing lll = __int128;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nconst long long INF = 1000000000000000005LL;\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multiple_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\nvoid dijkstra(ll start, vector<vector<P>> &G, vl &dist){\n priority_queue<P,vector<P>,greater<P>> q; // pair(cost,vertex)の順で入っていく\n\n dist.resize(sz(G));\n rep(i, 0, sz(G)) dist[i] = llINF;\n dist[start] = 0;\n q.push(make_pair(0, start));\n\n while(!q.empty()){\n auto [time, now] = q.top();\n q.pop();\n if(dist[now] != time) {\n continue;\n }\n for(auto p : G[now]){\n ll go = p.first; ll cost = p.second;\n\n if(dist[go] > dist[now] + cost){\n dist[go] = dist[now] + cost;\n q.push(make_pair(dist[go], go));\n }\n }\n }\n}\n\nvoid dijkstra2(ll start, vector<vector<T>> &G, vector<P> &dist){\n priority_queue<pair<P, ll>,vector<pair<P, ll>>,greater<pair<P, ll>>> q; // pair(cost,vertex)の順で入っていく\n\n dist.resize(sz(G));\n rep(i, 0, sz(G)) dist[i] = {llINF, llINF};\n dist[start] = {0ll ,0ll};\n q.push(make_pair(dist[start], start));\n\n while(!q.empty()){\n auto [time, now] = q.top();\n q.pop();\n if(dist[now] != time) {\n continue;\n }\n for(auto p : G[now]){\n ll go = get<0>(p), taxicost = get<1>(p), timecost = get<2>(p);\n P next = {time.first + taxicost, time.second + timecost};\n\n if(dist[go] > next){\n dist[go] = next;\n q.push(make_pair(dist[go], go));\n }\n }\n }\n}\n\nvoid solve(ll n, ll p, ll q) {\n vl a(p), b(p), c(p);\n rep(i, 0, p) {\n cin >> a[i] >> b[i] >> c[i];\n a[i]--; b[i]--;\n }\n vl d(q), e(q), f(q);\n rep(i, 0, q) {\n cin >> d[i] >> e[i] >> f[i];\n d[i]--; e[i]--;\n }\n\n ll mt = 45;\n vector<vector<P>> G(2 * mt * n);\n rep(i, 0, n) {\n rep(j, 0, 2 * mt - 1) {\n G[i + j * n].emplace_back(P({i + j * n + n, 0ll}));\n }\n }\n rep(i, 0, mt) {\n rep(j, 0, p) {\n G[2 * i * n + a[j]].emplace_back(P({2 * i * n + b[j], c[j]}));\n G[2 * i * n + b[j]].emplace_back(P({2 * i * n + a[j], c[j]}));\n }\n }\n rep(i, 0, mt - 1) {\n rep(j, 0, q) {\n G[(2 * i + 1) * n + d[j]].emplace_back(P({(2 * i + 1) * n + e[j]}, f[j]));\n G[(2 * i + 1) * n + e[j]].emplace_back(P({(2 * i + 1) * n + d[j]}, f[j]));\n }\n }\n\n vl dist;\n dijkstra(0ll, G, dist);\n\n vl pena(mt);\n pena[0] = 0;\n rep(i, 0, mt - 1) {\n pena[i + 1] = pena[i] * 2 + 1;\n }\n\n ll ans = llINF;\n for(ll i = n - 1; i < 2 * mt * n; i += 2 * n){\n if(dist[i] == llINF) continue;\n chmin(ans, dist[i] + pena[i / (2 * n)]);\n }\n if(ans != llINF) ans %= MOD;\n\n vector<vector<T>> G2(2 * n);\n rep(i, 0, n) {\n G2[i].emplace_back(T({i + n, 1ll, 0ll}));\n G2[i + n].emplace_back(T({i, 0ll, 0ll}));\n }\n rep(i, 0, p) {\n G2[a[i]].emplace_back(T({b[i], 0ll, c[i]}));\n G2[b[i]].emplace_back(T({a[i], 0ll, c[i]}));\n }\n rep(i, 0, q) {\n G2[n + d[i]].emplace_back(T({n + e[i], 0ll, f[i]}));\n G2[n + e[i]].emplace_back(T({n + d[i], 0ll, f[i]}));\n }\n\n vector<P> dist2;\n dijkstra2(0, G2, dist2);\n\n if(dist2[n - 1].first != llINF && dist2[n - 1].first >= mt) {\n ans = dist2[n - 1].second + pow_mod(2ll, dist2[n - 1].first, MOD) - 1 + MOD;\n ans %= MOD;\n }\n\n if(ans == llINF) cout << -1 << endl;\n else cout << ans << endl;\n return ;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n while(true) {\n ll n, p, q;\n cin >> n >> p >> q;\n if(n == 0) return 0;\n solve(n, p, q);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 5460, "memory_kb": 215060, "score_of_the_acc": -1.831, "final_rank": 19 }, { "submission_id": "aoj_1652_10672137", "code_snippet": "// # pragma GCC target(\"avx2\")\n// # pragma GCC optimize(\"O3\")\n// # pragma GCC optimize(\"unroll-loops\")\n// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\n#include <atcoder/modint>\n#include <atcoder/math>\nusing namespace atcoder;\n// using mint = modint998244353;\n// const ll MOD = 998244353;\n// // using mint = modint;\n// // const ll MOD = 998244353;\nusing mint = modint1000000007;\nconst ll MOD = 1000000007;\nusing vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n// #define rep(i, n) for (int i = 0; i < (n); i++)\n#define rep2(i, a, b) for (int i = (a); i < (b); i++)\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing lll = __int128;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nconst long long INF = 1000000000000000005LL;\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multiple_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\nvoid dijkstra(ll start, vector<vector<P>> &G, vl &dist){\n priority_queue<P,vector<P>,greater<P>> q; // pair(cost,vertex)の順で入っていく\n\n dist.resize(sz(G));\n rep(i, 0, sz(G)) dist[i] = llINF;\n dist[start] = 0;\n q.push(make_pair(0, start));\n\n while(!q.empty()){\n auto [time, now] = q.top();\n q.pop();\n if(dist[now] != time) {\n continue;\n }\n for(auto p : G[now]){\n ll go = p.first; ll cost = p.second;\n\n if(dist[go] > dist[now] + cost){\n dist[go] = dist[now] + cost;\n q.push(make_pair(dist[go], go));\n }\n }\n }\n}\n\nvoid dijkstra2(ll start, vector<vector<T>> &G, vector<P> &dist){\n priority_queue<pair<P, ll>,vector<pair<P, ll>>,greater<pair<P, ll>>> q; // pair(cost,vertex)の順で入っていく\n\n dist.resize(sz(G));\n rep(i, 0, sz(G)) dist[i] = {llINF, llINF};\n dist[start] = {0ll ,0ll};\n q.push(make_pair(dist[start], start));\n\n while(!q.empty()){\n auto [time, now] = q.top();\n q.pop();\n if(dist[now] != time) {\n continue;\n }\n for(auto p : G[now]){\n ll go = get<0>(p), taxicost = get<1>(p), travelcost = get<2>(p);\n P next = {time.first + taxicost, time.second + travelcost};\n\n if(dist[go] > next){\n dist[go] = next;\n q.push(make_pair(dist[go], go));\n }\n }\n }\n}\n\nvoid solve(ll n, ll p, ll q) {\n vl a(p), b(p), c(p);\n rep(i, 0, p) {\n cin >> a[i] >> b[i] >> c[i];\n a[i]--; b[i]--;\n }\n vl d(q), e(q), f(q);\n rep(i, 0, q) {\n cin >> d[i] >> e[i] >> f[i];\n d[i]--; e[i]--;\n }\n\n ll mt = 40;\n vector<vector<P>> G(2 * mt * n);\n rep(i, 0, n) {\n rep(j, 0, 2 * mt - 1) {\n G[i + j * n].emplace_back(P({i + j * n + n, 0ll}));\n }\n }\n rep(i, 0, mt) {\n rep(j, 0, p) {\n G[2 * i * n + a[j]].emplace_back(P({2 * i * n + b[j], c[j]}));\n G[2 * i * n + b[j]].emplace_back(P({2 * i * n + a[j], c[j]}));\n }\n }\n rep(i, 0, mt - 1) {\n rep(j, 0, q) {\n G[(2 * i + 1) * n + d[j]].emplace_back(P({(2 * i + 1) * n + e[j]}, f[j]));\n G[(2 * i + 1) * n + e[j]].emplace_back(P({(2 * i + 1) * n + d[j]}, f[j]));\n }\n }\n\n vl dist;\n dijkstra(0ll, G, dist);\n\n vl pena(mt);\n pena[0] = 0;\n rep(i, 0, mt - 1) {\n pena[i + 1] = pena[i] * 2 + 1;\n }\n\n ll ans = llINF;\n for(ll i = n - 1; i < 2 * mt * n; i += 2 * n){\n if(dist[i] == llINF) continue;\n\n chmin(ans, dist[i] + pena[i / (2 * n)]);\n }\n\n if(ans != llINF) {\n ans %= MOD;\n cout << ans << endl;\n return ;\n }\n\n vector<vector<T>> G2(2 * n);\n rep(i, 0, n) {\n G2[i].emplace_back(T({i + n, 1ll, 0ll}));\n G2[i + n].emplace_back(T({i, 0ll, 0ll}));\n }\n rep(i, 0, p) {\n G2[a[i]].emplace_back(T({b[i], 0ll, c[i]}));\n G2[b[i]].emplace_back(T({a[i], 0ll, c[i]}));\n }\n rep(i, 0, q) {\n G2[n + d[i]].emplace_back(T({n + e[i], 0ll, f[i]}));\n G2[n + e[i]].emplace_back(T({n + d[i], 0ll, f[i]}));\n }\n\n vector<P> dist2;\n dijkstra2(0, G2, dist2);\n\n assert(dist2[n - 1].first >= mt);\n\n if(dist2[n - 1].first == llINF) {\n cout << -1 << endl;\n }\n else {\n ans = dist2[n - 1].second + pow_mod(2ll, dist2[n - 1].first, MOD) - 1 + MOD;\n ans %= MOD;\n cout << ans << endl;\n }\n return ;\n\n // if(dist2[n - 1].first != llINF && dist2[n - 1].first >= mt) {\n // }\n // else assert(false);\n\n // if(ans == llINF) cout << -1 << endl;\n // else cout << ans << endl;\n // return ;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n while(true) {\n ll n, p, q;\n cin >> n >> p >> q;\n if(n == 0) return 0;\n solve(n, p, q);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 4560, "memory_kb": 190660, "score_of_the_acc": -1.5345, "final_rank": 17 }, { "submission_id": "aoj_1652_10643257", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define ov4(a, b, c, d, name, ...) name\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)\n#define fore(e, v) for (auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate <typename T, typename S>\nbool chmin(T& a, const S& b) {\n return a > b ? a = b, 1 : 0;\n}\ntemplate <typename T, typename S>\nbool chmax(T& a, const S& b) {\n return a < b ? a = b, 1 : 0;\n}\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n#define i128 __int128_t\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\n\nint main(){\n const int M = 36;\n const ll mod = 1e9 + 7;\n while (true) {\n int n, p, q; cin >> n >> p >> q;\n if (n == 0) break;\n vector<vector<pii>> g1(n), g2(n);\n rep(i, p) {\n int a, b, c;cin >> a >> b >> c;\n a--, b--;\n g1[a].emplace_back(b, c);\n g1[b].emplace_back(a, c);\n }\n rep(i, q) {\n int a, b, c;cin >> a >> b >> c;\n a--, b--;\n g2[a].emplace_back(b, c);\n g2[b].emplace_back(a, c);\n }\n\n using S = pair<int, ll>;\n using T = tuple<int, ll, int>;\n vector<vector<S>> dist(n * 2, vector<S>(M, S{INF, INFL}));\n \n priority_queue<T,vector<T>, greater<T>> pq;\n // cerr << \"!\" << endl;\n dist[0][0] = S{0, 0};\n pq.emplace(T{0, 0, 0});\n while(!pq.empty()) {\n auto [cnt, d, v] = pq.top();pq.pop();\n // cerr << cnt << \" \" << d << \" \" << v+1 << endl;\n if (S{cnt, d} != dist[v][min(M-1, cnt)]) continue;\n if (v >= n) {\n for(auto [u,cost] : g2[v-n]) {\n S nd = S{cnt, d + cost};\n T nt = T{cnt, d + cost, u + n};\n if (dist[u+n][min(M-1, cnt)] > nd) {\n dist[u+n][min(M-1, cnt)] = nd;\n pq.emplace(T{nd.first, nd.second, u + n});\n }\n }\n S nd = S{cnt, d};\n if (dist[v-n][min(M-1, cnt)] > nd) {\n dist[v-n][min(M-1, cnt)] = nd;\n pq.emplace(T{nd.first, nd.second, v-n});\n }\n }else {\n S nd = S{cnt+1, d};\n if (dist[v+n][min(M-1, cnt+1)] > nd) {\n dist[v+n][min(M-1, cnt+1)] = nd;\n pq.emplace(nd.first, nd.second, v + n);\n }\n \n for(auto [u,cost] : g1[v]) {\n S nd = S{cnt, d + cost};\n if (dist[u][min(M-1, cnt)] > nd) {\n dist[u][min(M-1, cnt)] = nd;\n pq.emplace(nd.first, nd.second, u);\n }\n }\n }\n }\n\n // rep(i, n) {\n // rep(j, M) {\n // cerr << dist[i][j].first << \"/\" << dist[i][j].second << \" \";\n // }\n // cerr << endl;\n // }\n\n ll ans = INFL;\n rep(i, M-1) {\n if (dist[n-1][i].first != INF) chmin(ans, (1ll<<i) - 1 + dist[n-1][i].second);\n // cerr << i << \" \" << (1ll<<i) - 1 + dist[n-1][i].second << endl;\n }\n if (ans != INFL) {\n cout << ans % mod << endl;\n }\n else if(dist[n-1].back().first == INF) {\n cout << -1 << endl;\n }\n else {\n ll ans = 1;\n auto [cnt, l] = dist[n-1].back();\n rep(i, cnt) {\n ans = ans * 2 % mod;\n }\n ans = (ans + mod-1) % mod;\n cout << (ans + l) % mod << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1140, "memory_kb": 32160, "score_of_the_acc": -0.1236, "final_rank": 3 }, { "submission_id": "aoj_1652_10643243", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define ov4(a, b, c, d, name, ...) name\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)\n#define fore(e, v) for (auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate <typename T, typename S>\nbool chmin(T& a, const S& b) {\n return a > b ? a = b, 1 : 0;\n}\ntemplate <typename T, typename S>\nbool chmax(T& a, const S& b) {\n return a < b ? a = b, 1 : 0;\n}\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n#define i128 __int128_t\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\n\nint main(){\n const int M = 40;\n const ll mod = 1e9 + 7;\n while (true) {\n int n, p, q; cin >> n >> p >> q;\n if (n == 0) break;\n vector<vector<pii>> g1(n), g2(n);\n rep(i, p) {\n int a, b, c;cin >> a >> b >> c;\n a--, b--;\n g1[a].emplace_back(b, c);\n g1[b].emplace_back(a, c);\n }\n rep(i, q) {\n int a, b, c;cin >> a >> b >> c;\n a--, b--;\n g2[a].emplace_back(b, c);\n g2[b].emplace_back(a, c);\n }\n\n using S = pair<int, ll>;\n using T = tuple<int, ll, int>;\n vector<vector<S>> dist(n * 2, vector<S>(M, S{INF, INFL}));\n \n priority_queue<T,vector<T>, greater<T>> pq;\n // cerr << \"!\" << endl;\n dist[0][0] = S{0, 0};\n pq.emplace(T{0, 0, 0});\n while(!pq.empty()) {\n auto [cnt, d, v] = pq.top();pq.pop();\n // cerr << cnt << \" \" << d << \" \" << v+1 << endl;\n if (S{cnt, d} != dist[v][min(M-1, cnt)]) continue;\n if (v >= n) {\n for(auto [u,cost] : g2[v-n]) {\n S nd = S{cnt, d + cost};\n T nt = T{cnt, d + cost, u + n};\n if (dist[u+n][min(M-1, cnt)] > nd) {\n dist[u+n][min(M-1, cnt)] = nd;\n pq.emplace(T{nd.first, nd.second, u + n});\n }\n }\n S nd = S{cnt, d};\n if (dist[v-n][min(M-1, cnt)] > nd) {\n dist[v-n][min(M-1, cnt)] = nd;\n pq.emplace(T{nd.first, nd.second, v-n});\n }\n }else {\n S nd = S{cnt+1, d};\n if (dist[v+n][min(M-1, cnt+1)] > nd) {\n dist[v+n][min(M-1, cnt+1)] = nd;\n pq.emplace(nd.first, nd.second, v + n);\n }\n \n for(auto [u,cost] : g1[v]) {\n S nd = S{cnt, d + cost};\n if (dist[u][min(M-1, cnt)] > nd) {\n dist[u][min(M-1, cnt)] = nd;\n pq.emplace(nd.first, nd.second, u);\n }\n }\n }\n }\n\n // rep(i, n) {\n // rep(j, M) {\n // cerr << dist[i][j].first << \"/\" << dist[i][j].second << \" \";\n // }\n // cerr << endl;\n // }\n\n ll ans = INFL;\n rep(i, M-1) {\n if (dist[n-1][i].first != INF) chmin(ans, (1ll<<i) - 1 + dist[n-1][i].second);\n // cerr << i << \" \" << (1ll<<i) - 1 + dist[n-1][i].second << endl;\n }\n if (ans != INFL) {\n cout << ans % mod << endl;\n }\n else if(dist[n-1].back().first == INF) {\n cout << -1 << endl;\n }\n else {\n ll ans = 1;\n auto [cnt, l] = dist[n-1].back();\n rep(i, cnt) {\n ans = ans * 2 % mod;\n }\n ans = (ans + mod-1) % mod;\n cout << (ans + l) % mod << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 34660, "score_of_the_acc": -0.162, "final_rank": 4 }, { "submission_id": "aoj_1652_10643240", "code_snippet": "#include <bits/extc++.h>\n\n#include <atcoder/all>\n\nusing mint = atcoder::modint1000000007;\n\nusing ll = long long;\n\nusing pii = std::pair<int, int>;\nusing pil = std::pair<int, ll>;\n\ntemplate <class T>\nusing pq_rev = std::priority_queue<T, std::vector<T>, std::greater<>>;\n\ntemplate <class T>\nbool chmax(T& x, const T& v) {\n if (x < v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\ntemplate <class T>\nbool chmin(T& x, const T& v) {\n if (x > v) {\n x = v;\n return true;\n } else {\n return false;\n }\n}\n\nusing node = std::tuple<ll, mint, int, int, int>;\n\nstruct compare {\n bool operator()(node l, node r) {\n auto [al, bl, cl, dl, el] = l;\n auto [ar, br, cr, dr, er] = r;\n if (el < 60 && er < 60) {\n return al + (1LL << el) > ar + (1LL << er);\n } else {\n return el > er || (el == er && al > ar);\n }\n }\n} cmp;\n\nbool solve() {\n int N, P, Q;\n std::cin >> N >> P >> Q;\n if (N == 0) {\n return false;\n }\n\n std::vector walk(N, std::vector<pil>());\n std::vector taxi(N, std::vector<pil>());\n\n for (int i = 0; i < P; i++) {\n int a, b;\n ll c;\n std::cin >> a >> b >> c;\n\n a--, b--;\n\n walk[a].emplace_back(b, c);\n walk[b].emplace_back(a, c);\n }\n\n for (int i = 0; i < Q; i++) {\n int a, b;\n ll c;\n std::cin >> a >> b >> c;\n\n a--, b--;\n\n taxi[a].emplace_back(b, c);\n taxi[b].emplace_back(a, c);\n }\n\n atcoder::dsu dsu(N);\n\n for (int i = 0; i < N; i++) {\n for (auto [w, c] : walk[i]) {\n dsu.merge(i, w);\n }\n for (auto [w, c] : taxi[i]) {\n dsu.merge(i, w);\n }\n }\n\n if (!dsu.same(0, N - 1)) {\n std::cout << \"-1\\n\";\n return true;\n }\n\n std::priority_queue<node, std::vector<node>, compare> priq;\n\n std::vector dist(\n N, std::vector(2, std::vector<node>(60, node(0, 0, 0, 0, 10000000))));\n std::vector locked(N, std::vector(2, std::vector(60, false)));\n\n dist[0][0][0] = {0, 0, 0, 0, 0};\n\n priq.emplace(0, 0, 0, 0, 0);\n\n auto move = [&](int v, int tx, int tc, node r) {\n if (!locked[v][tx][tc] && cmp(dist[v][tx][tc], r)) {\n dist[v][tx][tc] = r;\n priq.emplace(r);\n }\n };\n\n while (!priq.empty()) {\n node nodev = priq.top();\n auto [dl, dm, v, tx, tc] = nodev;\n priq.pop();\n\n if (v == N - 1) {\n std::cout << dm.val() << '\\n';\n break;\n }\n\n int tc2 = std::min(tc, 59);\n\n if (locked[v][tx][tc2]) continue;\n\n locked[v][tx][tc2] = true;\n\n if (tx) {\n move(v, 0, tc2, {dl, dm, v, 0, tc});\n\n for (auto [w, c] : taxi[v]) {\n move(w, 1, tc2, {dl + c, dm + c, w, 1, tc});\n }\n } else {\n move(v, 1, std::min(tc2 + 1, 59),\n {dl, dm + mint(2).pow(tc), v, 1, tc + 1});\n\n for (auto [w, c] : walk[v]) {\n move(w, 0, tc2, {dl + c, dm + c, w, 0, tc});\n }\n }\n }\n\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 1260, "memory_kb": 80280, "score_of_the_acc": -0.357, "final_rank": 9 }, { "submission_id": "aoj_1652_10643234", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define ov4(a, b, c, d, name, ...) name\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)\n#define fore(e, v) for (auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate <typename T, typename S>\nbool chmin(T& a, const S& b) {\n return a > b ? a = b, 1 : 0;\n}\ntemplate <typename T, typename S>\nbool chmax(T& a, const S& b) {\n return a < b ? a = b, 1 : 0;\n}\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n#define i128 __int128_t\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\n\nint main(){\n const int M = 50;\n const ll mod = 1e9 + 7;\n while (true) {\n int n, p, q; cin >> n >> p >> q;\n if (n == 0) break;\n vector<vector<pii>> g1(n), g2(n);\n rep(i, p) {\n int a, b, c;cin >> a >> b >> c;\n a--, b--;\n g1[a].emplace_back(b, c);\n g1[b].emplace_back(a, c);\n }\n rep(i, q) {\n int a, b, c;cin >> a >> b >> c;\n a--, b--;\n g2[a].emplace_back(b, c);\n g2[b].emplace_back(a, c);\n }\n\n using S = pair<int, ll>;\n using T = tuple<int, ll, int>;\n vector<vector<S>> dist(n * 2, vector<S>(M, S{INF, INFL}));\n \n priority_queue<T,vector<T>, greater<T>> pq;\n // cerr << \"!\" << endl;\n dist[0][0] = S{0, 0};\n pq.emplace(T{0, 0, 0});\n while(!pq.empty()) {\n auto [cnt, d, v] = pq.top();pq.pop();\n // cerr << cnt << \" \" << d << \" \" << v+1 << endl;\n if (S{cnt, d} != dist[v][min(M-1, cnt)]) continue;\n if (v >= n) {\n for(auto [u,cost] : g2[v-n]) {\n S nd = S{cnt, d + cost};\n T nt = T{cnt, d + cost, u + n};\n if (dist[u+n][min(M-1, cnt)] > nd) {\n dist[u+n][min(M-1, cnt)] = nd;\n pq.emplace(T{nd.first, nd.second, u + n});\n }\n }\n S nd = S{cnt, d};\n if (dist[v-n][min(M-1, cnt)] > nd) {\n dist[v-n][min(M-1, cnt)] = nd;\n pq.emplace(T{nd.first, nd.second, v-n});\n }\n }else {\n S nd = S{cnt+1, d};\n if (dist[v+n][min(M-1, cnt+1)] > nd) {\n dist[v+n][min(M-1, cnt+1)] = nd;\n pq.emplace(nd.first, nd.second, v + n);\n }\n \n for(auto [u,cost] : g1[v]) {\n S nd = S{cnt, d + cost};\n if (dist[u][min(M-1, cnt)] > nd) {\n dist[u][min(M-1, cnt)] = nd;\n pq.emplace(nd.first, nd.second, u);\n }\n }\n }\n }\n\n // rep(i, n) {\n // rep(j, M) {\n // cerr << dist[i][j].first << \"/\" << dist[i][j].second << \" \";\n // }\n // cerr << endl;\n // }\n\n ll ans = INFL;\n rep(i, M-1) {\n if (dist[n-1][i].first != INF) chmin(ans, (1ll<<i) - 1 + dist[n-1][i].second);\n // cerr << i << \" \" << (1ll<<i) - 1 + dist[n-1][i].second << endl;\n }\n if (ans != INFL) {\n cout << ans % mod << endl;\n }\n else if(dist[n-1].back().first == INF) {\n cout << -1 << endl;\n }\n else {\n ll ans = 1;\n auto [cnt, l] = dist[n-1].back();\n rep(i, cnt) {\n ans = ans * 2 % mod;\n }\n ans = (ans + mod-1) % mod;\n cout << (ans + l) % mod << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1660, "memory_kb": 40908, "score_of_the_acc": -0.2718, "final_rank": 7 }, { "submission_id": "aoj_1652_10643000", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst ll INF=1LL<<60;\nconst int MOD=1'000'000'007;\n\nll power(ll x,ll r){\n ll re=1,k=x;\n for(int i=0;i<60;i++){\n if(r>>i&1){\n re=(re*k)%MOD;\n }\n k=k*k%MOD;\n }\n return re;\n}\n\nint main() {\n while(1){\n int N,P,Q;\n cin>>N>>P>>Q;\n if(N==0)return 0;\n vector<vector<tuple<int,ll,int>>>G(N);\n rep(i,0,P){\n int a,b;\n ll c;\n cin>>a>>b>>c;\n a--;\n b--;\n G[a].push_back({b,c,0});\n G[b].push_back({a,c,0});\n }\n rep(i,0,Q){\n int d,e;\n ll f;\n cin>>d>>e>>f;\n d--;\n e--;\n G[d].push_back({e,f,1});\n G[e].push_back({d,f,1});\n }\n vector<ll>T(N,INF);\n {\n vector t2(N,vector<ll>(2,INF));\n t2[0][0]=0;\n deque<tuple<ll,int,int>>dq;\n dq.push_back({t2[0][0],0,0});\n while(dq.size()){\n auto[c,x,y]=dq.front();\n dq.pop_front();\n if(t2[x][y]<c)continue;\n if(y==0){\n if(chmin(t2[x][1],t2[x][y]+1)){\n dq.push_back({t2[x][1],x,1});\n }\n }else{\n if(chmin(t2[x][0],t2[x][y])){\n dq.push_front({t2[x][0],x,0});\n }\n }\n for(auto[i,e,t]:G[x]){\n if(y!=t)continue;\n if(chmin(t2[i][y],t2[x][y])){\n dq.push_front({t2[i][y],i,y});\n }\n }\n }\n rep(i,0,N)T[i]=min(t2[i][0],t2[i][1]);\n }\n if(T[N-1]==INF){\n cout<<-1<<endl;\n continue;\n }\n vector dp(N,vector(60,vector<ll>(2,INF)));\n dp[0][0][0]=0;\n {\n priority_queue<tuple<ll,int,int,int>,vector<tuple<ll,int,int,int>>,greater<tuple<ll,int,int,int>>>pq;\n pq.push({dp[0][0][0],0,0,0});\n while(pq.size()){\n auto[cost,x,y,z]=pq.top();\n pq.pop();\n if(dp[x][y][z]<cost)continue;\n if(z==0){\n if(y+1<60&&chmin(dp[x][y+1][1],dp[x][y][z])){\n pq.push({dp[x][y+1][1],x,y+1,1});\n }\n }else{\n if(chmin(dp[x][y][0],dp[x][y][z])){\n pq.push({dp[x][y][0],x,y,0});\n }\n }\n for(auto[i,e,t]:G[x]){\n if(z!=t)continue;\n int j=T[x]+y-T[i];\n if(0<=j&&j<60&&chmin(dp[i][j][z],dp[x][y][z]+e)){\n pq.push({dp[i][j][z],i,j,z});\n }\n }\n }\n }\n vector<ll>D(60);\n rep(i,0,60)D[i]=min(dp[N-1][i][0],dp[N-1][i][1]);\n if(T[N-1]>60){\n rep(i,0,60){\n if(D[i]!=INF){\n ll ans=power(2,T[N-1]+i)-1+D[i];\n ans%=MOD;\n if(ans<0)ans+=MOD;\n cout<<ans<<endl;\n break;\n }\n }\n }else{\n rep(i,0,60){\n if(D[i]==INF)continue;\n bool ok=true;\n rep(j,0,60){\n if(D[j]==INF)continue;\n __int128_t p=1,q=1;\n rep(k,0,T[N-1]+i)p*=2;\n rep(k,0,T[N-1]+j)q*=2;\n p+=D[i];\n q+=D[j];\n if(p>q){\n ok=false;\n break;\n }\n }\n if(ok){\n ll ans=power(2,T[N-1]+i)-1+D[i];\n ans%=MOD;\n if(ans<0)ans+=MOD;\n cout<<ans<<endl;\n break;\n }\n }\n }\n }\n // rep(i,0,60){\n // if(D[i]==INF)continue;\n // bool ok=true;\n // rep(j,0,60){\n // if(D[j]==INF)continue;\n // // 2^{T[N-1]+i}-1+D[i]<=2^{T[N-1]+j}-1+D[j]\n // if((1LL<<(T[N-1]+i))+D[i]>(1LL<<(T[N-1]+j))+D[j]){\n // ok=false;\n // break;\n // }\n // }\n // if(ok){\n // ll ans=power(2,T[N-1]+i)-1+D[i];\n // ans%=MOD;\n // if(ans<0)ans+=MOD;\n // cout<<ans<<endl;\n // break;\n // }\n // }\n // }\n}", "accuracy": 1, "time_ms": 2440, "memory_kb": 115412, "score_of_the_acc": -0.7593, "final_rank": 15 }, { "submission_id": "aoj_1652_10642684", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\nvoid solve(int n, int p, int q){\n vector<vector<pil>> a(n), b(n);\n while (p--){\n int u, v; in(u,v); u--, v--;\n ll w; in(w);\n a[u].emplace_back(v,w);\n a[v].emplace_back(u,w);\n }\n while (q--){\n int u, v; in(u,v); u--, v--;\n ll w; in(w);\n b[u].emplace_back(v,w);\n b[v].emplace_back(u,w);\n }\n const int mx = 62;\n auto cmp = [&](pli x, pli y){\n auto [A, B] = x;\n auto [C, D] = y;\n if (A == linf) return false;\n if (C == linf) return true;\n A--;\n C--;\n // A+2^B < C+2^D\n if (B == D){\n return A < C;\n }\n else if (B < D){\n if (A - C <= 0) return true;\n if (B >= mx) return true;\n if (D - B >= mx) return true;\n ll lhs = ((A - C) >> B) + 1;\n ll rhs = (1LL << (D - B)) - 1;\n return lhs <= rhs;\n }\n else {\n if (C - A <= 0) return false;\n if (D >= mx) return false;\n if (B - D >= mx) return false;\n ll lhs = 1LL << (B - D);\n ll rhs = (C - A + (1LL << D) - 1) >> D;\n return lhs <= rhs;\n }\n };\n using dat = pair<pli,pii>;\n auto comp = [&](dat x, dat y){\n if (cmp(y.first,x.first)) return true;\n if (cmp(x.first,y.first)) return false;\n return x.second < y.second;\n };\n vector<vector<pli>> dist(mx,vector<pli>(n*2,{linf,70}));\n priority_queue<dat,vector<dat>, decltype(comp)> pque(comp);\n dist[0][0] = pli(0,0);\n pque.emplace(pli(0,0),pii(0,0));\n while (!pque.empty()){\n auto [d, iv] = pque.top(); pque.pop();\n auto [i, v] = iv;\n if (cmp(dist[i][v],d)) continue;\n if (v < n){\n for (auto [u, w] : a[v]){\n pli nd = d;\n nd.first += w;\n int ni = i;\n if (cmp(nd,dist[ni][u])){\n dist[ni][u] = nd;\n pque.emplace(dist[ni][u],pii(ni,u));\n }\n }\n for (auto [u, w] : b[v]){\n pli nd = d;\n nd.first += w;\n nd.second++;\n int ni = min(mx-1,i+1);\n int nu = n + u;\n if (cmp(nd,dist[ni][nu])){\n dist[ni][nu] = nd;\n pque.emplace(dist[ni][nu],pii(ni,nu));\n }\n }\n }\n else {\n for (auto [u, w] : a[v-n]){\n pli nd = d;\n nd.first += w;\n int ni = i;\n if (cmp(nd,dist[ni][u])){\n dist[ni][u] = nd;\n pque.emplace(dist[ni][u],pii(ni,u));\n }\n }\n for (auto [u, w] : b[v-n]){\n pli nd = d;\n nd.first += w;\n int ni = i;\n int nu = n + u;\n if (cmp(nd,dist[ni][nu])){\n dist[ni][nu] = nd;\n pque.emplace(dist[ni][nu],pii(ni,nu));\n }\n }\n }\n }\n pli cost(linf,70);\n rep(i,mx){\n if (cmp(dist[i][n-1],cost)){\n cost = dist[i][n-1];\n }\n if (cmp(dist[i][n+n-1],cost)){\n cost = dist[i][n+n-1];\n }\n }\n if (cost.first == linf){\n out(-1);\n return ;\n }\n ll ans = pow_mod_constexpr(2,cost.second,mod107) + cost.first + (mod107 - 1);\n ans %= mod107;\n out(ans);\n}\n\nint main(){\n while (true){\n int n, p, q; in(n,p,q);\n if (n == 0) break;\n solve(n,p,q);\n }\n}", "accuracy": 1, "time_ms": 1730, "memory_kb": 48084, "score_of_the_acc": -0.3177, "final_rank": 8 }, { "submission_id": "aoj_1652_10624648", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nconst int MOD = 998244353;\nmt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());\n#define mid (l+(r-l)/2)\n\nusing ll = long long;\nusing ld = long double;\nusing str = string;\nusing i128 = __int128;\n\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pd = pair<ld,ld>;\n#define f first\n#define s second\n#define cf cout<<flush\n\ntemplate <class T> using V = vector<T>;\nusing vi = V<int>;\nusing vvi = V<vi>;\nusing vl = V<ll>;\nusing vvl = V<vl>;\nusing vd = V<ld>;\nusing vpi = V<pii>;\nusing vpl = V<pll>;\n\n#define sz(x) int((x).size())\n#define bg(x) begin(x)\n#define all(x) bg(x), end(x)\n#define sor(x) sort(all(x))\n#define sorr(x) sort(all(x)),reverse(all(x))\n#define uniq(x) sor(x), x.resize(unique(all(x))-x.begin())\n#define pb push_back\n#define ft front()\n#define bk back()\n#define chmi(x,y, ...) x = min(__VA_OPT__({) x,y __VA_OPT__(,)__VA_ARGS__ __VA_OPT__(}))\n#define chma(x,y, ...) x = max(__VA_OPT__({) x,y __VA_OPT__(,)__VA_ARGS__ __VA_OPT__(}))\n#define lwb(x,y) (int)(lower_bound(all(x),y)-x.begin())\n#define upb(x,y) (int)(upper_bound(all(x),y)-x.begin())\n#define compress(v) {auto _t=v;uniq(_t);for(auto &x:v)x=lwb(_t,x);}\n#define in(x,y,n,m) (x>=0&&x<n&&y>=0&&y<m)\n#define retu(args, ...) {ps(args __VA_OPT__(,)__VA_ARGS__);return ;}\n\n#define For(i,a,b) for(int i = (a); i <= (b); i++)\n#define F0r(i,a) for(int i = 0; i < (a); i++)\n#define Rof(i,a,b) for(int i = (b); i >= (a); i--)\n#define R0f(i,a) for(int i = (a)-1; i >= 0; i--)\n#define rep(a) F0r(_,a)\n#define each(a,x) for(auto &a:x)\n\nconst int dx[4]{1,0,-1,0},dy[4]{0,1,0,-1};\nconst int dx8[8]{1,1,0,-1,-1,-1,0,1},dy8[8]{0,1,1,1,0,-1,-1,-1};\ntemplate <class T> using pqg = priority_queue<T,vector<T>,greater<T>>;\n\nconstexpr int pct(int x){return __builtin_popcount(x);}\nconstexpr int pctll(long long x){return __builtin_popcountll(x);}\nconstexpr int bits(int x){return 31-__builtin_clz(x);}\n\n#define SFINAE(x, ...) \\\n template <class, class = void> struct x : std::false_type {}; \\\n template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}\n\nSFINAE(DefaultI, decltype(std::cin >> std::declval<T &>()));\nSFINAE(DefaultO, decltype(std::cout << std::declval<T &>()));\nSFINAE(IsTuple, typename std::tuple_size<T>::type);\nSFINAE(Iterable, decltype(std::begin(std::declval<T>())));\n\ntemplate <auto &is> struct Reader {\n template <class T> void Impl(T &t) {\n if constexpr (DefaultI<T>::value) is >> t;\n else if constexpr (Iterable<T>::value) {\n for (auto &x : t) Impl(x);\n } else if constexpr (IsTuple<T>::value) {\n std::apply([this](auto &...args) { (Impl(args), ...); }, t);\n } else static_assert(IsTuple<T>::value, \"No matching type for read\");\n }\n template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); }\n};\n\ntemplate <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); }\n#define def(t, args...) \\\n t args; \\\n re(args);\n\ntemplate <auto &os, bool debug, bool print_nd> struct Writer {\n string comma() const { return debug ? \",\" : \"\"; }\n template <class T> constexpr char Space(const T &) const {\n return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\\n'\n : ' ';\n }\n template <class T> void Impl(T const &t) const {\n if constexpr (DefaultO<T>::value) os << t;\n else if constexpr (Iterable<T>::value) {\n if (debug) os << '{';\n int i = 0;\n for (auto &&x : t)\n ((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x));\n if (debug) os << '}';\n } else if constexpr (IsTuple<T>::value) {\n if (debug) os << '(';\n std::apply(\n [this](auto const &...args) {\n int i = 0;\n (((i++) ? (os << comma() << \" \", Impl(args)) : Impl(args)),\n ...);\n },\n t);\n if (debug) os << ')';\n } else static_assert(IsTuple<T>::value, \"No matching type for print\");\n }\n template <class T> void ImplWrapper(T const &t) const {\n if (debug) os << \"\\033[0;31m\";\n Impl(t);\n if (debug) os << \"\\033[0m\";\n }\n template <class... Ts> void print(Ts const &...ts) const {\n ((Impl(ts)), ...);\n }\n template <class F, class... Ts>\n void print_with_sep(const std::string &sep, F const &f,\n Ts const &...ts) const {\n ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\\n';\n }\n void print_with_sep(const std::string &) const { os << '\\n'; }\n};\n\ntemplate <class... Ts> void pr(Ts const &...ts) {\n Writer<cout, false, true>{}.print(ts...);\n}\ntemplate <class... Ts> void ps(Ts const &...ts) {\n Writer<cout, false, true>{}.print_with_sep(\" \", ts...);\n}\n\nvoid setIn(str s) { freopen(s.c_str(), \"r\", stdin); }\nvoid setOut(str s) { freopen(s.c_str(), \"w\", stdout); }\nvoid setIO(str s = \"\") {\n cin.tie(0)->sync_with_stdio(0);\n cout << fixed << setprecision(12);\n if (sz(s)) setIn(s + \".in\"), setOut(s + \".out\");\n}\n\n//#include<atcoder/all>\n//using namespace atcoder;\n#define N 200005\n\nstruct DSU{\n\n struct NODE{\n int p,sz;\n bool operator==(const NODE& o)const{\n return p==o.p;\n }\n };\n\n int _n;\n vector<NODE> node;\n\n DSU():DSU(0){}\n\n DSU(int n){\n _n=n;\n node.resize(n+1);\n for(int i=0;i<=n;i++){\n node[i]={i,1};\n }\n }\n\n int fp(int v){\n return v==node[v].p?v:node[v].p=fp(node[v].p);\n }\n\n void merge(int x,int y){\n x=fp(x),y=fp(y);\n if(x==y)return ;\n node[x].sz+=node[y].sz;\n node[y].p=x;\n }\n\n const NODE& operator[](int v){\n return node[fp(v)];\n }\n\n};\n\n\nvoid solve(){\n def(int,n,c,d);\n if(!n)exit(0);\n V<V<array<ll,3>>> g(n);\n DSU ds(n);\n rep(c){\n def(int,x,y,w);\n x--,y--;\n g[x].pb({y,w,0});\n g[y].pb({x,w,0});\n ds.merge(x,y);\n }\n rep(d){\n def(int,x,y,w);\n x--,y--;\n g[x].pb({y,w,1});\n g[y].pb({x,w,1});\n ds.merge(x,y);\n }\n if(ds[0]!=ds[n-1])retu(-1);\n // ps(\"ge\");cf;\n V<vvl> dp(2,vvl(35,vl(n,1e18)));\n V<vpl> dpp(2,vpl(n,{1e18,1e18}));\n \n pqg<array<ll,4>> q;\n \n q.push({0,0,0,0});\n dp[0][0][0]=0;\n \n \n auto upd = [&](ll k,ll w,int st,int s) -> void {\n if(dp[st][k][s]>w){\n dp[st][k][s]=w;\n q.push({k,w,st,s});\n }\n \n };\n \n auto updd = [&](ll k,ll w,int st,int s) -> void {\n if(dpp[st][s]>pll{k,w}){\n dpp[st][s]={k,w};\n q.push({k,w,st,s});\n }\n \n };\n \n \n while(!q.empty()){\n auto [k,w,st,s]=q.top();\n q.pop();\n if(k>=35){\n if(dpp[st][s]>pll{k,w})continue;\n \n if(st){\n if(k<35)upd(k,w,0,s);\n else updd(k,w,0,s);\n }\n if(!st){\n if(k+1<35)upd(k+1,w,1,s);\n else updd(k+1,w,1,s);\n }\n \n for(auto &[x,xw,stt]:g[s]){\n if(st==stt){\n updd(k,w+xw,st,x);\n }\n }\n \n continue;\n }\n if(dp[st][k][s]>w)continue;\n \n if(st){\n if(k<35)upd(k,w,0,s);\n else updd(k,w,0,s);\n }\n if(!st){\n if(k+1<35)upd(k+1,w,1,s);\n else updd(k+1,w,1,s);\n }\n \n for(auto &[x,xw,stt]:g[s]){\n if(st==stt){\n upd(k,w+xw,st,x);\n // dp[st][k][x]=w+xw;\n // q.push({k,w+xw,st,x});\n }\n }\n \n }\n \n ll ans=1e18,mod=1e9+7;\n \n F0r(i,35)chmi(ans,dp[0][i][n-1]+(1ll<<i)-1);\n \n if(ans!=1e18){\n ps(ans%mod);\n }\n else{\n ll now=1;\n rep(dpp[0][n-1].f)now=(now*2)%mod;\n ps((now-1+mod+dpp[0][n-1].s)%mod); \n }\n \n \n return ;\n}\n\nint main(){\n setIO();\n int t=1;\n //re(t);\n while(true)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 24064, "score_of_the_acc": -0.0717, "final_rank": 2 }, { "submission_id": "aoj_1652_10601398", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 多倍長整数\n// #if __has_include(<boost/multiprecision/cpp_int.hpp>)\n// #include <boost/multiprecision/cpp_int.hpp>\n// using lll =boost::multiprecision::int128_t;\n// #endif\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint inf = 2e9;\nll INF = 9e18;\n#define endl '\\n';\n\n// 新規マクロ\n#define all(a) (a).begin(), (a).end()\n#define rep(i, N, limit) for (int i = (int)N; i < (int)limit; i++)\n\n///////////////////// vector関連\ntemplate <class T, size_t n, size_t idx = 0>\nauto make_vec(const size_t (&d)[n], const T &init) noexcept {\n if constexpr (idx < n)\n return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else\n return init;\n}\n\ntemplate <class T, size_t n> auto make_vec(const size_t (&d)[n]) noexcept {\n return make_vec(d, T{});\n}\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\n////////////////////////////\ninline string input();\ninline ll I();\ninline pll II();\n\n/////////////////////出力関連\ninline void Yes(bool f);\n\nbool solve() {\n int N, P, Q;\n cin >> N >> P >> Q;\n if (N == 0) {\n return false;\n }\n vector<vector<pll>> GP(N);\n vector<vector<pll>> GQ(N);\n for (int i = 0; i < P; i++) {\n ll a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n GP[a].emplace_back(b, c);\n GP[b].emplace_back(a, c);\n }\n for (int i = 0; i < Q; i++) {\n ll a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n GQ[a].emplace_back(b, c);\n GQ[b].emplace_back(a, c);\n }\n\n ll sh = 50;\n vector<ll> pow_2(1);\n pow_2[0] = 1;\n \n ll MOD = 1000000007;\n for (int i = 0; i < 20010; i++) {\n pow_2.push_back(pow_2[i] * 2LL);\n if (i >= sh) {\n pow_2.back()%=MOD;\n }\n }\n\n\n vector<vector<vector<ll>>> dist(\n 2, vector<vector<ll>>(sh + 1, vector<ll>(N+1, INF)));\n vector<vector<vector<ll>>> f(\n 2, vector<vector<ll>>(sh + 1, vector<ll>(N+1, 0)));\n prique<tuple<ll,ll,ll,ll>> que;\n\n que.emplace(0, 0, 0, 0);\n dist[0][0][0]=0;\n while (!que.empty()) {\n auto [d, v, k, o] = que.top();\n d=-d;\n que.pop();\n if (f[o][k][v]) {\n continue;\n }\n if (k == sh) {\n continue;\n }\n f[o][k][v] = true;\n for (auto [nv, w] : GP[v]) {\n if (dist[0][k][nv] > d + w) {\n dist[0][k][nv]=d+w;\n que.emplace(-d-w, nv,k,0);\n }\n }\n for (auto [nv, w] : GQ[v]) {\n ll cost = d + w, nk=k;\n if (o == 0) {\n cost += pow_2[k];\n nk++;\n }\n if (dist[1][nk][nv] > cost) {\n dist[1][nk][nv]=cost;\n que.emplace(-cost, nv, nk,1);\n }\n }\n }\n ll ans1 = INF;\n for (int i = 0; i <= sh; i++) {\n for (int j = 0; j < 2; j++) {\n ans1=min(ans1,dist[j][i][N-1]);\n }\n }\n if (ans1 != INF) {\n print(ans1 % MOD);\n return true;\n }\n\n //左側の方が小さいならtrueを返す\n auto hikaku = [&](ll k1, ll k2, ll d1, ll d2) {\n if (k1 == k2) {\n return d1<d2;\n }\n return k1<k2;\n };\n\n vector<vector<ll>> dist2(N, vector<ll>(2, INF));\n vector<vector<ll>> k_cnt(N,vector<ll>(2, INF));\n vector<vector<ll>> f2(N, vector<ll>(2, 0));\n prique<tuple<ll,ll,ll,ll>> que2;\n dist2[0][0] = 0;\n k_cnt[0][0]=0;\n que2.emplace(0,0, 0,0);\n while (!que2.empty()) {\n auto [k, d, v, o] = que2.top();\n que2.pop();\n k = -k;\n d = -d;\n if (f2[v][o]) {\n continue;\n }\n f2[v][o] = true;\n\n for (auto [nv, w] : GP[v]) {\n if (hikaku(k, k_cnt[nv][0], d+w, dist2[nv][0])) {\n dist2[nv][0] = d + w;\n k_cnt[nv][0] = k;\n que2.emplace(-k,-d-w, nv,0);\n }\n }\n for (auto [nv, w] : GQ[v]) {\n ll cost = d + w, nk=k;\n if (o == 0) {\n nk++;\n }\n if (hikaku(nk, k_cnt[nv][1], d + w, dist2[nv][1])) {\n dist2[nv][1] = d + w;\n k_cnt[nv][1] = nk;\n que2.emplace(-nk,-d-w, nv,1);\n }\n }\n }\n if (k_cnt[N - 1][0] == INF && k_cnt[N - 1][1] == INF) {\n print(-1);\n return true;\n }\n\n ll d1 = dist2[N - 1][0], d2 = dist2[N - 1][1];\n ll k1 = k_cnt[N - 1][0], k2 = k_cnt[N - 1][1];\n\n ll final_k=0, ans=0;\n if (hikaku(k1, k2, d1, d2)) {\n final_k = k1;\n ans=d1;\n } else {\n final_k = k2;\n ans=d2;\n }\n for (int i = 0; i < final_k; i++) {\n ans += pow_2[i];\n ans%=MOD;\n }\n print(ans);\n\n return true;\n \n\n\n\n\n\n}\n\n///////////////////ここから//////////////////////\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve());\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n/////////////////////初期化///////////////////////\n\ninline string input() {\n string S;\n cin >> S;\n return S;\n}\ninline ll I() {\n ll ret;\n cin >> ret;\n return ret;\n}\ninline pll II() {\n pll ret;\n cin >> ret.first >> ret.second;\n return ret;\n}\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 46880, "score_of_the_acc": -0.166, "final_rank": 5 }, { "submission_id": "aoj_1652_10565760", "code_snippet": "//#pragma GCC target(\"avx2\")\n//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pli = pair<ll,int>;\n//#define AMARI 998244353\n#define AMARI 1000000007\n#define el '\\n'\n#define El '\\n'\n#define YESNO(x) ((x) ? \"Yes\" : \"No\")\n#define YES YESNO(true)\n#define NO YESNO(false)\n#define REV_PRIORITY_QUEUE(tp) priority_queue<tp,vector<tp>,greater<tp>>\n#define EXIT_ANS(x) {cout << (x) << '\\n'; return;}\ntemplate <typename T> void inline SORT(vector<T> &v){sort(v.begin(),v.end()); return;}\ntemplate <typename T> void inline VEC_UNIQ(vector<T> &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}\ntemplate <typename T> T inline MAX(vector<T> &v){return *max_element(v.begin(),v.end());}\ntemplate <typename T> T inline MIN(vector<T> &v){return *min_element(v.begin(),v.end());}\ntemplate <typename T> T inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}\ntemplate <typename T> void inline DEC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;}\ntemplate <typename T> void inline INC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;}\nvoid inline TEST(void){cerr << \"TEST\" << endl; return;}\ntemplate <typename T> bool inline chmin(T &x,T y){\n if(x > y){\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T> bool inline chmax(T &x,T y){\n if(x < y){\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T = long long> vector<T> inline get_vec(int n){\n vector<T> ans(n);\n for(int i = 0; i < n; i++)cin >> ans[i];\n return ans;\n}\ntemplate <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){\n int n = (int)vec.size();\n for(int i = 0; i < n; i++){\n cout << vec[i];\n if(kaigyou || i == n - 1)cout << '\\n';\n else cout << ' ';\n }\n if(!n)cout << '\\n';\n return;\n}\ntemplate <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){\n int n = (int)vec.size();\n for(int i = 0; i < n; i++){\n cerr << vec[i];\n if(kaigyou || i == n - 1)cerr << '\\n';\n else cerr << ' ';\n }\n if(!n)cerr << '\\n';\n return;\n}\nvector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false){\n if(m == -1)m = n - 1;\n vector<vector<int>> g(n);\n while(m--){\n int u,v;\n cin >> u >> v;\n u--; v--;\n g[u].push_back(v);\n if(!direct)g[v].push_back(u);\n }\n return g;\n}\n\nvector<int> make_inv(vector<int> p){\n int n = (int)p.size();\n vector<int> ans(n);\n for(int i = 0; i < n; i++)ans[p[i]] = i;\n return ans;\n}\n\n\n#define IKITI 50\n\n#define MULTI_TEST_CASE false\nvoid solve(void){\n //問題を見たらまず「この問題設定から言えること」をいっぱい言う\n //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く\n //複数の解法のアイデアを思いついた時は全部メモしておく\n //g++ -D_GLIBCXX_DEBUG -Wall -O2 XXX.cpp -o o\n //N,P,Q <= 2e4\n int n;\n cin >> n;\n int p,q;\n cin >> p >> q;\n if(n == 0)exit(0);\n vector<vector<pli>> g(2 * n);\n while(p--){\n int u,v;\n ll w;\n cin >> u >> v >> w;\n u--; v--;\n g[u].push_back(pair(w,v));\n g[v].push_back(pair(w,u));\n }\n while(q--){\n int u,v;\n ll w;\n cin >> u >> v >> w;\n u--; v--;\n g[u + n].push_back(pair(w,v + n));\n g[v + n].push_back(pair(w,u + n));\n }\n //TEST();\n for(int i = 0; i < n; i++){\n g[i].push_back(pair(-1LL,i + n));\n g[i + n].push_back(pair(0LL,i));\n }\n\n n *= 2;\n //{time,taxi_cnt,idx}\n using tlll = tuple<ll,ll,ll>;\n priority_queue<tlll,vector<tlll>,greater<tlll>> que;\n que.push(tuple(0,0,0));\n\n vector<vector<ll>> dist(n,vector<ll>(IKITI + 1,LLONG_MAX / 2));\n vector<vector<bool>> seen(n,vector<bool>(IKITI + 1,false));\n dist[0][0] = 0LL;\n while(!que.empty()){\n ll time;\n int taxi_cnt,fr;\n tie(time,taxi_cnt,fr) = que.top(); que.pop();\n if(seen[fr][taxi_cnt])continue;\n seen[fr][taxi_cnt] = true;\n\n for(int i = 0; i < (int)g[fr].size(); i++){\n ll w;\n int to;\n tie(w,to) = g[fr][i];\n if(w == -1){\n if(taxi_cnt == IKITI)continue;\n if(chmin(dist[to][taxi_cnt + 1],dist[fr][taxi_cnt] + (1LL << taxi_cnt))){\n que.push(tuple(time + (1LL << taxi_cnt),taxi_cnt + 1,to));\n }\n continue;\n }\n if(chmin(dist[to][taxi_cnt],dist[fr][taxi_cnt] + w)){\n que.push(tuple(time + w,taxi_cnt,to));\n }\n }\n }\n\n ll ans = LLONG_MAX;\n for(int i = 0; i <= IKITI; i++){\n //chmin(ans,dist[n - 1][i]);\n chmin(ans,dist[n / 2 - 1][i]);\n }\n if(ans != LLONG_MAX / 2){\n EXIT_ANS(ans % AMARI);\n }\n assert(que.empty());\n //{taxi_cnt,time,idx}\n que.push(tuple(0,0,0));\n vector<pair<int,ll>> dist2(n,pair(INT_MAX / 2,LLONG_MAX / 2));\n dist2[0] = pair(0,0LL);\n vector<bool> seen2(n);\n\n while(!que.empty()){\n int taxi_cnt,fr;\n ll time;\n tie(taxi_cnt,time,fr) = que.top(); que.pop();\n if(seen2[fr])continue;\n seen2[fr] = true;\n for(int i = 0; i < (int)g[fr].size(); i++){\n ll w; int to;\n tie(w,to) = g[fr][i];\n if(w == -1){\n pair<int,ll> temp;\n temp.first = taxi_cnt + 1;\n temp.second = time;\n if(chmin(dist2[to],temp)){\n que.push(tuple(taxi_cnt + 1,time,to));\n }\n continue;\n }\n\n pair<int,ll> temp;\n temp.first = taxi_cnt;\n temp.second = time + w;\n if(chmin(dist2[to],temp)){\n que.push(tuple(taxi_cnt,time + w,to));\n }\n }\n }\n\n if(!seen2[n / 2 - 1]){\n EXIT_ANS(-1);\n }\n\n ans = dist2[n/2 - 1].second;\n ll ni = 1;\n for(int i = 0; i < dist2[n/2 - 1].first; i++){\n ni *= 2; ni %= AMARI;\n }\n ni--;\n ans += ni;\n ans %= AMARI;\n EXIT_ANS(ans);\n}\n\nvoid calc(void){\n return;\n}\n\n\nsigned main(void){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n calc();\n int t = 1;\n if(MULTI_TEST_CASE)cin >> t;\n while(1){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2270, "memory_kb": 38692, "score_of_the_acc": -0.3918, "final_rank": 11 }, { "submission_id": "aoj_1652_10555727", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define ALL(x) (x).begin(),(x).end()\n#define IO ios::sync_with_stdio(false),cin.tie(nullptr);\n#define REP(i, n) for(ll i=0; i<(ll)(n); i++)\n#define FOR(i, a, b) for(ll i=(ll)(a); i<(b); i++)\n#define ROF(i, a, b) for(ll i=(ll)(b)-1; i>=(a); i--)\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n\ntemplate<typename T> int LB(const vector<T>& v, T x) { return lower_bound(ALL(v),x)-(v).begin(); }\ntemplate<typename T> int UQ(T& v) { sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); return v.size(); }\ntemplate<typename T> bool chmax(T &a, T b) { return a<b ? a=b, true : false; }\ntemplate<typename T> bool chmin(T &a, T b) { return a>b ? a=b, true : false; }\ntemplate<typename T> using rpriority_queue=priority_queue<T,vector<T>,greater<T>>;\nusing ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double;\nusing ull=unsigned long long; using lll=__int128_t; using VST=vector<string>;\nusing VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;\nusing PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;\n\n#ifdef LOCAL\n#include \"./debug.hpp\"\n#else\n#define debug(...)\n#define print_line\n#endif\n\n\n\n/// @brief ModInt\ntemplate<ll MOD>\nstruct ModInt {\n ModInt(ll x=0){ value=(x>=0?x%MOD:MOD-(-x)%MOD); }\n\n ModInt operator-() const { return ModInt(-value); }\n ModInt operator+() const { return ModInt(*this); }\n ModInt& operator+=(const ModInt& other) {\n value+=other.value;\n if(value>=MOD) value-=MOD;\n return*this;\n }\n ModInt& operator-=(const ModInt& other) {\n value+=MOD-other.value;\n if(value>=MOD) value-=MOD;\n return*this;\n }\n ModInt& operator*=(const ModInt other) {\n value=value*other.value%MOD;\n return*this;\n }\n ModInt& operator/=(ModInt other) {\n (*this)*=other.inv();\n return*this;\n }\n ModInt operator+(const ModInt& other) const { return ModInt(*this)+=other; }\n ModInt operator-(const ModInt& other) const { return ModInt(*this)-=other; }\n ModInt operator*(const ModInt& other) const { return ModInt(*this)*=other; }\n ModInt operator/(const ModInt& other) const { return ModInt(*this)/=other; }\n bool operator==(const ModInt& other) const { return value==other.value; }\n bool operator!=(const ModInt& other) const { return value!=other.value; }\n friend ostream& operator<<(ostream& os, const ModInt& x) { return os<<x.value; }\n friend istream& operator>>(istream& is, ModInt& x) {\n ll v;\n is>>v;\n x=ModInt<MOD>(v);\n return is;\n }\n\n ModInt pow(ll x) const { \n ModInt ret(1),mul(value);\n while(x) {\n if(x&1) ret*=mul;\n mul*=mul;\n x>>=1;\n }\n return ret;\n }\n ModInt inv() const { return pow(MOD-2); }\n ll val() {return value; }\n static constexpr ll get_mod() { return MOD; }\n\nprivate:\n ll value;\n};\n\nusing Mod998=ModInt<998244353>;\nusing Mod107=ModInt<1000000007>;\nusing VM=vector<Mod998>;\n\nusing mint=Mod107;\n//----------------------------------------------------------\n\n\nbool solve() {\n ll N,P,Q; cin>>N>>P>>Q;\n if(N==0) return false;\n\n WG G(N),H(N);\n REP(i,P) {\n ll u,v,w; cin>>u>>v>>w; u--; v--;\n G[u].push_back({v,w});\n G[v].push_back({u,w});\n }\n REP(i,Q) {\n ll u,v,w; cin>>u>>v>>w; u--; v--;\n H[u].push_back({v,w});\n H[v].push_back({u,w});\n }\n\n {\n rpriority_queue<PL> pq;\n pq.push({0,0});\n VL dist(N,INF);\n dist[0]=0;\n while(!pq.empty()) {\n auto [d,now]=pq.top(); pq.pop();\n if(d>dist[now]) continue;\n debug(d,now);\n for(auto[a,b]:G[now]) if(chmin(dist[a],dist[now])) pq.push({dist[a],a});\n for(auto[a,b]:H[now]) if(chmin(dist[a],dist[now]+1)) pq.push({dist[a],a});\n }\n debug(dist);\n\n if(dist[N-1]==INF) {\n cout<<-1<<'\\n';\n return true;\n }\n\n\n if(dist[N-1]>=51) {\n ll mx=dist[N-1];\n //タクシーに乗ってるか、載っていいないか\n rpriority_queue<tuple<ll,ll,ll,ll>> pq;\n pq.push({0,0,0,0});\n vector<VP> dp(N,VP(2,{INFL,INFL}));\n dp[0][0]={0,0};\n while(!pq.empty()) {\n auto [cnt,tmp,now,taxi]=pq.top(); pq.pop();\n if(dp[now][taxi]<PL{cnt,tmp}) continue;\n\n ll ncnt=taxi==1?cnt:cnt+1;\n\n //taxi H\n if(ncnt<=mx) {\n for(auto [nxt,w]: H[now]) {\n if(chmin(dp[nxt][1],{ncnt,dp[now][taxi].second+w})) {\n pq.push({dp[nxt][1].first,dp[nxt][1].second,nxt,1});\n }\n }\n }\n //walk\n for(auto [nxt,w]: G[now]) {\n if(chmin(dp[nxt][0],{cnt,dp[now][taxi].second+w})) {\n pq.push({dp[nxt][0].first,dp[nxt][0].second,nxt,0});\n }\n }\n }\n\n auto [cnt,dist]=min(dp[N-1][0],dp[N-1][1]);\n mint ans=mint(2).pow(cnt)-1;\n ans+=dist;\n cout<<ans<<'\\n';\n return true;\n }\n }\n\n const int L=50;\n vector<VVL> dp(N,VVL(L+1,VL(2,INFL)));\n dp[0][0][0]=0;\n rpriority_queue<tuple<ll,ll,ll,ll>> pq;\n pq.push({0,0,0,0});\n\n while(!pq.empty()) {\n auto [tmp,now,cnt,taxi]=pq.top(); pq.pop();\n\n if(tmp>dp[now][cnt][taxi]) continue;\n\n debug(tmp,now,cnt,taxi);\n\n ll tak=taxi==1?0:1ll<<cnt;\n ll ncnt=taxi==1?cnt:cnt+1;\n\n //taxi H\n if(ncnt<=L) {\n for(auto [nxt,w]: H[now]) {\n if(chmin(dp[nxt][ncnt][1],dp[now][cnt][taxi]+w+tak)) {\n pq.push({dp[nxt][ncnt][1],nxt,ncnt,1});\n }\n }\n }\n //walk\n for(auto [nxt,w]: G[now]) {\n if(chmin(dp[nxt][cnt][0],dp[now][cnt][taxi]+w)) {\n pq.push({dp[nxt][cnt][0],nxt,cnt,0});\n }\n }\n }\n\n ll ans=INFL;\n REP(i,L+1) REP(j,2) chmin(ans,dp[N-1][i][j]);\n\n if(ans==INFL) {\n cout<<-1<<'\\n';\n return true;\n }\n\n ll mod=1e9+7;\n cout<<ans%mod<<'\\n';\n\n return true;\n}\n\n\nint main() {\n IO;\n while(solve());\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 66592, "score_of_the_acc": -0.3955, "final_rank": 12 }, { "submission_id": "aoj_1652_10389622", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\nusing S = tuple<ll,ll,ll,ll>;\n\nll solve1(ll n,ll p,ll q,vvpll edge1,vvpll edge2){\n vvvll dist(n,vvll(40,vll(2,1LL<<60)));\n priority_queue<S,vector<S>,greater<S>> d;\n dist[0][0][0] = 0;\n d.push({0,0,0,0});\n while(!d.empty()){\n auto [z,now,cnt,s] = d.top();\n d.pop();\n if(z > dist[now][cnt][s]){continue;}\n if(s == 0){\n for(auto [to,cost]:edge1[now]){\n if(dist[to][cnt][s] > dist[now][cnt][s] + cost){\n dist[to][cnt][s] = dist[now][cnt][s] + cost;\n d.push({dist[to][cnt][s],to,cnt,s});\n }\n }\n if(cnt+1 < 40 && dist[now][cnt+1][s^1] > dist[now][cnt][s] + 0){\n dist[now][cnt+1][s^1] = dist[now][cnt][s] + 0;\n d.push({dist[now][cnt+1][s^1],now,cnt+1,s^1});\n }\n }\n else{\n for(auto [to,cost]:edge2[now]){\n if(dist[to][cnt][s] > dist[now][cnt][s] + cost){\n dist[to][cnt][s] = dist[now][cnt][s] + cost;\n d.push({dist[to][cnt][s],to,cnt,s});\n }\n }\n if(cnt+1 < 40 && dist[now][cnt][s^1] > dist[now][cnt][s] + 0){\n dist[now][cnt][s^1] = dist[now][cnt][s] + 0;\n d.push({dist[now][cnt][s^1],now,cnt,s^1});\n }\n }\n \n \n }\n ll ans = 1LL<<60;\n rep(i,40){\n ans = min(ans,min(dist[n-1][i][0],dist[n-1][i][1]) + (1LL<<i)-1);\n //print(i,min(dist[n-1][i][0],dist[n-1][i][1]));\n }\n return ans;\n}\nusing F = tuple<ll,ll,ll>;\npll solve2(ll n,ll p,ll q,vvpll edge1,vvpll edge2){\n vvpll dist(n,vpll(2,{1LL<<60,1LL<<60}));\n priority_queue<S,vector<S>,greater<S>> d;\n dist[0][0] = {0,0};\n d.push({0,0,0,0});\n while(!d.empty()){\n auto [z,zz,now,s] = d.top();\n d.pop();\n if(make_pair(z,zz) > dist[now][s]){continue;}\n if(s == 0){\n for(auto [to,cost]:edge1[now]){\n if(dist[to][s] > make_pair(dist[now][s].first,dist[now][s].second + cost)){\n dist[to][s] = make_pair(dist[now][s].first,dist[now][s].second + cost);\n d.push({dist[to][s].first,dist[to][s].second,to,s});\n }\n }\n if(dist[now][s^1] > make_pair(dist[now][s].first + 1,dist[now][s].second)){\n dist[now][s^1] = make_pair(dist[now][s].first + 1,dist[now][s].second);\n d.push({dist[now][s^1].first,dist[now][s^1].second,now,s^1});\n }\n }\n else{\n for(auto [to,cost]:edge2[now]){\n if(dist[to][s] > make_pair(dist[now][s].first,dist[now][s].second + cost)){\n dist[to][s] = make_pair(dist[now][s].first,dist[now][s].second + cost);\n d.push({dist[to][s].first,dist[to][s].second,to,s});\n }\n }\n if(dist[now][s^1] > make_pair(dist[now][s].first,dist[now][s].second)){\n dist[now][s^1] = make_pair(dist[now][s].first,dist[now][s].second);\n d.push({dist[now][s^1].first,dist[now][s^1].second,now,s^1});\n }\n }\n \n \n }\n return min(dist[n-1][0],dist[n-1][1]);\n}\n\nint main(){\n while(1){\n LL(n,p,q);\n //if(n==4){break;}\n if(n == 0){break;}\n vvpll edge1(n);\n rep(i,p){\n LL(a,b,c);\n a--;\n b--;\n edge1[a].push_back({b,c});\n edge1[b].push_back({a,c});\n }\n vvpll edge2(n);\n rep(i,q){\n LL(a,b,c);\n a--;\n b--;\n edge2[a].push_back({b,c});\n edge2[b].push_back({a,c});\n }\n ll ans = solve1(n,p,q,edge1,edge2);\n if(ans != (1LL<<60)){\n print(ans % (1000000000 + 7));\n continue;\n }\n pll ans3 = solve2(n,p,q,edge1,edge2);\n if(ans3 == make_pair(1LL<<60,1LL<<60)){\n print(-1);\n continue;\n }\n else{\n ll ans2 = modpow(2,ans3.first,1000000000+7)-1 + ans3.second;\n ans2 %= 1000000000 + 7;\n print(ans2);\n continue;\n }\n\n }\n}", "accuracy": 1, "time_ms": 1330, "memory_kb": 84324, "score_of_the_acc": -0.3893, "final_rank": 10 }, { "submission_id": "aoj_1652_9458287", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\ntypedef pair<ll,ppi> pppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nint main(){\n while(1){\n ll N,_,__;cin>>N>>_>>__;\n if(!N)return 0;\n vector<vector<pi>>E(N),F(N);\n REP(i,_){\n ll a,b,c;cin>>a>>b>>c;a--;b--;\n E[a].emplace_back(b,c);\n E[b].emplace_back(a,c);\n }\n REP(i,__){\n ll a,b,c;cin>>a>>b>>c;a--;b--;\n F[a].emplace_back(b,c);\n F[b].emplace_back(a,c);\n }\n vvvi DP(2,vvi(60,vi(N,1e18)));\n DP[0][0][0]=0;\n min_priority_queue<pppi>Q;\n Q.emplace(pppi(0,ppi(0,pi(0,0))));\n while(sz(Q)){\n ll d=Q.top().F,tx=Q.top().S.F,c=Q.top().S.S.F,v=Q.top().S.S.S;Q.pop();\n if(d!=DP[tx][c][v])continue;\n if(tx){\n for(auto[u,x]:E[v])if(chmin(DP[0][c][u],d+x))Q.emplace(pppi(d+x,ppi(0,pi(c,u))));\n for(auto[u,x]:F[v])if(chmin(DP[1][c][u],d+x))Q.emplace(pppi(d+x,ppi(1,pi(c,u))));\n }\n else{\n for(auto[u,x]:E[v])if(chmin(DP[0][c][u],d+x))Q.emplace(pppi(d+x,ppi(0,pi(c,u))));\n for(auto[u,x]:F[v])if(c+1<60&&chmin(DP[1][c+1][u],d+x))Q.emplace(pppi(d+x,ppi(1,pi(c+1,u))));\n }\n }\n ll ans=8e18;\n REP(i,60){\n ll c=min(DP[0][i][N-1],DP[1][i][N-1]);\n chmin(ans,c+(1LL<<i)-1);\n }\n if(ans<1e17){\n cout<<ans%1000000007<<endl;\n continue;\n }\n {\n vector<vector<pi>>D(2,vector<pi>(N,pi(1e18,1e18)));\n D[0][0]=pi(0,0);\n min_priority_queue<pair<pi,pi>>Q;\n Q.emplace(D[0][0],pi(0,0));\n while(sz(Q)){\n ll tx=Q.top().S.F,v=Q.top().S.S;auto d=Q.top().F;Q.pop();\n if(d!=D[tx][v])continue;\n for(auto[u,x]:E[v])if(chmin(D[0][u],pi(d.F,d.S+x)))Q.emplace(D[0][u],pi(0,u));\n if(tx){\n for(auto[u,x]:F[v])if(chmin(D[1][u],pi(d.F,d.S+x)))Q.emplace(D[1][u],pi(1,u));\n }\n else{\n for(auto[u,x]:F[v])if(chmin(D[1][u],pi(d.F+1,d.S+x)))Q.emplace(D[1][u],pi(1,u));\n }\n }\n auto ans=min(D[0][N-1],D[1][N-1]);\n if(ans.F>1e17)cout<<-1<<endl;\n else{\n modint<1000000007>a=1;\n REP(i,ans.F)a*=2;\n a+=-1+ans.S;\n cout<<a.val()<<endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1380, "memory_kb": 34852, "score_of_the_acc": -0.1862, "final_rank": 6 }, { "submission_id": "aoj_1652_9418534", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct Edge {\n int To;\n ll Len;\n int Type;\n};\n\nint MAXS = 40;\n\ntypedef pair<ll,pair<int,pair<int,int>>> PP;\ntypedef pair<pair<int,ll>,pair<int,int>> PP2;\n\nll MOD = 1000000007;\n\nll Calc(int X) {\n if (X == 0) return 1;\n if (X % 2 == 0) {\n ll Y = Calc(X/2);\n return (Y*Y)%MOD;\n }\n return (2*Calc(X-1))%MOD;\n}\n\nint main() {\nwhile(1) {\n int N, P, Q;\n cin >> N >> P >> Q;\n if (N == 0 && P == 0 && Q == 0) return 0;\n vector<vector<Edge>> G(N);\n rep(i,0,P) {\n int A, B;\n ll C;\n cin >> A >> B >> C;\n A--,B--;\n G[A].push_back({B,C,0});\n G[B].push_back({A,C,0});\n }\n rep(i,0,Q) {\n int A, B;\n ll C;\n cin >> A >> B >> C;\n A--, B--;\n G[A].push_back({B,C,1});\n G[B].push_back({A,C,1});\n }\n vector<vector<vector<ll>>> DP(N,vector<vector<ll>>(MAXS,vector<ll>(2,INF)));\n priority_queue<PP,vector<PP>,greater<PP>> PQ;\n DP[0][0][0] = 0;\n PQ.push({0,{0,{0,0}}});\n while(!PQ.empty()) {\n PP P = PQ.top();\n PQ.pop();\n ll Le = P.first;\n int Ve = P.second.first, Co = P.second.second.first, Ri = P.second.second.second;\n if (DP[Ve][Co][Ri]<Le) continue;\n for (Edge E : G[Ve]) {\n if (E.Type == 0 && Ri == 0) {\n ll NLe = Le + E.Len;\n int NVe = E.To, NCo = Co, NRi = Ri;\n if (DP[NVe][NCo][NRi] > NLe) {\n DP[NVe][NCo][NRi] = NLe;\n PQ.push({NLe,{NVe,{NCo,NRi}}});\n }\n }\n if (E.Type == 1 && Ri == 1) {\n ll NLe = Le + E.Len;\n int NVe = E.To, NCo = Co, NRi = Ri;\n if (DP[NVe][NCo][NRi] > NLe) {\n DP[NVe][NCo][NRi] = NLe;\n PQ.push({NLe,{NVe,{NCo,NRi}}});\n }\n }\n if (Ri == 0 && Co + 1 != MAXS) {\n ll NLe = Le + (1LL<<Co);\n int NVe = Ve, NCo = Co + 1, NRi = 1;\n if (DP[NVe][NCo][NRi] > NLe) {\n DP[NVe][NCo][NRi] = NLe;\n PQ.push({NLe,{NVe,{NCo,NRi}}});\n }\n }\n if (Ri == 1) {\n ll NLe = Le;\n int NVe = Ve, NCo = Co, NRi = 0;\n if (DP[NVe][NCo][NRi] > NLe) {\n DP[NVe][NCo][NRi] = NLe;\n PQ.push({NLe,{NVe,{NCo,NRi}}});\n }\n }\n }\n }\n ll ANS = INF;\n rep(i,0,MAXS) rep(j,0,2) chmin(ANS,DP[N-1][i][j]);\n if (ANS != INF) {\n cout << ANS%MOD << endl;\n continue;\n }\n vector<vector<pair<int,ll>>> DP2(N,vector<pair<int,ll>>(2,{inf,INF}));\n DP2[0][0] = {0,0};\n priority_queue<PP2,vector<PP2>,greater<PP2>> PQ2;\n PQ2.push({{0,0},{0,0}});\n while(!PQ2.empty()) {\n PP2 P = PQ2.top();\n PQ2.pop();\n int Co = P.first.first;\n ll Le = P.first.second;\n int Ve = P.second.first;\n int Ri = P.second.second;\n if (P.first > DP2[Ve][Ri]) continue;\n int NCo;\n ll NLe;\n int NVe;\n int NRi;\n if (Ri == 0) {\n NCo = Co + 1, NLe = Le, NVe = Ve, NRi = 1;\n if (DP2[NVe][NRi] > pair<int,ll>{NCo, NLe}) {\n DP2[NVe][NRi] = {NCo, NLe};\n PQ2.push({{NCo,NLe},{NVe,NRi}});\n }\n }\n if (Ri == 1) {\n NCo = Co, NLe = Le, NVe = Ve, NRi = 0;\n if (DP2[NVe][NRi] > pair<int,ll>{NCo, NLe}) {\n DP2[NVe][NRi] = {NCo, NLe};\n PQ2.push({{NCo,NLe},{NVe,NRi}});\n }\n }\n for (Edge E : G[Ve]) {\n if (E.Type == 0 && Ri == 0) {\n NCo = Co, NLe = Le + E.Len, NVe = E.To, NRi = 0;\n if (DP2[NVe][NRi] > pair<int,ll>{NCo, NLe}) {\n DP2[NVe][NRi] = {NCo, NLe};\n PQ2.push({{NCo,NLe},{NVe,NRi}});\n }\n }\n if (E.Type == 1 && Ri == 1) {\n NCo = Co, NLe = Le + E.Len, NVe = E.To, NRi = 1;\n if (DP2[NVe][NRi] > pair<int,ll>{NCo, NLe}) {\n DP2[NVe][NRi] = {NCo, NLe};\n PQ2.push({{NCo,NLe},{NVe,NRi}});\n }\n }\n }\n }\n pair<int,ll> ANS2 = min(DP2[N-1][0], DP2[N-1][1]);\n if (ANS2 == pair<int,ll>{inf,INF}) {\n cout << -1 << endl;\n }\n else {\n cout << (Calc(ANS2.first) + ANS2.second + MOD-1)%MOD << endl;\n }\n}\n}", "accuracy": 1, "time_ms": 2070, "memory_kb": 62816, "score_of_the_acc": -0.4535, "final_rank": 14 }, { "submission_id": "aoj_1652_9412606", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n \n#include <bits/stdc++.h>\n// #include <atcoder/all>\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vc<vv<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst double pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\nconst ll mod = 1000000007;\n// const ll mod = 998244353;\ninline bool inside(long long y, long long x, long long H, long long W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\ntemplate<typename T> void view(const set<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\n\n\nll pow_mod(ll a, ll d, ll m){\n ll ret = 1;\n while (d > 0){\n if (d & 1){\n ret *= a;\n ret %= m;\n }\n a = a * a % m;\n d >>= 1;\n }\n return ret;\n}\n\n\nvl dijkstra(int s, int N, vv<pair<int, ll>> &g){\n using P = pair<ll, int>;\n vl dist(N, INF);\n priority_queue<P, vc<P>, greater<P>> q;\n dist[s] = 0;\n q.emplace(make_pair(0, s));\n while (!q.empty()){\n P tmp = q.top();q.pop();\n ll c = tmp.first; int now = tmp.second;\n if (dist[now] < c) continue;\n for (auto&& [nxt, nc]: g[now]){\n if (INF - nc > dist[now] && dist[nxt] > dist[now] + nc){\n dist[nxt] = dist[now] + nc;\n q.emplace(make_pair(dist[nxt], nxt));\n }\n }\n }\n return dist;\n}\n\nll solve(int N, int p, int q){\n int taxi = 40;\n ll lim = 100100100100;\n vv<pair<int, ll>> g(N * taxi * 2);\n vv<pair<int, ll>> ng(N * 2);\n rep(i, p){\n int a, b; ll c; cin >> a >> b >> c; a--; b--;\n rep(t, taxi){\n g[a * taxi * 2 + t * 2].push_back({b * taxi * 2 + t * 2, c});\n g[b * taxi * 2 + t * 2].push_back({a * taxi * 2 + t * 2, c});\n ng[a * 2].push_back({b * 2, c});\n ng[b * 2].push_back({a * 2, c});\n }\n }\n rep(i, q){\n int d, e; ll f; cin >> d >> e >> f; d--; e--;\n rep(t, taxi){\n g[d * taxi * 2 + t * 2 + 1].push_back({e * taxi * 2 + t * 2 + 1, f});\n g[e * taxi * 2 + t * 2 + 1].push_back({d * taxi * 2 + t * 2 + 1, f});\n ng[d * 2 + 1].push_back({e * 2 + 1, f});\n ng[e * 2 + 1].push_back({d * 2 + 1, f});\n }\n }\n rep(u, N) rep(t, taxi - 1){\n g[u * taxi * 2 + t * 2].push_back({u * taxi * 2 + (t + 1) * 2 + 1, 1ll << t});\n g[u * taxi * 2 + (t + 1) * 2 + 1].push_back({u * taxi * 2 + (t + 1) * 2, 0});\n }\n auto dist = dijkstra(0, N * taxi * 2, g);\n\n ll ret = INF;\n rep(t, taxi) ret = min(ret, min(dist[(N - 1) * taxi * 2 + t * 2], dist[(N - 1) * taxi * 2 + t * 2 + 1]));\n if (ret == INF){\n rep(u, N){\n ng[u * 2].push_back({u * 2 + 1, lim});\n ng[u * 2 + 1].push_back({u * 2, 0});\n }\n auto ndist = dijkstra(0, 2 * N, ng);\n ll nw = ndist[(N - 1) * 2];\n if (nw == INF) ret = -1;\n else{\n ll cnt = nw / lim, dst = nw % lim;\n ret = (pow_mod(2, cnt, mod) - 1 + mod + dst) % mod;\n }\n }else ret %= mod;\n g.clear();\n g.shrink_to_fit();\n ng.clear();\n ng.shrink_to_fit();\n dist.clear();\n dist.shrink_to_fit();\n return ret;\n}\n\nint main(){\n vl ans;\n while (true){\n int N, p, q; cin >> N >> p >> q;\n if (N == 0) break;\n ans.push_back(solve(N, p, q));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 3920, "memory_kb": 252212, "score_of_the_acc": -1.6645, "final_rank": 18 }, { "submission_id": "aoj_1652_9384295", "code_snippet": "#if 1\n// clang-format off\n#include <bits/stdc++.h>\n\nusing namespace std;\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing lf = long double;\nusing pll = pair<ll, ll>;\n#define vec vector\ntemplate <class T> using v = vector<T>;\ntemplate <class T> using vv = v<v<T>>;\ntemplate <class T> using vvv = v<vv<T>>;\nusing vl = v<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vvv<ll>;\nusing vpl = v<pll>;\nusing vs = v<string>;\nusing vb = v<bool>;\nusing vvb = v<vb>;\nusing vvvb = v<vvb>;\ntemplate<class T> using PQ = priority_queue<T, v<T>, greater<T>>;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }\n\n#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)\n#define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++)\n#define rrep(i, N) for (ll i = N - 1; i >= 0; i--)\n#define rrep1(i, N) for (ll i = N; i > 0; i--)\n#define fore(i, a) for (auto &i : a)\n#define fs first\n#define sc second\n#define eb emplace_back\n#define pb push_back\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define UNIQUE(x) (x).erase(unique((x).begin(), (x).end()), (x).end());\n#define YES(x) cout << ((x) ? \"YES\\n\" : \"NO\\n\");\n#define Yes(x) cout << ((x) ? \"Yes\\n\" : \"No\\n\");\n#define yes(x) cout << ((x) ? \"yes\\n\" : \"no\\n\");\ntemplate <class T, class U> void chmin(T &t, const U &u) { if (t > u) t = u; }\ntemplate <class T, class U> void chmax(T &t, const U &u) { if (t < u) t = u; }\ntemplate <class T> T min(const v<T> &lis) { return *min_element(all(lis)); }\ntemplate <class T> T max(v<T> &lis) { return *max_element(all(lis)); }\nconst int inf = (1 << 30);\nconst ll infl = (1LL << 60);\nconst ll mod93 = 998244353;\nconst ll mod17 = 1000000007;\nint popcnt(uint x) { return __builtin_popcount(x); }\nint popcnt(ull x) { return __builtin_popcountll(x); }\n// 桁数\nint bsr(uint x) { return 31 - __builtin_clz(x); }\nint bsr(ull x) { return 63 - __builtin_clzll(x); }\n// 2で割れる回数\nint bsf(uint x) { return __builtin_ctz(x); }\nint bsf(ull x) { return __builtin_ctzll(x); }\n\ntemplate <class T, class S> istream &operator>>(istream &is, pair<T, S> &x) { return is >> x.first >> x.second; }\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &x) { return os << x.first << \" \" << x.second; }\ntemplate <class T> istream &operator>>(istream &is, vector<T> &x) { for (auto &y : x) is >> y; return is; }\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &x) {\n for (size_t i = 0, size = x.size(); i < size; i++)\n os << x[i] << (i == size - 1 ? \"\" : \" \");\n return os;\n}\n\nll rand_int(ll l, ll r) { // [l, r]\n static random_device rd;\n static mt19937 gen(rd());\n return uniform_int_distribution<ll>(l, r)(gen);\n}\n\n// #include <boost/multiprecision/cpp_int.hpp>\n// using cpp_int = boost::multiprecision::cpp_int;\n\n// clang-format on\n#endif\n\n// #define _GLIBCXX_DEBUG\nnamespace atcoder {\n\n namespace internal {\n\n // @param m `1 <= m`\n // @return x mod m\n constexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n }\n\n // Fast modular multiplication by barrett reduction\n // Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n // NOTE: reconsider after Ice Lake\n struct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m < 2^31`\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1)\n // < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n };\n\n // @param n `0 <= n`\n // @param m `1 <= m`\n // @return `(x ** n) % m`\n constexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n }\n\n // Reference:\n // M. Forisek and J. Jancina,\n // Fast Primality Testing for Integers That Fit into a Machine Word\n // @param n `0 <= n`\n constexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n }\n template <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n // @param b `1 <= b`\n // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\n constexpr std::pair<long long, long long> inv_gcd(\n long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n }\n\n // Compile time primitive root\n // @param m must be prime\n // @return primitive root (and minimum in now)\n constexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n template <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n } // namespace internal\n\n long long pow_mod(long long x, long long n, int m) {\n assert(0 <= n && 1 <= m);\n if (m == 1) return 0;\n internal::barrett bt((unsigned int)(m));\n unsigned int r = 1, y = (unsigned int)(internal::safe_mod(x, m));\n while (n) {\n if (n & 1) r = bt.mul(r, y);\n y = bt.mul(y, y);\n n >>= 1;\n }\n return r;\n }\n} // namespace atcoder\nusing namespace atcoder;\n\nll m = 50;\nstruct S {\n pll cost;\n ll now, cnt, cur;\n};\nbool operator<(S a, S b) {\n if (a.cost.fs == b.cost.fs || max(a.cost.fs, b.cost.fs) >= m) {\n return a.cost < b.cost;\n }\n ll ac = a.cost.sc + (1ll << a.cost.fs) - 1;\n ll bc = b.cost.sc + (1ll << b.cost.fs) - 1;\n return ac < bc;\n}\nbool operator>(S a, S b) {\n if (a.cost.fs == b.cost.fs || max(a.cost.fs, b.cost.fs) >= m) {\n return a.cost > b.cost;\n }\n ll ac = a.cost.sc + (1ll << a.cost.fs) - 1;\n ll bc = b.cost.sc + (1ll << b.cost.fs) - 1;\n return ac > bc;\n}\nstruct Solver {\n void solve(ll n, ll p, ll q) {\n using tup = tuple<ll, ll, ll>;\n vv<tup> g(n);\n rep(i, p) {\n ll a, b, c;\n cin >> a >> b >> c;\n a--, b--;\n g[a].eb(b, c, 0);\n g[b].eb(a, c, 0);\n }\n rep(i, q) {\n ll a, b, c;\n cin >> a >> b >> c;\n a--, b--;\n g[a].eb(b, c, 1);\n g[b].eb(a, c, 1);\n }\n\n vv<vpl> dp(n, v<vpl>(m + 1, vpl(2, {infl, infl})));\n dp[0][0][0] = {0, 0};\n v<vvb> used(n, vvb(m + 1, vb(2)));\n PQ<S> pq;\n pq.push({\n {0, 0},\n 0, 0, 0\n });\n while (pq.size()) {\n ll now = pq.top().now;\n ll cnt = pq.top().cnt;\n ll cur = pq.top().cur;\n if (cnt > m) cnt = m;\n pq.pop();\n // cout << now << \" \" << cnt << \"\\n\";\n ll cnt_i = min(cnt, m);\n if (used[now][cnt_i][cur]) continue;\n used[now][cnt_i][cur] = true;\n\n for (auto [to, c1, c2] : g[now]) {\n pll tmp = dp[now][cnt_i][cur];\n if (cur == 0) tmp.fs += c2;\n tmp.sc += c1;\n if (tmp < dp[to][min(tmp.fs, m)][c2]) {\n dp[to][min(tmp.fs, m)][c2] = tmp;\n pq.push({tmp, to, min(tmp.fs, m), c2});\n }\n }\n }\n // rep(i, n) {\n // rep(j, m + 1) { cout << \"(\" << dp[i][j] << \")\" << \" \\n\"[j == m]; }\n // }\n pll ans = {infl, infl};\n rep(i, m + 1) {\n rep(j, 2) {\n pll tmp = dp[n - 1][i][j];\n if (tmp.fs == ans.fs || max(tmp.fs, ans.fs) >= m) {\n chmin(ans, tmp);\n } else {\n ll tc = tmp.sc + (1ll << tmp.fs) - 1;\n ll ac = ans.sc + (1ll << ans.fs) - 1;\n if (tc < ac) ans = tmp;\n }\n }\n }\n // cout << ans << \"\\n\";\n if (ans == pll{infl, infl}) {\n cout << -1 << \"\\n\";\n return;\n }\n ll MOD = TEN(9) + 7;\n ll out = pow_mod(2, ans.fs, MOD) - 1 + ans.sc;\n out %= MOD;\n cout << out << \"\\n\";\n }\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n\n ll t = 1;\n // cin >> t;\n while (t) {\n ll n, p, q;\n cin >> n >> p >> q;\n if (n == 0) break;\n\n Solver solver;\n solver.solve(n, p, q);\n // break;\n }\n}", "accuracy": 1, "time_ms": 4140, "memory_kb": 155280, "score_of_the_acc": -1.2925, "final_rank": 16 }, { "submission_id": "aoj_1652_9378799", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n#define rrep(i,start,end) for (long long i = start;i >= (long long)(end);i--)\n#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)\n#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)\n#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef vector<long long> vll;\ntypedef vector<pair<long long ,long long>> vpll;\ntypedef vector<vector<long long>> vvll;\ntypedef set<ll> sll;\ntypedef map<long long , long long> mpll;\ntypedef pair<long long ,long long> pll;\ntypedef tuple<long long , long long , long long> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (int)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\n// for AOJ or ICPC or etc..\n// 全部が0だったらtrueを返す\ntemplate<class Tp> bool zero (const Tp &x) {return x == 0;}\ntemplate<class Tp, class... Args> bool zero (const Tp &x, const Args& ...args) {return zero(x) and zero(args...);}\n\n\nll modpow(ll a, ll n){\n ll ans = 1, cur = a;\n while (n > 0){\n if (n & 1) ans = ans * cur % MOD10;\n cur = cur * cur % MOD10;\n n >>= 1;\n }\n return ans;\n}\n// 変数をちゃんと全部受け取る!\nvoid solve(ll n,ll P, ll Q){\n vector<vpll> h(n),r(n);\n rep(i,P){\n LL(a,b,c);\n a--;b--;\n h[a].push_back({b,c});\n h[b].push_back({a,c});\n }\n rep(i,Q){\n LL(d,e,f);\n d--;e--;\n r[d].push_back({e,f});\n r[e].push_back({d,f});\n }\n ll kcnt = 0;\n {\n deque<ll> que;\n //0からの距離が入る\n vector<ll> find(2 * n,INF);\n find[0] = 0;\n que.push_back(0);\n while(!que.empty()){\n ll now = que.front();\n que.pop_front();\n for(auto &[p,tmp]:h[now % n]){\n if(find[p] > find[now]){\n find[p] = find[now];\n que.push_front(p);\n }\n }\n for(auto &[p,tmp]:r[now % n]){\n if(find[p+n] > find[now] + (ll)(now < n)){\n find[p+n] = find[now] + (ll)(now < n);\n if(now < n){\n que.push_back(p+n); \n }else{\n que.push_front(p+n);\n }\n }\n }\n }\n if(find[n-1] == INF && find[2 * n-1] == INF){\n cout << -1 << endl;\n return ;\n }else{\n kcnt = min(find[n-1],find[2 * n-1]);\n }\n }\n // cerr << kcnt << endl;\n if(kcnt >= 40){\n //kcnt回だけ乗るのが最適\n //乗ってる乗ってないの頂点倍化だけして{乗った回数,移動時間}でダイクストラ\n using P = pair<pair<ll,ll>,ll> ;\n vpll dis(2 * n,{INF,INF});//i<n: 降りてる i>=n: 乗ってる\n dis[0] = {0,0};\n priority_queue<P,vector<P>,greater<P>> pq;\n pq.push((P){dis[0],0});\n while(!pq.empty()){\n auto [np,now] = pq.top();\n pq.pop();\n if(dis[now] < np){\n continue;\n }\n auto [cnt,cost] = np;\n for(auto &p:h[now % n]){\n auto[next,hcost] = p;\n if(dis[next] > (pll){cnt,cost + hcost}){\n dis[next] = (pll){cnt,cost + hcost};\n pq.push({(pll){cnt,cost + hcost},next});\n }\n }\n for(auto &p:r[now % n]){\n auto[next,rcost] = p;\n if(dis[next+n] > (pll){cnt +(ll)(now < n),cost + rcost}){\n dis[next+n] = (pll){cnt +(ll)(now < n),cost + rcost};\n pq.push({(pll){cnt+(ll)(now < n),cost + rcost},next+n});\n }\n }\n }\n if(dis[n-1] < dis[2 * n-1]){\n // cerr << kcnt << \" \" << dis[n-1].first << endl;\n assert(dis[n-1].first == kcnt);\n ll ans = modpow(2,dis[n-1].first) -1;\n ans += dis[n-1].second;\n ans %= MOD10;\n cout << ans << endl;\n }else{\n assert(dis[2 * n-1].first == kcnt);\n ll ans = modpow(2,dis[2 *n-1].first) -1;\n ans += dis[2*n-1].second;\n ans %= MOD10;\n cout << ans << endl;\n }\n }else{\n //頂点倍化ダイクストラ\n // G[i]<j,d> i->j の距離(d)\n // 「仮の最短距離, 頂点」が小さい順に並ぶ\n vll dis(80 * n,INF);//[0,39]: i回乗って降りている [40,79] i-40回乗って乗っている\n priority_queue<pll,vector<pll>,greater<pll>>pq;\n dis[0] = 0;\n pq.push({dis[0],0});\n while(!pq.empty()){\n auto [len,now] = pq.top();\n pq.pop();\n if(dis[now]< len){\n continue;\n }\n for(auto &p:h[now % n]){\n // 注意!\n auto [next,cost] = p;\n ll times = now/ n;\n if(times >= 40){\n times -= 40;\n }\n next += n * times;\n if(dis[next] > dis[now] + cost){\n dis[next] = dis[now] + cost;\n pq.push({dis[next],next});\n }\n }\n for(auto &p:r[now % n]){\n // 注意!\n auto [next,cost] = p;\n ll times = now/ n;\n ll ncost = dis[now] + cost;\n if(times >= 40){\n //乗ってるので乗ったまま\n next += times * n;\n }else{\n //降りてるので次のに乗る\n if(times == 39){\n //乗る意味ないのでパス\n continue;\n }else{\n ncost += (1LL << times);\n times++;//次の\n times += 40;//乗る\n next += times * n;\n }\n }\n if(dis[next] > ncost){\n dis[next] = ncost;\n pq.push({dis[next],next});\n }\n }\n }\n\n ll ans = INF;\n rep(i,40){\n chmin(ans,dis[(i+1) * n-1]);\n chmin(ans,dis[(i+1+40) * n -1]);\n }\n cout << ans % MOD10 << endl;\n }\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n,P,Q);//変数数調整\n if(zero(n,P,Q))break;\n solve(n,P,Q);\n }\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 20740, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1660_cpp
Problem E Village of Lore Icpca village, said to lie in a deep jungle, is known by strange, contradictory lore. Some say there lies a epitome of happiness while others say it is a terrible fortress of horror. An old soothsayer, who is a precious source of information, reported that Icpca village has a rectangular form with streets of a grid pattern. n west-east straight streets and m north-south straight streets pass through the village. n and m are both even. To investigate the reality of the lore, a team of n + m researchers were sent to Icpca village. The plan of the investigation was as follows. The i -th of the first n researchers walks from the western end to the eastern end on the i -th street from the north. The j -th of the remaining m researchers walks from the northern end to the southern end on the j -th street from the west. Some of the researchers returned from Icpca village as planned. However, nothing has been heard from the rest... According to the pseudopsia of the old soothsayer, there stands exactly one statue on each of the nm intersections of west-east and north-south streets in Icpca village. Each statue is either beautiful or horrible. Each researcher has a value called sanity. All the researchers have the sanity of 0 before they start the investigation. When a researcher comes to an intersection with a beautiful statue, his/her sanity is increased by 1. When a researcher comes to an intersection with a horrible statue, his/her sanity is decreased by 1. Since the horrible statues are extraordinarily horrible, if the sanity of a researcher becomes negative, he/she cannot move anymore because of the fear, and will never return from the village. Since the beautiful statues are extraordinarily beautiful, if the sanity of a researcher is positive on going out of the village, the euphoria prevents him/her from leaving the village. The rest of the researchers, those who keep non-negative sanity and have the sanity of 0 on exiting the village, will return. The investigation is over, regrettably with many missing persons. Now you have the list of returned researchers. Your task is to decide whether there exist any arrangements of beautiful and horrible statues not conflicting with the list, and, if any, show one of such arrangements. Input The input consists of multiple datasets, each in the following format. n m A B n and m are the numbers of west-east and north-south streets, respectively. n and m are both positive even numbers at most 100. A and B are strings representing whether each researcher has returned from the village. A has the length n and B has the length m . Each string consists only of 0 s and 1 s. The i -th character of A represents whether the i -th of the first n researchers has returned from the village; 1 means the researcher has returned, and 0 means he/she has not. The j -th character of B represents whether the j -th of the remaining m researchers has returned from the village in the same manner. The numbe ...(truncated)
[ { "submission_id": "aoj_1660_10676160", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n/*\n#include <atcoder/all>\nusing namespace atcoder;\nusing mint = modint998244353;\n// using mint = modint1000000007;\n//*/\n\nusing ll = long long;\nusing i128 = __int128_t;\nusing pll = pair<ll, ll>;\nusing tlll = tuple<ll, ll, ll>;\nusing ld = long double;\nconst int INF = 1000100100;\nconst ll INFF = 1000100100100100100LL;\nconst int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};\n#define overload4(_1, _2, _3, _4, name, ...) name\n#define rep1(i, n) for (ll i = 0; i < ll(n); i++)\n#define rep2(i, l, r) for (ll i = ll(l); i < ll(r); i++)\n#define rep3(i, l, r, d) for (ll i = ll(l); (d) > 0 ? i < ll(r) : i > ll(r); i += d)\n#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)\n#define per(i, n) for (int i = (n) - 1; i >= 0; --i)\n#define yesno(f) cout << (f ? \"Yes\" : \"No\") << endl;\n#define YESNO(f) cout << (f ? \"YES\" : \"NO\") << endl;\n#define all(a) (a).begin(), (a).end()\n#define popc(x) __builtin_popcountll(ll(x))\n\ntemplate <typename S, typename T>\nostream &operator<<(ostream &os, const pair<S, T> &p) {\n return os << p.first << ' ' << p.second;\n}\n\ntemplate <typename T>\nvoid printvec(const vector<T> &v) {\n int n = v.size();\n rep(i, n) cout << v[i] << (i == n - 1 ? \"\" : \" \");\n cout << '\\n';\n}\n\ntemplate <typename T>\nvoid printvect(const vector<T> &v) {\n for (auto &vi : v) cout << vi << '\\n';\n}\n\ntemplate <typename T>\nvoid printvec2(const vector<vector<T>> &v) {\n for (auto &vi : v) printvec(vi);\n}\n\ntemplate <typename S, typename T>\nbool chmax(S &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename S, typename T>\nbool chmin(S &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\n#ifdef LOCAL // https://zenn.dev/sassan/articles/19db660e4da0a4\n#include \"cpp-dump-main/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\nCPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(val());\n#else\n#define dump(...)\n#endif\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\nbool check(string a, string b, vector<vector<char>> ans) {\n int n = ans.size(), m = ans[0].size();\n bool ok = true;\n rep(i, n) rep(j, m) {\n if (ans[i][j] == '?') return false;\n }\n rep(i, n) {\n int cnt = 0;\n bool flag = true;\n rep(j, m) {\n if (ans[i][j] == '+') cnt++;\n else cnt--;\n if (cnt < 0) flag = false;\n }\n if (cnt != 0) flag = false;\n if ((a[i] == '1') ^ flag) ok = false;\n }\n rep(j, m) {\n int cnt = 0;\n bool flag = true;\n rep(i, n) {\n if (ans[i][j] == '+') cnt++;\n else cnt--;\n if (cnt < 0) flag = false;\n }\n if (cnt != 0) flag = false;\n if ((b[j] == '1') ^ flag) ok = false;\n }\n return ok;\n}\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0) exit(0);\n string a, b;\n cin >> a >> b;\n vector<vector<char>> ans(n, vector<char>(m, '?'));\n rep(i, n) {\n if (a[i] == '1') {\n ans[i][0] = '+';\n ans[i][m - 1] = '-';\n }\n }\n rep(i, m) {\n if (b[i] == '1') {\n ans[0][i] = '+';\n ans[n - 1][i] = '-';\n }\n }\n // いけるか\n bool ok = true;\n if (a[0] == '1') {\n int cnt = 0;\n int cntp = 0;\n rep(i, m) {\n if (ans[0][i] == '+') cntp++;\n }\n rep(i, m) {\n if (ans[0][i] == '+') cnt++;\n else if (ans[0][i] == '-') cnt--;\n else {\n if (cntp < m / 2) {\n cntp++;\n cnt++;\n ans[0][i] = '+';\n } else {\n cnt--;\n ans[0][i] = '-';\n }\n }\n if (cnt < 0) ok = false;\n }\n if (cnt != 0) ok = false;\n }\n\n if (a[n - 1] == '1') {\n int cnt = 0;\n int cntp = 0;\n rep(i, m) {\n if (ans[n - 1][i] == '+') cntp++;\n }\n rep(i, m) {\n if (ans[n - 1][i] == '+') cnt++;\n else if (ans[n - 1][i] == '-') cnt--;\n else {\n if (cntp < m / 2) {\n cntp++;\n cnt++;\n ans[n - 1][i] = '+';\n } else {\n cnt--;\n ans[n - 1][i] = '-';\n }\n }\n if (cnt < 0) ok = false;\n }\n if (cnt != 0) ok = false;\n }\n\n if (b[0] == '1') {\n int cnt = 0;\n int cntp = 0;\n rep(i, n) {\n if (ans[i][0] == '+') cntp++;\n }\n rep(i, n) {\n if (ans[i][0] == '+') cnt++;\n else if (ans[i][0] == '-') cnt--;\n else {\n if (cntp < n / 2) {\n cntp++;\n cnt++;\n ans[i][0] = '+';\n } else {\n cnt--;\n ans[i][0] = '-';\n }\n }\n if (cnt < 0) ok = false;\n }\n if (cnt != 0) ok = false;\n }\n\n if (b[m - 1] == '1') {\n int cnt = 0;\n int cntp = 0;\n rep(i, n) {\n if (ans[i][m - 1] == '+') cntp++;\n }\n rep(i, n) {\n if (ans[i][m - 1] == '+') cnt++;\n else if (ans[i][m - 1] == '-') cnt--;\n else {\n if (cntp < n / 2) {\n cntp++;\n cnt++;\n ans[i][m - 1] = '+';\n } else {\n cnt--;\n ans[i][m - 1] = '-';\n }\n }\n if (cnt < 0) ok = false;\n }\n if (cnt != 0) ok = false;\n }\n\n rep(i, 1, n - 1) rep(j, 1, m - 1) {\n if (a[i] == '1' || b[j] == '1') {\n if ((i + j) % 2 == 0) ans[i][j] = '+';\n else ans[i][j] = '-';\n }\n }\n\n dump(ans);\n auto ran = [&]() -> char {\n if (rand() % 2 == 0) return '+';\n else return '-';\n };\n rep(_, 1000) {\n auto res = ans;\n rep(i, n) rep(j, m) {\n if (res[i][j] == '?') {\n if (i == 0 && j != m - 1) res[i][j] = '-';\n else if (i != n - 1 && j == 0) res[i][j] = '-';\n else if (i == n - 1 && j != 0) res[i][j] = '+';\n else if (i != 0 && j == m - 1) res[i][j] = '+';\n else res[i][j] = ran();\n }\n }\n if (check(a, b, res)) {\n ans = res;\n break;\n }\n }\n if (ok & check(a, b, ans)) {\n cout << \"Yes\" << endl;\n rep(i, n) {\n rep(j, m) cout << ans[i][j];\n cout << endl;\n }\n } else {\n cout << \"No\" << endl;\n }\n}\nint main() {\n int t;\n // cin >> t;\n t = 1;\n while (1) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 3460, "score_of_the_acc": -0.9537, "final_rank": 3 }, { "submission_id": "aoj_1660_10662215", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 lll;\nusing ull = unsigned long long;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\ntemplate<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using pqmax = priority_queue<T>;\nconst ll inf=LLONG_MAX/3;\nconst ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\nconst ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};\n#define mp make_pair\n#define pb push_back\n#define eb emplace_back\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define si(x) ll(x.size())\n#define rept(n) for(ll _ovo_=0;_ovo_<n;_ovo_++)\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define per(i,n) for(ll i=n-1;i>=0;i--)\n#define rng(i,l,r) for(ll i=l;i<r;i++)\n#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)\n#define fore(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\ntemplate<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define VLL(name,size) vector<ll>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define SUM(...) accumulate(all(__VA_ARGS__),0LL)\ntemplate<class T> auto min(const T& a){ return *min_element(all(a)); }\ntemplate<class T> auto max(const T& a){ return *max_element(all(a)); }\ntemplate<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }\ntemplate<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }\ntemplate<class T, class F = less<>> map<T,vector<ll> > ivm(vector<T>& a, F b = F{}){ map<T,vector<ll> > ret; rep(i,si(a))ret[a[i]].push_back(i); return ret;}\ntemplate<class T, class F = less<>> map<T,ll> ivc(vector<T>& a, F b = F{}){ map<T,ll> ret; rep(i,si(a))ret[a[i]]++; return ret;}\ntemplate<class T, class F = less<>> vector<T> ivp(vector<T> a){ vector<ll> ret(si(a)); rep(i,si(a))ret[a[i]] = i; return ret;}\ntemplate<class T, class F = less<>> vector<ll> rev(vector<T> a){ reverse(all(a)); return a;}\ntemplate<class T, class F = less<>> vector<ll> sortby(vector<T> a, F b = F{}){vector<ll> w = a; sor(w,b); vector<pll> v; rep(i,si(a))v.eb(a[i],i); sor(v); if(w[0] != v[0].first)reverse(all(v)); vector<ll> ret; rep(i,si(v))ret.pb(v[i].second); return ret;}\ntemplate<class T, class P> vector<T> filter(vector<T> a,P f){vector<T> ret;rep(i,si(a)){if(f(a[i]))ret.pb(a[i]);}return ret;}\ntemplate<class T, class P> vector<ll> filter_id(vector<T> a,P f){vector<ll> ret;rep(i,si(a)){if(f(a[i]))ret.pb(i);}return ret;}\nll monotone_left(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_left(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_right(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\nll monotone_right(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\ndouble monotone_double_left(double l,double r,function<bool(double)> f){assert(f(l) >= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return l;}\ndouble monotone_double_right(double l,double r,function<bool(double)> f){assert(f(l) <= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return r;}\ntemplate<class S> S unimodal_max(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) < f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmax(ret,f(k)); return ret;}\ntemplate<class S> S unimodal_min(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) > f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmin(ret,f(k)); return ret;}\nvector<pll> neighbor4(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,4){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\nvector<pll> neighbor8(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,8){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\n\nvoid outb(bool x){cout<<(x?\"Yes\":\"No\")<<\"\\n\";}\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))\nll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}\nvector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }\nvector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }\nvll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}\nll powll(lll x,ll y){lll res = 1; while(y){ if(y & 1)res = res * x; x = x * x; y >>= 1;} return res;}\nll powmod(lll x,ll y,lll mod){lll res=1; while(y){ if(y&1)res=res*x%mod; x=x*x%mod; y>>=1;} return res; }\nll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0)u+=m;return u;}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <class T> vector<T> &operator++(vector<T> &v) {\n\t\tfore(e, v) e++;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator++(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e++;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {\n\t\tfore(e, v) e--;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator--(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e--;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {\n\t\tfore(e, r) l.eb(e);\n\t\treturn l;\n}\n\ntemplate<class... Ts> void in(Ts&... t);\n[[maybe_unused]] void print(){}\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts);\ntemplate<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\\n'; }\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }\ntemplate<class T> void o(const T& t){ o(t, P<4>{}); }\ntemplate<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }\ntemplate<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }\ntemplate<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }\ntemplate<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }\ntemplate<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }\ntemplate<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }\n#undef VOID\n}\n#define unpack(a) (void)initializer_list<int>{(a, 0)...}\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }\n#undef unpack\nbool solve(){\n\tll n,m;\n\tcin>>n>>m;\n\tif(n + m == 0)return false;\n\tvll a(n);\n\tvll b(m);\n\t{\n\t\tstring s,t;\n\t\tcin>>s>>t;\n\t\trep(i,n)a[i] = s[i] - '0';\n\t\trep(j,m)b[j] = t[j] - '0';\n\t}\n\tif(a[0] && b[m - 1]){\n\t\tcout<<\"No\"<<endl;\n\t\treturn true;\n\t}\n\tif(a[n - 1] && b[0]){\n\t\tcout<<\"No\"<<endl;\n\t\treturn true;\n\t}\n\trep(r,16){\n\t\tvector<string> s(n);\n\t\trep(i,n){\n\t\t\ts[i] = \"\";\n\t\t\trep(j,m){\n\t\t\t\tchar c = '#';\n\t\t\t\tif(0 < i && i < n - 1 && 0 < j && j < m - 1){\n\t\t\t\t\tif((i + j) % 2 == 1)c = '+';\n\t\t\t\t\telse c = '-';\n\t\t\t\t}\n\t\t\t\ts[i].pb(c);\n\t\t\t}\n\t\t}\n\t\tfor(ll i = 1;i < n - 1;i++){\n\t\t\tif(a[i]){\n\t\t\t\ts[i][0] = '+';\n\t\t\t\ts[i][m - 1] = '-';\n\t\t\t}\n\t\t}\n\t\tfor(ll j = 1;j < m - 1;j++){\n\t\t\tif(b[j]){\n\t\t\t\ts[0][j] = '+';\n\t\t\t\ts[n - 1][j] = '-';\n\t\t\t}\n\t\t}\n\n\t\ts[0][0] = (r >> 0 & 1) ? '+' : '-';\n\t\ts[0][m - 1] = (r >> 1 & 1) ? '+' : '-';\n\t\ts[n - 1][0] = (r >> 2 & 1) ? '+' : '-';\n\t\ts[n - 1][m - 1] = (r >> 3 & 1) ? '+' : '-';\n\n\t\tfor(ll k = 0;k < n; k += n - 1){\n\t\t\tif(a[k]){\n\t\t\t\tll rem = m / 2;\n\t\t\t\trep(j,m)rem -= s[k][j] == '+';\n\t\t\t\trep(j,m)if(s[k][j] == '#'){\n\t\t\t\t\tif(rem > 0)s[k][j] = '+';\n\t\t\t\t\telse s[k][j] = '-';\n\t\t\t\t\trem--;\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\trep(j,m)if(s[k][j] == '#'){\n\t\t\t\t\tif(b[j] == 0){\n\t\t\t\t\t\ts[k][j] = k ? '+' : '-';\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(ll k = 0;k < m; k += m - 1){\n\t\t\tif(b[k]){\n\t\t\t\tll rem = n / 2;\n\t\t\t\trep(i,n)rem -= s[i][k] == '+';\n\t\t\t\trep(i,n)if(s[i][k] == '#'){\n\t\t\t\t\tif(rem > 0)s[i][k] = '+';\n\t\t\t\t\telse s[i][k] = '-';\n\t\t\t\t\trem--;\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\trep(i,n)if(s[i][k] == '#'){\n\t\t\t\t\tif(a[i] == 0){\n\t\t\t\t\t\ts[i][k] = k ? '+' : '-';\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// rep(i,n){\n\t\t// \tcout<<s[i]<<endl;\n\t\t// }\n\t\t// cout<<endl;\n\n\t\tbool judge = true;\n\t\t{\n\t\t\trep(i,n){\n\t\t\t\tll rui = 0;\n\t\t\t\tbool ok = true;\n\t\t\t\trep(j,m){\n\t\t\t\t\trui += s[i][j] == '+' ? +1 : -1;\n\t\t\t\t\tok &= rui >= 0;\n\t\t\t\t}\n\t\t\t\tok &= rui == 0;\n\t\t\t\tjudge &= a[i] == ok;\n\t\t\t}\n\t\t\trep(j,m){\n\t\t\t\tll rui = 0;\n\t\t\t\tbool ok = true;\n\t\t\t\trep(i,n){\n\t\t\t\t\trui += s[i][j] == '+' ? +1 : -1;\n\t\t\t\t\tok &= rui >= 0;\n\t\t\t\t}\n\t\t\t\tok &= rui == 0;\n\t\t\t\tjudge &= b[j] == ok;\n\t\t\t}\n\t\t}\n\t\tif(judge){\n\t\t\tcout<<\"Yes\"<<endl;\n\t\t\trep(i,n){\n\t\t\t\tcout<<s[i]<<endl;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t}\n\tcout<<\"No\"<<endl;\n\treturn true;\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3424, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1660_10052659", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nbool check(vvi &S, string &A, string &B){\n int n = len(S), m = len(S[0]);\n bool flag = true;\n for (auto i : {0, n - 1}){\n int cnt = 0;\n bool correct = true;\n rep(j, m){\n cnt += S[i][j];\n if (cnt < 0) correct = false;\n }\n if (cnt != 0) correct = false;\n if (correct != (A[i] - '0')) flag = false;\n }\n for (auto j : {0, m - 1}){\n int cnt = 0;\n bool correct = true;\n rep(i, n){\n cnt += S[i][j];\n if (cnt < 0) correct = false;\n }\n if (cnt != 0) correct = false;\n if (correct != (B[j] - '0')) flag = false;\n }\n srep(i, 1, n - 1) if ((S[i][0] == 1 && S[i][m - 1] == -1) != (A[i] - '0')) flag = false;\n srep(j, 1, m - 1) if ((S[0][j] == 1 && S[n - 1][j] == -1) != (B[j] - '0')) flag = false;\n return flag;\n}\n\nvoid solve(int n, int m){\n string A, B; cin >> A >> B;\n vvi ans(n, vi(m, 0));\n srep(i, 1, n - 1) srep(j, 1, m - 1) ans[i][j] = ((i + j) % 2 ? -1 : 1);\n rep(i, n) if (A[i] == '1'){\n ans[i][0] = 1;\n ans[i][m - 1] = -1;\n }\n rep(j, m) if (B[j] == '1'){\n ans[0][j] = 1;\n ans[n - 1][j] = -1;\n }\n for (auto i : {0, n - 1}) if (A[i] == '1'){\n int one = m / 2;\n rep(j, m) if (ans[i][j] == 1) one--;\n rep(j, m) if (ans[i][j] == 0){\n if (one){\n ans[i][j] = 1;\n one--;\n }else ans[i][j] = -1;\n }\n }\n for (auto j : {0, m - 1}) if (B[j] == '1'){\n int one = n / 2;\n rep(i, n) if (ans[i][j] == 1) one--;\n rep(i, n) if (ans[i][j] == 0){\n if (one){\n ans[i][j] = 1;\n one--;\n }else ans[i][j] = -1;\n }\n }\n rep(_, 1000){\n vvi na = ans;\n rep(i, n){\n if (na[i][0] == 0) na[i][0] = (rand() % 2 ? 1 : -1);\n if (na[i][m - 1] == 0) na[i][m - 1] = (rand() % 2 ? 1 : -1);\n }\n rep(j, m){\n if (na[0][j] == 0) na[0][j] = (rand() % 2 ? 1 : -1);\n if (na[n - 1][j] == 0) na[n - 1][j] = (rand() % 2 ? 1 : -1);\n }\n if (check(na, A, B)){\n cout << \"Yes\\n\";\n rep(i, n){\n for (auto x : na[i]) cout << (x == 1 ? '+' : '-');\n cout << \"\\n\";\n }\n return;\n }\n }\n for (int k = -1; k < 2; k += 2){\n vvi na = ans;\n rep(i, n){\n if (na[i][0] == 0) na[i][0] = k;\n if (na[i][m - 1] == 0) na[i][m - 1] = k;\n }\n rep(j, m){\n if (na[0][j] == 0) na[0][j] = k;\n if (na[n - 1][j] == 0) na[n - 1][j] = k;\n }\n if (check(na, A, B)){\n cout << \"Yes\\n\";\n rep(i, n){\n for (auto x : na[i]) cout << (x == 1 ? '+' : '-');\n cout << \"\\n\";\n }\n return;\n }\n }\n cout << \"No\\n\";\n}\n\nint main(){\n while (true){\n int n, m; cin >> n >> m;\n if (n == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3516, "score_of_the_acc": -0.4912, "final_rank": 2 }, { "submission_id": "aoj_1660_9291083", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\n\npair<string,string> check(vvll &A){\n ll N=A.size();\n ll M=A[0].size();\n string S=\"\",T=\"\";\n rep(i,N){\n ll k=0;\n bool OK=1;\n rep(j,M){\n k+=A[i][j];\n if(k<0)OK=0;\n }\n if(k!=0)OK=0;\n S.push_back(OK?'1':'0');\n }\n rep(j,M){\n ll k=0;\n bool OK=1;\n rep(i,N){\n k+=A[i][j];\n if(k<0)OK=0;\n }\n if(k!=0)OK=0;\n T.push_back(OK?'1':'0');\n }\n return {S,T};\n} \n\n\nvoid solve(ll N,ll M) {\n string S,T;\n cin>>S>>T;\n vvll AN(N,vll(M,0));\n rep(i,N){\n if(S[i]=='1'){\n if(AN[i][0]==-1||AN[i][M-1]==1){\n cout<<\"No\"<<endl;\n return;\n }\n AN[i][0]=1;\n AN[i][M-1]=-1;\n }\n }\n rep(i,M){\n if(T[i]=='1'){\n if(AN[0][i]==-1||AN[N-1][i]==1){\n cout<<\"No\"<<endl;\n return;\n }\n AN[0][i]=1;\n AN[N-1][i]=-1;\n }\n }\n ll cnt=0;\n for(ll i=1;i<N-1;i++){\n if(S[i]=='1'){\n cnt++;\n ll dnt=0;\n for(ll j=1;j<M-1;j++){\n if(T[j]=='1'){\n dnt++;\n if((cnt+dnt)%2==0)AN[i][j]=1;\n else AN[i][j]=-1;\n }\n }\n }\n }\n\n rep(i,N){\n if(S[i]=='1'){\n ll p=0;\n rep(j,M)if(AN[i][j]==1)p++;\n rep(j,M){\n if(AN[i][j]==0){\n if(p<M/2)AN[i][j]=1;\n else AN[i][j]=-1;\n p++;\n }\n }\n }\n }\n rep(i,M){\n if(T[i]=='1'){\n ll p=0;\n rep(j,N)if(AN[j][i]==1)p++;\n rep(j,N){\n if(AN[j][i]==0){\n if(p<N/2)AN[j][i]=1;\n else AN[j][i]=-1;\n p++;\n }\n }\n }\n }\n rep(i,N){\n // if(AN[i][0]==0)AN[i][0]=-1;\n // if(AN[i][M-1]==0)AN[i][M-1]=1;\n }\n rep(i,M){\n // if(AN[0][i]==0)AN[0][i]=-1;\n // if(AN[N-1][i]==0)AN[N-1][i]=1;\n }\n auto BN=AN;\n srand(time(NULL));\n rep(k,100){\n rep(i,N)rep(j,M){\n \n if(BN[i][j]==0){\n if(k==0)AN[i][j]=1;\n else if(k==1)AN[i][j]=-1;\n else AN[i][j]=(rand()%2==1?1:-1);\n }\n }\n auto C=check(AN);\n if(C.first==S&&C.second==T){\n cout<<\"Yes\"<<endl;\n rep(i,N){\n rep(j,M)cout<<(AN[i][j]==1?\"+\":\"-\");\n cout<<endl;\n }\n return;\n }\n }cout<<\"No\"<<endl;\n \n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n ll N,M;\n while (cin >> N>>M) {\n if (N == 0)return 0;\n solve(N,M);\n }\n\n\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3676, "score_of_the_acc": -1.018, "final_rank": 4 }, { "submission_id": "aoj_1660_7952297", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = int(s); i < int(n); i++)\n#define rrep(i, s, n) for (int i = int(n) - 1; i >= int(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\n\ntemplate <class T> bool chmin(T &a, T b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T> bool chmax(T &a, T b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nusing namespace std;\n\ntemplate <ll m> struct modint {\n using mint = modint;\n ll a;\n modint(ll x = 0) : a((x%m + m)%m){}\n static ll mod() {\n return m;\n }\n ll& val(){\n return a;\n }\n mint pow(ll n){\n mint res = 1;\n mint x = a;\n while(n){\n if (n&1) res*= x;\n x *= x;\n n >>= 1;\n }\n return res;\n }\n mint inv(){\n return pow(m-2);\n }\n mint& operator+=(const mint rhs){\n a += rhs.a;\n if (a >= m) a-=m;\n return *this;\n }\n mint& operator-=(const mint rhs){\n if (a < rhs.a) a+=m;\n a -= rhs.a;\n return *this;\n }\n mint& operator*=(const mint rhs){\n a = a * rhs.a %m;\n return *this;\n }\n mint& operator/=(mint rhs){\n *this *= rhs.inv();\n return *this;\n }\n friend mint operator+(const mint& lhs, const mint& rhs){\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs){\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs){\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs){\n return mint(lhs) /= rhs;\n }\n mint operator+() const{\n return *this;\n }\n mint operator-() const {\n return mint() - *this;\n } \n};\n\nusing modint998244353 = modint<998244353>;\nusing mint = modint998244353;\n\ntemplate<typename T>\nstruct Binom{\n Binom(int lim = 300000){\n if (kaijo.empty()){\n kaijo = {1, 1};\n kainv = {1, 1};\n }\n kaijo.resize(lim+1), kainv.resize(lim+1);\n for (int i=2; i<=lim; i++){\n kaijo[i] = kaijo[i-1] * T(i);\n }\n kainv[lim] = kaijo[lim].inv();\n for (int i=lim-1; i>=2; i--){\n kainv[i] = kainv[i+1] * T(i+1);\n }\n }\n static T fact(int x){\n if (x < 0){\n return T(0);\n }\n return kaijo[x];\n }\n static T ifact(int x){\n if (x < 0){\n return T(0);\n }\n return kainv[x];\n }\n static T C(int n, int r){\n if (n < 0 || n < r || r < 0) return T(0);\n return kaijo[n] * kainv[r] * kainv[n-r];\n }\n static T inv(int n){\n return ifact(n) * fact(n-1);\n }\n T operator()(int n, int r){ return C(n, r);}\n private:\n static vector<T> kaijo, kainv;\n};\n\ntemplate<typename T>\nvector<T>Binom<T>::kaijo = vector<T>(2, T(1));\n\ntemplate<typename T>\nvector<T>Binom<T>::kainv = vector<T>(2, T(1));\n\n\nbool checkAD(int n, int m, int i, vector<vector<char>> &r){\n int hp = 0;\n rep(j,0,m){\n if (r[i][j] == '+') hp++;\n if (r[i][j] == '-') hp--;\n if (hp < 0) return false;\n }\n if (hp == 0) return true;\n return false;\n}\n\nbool checkWS(int n, int m, int i, vector<vector<char>> &r){\n int hp = 0;\n rep(j,0,n){\n if (r[j][i] == '+') hp++;\n if (r[j][i] == '-') hp--;\n if (hp < 0) return false;\n }\n if (hp == 0) return true;\n return false;\n}\n\nbool checkALL(int n, int m, vector<vector<char>> &r, vector<int> &a, vector<int> &b){\n rep(i,0,n){\n if (a[i] != checkAD(n, m, i, r)){\n return false;\n }\n }\n rep(j,0,m){\n if (b[j] != checkWS(n, m, j, r)){\n return false;\n }\n }\n return true;\n}\n\nvoid solve(int n, int m, uniform_int_distribution<int> &dist, mt19937 &engine){\n vector<int> a(n), b(m);\n rep(i,0,n){\n char c; cin >> c;\n a[i] = c - '0';\n }\n rep(i,0,m){\n char c; cin >> c;\n b[i] = c - '0';\n }\n\n // make O the head and tail\n vector<vector<char>> r(n, vector<char>(m, '?'));\n \n rep(i,0,n){\n if (a[i] == 1){\n r[i][0] = '+';\n r[i][m-1] = '-';\n }\n }\n\n rep(i,0,m){\n if (b[i] == 1){\n r[0][i] = '+';\n r[n-1][i] = '-';\n }\n }\n\n // ICHIMATSU MOYOU\n rep(i,1,n-1){\n rep(j,1,m-1){\n if ((i+j)%2 == 0){\n r[i][j] = '+';\n }else{\n r[i][j] = '-';\n }\n }\n }\n\n\n // make O great\n for (int i: {0, n-1}){\n if (a[i] == 1){\n int pos = 0, neg = 0;\n rep(j,0,m){\n if (r[i][j] == '+') pos++;\n if (r[i][j] == '-') neg++;\n }\n rep(j,0,m){\n if (r[i][j] == '?'){\n if (pos < m/2){\n r[i][j] = '+';\n pos++;\n }else{\n r[i][j] = '-';\n neg++;\n }\n }\n }\n }\n }\n\n \n for (int j: {0, m-1}){\n if (b[j] == 1){\n int pos = 0, neg = 0;\n rep(i,0,n){\n if (r[i][j] == '+') pos++;\n if (r[i][j] == '-') neg++;\n }\n rep(i,0,n){\n if (r[i][j] == '?'){\n if (pos < n/2){\n r[i][j] = '+';\n pos++;\n }else{\n r[i][j] = '-';\n neg++;\n }\n }\n }\n }\n }\n\n rep(i,0,n){\n rep(j,0,m){\n if (a[i] == 0 && b[j] == 0){\n r[i][j] = '?';\n }\n }\n }\n\n/*\n rep(i,0,n){\n rep(j,0,m){\n cout << r[i][j];\n }\n cout << endl;\n }\n*/\n\n // RANTAKU\n vector<int> x,y;\n\n rep(i,0,n){\n rep(j,0,m){\n if (r[i][j] == '?'){\n x.push_back(i);\n y.push_back(j);\n }\n }\n }\n\n int k = x.size();\n\n rep(NUMBER, 0, 1000){\n vector<vector<char>> nr = r;\n rep(i,0,k){\n if (dist(engine)) nr[x[i]][y[i]] = '+';\n else nr[x[i]][y[i]] = '-';\n }\n if(checkALL(n,m,nr,a,b)){\n cout << \"Yes\" << endl;\n rep(i,0,n){\n rep(j,0,m){\n cout << nr[i][j];\n }\n cout << endl;\n }\n return;\n };\n }\n\n {\n vector<vector<char>> nr = r;\n rep(i,0,k){\n nr[x[i]][y[i]] = '+';\n }\n if(checkALL(n,m,nr,a,b)){\n cout << \"Yes\" << endl;\n rep(i,0,n){\n rep(j,0,m){\n cout << nr[i][j];\n }\n cout << endl;\n }\n return;\n };\n }\n\n {\n vector<vector<char>> nr = r;\n rep(i,0,k){\n nr[x[i]][y[i]] = '-';\n }\n if(checkALL(n,m,nr,a,b)){\n cout << \"Yes\" << endl;\n rep(i,0,n){\n rep(j,0,m){\n cout << nr[i][j];\n }\n cout << endl;\n }\n return;\n };\n }\n\n cout << \"No\" << endl;\n \n return;\n}\n\nint main() {\n random_device seed_gen;\n mt19937 engine(seed_gen());\n uniform_int_distribution<int> dist(0, 1);\n\n while(true){\n int n, m; cin >> n >> m;\n if (n == 0 && m == 0) break;\n solve(n, m, dist, engine);\n }\n}", "accuracy": 1, "time_ms": 1120, "memory_kb": 3556, "score_of_the_acc": -1.5238, "final_rank": 5 } ]
aoj_1653_cpp
Princess' Perfectionism The princess has n spies, and she is planning to assign exactly n tasks to them. Skills of the spies constrain the eligibilities for the tasks. The princess is a perfectionist, and she is not satisfied unless each spy is assigned exactly one task, and each task is assigned to exactly one spy. Fortunately, this turned out to be feasible; there may be, however, selfish spies who cling to specific tasks among those eligible for themselves. She plans to train some of the spies so that a satisfiable task assignment is possible even when any single spy is selfish. One session trains a spy so that he/she becomes eligible for a new task. She wants to minimize the number of training sessions. Your mission is to suggest an optimal training plan to the princess. You are given all the eligible pairs of a spy and a task. If no spy is selfish, the tasks can be assigned to the spies so that the princess is satisfied. The eligible pairs should be augmented through training so that an assignment that satisfies the princess always exists even if any single spy clings to a task that is eligible for him/her. Spies may cling to a task newly made eligible through training. Note that no training at all might be required for the purpose. Input The input consists of multiple datasets, each in the following format. n m s 1 t 1 ... s m t m n is the number of spies as well as the number of tasks. n is a positive integer not exceeding 2000. m is the number of eligible pairs of a spy and a task. m is a positive integer not exceeding 10 5 . The spies and tasks are respectively represented by integers between 1 and n . For i = 1, 2, ..., m, ( s i , t i ) is an eligible pair, which represents that the spy s i is eligible for the task t i . In addition, if i ≠ j, then s i ≠ s j or t i ≠ t j . It is guaranteed that there exists a subset of eligible pairs in which all spies and tasks appear exactly once, that is, the princess can be satisfied by some assignment if no spy is selfish. The end of the input is indicated by a line containing two zeros with a space between them. The number of datasets in the input is at most 25. Output For each dataset, output a nonnegative integer k in the first line, and in each of the next k lines, output two integers x i and y i ( i = 1, 2, ..., k ) with a space between them. k is the minimum number of pairs of a spy and a task to be trained, and {( x 1 , y 1 ), ( x 2 , y 2 ), ..., ( x k , y k )} is a set of pairs that attains the minimum, where x i and y i represent a spy and a task, respectively. There may be two or more sets of pairs to be trained with the least number of elements; any of them shall be deemed correct. Sample Input 2 3 1 1 1 2 2 2 2 2 1 1 2 2 4 7 1 1 1 2 2 2 3 2 3 3 3 4 4 4 5 10 1 1 1 2 1 3 2 1 2 2 2 4 3 3 4 4 4 5 5 5 0 0 Output for the Sample Input 1 2 1 0 2 2 3 4 1 2 3 2 5 3
[ { "submission_id": "aoj_1653_10625084", "code_snippet": "#line 2 \"/home/nixos/workspace/CompPro-Make/library/KowerKoint/stl-expansion.hpp\"\n#include <bits/stdc++.h>\n\ntemplate <typename T1, typename T2>\nstd::istream& operator>>(std::istream& is, std::pair<T1, T2>& p) {\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& is, std::array<T, N>& a) {\n for (size_t i = 0; i < N; ++i) {\n is >> a[i];\n }\n return is;\n}\ntemplate <typename T>\nstd::istream& operator>>(std::istream& is, std::vector<T>& v) {\n for (auto& e : v) is >> e;\n return is;\n}\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& os, const std::array<T, N>& a) {\n for (size_t i = 0; i < N; ++i) {\n os << a[i] << (i + 1 == a.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\n#line 3 \"/home/nixos/workspace/CompPro-Make/library/KowerKoint/base.hpp\"\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define END(...) { print(__VA_ARGS__); return; }\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing ull = unsigned long long;\nusing VUL = vector<ull>;\nusing VVUL = vector<VUL>;\nusing VVVUL = vector<VVUL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef DEBUG\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#else\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#endif\n\ntemplate<typename T>\nvector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {\n vector<vector<T>> res;\n vector<T> cur;\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(*it);\n }\n res.push_back(cur);\n return res;\n}\n\nvector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {\n vector<string> res;\n string cur = \"\";\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(*it);\n }\n res.push_back(cur);\n return res;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename It>\nauto rle(It begin, It end) {\n vector<pair<typename It::value_type, int>> res;\n if(begin == end) return res;\n auto pre = *begin;\n int num = 1;\n for(auto it = begin + 1; it != end; it++) {\n if(pre != *it) {\n res.emplace_back(pre, num);\n pre = *it;\n num = 1;\n } else num++;\n }\n res.emplace_back(pre, num);\n return res;\n}\n\ntemplate <typename It>\nvector<pair<typename It::value_type, int>> rle_sort(It begin, It end) {\n vector<typename It::value_type> cloned(begin, end);\n sort(ALL(cloned));\n auto e = rle(ALL(cloned));\n sort(ALL(e), [](const auto& l, const auto& r) { return l.second < r.second; });\n return e;\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n#line 2 \"main.cpp\"\n\ntemplate <typename T>\nusing V = vector<T>;\ntemplate <typename T>\nusing VV = vector<V<T>>;\n\n/*\nstruct E {\n int to, rev, cap;\n};\nVV<E> g;\nauto add_edge = [&](int from, int to, int cap) {\n g[from].push_back(E{to, int(g[to].size()), cap});\n g[to].push_back(E{from, int(g[from].size())-1, 0});\n};\n*/\ntemplate <class C>\nstruct MaxFlow {\n C flow;\n V<char> dual; // false: S-side true: T-side\n};\ntemplate <class C, class E>\nstruct MFExec {\n static constexpr C INF = numeric_limits<C>::max();\n C eps;\n VV<E>& g;\n int s, t;\n V<int> level, iter;\n C dfs(int v, C f) {\n if (v == t) return f;\n C res = 0;\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n E& e = g[v][i];\n if (e.cap <= eps || level[v] >= level[e.to]) continue;\n C d = dfs(e.to, min(f, e.cap));\n e.cap -= d;\n g[e.to][e.rev].cap += d;\n res += d;\n f -= d;\n if (f == 0) break;\n }\n return res;\n }\n MaxFlow<C> info;\n MFExec(VV<E>& _g, int _s, int _t, C _eps)\n : eps(_eps), g(_g), s(_s), t(_t) {\n int N = int(g.size());\n C& flow = (info.flow = 0);\n while (true) {\n queue<int> que;\n level = V<int>(N, -1);\n level[s] = 0;\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (E e : g[v]) {\n if (e.cap <= eps || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n que.push(e.to);\n }\n }\n if (level[t] == -1) break;\n while (true) {\n iter = V<int>(N, 0);\n C f = dfs(s, INF);\n if (!f) break;\n flow += f;\n }\n }\n for (int i = 0; i < N; i++)\n info.dual.push_back(level[i] == -1);\n }\n};\ntemplate <class C, class E>\nMaxFlow<C> get_mf(VV<E>& g, int s, int t, C eps) {\n return MFExec<C, E>(g, s, t, eps).info;\n}\n\nstruct SCC {\n V<int> id;\n VV<int> groups;\n};\ntemplate <class E>\nstruct SCCExec : SCC {\n int n;\n const VV<E>& g;\n int tm = 0;\n V<bool> flag;\n V<int> low, ord, st;\n void dfs(int v) {\n low[v] = ord[v] = tm++;\n st.push_back(v);\n flag[v] = true;\n for (auto e : g[v]) {\n if (ord[e.to] == -1) {\n dfs(e.to);\n low[v] = min(low[v], low[e.to]);\n } else if (flag[e.to]) {\n low[v] = min(low[v], ord[e.to]);\n }\n }\n if (low[v] == ord[v]) {\n V<int> gr;\n while (true) {\n int u = st.back();\n st.pop_back();\n gr.push_back(u);\n if (u == v) break;\n }\n for (int x : gr) flag[x] = false;\n groups.push_back(gr);\n }\n }\n SCCExec(const VV<E>& _g)\n : n(int(_g.size())), g(_g), flag(n), low(n), ord(n, -1) {\n id = V<int>(n);\n for (int i = 0; i < n; i++) {\n if (ord[i] == -1) dfs(i);\n }\n reverse(groups.begin(), groups.end());\n for (int i = 0; i < int(groups.size()); i++) {\n for (int x : groups[i]) {\n id[x] = i;\n }\n }\n }\n};\ntemplate <class E>\nSCC get_scc(const VV<E>& g) {\n return SCCExec<E>(g);\n}\n\nbool solve(){\n struct E {\n int to, rev, cap;\n };\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) return false;\n int src = n*2, sink = n*2+1;\n VV<E> g(n*2+2);\n auto add_edge = [&](int from, int to, int cap) {\n g[from].push_back(E{to, int(g[to].size()), cap});\n g[to].push_back(E{from, int(g[from].size())-1, 0});\n };\n REP(i, m) {\n int s, t; cin >> s >> t; s--; t--;\n add_edge(s, n+t, 1);\n }\n // tajuhen\n REP(i, n) {\n if(ssize(g[i]) == 1 && ssize(g[g[i][0].to]) == 1) {\n // cerr << \"taju: \" << i << ' ' << g[i][0].to << endl;\n add_edge(i, g[i][0].to, 1);\n }\n }\n REP(i, n) add_edge(src, i, 1);\n REP(i, n) add_edge(n+i, sink, 1);\n auto mf = get_mf(g, src, sink, 0);\n assert(mf.flow == n);\n struct ES { int to; };\n VV<ES> rg(n*2);\n for(int i = 0; i < n*2; i++) {\n for(const auto& e : g[i]) {\n // cerr << \"rg:\" << i << ' ' << e.to << ' ' << e.cap << endl;\n if(e.cap == 0) continue;\n if(e.to >= n*2) continue;\n rg[i].push_back({e.to});\n }\n }\n auto scc = get_scc(rg);\n for(auto& group : scc.groups) {\n ranges::sort(group);\n }\n int ng = ssize(scc.groups);\n vector<vector<int>> dag(ng);\n vector<int> in(ng), out(ng);\n for(int i = 0; i < n*2; i++) {\n for(const auto& e : rg[i]) {\n int u = scc.id[i], v = scc.id[e.to];\n if(u == v) continue;\n dag[u].push_back(v);\n out[u]++;\n in[v]++;\n }\n }\n vector<int> seen(ng);\n for(int i = 0; i < ng; i++) {\n if(in[i] == 0 && out[i] == 0) seen[i] = 1;\n }\n vector<int> enter, exit;\n for(int i = 0; i < ng; i++) {\n if(in[i]==0) enter.push_back(i);\n if(out[i] == 0) exit.push_back(i);\n }\n if(ssize(enter) == 0 && ssize(exit) == 0) {\n cout << \"0\\n\";\n return true;\n }\n assert(ssize(enter) > 0 && ssize(exit) > 0);\n vector<int> st, ed;\n for(int i = 0; i < ssize(enter); i++) {\n if(seen[enter[i]]) continue;\n vector<int> path = {enter[i]};\n while(1) {\n bool proceed = false;\n int u = path.back();\n for(int v : dag[u]) {\n if(seen[v]) continue;\n path.push_back(v);\n proceed = true;\n break;\n }\n if(!proceed) break;\n }\n if(out[path.back()] == 0) {\n st.push_back(enter[i]);\n ed.push_back(path.back());\n for(int u : path) seen[u] = true;\n }\n }\n auto get_spy = [&](int u) {\n assert(scc.groups[u][0] < n);\n return scc.groups[u][0];\n };\n auto get_task = [&](int u) {\n assert(scc.groups[u].back() >= n);\n return scc.groups[u].back() - n;\n };\n vector<pair<int, int>> ans;\n for(int i = 0; i < ssize(st); i++) {\n int ni = i+1;\n if(ni == ssize(st)) ni = 0;\n ans.emplace_back(get_spy(ed[i]), get_task(st[ni]));\n }\n vector<int> enter_req, exit_req;\n for(int u : enter) {\n if(!seen[u]) {\n enter_req.push_back(u);\n }\n }\n for(int u : exit) {\n if(!seen[u]) {\n exit_req.push_back(u);\n }\n }\n int see = min(ssize(enter_req), ssize(exit_req));\n REP(i, see) {\n ans.emplace_back(get_spy(exit_req[i]), get_task(enter_req[i]));\n }\n for(int i = see; i < ssize(enter_req); i++) {\n ans.emplace_back(get_spy(ed[0]), get_task(enter_req[i]));\n }\n for(int i = see; i < ssize(exit_req); i++) {\n ans.emplace_back(get_spy(exit_req[i]), get_task(st[0]));\n }\n cout << ssize(ans) << '\\n';\n for(auto [u, v] : ans) {\n cout << u+1 << ' ' << v+1 << '\\n';\n }\n // for(const auto& group : scc.groups) {\n // for(int i : group) {\n // cerr << ' ' << i;\n // }\n // cerr << endl;\n // }\n // cerr << \"st: \";\n // for(int u : st) {\n // cerr << ' ' << u;\n // }\n // cerr << endl;\n // cerr << \"ed: \";\n // for(int u : ed) {\n // cerr << ' ' << u;\n // }\n // cerr << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n while(solve());\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 9472, "score_of_the_acc": -0.8679, "final_rank": 6 }, { "submission_id": "aoj_1653_10567711", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T>& a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end());\n return result;\n }\n\n private:\n int _n;\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E>\nstruct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n\n int num_vertices() { return _n; }\n\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct scc_graph {\n public:\n scc_graph() : internal(0) {}\n scc_graph(int n) : internal(n) {}\n\n void add_edge(int from, int to) {\n int n = internal.num_vertices();\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n internal.add_edge(from, to);\n }\n\n std::vector<std::vector<int>> scc() { return internal.scc(); }\n\n private:\n internal::scc_graph internal;\n};\n\n} // namespace atcoder\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) exit(0);\n atcoder::mf_graph<int> g(2 * n + 2);\n vector<int> s(m), t(m);\n rep(i, m) {\n cin >> s[i] >> t[i];\n s[i]--, t[i]--;\n g.add_edge(s[i], t[i] + n, 1);\n }\n rep(i, n) {\n g.add_edge(2 * n, i, 1);\n g.add_edge(i + n, 2 * n + 1, 1);\n }\n assert(g.flow(2 * n, 2 * n + 1) == n);\n vector<int> p(n), ip(n);\n vector<pair<int, int>> rem;\n for (auto [from, to, cap, flow] : g.edges()) {\n if (from == 2 * n || to == 2 * n + 1) continue;\n to -= n;\n if (flow == 1) {\n p[from] = to;\n ip[to] = from;\n } else {\n rem.push_back({from, to});\n }\n }\n\n // 完全マッチングを0->0,1->1,...と置きなおす\n // 他の辺をu->vと書き直す\n // x->yが固定されたとき、y->xのパスがあればいい\n // 強連結であればよい\n atcoder::scc_graph sg(n);\n for (auto& [x, y] : rem) {\n // x->ip[y]\n y = ip[y];\n sg.add_edge(x, y);\n }\n auto scc = sg.scc();\n int k = scc.size();\n vector<int> group(n);\n rep(i, k) for (int x : scc[i]) group[x] = i;\n vector<int> in(k), out(k);\n atcoder::dsu uf(k);\n vector<vector<int>> dag(k);\n for (auto [x, y] : rem) {\n if (group[x] == group[y]) continue;\n dag[group[x]].push_back(group[y]);\n out[group[x]]++, in[group[y]]++;\n uf.merge(group[x], group[y]);\n }\n vector<vector<int>> src(k), snk(k);\n rep(i, k) {\n if (in[i] == 0 && out[i] == 0) continue;\n if (out[i] == 0) snk[uf.leader(i)].push_back(i);\n if (in[i] == 0) src[uf.leader(i)].push_back(i);\n }\n\n vector<pair<int, int>> ans;\n vector<bool> vis(k);\n vector<int> rA, rB;\n int S = -1, T = -1;\n vector<int> loopA, loopB;\n rep(i, k) {\n if (uf.leader(i) == i && uf.size(i) > 1) {\n auto A = src[i], B = snk[i];\n assert(!A.empty());\n S = A[0], T = B[0];\n function<bool(int)> dfs = [&](int x) {\n vis[x] = true;\n if (dag[x].empty()) {\n loopB.push_back(x);\n return true;\n }\n for (int y : dag[x]) {\n if (vis[y]) continue;\n if (dfs(y)) return true;\n }\n return false;\n };\n vector<bool> usedA(A.size()), usedB(B.size());\n rep(p, A.size()) {\n if (dfs(A[p])) {\n loopA.push_back(A[p]);\n usedA[p] = true;\n }\n }\n rep(p, B.size()) {\n if (vis[B[p]]) usedB[p] = true;\n }\n rep(p, A.size()) {\n if (!usedA[p]) rA.push_back(A[p]);\n }\n rep(p, B.size()) {\n if (!usedB[p]) rB.push_back(B[p]);\n }\n }\n }\n\n if (loopA.size()) {\n int l = loopA.size();\n rep(j, l) ans.push_back({loopB[j], loopA[(j + 1) % l]});\n }\n if (rA.size() || rB.size()) {\n if (rA.empty()) rA.push_back(S);\n if (rB.empty()) rB.push_back(T);\n while (rA.size() < rB.size()) rA.push_back(rA.back());\n while (rB.size() < rA.size()) rB.push_back(rB.back());\n rep(j, rA.size()) { ans.push_back({rB[j], rA[j]}); }\n }\n cout << ans.size() << endl;\n for (auto [x, y] : ans) {\n cout << scc[x][0] + 1 << \" \" << p[scc[y][0]] + 1 << endl;\n }\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 13532, "score_of_the_acc": -0.9944, "final_rank": 7 }, { "submission_id": "aoj_1653_10567703", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T>& a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end());\n return result;\n }\n\n private:\n int _n;\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E>\nstruct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n\n int num_vertices() { return _n; }\n\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct scc_graph {\n public:\n scc_graph() : internal(0) {}\n scc_graph(int n) : internal(n) {}\n\n void add_edge(int from, int to) {\n int n = internal.num_vertices();\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n internal.add_edge(from, to);\n }\n\n std::vector<std::vector<int>> scc() { return internal.scc(); }\n\n private:\n internal::scc_graph internal;\n};\n\n} // namespace atcoder\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) exit(0);\n atcoder::mf_graph<int> g(2 * n + 2);\n vector<int> s(m), t(m);\n rep(i, m) {\n cin >> s[i] >> t[i];\n s[i]--, t[i]--;\n g.add_edge(s[i], t[i] + n, 1);\n }\n rep(i, n) {\n g.add_edge(2 * n, i, 1);\n g.add_edge(i + n, 2 * n + 1, 1);\n }\n assert(g.flow(2 * n, 2 * n + 1) == n);\n vector<int> p(n), ip(n);\n vector<pair<int, int>> rem;\n for (auto [from, to, cap, flow] : g.edges()) {\n if (from == 2 * n || to == 2 * n + 1) continue;\n to -= n;\n if (flow == 1) {\n p[from] = to;\n ip[to] = from;\n } else {\n rem.push_back({from, to});\n }\n }\n\n // 完全マッチングを0->0,1->1,...と置きなおす\n // 他の辺をu->vと書き直す\n // x->yが固定されたとき、y->xのパスがあればいい\n // 強連結であればよい\n atcoder::scc_graph sg(n);\n for (auto& [x, y] : rem) {\n // x->ip[y]\n y = ip[y];\n sg.add_edge(x, y);\n }\n auto scc = sg.scc();\n int k = scc.size();\n vector<int> group(n);\n rep(i, k) for (int x : scc[i]) group[x] = i;\n vector<int> in(k), out(k);\n atcoder::dsu uf(k);\n vector<vector<int>> dag(k);\n for (auto [x, y] : rem) {\n if (group[x] == group[y]) continue;\n dag[group[x]].push_back(group[y]);\n out[group[x]]++, in[group[y]]++;\n uf.merge(group[x], group[y]);\n }\n vector<vector<int>> src(k), snk(k);\n rep(i, k) {\n if (in[i] == 0 && out[i] == 0) continue;\n if (out[i] == 0) snk[uf.leader(i)].push_back(i);\n if (in[i] == 0) src[uf.leader(i)].push_back(i);\n }\n\n vector<pair<int, int>> ans;\n vector<bool> vis(k);\n vector<int> rA, rB;\n int S = -1, T = -1;\n rep(i, k) {\n if (uf.leader(i) == i && uf.size(i) > 1) {\n auto A = src[i], B = snk[i];\n assert(!A.empty());\n S = A[0], T = B[0];\n vector<int> loopA, loopB;\n function<bool(int)> dfs = [&](int x) {\n vis[x] = true;\n if (dag[x].empty()) {\n loopB.push_back(x);\n return true;\n }\n for (int y : dag[x]) {\n if (vis[y]) continue;\n if (dfs(y)) return true;\n }\n return false;\n };\n vector<bool> usedA(A.size()), usedB(B.size());\n rep(p, A.size()) {\n if (dfs(A[p])) {\n loopA.push_back(A[p]);\n usedA[p] = true;\n }\n }\n rep(p, B.size()) {\n if (vis[B[p]]) usedB[p] = true;\n }\n if (loopA.size()) {\n int l = loopA.size();\n rep(j, l) ans.push_back({loopB[j], loopA[(j + 1) % l]});\n }\n rep(p, A.size()) {\n if (!usedA[p]) rA.push_back(A[p]);\n }\n rep(p, B.size()) {\n if (!usedB[p]) rB.push_back(B[p]);\n }\n }\n }\n\n if (rA.size() || rB.size()) {\n if (rA.empty()) rA.push_back(S);\n if (rB.empty()) rB.push_back(T);\n while (rA.size() < rB.size()) rA.push_back(rA.back());\n while (rB.size() < rA.size()) rB.push_back(rB.back());\n rep(j, rA.size()) { ans.push_back({rB[j], rA[j]}); }\n }\n cout << ans.size() << endl;\n for (auto [x, y] : ans) {\n cout << scc[x][0] + 1 << \" \" << p[scc[y][0]] + 1 << endl;\n }\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 0.5, "time_ms": 170, "memory_kb": 13324, "score_of_the_acc": -1.0126, "final_rank": 13 }, { "submission_id": "aoj_1653_10567672", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T>& a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end());\n return result;\n }\n\n private:\n int _n;\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E>\nstruct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n\n int num_vertices() { return _n; }\n\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct scc_graph {\n public:\n scc_graph() : internal(0) {}\n scc_graph(int n) : internal(n) {}\n\n void add_edge(int from, int to) {\n int n = internal.num_vertices();\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n internal.add_edge(from, to);\n }\n\n std::vector<std::vector<int>> scc() { return internal.scc(); }\n\n private:\n internal::scc_graph internal;\n};\n\n} // namespace atcoder\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) exit(0);\n atcoder::mf_graph<int> g(2 * n + 2);\n vector<int> s(m), t(m);\n rep(i, m) {\n cin >> s[i] >> t[i];\n s[i]--, t[i]--;\n g.add_edge(s[i], t[i] + n, 1);\n }\n rep(i, n) {\n g.add_edge(2 * n, i, 1);\n g.add_edge(i + n, 2 * n + 1, 1);\n }\n assert(g.flow(2 * n, 2 * n + 1) == n);\n vector<int> p(n), ip(n);\n vector<pair<int, int>> rem;\n for (auto [from, to, cap, flow] : g.edges()) {\n if (from == 2 * n || to == 2 * n + 1) continue;\n to -= n;\n if (flow == 1) {\n p[from] = to;\n ip[to] = from;\n } else {\n rem.push_back({from, to});\n }\n }\n\n // 完全マッチングを0->0,1->1,...と置きなおす\n // 他の辺をu->vと書き直す\n // x->yが固定されたとき、y->xのパスがあればいい\n // 強連結であればよい\n atcoder::scc_graph sg(n);\n for (auto& [x, y] : rem) {\n // x->ip[y]\n y = ip[y];\n sg.add_edge(x, y);\n }\n auto scc = sg.scc();\n int k = scc.size();\n vector<int> group(n);\n rep(i, k) for (int x : scc[i]) group[x] = i;\n vector<int> in(k), out(k);\n atcoder::dsu uf(k);\n vector<vector<int>> dag(k);\n for (auto [x, y] : rem) {\n if (group[x] == group[y]) continue;\n dag[group[x]].push_back(group[y]);\n out[group[x]]++, in[group[y]]++;\n uf.merge(group[x], group[y]);\n }\n vector<vector<int>> src(k), snk(k);\n rep(i, k) {\n if (in[i] == 0 && out[i] == 0) continue;\n if (out[i] == 0) snk[uf.leader(i)].push_back(i);\n if (in[i] == 0) src[uf.leader(i)].push_back(i);\n }\n\n vector<pair<int, int>> ans;\n vector<bool> vis(k);\n rep(i, k) {\n if (uf.leader(i) == i && uf.size(i) > 1) {\n auto A = src[i], B = snk[i];\n assert(!A.empty());\n vector<int> loopA, loopB;\n function<bool(int)> dfs = [&](int x) {\n vis[x] = true;\n if (dag[x].empty()) {\n loopB.push_back(x);\n return true;\n }\n for (int y : dag[x]) {\n if (vis[y]) continue;\n if (dfs(y)) return true;\n }\n return false;\n };\n vector<bool> usedA(A.size()), usedB(B.size());\n rep(p, A.size()) {\n if (dfs(A[p])) {\n loopA.push_back(A[p]);\n usedA[p] = true;\n }\n }\n rep(p, B.size()) {\n if (vis[B[p]]) usedB[p] = true;\n }\n if (loopA.size()) {\n int l = loopA.size();\n rep(j, l) ans.push_back({loopB[j], loopA[(j + 1) % l]});\n }\n vector<int> rA, rB;\n rep(p, A.size()) {\n if (!usedA[p]) rA.push_back(A[p]);\n }\n rep(p, B.size()) {\n if (!usedB[p]) rB.push_back(B[p]);\n }\n if (rA.empty() && rB.empty()) continue;\n if (rA.empty()) rA.push_back(A[0]);\n if (rB.empty()) rB.push_back(B[0]);\n while (rA.size() < rB.size()) rA.push_back(rA.back());\n while (rB.size() < rA.size()) rB.push_back(rB.back());\n rep(j, rA.size()) { ans.push_back({rB[j], rA[j]}); }\n }\n }\n cout << ans.size() << endl;\n for (auto [x, y] : ans) {\n cout << scc[x][0] + 1 << \" \" << p[scc[y][0]] + 1 << endl;\n }\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 0.5, "time_ms": 170, "memory_kb": 13620, "score_of_the_acc": -1.0414, "final_rank": 14 }, { "submission_id": "aoj_1653_10567567", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T>& a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end());\n return result;\n }\n\n private:\n int _n;\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E>\nstruct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n\n int num_vertices() { return _n; }\n\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct scc_graph {\n public:\n scc_graph() : internal(0) {}\n scc_graph(int n) : internal(n) {}\n\n void add_edge(int from, int to) {\n int n = internal.num_vertices();\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n internal.add_edge(from, to);\n }\n\n std::vector<std::vector<int>> scc() { return internal.scc(); }\n\n private:\n internal::scc_graph internal;\n};\n\n} // namespace atcoder\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) exit(0);\n atcoder::mf_graph<int> g(2 * n + 2);\n vector<int> s(m), t(m);\n rep(i, m) {\n cin >> s[i] >> t[i];\n s[i]--, t[i]--;\n g.add_edge(s[i], t[i] + n, 1);\n }\n rep(i, n) {\n g.add_edge(2 * n, i, 1);\n g.add_edge(i + n, 2 * n + 1, 1);\n }\n assert(g.flow(2 * n, 2 * n + 1) == n);\n vector<int> p(n), ip(n);\n vector<pair<int, int>> rem;\n for (auto [from, to, cap, flow] : g.edges()) {\n if (from == 2 * n || to == 2 * n + 1) continue;\n to -= n;\n if (flow == 1) {\n p[from] = to;\n ip[to] = from;\n } else {\n rem.push_back({from, to});\n }\n }\n\n // 完全マッチングを0->0,1->1,...と置きなおす\n // 他の辺をu->vと書き直す\n // x->yが固定されたとき、y->xのパスがあればいい\n // 強連結であればよい\n atcoder::scc_graph sg(n);\n for (auto& [x, y] : rem) {\n // x->ip[y]\n y = ip[y];\n sg.add_edge(x, y);\n }\n auto scc = sg.scc();\n int k = scc.size();\n vector<int> group(n);\n rep(i, k) for (int x : scc[i]) group[x] = i;\n vector<int> in(k), out(k);\n atcoder::dsu uf(k);\n vector<bitset<2000>> bs(n);\n for (auto [x, y] : rem) {\n if (group[x] == group[y]) continue;\n bs[group[x]].set(group[y]);\n out[group[x]]++, in[group[y]]++;\n uf.merge(group[x], group[y]);\n }\n for (int i = n - 1; i >= 0; i--) {\n bs[i].set(i);\n for (int j = i + 1; j < n; j++) {\n if (bs[i][j]) bs[i] |= bs[j];\n }\n }\n vector<vector<int>> src(k), snk(k);\n rep(i, k) {\n if (in[i] == 0 && out[i] == 0) continue;\n if (out[i] == 0) snk[uf.leader(i)].push_back(i);\n if (in[i] == 0) src[uf.leader(i)].push_back(i);\n }\n\n vector<pair<int, int>> ans;\n rep(i, k) {\n if (uf.leader(i) == i) {\n auto A = src[i], B = snk[i];\n if (A.empty()) continue;\n int M = max(snk[i].size(), src[i].size());\n vector<bool> usedA(A.size()), usedB(B.size());\n // src->snkが到達不可能な時snk->srcに貼る\n rep(p, A.size()) {\n rep(q, B.size()) {\n if (!usedA[p] && !usedB[q] && !bs[A[p]][B[q]]) {\n ans.push_back({B[q], A[p]});\n usedA[p] = true;\n usedB[q] = true;\n }\n }\n }\n // 今、src->snkはどの対も到達可能\n vector<int> rA, rB;\n rep(p, A.size()) {\n if (!usedA[p]) rA.push_back(A[p]);\n }\n rep(p, B.size()) {\n if (!usedB[p]) rB.push_back(B[p]);\n }\n if (rA.empty()) rA.push_back(A[0]);\n if (rB.empty()) rB.push_back(B[0]);\n while (rA.size() < rB.size()) rA.push_back(rA.back());\n while (rB.size() < rA.size()) rB.push_back(rB.back());\n rep(j, max(rA.size(), rB.size())) { ans.push_back({rB[j], rA[j]}); }\n }\n }\n cout << ans.size() << endl;\n for (auto [x, y] : ans) {\n cout << scc[x][0] + 1 << \" \" << p[scc[y][0]] + 1 << endl;\n }\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 0.5, "time_ms": 300, "memory_kb": 13244, "score_of_the_acc": -1.5048, "final_rank": 17 }, { "submission_id": "aoj_1653_10206256", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n* @brief template\n*/\n\nvector<int> BipMatch(int n,int m,vector<vector<int>>& g){\n vector<int> L(n,-1),R(m,-1),d(n);\n queue<int> que;\n auto dfs=[&](auto& dfs,int v)->bool{\n int nd=exchange(d[v],0)+1;\n for(auto& u:g[v]){\n if(R[u]==-1 or (d[R[u]]==nd and dfs(dfs,R[u]))){\n L[v]=u,R[u]=v;\n return 1;\n }\n }\n return 0;\n };\n for(;;){\n d.assign(n,0);\n queue<int> dummy;\n swap(que,dummy);\n bool ch=0; \n rep(i,0,n)if(L[i]==-1){\n que.push(i);\n d[i]=1;\n }\n while(!que.empty()){\n int v=que.front();\n que.pop();\n for(auto& u:g[v]){\n if(R[u]==-1)ch=1;\n else if(!d[R[u]]){\n d[R[u]]=d[v]+1;\n que.push(R[u]);\n }\n }\n }\n if(!ch)break;\n rep(i,0,n)if(L[i]==-1)dfs(dfs,i);\n }\n return L;\n}\n\n/**\n * @brief Bipartite Matching\n */\n\n struct SCC{\n int n,m,cur;\n vector<vector<int>> g;\n vector<int> low,ord,id;\n SCC(int _n=0):n(_n),m(0),cur(0),g(_n),low(_n),ord(_n,-1),id(_n){}\n void resize(int _n){\n n=_n;\n g.resize(n);\n low.resize(n);\n ord.resize(n,-1);\n id.resize(n);\n }\n void add_edge(int u,int v){g[u].emplace_back(v);}\n void dfs(int v,vector<int>& used){\n ord[v]=low[v]=cur++;\n used.emplace_back(v);\n for(auto& nxt:g[v]){\n if(ord[nxt]==-1){\n dfs(nxt,used); chmin(low[v],low[nxt]);\n }\n else{\n chmin(low[v],ord[nxt]);\n }\n }\n if(ord[v]==low[v]){\n while(1){\n int add=used.back(); used.pop_back();\n ord[add]=n; id[add]=m;\n if(v==add)break;\n }\n m++;\n }\n }\n void run(){\n vector<int> used;\n rep(v,0,n)if(ord[v]==-1)dfs(v,used);\n for(auto& x:id)x=m-1-x;\n }\n};\n\n/**\n * @brief Strongly Connected Components\n */\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0) return 0;\n vector<int> S(M), T(M);\n vector<vector<int>> G(N);\n rep(i,0,M) cin >> S[i] >> T[i], S[i]--, T[i]--, G[S[i]].push_back(T[i]);\n vector<int> BM = BipMatch(N,N,G);\n SCC scc(N*2);\n rep(i,0,M) {\n if (BM[S[i]] == T[i]) {\n scc.add_edge(S[i], T[i]+N);\n scc.add_edge(T[i]+N, S[i]);\n }\n else {\n scc.add_edge(S[i], T[i]+N);\n }\n }\n scc.run();\n int K = scc.m;\n vector<vector<int>> H(K), RH(K);\n vector<int> SID(K), TID(K);\n rrep(i,0,N) {\n SID[scc.id[i]] = i;\n TID[scc.id[i+N]] = i;\n }\n rep(i,0,M) {\n if (scc.id[S[i]] != scc.id[T[i]+N]) {\n H[scc.id[S[i]]].push_back(scc.id[T[i]+N]);\n RH[scc.id[T[i]+N]].push_back(scc.id[S[i]]);\n }\n }\n vector<int> Source, Sink;\n rep(i,0,K) {\n if (H[i].empty() && RH[i].empty()) continue;\n if (H[i].empty()) Sink.push_back(i);\n if (RH[i].empty()) Source.push_back(i);\n }\n\n // cout << \"Source:\";\n // for (int so : Source) cout << so << ' ';\n // cout << endl;\n // cout << \"Sink:\";\n // for (int si : Sink) cout << si << ' ';\n // cout << endl;\n\n vector<bool> vis(K, false);\n auto findsink = [&](auto self, int V) -> int {\n vis[V] = true;\n if (H[V].empty()) return V;\n for (int NV : H[V]) {\n if (vis[NV]) continue;\n int ret = self(self, NV);\n if (ret >= 0) return ret;\n }\n return -1;\n };\n vector<pair<int,int>> P;\n vector<bool> soflag(K, false), siflag(K, false);\n for (int so : Source) {\n int ret = findsink(findsink, so);\n //cout << \"findsink:\" << so << ' ' << ret << endl;\n if (ret >= 0) P.push_back({so, ret}), soflag[so] = siflag[ret] = true;\n }\n vector<pair<int,int>> ANS;\n rep(i,0,SZ(P)-1) {\n ANS.push_back({P[i+1].second, P[i].first});\n }\n if (!P.empty()) ANS.push_back({P[0].second, P.back().first});\n\n vector<int> ConID(K,-1);\n auto DFS = [&](auto self, int V, int id) -> void {\n ConID[V] = id;\n for (int NV : H[V]) {\n if (ConID[NV] == -1) self(self, NV, id);\n }\n for (int NV : RH[V]) {\n if (ConID[NV] == -1) self(self, NV, id);\n }\n };\n\n int L = 0;\n rep(i,0,K) {\n if (ConID[i] == -1) DFS(DFS,i,L), L++;\n }\n\n vector<int> unso, unsi;\n for (int so : Source) if (!soflag[so]) unso.push_back(so);\n for (int si : Sink) if (!siflag[si]) unsi.push_back(si);\n\n // cout << \"SID:\";\n // for (int sid : SID) cout << sid << ' ';\n // cout << endl;\n // cout << \"TID:\";\n // for (int tid : TID) cout << tid << ' ';\n // cout << endl;\n // cout << \"Unso:\";\n // for (int si : unso) cout << si << ' ';\n // cout << endl;\n // cout << \"Unsi:\";\n // for (int si : unsi) cout << si << ' ';\n // cout << endl << endl;\n\n while(!unso.empty() || !unsi.empty()) {\n if (unso.empty()) {\n ANS.push_back({unsi.back(), Source[0]});\n unsi.pop_back();\n }\n else if (unsi.empty()) {\n ANS.push_back({Sink[0], unso.back()});\n unso.pop_back();\n }\n else {\n ANS.push_back({unsi.back(), unso.back()});\n unso.pop_back();\n unsi.pop_back();\n }\n }\n cout << SZ(ANS) << endl;\n for (auto [x,y] : ANS) {\n cout << SID[x] + 1 << ' ' << TID[y] + 1 << endl;\n }\n}\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 7396, "score_of_the_acc": -0.2425, "final_rank": 1 }, { "submission_id": "aoj_1653_9298405", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nmt19937 engine(42);\n\ntemplate<class flow_t = int>\nstruct Dinic\n{\n const flow_t INF;\n \n struct edge\n {\n int to;\n flow_t cap;\n int rev; \n bool isrev;\n int idx;\n };\n \n vector<vector<edge>> graph;\n vector<int> min_cost, iter;\n \n explicit Dinic(int V) : INF(1e9), graph(V) { }\n \n void add_edge(int from, int to, flow_t cap, int idx = -1)\n {\n graph[from].emplace_back((edge){to, cap, (int)graph[to].size(), false, idx});\n graph[to].emplace_back((edge){from, 0, (int)graph[from].size() - 1, true, idx});\n }\n \n bool build_augment_path(int s, int t)\n {\n min_cost.assign(graph.size(), -1);\n queue<int> que;\n min_cost[s] = 0;\n que.push(s);\n while (!que.empty() && min_cost[t] == -1)\n {\n int p = que.front();\n que.pop();\n \n for (auto &e : graph[p])\n {\n if (e.cap > 0 && min_cost[e.to] == -1)\n {\n min_cost[e.to] = min_cost[p] + 1;\n que.push(e.to);\n }\n }\n }\n return min_cost[t] != -1;\n }\n \n flow_t find_min_dist_augment_path(int idx, const int t, flow_t flow)\n {\n if (idx == t) return flow;\n for (int &i = iter[idx]; i < (int)graph[idx].size(); ++i)\n {\n edge &e = graph[idx][i];\n if (e.cap > 0 && min_cost[idx] < min_cost[e.to])\n {\n flow_t d = find_min_dist_augment_path(e.to, t, min(flow, e.cap));\n if (d > 0)\n {\n e.cap -= d;\n graph[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n \n flow_t max_flow(int s, int t)\n {\n flow_t flow = 0;\n while (build_augment_path(s, t))\n {\n iter.assign(graph.size(), 0);\n flow_t f;\n while ((f = find_min_dist_augment_path(s, t, INF)) > 0) flow += f;\n }\n return flow;\n }\n \n};\n\nstruct SCC {\n using G = vector<vector<int>>;\n \n int n;\n const G &g;\n G edge, redge;\n vector<int> comp, order, used; // comp にグループの番号が振られる\n\n SCC(int n, G &g) : n(n), g(g), edge(n), redge(n), comp(n, -1), used(n) {\n for (int i = 0; i < n; i++) {\n for (auto e : g[i]) {\n edge[i].emplace_back(e);\n redge[e].emplace_back(i);\n }\n }\n }\n\n void dfs(int idx) {\n if (used[idx]) return;\n used[idx] = true;\n for (int to : edge[idx]) dfs(to);\n order.emplace_back(idx);\n }\n\n void rdfs(int idx, int cnt) {\n if (comp[idx] != -1) return;\n comp[idx] = cnt;\n for (int to : redge[idx]) rdfs(to, cnt);\n }\n\n G build() {\n // 強連結成分ごとに,トポソ順で返す\n G t;\n for (int i = 0; i < n; i++) dfs(i);\n reverse(order.begin(), order.end());\n int ptr = 0;\n for (int i : order) {\n if (comp[i] == -1) {\n rdfs(i, ptr), ptr++;\n }\n }\n\n t.resize(ptr);\n for (int i = 0; i < n; i++) {\n t[comp[i]].emplace_back(i);\n }\n return t;\n }\n};\n\n\n\nbool is_end = false;\n\nvoid solve()\n{\n int N, M; cin >> N >> M;\n if (N == 0 && M == 0)\n {\n is_end = true;\n return;\n }\n \n int S = 2 * N, T = S + 1;\n Dinic<int> mf(T + 1);\n vector<array<int, 2>> edge;\n for (int i = 0; i < M; ++i)\n {\n int s, t; cin >> s >> t;\n s--, t--;\n mf.add_edge(s, N + t, 1);\n edge.push_back({s, N + t});\n }\n for (int i = 0; i < N; ++i)\n {\n mf.add_edge(S, i, 1);\n mf.add_edge(N + i, T, 1);\n }\n \n int F = mf.max_flow(S, T);\n assert(F == N);\n \n vector<int> matching(2 * N, -1);\n for (int s = 0; s < N; ++s)\n {\n for (auto e : mf.graph[s])\n {\n if (e.cap == 0 && e.to != S)\n {\n int t = e.to;\n assert(N <= t && t < 2 * N);\n matching[s] = t;\n matching[t] = s;\n break;\n }\n }\n }\n \n for (int i = 0; i < 2 * N; ++i)\n {\n int m = matching[i];\n if (i < N) assert(N <= m && m < 2 * N);\n else assert(0 <= m && m < N);\n }\n \n vector<vector<int>> graph(N);\n for (auto [s, t] : edge)\n {\n int ns = matching[t];\n if (s != ns) graph[s].emplace_back(ns);\n }\n \n SCC scc(N, graph);\n vector<vector<int>> components = scc.build();\n vector<int> v_to_comp(N, -1);\n for (int i = 0; i < components.size(); ++i)\n {\n for (auto v : components[i])\n {\n v_to_comp[v] = i;\n }\n }\n \n int C = components.size();\n vector<vector<int>> dag(C);\n vector<vector<int>> rev(C);\n for (auto [s, t] : edge)\n {\n int ns = matching[t];\n int sid = v_to_comp[s], nsid = v_to_comp[ns];\n if (sid != nsid)\n {\n dag[sid].emplace_back(nsid);\n rev[nsid].emplace_back(sid);\n }\n }\n for (auto &vec : dag)\n {\n sort(vec.begin(), vec.end());\n vec.erase(unique(vec.begin(), vec.end()), vec.end());\n }\n for (auto &vec : rev)\n {\n sort(vec.begin(), vec.end());\n vec.erase(unique(vec.begin(), vec.end()), vec.end());\n }\n \n using bs = bitset<2048>;\n vector<bs> in(C, 0), out(C, 0);\n vector<int> invec, outvec;\n for (int i = 0; i < C; ++i)\n {\n if (rev[i].size() == 0 && dag[i].size() > 0) invec.emplace_back(i);\n if (dag[i].size() == 0 && rev[i].size() > 0) outvec.emplace_back(i);\n in[i][i] = 1;\n for (auto nv : dag[i]) in[nv] |= in[i];\n }\n for (int i = C - 1; i >= 0; --i)\n {\n out[i][i] = 1;\n for (auto nv : dag[i]) out[i] |= out[nv];\n }\n \n // cout << -1 << endl;\n // for (int i = 0; i < C; ++i)\n // {\n // cout << i << \" : \";\n // for (auto v : components[i]) cout << v << \" \";\n // cout << endl;\n // }\n // for (int i = 0; i < C; ++i)\n // {\n // cout << in[i] << \" \" << out[i] << endl;\n // }\n \n vector<array<int, 2>> res;\n \n int K = max(invec.size(), outvec.size());\n \n while (K > 0)\n {\n int sid = engine() % invec.size();\n int tid = engine() % outvec.size();\n \n int s = invec[sid];\n int t = outvec[tid];\n \n bool keeps = false, keept = false;\n \n if (K == 1)\n {\n res.push_back({t, s});\n break;\n }\n \n // out[s] > in[t] <=> t->s is in\n if ((out[s] & in[t]) == in[t])\n {\n if (invec.size() >= outvec.size()) continue;\n keeps = true;\n }\n \n // out[s] < In[t] <=> t->s is out\n if ((out[s] & in[t]) == out[s])\n {\n if (invec.size() <= outvec.size()) continue;\n keept = true;\n }\n \n dag[t].emplace_back(s);\n rev[s].emplace_back(t);\n res.push_back({t, s});\n if (!keeps) invec.erase(invec.begin() + sid);\n if (!keept) outvec.erase(outvec.begin() + tid);\n \n // cout << s << \" \" << t << endl;\n // cout << t << \" \" << matching[s] - N << endl;\n \n {\n in[s] |= in[t];\n out[t] |= out[s];\n for (int i = 0; i < C; ++i)\n {\n if (out[s][i]) in[i] |= in[s];\n if (in[t][i]) out[i] |= out[t];\n }\n }\n \n K--;\n assert(max(invec.size(), outvec.size()) == K);\n }\n \n \n cout << res.size() << endl;\n for (auto [a, b] : res)\n {\n int s = components[a][0];\n int t = matching[components[b][0]];\n cout << s + 1 << \" \" << t - N + 1 << endl;\n }\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 15168, "score_of_the_acc": -2, "final_rank": 8 }, { "submission_id": "aoj_1653_9294945", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nmt19937 engine(42);\n\ntemplate<class flow_t = int>\nstruct Dinic\n{\n const flow_t INF;\n \n struct edge\n {\n int to;\n flow_t cap;\n int rev; \n bool isrev;\n int idx;\n };\n \n vector<vector<edge>> graph;\n vector<int> min_cost, iter;\n \n explicit Dinic(int V) : INF(1e9), graph(V) { }\n \n void add_edge(int from, int to, flow_t cap, int idx = -1)\n {\n graph[from].emplace_back((edge){to, cap, (int)graph[to].size(), false, idx});\n graph[to].emplace_back((edge){from, 0, (int)graph[from].size() - 1, true, idx});\n }\n \n bool build_augment_path(int s, int t)\n {\n min_cost.assign(graph.size(), -1);\n queue<int> que;\n min_cost[s] = 0;\n que.push(s);\n while (!que.empty() && min_cost[t] == -1)\n {\n int p = que.front();\n que.pop();\n \n for (auto &e : graph[p])\n {\n if (e.cap > 0 && min_cost[e.to] == -1)\n {\n min_cost[e.to] = min_cost[p] + 1;\n que.push(e.to);\n }\n }\n }\n return min_cost[t] != -1;\n }\n \n flow_t find_min_dist_augment_path(int idx, const int t, flow_t flow)\n {\n if (idx == t) return flow;\n for (int &i = iter[idx]; i < (int)graph[idx].size(); ++i)\n {\n edge &e = graph[idx][i];\n if (e.cap > 0 && min_cost[idx] < min_cost[e.to])\n {\n flow_t d = find_min_dist_augment_path(e.to, t, min(flow, e.cap));\n if (d > 0)\n {\n e.cap -= d;\n graph[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n \n flow_t max_flow(int s, int t)\n {\n flow_t flow = 0;\n while (build_augment_path(s, t))\n {\n iter.assign(graph.size(), 0);\n flow_t f;\n while ((f = find_min_dist_augment_path(s, t, INF)) > 0) flow += f;\n }\n return flow;\n }\n \n};\n\nbool is_end = false;\n\nvoid solve()\n{\n int N, M; cin >> N >> M;\n if (N == 0 && M == 0)\n {\n is_end = true;\n return;\n }\n \n vector<int> deg(2 * N, 0);\n int S = 2 * N, T = S + 1;\n Dinic<int> mf(T + 1);\n for (int i = 0; i < M; ++i)\n {\n int s, t; cin >> s >> t;\n s--, t--;\n mf.add_edge(s, N + t, 1);\n deg[s] += 1;\n deg[N + t] += 1;\n }\n for (int i = 0; i < N; ++i)\n {\n mf.add_edge(S, i, 1);\n mf.add_edge(N + i, T, 1);\n }\n \n int F = mf.max_flow(S, T);\n assert(F == N);\n \n vector<int> matching(2 * N, -1);\n for (int s = 0; s < N; ++s)\n {\n for (auto e : mf.graph[s])\n {\n if (e.cap == 0 && e.to != S)\n {\n int t = e.to;\n assert(N <= t && t < 2 * N);\n matching[s] = t;\n matching[t] = s;\n break;\n }\n }\n }\n \n for (int i = 0; i < 2 * N; ++i)\n {\n int m = matching[i];\n if (i < N) assert(N <= m && m < 2 * N);\n else assert(0 <= m && m < N);\n }\n \n vector<int> left, right;\n vector<int> free_left, free_right;\n for (int s = 0; s < N; ++s)\n {\n int t = matching[s];\n if (deg[s] == 1 && deg[t] == 1) continue;\n \n if (deg[s] == 1)\n {\n left.emplace_back(s);\n free_right.emplace_back(t);\n }\n else if (deg[t] == 1)\n {\n right.emplace_back(t);\n free_left.emplace_back(s);\n }\n else\n {\n free_left.emplace_back(s);\n free_right.emplace_back(t);\n }\n }\n \n \n vector<pair<int, int>> res;\n if (left.size() < right.size())\n {\n for (int i = 0; i < left.size(); ++i)\n {\n res.push_back({left[i], right[i]});\n }\n \n int siz = free_left.size();\n for (int i = left.size(); i < right.size(); ++i)\n {\n int s = matching[right[i]];\n \n while (s == matching[right[i]])\n {\n int id = engine() % siz;\n s = free_left[id];\n }\n \n res.push_back({s, right[i]});\n }\n \n }\n else\n {\n for (int i = 0; i < right.size(); ++i)\n {\n res.push_back({left[i], right[i]});\n }\n \n int siz = free_right.size();\n for (int i = right.size(); i < left.size(); ++i)\n {\n int t = matching[left[i]];\n \n while (t == matching[left[i]])\n {\n int id = engine() % siz;\n t = free_right[id];\n }\n \n res.push_back({left[i], t});\n }\n \n }\n \n assert(res.size() == max(left.size(), right.size()));\n cout << res.size() << endl;\n for (auto [s, t] : res)\n {\n cout << s + 1 << \" \" << t - N + 1 << endl;\n }\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 0.5, "time_ms": 290, "memory_kb": 10216, "score_of_the_acc": -1.1712, "final_rank": 16 }, { "submission_id": "aoj_1653_9294936", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nmt19937 engine(42);\n\ntemplate<class flow_t = int>\nstruct Dinic\n{\n const flow_t INF;\n \n struct edge\n {\n int to;\n flow_t cap;\n int rev; \n bool isrev;\n int idx;\n };\n \n vector<vector<edge>> graph;\n vector<int> min_cost, iter;\n \n explicit Dinic(int V) : INF(1e9), graph(V) { }\n \n void add_edge(int from, int to, flow_t cap, int idx = -1)\n {\n graph[from].emplace_back((edge){to, cap, (int)graph[to].size(), false, idx});\n graph[to].emplace_back((edge){from, 0, (int)graph[from].size() - 1, true, idx});\n }\n \n bool build_augment_path(int s, int t)\n {\n min_cost.assign(graph.size(), -1);\n queue<int> que;\n min_cost[s] = 0;\n que.push(s);\n while (!que.empty() && min_cost[t] == -1)\n {\n int p = que.front();\n que.pop();\n \n for (auto &e : graph[p])\n {\n if (e.cap > 0 && min_cost[e.to] == -1)\n {\n min_cost[e.to] = min_cost[p] + 1;\n que.push(e.to);\n }\n }\n }\n return min_cost[t] != -1;\n }\n \n flow_t find_min_dist_augment_path(int idx, const int t, flow_t flow)\n {\n if (idx == t) return flow;\n for (int &i = iter[idx]; i < (int)graph[idx].size(); ++i)\n {\n edge &e = graph[idx][i];\n if (e.cap > 0 && min_cost[idx] < min_cost[e.to])\n {\n flow_t d = find_min_dist_augment_path(e.to, t, min(flow, e.cap));\n if (d > 0)\n {\n e.cap -= d;\n graph[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n \n flow_t max_flow(int s, int t)\n {\n flow_t flow = 0;\n while (build_augment_path(s, t))\n {\n iter.assign(graph.size(), 0);\n flow_t f;\n while ((f = find_min_dist_augment_path(s, t, INF)) > 0) flow += f;\n }\n return flow;\n }\n \n};\n\nbool is_end = false;\n\nvoid solve()\n{\n int N, M; cin >> N >> M;\n if (N == 0 && M == 0)\n {\n is_end = true;\n return;\n }\n \n vector<int> deg(2 * N, 0);\n int S = 2 * N, T = S + 1;\n Dinic<int> mf(T + 1);\n for (int i = 0; i < M; ++i)\n {\n int s, t; cin >> s >> t;\n s--, t--;\n mf.add_edge(s, N + t, 1);\n deg[s] += 1;\n deg[N + t] += 1;\n }\n for (int i = 0; i < N; ++i)\n {\n mf.add_edge(S, i, 1);\n mf.add_edge(N + i, T, 1);\n }\n \n int F = mf.max_flow(S, T);\n assert(F == N);\n \n vector<int> matching(2 * N, -1);\n for (int s = 0; s < N; ++s)\n {\n for (auto e : mf.graph[s])\n {\n if (e.cap == 0 && e.to != S)\n {\n int t = e.to;\n assert(N <= t && t < 2 * N);\n matching[s] = t;\n matching[t] = s;\n break;\n }\n }\n }\n \n vector<int> left, right;\n vector<int> free_left, free_right;\n for (int s = 0; s < N; ++s)\n {\n int t = matching[s];\n if (deg[s] == 1 && deg[t] == 1) continue;\n if (deg[s] == 1)\n {\n left.emplace_back(s);\n free_right.emplace_back(t);\n }\n if (deg[t] == 1)\n {\n right.emplace_back(t);\n free_left.emplace_back(s);\n }\n }\n \n \n vector<pair<int, int>> res;\n if (left.size() < right.size())\n {\n for (int i = 0; i < left.size(); ++i)\n {\n res.push_back({left[i], right[i]});\n }\n \n int siz = free_left.size();\n for (int i = left.size(); i < right.size(); ++i)\n {\n int s = matching[right[i]];\n \n while (s == matching[right[i]])\n {\n int id = engine() % siz;\n s = free_left[id];\n }\n \n res.push_back({s, right[i]});\n }\n \n }\n else\n {\n for (int i = 0; i < right.size(); ++i)\n {\n res.push_back({left[i], right[i]});\n }\n \n int siz = free_right.size();\n for (int i = right.size(); i < left.size(); ++i)\n {\n int t = matching[left[i]];\n \n while (t == matching[left[i]])\n {\n int id = engine() % siz;\n t = free_right[id];\n }\n \n res.push_back({left[i], t});\n }\n \n }\n \n cout << res.size() << endl;\n for (auto [s, t] : res)\n {\n cout << s + 1 << \" \" << t - N + 1 << endl;\n }\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 0.5, "time_ms": 290, "memory_kb": 10196, "score_of_the_acc": -1.1692, "final_rank": 15 }, { "submission_id": "aoj_1653_9093082", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 1 \"cp-library/src/graph/max_flow.hpp\"\ntemplate < class Cap > struct mf_graph {\n public:\n explicit mf_graph(int n) : n(n), g(n) {}\n \n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n assert(0 <= cap);\n int m = pos.size();\n pos.push_back({from, g[from].size()});\n int from_id = g[from].size();\n int to_id = g[to].size() + (from == to);\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to; Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = pos.size();\n assert(0 <= i and i < m);\n _edge e = g[pos[i].first][pos[i].second];\n _edge r = g[e.to][e.rev];\n return edge{pos[i].first, e.to, e.cap + r.cap, r.cap};\n }\n\n vector<edge> edges() {\n int m = pos.size();\n vector<edge> res(m);\n for(int i : rep(m)) res[i] = get_edge(i);\n return res;\n }\n\n void change_edge(int i, Cap cap, Cap flow) {\n int m = pos.size();\n assert(0 <= i and i < m);\n assert(0 <= flow and flow <= cap);\n _edge& e = g[pos[i].first][pos[i].second];\n _edge& r = g[e.to][e.rev];\n e.cap = cap - flow;\n r.cap = flow;\n }\n\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s and s < n);\n assert(0 <= t and t < n);\n assert(s != t);\n\n vector<int> level(n), iter(n);\n auto bfs = [&]() {\n fill(level.begin(), level.end(), -1);\n level[s] = 0;\n queue<int> q;\n q.push(s);\n while(not q.empty()) {\n int v = q.front(); q.pop();\n for(_edge e : g[v]) {\n if(e.cap == 0 or level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if(e.to == t) return;\n q.push(e.to);\n }\n }\n };\n\n auto dfs = [&](auto self, int v, Cap up) {\n if(v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for(int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if(level_v <= level[e.to] or g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));\n if(d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if(res == up) return res;\n }\n level[v] = n;\n return res;\n };\n\n Cap flow = 0;\n while(flow < flow_limit) {\n bfs();\n if(level[t] == -1) break;\n fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if(f == 0) break;\n flow += f;\n }\n return flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, numeric_limits<Cap>::max());\n }\n\n vector<int> min_cut(int s) {\n vector<int> visited(n, 0);\n queue<int> q;\n q.push(s);\n while(not q.empty()) {\n int p = q.front(); q.pop();\n visited[p] = 1;\n for(_edge e : g[p]) {\n if(e.cap && not visited[e.to]) {\n visited[e.to] = 1;\n q.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int n;\n struct _edge {\n int to, rev; Cap cap;\n };\n vector<pair<int,int>> pos;\n vector<vector<_edge>> g;\n};\n#line 4 \"main.cpp\"\n\nstruct scc_graph {\n using size_type = int;\n size_type n;\n std::vector<std::vector<size_type>> g;\n\n size_type group_num;\n std::vector<size_type> ids;\n std::vector<std::vector<size_type>> scc;\n std::vector<std::vector<size_type>> dag;\n\n scc_graph(const size_type n) : n(n), g(n), group_num(0), ids(n) {}\n\n void add_edge(const size_type from, const size_type to) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n g[from].push_back(to);\n }\n void add_vertex(const size_type k) {\n assert(0 <= k);\n g.resize(n + k);\n ids.resize(n + k);\n n = n + k;\n }\n\n void build() {\n int now_ord = 0;\n std::vector<size_type> visited, low(n), ord(n, -1);\n visited.reserve(n);\n auto dfs = [&](auto self, size_type v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for(size_type to : g[v]) {\n if(ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if(low[v] == ord[v]) {\n while(true) {\n size_type u = visited.back(); visited.pop_back();\n ord[u] = n;\n ids[u] = group_num;\n if(u == v) break;\n }\n group_num++;\n }\n };\n for(size_type i = 0; i < n; i++) if(ord[i] == -1) dfs(dfs, i);\n for(size_type& x : ids) x = group_num - 1 - x;\n\n scc.resize(group_num);\n for(size_type i = 0; i < n; i++) scc[ids[i]].push_back(i);\n\n dag.resize(group_num);\n for(size_type from = 0; from < n; from++) {\n for(size_type to : g[from]) {\n const size_type from_id = ids[from];\n const size_type to_id = ids[to];\n if(from_id != to_id) dag[from_id].push_back(to_id);\n }\n }\n }\n};\n\nvoid solve(int n, int m) {\n mf_graph<int> B(1 + n + n + 1);\n int S = n + n, T = n + n + 1;\n for(int i : rep(m)) {\n int s = in(), t = in(); s--, t--;\n B.add_edge(s, n + t, 1);\n }\n for(int i : rep(n)) {\n B.add_edge(S, i, 1);\n B.add_edge(n + i, T, 1);\n }\n int f = B.flow(S, T);\n assert(f == n);\n\n vector<int> p(n), q(n);\n for(int i : rep(m)) {\n auto e = B.get_edge(i);\n if(e.flow == 1) {\n p[e.from] = e.to - n;\n q[e.to - n] = e.from;\n }\n }\n scc_graph G(n);\n for(int i : rep(m)) {\n auto e = B.get_edge(i);\n if(e.flow == 0) G.add_edge(e.from, q[e.to - n]);\n }\n G.build();\n const int a = G.group_num;\n vector<int> in(a, 0), out(a, 0);\n for(int from : rep(a)) {\n for(int to : G.dag[from]) {\n in[to]++;\n out[from]++;\n }\n }\n\n vector used(a, false);\n vector<pair<int,int>> path;\n for(int v : rep(a)) if(in[v] == 0 and out[v] > 0) {\n vector checked(a, false);\n auto dfs = [&](auto self, int x) -> bool {\n used[x] = true;\n if(out[x] == 0) {\n path.push_back({v, x});\n return true;\n }\n for(int to : G.dag[x]) if(not checked[to] and not used[to]) {\n if(self(self, to)) return true;\n }\n checked[x] = true;\n used[x] = false;\n return false;\n }; dfs(dfs, v);\n }\n\n vector<pair<int,int>> ans;\n {\n const int len = path.size();\n for(int i : rep(len)) {\n int s = G.scc[path[i].second][0];\n int t = p[G.scc[path[(i + 1) % len].first][0]];\n ans.push_back({s, t});\n }\n }\n\n vector<int> X, Y;\n int sX = -1, sY = -1;\n for(int v : rep(a)) {\n if(in [v] == 0 and out[v] == 0) continue;\n if(in [v] == 0) sX = v;\n if(out[v] == 0) sY = v;\n if(used[v]) continue;\n if(in [v] == 0) X.push_back(v);\n if(out[v] == 0) Y.push_back(v);\n }\n while(X.size() < Y.size()) X.push_back(sX);\n while(X.size() > Y.size()) Y.push_back(sY);\n {\n const int len = X.size();\n for(int i : rep(len)) {\n int s = G.scc[Y[i]][0];\n int t = p[G.scc[X[i]][0]];\n ans.push_back({s, t});\n }\n }\n print(ans.size());\n for(auto [s, t] : ans) print(s + 1, t + 1);\n}\n\nint main() {\n while(true) {\n int n = in(), m = in();\n if(make_pair(n, m) == make_pair(0, 0)) return 0;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 9764, "score_of_the_acc": -0.6656, "final_rank": 5 }, { "submission_id": "aoj_1653_8013625", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing P = pair<int,int>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define REP(i,n) for(ll i = 0;i < (ll)n;i++)\n#define FOR(i,s,n) for(ll i = (s);i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define MOD 998244353\n#define len(x) (int)(x).size()\n#define sz(x) (int)(x).size()\n#define LB(A,x) (int)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(ALL(A),x)-A.begin())\n//template<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\n//template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a < b){a==b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(a > b){a==b;return 1;}return 0;}\n\nbool solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0) return 1;\n vector<vector<int>> G(2*n);\n rep(i,m) {\n int u, v;\n cin >> u >> v;\n --u, --v;\n G[u].emplace_back(v+n);\n G[v+n].emplace_back(u);\n }\n vector<int> seen(2*n, 0);\n vector<P> ans;\n queue<int> que;\n rep(s,n) {\n if (seen[s]) continue;\n set<int> st1, st2;\n que.push(s);\n while(!que.empty()) {\n int v = que.front();\n que.pop();\n seen[v] = 1;\n if (v < n) st1.emplace(v);\n else st2.emplace(v);\n for (int nv : G[v]) {\n if (seen[nv]) continue;\n seen[nv] = 1;\n que.push(nv);\n }\n }\n //for (int v : st1) {\n // cout << v << \" \";\n //}\n //cout << endl;\n //for (int v : st2) {\n // cout << v << \" \";\n //}\n //cout << endl;\n if (len(st1) == 1) continue;\n set<int> l1, l2;\n int x = -1, y = -1;\n for (int v : st1) {\n if (len(G[v]) == 1) {\n l1.emplace(v);\n } else {\n x = v;\n }\n }\n for (int v : st2) {\n if (len(G[v]) == 1) {\n l2.emplace(v);\n } else {\n y = v;\n }\n }\n while(min(len(l1), len(l2))) {\n ans.emplace_back(make_pair(*l1.begin(), *l2.begin()));\n x = *l1.begin(), y = *l2.begin();\n l1.erase(l1.begin());\n l2.erase(l2.begin());\n }\n while (!l1.empty()) {\n ans.emplace_back(*l1.begin(), y);\n l1.erase(l1.begin());\n }\n while(!l2.empty()) {\n ans.emplace_back(x, *l2.begin());\n l2.erase(l2.begin());\n }\n }\n cout << len(ans) << endl;\n for (P p : ans) {\n cout << p.first+1 << \" \" << p.second+1-n << endl;\n }\n return 0;\n}\n\nint main(){\n while(!solve());\n return 0;\n}", "accuracy": 0.5, "time_ms": 180, "memory_kb": 4908, "score_of_the_acc": -0.2308, "final_rank": 9 }, { "submission_id": "aoj_1653_7966202", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#ifdef tabr\n#include \"library/debug.cpp\"\n#else\n#define debug(...)\n#endif\n\nstruct matching {\n vector<vector<int>> g;\n vector<int> pa;\n vector<int> pb;\n vector<int> was;\n int n, m;\n int res;\n int iter;\n\n matching(int _n, int _m) : n(_n), m(_m) {\n pa.assign(n, -1);\n pb.assign(m, -1);\n was.resize(n);\n g.resize(n);\n res = 0;\n iter = 0;\n }\n\n void add(int from, int to) {\n g[from].emplace_back(to);\n }\n\n bool dfs(int v) {\n was[v] = iter;\n for (int u : g[v]) {\n if (pb[u] == -1) {\n pa[v] = u;\n pb[u] = v;\n return true;\n }\n }\n for (int u : g[v]) {\n if (was[pb[u]] != iter && dfs(pb[u])) {\n pa[v] = u;\n pb[u] = v;\n return true;\n }\n }\n return false;\n }\n\n int solve() {\n while (true) {\n iter++;\n int add = 0;\n for (int i = 0; i < n; i++) {\n if (pa[i] == -1 && dfs(i)) {\n add++;\n }\n }\n if (add == 0) {\n break;\n }\n res += add;\n }\n return res;\n }\n};\n\nvector<int> scc(const vector<vector<int>> &g) {\n int n = (int) g.size();\n int cnt = 0;\n vector<vector<int>> rev_g(n);\n vector<int> order;\n vector<int> res(n, -1);\n vector<bool> was(n);\n for (int i = 0; i < n; i++) {\n for (int j : g[i]) {\n rev_g[j].emplace_back(i);\n }\n }\n function<void(int)> Dfs1 = [&](int v) {\n was[v] = true;\n for (int to : g[v]) {\n if (!was[to]) {\n Dfs1(to);\n }\n }\n order.emplace_back(v);\n };\n function<void(int)> Dfs2 = [&](int v) {\n for (int to : rev_g[v]) {\n if (res[to] == -1) {\n res[to] = res[v];\n Dfs2(to);\n }\n }\n };\n for (int i = 0; i < n; i++) {\n if (!was[i]) {\n Dfs1(i);\n }\n }\n for (int id = n - 1; id >= 0; id--) {\n int i = order[id];\n if (res[i] == -1) {\n res[i] = cnt++;\n Dfs2(i);\n }\n }\n return res;\n}\n\nvector<vector<int>> scc_graph(const vector<vector<int>> &g, const vector<int> &c) {\n vector<vector<int>> new_g(*max_element(c.begin(), c.end()) + 1);\n for (int i = 0; i < (int) g.size(); i++) {\n for (int j : g[i]) {\n if (c[i] < c[j]) {\n new_g[c[i]].emplace_back(c[j]);\n }\n }\n }\n for (int i = 0; i < (int) new_g.size(); i++) {\n sort(new_g[i].begin(), new_g[i].end());\n new_g[i].resize(unique(new_g[i].begin(), new_g[i].end()) - new_g[i].begin());\n }\n return new_g;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) {\n break;\n }\n matching mt(n, n);\n for (int i = 0; i < m; i++) {\n int x, y;\n cin >> x >> y;\n x--;\n y--;\n mt.add(x, y);\n }\n mt.solve();\n vector<vector<int>> g(2 * n);\n for (int i = 0; i < n; i++) {\n for (int j : mt.g[i]) {\n if (mt.pa[i] == j) {\n g[n + j].emplace_back(i);\n g[i].emplace_back(n + j);\n } else {\n g[i].emplace_back(n + j);\n }\n }\n }\n auto c = scc(g);\n auto f = scc_graph(g, c);\n int sz = (int) f.size();\n vector<pair<int, int>> ans;\n vector<int> deg(sz);\n for (int i = 0; i < sz; i++) {\n for (int j : f[i]) {\n deg[j]++;\n }\n }\n vector<int> h(sz);\n vector<pair<int, int>> t;\n for (int i = 0; i < sz; i++) {\n if (deg[i] != 0) {\n continue;\n }\n if (f[i].size() == 0) {\n continue;\n }\n set<int> st;\n function<bool(int)> Dfs = [&](int v) {\n st.emplace(v);\n if (f[v].size() == 0) {\n h[v] = 1;\n h[i] = 1;\n t.emplace_back(i, v);\n return true;\n }\n for (int to : f[v]) {\n if (h[to] || st.count(to)) {\n continue;\n }\n if (Dfs(to)) {\n h[to] = 1;\n h[v] = 1;\n return true;\n }\n }\n return false;\n };\n Dfs(i);\n }\n int z = (int) t.size();\n for (int i = 0; i < z; i++) {\n ans.emplace_back(t[(i + 1) % z].second, t[i].first);\n }\n vector<int> x, y;\n for (int i = 0; i < sz; i++) {\n if (deg[i] == 0 && f[i].size() == 0) {\n continue;\n }\n if (h[i]) {\n continue;\n }\n if (deg[i] == 0) {\n x.emplace_back(i);\n }\n if (f[i].size() == 0) {\n y.emplace_back(i);\n }\n }\n if (x.empty() && !y.empty()) {\n for (int i = 0; i < sz; i++) {\n if (deg[i] == 0 && f[i].size() == 0) {\n continue;\n }\n if (deg[i] == 0) {\n x.emplace_back(i);\n break;\n }\n }\n }\n if (!x.empty() && y.empty()) {\n for (int i = 0; i < sz; i++) {\n if (deg[i] == 0 && f[i].size() == 0) {\n continue;\n }\n if (f[i].size() == 0) {\n y.emplace_back(i);\n break;\n }\n }\n }\n for (int i = 0; i < (int) max(x.size(), y.size()); i++) {\n ans.emplace_back(y[min(i, (int) y.size() - 1)], x[min(i, (int) x.size() - 1)]);\n }\n vector<int> r(sz), s(sz);\n for (int i = 0; i < n; i++) {\n r[c[i]] = i + 1;\n }\n for (int i = n; i < 2 * n; i++) {\n s[c[i]] = i + 1 - n;\n }\n cout << ans.size() << '\\n';\n for (auto [i, j] : ans) {\n cout << r[i] << \" \" << s[j] << '\\n';\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 5908, "score_of_the_acc": -0.3282, "final_rank": 2 }, { "submission_id": "aoj_1653_6715112", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define rep2(i, x, n) for (int i = x; i <= n; i++)\n#define rep3(i, x, n) for (int i = x; i >= n; i--)\n#define each(e, v) for (auto &e : v)\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\n\nstruct Bipartite_Matching {\n vector<vector<int>> es;\n vector<int> d, match;\n vector<bool> used, used2;\n const int n, m;\n\n Bipartite_Matching(int n, int m) : es(n), d(n), match(m), used(n), used2(n), n(n), m(m) {}\n\n void add_edge(int u, int v) { es[u].push_back(v); }\n\n void _bfs() {\n fill(begin(d), end(d), -1);\n queue<int> que;\n for (int i = 0; i < n; i++) {\n if (!used[i]) que.emplace(i), d[i] = 0;\n }\n while (!que.empty()) {\n int i = que.front();\n que.pop();\n for (auto &e : es[i]) {\n int j = match[e];\n if (j != -1 && d[j] == -1) que.emplace(j), d[j] = d[i] + 1;\n }\n }\n }\n\n bool _dfs(int now) {\n used2[now] = true;\n for (auto &e : es[now]) {\n int u = match[e];\n if (u == -1 || (!used2[u] && d[u] == d[now] + 1 && _dfs(u))) {\n match[e] = now, used[now] = true;\n return true;\n }\n }\n return false;\n }\n\n int bipartite_matching() {\n fill(begin(match), end(match), -1), fill(begin(used), end(used), false);\n int ret = 0;\n while (true) {\n _bfs();\n fill(begin(used2), end(used2), false);\n int flow = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i] && _dfs(i)) flow++;\n }\n if (flow == 0) break;\n ret += flow;\n }\n return ret;\n }\n};\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n\n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n\n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n\n int operator[](int i) { return root(i); }\n\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n\n int size(int x) { return -data[root(x)]; }\n\n int count() { return cnt; };\n\n bool same(int x, int y) { return root(x) == root(y); }\n\n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <bool directed = true>\nstruct Graph {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es;\n const int n;\n int m;\n\n vector<int> deg1, deg2; // 出次数、入次数\n vector<int> id1, id2;\n vector<bool> used;\n\n Graph(int n) : es(n), n(n), m(0), deg1(n, 0), deg2(n, 0), id1(n, -1), id2(n, -1), used(n, false) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m);\n if (!directed) es[to].emplace_back(from, m);\n m++;\n deg1[from]++, deg2[to]++;\n }\n\n void dfs(int now, int s) {\n used[now] = true;\n if (deg1[now] == 0) {\n id1[s] = now;\n id2[now] = s;\n return;\n }\n each(e, es[now]) {\n if (used[e.to]) continue;\n dfs(e.to, s);\n if (id1[s] != -1) return;\n }\n }\n\n void solve() {\n rep(i, n) {\n if (deg2[i] == 0) dfs(i, i);\n }\n }\n};\n\ntemplate <bool directed = true>\nstruct Strongly_Connected_Components {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es, rs;\n vector<int> vs, comp;\n vector<bool> used;\n const int n;\n int m;\n\n Strongly_Connected_Components(int n) : es(n), rs(n), vs(n), comp(n), used(n), n(n), m(0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m), rs[to].emplace_back(from, m);\n if (!directed) es[to].emplace_back(from, m), rs[from].emplace_back(to, m);\n m++;\n }\n\n void _dfs(int now) {\n used[now] = true;\n for (auto &e : es[now]) {\n if (!used[e.to]) _dfs(e.to);\n }\n vs.push_back(now);\n }\n\n void _rdfs(int now, int cnt) {\n used[now] = true, comp[now] = cnt;\n for (auto &e : rs[now]) {\n if (!used[e.to]) _rdfs(e.to, cnt);\n }\n }\n\n Graph<true> decompose() {\n fill(begin(used), end(used), false);\n for (int i = 0; i < n; i++) {\n if (!used[i]) _dfs(i);\n }\n fill(begin(used), end(used), false), reverse(begin(vs), end(vs));\n int cnt = 0;\n for (auto &e : vs) {\n if (!used[e]) _rdfs(e, cnt++);\n }\n Graph<true> G(cnt);\n for (int i = 0; i < n; i++) {\n for (auto &e : es[i]) {\n int u = comp[i], v = comp[e.to];\n if (u != v) G.add_edge(u, v);\n }\n }\n return G;\n }\n\n int operator[](int k) const { return comp[k]; }\n};\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n\n if (N == 0) break;\n\n Bipartite_Matching BM(N, N);\n\n vector<int> u(M), v(M);\n rep(i, M) {\n cin >> u[i] >> v[i];\n u[i]--, v[i]--;\n BM.add_edge(u[i], v[i]);\n }\n\n BM.bipartite_matching();\n\n vector<int> p(N);\n rep(i, N) p[BM.match[i]] = i;\n\n // print(p);\n\n Strongly_Connected_Components G(N);\n Union_Find_Tree uf(N);\n\n rep(i, M) {\n if (u[i] == BM.match[v[i]]) continue;\n G.add_edge(u[i], BM.match[v[i]]);\n uf.unite(u[i], BM.match[v[i]]);\n }\n Graph G2 = G.decompose();\n int K = G2.n;\n G2.solve();\n vector<bool> used(K, false);\n\n vector<vector<int>> deg1(N), deg2(N); // 先頭、末端\n vector<int> V(K); // SCCの代表元\n // print(V);\n\n rep(i, N) {\n int c = G[i];\n if (used[c]) continue;\n V[c] = i;\n used[c] = true;\n if (G2.deg1[c] > 0 && G2.deg2[c] == 0) deg1[uf[i]].eb(c);\n if (G2.deg1[c] == 0 && G2.deg2[c] > 0) deg2[uf[i]].eb(c);\n }\n\n vector<pii> ans;\n\n vector<int> ids;\n int SX = 0, SY = 0;\n vector<int> xs1, ys1;\n vector<int> xs2, ys2;\n\n rep(i, N) {\n int X = sz(deg1[i]), Y = sz(deg2[i]);\n if (X == 0 && Y == 0) continue;\n each(e, deg1[i]) {\n if (G2.id1[e] != -1) {\n xs2.eb(e);\n ys2.eb(G2.id1[e]);\n }\n }\n each(e, deg1[i]) {\n if (G2.id1[e] == -1) xs1.eb(e);\n }\n each(e, deg2[i]) {\n if (G2.id2[e] == -1) ys1.eb(e);\n }\n }\n\n int L = sz(xs2);\n if (L == 0) {\n cout << \"0\\n\";\n continue;\n }\n rep(i, L) {\n int x = xs2[i], y = ys2[(i + 1) % L];\n ans.eb(V[y], p[V[x]]);\n }\n\n int X = sz(xs1), Y = sz(ys1);\n\n rep(j, min(X, Y)) {\n int x = xs1[j], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n\n if (X > Y) {\n rep2(j, Y, X - 1) {\n int x = xs1[j], y = ys2[0];\n ans.eb(V[y], p[V[x]]);\n }\n }\n if (Y > X) {\n rep2(j, X, Y - 1) {\n int x = xs2[0], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n }\n\n cout << sz(ans) << '\\n';\n if (!empty(ans)) each(e, ans) cout << e.first + 1 << ' ' << e.second + 1 << '\\n';\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 8892, "score_of_the_acc": -0.4652, "final_rank": 4 }, { "submission_id": "aoj_1653_6709084", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define rep2(i, x, n) for (int i = x; i <= n; i++)\n#define rep3(i, x, n) for (int i = x; i >= n; i--)\n#define each(e, v) for (auto &e : v)\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\n\nstruct Bipartite_Matching {\n vector<vector<int>> es;\n vector<int> d, match;\n vector<bool> used, used2;\n const int n, m;\n\n Bipartite_Matching(int n, int m) : es(n), d(n), match(m), used(n), used2(n), n(n), m(m) {}\n\n void add_edge(int u, int v) { es[u].push_back(v); }\n\n void _bfs() {\n fill(begin(d), end(d), -1);\n queue<int> que;\n for (int i = 0; i < n; i++) {\n if (!used[i]) que.emplace(i), d[i] = 0;\n }\n while (!que.empty()) {\n int i = que.front();\n que.pop();\n for (auto &e : es[i]) {\n int j = match[e];\n if (j != -1 && d[j] == -1) que.emplace(j), d[j] = d[i] + 1;\n }\n }\n }\n\n bool _dfs(int now) {\n used2[now] = true;\n for (auto &e : es[now]) {\n int u = match[e];\n if (u == -1 || (!used2[u] && d[u] == d[now] + 1 && _dfs(u))) {\n match[e] = now, used[now] = true;\n return true;\n }\n }\n return false;\n }\n\n int bipartite_matching() {\n fill(begin(match), end(match), -1), fill(begin(used), end(used), false);\n int ret = 0;\n while (true) {\n _bfs();\n fill(begin(used2), end(used2), false);\n int flow = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i] && _dfs(i)) flow++;\n }\n if (flow == 0) break;\n ret += flow;\n }\n return ret;\n }\n};\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n\n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n\n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n\n int operator[](int i) { return root(i); }\n\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n\n int size(int x) { return -data[root(x)]; }\n\n int count() { return cnt; };\n\n bool same(int x, int y) { return root(x) == root(y); }\n\n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <bool directed = true>\nstruct Graph {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es;\n const int n;\n int m;\n\n vector<int> deg1, deg2; // 出次数、入次数\n\n Graph(int n) : es(n), n(n), m(0), deg1(n, 0), deg2(n, 0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m);\n if (!directed) es[to].emplace_back(from, m);\n m++;\n deg1[from]++, deg2[to]++;\n }\n};\n\ntemplate <bool directed = true>\nstruct Strongly_Connected_Components {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es, rs;\n vector<int> vs, comp;\n vector<bool> used;\n const int n;\n int m;\n\n Strongly_Connected_Components(int n) : es(n), rs(n), vs(n), comp(n), used(n), n(n), m(0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m), rs[to].emplace_back(from, m);\n if (!directed) es[to].emplace_back(from, m), rs[from].emplace_back(to, m);\n m++;\n }\n\n void _dfs(int now) {\n used[now] = true;\n for (auto &e : es[now]) {\n if (!used[e.to]) _dfs(e.to);\n }\n vs.push_back(now);\n }\n\n void _rdfs(int now, int cnt) {\n used[now] = true, comp[now] = cnt;\n for (auto &e : rs[now]) {\n if (!used[e.to]) _rdfs(e.to, cnt);\n }\n }\n\n Graph<true> decompose() {\n fill(begin(used), end(used), false);\n for (int i = 0; i < n; i++) {\n if (!used[i]) _dfs(i);\n }\n fill(begin(used), end(used), false), reverse(begin(vs), end(vs));\n int cnt = 0;\n for (auto &e : vs) {\n if (!used[e]) _rdfs(e, cnt++);\n }\n Graph<true> G(cnt);\n for (int i = 0; i < n; i++) {\n for (auto &e : es[i]) {\n int u = comp[i], v = comp[e.to];\n if (u != v) G.add_edge(u, v);\n }\n }\n return G;\n }\n\n int operator[](int k) const { return comp[k]; }\n};\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n\n if (N == 0) break;\n\n Bipartite_Matching BM(N, N);\n\n vector<int> u(M), v(M);\n rep(i, M) {\n cin >> u[i] >> v[i];\n u[i]--, v[i]--;\n BM.add_edge(u[i], v[i]);\n }\n\n BM.bipartite_matching();\n\n vector<int> p(N);\n rep(i, N) p[BM.match[i]] = i;\n\n // print(p);\n\n Strongly_Connected_Components G(N);\n Union_Find_Tree uf(N);\n\n rep(i, M) {\n if (u[i] == BM.match[v[i]]) continue;\n G.add_edge(u[i], BM.match[v[i]]);\n uf.unite(u[i], BM.match[v[i]]);\n }\n Graph G2 = G.decompose();\n int K = G2.n;\n vector<bool> used(K, false);\n\n vector<vector<int>> deg1(N), deg2(N); // 先頭、末端\n vector<int> V(K); // SCCの代表元\n // print(V);\n\n rep(i, N) {\n int c = G[i];\n if (used[c]) continue;\n V[c] = i;\n used[c] = true;\n if (G2.deg1[c] > 0 && G2.deg2[c] == 0) deg1[uf[i]].eb(c);\n if (G2.deg1[c] == 0 && G2.deg2[c] > 0) deg2[uf[i]].eb(c);\n }\n\n vector<pii> ans;\n\n vector<int> ids;\n int SX = 0, SY = 0;\n vector<int> xs1, ys1;\n vector<int> xs2, ys2;\n\n rep(i, N) {\n int X = sz(deg1[i]), Y = sz(deg2[i]);\n if (X == 0 && Y == 0) continue;\n\n rep(j, X - 1) xs1.eb(deg1[i][j]);\n rep(j, Y - 1) ys1.eb(deg2[i][j]);\n\n xs2.eb(deg1[i].back());\n ys2.eb(deg2[i].back());\n }\n\n int L = sz(xs2);\n if (L == 0) {\n cout << \"0\\n\";\n continue;\n }\n rep(i, L) {\n int x = xs2[i], y = ys2[(i + 1) % L];\n ans.eb(V[y], p[V[x]]);\n }\n\n int X = sz(xs1), Y = sz(ys1);\n\n rep(j, min(X, Y)) {\n int x = xs1[j], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n\n if (X > Y) {\n rep2(j, Y, X - 1) {\n int x = xs1[j], y = ys2[0];\n ans.eb(V[y], p[V[x]]);\n }\n }\n if (Y > X) {\n rep2(j, X, Y - 1) {\n int x = xs2[0], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n }\n\n cout << sz(ans) << '\\n';\n if (!empty(ans)) each(e, ans) cout << e.first + 1 << ' ' << e.second + 1 << '\\n';\n }\n}", "accuracy": 0.5, "time_ms": 140, "memory_kb": 8836, "score_of_the_acc": -0.4598, "final_rank": 10 }, { "submission_id": "aoj_1653_6709051", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define rep2(i, x, n) for (int i = x; i <= n; i++)\n#define rep3(i, x, n) for (int i = x; i >= n; i--)\n#define each(e, v) for (auto &e : v)\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\n\nstruct Bipartite_Matching {\n vector<vector<int>> es;\n vector<int> d, match;\n vector<bool> used, used2;\n const int n, m;\n\n Bipartite_Matching(int n, int m) : es(n), d(n), match(m), used(n), used2(n), n(n), m(m) {}\n\n void add_edge(int u, int v) { es[u].push_back(v); }\n\n void _bfs() {\n fill(begin(d), end(d), -1);\n queue<int> que;\n for (int i = 0; i < n; i++) {\n if (!used[i]) que.emplace(i), d[i] = 0;\n }\n while (!que.empty()) {\n int i = que.front();\n que.pop();\n for (auto &e : es[i]) {\n int j = match[e];\n if (j != -1 && d[j] == -1) que.emplace(j), d[j] = d[i] + 1;\n }\n }\n }\n\n bool _dfs(int now) {\n used2[now] = true;\n for (auto &e : es[now]) {\n int u = match[e];\n if (u == -1 || (!used2[u] && d[u] == d[now] + 1 && _dfs(u))) {\n match[e] = now, used[now] = true;\n return true;\n }\n }\n return false;\n }\n\n int bipartite_matching() {\n fill(begin(match), end(match), -1), fill(begin(used), end(used), false);\n int ret = 0;\n while (true) {\n _bfs();\n fill(begin(used2), end(used2), false);\n int flow = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i] && _dfs(i)) flow++;\n }\n if (flow == 0) break;\n ret += flow;\n }\n return ret;\n }\n};\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n\n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n\n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n\n int operator[](int i) { return root(i); }\n\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n\n int size(int x) { return -data[root(x)]; }\n\n int count() { return cnt; };\n\n bool same(int x, int y) { return root(x) == root(y); }\n\n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <bool directed = true>\nstruct Graph {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es;\n const int n;\n int m;\n\n vector<int> deg1, deg2; // 出次数、入次数\n\n Graph(int n) : es(n), n(n), m(0), deg1(n, 0), deg2(n, 0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m);\n if (!directed) es[to].emplace_back(from, m);\n m++;\n deg1[from]++, deg2[to]++;\n }\n};\n\ntemplate <bool directed = true>\nstruct Strongly_Connected_Components {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es, rs;\n vector<int> vs, comp;\n vector<bool> used;\n const int n;\n int m;\n\n Strongly_Connected_Components(int n) : es(n), rs(n), vs(n), comp(n), used(n), n(n), m(0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m), rs[to].emplace_back(from, m);\n if (!directed) es[to].emplace_back(from, m), rs[from].emplace_back(to, m);\n m++;\n }\n\n void _dfs(int now) {\n used[now] = true;\n for (auto &e : es[now]) {\n if (!used[e.to]) _dfs(e.to);\n }\n vs.push_back(now);\n }\n\n void _rdfs(int now, int cnt) {\n used[now] = true, comp[now] = cnt;\n for (auto &e : rs[now]) {\n if (!used[e.to]) _rdfs(e.to, cnt);\n }\n }\n\n Graph<true> decompose() {\n fill(begin(used), end(used), false);\n for (int i = 0; i < n; i++) {\n if (!used[i]) _dfs(i);\n }\n fill(begin(used), end(used), false), reverse(begin(vs), end(vs));\n int cnt = 0;\n for (auto &e : vs) {\n if (!used[e]) _rdfs(e, cnt++);\n }\n Graph<true> G(cnt);\n for (int i = 0; i < n; i++) {\n for (auto &e : es[i]) {\n int u = comp[i], v = comp[e.to];\n if (u != v) G.add_edge(u, v);\n }\n }\n return G;\n }\n\n int operator[](int k) const { return comp[k]; }\n};\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n\n if (N == 0) break;\n\n Bipartite_Matching BM(N, N);\n\n vector<int> u(M), v(M);\n rep(i, M) {\n cin >> u[i] >> v[i];\n u[i]--, v[i]--;\n BM.add_edge(u[i], v[i]);\n }\n\n BM.bipartite_matching();\n\n vector<int> p(N);\n rep(i, N) p[BM.match[i]] = i;\n\n // print(p);\n\n Strongly_Connected_Components G(N);\n Union_Find_Tree uf(N);\n\n rep(i, M) {\n if (u[i] == BM.match[v[i]]) continue;\n G.add_edge(u[i], BM.match[v[i]]);\n uf.unite(u[i], BM.match[v[i]]);\n }\n Graph G2 = G.decompose();\n int K = G2.n;\n vector<bool> used(K, false);\n\n vector<vector<int>> deg1(N), deg2(N); // 先頭、末端\n vector<int> V(K); // SCCの代表元\n // print(V);\n\n rep(i, N) {\n int c = G[i];\n if (used[c]) continue;\n V[c] = i;\n used[c] = true;\n if (G2.deg1[c] > 0 && G2.deg2[c] == 0) deg1[uf[i]].eb(c);\n if (G2.deg1[c] == 0 && G2.deg2[c] > 0) deg2[uf[i]].eb(c);\n }\n\n vector<pii> ans;\n\n vector<int> ids;\n int SX = 0, SY = 0;\n vector<int> xs1, ys1;\n vector<int> xs2, ys2;\n\n rep(i, N) {\n int X = sz(deg1[i]), Y = sz(deg2[i]);\n if (X == 0 && Y == 0) continue;\n\n rep(j, X - 1) xs1.eb(deg1[i][j]);\n rep(j, Y - 1) ys1.eb(deg2[i][j]);\n\n xs2.eb(deg1[i].back());\n ys2.eb(deg2[i].back());\n }\n\n int L = sz(xs2);\n if (L == 0) {\n cout << \"0\\n\\n\";\n continue;\n }\n rep(i, L) {\n int x = xs2[i], y = ys2[(i + 1) % L];\n ans.eb(V[y], p[V[x]]);\n }\n\n int X = sz(xs1), Y = sz(ys1);\n\n rep(j, min(X, Y)) {\n int x = xs1[j], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n\n if (X > Y) {\n rep2(j, Y, X - 1) {\n int x = xs1[j], y = ys2[0];\n ans.eb(V[y], p[V[x]]);\n }\n }\n if (Y > X) {\n rep2(j, X, Y - 1) {\n int x = xs2[0], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n }\n\n cout << sz(ans) << '\\n';\n each(e, ans) cout << e.first + 1 << ' ' << e.second + 1 << '\\n';\n }\n}", "accuracy": 0.5, "time_ms": 150, "memory_kb": 8764, "score_of_the_acc": -0.4912, "final_rank": 12 }, { "submission_id": "aoj_1653_6709011", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define rep2(i, x, n) for (int i = x; i <= n; i++)\n#define rep3(i, x, n) for (int i = x; i >= n; i--)\n#define each(e, v) for (auto &e : v)\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\n\nstruct Bipartite_Matching {\n vector<vector<int>> es;\n vector<int> d, match;\n vector<bool> used, used2;\n const int n, m;\n\n Bipartite_Matching(int n, int m) : es(n), d(n), match(m), used(n), used2(n), n(n), m(m) {}\n\n void add_edge(int u, int v) { es[u].push_back(v); }\n\n void _bfs() {\n fill(begin(d), end(d), -1);\n queue<int> que;\n for (int i = 0; i < n; i++) {\n if (!used[i]) que.emplace(i), d[i] = 0;\n }\n while (!que.empty()) {\n int i = que.front();\n que.pop();\n for (auto &e : es[i]) {\n int j = match[e];\n if (j != -1 && d[j] == -1) que.emplace(j), d[j] = d[i] + 1;\n }\n }\n }\n\n bool _dfs(int now) {\n used2[now] = true;\n for (auto &e : es[now]) {\n int u = match[e];\n if (u == -1 || (!used2[u] && d[u] == d[now] + 1 && _dfs(u))) {\n match[e] = now, used[now] = true;\n return true;\n }\n }\n return false;\n }\n\n int bipartite_matching() {\n fill(begin(match), end(match), -1), fill(begin(used), end(used), false);\n int ret = 0;\n while (true) {\n _bfs();\n fill(begin(used2), end(used2), false);\n int flow = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i] && _dfs(i)) flow++;\n }\n if (flow == 0) break;\n ret += flow;\n }\n return ret;\n }\n};\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n\n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n\n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n\n int operator[](int i) { return root(i); }\n\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n\n int size(int x) { return -data[root(x)]; }\n\n int count() { return cnt; };\n\n bool same(int x, int y) { return root(x) == root(y); }\n\n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <bool directed = true>\nstruct Graph {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es;\n const int n;\n int m;\n\n vector<int> deg1, deg2; // 出次数、入次数\n\n Graph(int n) : es(n), n(n), m(0), deg1(n, 0), deg2(n, 0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m);\n if (!directed) es[to].emplace_back(from, m);\n m++;\n deg1[from]++, deg2[to]++;\n }\n};\n\ntemplate <bool directed = true>\nstruct Strongly_Connected_Components {\n struct edge {\n int to, id;\n edge(int to, int id) : to(to), id(id) {}\n };\n\n vector<vector<edge>> es, rs;\n vector<int> vs, comp;\n vector<bool> used;\n const int n;\n int m;\n\n Strongly_Connected_Components(int n) : es(n), rs(n), vs(n), comp(n), used(n), n(n), m(0) {}\n\n void add_edge(int from, int to) {\n es[from].emplace_back(to, m), rs[to].emplace_back(from, m);\n if (!directed) es[to].emplace_back(from, m), rs[from].emplace_back(to, m);\n m++;\n }\n\n void _dfs(int now) {\n used[now] = true;\n for (auto &e : es[now]) {\n if (!used[e.to]) _dfs(e.to);\n }\n vs.push_back(now);\n }\n\n void _rdfs(int now, int cnt) {\n used[now] = true, comp[now] = cnt;\n for (auto &e : rs[now]) {\n if (!used[e.to]) _rdfs(e.to, cnt);\n }\n }\n\n Graph<true> decompose() {\n fill(begin(used), end(used), false);\n for (int i = 0; i < n; i++) {\n if (!used[i]) _dfs(i);\n }\n fill(begin(used), end(used), false), reverse(begin(vs), end(vs));\n int cnt = 0;\n for (auto &e : vs) {\n if (!used[e]) _rdfs(e, cnt++);\n }\n Graph<true> G(cnt);\n for (int i = 0; i < n; i++) {\n for (auto &e : es[i]) {\n int u = comp[i], v = comp[e.to];\n if (u != v) G.add_edge(u, v);\n }\n }\n return G;\n }\n\n int operator[](int k) const { return comp[k]; }\n};\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n\n if (N == 0) break;\n\n Bipartite_Matching BM(N, N);\n\n vector<int> u(M), v(M);\n rep(i, M) {\n cin >> u[i] >> v[i];\n u[i]--, v[i]--;\n BM.add_edge(u[i], v[i]);\n }\n\n BM.bipartite_matching();\n\n vector<int> p(N);\n rep(i, N) p[BM.match[i]] = i;\n\n // print(p);\n\n Strongly_Connected_Components G(N);\n Union_Find_Tree uf(N);\n\n rep(i, M) {\n if (u[i] == BM.match[v[i]]) continue;\n G.add_edge(u[i], BM.match[v[i]]);\n uf.unite(u[i], BM.match[v[i]]);\n }\n Graph G2 = G.decompose();\n int K = G2.n;\n vector<bool> used(K, false);\n\n vector<vector<int>> deg1(N), deg2(N); // 先頭、末端\n vector<int> V(K); // SCCの代表元\n // print(V);\n\n rep(i, N) {\n int c = G[i];\n if (used[c]) continue;\n V[c] = i;\n used[c] = true;\n if (G2.deg1[c] > 0 && G2.deg2[c] == 0) deg1[uf[i]].eb(c);\n if (G2.deg1[c] == 0 && G2.deg2[c] > 0) deg2[uf[i]].eb(c);\n }\n\n vector<pii> ans;\n\n vector<int> ids;\n int SX = 0, SY = 0;\n vector<int> xs1, ys1;\n vector<int> xs2, ys2;\n\n rep(i, N) {\n int X = sz(deg1[i]), Y = sz(deg2[i]);\n if (X == 0 && Y == 0) continue;\n\n rep(j, X - 1) xs1.eb(deg1[i][j]);\n rep(j, Y - 1) ys1.eb(deg2[i][j]);\n\n xs2.eb(deg1[i].back());\n ys2.eb(deg2[i].back());\n }\n\n int L = sz(xs2);\n rep(i, L) {\n int x = xs2[i], y = ys2[(i + 1) % L];\n ans.eb(V[y], p[V[x]]);\n }\n\n int X = sz(xs1), Y = sz(ys1);\n // if (X == 0 && Y == 0) continue;\n\n rep(j, min(X, Y)) {\n int x = xs1[j], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n\n if (X > Y) {\n rep2(j, Y, X - 1) {\n int x = xs1[j], y = ys2[0];\n ans.eb(V[y], p[V[x]]);\n }\n }\n if (Y > X) {\n rep2(j, X, Y - 1) {\n int x = xs2[0], y = ys1[j];\n ans.eb(V[y], p[V[x]]);\n }\n }\n\n cout << sz(ans) << '\\n';\n each(e, ans) cout << e.first + 1 << ' ' << e.second + 1 << '\\n';\n }\n}", "accuracy": 0.5, "time_ms": 140, "memory_kb": 8952, "score_of_the_acc": -0.4711, "final_rank": 11 }, { "submission_id": "aoj_1653_6163964", "code_snippet": "#ifndef LOCAL\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define int ll\n\n#define rng(i,a,b) for(int i=int(a);i<int(b);i++)\n#define rep(i,b) rng(i,0,b)\n#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n\ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n\ntemplate<class t> using vc=vector<t>;\ntemplate<class t> using vvc=vc<vc<t>>;\n\nusing pi=pair<int,int>;\nusing vi=vc<int>;\n\ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.a<<\",\"<<p.b<<\"}\";\n}\n\ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n\n#define mp make_pair\n#define mt make_tuple\n#define one(x) memset(x,-1,sizeof(x))\n#define zero(x) memset(x,0,sizeof(x))\n#ifdef LOCAL\nvoid dmpr(ostream&os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" \";\n\tdmpr(os,args...);\n}\n#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)\n#else\n#define dmp2(...) void(0)\n#endif\n\nusing uint=unsigned;\nusing ull=unsigned long long;\n\ntemplate<class t,size_t n>\nostream& operator<<(ostream&os,const array<t,n>&a){\n\treturn os<<vc<t>(all(a));\n}\n\ntemplate<int i,class T>\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate<int i,class T,class H,class ...Args>\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<<get<i>(t);\n\tprint_tuple<i+1,T,Args...>(os,t);\n}\n\ntemplate<class ...Args>\nostream& operator<<(ostream&os,const tuple<Args...>&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple<Args...>,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\ntemplate<class t>\nvoid print(t x,int suc=1){\n\tcout<<x;\n\tif(suc==1)\n\t\tcout<<\"\\n\";\n\tif(suc==2)\n\t\tcout<<\" \";\n}\n\nll read(){\n\tll i;\n\tcin>>i;\n\treturn i;\n}\n\nvi readvi(int n,int off=0){\n\tvi v(n);\n\trep(i,n)v[i]=read()+off;\n\treturn v;\n}\n\npi readpi(int off=0){\n\tint a,b;cin>>a>>b;\n\treturn pi(a+off,b+off);\n}\n\ntemplate<class t,class u>\nvoid print(const pair<t,u>&p,int suc=1){\n\tprint(p.a,2);\n\tprint(p.b,suc);\n}\n\ntemplate<class T>\nvoid print(const vector<T>&v,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T>\nvoid print_offset(const vector<T>&v,ll off,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i]+off,i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T,size_t N>\nvoid print(const array<T,N>&v,int suc=1){\n\trep(i,N)\n\t\tprint(v[i],i==int(N)-1?suc:2);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate<class T>\nT sq(const T& t){\n\treturn t*t;\n}\n\n//#define CAPITAL\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)<<i)-1;\n}\n\nbool inc(int a,int b,int c){\n\treturn a<=b&&b<=c;\n}\n\ntemplate<class t> void mkuni(vc<t>&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif\n\treturn uniform_int_distribution<ll>(l, r)(gen);\n}\n\ntemplate<class t>\nvoid myshuffle(vc<t>&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate<class t>\nint lwb(const vc<t>&v,const t&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\n\nvvc<int> readGraph(int n,int m){\n\tvvc<int> g(n);\n\trep(i,m){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\t//sc.read(a,b);\n\t\ta--;b--;\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc<int> readTree(int n){\n\treturn readGraph(n,n-1);\n}\n\n//sz: the size of the max matching\n//VERIFY: yosupo\ntemplate<class E>\nstruct bipartitematching{\n\tint n,m;\n\tvvc<E> g;\n\tvi dist,vis;\n\tvi to,ot;\n\tint sz;\n\tbipartitematching(int nn,int mm):n(nn),m(mm),g(n),\n\tdist(n),vis(n),to(n,-1),ot(m,-1),sz(0){}\n\tvoid ae(int a,E e){\n\t\tg[a].pb(e);\n\t}\n\tvoid bfs(){\n\t\tfill(all(dist),-1);\n\t\tvi q;\n\t\tint h=0;\n\t\tauto rc=[&](int v,int d){\n\t\t\tif(v==-1||dist[v]!=-1)return;\n\t\t\tdist[v]=d;\n\t\t\tq.pb(v);\n\t\t};\n\t\trep(i,n)if(to[i]==-1)rc(i,0);\n\t\twhile(h<(int)q.size()){\n\t\t\tint v=q[h++];\n\t\t\tfor(auto e:g[v])\n\t\t\t\trc(ot[e],dist[v]+1);\n\t\t}\n\t}\n\tbool dfs(int v){\n\t\tif(vis[v])return 0;\n\t\tvis[v]=1;\n\t\tfor(auto e:g[v]){\n\t\t\tif(ot[e]==-1||(dist[v]+1==dist[ot[e]]&&dfs(ot[e]))){\n\t\t\t\tto[v]=e;\n\t\t\t\tot[e]=v;\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\tvoid calc(){\n\t\twhile(1){\n\t\t\tbfs();\n\t\t\tfill(all(vis),0);\n\t\t\tint f=0;\n\t\t\trep(i,n)if(to[i]==-1)\n\t\t\t\tif(dfs(i))f++;\n\t\t\tif(f==0)break;\n\t\t\tsz+=f;\n\t\t}\n\t}\n};\n\n//s: the number of SCCs.\n//bl[i]: SCC to which the vertex i belongs\n//idx[i]: list of the vertices in the i-th SCC\n//da: DAG of SCCs\n//SCCs are numbered in topological order\n//VERIFY: yosupo\n//AOJGRL3C(except gr)\ntemplate<class t>\nstruct scc{\n\tconst int n;\n\tconst vvc<t>&g;\n\tvi ord,low,bl,st;\n\tint s,head;\n\tvvc<int> idx,da;\n\tvoid dfs(int v){\n\t\tord[v]=low[v]=head++;\n\t\tst.pb(v);\n\t\tfor(auto to:g[v]){\n\t\t\tif(ord[to]==-1){\n\t\t\t\tdfs(to);\n\t\t\t\tchmin(low[v],low[to]);\n\t\t\t}else if(bl[to]==-1)\n\t\t\t\tchmin(low[v],ord[to]);\n\t\t}\n\t\tif(ord[v]==low[v]){\n\t\t\tint c=idx.size();\n\t\t\tidx.eb();\n\t\t\twhile(1){\n\t\t\t\tint a=st.back();\n\t\t\t\tst.pop_back();\n\t\t\t\tbl[a]=c;\n\t\t\t\tidx.back().pb(a);\n\t\t\t\tif(v==a)break;\n\t\t\t}\n\t\t}\n\t}\n\tscc(const vvc<t>&gg,bool cgr=true):n(gg.size()),g(gg),\n\t\tord(n,-1),low(n,-1),bl(n,-1){\n\t\thead=0;\n\t\trep(i,n)if(ord[i]==-1)\n\t\t\tdfs(i);\n\t\ts=idx.size();\n\t\trep(i,n)\n\t\t\tbl[i]=s-1-bl[i];\n\t\treverse(all(idx));\n\t\t//construct the graph\n\t\tif(cgr){\n\t\t\tvc<bool> u(s);\n\t\t\tda.resize(s);\n\t\t\trep(i,s){\n\t\t\t\tfor(auto v:idx[i]){\n\t\t\t\t\tfor(auto to:g[v])if(bl[v]<bl[to]){\n\t\t\t\t\t\tif(!u[bl[to]])\n\t\t\t\t\t\t\tda[bl[v]].pb(bl[to]);\n\t\t\t\t\t\tu[bl[to]]=1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor(auto v:idx[i])\n\t\t\t\t\tfor(auto to:g[v])\n\t\t\t\t\t\tu[bl[to]]=0;\n\t\t\t}\n\t\t}\n\t}\n};\n\n//ICPC 2021 Domestic F (AOJ1653)\n//各連結成分が強連結ならばよい\n//複数個の成分に分かれていてもかまわない\n//if(i==j)continue を消すことで解決可能かもしれない\nvc<pi> make_scc_dag(const vvc<int>&g){\n\tint n=si(g);\n\tvc<bool> vis(n);\n\tvc<bool> st(n,true);\n\trep(i,n)for(auto j:g[i])st[j]=false;\n\tauto dfs=[&](auto self,int v)->int{\n\t\tif(vis[v])return -1;\n\t\tvis[v]=true;\n\t\tif(g[v].empty())return v;\n\t\tfor(auto to:g[v]){\n\t\t\tint z=self(self,to);\n\t\t\tif(z!=-1){\n\t\t\t\treturn z;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t};\n\tvc<pi> ls;\n\trep(i,n)if(st[i]){\n\t\tint j=dfs(dfs,i);\n\t\tif(j!=-1){\n\t\t\tls.eb(i,j);\n\t\t}\n\t}\n\tvc<bool> used(n);\n\tfor(auto [i,j]:ls){\n\t\tused[i]=true;\n\t\tused[j]=true;\n\t}\n\t{\n\t\tvc<pi> tmp;\n\t\tfor(auto [i,j]:ls)if(i!=j)\n\t\t\ttmp.eb(i,j);\n\t\tls.swap(tmp);\n\t}\n\tvc<pi> ans;\n\trep(i,si(ls)){\n\t\tans.eb(ls[i].b,ls[(i+1)%si(ls)].a);\n\t}\n\tvi x,y;\n\trep(i,n)if(!used[i]){\n\t\tif(st[i])x.pb(i);\n\t\tif(g[i].empty())y.pb(i);\n\t}\n\tif(si(x)<si(y))x.resize(si(y),ls[0].a);\n\tif(si(y)<si(x))y.resize(si(x),ls[0].b);\n\trep(i,si(x))ans.eb(y[i],x[i]);\n\treturn ans;\n}\n\n//ICPC 2021 Domestic F (AOJ1653)\n//各連結成分が強連結ならばよい\n//複数個の成分に分かれていてもかまわない\nvc<pi> make_scc(const vvc<int>&g){\n\tscc<int> s(g);\n\tauto z=make_scc_dag(s.da);\n\tvc<pi> res;\n\tfor(auto [a,b]:z)\n\t\tres.eb(s.idx[a][0],s.idx[b][0]);\n\treturn res;\n}\n\nvoid slv(){\n\tint n,m;cin>>n>>m;\n\tif(n==0)exit(0);\n\tvc<pi> es(m);\n\tbipartitematching<int> bm(n,n);\n\trep(i,m){\n\t\tint a,b;cin>>a>>b;\n\t\ta--;b--;\n\t\tes[i]=pi(a,b);\n\t\tbm.ae(a,b);\n\t}\n\tbm.calc();\n\tvvc<int> g(n);\n\tfor(auto [a,b]:es){\n\t\tb=bm.ot[b];\n\t\tif(a!=b)g[a].pb(b);\n\t}\n\tauto ans=make_scc(g);\n\tprint(si(ans));\n\tfor(auto [a,b]:ans){\n\t\tb=bm.to[b];\n\t\tcout<<a+1<<\" \"<<b+1<<endl;\n\t}\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\t\n\t//int t;cin>>t;rep(_,t)\n\twhile(1)slv();\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 8984, "score_of_the_acc": -0.3973, "final_rank": 3 } ]
aoj_1663_cpp
Problem H Artist in Agony You, an artist, prepared a sufficient number of balls and also strings to link them for your artwork. Balls are to be given sequential numbers eventually, but only one of the balls is given number 1 at the beginning. No other balls are numbered and no balls are linked to any other balls yet. An artwork is created by carrying out either of the following actions repetitively. Replication : The work in progress is replicated. Let n be the number of balls numbered so far. You pick n unnumbered balls and number them n +1, ..., 2 n . This results in 2 n balls uniquely numbered 1 through 2 n. Then for all pairs of balls linked directly before replication numbered u and v, the two balls newly numbered n + u and n + v are linked with a string. Making a new link : Two distinct balls already numbered are chosen and linked with a string. When two balls are already linked directly, this action does nothing. Note that, as initially only one ball is numbered, you have to start with the replication action. Figure H-1 shows the process of artwork creation of the second dataset in Sample Input. Figure H-1: Artwork creation example Being a perfectionist as with all the real artists, you cannot stand imperfect works. When you find a work unsatisfactory, you have to destroy it completely as quick as possible by tearing off all the links in it. You grab some of the numbered balls in one hand, the rest in the other hand, and pull them apart tearing off all the strings linking the balls in your right hand and the balls in your left hand. If all the strings linking the balls are torn off by doing this operation just once, your destruction is successful. Conversely, any remaining links between two balls in one hand mean that the destruction is a failure. Figure H-2 shows the work of the second dataset in Sample Input being destroyed. To destroy the work, you have to grab three or more balls in each of your hands. Figure H-2: Destructing the artwork described above Given the sequence of actions that created the work, judge whether you can destroy it in the way described above. If you can, find the minimum number of balls you have to grab in your left hand. You may grab an arbitrary number of balls in your right hand. Input The input consists of multiple datasets, each in the following format. m a 1 ⋮ a m m is the number of actions you have carried out. m is a positive integer and does not exceed 300. The i -th action a i ( i = 1, ..., m ) is given by either of the following forms. COPY This represents the Replication action in the problem statement. It is guaranteed that each dataset contains no more than 60 COPY actions. LINK u v This represents the Making a new link action in the problem statement. In this action, the two balls numbered u and v were inspected, and a new link between them was made if they had not been directly connected yet. u and v are positive integers that satisfy u < v . It is guaranteed that two balls had already be ...(truncated)
[ { "submission_id": "aoj_1663_10569153", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T>& a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n\nstruct UnionFind {\n vector<int> par;\n vector<ll> siz;\n vector<ll> A, B;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n A.resize(x);\n B.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n A[i] = (i < x / 2);\n B[i] = (i >= x / 2);\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n A[x] += A[y];\n B[x] += B[y];\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\nvoid solve() {\n int m;\n cin >> m;\n if (m == 0) exit(0);\n vector<string> str(m);\n vector<ll> u(m), v(m);\n map<ll, int> mp;\n mp[0] = 0;\n rep(i, m) {\n cin >> str[i];\n if (str[i] == \"LINK\") {\n cin >> u[i] >> v[i];\n u[i]--, v[i]--;\n for (ll y : {u[i], v[i]}) {\n while (y) {\n mp[y] = 0;\n int d = 63 - __builtin_clzll(y);\n y &= ~(1LL << d);\n }\n }\n }\n }\n int CC = count(all(str), \"COPY\");\n int T = 0;\n vector<vector<ll>> xs(62);\n vector<ll> vec;\n for (auto [y, idx] : mp) {\n vec.push_back(y);\n mp[y] = T++;\n if(y==0)continue;\n int d = 63 - __builtin_clzll(y);\n xs[d].push_back(y);\n }\n UnionFind uf(T * 2);\n int d = 0;\n ll ans = 0;\n rep(i, m) {\n if (str[i] == \"COPY\") {\n ans *= 2;\n map<int, vector<int>> gr;\n for (ll y : xs[d]) {\n ll z = y & ~(1LL << d);\n gr[uf.find(mp[z])].push_back(mp[y]);\n gr[uf.find(mp[z] + T)].push_back(mp[y] + T);\n }\n // 元のrootごとに新しい頂点を記録\n for (auto [root, vs] : gr) {\n uf.par[vs[0]] = vs[0];\n uf.siz[vs[0]] = uf.siz[root];\n uf.A[vs[0]] = uf.A[root];\n uf.B[vs[0]] = uf.B[root];\n rep(j, (int)vs.size() - 1) uf.par[vs[j + 1]] = vs[0];\n }\n d++;\n } else {\n if (uf.same(mp[u[i]], mp[v[i]])) {\n cout << -1 << endl;\n return;\n }\n if (uf.same(mp[u[i]], mp[v[i]] + T)) continue;\n ans -= min(uf.A[uf.find(mp[u[i]])], uf.B[uf.find(mp[u[i]])]);\n ans -= min(uf.A[uf.find(mp[v[i]] + T)], uf.B[uf.find(mp[v[i]] + T)]);\n uf.unite(mp[u[i]], mp[v[i]] + T);\n uf.unite(mp[u[i]] + T, mp[v[i]]);\n ans += min(uf.A[uf.find(mp[u[i]])], uf.B[uf.find(mp[u[i]])]);\n }\n }\n rep(i, T) {\n if (uf.same(i, i + T)) {\n cout << -1 << endl;\n return;\n }\n }\n cout << ans << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 4244, "score_of_the_acc": -0.9674, "final_rank": 2 }, { "submission_id": "aoj_1663_10094430", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(A.begin(),A.end(),x)-A.begin())\nll solve(vector<vector<pi>>A){\n auto B=A;\n ll M=A.size();\n vector<vector<ll>>vs(M);\n vector<vector<pi>>nec(M);\n auto leader=[&](ll v,ll j){\n ll id=LB(vs[j],v%(1LL<<(j+2)));\n assert(vs[j][id]==v%(1LL<<(j+2)));\n while(1){\n if(nec[j][id].first+nec[j][id].second<0)return ((v>>(j+2))<<(j+2))+vs[j][id];\n id=nec[j][id].first;\n }\n };\n REP(i,M){\n FOR(j,i,M)for(auto[u,v]:A[j]){\n vs[i].emplace_back(u%(1LL<<(i+2)));\n vs[i].emplace_back(v%(1LL<<(i+2)));\n }\n sort(ALL(vs[i]));\n vs[i].erase(unique(ALL(vs[i])),vs[i].end());\n for(auto v:vs[i]){\n if(i&&binary_search(ALL(vs[i-1]),v%(1LL<<(i+1)))){\n ll t=LB(vs[i-1],v%(1LL<<(i+1)));\n assert(nec[i-1][t].first+nec[i-1][t].second<0);\n nec[i].emplace_back(nec[i-1][t]);\n }\n else nec[i].emplace_back(v%2?pi(0,-1):pi(-1,0));\n }\n vector<ll>real(vs[i].size(),-1);\n for(auto[u,v]:A[i]){\n ll idu=LB(vs[i],u),idv=LB(vs[i],v);\n while(1){\n if(real[idu]<0)break;\n idu=real[idu];\n }\n while(1){\n if(real[idv]<0)break;\n idv=real[idv];\n }\n if(idu==idv)continue;\n if(real[idu]<real[idv])swap(idu,idv);\n nec[i][idu].first+=nec[i][idv].first;\n nec[i][idu].second+=nec[i][idv].second;\n nec[i][idv].first=idu;\n nec[i][idv].second=0;\n real[idu]+=real[idv];\n real[idv]=idu;\n }\n FOR(j,i+1,M)for(auto&[u,v]:A[j]){\n u=leader(u,i);\n v=leader(v,i);\n }\n }\n ll ans=0;\n REP(i,M)REP(j,vs[i].size())if(nec[i][j].first+nec[i][j].second<0){\n ll a=min(-nec[i][j].first,-nec[i][j].second);\n if(i==M-1)ans+=a;\n else{\n if(!binary_search(ALL(vs[i+1]),vs[i][j]+(1LL<<(i+2))))ans+=a<<(M-2-i);\n if(!binary_search(ALL(vs[i+1]),vs[i][j]))ans+=a<<(M-2-i);\n }\n }\n REP(i,M)for(auto[u,v]:B[i]){\n u=u/2*2,v=u+1;\n REP(j,i+1)u=leader(u,j),v=leader(v,j);\n if(u==v)return -1;\n }\n assert(ans%2==0);\n return ans/2;\n}\nint main(){\n while(1){\n ll Q;cin>>Q;\n if(!Q)return 0;\n vector<vector<pi>>A;\n while(Q--){\n string S;cin>>S;\n if(S[0]=='C')A.emplace_back(vector<pi>{});\n else{\n ll u,v;cin>>u>>v;u--;v--;\n A.back().emplace_back(pi(2*u,2*v+1));\n A.back().emplace_back(pi(2*v,2*u+1));\n }\n }\n cout<<solve(A)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 4380, "score_of_the_acc": -2, "final_rank": 3 }, { "submission_id": "aoj_1663_9856689", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct UnionFind{\n vector<int> par; int n;\n vector<ll> ans;\n UnionFind(){}\n UnionFind(int _n):par(_n,-1),n(_n){\n ans.resize(_n,0);\n rep(i,_n/2,_n) ans[i] = 1;\n }\n int root(int x){\n if (par[x] < 0) return x;\n else {\n int r = root(par[x]);\n par[x]=r;\n return r;\n }\n }\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n ll answer(int x){\n return ans[root(x)];\n }\n void change(int x, ll a) {\n x = root(x);\n ans[x] = a;\n }\n bool unite(int x,int y){\n x=root(x),y=root(y); if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n ans[y]+=ans[x], par[y]+=par[x]; par[x]=y;\n return true;\n }\n};\n\n/**\n * @brief Union Find\n */\n\nstruct Query {\n ll Type, A, B;\n};\n\nll Solve(int N) {\n vector<Query> Q(N);\n ll X = 1;\n rep(i,0,N) {\n string S;\n cin >> S;\n if (S == \"COPY\") Q[i].Type = 0, X *= 2;\n else Q[i].Type = 1, cin >> Q[i].A >> Q[i].B, Q[i].A--, Q[i].B--;\n }\n vector<ll> AS;\n rep(i,0,N) {\n if (Q[i].Type == 0) continue;\n ll Cur = Q[i].A;\n while(1) {\n AS.push_back(Cur);\n if (Cur == 0) break;\n Cur -= (1LL<<topbit(Cur));\n }\n Cur = Q[i].B;\n while(1) {\n AS.push_back(Cur);\n if (Cur == 0) break;\n Cur -= (1LL<<topbit(Cur));\n }\n }\n UNIQUE(AS);\n int M = AS.size();\n rep(i,0,M) AS.push_back(AS[i] + X);\n UnionFind UF(M*2);\n ll ANS = 0, CurSize = 1;\n rep(i,0,N) {\n if (Q[i].Type == 0) {\n ANS *= 2;\n map<int, vector<int>> mp;\n rep(j,0,M) {\n if (CurSize <= AS[j] & AS[j] < CurSize * 2) {\n int ID = LB(AS, AS[j] - CurSize);\n int Root = UF.root(ID);\n if (mp.count(Root)) mp[Root].push_back(j);\n else mp[Root] = {j};\n ID += M;\n Root = UF.root(ID);\n if (mp.count(Root)) mp[Root].push_back(j+M);\n else mp[Root] = {j+M};\n }\n }\n for (auto it = mp.begin(); it != mp.end(); it++) {\n auto V = it->second;\n if (V.size() == 1) continue;\n rep(j,0,V.size()-1) UF.unite(V[j], V[j+1]);\n }\n rep(j,0,M) {\n if (CurSize <= AS[j] && AS[j] < CurSize * 2) {\n int ID = LB(AS, AS[j] - CurSize);\n UF.change(j,UF.answer(ID));\n UF.change(j+M,UF.answer(ID+M));\n }\n }\n CurSize *= 2;\n }\n else {\n ll A = Q[i].A, B = Q[i].B;\n int AID = LB(AS,A), BID = LB(AS,B);\n if (UF.same(AID,BID)) {\n return -1;\n }\n if (UF.same(AID,BID+M)) continue;\n ANS -= min(UF.answer(AID), UF.answer(AID+M));\n ANS -= min(UF.answer(BID), UF.answer(BID+M));\n UF.unite(AID, BID+M);\n UF.unite(AID+M, BID);\n ANS += min(UF.answer(AID), UF.answer(AID+M));\n }\n }\n rep(i,0,M) {\n if (UF.same(i,i+M)) return -1;\n }\n return ANS;\n}\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n cout << Solve(N) << endl;\n}\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3836, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1662_cpp
Problem G Keep in Touch Two agents A and B, known as a great combination, are conducting the infiltration mission to a secret base. Due to the strict security of the base, two agents have decided to follow the prechecked safe infiltration routes R A and R B , respectively. Each infiltration route is a two-dimensional polyline, a series of line segments. Each of the agents starts at the starting end of the first segment of the corresponding route. The mission is over when both agents are on the finishing ends of the last segments of their routes simultaneously. During the mission, each of the agents can move forward or backward at an arbitrary speed, or stop for a moment, as long as the movements along the route are continuous, but cannot jump changing the position abruptly. Although segments of a route may have crosses or overlaps with one another, they cannot be used for movements. Precisely, an agent on one segment can transfer to another segment only in the following cases. The destination segment is the next segment on the route, and the agent is on the finishing end of the current segment. The destination segment is the previous segment on the route, and the agent is on the starting end of the current segment. For example, in Figure G-1 below, the agent must move to the finishing end of the segment 3-4 of R B , (−1, 1), in order to transfer to the segment 4-5. Although, the agent passes the same point as the finishing end of the last segment, (2, 1), when the agent transfers from the segment 2-3 to the segment 3-4, the agent is not considered to be on the finishing end of the last segment at that time. Figure G-1: Infiltration routes for the first dataset of Sample Input Figure G-2: Infiltration routes for the second dataset of Sample Input To prepare for the contingency, the agents carry radio communicators and they should always be within the range allowing communication between them. Stronger wave enables longer communication distance, but increases the interception possibilities. Give the minimum required communication distance to accomplish the mission with appropriate movements of the agents. Input The input consists of multiple datasets, each in the following format. n x A,1 y A,1 ⋮ x A, n y A, n m x B,1 y B,1 ⋮ x B, m y B, m n is the number of the vertices of the infiltration route R A . n is an integer between 2 and 40, inclusive. x A, i and y A, i (1 ≤ i ≤ n ) are the coordinates of the i -th vertex. x A, i and y A, i are integers satisfying −1000 ≤ x A, i ≤ 1000 and −1000 ≤ y A, i ≤ 1000, respectively. For 1 ≤ i ≤ n −1, ( x A, i , y A, i ) is the starting end of the i- th line segment, and ( x A, i +1 , y A, i +1 ) is its finishing end. All line segments have non-zero lengths. That is, x A, i ≠ x A, i +1 and/or y A, i ≠ y A, i +1 holds. m and the value pairs of x B, j and y B, j (1 ≤ j ≤ m ) represent the infiltration route R B . The format and the constraints are the same as those for R A . The end of the input is in ...(truncated)
[ { "submission_id": "aoj_1662_10203399", "code_snippet": "#include <iostream>\n#include <vector>\n#include <math.h>\n#include <algorithm>\n#include <set>\n#include <map>\n\nusing namespace std;\nusing ld = long double;\n\nld eps = 1e-10;\nld INF = 1e9;\n\nstruct point {\n\tld x, y;\n\tpoint() {\n\t\tx = 0;\n\t\ty = 0;\n\t}\n\n\tpoint(ld x1, ld y1) {\n\t\tx = x1, y = y1;\n\t}\n};\n\n\nbool cmp1(point A, point B) {\n\treturn A.x > B.x;\n}\n\nbool cmp2(point A, point B) {\n\treturn A.y > B.y;\n}\n\nstruct line {\n\tld A, B, C;\n\n\tline() {\n\t\tA = 0;\n\t\tB = 0;\n\t\tC = 0;\n\t}\n\n\tline(ld A1, ld B1, ld C1) {\n\t\tA = A1;\n\t\tB = B1;\n\t\tC = C1;\n\t}\n\n\tline(point X, point Y) {\n\t\tA = X.y - Y.y;\n\t\tB = -X.x + Y.x;\n\t\tC = -(X.x * A + X.y * B);\n\t}\n};\n\n\nline perp(point X, line l) {\n\tld A = -l.B;\n\tld B = l.A;\n\tld C = -(X.x * A + X.y * B);\n\treturn line(A, B, C);\n}\n\n\npoint cross(line l, line k) {\n\tld D = l.A * k.B - l.B * k.A;\n\tld x = -l.C * k.B + k.C * l.B;\n\tld y = -l.A * k.C + k.A * l.C;\n\treturn point(x / D, y / D);\n}\n\n\npoint perp1(point X, line l) {\n\tline k = perp(X, l);\n\tpoint Y = cross(k, l);\n\treturn Y;\n}\n\nbool check(point A, point B, point C) {\n\tline l = line(A, B);\n\tif (abs(l.A * C.x + l.B * C.y + l.C) > eps)\n\t\treturn 0;\n\n\tif ((A.x - C.x) * (C.x - B.x) < -eps)\n\t\treturn 0;\n\n\tif ((A.y - C.y) * (C.y - B.y) < -eps)\n\t\treturn 0;\n\n\treturn 1;\n}\n\n\nld dist(point A, point B) {\n\tld q = A.x - B.x;\n\tld w = A.y - B.y;\n\treturn sqrt(q * q + w * w);\n}\n\n\npair<point, int> kek(point A, point B, point C) {\n\tpoint D = perp1(C, line(A, B));\n\tif (check(A, B, D))\n\t\treturn { D, 0 };\n\t\n\tif (dist(A, C) < dist(B, C))\n\t\treturn { A, -1 };\n\treturn { B, -2 };\n}\n\n\nvoid solve() {\n\tint n;\n\tcin >> n;\n\tif (!n)\n\t\texit(0);\n\tvector<point> p1(n);\n\n\tfor (int i = 0; i < n; i++) {\n\t\tld a, b;\n\t\tcin >> a >> b;\n\t\tp1[i] = point(a, b);\n\t}\n\n\tint m;\n\tcin >> m;\n\tvector<point> p2(m);\n\tfor (int i = 0; i < m; i++) {\n\t\tld a, b;\n\t\tcin >> a >> b;\n\t\tp2[i] = point(a, b);\n\t}\n\n\tmap<vector<int>, ld> dis;\n\tvector<int> st = { 0, -1, 0, -1 };\n\n\tdis[st] = 0;\n\n\tset<pair<ld, vector<int>>> s;\n\ts.insert({ 0, st });\n\n\twhile (!s.empty()) {\n\t\tvector<int> cur = s.begin()->second;\n\t\tld qwe = s.begin()->first;\n\t\ts.erase(*s.begin());\n\n\t\tvector<pair<vector<int>, ld>> nxt;\n\n\t\tint seg1 = cur[0];\n\t\tint seg2 = cur[2];\n\t\tint num1 = cur[1];\n\t\tint num2 = cur[3];\n\n\t\tvector<int> temp(4);\n\n\n\t\tif (num1 == -1 && seg1 > 0) {\n\t\t\ttemp = { seg1 - 1, -2, seg2, num2 };\n\t\t\tnxt.push_back({temp , 0 });\n\t\t}\n\n\t\tif (num1 == -2 && seg1 < n - 2) {\n\t\t\ttemp = { seg1 + 1, -1, seg2, num2 };\n\t\t\tnxt.push_back({ temp , 0 });\n\t\t}\n\n\t\tif (num2 == -1 && seg2 > 0) {\n\t\t\ttemp = {seg1, num1, seg2 - 1, - 2};\n\t\t\tnxt.push_back({ temp , 0 });\n\t\t}\n\n\t\tif (num2 == -2 && seg2 < m - 2) {\n\t\t\ttemp = {seg1, num1, seg2 + 1, -1};\n\t\t\tnxt.push_back({ temp , 0 });\n\t\t}\n\n\t\tpoint A, B;\n\t\tline l1 = line(p1[seg1], p1[seg1 + 1]);\n\t\tline l2 = line(p2[seg2], p2[seg2 + 1]);\n\n\t\tif (num2 >= 0)\n\t\t\tB = perp1(p1[num2], l2);\n\t\telse if (num2 == -1)\n\t\t\tB = p2[seg2];\n\t\telse\n\t\t\tB = p2[seg2 + 1];\n\n\t\tif (num1 >= 0)\n\t\t\tA = perp1(p2[num1], l1);\n\t\telse if (num1 == -1)\n\t\t\tA = p1[seg1];\n\t\telse\n\t\t\tA = p1[seg1 + 1];\n\n\t\tpoint C, D;\n\t\tld cost;\n\t\tpair<point, int> pp;\n\n\t\tC = p1[seg1];\n\t\tpp = kek(p2[seg2], p2[seg2 + 1], C);\n\t\tD = pp.first;\n\t\tcost = max(dist(A, B), dist(C, D));\n\n\t\tif (pp.second == 0)\n\t\t\tnxt.push_back({ {seg1, -1, seg2, seg1},cost });\n\t\telse\n\t\t\tnxt.push_back({ {seg1, -1, seg2, pp.second},cost });\n\n\t\tC = p1[seg1 + 1];\n\t\tpp = kek(p2[seg2], p2[seg2 + 1], C);\n\t\tD = pp.first;\n\t\tcost = max(dist(A, B), dist(C, D));\n\n\t\tif (pp.second == 0)\n\t\t\tnxt.push_back({ {seg1, -2, seg2, seg1 + 1},cost });\n\t\telse\n\t\t\tnxt.push_back({ {seg1, -2, seg2, pp.second},cost });\n\n\n\t\tC = p2[seg2];\n\t\tpp = kek(p1[seg1], p1[seg1 + 1], C);\n\t\tD = pp.first;\n\t\tcost = max(dist(A, B), dist(C, D));\n\n\t\tif (pp.second == 0)\n\t\t\tnxt.push_back({ {seg1, seg2, seg2, -1},cost });\n\t\telse\n\t\t\tnxt.push_back({ {seg1, pp.second, seg2, -1},cost });\n\n\t\tC = p2[seg2 + 1];\n\t\tpp = kek(p1[seg1], p1[seg1 + 1], C);\n\t\tD = pp.first;\n\t\tcost = max(dist(A, B), dist(C, D));\n\n\t\tif (pp.second == 0)\n\t\t\tnxt.push_back({ {seg1, seg2 + 1, seg2, -2},cost });\n\t\telse\n\t\t\tnxt.push_back({ {seg1, pp.second, seg2, -2},cost });\n\n\t\tif (seg1 == n - 2 && seg2 == m - 2) {\n\t\t\tC = p1[n - 1];\n\t\t\tD = p2[m - 1];\n\t\t\tcost = max(dist(A, B), dist(C, D));\n\t\t\tnxt.push_back({ { seg1, -2, seg2, -2 }, cost });\n\t\t}\n\n\n\t\tfor (auto p : nxt) {\n\t\t\tvector<int> v = p.first;\n\t\t\tif (!dis.count(v)) {\n\t\t\t\tdis[v] = INF;\n\t\t\t}\n\t\t\tif (dis[v] > max(dis[cur], p.second)) {\n\t\t\t\ts.erase({ dis[v], v });\n\t\t\t\tdis[v] = max(dis[cur], p.second);\n\t\t\t\ts.insert({ dis[v], v });\n\t\t\t}\n\t\t}\n\t}\n\n\tcout.precision(12);\n\tcout << fixed;\n\tcout << dis[{n - 2, -2, m - 2, -2}] << '\\n';\n\n}\n\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\twhile (true) {\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 4536, "score_of_the_acc": -0.3627, "final_rank": 3 }, { "submission_id": "aoj_1662_10100385", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<ll,ll> pi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define F first\n#define S second\nld dist(ll x,ll y){return sqrtl(x*x+y*y);}\nld f(pi a,pi b,pi c){\n ll x=a.F-b.F,y=a.S-b.S;\n ll p=a.F*x+a.S*y;\n ll q=b.F*x+b.S*y;\n ll r=c.F*x+c.S*y;\n if(p>q){\n swap(a,b);\n swap(p,q);\n }\n if(r<=p)return dist(a.F-c.F,a.S-c.S);\n if(r>=q)return dist(b.F-c.F,b.S-c.S);\n return abs(y*(c.F-a.F)-x*(c.S-a.S))/dist(x,y);\n}\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\nint main(){\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n vector<pi>P(N);\n REP(i,N)cin>>P[i].F>>P[i].S;\n ll M;cin>>M;\n vector<pi>Q(M);\n REP(i,M)cin>>Q[i].F>>Q[i].S;\n vector<vector<ld>>A(2*N-1,vector<ld>(2*M-1,1e18));\n REP(i,N)REP(j,M){\n A[2*i][2*j]=dist(P[i].F-Q[j].F,P[i].S-Q[j].S);\n }\n REP(i,N-1)REP(j,M){\n A[2*i+1][2*j]=f(P[i],P[i+1],Q[j]);\n }\n REP(i,N)REP(j,M-1){\n A[2*i][2*j+1]=f(Q[j],Q[j+1],P[i]);\n }\n vector<vector<ld>>D(2*N-1,vector<ld>(2*M-1,1e18));\n D[0][0]=A[0][0];\n min_priority_queue<pair<ld,pi>>que;\n que.emplace(make_pair(A[0][0],pi(0,0)));\n auto chmin=[](ld&a,ld b)->bool{\n if(a>b){a=b;return 1;}return 0;\n };\n while(que.size()){\n auto[d,p]=que.top();que.pop();\n if(D[p.F][p.S]!=d)continue;\n ll dx=(p.F%2?1:2);\n ll dy=(p.S%2?1:2);\n FOR(x,-dx,dx+1)if(0<=p.F+x&&p.F+x<2*N-1)FOR(y,-dy,dy+1)if(0<=p.S+y&&p.S+y<2*M-1){\n if(chmin(D[p.F+x][p.S+y],max(d,A[p.F+x][p.S+y]))){\n que.emplace(make_pair(D[p.F+x][p.S+y],pi(p.F+x,p.S+y)));\n }\n }\n }\n //REP(i,2*N-1){REP(j,2*M-1)cout<<D[i][j]<<\" \";cout<<endl;}\n printf(\"%.20Lf\\n\",D.back().back());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4064, "score_of_the_acc": -0.0124, "final_rank": 1 }, { "submission_id": "aoj_1662_9338003", "code_snippet": "#ifdef LOGX\n#define _GLIBCXX_DEBUG\n#endif\n#include <bits/stdc++.h>\nusing namespace std;\n//#include <atcoder/all>\n//using namespace atcoder;\n\n/*---------macro---------*/\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)\n#define unless(x) if(!(x))\n#define until(x) while(!(x))\n#define ALL(a) a.begin(),a.end()\n#define RALL(a) a.rbegin(),a.rend()\n#define mybit(i,j) (((i)>>(j))&1)\n\n/*---------type/const---------*/\nconstexpr int big=1000000007;\n//constexpr int big=998244353;\nconstexpr double EPS=1e-8; //適宜変える\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef std::string::const_iterator state; //構文解析\nconstexpr int dx[4]={1,0,-1,0};\nconstexpr int dy[4]={0,1,0,-1};\nconstexpr char newl='\\n';\nstruct{\n constexpr operator int(){return -int(1e9)-10;}\n constexpr operator ll(){return -ll(1e18)-10;}\n}neginf;\nstruct{\n constexpr operator int(){return int(1e9)+10;}\n constexpr operator ll(){return ll(1e18)+10;}\n constexpr auto operator -(){return neginf;}\n}inf;\n\n/*---------debug---------*/\n#ifdef LOGX\n#include <template/debug.hpp>\n#else\n#define dbg(...) ;\n#define dbgnewl ;\n#define prt(x) ;\n#define _prt(x) ;\n#endif\n\n/*---------function---------*/\ntemplate<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}\ntemplate<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}\ntemplate<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}\nbool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}\ntemplate<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}\ntemplate<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}\n\nusing ld = long double;\n\nstruct point{\n ld x,y;\n point(ld x=0, ld y=0):x(x),y(y){}\n\n ld len()const{\n return sqrtl(x*x + y*y);\n }\n\n point operator-()const{\n return point(-x, -y);\n }\n point& operator-=(point r){\n x-=r.x;\n y-=r.y;\n return *this;\n }\n point operator-(point r)const{\n auto a = *this;\n return a-=r;\n }\n friend ld cdot(const point l, const point r){\n return l.x*r.x + l.y*r.y;\n }\n};\n\n//st と p の距離\nld dist_pt_seg(const point s,const point t,const point p){\n if(cdot(p-s,t-s)>=0 && cdot(p-t,s-t)>=0){\n //直線stとpの距離\n //bx-ay+c=0\n ld a=(t-s).x, b=(t-s).y;\n ld c = a * (s.y) - b * (s.x);\n return abs(b*p.x - a*p.y + c) / sqrtl(a*a + b*b);\n }\n else{\n return min((p-s).len(), (p-t).len());\n }\n}\n\nint main(){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(15);\n/*------------------------------------*/\n \n int N[2];\n auto &n=N[0], &m=N[1];\n vector<point> A[2];\n auto &a=A[0], &b=A[1];\nwhile(cin >> n, n){\n a.resize(n);\n rep(i,n)cin >> a[i].x >> a[i].y;\n cin >> m;\n b.resize(m);\n rep(i,m)cin >> b[i].x >> b[i].y;\n\n auto canreachgoal = [&](ld r)->bool{\n //dp0[i][j] = A[i] にいながら B[j]B[j+1] にいられる\n //dp1[i][j] = B[i] にいながら A[j]A[j+1] にいられる\n vector<vector<bool>> DP[2];\n auto &dp0=DP[0], &dp1=DP[1];\n \n dp0 = vector(n, vector<bool>(m-1, false));\n dp1 = vector(m, vector<bool>(n-1, false));\n\n //始点間距離は大丈夫。\n dp0[0][0] = dp1[0][0] = true;\n using P = array<int,3>;\n queue<P> q;\n q.push({0,0,0});\n q.push({1,0,0});\n while(!q.empty()){\n auto [k,i,j] = q.front();q.pop();\n //dp[k][i][j]からの遷移は、\n //・いま辺上にいるのを A[!k][j] or A[!k][j+1] に移動、頂点にいるのは A[k][i-1,i] or A[k][i,i+1] へ。\n //・いま辺上にいるのは同じ辺にいるままで、頂点を A[k][i-1] or A[k][i+1] へ。\n if(i>0 && !DP[!k][j][i-1]){\n DP[!k][j][i-1] = (dist_pt_seg(A[k][i-1], A[k][i], A[!k][j])<=r);\n if(DP[!k][j][i-1])q.push({!k, j, i-1});\n }\n if(i>0 && !DP[!k][j+1][i-1]){\n DP[!k][j+1][i-1] = (dist_pt_seg(A[k][i-1], A[k][i], A[!k][j+1])<=r);\n if(DP[!k][j+1][i-1])q.push({!k, j+1, i-1});\n }\n if(i+1<N[k] && !DP[!k][j][i]){\n DP[!k][j][i] = (dist_pt_seg(A[k][i], A[k][i+1], A[!k][j])<=r);\n if(DP[!k][j][i])q.push({!k, j, i});\n }\n if(i+1<N[k] && !DP[!k][j+1][i]){\n DP[!k][j+1][i] = (dist_pt_seg(A[k][i], A[k][i+1], A[!k][j+1])<=r);\n if(DP[!k][j+1][i])q.push({!k, j+1, i});\n }\n if(i>0 && !DP[k][i-1][j]){\n DP[k][i-1][j] = (dist_pt_seg(A[!k][j], A[!k][j+1], A[k][i-1])<=r);\n if(DP[k][i-1][j])q.push({k,i-1,j});\n }\n if(i+1<N[k] && !DP[k][i+1][j]){\n DP[k][i+1][j] = (dist_pt_seg(A[!k][j], A[!k][j+1], A[k][i+1])<=r);\n if(DP[k][i+1][j])q.push({k,i+1,j});\n }\n }\n //終点間距離もokなので、n-1にいる時m-2→m-1の辺にいられさえすればok\n return dp0[n-1][m-2];\n };\n ld ng = max((a[0]-b[0]).len(), (a[n-1]-b[m-1]).len());\n ld ok = 5000;\n int _ = 100;\n\n while(_--){\n ld md = (ok+ng)/2;\n (canreachgoal(md) ? ok:ng) = md;\n }\n cout << ok << endl;\n}\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3520, "score_of_the_acc": -0.1743, "final_rank": 2 }, { "submission_id": "aoj_1662_9107958", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\n// template < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > long double abs(const point< T >& p) { return sqrtl(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T > p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nf64 solve(int n) {\n vector<point<f64>> A = in(n);\n int m = in();\n vector<point<f64>> B = in(m);\n\n const f64 INF = 1e30;\n vector dist(n, vector(n, vector(m, vector(m, INF))));\n heap_min<pair<f64, tuple<int,int,int,int>>> q;\n auto cost = [&](int si, int ti, int sj, int tj) -> f64 {\n if(si == ti and sj == tj) return abs(A[si] - B[sj]);\n if(si != ti and sj == tj) return distance_sp(segment<f64>(A[si], A[ti]), B[sj]);\n if(si == ti and sj != tj) return distance_sp(segment<f64>(B[sj], B[tj]), A[si]);\n return distance_ss(segment<f64>(A[si], A[ti]), segment<f64>(B[sj], B[tj]));\n };\n q.push({dist[0][0][0][0] = cost(0, 0, 0, 0), {0, 0, 0, 0}});\n const vector<tuple<int,int,int,int>> dir = {\n {+1, 0, 0, 0}, {-1, 0, 0, 0},\n {0, +1, 0, 0}, {0, -1, 0, 0},\n {0, 0, +1, 0}, {0, 0, -1, 0},\n {0, 0, 0, +1}, {0, 0, 0, -1}\n };\n while(not q.empty()) {\n auto [d, v] = q.top(); q.pop();\n auto [si, ti, sj, tj] = v;\n if(v == make_tuple(n - 1, n - 1, m - 1, m - 1)) return d;\n if(dist[si][ti][sj][tj] < d) continue;\n for(auto [dsi, dti, dsj, dtj] : dir) {\n int nsi = si + dsi, nti = ti + dti, nsj = sj + dsj, ntj = tj + dtj;\n if(0 <= nsi and (nsi == nti or nsi + 1 == nti) and nti < n and 0 <= nsj and (nsj == ntj or nsj + 1 == ntj) and ntj < m) {\n if(chmin(dist[nsi][nti][nsj][ntj], max(d, cost(nsi, nti, nsj, ntj)))) {\n q.push({dist[nsi][nti][nsj][ntj], {nsi, nti, nsj, ntj}});\n }\n }\n }\n }\n assert(0);\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n printer::precision(20);\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 47128, "score_of_the_acc": -1.3372, "final_rank": 6 }, { "submission_id": "aoj_1662_9107956", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\n// template < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > long double abs(const point< T >& p) { return sqrtl(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T > p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nf64 solve(int n) {\n vector<point<f64>> A = in(n);\n int m = in();\n vector<point<f64>> B = in(m);\n\n const f64 INF = 1e30;\n vector dist(n, vector(n, vector(m, vector(m, INF))));\n heap_min<pair<f64, tuple<int,int,int,int>>> q;\n auto cost = [&](int si, int ti, int sj, int tj) -> f64 {\n if(si == ti and sj == tj) return abs(A[si] - B[sj]);\n if(si != ti and sj == tj) return distance_sp(segment<f64>(A[si], A[ti]), B[sj]);\n if(si == ti and sj != tj) return distance_sp(segment<f64>(B[sj], B[tj]), A[si]);\n return distance_ss(segment<f64>(A[si], A[ti]), segment<f64>(B[sj], B[tj]));\n };\n q.push({dist[0][0][0][0] = cost(0, 0, 0, 0), {0, 0, 0, 0}});\n const vector<tuple<int,int,int,int>> dir = {\n {+1, 0, 0, 0}, {-1, 0, 0, 0},\n {0, +1, 0, 0}, {0, -1, 0, 0},\n {0, 0, +1, 0}, {0, 0, -1, 0},\n {0, 0, 0, +1}, {0, 0, 0, -1}\n };\n while(not q.empty()) {\n auto [d, v] = q.top(); q.pop();\n auto [si, ti, sj, tj] = v;\n if(v == make_tuple(n - 1, n - 1, m - 1, m - 1)) return d;\n if(dist[si][ti][sj][tj] < d) continue;\n for(auto [dsi, dti, dsj, dtj] : dir) {\n int nsi = si + dsi, nti = ti + dti, nsj = sj + dsj, ntj = tj + dtj;\n if(0 <= nsi and (nsi == nti or nsi + 1 == nti) and nti < n and 0 <= nsj and (nsj == ntj or nsj + 1 == ntj) and ntj < m) {\n if(chmin(dist[nsi][nti][nsj][ntj], max(d, cost(nsi, nti, nsj, ntj)))) {\n q.push({dist[nsi][nti][nsj][ntj], {nsi, nti, nsj, ntj}});\n }\n }\n }\n }\n return dist[n - 1][n - 1][m - 1][m - 1];\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n printer::precision(20);\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 47124, "score_of_the_acc": -1.3371, "final_rank": 5 }, { "submission_id": "aoj_1662_9107955", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\n// template < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > long double abs(const point< T >& p) { return sqrtl(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T > p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nf64 solve(int n) {\n vector<point<f64>> A = in(n);\n int m = in();\n vector<point<f64>> B = in(m);\n\n const f64 INF = 1e30;\n vector dist(n, vector(n, vector(m, vector(m, INF))));\n heap_min<pair<f64, tuple<int,int,int,int>>> q;\n auto cost = [&](int si, int ti, int sj, int tj) -> f64 {\n if(si == ti and sj == tj) return abs(A[si] - B[sj]);\n if(si != ti and sj == tj) return distance_sp(segment<f64>(A[si], A[ti]), B[sj]);\n if(si == ti and sj != tj) return distance_sp(segment<f64>(B[sj], B[tj]), A[si]);\n return distance_ss(segment<f64>(A[si], A[ti]), segment<f64>(B[sj], B[tj]));\n };\n q.push({dist[0][0][0][0] = cost(0, 0, 0, 0), {0, 0, 0, 0}});\n const vector<tuple<int,int,int,int>> dir = {\n {+1, 0, 0, 0}, {-1, 0, 0, 0},\n {0, +1, 0, 0}, {0, -1, 0, 0},\n {0, 0, +1, 0}, {0, 0, -1, 0},\n {0, 0, 0, +1}, {0, 0, 0, -1}\n };\n while(not q.empty()) {\n auto [d, v] = q.top(); q.pop();\n auto [si, ti, sj, tj] = v;\n if(dist[si][ti][sj][tj] < d) continue;\n for(auto [dsi, dti, dsj, dtj] : dir) {\n int nsi = si + dsi, nti = ti + dti, nsj = sj + dsj, ntj = tj + dtj;\n if(0 <= nsi and (nsi == nti or nsi + 1 == nti) and nti < n and 0 <= nsj and (nsj == ntj or nsj + 1 == ntj) and ntj < m) {\n if(chmin(dist[nsi][nti][nsj][ntj], max(d, cost(nsi, nti, nsj, ntj)))) {\n q.push({dist[nsi][nti][nsj][ntj], {nsi, nti, nsj, ntj}});\n }\n }\n }\n }\n return dist[n - 1][n - 1][m - 1][m - 1];\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n printer::precision(20);\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 47228, "score_of_the_acc": -1.3853, "final_rank": 7 }, { "submission_id": "aoj_1662_7963953", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct point {\n long double x, y;\n};\n\nint sgn(long double x) {\n if (x < -1e-10) {\n return -1;\n } else if (x > 1e-10) {\n return 1;\n } else {\n return 0;\n }\n}\n\nlong double cross(point a, point b) {\n return a.x * b.y - a.y * b.x;\n}\n\nlong double dot(point a, point b) {\n return a.x * b.x + a.y * b.y;\n}\n\nlong double norm(point a) {\n return a.x * a.x + a.y * a.y;\n}\n\nint ccw(point a, point b, point c) {\n b.x -= a.x;\n b.y -= a.y;\n c.x -= a.x;\n c.y -= a.y;\n if (sgn(cross(b, c)) == +1) {\n return 1;\n } else if (sgn(cross(b, c)) == -1) {\n return -1;\n } else if (sgn(dot(b, c)) == -1) {\n return 2;\n } else if (sgn(norm(b) - norm(c)) == -1) {\n return -2;\n } else {\n return 0;\n }\n}\n\n// a, b\nlong double dist(point a, point b) {\n return sqrtl(powl(a.x - b.x, 2) + powl(a.y - b.y, 2));\n}\n\npoint proj(point a, point b, point c) {\n c.x -= b.x;\n c.y -= b.y;\n long double d = sqrtl(norm(c));\n c.x /= d;\n c.y /= d;\n a.x -= b.x;\n a.y -= b.y;\n b.x += c.x * dot(c, a);\n b.y += c.y * dot(c, a);\n return b;\n}\n\nbool intersect(point a, point b, point c) {\n return ccw(b, c, a) == 0;\n}\n\n// a, b-c\nlong double dist(point a, point b, point c) {\n long double res = min(dist(a, b), dist(a, c));\n point h = proj(a, b, c);\n if (intersect(h, b, c)) {\n res = dist(a, h);\n }\n return res;\n}\n\nbool intersect(point a, point b, point c, point d) {\n return ccw(a, d, c) * ccw(a, b, d) <= 0 && ccw(c, d, a) * ccw(c, d, b) <= 0;\n}\n\n// a-b, c-d\nlong double dist(point a, point b, point c, point d) {\n long double res = dist(a, c, d);\n res = min(res, dist(b, c, d));\n res = min(res, dist(c, a, b));\n res = min(res, dist(d, a, b));\n if (intersect(a, b, c, d)) {\n res = 0;\n }\n return res;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true) {\n int n;\n cin >> n;\n if (n == 0) {\n break;\n }\n vector<point> a(n);\n for (int i = 0; i < n; i++) {\n cin >> a[i].x >> a[i].y;\n }\n int m;\n cin >> m;\n vector<point> b(m);\n for (int i = 0; i < m; i++) {\n cin >> b[i].x >> b[i].y;\n }\n vector<vector<long double>> c(2 * n - 1, vector<long double>(2 * m - 1));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n c[2 * i][2 * j] = dist(a[i], b[j]);\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m - 1; j++) {\n c[2 * i][2 * j + 1] = dist(a[i], b[j], b[j + 1]);\n }\n }\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < m; j++) {\n c[2 * i + 1][2 * j] = dist(b[j], a[i], a[i + 1]);\n }\n }\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < m - 1; j++) {\n c[2 * i + 1][2 * j + 1] = dist(a[i], a[i + 1], b[j], b[j + 1]);\n }\n }\n vector<long double> d;\n for (int i = 0; i < 2 * n - 1; i++) {\n for (int j = 0; j < 2 * m - 1; j++) {\n d.emplace_back(c[i][j]);\n }\n }\n sort(d.begin(), d.end());\n vector<int> dx = {0, 1, 0, -1};\n vector<int> dy = {1, 0, -1, 0};\n for (auto t : d) {\n vector<vector<int>> e(2 * n - 1, vector<int>(2 * m - 1));\n function<void(int, int)> Dfs = [&](int i, int j) {\n if (c[i][j] > t + 1e-10) {\n return;\n }\n e[i][j] = 1;\n for (int dir = 0; dir < 4; dir++) {\n int ni = i + dx[dir];\n int nj = j + dy[dir];\n if (0 <= ni && ni < 2 * n - 1 && 0 <= nj && nj < 2 * m - 1 && c[ni][nj] <= t + 1e-12 && !e[ni][nj]) {\n Dfs(ni, nj);\n }\n }\n };\n Dfs(0, 0);\n if (e[2 * n - 2][2 * m - 2]) {\n cout << fixed << setprecision(12) << t << '\\n';\n break;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 4228, "score_of_the_acc": -1.0162, "final_rank": 4 } ]
aoj_1657_cpp
Problem B Leave No One Behind A card game similar to Old Maid is played by n players P 1 , ..., and P n , sitting clockwise in this order. For this game, n pairs of cards numbered 1 through n are used. Each player has two cards at hand to start with. First, any player who has a pair of cards with the same number discards them. If by chance all the players discard the cards in hand, the game ends. Otherwise, the following actions are repeated until the game ends. Note that, from now on, for a player p, the next player means the player closest to p in the clockwise direction among those who still have one or more cards at that point in time. Choose the player p as follows. For the first turn, choose P 1 . If, however, P 1 has no cards, choose the next player of P 1 . For the second and subsequent turns, choose the next player of the one chosen for the previous turn. The next player of p , say p′, looks at the card(s) in the hand of p and draws the one having the smallest number among them. If that makes a pair of cards with the same number in the hand of p′ , the pair is discarded. If no cards remain in any players' hands at this point, the game ends. Figure B-1 shows the initial hands of the three players of the second dataset in Sample Input. Immediately after, the player P 1 discards the two cards in the hand. p of the first turn is P 2 because P 1 has no cards. Figure B-1: The initial hands of the second dataset in Sample Input Figure B-2 shows the hands of the five players of the last dataset in Sample Input after the following actions: P 1 is chosen first and the next player of P 1 , that is, P 2 , draws the card with the smallest number 1 from the P 1 's hand. P 2 has three cards with numbers 1, 3, and 5 in hand at the end of this turn; P 3 , P 4 , and P 5 , in this order, draw the same card with the number 1 that was initially in the P 1 's hand; and P 5 discards the pair of cards with the number 1. After this, P 1 draws the only remaining card in P 5 's hand and discards the drawn card and the card with the same number 2 in the hand. The player to be drawn from the hand next is the next player of P 5 , which is P 2 because P 1 's hand is empty now. Figure B-2: The hands of the last dataset in Sample Input after the fourth turn Since at least one pair of cards is discarded between two drawings from the hand of the same player, the game will end sooner or later. Unlike Old Maid, no cards will be left in the hand of any player at the end. Write a program that, for given initial hands of all the players, computes the number of times cards are drawn by the end of the game. Input The input consists of multiple datasets, each in the following format. n c 1,1 c 1,2 ⋮ c n ,1 c n ,2 n is the number of players. n is an integer no less than two and no more than 1000. c i ,1 and c i ,2 are the numbers on the two cards in the initial hand of P i (1 ≤ i ≤ n ). Each integer between 1 and n, inclusive ...(truncated)
[ { "submission_id": "aoj_1657_10684192", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <math.h>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <functional>\n#include <cassert>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define all(a) a.begin(), a.end()\n#define arr(a) a.rbegin(), a.rend()\ntemplate<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}\ntemplate<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}\nusing ll = long long;\n\n//Konishii\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n while(true) {\n int n; cin >> n;\n if(n == 0) break;\n\n vector<set<int>> c(n);\n rep(i, n) {\n int a, b; cin >> a >> b;\n c[i].insert(a);\n c[i].insert(b);\n if(a == b) c[i].clear();\n }\n\n int ans = 0;\n rep(_, n) {\n rep(i, n) {\n if(c[i].size()) {\n int idx = (i + 1) % n;\n while(c[idx].empty()) idx = (idx + 1) % n;\n int x = *c[i].begin();\n if(c[idx].find(x) != c[idx].end()) {\n c[idx].erase(x);\n } else {\n c[idx].insert(x);\n }\n c[i].erase(x);\n\n ans++;\n }\n }\n }\n cout << ans << \"\\n\";\n } \n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3712, "score_of_the_acc": -0.7841, "final_rank": 15 }, { "submission_id": "aoj_1657_10683790", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int n;\n vector<int> lastans;\n while(true){\n cin >> n;\n if(n==0){\n break;\n }\n int c1,c2;\n vector<int> vec(n);\n vector<set<int>> st(n);\n int count=0,ans=0,last=-1,start;\n for(int i=0;i<n;i++){\n cin >> c1 >> c2;\n if(c1!=c2){\n st[i].insert(c1);\n st[i].insert(c2);\n count++;\n if(last!=-1){\n vec[last]=i;\n }else{\n start=i;\n }\n last=i;\n }\n }\n if(last!=-1){\n vec[last]=start;\n }\n int i=start,j;\n while(count>0){\n if(st[i].size()==0){\n vec[j]=vec[i];\n i=vec[i];\n continue;\n }\n int p=*st[i].begin();\n st[i].erase(p);\n if(st[i].size()==0){\n vec[j]=vec[i];\n }\n if(st[vec[i]].count(p)){\n st[vec[i]].erase(p);\n count--;\n }else{\n st[vec[i]].insert(p);\n }\n ans++;\n if(st[i].size()!=0){\n j=i;\n }\n i=vec[i];\n }\n lastans.push_back(ans);\n }\n for(auto at:lastans){\n cout << at << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3596, "score_of_the_acc": -0.5611, "final_rank": 9 }, { "submission_id": "aoj_1657_10672021", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n#define rep2(i,m,n) for(ll i=ll(m);i<ll(n);i++)\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vvvl = vector<vvl>;\nusing pl = pair<ll,ll>;\nusing vpl = vector<pl>;\nusing vvpl = vector<vpl>;\n#define pb push_back\n\nconst long double EPS = 0.0000000001;\nconst ll INF = 1000000000000000000;\nconst double pi = std::acos(-1.0);\n\n__int128 read_int128(){ //__int128を入力する\n string S;\n cin >> S;\n int N = S.size();\n int st = 0;\n bool minus = false;\n if(S[0] == '-'){\n minus = true;\n st = 1;\n }\n __int128 res = 0;\n rep2(i,st,N) res = res*10+int(S[i]-'0');\n if(minus) res *= -1;\n return res;\n}\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { //これでcoutで__int128を出力できるように\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\nvoid Yes(){ cout << \"Yes\" << endl; } //文字列\"Yes\"を標準出力\nvoid No(){ cout << \"No\" << endl; } //文字列\"No\"を標準出力\n\ntemplate<class T> bool chmin(T& a,T b){\n if(a > b){\n a = b;\n return true;\n }\n else return false;\n}\n\ntemplate<class T> bool chmax(T& a,T b){\n if(a < b){\n a = b;\n return true;\n }\n else return false;\n}\n\n\n\nint main(){\n while(1){\n int N;\n cin >> N;\n if(N == 0) break;\n vvl card(N,vl(2));\n rep(i,N) rep(j,2) cin >> card[i][j];\n int res = 2*N;\n rep(i,N){\n if(card[i][0] == card[i][1]){\n card[i].pop_back();\n card[i].pop_back();\n res -= 2;\n }\n }\n int ans = 0;\n int p = 0;\n while(res > 0){\n ans++;\n while(1){\n if(card[p].size() == 0) p++;\n else break;\n p %= N;\n }\n int p_next = p+1;\n while(1){\n p_next %= N;\n if(card[p_next].size() == 0) p_next++;\n else break;\n }\n sort(card[p].rbegin(),card[p].rend());\n card[p_next].push_back(card[p].back());\n card[p].pop_back();\n rep(i,card[p_next].size()-1){\n bool br = false;\n rep2(j,i+1,card[p_next].size()){\n if(card[p_next][i] == card[p_next][j]){\n res -= 2;\n card[p_next][i] = N+1;\n card[p_next][j] = N+1;\n sort(card[p_next].begin(),card[p_next].end());\n while(card[p_next].size() > 0){\n if(card[p_next].back() == N+1) card[p_next].pop_back();\n else break;\n }\n br = true;\n break;\n }\n }\n if(br) break;\n }\n p = p_next;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3320, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1657_10661938", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 lll;\nusing ull = unsigned long long;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\ntemplate<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using pqmax = priority_queue<T>;\nconst ll inf=LLONG_MAX/3;\nconst ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\nconst ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};\n#define mp make_pair\n#define pb push_back\n#define eb emplace_back\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define si(x) ll(x.size())\n#define rept(n) for(ll _ovo_=0;_ovo_<n;_ovo_++)\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define per(i,n) for(ll i=n-1;i>=0;i--)\n#define rng(i,l,r) for(ll i=l;i<r;i++)\n#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)\n#define fore(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\ntemplate<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define VLL(name,size) vector<ll>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define SUM(...) accumulate(all(__VA_ARGS__),0LL)\ntemplate<class T> auto min(const T& a){ return *min_element(all(a)); }\ntemplate<class T> auto max(const T& a){ return *max_element(all(a)); }\ntemplate<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }\ntemplate<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }\ntemplate<class T, class F = less<>> map<T,vector<ll> > ivm(vector<T>& a, F b = F{}){ map<T,vector<ll> > ret; rep(i,si(a))ret[a[i]].push_back(i); return ret;}\ntemplate<class T, class F = less<>> map<T,ll> ivc(vector<T>& a, F b = F{}){ map<T,ll> ret; rep(i,si(a))ret[a[i]]++; return ret;}\ntemplate<class T, class F = less<>> vector<T> ivp(vector<T> a){ vector<ll> ret(si(a)); rep(i,si(a))ret[a[i]] = i; return ret;}\ntemplate<class T, class F = less<>> vector<ll> rev(vector<T> a){ reverse(all(a)); return a;}\ntemplate<class T, class F = less<>> vector<ll> sortby(vector<T> a, F b = F{}){vector<ll> w = a; sor(w,b); vector<pll> v; rep(i,si(a))v.eb(a[i],i); sor(v); if(w[0] != v[0].first)reverse(all(v)); vector<ll> ret; rep(i,si(v))ret.pb(v[i].second); return ret;}\ntemplate<class T, class P> vector<T> filter(vector<T> a,P f){vector<T> ret;rep(i,si(a)){if(f(a[i]))ret.pb(a[i]);}return ret;}\ntemplate<class T, class P> vector<ll> filter_id(vector<T> a,P f){vector<ll> ret;rep(i,si(a)){if(f(a[i]))ret.pb(i);}return ret;}\nll monotone_left(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_left(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_right(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\nll monotone_right(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\ndouble monotone_double_left(double l,double r,function<bool(double)> f){assert(f(l) >= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return l;}\ndouble monotone_double_right(double l,double r,function<bool(double)> f){assert(f(l) <= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return r;}\ntemplate<class S> S unimodal_max(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) < f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmax(ret,f(k)); return ret;}\ntemplate<class S> S unimodal_min(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) > f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmin(ret,f(k)); return ret;}\nvector<pll> neighbor4(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,4){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\nvector<pll> neighbor8(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,8){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\n\nvoid outb(bool x){cout<<(x?\"Yes\":\"No\")<<\"\\n\";}\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))\nll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}\nvector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }\nvector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }\nvll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}\nll powll(lll x,ll y){lll res = 1; while(y){ if(y & 1)res = res * x; x = x * x; y >>= 1;} return res;}\nll powmod(lll x,ll y,lll mod){lll res=1; while(y){ if(y&1)res=res*x%mod; x=x*x%mod; y>>=1;} return res; }\nll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0)u+=m;return u;}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <class T> vector<T> &operator++(vector<T> &v) {\n\t\tfore(e, v) e++;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator++(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e++;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {\n\t\tfore(e, v) e--;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator--(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e--;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {\n\t\tfore(e, r) l.eb(e);\n\t\treturn l;\n}\n\ntemplate<class... Ts> void in(Ts&... t);\n[[maybe_unused]] void print(){}\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts);\ntemplate<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\\n'; }\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }\ntemplate<class T> void o(const T& t){ o(t, P<4>{}); }\ntemplate<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }\ntemplate<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }\ntemplate<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }\ntemplate<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }\ntemplate<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }\ntemplate<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }\n#undef VOID\n}\n#define unpack(a) (void)initializer_list<int>{(a, 0)...}\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }\n#undef unpack\nbool solve(){\n\tll n;\n\tcin>>n;\n\tif(n == 0)return false;\n\tvector<vll> c(n);\n\trep(i,n){\n\t\tc[i].resize(2);\n\t\trep(j,2)cin>>c[i][j];\n\t\tif(c[i][0] == c[i][1]){\n\t\t\tc[i].pop_back();\n\t\t\tc[i].pop_back();\n\t\t}\n\t}\n\tll ans = 0;\n\tll p = 0;\n\twhile(si(c[p]) == 0){\n\t\tp = (p + 1) % n;\n\t\tif(p == 0)break;\n\t}\n\twhile(1){\n\t\t{\n\t\t\tll q = (p + 1) % n;\n\t\t\twhile(si(c[q]) == 0 && p != q){\n\t\t\t\tq = (q + 1) % n;\n\t\t\t}\n\t\t\tif(p == q)break;\n\t\t\tsort(all(c[p]));\n\t\t\treverse(all(c[p]));\n\t\t\tll k = c[p].back();\n\t\t\tc[p].pop_back();\n\t\t\tans++;\n\t\t\tvll v;\n\t\t\trep(j,si(c[q])){\n\t\t\t\tif(c[q][j] != k){\n\t\t\t\t\tv.eb(c[q][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(si(v) == si(c[q]))c[q].eb(k);\n\t\t\telse c[q] = v;\n\t\t}\n\t\t{\n\t\t\tll q = (p + 1) % n;\n\t\t\twhile(si(c[q]) == 0 && p != q){\n\t\t\t\tq = (q + 1) % n;\n\t\t\t}\n\t\t\tif(p == q)break;\n\t\t\tp = q;\n\t\t}\n\t}\n\tcout<<ans<<endl;\n\treturn true;\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3416, "score_of_the_acc": -0.3361, "final_rank": 4 }, { "submission_id": "aoj_1657_10661496", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef std::pair<long long, long long> P;\ntypedef std::priority_queue<P, std::vector<P>, std::greater<P>> PQ;\ntypedef std::complex<double> cd;\n\nstruct P3 {\n long long first, second, third;\n};\n\nstruct P3P {\n P first, second, third;\n};\n\nstruct compP3{\n bool operator()(const P3 &p1,const P3 &p2) const {\n if (p1.first != p2.first) return p1.first < p2.first;\n if (p1.second != p2.second) return p1.second < p2.second;\n else return p1.third < p2.third;\n }\n};\n\nstruct gcompP3{\n bool operator()(const P3 &p1,const P3 &p2) const {\n if (p1.first != p2.first) return p1.first > p2.first;\n if (p1.second != p2.second) return p1.second > p2.second;\n else return p1.third > p2.third;\n }\n};\n\nconst double PI = acos(-1.0);\n\nbool ckran(int a, int n) {\n return (a >= 0 && a < n);\n}\n\nvoid yn (bool f) {\n if (f) std::cout << \"Yes\" << '\\n';\n else std::cout << \"No\" << '\\n';\n}\n\nlong long pplus(P a) {\n return a.first + a.second;\n}\n\nlong long pminus(P a) {\n return a.first - a.second;\n}\n\nlong long ptime(P a) {\n return a.first * a.second;\n}\n\nlong long pdiv(P a) {\n return a.first / a.second;\n}\n\ntemplate<typename T, typename U>\nbool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n } else {\n return false;\n }\n}\n\ntemplate<typename T, typename U>\nbool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n } else {\n return false;\n }\n}\n\ntemplate<typename T>\nvoid outspace(std::vector<T> a) {\n int n = a.size();\n for (int i = 0; i < n; ++i) {\n std::cout << a[i];\n if (i != n - 1) std::cout << \" \";\n else std::cout << std::endl;\n }\n}\n\nvoid outspace(P a) {\n std::cout << a.first << ' ' << a.second << '\\n';\n}\n\ntemplate<typename T>\nvoid outendl(std::vector<T> a) {\n int n = a.size();\n for (int i = 0; i < n; ++i) {\n std::cout << a[i] << '\\n';\n }\n}\n\nstd::vector<long long> lltovec(long long n, long long base = 10, long long minsize = -1) {\n std::vector<long long> res;\n while (minsize-- > 0 || n > 0) {\n res.push_back(n % base);\n n /= base;\n }\n // std::reverse(res.begin(), res.end());\n return res;\n}\n\nlong long vectoll(std::vector<long long> vec, long long base = 10) {\n long long res = 0;\n std::reverse(vec.begin(), vec.end());\n for (auto i : vec) {\n res *= base;\n res += i;\n }\n std::reverse(vec.begin(), vec.end());\n return res;\n}\n\nstatic const long long MOD = 998244353;\nstatic const int MAXN = 1000000; // 必要に応じて大きく設定\n\n// 階乗・階乗逆元の配列\nstatic long long fact[MAXN+1], invFact[MAXN+1];\n\n// 繰り返し二乗法 (a^b mod M)\nlong long modpow(long long a, long long b, long long M) {\n long long ret = 1 % M;\n a %= M;\n while (b > 0) {\n if (b & 1) ret = (ret * a) % M;\n a = (a * a) % M;\n b >>= 1;\n }\n return ret;\n}\n\n// 前処理: 階乗と逆元のテーブルを作る\nvoid initFactorials() {\n // 階乗テーブル\n fact[0] = 1;\n for (int i = 1; i <= MAXN; i++) {\n fact[i] = fact[i-1] * i % MOD;\n }\n // 階乗逆元テーブル\n invFact[MAXN] = modpow(fact[MAXN], MOD - 2, MOD); // フェルマーの小定理で逆元を計算\n for (int i = MAXN; i >= 1; i--) {\n invFact[i-1] = invFact[i] * i % MOD;\n }\n}\n\n// 組み合わせ数 C(n, r)\nlong long comb(int n, int r) {\n if (r < 0 || r > n) return 0;\n return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;\n}\n\nint main()\n{\n vector<ll> an;\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<set<int>> c(n);\n int cur = -1;\n ll ans = 0;\n ll count = 0;\n for (int i = 0; i < n; ++i) {\n int a, b;\n cin >> a >> b;\n if (a == b) continue;\n c[i].insert(a);\n c[i].insert(b);\n if (cur == -1) cur = i;\n count += 2;\n }\n while (count) {\n int next = cur + 1;\n next %= n;\n while (c[next].size() == 0) {\n next++;\n next %= n;\n }\n int d = *(c[cur].begin());\n c[cur].erase(d);\n ans++;\n cur = next;\n if (c[next].find(d) != c[next].end()) {\n c[next].erase(d);\n count -= 2;\n if (count == 0) break;\n if (c[next].size() == 0) {\n while (c[cur].size() == 0) {\n cur++;\n cur %= n;\n }\n }\n } else {\n c[next].insert(d);\n }\n }\n an.push_back(ans);\n continue;\n }\n outendl(an);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3516, "score_of_the_acc": -0.4375, "final_rank": 5 }, { "submission_id": "aoj_1657_10649418", "code_snippet": "#include <bits/stdc++.h>\n// using namespace std;\n\nint main() {\n\n while (true) {\n\n int n;\n std::cin >> n;\n\n if (n == 0) {\n break;\n }\n\n std::list<std::set<int>> a;\n for (int i = 0; i < n; ++i) {\n\n int c1, c2;\n std::cin >> c1 >> c2;\n\n if (c1 == c2) {\n continue;\n }\n\n a.push_back({c1, c2});\n }\n\n int ans = 0;\n for (auto now = a.begin(); not a.empty();) {\n\n if (now == a.end()) {\n now = a.begin();\n }\n\n if (now->empty()) {\n now = a.erase(now);\n continue;\n }\n\n int num = *now->begin();\n now->erase(num);\n ++ans;\n\n auto next = std::next(now);\n\n if (next == a.end()) {\n next = a.begin();\n }\n\n if (next->contains(num)) {\n next->erase(num);\n } else {\n next->insert(num);\n }\n\n if (now->empty()) {\n now = a.erase(now);\n } else {\n ++now;\n }\n }\n\n std::cout << ans << std::endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.7538, "final_rank": 13 }, { "submission_id": "aoj_1657_10649267", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\n\n// 問題文より\n// > 遅かれ早かれゲームは終わる\n// よって素直にシミュレーションすれば良いが実装が大変\n// この実装ではdequeを使って実装しました\n// deque: 先頭/末尾への値の挿入/削除がO(1)で行えるデータ構造\n//\n// 遅かれ早かれゲームは終わるについて\n// 全体のカードの中で値が最小のカードに着目する\n// 一周する間に必ず最小のカードは捨てられる\n// よってO(N^2)回の操作でゲームは終わる\nint solve() {\n int n;\n cin >> n;\n if (n == 0) return 1;\n // 持っているカード\n // 値の降順で管理する\n deque<vector<int>> que;\n rep(i, 0, n) {\n int a, b;\n cin >> a >> b;\n if (a == b) continue;\n if (a < b) swap(a, b);\n que.push_back(vector{a, b});\n }\n int ans = 0;\n while (!que.empty()) {\n assert(ssize(que) >= 2);\n // player p\n auto p1 = que.front();\n que.pop_front();\n // player p'\n auto p2 = que.front();\n que.pop_front();\n\n ans++;\n // カードは降順で管理しているので、\n // 最小の要素は最奥\n int card = p1.back();\n p1.pop_back();\n auto it = find(begin(p2), end(p2), card);\n if (it == end(p2)) {\n p2.push_back(card);\n sort(begin(p2), end(p2), greater());\n } else {\n p2.erase(it);\n }\n\n if (!empty(p1)) {\n que.push_back(p1);\n }\n if (!empty(p2)) {\n que.push_front(p2);\n }\n }\n cout << ans << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3584, "score_of_the_acc": -0.6289, "final_rank": 10 }, { "submission_id": "aoj_1657_10648627", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n cin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n while (true){\n int n;\n cin >> n;\n if (n == 0) break;\n vector<vector<int>> c(n, vector<int>(2));\n int cnt = n;\n for (int i = 0; i < n; i++){\n cin >> c[i][0] >> c[i][1];\n if (c[i][0] == c[i][1]){\n cnt--;\n c[i].pop_back();\n c[i].pop_back();\n }\n }\n int k = 0, ans = 0, m = -1;\n while (cnt > 0){\n while (c[k % n].empty()){\n k++;\n }\n if (m != -1) c[k % n].push_back(m);\n set<int> st(c[k % n].begin(), c[k % n].end());\n map<int, int> mp;\n for (int i : c[k % n]) mp[i]++;\n for (auto [k, v] : mp){\n if (v == 2){\n st.erase(k);\n cnt--;\n }\n }\n if (st.empty()){\n c[k % n].clear();\n m = -1;\n continue;\n }\n m = *st.begin();\n st.erase(m);\n c[k % n] = {st.begin(), st.end()};\n ans++;\n k++;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3584, "score_of_the_acc": -0.8107, "final_rank": 16 }, { "submission_id": "aoj_1657_10648210", "code_snippet": "#include <bits/stdc++.h>\nnamespace ranges = std::ranges;\nint N;\nstd::vector<int> C[1010];\nint next(int v) {\n assert(0 <= v and v < N);\n for (int i = 0 ; i < N ; i++) if (C[(i + v) % N].size())\n return (i + v) % N;\n return -1;\n}\nvoid merge(std::vector<int>& c, int val) {\n auto it = ranges::find(c, val);\n if (it != c.end()) {\n c.erase(it);\n }\n else {\n c.push_back(val);\n }\n}\nint solve() {\n for (int i = 0 ; i < N ; i++) if (C[i][0] == C[i][1]) {\n C[i].clear();\n }\n int ans = 0, p = next(0);\n while (p != -1) {\n assert(C[p].size());\n int v = next((p + 1)%N);\n assert(C[v].size());\n assert(p != v);\n auto pull = ranges::min_element(C[p]);\n merge(C[v], *pull);\n C[p].erase(pull);\n ans++;\n p = next(v);\n }\n return ans;\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n for (int i = 0 ; i < N ; i++) {\n C[i] = std::vector<int>(2);\n std::cin >> C[i][0] >> C[i][1];\n }\n std::cout << solve() << '\\n';\n std::cout.flush();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.5077, "final_rank": 7 }, { "submission_id": "aoj_1657_10648196", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\nusing namespace std;\nusing lint = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n;\n while (1) {\n in(n);\n if (n == 0) break;\n\n deque<set<int>> q;\n rep(i, n) {\n set<int> s;\n int a, b;\n in(a, b);\n if (a == b) continue;\n s.insert(a);\n s.insert(b);\n q.push_back(s);\n }\n int res = 0;\n while (!q.empty()) {\n set<int> cur = q.front();\n q.pop_front();\n set<int> nex = q.front();\n q.pop_front();\n int min_card = *cur.begin();\n cur.erase(cur.begin());\n res++;\n if (nex.contains(min_card)) {\n nex.erase(min_card);\n } else {\n nex.insert(min_card);\n }\n\n if (!cur.empty()) q.push_back(cur);\n if (!nex.empty()) q.push_front(nex);\n }\n out(res);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3712, "score_of_the_acc": -1.0569, "final_rank": 18 }, { "submission_id": "aoj_1657_10647667", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\n\nint solve() {\n int n;\n cin >> n;\n if (n == 0) return 1;\n\n deque<vector<int>> que;\n rep(i, 0, n) {\n int a, b;\n cin >> a >> b;\n if (a == b) continue;\n if (a < b) swap(a, b);\n que.push_back(vector{a, b});\n }\n int ans = 0;\n while (!que.empty()) {\n auto p1 = que.front();\n que.pop_front();\n\n auto p2 = que.front();\n que.pop_front();\n\n ans++;\n\n int card = p1.back();\n p1.pop_back();\n auto it = find(begin(p2), end(p2), card);\n if (it == end(p2)) {\n p2.push_back(card);\n sort(begin(p2), end(p2), greater());\n } else {\n p2.erase(it);\n }\n\n if (!empty(p1)) {\n que.push_back(p1);\n }\n if (!empty(p2)) {\n que.push_front(p2);\n }\n }\n cout << ans << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3712, "score_of_the_acc": -0.8751, "final_rank": 17 }, { "submission_id": "aoj_1657_10643025", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n){\n deque<set<int>> dq;\n for (int i = 0; i < n; i++){\n int a,b;cin >> a >> b;\n if (a != b){\n set<int> s;\n s.insert(a);s.insert(b);\n dq.push_back(s);\n }\n }\n\n int ans = 0;\n\n while(!dq.empty()){\n set<int> s;\n s = dq.front();\n dq.pop_front();\n\n int mn = *s.begin();\n\n if (s.size() > 1){\n s.erase(mn);\n dq.push_back(s);\n }\n\n if (dq.front().count(mn)){\n dq.front().erase(mn);\n }\n else dq.front().insert(mn);\n if (dq.front().size() == 0){\n dq.pop_front();\n }\n ans++;\n }\n cout << ans << endl;\n}\n\nint main(){\n int n;cin >> n;\n while(n){\n solve(n);\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3496, "score_of_the_acc": -0.5506, "final_rank": 8 }, { "submission_id": "aoj_1657_10616214", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<set<int>> C(N, set<int>{});\n int erase = 0;\n for (int i = 0; i < N; i++) {\n int x, y;\n cin >> x >> y;\n if (x != y) C[i].insert(x), C[i].insert(y);\n else erase += 2;\n }\n int ans = 0;\n for (int p = 0; erase < 2 * N; ) {\n ans++;\n while (C[p].empty()) p = (p == N - 1 ? 0 : p + 1);\n int q = (p == N - 1 ? 0 : p + 1);\n while (C[q].empty()) q = (q == N - 1 ? 0 : q + 1);\n int c = *C[p].begin();\n C[p].erase(C[p].begin());\n auto it = C[q].find(c);\n if (it == C[q].end()) {\n C[q].insert(c);\n } else {\n C[q].erase(it);\n erase += 2;\n }\n p = q;\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.7538, "final_rank": 13 }, { "submission_id": "aoj_1657_10611777", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nll n;\nvector<a2> v;\nvoid input() {\n cin >> n;\n v.resize(n);\n for(auto &x : v)\n cin >> x[0] >> x[1];\n}\n\nvoid solve() {\n ll ans = 0;\n ll cnt = 0;\n for(int i = 0; i < n; i++) {\n if(v[i][0] == v[i][1]) {\n v[i] = {0, 0};\n cnt++;\n }\n }\n\n\n ll cho = 0, now = 0;\n while(cnt < n) {\n if(cho == 0) {\n if(v[now][0]&&(!v[now][1]||v[now][1]>v[now][0])) {\n swap(cho, v[now][0]);\n ans++;\n } else if(v[now][1]&&(!v[now][0]||v[now][0]>v[now][1])) {\n swap(cho, v[now][1]);\n ans++;\n }\n } else {\n if(v[now][0] == cho) {\n ans += v[now][1] != 0;\n cho = v[now][1];\n v[now][0] = 0;\n v[now][1] = 0;\n cnt++;\n } else if(v[now][1] == cho) {\n ans += v[now][0] != 0;\n cho = v[now][0];\n v[now][0] = 0;\n v[now][1] = 0;\n cnt++;\n } else if(v[now][0]||v[now][1]){\n if(v[now][0] < cho && v[now][0]) {\n swap(cho, v[now][0]);\n }\n if(v[now][1] < cho && v[now][1]) {\n swap(cho, v[now][1]);\n }\n ans++;\n }\n }\n now++;\n now %= n;\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3364, "score_of_the_acc": -0.0846, "final_rank": 2 }, { "submission_id": "aoj_1657_10605653", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\nusing namespace std;\n\nclass People {\nprivate:\n map<long long, long long> p;\n\npublic:\n People(long long p1 = 1e9, long long p2 = 1e9) {\n if (p1 != p2) {\n p[p1]++;\n p[p2]++;\n }\n }\n\n void addCard(int c) {\n p[c]++;\n if (p[c] == 2) p.erase(c);\n }\n\n int getMin() {\n int res = p.begin()->first;\n p[res]--;\n if (p[res] == 0) p.erase(res);\n return res;\n }\n\n bool isEmpty() {\n return p.empty();\n }\n};\n\nbool isEnd(vector<People>& p) {\n bool res = true;\n for (auto& P : p) {\n if (!P.isEmpty()) res = false;\n }\n return res;\n}\n\nint main() {\n int n;\n while (cin >> n, n!=0) {\n int ans = 0;\n vector<People> p(n);\n for (int i = 0; i < n; i++) {\n int c1, c2;\n cin >> c1 >> c2;\n p[i] = People(c1, c2);\n }\n\n int now = 0, next = 0;\n\n while (!isEnd(p)) {\n if(p[now].isEmpty()){\n for (int i = 0; i < n; i++) {\n if (!p[i].isEmpty()) {\n now = i;\n break;\n }\n }\n }\n for (int i = 1; i <= n; i++) {\n if (!p[(i + now) % n].isEmpty()) {\n next = (i + now) % n;\n break;\n }\n }\n p[next].addCard(p[now].getMin());\n now = next;\n ans++;\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3528, "score_of_the_acc": -1.4, "final_rank": 20 }, { "submission_id": "aoj_1657_10588322", "code_snippet": "#include <bits/stdc++.h>\n#include <iostream>\n#include <vector>\n#include <string>\n#include <stack>\n#include <algorithm>\n#include <iomanip>\nusing namespace std;\nusing ll = long long;//63bit型整数型\nint main(){\nwhile(true){\n ll N;\n cin >> N;\n if(N==0)return 0;\n vector<set<ll>>X(N);\n set<ll>out;\n for(int i = 0;i<N;i++){\n ll a,b;\n cin >> a >> b;\n if(a!=b){\n X[i].insert(a);\n X[i].insert(b);\n out.insert(i);\n out.insert(i+N); \n }\n }\n ll cnt = 0;\n if(out.empty()){\n cout << 0 << \"\\n\";\n continue;\n }\n ll start = *out.lower_bound(0);\n start%=N;\n while(true){\n if(out.empty())break;\n if(X[start].empty()){\n start = *out.lower_bound(start+1);\n start%=N;\n continue;\n }\n ll x = *X[start].begin();\n ll y = *out.lower_bound(start+1);\n y%=N;\n if(X[start].size()==1){\n out.erase(start);\n out.erase(start+N);\n }\n X[start].erase(x);\n if(X[y].count(x)){\n X[y].erase(x);\n if(X[y].empty()){\n out.erase(y);\n out.erase(y+N);\n }\n }\n else X[y].insert(x);\n cnt++;\n start = y;\n }\n cout << cnt << \"\\n\";\n}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3612, "score_of_the_acc": -0.6828, "final_rank": 11 }, { "submission_id": "aoj_1657_10588293", "code_snippet": "#include <bits/stdc++.h>\n#include <iostream>\n#include <vector>\n#include <string>\n#include <stack>\n#include <algorithm>\n#include <iomanip>\n#include <chrono>\n#pragma GCC target (\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#define rep(i,n) for (ll i = 0;i < (ll)(n);i++)\n#define Yes cout << \"Yes\" << \"\\n\"// YESの短縮\n#define No cout << \"No\" << \"\\n\"// NOの短縮\n#define rtr0 return(0)//return(0)の短縮\n#define gyakugen(x) modpow(x,mod - 2,mod);\n#define agyakugen(x) modpow(x,amod - 2,amod);\n#define st(A) sort(A.begin(),A.end());\n#define rst(A) sort(A.rbegin(),A.rend());\nusing namespace std;\n//using namespace ranges;\nusing ll = long long;//63bit型整数型\nusing ld = long double;//doubleよりも長い値を保存できるもの\nusing ull = unsigned long long;//符号がない64bit型整数\nll mod = 998244353;\nll amod = 1000000007;\nll MINF = -5000000000000000000;\nll INF = 5000000000000000000;\nll inf = 2000000000;\nll minf = -2000000000;\nll BAD = -1;\nll zero = 0;\nld EPS = 1e-10;\nvector<ll>randomhash = {(ll)1e9 + 7,(ll)1e9 + 801,((ll)1e8 * 8) + 29,((ll)1e8 * 7) + 159,((ll)1e8 * 9) + 221};\nvector<ll>tate = {0,-1,0,1};//グリッド上の全探索時の四方向の上下のチェック\nvector<ll>yoko = {1,0,-1,0};//グリッド上の全探索時の四方向の右左のチェック\nvector<ll>eightx = {0,-1,-1,-1,0,1,1,1};//グリッド上の全探索時の八方向の上下のチェック\nvector<ll>eighty = {1,1,0,-1,-1,-1,0,1};//グリッド上の全探索時の八方向の右左のチェック\nvector<ll>hexsax = {0,1,1,0,-1,-1};\nvector<ll>hexsay = {1,1,0,-1,-1,0};\n//返り値は素数のリスト。\nvector < bool > isprime;\nvector < ll > Era(int n){//書き方 vector<ll>[] = Era(x); とやるとxまでの素数が出てくる。\n\tisprime.resize(n, true);\n\tvector < ll > res;\n\tisprime[0] = false;\n\tisprime[1] = false;\n\tfor(ll i = 2; i < n; ++i) isprime[i] = true;\n\tfor(ll i = 2; i < n; ++i) {\n\t\tif(isprime[i]) {\n\t\t\tres.push_back(i);\n\t\t\tfor(ll j = i * 2; j < n; j += i) isprime[j] = false;\n\t\t}\n\t}\n\treturn res;\n}\n//      素数判定 21~35\null modmul(ull a,ull b,ull mod) {\n return (__uint128_t)a*b%mod;\n}\nll modpow(ll a, ll n, ll mod) {\n\tll res = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long npow(long long a, long long n){\n long long res = 1;\n while (n > 0) {\n\t\tif (n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n// モッドを使った累乗\nlong long isqrt(long long num){\n return((long long)sqrtl(num));\n}\nld get_theta(ld px,ld py,ld fx,ld fy,ld sx,ld sy){\n ld fxv = fx-px;\n ld fyv = fy-py;\n ld sxv = sx-px;\n ld syv = sy-py;\n ld fsv = fxv*sxv + fyv*syv;\n ld pfs = (ld)sqrt(fxv*fxv+fyv*fyv);\n ld pss = (ld)sqrt(sxv*sxv+syv*syv);\n return acos(fsv/(pfs*pss))*180/M_PI;\n};\nld Euclidean_distance(ll x1,ll y1,ll x2,ll y2){\n ld x = abs((ld)x1 - (ld)x2);\n ld y = abs((ld)y1 - (ld)y2);\n return sqrt((x*x) + (y*y));\n};\nll Manhattan_distance(ll x1,ll y1,ll x2,ll y2){\n return abs(x1-x2)+abs(y1-y2);\n};\nvoid change_bit(ll N,ll start,vector<ll>&X){\n for(ll i = start;i>=0;i--){\n ll a = npow(2,i);\n if(N/a==1){\n X[i]=1;\n N-=a;\n }\n }\n}\nbool is_in_grid(ll i,ll ii,ll H,ll W){\n bool res = false;\n if(i>=0&&i<H&&ii>=0&&ii<W)res=true;\n return res;\n}\ntemplate<class T>\nstruct rolling_hash{\n vector<ull>Power,hash,InvPower;\n ll B = 0;\n ll MOD = 0;\n void set_number(ll base,ll mod){\n B = base;\n MOD = mod;\n }\n void do_hash(const T &S) {\n ll N = S.size();\n Power.resize(N+1);\n InvPower.resize(N+1);\n hash.resize(N+1);\n Power[0] = 1;\n InvPower[0] = 1;\n for (ll i = 0;i<N;i++) {\n Power[i + 1] = modmul(Power[i],B,MOD);\n InvPower[i + 1] = modpow(Power[i+1],MOD-2,MOD);\n hash[i + 1] = (hash[i] + modmul(S[i], Power[i],MOD))%MOD;\n }\n }\n ll get_hash(ll l, ll r) {\n ull res = (hash[r]+MOD-hash[l])%MOD;\n res = modmul(res,InvPower[l],MOD);\n return res;\n }\n};\ntemplate<class T>\nstruct combination{\n public:\n vector<T>factorial;\n ll MOD;\n ll N;\n void reset(T n,T mod){\n N = n+1;\n factorial.assign(N,1);\n MOD = mod;\n }\n void calu(){\n for(ll i = 1;i<N;i++){\n factorial[i] = (factorial[i-1]*(i))%MOD;\n }\n }\n T get(T n, T r){\n ll a = factorial[n];\n ll b = (factorial[r]*(factorial[n-r]))%MOD;\n b = modpow(b,MOD - 2,MOD);\n return (a*b)%MOD;\n }\n};\ntemplate<class T>\nstruct dijkstra{\n public:\n\tvector<vector<pair<T,T>>>graph;\n\tvector<T>ans;\n\tpriority_queue<pair<T,T>,vector<pair<T,T>>,greater<pair<T,T>>>pq;\n void do_dijkstra(T start){//頂点xからダイクストラを行う\n\t\tpq.push({0,start});\n\t\tans[start] = 0;\n\t\twhile(true){\n\t\t\tif(pq.empty())break;\n\t\t\tT cost = pq.top().first;\n\t\t\tT vertex = pq.top().second;\n\t\t\tpq.pop();\n if (cost > ans[vertex]) continue; \n\t\t\tfor(T i = 0;i<graph[vertex].size();i++){\n\t\t\t\tT nextvertex = graph[vertex][i].first;\n\t\t\t\tT nextcost = graph[vertex][i].second + cost;\n\t\t\t\tif(ans[nextvertex] <= nextcost)continue;\n\t\t\t\t\tans[nextvertex] = nextcost;\n\t\t\t\t\tpq.push({nextcost,nextvertex});\n\t\t\t}\n\t\t}\n\t}\n\tvoid make_indirectedgraph(T u,T v,T cost){//無向辺のグラフ作成\n\t\tgraph[u].push_back({v,cost});\n\t\tgraph[v].push_back({u,cost});\n\t}\n\tvoid make_directedgraph(T u,T v,T cost){//有向辺のグラフ作成\n\t\tgraph[u].push_back({v,cost});\n\t}\n\tT output(T end){//答えを出す\n\t\treturn ans[end];\n\t} \n\tvoid reset(T N){//リセット\n\t\tgraph.assign(N, {});\n ans.assign(N, INF);\n\t}\n};\ntemplate<class T>\nstruct unionfind {\npublic:\n vector<T> parent, rank;\n void reset(T N) { // 初期化\n parent.resize(N);\n rank.assign(N, 1); // 各集合のサイズを 1 にする\n for (T i = 0; i < N; i++) parent[i] = i;\n }\n T leader(T x) { // 経路圧縮による親の取得\n if (parent[x] == x) return x;\n return parent[x] = leader(parent[x]); // 経路圧縮\n }\n void marge(T x, T y) { // ランクによるマージ\n T a = leader(x), b = leader(y);\n if(a!=b){\n if (rank[a] < rank[b]) swap(a, b);\n parent[b] = a;\n rank[a] += rank[b]; // サイズを更新 \n }\n }\n bool same(T x, T y) { // 同じ集合か判定\n return leader(x) == leader(y);\n }\n T size(T x) { // 集合のサイズを取得\n return rank[leader(x)];\n }\n void check(T N) { // デバッグ用: 親の確認\n for (T i = 0; i < N; i++) cout << parent[i] << \" \";\n cout << \"\\n\";\n }\n};\n//セグ木\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n public:\n segtree() : segtree(0) {}\n segtree(int n) : segtree(std::vector<S>(n, e())) {}\n segtree(const std::vector<S>& v) : _n(int(v.size())){\n log = ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n \n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n \n S get(int p) {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n \n S prod(int l, int r) {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n \n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n \n S all_prod() { return d[1]; }\n \n template <bool (*f)(S)> int max_right(int l) {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do{\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n }while ((l & -l) != l);\n return _n;\n }\n \n template <bool (*f)(S)> int min_left(int r) {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do{\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n }while ((r & -r) != r);\n return 0;\n }\n \n private:\n int _n, size, log;\n std::vector<S> d;\n \n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n int ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n }\n};\n\n//遅延セグ木\ntemplate <class S,S (*op)(S, S),S (*e)(),class F,S (*mapping)(F, S),F (*composition)(F, F),F (*id)()>\nstruct lazy_segtree {\n\n public:\n lazy_segtree() : lazy_segtree(0) {}\n lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}\n lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {\n log = ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n lz = std::vector<F>(size, id());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) {\n assert(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n return d[p];\n }\n\n S prod(int l, int r) {\n assert(0 <= l && l <= r && r <= _n);\n if (l == r) return e();\n\n l += size;\n r += size;\n\n for (int i = log; i >= 1; i--) {\n if (((l >> i) << i) != l) push(l >> i);\n if (((r >> i) << i) != r) push(r >> i);\n }\n\n S sml = e(), smr = e();\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n\n return op(sml, smr);\n }\n\n S all_prod() { return d[1]; }\n\n void apply(int p, F f) {\n assert(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n d[p] = mapping(f, d[p]);\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n void apply(int l, int r, F f) {\n assert(0 <= l && l <= r && r <= _n);\n if (l == r) return;\n\n l += size;\n r += size;\n\n for (int i = log; i >= 1; i--) {\n if (((l >> i) << i) != l) push(l >> i);\n if (((r >> i) << i) != r) push((r - 1) >> i);\n }\n\n {\n int l2 = l, r2 = r;\n while (l < r) {\n if (l & 1) all_apply(l++, f);\n if (r & 1) all_apply(--r, f);\n l >>= 1;\n r >>= 1;\n }\n l = l2;\n r = r2;\n }\n\n for (int i = 1; i <= log; i++) {\n if (((l >> i) << i) != l) update(l >> i);\n if (((r >> i) << i) != r) update((r - 1) >> i);\n }\n }\n\n template <bool (*g)(S)> int max_right(int l) {\n return max_right(l, [](S x) { return g(x); });\n }\n template <class G> int max_right(int l, G g) {\n assert(0 <= l && l <= _n);\n assert(g(e()));\n if (l == _n) return _n;\n l += size;\n for (int i = log; i >= 1; i--) push(l >> i);\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!g(op(sm, d[l]))) {\n while (l < size) {\n push(l);\n l = (2 * l);\n if (g(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*g)(S)> int min_left(int r) {\n return min_left(r, [](S x) { return g(x); });\n }\n template <class G> int min_left(int r, G g) {\n assert(0 <= r && r <= _n);\n assert(g(e()));\n if (r == 0) return 0;\n r += size;\n for (int i = log; i >= 1; i--) push((r - 1) >> i);\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!g(op(d[r], sm))) {\n while (r < size) {\n push(r);\n r = (2 * r + 1);\n if (g(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n std::vector<F> lz;\n int ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n }\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n void all_apply(int k, F f) {\n d[k] = mapping(f, d[k]);\n if (k < size) lz[k] = composition(f, lz[k]);\n }\n void push(int k) {\n all_apply(2 * k, lz[k]);\n all_apply(2 * k + 1, lz[k]);\n lz[k] = id();\n }\n};\nstruct Euler_Tour{//最小共通祖先,部分木のコスト,通り道のコスト,のグラフを作る部分(オイラーツアー)\n //必要なもの集\n //セグ木は2*Nがベース\n //LCA(最小共通祖先)discovery,finish,visit,high\n //とある頂点xを根とする部分木の範囲 discovery,finish\n //頂点xを根とする部分木の辺と頂点のコストの総和 discovery,finish,vcost1,ecost1\n //頂点xを根とする頂点yまでの最短の辺と頂点のコストの総和 discovery,finish,vcost2,ecost2\n vector<int>discovery,finish,visit,high,vweight,vcost1,vcost2,ecost1,ecost2;\n vector<int>other;\n vector<vector<pair<int,int>>>graph;\n int cnt;\n int othercnt;\n void reset(ll N){\n cnt = 0;\n othercnt = 0;\n other.assign(2*N,0);\n discovery.assign(N,inf);\n finish.assign(N,minf);\n vweight.assign(N,0);\n graph.assign(N,vector<pair<int,int>>(0));\n visit.assign(2*N,0);\n high.assign(2*N,0);\n vcost1.assign(2*N,0);\n vcost2.assign(2*N,0);\n ecost1.assign(2*N,0);\n ecost2.assign(2*N,0);\n };\n void make_graph(ll u,ll v,ll cost){\n if(max(u,v)>graph.size()){\n cout << \"not reset or out of range\" << \"\\n\";\n return;\n }\n graph[u].push_back({v,cost});\n graph[v].push_back({u,cost});\n }\n void set_vweight(ll vertex,ll cost){\n vweight[vertex]+=cost;\n }\n void DFS(ll vertex,ll nowdepth,ll cost){\n if(discovery[vertex]==inf){\n vcost1[cnt]=vweight[vertex];\n vcost2[cnt]=vweight[vertex];\n }\n else{\n vcost1[cnt]=0;\n vcost2[cnt]=0;\n }\n ecost1[cnt]=cost;\n ecost2[cnt]=cost;\n discovery[vertex]=min(discovery[vertex],cnt);\n visit[cnt]=vertex;\n high[cnt]=nowdepth;\n for(int i = 0;i<graph[vertex].size();i++){\n int x = graph[vertex][i].first;\n int nextcost = graph[vertex][i].second;\n if(discovery[x]!=inf&&graph[vertex].size()==1)othercnt++;\n other[cnt]=othercnt;\n if(discovery[x]==inf){\n cnt++;\n DFS(x,nowdepth+1,nextcost);\n cnt++;\n visit[cnt]=vertex;\n high[cnt]=nowdepth;\n vcost1[cnt]=0;\n ecost1[cnt]=0;\n vcost2[cnt]=vweight[x]*-1;\n ecost2[cnt]=nextcost*-1;\n }\n other[cnt]=othercnt;\n }\n finish[vertex]=cnt+1;\n return;\n }\n int get_size(){\n ll res = high.size()-1;\n return res;\n }\n int get_vcost1(ll i){\n return ecost1[i];\n }\n int get_vcost2(ll i){\n return vcost2[i];\n }\n int get_ecost1(ll i){\n return vcost1[i];\n }\n int get_ecost2(ll i){\n return ecost2[i];\n }\n int get_high(ll i){\n return high[i];\n }\n int get_visit(ll i){\n return visit[i];\n }\n int get_discovery(ll i){\n return discovery[i];\n }\n int get_finish(ll i){\n return finish[i];\n }\n};\n//遅延セグ木のベース\n//型\nusing segS = ll;\nsegS oop(segS a,segS b){\n return max(a,b);\n}\nsegS ee(){\n return 0;\n}\n/*struct segS{\n ll mi = 0;\n ll ma = 0;\n};\nsegS oop(segS a,segS b){\n segS res;\n res.mi = min(a.mi,b.mi);\n res.ma = max(a.ma,b.ma);\n return res;\n}\nsegS ee(){\n segS res;\n res.mi = INF;\n res.ma = MINF;\n return res;\n}*/\n\n//using lazyS = ll;\nusing lazyS = ll;\nusing lazyF = ll;\nlazyS e(){\n lazyS res;\n res = 0;\n return res;\n}//範囲外などを起こしてしまったときの返り値\nlazyS op(lazyS a, lazyS b){\n lazyS res;\n res = a+b;\n return res; \n}//操作したいこと\nlazyS mapping(lazyF f, lazyS x){ \n x*=f;\n return x; \n}//下まで伝播させたいこと\nlazyF composition(lazyF f, lazyF g){ return g *= f; }//まとめてやった場合おこしたいこと\nlazyF id(){ return 1; }//遅延セグ木のベース\n/*\nusing lazyS = ll;\nusing lazyF = ll;\nlazyS e(){ return 0;}//範囲外などを起こしてしまったときの返り値\nlazyS op(lazyS a, lazyS b){ return a+b; }//操作したいこと\nlazyS mapping(lazyF f, lazyS x){ return x /= f; }//下まで伝播させたいこと\nlazyF composition(lazyF f, lazyF g){ return g /= f; }//まとめてやった場合おこしたいこと\nlazyF id(){ return 1; }//遅延セグ木のベース\n*/\n\nint main(){\n//B以上は基本詳細を書いておくと便利 A = ooの値とか 見直す時に便利\n// この問題の本質\n//cout << get_theta(0,0,0,0,0,1) << \"\\n\";\n//nCr = A+B+i C B\n//nCr = C-i+D-1 C D-1\nwhile(true){\n ll N;\n cin >> N;\n if(N==0)rtr0;\n vector<set<ll>>X(N);\n set<ll>out;\n for(int i = 0;i<N;i++){\n ll a,b;\n cin >> a >> b;\n if(a!=b){\n X[i].insert(a);\n X[i].insert(b);\n out.insert(i);\n out.insert(i+N); \n }\n }\n ll cnt = 0;\n if(out.empty()){\n cout << 0 << \"\\n\";\n continue;\n }\n ll start = *out.lower_bound(0);\n start%=N;\n while(true){\n if(out.empty())break;\n if(X[start].empty()){\n start = *out.lower_bound(start+1);\n start%=N;\n continue;\n }\n ll x = *X[start].begin();\n ll y = *out.lower_bound(start+1);\n y%=N;\n if(X[start].size()==1){\n out.erase(start);\n out.erase(start+N);\n }\n X[start].erase(x);\n if(X[y].count(x)){\n X[y].erase(x);\n if(X[y].empty()){\n out.erase(y);\n out.erase(y+N);\n }\n }\n else X[y].insert(x);\n cnt++;\n start = y;\n //cout << start <<\"\\n\";\n }\n cout << cnt << \"\\n\";\n}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3620, "score_of_the_acc": -0.6981, "final_rank": 12 }, { "submission_id": "aoj_1657_10583895", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(int i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(int i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\ntemplate<class T, size_t n, size_t idx = 0>\nauto make_vec(const size_t (&d)[n], const T& init) noexcept {\n if constexpr (idx < n) return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else return init;\n}\ntemplate<class T, size_t n>\nauto make_vec(const size_t (&d)[n]) noexcept {\n return make_vec(d, T{});\n}\n/**\n * Read a vector from input. Set start to 1 if you want it to be 1-indexed.\n */\ntemplate<typename T>\nvector<T> read_vector(int N, int start = 0) {\n vector<T> v(start + N);\n for (int i = start; i < (int)v.size(); i++) {\n std::cin >> v[i];\n }\n return v;\n}\n\nvoid solve(int n) {\n queue<vector<int>> Q;\n rep(i, 0, n) {\n int c1, c2;\n cin >> c1 >> c2;\n if(c1 == c2) continue;\n Q.push({c1, c2});\n }\n\n auto turn = [&](vector<int> &hand, vector<int> &nexthand) -> void {\n int card = *min_element(all(hand));\n hand.erase(find(all(hand), card));\n\n auto it = find(all(nexthand), card);\n if(it != nexthand.end()) {\n nexthand.erase(it);\n }\n else nexthand.emplace_back(card);\n };\n\n int ans = 0;\n while(!Q.empty()) {\n auto hand = Q.front();\n Q.pop();\n\n auto &nexthand = Q.front();\n\n turn(hand, nexthand);\n ans++;\n\n if(!hand.empty()) {\n Q.push(hand);\n }\n if(nexthand.empty()) {\n Q.pop();\n }\n }\n\n cout << ans << endl;\n}\n\nint main() {\n int n;\n while(true) {\n cin >> n;\n if(n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3396, "score_of_the_acc": -0.2371, "final_rank": 3 }, { "submission_id": "aoj_1657_10554758", "code_snippet": "// AOJ 1657 - Leave No One Behind\n// ICPC Japan Domestic Contest 2022 - Problem B\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while(true) {\n int N; cin >> N;\n if(N == 0) break;\n vector<set<int>> C(N);\n int win = 0;\n for(int i=0; i<N; ++i) {\n int c1, c2; cin >> c1 >> c2;\n if(c1 == c2) {\n ++win;\n } else {\n C[i].insert(c1);\n C[i].insert(c2);\n }\n }\n if(win == N) {\n cout << 0 << endl;\n continue;\n }\n\n int turn = 0;\n int i = 0;\n for(; C[i].empty(); ++i) {}\n while(true) {\n ++turn;\n int chosen = *C[i].begin();\n int j = (i+1) % N;\n for(; C[j].empty(); j = (j+1)%N) {}\n if(C[j].count(chosen)) {\n C[j].erase(chosen);\n if(C[j].empty()) {\n ++win;\n }\n }\n else C[j].insert(chosen);\n C[i].erase(chosen);\n if(C[i].empty()) {\n ++win;\n }\n if(win == N) break;\n i = j;\n for(; C[i].empty(); ++i) {}\n }\n cout << turn << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3520, "score_of_the_acc": -0.4452, "final_rank": 6 }, { "submission_id": "aoj_1657_10554414", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\nlong long mod_mul(long long a, long long b, long long mod) {\n return (__int128)a * b % mod;\n}\n\nlong long mod_pow(long long a, long long b, long long mod) {\n long long result = 1;\n a %= mod;\n while (b > 0) {\n if (b & 1) result = mod_mul(result, a, mod);\n a = mod_mul(a, a, mod);\n b >>= 1;\n }\n return result;\n}\n\nbool MillerRabin(ll x){\n \n if(x <= 1){return false;}\n if(x == 2){return true;}\n if((x & 1) == 0){return false;}\n vll test;\n if(x < (1 << 30)){\n test = {2, 7, 61};\n } \n else{\n test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n }\n ll s = 0;\n ll d = x - 1;\n while((d & 1) == 0){\n s++;\n d >>= 1;\n }\n for(ll i:test){\n if(x <= i){return true;}\n ll y = mod_pow(i,d,x);\n if(y == 1 || y == x - 1){continue;}\n ll c = 0;\n for(int j=0;j<s-1;j++){\n y = mod_mul(y,y,x);\n if(y == x - 1){\n c = 1;\n break;\n }\n }\n if(c == 0){\n return false;\n }\n }\n return true;\n}\n\n#include<atcoder/dsu>\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n while(1){\n LL(n);\n if(n == 0){break;}\n vector<set<ll>> a(n);\n set<ll> s;\n rep(i,n){\n LL(x,y);\n if(x != y){\n a[i].emplace(x);\n a[i].emplace(y);\n s.emplace(i);\n s.emplace(i + n);\n }\n }\n ll ans = 0;\n ll p = n-1;\n while(1){\n if(s.empty()){\n break;\n }\n //rep(i,n){\n // cout << i << endl;\n // cout << a[i] << endl;\n //}\n //cout << endl;\n auto y = s.upper_bound(p);\n p = *y;\n p %= n;\n ll q = *(s.upper_bound(p));\n q %= n;\n ll x = *(a[p].begin());\n a[p].erase(a[p].find(x));\n if(a[q].count(x) == 1){\n a[q].erase(a[q].find(x));\n }\n else{\n a[q].emplace(x);\n }\n if(a[p].empty()){\n s.erase(s.find(p));\n s.erase(s.find(p + n));\n }\n if(a[q].empty()){\n s.erase(s.find(q));\n s.erase(s.find(q + n));\n }\n ans++;\n\n }\n print(ans);\n \n \n }\n \n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3840, "score_of_the_acc": -1.0909, "final_rank": 19 } ]
aoj_1666_cpp
Problem C Changing the Sitting Arrangement You are the teacher of a class of n 2 pupils at an elementary school. Seats in the classroom are set up in a square shape of n rows (row 1 through row n ) by n columns (column 1 through column n ). You are planning a change in the sitting arrangement. In order for your pupils to interact with many different pupils, you want to make those currently on adjacent seats have remote seats. There is at least one sitting arrangement such that every pair of pupils currently on adjacent seats have seats with the Manhattan distance of no less than ⌊ n / 2⌋ between them. Your task is to find such an arrangement. Here, ⌊ x ⌋ represents the largest integer less than or equal to x. The Manhattan distance of two seats are the sum of the absolute difference of their row numbers and that of their column numbers. Adjacent seats mean those with the Manhattan distance 1. For example, in the first dataset of Sample Input ( n = 4), pupils at the four adjacent seats of the pupil no. 10 are those numbered 6, 9, 11, and 14. In the Output for the Sample Input corresponding to this, their seats have Manhattan distances 3, 3, 2, and 3 from the seat of the pupil no. 10, all of which are ⌊ 4 / 2 ⌋ = 2 or greater. This condition also holds for all the other pupils. Input The input consists of multiple datasets, each in the format below. The number of datasets does not exceed 50. n a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ a n 1 a n 2 ⋯ a n n Here, n is the number of rows and also columns of the seats in the classroom (2 ≤ n ≤ 50). The following n lines denote the current sitting arrangement. Each a ij gives the ID number of the pupil currently sitting in the i -th row from the back and the j -th column from the left, as seen from the teacher's desk. a ij 's are integers 1 through n 2 , without duplicates. The end of the input is indicated by a line consisting of a single zero. Output For each dataset, output a sitting arrangement satisfying the condition stated above in the same format as in the input. If there are two or more such arrangements, any of them will do. Sample Input 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 7 1 8 15 22 29 36 43 2 9 16 23 30 37 44 3 10 17 24 31 38 45 4 11 18 25 32 39 46 5 12 19 26 33 40 47 6 13 20 27 34 41 48 7 14 21 28 35 42 49 0 Output for the Sample Input 6 1 14 8 16 13 11 9 4 10 2 12 5 3 15 7 4 20 18 44 12 16 36 23 38 40 48 7 39 26 29 9 14 35 37 2 49 13 46 30 5 45 43 24 42 33 17 22 19 27 47 1 3 21 25 11 15 41 31 6 8 34 28 32 10
[ { "submission_id": "aoj_1666_10669785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nbool check(int N,vector<vector<int>>X,vector<vector<int>>Y){\n vector<pair<int,int>>at(N*N+1);\n rep(i,0,N)rep(j,0,N){\n at[Y[i][j]]={i,j};\n }\n rep(i,0,N)rep(j,0,N)rep(k,0,N)rep(l,0,N){\n if(abs(i-k)+abs(j-l)==1){\n int a=X[i][j],b=X[k][l];\n auto[x,y]=at[a];\n auto[z,w]=at[b];\n if(abs(x-z)+abs(y-w)<N/2)return false;\n }\n }\n return true;\n}\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n vector A(N,vector<int>(N));\n rep(i,0,N)rep(j,0,N)cin>>A[i][j];\n vector ans(N,vector<int>(N,-1));\n if(N%2==0){\n rep(i,0,N){\n rep(j,0,N){\n if(i%2==0){\n if(j%2==0){\n ans[i/2][j]=A[i][j];\n }else{\n ans[i/2+N/2][j]=A[i][j];\n }\n }else{\n if(j%2==0){\n ans[(N%4==0?i/2+N/2:i/2)][(j+N/2)%N]=A[i][j];\n }else{\n ans[(N%4!=0?i/2+N/2:i/2)][(j+N/2)%N]=A[i][j];\n }\n }\n }\n }\n }else{\n rep(i,0,N){\n rep(j,0,N){\n if(j%2==1)ans[i][j/2+(N+1)/2]=A[i][j];\n else ans[i][j/2]=A[i][j];\n }\n }\n vector<vector<int>>X(N);\n for(int i=0;i<N;i+=2)X[i/2]=ans[i];\n for(int i=1;i<N;i+=2)X[(N+1)/2+i/2]=ans[i];\n swap(ans,X);\n }\n check(N,A,ans);\n rep(i,0,N){\n rep(j,0,N)cout<<ans[i][j]<<\" \";\n cout<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3840, "score_of_the_acc": -2, "final_rank": 4 }, { "submission_id": "aoj_1666_9349263", "code_snippet": "#include <bits/extc++.h>\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\ntypedef vector<int> vi;\n\nbool solve();\n\nint main() {\n\twhile (solve());\n\treturn 0;\n}\n\nbool solve() {\n\tint n;\n\tcin >> n;\n\tif (n == 0) return false;\n\tvector<vi> a(n, vi(n));\n\trep(i, n) rep(j, n) cin >> a[i][j];\n\n\tfor (int i = 1; i < n; i += 2) {\n for (int j = 0; j < n; j += 2) cout << a[i][j] << \" \";\n for (int j = 1; j < n; j += 2) cout << a[n-i-1][j] << \" \\n\"[j + 2 >= n];\n\t}\n for (int i = 0; i < n; i += 2) {\n for (int j = 0; j < n; j += 2) cout << a[i][j] << \" \";\n for (int j = 1; j < n; j += 2) cout << a[n-i-1][j] << \" \\n\"[j + 2 >= n];\n }\n\n\treturn true;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1666_9251362", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int i = 0;i < (int)n;i++)\n\nvector<vector<ll>> clc(vector<vector<ll>> a){\n ll n = a.size();\n vector<vector<ll>> ret;\n rep(i,n){\n vector<ll> gu,ki;\n rep(j,n){\n if(j % 2 == 0){\n gu.push_back(a[i][j]);\n\n }else{\n ki.push_back(a[i][j]);\n }\n }\n reverse(gu.begin(),gu.end());\n reverse(ki.begin(),ki.end());\n vector<ll> now; \n for(auto & p:gu){\n now.push_back(p);\n }\n for(auto &p:ki){\n now.push_back(p);\n }\n ret.push_back(now);\n\n }\n return ret;\n}\n\nvector<vector<ll>> tenti(vector<vector<ll>> &a){\n ll n = a.size();\n vector<vector<ll>> b(n,vector<ll>(n));\n rep(i,n){\n rep(j,n){\n b[j][i] = a[i][j];\n }\n }\n return b;\n}\n\nvoid solve(ll n,vector<vector<ll>> x){\n \n x = clc(x);\n x = tenti(x);\n x = clc(x);\n rep(i,n){\n rep(j,n){\n cout << x[i][j] << \" \" ;\n }\n cout << endl;\n }\n\n\n}\n\n\nint main(){\n while(true){\n ll n;cin >> n;\n vector<vector<ll>> x(n,vector<ll>(n));\n rep(i,n){\n rep(j,n){\n cin >> x[i][j];\n }\n }\n if(n == 0)break;\n solve(n,x);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3496, "score_of_the_acc": -0.4724, "final_rank": 2 }, { "submission_id": "aoj_1666_9105404", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n\nvoid test(vector<vector<int>> &x) {\n map<int, pair<int, int>> mp;\n rep(i, x.size()) rep(j, x.size()) {\n mp[x[i][j]] = { i, j };\n }\n vector Grid(x.size(), vector<int>(x.size()));\n int idx = 0;\n rep(i, x.size()) rep(j, x.size()) {\n Grid[i][j] = ++idx;\n }\n rep(i, x.size() - 1) rep(j, x.size() - 1) {\n auto [i1, j1] = mp[Grid[i][j]];\n if (i > 0) {\n auto [i2, j2] = mp[Grid[i - 1][j]];\n if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {\n cerr << Grid[i][j] << \" \" << Grid[i - 1][j] << endl;\n exit(0);\n }\n }\n if (j > 0) {\n auto [i2, j2] = mp[Grid[i][j - 1]];\n if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {\n cerr << Grid[i][j] << \" \" << Grid[i][j - 1] << endl;\n exit(0);\n }\n }\n if (i + 1 < x.size()) {\n auto [i2, j2] = mp[Grid[i + 1][j]];\n if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {\n cerr << Grid[i][j] << \" \" << Grid[i + 1][j] << endl;\n exit(0);\n }\n }\n if (j + 1 < x.size()) {\n auto [i2, j2] = mp[Grid[i + 1][j]];\n if (abs(i1 - i2) + abs(j1 - j2) < x.size() / 2) {\n cerr << Grid[i][j] << \" \" << Grid[i][j + 1] << endl;\n exit(0);\n }\n }\n }\n}\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0)\n break;\n vector Grid(n, vector<int>(n));\n rep(i, n) rep(j, n) cin >> Grid[i][j];\n if (n == 2 || n == 3) {\n rep(i, n) rep(j, n) {\n cout << Grid[i][j] << \" \\n\"[j == n - 1];\n }\n continue;\n }\n\n map<int, int> mp;\n int idx = 0;\n rep(i, n) rep(j, n) {\n mp[idx] = Grid[i][j];\n Grid[i][j] = idx++;\n }\n bool isOdd = n % 2;\n if (isOdd)\n n--;\n int mid = n / 2;\n vector nGrid = Grid;\n rep(i, n) rep(j, n) {\n if ((i + j) % 2 == 0) {\n nGrid[i][j] = Grid[(i + mid) % n][(j + mid) % n];\n }\n }\n swap(nGrid, Grid);\n if (isOdd) {\n n++;\n vector<int> num(0);\n for (int i = 0; i < n; i += 2) {\n num.push_back(Grid[i][n - 1]);\n }\n for (int i = n - 3; i >= 0; i -= 2) {\n num.push_back(Grid[n - 1][i]);\n }\n int idx = 0;\n for (int i = 0; i < n; i += 2) {\n Grid[i][n - 1] = num[(idx + n / 2) % num.size()];\n idx++;\n }\n for (int i = n - 3; i >= 0; i -= 2) {\n Grid[n - 1][i] = num[(idx + n / 2) % num.size()];\n idx++;\n }\n }\n rep(i, n) rep(j, n) {\n cout << mp[Grid[i][j]] << \" \\n\"[j == n - 1];\n // cout << Grid[i][j] << \" \\n\"[j == n - 1];\n }\n // test(Grid);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3564, "score_of_the_acc": -0.5767, "final_rank": 3 } ]
aoj_1661_cpp
Problem F Trim It Step by Step The intelligence agency of the Kingdom uses a unique encryption method; a nonempty message consisting only of lowercase letters is encrypted into an expression <expr> complying with the grammar described by the following BNF. <expr> ::= <char> | <expr><expr> | ?( <expr> ) <char> ::= a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z The original message before encryption is the string appearing first in the lexicographic order among the possible results of applying the procedure described below to the encrypted message. Procedure: Repeat the following operation while ? remains in the expression. Choose any occurrence of a contiguous substring of the form ?( s ) (possibly the whole expression) such that s does not contain any symbols ? , ( , or ) . If s is empty, simply remove the chosen occurrence of ?() . Otherwise, replace the chosen occurrence of ?( s ) with the string obtained from s by deleting either the first or the last character. For example, consider the expression ?(?(icp)c) . Then, ?(icp) is the only occurrence that can be chosen, and this part is replaced with either cp or ic . Thus, the whole expression becomes either ?(cpc) or ?(icc) . After that, by choosing the whole expressions and replace them, there are four possible results, pc , cp , cc , and ic . The original message is cc , which appears first in the lexicographic order among them. You, an intelligence agent of the Republic, hostile to the Kingdom, have succeeded in obtaining an encrypted message, that is, an expression described above. Find the original message. Input The input consists of multiple datasets. The first line of the input consists only of the number n of the datasets, where n does not exceed 50. The following n lines give the datasets, one in each line. Each dataset is given in a single line consisting only of an expression <expr> complying with the grammar described by the BNF in the problem statement, and the expression consists of at most 1000 characters including symbols. Output For each dataset, output in a line the string appearing first in the lexicographic order among the possible results of applying to it the procedure in the problem statement. It is guaranteed that the correct output is never an empty string. Sample Input 6 ?(icpc) ?(?(icpc)) ?(ic)?(pc) ?(?(icp)c) ?(i?(?(c))pc) ?(bca)?(?(bca)) Output for the Sample Input cpc cp cc cc ip bca
[ { "submission_id": "aoj_1661_10483298", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nvoid solve() {\n string s; cin >> s;\n int n = ssize(s);\n vector<int> closing(n, -1);\n stack<int> stk;\n for(int i = 0; i < n; i++) {\n if(s[i] == '?') stk.push(i);\n else if(s[i] == ')') {\n closing[stk.top()] = i;\n stk.pop();\n }\n }\n assert(stk.empty());\n auto expr = [&](auto&& self, int l, int r) -> vector<string> {\n vector<string> dp(1);\n auto merge = [&](const vector<string>& dpr) {\n vector<string> res(ssize(dp) + ssize(dpr) - 1);\n for(int i = 0; i < ssize(dp)-1; i++) {\n res[i] = dp[i] + dpr[0];\n }\n for(int i = ssize(dp)-1; i < ssize(res); i++) {\n res[i] = dpr[i-(ssize(dp)-1)];\n }\n dp.swap(res);\n };\n auto eraseone = [&](const vector<string>& cur) -> vector<string> {\n if(ssize(cur) == 1) return cur;\n vector<string> res(ssize(cur)-1);\n for(int i = 0; i < ssize(cur)-1; i++) {\n res[i] = min(cur[i+1], cur[i].substr(0, ssize(cur[i])-1));\n }\n return res;\n };\n for(int i = l; i < r; i++) {\n if(s[i] == '?') {\n vector<string> tmp = self(self, i+2, closing[i]);\n merge(eraseone(tmp));\n i = closing[i];\n } else {\n merge(vector<string>{\"\"s + s[i], \"\"s});\n }\n }\n return dp;\n };\n cout << expr(expr, 0, n)[0] << '\\n';\n}\n\nint main() {\n int t; cin >> t;\n while(t--) solve();\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 5248, "score_of_the_acc": -0.0601, "final_rank": 4 }, { "submission_id": "aoj_1661_9552168", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstring S;\nint N;\nchar CINF = 'z'+1;\n\nvoid Trim(vector<string>& A) {\n int L = A.size();\n if (L <= 1) {\n A = {};\n return;\n }\n string SINF = \"\";\n rep(i,0,L) SINF += CINF;\n vector<string> Ret(L-1,SINF);\n rep(i,0,L) {\n if (i != 0) chmin(Ret[i-1],A[i].substr(1,L-1));\n if (i != L-1) chmin(Ret[i], A[i].substr(0,L-1));\n }\n A = Ret;\n return;\n}\n\nvoid Join(vector<string>& A, vector<string> B) {\n int L = A.size(), M = B.size();\n if (L == 0) {\n A = B;\n return;\n }\n if (M == 0) {\n return;\n }\n vector<string> Ret(L+M);\n string BMIN = B[0];\n rep(i,1,M) chmin(BMIN, B[i]);\n rep(i,0,L) Ret[i] = A[i] + BMIN;\n string Temp = \"\";\n rep(i,0,L) Temp += CINF;\n rep(i,0,M) Ret[i+L] = Temp + B[i];\n A = Ret;\n return;\n}\n\nint main() {\nint _;\ncin >> _;\nrep(__,0,_) {\n cin >> S;\n N = S.size();\n vector<vector<string>> DP(N);\n int Cur = 0;\n rep(i,0,N) {\n if (S[i] == '?') {\n i++;\n Cur++;\n }\n else if (S[i] == ')') {\n vector<string> A = DP[Cur];\n DP[Cur] = {};\n Cur--;\n Trim(A);\n Join(DP[Cur],A);\n }\n else {\n Join(DP[Cur],{S.substr(i,1)});\n }\n }\n cout << DP[0][0] << endl;\n}\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 6864, "score_of_the_acc": -0.1741, "final_rank": 10 }, { "submission_id": "aoj_1661_9448187", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nint main(){\n ll _;cin>>_;\n while(_--){\n string S;cin>>S;\n ll N=sz(S);\n vector<vector<vector<string>>>DP(N+1,vector<vector<string>>(N+1));\n vi right(N);\n {\n stack<ll>st;\n REP(i,N){\n if(S[i]=='(')st.emplace(i);\n if(S[i]==')'){right[st.top()]=i;st.pop();}\n }\n }\n function<void(ll,ll)>f=[&](ll l,ll r){\n if(S[l]=='?'&&right[l+1]==r-1){\n f(l+2,r-1);\n ll L=sz(DP[l+2][r-1]);\n if(!L)return;\n DP[l][r].resize(L-1);\n REP(i,L-1)DP[l][r][i]=min(DP[l+2][r-1][i+1],DP[l+2][r-1][i].substr(0,sz(DP[l+2][r-1][i])-1));\n return;\n }\n vector<pi>P;\n FOR(i,l,r){\n if(S[i]=='?'){\n ll j=right[i+1];\n f(i,j+1);\n P.emplace_back(pi(i,j+1));\n i=j;\n }\n else{\n ll j=i;\n while(i<r&&S[i]!='?')i++;\n DP[j][i].resize(i-j);\n DP[j][i][0]=S.substr(j,i-j);\n FOR(k,1,i-j)DP[j][i][k]=S.substr(j+k,i-j-k);\n P.emplace_back(pi(j,i));\n i--;\n }\n }\n ll L=0;\n for(auto[i,j]:P)L+=sz(DP[i][j]);\n DP[l][r].resize(L);\n string s;\n RREP(i,sz(P)){\n ll x=P[i].F,y=P[i].S;\n FOR(j,L-sz(DP[x][y]),L)DP[l][r][j]=DP[x][y][j-L+sz(DP[x][y])]+s;\n if(sz(DP[x][y]))s=DP[x][y][0]+s;\n L-=sz(DP[x][y]);\n }\n };\n f(0,N);\n cout<<DP[0][N][0]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 54236, "score_of_the_acc": -1.0197, "final_rank": 13 }, { "submission_id": "aoj_1661_9416433", "code_snippet": "/// 生成AI不使用 / Not using generative AI\n\n// #pragma GCC target (\"avx\")\n// #pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#ifdef MARC_LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n\n#ifdef ATCODER\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n//#define int long long\nusing ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vvvvll = vector<vvvll>;\nusing vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;\nusing ld = long double; using vld = vector<ld>; using vvld = vector<vld>; using vd = vector<double>;\nusing vc = vector<char>; using vvc = vector<vc>; using vs = vector<string>;\nusing vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;\nusing pii = pair<int, int>; using pcc = pair<char, char>; using pll = pair<ll, ll>; using pli = pair<ll, int>; using pdd = pair<double, double>; using pldld = pair<ld,ld>;\nusing vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpldld = vector<pldld>;\nusing ui = unsigned int; using ull = unsigned long long;\nusing i128 = __int128; using f128 = __float128;\ntemplate<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type> > name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type> > > name(h, vector<vector<type> >(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) vector<vector<vector<vector<type> > > > name(a, vector<vector<vector<type> > >(b, vector<vector<type> >(c, vector<type>(__VA_ARGS__))))\n\n#define overload4(a,b,c,d,name,...) name\n#define rep1(n) for(ll i = 0; i < (ll)n; i++)\n#define rep2(i,n) for(ll i = 0; i < (ll)n; i++)\n#define rep3(i,a,b) for (ll i = a; i < (ll)b; i++)\n#define rep4(i,a,b,c) for (ll i = a; i < (ll)b; i += (ll)c)\n#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)\n#define repback1(n) for (ll i = (ll)n-1; i >= 0; i--)\n#define repback2(i,n) for (ll i = (ll)n-1; i >= 0; i--)\n#define repback3(i,a,b) for (ll i = (ll)b-1; i >= (ll)a; i--)\n#define repback4(i,a,b,c) for (ll i = (ll)b-1; i >= (ll)a; i -= (ll)c)\n#define repback(...) overload4(__VA_ARGS__,repback4,repback3,repback2,repback1)(__VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\n#define include(y, x, H, W) (0 <= (y) && (y) < (H) && 0 <= (x) && (x) < (W))\n#define inrange(x, down, up) ((down) <= (x) && (x) <= (up))\n#define pb push_back\n#define eb emplace_back\n#define fir first\n#define sec second\n#define TIMER_START TIME_START = clock()\n#ifdef MARC_LOCAL\n// https://trap.jp/post/1224/\n#define debug1(x) cout << \"debug L\" << __LINE__ <<\": \" << (#x) << \": \" << (x) << endl\n#define debug2(x, y) cout << \"debug L\" << __LINE__ <<\": \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << endl\n#define debug3(x, y, z) cout << \"debug L\" << __LINE__ <<\": \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << endl\n#define debug4(x, y, z, w) cout << \"debug L\" << __LINE__ <<\": \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << endl\n#define debug5(x, y, z, w, v) cout << \"debug L\" << __LINE__ <<\": \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << \", \" << (#v) << \": \" << (v) << endl\n#define debug6(x, y, z, w, v, u) cout << \"debug L\" << __LINE__ <<\": \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << \", \" << (#v) << \": \" << (v) << \", \" << (#u) << \": \" << (u) << endl\n#define overload6(a, b, c, d, e, f, g,...) g\n#define debug(...) overload6(__VA_ARGS__, debug6, debug5, debug4, debug3, debug2, debug1)(__VA_ARGS__)\n#define debuga cerr << __LINE__ << endl\n#define debugnl cout << endl\n#define Case(i) cout << \"Case #\" << (i) << \": \"\n#define TIMECHECK cerr << 1000.0 * static_cast<double>(clock() - TIME_START) / CLOCKS_PER_SEC << \"ms\" << endl\n#define LOCAL 1\n#else\n#define debug1(x) void(0)\n#define debug2(x, y) void(0)\n#define debug3(x, y, z) void(0)\n#define debug4(x, y, z, w) void(0)\n#define debug5(x, y, z, w, v) void(0)\n#define debug6(x, y, z, w, v, u) void(0)\n#define debug(...) void(0)\n#define debuga void(0)\n#define debugnl void(0)\n#define Case(i) void(0)\n#define TIMECHECK void(0)\n#define LOCAL 0\n#endif\n\n//mt19937_64 rng(0);\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nclock_t TIME_START;\nconst long double pi = 3.141592653589793238462643383279L;\nconst long long INFL = 1000000000000000000ll;\nconst long long INFLMAX = numeric_limits< long long >::max(); // 9223372036854775807\nconst int INF = 1000000000;\nconst int INFMAX = numeric_limits< int >::max(); // 2147483647\nconst long double INFD = numeric_limits<ld>::infinity();\nconst long double EPS = 1e-10;\nconst int mod1 = 1000000007;\nconst int mod2 = 998244353;\nconst vi dx1 = {1,0,-1,0};\nconst vi dy1 = {0,1,0,-1};\nconst vi dx2 = {0, 1, 1, 1, 0, -1, -1, -1};\nconst vi dy2 = {1, 1, 0, -1, -1, -1, 0, 1};\nconstexpr char nl = '\\n';\nconstexpr char bl = ' ';\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) {__uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do {--d;*d = \"0123456789\"[tmp % 10];tmp /= 10;} while (tmp != 0);if (value < 0) {--d;*d = '-';}int len = std::end(buffer) - d;if (dest.rdbuf()->sputn(d, len) != len) {dest.setstate(std::ios_base::badbit);}}return dest; }\n__int128 parse(string &s) { __int128 ret = 0; for (int i = 0; i < (int)s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } // for __int128 (https://kenkoooo.hatenablog.com/entry/2016/11/30/163533)\ntemplate<class T> ostream& operator << (ostream& os, vector<T>& vec) { os << \"[\"; for (int i = 0; i<(int)vec.size(); i++) { os << vec[i] << (i + 1 == (int)vec.size() ? \"\" : \", \"); } os << \"]\"; return os; } /// vector 出力\ntemplate<class T, class U> ostream& operator << (ostream& os, pair<T, U>& pair_var) { os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\"; return os; } // pair 出力\ntemplate<class T, class U> ostream& operator << (ostream& os, map<T, U>& map_var) { os << \"{\"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << \"(\" << itr->first << \", \" << itr->second << \")\"; itr++; if(itr != map_var.end()) os << \", \"; itr--; } os << \"}\"; return os; } // map出力\ntemplate<class T> ostream& operator << (ostream& os, set<T>& set_var) { os << \"{\"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if(itr != set_var.end()) os << \", \"; itr--; } os << \"}\"; return os; } /// set 出力\n\nbool equals(long double a, long double b) { return fabsl(a - b) < EPS; }\ntemplate<class T> int sign(T a) { return equals(a, 0) ? 0 : a > 0 ? 1 : -1; }\nlong double radian_to_degree(long double r) { return (r * 180.0 / pi); }\nlong double degree_to_radian(long double d) { return (d * pi / 180.0); }\n\nint popcnt(unsigned long long a){ return __builtin_popcountll(a); } // ll は 64bit対応!\nint MSB1(unsigned long long x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // MSB(1), 0-indexed ( (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) )\nint LSB1(unsigned long long x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } // LSB(1), 0-indexed ( (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) )\nlong long maskbit(int n) { return (1LL << n) - 1; }\nbool bit_is1(long long x, int i) { return ((x>>i) & 1); }\nstring charrep(int n, char c) { return std::string(n, c); }\ntemplate<class T> T square(T x) { return (x) * (x); }\ntemplate<class T>void UNIQUE(T& A) {sort(all(A)); A.erase(unique(all(A)), A.end());}\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\nvoid YESNO(bool b) {if(b){cout<<\"YES\"<<'\\n';} else{cout<<\"NO\"<<'\\n';}} void YES() {YESNO(true);} void NO() {YESNO(false);}\nvoid yesno(bool b) {if(b){cout<<\"yes\"<<'\\n';} else{cout<<\"no\"<<'\\n';}} void yes() {yesno(true);} void no() {yesno(false);}\nvoid YesNo(bool b) {if(b){cout<<\"Yes\"<<'\\n';} else{cout<<\"No\"<<'\\n';}} void Yes() {YesNo(true);} void No() {YesNo(false);}\nvoid POSIMPOS(bool b) {if(b){cout<<\"POSSIBLE\"<<'\\n';} else{cout<<\"IMPOSSIBLE\"<<'\\n';}}\nvoid PosImpos(bool b) {if(b){cout<<\"Possible\"<<'\\n';} else{cout<<\"Impossible\"<<'\\n';}}\nvoid posimpos(bool b) {if(b){cout<<\"possible\"<<'\\n';} else{cout<<\"impossible\"<<'\\n';}}\nvoid FIRSEC(bool b) {if(b){cout<<\"FIRST\"<<'\\n';} else{cout<<\"SECOND\"<<'\\n';}}\nvoid firsec(bool b) {if(b){cout<<\"first\"<<'\\n';} else{cout<<\"second\"<<'\\n';}}\nvoid FirSec(bool b) {if(b){cout<<\"First\"<<'\\n';} else{cout<<\"Second\"<<'\\n';}}\nvoid AliBob(bool b) {if(b){cout<<\"Alice\"<<'\\n';} else{cout<<\"Bob\"<<'\\n';}}\nvoid TakAok(bool b) {if(b){cout<<\"Takahashi\"<<'\\n';} else{cout<<\"Aoki\"<<'\\n';}}\nint GetTime() {return 1000.0*static_cast<double>(clock() - TIME_START) / CLOCKS_PER_SEC;}\nll myRand(ll B) {return (unsigned long long)rng() % B;}\ntemplate<class T> void print(const T& x, const char endch = '\\n') { cout << x << endch; }\n\ntemplate<class T> T ceil_div(T x, T y) { assert(y); return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate<class T> T floor_div(T x, T y) { assert(y); return (x > 0 ? x / y : (x - y + 1) / y); }\ntemplate<class T> pair<T, T> divmod(T x, T y) { T q = floor_div(x, y); return {q, x - q * y}; } /// (q, r) s.t. x = q*y + r \nll GCD(ll a, ll b) { if(a < b) swap(a, b); if(b == 0) return a; if(a%b == 0) return b; else return GCD(b, a%b); }\nll LCM(ll a, ll b) { assert(GCD(a,b) != 0); return a / GCD(a, b) * b; }\nll MOD(ll &x, const ll P) { ll ret = x%P; if(ret < 0) ret += P; return x = ret; } /// x % P を非負整数に直す\nll mpow(ll x, ll n, const ll mod) { x %= mod; ll ret = 1; while(n > 0) { if(n & 1) ret = ret * x % mod; x = x * x % mod; n >>= 1; } return ret; } /// x^n % mod を計算\nll lpow(ll x, ll n) { ll ret = 1; while(n > 0){ if(n & 1) ret = ret * x; x = x * x; n >>= 1; } return ret; } /// x^nを計算\nstring toBinary(ll n) { if(n == 0) return \"0\"; assert(n > 0); string ret; while (n != 0){ ret += ( (n & 1) == 1 ? '1' : '0' ); n >>= 1; } reverse(ret.begin(), ret.end()); return ret; } /// 10進数(long long) -> 2進数(string)への変換\nll toDecimal(string S) { ll ret = 0; for(int i = 0; i < (int)S.size(); i++){ ret *= 2LL; if(S[i] == '1') ret += 1; } return ret; } /// 2進数(string) → 10進数(long long)への変換\nint ceil_pow2(ll n) { int x = 0; while ((1ll << x) < n) x++; return x;} /// return minimum non-negative `x` s.t. `n <= 2**x`\nint floor_pow2(ll n) { int x = 0; while ((1ll << (x+1)) <= n) x++; return x;} /// return maximum non-negative `x` s.t. `n >= 2**x`\n\n\n\n// ############################\n// # #\n// # C O D E S T A R T #\n// # #\n// ############################\n\ntypedef string::const_iterator State;\n\n\n/// ParseErrorが発生したときのエラー情報用\n// TODO : 使いやすいよう設定し直す\nclass ParseError : public std::exception {\n public:\n // デフォルトコンストラクタ\n ParseError() : message(\"Parse error\") {}\n\n // コンストラクタ:カスタムエラーメッセージを持つ\n ParseError(const std::string& msg, int line = -1, int column = -1)\n : message(msg), line(line), column(column) {\n buildFullMessage();\n }\n\n // コンストラクタ:行と列情報を含む\n ParseError(int line, int column)\n : message(\"Parse error\"), line(line), column(column) {\n buildFullMessage();\n }\n\n // エラーメッセージを取得するメンバ関数\n const char* what() const noexcept override {\n return fullMessage.c_str();\n }\n\n // 行番号を取得するメンバ関数\n int getLine() const {\n return line;\n }\n\n // 列番号を取得するメンバ関数\n int getColumn() const {\n return column;\n }\n\n private:\n std::string message;\n std::string fullMessage;\n int line;\n int column;\n\n // エラーメッセージを構築するプライベート関数\n void buildFullMessage() {\n std::ostringstream oss;\n oss << message;\n if (line >= 0) {\n oss << \" at line \" << line;\n }\n if (column >= 0) {\n oss << \", column \" << column;\n }\n fullMessage = oss.str();\n }\n};\n\n\n/// begin が 期待される文字 expectedを指していたらbeginを一つ進める(期待する文字でなければエラー発生)\n// assertのようなイメージ、デバッグ用(使用例: 式の最後に=が来るのであれば、consume(begin, '=')として最後まで到達してることを確認できる)\nvoid consume(State& begin, const char expected) {\n if (*begin == expected) {\n begin++;\n }\n else {\n cerr << \"Expected char is '\" << expected << \"' but got '\" << *begin << \"'\" << endl;\n cerr << \"Rest string is '\";\n while (*begin) {\n cerr << *begin++;\n }\n cerr << \"'\" << endl;\n throw ParseError();\n }\n}\n\nstruct Node {\n int lch = -1; // 左の子のidx \n int rch = -1; // 右の子のidx\n int type; // どの生成規則を適用したか? (連接: 0, 削減: 1, 文字列: 2)\n int len; // そのノードから生成される文字列の最終的な長さ\n string S; // 葉の場合のみ使用\n};\n\nvector<Node> tree; // 構文木 -> 構文木上でdpすることを意識\n\n// chomsky 標準形的な感じで、\n// ・expr1 -> expr2 expr1 | expr2\n// ・expr2 -> char | ?(expr1)\n// ・char -> [a~z]*\n// と書き換え\n\nint expr2(State& it);\nint expr1(State& it) {\n int idx = expr2(it);\n \n while(*it != ')' && *it != '#') {\n Node node;\n node.lch = idx;\n node.rch = expr1(it);\n node.len = tree[node.lch].len + tree[node.rch].len;\n node.type = 0;\n\n idx = tree.size();\n tree.eb(node);\n }\n\n return idx;\n}\n\nstring str(State& it);\nint expr2(State& it) {\n int idx;\n\n if(*it == '?') {\n consume(it, '?');\n consume(it, '(');\n\n Node node;\n node.lch = expr1(it);\n node.len = max(0,tree[node.lch].len - 1); // 削られるので引く必要\n node.type = 1;\n\n idx = tree.size();\n tree.eb(node);\n consume(it, ')');\n }\n else {\n Node node;\n node.S = str(it);\n node.len = node.S.size();\n node.type = 2;\n\n idx = tree.size();\n tree.eb(node);\n }\n\n return idx;\n}\n\nstring str(State& it) {\n string ret;\n while(*it != '?' && *it != ')' && *it != '#') {\n ret += *it;\n it++;\n }\n return ret;\n}\n\n\n\nvoid solve() {\n tree.clear();\n string S; cin >> S;\n S += '#';\n State it = S.begin();\n\n int root = expr1(it);\n int M = tree.size();\n\n vector<map<int,string> > dp(M); // dp[i][j]:= 頂点iを根とする部分木に関して、その祖先によって左からj文字削られている時の、辞書順最小の文字列 -> 右から削られている場合に関しては、prefixを取れば良い\n auto dfs = [&](auto dfs, int v, int l)->string{\n string ret = \"\";\n if(dp[v].find(l) != dp[v].end()) {\n return dp[v][l];\n }\n\n\n if(tree[v].type == 0) {\n int lidx = tree[v].lch;\n int ridx = tree[v].rch;\n\n if(tree[lidx].len > l) {\n ret = dfs(dfs, lidx, l) + dfs(dfs, ridx, 0);\n }\n else {\n ret = dfs(dfs, ridx, l-tree[lidx].len);\n }\n }\n else if(tree[v].type == 1) {\n int lidx = tree[v].lch;\n // 右消去\n ret = dfs(dfs, lidx, l);\n if(ret.size()) ret.pop_back();\n\n // 左消去\n chmin(ret, dfs(dfs, lidx, l+1));\n }\n else {\n if(tree[v].len <= l) {\n ret = \"\";\n }\n else {\n ret = tree[v].S.substr(l);\n }\n }\n\n return dp[v][l] = ret;\n };\n debug(dp);\n\n cout << dfs(dfs, root, 0) << nl;\n\n}\n\n\n\nsigned main() {\n cin.tie(0); ios_base::sync_with_stdio(false);\n TIMER_START;\n //cout << fixed << setprecision(15);\n \n\n int tt = 1;\n cin >> tt;\n while(tt--){\n solve();\n }\n\n TIMECHECK;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7964, "score_of_the_acc": -0.0717, "final_rank": 7 }, { "submission_id": "aoj_1661_9402505", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n auto operate = [](vector<string>& dp) {\n int n = dp.size();\n if (n == 0) return;\n int k = dp[0].size();\n if (k == 0) return;\n // dp[i]:=前からi文字消すことになった時のmin (予定調和dp)\n for (int i = 0; i < n; i++) {\n dp[i] = dp[i].substr(0, k - 1);\n if (i < n - 1) dp[i] = min(dp[i], dp[i + 1].substr(1));\n }\n dp.pop_back();\n };\n auto merge = [](vector<string>& l, vector<string> r) {\n int L = l.size(), R = r.size();\n if (R == 0) return;\n if (L == 0) return swap(l, r);\n string min_r = *min_element(r.begin(), r.end());\n int k = l[0].size();\n for (int i = 0; i < L; i++) l[i] += min_r;\n // 先頭からk文字消さないとペナルティINF\n for (int i = 0; i < R; i++) l.push_back(string(k, 'z' + 1) + r[i]);\n return;\n };\n string s;\n cin >> s;\n vector<vector<string>> dp(s.size());\n int cur = 0;\n for (char c : s) {\n if (c == '?') continue;\n if (c == '(') {\n cur++;\n } else if (c == ')') {\n operate(dp[cur]);\n merge(dp[cur - 1], dp[cur]);\n dp[cur].clear();\n cur--;\n } else {\n merge(dp[cur], {string(1, c)});\n }\n }\n cout << *min_element(dp[0].begin(), dp[0].end()) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t;\n cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 6508, "score_of_the_acc": -0.0674, "final_rank": 5 }, { "submission_id": "aoj_1661_9402336", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n auto operate = [](vector<string>& dp) {\n int n = dp.size();\n // 消し方がn通り\n if (n == 0) return; // 空文字列なので\n int k = dp[0].size();\n if (k == 1) return dp.clear(); // 空文字列になるので\n // dp\n // if 必ず空文字列 : empty\n // otherwise : dp[i]:=前からi文字消すことになった時のmin\n for (int i = 0; i < n; i++) {\n dp[i] = dp[i].substr(0, k - 1);\n if (i < n - 1) dp[i] = min(dp[i], dp[i + 1].substr(1));\n }\n dp.pop_back();\n };\n auto merge = [](vector<string>& l, vector<string> r) {\n int L = l.size(), R = r.size();\n if (R == 0) return;\n if (L == 0) return swap(l, r);\n string min_r = *min_element(r.begin(), r.end());\n int k = l[0].size();\n for (int i = 0; i < L; i++) l[i] += min_r;\n // 先頭から消さないといけないことにする\n for (int i = 0; i < R; i++) l.push_back(string(k, 'z' + 1) + r[i]);\n return;\n };\n string s;\n cin >> s;\n vector<vector<string>> dp(s.size());\n int cur = 0;\n for (char c : s) {\n if (c == '?') continue;\n if (c == '(') {\n cur++;\n } else if (c == ')') {\n operate(dp[cur]);\n merge(dp[cur - 1], dp[cur]);\n dp[cur].clear();\n cur--;\n } else {\n merge(dp[cur], {string(1, c)});\n }\n }\n cout << *min_element(dp[0].begin(), dp[0].end()) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t;\n cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 6592, "score_of_the_acc": -0.0723, "final_rank": 8 }, { "submission_id": "aoj_1661_9402333", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n auto operate = [](vector<string>& dp) {\n int n = dp.size();\n // 消し方がn通り\n if (n == 0) return; // 空文字列なので\n int k = dp[0].size();\n if (k == 1) return dp.clear(); // 空文字列になるので\n // dp\n // if 必ず空文字列 : empty\n // otherwise : dp[i]:=前からi文字消すことになった時のmin\n for (int i = 0; i < n; i++) {\n dp[i] = dp[i].substr(0, k - 1);\n if (i < n - 1) dp[i] = min(dp[i], dp[i + 1].substr(1));\n }\n dp.pop_back();\n };\n auto merge = [](vector<string>& l, vector<string> r) {\n int L = l.size(), R = r.size();\n if (R == 0) return;\n if (L == 0) return swap(l, r);\n string min_r = *min_element(r.begin(), r.end());\n int k = l[0].size();\n for (int i = 0; i < L; i++) l[i] += min_r;\n // 先頭から消さないといけないことにする\n for (int i = 0; i < R; i++) l.push_back(string(L, 'z' + 1) + r[i]);\n return;\n };\n string s;\n cin >> s;\n vector<vector<string>> dp(s.size());\n int cur = 0;\n for (char c : s) {\n if (c == '?') continue;\n if (c == '(') {\n cur++;\n } else if (c == ')') {\n operate(dp[cur]);\n merge(dp[cur - 1], dp[cur]);\n dp[cur].clear();\n cur--;\n } else {\n merge(dp[cur], {string(1, c)});\n }\n }\n cout << *min_element(dp[0].begin(), dp[0].end()) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t;\n cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 6512, "score_of_the_acc": -0.0708, "final_rank": 6 }, { "submission_id": "aoj_1661_9350143", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n#define all(A) A.begin(),A.end()\ntemplate <class T>\nvoid chmax(T& p, T q) { p = max(p, q); };\ntemplate <class T>\nvoid chmin(T& p, T q) { p = min(p, q); };\n\nconst char INF = 'z' + 1;\nstring BASESTR=\"{\";\nvector<string> trim(vector<string>& S) {\n\n ll SN = S.size();\n if (SN <= 1)return {};\n ll N = S[0].size();\n BASESTR=\"\";\n rep(i,SN-1)BASESTR.push_back(INF);\n vector<string> R(SN - 1,BASESTR);\n rep(i, SN) {\n if(i!=0)chmin(R[i-1],S[i].substr(1, N - 1));\n if(i!=SN-1)chmin(R[i],S[i].substr(0, N - 1));\n }\n return R;\n}\n\nvector<string> merge(vector<string>& A, vector<string> B) {\n ll AN = A.size();\n ll BN = B.size();\n if (AN == 0) {\n A = B;\n return B;\n }\n if (BN == 0)return A;\n vector<string> R(AN + BN);\n string BM = B[0];\n rep(i, BN)chmin(BM, B[i]);\n rep(i, AN) {\n R[i] = A[i] + BM;\n }\n string BASE;\n rep(i, AN)BASE.push_back(INF);\n rep(i, BN) {\n R[i + AN] = BASE + B[i];\n }\n A = R;\n return R;\n}\n\nvoid solve(string S) {\n ll N = S.size();\n stack<vector<string>> ST;\n vector<vector<string>> STT(N);\n ll cnt = 0;\n rep(i, N) {\n if (S[i] == '?') {\n i++;\n cnt++;\n }\n else if (S[i] == ')') {\n auto A = STT[cnt];\n STT[cnt] = {};\n A = trim(A);\n cnt--;\n merge(STT[cnt], A);\n }\n else {\n string NW = \"\";\n NW.push_back(S[i]);\n merge(STT[cnt], { NW });\n }\n }\n auto AN = STT[0];\n for (auto v : AN)chmin(AN[0], v);\n cout << AN[0] << endl;\n}\n\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n ll N;\n cin >> N;\n rep(i, N) {\n string S;\n cin >> S;\n solve(S);\n }\n\n\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 6820, "score_of_the_acc": -0.1833, "final_rank": 11 }, { "submission_id": "aoj_1661_9099944", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nstring s;\nint n;\n\nvector<string> solve(int L, int R) { \n vector<string> ans = {};\n vector<int> stack;\n for(int i : rep(L, R)) {\n if(s[i] == '?') {\n\n } else if(s[i] == '(') {\n stack.push_back(i);\n } else if(s[i] == ')') {\n int p = stack.back() + 1; stack.pop_back();\n if(stack.empty()) {\n vector<string> a = solve(p, i);\n const int m = a.size();\n if(m <= 1) continue;\n vector<string> b(m - 1);\n for(int k : rep(m - 1)) {\n b[k] = min(a[k].substr(0, a[k].size() - 1), a[k + 1]);\n }\n for(string& x : ans) x += b[0];\n ans.insert(ans.end(), b.begin(), b.end());\n }\n } else {\n if(stack.empty()) {\n for(string& x : ans) x += s[i];\n ans.push_back(\"\"s + s[i]);\n }\n }\n }\n return ans;\n}\n\nint main() {\n int t = in();\n for(int t_ : rep(t)) {\n cin >> s;\n n = s.size();\n print(solve(0, n)[0]);\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4216, "score_of_the_acc": -0.0205, "final_rank": 2 }, { "submission_id": "aoj_1661_7974800", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n\nvoid debug_out() {\n std::cerr << std::endl;\n}\n\ntemplate <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cerr << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntypedef std::string::const_iterator State;\n\nbool expect(State &begin, char expected) {\n return *begin == expected;\n}\n\nvoid consume(State &begin, char expected) {\n assert(*begin == expected);\n begin++;\n}\n\nbool isdigit(char c) {\n return '0' <= c && c <= '9';\n}\n\nbool isAlpha(char c) {\n return 'A' <= c && c <= 'Z';\n}\n\nbool isalpha(char c) {\n return 'a' <= c && c <= 'z';\n}\n\nstruct Node {\n int lch = -1, rch = -1;\n int type; // 0: merged type, 1: ?() type, 2: string type;\n int len = 0;\n std::string s = \"\";\n};\n\nstd::vector<Node> tree;\n\nint Stmt(State &);\nint Expr(State &);\nint Char(State &);\n\nint Stmt(State &begin) {\n int now = Expr(begin);\n while (!expect(begin, ')') && !expect(begin, ';')) {\n Node node;\n node.type = 0;\n node.lch = now;\n node.rch = Expr(begin);\n node.len = tree[node.lch].len + tree[node.rch].len;\n now = tree.size();\n tree.emplace_back(node);\n }\n return now;\n}\n\nint Expr(State &begin) {\n if (expect(begin, '?')) {\n Node node;\n node.type = 1;\n consume(begin, '?');\n consume(begin, '(');\n node.lch = Stmt(begin);\n consume(begin, ')');\n int idx = tree.size();\n chmax(node.len, tree[node.lch].len - 1);\n tree.emplace_back(node);\n return idx;\n } else {\n return Char(begin);\n }\n}\n\nint Char(State &begin) {\n assert(isalpha(*begin));\n Node node;\n node.type = 2;\n while (isalpha(*begin)) {\n node.s += *begin;\n consume(begin, node.s.back());\n }\n node.len = node.s.size();\n int idx = tree.size();\n tree.emplace_back(node);\n return idx;\n}\n\nvoid solve() {\n tree.clear();\n std::string s;\n std::cin >> s;\n s += ';';\n State begin = s.begin();\n int root = Stmt(begin);\n consume(begin, ';');\n int n = tree.size();\n std::vector<std::map<int, std::string>> dp(n);\n auto update = [&](int idx, int l, const std::string &t) -> void {\n if(dp[idx].find(l) == dp[idx].end()) dp[idx][l] = t;\n else if(t < dp[idx][l]) dp[idx][l] = t;\n };\n auto dfs = [&](auto &&self, int v, int l) -> void {\n if(dp[v].find(l) != dp[v].end()) {\n return;\n }\n if(tree[v].type == 0) {\n int lch = tree[v].lch;\n int rch = tree[v].rch;\n assert(lch != -1 && rch != -1);\n if(l < tree[lch].len) {\n self(self, lch, l);\n self(self, rch, 0);\n dp[v][l] = dp[lch][l] + dp[rch][0];\n }\n else {\n int nl = l - tree[lch].len;\n self(self, rch, nl);\n dp[v][l] = dp[rch][nl];\n }\n }\n else if(tree[v].type == 1) {\n int nv = tree[v].lch;\n assert(tree[v].rch < 0);\n {\n self(self, nv, l);\n std::string t = dp[nv][l];\n if(!t.empty()) t.pop_back();\n update(v, l, t);\n }\n {\n self(self, nv, l+1);\n update(v, l, dp[nv][l+1]);\n }\n }\n else if(tree[v].type == 2) {\n if(int(tree[v].s.size()) <= l) {\n dp[v][l] = \"\";\n return;\n }\n dp[v][l] = tree[v].s.substr(l);\n return;\n }\n else {\n assert(0);\n }\n };\n dfs(dfs, root, 0);\n std::cout << dp[root][0] << '\\n';\n}\n\nint main() {\n int n;\n std::cin >> n;\n while (n--) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8420, "score_of_the_acc": -0.0803, "final_rank": 9 }, { "submission_id": "aoj_1661_7049782", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nchar S[1001];\n\nvector<string> dfs(int l, int r) {\n bool f = true;\n int cc = 1;\n for (int i=l+2; i<r-1; i++) {\n cc += (S[i] == '(') - (S[i] == ')');\n f &= cc > 0;\n }\n if (f && S[l] == '?' && S[r-1] == ')') {\n if (r-l <= 3) return {\"\"};\n auto p = dfs(l+2, r-1);\n for (int i=0; i+1<p.size(); i++) {\n p[i] = min(p[i].substr(0, p[i].size()-1), p[i+1]);\n }\n if (p.size()) p.pop_back();\n if (p.empty()) p.push_back(\"\");\n return p;\n }\n bool pure = true;\n for (int i=l; i<r; i++) pure &= !!islower(S[i]);\n if (pure) {\n vector<string> res;\n string t = \"\";\n for (int i=l; i<r; i++) t += S[i];\n for (int i=0; i<=r-l; i++) {\n res.push_back(t.substr(i, t.size()-i));\n }\n return res;\n }\n\n vector<string> res;\n auto update = [&](vector<string> &X) {\n if (res.empty()) {\n res = X;\n return;\n }\n\n vector<string> temp;\n\n for (auto &item : res) temp.push_back(item + X[0]);\n for (int i=0; i<X.size(); i++) {\n int t = i + res[0].size();\n// cerr << \"res[0]:\" << res[0] << \"+\" << X[i] << \"=>\" << t << \"(\" << temp[t] << \")\" << endl;\n if (t < temp.size()) {\n temp[t] = min(temp[t], X[i]);\n } else {\n assert(temp.size() == t);\n temp.push_back(X[i]);\n }\n }\n res = temp;\n };\n\n //split\n int sl = l, c = 0;\n for (int i=l; i<=r; i++) {\n if ((S[i] == '?' && c == 0 && sl < i) || (i == r && sl < i)) {\n auto r = dfs(sl, i);\n update(r);\n sl = i;\n } else if (S[i] == '(') {\n c++;\n } else if (S[i] == ')') {\n c--;\n if (c == 0) {\n auto r = dfs(sl, i+1);\n update(r);\n sl = i+1;\n }\n }\n }\n\n return res;\n}\n\nvoid solve() {\n scanf(\"%s\", S);\n int N = strlen(S);\n\n auto r = dfs(0, N);\n\n cout << r[0] << endl;\n}\n\nint main() {\n int T;\n scanf(\"%d\", &T);\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4188, "score_of_the_acc": -0.0133, "final_rank": 1 }, { "submission_id": "aoj_1661_6971742", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nmap<pair<int,pair<int,int>>,string> dp;\n\n// exprの初期位置,前から後ろから何文字取り除くか\nstring parse(string &s,int idx,int pr,int ba){\n // cerr << s << \" \" << idx << \" \" << pr << \" \" << ba << endl;\n if(dp.find({idx,{pr,ba}}) != dp.end()){\n return dp[{idx,{pr,ba}}];\n }\n if(s[idx] != '('){\n string res = \"\"; res += s[idx];\n if(pr+ba >= 1) res = \"\";\n return res;\n }\n // 前から取り除くか後ろから取り除くかの選択が求められる\n // 開始位置,個数を持つ\n // 0のは除いてしまって良さそう\n vector<pair<int,int>> v;\n int cnt = 0;\n // idx++;\n\n for(int i=idx+1;i<s.size();){\n if(s[i] != '(' and s[i] != ')'){\n v.push_back({i,1});\n i++;\n }\n else if(s[i] == '('){\n int j = i;\n cnt++;\n i++;\n vector<char> uuuu;\n uuuu.push_back('(');\n while(i<s.size()){\n if(s[i] == '('){\n uuuu.push_back('(');\n cnt++;\n }\n else if(s[i] == ')'){\n if(uuuu.back() != '('){\n uuuu.pop_back();\n }\n for(int j=uuuu.size()-1;j>=0;j--){\n if(uuuu[j] == '('){\n swap(uuuu[j],uuuu.back());\n uuuu.pop_back();\n break;\n }\n }\n cnt--;\n }\n else uuuu.push_back(s[i]);\n i++;\n if(cnt == 0) break;\n }\n if(uuuu.size() > 0){\n v.push_back({j,uuuu.size()});\n }\n }\n else if(s[i] == ')') break;\n }\n\n string r1,r2;\n\n int sum = 0;\n for(auto p:v){\n sum += p.second;\n }\n if(sum <= 1+pr+ba){\n dp[{idx,{pr,ba}}] = \"\";\n return \"\";\n }\n else{\n // 前から\n {\n pr++;\n string r = \"\";\n int P = pr, B = ba;\n for(int i=0;i<v.size();i++){\n if(v[i].second <= P){\n P -= v[i].second;\n }\n else{\n int j = v.size()-1;\n while(1){\n if(v[j].second <= B){\n B -= v[j].second;\n j--;\n }\n else break;\n }\n if(i == j){\n r += parse(s,v[i].first,P,B);\n }\n else{\n r += parse(s,v[i].first,P,0);\n for(int k=i+1;k<j;k++){\n r += parse(s,v[k].first,0,0);\n }\n r += parse(s,v[j].first,0,B);\n }\n break;\n }\n }\n r1 = r;\n pr--;\n }\n {\n ba++;\n string r = \"\";\n int P = pr, B = ba;\n for(int i=0;i<v.size();i++){\n if(v[i].second <= P){\n P -= v[i].second;\n }\n else{\n int j = v.size()-1;\n while(1){\n if(v[j].second <= B){\n B -= v[j].second;\n j--;\n }\n else break;\n }\n if(i == j){\n r += parse(s,v[i].first,P,B);\n }\n else{\n r += parse(s,v[i].first,P,0);\n for(int k=i+1;k<j;k++){\n r += parse(s,v[k].first,0,0);\n }\n r += parse(s,v[j].first,0,B);\n }\n break;\n }\n }\n r2 = r;\n ba--;\n }\n }\n string res = min(r1,r2);\n dp[{idx,{pr,ba}}] = res;\n return res;\n}\n\nstring solve(string s){\n dp.clear();\n return parse(s,0,0,0);\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int q; cin >> q;\n while(q--){\n string s; cin >> s;\n string t = \"\";\n for(char c:s){\n if(c != '?') t += c;\n }\n string res = \"\";\n for(int i=0;i<t.size();){\n if(t[i] == '('){\n int j = i;\n int cnt = 1; i++;\n while(i<t.size()){\n if(t[i] == '(') cnt++;\n else if(t[i] == ')') cnt--;\n i++;\n if(cnt == 0) break;\n }\n res += solve(t.substr(j,i-j));\n }\n else{\n res += t[i]; i++;\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 3010, "memory_kb": 56884, "score_of_the_acc": -2, "final_rank": 14 }, { "submission_id": "aoj_1661_6913822", "code_snippet": "#if 1\n#include <iostream>\n#include <fstream>\n#include <string>\n#include <vector>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n#include <queue>\n#include <stack>\n#include <array>\n#include <deque>\n#include <algorithm>\n#include <utility>\n#include <cstdint>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <assert.h>\n#include <bitset>\n#include <list>\n#include <cmath>\n#include <stdio.h>\n\nauto& in = std::cin;\nauto& out = std::cout;\n#define all_range(C) std::begin(C), std::end(C)\nconst double PI = 3.141592653589793238462643383279502884197169399375105820974944;\n\n\ntemplate<typename T, typename U>\nstd::enable_if_t<std::rank<T>::value == 0> fill_all(T& arr, const U& v) {\n arr = v;\n}\ntemplate<typename ARR, typename U>\nstd::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR& arr, const U& v) {\n for (auto& i : arr) {\n fill_all(i, v);\n }\n}\n\n\n//先頭か末尾を取り除く\nstd::string_view get_minpop(std::string_view base) {\n return std::min(base.substr(1), base.substr(0, base.size()-1));\n}\n\nstd::string S;\nstd::unordered_map<int, std::unordered_map<int, std::pair<std::string, int>>> dp;\n//std::vector<int> next_q[1000];\nint end_q[1000];\n//先頭から追加でpop_front文字消去したときの?(S)の辞書順最小\nstd::pair<std::string, int> func(const int i, const int pop_front) {\n\n assert(pop_front >= 0);\n if (dp.count(i)>0 && dp[i].count(pop_front)>0) {\n return dp[i][pop_front];\n }\n auto& memo = dp[i][pop_front];\n\n assert(S[i] == '?');\n assert(S[i + 1] == '(');\n\n //if (next_q[i].empty()) {\n //\n // auto len = end_q[i] - (i + 2);\n // if (len - 1 <= pop_front) {\n // return memo = { \"\", pop_front - (len - 1) };\n // }\n // std::string_view poped(S);\n // poped = poped.substr(i+2, len).substr(pop_front);\n //\n // assert(poped.find(')') == -1);\n //\n // assert(get_minpop(poped).size() == len - 1 - pop_front);\n // return memo = { std::string(get_minpop(poped)), 0};\n //}\n\n auto work = [i](int pop_f)->std::pair<std::string, int> {\n std::string res = \"\";\n auto index = i + 2;\n\n bool char_found = false;\n\n while (index < end_q[i]) {\n assert(index < S.size());\n assert(pop_f >= 0);\n assert(S[index] != '(');\n assert(S[index] != ')');\n if (S[index] == '?') {\n\n auto&& tmp = func(index, pop_f);\n if (!tmp.first.empty()) {\n char_found = true;\n }\n if (tmp.first.empty()) {\n assert(tmp.second >= 0);\n if (pop_f != tmp.second) {\n char_found = true;\n }\n pop_f = tmp.second;\n }\n else {\n if (pop_f != 0) {\n char_found = true;\n }\n pop_f = 0;\n res += (tmp.first);\n }\n\n index = end_q[index];\n assert(S[index] == ')');\n }\n else if (pop_f > 0) {\n char_found = true;\n --pop_f;\n }\n else {\n char_found = true;\n res += S[index];\n }\n ++index;\n }\n assert(S[index] == ')');\n\n if (!char_found) {\n return { \"\", -1 };\n }\n assert(res.empty() || pop_f==0);\n return { res, pop_f };\n };\n\n auto pf = work(pop_front + 1);\n auto pb = work(pop_front);\n if (pb.first.empty()) {\n assert(pf.first.empty());\n if (pb.second == -1) {\n assert(pf.second == -1);\n return memo = { \"\", pop_front };\n }\n assert(pb.second + 1 == pf.second );\n assert(pb.second + 1 <= pop_front);\n return memo = { \"\", pb.second + 1 };\n }\n pb.first.pop_back();\n\n return memo = { std::min(pf.first,pb.first), 0 };\n}\n\nint func_q(int i) {\n //if (!next_q[i].empty()) { return end_q[i]; }\n if (end_q[i] != 0) { return end_q[i]; }\n \n assert(S[i] == '?');\n assert(S[i+1] == '(');\n\n //auto& next_qi = next_q[i];\n auto& end_qi = end_q[i];\n\n i += 2;\n while (S[i] != ')') {\n if (S[i] == '?') {\n //next_qi.push_back(i);\n i = func_q(i);\n }\n ++i;\n }\n return end_qi = i;\n}\n\n//std::string minres = { 'z' + 1,'a','a' ,'a' ,'a' ,'a' ,'a' ,'a' ,'a' ,'a' ,'a' ,'a' };\n//std::map<int, bool> is_pop_front;\n//std::string all_s(int i)\n//{\n// assert(S[i] == '?');\n// assert(S[i + 1] == '(');\n//\n// auto& next_qi = next_q[i];\n// auto& end_qi = end_q[i];\n// bool pop_F = is_pop_front[i];\n//\n// std::string res = \"\";\n// i += 2;\n// while (S[i] != ')') {\n// if (S[i] == '?') {\n// res += all_s(i);\n// i = end_q[i];\n// }\n// else {\n// res += S[i];\n// }\n// ++i;\n// }\n// if (pop_F) { res = res.substr(1); }\n// else { res.pop_back(); }\n// return res;\n//}\n//std::string all_ss() {\n//\n//}\n\nint main()\n{\n using std::endl;\n in.sync_with_stdio(false);\n out.sync_with_stdio(false);\n in.tie(nullptr);\n out.tie(nullptr);\n\n int q;\n in >> q;\n while (q-- > 0) {\n in >> S;\n \n dp.clear();\n //for (auto& v : next_q) { v.clear(); }\n fill_all(end_q, 0);\n\n for (int i = 0; i < S.size(); i++)\n {\n if (S[i]=='?') {\n func_q(i);\n }\n }\n\n //for (size_t i = 0; i < S.size(); i++)\n //{\n // if (!next_q[i].empty()) {\n // out << \"NEXT:\" << i << ':';\n // for (auto& t : next_q[i]) { out << t << ' '; }\n // out << endl;\n // }\n //}\n\n std::string res = \"\";\n for (int i = 0; i < S.size(); i++)\n {\n if (S[i] != '?') {\n assert(S[i] != '(');\n assert(S[i] != ')');\n res += S[i];\n }\n else {\n res += func(i, 0).first;\n i = end_q[i];\n }\n }\n out << res << endl;\n\n\n }\n\n\n using std::string;\n using std::string_view;\n auto check = [](string token, const string (&words)[5]) -> bool {\n for (string_view tmp : words) {\n if (token == tmp) return true;\n }\n return false;\n };\n\n return 0;\n}\n#endif", "accuracy": 1, "time_ms": 10, "memory_kb": 6604, "score_of_the_acc": -0.0458, "final_rank": 3 }, { "submission_id": "aoj_1661_6827541", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=2167167167167167167;\nconst int INF=2100000000;\nconst ll mod=998244353;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\nbool eng(char c){\n\treturn ('a'<=c)&&(c<='z');\n}\n\nvoid solve();\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\tint t=1;\n\tcin>>t;\n\trep(i,t) solve();\n}\n\nvoid solve(){\n\tstring S;\n\tcin>>S;\n\tS=\"!(\"+S+\")\";\n\tint ind=0;\n\tvector<vector<int>> G;\n\tvector<string> T;\n\tint l=0,N=0;\n\tvector<int> order;\n\tauto f=[&](auto self,int pare)->void{\n\t\tint now=ind;\n\t\tG.push_back({});\n\t\t//cout<<now<<\" \"<<l<<endl;\n\t\tif(!eng(S[l])){\n\t\t\tT.push_back(\"\");\n\t\t\tT[ind]=S[l];\n\t\t\tl+=2;\n\t\t\twhile(S[l]!=')'){\n\t\t\t\tind++;\n\t\t\t\tself(self,now);\n\t\t\t}\n\t\t\tl++;\n\t\t}\n\t\telse{\n\t\t\tstring tmp=\"\";\n\t\t\twhile(eng(S[l])){\n\t\t\t\tN++;\n\t\t\t\ttmp+=S[l];\n\t\t\t\tl++;\n\t\t\t}\n\t\t\tT.push_back(tmp);\n\t\t}\n\t\tif(pare!=-1){\n\t\t\tG[pare].push_back(now);\n\t\t}\n\t\torder.push_back(now);\n\t};\n\tf(f,-1);/*\n\trep(i,(int)(G.size())){\n\t\tcout<<i<<\" : \";\n\t\tvec_out(G[i]);\n\t\tcout<<T[i]<<\"\\n\";\n\t}\n\tcout<<\"##########\\n\";\n\tstring I=\"{\";*/\n\tvector<vector<string>> dp(ind+1,vector<string>(N+1));\n\t\n\tvector<int> D(ind+1);\n\tstring ans=\"\";\n\tfor(auto x:order){\n\t\t//cout<<x<<\" \"<<T[x]<<endl;\n\t\tif(eng(T[x][0])){\n\t\t\tD[x]=T[x].size();\n\t\t\tfor(int L=0;L<=D[x];L++){\n\t\t\t\t//cout<<T[x].substr(L,C[x]-L-R)<<endl;\n\t\t\t\tdp[x][L]=T[x].substr(L,D[x]-L);\n\t\t\t\t//cout<<x<<\" \"<<L<<\" \"<<R<<\" \"<<dp[x][L][R]<<endl;\n\t\t\t}\n\t\t}\n\t\telse if(T[x][0]=='!'){\n\t\t\tfor(auto y:G[x]){\n\t\t\t\t//cout<<y<<\" : \"<<dp[y][0][0]<<\"\\n\";\n\t\t\t\tans+=(dp[y][0]).substr(0,D[y]);\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tvector<int> A;\n\t\t\tfor(auto y:G[x]) D[x]+=D[y],A.push_back(0);\n\t\t\tD[x]--;\n\t\t\t//cout<<x<<\" \"<<D[x]<<\"\\n\";\n\t\t\tchmax(D[x],0);\n\t\t\tif(D[x]==0) continue;\n\t\t\tfor(int L=0;L<=D[x];L++){\n\t\t\t\trep(ty,2){\n\t\t\t\t\tint n_l=L;\n\t\t\t\t\tif(ty==0) n_l++;\n\t\t\t\t\tint a=0;\n\t\t\t\t\tfor(auto y:G[x]){\n\t\t\t\t\t\tif(n_l>=D[y]) A[a]=D[y],n_l-=D[y];\n\t\t\t\t\t\telse A[a]=n_l,n_l=0;\n\t\t\t\t\t\ta++;\n\t\t\t\t\t}\n\t\t\t\t\ta=0;\n\t\t\t\t\tstring tmp;\n\t\t\t\t\tfor(auto y:G[x]){\n\t\t\t\t\t\ttmp+=dp[y][A[a]].substr(0,D[y]-A[a]);\n\t\t\t\t\t\ta++;\n\t\t\t\t\t}\n\t\t\t\t\tif(dp[x][L].empty()) dp[x][L]=tmp;\n\t\t\t\t\telse chmin(dp[x][L],tmp);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<<ans<<\"\\n\";\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 25428, "score_of_the_acc": -0.4364, "final_rank": 12 } ]
aoj_1658_cpp
Problem C Training Schedule for ICPC With little time remaining until ICPC, you decided to reschedule your training plan. As maintaining both mental and physical vitality is important, you decided to spend n days of the remaining n + m days for training, and m days for repose. The question is which days should be used for the training and which for the repose. A schedule that increases your ICPC power more is better. Training days increase the power, and consecutive training days are more effective. One day of training on the k- th day of consecutive training days increases the power by 2 k − 1, where k = 1 for the first day of the consecutive training days. A single training day increases the power by only 1, but two consecutive training days increase it by 1 + 3 = 4, and three consecutive training days increase it by 1 + 3 + 5 = 9. Repose days, on the other hand, decrease the power, and consecutive repose days decrease it more rapidly. One day of repose on the k- th day of consecutive repose days decreases the power by 2 k − 1, where k = 1 for the first day of the consecutive repose days. A single repose day decreases the power by only 1, but two consecutive repose days decrease it by 1 + 3 = 4, and three consecutive repose days decrease it by 1 + 3 + 5 = 9. Let us compute the largest increment of your ICPC power after n + m days of training and repose by the best training schedule. Note that, if you have too many repose days, this may be negative. Input The input consists of multiple datasets, each in the following format. n m Here, n and m are the numbers of training and repose days, respectively. Neither of them exceeds 10 6 , and at least one of them is non-zero. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each of the datasets, output in a line the largest increment of ICPC power after n + m days of training and repose by the best training schedule. Sample Input 1 1 3 2 6 1 1 6 0 3 2 4 7 9 0 0 Output for the Sample Input 0 7 35 -17 -9 -4 10
[ { "submission_id": "aoj_1658_10684472", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n long long n,m;\n vector<long long> lastans;\n while(true){\n cin >> n >> m;\n if(n==0&&m==0){\n break;\n }\n long long ans=n*n-m*m;\n for(long long i=1;i<=n&&i-1<=m;i++){\n long long sum=0;\n long long r=m/(i+1);\n if(m%(i+1)==0){\n sum+=(n-i+1)*(n-i+1)+(i-1)-((i+1)*r*r);\n }else{\n sum+=(n-i+1)*(n-i+1)+(i-1)-((i+1)*r*r+(m%(i+1))*(r+1)*(r+1)-(m%(i+1))*r*r);\n } \n if(sum>ans){\n ans=sum;\n }\n }\n lastans.push_back(ans);\n }\n for(auto at:lastans){\n cout << at << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3144, "score_of_the_acc": -0.4129, "final_rank": 2 }, { "submission_id": "aoj_1658_10676397", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nstruct Node{\n vector<Node*>ns;\n char c;\n int idx;\n};\n\nll f(ll x){return x*x;}\nbool solve() {\n int N,M;cin>>N>>M;\n if(N+M==0)return 0;\n\n ll ans=f(N)-f(M);\n //平方数の加減\n for(int i=1;i<=N;i++){\n ll ret = 0; \n //練習日の塊の数i\n int j=i+1;\n //休息日の塊の数j\n if(j>M)break;\n //練習 i-1個の1日と1個のN-(i-1)日\n ret+=ll(i-1)*f(1)+1LL*f(N-(i-1));\n //休息 a個のfloor(M/j)日 b個のfloor(M/j)+1日\n ll b=M%j;\n ll a=j-b;\n ret-=a*f(M/j)+b*f(M/j+1);\n chmax(ans,ret);\n }\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_1658_10676296", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n/*\nk回に休みを分割するとk-2回1日、1回n-k+2日\nm/k回ずつ\n*/\n\nvoid solve(ll n, ll m) {\n if (m == 0) {\n cout << n * n << endl;\n return;\n }\n ll ans = n * n - m * m;\n for (ll k = 2; k <= min(n + 1, m); k++) {\n ll kari = k - 2 + (n - k + 2) * (n - k + 2);\n ll len = m / k;\n ll mod = m % k;\n kari -= mod * (len + 1) * (len + 1) + (k - mod) * len * len;\n ans = max(ans, kari);\n }\n cout << ans << endl;\n}\n\nint main() {\n while (true) {\n ll n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3052, "score_of_the_acc": -0.24, "final_rank": 1 }, { "submission_id": "aoj_1658_10674565", "code_snippet": "/* template */\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(), (x).end()\n\n\nint main(){\n cin.tie(nullptr);ios::sync_with_stdio(false);\n\n /* (>_<) mycode! */\n ll n, m;\n while (true) {\n cin >> n >> m;\n if (n == 0 && m == 0) {\n return 0;\n }\n\n if (n == 0) {\n cout << -(m * m) << endl;\n continue;\n }\n if (m == 0) {\n cout << n * n << endl;\n continue;\n }\n if (m == 1) {\n cout << n * n - 1 << endl;\n continue;\n }\n ll ans = -1e18;\n ll x = min(n, m - 1);\n for (ll i = 1; i <= x; i++) {\n ll max_n = n - i + 1;\n ll min_m = m / (i + 1);\n ll cnt_min = (min_m + 1) * (i + 1) - m;\n ll score = i - 1 + max_n * max_n;\n score -= min_m * min_m * cnt_min;\n score -= (min_m + 1) * (min_m + 1) * (i + 1 - cnt_min);\n ans = max(ans, score);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3292, "score_of_the_acc": -0.6911, "final_rank": 4 }, { "submission_id": "aoj_1658_10672061", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n#define rep2(i,m,n) for(ll i=ll(m);i<ll(n);i++)\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vvvl = vector<vvl>;\nusing pl = pair<ll,ll>;\nusing vpl = vector<pl>;\nusing vvpl = vector<vpl>;\n#define pb push_back\n\nconst long double EPS = 0.0000000001;\nconst ll INF = 1000000000000000000;\nconst double pi = std::acos(-1.0);\n\n__int128 read_int128(){ //__int128を入力する\n string S;\n cin >> S;\n int N = S.size();\n int st = 0;\n bool minus = false;\n if(S[0] == '-'){\n minus = true;\n st = 1;\n }\n __int128 res = 0;\n rep2(i,st,N) res = res*10+int(S[i]-'0');\n if(minus) res *= -1;\n return res;\n}\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { //これでcoutで__int128を出力できるように\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\nvoid Yes(){ cout << \"Yes\" << endl; } //文字列\"Yes\"を標準出力\nvoid No(){ cout << \"No\" << endl; } //文字列\"No\"を標準出力\n\ntemplate<class T> bool chmin(T& a,T b){\n if(a > b){\n a = b;\n return true;\n }\n else return false;\n}\n\ntemplate<class T> bool chmax(T& a,T b){\n if(a < b){\n a = b;\n return true;\n }\n else return false;\n}\n\n\n\nint main(){\n while(1){\n ll N,M;\n cin >> N >> M;\n if(N == 0 && M == 0) break;\n if(M == 0) cout << N*N << endl;\n else if(N == 0) cout << -M*M << endl;\n else{\n ll ans = N*N-M*M;\n rep2(i,2,M+1){ //休憩をi個に分ける→練習はi-1個に分ける\n if(i-1 > N) break;\n ll plus = (N+2-i)*(N+2-i)+1*(i-2);\n ll small = M/i;\n ll big_num = M%i;\n ll big = small+1;\n ll small_num = i-big_num;\n ll minus = small*small*small_num + big*big*big_num;\n ans = max(ans,plus-minus);\n }\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3352, "score_of_the_acc": -0.8039, "final_rank": 8 }, { "submission_id": "aoj_1658_10666061", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#define rep(i,l,r) for(int i=(l);i<(r);++i)\n#define rrep(i,l,r) for(int i=(r)-1;i>=(l);--i)\nusing ll = long long;\nusing ull = unsigned long long;\nusing pii = pair<int, int>;\nusing pll = pair<long long,long long>;\n#define pb push_back\n#define fi first\n#define se second\n#define sz(x) ((int)x.size())\n#define pqueue priority_queue\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define inr(l, x, r) (l <= x && x < r)\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n#define INF (1<<30)-1\n#define INFLL (1LL<<60)-1\n#define popcount __builtin_popcount\n#define UNIQUE(v) sort(v.begin(),v.end()),v.erase(unique(v.begin(),v.end()),v.end());\nvoid yn(bool flag){if(flag)cout<<\"Yes\"<<endl; else cout<<\"No\"<<endl;}\ntemplate <typename T>\nbool chmax(T &a,const T& b){if(a<b){a=b;return true;}return false;}\ntemplate <typename T>\nbool chmin(T &a,const T& b){if(a>b){a=b;return true;}return false;}\nvoid solve();\n\n// using mint = modint998244353;\n\n#ifdef LOCAL\n# include \"debug_print.hpp\"\n# define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)\n#else\n# define debug(...) (static_cast<void>(0))\n#endif\n\nint main(){\n cin.tie(0); ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(10);\n solve();\n}\n\nvoid solve(){\n while(true){\n ll N,M; cin>>N>>M;\n if(N==0&&M==0)return;\n ll ans=-INFLL;\n if(N==0){\n cout<<-M*M<<endl;\n goto g;\n }\n if(M==0){\n cout<<N*N<<endl;\n goto g;\n }\n if(M==1){\n cout<<N*N-1<<endl;\n goto g;\n }\n rep(i,2,min(M+1,N+2)){\n ll n1=M/i;\n ll n2=n1+1;\n n1*=n1,n2*=n2;\n n2*=M%i;\n n1*=i-M%i;\n // debug(i,n1,n2,(N-i+2)*(N-i+2)+(i-2)-n1-n2);\n chmax(ans,(N-i+2)*(N-i+2)+(i-2)-n1-n2);\n }\n cout<<ans<<endl;\n g:;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3384, "score_of_the_acc": -0.8641, "final_rank": 10 }, { "submission_id": "aoj_1658_10661968", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 lll;\nusing ull = unsigned long long;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\ntemplate<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using pqmax = priority_queue<T>;\nconst ll inf=LLONG_MAX/3;\nconst ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\nconst ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};\n#define mp make_pair\n#define pb push_back\n#define eb emplace_back\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define si(x) ll(x.size())\n#define rept(n) for(ll _ovo_=0;_ovo_<n;_ovo_++)\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define per(i,n) for(ll i=n-1;i>=0;i--)\n#define rng(i,l,r) for(ll i=l;i<r;i++)\n#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)\n#define fore(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\ntemplate<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define VLL(name,size) vector<ll>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define SUM(...) accumulate(all(__VA_ARGS__),0LL)\ntemplate<class T> auto min(const T& a){ return *min_element(all(a)); }\ntemplate<class T> auto max(const T& a){ return *max_element(all(a)); }\ntemplate<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }\ntemplate<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }\ntemplate<class T, class F = less<>> map<T,vector<ll> > ivm(vector<T>& a, F b = F{}){ map<T,vector<ll> > ret; rep(i,si(a))ret[a[i]].push_back(i); return ret;}\ntemplate<class T, class F = less<>> map<T,ll> ivc(vector<T>& a, F b = F{}){ map<T,ll> ret; rep(i,si(a))ret[a[i]]++; return ret;}\ntemplate<class T, class F = less<>> vector<T> ivp(vector<T> a){ vector<ll> ret(si(a)); rep(i,si(a))ret[a[i]] = i; return ret;}\ntemplate<class T, class F = less<>> vector<ll> rev(vector<T> a){ reverse(all(a)); return a;}\ntemplate<class T, class F = less<>> vector<ll> sortby(vector<T> a, F b = F{}){vector<ll> w = a; sor(w,b); vector<pll> v; rep(i,si(a))v.eb(a[i],i); sor(v); if(w[0] != v[0].first)reverse(all(v)); vector<ll> ret; rep(i,si(v))ret.pb(v[i].second); return ret;}\ntemplate<class T, class P> vector<T> filter(vector<T> a,P f){vector<T> ret;rep(i,si(a)){if(f(a[i]))ret.pb(a[i]);}return ret;}\ntemplate<class T, class P> vector<ll> filter_id(vector<T> a,P f){vector<ll> ret;rep(i,si(a)){if(f(a[i]))ret.pb(i);}return ret;}\nll monotone_left(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_left(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_right(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\nll monotone_right(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\ndouble monotone_double_left(double l,double r,function<bool(double)> f){assert(f(l) >= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return l;}\ndouble monotone_double_right(double l,double r,function<bool(double)> f){assert(f(l) <= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return r;}\ntemplate<class S> S unimodal_max(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) < f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmax(ret,f(k)); return ret;}\ntemplate<class S> S unimodal_min(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) > f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmin(ret,f(k)); return ret;}\nvector<pll> neighbor4(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,4){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\nvector<pll> neighbor8(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,8){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\n\nvoid outb(bool x){cout<<(x?\"Yes\":\"No\")<<\"\\n\";}\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))\nll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}\nvector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }\nvector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }\nvll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}\nll powll(lll x,ll y){lll res = 1; while(y){ if(y & 1)res = res * x; x = x * x; y >>= 1;} return res;}\nll powmod(lll x,ll y,lll mod){lll res=1; while(y){ if(y&1)res=res*x%mod; x=x*x%mod; y>>=1;} return res; }\nll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0)u+=m;return u;}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <class T> vector<T> &operator++(vector<T> &v) {\n\t\tfore(e, v) e++;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator++(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e++;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {\n\t\tfore(e, v) e--;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator--(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e--;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {\n\t\tfore(e, r) l.eb(e);\n\t\treturn l;\n}\n\ntemplate<class... Ts> void in(Ts&... t);\n[[maybe_unused]] void print(){}\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts);\ntemplate<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\\n'; }\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }\ntemplate<class T> void o(const T& t){ o(t, P<4>{}); }\ntemplate<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }\ntemplate<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }\ntemplate<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }\ntemplate<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }\ntemplate<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }\ntemplate<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }\n#undef VOID\n}\n#define unpack(a) (void)initializer_list<int>{(a, 0)...}\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }\n#undef unpack\nbool solve(){\n\tll n,m;\n\tcin>>n>>m;\n\tif(n + m == 0)return false;\n\tll ans = -inf;\n\tif(n == 0){\n\t\tans = -m * m;\n\t\tcout<<ans<<endl;\n\t\treturn true;\n\t}\n\tfor(ll k = 1;k <= n;k++){\n\t\tll res = 0;\n\t\t{\n\t\t\tll b = n - (k - 1);\n\t\t\tres += k - 1 + b * b;\n\t\t}\n\t\t{\n\t\t\tll q = m / (k + 1);\n\t\t\tll r = m % (k + 1);\n\t\t\tres -= r * (q + 1) * (q + 1) + (k + 1 - r) * q * q;\n\t\t}\n\t\tchmax(ans,res);\n\t}\n\tcout<<ans<<endl;\n\treturn true;\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3508, "score_of_the_acc": -1.8171, "final_rank": 20 }, { "submission_id": "aoj_1658_10661511", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef std::pair<long long, long long> P;\ntypedef std::priority_queue<P, std::vector<P>, std::greater<P>> PQ;\ntypedef std::complex<double> cd;\n\nstruct P3 {\n long long first, second, third;\n};\n\nstruct P3P {\n P first, second, third;\n};\n\nstruct compP3{\n bool operator()(const P3 &p1,const P3 &p2) const {\n if (p1.first != p2.first) return p1.first < p2.first;\n if (p1.second != p2.second) return p1.second < p2.second;\n else return p1.third < p2.third;\n }\n};\n\nstruct gcompP3{\n bool operator()(const P3 &p1,const P3 &p2) const {\n if (p1.first != p2.first) return p1.first > p2.first;\n if (p1.second != p2.second) return p1.second > p2.second;\n else return p1.third > p2.third;\n }\n};\n\nconst double PI = acos(-1.0);\n\nbool ckran(int a, int n) {\n return (a >= 0 && a < n);\n}\n\nvoid yn (bool f) {\n if (f) std::cout << \"Yes\" << '\\n';\n else std::cout << \"No\" << '\\n';\n}\n\nlong long pplus(P a) {\n return a.first + a.second;\n}\n\nlong long pminus(P a) {\n return a.first - a.second;\n}\n\nlong long ptime(P a) {\n return a.first * a.second;\n}\n\nlong long pdiv(P a) {\n return a.first / a.second;\n}\n\ntemplate<typename T, typename U>\nbool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n } else {\n return false;\n }\n}\n\ntemplate<typename T, typename U>\nbool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n } else {\n return false;\n }\n}\n\ntemplate<typename T>\nvoid outspace(std::vector<T> a) {\n int n = a.size();\n for (int i = 0; i < n; ++i) {\n std::cout << a[i];\n if (i != n - 1) std::cout << \" \";\n else std::cout << std::endl;\n }\n}\n\nvoid outspace(P a) {\n std::cout << a.first << ' ' << a.second << '\\n';\n}\n\ntemplate<typename T>\nvoid outendl(std::vector<T> a) {\n int n = a.size();\n for (int i = 0; i < n; ++i) {\n std::cout << a[i] << '\\n';\n }\n}\n\nstd::vector<long long> lltovec(long long n, long long base = 10, long long minsize = -1) {\n std::vector<long long> res;\n while (minsize-- > 0 || n > 0) {\n res.push_back(n % base);\n n /= base;\n }\n // std::reverse(res.begin(), res.end());\n return res;\n}\n\nlong long vectoll(std::vector<long long> vec, long long base = 10) {\n long long res = 0;\n std::reverse(vec.begin(), vec.end());\n for (auto i : vec) {\n res *= base;\n res += i;\n }\n std::reverse(vec.begin(), vec.end());\n return res;\n}\n\nstatic const long long MOD = 998244353;\nstatic const int MAXN = 1000000; // 必要に応じて大きく設定\n\n// 階乗・階乗逆元の配列\nstatic long long fact[MAXN+1], invFact[MAXN+1];\n\n// 繰り返し二乗法 (a^b mod M)\nlong long modpow(long long a, long long b, long long M) {\n long long ret = 1 % M;\n a %= M;\n while (b > 0) {\n if (b & 1) ret = (ret * a) % M;\n a = (a * a) % M;\n b >>= 1;\n }\n return ret;\n}\n\n// 前処理: 階乗と逆元のテーブルを作る\nvoid initFactorials() {\n // 階乗テーブル\n fact[0] = 1;\n for (int i = 1; i <= MAXN; i++) {\n fact[i] = fact[i-1] * i % MOD;\n }\n // 階乗逆元テーブル\n invFact[MAXN] = modpow(fact[MAXN], MOD - 2, MOD); // フェルマーの小定理で逆元を計算\n for (int i = MAXN; i >= 1; i--) {\n invFact[i-1] = invFact[i] * i % MOD;\n }\n}\n\n// 組み合わせ数 C(n, r)\nlong long comb(int n, int r) {\n if (r < 0 || r > n) return 0;\n return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;\n}\n\nint main()\n{\n vector<ll> an;\n while (true) {\n ll n, m;\n cin >> n >> m;\n if (n == m && n == 0) break;\n ll ans = -1e18;\n if (n == 0) {\n ans = -m * m;\n an.push_back(ans);\n continue;\n } else if (n == 1) {\n ans = -(m / 2) * (m / 2) - ((m + 1) / 2) * ((m + 1) / 2) + 1;\n an.push_back(ans);\n continue;\n }\n for (ll i = 2; i <= n + 1; ++i) {\n ll cur = 0;\n cur += (n - i + 2) * (n - i + 2);\n cur += i - 2;\n ll d = m / i;\n ll o = m % i;\n cur -= (d * d) * (i - o);\n cur -= ((d + 1) * (d + 1)) * o;\n chmax(ans, cur);\n }\n an.push_back(ans);\n }\n outendl(an);\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3180, "score_of_the_acc": -1.1606, "final_rank": 15 }, { "submission_id": "aoj_1658_10650509", "code_snippet": "// written in sccp_20250626 \"aizu_d\"\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,l,r) for(int i=(l);i<(r);i++)\ninline bool chmax(ll &a,ll b){ return (a<b?a=b,true:false); }\n\nint main(){\n const ll inf=1ll<<60;\n auto calc_two=[](ll x){\n return x*x;\n };\n while(true){\n ll n,m;\n cin>>n>>m;\n if(n==0 and m==0) return 0;\n ll ans=-inf;\n if(n==0){\n ans=-calc_two(m);\n }else if(m==0){\n ans=calc_two(n);\n }else if(m==1){\n ans=calc_two(n)-1;\n }else{\n rep(k,1,n+1){\n const ll d=m/(k+1);\n const ll c=m%(k+1);\n if(d<=0) break;\n ll sum=calc_two(n-k+1)+(k-1);\n sum-=calc_two(d)*((k+1)-c)+calc_two(d+1)*c;\n chmax(ans,sum);\n }\n }\n cout<<ans<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3456, "score_of_the_acc": -0.7994, "final_rank": 5 }, { "submission_id": "aoj_1658_10649272", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\nusing ll = long long;\n\n// o := 練習日\n// x := 休息日\n//\n// 前提:\n// [1] 休息日、練習日ともに連続する長さ k の二乗の能力が増減する\n// [2] 休息日は可能な限り分割すべき\n// [3] 練習日は可能な限り連続にすべき\n//\n// これらは x^2 + y^2 < (x + y)^2 を考えると良い\n//\n// どのように分割すべきか?\n//\n// **休息日は均等に割り振るべき**\n// 例\n// n = 5, m = 5\n// xoooooxxxx : score 8\n// xxoooooxxx : score 12\n//\n// これは f(x) = x^2 + (m - x) のグラフの最小値を観察すると良い\n//\n// **練習日は偏るように割り振るべき**\n// 例\n// n = 5, m = 5\n// xxooxxooox : score 4\n// xxoxxoooox : score 8\n//\n// これは f(x) = x^2 + (n - x) のグラフの最大値を観察すると良い\n//\n// ここまで考察できれば、あとは様々な考え方がある\n// ここでは、休息日をいくつに分割するか全探索する\n//\n// 休息日をk個に分割するとき、\n// 長さ ceil(m / k) の連続する休息日は m % k個\n// 長さ floor(m / k)の連続する休息日は k - (m % k) 個\n//\n// それらの間に練習日を偏らせて詰めていく\n// 長さ1の練習日をk - 2個, 長さn - (k - 2)の練習日を1個を詰めるのが最善\n\nint solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) return 1;\n auto f = [&](ll len) {\n return len * len;\n };\n if (n == 0) {\n cout << -f(m) << '\\n';\n return 0;\n }\n if (m == 0) {\n cout << f(n) << '\\n';\n return 0;\n }\n ll ans = f(n) - f(m);\n rep(k, 2, m + 1) {\n if (k - 1 > n) break;\n ll cur = 0;\n { // 休息日\n ll len1 = (m + k - 1) / k;\n ll len2 = m / k;\n ll num1 = m % k;\n ll num2 = k - (m % k);\n cur -= num1 * f(len1) + num2 * f(len2);\n }\n { // 練習日\n ll len1 = 1;\n ll len2 = n - (k - 2);\n ll num1 = k - 2;\n ll num2 = 1;\n cur += num1 * f(len1) + num2 * f(len2);\n }\n if (ans < cur) ans = cur;\n }\n cout << ans << '\\n';\n return 0;\n}\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3456, "score_of_the_acc": -0.8394, "final_rank": 9 }, { "submission_id": "aoj_1658_10649006", "code_snippet": "#include<bits/extc++.h>\n#define rep(i,n) for(int i=0; i< (n); i++)\n#define all(a) begin(a),end(a)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 998244353;\nconst int inf = (1<<30);\nconst ll INF = (1ull<<62);\nconst int dx[8] = {1,0,-1,0,1,1,-1,-1};\nconst int dy[8] = {0,1,0,-1,1,-1,1,-1};\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\nint main(){\n while(1){\n ll n,m;\n cin>>n>>m;\n if(n == 0 && m == 0) break;\n if( n == 0) cout<<-(m*m)<<endl;\n else{\n ll ans = -INF;\n for(int i = 0; i<n; i++){\n ll k = (n-i)*(n-i);\n ll d = m/(i+2);\n ll c = m%(i+2);\n //cout<<i<<\" \"<<k<<\" \"<<d<<\" \"<<c<<endl;\n chmax(ans,k - (d)*(d)*(i+2 - c) - (d+1)*(d+1)*(c) + i);\n }\n cout<<ans<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3320, "score_of_the_acc": -1.5038, "final_rank": 17 }, { "submission_id": "aoj_1658_10648907", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,l,r) for(int i=(l);i<(r);i++)\ninline bool chmax(ll &a,ll b){ return (a<b?a=b,true:false); }\n\nint main(){\n const ll inf=1ll<<60;\n auto calc_two=[](ll x){\n return x*x;\n };\n while(true){\n ll n,m;\n cin>>n>>m;\n if(n==0 and m==0) return 0;\n ll ans=-inf;\n if(n==0){\n ans=-calc_two(m);\n }else if(m==0){\n ans=calc_two(n);\n }else if(m==1){\n ans=calc_two(n)-1;\n }else{\n rep(k,1,n+1){\n const ll d=m/(k+1);\n const ll c=m%(k+1);\n if(d<=0) break;\n ll sum=calc_two(n-k+1)+(k-1);\n sum-=calc_two(d)*((k+1)-c)+calc_two(d+1)*c;\n chmax(ans,sum);\n }\n }\n cout<<ans<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3456, "score_of_the_acc": -0.7994, "final_rank": 5 }, { "submission_id": "aoj_1658_10648805", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n cin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n while (true){\n long long n, m;\n cin >> n >> m;\n if (n == 0 and m == 0) break;\n long long ans = n * n - m * m;\n for (int i = 0; i < n; i++){\n long long sum = (n - i) * (n - i) + i;\n int d = i + 2;\n sum -= (m / d) * (m / d) * (d - m % d);\n sum -= (m / d + 1) * (m / d + 1) * (m % d);\n ans = max(ans, sum);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3456, "score_of_the_acc": -1.0794, "final_rank": 13 }, { "submission_id": "aoj_1658_10648800", "code_snippet": "#include <bits/stdc++.h>\nnamespace ranges = std::ranges;\nlong long f(const long long sum, const long long num) {\n assert(1 <= num and num <= sum);\n const long long fl = sum / num, ce = (sum + num - 1) / num;\n const long long cn = sum % num, fn = num - cn;\n const long long res = fn * fl * fl + cn * ce * ce;\n return res;\n}\nlong long g(const long long sum, long long num) {\n assert(1 <= num and num <= sum);\n num--;\n return num + (sum - num) * (sum - num);\n}\nlong long solve(const long long N, const long long M) {\n long long ans = N * N - M * M;\n for (int n = 1 ; n <= N ; n++) {\n int m = n + 1;\n if (M < m) continue;\n const long long val = g(N, n) - f(M, m);\n ans = std::max(ans, val);\n }\n return ans;\n}\nint main() {\n while (true) {\n long long N, M;\n std::cin >> N >> M;\n if (N == 0 and M == 0) break;\n std::cout << solve(N, M) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3456, "score_of_the_acc": -0.9194, "final_rank": 11 }, { "submission_id": "aoj_1658_10647851", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\nusing ll = long long;\n\nint solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) return 1;\n auto f = [&](ll len) {\n return len * len;\n };\n if (n == 0) {\n cout << -f(m) << '\\n';\n return 0;\n }\n if (m == 0) {\n cout << f(n) << '\\n';\n return 0;\n }\n ll ans = f(n) - f(m);\n rep(k, 2, m + 1) {\n if (k - 1 > n) break;\n ll cur = 0;\n {\n ll len1 = (m + k - 1) / k;\n ll len2 = m / k;\n ll num1 = m % k;\n ll num2 = k - (m % k);\n cur -= num1 * f(len1) + num2 * f(len2);\n }\n {\n ll len1 = 1;\n ll len2 = n - (k - 2);\n ll num1 = k - 2;\n ll num2 = 1;\n cur += num1 * f(len1) + num2 * f(len2);\n }\n if (ans < cur) ans = cur;\n }\n cout << ans << '\\n';\n return 0;\n}\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3584, "score_of_the_acc": -1.08, "final_rank": 14 }, { "submission_id": "aoj_1658_10643083", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(ll n, ll m){\n if (n == 0){\n cout << -m*m << endl;\n return;\n }\n if (m == 1){\n cout << n*n - m*m << endl;\n return;\n }\n\n ll ans = -1e18;\n for (ll div = 1; div <= n; div++){\n ll k = 0;\n k = (n-div+1)*(n-div+1) + div-1;\n\n k -= (m/(div+1))*(m/(div+1))*(div+1-(m%(div+1))) + (m/(div+1)+1)*(m/(div+1)+1)*(m%(div+1));\n ans = max(ans, k);\n }\n cout << ans << endl;\n}\n\nint main(){\n ll n,m;cin >> n >> m;\n while(n || m){\n solve(n,m);\n cin >> n >> m;\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3348, "score_of_the_acc": -1.4764, "final_rank": 16 }, { "submission_id": "aoj_1658_10614178", "code_snippet": "// AOJ 1658 - Training Schedule for ICPC\n// ICPC Japan Domestic Contest 2022 - Problem C\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nbool solving() {\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) return false;\n\n ll ans = -9e18;\n if(m == 0) ans = (ll)n * n;\n for(int i=1; i<=min(n+1,m); ++i) {\n ll prac, rest;\n prac = max(0, i-2) + (ll)(n - max(0, i-2)) * (n - max(0, i-2));\n int more = m % i;\n rest = (ll)(m/i + 1) * (m/i + 1) * more + (ll)(m/i) * (m/i) * (i - more);\n ans = max(ans, prac - rest);\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while(solving()) {}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3340, "score_of_the_acc": -0.6214, "final_rank": 3 }, { "submission_id": "aoj_1658_10612181", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvector<char> isprime;\nvoid init() {\n isprime.assign(1000005, 1);\n isprime[0] = 0;\n isprime[1] = 0;\n for(int i = 2; i < 1000005; i++) {\n if(isprime[i]) {\n for(int j = i * 2; j < 1000005; j += i) {\n isprime[j] = 0;\n }\n }\n }\n}\n\nclass UnionFind {\n private:\n int sz;\n vector<int> par, size_;\n\n public:\n UnionFind() {}\n UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1) {\n iota(par.begin(), par.end(), 0);\n }\n int find(int x) {\n if(par[x] == x)\n return x;\n else\n return par[x] = find(par[x]);\n }\n void unite(int x, int y) {\n x = find(x), y = find(y);\n if(x == y)\n return;\n if(size_[x] < size_[y])\n swap(x, y);\n par[y] = x;\n size_[x] += size_[y];\n }\n int size(int x) {\n x = find(x);\n return size_[x];\n }\n bool same(int x, int y) { return find(x) == find(y); }\n};\n\nll n, m;\nvector<string> v;\nvoid input() { cin >> n >> m; }\n\nvoid solve() {\n ll ans = -m * m;\n for(int i = 1; i <= n; i++) {\n ll a = (n - i + 1) * (n - i + 1) + (i - 1);\n\n ll b = m / (i + 1), c = m % (i + 1);\n a -= b * b * (i + 1 - c) + (b + 1) * (b + 1) * c;\n\n chmax(ans, a);\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0 && m == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3380, "score_of_the_acc": -1.5765, "final_rank": 19 }, { "submission_id": "aoj_1658_10556879", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <cmath>\n#include <numeric>\n#include <map>\n\n#define fast ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\nusing ll = long long;\n\nll n,m;\nint main() {\n while(1){\n cin >> n >> m;\n if(n == 0 && m == 0) break;\n ll ans = -m * m;\n for(int i = 1; i <= n; i++){\n ll now = i - 1 + (n - i + 1) * (n - i + 1);\n ll r = m / (i + 1);\n ll rem = m - r * (i + 1);\n now -= (r + 1) * (r + 1) * rem + (i + 1 - rem) * (r * r);\n ans = max(ans, now);\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3344, "score_of_the_acc": -1.5089, "final_rank": 18 }, { "submission_id": "aoj_1658_10554538", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\nlong long mod_mul(long long a, long long b, long long mod) {\n return (__int128)a * b % mod;\n}\n\nlong long mod_pow(long long a, long long b, long long mod) {\n long long result = 1;\n a %= mod;\n while (b > 0) {\n if (b & 1) result = mod_mul(result, a, mod);\n a = mod_mul(a, a, mod);\n b >>= 1;\n }\n return result;\n}\n\nbool MillerRabin(ll x){\n \n if(x <= 1){return false;}\n if(x == 2){return true;}\n if((x & 1) == 0){return false;}\n vll test;\n if(x < (1 << 30)){\n test = {2, 7, 61};\n } \n else{\n test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n }\n ll s = 0;\n ll d = x - 1;\n while((d & 1) == 0){\n s++;\n d >>= 1;\n }\n for(ll i:test){\n if(x <= i){return true;}\n ll y = mod_pow(i,d,x);\n if(y == 1 || y == x - 1){continue;}\n ll c = 0;\n for(int j=0;j<s-1;j++){\n y = mod_mul(y,y,x);\n if(y == x - 1){\n c = 1;\n break;\n }\n }\n if(c == 0){\n return false;\n }\n }\n return true;\n}\n\n#include<atcoder/dsu>\nvpll rle(vll seq){\n vpll res;\n for(auto x:seq){\n if(res.empty() || res.back().first != x){\n res.emplace_back(x,1);\n }\n else{\n res.back().second++;\n }\n }\n return res;\n}\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n \n rep(i,(1<<16)){\n break;\n vll a;\n ll t = 0;\n rep(j,16){\n a.push_back((i >> j) & 1);\n }\n t = sum(a);\n if(t != 7){continue;}\n vpll s = rle(a);\n ll score = 0;\n for(auto [x,y]:s){\n score += y * y * (x == 0?-1:1);\n }\n if(score >= 0){\n print(a,score);\n }\n \n\n\n }\n //exit(0);\n while(1){\n LL(n,m);\n if(n == 0 && m == 0){break;}\n ll ans = -1LL<<60;\n if(n == 0){\n print(-(m * m));\n continue;\n }\n if(m == 0){\n print(n * n);\n continue;\n }\n chmax(ans, n * n - m * m);\n rep(i,2,m+1){\n if(i-1 > n){break;}\n ll temp = 0;\n ll x = m / i;\n ll y = m % i;\n temp -= x * x * (i - y) + (x + 1) * (x + 1) * y;\n temp += 1LL * (i-2) + (n - (i-2)) * (n - (i-2)) * 1LL;\n chmax(ans,temp);\n }\n print(ans);\n \n \n }\n \n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3456, "score_of_the_acc": -0.7994, "final_rank": 5 } ]
aoj_1659_cpp
Problem D Audience Queue You are working as a staff member at a concert venue, organizing the audience queue. Everyone in the queue holds one ticket with a seat number printed. The seat numbers are integers from 1 to n without duplicates. Since this is a very popular concert, all the seats are reserved and exactly n people are in a single line. Figure D-1: An example of the audience queue (The first dataset in Sample Input) Your job is to split the line at some points and navigate each of the sublines to a distinct entrance gate. The audience are so polite that the order in each of the sublines is kept. There are k entrance gates, and thus you can split the line into k or less sublines. Sublines may have widely varying lengths, but empty sublines are not allowed. In the example of the figure below, the original line [4 2 3 5 1] has been split into three sublines, [4 2], [3 5], and [1]. Figure D-2: An example of splitting the line Although up to k lines are formed, the audience can enter the hall one at a time. Among those who head the lines, one with the smallest seat number enters the hall first. In the example figure below, among the heads 4, 3, and 1 of the lines, one with the smallest seat number, 1, enters the hall first. Subsequent entrance order is decided in the same way. Among those who head the lines at the time, one with the smallest seat number is the next to enter the hall. Figure D-3: An example of the order of the entrance The order of entrance depends on how you split the audience line. Different ways of splitting may result in the same order. For the example above, splitting into [4 2], [3], [5], and [1] results in the same order. Given the initial order in the queue and the final entrance order of the audience, please compute how many different ways of splitting the line result in the given final entrance order. For the same partitioning of the line, different assignments of sublines to gates are not counted separately. Input The input consists of multiple datasets, each in the following format. n k s 1 ... s n t 1 ... t n Integers n and k satisfies 1 ≤ k ≤ n ≤ 10 4 . The number of the people in the audience line is represented by n , and the number of the gates is represented by k . The sequence s 1 ... s n is a permutation of the integers 1 through n , which describes the initial order of the audience in the queue. The sequence t 1 ... t n is also a permutation of the integers 1 through n , which describes the final entrance order. The end of the input is indicated by a line " 0 0 ". The number of datasets does not exceed 50. Output For each dataset, output in a line the number of ways to split the line as described in the problem statement. Since the number can be very large, the output should be the number modulo the prime number 998 244 353 . Sample Input 5 4 4 2 3 5 1 1 3 4 2 5 3 3 1 2 3 1 2 3 3 2 3 2 1 1 2 3 7 5 4 5 6 7 1 2 3 1 2 3 4 5 6 7 4 4 2 1 4 3 1 4 3 2 31 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...(truncated)
[ { "submission_id": "aoj_1659_10676345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll mod = 998244353;\n\nll powmod(ll a, ll b) {\n ll ret = 1;\n ll k = a;\n while (b) {\n if (b & 1) {\n ret *= k;\n ret %= mod;\n }\n k *= k;\n k %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nvector<ll> fact, ifact;\n\nll nCk(ll n, ll k) {\n if (n < 0 || k > n) return 0;\n return fact[n] * ifact[k] % mod * ifact[n - k] % mod;\n}\n\n/*\n6, 7\n6, 7\nどちらでもいい\n\n6, 7\n7, 6\n詰んだ\n\n7, 6\n7, 6\n切ってはいけないし連続していないといけない\nので増加列になる\n\n7, 6\n6, 7\n切らないといけない\n*/\nvoid solve(int n, int k) {\n int needcut = 0, cont = 0;\n vector<int> s(n), t(n), rt(n);\n for (auto &i: s) {\n cin >> i;\n i--;\n }\n for (int i = 0; i < n; i++) {\n cin >> t[i];\n t[i]--;\n rt[t[i]] = i;\n }\n vector<vector<int>> st;\n st.emplace_back(1, s[0]);\n for (int i = 1; i < n; i++) {\n if (st.back().back() < s[i]) {\n if (rt[st.back().back()] > rt[s[i]]) {\n cout << 0 << endl;\n return;\n }\n else {\n st.back().push_back(s[i]);\n }\n }\n else {\n if (rt[st.back().back()] < rt[s[i]]) {\n cont++;\n }\n else {\n needcut++;\n st.emplace_back(1, s[i]);\n }\n }\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n for (int i = 0; i < (int)st.size(); i++) {\n pq.emplace(st[i][0], i);\n reverse(st[i].begin(), st[i].end());\n st[i].pop_back();\n }\n int lastout = -1;\n while (!pq.empty()) {\n auto [a, idx] = pq.top();\n pq.pop();\n if (lastout > rt[a]) {\n cout << 0 << endl;\n return;\n }\n lastout = rt[a];\n if (!st[idx].empty()) {\n pq.emplace(st[idx].back(), idx);\n st[idx].pop_back();\n }\n }\n ll ans = 0;\n for (int i = 0; i <= k - 1 - needcut; i++) {\n ans += nCk(n - 1 - cont - needcut, i);\n ans %= mod;\n }\n cout << ans << endl;\n}\n\nint main() {\n fact.resize(10001);\n ifact.resize(10001);\n fact[0] = 1;\n for (int i = 1; i < 10001; i++) {\n fact[i] = fact[i - 1] * i % mod;\n }\n ifact.back() = powmod(fact.back(), mod - 2);\n for (int i = 10000; i > 0; i--) {\n ifact[i - 1] = ifact[i] * i % mod;\n }\n while (true) {\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) break;\n solve(n, k);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 4096, "score_of_the_acc": -0.025, "final_rank": 8 }, { "submission_id": "aoj_1659_10675338", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n\nusing namespace std;\nusing ll = long long;\n\n#include<queue>\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\n\n#include <utility>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n unsigned int umod() const { return _m; }\n\n unsigned int mul(unsigned int a, unsigned int b) const {\n\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned long long y = x * _m;\n return (unsigned int)(z - y + (z < y ? _m : 0));\n }\n};\n\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\nunsigned long long floor_sum_unsigned(unsigned long long n,\n unsigned long long m,\n unsigned long long a,\n unsigned long long b) {\n unsigned long long ans = 0;\n while (true) {\n if (a >= m) {\n ans += n * (n - 1) / 2 * (a / m);\n a %= m;\n }\n if (b >= m) {\n ans += n * (b / m);\n b %= m;\n }\n\n unsigned long long y_max = a * n + b;\n if (y_max < m) break;\n n = (unsigned long long)(y_max / m);\n b = (unsigned long long)(y_max % m);\n std::swap(m, a);\n }\n return ans;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\nusing mint = atcoder::modint998244353;\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n const int B = 1e5;\n vector<mint> fac(B,1),ifac(B,1);\n for(int i = 1;i<B;i++) fac[i] = fac[i-1] * i;\n for(int i = 0;i<B;i++) ifac[i] = fac[i].inv();\n\n while(true){\n int n, k;\n cin>>n>>k;\n if(n==0){\n return 0;\n }\n vector<int> s(n), t(n);\n for(int i = 0;i<n;i++) cin>>s[i];\n for(int i = 0;i<n;i++) cin>>t[i];\n vector<int> idx(n,0);\n for(int i = 0;i<n;i++){\n s[i]--;t[i]--;\n idx[t[i]] = i;\n }\n k--;\n int cnt = 1;\n int use = 0;\n int p = 0;\n int last = s[0];\n vector<vector<int>> all;\n all.push_back({last});\n for(int i = 1;i<n;i++) {\n int g = all.size();\n if(last<s[i]){\n // if(idx[last] > idx[s[i]]) {\n // use++;\n // all.push_back({s[i]});\n // last = s[i];\n // continue;\n // }\n last = s[i];\n p++;\n all[g-1].push_back(last);\n continue;\n }\n if(idx[last]<idx[s[i]]){\n all[g-1].push_back(s[i]);\n continue;\n }\n use++;\n all.push_back({s[i]});\n last = s[i];\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> que;\n int g = all.size();\n for(int i = 0;i<g;i++){\n reverse(all[i].begin(),all[i].end());\n que.push(make_pair(all[i].back(),i));\n all[i].pop_back();\n }\n vector<int> now;\n while(que.size()) {\n auto u = que.top();\n que.pop();\n now.push_back(u.first);\n int ni = u.second;\n if(all[ni].size()>0){\n que.push(make_pair(all[ni].back(),ni));\n all[ni].pop_back();\n }\n }\n mint ans = 0;\n if(now==t) {\n if(use<=k) {\n int can = k - use;\n for(int i = 0;i<=can;i++) {\n if(i <= p) {\n ans += fac[p] * ifac[i] * ifac[p-i];\n }\n }\n }\n }\n cout<<ans.val()<<endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4448, "score_of_the_acc": -0.028, "final_rank": 9 }, { "submission_id": "aoj_1659_10672140", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n#define rep2(i,m,n) for(ll i=ll(m);i<ll(n);i++)\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vvvl = vector<vvl>;\nusing pl = pair<ll,ll>;\nusing vpl = vector<pl>;\nusing vvpl = vector<vpl>;\n#define pb push_back\n\nconst long double EPS = 0.0000000001;\nconst ll INF = 1000000000000000000;\nconst double pi = std::acos(-1.0);\n\n__int128 read_int128(){ //__int128を入力する\n string S;\n cin >> S;\n int N = S.size();\n int st = 0;\n bool minus = false;\n if(S[0] == '-'){\n minus = true;\n st = 1;\n }\n __int128 res = 0;\n rep2(i,st,N) res = res*10+int(S[i]-'0');\n if(minus) res *= -1;\n return res;\n}\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { //これでcoutで__int128を出力できるように\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\nvoid Yes(){ cout << \"Yes\" << endl; } //文字列\"Yes\"を標準出力\nvoid No(){ cout << \"No\" << endl; } //文字列\"No\"を標準出力\n\ntemplate<class T> bool chmin(T& a,T b){\n if(a > b){\n a = b;\n return true;\n }\n else return false;\n}\n\ntemplate<class T> bool chmax(T& a,T b){\n if(a < b){\n a = b;\n return true;\n }\n else return false;\n}\n\nconst long long int MAX = 510000; //最大値\nconst long long int MOD = 998244353;\n\nlong long int fac[MAX], finv[MAX], inv[MAX];\n\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for(int i=2;i<MAX;i++){\n fac[i] = fac[i-1]*i%MOD;\n inv[i] = MOD-inv[MOD%i]*(MOD/i)%MOD;\n finv[i] = finv[i-1]*inv[i]%MOD;\n }\n}\n\nlong long int COM(long long int n,long long int k){\n if(n<k) return 0;\n if(n<0||k<0) return 0;\n return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;\n}\n\nint main(){\n COMinit();\n while(1){\n ll N,K;\n cin >> N >> K;\n if(N == 0 && K == 0) break;\n vector<bool> myturn(N,false);\n vl s(N),t(N);\n rep(i,N) cin >> s[i];\n rep(i,N) cin >> t[i];\n rep(i,N) s[i]--;\n rep(i,N) t[i]--;\n myturn[s[0]] = true;\n vector<int> border(N,0);\n border[0] = 1;\n vl pos(N);\n rep(i,N) pos[s[i]] = i;\n bool ok = true;\n rep(i,N){\n rep2(j,i+1,N){\n if(t[i] > t[j]){\n if(myturn[t[j]]){\n ok = false;\n break;\n }\n else{\n if(border[pos[t[j]]] == 1){\n ok = false;\n break;\n }\n border[pos[t[j]]] = -1;\n }\n }\n }\n if(myturn[t[i]]){\n myturn[t[i]] = false;\n if(pos[t[i]]+1 < N) myturn[s[pos[t[i]]+1]] = true;\n }\n else{\n border[pos[t[i]]] = 1;\n if(pos[t[i]]+1 < N) myturn[s[pos[t[i]]+1]] = true;\n }\n if(!ok) break;\n }\n if(!ok) cout << 0 << endl;\n else{\n ll exist = 0;\n ll free = 0;\n rep(i,N){\n if(border[i] == 1) exist++;\n else if(border[i] == 0) free++;\n }\n if(exist > K) cout << 0 << endl;\n else{\n ll ans = 0;\n rep(i,K-exist+1){\n ans += COM(free,i);\n ans %= MOD;\n }\n cout << ans << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 1440, "memory_kb": 15488, "score_of_the_acc": -0.6147, "final_rank": 18 }, { "submission_id": "aoj_1659_10668110", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\nusing ll = long long;\nll INF = 2e18;\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define fi first\n#define se second\n#define endl \"\\n\" // コメントアウトで随時出力\n\n/*=========================================mint=======================================*/\nconst int mintMod = MOD2; // ここを変更\nclass mint\n{\n long long x;\n\npublic:\n mint(long long x = 0) : x((x % mintMod + mintMod) % mintMod) {}\n long long val() const { return x; }\n mint operator-() const\n {\n return mint(-x);\n }\n mint &operator+=(const mint &a)\n {\n if ((x += a.x) >= mintMod)\n x -= mintMod;\n return *this;\n }\n mint &operator-=(const mint &a)\n {\n if ((x += mintMod - a.x) >= mintMod)\n x -= mintMod;\n return *this;\n }\n mint &operator*=(const mint &a)\n {\n (x *= a.x) %= mintMod;\n return *this;\n }\n mint operator+(const mint &a) const\n {\n mint res(*this);\n return res += a;\n }\n mint operator-(const mint &a) const\n {\n mint res(*this);\n return res -= a;\n }\n mint operator*(const mint &a) const\n {\n mint res(*this);\n return res *= a;\n }\n mint pow(ll t) const\n {\n if (!t)\n return 1;\n mint a = pow(t >> 1);\n a *= a;\n if (t & 1)\n a *= *this;\n return a;\n }\n // for prime mod\n mint inv() const\n {\n return pow(mintMod - 2);\n }\n mint &operator/=(const mint &a)\n {\n return (*this) *= a.inv();\n }\n mint operator/(const mint &a) const\n {\n mint res(*this);\n return res /= a;\n }\n\n friend ostream &operator<<(ostream &os, const mint &m)\n {\n os << m.x;\n return os;\n }\n friend istream &operator>>(istream &is, mint &m)\n {\n long long v;\n is >> v;\n m = mint(v);\n return is;\n }\n};\n/*========================================================================================*/\n\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\n\ntemplate <typename T>\nusing vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます\ntemplate <typename T>\nusing vv = vc<vc<T>>;\n\n// 提出の際はコメントアウト\n#define debug_x(x) cout << #x << \" = \" << (x) << endl\n#define debug_vc(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) cout << v[i] << ' '; \\\n cout << endl; \\\n } // 一次元配列を出力する\n#define debug_vv(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) \\\n { \\\n rep(j, size(v[i])) { cout << v[i][j] << ' '; } \\\n cout << endl; \\\n } \\\n } // 二次元配列を出力する\n\n// #define debug_x(x) \\\n// do \\\n// { \\\n// } while (0)\n// #define debug_vc(v) \\\n// do \\\n// { \\\n// } while (0)\n// #define debug_vv(v) \\\n// do \\\n// { \\\n// } while (0)\n\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = 1; i <= (n); ++i)\n#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define nfor(i, s, n) for (ll i = s; i < n; i++) // i=s,s+1...n-1 ノーマルfor\n#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる\n\nusing ld = long double;\nusing bl = bool;\n\ntemplate <class T>\nusing pq = priority_queue<T, vc<T>>; // 大きい順\ntemplate <class T>\nusing pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順\n\nusing vl = vc<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vv<vl>;\nusing vvvvl = vv<vvl>;\nusing vs = vc<string>;\nusing vvs = vv<string>;\nusing vb = vc<bl>;\nusing vvb = vv<bl>;\nusing vvvb = vv<vb>;\nusing vld = vc<ld>;\nusing vvld = vv<ld>;\nusing vvvld = vv<vld>;\nusing P = pair<ll, ll>;\nusing vP = vc<P>;\nusing vvP = vc<vP>;\nusing vvvP = vv<vP>;\nusing vmint = vc<mint>;\nusing vvmint = vv<mint>;\nusing vvvmint = vv<vmint>;\n\n#define pb push_back\n#define eb emplace_back\n\n#define YES cout << \"Yes\" << endl\n#define NO cout << \"No\" << endl\n#define YN \\\n { \\\n cout << \"Yes\" << endl; \\\n } \\\n else \\\n { \\\n cout << \"No\" << endl; \\\n } // if(a==b)YN;\n#define dame cout << -1 << endl\n\nstring dir = \"DRUL\"; // 第4象限の場合\nvl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};\nvl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};\n\nbool out_grid(ll i, ll j, ll h, ll w)\n{ // trueならcontinue\n return (!(0 <= i && i < h && 0 <= j && j < w));\n}\n\n// using mint = atcoder::modint998244353;\n\nconst int COM_MAX = 510000;\nmint fac[COM_MAX], finv[COM_MAX], inv[COM_MAX];\n\n// テーブルを作る前処理\nvoid COMinit()\n{\n const int MOD = mintMod;\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < COM_MAX; i++)\n {\n fac[i] = fac[i - 1] * i;\n inv[i] = mint(MOD) - inv[MOD % i] * (MOD / i);\n finv[i] = finv[i - 1] * inv[i];\n }\n}\n\n// 二項係数計算\nmint COM(int n, int k)\n{\n if (n < k)\n return 0;\n if (n < 0 || k < 0)\n return 0;\n return fac[n] * finv[k] * finv[n - k];\n}\n\n/*int main() {\n // 前処理\n COMinit();\n\n // 計算例\n cout << COM(100000, 50000).val() << endl;\n}*/\n\nll n, k;\n\nvoid solve()\n{\n vl s(n), t(n);\n vl invT(n + 1);\n rep(i, n)\n {\n cin >> s[i];\n }\n rep(i, n)\n {\n cin >> t[i];\n invT[t[i]] = i;\n }\n vl div;\n vvl crew(k);\n ll idx = 0;\n ll maxSubCrew = -1;\n ll cand = 0;\n rep(i, n - 1)\n {\n if (idx >= k)\n {\n cout << 0 << endl;\n return;\n }\n crew[idx].pb(s[i]);\n maxSubCrew = max(s[i], maxSubCrew);\n // マージ後に転倒\n if (i + 1 < n && invT[s[i]] > invT[s[i + 1]])\n {\n idx++;\n maxSubCrew = -1;\n }\n else\n {\n if (i + 1 < n && maxSubCrew < s[i + 1])\n {\n cand++;\n }\n }\n }\n if (idx >= k)\n {\n cout << 0 << endl;\n return;\n }\n crew[idx].pb(s[n-1]);\n\n vl see(k);\n vl create;\n rep(i,n){\n ll minVal = INF;\n ll minIdx = -1;\n rep(j,k){\n if(crew[j].size()<=see[j])continue;\n if(minVal > crew[j][see[j]]){\n minVal = crew[j][see[j]];\n minIdx = j;\n }\n }\n create.pb(minVal);\n see[minIdx]++;\n }\n if(t!=create){\n cout << 0 << endl;\n return;\n }\n\n mint ans = 0;\n rep(i,k-idx){\n ans += COM(cand,i);\n }\n cout << ans.val() << endl;\n\n return;\n}\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n COMinit();\n while (true)\n {\n cin >> n >> k;\n if (n == 0)\n break;\n solve();\n }\n return 0;\n}\n\n/*\nshift+alt+Fで成形\nf2で変数名変更\n\nスニペット\nbasic\n m:atcoder以外用\n ma:ABC/ARC用\n mahc:AHC用\nmath\n floor:床関数\n ceil:天井関数\n comv:二項係数\n modpow: a^b mod m\n GCD:最大公約数\n extGCD:拡張ユークリッド互除法\n PFD:素因数分解\n isprime:エラトステネスの篩\n matrixpow:行列累乗\ndata_structure\n seg:セグメント木\ntechnic\n Compress:座標圧縮\n runlength:ランレングス圧縮\ngraph\n warshall:ワーシャルフロイド法\n dfs:深さ優先探索\n bfs:幅優先探索\n LCA:最近共通祖先\n*/", "accuracy": 1, "time_ms": 1220, "memory_kb": 15932, "score_of_the_acc": -0.5583, "final_rank": 17 }, { "submission_id": "aoj_1659_10666284", "code_snippet": "// # pragma GCC target(\"avx2\")\n// # pragma GCC optimize(\"O3\")\n// # pragma GCC optimize(\"unroll-loops\")\n// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\n#include <atcoder/modint>\nusing namespace atcoder;\nusing mint = modint998244353;\nconst ll MOD = 998244353;\n// // using mint = modint;\n// // const ll MOD = 998244353;\n// // using mint = modint1000000007;\n// // const ll MOD = 1000000007;\nusing vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n// #define rep(i, n) for (int i = 0; i < (n); i++)\n#define rep2(i, a, b) for (int i = (a); i < (b); i++)\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing lll = __int128;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nconst long long INF = 1000000000000000005LL;\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\nmint fac[MAX], finv[MAX], inv[MAX];\nvoid nCrprep() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (ll i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i;\n inv[i] = MOD - inv[MOD%i] * (MOD / i);\n finv[i] = finv[i - 1] * inv[i];\n }\n}\nmint nCr(ll n, ll r){\n if (n < r) return 0;\n if (n < 0 || r < 0) return 0;\n return fac[n] * (finv[r] * finv[n - r] );\n}\nll nCrcheep(ll n,ll r){\n if(r == 0) return 1;\n else if(r == 1) return n;\n else return nCrcheep(n-1,r-1)*n/r;\n}\n\nvoid solve(ll n, ll k) {\n k--;\n vl s(n), t(n);\n rep(i, 0, n) {\n cin >> s[i];\n s[i]--;\n }\n rep(i, 0, n) {\n cin >> t[i];\n t[i]--;\n }\n\n vl ss = s;\n vl sid(n);\n rep(i, 0, n) {\n sid[s[i]] = i;\n }\n \n ll m = 0;\n vl mcut;\n rep(i, 0, n) {\n if(sid[t[i]] != 0 && ss[sid[t[i]] - 1] != -1) {\n m++;\n mcut.emplace_back(sid[t[i]]);\n }\n ss[sid[t[i]]] = -1;\n }\n\n if(sz(mcut) > k) {\n cout << 0 << endl;\n return ;\n }\n\n sort(all(mcut));\n mcut.emplace_back(n);\n vvl cols;\n ll id = 0;\n rep(i, 0, sz(mcut)) {\n vl curcol;\n while(id < mcut[i]) {\n curcol.emplace_back(s[id]);\n id++;\n }\n reverse(all(curcol));\n cols.emplace_back(curcol);\n }\n mcut.pop_back();\n \n vvl cols2 = cols;\n\n priority_queue<P, vector<P>, greater<P>> pq;\n rep(i, 0, sz(cols)) {\n pq.push(P({cols[i].back(), i}));\n cols[i].pop_back();\n }\n \n vl tt;\n rep(i, 0, n) {\n auto [v, coli] = pq.top();\n pq.pop();\n tt.emplace_back(v);\n if(!cols[coli].empty()) {\n pq.push(P({cols[coli].back(), coli}));\n cols[coli].pop_back();\n }\n }\n \n if(tt != t) {\n cout << 0 << endl;\n return ;\n }\n\n cols = cols2;\n ll c = 0;\n for(auto col : cols) {\n reverse(all(col));\n ll curmax = col[0];\n rep(i, 1, sz(col)) {\n if(curmax < col[i]) c++;\n chmax(curmax, col[i]);\n }\n }\n\n mint ans = 0;\n rep(i, 0, k - m + 1) ans += nCr(c, i);\n\n cout << ans.val() << endl;\n\n return ;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n nCrprep();\n while(true) {\n ll n, k;\n cin >> n >> k;\n if(n == 0 && k == 0) return 0;\n solve(n, k);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 63516, "score_of_the_acc": -1.0145, "final_rank": 19 }, { "submission_id": "aoj_1659_10663368", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < (ll)n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n/**\n * @brief 拡張ユークリッドの互除法\n */\nll ext_gcd(ll a, ll b, ll &x, ll &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = ext_gcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\n/**\n * @brief 負に対応した mod\n */\ninline ll mmod(ll a, ll mod) {\n return (a % mod + mod) % mod;\n}\n/**\n * @brief 法がmodのときのaの逆元を求める\n * @remark aとmodが互いに素である必要がある\n */\nll inv(ll a, ll mod) {\n ll x, y;\n ext_gcd(a, mod, x, y);\n return mmod(x, mod);\n}\n\nll pow(ll a, ll b) {\n ll res = 1;\n while (b > 0) {\n if (b & 1) res = res * a;\n a = a * a;\n b >>= 1;\n }\n return res;\n}\nll pow(ll a, ll b, ll mod) {\n\tbool inverse = b < 0;\n\tif (inverse) b = -b;\n ll res = 1;\n while (b > 0) {\n if (b & 1) res = res * a % mod;\n a = a * a % mod;\n b >>= 1;\n }\n return inverse ? inv(res, mod) : res;\n}\n\nll nCrDP[67][67];\nll nCr(ll n, ll r) {\n assert(n < 67 && r < 67);\n assert(n >= r);\n assert(n >= 0 && r >= 0);\n if (nCrDP[n][r] != 0) return nCrDP[n][r];\n if (r == 0 || n == r) return 1;\n return nCrDP[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r);\n}\nll nHr(ll n, ll r) {\n return nCr(n + r - 1, r);\n}\n\nvector<ll> fact;\nvoid calc_fact(ll size) {\n assert(size <= 20);\n fact = vector<ll>(size + 1, 0);\n fact[0] = 1;\n for (int i = 0; i < size; ++i)\n fact[i + 1] = fact[i] * (i + 1);\n}\nunordered_map<ll, vector<ll>> modfact, modinvfact;\nvoid calc_fact(ll size, ll mod) {\n\tif (modfact.count(mod) && modfact[mod].size() - 1 > size) return;\n\n\tll oldsize = max(0, (int)modfact[mod].size() - 1);\n\tmodfact[mod].resize(size + 1, 1);\n\tmodinvfact[mod].resize(size + 1, 1);\n\n for (int i = oldsize; i < size; ++i)\n modfact[mod][i + 1] = modfact[mod][i] * (i + 1) % mod;\n modinvfact[mod][size] = inv(modfact[mod][size], mod);\n for (int i = size - 1; i >= oldsize; --i)\n modinvfact[mod][i] = modinvfact[mod][i + 1] * (i + 1) % mod;\n}\nll nCr(ll n, ll r, ll mod, ll dp_size = 500000LL) {\n assert(n >= r);\n assert(n >= 0 && r >= 0);\n\tcalc_fact(max(n, dp_size), mod);\n return modfact[mod][n] * modinvfact[mod][r] % mod * modinvfact[mod][n - r] % mod;\n}\nll nHr(ll n, ll r, ll mod, ll dp_size = 500000LL) {\n return nCr(n + r - 1, r, mod, dp_size);\n}\n\nstruct mint\n{\n private:\n long long n;\n long long mod;\n\n public:\n static long long default_mod;\n\n\tmint() : n(0), mod(default_mod == 0 ? 998244353 : default_mod) {}\n mint(const mint &m) {\n n = m.n;\n mod = m.mod;\n }\n mint(long long n, long long mod = default_mod) {\n if (default_mod == 0) {\n\t\t\tdefault_mod = mod == 0 ? 998244353 : mod;\n\t\t\tmod = default_mod;\n\t\t}\n assert(1 <= mod);\n\n this->n = (n % mod + mod) % mod;\n this->mod = mod;\n }\n\n mint inv() const {\n assert(gcd(n, mod) == 1);\n auto ext_gcd = [&](auto f, long long a, long long b, long long &x, long long &y) -> long long {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n long long d = f(f, b, a % b, y, x);\n y -= a / b * x;\n return d;\n };\n\n long long x, y;\n ext_gcd(ext_gcd, n, mod, x, y);\n return mint((x % mod + mod) % mod, mod);\n }\n\n mint pow(long long exp) const {\n bool inverse = exp < 0;\n if (inverse) exp = -exp;\n\n\t\tll a = n;\n ll res = 1;\n while (exp > 0) {\n if (exp & 1) res = res * a % mod;\n a = a * a % mod;\n exp >>= 1;\n }\n return (inverse ? mint(res, mod).inv() : mint(res, mod));\n }\n\n\tmint &operator=(const mint &o) {\n\t\tn = o.n;\n\t\tmod = o.mod;\n\t\treturn *this;\n\t}\n\n\tmint operator+() const { return *this; }\n\tmint operator-() const { return 0 - *this; }\n\n\tmint &operator++() {\n\t\tn++;\n\t\tif (n == mod) n = 0;\n\t\treturn *this;\n\t}\n\tmint &operator--() {\n\t\tif (n == 0) n = mod;\n\t\tn--;\n\t\treturn *this;\n\t}\n\tmint operator++(int) {\n\t\tmint res = *this;\n\t\t++*this;\n\t\treturn res;\n\t}\n\tmint operator--(int) {\n\t\tmint res = *this;\n\t\t--*this;\n\t\treturn res;\n\t}\n\n mint &operator+=(const mint &o) {\n assert(mod == o.mod);\n n += o.n;\n if (n >= mod) n -= mod;\n return *this;\n }\n mint &operator-=(const mint &o) {\n assert(mod == o.mod);\n n += mod - o.n;\n if (n >= mod) n -= mod;\n return *this;\n }\n mint &operator*=(const mint &o) {\n assert(mod == o.mod);\n n = n * o.n % mod;\n return *this;\n }\n mint &operator/=(const mint &o) {\n assert(mod == o.mod);\n n = n * o.inv().n % mod;\n return *this;\n }\n friend mint operator+(const mint &a, const mint &b) {\n return mint(a) += b;\n }\n friend mint operator-(const mint &a, const mint &b) {\n return mint(a) -= b;\n }\n friend mint operator*(const mint &a, const mint &b) {\n return mint(a) *= b;\n }\n friend mint operator/(const mint &a, const mint &b) {\n return mint(a) /= b;\n }\n\n\tfriend bool operator==(const mint &a, const mint &b) {\n\t\treturn a.n == b.n && a.mod == b.mod;\n\t}\n\tfriend bool operator!=(const mint &a, const mint &b) {\n\t\treturn a.n != b.n || a.mod != b.mod;\n\t}\n\n friend ostream &operator<<(ostream &os, const mint &m) {\n os << m.n;\n return os;\n }\n};\nlong long mint::default_mod = 0;\n\n\n\n\n\nint solve() {\n\tll n, k; cin >> n >> k;\n\tif (n == 0 && k == 0) return 1;\n\tvll s(n), t(n); cin >> s >> t;\n\n\tk--;\n\n\tvector<PLL> q;\n\trep(i, n) {\n\t\tll j = i;\n\t\twhile (j + 1 < n && t[i] > t[j + 1]) j++;\n\n\t\tbool ok = false;\n\t\trep(l, s.size()) {\n\t\t\tif (s[l] != t[i]) continue;\n\t\t\tif (s.size() <= l + (j - i)) break;\n\t\t\tbool match = true;\n\t\t\trep(m, j - i + 1) {\n\t\t\t\tif (s[l + m] != t[i + m]) {\n\t\t\t\t\tmatch = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (match) {\n\t\t\t\tok = true;\n\t\t\t\tq.push_back({l, s[l]});\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tif (!ok) {\n\t\t\tcout << 0 << \"\\n\";\n\t\t\treturn 0;\n\t\t}\n\n\t\ti = j;\n\t}\n\n\n\tsort(all(q));\n\n\tll canCut = q.size() - 1;\n\trep(i, q.size() - 1) {\n\t\tif (q[i].second > q[i + 1].second) {\n\t\t\tcanCut--;\n\t\t\tk--;\n\t\t}\n\t}\n\n\tif (k < 0) {\n\t\tcout << 0 << \"\\n\";\n\t\treturn 0;\n\t}\n\n\t\n\tmint ans = 0;\n\trep(i, min(k, canCut) + 1) {\n\t\tans += nCr(canCut, i, MOD);\n\t}\n\n\tcout << ans << \"\\n\";\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 11644, "score_of_the_acc": -0.1971, "final_rank": 11 }, { "submission_id": "aoj_1659_10662123", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef __int128 lll;\nusing ull = unsigned long long;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\ntemplate<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using pqmax = priority_queue<T>;\nconst ll inf=LLONG_MAX/3;\nconst ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\nconst ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};\n#define mp make_pair\n#define pb push_back\n#define eb emplace_back\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define si(x) ll(x.size())\n#define rept(n) for(ll _ovo_=0;_ovo_<n;_ovo_++)\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define per(i,n) for(ll i=n-1;i>=0;i--)\n#define rng(i,l,r) for(ll i=l;i<r;i++)\n#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)\n#define fore(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\ntemplate<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define VLL(name,size) vector<ll>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define SUM(...) accumulate(all(__VA_ARGS__),0LL)\ntemplate<class T> auto min(const T& a){ return *min_element(all(a)); }\ntemplate<class T> auto max(const T& a){ return *max_element(all(a)); }\ntemplate<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }\ntemplate<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }\ntemplate<class T, class F = less<>> map<T,vector<ll> > ivm(vector<T>& a, F b = F{}){ map<T,vector<ll> > ret; rep(i,si(a))ret[a[i]].push_back(i); return ret;}\ntemplate<class T, class F = less<>> map<T,ll> ivc(vector<T>& a, F b = F{}){ map<T,ll> ret; rep(i,si(a))ret[a[i]]++; return ret;}\ntemplate<class T, class F = less<>> vector<T> ivp(vector<T> a){ vector<ll> ret(si(a)); rep(i,si(a))ret[a[i]] = i; return ret;}\ntemplate<class T, class F = less<>> vector<ll> rev(vector<T> a){ reverse(all(a)); return a;}\ntemplate<class T, class F = less<>> vector<ll> sortby(vector<T> a, F b = F{}){vector<ll> w = a; sor(w,b); vector<pll> v; rep(i,si(a))v.eb(a[i],i); sor(v); if(w[0] != v[0].first)reverse(all(v)); vector<ll> ret; rep(i,si(v))ret.pb(v[i].second); return ret;}\ntemplate<class T, class P> vector<T> filter(vector<T> a,P f){vector<T> ret;rep(i,si(a)){if(f(a[i]))ret.pb(a[i]);}return ret;}\ntemplate<class T, class P> vector<ll> filter_id(vector<T> a,P f){vector<ll> ret;rep(i,si(a)){if(f(a[i]))ret.pb(i);}return ret;}\nll monotone_left(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_left(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}\nll monotone_right(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\nll monotone_right(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}\ndouble monotone_double_left(double l,double r,function<bool(double)> f){assert(f(l) >= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return l;}\ndouble monotone_double_right(double l,double r,function<bool(double)> f){assert(f(l) <= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return r;}\ntemplate<class S> S unimodal_max(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) < f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmax(ret,f(k)); return ret;}\ntemplate<class S> S unimodal_min(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) > f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmin(ret,f(k)); return ret;}\nvector<pll> neighbor4(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,4){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\nvector<pll> neighbor8(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,8){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};\n\nvoid outb(bool x){cout<<(x?\"Yes\":\"No\")<<\"\\n\";}\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))\nll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}\nvector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }\nvector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }\nvll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}\nll powll(lll x,ll y){lll res = 1; while(y){ if(y & 1)res = res * x; x = x * x; y >>= 1;} return res;}\nll powmod(lll x,ll y,lll mod){lll res=1; while(y){ if(y&1)res=res*x%mod; x=x*x%mod; y>>=1;} return res; }\nll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0)u+=m;return u;}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <class T> vector<T> &operator++(vector<T> &v) {\n\t\tfore(e, v) e++;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator++(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e++;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {\n\t\tfore(e, v) e--;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator--(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e--;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {\n\t\tfore(e, r) l.eb(e);\n\t\treturn l;\n}\n\ntemplate<class... Ts> void in(Ts&... t);\n[[maybe_unused]] void print(){}\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts);\ntemplate<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\\n'; }\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }\ntemplate<class T> void o(const T& t){ o(t, P<4>{}); }\ntemplate<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }\ntemplate<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }\ntemplate<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }\ntemplate<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }\ntemplate<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }\ntemplate<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }\n#undef VOID\n}\n#define unpack(a) (void)initializer_list<int>{(a, 0)...}\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }\n#undef unpack\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n \nnamespace atcoder {\n \nnamespace internal {\n \nint ceil_pow2(int n) {\n\tint x = 0;\n\twhile ((1U << x) < (unsigned int)(n)) x++;\n\treturn x;\n}\n \nconstexpr int bsf_constexpr(unsigned int n) {\n\tint x = 0;\n\twhile (!(n & (1 << x))) x++;\n\treturn x;\n}\n \nint bsf(unsigned int n) {\n#ifdef _MSC_VER\n\tunsigned long index;\n\t_BitScanForward(&index, n);\n\treturn index;\n#else\n\treturn __builtin_ctz(n);\n#endif\n}\n \n} // namespace internal\n \n} // namespace atcoder\n \n \n#include <cassert>\n#include <numeric>\n#include <type_traits>\n \n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n \n \n#include <utility>\n \n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n \nnamespace atcoder {\n \nnamespace internal {\n \nconstexpr long long safe_mod(long long x, long long m) {\n\tx %= m;\n\tif (x < 0) x += m;\n\treturn x;\n}\n \nstruct barrett {\n\tunsigned int _m;\n\tunsigned long long im;\n \n\texplicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n \n\tunsigned int umod() const { return _m; }\n \n\tunsigned int mul(unsigned int a, unsigned int b) const {\n \n\t\tunsigned long long z = a;\n\t\tz *= b;\n#ifdef _MSC_VER\n\t\tunsigned long long x;\n\t\t_umul128(z, im, &x);\n#else\n\t\tunsigned long long x =\n\t\t\t(unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n\t\tunsigned int v = (unsigned int)(z - x * _m);\n\t\tif (_m <= v) v += _m;\n\t\treturn v;\n\t}\n};\n \nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n\tif (m == 1) return 0;\n\tunsigned int _m = (unsigned int)(m);\n\tunsigned long long r = 1;\n\tunsigned long long y = safe_mod(x, m);\n\twhile (n) {\n\t\tif (n & 1) r = (r * y) % _m;\n\t\ty = (y * y) % _m;\n\t\tn >>= 1;\n\t}\n\treturn r;\n}\n \nconstexpr bool is_prime_constexpr(int n) {\n\tif (n <= 1) return false;\n\tif (n == 2 || n == 7 || n == 61) return true;\n\tif (n % 2 == 0) return false;\n\tlong long d = n - 1;\n\twhile (d % 2 == 0) d /= 2;\n\tconstexpr long long bases[3] = {2, 7, 61};\n\tfor (long long a : bases) {\n\t\tlong long t = d;\n\t\tlong long y = pow_mod_constexpr(a, t, n);\n\t\twhile (t != n - 1 && y != 1 && y != n - 1) {\n\t\t\ty = y * y % n;\n\t\t\tt <<= 1;\n\t\t}\n\t\tif (y != n - 1 && t % 2 == 0) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n \nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n\ta = safe_mod(a, b);\n\tif (a == 0) return {b, 0};\n \n\tlong long s = b, t = a;\n\tlong long m0 = 0, m1 = 1;\n \n\twhile (t) {\n\t\tlong long u = s / t;\n\t\ts -= t * u;\n\t\tm0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n \n \n\t\tauto tmp = s;\n\t\ts = t;\n\t\tt = tmp;\n\t\ttmp = m0;\n\t\tm0 = m1;\n\t\tm1 = tmp;\n\t}\n\tif (m0 < 0) m0 += b / s;\n\treturn {s, m0};\n}\n \nconstexpr int primitive_root_constexpr(int m) {\n\tif (m == 2) return 1;\n\tif (m == 167772161) return 3;\n\tif (m == 469762049) return 3;\n\tif (m == 754974721) return 11;\n\tif (m == 998244353) return 3;\n\tint divs[20] = {};\n\tdivs[0] = 2;\n\tint cnt = 1;\n\tint x = (m - 1) / 2;\n\twhile (x % 2 == 0) x /= 2;\n\tfor (int i = 3; (long long)(i)*i <= x; i += 2) {\n\t\tif (x % i == 0) {\n\t\t\tdivs[cnt++] = i;\n\t\t\twhile (x % i == 0) {\n\t\t\t\tx /= i;\n\t\t\t}\n\t\t}\n\t}\n\tif (x > 1) {\n\t\tdivs[cnt++] = x;\n\t}\n\tfor (int g = 2;; g++) {\n\t\tbool ok = true;\n\t\tfor (int i = 0; i < cnt; i++) {\n\t\t\tif (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (ok) return g;\n\t}\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n \nunsigned long long floor_sum_unsigned(unsigned long long n,\n\t\t\t\t\t\t\t\t\t\tunsigned long long m,\n\t\t\t\t\t\t\t\t\t\tunsigned long long a,\n\t\t\t\t\t\t\t\t\t\tunsigned long long b) {\n\tunsigned long long ans = 0;\n\twhile (true) {\n\t\tif (a >= m) {\n\t\t\tans += n * (n - 1) / 2 * (a / m);\n\t\t\ta %= m;\n\t\t}\n\t\tif (b >= m) {\n\t\t\tans += n * (b / m);\n\t\t\tb %= m;\n\t\t}\n \n\t\tunsigned long long y_max = a * n + b;\n\t\tif (y_max < m) break;\n\t\tn = (unsigned long long)(y_max / m);\n\t\tb = (unsigned long long)(y_max % m);\n\t\tstd::swap(m, a);\n\t}\n\treturn ans;\n}\n \n} // namespace internal\n \n} // namespace atcoder\n \n \n#include <cassert>\n#include <numeric>\n#include <type_traits>\n \nnamespace atcoder {\n \nnamespace internal {\n \n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n\ttypename std::conditional<std::is_same<T, __int128_t>::value ||\n\t\t\t\t\t\t\t\t\tstd::is_same<T, __int128>::value,\n\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing is_unsigned_int128 =\n\ttypename std::conditional<std::is_same<T, __uint128_t>::value ||\n\t\t\t\t\t\t\t\t\tstd::is_same<T, unsigned __int128>::value,\n\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing make_unsigned_int128 =\n\ttypename std::conditional<std::is_same<T, __int128_t>::value,\n\t\t\t\t\t\t\t\t__uint128_t,\n\t\t\t\t\t\t\t\tunsigned __int128>;\n \ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n\t\t\t\t\t\t\t\t\t\t\t\t\tis_signed_int128<T>::value ||\n\t\t\t\t\t\t\t\t\t\t\t\t\tis_unsigned_int128<T>::value,\n\t\t\t\t\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n\t\t\t\t\t\t\t\t\t\t\t\t std::is_signed<T>::value) ||\n\t\t\t\t\t\t\t\t\t\t\t\t\tis_signed_int128<T>::value,\n\t\t\t\t\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing is_unsigned_int =\n\ttypename std::conditional<(is_integral<T>::value &&\n\t\t\t\t\t\t\t\t std::is_unsigned<T>::value) ||\n\t\t\t\t\t\t\t\t\tis_unsigned_int128<T>::value,\n\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n\tis_signed_int128<T>::value,\n\tmake_unsigned_int128<T>,\n\ttypename std::conditional<std::is_signed<T>::value,\n\t\t\t\t\t\t\t\tstd::make_unsigned<T>,\n\t\t\t\t\t\t\t\tstd::common_type<T>>::type>::type;\n \n#else\n \ntemplate <class T> using is_integral = typename std::is_integral<T>;\n \ntemplate <class T>\nusing is_signed_int =\n\ttypename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing is_unsigned_int =\n\ttypename std::conditional<is_integral<T>::value &&\n\t\t\t\t\t\t\t\t\tstd::is_unsigned<T>::value,\n\t\t\t\t\t\t\t\tstd::true_type,\n\t\t\t\t\t\t\t\tstd::false_type>::type;\n \ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n\t\t\t\t\t\t\t\t\t\t\t\tstd::make_unsigned<T>,\n\t\t\t\t\t\t\t\t\t\t\t\tstd::common_type<T>>::type;\n \n#endif\n \ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n \ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n \ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n \n} // namespace internal\n \n} // namespace atcoder\n \n \nnamespace atcoder {\n \nnamespace internal {\n \nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n \ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n \n} // namespace internal\n \ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n\tusing mint = static_modint;\n \n\tpublic:\n\tstatic constexpr int mod() { return m; }\n\tstatic mint raw(int v) {\n\t\tmint x;\n\t\tx._v = v;\n\t\treturn x;\n\t}\n \n\tstatic_modint() : _v(0) {}\n\ttemplate <class T, internal::is_signed_int_t<T>* = nullptr>\n\tstatic_modint(T v) {\n\t\tlong long x = (long long)(v % (long long)(umod()));\n\t\tif (x < 0) x += umod();\n\t\t_v = (unsigned int)(x);\n\t}\n\ttemplate <class T, internal::is_unsigned_int_t<T>* = nullptr>\n\tstatic_modint(T v) {\n\t\t_v = (unsigned int)(v % umod());\n\t}\n \n\tunsigned int val() const { return _v; }\n \n\tmint& operator++() {\n\t\t_v++;\n\t\tif (_v == umod()) _v = 0;\n\t\treturn *this;\n\t}\n\tmint& operator--() {\n\t\tif (_v == 0) _v = umod();\n\t\t_v--;\n\t\treturn *this;\n\t}\n\tmint operator++(int) {\n\t\tmint result = *this;\n\t\t++*this;\n\t\treturn result;\n\t}\n\tmint operator--(int) {\n\t\tmint result = *this;\n\t\t--*this;\n\t\treturn result;\n\t}\n \n\tmint& operator+=(const mint& rhs) {\n\t\t_v += rhs._v;\n\t\tif (_v >= umod()) _v -= umod();\n\t\treturn *this;\n\t}\n\tmint& operator-=(const mint& rhs) {\n\t\t_v -= rhs._v;\n\t\tif (_v >= umod()) _v += umod();\n\t\treturn *this;\n\t}\n\tmint& operator*=(const mint& rhs) {\n\t\tunsigned long long z = _v;\n\t\tz *= rhs._v;\n\t\t_v = (unsigned int)(z % umod());\n\t\treturn *this;\n\t}\n\tmint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n\tmint operator+() const { return *this; }\n\tmint operator-() const { return mint() - *this; }\n \n\tmint pow(long long n) const {\n\t\tassert(0 <= n);\n\t\tmint x = *this, r = 1;\n\t\twhile (n) {\n\t\t\tif (n & 1) r *= x;\n\t\t\tx *= x;\n\t\t\tn >>= 1;\n\t\t}\n\t\treturn r;\n\t}\n\tmint inv() const {\n\t\tif (prime) {\n\t\t\tassert(_v);\n\t\t\treturn pow(umod() - 2);\n\t\t} else {\n\t\t\tauto eg = internal::inv_gcd(_v, m);\n\t\t\tassert(eg.first == 1);\n\t\t\treturn eg.second;\n\t\t}\n\t}\n \n\tfriend mint operator+(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) += rhs;\n\t}\n\tfriend mint operator-(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) -= rhs;\n\t}\n\tfriend mint operator*(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) *= rhs;\n\t}\n\tfriend mint operator/(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) /= rhs;\n\t}\n\tfriend bool operator==(const mint& lhs, const mint& rhs) {\n\t\treturn lhs._v == rhs._v;\n\t}\n\tfriend bool operator!=(const mint& lhs, const mint& rhs) {\n\t\treturn lhs._v != rhs._v;\n\t}\n \n\tprivate:\n\tunsigned int _v;\n\tstatic constexpr unsigned int umod() { return m; }\n\tstatic constexpr bool prime = internal::is_prime<m>;\n};\n \ntemplate <int id> struct dynamic_modint : internal::modint_base {\n\tusing mint = dynamic_modint;\n \n\tpublic:\n\tstatic int mod() { return (int)(bt.umod()); }\n\tstatic void set_mod(int m) {\n\t\tassert(1 <= m);\n\t\tbt = internal::barrett(m);\n\t}\n\tstatic mint raw(int v) {\n\t\tmint x;\n\t\tx._v = v;\n\t\treturn x;\n\t}\n \n\tdynamic_modint() : _v(0) {}\n\ttemplate <class T, internal::is_signed_int_t<T>* = nullptr>\n\tdynamic_modint(T v) {\n\t\tlong long x = (long long)(v % (long long)(mod()));\n\t\tif (x < 0) x += mod();\n\t\t_v = (unsigned int)(x);\n\t}\n\ttemplate <class T, internal::is_unsigned_int_t<T>* = nullptr>\n\tdynamic_modint(T v) {\n\t\t_v = (unsigned int)(v % mod());\n\t}\n \n\tunsigned int val() const { return _v; }\n \n\tmint& operator++() {\n\t\t_v++;\n\t\tif (_v == umod()) _v = 0;\n\t\treturn *this;\n\t}\n\tmint& operator--() {\n\t\tif (_v == 0) _v = umod();\n\t\t_v--;\n\t\treturn *this;\n\t}\n\tmint operator++(int) {\n\t\tmint result = *this;\n\t\t++*this;\n\t\treturn result;\n\t}\n\tmint operator--(int) {\n\t\tmint result = *this;\n\t\t--*this;\n\t\treturn result;\n\t}\n \n\tmint& operator+=(const mint& rhs) {\n\t\t_v += rhs._v;\n\t\tif (_v >= umod()) _v -= umod();\n\t\treturn *this;\n\t}\n\tmint& operator-=(const mint& rhs) {\n\t\t_v += mod() - rhs._v;\n\t\tif (_v >= umod()) _v -= umod();\n\t\treturn *this;\n\t}\n\tmint& operator*=(const mint& rhs) {\n\t\t_v = bt.mul(_v, rhs._v);\n\t\treturn *this;\n\t}\n\tmint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n\tmint operator+() const { return *this; }\n\tmint operator-() const { return mint() - *this; }\n \n\tmint pow(long long n) const {\n\t\tassert(0 <= n);\n\t\tmint x = *this, r = 1;\n\t\twhile (n) {\n\t\t\tif (n & 1) r *= x;\n\t\t\tx *= x;\n\t\t\tn >>= 1;\n\t\t}\n\t\treturn r;\n\t}\n\tmint inv() const {\n\t\tauto eg = internal::inv_gcd(_v, mod());\n\t\tassert(eg.first == 1);\n\t\treturn eg.second;\n\t}\n \n\tfriend mint operator+(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) += rhs;\n\t}\n\tfriend mint operator-(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) -= rhs;\n\t}\n\tfriend mint operator*(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) *= rhs;\n\t}\n\tfriend mint operator/(const mint& lhs, const mint& rhs) {\n\t\treturn mint(lhs) /= rhs;\n\t}\n\tfriend bool operator==(const mint& lhs, const mint& rhs) {\n\t\treturn lhs._v == rhs._v;\n\t}\n\tfriend bool operator!=(const mint& lhs, const mint& rhs) {\n\t\treturn lhs._v != rhs._v;\n\t}\n \n\tprivate:\n\tunsigned int _v;\n\tstatic internal::barrett bt;\n\tstatic unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n \nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n \nnamespace internal {\n \ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n \ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n \ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n \ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n \n} // namespace internal\n \n} // namespace atcoder\n \nusing namespace atcoder;\nusing mint=modint998244353;\nusing vmint=vector<mint>;\n\n// combination mod prime\n// https://youtu.be/8uowVvQ_-Mo?t=6002\n// https://youtu.be/Tgd_zLfRZOQ?t=9928\nstruct modinv {\n\tint n; vector<mint> d;\n\tmodinv(): n(2), d({0,1}) {}\n\tmint operator()(int i) {\n\twhile (n <= i) d.push_back(-d[mint::mod()%n]*(mint::mod()/n)), ++n;\n\treturn d[i];\n\t}\n\tmint operator[](int i) const { return d[i];}\n} invs;\nstruct modfact {\n\tint n; vector<mint> d;\n\tmodfact(): n(2), d({1,1}) {}\n\tmint operator()(int i) {\n\twhile (n <= i) d.push_back(d.back()*n), ++n;\n\treturn d[i];\n\t}\n\tmint operator[](int i) const { return d[i];}\n} facts;\nstruct modfactinv {\n\tint n; vector<mint> d;\n\tmodfactinv(): n(2), d({1,1}) {}\n\tmint operator()(int i) {\n\twhile (n <= i) d.push_back(d.back()*invs(n)), ++n;\n\treturn d[i];\n\t}\n\tmint operator[](int i) const { return d[i];}\n} ifacts;\nmint comb(int n, int k) {\n\tif (n < k || k < 0) return 0;\n\treturn facts(n)*ifacts(k)*ifacts(n-k);\n}\nmint perm(int n, int k) {\n\tif (n < k || k < 0) return 0;\n\treturn facts(n)*ifacts(n-k);\n}\nmint mcomb(int n,int m){//m objects into n places\n\treturn comb(n+m-1,m);\n}\nbool solve(){\n\tll n,k;\n\tcin>>n>>k;\n\tif(n + k == 0)return false;\n\tvll s(n);\n\tvll t(n);\n\trep(i,n){\n\t\tcin>>s[i];\n\t\ts[i]--;\n\t}\n\trep(i,n){\n\t\tcin>>t[i];\n\t\tt[i]--;\n\t}\n\tvll rs(n);\n\trep(i,n)rs[s[i]] = i;\n\tset<ll> st;\n\t{\n\t\tll bef = -1;\n\t\tbool valid = true;\n\t\trep(i,n){\n\t\t\tif(t[i] > bef){\n\t\t\t\tst.insert(t[i]);\n\t\t\t\tbef = t[i];\n\n\t\t\t\tll j = i, l = rs[t[i]];\n\t\t\t\tbool ok = true;\n\t\t\t\twhile(j < n && t[j] <= t[i]){\n\t\t\t\t\tif(l >= n){\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif(t[j] != s[l]){\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tj++,l++;\n\t\t\t\t}\n\t\t\t\tvalid &= ok;\n\t\t\t}\n\t\t}\n\t\tif(!valid){\n\t\t\tcout<<0<<endl;\n\t\t\treturn true;\n\t\t}\n\t}\n\tll cnt = k - 1;\n\tll rem = 0;\n\t{\n\t\tvll v;\n\t\trep(i,si(s)){\n\t\t\tif(st.count(s[i])){\n\t\t\t\tv.eb(s[i]);\n\t\t\t}\n\t\t}\n\t\trep(i,si(v) - 1){\n\t\t\tcnt -= v[i] > v[i + 1];\n\t\t\trem += v[i] < v[i + 1];\n\t\t}\n\t}\n\tif(cnt < 0){\n\t\tcout<<0<<endl;\n\t\treturn true;\n\t}\n\tmint ans = 0;\n\tfor(ll i = 0;i <= cnt;i++){\n\t\tans += comb(rem,i);\n\t}\n\tcout<<ans.val()<<endl;\n\treturn true;\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4344, "score_of_the_acc": -0.0233, "final_rank": 6 }, { "submission_id": "aoj_1659_10659992", "code_snippet": "#include <bits/stdc++.h>\nnamespace ranges = std::ranges;\nconst long long MOD = 998244353;\nlong long mod_inv(long long v) {\n long long res = 1, e = MOD - 2;\n while (e) {\n if (e & 1) res = (res * v) % MOD;\n v = (v * v) % MOD;\n e >>= 1;\n }\n return res;\n}\nint N, K, S[10010], T[10010];\nstd::vector<std::vector<int>> valid(std::vector<int> can) {\n std::vector<std::vector<int>> A;\n for (int i = 0, j = 0 ; i < N ; i = j) {\n j = can[i];\n std::vector<int> cur;\n for (int k = i ; k < j ; k++) cur.push_back(S[k]);\n A.push_back(std::move(cur));\n }\n if (std::ssize(A) > K) return {};\n std::vector<int> ptr(A.size());\n for (int i = 0 ; i < N ; i++) {\n int mn = -1;\n for (int j = 0 ; j < std::ssize(A) ; j++) if (ptr[j] < std::ssize(A[j])) {\n if (mn == -1 or A[mn][ptr[mn]] > A[j][ptr[j]]) mn = j;\n }\n if (mn == -1 or A[mn][ptr[mn]] != T[i]) return {};\n ptr[mn]++;\n }\n return A;\n}\nconst int MAXNK = 10010;\nlong long F[MAXNK + 1], invF[MAXNK + 1];\nlong long binom(int n, int r) {\n if (r < 0 or r > n) return 0;\n long long res = (F[n] * invF[r]) % MOD;\n res = (res * invF[n - r]) % MOD;\n return res;\n}\nlong long solve() {\n std::vector<int> invS(N), invT(N);\n for (int i = 0 ; i < N ; i++) invS[S[i]] = i;\n for (int i = 0 ; i < N ; i++) invT[T[i]] = i;\n std::vector<int> can(N, N);\n for (int i = 0 ; i < N ; i++) {\n for (int j = i + 1 ; j < N ; j++) {\n if (invT[S[j]] < invT[S[i]]) {\n can[i] = std::min(can[i], j);\n }\n }\n }\n //for (int c : can) std::cout << c + 1 << ' ';\n //std::cout << std::endl;\n for (int i = N - 2 ; i >= 0 ; i--) can[i] = std::min(can[i], can[i + 1]);\n auto part = valid(can);\n if (part.empty()) return 0;\n std::vector<long long> dp(1, 1);\n for (const auto& arr : part) {\n assert(arr.size());\n int mx = arr[0], cnt = 0;\n for (int i = 1 ; i < std::ssize(arr) ; i++) {\n if (mx < arr[i]) {\n cnt++;\n mx = arr[i];\n }\n }\n std::vector<long long> op(cnt + 2);\n std::vector<long long> next;\n next.reserve(dp.size() + op.size() + 5);\n for (int i = 0 ; i <= cnt ; i++) op[i + 1] = binom(cnt, i);\n for (int i = 0 ; i < std::ssize(dp) ; i++) {\n for (int j = 0 ; j < std::ssize(op) ; j++) {\n while (i + j >= std::ssize(next)) next.push_back(0LL);\n next[i + j] += (dp[i] * op[j]) % MOD;\n next[i + j] %= MOD;\n }\n }\n dp = std::move(next);\n }\n long long ans = 0;\n for (int i = 1 ; i < std::min<int>(std::ssize(dp), K + 1) ; i++) {\n ans += dp[i];\n ans %= MOD;\n }\n return ans;\n}\nlong long naive() {\n std::vector<std::vector<int>> A(1);\n A[0] = {S[0]};\n long long ans = 0;\n auto check = [&]() -> void {\n if (std::ssize(A) > K) return;\n std::vector<int> ptr(A.size());\n for (int i = 0 ; i < N ; i++) {\n int mn = -1;\n for (int j = 0 ; j < std::ssize(A) ; j++) if (ptr[j] < std::ssize(A[j])) {\n if (mn == -1 or A[mn][ptr[mn]] > A[j][ptr[j]]) mn = j;\n }\n if (mn == -1 or A[mn][ptr[mn]] != T[i]) return;\n ptr[mn]++;\n }\n ans++;\n };\n auto dfs = [&](auto dfs, int i) -> void {\n if (i == N) check();\n else {\n A.back().push_back(S[i]);\n dfs(dfs, i + 1);\n A.back().pop_back();\n A.push_back({S[i]});\n dfs(dfs, i + 1);\n A.pop_back();\n }\n };\n dfs(dfs, 1);\n return ans;\n}\n#include <random>\nint main() {\n F[0] = 1;\n for (int i = 1 ; i <= MAXNK ; i++) F[i] = (F[i - 1] * i) % MOD;\n invF[MAXNK] = mod_inv(F[MAXNK]);\n for (int i = MAXNK ; i >= 1 ; i--) invF[i - 1] = (invF[i] * i) % MOD;\n#ifdef DEBUG\n std::mt19937 mt{std::random_device{}()};\n while (true) {\n N = mt() % 7 + 1;\n K = mt() % N + 1;\n std::iota(S, S + N, 0);\n std::iota(T, T + N, 0);\n std::shuffle(S, S + N, mt);\n std::shuffle(T, T + N, mt);\n std::cout << N << ' ' << K << std::endl;\n for (int i = 0 ; i < N ; i++) std::cout << S[i] + 1 << ' ';\n std::cout << std::endl;\n for (int i = 0 ; i < N ; i++) std::cout << T[i] + 1 << ' ';\n std::cout << std::endl;\n long long ans = naive(), my = solve();\n if (ans != my) {\n std::cout << ans << ' ' << my << std::endl;\n std::exit(0);\n }\n std::cout << \"-----------------------\" << std::endl;\n }\n#else\n while (true) {\n std::cin >> N >> K;\n if (N == 0 and K == 0) break;\n for (int i = 0 ; i < N ; i++) {\n std::cin >> S[i];\n S[i]--;\n }\n for (int i = 0 ; i < N ; i++) {\n std::cin >> T[i];\n T[i]--;\n }\n std::cout << solve() << '\\n';\n }\n#endif\n}", "accuracy": 1, "time_ms": 3460, "memory_kb": 4384, "score_of_the_acc": -1.0153, "final_rank": 20 }, { "submission_id": "aoj_1659_10649273", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/modint>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\nusing ll = long long;\n\n// 公式解説とはやっていることが少し違うので注意\n//\n// P[i]: Sにおけるiの場所\n// Q[i]: Tにおけるiの場所\n//\n// Tの前から構築していく\n//\n// dp[i][j] : T[0:i]まで構築可能な列ができており、\n// すでにj個の列を占有している場合の数\n//\n// 初期値\n// dp[i][j] = 0\n// dp[0][0] = 1\n//\n// 遷移\n// [1] 新しい列を作る遷移\n// T[0:i]のどの値よりもT[i]が大きければ可能\n// この条件が成り立たないと、T[0:i]が構築される途中でT[i]の値が先に出てきてしまう\n// dp[i + 1][j + 1] += dp[i][j]\n//\n// [2] 既存の列に後ろにつける遷移\n// 以下の二つが成り立つならOK\n// 1. U = S[P[T[i]] - 1]がT[0:i]に含まれている\n// Sにおいて、T[i]の前の値の列の後ろにつける必要がある\n// そのような値UがT[0:i]に含まれている必要がある\n// 2. T[Q[U] + 1:i]のどの値よりもT[i]が大きい\n// [1]と同様.\n// Uが列に追加されたあと、T[i]が列の先頭になる\n// 2. の条件を満たさないと、T[Q[U] + 1:i]を構築される途中でT[i]の値が先に出てきてしまう\n// dp[i + 1][j] += dp[i][j]\n//\n// 解\n// sum_i dp[n][i]\n\nint solve() {\n using mint = atcoder::modint998244353;\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) return 1;\n vector<int> S(n), T(n);\n vector<int> P(n + 1), Q(n + 1);\n rep(i, 0, n) cin >> S[i];\n rep(i, 0, n) cin >> T[i];\n rep(i, 0, n) P[S[i]] = i;\n rep(i, 0, n) Q[T[i]] = i;\n vector<mint> dp(k + 1);\n dp[0] = 1;\n int prefix_max = 0;\n rep(i, 0, n) {\n vector<mint> ndp(k + 1);\n bool enable_transition_1 = prefix_max < T[i];\n prefix_max = max(prefix_max, T[i]);\n bool enable_transition_2 = false;\n if (P[T[i]] - 1 >= 0) {\n int U = S[P[T[i]] - 1];\n enable_transition_2 |= Q[U] < i;\n }\n rep(j, 0, k + 1) {\n if (enable_transition_1 && j + 1 <= k) {\n ndp[j + 1] += dp[j];\n }\n if (enable_transition_2) {\n ndp[j] += dp[j];\n }\n }\n dp = std::move(ndp);\n }\n auto ans = reduce(begin(dp), end(dp));\n cout << ans.val() << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 3840, "score_of_the_acc": -0.2729, "final_rank": 12 }, { "submission_id": "aoj_1659_10648992", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/modint>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\nusing ll = long long;\n\nint solve() {\n using mint = atcoder::modint998244353;\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) return 1;\n vector<int> S(n), T(n);\n vector<int> P(n + 1), Q(n + 1);\n rep(i, 0, n) cin >> S[i];\n rep(i, 0, n) cin >> T[i];\n rep(i, 0, n) P[S[i]] = i;\n rep(i, 0, n) Q[T[i]] = i;\n vector<mint> dp(k + 1);\n dp[0] = 1;\n int prefix_max = 0;\n rep(i, 0, n) {\n vector<mint> ndp(k + 1);\n bool enable_transition_1 = prefix_max < T[i];\n prefix_max = max(prefix_max, T[i]);\n bool enable_transition_2 = false;\n if (P[T[i]] - 1 >= 0) {\n int U = S[P[T[i]] - 1];\n enable_transition_2 |= Q[U] < i;\n }\n rep(j, 0, k + 1) {\n if (enable_transition_1 && j + 1 <= k) {\n ndp[j + 1] += dp[j];\n }\n if (enable_transition_2) {\n ndp[j] += dp[j];\n }\n }\n dp = std::move(ndp);\n }\n auto ans = reduce(begin(dp), end(dp));\n cout << ans.val() << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 3840, "score_of_the_acc": -0.2729, "final_rank": 12 }, { "submission_id": "aoj_1659_10648949", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/convolution>\n#include <atcoder/modint>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\nusing ll = long long;\n\nint solve() {\n using mint = atcoder::modint998244353;\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) return 1;\n vector<int> S(n), T(n);\n vector<int> P(n + 1), Q(n + 1);\n rep(i, 0, n) cin >> S[i];\n rep(i, 0, n) cin >> T[i];\n rep(i, 0, n) P[S[i]] = i;\n rep(i, 0, n) Q[T[i]] = i;\n queue<vector<mint>> que;\n int prefix_max = 0;\n rep(i, 0, n) {\n bool enable_transition_1 = prefix_max < T[i];\n prefix_max = max(prefix_max, T[i]);\n bool enable_transition_2 = false;\n if (P[T[i]] - 1 >= 0) {\n int U = S[P[T[i]] - 1];\n enable_transition_2 |= Q[U] < i;\n }\n vector<mint> cur(2);\n if (enable_transition_1) {\n cur[1] = 1;\n }\n if (enable_transition_2) {\n cur[0] = 1;\n }\n que.push(cur);\n }\n while (que.size() >= 2) {\n auto lhs = que.front();\n que.pop();\n auto rhs = que.front();\n que.pop();\n auto res = atcoder::convolution(lhs, rhs);\n if (ssize(res) > k + 1) res.resize(k + 1);\n que.push(res);\n }\n auto dp = que.front();\n que.pop();\n auto ans = reduce(begin(dp), end(dp));\n cout << ans.val() << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 4608, "score_of_the_acc": -0.0596, "final_rank": 10 }, { "submission_id": "aoj_1659_10648234", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/modint>\nusing namespace std;\n#define rep(i, a, b) for (int i = (a); i < (b); i++)\nusing ll = long long;\n\nint solve() {\n using mint = atcoder::modint998244353;\n int n, k;\n cin >> n >> k;\n if (n == 0 && k == 0) return 1;\n vector<int> S(n), T(n);\n vector<int> P(n + 1), Q(n + 1);\n rep(i, 0, n) cin >> S[i];\n rep(i, 0, n) cin >> T[i];\n rep(i, 0, n) P[S[i]] = i;\n rep(i, 0, n) Q[T[i]] = i;\n vector<mint> dp(k + 1);\n dp[0] = 1;\n rep(i, 0, n) {\n vector<mint> ndp(k + 1);\n bool enable_transition_1 = true;\n rep(j, 0, i) enable_transition_1 &= T[i] > T[j];\n bool enable_transition_2 = false;\n if (P[T[i]] - 1 >= 0) {\n int U = S[P[T[i]] - 1];\n enable_transition_2 |= Q[U] < i;\n rep(j, Q[U] + 1, i) enable_transition_2 &= T[i] > T[j];\n }\n rep(j, 0, k + 1) {\n if (enable_transition_1 && j + 1 <= k) {\n ndp[j + 1] += dp[j];\n }\n if (enable_transition_2) {\n ndp[j] += dp[j];\n }\n }\n dp = std::move(ndp);\n }\n mint ans = 0;\n rep(i, 0, k + 1) ans += dp[i];\n cout << ans.val() << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 1680, "memory_kb": 3840, "score_of_the_acc": -0.4903, "final_rank": 16 }, { "submission_id": "aoj_1659_10625389", "code_snippet": "#line 1 \"2022D.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2022D.cpp\"\n\n#line 1 \"/home/ardririy/repos/cp-libs/util/include/atcoder/modint.hpp\"\n\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/util/include/atcoder/modint.hpp\"\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\n#line 1 \"/home/ardririy/repos/cp-libs/util/include/atcoder/internal_math.hpp\"\n\n\n\n#line 5 \"/home/ardririy/repos/cp-libs/util/include/atcoder/internal_math.hpp\"\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast modular multiplication by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m`\n explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned long long y = x * _m;\n return (unsigned int)(z - y + (z < y ? _m : 0));\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n// @param n `n < 2^32`\n// @param m `1 <= m < 2^32`\n// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)\nunsigned long long floor_sum_unsigned(unsigned long long n,\n unsigned long long m,\n unsigned long long a,\n unsigned long long b) {\n unsigned long long ans = 0;\n while (true) {\n if (a >= m) {\n ans += n * (n - 1) / 2 * (a / m);\n a %= m;\n }\n if (b >= m) {\n ans += n * (b / m);\n b %= m;\n }\n\n unsigned long long y_max = a * n + b;\n if (y_max < m) break;\n // y_max < m * (n + 1)\n // floor(y_max / m) <= n\n n = (unsigned long long)(y_max / m);\n b = (unsigned long long)(y_max % m);\n std::swap(m, a);\n }\n return ans;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#line 1 \"/home/ardririy/repos/cp-libs/util/include/atcoder/internal_type_traits.hpp\"\n\n\n\n#line 7 \"/home/ardririy/repos/cp-libs/util/include/atcoder/internal_type_traits.hpp\"\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#line 14 \"/home/ardririy/repos/cp-libs/util/include/atcoder/modint.hpp\"\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#line 5 \"2022D.cpp\"\n\nusing namespace std;\n\nusing namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(...)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nusing mint = modint998244353;\n\nvector<mint> fact(20000, 1);\nmint nck(int n, int k) {\n if(n<k) return 0;\n return fact[n] / fact[n-k] / fact[k];\n}\n\n\nunordered_map<int, int> mp;\nvector<int> s, t, q;\nvoid solve(int n, int k) {\n dbg(\"===\");\n int a;\n \n mp.clear();\n s.clear();\n rep(i,n) {\n cin >> a;\n s.emplace_back(a);\n }\n\n t.clear();\n rep(i,n) {\n cin >> a;\n t.emplace_back(a);\n }\n\n rep(i,n) {\n mp[t[i]] = i;\n }\n\n q.clear();\n rep(i,n){\n q.emplace_back(mp[s[i]]);\n }\n dbg(q);\n\n vll pos;\n rep(i,n-1) {\n if(q[i] > q[i+1]) {\n pos.emplace_back(i+1);\n }\n }\n pos.emplace_back(n);\n\n {\n // check\n dbg(pos.size());\n vll indicates(pos.size(),0);\n rep(i,pos.size()-1){\n indicates[i+1] = pos[i];\n }\n \n djks pq;\n rep(i,indicates.size()) {\n pq.push({s[indicates[i]], i});\n }\n\n ll cur = 0;\n while(!pq.empty()) {\n dbg(pq);\n auto [val, idx] = pq.top();\n pq.pop();\n if(t[cur]==val) cur++;\n else break;\n\n if(indicates[idx]+1<pos[idx]) {\n indicates[idx]++;\n pq.push({s[indicates[idx]], idx});\n }\n }\n if(cur!=n) {\n cout << \"0\\n\";\n return;\n }\n }\n\n ll cnt = 0;\n\n \n if(pos.size()>k){\n cout << \"0\\n\";\n return;\n }\n k -= pos.size();\n\n ll l = 0;\n for(auto r: pos) {\n ll maximam = s[l];\n rep2(i,l+1,r) {\n if(maximam<s[i]) cnt++;\n chmax(maximam, ll(s[i]));\n }\n l = r;\n }\n \n mint ans = 0;\n rep(i, k+1) {\n ans += nck(cnt, i);\n }\n cout << ans.val() << '\\n';\n\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n rep(i,20000-1) {\n fact[i+1] = fact[i] * (i+1);\n }\n\n int n, k;\n cin >> n >> k;\n while(n) {\n solve(n, k);\n cin >> n >> k;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4376, "score_of_the_acc": -0.021, "final_rank": 5 }, { "submission_id": "aoj_1659_10517258", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n\t\nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\ntemplate<uint32_t m> class static_modint {\n\t\tusing mint = static_modint;\n\t\tuint32_t _v = 0;\n\t\tstatic constexpr bool prime = []() -> bool {\n\t\t\t\tif (m == 1) return 0;\n\t\t\t\tif (m == 2 || m == 7 || m == 61) return 1;\n\t\t\t\tif (m % 2 == 0) return 0;\n\t\t\t\tuint32_t d = m - 1;\n\t\t\t\twhile (d % 2 == 0) d /= 2;\n\t\t\t\tfor (uint32_t a : {2, 7, 61}) {\n\t\t\t\t\t\tuint32_t t = d;\n\t\t\t\t\t\tmint y = mint(a).pow(t);\n\t\t\t\t\t\twhile (t != m - 1 && y != 1 && y != m - 1) {\n\t\t\t\t\t\t\t\ty *= y; t <<= 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (y != m - 1 && t % 2 == 0) return 0;\n\t\t\t\t}\n\t\t\t\treturn 1;\n\t\t}();\n\t\tstatic constexpr pair<int32_t, int32_t> inv_gcd(int32_t a, int32_t b) {\n\t\t\t\tif (a == 0) return {b, 0};\n\t\t\t\tint32_t s = b, t = a, m0 = 0, m1 = 1;\n\t\t\t\twhile (t) {\n\t\t\t\t\t\tconst int32_t u = s / t;\n\t\t\t\t\t\ts -= t * u; m0 -= m1 * u;\n\t\t\t\t\t\tswap(s, t); swap(m0, m1);\n\t\t\t\t}\n\t\t\t\tif (m0 < 0) m0 += b / s;\n\t\t\t\treturn {s, m0};\n\t\t}\npublic:\n\t\tstatic constexpr mint raw(uint32_t v) { mint a; a._v = v; return a; }\n\t\tconstexpr static_modint() {}\n\t\ttemplate <class T>\n\t\tconstexpr static_modint(T v) {\n\t\t\t\tstatic_assert(is_integral_v<T>, \"T is not integral type.\");\n\t\t\t\tif constexpr (is_signed_v<T>) {\n\t\t\t\t\t\tint64_t x = int64_t(v % int64_t(m));\n\t\t\t\t\t\tif (x < 0) x += m; _v = uint32_t(x);\n\t\t\t\t}\n\t\t\t\telse _v = uint32_t(v % m);\n\t\t}\n\t\tstatic constexpr uint32_t mod() { return m; }\n\t\tconstexpr uint32_t val() const { return _v; }\n\t\tconstexpr mint& operator++() { return *this += 1; }\n\t\tconstexpr mint& operator--() { return *this -= 1; }\n\t\tconstexpr mint operator++(int) { mint res = *this; ++*this; return res; }\n\t\tconstexpr mint operator--(int) { mint res = *this; --*this; return res; }\n\t\tconstexpr mint& operator+=(mint rhs) {\n\t\t\t\tif (_v >= m - rhs._v) _v -= m;\n\t\t\t\t_v += rhs._v; return *this;\n\t\t}\n\t\tconstexpr mint& operator-=(mint rhs) {\n\t\t\t\tif (_v < rhs._v) _v += m;\n\t\t\t\t_v -= rhs._v; return *this;\n\t\t}\n\t\tconstexpr mint& operator*=(mint rhs) { return *this = *this * rhs; }\n\t\tconstexpr mint& operator/=(mint rhs) { return *this *= rhs.inv(); }\n\t\tconstexpr mint operator+() const { return *this; }\n\t\tconstexpr mint operator-() const { return mint{} - *this; }\n\t\tconstexpr mint pow(long long n) const {\n\t\t\t\tassert(0 <= n);\n\t\t\t\tif(n == 0) return 1;\n\t\t\t\tmint x = *this, r = 1;\n\t\t\t\twhile (1) {\n\t\t\t\t\t\tif (n & 1) r *= x; n >>= 1;\n\t\t\t\t\t\tif (n == 0) return r;\n\t\t\t\t\t\tx *= x;\n\t\t\t\t}\n\t\t}\n\t\tconstexpr mint inv() const {\n\t\t\t\tif constexpr (prime) {\n\t\t\t\t\t\tassert(_v);\n\t\t\t\t\t\treturn pow(m - 2);\n\t\t\t\t} else {\n\t\t\t\t\t\tauto eg = inv_gcd(_v, m);\n\t\t\t\t\t\tassert(eg.first == 1);\n\t\t\t\t\t\treturn eg.second;\n\t\t\t\t}\n\t\t}\n\t\tfriend constexpr mint operator+(mint lhs, mint rhs) { return lhs += rhs; }\n\t\tfriend constexpr mint operator-(mint lhs, mint rhs) { return lhs -= rhs; }\n\t\tfriend constexpr mint operator*(mint lhs, mint rhs) { return uint64_t(lhs._v) * rhs._v; }\n\t\tfriend constexpr mint operator/(mint lhs, mint rhs) { return lhs /= rhs; }\n\t\tfriend constexpr bool operator==(mint lhs, mint rhs) { return lhs._v == rhs._v; }\n\t\tfriend constexpr bool operator!=(mint lhs, mint rhs) { return lhs._v != rhs._v; }\n};\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\n\nusing mint = modint998244353;\n\n\n//最強,前処理(n), 計算(1)\n//n,k <= 10^7の時\nconst ll MOD = 998244353;\nvector<long long> fact, fact_inv, inv;\n/* init_nCk :二項係数のための前処理\n\t\t計算量:O(n)\n*/\nvoid init_nCk(ll SIZE) {\n\t\tfact.resize(SIZE + 5);\n\t\tfact_inv.resize(SIZE + 5);\n\t\tinv.resize(SIZE + 5);\n\t\tfact[0] = fact[1] = 1;\n\t\tfact_inv[0] = fact_inv[1] = 1;\n\t\tinv[1] = 1;\n\t\tfor (int i = 2; i < SIZE + 5; i++) {\n\t\t\t\tfact[i] = fact[i - 1] * i % MOD;\n\t\t\t\tinv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;//iの逆元\n\t\t\t\tfact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;//i!の逆元\n\t\t}\n}\n/* nCk :MODでの二項係数を求める(前処理 int_nCk が必要)\n\t\t計算量:O(1)\n*/\nlong long nCk(ll n, ll k) {\n\t\tassert(!(n < k));\n\t\tassert(!(n < 0 || k < 0));\n\t\treturn fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;\n}\n\n//参照渡しで値を作成して渡す\nvoid Maketest(ll &n,ll &k,vll &s,vll &t){\n//配列を渡すときは最初に.clear()をしておく\n\trandom_device seed_gen;\n\tmt19937_64 rnd(seed_gen());\n\t\n\tuniform_int_distribution<ll> da(1, 10);\n\tuniform_int_distribution<ll> db(1, n);\n\tn = da(rnd);\n\tk = db(rnd);\n\n\ts.resize(n);\n\tt.resize(n);\n\tiota(all(s),1);\n\tiota(all(t),1);\n\tll kaimax = 1;\n\trepsn(i,1,n){\n\t\tkaimax *= i;\n\t}\n\n\tuniform_int_distribution<ll> dc(0, kaimax);\n\n\tll dx = dc(rnd);\n\trep(i,dx){\n\t\tnext_permutation(all(s));\n\t}\n\tll dy = dc(rnd);\n\trep(i,dy)next_permutation(all(t));\n\n\tuniform_int_distribution<ll> dd(0, 1);\n}\n\n\n\nmint correct(ll n,ll k,vll s,vll t){\n\t//答えの型注意\n\tll ans = 0;\n\trep(i,1 << (n-1))if(popcnt(i)<= k-1){\n\t\tvector<queue<ll>> veq;\n\t\tqueue<ll> que;\n\t\tque.push(s[0]);\n\t\trep(j,n-1){\n\t\t\tif(i >> j & 1){\n\t\t\t\tveq.push_back(que);\n\t\t\t\twhile(!que.empty())que.pop();\n\t\t\t\tque.push(s[j+1]);\n\t\t\t}else{\n\t\t\t\tque.push(s[j+1]);\n\t\t\t}\n\t\t}\n\t\tveq.push_back(que);\n\t\tpriority_queue<pll,vector<pll>,greater<pll>> pq;\n\t\trep(i,sz(veq)){\n\t\t\tpq.emplace(veq[i].front(),i);\n\t\t\tveq[i].pop();\n\t\t}\n\n\t\tvll sample;\n\t\twhile(!pq.empty()){\n\t\t\tauto [val,id] = pq.top();\n\t\t\tpq.pop();\n\t\t\tsample.push_back(val);\n\t\t\tif(!veq[id].empty()){\n\t\t\t\tpq.emplace(veq[id].front(),id);\n\t\t\t\tveq[id].pop();\n\t\t\t}\n\t\t}\n\t\tif(sample == t)ans++;\n\n\t}\n\treturn ans;\n}\nmint solve(ll n,ll k,vll s,vll t){\n\tk--;\n\n\ts--;t--;\n\tvll tp(n);\n\trep(i,n){\n\t\ttp[t[i]] = i;\n\t}\n\tll badcnt = 0;\n\tll mustcnt = 0;\n\n\tvector<queue<ll>> veq;\n\tqueue<ll> que;\n\tque.push(s[0]);\n\tll ma = s[0];\n\tll able = 0;\n\trep(i,n-1){\n\t\tif(s[i] > s[i+1]){\n\t\t\tif(tp[s[i]] < tp[s[i+1]]){\n\t\t\t\tbadcnt++;\n\t\t\t\tque.push(s[i+1]);\n\t\t\t}else {\n\t\t\t\tmustcnt++;\n\t\t\t\tveq.push_back(que);\n\t\t\t\twhile(!que.empty())que.pop();\n\t\t\t\tque.push(s[i+1]);\n\t\t\t}\n\t\t}else {\n\t\t\tif(tp[s[i+1]] < tp[s[i]]){\n\t\t\t\tmustcnt++;\n\t\t\t\tveq.push_back(que);\n\t\t\t\twhile(!que.empty())que.pop();\n\t\t\t\tque.push(s[i+1]);\n\t\t\t}else{\n\t\t\t\tbool ok = true;\n\t\t\t\trepn(j,i){\n\t\t\t\t\tif(s[j] > s[i+1] && tp[s[j]] < tp[s[i+1]]){\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!ok)badcnt++;\n\t\t\t\tque.push(s[i+1]);\n\t\t\t}\n\t\t}\n\t\tchmax(ma,s[i+1]);\n\t}\n\tveq.push_back(que);\n\tpriority_queue<pll,vector<pll>,greater<pll>> pq;\n\trep(i,sz(veq)){\n\t\tpq.emplace(veq[i].front(),i);\n\t\tveq[i].pop();\n\t}\n\n\tvll sample;\n\twhile(!pq.empty()){\n\t\tauto [val,id] = pq.top();\n\t\tpq.pop();\n\t\tsample.push_back(val);\n\t\tif(!veq[id].empty()){\n\t\t\tpq.emplace(veq[id].front(),id);\n\t\t\tveq[id].pop();\n\t\t}\n\t\t\n\t}\n\tif(sample != t or mustcnt > k){\n\t\treturn 0;\n\t}else{\n\t\tll rcut = k - mustcnt;\n\t\tll free = n-1-mustcnt-badcnt;\n\t\tmint ans = 1;//mustcutだけ\n\t\tfor(ll i = 1;i <= min(free,rcut);i++){\n\t\t\tans += nCk(free,i);\n\t\t}\n\t\treturn ans;\n\t}\n}\n \n\nvoid test(){\n\trep(z,1000){\n\t\tif(z % 10 == 0)cerr << z << endl;\n\t\tll n,k;\n\t\tvll s,t;\n\t\tMaketest(n,k,s,t);\n\t\tmint a1 = solve(n,k,s,t);\n\t\tmint a2 = correct(n,k,s,t);\n\t\tif(a1 != a2){\n\t\t\tprint(n,k);\n\t\t\tprint(s);\n\t\t\tprint(t);\n\t\t\tprint(a1.val(),a2.val());\n\t\t\tprint();\n\t\t}\n\t}\n}\n\nvoid nomal(){\n\n}\n\n\n\n\nint main(){\n\tios::sync_with_stdio(false);cin.tie(nullptr);\n\tinit_nCk(lpow(10,5));\n\twhile(true){\n\t\tLL(n,k);\n\t\tif(n== 0 && k == 0)break;\n\t\t\tvll s(n);cin>> s;\n\tvll t(n);cin >>t;\n\t\tcout << solve(n,k,s,t).val() << endl;\n\t}\n\t// test();\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 11712, "score_of_the_acc": -0.4127, "final_rank": 15 }, { "submission_id": "aoj_1659_10464946", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nll modpow(ll x, ll n, ll m) {\n ll res = 1;\n while (n > 0) {\n if (n & 1) res = res * x % m;\n x = x * x % m;\n n >>= 1LL;\n }\n return res;\n}\nbool solve() {\n int N, K;\n cin >> N >> K;\n if (N == 0) return 0;\n vi S(N), T(N);\n vector<int> sidx(N);\n for (int i = 0; i < N; i++) {\n cin >> S[i];\n S[i]--;\n sidx[S[i]] = i;\n }\n for (int i = 0; i < N; i++) {\n cin >> T[i];\n T[i]--;\n }\n vector<bool> used(N);\n ll free_wall = 0; // 自由な仕切り\n ll wall = 0; // 必ず必要な仕切り\n\n int max_elem = -1;\n for (int i = 0; i < N; i++) {\n int j = sidx[T[i]];\n if (j > 0 and used[j - 1]) {\n if (max_elem < S[j]) {\n free_wall++;\n }\n } else {\n if (max_elem > S[j]) {\n // 詰み どうやっても先にS[j]を使用してしまう\n cout << 0 << endl;\n return 1;\n }\n wall++;\n }\n max_elem = max(max_elem, S[j]);\n used[j] = true;\n }\n const ll MOD = 998244353;\n ll ans = 0;\n ll ans1 = 1;\n vector<ll> fac(N + 1, 1);\n vector<ll> rfac(N + 1, 1);\n for (ll i = 1; i <= free_wall; i++) {\n fac[i] = fac[i - 1] * i % MOD;\n rfac[i] = rfac[i - 1] * modpow(i, MOD - 2, MOD) % MOD;\n }\n ll n = free_wall;\n for (ll k = 0; k <= min(n, K - wall); k++) { // k:使用する壁の数\n // cerr << ans1 << endl;\n ans1 = fac[n] * rfac[k] % MOD * rfac[n - k] % MOD;\n ans = (ans + ans1) % MOD;\n }\n cout << ans << endl;\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3840, "score_of_the_acc": -0.0208, "final_rank": 4 }, { "submission_id": "aoj_1659_10445019", "code_snippet": "// AOJ 1659 Audience Queue\n// 2025.5.3\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 998244353;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) {\n\tint i; char b[30];\n\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\ti = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nint modpow(int a, int e = MOD-2) {\n ll r = 1, x = a;\n while (e) {\n if (e & 1) r = r * x % MOD;\n x = x * x % MOD;\n e >>= 1;\n }\n return (int)r;\n}\n\nconst int NMAX = 10000;\nvector<ll> fact(NMAX+1,1), ifact(NMAX+1,1);\n\nint main(){\n for(int i = 1; i <= NMAX; i++) fact[i] = fact[i-1] * i % MOD;\n ifact[NMAX] = modpow((int)fact[NMAX]);\n for(int i = NMAX; i > 0; i--) ifact[i-1] = ifact[i] * i % MOD;\n\n auto C = [&](int n, int k)->ll {\n if (k < 0 || k > n) return 0;\n return fact[n] * ifact[k] % MOD * ifact[n-k] % MOD;\n };\n\n int n, k;\n while (true) {\n int n = Cin(), k = Cin();\n if (n == 0) break;\n vector<int> a(n), b(n), pos(n+1), c(n);\n for (int i = 0; i < n; i++) a[i] = Cin();\n for (int i = 0; i < n; i++) {\n b[i] = Cin();\n pos[b[i]] = i;\n }\n for (int i = 0; i < n; i++) c[i] = pos[a[i]];\n\n vector<bool> fr(n-1,false);\n int m = 0;\n for (int i = 0; i < n-1; i++) {\n if (c[i] > c[i+1]) {\n fr[i] = true;\n m++;\n }\n }\n if (m+1 > k) { Cout(0); continue; }\n\n vector<int> st;\n st.reserve(m+1);\n st.push_back(0);\n for (int i = 0; i < n-1; i++) if (fr[i]) st.push_back(i+1);\n int S = (int)st.size();\n\n struct Node { int v, s, i; };\n struct Cmp { bool operator()(Node const &x, Node const &y) const {\n return x.v > y.v;\n } };\n priority_queue<Node, vector<Node>, Cmp> pq;\n for (int j = 0; j < S; j++) pq.push({ a[st[j]], j, 0 });\n\n vector<int> out;\n out.reserve(n);\n while (!pq.empty()) {\n auto t = pq.top(); pq.pop();\n out.push_back(t.v);\n int L = st[t.s], R = (t.s+1 < S ? st[t.s+1] : n);\n if (L + t.i + 1 < R) pq.push({ a[L + t.i + 1], t.s, t.i + 1 });\n }\n if (out != b) { Cout(0); continue; }\n\n int O = 0;\n for (int j = 0; j < S; j++) {\n int L = st[j], R = (j+1 < S ? st[j+1] : n);\n if (R - L <= 1) continue;\n int mx = a[L];\n for (int i = L; i <= R-2; i++) {\n mx = max(mx, a[i]);\n if (!fr[i] && a[i+1] >= mx) O++;\n }\n }\n\n int rem = k - 1 - m;\n ll ans = 0;\n for (int j = 0; j <= rem && j <= O; j++) ans = (ans + C(O, j)) % MOD;\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3808, "score_of_the_acc": -0.0057, "final_rank": 2 }, { "submission_id": "aoj_1659_10091705", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\n\nstruct modint {\n int Mod = mod;\n int val;\n using mint = modint;\n modint() {};\n modint(ll v) {\n if (0 <= v && v < Mod) val = v;\n else{\n v %= Mod;\n if (v < 0) v += Mod;\n val = v;\n }\n }\n mint operator++(int){\n val++;\n if (val == Mod) val = 0;\n return *this;\n }\n mint operator--(int){\n if (val == 0) val = Mod;\n val--;\n return *this;\n }\n mint& operator+=(const mint& v){\n val += v.val;\n if (val >= Mod) val -= Mod;\n return *this;\n }\n mint& operator-=(const mint& v){\n val -= v.val;\n if (val < 0) val += Mod;\n return *this;\n }\n mint& operator*=(const mint& v){\n val = (int)(1ll * val * v.val % Mod);\n return *this;\n }\n modint inverse() const {\n int a = val, b = mod, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b), swap(u -= t * v, v);\n }\n return modint(u);\n }\n mint pow(ll n) const {\n mint a = *this, ret = 1;\n while (n > 0){\n if (n & 1) ret *= a;\n a *= a;\n n >>= 1;\n }\n return ret;\n }\n mint& operator>>=(ll k){\n *this *= mint(2).pow(k).pow(Mod - 2);\n return *this;\n }\n mint& operator<<=(ll k){\n *this *= mint(2).pow(k);\n return *this;\n}\n mint operator/=(const mint& v){\n *this *= v.inverse();\n return *this;\n }\n mint operator-() const { return mint(-val);}\n friend mint operator+(const mint& u, const mint& v){return mint(u) += v;}\n friend mint operator-(const mint& u, const mint& v){return mint(u) -= v;}\n friend mint operator*(const mint& u, const mint& v){return mint(u) *= v;}\n friend mint operator/(const mint& u, const mint& v){return mint(u) /= v;}\n friend bool operator==(const mint& u, const mint& v){return u.val == v.val;}\n friend bool operator!=(const mint& u, const mint& v){return u.val != v.val;}\n friend bool operator>(const mint& u, const mint& v){return u.val > v.val;}\n friend bool operator<(const mint& u, const mint& v){return u.val < v.val;}\n friend mint operator>>(mint& u, const ll k){return u >>= k;}\n friend mint operator<<(mint& u, const ll k){return u <<= k;}\n friend ostream& operator<<(ostream& stream, mint& v){\n stream << v.val;\n return stream;\n }\n};\n\nusing mint = modint;\n\nstruct Binoms {\n public : \n Binoms(int n){\n fact = vc<mint>(1, 1);\n inv_fact = vc<mint>(1, 1);\n update(n);\n };\n\n void update(int n){\n if (sz >= n) return;\n fact.resize(n + 1, 1);\n inv_fact.resize(n + 1, 1);\n for (int i = sz; i < n; i++) fact[i + 1] = fact[i] * (i + 1);\n inv_fact[n] = 1 / fact[n];\n for (int i = n - 1; i >= sz + 1; i--) inv_fact[i] = inv_fact[i + 1] * (i + 1);\n sz = n;\n return;\n }\n\n mint C(int n, int k){\n if (n < k || k < 0) return 0;\n if (sz < n) update(n);\n return fact[n] * inv_fact[n - k] * inv_fact[k];\n }\n\n mint fac(int n){\n if (sz < n) update(n);\n return fact[n];\n }\n\n mint invfac(int n){\n if (sz < n) update(n);\n return inv_fact[n];\n }\n\n private :\n int sz = 0;\n vc<mint> fact, inv_fact;\n};\n\nvoid solve(int N, int K){\n int must = 0, ng = 0, ok = 0;\n vi S(N); rep(i, N) cin >> S[i];\n vi T(N); rep(i, N) cin >> T[i];\n vi rT(N + 1);\n rep(i, N) rT[T[i]] = i;\n int mx = S[0];\n priority_queue<pair<int, int>, vc<pair<int, int>>, greater<pair<int, int>>> pq;\n vi seen(N, 0);\n seen[0] = 1;\n pq.push({S[0], 0});\n srep(i, 1, N){\n if (rT[S[i - 1]] > rT[S[i]]){\n must++;\n mx = S[i];\n pq.push({S[i], i});\n seen[i] = 1;\n }else if (mx < S[i]) ok++;\n mx = max(mx, S[i]);\n }\n vi nt;\n while (!pq.empty()){\n auto [d, id] = pq.top(); pq.pop();\n nt.push_back(d);\n if (id < N - 1 && !seen[id + 1]){\n pq.push({S[id + 1], id + 1});\n seen[id + 1] = 1;\n }\n }\n if (nt != T){\n cout << 0 << endl;\n return;\n }\n Binoms B(1);\n mint ans = 0;\n rep(i, K - must) ans += B.C(ok, i);\n cout << ans << endl;\n}\nint main(){\n while (true){\n int N, K; cin >> N >> K;\n if (N == 0) break;\n solve(N, K);\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3564, "score_of_the_acc": -0.0249, "final_rank": 7 }, { "submission_id": "aoj_1659_10087242", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\n\n#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP\n#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP\n\n#ifndef ATCODER_INTERNAL_MATH_HPP\n#define ATCODER_INTERNAL_MATH_HPP 1\n\n#include <utility>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast modular multiplication by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m`\n explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned long long y = x * _m;\n return (unsigned int)(z - y + (z < y ? _m : 0));\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n// @param n `n < 2^32`\n// @param m `1 <= m < 2^32`\n// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)\nunsigned long long floor_sum_unsigned(unsigned long long n,\n unsigned long long m,\n unsigned long long a,\n unsigned long long b) {\n unsigned long long ans = 0;\n while (true) {\n if (a >= m) {\n ans += n * (n - 1) / 2 * (a / m);\n a %= m;\n }\n if (b >= m) {\n ans += n * (b / m);\n b %= m;\n }\n\n unsigned long long y_max = a * n + b;\n if (y_max < m) break;\n // y_max < m * (n + 1)\n // floor(y_max / m) <= n\n n = (unsigned long long)(y_max / m);\n b = (unsigned long long)(y_max % m);\n std::swap(m, a);\n }\n return ans;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_MATH_HPP\n\n#ifndef ATCODER_MODINT_HPP\n#define ATCODER_MODINT_HPP 1\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_MODINT_HPP\n\n\nusing namespace atcoder;\nusing mint = modint998244353;\n\n\nll n;\nll k;\n\nvoid solve() {\n vector<int> s(n),t(n);\n vector<int> next(n,-1);\n rep(i,n) {\n cin >> s[i];\n s[i]--;\n }\n rep(i,n) {\n cin >> t[i];\n t[i]--;\n }\n\n vector<int> si(n),ti(n);\n rep(i,n) {\n si[s[i]] = i;\n ti[t[i]] = i;\n }\n\n rep(i,n-1) {\n if (t[i]>t[i+1]) {\n if (si[t[i]]+1 != si[t[i+1]]) {\n cout << 0 << endl;\n return;\n }\n }\n }\n\n\n ll must = 0;\n ll ok = 0;\n\n int mx = 0;\n\n rep(i,n-1) {\n mx = max(mx,s[i]);\n if (s[i] > s[i+1]) {\n if (ti[s[i]] > ti[s[i+1]]) {\n must++;\n mx = 0;\n }\n else {\n }\n }\n else {\n if (ti[s[i]] > ti[s[i+1]]) {\n must++;\n mx = 0;\n }\n else {\n if (mx <= s[i+1]) {\n ok++;\n }\n }\n }\n }\n\n k -= must+1;\n if (k < 0) {\n cout << 0 << endl;\n return;\n }\n\n mint ans = 1;\n mint comb = 1;\n\n rep(i,k) {\n if (ok < i) break;\n comb *= ok-i;\n comb /= i+1;\n\n ans += comb;\n }\n\n cout << ans.val() << endl;\n return;\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while (cin >> n >> k && n != 0) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3596, "score_of_the_acc": -0.0051, "final_rank": 1 }, { "submission_id": "aoj_1659_10087236", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\n\n#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP\n#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP\n\n#ifndef ATCODER_INTERNAL_MATH_HPP\n#define ATCODER_INTERNAL_MATH_HPP 1\n\n#include <utility>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast modular multiplication by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m`\n explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned long long y = x * _m;\n return (unsigned int)(z - y + (z < y ? _m : 0));\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n// @param n `n < 2^32`\n// @param m `1 <= m < 2^32`\n// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)\nunsigned long long floor_sum_unsigned(unsigned long long n,\n unsigned long long m,\n unsigned long long a,\n unsigned long long b) {\n unsigned long long ans = 0;\n while (true) {\n if (a >= m) {\n ans += n * (n - 1) / 2 * (a / m);\n a %= m;\n }\n if (b >= m) {\n ans += n * (b / m);\n b %= m;\n }\n\n unsigned long long y_max = a * n + b;\n if (y_max < m) break;\n // y_max < m * (n + 1)\n // floor(y_max / m) <= n\n n = (unsigned long long)(y_max / m);\n b = (unsigned long long)(y_max % m);\n std::swap(m, a);\n }\n return ans;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_MATH_HPP\n\n#ifndef ATCODER_MODINT_HPP\n#define ATCODER_MODINT_HPP 1\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_MODINT_HPP\n\n\nusing namespace atcoder;\nusing mint = modint998244353;\n\n\nll n;\nll k;\n\nvoid solve() {\n vector<int> s(n),t(n);\n vector<int> next(n,-1);\n rep(i,n) {\n cin >> s[i];\n s[i]--;\n }\n rep(i,n) {\n cin >> t[i];\n t[i]--;\n }\n\n vector<int> si(n),ti(n);\n rep(i,n) {\n si[s[i]] = i;\n ti[t[i]] = i;\n }\n\n priority_queue<int,vector<int>,greater<int>> pq;\n\n rep(i,n-1) {\n if (t[i]>t[i+1]) {\n if (si[t[i]]+1 != si[t[i+1]]) {\n cout << 0 << endl;\n return;\n }\n }\n }\n\n\n ll must = 0;\n ll ok = 0;\n\n int bef = 1;\n int mx = 0;\n\n rep(i,n-1) {\n mx = max(mx,s[i]);\n if (bef) pq.push(s[i]);\n if (s[i] > s[i+1]) {\n if (ti[s[i]] > ti[s[i+1]]) {\n must++;\n next[s[i]] = -1;\n mx = 0;\n bef = 1;\n }\n else {\n next[s[i]] = s[i+1];\n bef = 0;\n }\n }\n else {\n if (ti[s[i]] > ti[s[i+1]]) {\n must++;\n mx = 0;\n bef = 1;\n }\n else {\n if (mx <= s[i+1]) {\n ok++;\n }\n next[s[i]] = s[i+1];\n bef = 0;\n }\n }\n }\n if (bef) pq.push(s[n-1]);\n\n int now = 0;\n while (!pq.empty()) {\n auto p = pq.top();\n pq.pop();\n if (p == t[now]) {\n if (next[p]>=0) pq.push(next[p]);\n }\n else {\n cout << 0 << endl;\n return;\n }\n now++;\n }\n\n k -= must+1;\n if (k < 0) {\n cout << 0 << endl;\n return;\n }\n\n\n\n mint ans = 1;\n mint comb = 1;\n\n rep(i,k) {\n if (ok < i) break;\n comb *= ok-i;\n comb /= i+1;\n\n ans += comb;\n }\n\n cout << ans.val() << endl;\n return;\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while (cin >> n >> k && n != 0) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3464, "score_of_the_acc": -0.0058, "final_rank": 3 }, { "submission_id": "aoj_1659_9655587", "code_snippet": "#line 1 \"library/Template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n#line 2 \"library/Utility/fastio.hpp\"\n#include <unistd.h>\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n#line 3 \"sol.cpp\"\n\n#line 2 \"library/Math/modint.hpp\"\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n#line 5 \"sol.cpp\"\nusing Fp = fp<998244353>;\n\nint main() {\n for (;;) {\n int n, k;\n read(n, k);\n if (n == 0)\n break;\n vector<int> a(n), b(n);\n read(a, b);\n for (auto &x : a)\n x--;\n for (auto &x : b)\n x--;\n\n vector<int> inva(n);\n rep(i, 0, n) inva[a[i]] = i;\n vector<int> invb(n);\n rep(i, 0, n) invb[b[i]] = i;\n int mx = -1;\n vector<int> type(n - 1, 2);\n // 0:any,1:keep,2:don't keep\n int pos = 0;\n bool ch = 0;\n rep(i, 0, n) {\n if (chmax(mx, b[i])) {\n pos = inva[mx];\n } else {\n pos++;\n if (pos >= n or a[pos] != b[i]) {\n ch = 1;\n break;\n }\n type[pos - 1] = 1;\n }\n }\n if (ch) {\n print(0);\n continue;\n }\n rep(i, 0, n - 1) {\n if (type[i] != 1 and invb[a[i]] < invb[a[i + 1]])\n type[i] = 0;\n }\n show(type);\n vector<Fp> from(k + 1), to(k + 1);\n from[0] = 1;\n rep(i, 0, n - 1) {\n to.assign(k + 1, 0);\n rep(j, 0, k) {\n if (type[i] != 2) {\n to[j] += from[j];\n }\n if (type[i] != 1) {\n to[j + 1] += from[j];\n }\n }\n swap(from, to);\n }\n Fp ret;\n rep(j, 0, k) ret += from[j];\n print(ret);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 3928, "score_of_the_acc": -0.3063, "final_rank": 14 } ]
aoj_1673_cpp
Problem B Overtaking Two athletes run a long-distance race, but the race is different from usual athletics events. They run for a predetermined duration, and the one who runs longer will be the winner. At every minute after the start, the distances both runners ran in the previous one minute are recorded. You are expected to write a program that, given the record of a race, counts the number of overtakings by either runner during the race. You should assume that both runners keep constant paces for every one minute before their distances are recorded. Here, the term overtaking stands for the event where the runner previously behind takes the lead. Note that, before overtaking, the two runners may run side by side for a while, during which neither takes the lead. Figure B-1 shows the times and the positions of two runners for the third dataset of Sample Input. The number of overtakings in this case is two. Figure B-1: The third dataset of Sample Input Input The input consists of multiple datasets, each in the following format. n a 1 ⋯ a n b 1 ⋯ b n n is an integer between 2 and 1000, inclusive. It represents the duration of the race in minutes. Each of a k ( k = 1, , n ) is an integer between 1 and 1000, inclusive. It represents the distance the first runner ran between k − 1 minutes and k minutes after the start of the race, in meters. Similarly, b k ( k = 1, , n ) represent the distances the second runner ran. The end of the input is indicated by a line consisting of a zero. The number of datasets does not exceed 100. Output For each dataset, output the number of overtakings on a line. Sample Input 3 5 15 5 10 5 15 9 10 10 10 10 10 10 10 10 10 5 15 10 5 15 10 5 15 10 9 10 10 10 5 15 10 10 10 10 5 15 10 10 10 10 5 15 10 0 Output for the Sample Input 2 0 2
[ { "submission_id": "aoj_1673_9694077", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\nusing namespace std;\n#undef long\n#define long long long\n#define vec vector\nvoid solve()\n{\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n vec<int> a(n), b(n);\n rep(i, n) cin >> a[i];\n rep(i, n) cin >> b[i];\n rep(i, n - 1)\n {\n a[i + 1] += a[i];\n b[i + 1] += b[i];\n }\n int fu = 0;\n int ans = 0;\n rep(i, n)\n {\n if (a[i] < b[i])\n {\n ans += fu == -1;\n fu = 1;\n }\n if (a[i] > b[i])\n {\n ans += fu == 1;\n fu = -1;\n }\n }\n cout << ans << endl;\n}\nint main()\n{\n while (1)\n solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3120, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1673_9491722", "code_snippet": "#include <iostream>\n#include <vector>\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\nusing namespace std;\n\nint main() {\n while(true) {\n int n; \n cin >> n;\n\n if (n == 0) break;\n \n vector<int> a(n), b(n); \n\n rep(i,n) cin >> a[i];\n rep(i,n) cin >> b[i];\n\n int lead = 0, new_lead = 0, ans = 0, disA = 0, disB = 0; \n\n rep(i, n) {\n disA += a[i];\n disB += b[i];\n\n if (disA > disB) {\n new_lead = 1;\n } else if (disA < disB) {\n new_lead = -1;\n }\n if (lead != 0 && new_lead != 0 && lead != new_lead) {\n ans++;\n }\n lead = new_lead;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3076, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1673_9491547", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint main(void){\n bool is_a_lead = false;\n bool is_draw = true;\n queue<int> ans;\n while(1){\n int n;\n cin >> n;\n if(n == 0){\n break;\n }\n vector<int> a(n);\n for(int i=0;i<n;i++){\n cin >> a[i];\n }\n is_a_lead = false;\n is_draw = true;\n int sum1 = 0;\n int sum2 = 0;\n int count = 0;\n for(int i=0;i<n;i++){\n int v;\n cin >> v;\n sum1 += a[i];\n sum2 += v;\n if(sum1==sum2){\n continue;\n }\n if(sum1 > sum2){\n if(!is_draw && !is_a_lead){\n count++;\n }\n is_a_lead = true;\n is_draw =false;\n }\n else{\n if(!is_draw && is_a_lead){\n count++;\n }\n is_a_lead = false;\n is_draw =false;\n }\n }\n ans.push(count);\n }\n while(!ans.empty()){\n cout << ans.front() << endl;\n ans.pop();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3092, "score_of_the_acc": -0.3636, "final_rank": 2 } ]
aoj_1665_cpp
Problem B Amidakuji Amidakuji is a method of lottery popular in East Asia. It is often used when a number of gifts are distributed to people or when a number of tasks are assigned. Figure B-1: An example of amidakuji Figure B-1 shows an example of amidakuji, which corresponds to the first dataset of Sample Input. A number of vertical lines are drawn. The number of lines is the same as the number of participants. Some number of horizontal bars are drawn between some pairs of adjacent vertical lines. The number and positions of these bars are arbitrary, but no two bars should share their ends. Usually, many horizontal bars are drawn at random. To use an amidakuji, first, the names of an item, a gift or a task, are written down at the bottom end of vertical lines. Then, each participant chooses the top end of a vertical line, different from one another. Participants trace down the chosen line from the top. When reaching a T-junction with a horizontal bar, that bar is followed, switching to the adjacent vertical line it is connected, and the trace continues downward. When the bottom end of a vertical line is reached, the item written there is the awarded gift or the assigned task. Figure B-2 shows the case when the top end P is chosen. In this case, the item at R is obtained. Figure B-2: The trace from P Assume that the participant choosing P of Figure B-2 wants the item at Q, which is not fulfilled. But wait, a special rule just introduced may grant that wish. The new rule allows adding a single horizontal bar at an arbitrary position. Actually, adding a bar marked X in Figure B-3 will make the trace reach Q. Figure B-3: The new trace after adding a horizontal bar Your task in this problem is to write a program that finds the position of a horizontal bar to add to the given amidakuji for making the trace starting from the top end of the specified vertical line reach the bottom end of the specified vertical line. Input The input consists of multiple datasets, each in the following format. n m p q x 1 ⋯ x m All the input items in a dataset are positive integers. n is the number of vertical lines. m is the number of horizontal bars in the original amidakuji. The participant choosing the top end of the p -th line from the left wants the trace to reach the bottom end of the q -th line from the left. You can assume 2 ≤ n ≤ 50, m ≤ 500, p ≤ n , and q ≤ n hold. x 1 , ..., and x m give the positions of horizontal bars in the original amidakuji. x k is the position of the k -th bar from the top. Thus, vertical positions of all bars are different. x k ≤ n − 1 holds. The bar connects the x k -th line from the left and the ( x k + 1)-th line. The end of the input is indicated by a line consisting of four zeros. The number of datasets in the input does not exceed 300. Output For each dataset, output a single line, which is one of the following. If q can be reached from p without adding a bar, OK . If not, and adding a single bar cannot make q reachable from p , N ...(truncated)
[ { "submission_id": "aoj_1665_10845715", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int n, m, p, q;\n cin >> n >> m >> p >> q;\n if(!n) break;\n\n vector<int> amida(m);\n for(auto& i:amida) cin >> i;\n\n int save_p = p; //追加あり用にpを保存\n\n // 追加なしでの成立判定\n for(auto i:amida){ \n if(p == i) p = i+1;\n else if(p == i+1) p = i;\n }\n if(p == q){\n cout << \"OK\" << endl;\n continue;\n } \n\n // 追加ありでの成立判定\n p = save_p;\n int ans_x;\n bool end = false; // 終了判定\n bool pushed = false; // 挿入判定\n\n for(int i=0;i<=m;i++){\n for(int j=0;j<2;j++){ // 該当の縦線から左右に線を引くのをjで管理\n for(int k=0;k<amida.size();k++){\n int line;\n if(i==k && !pushed){ // 挿入処理\n line = p-1+j;\n ans_x = line;\n k--;\n pushed = true;\n }\n else if(i==m && k==m-1){ // 一番最後に横線を挿入\n line = amida[k];\n\n if(p == line) p = line+1;\n else if(p == line+1) p = line;\n \n line = p-1+j;\n ans_x = line;\n pushed = true;\n }\n else{\n line = amida[k];\n }\n if(p == line) p = line+1;\n else if(p == line+1) p = line;\n }\n if(p==q){\n cout << ans_x << ' ' << i << endl;\n end = true;\n }\n if(end) break;\n p = save_p; //値リセット\n pushed = false;\n }\n if(end) break;\n }\n if(end) continue;\n\n cout << \"NG\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3460, "score_of_the_acc": -0.6593, "final_rank": 8 }, { "submission_id": "aoj_1665_10845712", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int n, m, p, q;\n cin >> n >> m >> p >> q;\n if(!n) break;\n\n vector<int> amida(m);\n for(auto& i:amida) cin >> i;\n\n int save_p = p; //追加あり用にpを保存\n\n // 追加なしでの成立判定\n for(auto i:amida){ \n if(p == i) p = i+1;\n else if(p == i+1) p = i;\n }\n if(p == q){\n cout << \"OK\" << endl;\n continue;\n } \n\n // 追加ありでの成立判定\n p = save_p;\n int ans_x;\n bool end = false; // 終了判定\n bool pushed = false; // 挿入判定\n\n for(int i=0;i<=m;i++){\n for(int j=0;j<2;j++){ // 該当の縦線から左右に線を引くのをjで管理\n for(int k=0;k<amida.size();k++){\n int line;\n if(i==k && !pushed){ // 挿入処理\n line = p-1+j;\n ans_x = line;\n k--;\n pushed = true;\n }else if(i==m && k==m-1){\n line = amida[k];\n if(p == line) p = line+1;\n else if(p == line+1) p = line;\n line = p-1+j;\n ans_x = line;\n pushed = true;\n }else{\n line = amida[k];\n }\n if(p == line) p = line+1;\n else if(p == line+1) p = line;\n }\n if(p==q){\n cout << ans_x << ' ' << i << endl;\n end = true;\n }\n if(end) break;\n p = save_p; //値リセット\n pushed = false;\n }\n if(end) break;\n }\n if(end) continue;\n\n cout << \"NG\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.4066, "final_rank": 3 }, { "submission_id": "aoj_1665_10683972", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,n) for(ll i=0;i<n;i++)\n\nvoid solve(int n,int m,int p,int q){\n p--;\n q--;\n vector<int> a(m);\n rep(i,m) cin>>a[i];\n rep(i,m) a[i]--;\n\n int INF=1e9;\n\n int now=p;\n rep(j,m){\n if(a[j]==now) now++;\n else if(a[j]+1==now) now--;\n }\n\n if(now==q){\n cout << \"OK\" << endl;\n return;\n }\n\n for(int i=0;i<=m;i++){\n rep(k,2){\n int now=p,x=-1;\n rep(j,m){\n if(j==i){\n x=now;\n if(k==0) now--;\n else now++;\n if(now<0 && now>=n) now=-INF;\n }\n if(a[j]==now) now++;\n else if(a[j]+1==now) now--;\n }\n\n if(m==i){\n x=now;\n if(k==0) now--;\n else now++;\n if(now<0 && now>=n) now=-INF;\n }\n\n if(now==q){\n if(k==0) x--;\n cout << x+1 << \" \" << i << endl;\n return;\n }\n }\n }\n\n cout << \"NG\" << endl;\n return;\n}\n\nint main(){\n while(1){\n int n,m,p,q; cin>>n>>m>>p>>q;\n if(n==0) break;\n solve(n,m,p,q);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.6484, "final_rank": 7 }, { "submission_id": "aoj_1665_10669584", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while(1){\n int N,M,P,Q;\n cin>>N>>M>>P>>Q;\n if(N==0)return 0;\n P--;\n Q--;\n vector<int>X(M);\n rep(i,0,M){\n cin>>X[i];\n X[i]--;\n }\n {\n int now=P;\n rep(i,0,M){\n if(now==X[i])now++;\n else if(now==X[i]+1)now--;\n }\n if(now==Q){\n cout<<\"OK\"<<endl;\n continue;\n }\n }\n bool flag=false;\n for(int y=0;y<=M;y++){\n rep(x,0,N-1){\n int now=P;\n vector<int>Z;\n rep(i,0,M){\n if(y==i)Z.push_back(x);\n Z.push_back(X[i]);\n }\n if(y==M)Z.push_back(x);\n rep(i,0,M+1){\n if(now==Z[i])now++;\n else if(now==Z[i]+1)now--;\n }\n if(now==Q){\n cout<<x+1<<\" \"<<y<<endl;\n flag=true;\n break;\n }\n }\n if(flag)break;\n }\n if(!flag){\n cout<<\"NG\"<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3584, "score_of_the_acc": -1.5946, "final_rank": 19 }, { "submission_id": "aoj_1665_10667816", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n while (true) {\n int n, m, p, q;\n cin >> n >> m >> p >> q;\n if (n == 0 && m == 0 && p == 0 && q == 0) {\n break;\n }\n vector<pair<int, int>> xs;\n for (int i = 0; m > i; i++) {\n int x;\n cin >> x;\n xs.push_back(make_pair(x, x + 1));\n }\n xs.push_back(make_pair(0, 0));\n\n int cur = p;\n for (int i = 0; xs.size() > i; i++) {\n auto [a, b] = xs[i];\n if (cur == a) {\n cur = b;\n }\n else if (cur == b) {\n cur = a;\n }\n }\n // cout << cur << endl;\n if (cur == q) {\n cout << \"OK\" << endl;\n continue;\n }\n\n auto flag = false;\n for (auto r = 0; xs.size() > r; r++) {\n for (int d = -1; 1 >= d; d++) {\n int cur = p;\n int pos = -1;\n for (int i = 0; xs.size() > i; i++) {\n auto [a, b] = xs[i];\n if (i == r) {\n if (1 <= cur + d && cur + d <= n) {\n pos = min(cur + d, cur);\n cur += d;\n }\n }\n if (cur == a) {\n cur = b;\n }\n else if (cur == b) {\n cur = a;\n }\n }\n if (cur == q && pos != -1) {\n cout << pos << ' '\n << r << endl;\n flag = true;\n break;\n }\n }\n if (flag) {\n break;\n }\n }\n if (!flag) {\n cout << \"NG\" << endl;\n }\n }\n return EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -1, "final_rank": 11 }, { "submission_id": "aoj_1665_10650301", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <utility>\n#include <vector>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nint main()\n{\n while (true) \n {\n int n, m, p, q;\n cin >> n >> m >> p >> q;\n if (n + m + p + q == 0) break;\n vector<int> x(m);\n rep(i, m) cin >> x[i];\n\n int pos = p;\n\n rep(i, m)\n {\n if (x[i] == pos)\n {\n pos++;\n }\n else if (x[i] + 1 == pos)\n {\n pos--;\n }\n }\n\n if (pos == q)\n {\n cout << \"OK\" << endl;\n } \n else\n {\n pair<int, int> ans;\n ans.first = 1e9;\n ans.second = 1e9;\n\n for(int i = 0; i <= m; i++)\n {\n int pos = p;\n int X = 1e9, Y = 1e9;\n // i回目に横線を追加するとしてシミュレーション\n for (int j = 0; j < m; j++)\n {\n if (i == j)\n {\n X = pos;\n Y = j;\n pos++;\n }\n\n if (x[j] == pos)\n {\n pos++;\n }\n else if (x[j] + 1 == pos)\n {\n pos--;\n }\n\n if (j == m - 1 && i == m)\n {\n X = pos;\n Y = i;\n pos++;\n }\n\n }\n\n if (pos == q)\n {\n if (ans.second > Y)\n {\n ans = {X, Y};\n }\n }\n }\n\n for (int i = 0; i <= m; i++)\n {\n int X = 1e9, Y = 1e9;\n int pos = p;\n for (int j = 0; j < m; j++)\n {\n if (i == j)\n {\n X = pos;\n Y = i;\n pos--;\n }\n\n if (x[j] == pos)\n {\n pos++;\n }\n else if (x[j] + 1 == pos)\n {\n pos--;\n }\n\n if (i == m && j == m - 1)\n {\n X = pos;\n pos--;\n Y = i;\n }\n }\n\n if (pos == q)\n {\n if (ans.second > Y)\n {\n ans = {X - 1, Y};\n }\n }\n }\n\n if (ans.first != 1e9)\n {\n auto [i, j] = ans;\n cout << i << \" \" << j << endl;\n }\n else \n {\n cout << \"NG\" << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3220, "score_of_the_acc": -0.027, "final_rank": 1 }, { "submission_id": "aoj_1665_10649379", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#ifdef LOCAL\n #include \"settings/debug.cpp\"\n#else\n #define Debug(...) void(0)\n#endif\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nusing ull = unsigned long long;\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n while (true) {\n int w, h, p, q;\n cin >> w >> h >> p >> q;\n if (w + h + p + q == 0) break;\n vector<int> x(h);\n rep(i, h) cin >> x[i];\n auto simulate = [&]() {\n int now = p;\n rep(i, x.size()) {\n if (x[i] == now - 1)\n now -= 1;\n else if (x[i] == now)\n now += 1;\n }\n return now == q;\n };\n if (simulate()) {\n cout << \"OK\" << '\\n';\n continue;\n }\n pair<int, int> ans = { -1, -1 };\n rep(i, h + 1) {\n rep(j, w - 1) {\n x.insert(x.begin() + i, j + 1);\n if (simulate()) {\n ans = { i, j + 1 };\n break;\n }\n x.erase(x.begin() + i);\n }\n if (ans.first != -1) break;\n }\n if (ans.first == -1) {\n cout << \"NG\" << '\\n';\n }\n else {\n cout << ans.second << ' ' << ans.first << '\\n';\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3584, "score_of_the_acc": -1.2973, "final_rank": 17 }, { "submission_id": "aoj_1665_10649160", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(true) {\n ll n, m, p, q;\n cin >> n >> m >> p >> q;\n if(n == 0) break;\n p--;\n q--;\n\n vector<ll> x(m);\n rep(i, m) {\n cin >> x[i];\n x[i]--;\n }\n auto check = [&](const vector<ll>& xx) -> bool {\n ll now = p;\n rep(i, xx.size()) {\n if(now == xx[i]) now = xx[i] + 1;\n else if(now == xx[i] + 1) now = xx[i];\n }\n return now == q;\n };\n if(check(x)) {\n cout << \"OK\" << endl;\n continue;\n }\n\n ll ans_x = -1, ans_y = -1;\n rep(y, m + 1) {\n rep(nx, n - 1) {\n vector<ll> next_x;\n rep(i, m) {\n if(i == y) next_x.emplace_back(nx);\n next_x.emplace_back(x[i]);\n }\n if(y == m) next_x.emplace_back(nx);\n if(check(next_x)) {\n ans_x = nx + 1;\n ans_y = y;\n break;\n }\n }\n if(ans_x != -1) break;\n }\n if(ans_x == -1) cout << \"NG\" << endl;\n else cout << ans_x << \" \" << ans_y << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3492, "score_of_the_acc": -1.7473, "final_rank": 20 }, { "submission_id": "aoj_1665_10642753", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,l,r) for(int i=(l);(i)<(r);i++)\n#define rrep(i,l,r) for(int i=(r-1);(l)<=(i);i--)\n\nint main(){\n while(true){\n int N,M,p,q; cin>>N>>M>>p>>q; p--,q--;\n if(N==0)return 0;\n vector<int> X(M); rep(i,0,M)(cin>>X[i]),X[i]--;\n vector<int> P1(N); rep(i,0,N)P1[i]=i;\n vector<vector<int>> p1,p2;\n vector<int> P2(N); rep(i,0,N)P2[i]=i;\n p1.push_back(P1);\n rep(i,0,M){\n swap(P1[X[i]],P1[X[i]+1]);\n p1.push_back(P1);\n }\n p2.push_back(P2);\n rrep(i,0,M){\n swap(P2[X[i]],P2[X[i]+1]);\n p2.push_back(P2);\n }\n reverse(p2.begin(),p2.end());\n // rep(i,0,M+1){\n // rep(j,0,N)cout<<p1[i][j]<<' ';\n // cout<<endl;\n // }\n // cout<<endl<<endl;\n\n // rep(i,0,M+1){\n // rep(j,0,N)cout<<p2[i][j]<<' ';\n // cout<<endl;\n // }\n // cout<<endl;\n vector<pair<int,int>> ans;\n rep(j,0,N){\n if(p1[0][j]==p&&p2[0][j]==q){\n cout<<\"OK\"<<endl;\n goto g;\n }\n }\n rep(i,0,M+1){\n rep(j,0,N-1){\n // cout<<i<<' '<<j<<' '<<p1[i][j]<<' '<<p2[i][j+1]<<' ';\n // cout<<(p1[i][j]==p)<<' '<<(p2[i][j+1]==q)<<endl;\n if((p1[i][j]==p&&p2[i][j+1]==q)||(p1[i][j+1]==p&&p2[i][j]==q)){\n ans.push_back({i,j+1});\n }\n }\n }\n if(ans.size()==0)cout<<\"NG\"<<endl;\n else{\n sort(ans.begin(),ans.end());\n cout<<ans[0].second<<' '<<ans[0].first<<endl;\n }\n // cout<<p<<' '<<q<<endl;\n g:;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3300, "score_of_the_acc": -0.2198, "final_rank": 2 }, { "submission_id": "aoj_1665_10610614", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint nextcp(int cp, int cx) {\n if (cp == cx) {\n return cp + 1;\n } else if (cp == cx + 1) {\n return cp - 1;\n } else {\n return cp;\n }\n}\n\nbool check(int n, int m, int p, int q, vector<int>& x) {\n int cp = p;\n for (int i = 0; i < m; i++) {\n int cx = x[i]-1;\n cp = nextcp(cp, cx);\n }\n return cp == q;\n}\n\nbool check(int ax, int ay, int n, int m, int p, int q, vector<int>& x) {\n int cp = p;\n for (int i = 0; i <= m; i++) {\n if (ay == i) {\n int cx = ax;\n cp = nextcp(cp, cx);\n }\n if (i != m) {\n int cx = x[i]-1;\n cp = nextcp(cp, cx);\n }\n }\n return cp == q;\n}\n\nint main() {\n while (1) {\n int n, m, p, q;\n cin >> n >> m >> p >> q;\n if (n == 0 and m == 0 and p == 0 and q == 0) break;\n p--;q--;\n vector<int> x(m);\n for (int i = 0; i < m; i++) {\n cin >> x.at(i);\n }\n if (check(n, m, p, q, x)) {\n cout << \"OK\" << endl;\n } else {\n bool f = true;\n for (int ay = 0; ay <= m; ay++) {\n for (int ax = 0; ax < n - 1; ax++) {\n if (check(ax, ay, n, m, p, q, x)) {\n cout << ax + 1 << \" \" << ay << endl;\n f = false;\n break;\n }\n }\n if (!f) break;\n }\n if (f) {\n cout << \"NG\" << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3456, "score_of_the_acc": -0.8916, "final_rank": 10 }, { "submission_id": "aoj_1665_10610136", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\nusing lint = long long;\nusing mint = modint998244353;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nbool check(const vector<int> &x, int p, int q) {\n int cur = p;\n int n = x.size();\n rep(i, n) {\n if (cur == x[i]) {\n cur++;\n } else if (cur - 1 == x[i]) {\n cur--;\n }\n }\n\n if (cur == q) {\n return true;\n } else {\n return false;\n }\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n, m, p, q;\n while (1) {\n in(n, m, p, q);\n if (n == 0 and m == 0 and p == 0 and q == 0) break;\n\n vector<int> l(m);\n in(l);\n bool fin = false;\n if (check(l, p, q)) {\n out(\"OK\");\n continue;\n }\n rep(y, m + 1) {\n rep(x, 1, n) {\n vector<int> tmp;\n\n rep(i, y) {\n tmp.emplace_back(l[i]);\n }\n tmp.emplace_back(x);\n rep(i, y, m) {\n tmp.emplace_back(l[i]);\n }\n\n bool res = check(tmp, p, q);\n if (res) {\n out(x, y);\n fin = 1;\n break;\n }\n }\n if (fin) break;\n }\n\n if (!fin) {\n out(\"NG\");\n }\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3584, "score_of_the_acc": -1.5405, "final_rank": 18 }, { "submission_id": "aoj_1665_10606351", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nld pie = 3.14159265393;\nll mod = 998244353;\nll inf = 100900000;\nint main(){\n vector<string>ans;\n while (1)\n {\n ll n,m,p,q;\n cin >> n >> m >> p >> q;\n if (n==0)\n {\n break;\n }\n \n p--,q--;\n vector<ll>x(m);\n for (ll i = 0; i < m; i++)\n {\n cin >> x[i];\n x[i]--;\n }\n ll now=p;\n for (ll i = 0; i < m; i++)\n {\n if (x[i]==now)\n {\n now++;\n }else if (x[i]==now-1)\n {\n now--;\n }\n }\n if (now==q)\n {\n ans.push_back(\"OK\");\n continue;\n }\n ll xx=-1,yy=-1;\n for (ll i = 0; i < m+1; i++)\n {\n for (ll j = 0; j < n-1; j++)\n {\n ll now=p;\n for (ll k = 0; k < m+1; k++)\n {\n if (i==k)\n {\n if (j==now)\n {\n now++;\n }else if (j==now-1)\n {\n now--;\n }\n }else if (k>i)\n {\n if (x[k-1]==now)\n {\n now++;\n }else if (x[k-1]==now-1)\n {\n now--;\n }\n }else{\n if (x[k]==now)\n {\n now++;\n }else if (x[k]==now-1)\n {\n now--;\n }\n }\n }\n if (now==q)\n {\n xx=i,yy=j;\n break;\n }\n }\n if (xx!=-1)\n {\n break;\n }\n \n }\n if (xx!=-1)\n {\n string s=to_string(yy+1);\n s.push_back(' ');\n s+=to_string(xx);\n ans.push_back(s);\n }else{\n ans.push_back(\"NG\");\n }\n \n }\n \n for (ll i = 0; i < ans.size(); i++)\n {\n cout << ans[i] << endl;\n }\n \n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3520, "score_of_the_acc": -1.1755, "final_rank": 14 }, { "submission_id": "aoj_1665_10601205", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A>\nvoid chmin(A &l, const A &r) {\n if (r < l)\n l = r;\n}\ntemplate <typename A>\nvoid chmax(A &l, const A &r) {\n if (l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nll n, m, p, q;\nvector<ll> v;\nvoid input() {\n cin >> n >> m >> p >> q;\n v.resize(m);\n for (auto &x : v) cin >> x;\n}\n\nvoid solve() {\n p--;\n q--;\n for (auto &x : v)\n x--;\n\n ll now = p;\n for (int i = 0; i < m; i++) {\n if (v[i] == now) {\n now = v[i] + 1;\n } else if (v[i] + 1 == now) {\n now = v[i];\n }\n }\n if (now == q) {\n cout << \"OK\" << endl;\n return;\n }\n\n for (int j = 0; j < m + 1; j++) {\n for (int k = 0; k < n - 1; k++) {\n ll now = p;\n for (int i = 0; i < m; i++) {\n if (i == j) {\n if (k == now) {\n now = k + 1;\n } else if (k + 1 == now) {\n now = k;\n }\n }\n if (v[i] == now) {\n now = v[i] + 1;\n } else if (v[i] + 1 == now) {\n now = v[i];\n }\n }\n if (j == m) {\n if (k == now) {\n now = k + 1;\n } else if (k + 1 == now) {\n now = k;\n }\n }\n if (now == q) {\n cout << k+1 << ' ' << j << endl;\n return;\n }\n }\n }\n cout << \"NG\" << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n while (1) {\n input();\n if (n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3380, "score_of_the_acc": -0.6017, "final_rank": 6 }, { "submission_id": "aoj_1665_10595940", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing namespace std;\nusing ll = long long;//10^18まで\nusing ull = unsigned long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll,ll>;\n\n#define each(variable, container) for(auto &variable : container) \n#define time(variable,end) for(ll variable = 0; variable < end; variable++)\n#define rep(variable, start, end) for(ll variable = start; variable < end; variable++)\n#define INF 1000390039\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++)\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n return os;\n}\ntemplate <typename T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &in : v)\n is >> in;\n return is;\n}\n\nint main(){\n //ofstream cout(\"./output.txt\");\n\n while(1){\n bool ok = 0;\n int n,m,p,q;\n cin >> n >> m >> p >> q;\n if(n == 0 && m == 0){\n return 0;\n }\n\n vector<int> A(m);\n cin >> A;\n\n //initial state\n int pos = p;\n rep(i,0,m){\n if(A[i] == pos)pos++;\n else if(A[i] + 1 == pos)pos--;\n\n }\n if(pos == q){\n cout << \"OK\" << endl;\n ok = 1;\n }\n A.push_back({INF});\n m++;\n\n rep(i,0,m){\n rep(j,0,n){\n pos = p;\n rep(idx,0,m){\n if(i == idx){\n if(j == pos)pos++;\n else if(j+1 == pos)pos--;\n }\n int e = A[idx];\n if(e == pos)pos++;\n else if(e + 1 == pos)pos--;\n }\n if(pos == q && !ok){\n cout << j << ' ' << i << endl;\n ok = 1;\n }\n }\n }\n if(!ok)cout << \"NG\" << endl;\n }\n \n\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3476, "score_of_the_acc": -1.0817, "final_rank": 13 }, { "submission_id": "aoj_1665_10581392", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n, int m, int p, int q){\n vector<int> x(m);\n for (int i = 0; i < m; i++)cin >> x[i];\n \n int now = p;\n\n for (int i = 0; i < m; i++){\n if (now == x[i])now++;\n else if (now == x[i]+1)now--;\n }\n if (now == q){\n cout << \"OK\" << endl;\n return;\n }\n\n\n for (int i = 0; i <= m; i++){\n for (int j = 1; j < n; j++){\n now = p;\n for (int k = 0; k < m; k++){\n if (i == k){\n if (now == j)now++;\n else if (now == j+1)now--;\n }\n if (now == x[k])now++;\n else if (now == x[k]+1)now--;\n }\n if (i == m){\n if (now == j)now++;\n else if (now == j+1)now--;\n }\n if (now == q){\n cout << j << ' ' << i << endl;\n return;\n }\n }\n }\n cout << \"NG\" << endl;\n}\n\nint main(){\n while(1){\n int n,m,p,q;\n cin >> n >> m >> p >> q;\n if (n == 0)return 0;\n solve(n,m,p,q);\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3368, "score_of_the_acc": -0.7309, "final_rank": 9 }, { "submission_id": "aoj_1665_10579990", "code_snippet": "# pragma GCC target(\"avx\")\n# pragma GCC optimize(\"O3\")\n# pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#include <atcoder/all>\nusing namespace atcoder;\n\nusing ll = long long;\nusing mint = modint998244353;\n\nbool solve() {\n int N, M, P, Q;\n cin >> N >> M >> P >> Q;\n if(N == 0) return false;\n P--, Q--;\n auto CHECK = [&](vector<int> a) {\n int p = P;\n for(int x : a) {\n // cout << p + 1 << endl;\n if(p == x) p++;\n else if(p == x + 1) p--;\n }\n // cout << p + 1 << endl;\n return p == Q;\n };\n vector<int> A(M);\n for(int &x : A) cin >> x;\n for(int &x : A) x--;\n if(CHECK(A)) {\n cout << \"OK\" << endl;\n return true;\n }\n for(int y = 0; y <= M; y++) {\n for(int x = 0; x < N; x++) {\n vector<int> B = A;\n B.insert(B.begin() + y, x);\n // for(int v : B) cout << v + 1 << \" \"; cout << \"\\n\";\n if(CHECK(B)) {\n cout << x + 1 << \" \" << y << endl;\n return true;\n }\n }\n }\n cout << \"NG\" << endl;\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3584, "score_of_the_acc": -1.2162, "final_rank": 15 }, { "submission_id": "aoj_1665_10576068", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(int i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(int i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\n\n\nvoid solve(int &n, int &m, int &p, int &q) {\n auto change = [](int &pp, int xx) -> void {\n if(pp == xx) pp = xx + 1;\n else if(pp == xx + 1) pp = xx;\n };\n\n p--; q--;\n vector<int> x(m);\n rep(i, 0, m) {\n cin >> x[i];\n x[i]--;\n }\n\n int cur = p;\n rep(i, 0, m) {\n change(cur, x[i]);\n }\n if(cur == q) {\n cout << \"OK\" << endl;\n return ;\n }\n\n vector<pair<int, int>> candidate;\n\n cur = p;\n rep(i, 0, m + 1) {\n rep(j, 0, n) {\n if(j == cur) {\n candidate.emplace_back(make_pair(i, j - 1));\n if(j + 1 < n) candidate.emplace_back(make_pair(i, j));\n }\n }\n\n if(i == m) break;\n\n change(cur, x[i]);\n }\n\n for(auto [ansy, ansx] : candidate) {\n cur = p;\n rep(i, 0, m + 1) {\n if(ansy == i) {\n change(cur, ansx);\n }\n if(i == m) break;\n change(cur, x[i]);\n }\n if(cur == q) {\n cout << ansx + 1 << \" \" << ansy << endl;\n return ;\n }\n }\n cout << \"NG\" << endl;\n return ;\n}\n\nint main() {\n int n, m, p, q;\n while(true) {\n cin >> n >> m >> p >> q;\n if(n == 0) return 0;\n solve(n, m, p, q);\n }\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.4286, "final_rank": 4 }, { "submission_id": "aoj_1665_10554437", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while(true) {\n int N, M, P, Q;\n cin >> N >> M >> P >> Q;\n if(N == 0) break;\n --P; --Q;\n vector<int> X(M);\n for(int i=0; i<M; ++i) {\n cin >> X[i];\n --X[i];\n }\n\n int line = P;\n for(int i=0; i<M; ++i) {\n if(X[i] == line-1) --line;\n else if(X[i] == line) ++line;\n }\n if(line == Q) {\n cout << \"OK\" << endl;\n continue;\n }\n\n bool ok = false;\n for(int y=0; y<2*M; ++y) {\n int line = P;\n int x;\n for(int i=0; i<M; ++i) {\n if(i == y/2) {\n if(0 < line && y%2 == 0) {\n x = line-1;\n --line;\n } else if(line < N-1 && y%2 == 1) {\n x = line;\n ++line;\n }\n }\n if(X[i] == line-1) --line;\n else if(X[i] == line) ++line;\n }\n if(line == Q) {\n ok = true;\n cout << x+1 << ' ' << y/2 << endl;\n break;\n }\n }\n if(ok) continue;\n\n line = P;\n for(int i=0; i<M; ++i) {\n if(X[i] == line-1) --line;\n else if(X[i] == line) ++line;\n }\n if(line == Q-1) {\n cout << line+1 << ' ' << M << endl;\n continue;\n }\n if(line == Q+1) {\n cout << Q+1 << ' ' << M << endl;\n continue;\n }\n\n cout << \"NG\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -1, "final_rank": 11 }, { "submission_id": "aoj_1665_10521341", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//#include \"atcoder/dsu.hpp\"\nstruct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;\nusing ll= long long;\nusing ull=unsigned long long;\nusing ldo =long double;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_sum(_1, _2, _3,_4, name, ...) name\ntemplate<typename T>ll sum1(T &a){ll ans = 0;for(ll i=0;i<a.size();i++)ans+=a[i];return ans;}\ntemplate<typename T>ll sum2(T &a,ll start){ll ans = 0;for(ll i=start;i<a.size();i++)ans+=a[i];return ans;}\ntemplate<typename T>ll sum3(T &a,ll start,ll en){ll ans = 0;for(ll i=start;i<en;i++)ans+=a[i];return ans;}\ntemplate<typename T>ll sum4(T &a,ll start,ll en,ll tolerance){ll ans = 0;for(ll i=start;i<en;i+=tolerance)ans+=a[i];return ans;}\n#define sum(...) OVERLOAD_sum(__VA_ARGS__,sum4, sum3, sum2, sum1)(__VA_ARGS__)\n#define OVERLOAD_rep(_1, _2, _3,_4, name, ...) name\n#define rep(i,n,k) for(ll i = k; i < (ll)(n); i++)\n\n\nint amida(int n, int m, int p_start, const vector<int>& original_lines, int add_x, int add_y) {\n int current_pos = p_start; \n for (int y_level = 0; y_level <= m; ++y_level){\n if (add_x != 0 && y_level == add_y) {\n if (current_pos == add_x) {\n current_pos++;\n } else if (current_pos == add_x + 1) {\n current_pos--;\n }\n }\n if (y_level < m) { \n int original_line_x = original_lines[y_level]; \n if (current_pos == original_line_x) {\n current_pos++;\n } else if (current_pos == original_line_x + 1) {\n current_pos--;\n }\n }\n }\n return current_pos;\n}\n\nint main() {\n while(true){\n int n, m, p_start_orig, q_target_orig;\n cin >> n >> m >> p_start_orig >> q_target_orig;\n if (n == 0 && m == 0 && p_start_orig == 0 && q_target_orig == 0) {\n break;\n }\n vector<int> original_lines(m);\n rep(i, m, 0) {\n cin >> original_lines[i];\n }\n if (amida(n, m, p_start_orig, original_lines, 0, 0) == q_target_orig) {\n cout << \"OK\" << endl;\n continue;\n }\n int best_add_x = -1;\n int best_add_y = -1;\n for (int add_y_try = 0; add_y_try <= m; ++add_y_try) { \n for (int add_x_try = 1; add_x_try < n; ++add_x_try) { \n if (amida(n, m, p_start_orig, original_lines, add_x_try, add_y_try) == q_target_orig) {\n if (best_add_y == -1 || add_y_try < best_add_y) {\n best_add_y = add_y_try;\n best_add_x = add_x_try;\n } else if (add_y_try == best_add_y) {\n }\n }\n }\n }\n if (best_add_x != -1) {\n cout << best_add_x << \" \" << best_add_y << endl;\n } else {\n cout << \"NG\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3408, "score_of_the_acc": -1.2192, "final_rank": 16 }, { "submission_id": "aoj_1665_10417701", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// using mint = modint998244353;\n// using mint = modint1000000007;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(true) {\n ll n, m, p, q;\n cin >> n >> m >> p >> q;\n if(n == 0) break;\n p--, q--;\n vector<ll> x(m);\n rep(i, m) {\n cin >> x[i];\n x[i]--;\n }\n auto check = [&](ll a, ll b) -> bool {\n // 上から a 番目, b ⇔ b + 1 に対して線を追加する\n ll now = p;\n rep(i, m) {\n if(i == a) {\n if(b + 1 == now) now = b;\n else if(b == now) now = b + 1;\n }\n if(x[i] + 1 == now) now = x[i];\n else if(x[i] == now) now = x[i] + 1;\n }\n if(a == m) {\n if(b == now) now = b + 1;\n else if(b + 1 == now) now = b;\n }\n return now == q;\n };\n if(check(-1, -1)) {\n cout << \"OK\" << endl;\n continue;\n }\n bool ok = false;\n rep(j, m + 1) {\n rep(i, n - 1) {\n if(check(j, i)) {\n cout << i + 1 << \" \" << j << endl;\n ok = true;\n break;\n }\n }\n if(ok) break;\n }\n if(!ok) cout << \"NG\" << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3368, "score_of_the_acc": -0.5958, "final_rank": 5 } ]
aoj_1667_cpp
Problem D Efficient Problem Set You are planning a problem set for the upcoming programming contest. The problem set should consist of one or more problems each allocated with a certain amount of points, which is a positive integer. The score of each contestant is the sum of the points of the problems that the contestant correctly answers. The total of points of problems in the problem set must be equal to the full score given by the contest organizer. In addition, some of the contest sponsor companies want to give special prizes to the contestants first attaining prescribed scores exactly. Thus, you have to make the problem set so that every prize-obtaining score specified by a sponsor can possibly be the score of a contestant at some point of time. You have not prepared any problems yet, as you are planning to start it after deciding the points of each problem in the set. Because it is troublesome to prepare many problems, you want to meet the above-described requirements with as few problems as possible. For example, when the full score is 7 and the prize-obtaining scores are 2 and 5, you can meet them by preparing two problems with points 2 and 5. When the full score is 7 and the prize-obtaining scores are 2 and 4, however, you have to prepare three problems, e.g., those with points 2, 2, and 3. Find the minimum possible number of problems that meet the requirements. Input The input consists of multiple datasets. Each dataset is in the following format. n s n is the full score of the contest (i.e., the total of points of problems), which is a positive integer not exceeding 100. s represents which values should have possibilities to be contestants' scores. s is a string of length ( n + 1) consisting of o 's and x 's, whose ( k + 1)-st character being an o means "contestants should have possibilities to get exactly k points as their scores", and being an x means it is not required, i.e., "either is fine whether contestants can get exactly k points as their scores or not". The first and the last characters of s are always o 's. The end of the input is indicated by a line consisting of a zero. The input consists of at most 100 datasets. Output For each dataset, output in a line the minimum possible number of problems that meet the requirements. Sample Input 3 ooxo 5 oxxxxo 7 oxoxoxxo 7 oxoxxoxo 8 oxxooooxo 0 Output for the Sample Input 2 1 3 2 4
[ { "submission_id": "aoj_1667_10824752", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define MAXS 100 // 最大データセット数\n#define MAXN 100 // 最大満点\n\n\n\nint solve(int n, const string& s) {\n for (int k = 1; k <= n; ++k) {\n bool found = false;\n vector<int> tmp;\n\n function<void(int, int, int)> dfs = [&](int rem, int left, int last) {\n if (found) return;\n if (left == 0) {\n if (rem == 0) {\n bitset<101> reachable;\n reachable[0] = 1;\n for (int p : tmp) {\n reachable |= reachable << p;\n }\n for (int i = 0; i <= n; ++i) {\n if (s[i] == 'o' && !reachable[i]) return;\n }\n found = true;\n }\n return;\n }\n\n for (int i = last; i <= rem; ++i) {\n tmp.push_back(i);\n dfs(rem - i, left - 1, i);\n tmp.pop_back();\n }\n };\n\n dfs(n, k, 1); \n\n if (found) return k;\n }\n\n return -1;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n for (int i = 0; i < MAXS; ++i) {\n int n;\n if (!(cin >> n)) break;\n if (n == 0) break;\n\n string s;\n cin >> s;\n\n cout << solve(n, s) << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3464, "score_of_the_acc": -0.0701, "final_rank": 2 }, { "submission_id": "aoj_1667_10675550", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\n\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\n while(1){\n LL(n);\n if(n == 0){break;}\n STR(s);\n ll ans = 7;\n auto f = [&](vll a){\n ll res = 1LL<<60;\n ll x = 0;\n for(auto y:a){\n x += y;\n }\n x = n - x;\n if(x < 0 || x > n){return res;}\n a.push_back(x);\n if(len(a) >= ans){return res;}\n //print(a);\n bitset<101> b;\n b[0] = 1;\n for(auto x:a){\n bitset<101> nb = b | (b << x);\n swap(b,nb);\n }\n ll check = 1;\n rep(i,n+1){\n if(s[i] == 'o'){\n if(b[i] == 0){\n check = 0;\n break;\n }\n }\n }\n if(check){\n res = len(a);\n }\n return res;\n };\n chmin(ans,f({}));\n rep(i,1,n+1){\n chmin(ans,f({i}));\n rep(j,i+1,n+1){\n if(i + j > n){break;}\n chmin(ans,f({i,j}));\n rep(k,j+1,n+1){\n if(i + j + k > n){break;}\n chmin(ans,f({i,j,k}));\n rep(l,k+1,n+1){\n if(i + j + k + l > n){break;}\n chmin(ans,f({i,j,k,l}));\n rep(m,l+1,n+1){\n if(i + j + k + l + m > n){break;}\n chmin(ans,f({i,j,k,l,m}));\n }\n }\n }\n }\n }\n print(ans);\n }\n}", "accuracy": 1, "time_ms": 1440, "memory_kb": 3308, "score_of_the_acc": -0.174, "final_rank": 3 }, { "submission_id": "aoj_1667_10673000", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nstring s;\nint ans;\nvoid dfs(int depth,int last,int sum,int count,bitset<128> bs){\n if(count>=ans){\n return ;\n }\n if(depth==7){\n bool flag=true;\n for(int i=0;i<n+1;i++){\n if(s[i]=='o'&&!bs[i]){\n flag=false;\n break;\n }\n }\n if(flag){\n ans = min(ans,count);\n }\n return;\n }\n for(int i=last;i>=0;i--){\n if(sum+i>n){\n continue;\n }\n auto next=bs;\n if(i!=0){\n next|=(bs<<i);\n \n }\n dfs(depth+1,i,sum+i,count+(i!=0),next);\n }\n}\nint main(){\n while(true){\n cin >> n;\n if(n == 0) break;\n cin >> s;\n ans=7;\n bitset<128> bs;\n bs.set(0);\n dfs(0,n,0,0,bs);\n cout << ans << endl;\n } \n}", "accuracy": 1, "time_ms": 2820, "memory_kb": 3440, "score_of_the_acc": -0.3722, "final_rank": 7 }, { "submission_id": "aoj_1667_10671695", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\nusing ll = long long;\nll INF = 2e18;\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define fi first\n#define se second\n#define endl \"\\n\" // コメントアウトで随時出力\n\n/*=========================================mint=======================================*/\nconst int mintMod = MOD2; // ここを変更\nclass mint\n{\n long long x;\n\npublic:\n mint(long long x = 0) : x((x % mintMod + mintMod) % mintMod) {}\n long long val() const { return x; }\n mint operator-() const\n {\n return mint(-x);\n }\n mint &operator+=(const mint &a)\n {\n if ((x += a.x) >= mintMod)\n x -= mintMod;\n return *this;\n }\n mint &operator-=(const mint &a)\n {\n if ((x += mintMod - a.x) >= mintMod)\n x -= mintMod;\n return *this;\n }\n mint &operator*=(const mint &a)\n {\n (x *= a.x) %= mintMod;\n return *this;\n }\n mint operator+(const mint &a) const\n {\n mint res(*this);\n return res += a;\n }\n mint operator-(const mint &a) const\n {\n mint res(*this);\n return res -= a;\n }\n mint operator*(const mint &a) const\n {\n mint res(*this);\n return res *= a;\n }\n mint pow(ll t) const\n {\n if (!t)\n return 1;\n mint a = pow(t >> 1);\n a *= a;\n if (t & 1)\n a *= *this;\n return a;\n }\n // for prime mod\n mint inv() const\n {\n return pow(mintMod - 2);\n }\n mint &operator/=(const mint &a)\n {\n return (*this) *= a.inv();\n }\n mint operator/(const mint &a) const\n {\n mint res(*this);\n return res /= a;\n }\n\n friend ostream &operator<<(ostream &os, const mint &m)\n {\n os << m.x;\n return os;\n }\n friend istream &operator>>(istream &is, mint &m)\n {\n long long v;\n is >> v;\n m = mint(v);\n return is;\n }\n};\n/*========================================================================================*/\n\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\n\ntemplate <typename T>\nusing vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます\ntemplate <typename T>\nusing vv = vc<vc<T>>;\n\n// 提出の際はコメントアウト\n#define debug_x(x) cout << #x << \" = \" << (x) << endl\n#define debug_vc(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) cout << v[i] << ' '; \\\n cout << endl; \\\n } // 一次元配列を出力する\n#define debug_vv(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) \\\n { \\\n rep(j, size(v[i])) { cout << v[i][j] << ' '; } \\\n cout << endl; \\\n } \\\n } // 二次元配列を出力する\n\n// #define debug_x(x) \\\n// do \\\n// { \\\n// } while (0)\n// #define debug_vc(v) \\\n// do \\\n// { \\\n// } while (0)\n// #define debug_vv(v) \\\n// do \\\n// { \\\n// } while (0)\n\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = 1; i <= (n); ++i)\n#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define nfor(i, s, n) for (ll i = s; i < n; i++) // i=s,s+1...n-1 ノーマルfor\n#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる\n\nusing ld = long double;\nusing bl = bool;\n\ntemplate <class T>\nusing pq = priority_queue<T, vc<T>>; // 大きい順\ntemplate <class T>\nusing pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順\n\nusing vl = vc<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vv<vl>;\nusing vvvvl = vv<vvl>;\nusing vs = vc<string>;\nusing vvs = vv<string>;\nusing vb = vc<bl>;\nusing vvb = vv<bl>;\nusing vvvb = vv<vb>;\nusing vld = vc<ld>;\nusing vvld = vv<ld>;\nusing vvvld = vv<vld>;\nusing P = pair<ll, ll>;\nusing vP = vc<P>;\nusing vvP = vc<vP>;\nusing vvvP = vv<vP>;\nusing vmint = vc<mint>;\nusing vvmint = vv<mint>;\nusing vvvmint = vv<vmint>;\n\n#define pb push_back\n#define eb emplace_back\n\n#define YES cout << \"Yes\" << endl\n#define NO cout << \"No\" << endl\n#define YN \\\n { \\\n cout << \"Yes\" << endl; \\\n } \\\n else \\\n { \\\n cout << \"No\" << endl; \\\n } // if(a==b)YN;\n#define dame cout << -1 << endl\n\nstring dir = \"DRUL\"; // 第4象限の場合\nvl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};\nvl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};\n\nbool out_grid(ll i, ll j, ll h, ll w)\n{ // trueならcontinue\n return (!(0 <= i && i < h && 0 <= j && j < w));\n}\n\nll n;\nvl v = {1};\nll ans = 7;\nvb isPoint;\n\nvoid dp(ll cnt)\n{\n // debug_vc(v);\n vb dp(n + 1);\n rep(i, 1 << cnt)\n {\n ll sum = 0;\n rep(j, cnt)\n {\n if (i & (1 << j))\n sum += v[j + 1];\n }\n dp[sum] = true;\n }\n bl ok = true;\n rep(k, n + 1)\n {\n if (isPoint[k] && !dp[k])\n {\n ok = false;\n break;\n }\n }\n if (ok)\n {\n ans = min(ans,cnt);\n }\n // debug_x(ans);\n}\n\nvoid dfs(ll cnt, ll sum)\n{\n if (sum == n)\n {\n dp(cnt);\n }\n if (cnt >= 6)\n {\n return;\n }\n for (ll i = v.back(); i <= n - sum; i++)\n {\n v.pb(i);\n dfs(cnt + 1, sum + i);\n v.pop_back();\n }\n}\n\nvoid solve()\n{\n string s;\n cin >> s;\n ans = 7;\n isPoint.assign(n+1,false);\n rep(i, n + 1)\n {\n if (s[i] == 'o')\n isPoint[i] = true;\n }\n // debug_vc(isPoint);\n dfs(0, 0);\n \n // rep(_,7){\n // // if(p[].size()==0)continue;\n // debug_vv(p[_]);\n // }\n\n // \n cout << ans << endl;\n return;\n}\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true)\n {\n cin >> n;\n if (n == 0)\n break;\n solve();\n }\n return 0;\n}\n\n/*\nshift+alt+Fで成形\nf2で変数名変更\n\nスニペット\nbasic\n m:atcoder以外用\n ma:ABC/ARC用\n mahc:AHC用\nmath\n floor:床関数\n ceil:天井関数\n comv:二項係数\n modpow: a^b mod m\n GCD:最大公約数\n extGCD:拡張ユークリッド互除法\n PFD:素因数分解\n isprime:エラトステネスの篩\n matrixpow:行列累乗\ndata_structure\n seg:セグメント木\ntechnic\n Compress:座標圧縮\n runlength:ランレングス圧縮\ngraph\n warshall:ワーシャルフロイド法\n dfs:深さ優先探索\n bfs:幅優先探索\n LCA:最近共通祖先\n*/", "accuracy": 1, "time_ms": 4730, "memory_kb": 3472, "score_of_the_acc": -0.645, "final_rank": 14 }, { "submission_id": "aoj_1667_10669973", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nbool check(int N,vector<vector<int>>X,vector<vector<int>>Y){\n vector<pair<int,int>>at(N*N+1);\n rep(i,0,N)rep(j,0,N){\n at[Y[i][j]]={i,j};\n }\n rep(i,0,N)rep(j,0,N)rep(k,0,N)rep(l,0,N){\n if(abs(i-k)+abs(j-l)==1){\n int a=X[i][j],b=X[k][l];\n auto[x,y]=at[a];\n auto[z,w]=at[b];\n if(abs(x-z)+abs(y-w)<N/2)return false;\n }\n }\n return true;\n}\n\nint solve(int N,string S){\n int ans=1<<30;\n vector<int>vec;\n vector<bool>flag(N+1);\n rep(a,0,N+1){\n if(6*a>N-a)break;\n if(a)vec.push_back(a);\n rep(b,a,N+1-a){\n if(5*b>N-a-b)break;\n if(b)vec.push_back(b);\n rep(c,b,N+1-a-b){\n if(4*c>N-a-b-c)break;\n if(c)vec.push_back(c);\n rep(d,c,N+1-a-b-c){\n if(3*d>N-a-b-c-d)break;\n if(d)vec.push_back(d);\n rep(e,d,N+1-a-b-c-d){\n if(2*e>N-a-b-c-d-e)break;\n if(e)vec.push_back(e);\n rep(f,e,N+1-a-b-c-d-e){\n if(f>N-a-b-c-d-e-f)break;\n if(f)vec.push_back(f);\n {\n int g=N-a-b-c-d-e-f;\n vec.push_back(g);\n for(int bit=0;bit<1<<(int)vec.size();bit++){\n int cnt=0;\n rep(i,0,(int)vec.size()){\n if(bit>>i&1)cnt+=vec[i];\n }\n flag[cnt]=true;\n }\n bool ok=true;\n rep(i,0,N+1){\n if(S[i]=='o'&&!flag[i]){\n ok=false;\n break;\n }\n }\n if(ok)return (int)vec.size();\n vec.pop_back();\n rep(i,0,N+1)flag[i]=false;\n }\n if(f)vec.pop_back();\n }\n if(e)vec.pop_back();\n }\n if(d)vec.pop_back();\n }\n if(c)vec.pop_back();\n }\n if(b)vec.pop_back();\n }\n if(a)vec.pop_back();\n }\n return -1;\n}\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n string S;\n cin>>S;\n int ans=solve(N,S);\n assert(ans!=-1);\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 2620, "memory_kb": 3584, "score_of_the_acc": -0.3452, "final_rank": 6 }, { "submission_id": "aoj_1667_10665664", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nll INF = 1e18;\n\ntemplate <typename A>\nvoid chmin(A &l, const A &r) {\n if(r < l) l = r;\n}\ntemplate <typename A>\nvoid chmax(A &l, const A &r) {\n if(l < r) l = r;\n}\n\nvector<int> v;\nvector<vector<int>> su;\nvector<bitset<101>> w;\nvoid init() {\n su.resize(101);\n auto check = [&](vector<int> &v) -> bitset<101> {\n vector<char> ok(101, 0);\n for(int i = 0; i < (1 << v.size()); i++) {\n ll sum = 0;\n for(int j = 0; j < v.size(); j++) {\n if(i & (1 << j)) sum += v[j];\n }\n ok[sum] = 1;\n }\n bitset<101> ret;\n for(int i = 0; i <= 100; i++) {\n if(ok[i]) ret.set(i, 1);\n }\n return ret;\n };\n auto make = [&](auto self, vector<int> &now, ll depth, ll sum) -> void {\n if(now.size()) {\n auto ret = check(now);\n su[sum].push_back(v.size());\n v.push_back(now.size());\n w.push_back(ret);\n }\n if(depth){\n int st = 1;\n if(now.size()) st = now.back();\n for(int i = st; i * depth <= (100 - sum); i++) {\n now.push_back(i);\n self(self, now, depth - 1, sum + i);\n now.pop_back();\n }\n }\n };\n for(int i = 1; i <= 6; i++) {\n vector<int> now;\n make(make, now, i, 0);\n }\n}\nll n;\nstring s;\nvoid input() { cin >> n >> s; }\nvoid solve() {\n bitset<101> bt;\n for(int i = 0; i <= n; i++) {\n bt.set(i, (s[i] == 'x' ? 1 : 0));\n }\n for(int i = n + 1; i < 101; i++) {\n bt.set(i, 1);\n }\n ll ans = 7;\n bitset<101> fin;\n for(auto &i:su[n]) {\n auto res = bt | w[i];\n if(res.count() == 101) {\n if(ans>v[i])\n fin=w[i];\n chmin(ans,(ll)v[i]);\n }\n }\n cout << ans << endl;\n}\n\nifstream in;\nofstream out;\nint main(int argc, char **argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n init();\n while(1) {\n input();\n if(n == 0) break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1360, "memory_kb": 101808, "score_of_the_acc": -1.1626, "final_rank": 20 }, { "submission_id": "aoj_1667_10662419", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\n#undef long\n#define long long long\n#define ll long\n#define vec vector\n#define rep(i, n) for (long i = 0; i < n; i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(a).size()\ntemplate <typename T>\nbool chmin(T &x, T y)\n{\n if (y < x)\n {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmax(T &x, T y)\n{\n if (x < y)\n {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<T> a)\n{\n const int n = a.size();\n rep(i, n)\n {\n os << a[i];\n if (i + 1 != n)\n os << \" \";\n }\n return os;\n}\nusing iint = __int128_t;\nstruct S\n{\n iint now;\n int gekai;\n int nok;\n};\nvoid solve()\n{\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n string ss;\n cin >> ss;\n iint s = 0;\n rep(i, n + 1) if (ss[i] == 'o') s |= ((iint)1) << i;\n constexpr int INF = 105;\n vec<iint> ma(INF);\n rep(i, INF) ma[i] = (((iint)1) << i) - 1;\n vec<S> a;\n a.push_back({(iint)1, 1, n});\n int ans = 0;\n while (true)\n {\n ans++;\n vec<S> aa;\n for (auto [now, gekai, nok] : a)\n {\n for (int i = gekai; i <= n && i <= nok; i++)\n {\n iint nnow = (now | (now << i)) & ma[n + 1];\n if ((nnow & s) == s)\n {\n cout << ans << endl;\n return;\n }\n if ((now & s & ma[gekai]) != (s & ma[gekai]))\n continue;\n aa.push_back({nnow, i, nok - i});\n }\n }\n swap(a, aa);\n }\n}\nint main()\n{\n cin.tie(0)->sync_with_stdio(0), cout.tie(0);\n cout << fixed << setprecision(20);\n int t = 1;\n // cin >> t;\n while (1)\n solve();\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 70780, "score_of_the_acc": -0.685, "final_rank": 15 }, { "submission_id": "aoj_1667_10661862", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n string s;cin>>s;\n int m=1;\n while(1<<m <=n)m++;\n vi v;\n int sum=0;\n int ans=1001010;\n bitset<101> dp(0);\n auto dfs=[&](auto dfs,int i)->void{\n if(sum==n&&sz(v)<=m){\n dp=0;\n dp[0]=1;\n for(auto x:v)dp|=dp<<x;\n bool ok = 1;\n rep(j,0,n+1)if(s[j]=='o'&&dp[j]==0)ok=0;\n if(ok)chmin(ans,sz(v));\n }\n if(sz(v)==m)return;\n if(sz(v)>=ans)return;\n for(int j=i;j<=n-sum;j++){\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n }\n };\n dfs(dfs,1);\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 3160, "memory_kb": 3456, "score_of_the_acc": -0.4209, "final_rank": 9 }, { "submission_id": "aoj_1667_10661860", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n string s;cin>>s;\n int m=1;\n while(1<<m <=n)m++;\n vi v;\n int sum=0;\n int ans=1001010;\n bitset<101> dp(0);\n auto dfs=[&](auto dfs,int i)->void{\n if(sum==n&&sz(v)<=m){\n dp=0;\n dp[0]=1;\n for(auto x:v)dp|=dp<<x;\n bool ok = 1;\n rep(j,0,n+1)if(s[j]=='o'&&dp[j]==0)ok=0;\n if(ok)chmin(ans,sz(v));\n }\n if(sz(v)==m)return;\n if(sz(v)>=ans)return;\n if(sz(v)==m-1){\n int j=n-sum;\n if(j<1)return;\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n return;\n }\n for(int j=i;j<=n-sum;j++){\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n }\n };\n dfs(dfs,1);\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 5690, "memory_kb": 3456, "score_of_the_acc": -0.7818, "final_rank": 17 }, { "submission_id": "aoj_1667_10661856", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n string s;cin>>s;\n int m=1;\n while(1<<m <=n)m++;\n vi v;\n int sum=0;\n int ans=1001010;\n bitset<101> dp(0);\n auto dfs=[&](auto dfs,int i)->void{\n if(sum==n&&sz(v)<=m){\n dp=0;\n dp[0]=1;\n for(auto x:v)dp|=dp<<x;\n bool ok = 1;\n rep(j,0,n+1)if(s[j]=='o'&&dp[j]==0)ok=0;\n if(ok)chmin(ans,sz(v));\n }\n if(sz(v)==m)return;\n if(sz(v)>=ans)return;\n // if(sz(v)==m-1){\n // int j=n-sum;\n // v.push_back(j);\n // sum+=j;\n // dfs(dfs,j);\n // sum-=j;\n // v.pop_back();\n // return;\n // }\n for(int j=i;j<=n-sum;j++){\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n }\n };\n dfs(dfs,1);\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 3160, "memory_kb": 3456, "score_of_the_acc": -0.4209, "final_rank": 9 }, { "submission_id": "aoj_1667_10661854", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n string s;cin>>s;\n int m=1;\n while(1<<m <=n)m++;\n vi v;\n int sum=0;\n int ans=1001010;\n bitset<101> dp(0);\n auto dfs=[&](auto dfs,int i)->void{\n if(sum==n&&sz(v)<=m){\n dp=0;\n dp[0]=1;\n for(auto x:v)dp|=dp<<x;\n bool ok = 1;\n rep(j,0,n+1)if(s[j]=='o'&&dp[j]==0)ok=0;\n if(ok)chmin(ans,sz(v));\n }\n if(sz(v)==m)return;\n if(sz(v)>=ans)return;\n if(sz(v)==m-1){\n int j=n-sum;\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n return;\n }\n for(int j=i;j<=n-sum;j++){\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n }\n };\n dfs(dfs,1);\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 7230, "memory_kb": 3456, "score_of_the_acc": -1.0015, "final_rank": 19 }, { "submission_id": "aoj_1667_10661844", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n string s;cin>>s;\n int m=1;\n while(1<<m <=n)m++;\n vi v;\n int sum=0;\n int ans=1001010;\n bitset<101> dp(0);\n auto dfs=[&](auto dfs,int i)->void{\n if(sum==n&&sz(v)<=m){\n dp=0;\n dp[0]=1;\n rep(j,0,sz(v)){\n int u=v[j];\n dp|=(dp<<u);\n }\n bool ok = 1;\n rep(j,0,n+1)if(s[j]=='o'&&dp[j]==0)ok=0;\n if(ok){\n chmin(ans,sz(v));\n }\n }\n if(sz(v)==m)return;\n for(int j=i;j<=n-sum;j++){\n v.push_back(j);\n sum+=j;\n dfs(dfs,j);\n sum-=j;\n v.pop_back();\n }\n };\n dfs(dfs,1);\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 5240, "memory_kb": 3584, "score_of_the_acc": -0.7189, "final_rank": 16 }, { "submission_id": "aoj_1667_10650415", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,N) for(i=0;i<N;i++)\n#define ll long long\n\nvector<vector<ll>>P[7];\nvector<ll>p;\nvoid dfs(ll pre,ll rest){\n if(rest==0){\n P[p.size()].push_back(p);\n return;\n }\n if(p.size()==5){\n if(pre<=rest){\n p.push_back(rest);\n P[p.size()].push_back(p);\n p.pop_back();\n }\n return;\n }\n for(ll i=pre;i<=rest;i++){\n p.push_back(i);\n dfs(i,rest-i);\n p.pop_back();\n }\n}\n\nint main(void){\n ll i,j,k;\n\n ll N;\n string S;\n\n for(;;){\n cin>>N;\n if(N==0)break;\n cin>>S;\n vector<ll> need;\n rep(i,S.size()){\n if(S[i]=='o')need.push_back(i);\n }\n\n for(i=1;i<=6;i++)P[i].clear();\n dfs(1,N);\n\n ll ans=7;\n for(k=1;k<=6;k++){\n for(auto v:P[k]){\n bitset<101>dp(1);\n for(auto x:v){\n for(i=N-x;0<=i;i--){\n if(dp[i])dp.set(i+x);\n }\n }\n\n ll cnt=0;\n for(auto n:need){\n cnt+=dp[n];\n }\n\n if(cnt==need.size()){\n ans=v.size();\n break;\n }\n }\n if(ans!=7)break;\n }\n\n cout<<ans<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 3010, "memory_kb": 21064, "score_of_the_acc": -0.5783, "final_rank": 13 }, { "submission_id": "aoj_1667_10649576", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#ifdef LOCAL\n #include \"settings/debug.cpp\"\n#else\n #define Debug(...) void(0)\n#endif\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nusing ull = unsigned long long;\n\ninline void solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n string s;\n cin >> s;\n bitset<101> flg;\n reverse(s.begin(), s.end());\n rep(i, n + 1) flg[i] = s[i] == 'o';\n int ans = 7;\n for (int p1 = 0; p1 <= n; p1++) {\n for (int p2 = p1; p1 + p2 <= n; p2++) {\n for (int p3 = p2; p1 + p2 + p3 <= n; p3++) {\n for (int p4 = p3; p1 + p2 + p3 + p4 <= n; p4++) {\n for (int p5 = p4; p1 + p2 + p3 + p4 + p5 <= n; p5++) {\n for (int p6 = p5; p1 + p2 + p3 + p4 + p5 + p6 <= n; p6++) {\n bitset<101> flg2;\n flg2[0] = true;\n flg2 |= flg2 << p1;\n flg2 |= flg2 << p2;\n flg2 |= flg2 << p3;\n flg2 |= flg2 << p4;\n flg2 |= flg2 << p5;\n flg2 |= flg2 << p6;\n if ((flg2 & flg) == flg) {\n int cnt = 6;\n if (p1 == 0) cnt--;\n if (p2 == 0) cnt--;\n if (p3 == 0) cnt--;\n if (p4 == 0) cnt--;\n if (p5 == 0) cnt--;\n if (p6 == 0) cnt--;\n ans = min(ans, cnt);\n }\n }\n }\n }\n }\n }\n }\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n while (true) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 2840, "memory_kb": 3456, "score_of_the_acc": -0.3753, "final_rank": 8 }, { "submission_id": "aoj_1667_10649488", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(true) {\n ll n;\n cin >> n;\n if(n == 0) break;\n\n string s;\n cin >> s;\n ll o_cnt = 0;\n rep(i, s.size()) if(s[i] == 'o') o_cnt++;\n\n // 高々答えは 7\n // 99_C_5 が間に合う → 6 個まででできないなら 7 できるならそれが答え\n\n vector<bool> ok(s.size(), false);\n auto check = [&](const vector<ll>& g) -> bool {\n ok.assign(s.size(), false);\n ll cnt = 0;\n rep(i, (1LL << g.size())) {\n ll sum = 0;\n rep(j, g.size()) if(i & (1LL << j)) sum += g[j];\n if(!ok[sum] && s[sum] == 'o') {\n ok[sum] = true;\n cnt++;\n }\n }\n // cerr << \"Checking group: \";\n // for(ll x: g) cerr << x << ' ';\n // cerr << \" -> cnt: \" << cnt << '\\n';\n return cnt == o_cnt;\n };\n\n ll ans = 7, g_size;\n vector<ll> group;\n auto dfs = [&](auto self, ll sum) -> void {\n if(group.size() == g_size - 1) {\n if(n - sum < 0 || (group.empty() ? 0 : group.back()) > n - sum) return;\n group.emplace_back(n - sum);\n if(check(group)) ans = g_size;\n group.pop_back();\n return;\n }\n for(ll i = (group.empty() ? 1 : group.back()); i <= (n - sum) / 2; i++) {\n group.emplace_back(i);\n self(self, sum + i);\n if(ans != 7) return;\n group.pop_back();\n }\n };\n for(ll size = 1; size <= 6; size++) {\n g_size = size;\n group.clear();\n dfs(dfs, 0);\n if(ans != 7) break;\n }\n cout << ans << '\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2500, "memory_kb": 3396, "score_of_the_acc": -0.3261, "final_rank": 5 }, { "submission_id": "aoj_1667_10609979", "code_snippet": "#include <iostream>\n#include <vector>\n#include <bit>\n#include <bitset>\n#include <algorithm>\nconst int MAXN = 101;\nusing bitset = std::bitset<MAXN>;\nint N, BOUND;\nstd::string S;\nbitset S_BIT;\n// num: 現在の問題セットの問題数\n// sum: 現在の問題セットの満点\n// max: 現在の問題セットで一番点数が高い問題の点数\n// dp : 実現可能な点数\nint dfs(int num, int sum, int max, const bitset& dp) {\n if (num >= BOUND) return BOUND;\n if (sum == N) {\n if ((S_BIT | (S_BIT ^ dp)) == dp) {\n return num;\n }\n else {\n return BOUND;\n }\n }\n int res = BOUND;\n for (int i = max ; i <= N - sum ; i++) {\n bitset next_dp = dp | (dp << i); \n res = std::min(res, dfs(num + 1, sum + i, i, next_dp));\n }\n return res;\n}\nint solve() {\n // 解は必ずBOUND以下\n BOUND = std::bit_width((unsigned)N);\n // std::bitsetのデフォルトコンストラクタは全部falseで初期化\n S_BIT = bitset{};\n for (int i = 0 ; i <= N ; i++) {\n S_BIT[i] = S[i] == 'o';\n }\n bitset start{};\n start[0] = 1;\n return dfs(0, 0, 1, start);\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n std::cin >> S;\n std::cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 3584, "score_of_the_acc": -0.2225, "final_rank": 4 }, { "submission_id": "aoj_1667_10609560", "code_snippet": "#include <iostream>\n#include <vector>\n#include <bit>\n#include <bitset>\n#include <algorithm>\nconst int MAXN = 101;\nusing bitset = std::bitset<MAXN>;\nint N, BOUND;\nstd::string S;\nbitset S_BIT;\n// num: 現在の問題セットの問題数\n// sum: 現在の問題セットの満点\n// max: 現在の問題セットで一番点数が高い問題の点数\n// dp : 実現可能な点数\nint dfs(int num, int sum, int max, const bitset& dp) {\n if (num > BOUND) return (int)1e9;\n if (sum == N) {\n if ((S_BIT | (S_BIT ^ dp)) == dp) {\n return num;\n }\n else {\n return (int)1e9;\n }\n }\n int res = (int)1e9;\n for (int i = max ; i <= N - sum ; i++) {\n bitset next_dp = dp | (dp << i); \n res = std::min(res, dfs(num + 1, sum + i, i, next_dp));\n }\n return res;\n}\nint solve() {\n // 解は必ずBOUND以下\n BOUND = std::bit_width((unsigned)N);\n // std::bitsetのデフォルトコンストラクタは全部falseで初期化\n S_BIT = bitset{};\n for (int i = 0 ; i <= N ; i++) {\n S_BIT[i] = S[i] == 'o';\n }\n bitset start{};\n start[0] = 1;\n return dfs(0, 0, 1, start);\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n std::cin >> S;\n std::cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 4040, "memory_kb": 3584, "score_of_the_acc": -0.5477, "final_rank": 11 }, { "submission_id": "aoj_1667_10609554", "code_snippet": "#include <iostream>\n#include <vector>\n#include <bit>\n#include <bitset>\n#include <algorithm>\nconst int MAXN = 101;\nusing bitset = std::bitset<MAXN>;\nint N, BOUND;\nstd::string S;\nbitset S_BIT;\n// num: 現在の問題セットの問題数\n// sum: 現在の問題セットの満点\n// max: 現在の問題セットで一番点数が高い問題の点数\n// dp : 実現可能な点数\nint dfs(int num, int sum, int max, bitset dp) {\n if (num > BOUND) return (int)1e9;\n if (sum == N) {\n if ((S_BIT | (S_BIT ^ dp)) == dp) {\n return num;\n }\n else {\n return (int)1e9;\n }\n }\n int res = (int)1e9;\n for (int i = max ; i <= N - sum ; i++) {\n bitset next_dp = dp | (dp << i); \n res = std::min(res, dfs(num + 1, sum + i, i, next_dp));\n }\n return res;\n}\nint solve() {\n // 解は必ずBOUND以下\n BOUND = std::bit_width((unsigned)N);\n // std::bitsetのデフォルトコンストラクタは全部falseで初期化\n S_BIT = bitset{};\n for (int i = 0 ; i <= N ; i++) {\n S_BIT[i] = S[i] == 'o';\n }\n bitset start{};\n start[0] = 1;\n return dfs(0, 0, 1, start);\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n std::cin >> S;\n std::cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 4160, "memory_kb": 3456, "score_of_the_acc": -0.5636, "final_rank": 12 }, { "submission_id": "aoj_1667_10604290", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\ntemplate<class T, size_t n, size_t idx = 0>\nauto make_vec(const size_t (&d)[n], const T& init) noexcept {\n if constexpr (idx < n) return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else return init;\n}\ntemplate<class T, size_t n>\nauto make_vec(const size_t (&d)[n]) noexcept {\n return make_vec(d, T{});\n}\n/**\n * Read a vector from input. Set start to 1 if you want it to be 1-indexed.\n */\ntemplate<typename T>\nvector<T> read_vector(int N, int start = 0) {\n vector<T> v(start + N);\n for (int i = start; i < (int)v.size(); i++) {\n std::cin >> v[i];\n }\n return v;\n}\n\nvoid solve(int &n, string &s) {\n bitset<101> sbit;\n rep(i, 0, n + 1) {\n if(s[i] == 'o') {\n sbit.set(i);\n }\n }\n int maxProblemCount = 7;\n\n int ans = maxProblemCount;\n\n vector<int> scores;\n vector<int> maxscores = {n/7, n/6, n/5, n/4, n/3, n/2, n/1};\n int scoresum = 0;\n bool isfinished = false;\n function<void(ll)> dfs = [&](ll depth) -> void {\n if(depth == maxProblemCount - 1) {\n int lastscore = n - scoresum;\n if(lastscore < scores.back()) return ;\n\n scores.emplace_back(lastscore);\n\n // 判定\n bitset<101> curbit;\n curbit.set(0);\n rep(i, 0, sz(scores)) {\n bitset<101> nextbit = curbit;\n nextbit |= (curbit << scores[i]);\n swap(nextbit, curbit);\n }\n\n if((curbit & sbit) == sbit) {\n rep(i, 0, sz(scores)) {\n if(scores[i] != 0) {\n ans = maxProblemCount - i;\n break;\n }\n }\n isfinished = true;\n }\n\n scores.pop_back();\n return ;\n }\n\n int minscore = 0;\n if(depth > 0) minscore = scores.back();\n\n rep(i, minscore, maxscores[depth] + 1) {\n if(isfinished) return ;\n if(scoresum + i > n) break;\n \n scores.emplace_back(i);\n scoresum += i;\n\n dfs(depth + 1);\n\n if(isfinished) return ;\n scoresum -= i;\n scores.pop_back();\n }\n\n };\n\n dfs(0);\n cout << ans << endl;\n}\n\nint main() {\n int n;\n string s;\n while(true) {\n cin >> n;\n if(n == 0) return 0;\n cin >> s;\n solve(n, s);\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3448, "score_of_the_acc": -0.0057, "final_rank": 1 }, { "submission_id": "aoj_1667_10447878", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\nusing pii = pair<int, int>;\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\ntemplate <typename T> void print(vector<T> A);\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (true) {\n int N;\n cin >> N;\n if (N == 0) {\n break;\n }\n string S;\n cin >> S;\n vector<int> sp;\n for (int i = 0; i < N + 1; i++) {\n if (S[i] == 'o') {\n sp.push_back(i);\n }\n }\n\n\n\n auto f = [&](vector<int> &ps) {\n int n = ps.size();\n bitset<101 * 7> dp;\n dp[0]=1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 101; j++) {\n int ind = 101 * i + j;\n if (dp[ind] == 0) {\n continue;\n }\n\n int nex_ind = 101 * (i + 1) + j;\n dp[nex_ind] = 1;\n if (j + ps[i] < 101) {\n int nex_ind2 = 101 * (i + 1) + j + ps[i];\n dp[nex_ind2]=1;\n }\n }\n }\n bool ret=true;\n for (auto num : sp) {\n if (dp[101 * n + num] == 0) {\n ret=false;\n }\n }\n \n return ret;\n };\n\n vector<int> lis;\n int sum=0;\n auto solve = [&](auto solve) {\n //if (lis.size())print(lis);\n int ret = 7;\n if (lis.size() == 7) {\n return ret;\n }\n if (sum == N) {\n if (f(lis)) {\n ret=lis.size();\n }\n return ret;\n }\n if (lis.size() == 0) {\n for (int v = 1; v <= N; v++) {\n lis.push_back(v);\n sum += v;\n ret=min(ret,solve(solve));\n sum -= v;\n lis.pop_back();\n }\n } else {\n for (int v = lis.back(); v <= N - sum; v++) {\n lis.push_back(v);\n sum += v;\n ret=min(ret,solve(solve));\n sum -= v;\n lis.pop_back();\n }\n }\n return ret;\n };\n cout<<solve(solve)<<endl;\n }\n}\n\ntemplate <typename T> void print(vector<T> A) {\n for (int i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}", "accuracy": 1, "time_ms": 6880, "memory_kb": 3584, "score_of_the_acc": -0.9529, "final_rank": 18 } ]
aoj_1669_cpp
Problem F Villa of Emblem Shape The princess has a vast private estate. She has decided to build a new villa there. According to her creative request, the ground floor outline of the main house of the villa must be derived from the emblem of the royal family. The emblem is a simple polygon. Four examples are shown in Figure F-1. The outline does not have to be the exact shape of the emblem; it can be formed by sliding and overlaying multiple copies of the emblem, like in Figure F-2. Any finite number of copies can be used, but all the copies should have the same size and orientation without being reversed. Dataset 1 Dataset 2 Dataset 3 Dataset 4 Figure F-1: The emblems in Sample Input Figure F-2: An example of overlaying of copies for the first dataset of Sample Input For security reasons, the outline must be a convex polygon. The rightmost of Figure F-2 is a convex polygon. Your task is to judge whether a convex polygon can be constructed by overlaying a finite number of copies of the emblem. Input The input consists of multiple datasets, each in the following format. n x 1 y 1 ⋮ x n y n n is the number of the vertices of the emblem, an integer between 3 and 200, inclusive. ( x i , y i ) gives the coordinates of the i -th vertex in the right-hand coordinate system ( i = 1, ..., n ). Each of x i and y i is an integer between −10000 and 10000, inclusive. Any two vertices have different coordinates. The vertices are listed in a counterclockwise order. Any two edges of the emblem do not have a common point other than their connecting vertex. For each vertex, the angle of the two edges is not 180 degrees. The end of the input is indicated by a line consisting of a zero. The input consists of at most 500 datasets. Output For each dataset, output in a line Yes if a convex polygon can be constructed, and No , otherwise. Sample Input 12 -5 -7 5 -7 5 -3 1 -3 1 3 5 3 5 7 -5 7 -5 3 -1 3 -1 -3 -5 -3 7 0 0 7 1 3 2 2 4 6 9 4 10 1 7 14 0 1 2 0 3 1 2 4 0 4 1 2 -1 1 0 3 -1 4 -2 4 -3 3 -2 -3 -1 -3 0 -2 5 -2 0 1 -4 2 -3 2 3 1 4 0 Output for the Sample Input Yes No Yes Yes
[ { "submission_id": "aoj_1669_10958528", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <vector>\ntypedef long long ll;\ntypedef double ld;\ninline int sign(const ll& x) { return x < 0 ? -1 : !!x; }\n\nint N;\nstruct Pos {\n\tint x, y, i;\n\tPos(int x_ = 0, int y_ = 0, int i_ = -1) : x(x_), y(y_), i(i_) {}\n\tbool operator == (const Pos& p) const { return x == p.x && y == p.y; }\n\tbool operator < (const Pos& p) const { return x == p.x ? y < p.y : x < p.x; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tll operator / (const Pos& p) const { return (ll)x * p.y - (ll)y * p.x; }\n\tll Euc() const { return (ll)x * x + (ll)y * y; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = Pos(0, 0);\ntypedef std::vector<Pos> Polygon;\nll cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { return sign(cross(d1, d2, d3)); }\nPolygon graham_scan(Polygon C) {\n\tPolygon H;\n\tif (C.size() < 3) {\n\t\tstd::sort(C.begin(), C.end());\n\t\treturn C;\n\t}\n\tstd::swap(C[0], *min_element(C.begin(), C.end()));\n\tstd::sort(C.begin() + 1, C.end(), [&](const Pos& p, const Pos& q) -> bool {\n\t\tint ret = ccw(C[0], p, q);\n\t\tif (!ret) return (C[0] - p).Euc() < (C[0] - q).Euc();\n\t\treturn ret > 0;\n\t\t}\n\t);\n\tC.erase(unique(C.begin(), C.end()), C.end());\n\tint sz = C.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\twhile (H.size() >= 2 && ccw(H[H.size() - 2], H.back(), C[i]) <= 0)\n\t\t\tH.pop_back();\n\t\tH.push_back(C[i]);\n\t}\n\treturn H;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(9);\n\twhile (1) {\n\t\tstd::cin >> N;\n\t\tif (!N) break;\n\t\tPolygon P(N);\n\t\tfor (int i = 0; i < N; i++) std::cin >> P[i], P[i].i = i;\n\t\tPolygon H = graham_scan(P);\n\t\tint sz = H.size();\n\t\tbool f = 1;\n\t\tfor (int i = 0, s, e; i < sz; i++) {\n\t\t\ts = H[i].i, e = H[(i + 1) % sz].i;\n\t\t\tif ((s + 1) % N == e) continue;\n\t\t\tif (ccw(P[s], P[e], P[(s + 1) % N])) { f = 0; break; }\n\t\t\tif (ccw(P[s], P[e], P[(e - 1 + N) % N])) { f = 0; break; }\n\t\t}\n\t\tstd::cout << (f ? \"Yes\\n\" : \"No\\n\");\n\t}\n\treturn;\n}\nint main() { solve(); return 0; }", "accuracy": 1, "time_ms": 10, "memory_kb": 3436, "score_of_the_acc": -0.0068, "final_rank": 2 }, { "submission_id": "aoj_1669_10686294", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define REPR(i, n) for (int i = n; i >= 0; i--)\n#define FOR(i, m, n) for (ll i = m; i < n; i++)\n#define FORR(i, m, n) for (ll i = m; i >= n; i--)\n#define REPO(i, n) for (ll i = 1; i <= n; i++)\n#define ll long long\n#define INFll (ll)1ll << 60 \n#define MINFll (-1 * INFll)\n#define ALL(n) n.begin(), n.end()\n#define MOD (ll)998244353\n#define P pair<ll, ll>\n\n//ok -> verified\nusing Real = double;\nusing Pt = complex<Real>;\nconst Real EPS = 1e-8;\n\nint sgn(Real a){//ok\n return (a < -EPS) ? -1 : (a > EPS) ? 1 : 0;\n}\ninline Pt operator*= (const Pt &a, const Real &b){\n return Pt(real(a) * b, imag(a) * b);\n}\n\ninline Pt operator* (const Pt &a, const Real &b){\n return a *= b;\n}\n\nnamespace std {\n bool operator<(const Pt &a, const Pt &b){\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nistream &operator>> (istream &is, Pt &p){//ok\n Real a, b;\n is >> a >> b;\n p = Pt(a, b);\n return is;\n}\n\nReal dot(const Pt &a, const Pt &b) {//ok\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\nReal cross(const Pt &a, const Pt &b){//ok\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nReal arg(const Pt &a){\n return atan2(imag(a), real(a));\n}\n\nint ccw(const Pt &a, const Pt &b, const Pt &c){//ok\n if (cross(b - a, c - a) > EPS) return 1; //CCW\n if (cross(b - a, c - a) < -EPS) return -1; //CW\n if (dot(a - b, c - b) < -EPS) return 2; //a-b-c\n if (dot(b - a, c - a) < -EPS) return -2; //c-a-b\n return 0; //a-c-b(on seg)\n}\n\nstruct Line{//ok\n Pt a, b;\n Line(Pt a, Pt b) : a(a), b(b){};\n};\n\nstruct Segment : Line{\n Segment(Pt a, Pt b) : Line(a, b){};\n};\n\nstruct Circle{//ok\n Pt a;\n Real r;\n Circle(Pt a, Real r) : a(a), r(r){};\n};\n\nusing Polygon = vector<Pt>;\n\nPt projection(const Line &a, const Pt &p){//ok\n Real t = dot(p - a.a, a.a - a.b) / norm(a.a - a.b);\n return a.a + (a.a - a.b) * t;\n}\n\nPt projection(const Segment &a, const Pt &p){//ok\n Real t = dot(p - a.a, a.a - a.b) / norm(a.a - a.b);\n return a.a + (a.a - a.b) * t;\n}\n\nPt reflection(const Line &a, const Pt &b){//ok\n return b + (projection(a, b) - b) * 2.0;\n}\n\nPt reflection(const Segment &a, const Pt &b){//ok\n return b + (projection(a, b) - b) * 2.0;\n}\n\n//平行\nbool parallel(const Line &a, const Line &b){\n return !sgn(cross(a.b - a.a, b.b - b.a));\n}\n\n//直角\nbool orthogonal(const Line &a, const Line &b){\n return !sgn(dot(a.a - a.b, b.a - b.b));\n}\n\nbool intersect(const Line &a, const Pt &b){\n return abs(ccw(a.a, a.b, b)) != 1;\n}\n\nbool intersect(const Line &a, const Line &b){\n return !parallel(a, b);\n}\n\nbool intersect(const Line &a, const Segment &b){\n return cross(a.b - a.a, b.a - a.a) * cross(a.b - a.a, b.b - a.a) < EPS;\n}\n\nbool intersect(const Segment &a, const Pt &b){//ok\n return ccw(a.a, a.b, b) == 0;\n}\n\nbool intersect(const Segment &a, const Segment &b){//ok\n return (sgn(ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b)) <= 0 and\n sgn(ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b)) <= 0);\n}\n\nReal distance(const Line &a, const Pt &b);\n\nbool intersect(const Circle &a, const Pt &b){\n return sgn(abs(a.a - b) - a.r) == 0;\n}\n\nbool intersect(const Circle &a, const Line &b){\n return sgn(distance(b, a.a) - a.r) == -1;\n}\n\nint intersect(const Circle &a, const Segment &b){\n if (norm(projection(b, a.a) - a.a) - a.r * a.r > EPS) return 0;\n auto d1 = abs(a.a - b.a), d2 = abs(a.a - b.b);\n if (d1 < a.r + EPS and d2 < a.r + EPS) return 0;\n if (d1 < a.r - EPS and d2 > a.r + EPS or d1 > a.r + EPS and d2 < a.r - EPS) return 1;\n const Pt j = projection(b, a.a);\n if (dot(b.a - j, b.b - j) < 0) return 2;\n return 0;\n}\n\nint intersect(Circle a, Circle b){ //共通接線の数 ok\n if (a.r < b.r) swap(a, b);\n Real d = abs(a.a - b.a);\n if (sgn(a.r + b.r - d) == -1) return 4;\n if (sgn(a.r + b.r - d) == 0) return 3;\n if (sgn(a.r - b.r - d) == -1) return 2;\n if (sgn(a.r - b.r - d) == 0) return 1;\n return 0;\n}\n\nReal distance(const Pt &a, const Pt &b){\n return abs(a - b);\n}\n\nReal distance(const Line &a, const Pt &b){\n return abs(b - projection(a, b));\n}\n\nReal distance(const Line &a, const Line &b){\n return intersect(a, b) ? 0 : distance(a, b.a);\n}\n\nReal distance(const Line &a, const Segment &b){\n return intersect(a, b) ? 0 : min(distance(a, b.a), distance(a, b.b));\n}\n\nReal distance(const Segment &a, const Pt &b){//ok\n Pt p = projection(a, b);\n if (intersect(a, p)) return abs(p - b);\n return min(abs(a.a - b), abs(a.b - b));\n}\n\nReal distance(const Segment &a, const Segment &b){//ok\n if (intersect(a, b)) return 0;\n return min({distance(a, b.a), distance(a, b.b),\n distance(b, a.a), distance(b, a.b)});\n}\n\nPt crosspoint(const Line &a, const Line &b){//ok\n return a.a + (a.b - a.a) * cross(b.a - a.a, b.b - b.a) / cross(a.b - a.a, b.b - b.a);\n}\n\nPt crosspoint(const Segment &a, const Segment &b){//ok\n return crosspoint(Line(a), Line(b));\n}\n\npair<Pt, Pt> crosspoint(const Circle &a, const Line &b){//ok\n Pt pt = projection(b, a.a);\n Pt e = (b.b - b.a) / abs(b.b - b.a);\n if (!sgn(distance(b, a.a) - a.r)) return {pt, pt};\n double base = sqrt(a.r * a.r - norm(pt - a.a));\n return {pt - e * base, pt + e * base};\n}\n\npair<Pt, Pt> crosspoint(const Circle &a, const Segment &b){\n Line c = Line(b);\n if (intersect(a, b) == 2) return crosspoint(a, c);\n auto res = crosspoint(a, c);\n if (dot(b.a - res.first, b.b - res.first) < 0) res.second = res.first;\n return res;\n}\n\nbool is_convex(const Polygon &p){//ok\n ll sz = p.size();\n REP(i, sz){\n if (ccw(p[(i - 1 + sz) % sz], p[i], p[(i + 1) % sz]) == -1) return false;\n }\n return true;\n}\n\nPolygon convex_hull(Polygon &p){//ok\n int n = p.size(), k = 0;\n sort(ALL(p));\n vector<Pt> ch(2 * n);\n for (int i = 0; i < n; ch[k++] = p[i++])\n while (k >= 2 and ccw(ch[k - 2], ch[k - 1], p[i]) <= 0) k--;\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while (k >= t and ccw(ch[k - 2], ch[k - 1], p[i]) <= 0) k--;\n ch.resize(k - 1);\n return ch;\n}\n\n\nmap<Pt, ll> mp;\nvoid solve(ll n){\n vector<P> s(n);\n Polygon ps(n);\n REP(i, n){\n ll x, y;\n cin >> x >> y;\n ps[i] = Pt(x, y);\n mp[ps[i]] = i;\n }\n Polygon ch = convex_hull(ps);\n ll m = ch.size();\n bool ok = true;\n \n REP(i, m) {\n Pt a = ch[(i - 1 + m) % m];\n Pt b = ch[i];\n Pt c = ch[(i + 1) % m];\n if(abs(ccw(a, b, c)) == 1){\n ll d1 = abs(mp[b] - mp[a]), d2 = abs(mp[c] - mp[b]);\n if(min(d1, n - d1) > 1 or min(d2, n - d2) > 1) ok = false;\n }\n }\n cout << (ok ? \"Yes\" : \"No\") << endl;\n}\n\nint main(){\n while(1){\n ll n;\n cin >> n;\n if(n == 0)return 0;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 8092, "score_of_the_acc": -1.0093, "final_rank": 15 }, { "submission_id": "aoj_1669_10670212", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\nusing Point = complex<long double>;\nusing Polygon = vector<Point>;\n\nstruct Line {\n Point a, b;\n Line(Point a, Point b) : a(a), b(b) {}\n};\n\nstruct Segment : Line {\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n long double r;\n Circle(Point p, long double r) : p(p), r(r) {}\n};\n\nconst long double pi = acos(-1.0);\nconst long double EPS = 1e-12;\n\n// 内積\nlong double dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n\n// 外積\nlong double cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\n// 直線 l に対する点 p の射影\nPoint projection(const Line l, const Point &p) {\n long double t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return Point(l.a.real() + t * (l.b - l.a).real(), l.a.imag() + t * (l.b - l.a).imag());\n}\n\n// 直線 l に対する点 p の反射\nPoint reflection(const Line l, const Point &p) {\n long double t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n Point q(l.a.real() + t * (l.b - l.a).real(), l.a.imag() + t * (l.b - l.a).imag());\n return Point(2 * q.real() - p.real(), 2 * q.imag() - p.imag());\n}\n\n// p0 から p1 へ結んだベクトルから見た p2 の位置\nint counter_clockwise(const Point &p2, Point p0, Point p1) {\n // 反時計回り\n if (cross(p1 - p0, p2 - p0) > EPS) {\n return 1;\n }\n // 時計回り\n if (cross(p1 - p0, p2 - p0) < -EPS) {\n return -1;\n }\n // p2, p0, p1 の順で同一直線上\n if (dot(p1 - p0, p2 - p0) < -EPS) {\n return 2;\n }\n // p0, p1, p2 の順で同一直線上\n if (dot(p1 - p0, p2 - p0) > norm(p1 - p0) + EPS) {\n return -2;\n }\n // p2 は p0 と p1 を結ぶ線分上\n return 0;\n}\n\n// 平行判定\nbool is_parallel(const Line &l1, const Line &l2) {\n return (cross(l1.b - l1.a, l2.b - l2.a) == 0);\n}\n\n// 垂直判定\nbool is_orthogonal(const Line &l1, const Line &l2) {\n return (dot(l1.b - l1.a, l2.b - l2.a) == 0);\n}\n\n// 線分と線分の交差判定\nbool is_intersection(const Segment &s1, const Segment &s2) {\n return (counter_clockwise(s2.a, s1.a, s1.b) * counter_clockwise(s2.b, s1.a, s1.b) <= 0 && counter_clockwise(s1.a, s2.a, s2.b) * counter_clockwise(s1.b, s2.a, s2.b) <= 0);\n}\n\n// 線分と線分の交点の座標\nPoint cross_point(const Segment &s1, const Segment &s2) {\n long double d1 = cross(s1.a - s2.a, s1.b - s2.a);\n long double d2 = cross(s1.a - s1.b, s2.b - s2.a);\n if (abs(d1) < EPS && abs(d2) < EPS) {\n if (counter_clockwise(s1.a, s2.a, s2.b) == 0) return s1.a;\n else return s1.b;\n }\n return s2.a + (s2.b - s2.a) * (d1 / d2);\n}\n\n// 直線と線分の交差判定\nbool is_intersection(const Line &l1, const Segment &s2) {\n return (counter_clockwise(s2.a, l1.a, l1.b) * counter_clockwise(s2.b, l1.a, l1.b) <= 0);\n}\n\n// 直線と線分の交点の座標\nPoint cross_point(const Line &s1, const Segment &s2) {\n long double d1 = cross(s1.a - s2.a, s1.b - s2.a);\n long double d2 = cross(s1.a - s1.b, s2.b - s2.a);\n if (abs(d1) < EPS && abs(d2) < EPS) {\n return s2.a;\n }\n return s2.a + (s2.b - s2.a) * (d1 / d2);\n}\n\n// 直線と点の距離\nlong double distance(const Line s, const Point &p) {\n long double t = dot(p - s.a, s.b - s.a) / norm(s.b - s.a);\n Point proj = Point(s.a.real() + t * (s.b - s.a).real(), s.a.imag() + t * (s.b - s.a).imag());\n return abs(p - proj);\n}\n\n// 線分と点の距離\nlong double distance(const Segment s, const Point &p) {\n long double t = dot(p - s.a, s.b - s.a) / norm(s.b - s.a);\n if (t > -EPS && t < 1 + EPS) {\n Point proj = Point(s.a.real() + t * (s.b - s.a).real(), s.a.imag() + t * (s.b - s.a).imag());\n return abs(p - proj);\n } else {\n return min(abs(p - s.a), abs(p - s.b));\n }\n}\n\n// 多角形の面積\nlong double area(const Polygon &poly) {\n long double ans = 0;\n int N = poly.size();\n for (int i = 0; i < N; i++) {\n ans += cross(poly[i], poly[(i + 1) % N]);\n }\n ans *= 0.5;\n return ans;\n}\n\n// 凸性判定\nbool is_convex(const Polygon &poly) {\n int N = poly.size();\n for (int i = 0; i < N; i++) {\n if (counter_clockwise(poly[i], poly[(i + 1) % N], poly[(i + 2) % N]) == -1) return false;\n }\n return true;\n}\n\n// 多角形と点の包含関係\n// 0 : 含まれない\n// 1 : 辺の上にある\n// 2 : 内部に含まれる\nint is_contained(const Point p, const Polygon poly) {\n int N = poly.size();\n int cnt = 0;\n for (int i = 0; i < N; i++) {\n if (counter_clockwise(p, poly[i], poly[(i + 1) % N]) == 0) {\n return 1;\n }\n Point a = poly[i], b = poly[(i + 1) % N];\n if (a.imag() > b.imag()) swap(a, b);\n if (p.imag() < b.imag() + EPS && p.imag() > a.imag() + EPS && counter_clockwise(p, a, b) < 0) {\n cnt++;\n }\n }\n if (cnt % 2 == 0) return 0;\n else return 2;\n}\n\n// 凸包\nPolygon convex_hull(vector<Point> &ps) {\n int N = ps.size();\n auto compare = [](const Point &p1, const Point &p2) {\n if (p1.real() != p2.real()) return p1.real() < p2.real();\n return p1.imag() < p2.imag();\n };\n sort(ps.begin(), ps.end(), compare);\n int k = 0;\n Polygon qs(2 * N);\n // 下側凸包\n for (int i = 0; i < N; i++) {\n while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < EPS) k--;\n qs[k++] = ps[i];\n }\n // 上側凸包\n for (int i = N - 2, t = k; i >= 0; i--) {\n while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < EPS) k--;\n qs[k++] = ps[i];\n }\n qs.resize(k - 1);\n return qs;\n}\n\n// 最も遠い2点の距離を求める\nlong double farest_pair(vector<Point> &ps) {\n Polygon poly = convex_hull(ps);\n long double ans = 0;\n for (int i = 0; i < (int)poly.size(); i++) {\n for (int j = 0; j < i; j++) {\n ans = max(ans, abs(poly[i] - poly[j]));\n }\n }\n return ans;\n}\n\n// 直線 l で凸多角形を切断し、左側の凸多角形を出力する\nPolygon convex_cut(const Polygon &poly, const Line &l) {\n int N = poly.size();\n vector<Point> ps;\n for (int i = 0; i < N; i++) {\n if (cross(l.b - l.a, poly[i] - l.a) > -EPS) {\n ps.push_back(poly[i]);\n }\n if (is_intersection(l, Segment(poly[i], poly[(i + 1) % N]))) {\n ps.push_back(cross_point(l, Segment(poly[i], poly[(i + 1) % N])));\n }\n }\n if (ps.size() <= 0) return {};\n Polygon ch = convex_hull(ps);\n return ch;\n}\n\n// 最も近い2点の距離を求める\nlong double closest_pair(vector<Point> &ps) {\n auto dfs = [&](auto dfs, vector<Point> qs) -> long double {\n int N = qs.size();\n if (N <= 1) return 1e18;\n sort(qs.begin(), qs.end(), [](const Point &p1, const Point &p2) {\n if (abs(p1.real() - p2.real()) < EPS) return p1.imag() < p2.imag();\n return p1.real() < p2.real();\n });\n vector<Point> P1, P2;\n for (int i = 0; i < N / 2; i++) P1.push_back(qs[i]);\n for (int i = N / 2; i < N; i++) P2.push_back(qs[i]);\n long double d1 = dfs(dfs, P1), d2 = dfs(dfs, P2);\n long double d = min(d1, d2), ans = d;\n long double px = P1[N / 2 - 1].real(), py = P1[N / 2 - 1].imag();\n vector<pair<long double, int>> V;\n for (int i = 0; i < N; i++) {\n if (qs[i].real() < px - d - EPS || qs[i].real() > px + d + EPS) continue;\n V.push_back(make_pair(qs[i].imag(), i));\n }\n sort(V.begin(), V.end());\n int r = 0;\n for (int l = 0; l < (int)V.size(); l++) {\n r = max(r, l);\n while (r < (int)V.size() && V[r].first < V[l].first + d + EPS) {\n if (l != r) ans = min(ans, abs(qs[V[l].second] - qs[V[r].second]));\n r++;\n }\n }\n return ans;\n };\n return dfs(dfs, ps);\n}\n\n// 円の交差判定\nint intersection_of_circle(const Circle &c1, const Circle &c2) {\n long double d = abs(c1.p - c2.p);\n // 分離\n if (d > c1.r + c2.r + EPS) {\n return 4;\n }\n // 外接\n if (abs(d - c1.r - c2.r) < EPS) {\n return 3;\n }\n // 交差\n if (d < c1.r + c2.r - EPS && d > abs(c1.r - c2.r) + EPS) {\n return 2;\n }\n // 内接\n if (abs(d - abs(c1.r - c2.r)) < EPS) {\n return 1;\n }\n // 包含\n return 0;\n}\n\n// 内接円\nCircle incircle(const Point &p1, const Point &p2, const Point &p3) {\n long double d = abs((p1 - p2)) + abs(p2 - p3) + abs(p3 - p1);\n Point p = p1 + (abs(p3 - p1) / d) * (p2 - p1) + (abs(p2 - p1) / d) * (p3 - p1);\n long double r = abs(cross(p2 - p1, p3 - p1)) / d;\n return Circle(p, r);\n}\n\n// 外接円\nCircle circumscribed_circle(const Point &p1, const Point &p2, const Point &p3) {\n long double a = abs(p1 - p2), b = abs(p2 - p3), c = abs(p3 - p1), S = abs(cross(p2 - p1, p3 - p1)) * 0.5;\n long double cx = norm(p2 - p1) * (p3 - p1).imag() - norm(p3 - p1) * (p2 - p1).imag(), cy = norm(p3 - p1) * (p2 - p1).real() - norm(p2 - p1) * (p3 - p1).real();\n cx /= 2.0 * cross(p2 - p1, p3 - p1);\n cy /= 2.0 * cross(p2 - p1, p3 - p1);\n Point p = p1 + Point(cx, cy);\n long double r = a * b * c / (4.0 * S);\n return Circle(p, r);\n}\n\n// 円と直線の交点\nvector<Point> cross_points(Circle c, Line l) {\n Point proj = projection(l, c.p);\n long double d = abs(proj - c.p);\n if (d > c.r + EPS) {\n return {};\n } else if (abs(c.r - abs(proj - c.p)) < EPS) {\n return {proj};\n } else {\n long double s = sqrt(c.r * c.r - norm(proj - c.p));\n return {proj - (s / abs(l.b - l.a)) * (l.b - l.a), proj + (s / abs(l.b - l.a)) * (l.b - l.a)};\n }\n}\n\n// 円と円の交点\nvector<Point> cross_points(Circle c1, Circle c2) {\n int t = intersection_of_circle(c1, c2);\n if (t == 0 || t == 4) {\n return {};\n }\n if (t == 3) {\n return {c1.p + (c1.r / (c1.r + c2.r)) * (c2.p - c1.p)};\n }\n if (t == 1) {\n if (c1.r > c2.r) return {(-c2.r / abs(c1.r - c2.r)) * c1.p + (c1.r / abs(c1.r - c2.r)) * c2.p};\n return {(c2.r / abs(c1.r - c2.r)) * c1.p + (-c1.r / abs(c1.r - c2.r)) * c2.p};\n }\n long double d = abs(c1.p - c2.p);\n long double s = (c1.r * c1.r - c2.r * c2.r + d * d) / (2.0 * d);\n Point p = c1.p + (s / d) * (c2.p - c1.p);\n return {p - (sqrt(c1.r * c1.r - s * s) / d) * Point((c2.p - c1.p).imag(), -(c2.p - c1.p).real()), p + (sqrt(c1.r * c1.r - s * s) / d) * Point((c2.p - c1.p).imag(), -(c2.p - c1.p).real())};\n}\n\n// 円の接線\nvector<Point> tangent(Circle c, Point p) {\n return cross_points(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\n\n// 円の共通部分の面積\nlong double area_of_intersection(Circle c1, Circle c2) {\n int t = intersection_of_circle(c1, c2);\n if (t >= 3) {\n return 0;\n }\n if (t <= 1) {\n return pi * min(c1.r, c2.r) * min(c1.r, c2.r);\n }\n vector<Point> ps = cross_points(c1, c2);\n long double a1 = arg(ps[0] - c1.p) - arg(ps[1] - c1.p), a2 = arg(ps[1] - c2.p) - arg(ps[0] - c2.p);\n if (a1 < -EPS) a1 += 2.0 * pi;\n if (a2 < -EPS) a2 += 2.0 * pi;\n return (a1 / 2) * c1.r * c1.r + (a2 / 2) * c2.r * c2.r - abs(area({ps[0], c1.p, ps[1], c2.p}));\n}\n\nint solve(int N,vector<Point>P){\n rep(i,0,N-1)rep(j,i+1,N){\n vector<pair<long double,int>>vpx,vpy;\n set<int>se;\n vector<bool>flag(N);\n rep(k,0,N){\n int c=counter_clockwise(P[i],P[j],P[k]);\n if(abs(c)%2==0){\n vpx.push_back({P[k].real(),k});\n vpy.push_back({P[k].imag(),k});\n flag[k]=true;\n }else{\n se.insert(c);\n }\n }\n if((int)se.size()==2)continue;\n sort(all(vpx));\n sort(all(vpy));\n if(abs(vpx.front().first-vpx.back().first)<EPS)swap(vpx,vpy);\n int a=vpx.front().second,b=vpx.back().second;\n if((!flag[(a+1)%N]&&!flag[(a-1+N)%N])||(!flag[(b+1)%N]&&!flag[(b-1+N)%N])){\n return false;\n }\n }\n return true;\n}\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<Point>P(N);\n rep(i,0,N){\n int x,y;\n cin>>x>>y;\n Point p(x,y);\n P[i]=p;\n }\n bool ans=solve(N,P);\n if(ans)cout<<\"Yes\"<<endl;\n else cout<<\"No\"<<endl;\n }\n}", "accuracy": 1, "time_ms": 3220, "memory_kb": 3712, "score_of_the_acc": -1.0657, "final_rank": 16 }, { "submission_id": "aoj_1669_10610620", "code_snippet": "#include <bits/stdc++.h>\n#include <cstdint>\n#include <cstddef>\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n#include <cassert>\n\nnamespace zawa {\n\nnamespace geometryZ2 {\n\nusing Zahlen = i64;\n\nnamespace internal {\n\nconstexpr i32 positive{1};\nconstexpr i32 zero{0};\nconstexpr i32 negative{-1};\n\n} // namespace internal\n\nconstexpr i32 Sign(Zahlen value) {\n if (value < 0) return internal::negative;\n if (value > 0) return internal::positive;\n return internal::zero;\n}\n\nconstexpr bool Positive(Zahlen value) {\n return Sign(value) == internal::positive;\n}\n\nconstexpr bool Zero(Zahlen value) {\n return Sign(value) == internal::zero;\n}\n\nconstexpr bool Negative(Zahlen value) {\n return Sign(value) == internal::negative;\n}\n\nconstexpr Zahlen Abs(Zahlen value) {\n return (value > 0 ? value : -value);\n}\n\nconstexpr Zahlen Square(Zahlen value) {\n return value * value;\n}\n\n} // namespace geometryZ2\n\n} // namespace zawa\n\n#include <algorithm>\n#include <iostream>\n#include <limits>\n\nnamespace zawa {\n\nnamespace geometryZ2 {\n\nclass Point {\nprivate:\n Zahlen x_{}, y_{};\n static constexpr i32 origin{0};\n static constexpr i32 firstQuadrant{1};\n static constexpr i32 secondQuadrant{2};\n static constexpr i32 thirdQuadrant{-2};\n static constexpr i32 forthQuadrant{-1};\npublic:\n /* constructor */\n Point() = default;\n Point(const Point& p) : x_{p.x()}, y_{p.y()} {}\n Point(Zahlen x, Zahlen y) : x_{x}, y_{y} {}\n\n /* getter setter */\n Zahlen& x() {\n return x_;\n }\n const Zahlen& x() const {\n return x_;\n }\n Zahlen& y() {\n return y_;\n }\n const Zahlen& y() const {\n return y_;\n }\n\n /* operator */\n Point& operator=(const Point& p) {\n x() = p.x();\n y() = p.y();\n return *this;\n }\n Point& operator+=(const Point& p) {\n x() += p.x();\n y() += p.y();\n return *this;\n }\n friend Point operator+(const Point& p0, const Point& p1) {\n return Point{p0} += p1;\n }\n Point& operator-=(const Point& p) {\n x() -= p.x();\n y() -= p.y();\n return *this;\n }\n friend Point operator-(const Point& p0, const Point& p1) {\n return Point{p0} -= p1;\n }\n Point& operator*=(Zahlen k) {\n x() *= k;\n y() *= k;\n return *this;\n }\n friend Point operator*(const Point& p, Zahlen k) {\n return Point{p} *= k;\n }\n friend Point operator*(Zahlen k, const Point& p) {\n return Point{p} *= k;\n }\n Point& operator/=(Zahlen k) {\n assert(k);\n assert(x() % k == 0);\n assert(y() % k == 0);\n x() /= k;\n y() /= k;\n return *this;\n }\n friend Point operator/(const Point& p, Zahlen k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& p0, const Point& p1) {\n return p0.x() == p1.x() and p0.y() == p1.y();\n }\n friend bool operator!=(const Point& p0, const Point& p1) {\n return p0.x() != p1.x() or p0.y() != p1.y();\n }\n friend bool operator<(const Point& p0, const Point& p1) {\n if (p0.x() != p1.x()) return p0.x() < p1.x();\n else return p0.y() < p1.y();\n }\n friend bool operator<=(const Point& p0, const Point& p1) {\n return (p0 < p1) or (p0 == p1);\n }\n friend bool operator>(const Point& p0, const Point& p1) {\n if (p0.x() != p1.x()) return p0.x() > p1.x();\n else return p0.y() > p1.y();\n }\n friend bool operator>=(const Point& p0, const Point& p1) {\n return (p0 > p1) or (p0 == p1);\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x() >> p.y();\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x() << ',' << p.y() << ')';\n return os;\n }\n\n /* member function */\n Zahlen normSquare() const {\n return Square(x()) + Square(y());\n }\n bool isNormSquareOver(Zahlen d) const {\n assert(!Negative(d));\n auto [mn, mx]{std::minmax({ Abs(x()), Abs(y()) })};\n if (mx and mx > d / mx) {\n return true;\n }\n long long s1{Square(mn)}, s2{Square(mx)};\n if (s1 > d - s2) {\n return true;\n }\n return false;\n }\n bool isNormSquareOverflow() const {\n return isNormSquareOver(std::numeric_limits<Zahlen>::max());\n }\n\n i32 area() const {\n if (x_ == 0 and y_ == 0) return origin;\n if (x_ <= 0 and y_ < 0) return thirdQuadrant;\n if (x_ > 0 and y_ <= 0) return forthQuadrant;\n if (x_ >= 0 and y_ > 0) return firstQuadrant;\n return secondQuadrant;\n }\n\n /* static member */\n static bool ArgComp(const Point& p0, const Point& p1) {\n if (p0.area() != p1.area()) return p0.area() < p1.area();\n Zahlen cross{Cross(p0, p1)};\n return (!Zero(cross) ? Positive(cross) : p0.normSquare() < p1.normSquare());\n }\n\n /* friend function */\n friend Zahlen Dot(const Point& p0, const Point& p1) {\n return p0.x() * p1.x() + p0.y() * p1.y();\n }\n friend Zahlen Cross(const Point& p0, const Point& p1) {\n return p0.x() * p1.y() - p0.y() * p1.x();\n }\n};\nusing Vector = Point;\n\n} // namespace geometryZ2\n\n} // namespace zawa\n\n#include <vector>\n\nnamespace zawa {\n\nnamespace geometryZ2 {\n\nusing PointCloud = std::vector<Point>;\n\nvoid ArgSort(PointCloud& p) {\n std::sort(p.begin(), p.end(), Point::ArgComp);\n}\n\n} // namespace geometryZ2\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryZ2 {\n\nenum RELATION {\n // p0 -> p1 -> p2の順で直線上に並んでいる\n ONLINE_FRONT = -2,\n // (p1 - p0) -> (p2 - p0)が時計回りになっている\n CLOCKWISE = -1,\n // p0 -> p2 -> p1の順で直線上に並んでいる\n ON_SEGMENT = 0,\n // (p1 - p0) -> (p2 - p0)が反時計回りになっている\n COUNTER_CLOCKWISE = +1,\n // p2 -> p0 -> p1、またはp1 -> p0 -> p2の順で直線上に並んでいる\n ONLINE_BACK = +2\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (a.normSquare() < b.normSquare()) return ONLINE_FRONT;\n return ON_SEGMENT;\n};\n\n} // namespace geometryZ2\n\n} // namespace zawa\n\n#include <iterator>\n#include <type_traits>\n\nnamespace zawa {\n\nnamespace geometryZ2 {\n\nclass Polygon {\nprivate:\n std::vector<Point> data_;\npublic:\n usize size() const {\n return data_.size();\n }\n\n /* constructor */\n Polygon() = default;\n Polygon(const Polygon& polygon) : data_{polygon.data_} {}\n Polygon(const std::vector<Point>& data) : data_{data} {}\n Polygon(usize n) : data_{n} {\n assert(n >= static_cast<usize>(3));\n }\n\n /* operator */\n Polygon& operator=(const Polygon& polygon) {\n data_ = polygon.data_;\n return *this;\n }\n Point& operator[](usize i) {\n assert(i < size());\n return data_[i];\n }\n const Point& operator[](usize i) const {\n assert(i < size());\n return data_[i];\n }\n friend std::istream& operator>>(std::istream& is, Polygon& polygon) {\n for (size_t i{} ; i < polygon.size() ; i++) {\n is >> polygon[i];\n }\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Polygon& polygon) {\n for (usize i{} ; i < polygon.size() ; i++) {\n std::cout << polygon[i] << (i + 1 == polygon.size() ? \"\" : \" \");\n }\n return os;\n }\n\n /* member function */\n void reserve(usize n) {\n data_.reserve(n);\n }\n void pushBack(const Point& p) {\n data_.push_back(p);\n }\n void emplaceBack(Zahlen x, Zahlen y) {\n data_.emplace_back(x, y);\n }\n template <class RandomAccessIterator>\n void insert(usize n, RandomAccessIterator first, RandomAccessIterator last) {\n assert(n <= size());\n data_.insert(std::next(data_.begin(), n), first, last);\n }\n void orderRotate(usize i) {\n assert(i < size());\n std::rotate(data_.begin(), data_.begin() + i, data_.end());\n }\n template <class F>\n void normalForm(const F& func) {\n auto index{std::distance(data_.begin(), std::min_element(data_.begin(), data_.end(), func))};\n orderRotate(index);\n }\n void normalForm() {\n auto index{std::distance(data_.begin(), std::min_element(data_.begin(), data_.end()))};\n orderRotate(index);\n }\n template <class F>\n Polygon normalFormed(const F& func = [](const Point& a, const Point& b) -> bool { return a < b; }) const {\n Polygon res{*this};\n res.normalForm(func);\n return res;\n }\n Polygon normalFormed() {\n Polygon res{*this};\n res.normalForm();\n return res;\n }\n bool isConvex() const {\n assert(size() >= static_cast<usize>(3));\n for (usize i{} ; i < size() ; i++) {\n if (Relation(data_[i], data_[i+1==size()?0:i+1], data_[i+2>=size()?i+2-size():i+2])\n == CLOCKWISE) {\n return false;\n }\n }\n return true;\n }\n Zahlen areaTwice() const {\n assert(size() >= static_cast<usize>(3));\n Zahlen res{};\n for (usize i{1} ; i < size() ; i++) {\n res += Cross(data_[i] - data_[0], data_[i+1==size()?0:i+1] - data_[0]);\n }\n return res;\n }\n Polygon subtriangle(usize i, usize j, usize k) const {\n assert(i < size());\n assert(j < size());\n assert(k < size());\n return Polygon{std::vector<Point>{ data_[i], data_[j], data_[k] }};\n }\n};\n\n}\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryZ2 {\n\ntemplate <bool strictly>\nPolygon ConvexHull(const PointCloud& p) {\n PointCloud cp{p};\n std::sort(cp.begin(), cp.end());\n cp.erase(std::unique(cp.begin(), cp.end()), cp.end());\n if (cp.size() <= 2u) {\n return Polygon{cp};\n }\n PointCloud lower;\n lower.reserve(p.size());\n for (auto it{cp.begin()} ; it != cp.end() ; it++) {\n lower.push_back(*it);\n while (lower.size() >= 3u) {\n if (Relation(lower[lower.size() - 3], lower[lower.size() - 2], lower[lower.size() - 1]) == COUNTER_CLOCKWISE) {\n break;\n }\n if constexpr (!strictly) {\n if (Relation(lower[lower.size() - 3], lower[lower.size() - 2], lower[lower.size() - 1]) == ONLINE_FRONT) {\n break;\n }\n }\n std::swap(lower[lower.size() - 2], lower[lower.size() - 1]);\n lower.pop_back();\n }\n }\n PointCloud upper;\n upper.reserve(p.size());\n for (auto it{cp.rbegin()} ; it != cp.rend() ; it++) {\n upper.push_back(*it);\n while (upper.size() >= 3u) {\n if (Relation(upper[upper.size() - 3], upper[upper.size() - 2], upper[upper.size() - 1]) == COUNTER_CLOCKWISE) {\n break;\n }\n if constexpr (!strictly) {\n if (Relation(upper[upper.size() - 3], upper[upper.size() - 2], upper[upper.size() - 1]) == ONLINE_FRONT) {\n break;\n }\n }\n std::swap(upper[upper.size() - 2], upper[upper.size() - 1]);\n upper.pop_back();\n }\n }\n\n Polygon res;\n res.reserve(lower.size() + upper.size() - 2);\n res.insert(res.size(), lower.begin(), lower.end());\n res.insert(res.size(), std::next(upper.begin()), std::prev(upper.end()));\n return res;\n}\n\n} // namespace geometryZ2\n\n} // namespace zawa\nusing namespace zawa::geometryZ2;\nint N;\nbool solve() {\n PointCloud p(N);\n for (int i = 0 ; i < N ; i++) std::cin >> p[i];\n Polygon poly = p, ch = ConvexHull<true>(p);\n std::vector<bool> ok(ch.size());\n // ch[0] -> ch[1]の被覆\n for (int i = 0 ; i < std::ssize(ch) ; i++) {\n bool cur = false;\n for (int j = 0 ; j < N ; j++) if (ch[i] == p[j]) {\n for (int k : {-1, 1}) {\n cur |= ch[(i+1)%ch.size()] == p[(j+k+N)%N];\n cur |= Relation(ch[i], ch[(i+1)%ch.size()], p[(j+k+N)%N]) == ON_SEGMENT;\n }\n }\n ok[i] = cur;\n }\n // ch[1] -> ch[0]の被覆\n std::vector<bool> ko(ch.size());\n for (int i = 0 ; i < std::ssize(ch) ; i++) {\n bool cur = false;\n for (int j = 0 ; j < N ; j++) if (ch[i] == p[j]) {\n for (int k : {-1, 1}) {\n cur |= ch[(i-1+ch.size())%ch.size()] == p[(j+k+N)%N];\n cur |= Relation(ch[i], ch[(i-1+ch.size())%ch.size()], p[(j+k+N)%N]) == ON_SEGMENT;\n }\n }\n ko[i] = cur;\n }\n return (int)std::ranges::count(ok, true) + (int)std::ranges::count(ko, true) == 2 * std::ssize(ch);\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n std::cout << (solve() ? \"Yes\\n\" : \"No\\n\");\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3712, "score_of_the_acc": -0.0688, "final_rank": 13 }, { "submission_id": "aoj_1669_10569223", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\nnamespace Geometry {\n\nconstexpr double eps = 1e-10;\ntemplate <class T>\nconstexpr int sign(const T &a) {\n if (isZero(a)) return 0;\n if (a > 0) return 1;\n return -1;\n}\ntemplate <class T, class U>\nconstexpr bool equal(const T &a, const U &b) {\n return isZero(a - b);\n}\ntemplate <class T>\nconstexpr bool isZero(const T &a) {\n if (is_floating_point<T>()) return fabs(a) < eps;\n return a == 0;\n}\ntemplate <class T>\nconstexpr T square(const T &a) {\n return a * a;\n}\ntemplate <class T>\nstruct Vec2 {\n T x, y;\n Vec2() = default;\n Vec2(T x, T y) : x(x), y(y) {};\n constexpr Vec2 &operator+=(const Vec2 &P) {\n x += P.x, y += P.y;\n return *this;\n }\n constexpr Vec2 &operator-=(const Vec2 &P) {\n x -= P.x, y -= P.y;\n return *this;\n }\n constexpr Vec2 &operator*=(const T k) {\n x *= k, y *= k;\n return *this;\n }\n constexpr Vec2 &operator/=(const T k) {\n x /= k, y /= k;\n return *this;\n }\n constexpr Vec2 operator+() const { return *this; }\n constexpr Vec2 operator-() const { return {-x, -y}; }\n constexpr Vec2 operator+(const Vec2 &P) const { return {x + P.x, y + P.y}; }\n constexpr Vec2 operator-(const Vec2 &P) const { return {x - P.x, y - P.y}; }\n constexpr Vec2 operator*(const T k) const { return {x * k, y * k}; }\n constexpr Vec2 operator/(const T k) const { return {x / k, y / k}; }\n constexpr bool operator==(const Vec2 &P) const { return isZero(x - P.x) && isZero(y - P.y); }\n constexpr bool operator!=(const Vec2 &P) const { return !(*this == P); }\n constexpr bool operator<(const Vec2 &P) const {\n if (!isZero(x - P.x)) return x < P.x;\n return y < P.y;\n }\n constexpr bool operator>(const Vec2 &P) const { return P < *this; }\n constexpr bool isZeroVec() const { return x == T(0) && y == T(0); }\n constexpr T abs2() const { return x * x + y * y; }\n constexpr T abs() const { return sqrt(abs2()); }\n constexpr T dot(const Vec2 &v) const { return x * v.x + y * v.y; }\n constexpr T cross(const Vec2 &v) const { return x * v.y - y * v.x; }\n constexpr T dist(const Vec2 &P) const { return (P - (*this)).abs(); }\n constexpr T distSq(const Vec2 &P) const { return (P - (*this)).abs2(); }\n constexpr T unitVec() const { return (*this) / abs(); }\n Vec2 &unitize() { return *this /= abs(); }\n friend constexpr T abs2(const Vec2 &P) { return P.abs2(); }\n friend constexpr T abs(const Vec2 &P) { return P.abs(); }\n friend constexpr T dot(const Vec2 &P, const Vec2 &Q) { return P.dot(Q); }\n friend constexpr T dot(const Vec2 &A, const Vec2 &B, const Vec2 &C) { return (B - A).dot(C - A); }\n friend constexpr T cross(const Vec2 &P, const Vec2 &Q) { return P.cross(Q); }\n friend constexpr T cross(const Vec2 &A, const Vec2 &B, const Vec2 &C) { return (B - A).cross(C - A); }\n friend constexpr T dist(const Vec2 &P, const Vec2 &Q) { return P.dist(Q); }\n friend constexpr T distSq(const Vec2 &P, const Vec2 &Q) { return P.distSq(Q); }\n};\ntemplate <class T>\nconstexpr int ccw(const Vec2<T> &A, const Vec2<T> &B, const Vec2<T> &C) {\n if (cross(B - A, C - A) > eps) return +1;\n if (cross(B - A, C - A) < -eps) return -1;\n if (dot(B - A, C - A) < -eps) return +2;\n if (abs2(B - A) + eps < abs2(C - A)) return -2;\n return 0;\n}\nstruct Line {\n using T = double;\n using Point = Vec2<T>;\n Point A, B;\n Line() = default;\n Line(Point A, Point B) : A(A), B(B) {}\n constexpr Point vec() const { return B - A; }\n constexpr bool isParallelTo(const Line &L) const { return isZero(cross(vec(), L.vec())); }\n constexpr bool isOrthogonalTo(const Line &L) const { return isZero(dot(vec(), L.vec())); }\n constexpr T distanceFrom(const Point &P) const { return abs(cross(P - A, vec())) / vec().abs(); }\n constexpr Point crosspoint(const Line &L) const { return A + vec() * (cross(A - L.A, L.vec())) / cross(L.vec(), vec()); }\n friend constexpr Point crosspoint(const Line &L, const Line &M) { return L.crosspoint(M); }\n};\nstruct Segment : Line {\n Segment() = default;\n Segment(Point A, Point B) : Line(A, B) {}\n constexpr bool intersect(const Segment &L) const { return ccw(L.A, L.B, A) * ccw(L.A, L.B, B) <= 0 && ccw(A, B, L.A) * ccw(A, B, L.B) <= 0; }\n constexpr T distanceFrom(const Point &P) const {\n if (dot(P - A, vec()) < 0) return P.dist(A);\n if (dot(P - B, vec()) > 0) return P.dist(B);\n return Line::distanceFrom(P);\n }\n constexpr T distanceFrom(const Segment &L) const {\n if (intersect(L)) return 0;\n return min({Segment::distanceFrom(L.A), Segment::distanceFrom(L.B), Segment(L).distanceFrom(A), Segment(L).distanceFrom(B)});\n }\n};\nstruct intLine {\n using T = long long;\n using Point = Vec2<T>;\n Point A, B;\n intLine() = default;\n intLine(Point A, Point B) : A(A), B(B) {}\n constexpr Point vec() const { return B - A; }\n constexpr bool isParallelTo(const intLine &L) const { return isZero(cross(vec(), L.vec())); }\n constexpr bool isOrthogonalTo(const intLine &L) const { return isZero(dot(vec(), L.vec())); }\n constexpr double distanceSqFrom(const Point &P) const { return square(cross(P - A, vec())) / vec().abs2(); }\n // constexpr Point crosspoint(const intLine &L) const { return A + vec() * (cross(A - L.A, L.vec())) / cross(L.vec(), vec()); }\n};\nstruct intSegment : intLine {\n intSegment() = default;\n intSegment(Point A, Point B) : intLine(A, B) {}\n constexpr bool intersect(const intSegment &L) const { return ccw(L.A, L.B, A) * ccw(L.A, L.B, B) <= 0 && ccw(A, B, L.A) * ccw(A, B, L.B) <= 0; }\n constexpr T distanceSqFrom(const Point &P) {\n if (dot(P - A, vec()) < 0) return P.distSq(A);\n if (dot(P - B, vec()) > 0) return P.distSq(B);\n return intLine::distanceSqFrom(P);\n }\n constexpr T distanceSqFrom(const intSegment &L) {\n if (intersect(L)) return 0;\n return min(\n {intSegment::distanceSqFrom(L.A), intSegment::distanceSqFrom(L.B), intSegment(L).distanceSqFrom(A), intSegment(L).distanceSqFrom(B)});\n }\n friend constexpr bool intersect(const intSegment &L, const intSegment &M) { return L.intersect(M); }\n};\ntemplate <class T>\nvector<T> convex_hull(vector<T> ps) {\n sort(ps.begin(), ps.end());\n ps.erase(unique(ps.begin(), ps.end()), ps.end());\n int n = ps.size();\n if (n <= 2) return ps;\n vector<T> qs;\n for (auto &p : ps) {\n //<=0 if want to remove \"3 points on a same line\"\n while (qs.size() > 1 && cross(qs[qs.size() - 2], qs[qs.size() - 1], p) <= 0) {\n qs.pop_back();\n }\n qs.push_back(p);\n }\n int t = qs.size();\n for (int i = n - 2; i >= 0; i--) {\n T &p = ps[i];\n while ((int)qs.size() > t && cross(qs[qs.size() - 2], qs[qs.size() - 1], p) <= 0) {\n qs.pop_back();\n }\n if (i) qs.push_back(p);\n }\n return qs;\n}\n\ntemplate <typename T>\ninline istream &operator>>(istream &is, Vec2<T> &rhs) {\n return is >> rhs.x >> rhs.y;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, Vec2<T> &rhs) {\n return os << rhs.x << \" \" << rhs.y;\n}\ninline istream &operator>>(istream &is, Line &rhs) { return is >> rhs.A >> rhs.B; }\ninline ostream &operator<<(ostream &os, Line &rhs) { return os << rhs.A << rhs.B; }\ninline istream &operator>>(istream &is, Segment &rhs) { return is >> rhs.A >> rhs.B; }\ninline ostream &operator<<(ostream &os, Segment &rhs) { return os << rhs.A << rhs.B; }\ninline istream &operator>>(istream &is, intLine &rhs) { return is >> rhs.A >> rhs.B; }\ninline ostream &operator<<(ostream &os, intLine &rhs) { return os << rhs.A << rhs.B; }\ninline istream &operator>>(istream &is, intSegment &rhs) { return is >> rhs.A >> rhs.B; }\ninline ostream &operator<<(ostream &os, intSegment &rhs) { return os << rhs.A << rhs.B; }\n}; // namespace Geometry\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\nstruct HLD {\n vector<vector<int>> g;\n vector<int> sz, in, out, par, head, dep, ord;\n HLD(vector<vector<int>> &g_, int root = 0)\n : g(g_), sz((int)g_.size()), in((int)g_.size()), out((int)g_.size()), par((int)g_.size()), head((int)g_.size(), root), dep((int)g_.size()) {\n dfs_sz(root, -1);\n dfs_hld(root, -1);\n }\n void dfs_sz(int v, int p) {\n par[v] = p;\n sz[v] = 1;\n if (g[v].size() && g[v][0] == p) swap(g[v][0], g[v].back());\n for (auto &i : g[v]) {\n if (i != p) {\n dep[i] = dep[v] + 1;\n dfs_sz(i, v);\n sz[v] += sz[i];\n if (sz[g[v][0]] < sz[i]) swap(g[v][0], i);\n }\n }\n }\n void dfs_hld(int v, int p) {\n in[v] = ord.size();\n ord.push_back(v);\n for (auto i : g[v]) {\n if (i != p) {\n if (i == g[v][0]) {\n // Heavy\n head[i] = head[v];\n } else {\n // Light\n head[i] = i;\n }\n dfs_hld(i, v);\n }\n }\n out[v] = ord.size();\n }\n int lca(int u, int v) {\n while (1) {\n if (in[u] > in[v]) swap(u, v);\n if (head[u] == head[v]) return u;\n v = par[head[v]];\n }\n }\n int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }\n int la(int v, int d) {\n while (v != -1) {\n int u = head[v];\n if (in[v] - d >= in[u]) return ord[in[v] - d];\n d -= in[v] - in[u] + 1, v = par[u];\n }\n return -1;\n }\n int jump(int from, int to, int d) {\n int l = lca(from, to);\n if (d <= dep[from] - dep[l]) return la(from, d);\n d -= dep[from] - dep[l];\n if (d <= dep[to] - dep[l]) return la(to, dep[to] - dep[l] - d);\n return -1;\n }\n};\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nvoid solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n using namespace Geometry;\n vector<Vec2<ll>> p(n);\n cin >> p;\n auto cv = convex_hull(p);\n int K = cv.size();\n cv.push_back(cv[0]);\n p.push_back(p[0]);\n rep(i, K) {\n bool ok = false;\n bool ok2 = false;\n rep(j, n) {\n auto L = intLine(cv[i], cv[i + 1]);\n auto M = intLine(p[j], p[j + 1]);\n if (L.isParallelTo(M) && (L.A == M.A || L.A == M.B)) ok = true;\n if (L.isParallelTo(M) && (L.B == M.A || L.B == M.B)) ok2 = true;\n }\n if (!ok || !ok2) {\n cout << \"No\\n\";\n return;\n }\n }\n cout << \"Yes\\n\";\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3436, "score_of_the_acc": -0.0068, "final_rank": 2 }, { "submission_id": "aoj_1669_10482397", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\n#undef long\n#define long long long\n#define ll long\n#define vec vector\n#define rep(i, n) for (long i = 0; i < n; i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(a).size()\ntemplate <typename T>\nbool chmin(T &x, T y)\n{\n if (y < x)\n {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmax(T &x, T y)\n{\n if (x < y)\n {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<T> a)\n{\n const int n = a.size();\n rep(i, n)\n {\n os << a[i];\n if (i + 1 != n)\n os << \" \";\n }\n return os;\n}\nusing R = ld;\nconstexpr R EPS = 1e-10;\nusing point = complex<R>;\nusing points = vec<point>;\nusing polygon = vec<point>;\nR cross(const point &a, const point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nR dot(const point &a, const point &b) { return real(a) * real(b) + imag(a) * imag(b); }\nnamespace std\n{\n bool operator<(const point &a, const point &b)\n {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\nenum CCW\n{\n ONLINE_FRONT = -2, // a,b,c が一直線上\n CLOCKWISE = -1, // c が a,b から時計回り側\n ON_SEGMENT = 0, // a,c,b が一直線上\n COUNTER_CLOCKWISE = 1, // c が a,b から反時計回り側\n ONLINE_BACK = 2, // c,a,b が一直線上\n};\nint ccw(const point &a, point b, point c)\n{\n b -= a, c -= a;\n const R crs_b_c = cross(b, c);\n if (crs_b_c > EPS)\n return CCW::COUNTER_CLOCKWISE;\n if (crs_b_c < -EPS)\n return CCW::CLOCKWISE;\n if (dot(b, c) < -EPS)\n return CCW::ONLINE_BACK;\n if (norm(b) + EPS < norm(c))\n return CCW::ONLINE_FRONT;\n return CCW::ON_SEGMENT;\n}\npolygon convex_hull(polygon p, bool vertices_on_edge_remain = true)\n{\n int n = (int)p.size(), k = 0;\n if (n <= 2)\n return p;\n sort(all(p));\n points ch(2 * n);\n if (vertices_on_edge_remain)\n {\n for (int i = 0; i < n; ch[k++] = p[i++])\n while (k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) == -1)\n --k;\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while (k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) == -1)\n --k;\n }\n else\n {\n for (int i = 0; i < n; ch[k++] = p[i++])\n while (k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) != 1)\n --k;\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while (k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) != 1)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n}\nvoid solve()\n{\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n vec<point> a(n);\n rep(i, n)\n {\n R x, y;\n cin >> x >> y;\n a[i] = {x, y};\n }\n auto b = convex_hull(a, true);\n auto c = convex_hull(a, false);\n int m1 = sz(b), m2 = sz(c);\n vec<int> bi(m1), ci(m2);\n rep(i, m1) rep(k, n) if (b[i] == a[k]) bi[i] = k;\n rep(i, m2) rep(k, n) if (c[i] == a[k]) ci[i] = k;\n vec<bool> ce(n);\n for (int i : ci)\n ce[i] = true;\n rep(i, m1)\n {\n int idx1 = bi[(i + 1) % m1], idx2 = bi[i];\n if ((idx1 - idx2 + n) % n == 1)\n continue;\n if (ce[idx1] || ce[idx2])\n {\n cout << \"No\" << '\\n';\n return;\n }\n }\n cout << \"Yes\" << '\\n';\n return;\n}\nint main()\n{\n cin.tie(0)->sync_with_stdio(0), cout.tie(0);\n cout << fixed << setprecision(20);\n int t = 1;\n // cin >> t;\n // while (t--)\n while (true)\n solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3656, "score_of_the_acc": -0.06, "final_rank": 12 }, { "submission_id": "aoj_1669_9860523", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nusing T = long double;\nconst T eps = 1e-6;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, (T).0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, (T).0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * (T)2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), (T).0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), (T).0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return (T).0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, (T)0.) and eq(d2, (T)0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / (T)2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / (T)2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return (T).0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = (T).0;\n if (eq(f, (T).0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = (T).0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / (T)2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return (T).0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / (T)2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / (T)2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / (T)2.);\n res += b.r * b.r * (bth - sin(bth * 2) / (T)2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), (T).0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / (T)2., (a + b) / (T)2. + (b - a) * Point(0, 1));\n Line l2((b + c) / (T)2., (b + c) / (T)2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, (T)0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, (T)1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n Poly P(N);\n rep(i,0,N) {\n long double x, y;\n cin >> x >> y;\n P[i] = Point(x,y);\n }\n auto Q = P;\n Q = ConvexHull(Q);\n auto ison = [&](Point R) -> bool {\n rep(i,0,N) {\n if (ccw(P[i],P[(i+1)%N],R) == 0) return true;\n }\n return false;\n };\n int M = Q.size();\n bool check = true;\n rep(i,0,M) {\n Point R1 = Q[i], R2 = Q[(i+1)%M];\n Point V = unit(R2-R1) * 0.5L;\n if (!ison(R1+V)) check = false;\n if (!ison(R2-V)) check = false;\n }\n cout << (check ? \"Yes\" : \"No\") << endl;\n}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3532, "score_of_the_acc": -0.046, "final_rank": 11 }, { "submission_id": "aoj_1669_9438388", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\npi diff(pi a,pi b){return pi(a.F-b.F,a.S-b.S);}\nint main(){\n while(1){\n int N;cin>>N;\n if(!N)return 0;\n vector<pi>A(N);cin>>A;\n map<pi,ll>memo;\n REP(i,N)memo[A[i]]=i;\n stack<pi>st;\n auto B=A;\n sort(ALL(B));\n st.emplace(B[0]);\n st.emplace(B[1]);\n FOR(i,2,N){\n while(sz(st)>1){\n auto p=st.top();st.pop();\n auto q=st.top();\n auto a=diff(p,q);\n auto b=diff(B[i],q);\n if(b.F*a.S<a.F*b.S){\n st.emplace(p);break;\n }\n }\n st.emplace(B[i]);\n }\n vector<pi>C;\n while(sz(st)){\n C.emplace_back(st.top());\n st.pop();\n }\n st.emplace(B[N-1]);\n st.emplace(B[N-2]);\n RREP(i,N-2){\n while(sz(st)>1){\n auto p=st.top();st.pop();\n auto q=st.top();\n auto a=diff(p,q);\n auto b=diff(B[i],q);\n if(b.F*a.S<a.F*b.S){\n st.emplace(p);break;\n }\n }\n st.emplace(B[i]);\n }\n {\n vector<pi>_C;\n while(sz(st)){\n _C.emplace_back(st.top());st.pop();\n }\n reverse(ALL(C));\n reverse(ALL(_C));\n FOR(i,1,sz(_C))C.emplace_back(_C[i]);\n }\n C.emplace_back(C[1]);\n A.emplace_back(A[0]);\n A.emplace_back(A[1]);\n bool ok=1;\n REP(i,sz(C)-2){\n //C[i]->C[i+1]->C[i+2]がぽり\n auto j=memo[C[i+1]];\n j=(j+N-1)%N;\n //A[j]->A[j+1]->A[j+2]\n //cout<<C[i]<<\" -> \"<<C[i+1]<<\" -> \"<<C[i+2]<<endl;\n //cout<<A[j]<<\" -> \"<<A[j+1]<<\" -> \"<<A[j+2]<<endl;\n auto a=diff(C[i],C[i+1]),b=diff(A[j],A[j+1]);\n if(a.S*b.F!=a.F*b.S)ok=0;\n a=diff(C[i+2],C[i+1]),b=diff(A[j+2],A[j+1]);\n if(a.S*b.F!=a.F*b.S)ok=0;\n }\n if(ok)cout<<\"Yes\"<<endl;\n else cout<<\"No\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3440, "score_of_the_acc": -0.0139, "final_rank": 4 }, { "submission_id": "aoj_1669_9407652", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nusing Real = long double;\nconst Real EPS = Real(1e-8), PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << \" \" << p.imag();\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(p.real() * d, p.imag() * d);\n}\nPoint operator/(const Point& p, const Real& d) {\n return Point(p.real() / d, p.imag() / d);\n}\nPoint rot(const Point& p, const Real& theta) {\n return p * Point(cos(theta), sin(theta));\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nReal dist(const Point& p1, const Point& p2) {\n return abs(p1 - p2);\n}\nbool comp_x(const Point& p1, const Point& p2) {\n return eq(p1.real(), p2.real()) ? p1.imag() < p2.imag() : p1.real() < p2.real();\n}\nbool comp_y(const Point& p1, const Point& p2) {\n return eq(p1.imag(), p2.imag()) ? p1.real() < p2.real() : p1.imag() < p2.imag();\n}\nbool comp_arg(const Point& p1, const Point& p2) {\n return arg(p1) < arg(p2);\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nReal closest_pair(vector<Point> ps) {\n if((int)ps.size() <= 1) return Real(1e18);\n sort(ps.begin(), ps.end(), comp_x);\n vector<Point> memo(ps.size());\n auto func = [&](auto& func, int l, int r) -> Real {\n if(r - l <= 1) return Real(1e18);\n int m = (l + r) >> 1;\n Real x = ps[m].real();\n Real res = min(func(func, l, m), func(func, m, r));\n inplace_merge(ps.begin() + l, ps.begin() + m, ps.begin() + r, comp_y);\n int cnt = 0;\n for(int i = l; i < r; ++i) {\n if(abs(ps[i].real() - x) >= res) continue;\n for(int j = 0; j < cnt; ++j) {\n Point d = ps[i] - memo[cnt - j - 1];\n if(d.imag() >= res) break;\n res = min(res, abs(d));\n }\n memo[cnt++] = ps[i];\n }\n return res;\n };\n return func(func, 0, (int)ps.size());\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_orthogonal(const Line& a, const Line& b) {\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\nPoint projection(const Line& l, const Point& p) {\n Real t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\nPoint reflection(const Line& l, const Point& p) {\n return p + (projection(l, p) - p) * 2.0;\n}\nbool is_intersect_lp(const Line& l, const Point& p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\nbool is_intersect_sp(const Segment& s, const Point& p) {\n return ccw(s.a, s.b, p) == 0;\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nbool is_intersect_ls(const Line& l, const Segment& s) {\n return sign(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a)) <= 0;\n}\nbool is_intersect_sl(const Segment& s, const Line& l) {\n return is_intersect_ls(l, s);\n}\nbool is_intersect_ss(const Segment& s1, const Segment& s2) {\n if(ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) > 0) return false;\n return ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.emplace_back(l2.a);\n } else {\n res.emplace_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nvector<Point> intersection_ss(const Segment& s1, const Segment& s2) {\n return is_intersect_ss(s1, s2) ? intersection_ll(Line(s1), Line(s2)) : vector<Point>();\n}\nReal dist_lp(const Line& l, const Point& p) {\n return abs(p - projection(l, p));\n}\nReal dist_sp(const Segment& s, const Point& p) {\n const Point h = projection(s, p);\n if(is_intersect_sp(s, h)) return abs(h - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\nReal dist_ll(const Line& l1, const Line& l2) {\n if(is_intersect_ll(l1, l2)) return 0.0;\n return dist_lp(l1, l2.a);\n}\nReal dist_ss(const Segment& s1, const Segment& s2) {\n if(is_intersect_ss(s1, s2)) return 0.0;\n return min({dist_sp(s1, s2.a), dist_sp(s1, s2.b), dist_sp(s2, s1.a), dist_sp(s2, s1.b)});\n}\nReal dist_ls(const Line& l, const Segment& s) {\n if(is_intersect_ls(l, s)) return 0.0;\n return min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\nReal dist_sl(const Segment& s, const Line& l) {\n return dist_ls(l, s);\n}\nReal area(const vector<Point>& polygon) {\n Real res = 0.0;\n const int n = (int)polygon.size();\n for(int i = 0; i < n; ++i) {\n res += cross(polygon[i], polygon[(i + 1) % n]);\n }\n return abs(res * 0.5);\n}\nbool is_convex(const vector<Point>& polygon) {\n const int n = (int)polygon.size();\n for(int i = 0; i < n; ++i) {\n if(ccw(polygon[(i - 1 + n) % n], polygon[i], polygon[(i + 1) % n]) == -1) return false;\n }\n return true;\n}\nint in_polygon(const vector<Point>& polygon, const Point& p) {\n const int n = (int)polygon.size();\n int ret = 0;\n for(int i = 0; i < n; ++i) {\n Point a = polygon[i] - p, b = polygon[(i + 1) % n] - p;\n if(eq(cross(a, b), 0.0) and sign(dot(a, b)) <= 0) return 1;\n if(a.imag() > b.imag()) swap(a, b);\n if(sign(a.imag()) <= 0 and sign(b.imag()) == 1 and sign(cross(a, b)) == 1) ret ^= 2;\n }\n return ret;\n}\nvector<Point> convex_hull(vector<Point> ps) {\n sort(ps.begin(), ps.end(), comp_x);\n ps.erase(unique(ps.begin(), ps.end()), ps.end());\n int n = (int)ps.size(), k = 0;\n if(n == 1) return ps;\n vector<Point> ch(2 * n);\n for(int i = 0; i < n; ch[k++] = ps[i++]) {\n while(k >= 2 and sign(cross(ch[k - 1] - ch[k - 2], ps[i] - ch[k - 1])) == -1) {\n --k;\n }\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) {\n while(k >= t and sign(cross(ch[k - 1] - ch[k - 2], ps[i] - ch[k - 1])) == -1) {\n --k;\n }\n }\n ch.resize(k - 1);\n return ch;\n}\nReal convex_diameter(const vector<Point>& polygon) {\n int n = (int)polygon.size(), is = 0, js = 0;\n for(int i = 1; i < n; ++i) {\n if(sign(polygon[i].imag() - polygon[is].imag()) == 1) is = i;\n if(sign(polygon[i].imag() - polygon[js].imag()) == -1) js = i;\n }\n Real maxdis = norm(polygon[is] - polygon[js]);\n int i = is, j = js;\n do {\n if(sign(cross(polygon[(i + 1) % n] - polygon[i], polygon[(j + 1) % n] - polygon[j])) >= 0) {\n j = (j + 1) % n;\n } else {\n i = (i + 1) % n;\n }\n if(norm(polygon[i] - polygon[j]) > maxdis) {\n maxdis = norm(polygon[i] - polygon[j]);\n }\n } while(i != is or j != js);\n return sqrt(maxdis);\n}\nvector<Point> convex_cut(const vector<Point>& polygon, const Line& l) {\n const int n = (int)polygon.size();\n vector<Point> res;\n for(int i = 0; i < n; ++i) {\n const Point cur = polygon[i], nex = polygon[(i + 1) % n];\n if(ccw(l.a, l.b, cur) != -1) res.emplace_back(cur);\n if(ccw(l.a, l.b, cur) * ccw(l.a, l.b, nex) < 0) {\n res.emplace_back(intersection_ll(Line(cur, nex), l)[0]);\n }\n }\n return res;\n}\n\nint main(void) {\n while(1) {\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<Point> poly(n);\n rep(i, 0, n) {\n cin >> poly[i];\n }\n\n vector<Point> ch = convex_hull(poly);\n\n bool ok = true;\n rep(i, 0, n) {\n Point p = poly[i];\n if(in_polygon(ch, p) == 2) {\n int l1 = i; int r1 = i;\n while(in_polygon(ch, poly[l1]) != 1) {\n l1 = (l1-1 + n) % n;\n }\n while(in_polygon(ch, poly[r1]) != 1) {\n r1 = (r1+1) % n;\n }\n\n int l2 = (l1-1+n) % n; int r2 = (r1+1) % n;\n while(in_polygon(ch, poly[l2]) != 1) {\n l2 = (l2-1 + n) % n;\n }\n while(in_polygon(ch, poly[r2]) != 1) {\n r2 = (r2+1) % n;\n }\n if(!is_parallel(Line(poly[l1], poly[r1]), Line(poly[l1], poly[l2]))) ok = false;\n if(!is_parallel(Line(poly[l1], poly[r1]), Line(poly[r1], poly[r2]))) ok = false;\n }\n }\n\n if(ok) cout << \"Yes\\n\";\n else cout << \"No\\n\";\n }\n}", "accuracy": 1, "time_ms": 1370, "memory_kb": 3524, "score_of_the_acc": -0.4493, "final_rank": 14 }, { "submission_id": "aoj_1669_9407515", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,s,t) for(ll i=s;i<(ll)(t);i++)\n#define rrep(i,s,t) for(ll i=(ll)(t)-1;i>=(ll)s;i--)\n#define all(x) begin(x),end(x)\n#define rall(x) rbegin(x),rend(x)\n\n#define TT template<typename T>\nTT using vec=vector<T>;\nTT bool chmin(T &x,T y){return x>y?(x=y,true):false;}\nTT bool chmax(T &x,T y){return x<y?(x=y,true):false;}\n\nstruct io_setup{\n io_setup(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(15);\n }\n} io_setup;\n\nusing Point = complex<ll>;\nusing Polygon = vector<Point>;\n\ndouble dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n\ndouble cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nint counter_clockwise(const Point &p2, Point p0, Point p1) {\n // 反時計回り\n if (cross(p1 - p0, p2 - p0) > 0) {\n return 1;\n }\n // 時計回り\n if (cross(p1 - p0, p2 - p0) < 0) {\n return -1;\n }\n // p2, p0, p1 の順で同一直線上\n if (dot(p1 - p0, p2 - p0) < 0) {\n return 2;\n }\n // p0, p1, p2 の順で同一直線上\n if (dot(p1 - p0, p2 - p0) > norm(p1 - p0)) {\n return -2;\n }\n // p2 は p0 と p1 を結ぶ線分上\n return 0;\n}\n\nPolygon convex_hull(vector<Point> &ps) {\n int N = ps.size();\n auto compare = [](const Point &p1, const Point &p2) {\n if (p1.real() != p2.real()) return p1.real() < p2.real();\n return p1.imag() < p2.imag();\n };\n sort(ps.begin(), ps.end(), compare);\n int k = 0;\n Polygon qs(2 * N);\n // 下側凸包\n for (int i = 0; i < N; i++) {\n while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0) k--;\n qs[k++] = ps[i];\n }\n // 上側凸包\n for (int i = N - 2, t = k; i >= 0; i--) {\n while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0) k--;\n qs[k++] = ps[i];\n }\n qs.resize(k - 1);\n return qs;\n}\n\nint solve(){\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<ll>X(N),Y(N);\n for(int i=0;i<N;i++)cin>>X[i]>>Y[i];\n vector<Point>vp(N);\n for(int i=0;i<N;i++)vp[i]=Point(X[i],Y[i]);\n Polygon pl=convex_hull(vp);\n int L=pl.size();\n for(int i=0;i<L;i++){\n bool ok=true;\n for(int j=0;j<N;j++){\n if(abs(counter_clockwise(Point(X[j],Y[j]),pl[i],pl[(i+1)%L]))==1\n &&counter_clockwise(Point(X[(j+1)%N],Y[(j+1)%N]),pl[i],pl[(i+1)%L])==0\n &&abs(counter_clockwise(Point(X[(j+2)%N],Y[(j+2)%N]),pl[i],pl[(i+1)%L]))==1){\n ok=false;\n break;\n }\n }\n if(!ok){\n cout<<\"No\"<<endl;\n return 1;\n }\n }\n cout<<\"Yes\"<<endl;\n return 1;\n}\n\nint main(){\n while(solve());\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3460, "score_of_the_acc": -0.0151, "final_rank": 5 }, { "submission_id": "aoj_1669_9403114", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\n#include<complex>\nconst int INF = 1000000000;\nconst ll LINF = 1001002003004005006ll;\nint dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 };\n\nstruct IOSetup {\n IOSetup() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n }\n} iosetup;\n\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n return os;\n}\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v)is >> x;\n return is;\n}\n\n// line 1 \"Geometry/template.cpp\"\n// Real\nusing Real = double;\nconst Real EPS = 1e-6;\nconst Real pi = acosl(-1);\n\n// Point\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, Point& p) {\n return os << fixed << setprecision(12) << p.real() << ' ' << p.imag();\n}\ninline bool eq(Real a, Real b) {\n return fabs(a - b) < EPS;\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\n// Line\nstruct Line {\n Point p1, p2;\n Line() = default;\n Line(Point p1, Point p2) :p1(p1), p2(p2) {}\n //Ax + By = C\n Line(Real A, Real B, Real C) {\n if (eq(A, 0)) p1 = Point(0, C / B), p2 = Point(1, C / B);\n else if (eq(B, 0))p1 = Point(C / A, 0), p2 = Point(C / A, 1);\n else p1 = Point(0, C / B), p2 = Point(C / A, 0);\n }\n};\n\n// Segment\nstruct Segment :Line {\n Segment() = default;\n Segment(Point p1, Point p2) :Line(p1, p2) {}\n};\nReal dot(Point a, Point b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\nReal cross(Point a, Point b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return 1;//COUNTER CLOCKWISE\n else if (cross(b, c) < -EPS) return -1;//CLOCKWISE\n else if (dot(b, c) < 0) return 2;//c--a--b ONLINE BACK\n else if (norm(b) < norm(c)) return -2;//a--b--c ONLINE FRONT\n else return 0;//a--c--b ON SEGMENT\n}\nPoint projection(Segment l, Point p) {\n Real k = dot(l.p1 - l.p2, p - l.p1) / norm(l.p1 - l.p2);\n return l.p1 + (l.p1 - l.p2) * k;\n}\nbool intersect(Segment s, Segment t) {\n return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) < 0 && ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <0;\n}\nbool intersect(Segment s, Point p) {\n return ccw(s.p1, s.p2, p) == 0;\n}\nReal dis(Point a, Point b) {\n return abs(a - b);\n}\nReal dis(Segment s, Point p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\nint contains(vector<Point> P, Point p) {\n int res = 0;\n int n = (int)P.size();\n for (int i = 0; i < n; i++) {\n Point a = P[i] - p, b = P[(i + 1) % n] - p;\n if (imag(a) > imag(b)) swap(a, b);\n if (imag(a) <= 0 && 0 < imag(b) && cross(a, b) < 0) res ^= 1;\n if (eq(cross(a, b), 0) && (dot(a, b) < 0 || eq(dot(a, b), 0))) return 1;\n }\n if (res) res = 2;\n return res;\n}\n\nvector<Point> convex_hull(vector<Point> p) {\n int n = (int)p.size(), k = 0;\n // if (n <= 2)return p;\n sort(begin(p), end(p), [](Point a, Point b) {\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n });\n vector<Point>ch(2 * n);\n for (int i = 0; i < n; ch[k++] = p[i++]) {\n while(k>=2 and cross(ch[k-1]-ch[k-2],p[i]-ch[k-1])<EPS)k--;\n // while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while(k>=t and cross(ch[k-1]-ch[k-2],p[i]-ch[k-1])<EPS)k--;\n // while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)k--;\n }\n ch.resize(k - 1);\n return ch;\n}\n\n\nvoid solve(ll N) {\n\n vector<Point> P(N);\n rep(i, N)cin >> P[i];\n auto C = convex_hull(P);\n ll CN = C.size();\n // cout<<CN<<endl;\n bool OK = 1;\n rep(i, CN) {\n Point P1 = C[i];\n Point P2 = C[(i + 1) % CN];\n double d = dis(P1, P2);\n Point R = P1 + 0.5 / d * (P2 - P1);\n if (contains(P, R) == 0)OK = 0;\n R = P2 + 0.5 / d * (P1 - P2);\n if (contains(P, R) == 0)OK = 0;\n }\n cout << (OK ? \"Yes\" : \"No\") << endl;\n}\n\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N;\n while (cin >> N, N != 0)solve(N);\n\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3480, "score_of_the_acc": -0.0256, "final_rank": 8 }, { "submission_id": "aoj_1669_9401483", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nusing Real = long double;\nconst Real EPS = Real(1e-8), PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << \" \" << p.imag();\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(p.real() * d, p.imag() * d);\n}\nPoint operator/(const Point& p, const Real& d) {\n return Point(p.real() / d, p.imag() / d);\n}\nPoint rot(const Point& p, const Real& theta) {\n return p * Point(cos(theta), sin(theta));\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nReal dist(const Point& p1, const Point& p2) {\n return abs(p1 - p2);\n}\nbool comp_x(const Point& p1, const Point& p2) {\n return eq(p1.real(), p2.real()) ? p1.imag() < p2.imag() : p1.real() < p2.real();\n}\nbool comp_y(const Point& p1, const Point& p2) {\n return eq(p1.imag(), p2.imag()) ? p1.real() < p2.real() : p1.imag() < p2.imag();\n}\nbool comp_arg(const Point& p1, const Point& p2) {\n return arg(p1) < arg(p2);\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nReal closest_pair(vector<Point> ps) {\n if((int)ps.size() <= 1) return Real(1e18);\n sort(ps.begin(), ps.end(), comp_x);\n vector<Point> memo(ps.size());\n auto func = [&](auto& func, int l, int r) -> Real {\n if(r - l <= 1) return Real(1e18);\n int m = (l + r) >> 1;\n Real x = ps[m].real();\n Real res = min(func(func, l, m), func(func, m, r));\n inplace_merge(ps.begin() + l, ps.begin() + m, ps.begin() + r, comp_y);\n int cnt = 0;\n for(int i = l; i < r; ++i) {\n if(abs(ps[i].real() - x) >= res) continue;\n for(int j = 0; j < cnt; ++j) {\n Point d = ps[i] - memo[cnt - j - 1];\n if(d.imag() >= res) break;\n res = min(res, abs(d));\n }\n memo[cnt++] = ps[i];\n }\n return res;\n };\n return func(func, 0, (int)ps.size());\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_orthogonal(const Line& a, const Line& b) {\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\nPoint projection(const Line& l, const Point& p) {\n Real t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\nPoint reflection(const Line& l, const Point& p) {\n return p + (projection(l, p) - p) * 2.0;\n}\nbool is_intersect_lp(const Line& l, const Point& p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\nbool is_intersect_sp(const Segment& s, const Point& p) {\n return ccw(s.a, s.b, p) == 0;\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nbool is_intersect_ls(const Line& l, const Segment& s) {\n return sign(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a)) <= 0;\n}\nbool is_intersect_sl(const Segment& s, const Line& l) {\n return is_intersect_ls(l, s);\n}\nbool is_intersect_ss(const Segment& s1, const Segment& s2) {\n if(ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) > 0) return false;\n return ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.emplace_back(l2.a);\n } else {\n res.emplace_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nvector<Point> intersection_ss(const Segment& s1, const Segment& s2) {\n return is_intersect_ss(s1, s2) ? intersection_ll(Line(s1), Line(s2)) : vector<Point>();\n}\nReal dist_lp(const Line& l, const Point& p) {\n return abs(p - projection(l, p));\n}\nReal dist_sp(const Segment& s, const Point& p) {\n const Point h = projection(s, p);\n if(is_intersect_sp(s, h)) return abs(h - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\nReal dist_ll(const Line& l1, const Line& l2) {\n if(is_intersect_ll(l1, l2)) return 0.0;\n return dist_lp(l1, l2.a);\n}\nReal dist_ss(const Segment& s1, const Segment& s2) {\n if(is_intersect_ss(s1, s2)) return 0.0;\n return min({dist_sp(s1, s2.a), dist_sp(s1, s2.b), dist_sp(s2, s1.a), dist_sp(s2, s1.b)});\n}\nReal dist_ls(const Line& l, const Segment& s) {\n if(is_intersect_ls(l, s)) return 0.0;\n return min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\nReal dist_sl(const Segment& s, const Line& l) {\n return dist_ls(l, s);\n}\nReal area(const vector<Point>& polygon) {\n Real res = 0.0;\n const int n = (int)polygon.size();\n for(int i = 0; i < n; ++i) {\n res += cross(polygon[i], polygon[(i + 1) % n]);\n }\n return abs(res * 0.5);\n}\nbool is_convex(const vector<Point>& polygon) {\n const int n = (int)polygon.size();\n for(int i = 0; i < n; ++i) {\n if(ccw(polygon[(i - 1 + n) % n], polygon[i], polygon[(i + 1) % n]) == -1) return false;\n }\n return true;\n}\nint in_polygon(const vector<Point>& polygon, const Point& p) {\n const int n = (int)polygon.size();\n int ret = 0;\n for(int i = 0; i < n; ++i) {\n Point a = polygon[i] - p, b = polygon[(i + 1) % n] - p;\n if(eq(cross(a, b), 0.0) and sign(dot(a, b)) <= 0) return 1;\n if(a.imag() > b.imag()) swap(a, b);\n if(sign(a.imag()) <= 0 and sign(b.imag()) == 1 and sign(cross(a, b)) == 1) ret ^= 2;\n }\n return ret;\n}\nvector<Point> convex_hull(vector<Point> ps) {\n sort(ps.begin(), ps.end(), comp_x);\n ps.erase(unique(ps.begin(), ps.end()), ps.end());\n int n = (int)ps.size(), k = 0;\n if(n == 1) return ps;\n vector<Point> ch(2 * n);\n for(int i = 0; i < n; ch[k++] = ps[i++]) {\n while(k >= 2 and sign(cross(ch[k - 1] - ch[k - 2], ps[i] - ch[k - 1])) == -1) {\n --k;\n }\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) {\n while(k >= t and sign(cross(ch[k - 1] - ch[k - 2], ps[i] - ch[k - 1])) == -1) {\n --k;\n }\n }\n ch.resize(k - 1);\n return ch;\n}\nReal convex_diameter(const vector<Point>& polygon) {\n int n = (int)polygon.size(), is = 0, js = 0;\n for(int i = 1; i < n; ++i) {\n if(sign(polygon[i].imag() - polygon[is].imag()) == 1) is = i;\n if(sign(polygon[i].imag() - polygon[js].imag()) == -1) js = i;\n }\n Real maxdis = norm(polygon[is] - polygon[js]);\n int i = is, j = js;\n do {\n if(sign(cross(polygon[(i + 1) % n] - polygon[i], polygon[(j + 1) % n] - polygon[j])) >= 0) {\n j = (j + 1) % n;\n } else {\n i = (i + 1) % n;\n }\n if(norm(polygon[i] - polygon[j]) > maxdis) {\n maxdis = norm(polygon[i] - polygon[j]);\n }\n } while(i != is or j != js);\n return sqrt(maxdis);\n}\nvector<Point> convex_cut(const vector<Point>& polygon, const Line& l) {\n const int n = (int)polygon.size();\n vector<Point> res;\n for(int i = 0; i < n; ++i) {\n const Point cur = polygon[i], nex = polygon[(i + 1) % n];\n if(ccw(l.a, l.b, cur) != -1) res.emplace_back(cur);\n if(ccw(l.a, l.b, cur) * ccw(l.a, l.b, nex) < 0) {\n res.emplace_back(intersection_ll(Line(cur, nex), l)[0]);\n }\n }\n return res;\n}\nint main(void) {\n while(1) {\n int n;\n cin >> n;\n if(n == 0) break;\n vector<Point> poly(n);\n rep(i, 0, n) {\n cin >> poly[i];\n }\n vector<Point> ch = convex_hull(poly);\n vector<bool> is_in(n);\n rep(i, 0, n) {\n if(in_polygon(ch, poly[i]) == 2) {\n is_in[i] = true;\n }\n }\n bool ans = true;\n rep(i, 0, n) {\n if(is_in[i]) {\n int i1 = -1, i2 = -1;\n rrep(j, i - 1, -n) {\n if(!is_in[(j + 2 * n) % n] and !is_in[(j - 1 + 2 * n) % n]) {\n i1 = (j + 2 * n) % n;\n i2 = (j - 1 + 2 * n) % n;\n break;\n }\n }\n if(i1 == -1 and i2 == -1) {\n ans = false;\n break;\n }\n int j1 = -1, j2 = -1;\n rep(j, i + 1, 2 * n) {\n if(!is_in[j % n] and !is_in[(j + 1) % n]) {\n j1 = j % n;\n j2 = (j + 1) % n;\n break;\n }\n }\n if(j1 == -1 and j2 == -1) {\n ans = false;\n break;\n }\n if(i1 == j2) {\n ans = false;\n break;\n }\n Line l1 = Line(poly[i1], poly[i2]), l2 = Line(poly[j1], poly[j2]);\n if(is_parallel(l1, l2) and eq(dist_ll(l1, l2), 0.0)) {\n } else {\n ans = false;\n }\n if(j1 < i) break;\n i = j1;\n }\n }\n if(ans) {\n cout << \"Yes\" << '\\n';\n } else {\n cout << \"No\" << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3492, "score_of_the_acc": -0.0375, "final_rank": 9 }, { "submission_id": "aoj_1669_9344031", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Real = long double;\nusing Point = complex<Real>;\nconst Real EPS = 1e-9, PI = acos(-1);\n\ninline bool eq(Real a, Real b)\n{\n return abs(b - a) < EPS;\n}\n\nusing Polygon = vector<Point>;\n\nnamespace std {\nbool operator< (const Point &a, const Point &b)\n{\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n}\n}\n\nReal cross(const Point &a, const Point &b)\n{\n return real(a) * imag(b) - imag(a) * real(b);\n}\n\nbool parallel(const Point &a, const Point &b)\n{\n return eq(cross(a, b), 0.0);\n}\n\nvector<int> convex_hull(const Polygon &p)\n{\n int n = (int)p.size(), k = 0;\n assert(n >= 3);\n \n vector< pair<Point, int> > vec;\n for (int i = 0; i < n; ++i) vec.push_back({p[i], i});\n sort(vec.begin(), vec.end());\n \n vector<int> ch(2 * n);\n vector< Point > pp(2 * n);\n for (int i = 0; i < n; pp[k] = vec[i].first, ch[k++] = vec[i++].second)\n {\n while (k >= 2 && cross(pp[k - 1] - pp[k - 2], vec[i].first - pp[k-1]) < EPS) --k;\n }\n for (int i = n - 2, t = k + 1; i >= 0; pp[k] = vec[i].first, ch[k++] = vec[i--].second)\n {\n while (k >= t && cross(pp[k - 1] - pp[k - 2], vec[i].first - pp[k-1]) < EPS) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\nbool is_end = false;\n\nvoid solve()\n{\n int N; cin >> N;\n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n vector< Point > real_points(N);\n for (int i = 0; i < N; ++i)\n {\n int x, y; cin >> x >> y;\n real_points[i] = Point(x, y);\n }\n \n vector< int > conv_ids = convex_hull(real_points);\n int siz = conv_ids.size();\n bool ans = true;\n for (int i = 0; i < siz; ++i)\n {\n int id0 = conv_ids[i], id1 = conv_ids[(i+1)%siz];\n if ((id1 - id0 + N) % N == 1) continue;\n \n Point dp = real_points[id1] - real_points[id0];\n \n {\n Point diff = real_points[(id0 + 1) % N] - real_points[id0];\n if (!parallel(dp, diff)) ans = false;\n \n diff = real_points[id1] - real_points[(id1 - 1 + N) % N];\n if (!parallel(dp, diff)) ans = false;\n }\n }\n \n cout << (ans ? \"Yes\" : \"No\") << endl;\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3404, "score_of_the_acc": -0.0031, "final_rank": 1 }, { "submission_id": "aoj_1669_9340813", "code_snippet": "/// 生成AI不使用 / Not using generative AI\n\n// #pragma GCC target (\"avx\")\n// #pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#ifdef MARC_LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n\n#ifdef ATCODER\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n//#define int long long\nusing ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vvvvll = vector<vvvll>;\nusing vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;\nusing ld = long double; using vld = vector<ld>; using vvld = vector<vld>; using vd = vector<double>;\nusing vc = vector<char>; using vvc = vector<vc>; using vs = vector<string>;\nusing vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;\nusing pii = pair<int, int>; using pcc = pair<char, char>; using pll = pair<ll, ll>; using pli = pair<ll, int>; using pdd = pair<double, double>; using pldld = pair<ld,ld>;\nusing vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpldld = vector<pldld>;\nusing ui = unsigned int; using ull = unsigned long long;\nusing i128 = __int128; using f128 = __float128;\ntemplate<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type> > name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type> > > name(h, vector<vector<type> >(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) vector<vector<vector<vector<type> > > > name(a, vector<vector<vector<type> > >(b, vector<vector<type> >(c, vector<type>(__VA_ARGS__))))\n\n#define overload4(a,b,c,d,name,...) name\n#define rep1(n) for(ll i = 0; i < (ll)n; i++)\n#define rep2(i,n) for(ll i = 0; i < (ll)n; i++)\n#define rep3(i,a,b) for (ll i = a; i < (ll)b; i++)\n#define rep4(i,a,b,c) for (ll i = a; i < (ll)b; i += (ll)c)\n#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)\n#define repback1(n) for (ll i = (ll)n-1; i >= 0; i--)\n#define repback2(i,n) for (ll i = (ll)n-1; i >= 0; i--)\n#define repback3(i,a,b) for (ll i = (ll)b-1; i >= (ll)a; i--)\n#define repback4(i,a,b,c) for (ll i = (ll)b-1; i >= (ll)a; i -= (ll)c)\n#define repback(...) overload4(__VA_ARGS__,repback4,repback3,repback2,repback1)(__VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\n#define include(y, x, H, W) (0 <= (y) && (y) < (H) && 0 <= (x) && (x) < (W))\n#define inrange(x, down, up) ((down) <= (x) && (x) <= (up))\n#define pb push_back\n#define eb emplace_back\n#define fir first\n#define sec second\n#define TIMER_START TIME_START = clock()\n#ifdef MARC_LOCAL\n// https://trap.jp/post/1224/\n#define debug1(x) cout << \"debug: \" << (#x) << \": \" << (x) << endl\n#define debug2(x, y) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << endl\n#define debug3(x, y, z) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << endl\n#define debug4(x, y, z, w) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << endl\n#define debug5(x, y, z, w, v) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << \", \" << (#v) << \": \" << (v) << endl\n#define debug6(x, y, z, w, v, u) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << \", \" << (#v) << \": \" << (v) << \", \" << (#u) << \": \" << (u) << endl\n#define overload6(a, b, c, d, e, f, g,...) g\n#define debug(...) overload6(__VA_ARGS__, debug6, debug5, debug4, debug3, debug2, debug1)(__VA_ARGS__)\n#define debuga cerr << \"a\" << endl\n#define debugnl cout << endl\n#define Case(i) cout << \"Case #\" << (i) << \": \"\n#define TIMECHECK cerr << 1000.0 * static_cast<double>(clock() - TIME_START) / CLOCKS_PER_SEC << \"ms\" << endl\n#define LOCAL 1\n#else\n#define debug1(x) void(0)\n#define debug2(x, y) void(0)\n#define debug3(x, y, z) void(0)\n#define debug4(x, y, z, w) void(0)\n#define debug5(x, y, z, w, v) void(0)\n#define debug6(x, y, z, w, v, u) void(0)\n#define debug(...) void(0)\n#define debuga void(0)\n#define debugnl void(0)\n#define Case(i) void(0)\n#define TIMECHECK void(0)\n#define LOCAL 0\n#endif\n\n//mt19937_64 rng(0);\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nclock_t TIME_START;\nconst long double pi = 3.141592653589793238462643383279L;\nconst long long INFL = 1000000000000000000ll;\nconst long long INFLMAX = numeric_limits< long long >::max(); // 9223372036854775807\nconst int INF = 1000000000;\nconst int INFMAX = numeric_limits< int >::max(); // 2147483647\nconst long double INFD = numeric_limits<ld>::infinity();\nconst long double EPS = 1e-10;\nconst int mod1 = 1000000007;\nconst int mod2 = 998244353;\nconst vi dx1 = {1,0,-1,0};\nconst vi dy1 = {0,1,0,-1};\nconst vi dx2 = {0, 1, 1, 1, 0, -1, -1, -1};\nconst vi dy2 = {1, 1, 0, -1, -1, -1, 0, 1};\nconstexpr char nl = '\\n';\nconstexpr char bl = ' ';\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) {__uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do {--d;*d = \"0123456789\"[tmp % 10];tmp /= 10;} while (tmp != 0);if (value < 0) {--d;*d = '-';}int len = std::end(buffer) - d;if (dest.rdbuf()->sputn(d, len) != len) {dest.setstate(std::ios_base::badbit);}}return dest; }\n__int128 parse(string &s) { __int128 ret = 0; for (int i = 0; i < (int)s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } // for __int128 (https://kenkoooo.hatenablog.com/entry/2016/11/30/163533)\ntemplate<class T> ostream& operator << (ostream& os, vector<T>& vec) { os << \"[\"; for (int i = 0; i<(int)vec.size(); i++) { os << vec[i] << (i + 1 == (int)vec.size() ? \"\" : \", \"); } os << \"]\"; return os; } /// vector 出力\ntemplate<class T, class U> ostream& operator << (ostream& os, pair<T, U>& pair_var) { os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\"; return os; } // pair 出力\ntemplate<class T, class U> ostream& operator << (ostream& os, map<T, U>& map_var) { os << \"{\"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << \"(\" << itr->first << \", \" << itr->second << \")\"; itr++; if(itr != map_var.end()) os << \", \"; itr--; } os << \"}\"; return os; } // map出力\ntemplate<class T> ostream& operator << (ostream& os, set<T>& set_var) { os << \"{\"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if(itr != set_var.end()) os << \", \"; itr--; } os << \"}\"; return os; } /// set 出力\n\nbool equals(long double a, long double b) { return fabsl(a - b) < EPS; }\ntemplate<class T> int sign(T a) { return equals(a, 0) ? 0 : a > 0 ? 1 : -1; }\nlong double radian_to_degree(long double r) { return (r * 180.0 / pi); }\nlong double degree_to_radian(long double d) { return (d * pi / 180.0); }\n\nint popcnt(unsigned long long a){ return __builtin_popcountll(a); } // ll は 64bit対応!\nint MSB1(unsigned long long x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // MSB(1), 0-indexed ( (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) )\nint LSB1(unsigned long long x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } // LSB(1), 0-indexed ( (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) )\nlong long maskbit(int n) { return (1LL << n) - 1; }\nbool bit_is1(long long x, int i) { return ((x>>i) & 1); }\nstring charrep(int n, char c) { return std::string(n, c); }\ntemplate<class T> T square(T x) { return (x) * (x); }\ntemplate<class T>void UNIQUE(T& A) {sort(all(A)); A.erase(unique(all(A)), A.end());}\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\nvoid YESNO(bool b) {if(b){cout<<\"YES\"<<'\\n';} else{cout<<\"NO\"<<'\\n';}} void YES() {YESNO(true);} void NO() {YESNO(false);}\nvoid yesno(bool b) {if(b){cout<<\"yes\"<<'\\n';} else{cout<<\"no\"<<'\\n';}} void yes() {yesno(true);} void no() {yesno(false);}\nvoid YesNo(bool b) {if(b){cout<<\"Yes\"<<'\\n';} else{cout<<\"No\"<<'\\n';}} void Yes() {YesNo(true);} void No() {YesNo(false);}\nvoid POSIMPOS(bool b) {if(b){cout<<\"POSSIBLE\"<<'\\n';} else{cout<<\"IMPOSSIBLE\"<<'\\n';}}\nvoid PosImpos(bool b) {if(b){cout<<\"Possible\"<<'\\n';} else{cout<<\"Impossible\"<<'\\n';}}\nvoid posimpos(bool b) {if(b){cout<<\"possible\"<<'\\n';} else{cout<<\"impossible\"<<'\\n';}}\nvoid FIRSEC(bool b) {if(b){cout<<\"FIRST\"<<'\\n';} else{cout<<\"SECOND\"<<'\\n';}}\nvoid firsec(bool b) {if(b){cout<<\"first\"<<'\\n';} else{cout<<\"second\"<<'\\n';}}\nvoid FirSec(bool b) {if(b){cout<<\"First\"<<'\\n';} else{cout<<\"Second\"<<'\\n';}}\nvoid AliBob(bool b) {if(b){cout<<\"Alice\"<<'\\n';} else{cout<<\"Bob\"<<'\\n';}}\nvoid TakAok(bool b) {if(b){cout<<\"Takahashi\"<<'\\n';} else{cout<<\"Aoki\"<<'\\n';}}\nint GetTime() {return 1000.0*static_cast<double>(clock() - TIME_START) / CLOCKS_PER_SEC;}\nll myRand(ll B) {return (unsigned long long)rng() % B;}\ntemplate<class T> void print(const T& x, const char endch = '\\n') { cout << x << endch; }\n\ntemplate<class T> T ceil_div(T x, T y) { assert(y); return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate<class T> T floor_div(T x, T y) { assert(y); return (x > 0 ? x / y : (x - y + 1) / y); }\ntemplate<class T> pair<T, T> divmod(T x, T y) { T q = floor_div(x, y); return {q, x - q * y}; } /// (q, r) s.t. x = q*y + r \nll GCD(ll a, ll b) { if(a < b) swap(a, b); if(b == 0) return a; if(a%b == 0) return b; else return GCD(b, a%b); }\nll LCM(ll a, ll b) { assert(GCD(a,b) != 0); return a / GCD(a, b) * b; }\nll MOD(ll &x, const ll P) { ll ret = x%P; if(ret < 0) ret += P; return x = ret; } /// x % P を非負整数に直す\nll mpow(ll x, ll n, const ll mod) { x %= mod; ll ret = 1; while(n > 0) { if(n & 1) ret = ret * x % mod; x = x * x % mod; n >>= 1; } return ret; } /// x^n % mod を計算\nll lpow(ll x, ll n) { ll ret = 1; while(n > 0){ if(n & 1) ret = ret * x; x = x * x; n >>= 1; } return ret; } /// x^nを計算\nstring toBinary(ll n) { if(n == 0) return \"0\"; assert(n > 0); string ret; while (n != 0){ ret += ( (n & 1) == 1 ? '1' : '0' ); n >>= 1; } reverse(ret.begin(), ret.end()); return ret; } /// 10進数(long long) -> 2進数(string)への変換\nll toDecimal(string S) { ll ret = 0; for(int i = 0; i < (int)S.size(); i++){ ret *= 2LL; if(S[i] == '1') ret += 1; } return ret; } /// 2進数(string) → 10進数(long long)への変換\nint ceil_pow2(ll n) { int x = 0; while ((1ll << x) < n) x++; return x;} /// return minimum non-negative `x` s.t. `n <= 2**x`\nint floor_pow2(ll n) { int x = 0; while ((1ll << (x+1)) <= n) x++; return x;} /// return maximum non-negative `x` s.t. `n >= 2**x`\n\n\n// ############################\n// # #\n// # C O D E S T A R T #\n// # #\n// ############################\n\n/// 2D計算幾何ライブラリ\n// 謎のバグが発生したら、比較部分での誤差(±EPSを設定)や整数幾何で出来ないか?を考えると良いかも\n// AOJの文字数制限に引っかかる場合は、仕方ないので適当に不要な関数を削る\nnamespace Geometry {\n constexpr long double EPS = (1e-10);\n constexpr long double pi = 3.141592653589793238462643383279L;\n std::mt19937_64 mt(std::chrono::steady_clock::now().time_since_epoch().count());\n\n template <class T, class U> bool equals(T a, U b) { return fabsl(a - b) < EPS; }\n template <class T, class U> bool notequals(T a, U b) { return !equals(a, b); }\n template <class T> int sign(T a) { return equals(a, 0) ? 0 : a > 0 ? 1 : -1; }\n\n\n /// 2次元平面上の点(x,y)\n template <class T>\n class Point2D { // D は dimension の D (Double ではない) -> OpenGL等の著名ライブラリとの命名規則統一すべき?\n public:\n T x, y; // privateにしても良い、要検討\n bool valid; // 有効な点か?\n int idx;\n\n Point2D() : x(0), y(0), valid(false) {}\n Point2D(T x, T y): x(x), y(y), valid(true) {}\n template <class U> Point2D(const Point2D<U>& P_) : x((T)P_.x), y((T)P_.y), valid(P_.valid), idx(P_.idx) {}\n template <class U1, class U2> Point2D(const std::pair<U1, U2>& p) : x(p.first), y(p.second), valid(true) {}\n \n \n Point2D operator + (const Point2D& p) const {return Point2D(x + p.x, y + p.y); }\n Point2D operator - (const Point2D& p) const {return Point2D(x - p.x, y - p.y); }\n Point2D operator * (T a) const {return Point2D(a * x, a * y); }\n Point2D operator / (T a) const {return Point2D(x / a, y / a); }\n Point2D& operator+=(const Point2D& p) { return (*this) = (*this) + p; }\n Point2D& operator-=(const Point2D& p) { return (*this) = (*this) - p; }\n Point2D& operator*=(T a) { return (*this) = (*this) * a; }\n Point2D& operator/=(T a) { return (*this) = (*this) / a; }\n bool operator<(const Point2D& p) const { return notequals(x, p.x) ? x < p.x : y < p.y; }\n bool operator==(const Point2D& p) const { return equals(x, p.x) && equals(y, p.y); }\n bool operator!=(const Point2D& p) const { return !((*this) == p); }\n \n\n T real() const { return x; }\n T imag() const { return y; }\n T xpos() const { return x; }\n T ypos() const { return y; }\n T dot(const Point2D& p) const { return x * p.x + y * p.y; }\n T cross(const Point2D& p) const { return x * p.y - y * p.x; }\n T norm() const { return x * x + y * y; }\n long double abs() const { return std::sqrt(norm()); }\n long double arg() const { return std::atan2(y, x); }\n\n // Center中心、反時計回りにrad度回転\n void rotate(const Point2D& Center, const T rad) {\n x -= Center.x, y -= Center.y;\n T x_ = x * std::cos(rad) - y * std::sin(rad);\n T y_ = x * std::sin(rad) + y * std::cos(rad);\n x = x_ + Center.x, y = y_ + Center.y;\n }\n\n // 原点中心、反時計回りにrad度回転\n void rotate(const T rad) { \n rotate(Point2D(0, 0), rad);\n }\n\n // 原点中心、反時計回りに90度回転\n void rot90() {\n T x_ = -y;\n y = x, x = x_;\n }\n\n // 単位ベクトルになるように変換\n void unit() {\n T len = this->abs();\n x /= len, y /= len;\n }\n\n // 偏角ソート用\n int argpos() const {\n if (equals(y, 0) && 0 <= x) return 0;\n if (y < 0) return -1;\n return 1;\n }\n\n void set_index(int i) { idx = i; }\n \n friend std::istream& operator>>(std::istream& is, Point2D& p) {\n T a, b;\n is >> a >> b;\n p = Point2D(a, b);\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point2D& p) {\n return os << std::fixed << std::setprecision(15) << p.x << \" \" << p.y;\n }\n\n\n private :\n // constexpr long double EPS = (1e-10);\n // constexpr long double pi = 3.141592653589793238462643383279L;\n \n // bool equals(T a, T b) { return fabsl(a - b) < EPS; }\n // bool notequals(T a, T b) { return !equlas(a, b); }\n // int sign(T a) { return equals(a, 0) ? 0 : a > 0 ? 1 : -1; }\n };\n using Point = Point2D<long double>;\n using Points = std::vector<Point>;\n using PoINT = Point2D<long long>;\n using PoINTs = std::vector<PoINT>;\n template <class T> \n using Polygon = std::vector<Point2D<T> >; // 多角形\n\n template <class T>\n using Vector2D = Point2D<T>; // 向き(x,y)と大きさ(abs(x,y))を持つ\n using Vector = Vector2D<long double>;\n\n\n template <class T> T real(const Point2D<T>& p) { return p.x; } // 実部\n template <class T> T imag(const Point2D<T>& p) { return p.y; } // 虚部\n template <class T> T dot(const Point2D<T>& l, const Point2D<T>& r) { return l.x * r.x + l.y * r.y; } // 内積\n template <class T> T cross(const Point2D<T>& l, const Point2D<T>& r) { return l.x * r.y - l.y * r.x; } // 外積\n template <class T> T norm(const Point2D<T>& p) { return p.x * p.x + p.y * p.y; } // 2-ノルム(ユークリッドノルム)の2乗\n template <class T> long double abs(const Point2D<T>& p) { return std::sqrt(norm(p)); } // 大きさ(絶対値)\n template <class T> long double arg(const Point2D<T>& p) { return std::atan2(p.y, p.x); } // 偏角\n Point polar_to_xy(const long double r, const long double theta) { return Point(r * std::cos(theta), r * std::sin(theta)); } // 極座標(r, theta) -> xy座標(x, y)\n\n template <class T>\n long double arg(const Point2D<T>& P1, const Point2D<T>& P2) {\n return std::atan2(cross(P1, P2), dot(P1, P2));\n }\n\n // 偏角ソート(-pi ~ pi)\n template<class T>\n void arg_sort(std::vector<Point2D<T> >& Ps) {\n std::sort(Ps.begin(), Ps.end(), [](const Point2D<T>& P1, const Point2D<T>& P2) {\n if(P1.argpos() != P2.argpos()) return P1.argpos() < P2.argpos();\n return cross(P1, P2) > 0;\n });\n }\n\n // x座標でソート\n template<class T>\n void x_sort(std::vector<Point2D<T> >& Ps) {\n std::sort(Ps.begin(), Ps.end(), [](const Point2D<T>& a, const Point2D<T>& b){ return equals(a.x, b.x) ? a.y < b.y : a.x < b.x; });\n }\n\n // y座標でソート\n template<class T>\n void y_sort(std::vector<Point2D<T> >& Ps) {\n std::sort(Ps.begin(), Ps.end(), [](const Point2D<T>& a, const Point2D<T>& b){ return equals(a.y, b.y) ? a.x < b.x : a.y < b.y; });\n }\n\n\n /// Pを、Center中心、反時計回りにrad度回転\n template <class T>\n Point2D<T> rotate(Point2D<T> P, const Point2D<T>& Center, const T rad) {\n P -= Center;\n T x_ = P.x * std::cos(rad) - P.y * std::sin(rad);\n T y_ = P.x * std::sin(rad) + P.y * std::cos(rad);\n return Point2D<T>(x_ + Center.x, y_ + Center.y);\n }\n\n /// 点Pを、原点中心、反時計回りにrad度回転\n template <class T>\n Point2D<T> rotate(const Point2D<T>& P, const T rad) { \n return rotate(P, Point2D<T>(0, 0), rad);\n }\n\n // 原点中心、反時計回りに90度回転\n template <class T>\n Point2D<T> rot90(const Point2D<T>& P) {\n return Point2D<T>(-P.y, P.x);\n }\n\n /// V1の単位ベクトルを取得\n template <class T>\n Vector2D<T> unit(const Vector2D<T>& V1) {\n return V1 / V1.abs();\n }\n template <class T>\n Vector2D<T> unit(const Point2D<T>& P1, const Point2D<T>& P2) {\n Vector2D<T> V = P2 - P1;\n return unit(V);\n }\n\n /// 二点間の中点を取得\n template <class T>\n Point2D<T> mid(const Point2D<T>& P1, const Point2D<T>& P2) {\n return Point2D<T>((P1.x + P2.x) / 2, (P1.y + P2.y) / 2);\n }\n\n /// ベクトルAとベクトルBのなす角(B->Aの回転角、[-pi,pi])\n template <class T>\n long double get_angle(const Vector2D<T>& V1, const Vector2D<T>& V2) {\n long double ret = arg(V1) - arg(V2);\n while(ret > pi + EPS) ret -= 2 * pi;\n while(ret < -pi - EPS) ret += 2 * pi;\n return ret;\n }\n\n /// 2点 P1, P2 を通る二次元平面上の直線 Ax + By + C = 0\n template <class T>\n class Line2D {\n public : \n Point2D<T> P1, P2;\n T A, B, C;\n int idx;\n\n Line2D() = default;\n\n // P1, P2 -> Ax + By + C = 0 を計算\n Line2D(Point2D<T> P1, Point2D<T> P2) : P1(P1), P2(P2) // check\n {\n A = P2.y - P1.y;\n B = P1.x - P2.x;\n C = P1.y * (P2.x - P1.x) - P1.x * (P2.y - P1.y);\n }\n Line2D(T x1, T y1, T x2, T y2) : P1(Point2D<T>(x1, y1)), P2(Point2D<T>(x2, y2)) // check\n {\n A = P2.y - P1.y;\n B = P1.x - P2.x;\n C = P1.y * (P2.x - P1.x) - P1.x * (P2.y - P1.y);\n }\n\n // Ax + By + C = 0 -> P1, P2 を計算\n Line2D(T A, T B, T C) : A(A), B(B), C(C) // check\n {\n if(equals(A, 0)) P1 = Point2D(0, - C / B), P2 = Point2D(1, - C / B);\n else if(equals(B, 0)) P1 = Point2D(- C / A, 0), P2 = Point2D(- C / A, 1);\n else P1 = Point2D(0, - C / B), P2 = Point2D(- C / A , 0);\n }\n\n template <class U>\n Line2D(const Line2D<U>& L_) : Line2D<T>(Point2D<T>(L_.P1), Point2D<T>(L_.P2)) {}\n\n friend std::ostream &operator<<(std::ostream &os, Line2D &L) {\n return os << \"(\" << L.P1 << \"), (\" << L.P2 << \") / \" << L.A << \"x + \" << L.B << \"y + \"<< L.C;\n }\n\n\n // 直線の傾きを取得\n T Grad() {\n if(equals(B, 0)) return std::numeric_limits< T >::max();\n return - A / B;\n }\n\n // 中点を取得\n Point2D<T> mid() {\n return Point2D<T>((P1.x + P2.x) / 2, (P1.y + P2.y) / 2);\n }\n\n // 垂直二等分線を取得\n Line2D<T> bisector() {\n Point2D<T> M = this->mid();\n return Line2D<T>(M, M + rot90(P2-P1));\n }\n\n void set_index(int i) { idx = i; }\n \n\n private:\n // constexpr long double EPS = (1e-10);\n // constexpr long double pi = 3.141592653589793238462643383279L;\n \n // bool equals(T a, T b) { return fabsl(a - b) < EPS; }\n // bool notequals(T a, T b) { return !equlas(a, b); }\n // int sign(T a) { return equals(a, 0) ? 0 : a > 0 ? 1 : -1; }\n\n };\n using Line = Line2D<long double>;\n using Lines = std::vector<Line>;\n\n template <class T>\n using Segment2D = Line2D<T>; // P1, P2を端点とする線分\n using Segment = Segment2D<long double>;\n using Segments = std::vector<Segment>;\n\n /// 直線Aと直線Bのなす角(is_abs : 絶対値取る?, under90 : 0~pi/2に正規化?)\n template <class T>\n long double get_angle(const Line2D<T>& L1, const Line2D<T>& L2, const bool is_abs = false, const bool under90 = false) {\n long double ret = get_angle(L1.P2 - L1.P1, L2.P2 - L2.P1);\n if(is_abs) ret = std::abs(ret);\n if(under90 && ret > pi/2 + EPS) ret = pi - ret;\n if(under90 && ret < -pi/2 - EPS) ret = -pi - ret;\n return ret;\n }\n\n /// 二点間の垂直二等分線を取得\n template <class T>\n Line2D<T> bisector(const Point2D<T>& P1, const Point2D<T>& P2) {\n Point2D<T> M = mid(P1, P2);\n return Line2D<T>(M, M + rot90(P2-P1));\n }\n\n /// 中心点 c と、半径 r を持つ二次元平面上の円\n // TODO: 整数用に、半径を2乗で管理?2乗用の変数を追加しても良い \n template <class T>\n class Circle {\n public:\n Point2D<T> center;\n T radius;\n\n Circle() = default;\n\n Circle(T x, T y, T radius) : center(Point2D<T>(x, y)), radius(radius) {}\n\n Circle(Point2D<T> center, T radius) : center(center), radius(radius) {}\n\n template<class U>\n Circle(Circle<U> C_) : center(Point2D<T>(C_.center)), radius((T)C_.radius) {}\n\n bool operator==(const Circle& C) const { return center == C.center && equals(radius, C.radius); }\n bool operator!=(const Circle& C) const { return !((*this) == C); }\n\n \n };\n template <class T>\n using Circles = std::vector<Circle<T> >;\n using Circld = Circle<long double>;\n using Circlds = std::vector<Circld >;\n\n static const int COUNTER_CLOCKWISE = 1; // 反時計回り\n static const int CLOCKWISE = -1; // 時計回り\n static const int ONLINE_BACK = 2; // P2 -> P0 -> P1 の順で一直線 (P0 -> P1 とは逆方向にP2が存在)\n static const int ONLINE_FRONT = -2; // P0 -> P1 -> P2 の順で一直線 (P0 -> P1 と同方向にP2が存在)\n static const int ON_SEGMENT = 0; // P0 -> P2 -> P1 の順で一直線 (P0 -> P1 内にP2が存在)\n\n /**\n * @brief p0, p1, p2 がこの順で反時計回り(Counter Clock Wise)か?の判定\n * \n * @return 1 : COUNTER_CLOCKWISE(反時計回り)\n * -1 : CLOCKWISE(時計回り)\n * 2 : ONLINE_BACK(P2 -> P0 -> P1 の順で一直線 (P0 -> P1 とは逆方向にP2が存在))\n * -2 : ONLINE_FRONT(P0 -> P1 -> P2 の順で一直線 (P0 -> P1 と同方向にP2が存在))\n * 0 : ON_SEGMENT(P0 -> P2 -> P1 の順で一直線 (P0 -> P1 内にP2が存在))\n */\n template <class T>\n int ccw(const Point2D<T>& P0, const Point2D<T>& P1, const Point2D<T>& P2) {\n Vector2D<T> V1 = P1 - P0;\n Vector2D<T> V2 = P2 - P0;\n\n if(cross(V1, V2) > EPS) return COUNTER_CLOCKWISE; // 反時計回り\n else if(cross(V1, V2) < -EPS) return CLOCKWISE; // 時計回り\n else if(dot(V1, V2) < -EPS) return ONLINE_BACK; // P2 -> P0 -> P1 の順で一直線 (P0 -> P1 とは逆方向にP2が存在)\n else if(V1.norm() < V2.norm()) return ONLINE_FRONT; // P0 -> P1 -> P2 の順で一直線 (P0 -> P1 と同方向にP2が存在)\n else return ON_SEGMENT; // P0 -> P2 -> P1 の順で一直線 (P0 -> P1 内にP2が存在)\n }\n\n\n /// 直交判定\n template<class T>\n bool isOrthogonal(const Vector2D<T>& a, const Vector2D<T>& b) {\n return equals(dot(a, b), 0.0);\n }\n template<class T>\n bool isOrthogonal(const Point2D<T>& a1, const Point2D<T>& a2, const Point2D<T>& b1, const Point2D<T>& b2) {\n return isOrthogonal(a1 - a2, b1 - b2);\n }\n template<class T>\n bool isOrthogonal(const Line2D<T>& l1, const Line2D<T>& l2) {\n return equals(dot(l1.P2 - l1.P1, l2.P2 - l2.P1), 0.0);\n }\n\n\n /// 平行判定\n template<class T>\n bool isParallel(const Vector2D<T>& a, const Vector2D<T>& b) {\n return equals(cross(a, b), 0.0);\n }\n template<class T>\n bool isParallel(const Point2D<T>& a1, const Point2D<T>& a2, const Point2D<T>& b1, const Point2D<T>& b2) {\n return isParallel(a1 - a2, b1 - b2);\n }\n template<class T>\n bool isParallel(const Line2D<T>& l1, const Line2D<T>& l2) {\n return equals(cross(l1.P2 - l1.P1, l2.P2 - l2.P1), 0.0);\n }\n\n\n /// 射影\n // 直線l に対して 点p から垂線をおろした交点を求める\n // 内積とcosの関係を用いて計算\n template<class T>\n Point2D<T> projection(const Line2D<T>& l, const Point2D<T>& p) {\n Vector2D<T> base = l.P2 - l.P1;\n long double t = dot(p - l.P1, base) / norm(base);\n return l.P1 + base * t;\n }\n\n\n /// 反射\n // 直線l を対称軸として、点p と線対称にある点を求める\n template<class T>\n Point2D<T> reflection(const Line2D<T>& l, const Point2D<T>& p) {\n return p + (projection(l, p) - p) * 2.0;\n }\n\n\n /// 凸包(反時計回り)を生成\n // Andrew's Algorithm (x must be sorted)\n // boundary : 周上の点も列挙する場合 true \n template<class T>\n std::vector<Point2D<T> > ConvexHull(std::vector<Point2D<T> > ps, bool boundary = false) {\n x_sort(ps);\n ps.erase(std::unique(ps.begin(), ps.end()), ps.end()); \n int N = (int)ps.size();\n if (N <= 2) return ps;\n\n std::vector<Point2D<T> > convex(2*N);\n int k = 0;\n long double th = boundary ? -EPS : +EPS;\n for (int i = 0; i < N; convex[k++] = ps[i++]) {\n while (k >= 2 && cross(convex[k - 1] - convex[k - 2], ps[i] - convex[k - 1]) < th) --k;\n }\n for (int i = N - 2, t = k + 1; i >= 0; convex[k++] = ps[i--]) {\n while (k >= t && cross(convex[k - 1] - convex[k - 2], ps[i] - convex[k - 1]) < th) --k;\n }\n convex.resize(k - 1);\n return convex;\n }\n}\n\n\nvoid solve() {\n while(true) {\n int N; cin >> N;\n if(N == 0) break;\n\n Geometry::Polygon<ld> G(N);\n rep(i,N) {\n ld x,y; cin >> x >> y;\n G[i] = Geometry::Point(x,y);\n G[i].set_index(i);\n }\n auto memo = G;\n\n auto convex = Geometry::ConvexHull(G, true);\n int M = convex.size();\n debug(convex);\n bool ans = true;\n rep(i,M) {\n auto cur = convex[i];\n int idx = cur.idx;\n \n auto gprev = G[(N-1+idx)%N];\n auto gnxt = G[(idx+1)%N];\n //if(gnxt < gprev) swap(gprev, gnxt);\n\n auto prev = convex[(M-1+i)%M];\n auto nxt = convex[(i+1)%M];\n //if(nxt < prev) swap(prev, nxt);\n\n if(nxt == gnxt && prev == gprev) continue;\n if(nxt == gprev && prev == gnxt) continue;\n\n debug(cur,gprev,gnxt,prev,nxt);\n if(prev == gnxt) swap(prev, gnxt);\n if(nxt == gprev) swap(nxt, gprev);\n\n if(prev == gprev) {\n Geometry::Line L1(gprev, cur);\n Geometry::Line L2(cur, nxt);\n\n if(!Geometry::isParallel(L1, L2)) {\n ans = false;\n }\n }\n else {\n Geometry::Line L1(gnxt, cur);\n Geometry::Line L2(cur, prev);\n\n if(!Geometry::isParallel(L1, L2)) {\n ans = false;\n } \n }\n }\n\n YesNo(ans);\n \n }\n\n\n\n}\n\n\n\nsigned main() {\n cin.tie(0); ios_base::sync_with_stdio(false);\n TIMER_START;\n //cout << fixed << setprecision(15);\n \n\n int tt = 1;\n //cin >> tt;\n //cin.ignore();\n while(tt--){\n solve();\n }\n\n TIMECHECK;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3568, "score_of_the_acc": -0.0381, "final_rank": 10 }, { "submission_id": "aoj_1669_9329159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1000000000;\nconstexpr int64_t llINF = 3000000000000000000;\nconstexpr double eps = 1e-10;\nconst double pi = acos(-1);\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\nll extgcd(ll a, ll b, ll& x, ll& y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nll modpow(ll a, ll b, ll m) {\n ll res = 1;\n while (b) {\n if (b & 1) {\n res *= a;\n res %= m;\n }\n a *= a;\n a %= m;\n b >>= 1;\n }\n return res;\n}\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint& operator+=(const modint& p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint& operator-=(const modint& p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint& operator*=(const modint& p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint& operator/=(const modint& p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint& p) const { return modint(*this) += p; }\n modint operator-(const modint& p) const { return modint(*this) -= p; }\n modint operator*(const modint& p) const { return modint(*this) *= p; }\n modint operator/(const modint& p) const { return modint(*this) /= p; }\n bool operator==(const modint& p) const { return x == p.x; }\n bool operator!=(const modint& p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream& operator<<(ostream& os, const modint& p) { return os << p.x; }\n friend istream& operator>>(istream& is, modint& a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\nnamespace geometry {\n\nusing floating_point_type = long double;\n\nconstexpr floating_point_type EPS = 1e-13;\nconstexpr floating_point_type Pi = 3.141592653589793;\n\nconstexpr floating_point_type operator\"\" _deg(floating_point_type deg) { return static_cast<floating_point_type>(deg * Pi / 180.0); }\nconstexpr floating_point_type operator\"\" _deg(unsigned long long deg) { return static_cast<floating_point_type>(deg * Pi / 180.0); }\n\ntemplate <typename T>\nconstexpr int sgn(T a) noexcept {\n return (a < -EPS ? -1 : (a > EPS ? +1 : 0));\n}\n\ntemplate <typename T, typename U>\nconstexpr bool same(T a, U b) noexcept {\n return sgn(a - b) == 0;\n}\n\ntemplate <typename T>\nconstexpr T square(T x) noexcept {\n return x * x;\n}\n\n// ax + by + c = 0, gcd(a,b,c) = 1\n// a != 0 : a > 0\n// a = 0: b > 0\ntuple<long long, long long, long long> general_form(long long x1, long long y1, long long x2, long long y2) {\n long long a = y1 - y2, b = x2 - x1, c = x1 * y2 - x2 * y1;\n if (a < 0) a = -a, b = -b, c = -c;\n if (a == 0 && b < 0) b = -b, c = -c;\n long long g = gcd(gcd(a, b), c);\n return {a / g, b / g, c / g};\n}\n\ntemplate <typename T>\nstruct Vector2D {\n using value_type = T;\n T x, y;\n\n Vector2D() = default;\n explicit Vector2D(T arg) : x(cos(arg)), y(sin(arg)) {}\n constexpr Vector2D(T x, T y) noexcept : x(x), y(y) {}\n\n constexpr bool operator<(const Vector2D& v) const noexcept {\n if (!same(x, v.x)) return x < v.x;\n return y < v.y;\n }\n constexpr bool operator>(const Vector2D& v) const noexcept { return v < *this; }\n constexpr bool operator==(const Vector2D& v) const noexcept { return same(x, v.x) && same(y, v.y); }\n constexpr bool operator!=(const Vector2D& v) const noexcept { return !(*this == v); }\n\n constexpr Vector2D operator+() const noexcept { return *this; }\n constexpr Vector2D operator-() const noexcept { return {-x, -y}; }\n\n constexpr Vector2D operator+(const Vector2D& v) const noexcept { return {x + v.x, y + v.y}; }\n constexpr Vector2D operator-(const Vector2D& v) const noexcept { return {x - v.x, y - v.y}; }\n constexpr Vector2D operator*(T s) const noexcept { return {x * s, y * s}; }\n constexpr Vector2D operator/(T s) const noexcept { return {x / s, y / s}; }\n\n constexpr Vector2D& operator+=(const Vector2D& v) noexcept {\n x += v.x;\n y += v.y;\n return *this;\n }\n constexpr Vector2D& operator-=(const Vector2D& v) noexcept {\n x -= v.x;\n y -= v.y;\n return *this;\n }\n constexpr Vector2D& operator*=(T s) noexcept {\n x *= s;\n y *= s;\n return *this;\n }\n constexpr Vector2D& operator/=(T s) noexcept {\n x /= s;\n y /= s;\n return *this;\n }\n\n constexpr bool isZero() const noexcept { return x == T(0) && y == T(0); }\n bool hasNan() const { return isnan(x) || isnan(y); }\n\n constexpr T dot(const Vector2D& v) const noexcept { return x * v.x + y * v.y; }\n constexpr T cross(const Vector2D& v) const noexcept { return x * v.y - y * v.x; }\n\n floating_point_type length() const {\n if (isZero()) return 0.0;\n\n const T abs_x = std::abs(x);\n const T abs_y = std::abs(y);\n\n const auto [u, v] = std::minmax(abs_x, abs_y);\n const T t = u / v;\n\n return v * std::sqrt(std::fma(t, t, 1.0));\n }\n constexpr T lengthSq() const noexcept { return dot(*this); }\n\n floating_point_type distanceFrom(const Vector2D& v) const { return (v - *this).length(); }\n constexpr T distanceFromSq(const Vector2D& v) const noexcept { return (v - *this).lengthSq(); }\n\n Vector2D normalized() const { return *this / length(); }\n Vector2D& normalize() { return *this /= length(); }\n\n Vector2D rotated(floating_point_type angle) const {\n const T s = sin(angle);\n const T c = cos(angle);\n return {x * c - y * s, x * s + y * c};\n }\n Vector2D& rotate(floating_point_type angle) { return *this = rotated(angle); }\n\n floating_point_type arg() const { return atan2<floating_point_type>(y, x); }\n floating_point_type arg(const Vector2D& v) const {\n if (isZero() || v.isZero()) {\n return numeric_limits<T>::signaling_NaN();\n }\n\n return atan2<floating_point_type>(cross(v), dot(v));\n }\n floating_point_type arg(const Vector2D& a, const Vector2D& b) const { return (a - *this).arg(b - *this); }\n\n constexpr Vector2D lerp(const Vector2D& v, T f) const noexcept { return {x + (v.x - x) * f, y + (v.y - y) * f}; }\n\n constexpr friend Vector2D operator*(T s, const Vector2D& v) noexcept { return {s * v.x, s * v.y}; }\n friend istream& operator>>(istream& is, Vector2D& v) { return is >> v.x >> v.y; }\n friend ostream& operator<<(ostream& os, const Vector2D& v) { return os << \"(\" << v.x << \", \" << v.y << \")\"; }\n};\n\ntemplate <typename T>\nvoid argument_sort(vector<Vector2D<T>>& vec, const Vector2D<T>& center = Vector2D<T>(0, 0)) {\n auto orthant = [](const Vector2D<T>& v) {\n if (sgn(v.x) < 0 && sgn(v.y) < 0) return 0;\n if (sgn(v.x) >= 0 && sgn(v.y) < 0) return 1;\n if (sgn(v.x) == 0 && sgn(v.y) == 0) return 2;\n if (sgn(v.x) >= 0 && sgn(v.y) >= 0) return 3;\n return 4;\n };\n\n auto comp = [orthant, center](const Vector2D<T>& a, const Vector2D<T>& b) {\n const int o1 = orthant(a - center), o2 = orthant(b - center);\n if (o1 != o2) return o1 < o2;\n\n return (a - center).cross(b - center) > 0;\n };\n\n sort(vec.begin(), vec.end(), comp);\n}\n\ntemplate <typename T>\nvector<Vector2D<T>> convex_hull(vector<Vector2D<T>> points) {\n int n = points.size(), k = 0;\n vector<Vector2D<T>> res(n * 2);\n\n sort(points.begin(), points.end());\n points.erase(unique(points.begin(), points.end()), points.end());\n\n for (int i = 0; i < n; ++i) {\n while (k > 1 && sgn((res[k - 1] - res[k - 2]).cross(points[i] - res[k - 1])) <= 0) k--;\n res[k++] = points[i];\n }\n\n for (int i = n - 2, t = k; i >= 0; --i) {\n while (k > t && sgn((res[k - 1] - res[k - 2]).cross(points[i] - res[k - 1])) <= 0) k--;\n res[k++] = points[i];\n }\n\n res.resize(k - 1);\n return res;\n}\n\n/*\nABから見てBCは左に曲がるのなら +1\nABから見てBCは右に曲がるのなら -1\nBACの順番で一直線上に並ぶなら +2\nABCの順番で一直線上に並ぶなら -2\nCがAB上なら 0\n*/\ntemplate <typename T>\nconstexpr int ccw(const Vector2D<T>& a, const Vector2D<T>& b, const Vector2D<T>& c) {\n const int res = sgn((b - a).cross(c - a));\n\n if (res != 0) return res;\n if (sgn((a - b).dot(c - a)) > 0) return +2;\n if (sgn((b - a).dot(c - b)) > 0) return -2;\n return 0;\n}\n\nusing Vec2 = Vector2D<floating_point_type>;\n\nstruct Line {\n using value_type = Vec2::value_type;\n Vec2 begin, end;\n\n Line() = default;\n constexpr Line(const Vec2& begin, const Vec2& end) : begin(begin), end(end) {}\n constexpr Line(value_type sx, value_type sy, value_type gx, value_type gy) : begin(sx, sy), end(gx, gy) {}\n\n // ax + by + c = 0\n Line(value_type a, value_type b, value_type c) {\n assert(sgn(a) != 0 || sgn(b) != 0);\n\n if (sgn(b) == 0) {\n begin = Vec2(-c / a, 0.0);\n end = Vec2(-c / a, 1.0);\n } else {\n begin = Vec2(0, -c / b);\n end = Vec2(1.0, -(a + c) / b);\n }\n }\n\n constexpr Vec2 vector() const { return end - begin; }\n\n constexpr Line& reverse() {\n const Vec2 tmp = begin;\n begin = end;\n end = tmp;\n return *this;\n }\n constexpr Line reversed() const { return {end, begin}; }\n\n constexpr Line& moveBy(const Vec2& v) {\n begin += v;\n end += v;\n return *this;\n }\n constexpr Line movedBy(const Vec2& v) const { return {begin + v, end + v}; }\n\n constexpr bool isParallel(const Line& line) const { return sgn(vector().cross(line.vector())) == 0; }\n constexpr bool isOrthogonal(const Line& line) const { return sgn(vector().dot(line.vector())) == 0; }\n\n floating_point_type length() const { return vector().length(); }\n constexpr value_type lengthSq() const { return vector().lengthSq(); }\n\n floating_point_type distanceFrom(const Vec2& v) const { return fabs(vector().cross(v - begin)) / length(); }\n constexpr value_type distanceFromSq(const Vec2& v) const {\n const value_type d = vector().cross(v - begin);\n\n return d / lengthSq() * d;\n }\n\n optional<Vec2> intersectsAt(const Line& line) const {\n if (sgn(vector().cross(line.vector())) == 0) return nullopt;\n\n return begin + vector() * fabs((line.end - begin).cross(line.vector()) / vector().cross(line.vector()));\n }\n\n constexpr Vec2 projection(const Vec2& v) const { return begin + vector() * (v - begin).dot(vector()) / lengthSq(); }\n constexpr Vec2 reflection(const Vec2& v) const { return 2 * projection(v) - v; }\n\n // ax + by + c = 0\n constexpr tuple<value_type, value_type, value_type> generalForm() const {\n const Vec2 vec = vector();\n return {vec.y, -vec.x, end.cross(begin)};\n }\n\n friend istream& operator>>(istream& is, Line& rhs) { return is >> rhs.begin >> rhs.end; }\n};\n\nnamespace segment {\n\nusing Segment = Line;\n\nconstexpr bool intersect(const Segment& a, const Segment& b) {\n return ccw(a.begin, a.end, b.begin) * ccw(a.begin, a.end, b.end) <= 0 && ccw(b.begin, b.end, a.begin) * ccw(b.begin, b.end, a.end) <= 0;\n}\n\nfloating_point_type distance(const Segment& a, const Vec2& v) {\n if (sgn(a.vector().dot(v - a.begin)) < 0 || sgn((-a.vector()).dot(v - a.end)) < 0) {\n return min(v.distanceFrom(a.begin), v.distanceFrom(a.end));\n }\n return a.distanceFrom(v);\n}\n\nconstexpr floating_point_type distanceSq(const Segment& a, const Vec2& v) {\n if (sgn(a.vector().dot(v - a.begin)) < 0 || sgn((-a.vector()).dot(v - a.end)) < 0) {\n return min(v.distanceFromSq(a.begin), v.distanceFromSq(a.end));\n }\n return a.distanceFromSq(v);\n}\n\nfloating_point_type distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0.0;\n\n return min({distance(a, b.begin), distance(a, b.end), distance(b, a.begin), distance(b, a.end)});\n}\n} // namespace segment\n\nstruct Triangle {\n using value_type = Vec2::value_type;\n Vec2 p0, p1, p2;\n\n Triangle() = default;\n constexpr Triangle(const Vec2& p0, const Vec2& p1, const Vec2& p2) : p0(p0), p1(p1), p2(p2) {}\n\n constexpr Vec2& p(size_t index) { return (&p0)[index]; }\n constexpr const Vec2& p(size_t index) const { return (&p0)[index]; }\n\n constexpr Line side(size_t index) const {\n if (index == 0)\n return Line(p1, p2);\n else if (index == 1)\n return Line(p2, p0);\n else if (index == 2)\n return Line(p0, p1);\n else {\n throw std::out_of_range(\"Triangle::side() index out of range\");\n }\n }\n\n floating_point_type angle(size_t index) const {\n const Vec2 vec[3] = {p(index), p((index + 1) % 3), p((index + 2) % 3)};\n\n const value_type res = vec[0].arg(vec[1], vec[2]);\n\n return fabs(res);\n }\n\n constexpr value_type area() const {\n const value_type res = (p1 - p0).cross(p2 - p0) / 2;\n return (0 < res ? res : -res);\n }\n\n constexpr Vec2 circumCenter() const {\n const value_type d0 = side(0).lengthSq(), d1 = side(1).lengthSq(), d2 = side(2).lengthSq(), t0 = d0 * (-d0 + d1 + d2),\n t1 = d1 * (+d0 - d1 + d2), t2 = d2 * (+d0 + d1 - d2), t3 = t0 + t1 + t2;\n\n return t0 / t3 * p0 + t1 / t3 * p1 + t2 / t3 * p2;\n }\n\n Vec2 innerCenter() const {\n const value_type dist[3] = {side(0).length(), side(1).length(), side(2).length()};\n\n return (dist[0] * p0 + dist[1] * p1 + dist[2] * p2) / (dist[0] + dist[1] + dist[2]);\n }\n\n friend istream& operator>>(istream& is, Triangle& rhs) { return is >> rhs.p0 >> rhs.p1 >> rhs.p2; }\n};\n\nstruct Circle {\n using value_type = Vec2::value_type;\n Vec2 center;\n value_type r;\n\n Circle() = default;\n explicit constexpr Circle(value_type r) : center(0.0, 0.0), r(r) {}\n constexpr Circle(value_type x, value_type y, value_type r) : center(x, y), r(r) {}\n constexpr Circle(const Vec2& center, value_type r) : center(center), r(r) {}\n\n constexpr bool intersects(const Line& line) const { return sgn(line.distanceFromSq(center) - r * r) <= 0; }\n\n constexpr bool intersects(const Circle& circle) const {\n const value_type d = center.distanceFromSq(circle.center);\n\n return sgn(square(r - circle.r) - d) <= 0 && sgn(d - square(r + circle.r)) <= 0;\n }\n\n constexpr bool circumcribes(const Circle& circle) const { return same(center.distanceFromSq(circle.center), square(r + circle.r)); }\n\n constexpr bool inscribes(const Circle& circle) const { return same(center.distanceFromSq(circle.center), square(r - circle.r)); }\n\n constexpr bool contains(const Circle& circle) const {\n return 0 < sgn(square(r - circle.r) - center.distanceFromSq(circle.center)) && 0 < sgn(r - circle.r);\n }\n\n constexpr value_type area() const { return Pi * r * r; }\n\n value_type commonArea(const Circle& circle) const {\n const value_type d_sq = center.distanceFromSq(circle.center);\n const value_type d = center.distanceFrom(circle.center);\n\n const value_type r1 = r;\n const value_type r2 = circle.r;\n\n if (sgn(d_sq - square(r1 + r2)) >= 0) return 0.0;\n if (sgn(d_sq - square(r1 - r2)) <= 0) return min(this->area(), circle.area());\n\n const value_type arg1 = acos(((r1 - r2) * (r1 + r2) + d_sq) / (2 * r1 * d));\n const value_type arg2 = acos(((r2 - r1) * (r2 + r1) + d_sq) / (2 * r2 * d));\n\n return r1 * r1 * (arg1 - sin(2 * arg1) / 2) + r2 * r2 * (arg2 - sin(2 * arg2) / 2);\n }\n\n constexpr Circle& moveBy(const Vec2& v) {\n center += v;\n return *this;\n }\n constexpr Circle movedBy(const Vec2& v) const { return {center + v, r}; }\n\n optional<vector<Vec2>> intersectsAt(const Line& line) const {\n if (!intersects(line)) return nullopt;\n\n vector<Vec2> res;\n\n const Vec2 v = line.projection(center);\n\n if (same(line.distanceFrom(center), r))\n res.emplace_back(v);\n else {\n value_type d = sqrt(r * r - (v - center).lengthSq());\n const Vec2 e = line.vector().normalized();\n\n res.emplace_back(v - d * e);\n res.emplace_back(v + d * e);\n }\n\n return res;\n }\n\n optional<vector<Vec2>> intersectsAt(const Circle& circle) const {\n if (!intersects(circle)) return nullopt;\n\n const value_type d = center.distanceFrom(circle.center), arg = (circle.center - center).arg(),\n theta = acos((d * d + r * r - circle.r * circle.r) / (2 * d * r));\n\n vector<Vec2> res;\n res.emplace_back(center + r * Vec2(arg + theta));\n res.emplace_back(center + r * Vec2(arg - theta));\n\n if (res[0] > res[1]) swap(res[0], res[1]);\n\n return res;\n }\n\n optional<vector<Vec2>> tangentPointFrom(const Vec2& v) const {\n const value_type d = center.distanceFromSq(v);\n\n if (sgn(d - r * r) < 0) return nullopt;\n if (same(d, r * r)) return vector<Vec2>{v};\n\n const auto [x1, y1] = v - center;\n\n const auto& opt = Circle(r).intersectsAt(Line(x1, y1, -r * r));\n\n vector<Vec2> res = opt.value();\n\n for (Vec2& v : res) v += center;\n\n if (res[0] > res[1]) swap(res[0], res[1]);\n\n return res;\n }\n optional<vector<Vec2>> commonTangentPointFrom(const Circle& circle) const {\n const value_type r1 = r, r2 = circle.r;\n\n vector<Vec2> res;\n\n if (!same(r1, r2)) {\n const Vec2 p((r2 * center - r1 * circle.center) / (r2 - r1));\n\n const auto& opt = tangentPointFrom(p);\n\n if (opt.has_value()) {\n for (const Vec2& vec : opt.value()) {\n res.emplace_back(vec);\n }\n }\n } else {\n const value_type theta = (circle.center - center).arg();\n\n res.emplace_back(center + r1 * Vec2(theta + 90_deg));\n res.emplace_back(center + r1 * Vec2(theta - 90_deg));\n }\n\n const Vec2 q((r2 * center + r1 * circle.center) / (r1 + r2));\n\n const auto& opt = tangentPointFrom(q);\n\n if (opt.has_value()) {\n for (const Vec2& vec : opt.value()) {\n res.emplace_back(vec);\n }\n }\n\n if (res.empty()) return nullopt;\n\n sort(res.begin(), res.end());\n\n return res;\n }\n\n friend istream& operator>>(istream& is, Circle& rhs) { return is >> rhs.center >> rhs.r; }\n};\n\nstruct Polygon {\n using value_type = Vec2::value_type;\n int n;\n vector<Vec2> vertex;\n\n Polygon() = default;\n Polygon(int _n) : n(_n), vertex(_n) {}\n Polygon(const vector<Vec2>& vec) : n(static_cast<int>(vec.size())), vertex(vec) {}\n\n Line side(size_t index) const { return Line(vertex[index], vertex[(index + 1) % n]); }\n\n Polygon& moveBy(const Vec2& v) {\n for (Vec2& vec : vertex) vec += v;\n return *this;\n }\n Polygon movedBy(const Vec2& v) const {\n vector<Vec2> new_vertex;\n for (const Vec2& vec : vertex) new_vertex.emplace_back(vec + v);\n\n return Polygon(new_vertex);\n }\n\n bool isConvex() const {\n for (int i = 0; i < n; ++i) {\n if (ccw(vertex[i], vertex[(i + 1) % n], vertex[(i + 2) % n]) == -1) return false;\n }\n return true;\n }\n\n bool isOnEdge(const Vec2& v) const {\n for (int i = 0; i < n; ++i) {\n if (ccw(vertex[i], vertex[(i + 1) % n], v) == 0) return true;\n }\n return false;\n }\n\n bool intersects(const Vec2& v) const {\n floating_point_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += v.arg(vertex[i], vertex[(i + 1) % n]);\n }\n\n return sgn(sum - 2.0 * Pi) == 0;\n }\n\n double area() const {\n value_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += vertex[i].cross(vertex[i == n - 1 ? 0 : i + 1]);\n }\n\n return 0.5 * fabs(sum);\n }\n\n value_type commonArea(const Circle& circle) const {\n if (n < 3) return 0.0;\n\n if (!same(circle.center.x, 0.0) || !same(circle.center.y, 0.0)) {\n return (*this).movedBy(-circle.center).commonArea(Circle(circle.r));\n }\n\n auto cross_area = [&](auto self, Circle c, Vec2 a, Vec2 b) -> value_type {\n const Vec2 va = c.center - a, vb = c.center - b;\n\n value_type f = va.cross(vb), res = 0.0;\n\n if (same(f, 0.0)) return 0.0;\n if (sgn(max(va.length(), vb.length()) - c.r) <= 0) return f;\n\n const Line line(a, b);\n\n if (sgn(segment::distance(line, c.center) - c.r) >= 0) {\n return c.r * c.r * va.arg(vb);\n }\n\n const auto opt = c.intersectsAt(line);\n vector<Vec2> vec1 = opt.value();\n\n const vector<Vec2> vec2{a, vec1.front(), vec1.back(), b};\n\n for (int i = 0; i + 1 < vec2.size(); ++i) {\n res += self(self, c, vec2[i], vec2[i + 1]);\n }\n\n return res;\n };\n\n value_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += cross_area(cross_area, circle, vertex[i], vertex[(i + 1) % n]);\n }\n\n sum *= 0.5;\n\n return fabs(sum);\n }\n\n Polygon convexHull() const { return convex_hull(vertex); }\n\n Polygon convexCut(const Line& line) const {\n assert(isConvex());\n\n vector<Vec2> res;\n\n for (int i = 0; i < n; ++i) {\n const Vec2 &now = vertex[i], &nxt = vertex[(i + 1) % n];\n\n if (ccw(line.begin, line.end, now) != -1) res.emplace_back(now);\n if (ccw(line.begin, line.end, now) * ccw(line.begin, line.end, nxt) < 0) {\n const auto& opt = Line(now, nxt).intersectsAt(line);\n if (opt.has_value()) res.emplace_back(opt.value());\n }\n }\n\n return Polygon(res);\n }\n\n value_type diameter() const {\n const auto convex = (isConvex() ? (*this) : convexHull()).vertex;\n\n const size_t n = convex.size();\n\n if (n == 2)\n return convex[0].distanceFrom(convex[1]);\n else {\n size_t i = 0, j = 0;\n\n for (size_t k = 0; k < n; ++k) {\n if (!(convex[i] < convex[k])) i = k;\n if (convex[j] < convex[k]) j = k;\n }\n\n value_type res = 0.0;\n\n const size_t si = i, sj = j;\n\n while (i != sj || j != si) {\n res = max(res, convex[i].distanceFrom(convex[j]));\n\n if ((convex[(i + 1) % n] - convex[i]).cross(convex[(j + 1) % n] - convex[j]) < 0) {\n i = (i + 1) % n;\n } else\n j = (j + 1) % n;\n }\n\n return res;\n }\n }\n\n friend istream& operator>>(istream& is, Polygon& rhs) {\n for (Vec2& e : rhs.vertex) is >> e;\n return is;\n }\n};\n\n// x座標でsort済み\nfloating_point_type distance_closest_pair(vector<Vec2>& points, int left, int right) {\n if (right - left <= 1) return 1e20;\n\n int mid = (left + right) / 2;\n floating_point_type x = points[mid].x;\n floating_point_type d = min(distance_closest_pair(points, left, mid), distance_closest_pair(points, mid, right));\n\n inplace_merge(points.begin() + left, points.begin() + mid, points.begin() + right,\n [](const Vec2& a, const Vec2& b) { return sgn(a.y - b.y) < 0; });\n\n vector<Vec2> a;\n\n for (int i = left; i < right; ++i) {\n if (sgn(fabs(points[i].x - x) - d) >= 0) continue;\n\n for (int j = static_cast<int>(a.size()) - 1; j >= 0; --j) {\n if (sgn(fabs((points[i] - a[j]).y) - d) >= 0) break;\n d = min(d, points[i].distanceFrom(a[j]));\n }\n\n a.emplace_back(points[i]);\n }\n\n return d;\n}\n\n// 垂直または水平線分の交点\n// begin < end\nvector<Vec2> intersections(const vector<Line>& lines) {\n const int n = static_cast<int>(lines.size());\n\n vector<pair<floating_point_type, int>> events;\n\n for (int i = 0; i < n; ++i) {\n const Line& line = lines[i];\n\n if (same(line.begin.y, line.end.y)) { // 水平\n events.emplace_back(make_pair(line.begin.x, i));\n events.emplace_back(make_pair(line.end.x, i + 2 * n));\n } else { // 垂直\n events.emplace_back(make_pair(line.begin.x, i + n));\n }\n }\n\n sort(events.begin(), events.end());\n\n set<floating_point_type> y_list;\n vector<Vec2> res;\n\n for (int i = 0; i < static_cast<int>(events.size()); ++i) {\n const int kind = events[i].second;\n const int id = kind % n;\n const Line& line = lines[id];\n\n if (kind < n) { // 左端\n y_list.insert(line.begin.y);\n } else if (kind < 2 * n) { // 垂直\n for (auto y : y_list) {\n if (line.begin.y - EPS <= y && y <= line.end.y + EPS) {\n res.emplace_back(Vec2(line.begin.x, y));\n }\n }\n } else { // 右端\n y_list.erase(line.end.y);\n }\n }\n\n return res;\n}\n} // namespace geometry\n\nusing namespace geometry;\nusing Point = Vector2D<ll>;\nvoid solve(int n) {\n vector<Point> p(n);\n for (auto& e : p) cin >> e;\n vector<Point> p2(2 * n);\n rep(i, n) p2[i] = p2[i + n] = p[i];\n auto c = convex_hull(p);\n int m = c.size();\n vector<int> ind(m);\n rep(i, m) {\n rep(j, n) {\n if (c[i] == p[j]) {\n ind[i] = j;\n break;\n }\n }\n }\n rep(i, m) {\n int a = ind[i], b = ind[(i + 1) % m];\n if (a > b) b += n;\n if (a + 1 == b) continue;\n\n int cc1 = ccw(p2[a], p2[a + 1], p2[b]);\n int cc2 = ccw(p2[a], p2[b - 1], p2[b]);\n\n if ((cc1 == 1 || cc1 == -1) || (cc2 == 1 || cc2 == -1)) {\n cout << \"No\\n\";\n return;\n }\n }\n cout << \"Yes\\n\";\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m, k, p, q;\n string s;\n while (cin >> n) {\n if (n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3520, "score_of_the_acc": -0.0247, "final_rank": 7 }, { "submission_id": "aoj_1669_9104989", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\ntemplate < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T > p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nbool solve(int n) {\n vector<point<int>> p = in(n);\n vector<point<int>> c = convex_hull(p);\n const int m = c.size();\n\n auto is_on = [&](point<int> a, point<int> b, point<int> c) {\n return abs(ccw(a, b, c)) != 1;\n };\n\n for(int j : rep(m)) {\n point<int> a = c[j], b = c[(j + 1) % m];\n int cnt = 0;\n for(int i : rep(n)) {\n if(is_on(a, b, p[i])) cnt += 1;\n }\n for(int i : rep(n)) {\n if(is_on(a, b, p[i]) and is_on(a, b, p[(i + 1) % n])) {\n cnt -= 2;\n }\n }\n if(cnt >= 1) return false;\n }\n return true;\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) return 0;\n print(solve(n) ? \"Yes\" : \"No\");\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3468, "score_of_the_acc": -0.0168, "final_rank": 6 } ]
aoj_1670_cpp
Problem G Fair Deal of Dice You are serving as the gamemaster of a two-player game in which several cubic dice are used. Each face of the dice has a number on it. Unlike conventional dice, the numbers are not necessarily one through six. Before starting a game, you select a predefined number of dice from the set of dice at hand and deal them out to the two players. Here, the numbers of dice the two players receive are not necessarily the same, but both players receive at least one die. Both of the players cast all of the dice dealt in some situations of the game. At that time, the greater the sum of the numbers on the top faces of the dice of a player is, the more advantage the player gains. For more exciting games, selecting and dealing the dice in a way that achieves the least unfairness is the best. Definition of the unfairness: Let x denote the sum of the numbers on the top of the dice of one player, and y denote that of the other player. The unfairness is defined as the expected value of ( x − y ) 2 . Here, each side of a die comes to the top with probability 1/6 independently of the other dice. Please compute the minimum achievable unfairness with the dice at hand. Input The input consists of multiple datasets, each in the following format. n m a 1,1 a 1,2 a 1,3 a 1,4 a 1,5 a 1,6 ⋮ a n ,1 a n ,2 a n ,3 a n ,4 a n ,5 a n ,6 n is the number of dice you have, and m is the number of dice you select to deal to players. n and m are integers that satisfy 2 ≤ m ≤ n ≤ 60. a i , j gives the number on the j -th face of the i -th die. a i , j is an integer that satisfies 0 ≤ a i , j ≤ 60. The end of the input is indicated by a line consisting of two zeros. The number of datasets does not exceed 100. Output For each dataset, output the minimum achievable value of the unfairness multiplied by 36 in a line. Note that this value can be proved to be an integer. Sample Input 4 3 1 2 3 4 5 6 10 10 10 20 20 20 10 11 12 13 14 15 60 0 60 0 60 0 2 2 0 0 0 0 0 0 60 60 60 60 60 60 8 6 58 43 60 42 53 37 13 41 55 4 21 50 35 13 54 43 24 5 40 60 57 48 60 32 51 32 58 44 60 52 40 29 25 22 0 44 31 22 26 30 49 23 16 34 4 40 21 6 0 0 Output for the Sample Input 1146 129600 31878
[ { "submission_id": "aoj_1670_10567721", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1000000000;\nconstexpr int64_t llINF = 3000000000000000000;\nconstexpr double eps = 1e-10;\nconst double pi = acos(-1);\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\nll extgcd(ll a, ll b, ll& x, ll& y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nll modpow(ll a, ll b, ll m) {\n ll res = 1;\n while (b) {\n if (b & 1) {\n res *= a;\n res %= m;\n }\n a *= a;\n a %= m;\n b >>= 1;\n }\n return res;\n}\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint& operator+=(const modint& p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint& operator-=(const modint& p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint& operator*=(const modint& p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint& operator/=(const modint& p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint& p) const { return modint(*this) += p; }\n modint operator-(const modint& p) const { return modint(*this) -= p; }\n modint operator*(const modint& p) const { return modint(*this) *= p; }\n modint operator/(const modint& p) const { return modint(*this) /= p; }\n bool operator==(const modint& p) const { return x == p.x; }\n bool operator!=(const modint& p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream& operator<<(ostream& os, const modint& p) { return os << p.x; }\n friend istream& operator>>(istream& is, modint& a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\nnamespace geometry {\n\nusing floating_point_type = long double;\n\nconstexpr floating_point_type EPS = 1e-13;\nconstexpr floating_point_type Pi = 3.141592653589793;\n\nconstexpr floating_point_type operator\"\" _deg(floating_point_type deg) { return static_cast<floating_point_type>(deg * Pi / 180.0); }\nconstexpr floating_point_type operator\"\" _deg(unsigned long long deg) { return static_cast<floating_point_type>(deg * Pi / 180.0); }\n\ntemplate <typename T>\nconstexpr int sgn(T a) noexcept {\n return (a < -EPS ? -1 : (a > EPS ? +1 : 0));\n}\n\ntemplate <typename T, typename U>\nconstexpr bool same(T a, U b) noexcept {\n return sgn(a - b) == 0;\n}\n\ntemplate <typename T>\nconstexpr T square(T x) noexcept {\n return x * x;\n}\n\n// ax + by + c = 0, gcd(a,b,c) = 1\n// a != 0 : a > 0\n// a = 0: b > 0\ntuple<long long, long long, long long> general_form(long long x1, long long y1, long long x2, long long y2) {\n long long a = y1 - y2, b = x2 - x1, c = x1 * y2 - x2 * y1;\n if (a < 0) a = -a, b = -b, c = -c;\n if (a == 0 && b < 0) b = -b, c = -c;\n long long g = gcd(gcd(a, b), c);\n return {a / g, b / g, c / g};\n}\n\ntemplate <typename T>\nstruct Vector2D {\n using value_type = T;\n T x, y;\n\n Vector2D() = default;\n explicit Vector2D(T arg) : x(cos(arg)), y(sin(arg)) {}\n constexpr Vector2D(T x, T y) noexcept : x(x), y(y) {}\n\n constexpr bool operator<(const Vector2D& v) const noexcept {\n if (!same(x, v.x)) return x < v.x;\n return y < v.y;\n }\n constexpr bool operator>(const Vector2D& v) const noexcept { return v < *this; }\n constexpr bool operator==(const Vector2D& v) const noexcept { return same(x, v.x) && same(y, v.y); }\n constexpr bool operator!=(const Vector2D& v) const noexcept { return !(*this == v); }\n\n constexpr Vector2D operator+() const noexcept { return *this; }\n constexpr Vector2D operator-() const noexcept { return {-x, -y}; }\n\n constexpr Vector2D operator+(const Vector2D& v) const noexcept { return {x + v.x, y + v.y}; }\n constexpr Vector2D operator-(const Vector2D& v) const noexcept { return {x - v.x, y - v.y}; }\n constexpr Vector2D operator*(T s) const noexcept { return {x * s, y * s}; }\n constexpr Vector2D operator/(T s) const noexcept { return {x / s, y / s}; }\n\n constexpr Vector2D& operator+=(const Vector2D& v) noexcept {\n x += v.x;\n y += v.y;\n return *this;\n }\n constexpr Vector2D& operator-=(const Vector2D& v) noexcept {\n x -= v.x;\n y -= v.y;\n return *this;\n }\n constexpr Vector2D& operator*=(T s) noexcept {\n x *= s;\n y *= s;\n return *this;\n }\n constexpr Vector2D& operator/=(T s) noexcept {\n x /= s;\n y /= s;\n return *this;\n }\n\n constexpr bool isZero() const noexcept { return x == T(0) && y == T(0); }\n bool hasNan() const { return isnan(x) || isnan(y); }\n\n constexpr T dot(const Vector2D& v) const noexcept { return x * v.x + y * v.y; }\n constexpr T cross(const Vector2D& v) const noexcept { return x * v.y - y * v.x; }\n\n floating_point_type length() const {\n if (isZero()) return 0.0;\n\n const T abs_x = std::abs(x);\n const T abs_y = std::abs(y);\n\n const auto [u, v] = std::minmax(abs_x, abs_y);\n const T t = u / v;\n\n return v * std::sqrt(std::fma(t, t, 1.0));\n }\n constexpr T lengthSq() const noexcept { return dot(*this); }\n\n floating_point_type distanceFrom(const Vector2D& v) const { return (v - *this).length(); }\n constexpr T distanceFromSq(const Vector2D& v) const noexcept { return (v - *this).lengthSq(); }\n\n Vector2D normalized() const { return *this / length(); }\n Vector2D& normalize() { return *this /= length(); }\n\n Vector2D rotated(floating_point_type angle) const {\n const T s = sin(angle);\n const T c = cos(angle);\n return {x * c - y * s, x * s + y * c};\n }\n Vector2D& rotate(floating_point_type angle) { return *this = rotated(angle); }\n\n floating_point_type arg() const { return atan2<floating_point_type>(y, x); }\n floating_point_type arg(const Vector2D& v) const {\n if (isZero() || v.isZero()) {\n return numeric_limits<T>::signaling_NaN();\n }\n\n return atan2<floating_point_type>(cross(v), dot(v));\n }\n floating_point_type arg(const Vector2D& a, const Vector2D& b) const { return (a - *this).arg(b - *this); }\n\n constexpr Vector2D lerp(const Vector2D& v, T f) const noexcept { return {x + (v.x - x) * f, y + (v.y - y) * f}; }\n\n constexpr friend Vector2D operator*(T s, const Vector2D& v) noexcept { return {s * v.x, s * v.y}; }\n friend istream& operator>>(istream& is, Vector2D& v) { return is >> v.x >> v.y; }\n friend ostream& operator<<(ostream& os, const Vector2D& v) { return os << \"(\" << v.x << \", \" << v.y << \")\"; }\n};\n\ntemplate <typename T>\nvoid argument_sort(vector<Vector2D<T>>& vec, const Vector2D<T>& center = Vector2D<T>(0, 0)) {\n auto orthant = [](const Vector2D<T>& v) {\n if (sgn(v.x) < 0 && sgn(v.y) < 0) return 0;\n if (sgn(v.x) >= 0 && sgn(v.y) < 0) return 1;\n if (sgn(v.x) == 0 && sgn(v.y) == 0) return 2;\n if (sgn(v.x) >= 0 && sgn(v.y) >= 0) return 3;\n return 4;\n };\n\n auto comp = [orthant, center](const Vector2D<T>& a, const Vector2D<T>& b) {\n const int o1 = orthant(a - center), o2 = orthant(b - center);\n if (o1 != o2) return o1 < o2;\n\n return (a - center).cross(b - center) > 0;\n };\n\n sort(vec.begin(), vec.end(), comp);\n}\n\ntemplate <typename T>\nvector<Vector2D<T>> convex_hull(vector<Vector2D<T>> points) {\n int n = points.size(), k = 0;\n vector<Vector2D<T>> res(n * 2);\n\n sort(points.begin(), points.end());\n points.erase(unique(points.begin(), points.end()), points.end());\n\n for (int i = 0; i < n; ++i) {\n while (k > 1 && sgn((res[k - 1] - res[k - 2]).cross(points[i] - res[k - 1])) <= 0) k--;\n res[k++] = points[i];\n }\n\n for (int i = n - 2, t = k; i >= 0; --i) {\n while (k > t && sgn((res[k - 1] - res[k - 2]).cross(points[i] - res[k - 1])) <= 0) k--;\n res[k++] = points[i];\n }\n\n res.resize(k - 1);\n return res;\n}\n\n/*\nABから見てBCは左に曲がるのなら +1\nABから見てBCは右に曲がるのなら -1\nBACの順番で一直線上に並ぶなら +2\nABCの順番で一直線上に並ぶなら -2\nCがAB上なら 0\n*/\ntemplate <typename T>\nconstexpr int ccw(const Vector2D<T>& a, const Vector2D<T>& b, const Vector2D<T>& c) {\n const int res = sgn((b - a).cross(c - a));\n\n if (res != 0) return res;\n if (sgn((a - b).dot(c - a)) > 0) return +2;\n if (sgn((b - a).dot(c - b)) > 0) return -2;\n return 0;\n}\n\nusing Vec2 = Vector2D<floating_point_type>;\n\nstruct Line {\n using value_type = Vec2::value_type;\n Vec2 begin, end;\n\n Line() = default;\n constexpr Line(const Vec2& begin, const Vec2& end) : begin(begin), end(end) {}\n constexpr Line(value_type sx, value_type sy, value_type gx, value_type gy) : begin(sx, sy), end(gx, gy) {}\n\n // ax + by + c = 0\n Line(value_type a, value_type b, value_type c) {\n assert(sgn(a) != 0 || sgn(b) != 0);\n\n if (sgn(b) == 0) {\n begin = Vec2(-c / a, 0.0);\n end = Vec2(-c / a, 1.0);\n } else {\n begin = Vec2(0, -c / b);\n end = Vec2(1.0, -(a + c) / b);\n }\n }\n\n constexpr Vec2 vector() const { return end - begin; }\n\n constexpr Line& reverse() {\n const Vec2 tmp = begin;\n begin = end;\n end = tmp;\n return *this;\n }\n constexpr Line reversed() const { return {end, begin}; }\n\n constexpr Line& moveBy(const Vec2& v) {\n begin += v;\n end += v;\n return *this;\n }\n constexpr Line movedBy(const Vec2& v) const { return {begin + v, end + v}; }\n\n constexpr bool isParallel(const Line& line) const { return sgn(vector().cross(line.vector())) == 0; }\n constexpr bool isOrthogonal(const Line& line) const { return sgn(vector().dot(line.vector())) == 0; }\n\n floating_point_type length() const { return vector().length(); }\n constexpr value_type lengthSq() const { return vector().lengthSq(); }\n\n floating_point_type distanceFrom(const Vec2& v) const { return fabs(vector().cross(v - begin)) / length(); }\n constexpr value_type distanceFromSq(const Vec2& v) const {\n const value_type d = vector().cross(v - begin);\n\n return d / lengthSq() * d;\n }\n\n optional<Vec2> intersectsAt(const Line& line) const {\n if (sgn(vector().cross(line.vector())) == 0) return nullopt;\n\n return begin + vector() * fabs((line.end - begin).cross(line.vector()) / vector().cross(line.vector()));\n }\n\n constexpr Vec2 projection(const Vec2& v) const { return begin + vector() * (v - begin).dot(vector()) / lengthSq(); }\n constexpr Vec2 reflection(const Vec2& v) const { return 2 * projection(v) - v; }\n\n // ax + by + c = 0\n constexpr tuple<value_type, value_type, value_type> generalForm() const {\n const Vec2 vec = vector();\n return {vec.y, -vec.x, end.cross(begin)};\n }\n\n friend istream& operator>>(istream& is, Line& rhs) { return is >> rhs.begin >> rhs.end; }\n};\n\nnamespace segment {\n\nusing Segment = Line;\n\nconstexpr bool intersect(const Segment& a, const Segment& b) {\n return ccw(a.begin, a.end, b.begin) * ccw(a.begin, a.end, b.end) <= 0 && ccw(b.begin, b.end, a.begin) * ccw(b.begin, b.end, a.end) <= 0;\n}\n\nfloating_point_type distance(const Segment& a, const Vec2& v) {\n if (sgn(a.vector().dot(v - a.begin)) < 0 || sgn((-a.vector()).dot(v - a.end)) < 0) {\n return min(v.distanceFrom(a.begin), v.distanceFrom(a.end));\n }\n return a.distanceFrom(v);\n}\n\nconstexpr floating_point_type distanceSq(const Segment& a, const Vec2& v) {\n if (sgn(a.vector().dot(v - a.begin)) < 0 || sgn((-a.vector()).dot(v - a.end)) < 0) {\n return min(v.distanceFromSq(a.begin), v.distanceFromSq(a.end));\n }\n return a.distanceFromSq(v);\n}\n\nfloating_point_type distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0.0;\n\n return min({distance(a, b.begin), distance(a, b.end), distance(b, a.begin), distance(b, a.end)});\n}\n} // namespace segment\n\nstruct Triangle {\n using value_type = Vec2::value_type;\n Vec2 p0, p1, p2;\n\n Triangle() = default;\n constexpr Triangle(const Vec2& p0, const Vec2& p1, const Vec2& p2) : p0(p0), p1(p1), p2(p2) {}\n\n constexpr Vec2& p(size_t index) { return (&p0)[index]; }\n constexpr const Vec2& p(size_t index) const { return (&p0)[index]; }\n\n constexpr Line side(size_t index) const {\n if (index == 0)\n return Line(p1, p2);\n else if (index == 1)\n return Line(p2, p0);\n else if (index == 2)\n return Line(p0, p1);\n else {\n throw std::out_of_range(\"Triangle::side() index out of range\");\n }\n }\n\n floating_point_type angle(size_t index) const {\n const Vec2 vec[3] = {p(index), p((index + 1) % 3), p((index + 2) % 3)};\n\n const value_type res = vec[0].arg(vec[1], vec[2]);\n\n return fabs(res);\n }\n\n constexpr value_type area() const {\n const value_type res = (p1 - p0).cross(p2 - p0) / 2;\n return (0 < res ? res : -res);\n }\n\n constexpr Vec2 circumCenter() const {\n const value_type d0 = side(0).lengthSq(), d1 = side(1).lengthSq(), d2 = side(2).lengthSq(), t0 = d0 * (-d0 + d1 + d2),\n t1 = d1 * (+d0 - d1 + d2), t2 = d2 * (+d0 + d1 - d2), t3 = t0 + t1 + t2;\n\n return t0 / t3 * p0 + t1 / t3 * p1 + t2 / t3 * p2;\n }\n\n Vec2 innerCenter() const {\n const value_type dist[3] = {side(0).length(), side(1).length(), side(2).length()};\n\n return (dist[0] * p0 + dist[1] * p1 + dist[2] * p2) / (dist[0] + dist[1] + dist[2]);\n }\n\n friend istream& operator>>(istream& is, Triangle& rhs) { return is >> rhs.p0 >> rhs.p1 >> rhs.p2; }\n};\n\nstruct Circle {\n using value_type = Vec2::value_type;\n Vec2 center;\n value_type r;\n\n Circle() = default;\n explicit constexpr Circle(value_type r) : center(0.0, 0.0), r(r) {}\n constexpr Circle(value_type x, value_type y, value_type r) : center(x, y), r(r) {}\n constexpr Circle(const Vec2& center, value_type r) : center(center), r(r) {}\n\n constexpr bool intersects(const Line& line) const { return sgn(line.distanceFromSq(center) - r * r) <= 0; }\n\n constexpr bool intersects(const Circle& circle) const {\n const value_type d = center.distanceFromSq(circle.center);\n\n return sgn(square(r - circle.r) - d) <= 0 && sgn(d - square(r + circle.r)) <= 0;\n }\n\n constexpr bool circumcribes(const Circle& circle) const { return same(center.distanceFromSq(circle.center), square(r + circle.r)); }\n\n constexpr bool inscribes(const Circle& circle) const { return same(center.distanceFromSq(circle.center), square(r - circle.r)); }\n\n constexpr bool contains(const Circle& circle) const {\n return 0 < sgn(square(r - circle.r) - center.distanceFromSq(circle.center)) && 0 < sgn(r - circle.r);\n }\n\n constexpr value_type area() const { return Pi * r * r; }\n\n value_type commonArea(const Circle& circle) const {\n const value_type d_sq = center.distanceFromSq(circle.center);\n const value_type d = center.distanceFrom(circle.center);\n\n const value_type r1 = r;\n const value_type r2 = circle.r;\n\n if (sgn(d_sq - square(r1 + r2)) >= 0) return 0.0;\n if (sgn(d_sq - square(r1 - r2)) <= 0) return min(this->area(), circle.area());\n\n const value_type arg1 = acos(((r1 - r2) * (r1 + r2) + d_sq) / (2 * r1 * d));\n const value_type arg2 = acos(((r2 - r1) * (r2 + r1) + d_sq) / (2 * r2 * d));\n\n return r1 * r1 * (arg1 - sin(2 * arg1) / 2) + r2 * r2 * (arg2 - sin(2 * arg2) / 2);\n }\n\n constexpr Circle& moveBy(const Vec2& v) {\n center += v;\n return *this;\n }\n constexpr Circle movedBy(const Vec2& v) const { return {center + v, r}; }\n\n optional<vector<Vec2>> intersectsAt(const Line& line) const {\n if (!intersects(line)) return nullopt;\n\n vector<Vec2> res;\n\n const Vec2 v = line.projection(center);\n\n if (same(line.distanceFrom(center), r))\n res.emplace_back(v);\n else {\n value_type d = sqrt(r * r - (v - center).lengthSq());\n const Vec2 e = line.vector().normalized();\n\n res.emplace_back(v - d * e);\n res.emplace_back(v + d * e);\n }\n\n return res;\n }\n\n optional<vector<Vec2>> intersectsAt(const Circle& circle) const {\n if (!intersects(circle)) return nullopt;\n\n const value_type d = center.distanceFrom(circle.center), arg = (circle.center - center).arg(),\n theta = acos((d * d + r * r - circle.r * circle.r) / (2 * d * r));\n\n vector<Vec2> res;\n res.emplace_back(center + r * Vec2(arg + theta));\n res.emplace_back(center + r * Vec2(arg - theta));\n\n if (res[0] > res[1]) swap(res[0], res[1]);\n\n return res;\n }\n\n optional<vector<Vec2>> tangentPointFrom(const Vec2& v) const {\n const value_type d = center.distanceFromSq(v);\n\n if (sgn(d - r * r) < 0) return nullopt;\n if (same(d, r * r)) return vector<Vec2>{v};\n\n const auto [x1, y1] = v - center;\n\n const auto& opt = Circle(r).intersectsAt(Line(x1, y1, -r * r));\n\n vector<Vec2> res = opt.value();\n\n for (Vec2& v : res) v += center;\n\n if (res[0] > res[1]) swap(res[0], res[1]);\n\n return res;\n }\n optional<vector<Vec2>> commonTangentPointFrom(const Circle& circle) const {\n const value_type r1 = r, r2 = circle.r;\n\n vector<Vec2> res;\n\n if (!same(r1, r2)) {\n const Vec2 p((r2 * center - r1 * circle.center) / (r2 - r1));\n\n const auto& opt = tangentPointFrom(p);\n\n if (opt.has_value()) {\n for (const Vec2& vec : opt.value()) {\n res.emplace_back(vec);\n }\n }\n } else {\n const value_type theta = (circle.center - center).arg();\n\n res.emplace_back(center + r1 * Vec2(theta + 90_deg));\n res.emplace_back(center + r1 * Vec2(theta - 90_deg));\n }\n\n const Vec2 q((r2 * center + r1 * circle.center) / (r1 + r2));\n\n const auto& opt = tangentPointFrom(q);\n\n if (opt.has_value()) {\n for (const Vec2& vec : opt.value()) {\n res.emplace_back(vec);\n }\n }\n\n if (res.empty()) return nullopt;\n\n sort(res.begin(), res.end());\n\n return res;\n }\n\n friend istream& operator>>(istream& is, Circle& rhs) { return is >> rhs.center >> rhs.r; }\n};\n\nstruct Polygon {\n using value_type = Vec2::value_type;\n int n;\n vector<Vec2> vertex;\n\n Polygon() = default;\n Polygon(int _n) : n(_n), vertex(_n) {}\n Polygon(const vector<Vec2>& vec) : n(static_cast<int>(vec.size())), vertex(vec) {}\n\n Line side(size_t index) const { return Line(vertex[index], vertex[(index + 1) % n]); }\n\n Polygon& moveBy(const Vec2& v) {\n for (Vec2& vec : vertex) vec += v;\n return *this;\n }\n Polygon movedBy(const Vec2& v) const {\n vector<Vec2> new_vertex;\n for (const Vec2& vec : vertex) new_vertex.emplace_back(vec + v);\n\n return Polygon(new_vertex);\n }\n\n bool isConvex() const {\n for (int i = 0; i < n; ++i) {\n if (ccw(vertex[i], vertex[(i + 1) % n], vertex[(i + 2) % n]) == -1) return false;\n }\n return true;\n }\n\n bool isOnEdge(const Vec2& v) const {\n for (int i = 0; i < n; ++i) {\n if (ccw(vertex[i], vertex[(i + 1) % n], v) == 0) return true;\n }\n return false;\n }\n\n bool intersects(const Vec2& v) const {\n floating_point_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += v.arg(vertex[i], vertex[(i + 1) % n]);\n }\n\n return sgn(sum - 2.0 * Pi) == 0;\n }\n\n double area() const {\n value_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += vertex[i].cross(vertex[i == n - 1 ? 0 : i + 1]);\n }\n\n return 0.5 * fabs(sum);\n }\n\n value_type commonArea(const Circle& circle) const {\n if (n < 3) return 0.0;\n\n if (!same(circle.center.x, 0.0) || !same(circle.center.y, 0.0)) {\n return (*this).movedBy(-circle.center).commonArea(Circle(circle.r));\n }\n\n auto cross_area = [&](auto self, Circle c, Vec2 a, Vec2 b) -> value_type {\n const Vec2 va = c.center - a, vb = c.center - b;\n\n value_type f = va.cross(vb), res = 0.0;\n\n if (same(f, 0.0)) return 0.0;\n if (sgn(max(va.length(), vb.length()) - c.r) <= 0) return f;\n\n const Line line(a, b);\n\n if (sgn(segment::distance(line, c.center) - c.r) >= 0) {\n return c.r * c.r * va.arg(vb);\n }\n\n const auto opt = c.intersectsAt(line);\n vector<Vec2> vec1 = opt.value();\n\n const vector<Vec2> vec2{a, vec1.front(), vec1.back(), b};\n\n for (int i = 0; i + 1 < vec2.size(); ++i) {\n res += self(self, c, vec2[i], vec2[i + 1]);\n }\n\n return res;\n };\n\n value_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += cross_area(cross_area, circle, vertex[i], vertex[(i + 1) % n]);\n }\n\n sum *= 0.5;\n\n return fabs(sum);\n }\n\n Polygon convexHull() const { return convex_hull(vertex); }\n\n Polygon convexCut(const Line& line) const {\n assert(isConvex());\n\n vector<Vec2> res;\n\n for (int i = 0; i < n; ++i) {\n const Vec2 &now = vertex[i], &nxt = vertex[(i + 1) % n];\n\n if (ccw(line.begin, line.end, now) != -1) res.emplace_back(now);\n if (ccw(line.begin, line.end, now) * ccw(line.begin, line.end, nxt) < 0) {\n const auto& opt = Line(now, nxt).intersectsAt(line);\n if (opt.has_value()) res.emplace_back(opt.value());\n }\n }\n\n return Polygon(res);\n }\n\n value_type diameter() const {\n const auto convex = (isConvex() ? (*this) : convexHull()).vertex;\n\n const size_t n = convex.size();\n\n if (n == 2)\n return convex[0].distanceFrom(convex[1]);\n else {\n size_t i = 0, j = 0;\n\n for (size_t k = 0; k < n; ++k) {\n if (!(convex[i] < convex[k])) i = k;\n if (convex[j] < convex[k]) j = k;\n }\n\n value_type res = 0.0;\n\n const size_t si = i, sj = j;\n\n while (i != sj || j != si) {\n res = max(res, convex[i].distanceFrom(convex[j]));\n\n if ((convex[(i + 1) % n] - convex[i]).cross(convex[(j + 1) % n] - convex[j]) < 0) {\n i = (i + 1) % n;\n } else\n j = (j + 1) % n;\n }\n\n return res;\n }\n }\n\n friend istream& operator>>(istream& is, Polygon& rhs) {\n for (Vec2& e : rhs.vertex) is >> e;\n return is;\n }\n};\n\n// x座標でsort済み\nfloating_point_type distance_closest_pair(vector<Vec2>& points, int left, int right) {\n if (right - left <= 1) return 1e20;\n\n int mid = (left + right) / 2;\n floating_point_type x = points[mid].x;\n floating_point_type d = min(distance_closest_pair(points, left, mid), distance_closest_pair(points, mid, right));\n\n inplace_merge(points.begin() + left, points.begin() + mid, points.begin() + right,\n [](const Vec2& a, const Vec2& b) { return sgn(a.y - b.y) < 0; });\n\n vector<Vec2> a;\n\n for (int i = left; i < right; ++i) {\n if (sgn(fabs(points[i].x - x) - d) >= 0) continue;\n\n for (int j = static_cast<int>(a.size()) - 1; j >= 0; --j) {\n if (sgn(fabs((points[i] - a[j]).y) - d) >= 0) break;\n d = min(d, points[i].distanceFrom(a[j]));\n }\n\n a.emplace_back(points[i]);\n }\n\n return d;\n}\n\n// 垂直または水平線分の交点\n// begin < end\nvector<Vec2> intersections(const vector<Line>& lines) {\n const int n = static_cast<int>(lines.size());\n\n vector<pair<floating_point_type, int>> events;\n\n for (int i = 0; i < n; ++i) {\n const Line& line = lines[i];\n\n if (same(line.begin.y, line.end.y)) { // 水平\n events.emplace_back(make_pair(line.begin.x, i));\n events.emplace_back(make_pair(line.end.x, i + 2 * n));\n } else { // 垂直\n events.emplace_back(make_pair(line.begin.x, i + n));\n }\n }\n\n sort(events.begin(), events.end());\n\n set<floating_point_type> y_list;\n vector<Vec2> res;\n\n for (int i = 0; i < static_cast<int>(events.size()); ++i) {\n const int kind = events[i].second;\n const int id = kind % n;\n const Line& line = lines[id];\n\n if (kind < n) { // 左端\n y_list.insert(line.begin.y);\n } else if (kind < 2 * n) { // 垂直\n for (auto y : y_list) {\n if (line.begin.y - EPS <= y && y <= line.end.y + EPS) {\n res.emplace_back(Vec2(line.begin.x, y));\n }\n }\n } else { // 右端\n y_list.erase(line.end.y);\n }\n }\n\n return res;\n}\n} // namespace geometry\n\nusing namespace geometry;\nusing Point = Vector2D<ll>;\nvoid solve(int n, int m) {\n vector<vector<int>> a(n, vector<int>(6));\n rep(i, n) rep(j, 6) cin >> a[i][j];\n const int S = 360 * n;\n vector<vector<ll>> dp(m + 1, vector<ll>(S * 2 + 1, llINF));\n dp[0][S] = 0;\n rep(i, n) {\n int sum = 0, sqsum = 0;\n rep(j, 6) {\n sum += a[i][j];\n sqsum += a[i][j] * a[i][j];\n }\n sqsum *= 6;\n for (int c = m - 1; c >= 0; c--) {\n rep(s, S * 2 + 1) {\n if (dp[c][s] == llINF) continue;\n chmin(dp[c + 1][s + sum], dp[c][s] + sqsum + 2 * (s - S) * sum);\n chmin(dp[c + 1][s - sum], dp[c][s] + sqsum - 2 * (s - S) * sum);\n }\n }\n }\n cout << *min_element(all(dp[m])) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m, k, p, q;\n string s;\n while (cin >> n >> m) {\n if (n == 0 && m == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 6530, "memory_kb": 24060, "score_of_the_acc": -0.9611, "final_rank": 13 }, { "submission_id": "aoj_1670_10516353", "code_snippet": "// AOJ #1670 Fair Deal of Dice\n// 2025.5.22\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = 1000000000;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nint main(){\n while (true) {\n int n = Cin(), m = Cin();\n if (n==0) break;\n vector<int> w(n), d(n);\n for (int i = 0; i < n; i++) {\n int s1=0, s2=0;\n for (int j = 0; j < 6; j++) {\n int a = Cin();\n s1 += a;\n s2 += a*a;\n }\n w[i] = s1;\n d[i] = 6*s2 - s1*s1;\n }\n vector<int> L(m+1, INF), R(m+1, -INF);\n vector<vector<int>> st(m+1), md(m+1), ed(m+1);\n\n L[0] = R[0] = 0;\n st[0] = {0};\n ed[0] = {0};\n md[0] = {INF};\n\n for (int i = 0; i < n; i++) {\n int wi = w[i], di = d[i];\n for (int k = min(i+1, m); k >= 1; k--) {\n if (L[k-1] > R[k-1]) continue;\n int l0 = L[k-1], r0 = R[k-1];\n int l1 = L[k], r1 = R[k];\n\n int nl = min({l1, l0 + wi, l0 - wi});\n int nr = max({r1, r0 + wi, r0 - wi});\n int sz = nr - nl + 1;\n\n vector<int> nst(sz, INF), nmd(sz, INF), ned(sz, INF);\n\n if (l1 <= r1) {\n for (int D = l1; D <= r1; D++) {\n int idx_old = D - l1;\n int idx_new = D - nl;\n nst[idx_new] = min(nst[idx_new], st[k][idx_old]);\n nmd[idx_new] = min(nmd[idx_new], md[k][idx_old]);\n ned[idx_new] = min(ned[idx_new], ed[k][idx_old]);\n }\n }\n\n for (int D0 = l0; D0 <= r0; D0++) {\n int io = D0 - l0;\n int cst = st[k-1][io];\n int cmd = md[k-1][io];\n int ced = ed[k-1][io];\n {\n int Dn = D0 - wi;\n int in = Dn - nl;\n if (cst < INF) nst[in] = min(nst[in], cst + di);\n if (cmd < INF) nmd[in] = min(nmd[in], cmd + di);\n if (ced < INF) nmd[in] = min(nmd[in], ced + di);\n }\n {\n int Dn = D0 + wi;\n int in = Dn - nl;\n if (cst < INF) {\n if (k == 1) ned[in] = min(ned[in], cst + di);\n else nmd[in] = min(nmd[in], cst + di);\n }\n if (cmd < INF) nmd[in] = min(nmd[in], cmd + di);\n if (ced < INF) ned[in] = min(ned[in], ced + di);\n }\n }\n\n L[k] = nl; R[k] = nr;\n st[k].swap(nst);\n md[k].swap(nmd);\n ed[k].swap(ned);\n }\n }\n\n ll ans = LLONG_MAX;\n for (int D = L[m]; D <= R[m]; D++) {\n int v = md[m][D - L[m]];\n if (v == INF) continue;\n ans = min(ans, (ll)D*D + v);\n }\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4120, "memory_kb": 14880, "score_of_the_acc": -0.5353, "final_rank": 8 }, { "submission_id": "aoj_1670_10471649", "code_snippet": "#include <array>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nint solve(int N, int M, const vector<array<int, 6> >& A) {\n\t// step #1. preparation\n\tvector<int> S(N), V(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < 6; j++) {\n\t\t\tS[i] += A[i][j];\n\t\t}\n\t\tfor (int j = 0; j < 6; j++) {\n\t\t\tint x = A[i][j] * 6 - S[i];\n\t\t\tV[i] += x * x;\n\t\t}\n\t\tV[i] /= 6;\n\t}\n\n\t// step #2. dynamic programming\n\tvector<vector<int> > dp = { vector<int>({0}) };\n\tfor (int i = 0; i < N; i++) {\n\t\tvector<vector<int> > ndp(i + 2, vector<int>(dp[0].size() + 2 * S[i], INF));\n\t\tfor (int j = 0; j <= i; j++) {\n\t\t\tfor (int k = 0; k < int(dp[0].size()); k++) {\n\t\t\t\tif (dp[j][k] != INF) {\n\t\t\t\t\tndp[j][k + S[i]] = min(ndp[j][k + S[i]], dp[j][k]);\n\t\t\t\t\tndp[j + 1][k] = min(ndp[j + 1][k], dp[j][k] + V[i]);\n\t\t\t\t\tndp[j + 1][k + 2 * S[i]] = min(ndp[j + 1][k + 2 * S[i]], dp[j][k] + V[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdp = ndp;\n\t}\n\n\t// step #3. compute answer\n\tconst int C = (dp[0].size() - 1) / 2;\n\tint ans = INF;\n\tfor (int i = 0; i <= 2 * C; i++) {\n\t\tif (dp[M][i] != INF) {\n\t\t\tint score = dp[M][i] + (i - C) * (i - C);\n\t\t\tans = min(ans, score);\n\t\t}\n\t}\n\n\treturn ans;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<array<int, 6> > A(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < 6; j++) {\n\t\t\t\tcin >> A[i][j];\n\t\t\t}\n\t\t}\n\t\tint ans = solve(N, M, A);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3630, "memory_kb": 21068, "score_of_the_acc": -0.5019, "final_rank": 7 }, { "submission_id": "aoj_1670_9449021", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<pair<int,int>> D(N);\n rep(i,0,N) {\n int A[6], SUM = 0, SUM2 = 0;\n rep(j,0,6) cin >> A[j], SUM += A[j];\n rep(j,0,6) SUM2 += (SUM - A[j] * 6) * (SUM - A[j] * 6);\n D[i] = {SUM, SUM2 / 6};\n }\n int MAXS = 360 * N + 1;\n static ll DP[2][61][21601] = {};\n rep(i,0,2) {\n rep(j,0,N+1) {\n rep(k,0,MAXS) DP[i][j][k] = INF;\n }\n }\n DP[0][0][0] = 0;\n rep(i,0,N) {\n rep(j,0,N+1) {\n rep(k,0,360*(i+1)+1) DP[(i&1)^1][j][k] = INF;\n }\n rep(j,0,N+1) {\n rep(k,0,360*i+1) {\n if (DP[i&1][j][k] == INF) continue;\n chmin(DP[(i&1)^1][j+1][k+D[i].first], DP[i&1][j][k]+D[i].second);\n chmin(DP[(i&1)^1][j+1][abs(k-D[i].first)], DP[i&1][j][k]+D[i].second);\n chmin(DP[(i&1)^1][j][k], DP[i&1][j][k]);\n }\n }\n }\n ll ANS = INF;\n for (ll i = 0; i < MAXS; i++) {\n chmin(ANS, i*i+DP[N&1][M][i]);\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 4370, "memory_kb": 24092, "score_of_the_acc": -0.634, "final_rank": 9 }, { "submission_id": "aoj_1670_9449015", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nstatic ll DP[2][61][21601] = {};\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<pair<int,int>> D(N);\n rep(i,0,N) {\n int A[6], SUM = 0, SUM2 = 0;\n rep(j,0,6) cin >> A[j], SUM += A[j];\n rep(j,0,6) SUM2 += (SUM - A[j] * 6) * (SUM - A[j] * 6);\n D[i] = {SUM, SUM2 / 6};\n }\n int MAXS = 360 * N + 1;\n rep(i,0,2) {\n rep(j,0,N+1) {\n rep(k,0,MAXS) DP[i][j][k] = INF;\n }\n }\n DP[0][0][0] = 0;\n rep(i,0,N) {\n rep(j,0,i+1) {\n rep(k,0,MAXS) DP[(i&1)^1][j][k] = INF;\n }\n rep(j,0,i+1) {\n rep(k,0,MAXS) {\n if (DP[i&1][j][k] == INF) continue;\n chmin(DP[(i&1)^1][j+1][k+D[i].first], DP[i&1][j][k]+D[i].second);\n chmin(DP[(i&1)^1][j+1][abs(k-D[i].first)], DP[i&1][j][k]+D[i].second);\n chmin(DP[(i&1)^1][j][k], DP[i&1][j][k]);\n }\n }\n }\n ll ANS = INF;\n for (ll i = 0; i < MAXS; i++) {\n chmin(ANS, i*i+DP[N&1][M][i]);\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 4430, "memory_kb": 24004, "score_of_the_acc": -0.6425, "final_rank": 10 }, { "submission_id": "aoj_1670_9449013", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nstatic ll DP[2][61][21601] = {};\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<pair<int,int>> D(N);\n rep(i,0,N) {\n int A[6], SUM = 0, SUM2 = 0;\n rep(j,0,6) cin >> A[j], SUM += A[j];\n rep(j,0,6) SUM2 += (SUM - A[j] * 6) * (SUM - A[j] * 6);\n D[i] = {SUM, SUM2 / 6};\n }\n int MAXS = 360 * N + 1;\n rep(i,0,2) {\n rep(j,0,N+1) {\n rep(k,0,MAXS) DP[i][j][k] = INF;\n }\n }\n DP[0][0][0] = 0;\n rep(i,0,N) {\n rep(j,0,N+1) {\n rep(k,0,MAXS) DP[(i&1)^1][j][k] = INF;\n }\n rep(j,0,i+1) {\n rep(k,0,MAXS) {\n if (DP[i&1][j][k] == INF) continue;\n chmin(DP[(i&1)^1][j+1][k+D[i].first], DP[i&1][j][k]+D[i].second);\n chmin(DP[(i&1)^1][j+1][abs(k-D[i].first)], DP[i&1][j][k]+D[i].second);\n chmin(DP[(i&1)^1][j][k], DP[i&1][j][k]);\n }\n }\n }\n ll ANS = INF;\n for (ll i = 0; i < MAXS; i++) {\n chmin(ANS, i*i+DP[N&1][M][i]);\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 5800, "memory_kb": 24020, "score_of_the_acc": -0.8502, "final_rank": 11 }, { "submission_id": "aoj_1670_9438455", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\nint main(){\n while(1){\n int N,M;cin>>N>>M;\n if(!N)return 0;\n vector<int>B(N),C(N);\n REP(i,N){\n vector<int>A(6);cin>>A;\n REP(j,6)B[i]+=A[j];\n REP(j,6)C[i]+=A[j]*A[j];\n C[i]*=6;C[i]-=B[i]*B[i];\n }\n vector<vector<int>>DP(M+1,vector<int>(360*M+1,1e9));\n DP[0][0]=0;\n REP(i,N){\n auto EP=DP;\n REP(j,M)REP(k,360*M+1){\n chmin(EP[j+1][abs(k-B[i])],DP[j][k]+C[i]);\n if(k+B[i]<=360*M)chmin(EP[j+1][k+B[i]],DP[j][k]+C[i]);\n }\n DP=EP;\n }\n ll ans=1e18;\n REP(i,360*M+1)chmin(ans,DP[M][i]+i*i);\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7560, "memory_kb": 13516, "score_of_the_acc": -1.0476, "final_rank": 15 }, { "submission_id": "aoj_1670_9397515", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int MAX_N = 60;\nconstexpr int MAX_A = 360;\nconstexpr int OFFSET = MAX_N * MAX_A;\nconstexpr int inf = (1 << 30) - 1;\n\ntemplate <class T> void chmin(T& x, const T& y) {\n if (x > y) x = y;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n int N, M;\n while (true) {\n cin >> N >> M;\n if (N == 0) break;\n int nowM = 0; \n int nowOff = nowM * MAX_A;\n vector dp(nowM + 1, vector(2 * nowOff + 1, inf));\n dp[0][0 + nowOff] = 0;\n for (int i = 0; i < N; ++i) {\n int A = 0, B = 0;\n for (int j = 0; j < 6; ++j) {\n int x;\n cin >> x;\n A += x;\n B += x * x;\n }\n int nextM = min(nowM + 1, M);\n int nextOff = nextM * MAX_A;\n vector ndp(nextM + 1, vector(2 * nextOff + 1, inf));\n for (int j = 0; j <= nowM; ++j) {\n for (int k = -j * MAX_A; k <= j * MAX_A; ++k) {\n const int x = dp[j][k + nowOff];\n if (x == inf) continue;\n chmin(ndp[j][k + nextOff], x);\n if (j < nextM) {\n chmin(ndp[j + 1][k + A + nextOff], x + 2 * A * k + 6 * B);\n chmin(ndp[j + 1][k - A + nextOff], x - 2 * A * k + 6 * B);\n }\n }\n }\n nowM = nextM;\n nowOff = nextOff;\n dp = move(ndp);\n }\n const auto& vec = dp[M];\n cout << *min_element(begin(vec), end(vec)) << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3220, "memory_kb": 25020, "score_of_the_acc": -0.4659, "final_rank": 6 }, { "submission_id": "aoj_1670_9386078", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define all(A) A.begin(),A.end()\n\n\nvoid solve(ll N, ll M) {\n ll B=min(M*60*6+1,ll(8000));\n vvll DP(M+1,vll(B*2+1,1e18));\n DP[0][B]=0;\n rep(i,N){\n vvll NDP=DP;\n vll A(6);\n ll S=0,T=0;\n rep(j,6){\n cin>>A[j];\n S+=A[j];\n T+=A[j]*A[j];\n }\n ll av=6*S;\n ll va=6*T-S;\n rep(m,M){\n rep(a,B*2+1){\n if(DP[m][a]>1e17)continue;\n ll sm=a-(B);\n NDP[m+1][a+S]=min(NDP[m+1][a+S],DP[m][a]+2*S*sm+6*T);\n NDP[m+1][a-S]=min(NDP[m+1][a-S],DP[m][a]-2*S*sm+6*T);\n }\n }\n swap(DP,NDP);\n }\n ll an=1e18;\n rep(i,B*2+1){\n an=min(an,DP[M][i]);\n }\n cout<<an<<endl;\n}\n\nint main() {\n \n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n ll N,M;\n // clock_t Start=clock();\n while (cin >> N >> M, N+M != 0){\n clock_t start=clock();\n solve(N, M);\n clock_t end=clock();\n // cout<<fixed<<setprecision(15)<<double(start-end)/CLOCKS_PER_SEC<<endl;\n }\n // clock_t End=clock();\n // cout<<fixed<<setprecision(15)<<double(End-Start)/CLOCKS_PER_SEC<<endl;\n}", "accuracy": 1, "time_ms": 6540, "memory_kb": 18480, "score_of_the_acc": -0.9258, "final_rank": 12 }, { "submission_id": "aoj_1670_9383134", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i, m, n) for (ll i = m; i < n; i++)\n#define rrep(i, m, n) for (ll i = m - 1; i >= n; i--)\n#define pb(n) push_back(n)\n#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());\n#define Sort(n) sort(n.begin(), n.end())\n#define Rev(n) reverse(n.begin(), n.end())\n#define HOUT(S) cout << setprecision(50) << S << endl\n#define pint pair<ll, ll>\n#define tint tuple<ll, ll, ll>\n#define mod 1000000007\n#define MAX 100010LL\n#define endl \"\\n\"\n#define ALL(a) a.begin(), a.end()\n#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))\n#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))\n\ntemplate <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }\n\ntemplate <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {\n return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));\n}\n\ntemplate <typename T, typename U, typename... V>\ntypename enable_if<is_same<T, U>::value != 0>::type fill_v(U& u, const V... v) {\n u = U(v...);\n}\n\ntemplate <typename T, typename U, typename... V>\ntypename enable_if<is_same<T, U>::value == 0>::type fill_v(U& u, const V... v) {\n for (auto& e : u) fill_v<T>(e, v...);\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n ll N, M;\n cin >> N >> M;\n while(N) {\n vector<ll> Dice(N, 0), DD(N, 0);\n rep(i, 0, N) rep(j, 0, 6) {\n ll x;\n cin >> x;\n Dice[i] += x;\n DD[i] += x * x;\n }\n ll u = 3600;\n auto dp = make_v<ll>(2, M + 1, 2 * u + 1);\n fill_v<ll>(dp, mod);\n dp[0][0][u] = 0;\n ll w = 0;\n rep(i, 0, N) {\n dp[w ^ 1] = dp[w];\n rep(j, 0, min(i + 1, M + 1)) {\n rep(k, 0, 2 * u + 1) {\n if(dp[w][j][k] == mod) continue;\n if(j < M) {\n if(k + Dice[i] < 2 * u + 1) chmin(dp[w ^ 1][j + 1][k + Dice[i]], dp[w][j][k] + 2 * (k - u) * Dice[i] + 6 * DD[i]);\n if(k - Dice[i] > -1) chmin(dp[w ^ 1][j + 1][k - Dice[i]], dp[w][j][k] - 2 * (k - u) * Dice[i] + 6 * DD[i]);\n }\n }\n }\n w ^= 1;\n }\n ll ans = mod;\n rep(i, 0, 2 * u + 1) chmin(ans, dp[w][M][i]);\n cout << ans << endl;\n cin >> N >> M;\n }\n}", "accuracy": 1, "time_ms": 1810, "memory_kb": 13416, "score_of_the_acc": -0.1757, "final_rank": 3 }, { "submission_id": "aoj_1670_9342581", "code_snippet": "/// 生成AI不使用 / Not using generative AI\n\n// #pragma GCC target (\"avx\")\n// #pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#ifdef MARC_LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n\n#ifdef ATCODER\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n//#define int long long\nusing ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vvvvll = vector<vvvll>;\nusing vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;\nusing ld = long double; using vld = vector<ld>; using vvld = vector<vld>; using vd = vector<double>;\nusing vc = vector<char>; using vvc = vector<vc>; using vs = vector<string>;\nusing vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;\nusing pii = pair<int, int>; using pcc = pair<char, char>; using pll = pair<ll, ll>; using pli = pair<ll, int>; using pdd = pair<double, double>; using pldld = pair<ld,ld>;\nusing vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpldld = vector<pldld>;\nusing ui = unsigned int; using ull = unsigned long long;\nusing i128 = __int128; using f128 = __float128;\ntemplate<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type> > name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type> > > name(h, vector<vector<type> >(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) vector<vector<vector<vector<type> > > > name(a, vector<vector<vector<type> > >(b, vector<vector<type> >(c, vector<type>(__VA_ARGS__))))\n\n#define overload4(a,b,c,d,name,...) name\n#define rep1(n) for(ll i = 0; i < (ll)n; i++)\n#define rep2(i,n) for(int i = 0; i < (ll)n; i++)\n#define rep3(i,a,b) for (ll i = a; i < (ll)b; i++)\n#define rep4(i,a,b,c) for (ll i = a; i < (ll)b; i += (ll)c)\n#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)\n#define repback1(n) for (ll i = (ll)n-1; i >= 0; i--)\n#define repback2(i,n) for (ll i = (ll)n-1; i >= 0; i--)\n#define repback3(i,a,b) for (ll i = (ll)b-1; i >= (ll)a; i--)\n#define repback4(i,a,b,c) for (ll i = (ll)b-1; i >= (ll)a; i -= (ll)c)\n#define repback(...) overload4(__VA_ARGS__,repback4,repback3,repback2,repback1)(__VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\n#define include(y, x, H, W) (0 <= (y) && (y) < (H) && 0 <= (x) && (x) < (W))\n#define inrange(x, down, up) ((down) <= (x) && (x) <= (up))\n#define pb push_back\n#define eb emplace_back\n#define fir first\n#define sec second\n#define TIMER_START TIME_START = clock()\n#ifdef MARC_LOCAL\n// https://trap.jp/post/1224/\n#define debug1(x) cout << \"debug: \" << (#x) << \": \" << (x) << endl\n#define debug2(x, y) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << endl\n#define debug3(x, y, z) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << endl\n#define debug4(x, y, z, w) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << endl\n#define debug5(x, y, z, w, v) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << \", \" << (#v) << \": \" << (v) << endl\n#define debug6(x, y, z, w, v, u) cout << \"debug: \" << (#x) << \": \" << (x) << \", \" << (#y) << \": \" << (y) << \", \" << (#z) << \": \" << (z) << \", \" << (#w) << \": \" << (w) << \", \" << (#v) << \": \" << (v) << \", \" << (#u) << \": \" << (u) << endl\n#define overload6(a, b, c, d, e, f, g,...) g\n#define debug(...) overload6(__VA_ARGS__, debug6, debug5, debug4, debug3, debug2, debug1)(__VA_ARGS__)\n#define debuga cerr << \"a\" << endl\n#define debugnl cout << endl\n#define Case(i) cout << \"Case #\" << (i) << \": \"\n#define TIMECHECK cerr << 1000.0 * static_cast<double>(clock() - TIME_START) / CLOCKS_PER_SEC << \"ms\" << endl\n#define LOCAL 1\n#else\n#define debug1(x) void(0)\n#define debug2(x, y) void(0)\n#define debug3(x, y, z) void(0)\n#define debug4(x, y, z, w) void(0)\n#define debug5(x, y, z, w, v) void(0)\n#define debug6(x, y, z, w, v, u) void(0)\n#define debug(...) void(0)\n#define debuga void(0)\n#define debugnl void(0)\n#define Case(i) void(0)\n#define TIMECHECK void(0)\n#define LOCAL 0\n#endif\n\n//mt19937_64 rng(0);\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nclock_t TIME_START;\nconst long double pi = 3.141592653589793238462643383279L;\nconst long long INFL = 1000000000000000000ll;\nconst long long INFLMAX = numeric_limits< long long >::max(); // 9223372036854775807\nconst int INF = 1000000000;\nconst int INFMAX = numeric_limits< int >::max(); // 2147483647\nconst long double INFD = numeric_limits<ld>::infinity();\nconst long double EPS = 1e-10;\nconst int mod1 = 1000000007;\nconst int mod2 = 998244353;\nconst vi dx1 = {1,0,-1,0};\nconst vi dy1 = {0,1,0,-1};\nconst vi dx2 = {0, 1, 1, 1, 0, -1, -1, -1};\nconst vi dy2 = {1, 1, 0, -1, -1, -1, 0, 1};\nconstexpr char nl = '\\n';\nconstexpr char bl = ' ';\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) {__uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do {--d;*d = \"0123456789\"[tmp % 10];tmp /= 10;} while (tmp != 0);if (value < 0) {--d;*d = '-';}int len = std::end(buffer) - d;if (dest.rdbuf()->sputn(d, len) != len) {dest.setstate(std::ios_base::badbit);}}return dest; }\n__int128 parse(string &s) { __int128 ret = 0; for (int i = 0; i < (int)s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } // for __int128 (https://kenkoooo.hatenablog.com/entry/2016/11/30/163533)\ntemplate<class T> ostream& operator << (ostream& os, vector<T>& vec) { os << \"[\"; for (int i = 0; i<(int)vec.size(); i++) { os << vec[i] << (i + 1 == (int)vec.size() ? \"\" : \", \"); } os << \"]\"; return os; } /// vector 出力\ntemplate<class T, class U> ostream& operator << (ostream& os, pair<T, U>& pair_var) { os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\"; return os; } // pair 出力\ntemplate<class T, class U> ostream& operator << (ostream& os, map<T, U>& map_var) { os << \"{\"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << \"(\" << itr->first << \", \" << itr->second << \")\"; itr++; if(itr != map_var.end()) os << \", \"; itr--; } os << \"}\"; return os; } // map出力\ntemplate<class T> ostream& operator << (ostream& os, set<T>& set_var) { os << \"{\"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if(itr != set_var.end()) os << \", \"; itr--; } os << \"}\"; return os; } /// set 出力\n\nbool equals(long double a, long double b) { return fabsl(a - b) < EPS; }\ntemplate<class T> int sign(T a) { return equals(a, 0) ? 0 : a > 0 ? 1 : -1; }\nlong double radian_to_degree(long double r) { return (r * 180.0 / pi); }\nlong double degree_to_radian(long double d) { return (d * pi / 180.0); }\n\nint popcnt(unsigned long long a){ return __builtin_popcountll(a); } // ll は 64bit対応!\nint MSB1(unsigned long long x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // MSB(1), 0-indexed ( (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) )\nint LSB1(unsigned long long x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } // LSB(1), 0-indexed ( (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) )\nlong long maskbit(int n) { return (1LL << n) - 1; }\nbool bit_is1(long long x, int i) { return ((x>>i) & 1); }\nstring charrep(int n, char c) { return std::string(n, c); }\ntemplate<class T> T square(T x) { return (x) * (x); }\ntemplate<class T>void UNIQUE(T& A) {sort(all(A)); A.erase(unique(all(A)), A.end());}\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\nvoid YESNO(bool b) {if(b){cout<<\"YES\"<<'\\n';} else{cout<<\"NO\"<<'\\n';}} void YES() {YESNO(true);} void NO() {YESNO(false);}\nvoid yesno(bool b) {if(b){cout<<\"yes\"<<'\\n';} else{cout<<\"no\"<<'\\n';}} void yes() {yesno(true);} void no() {yesno(false);}\nvoid YesNo(bool b) {if(b){cout<<\"Yes\"<<'\\n';} else{cout<<\"No\"<<'\\n';}} void Yes() {YesNo(true);} void No() {YesNo(false);}\nvoid POSIMPOS(bool b) {if(b){cout<<\"POSSIBLE\"<<'\\n';} else{cout<<\"IMPOSSIBLE\"<<'\\n';}}\nvoid PosImpos(bool b) {if(b){cout<<\"Possible\"<<'\\n';} else{cout<<\"Impossible\"<<'\\n';}}\nvoid posimpos(bool b) {if(b){cout<<\"possible\"<<'\\n';} else{cout<<\"impossible\"<<'\\n';}}\nvoid FIRSEC(bool b) {if(b){cout<<\"FIRST\"<<'\\n';} else{cout<<\"SECOND\"<<'\\n';}}\nvoid firsec(bool b) {if(b){cout<<\"first\"<<'\\n';} else{cout<<\"second\"<<'\\n';}}\nvoid FirSec(bool b) {if(b){cout<<\"First\"<<'\\n';} else{cout<<\"Second\"<<'\\n';}}\nvoid AliBob(bool b) {if(b){cout<<\"Alice\"<<'\\n';} else{cout<<\"Bob\"<<'\\n';}}\nvoid TakAok(bool b) {if(b){cout<<\"Takahashi\"<<'\\n';} else{cout<<\"Aoki\"<<'\\n';}}\nint GetTime() {return 1000.0*static_cast<double>(clock() - TIME_START) / CLOCKS_PER_SEC;}\nll myRand(ll B) {return (unsigned long long)rng() % B;}\ntemplate<class T> void print(const T& x, const char endch = '\\n') { cout << x << endch; }\n\ntemplate<class T> T ceil_div(T x, T y) { assert(y); return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate<class T> T floor_div(T x, T y) { assert(y); return (x > 0 ? x / y : (x - y + 1) / y); }\ntemplate<class T> pair<T, T> divmod(T x, T y) { T q = floor_div(x, y); return {q, x - q * y}; } /// (q, r) s.t. x = q*y + r \nll GCD(ll a, ll b) { if(a < b) swap(a, b); if(b == 0) return a; if(a%b == 0) return b; else return GCD(b, a%b); }\nll LCM(ll a, ll b) { assert(GCD(a,b) != 0); return a / GCD(a, b) * b; }\nll MOD(ll &x, const ll P) { ll ret = x%P; if(ret < 0) ret += P; return x = ret; } /// x % P を非負整数に直す\nll mpow(ll x, ll n, const ll mod) { x %= mod; ll ret = 1; while(n > 0) { if(n & 1) ret = ret * x % mod; x = x * x % mod; n >>= 1; } return ret; } /// x^n % mod を計算\nll lpow(ll x, ll n) { ll ret = 1; while(n > 0){ if(n & 1) ret = ret * x; x = x * x; n >>= 1; } return ret; } /// x^nを計算\nstring toBinary(ll n) { if(n == 0) return \"0\"; assert(n > 0); string ret; while (n != 0){ ret += ( (n & 1) == 1 ? '1' : '0' ); n >>= 1; } reverse(ret.begin(), ret.end()); return ret; } /// 10進数(long long) -> 2進数(string)への変換\nll toDecimal(string S) { ll ret = 0; for(int i = 0; i < (int)S.size(); i++){ ret *= 2LL; if(S[i] == '1') ret += 1; } return ret; } /// 2進数(string) → 10進数(long long)への変換\nint ceil_pow2(ll n) { int x = 0; while ((1ll << x) < n) x++; return x;} /// return minimum non-negative `x` s.t. `n <= 2**x`\nint floor_pow2(ll n) { int x = 0; while ((1ll << (x+1)) <= n) x++; return x;} /// return maximum non-negative `x` s.t. `n >= 2**x`\n\n\n// ############################\n// # #\n// # C O D E S T A R T #\n// # #\n// ############################\n\n/// main()の中に0を受け取る変数(終了判定)を定義しておく\nint N,M;\n\n// E[(X+A)^2] = E[X^2] + 2*E[A]*E[X] + E[A^2] を用いて、DPを更新する(期待値の線形性に加えて、各サイコロ独立なので、E[AX] = E[A]E[X]が成立する!)\n// -> あらかじめ全て36倍として取り扱えるように調整する\n// E[X^2] = (E[X])^2 + V[X] を用いても良い(これも各サイコロ独立なので、V[X+Y] = V[X]+V[Y]が成立する!)\nvoid solve() {\n vvi A(N, vi(6)); rep(i,N) rep(j,6) cin >> A[i][j];\n vi B(N,0); rep(i,N) rep(j,6) B[i] += A[i][j]; // E[A[i]]\n vi C(N,0); rep(i,N) rep(j,6) C[i] += A[i][j] * A[i][j] * 6; // E[A[i]^2]\n vi ac(N+1,0); rep(i,N) ac[i+1] = ac[i] + B[i];\n \n vvi dp(M+1, vi(1,0)); // dp[i][j] := サイコロをi個採用して、(x-y)がjのときの、E[x-y]の期待値の最小値\n dp[0][0] = 0;\n rep(i,N) {\n int prev = dp[0].size();\n int cur = 2*ac[i+1] + 1;\n int prevofs = ac[i];\n int curofs = ac[i+1];\n vvi nxt(M+1, vi(cur, INF));\n\n rep(j,min(i,M)+1) {\n rep(k,prev) {\n if(dp[j][k] == INF) continue;\n chmin(nxt[j][k-prevofs+curofs], dp[j][k]); // 採用しない\n if(j < M) {\n chmin(nxt[j+1][k+B[i]-prevofs+curofs], dp[j][k] + C[i] + 2*B[i]*(k-prevofs)); // xに渡す\n chmin(nxt[j+1][k-B[i]-prevofs+curofs], dp[j][k] + C[i] - 2*B[i]*(k-prevofs)); // yに渡す\n }\n }\n }\n \n swap(dp, nxt);\n }\n\n int ans = INF;\n for(auto v:dp[M]) chmin(ans, v);\n \n cout << ans << endl;\n\n}\n\n\n\nsigned main() {\n cin.tie(0); ios_base::sync_with_stdio(false);\n TIMER_START;\n //cout << fixed << setprecision(15);\n \n\n //int tt = 1;\n //cin >> tt;\n while(true){\n cin >> N >> M;\n //debug(N,M);\n if(N == 0 && M == 0) break;\n\n solve();\n }\n\n TIMECHECK;\n return 0;\n}", "accuracy": 1, "time_ms": 3560, "memory_kb": 15648, "score_of_the_acc": -0.4556, "final_rank": 5 }, { "submission_id": "aoj_1670_9329558", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1000000000;\nconstexpr int64_t llINF = 3000000000000000000;\nconstexpr double eps = 1e-10;\nconst double pi = acos(-1);\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\nll extgcd(ll a, ll b, ll& x, ll& y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nll modpow(ll a, ll b, ll m) {\n ll res = 1;\n while (b) {\n if (b & 1) {\n res *= a;\n res %= m;\n }\n a *= a;\n a %= m;\n b >>= 1;\n }\n return res;\n}\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint& operator+=(const modint& p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint& operator-=(const modint& p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint& operator*=(const modint& p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint& operator/=(const modint& p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint& p) const { return modint(*this) += p; }\n modint operator-(const modint& p) const { return modint(*this) -= p; }\n modint operator*(const modint& p) const { return modint(*this) *= p; }\n modint operator/(const modint& p) const { return modint(*this) /= p; }\n bool operator==(const modint& p) const { return x == p.x; }\n bool operator!=(const modint& p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream& operator<<(ostream& os, const modint& p) { return os << p.x; }\n friend istream& operator>>(istream& is, modint& a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\nnamespace geometry {\n\nusing floating_point_type = long double;\n\nconstexpr floating_point_type EPS = 1e-13;\nconstexpr floating_point_type Pi = 3.141592653589793;\n\nconstexpr floating_point_type operator\"\" _deg(floating_point_type deg) { return static_cast<floating_point_type>(deg * Pi / 180.0); }\nconstexpr floating_point_type operator\"\" _deg(unsigned long long deg) { return static_cast<floating_point_type>(deg * Pi / 180.0); }\n\ntemplate <typename T>\nconstexpr int sgn(T a) noexcept {\n return (a < -EPS ? -1 : (a > EPS ? +1 : 0));\n}\n\ntemplate <typename T, typename U>\nconstexpr bool same(T a, U b) noexcept {\n return sgn(a - b) == 0;\n}\n\ntemplate <typename T>\nconstexpr T square(T x) noexcept {\n return x * x;\n}\n\n// ax + by + c = 0, gcd(a,b,c) = 1\n// a != 0 : a > 0\n// a = 0: b > 0\ntuple<long long, long long, long long> general_form(long long x1, long long y1, long long x2, long long y2) {\n long long a = y1 - y2, b = x2 - x1, c = x1 * y2 - x2 * y1;\n if (a < 0) a = -a, b = -b, c = -c;\n if (a == 0 && b < 0) b = -b, c = -c;\n long long g = gcd(gcd(a, b), c);\n return {a / g, b / g, c / g};\n}\n\ntemplate <typename T>\nstruct Vector2D {\n using value_type = T;\n T x, y;\n\n Vector2D() = default;\n explicit Vector2D(T arg) : x(cos(arg)), y(sin(arg)) {}\n constexpr Vector2D(T x, T y) noexcept : x(x), y(y) {}\n\n constexpr bool operator<(const Vector2D& v) const noexcept {\n if (!same(x, v.x)) return x < v.x;\n return y < v.y;\n }\n constexpr bool operator>(const Vector2D& v) const noexcept { return v < *this; }\n constexpr bool operator==(const Vector2D& v) const noexcept { return same(x, v.x) && same(y, v.y); }\n constexpr bool operator!=(const Vector2D& v) const noexcept { return !(*this == v); }\n\n constexpr Vector2D operator+() const noexcept { return *this; }\n constexpr Vector2D operator-() const noexcept { return {-x, -y}; }\n\n constexpr Vector2D operator+(const Vector2D& v) const noexcept { return {x + v.x, y + v.y}; }\n constexpr Vector2D operator-(const Vector2D& v) const noexcept { return {x - v.x, y - v.y}; }\n constexpr Vector2D operator*(T s) const noexcept { return {x * s, y * s}; }\n constexpr Vector2D operator/(T s) const noexcept { return {x / s, y / s}; }\n\n constexpr Vector2D& operator+=(const Vector2D& v) noexcept {\n x += v.x;\n y += v.y;\n return *this;\n }\n constexpr Vector2D& operator-=(const Vector2D& v) noexcept {\n x -= v.x;\n y -= v.y;\n return *this;\n }\n constexpr Vector2D& operator*=(T s) noexcept {\n x *= s;\n y *= s;\n return *this;\n }\n constexpr Vector2D& operator/=(T s) noexcept {\n x /= s;\n y /= s;\n return *this;\n }\n\n constexpr bool isZero() const noexcept { return x == T(0) && y == T(0); }\n bool hasNan() const { return isnan(x) || isnan(y); }\n\n constexpr T dot(const Vector2D& v) const noexcept { return x * v.x + y * v.y; }\n constexpr T cross(const Vector2D& v) const noexcept { return x * v.y - y * v.x; }\n\n floating_point_type length() const {\n if (isZero()) return 0.0;\n\n const T abs_x = std::abs(x);\n const T abs_y = std::abs(y);\n\n const auto [u, v] = std::minmax(abs_x, abs_y);\n const T t = u / v;\n\n return v * std::sqrt(std::fma(t, t, 1.0));\n }\n constexpr T lengthSq() const noexcept { return dot(*this); }\n\n floating_point_type distanceFrom(const Vector2D& v) const { return (v - *this).length(); }\n constexpr T distanceFromSq(const Vector2D& v) const noexcept { return (v - *this).lengthSq(); }\n\n Vector2D normalized() const { return *this / length(); }\n Vector2D& normalize() { return *this /= length(); }\n\n Vector2D rotated(floating_point_type angle) const {\n const T s = sin(angle);\n const T c = cos(angle);\n return {x * c - y * s, x * s + y * c};\n }\n Vector2D& rotate(floating_point_type angle) { return *this = rotated(angle); }\n\n floating_point_type arg() const { return atan2<floating_point_type>(y, x); }\n floating_point_type arg(const Vector2D& v) const {\n if (isZero() || v.isZero()) {\n return numeric_limits<T>::signaling_NaN();\n }\n\n return atan2<floating_point_type>(cross(v), dot(v));\n }\n floating_point_type arg(const Vector2D& a, const Vector2D& b) const { return (a - *this).arg(b - *this); }\n\n constexpr Vector2D lerp(const Vector2D& v, T f) const noexcept { return {x + (v.x - x) * f, y + (v.y - y) * f}; }\n\n constexpr friend Vector2D operator*(T s, const Vector2D& v) noexcept { return {s * v.x, s * v.y}; }\n friend istream& operator>>(istream& is, Vector2D& v) { return is >> v.x >> v.y; }\n friend ostream& operator<<(ostream& os, const Vector2D& v) { return os << \"(\" << v.x << \", \" << v.y << \")\"; }\n};\n\ntemplate <typename T>\nvoid argument_sort(vector<Vector2D<T>>& vec, const Vector2D<T>& center = Vector2D<T>(0, 0)) {\n auto orthant = [](const Vector2D<T>& v) {\n if (sgn(v.x) < 0 && sgn(v.y) < 0) return 0;\n if (sgn(v.x) >= 0 && sgn(v.y) < 0) return 1;\n if (sgn(v.x) == 0 && sgn(v.y) == 0) return 2;\n if (sgn(v.x) >= 0 && sgn(v.y) >= 0) return 3;\n return 4;\n };\n\n auto comp = [orthant, center](const Vector2D<T>& a, const Vector2D<T>& b) {\n const int o1 = orthant(a - center), o2 = orthant(b - center);\n if (o1 != o2) return o1 < o2;\n\n return (a - center).cross(b - center) > 0;\n };\n\n sort(vec.begin(), vec.end(), comp);\n}\n\ntemplate <typename T>\nvector<Vector2D<T>> convex_hull(vector<Vector2D<T>> points) {\n int n = points.size(), k = 0;\n vector<Vector2D<T>> res(n * 2);\n\n sort(points.begin(), points.end());\n points.erase(unique(points.begin(), points.end()), points.end());\n\n for (int i = 0; i < n; ++i) {\n while (k > 1 && sgn((res[k - 1] - res[k - 2]).cross(points[i] - res[k - 1])) <= 0) k--;\n res[k++] = points[i];\n }\n\n for (int i = n - 2, t = k; i >= 0; --i) {\n while (k > t && sgn((res[k - 1] - res[k - 2]).cross(points[i] - res[k - 1])) <= 0) k--;\n res[k++] = points[i];\n }\n\n res.resize(k - 1);\n return res;\n}\n\n/*\nABから見てBCは左に曲がるのなら +1\nABから見てBCは右に曲がるのなら -1\nBACの順番で一直線上に並ぶなら +2\nABCの順番で一直線上に並ぶなら -2\nCがAB上なら 0\n*/\ntemplate <typename T>\nconstexpr int ccw(const Vector2D<T>& a, const Vector2D<T>& b, const Vector2D<T>& c) {\n const int res = sgn((b - a).cross(c - a));\n\n if (res != 0) return res;\n if (sgn((a - b).dot(c - a)) > 0) return +2;\n if (sgn((b - a).dot(c - b)) > 0) return -2;\n return 0;\n}\n\nusing Vec2 = Vector2D<floating_point_type>;\n\nstruct Line {\n using value_type = Vec2::value_type;\n Vec2 begin, end;\n\n Line() = default;\n constexpr Line(const Vec2& begin, const Vec2& end) : begin(begin), end(end) {}\n constexpr Line(value_type sx, value_type sy, value_type gx, value_type gy) : begin(sx, sy), end(gx, gy) {}\n\n // ax + by + c = 0\n Line(value_type a, value_type b, value_type c) {\n assert(sgn(a) != 0 || sgn(b) != 0);\n\n if (sgn(b) == 0) {\n begin = Vec2(-c / a, 0.0);\n end = Vec2(-c / a, 1.0);\n } else {\n begin = Vec2(0, -c / b);\n end = Vec2(1.0, -(a + c) / b);\n }\n }\n\n constexpr Vec2 vector() const { return end - begin; }\n\n constexpr Line& reverse() {\n const Vec2 tmp = begin;\n begin = end;\n end = tmp;\n return *this;\n }\n constexpr Line reversed() const { return {end, begin}; }\n\n constexpr Line& moveBy(const Vec2& v) {\n begin += v;\n end += v;\n return *this;\n }\n constexpr Line movedBy(const Vec2& v) const { return {begin + v, end + v}; }\n\n constexpr bool isParallel(const Line& line) const { return sgn(vector().cross(line.vector())) == 0; }\n constexpr bool isOrthogonal(const Line& line) const { return sgn(vector().dot(line.vector())) == 0; }\n\n floating_point_type length() const { return vector().length(); }\n constexpr value_type lengthSq() const { return vector().lengthSq(); }\n\n floating_point_type distanceFrom(const Vec2& v) const { return fabs(vector().cross(v - begin)) / length(); }\n constexpr value_type distanceFromSq(const Vec2& v) const {\n const value_type d = vector().cross(v - begin);\n\n return d / lengthSq() * d;\n }\n\n optional<Vec2> intersectsAt(const Line& line) const {\n if (sgn(vector().cross(line.vector())) == 0) return nullopt;\n\n return begin + vector() * fabs((line.end - begin).cross(line.vector()) / vector().cross(line.vector()));\n }\n\n constexpr Vec2 projection(const Vec2& v) const { return begin + vector() * (v - begin).dot(vector()) / lengthSq(); }\n constexpr Vec2 reflection(const Vec2& v) const { return 2 * projection(v) - v; }\n\n // ax + by + c = 0\n constexpr tuple<value_type, value_type, value_type> generalForm() const {\n const Vec2 vec = vector();\n return {vec.y, -vec.x, end.cross(begin)};\n }\n\n friend istream& operator>>(istream& is, Line& rhs) { return is >> rhs.begin >> rhs.end; }\n};\n\nnamespace segment {\n\nusing Segment = Line;\n\nconstexpr bool intersect(const Segment& a, const Segment& b) {\n return ccw(a.begin, a.end, b.begin) * ccw(a.begin, a.end, b.end) <= 0 && ccw(b.begin, b.end, a.begin) * ccw(b.begin, b.end, a.end) <= 0;\n}\n\nfloating_point_type distance(const Segment& a, const Vec2& v) {\n if (sgn(a.vector().dot(v - a.begin)) < 0 || sgn((-a.vector()).dot(v - a.end)) < 0) {\n return min(v.distanceFrom(a.begin), v.distanceFrom(a.end));\n }\n return a.distanceFrom(v);\n}\n\nconstexpr floating_point_type distanceSq(const Segment& a, const Vec2& v) {\n if (sgn(a.vector().dot(v - a.begin)) < 0 || sgn((-a.vector()).dot(v - a.end)) < 0) {\n return min(v.distanceFromSq(a.begin), v.distanceFromSq(a.end));\n }\n return a.distanceFromSq(v);\n}\n\nfloating_point_type distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0.0;\n\n return min({distance(a, b.begin), distance(a, b.end), distance(b, a.begin), distance(b, a.end)});\n}\n} // namespace segment\n\nstruct Triangle {\n using value_type = Vec2::value_type;\n Vec2 p0, p1, p2;\n\n Triangle() = default;\n constexpr Triangle(const Vec2& p0, const Vec2& p1, const Vec2& p2) : p0(p0), p1(p1), p2(p2) {}\n\n constexpr Vec2& p(size_t index) { return (&p0)[index]; }\n constexpr const Vec2& p(size_t index) const { return (&p0)[index]; }\n\n constexpr Line side(size_t index) const {\n if (index == 0)\n return Line(p1, p2);\n else if (index == 1)\n return Line(p2, p0);\n else if (index == 2)\n return Line(p0, p1);\n else {\n throw std::out_of_range(\"Triangle::side() index out of range\");\n }\n }\n\n floating_point_type angle(size_t index) const {\n const Vec2 vec[3] = {p(index), p((index + 1) % 3), p((index + 2) % 3)};\n\n const value_type res = vec[0].arg(vec[1], vec[2]);\n\n return fabs(res);\n }\n\n constexpr value_type area() const {\n const value_type res = (p1 - p0).cross(p2 - p0) / 2;\n return (0 < res ? res : -res);\n }\n\n constexpr Vec2 circumCenter() const {\n const value_type d0 = side(0).lengthSq(), d1 = side(1).lengthSq(), d2 = side(2).lengthSq(), t0 = d0 * (-d0 + d1 + d2),\n t1 = d1 * (+d0 - d1 + d2), t2 = d2 * (+d0 + d1 - d2), t3 = t0 + t1 + t2;\n\n return t0 / t3 * p0 + t1 / t3 * p1 + t2 / t3 * p2;\n }\n\n Vec2 innerCenter() const {\n const value_type dist[3] = {side(0).length(), side(1).length(), side(2).length()};\n\n return (dist[0] * p0 + dist[1] * p1 + dist[2] * p2) / (dist[0] + dist[1] + dist[2]);\n }\n\n friend istream& operator>>(istream& is, Triangle& rhs) { return is >> rhs.p0 >> rhs.p1 >> rhs.p2; }\n};\n\nstruct Circle {\n using value_type = Vec2::value_type;\n Vec2 center;\n value_type r;\n\n Circle() = default;\n explicit constexpr Circle(value_type r) : center(0.0, 0.0), r(r) {}\n constexpr Circle(value_type x, value_type y, value_type r) : center(x, y), r(r) {}\n constexpr Circle(const Vec2& center, value_type r) : center(center), r(r) {}\n\n constexpr bool intersects(const Line& line) const { return sgn(line.distanceFromSq(center) - r * r) <= 0; }\n\n constexpr bool intersects(const Circle& circle) const {\n const value_type d = center.distanceFromSq(circle.center);\n\n return sgn(square(r - circle.r) - d) <= 0 && sgn(d - square(r + circle.r)) <= 0;\n }\n\n constexpr bool circumcribes(const Circle& circle) const { return same(center.distanceFromSq(circle.center), square(r + circle.r)); }\n\n constexpr bool inscribes(const Circle& circle) const { return same(center.distanceFromSq(circle.center), square(r - circle.r)); }\n\n constexpr bool contains(const Circle& circle) const {\n return 0 < sgn(square(r - circle.r) - center.distanceFromSq(circle.center)) && 0 < sgn(r - circle.r);\n }\n\n constexpr value_type area() const { return Pi * r * r; }\n\n value_type commonArea(const Circle& circle) const {\n const value_type d_sq = center.distanceFromSq(circle.center);\n const value_type d = center.distanceFrom(circle.center);\n\n const value_type r1 = r;\n const value_type r2 = circle.r;\n\n if (sgn(d_sq - square(r1 + r2)) >= 0) return 0.0;\n if (sgn(d_sq - square(r1 - r2)) <= 0) return min(this->area(), circle.area());\n\n const value_type arg1 = acos(((r1 - r2) * (r1 + r2) + d_sq) / (2 * r1 * d));\n const value_type arg2 = acos(((r2 - r1) * (r2 + r1) + d_sq) / (2 * r2 * d));\n\n return r1 * r1 * (arg1 - sin(2 * arg1) / 2) + r2 * r2 * (arg2 - sin(2 * arg2) / 2);\n }\n\n constexpr Circle& moveBy(const Vec2& v) {\n center += v;\n return *this;\n }\n constexpr Circle movedBy(const Vec2& v) const { return {center + v, r}; }\n\n optional<vector<Vec2>> intersectsAt(const Line& line) const {\n if (!intersects(line)) return nullopt;\n\n vector<Vec2> res;\n\n const Vec2 v = line.projection(center);\n\n if (same(line.distanceFrom(center), r))\n res.emplace_back(v);\n else {\n value_type d = sqrt(r * r - (v - center).lengthSq());\n const Vec2 e = line.vector().normalized();\n\n res.emplace_back(v - d * e);\n res.emplace_back(v + d * e);\n }\n\n return res;\n }\n\n optional<vector<Vec2>> intersectsAt(const Circle& circle) const {\n if (!intersects(circle)) return nullopt;\n\n const value_type d = center.distanceFrom(circle.center), arg = (circle.center - center).arg(),\n theta = acos((d * d + r * r - circle.r * circle.r) / (2 * d * r));\n\n vector<Vec2> res;\n res.emplace_back(center + r * Vec2(arg + theta));\n res.emplace_back(center + r * Vec2(arg - theta));\n\n if (res[0] > res[1]) swap(res[0], res[1]);\n\n return res;\n }\n\n optional<vector<Vec2>> tangentPointFrom(const Vec2& v) const {\n const value_type d = center.distanceFromSq(v);\n\n if (sgn(d - r * r) < 0) return nullopt;\n if (same(d, r * r)) return vector<Vec2>{v};\n\n const auto [x1, y1] = v - center;\n\n const auto& opt = Circle(r).intersectsAt(Line(x1, y1, -r * r));\n\n vector<Vec2> res = opt.value();\n\n for (Vec2& v : res) v += center;\n\n if (res[0] > res[1]) swap(res[0], res[1]);\n\n return res;\n }\n optional<vector<Vec2>> commonTangentPointFrom(const Circle& circle) const {\n const value_type r1 = r, r2 = circle.r;\n\n vector<Vec2> res;\n\n if (!same(r1, r2)) {\n const Vec2 p((r2 * center - r1 * circle.center) / (r2 - r1));\n\n const auto& opt = tangentPointFrom(p);\n\n if (opt.has_value()) {\n for (const Vec2& vec : opt.value()) {\n res.emplace_back(vec);\n }\n }\n } else {\n const value_type theta = (circle.center - center).arg();\n\n res.emplace_back(center + r1 * Vec2(theta + 90_deg));\n res.emplace_back(center + r1 * Vec2(theta - 90_deg));\n }\n\n const Vec2 q((r2 * center + r1 * circle.center) / (r1 + r2));\n\n const auto& opt = tangentPointFrom(q);\n\n if (opt.has_value()) {\n for (const Vec2& vec : opt.value()) {\n res.emplace_back(vec);\n }\n }\n\n if (res.empty()) return nullopt;\n\n sort(res.begin(), res.end());\n\n return res;\n }\n\n friend istream& operator>>(istream& is, Circle& rhs) { return is >> rhs.center >> rhs.r; }\n};\n\nstruct Polygon {\n using value_type = Vec2::value_type;\n int n;\n vector<Vec2> vertex;\n\n Polygon() = default;\n Polygon(int _n) : n(_n), vertex(_n) {}\n Polygon(const vector<Vec2>& vec) : n(static_cast<int>(vec.size())), vertex(vec) {}\n\n Line side(size_t index) const { return Line(vertex[index], vertex[(index + 1) % n]); }\n\n Polygon& moveBy(const Vec2& v) {\n for (Vec2& vec : vertex) vec += v;\n return *this;\n }\n Polygon movedBy(const Vec2& v) const {\n vector<Vec2> new_vertex;\n for (const Vec2& vec : vertex) new_vertex.emplace_back(vec + v);\n\n return Polygon(new_vertex);\n }\n\n bool isConvex() const {\n for (int i = 0; i < n; ++i) {\n if (ccw(vertex[i], vertex[(i + 1) % n], vertex[(i + 2) % n]) == -1) return false;\n }\n return true;\n }\n\n bool isOnEdge(const Vec2& v) const {\n for (int i = 0; i < n; ++i) {\n if (ccw(vertex[i], vertex[(i + 1) % n], v) == 0) return true;\n }\n return false;\n }\n\n bool intersects(const Vec2& v) const {\n floating_point_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += v.arg(vertex[i], vertex[(i + 1) % n]);\n }\n\n return sgn(sum - 2.0 * Pi) == 0;\n }\n\n double area() const {\n value_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += vertex[i].cross(vertex[i == n - 1 ? 0 : i + 1]);\n }\n\n return 0.5 * fabs(sum);\n }\n\n value_type commonArea(const Circle& circle) const {\n if (n < 3) return 0.0;\n\n if (!same(circle.center.x, 0.0) || !same(circle.center.y, 0.0)) {\n return (*this).movedBy(-circle.center).commonArea(Circle(circle.r));\n }\n\n auto cross_area = [&](auto self, Circle c, Vec2 a, Vec2 b) -> value_type {\n const Vec2 va = c.center - a, vb = c.center - b;\n\n value_type f = va.cross(vb), res = 0.0;\n\n if (same(f, 0.0)) return 0.0;\n if (sgn(max(va.length(), vb.length()) - c.r) <= 0) return f;\n\n const Line line(a, b);\n\n if (sgn(segment::distance(line, c.center) - c.r) >= 0) {\n return c.r * c.r * va.arg(vb);\n }\n\n const auto opt = c.intersectsAt(line);\n vector<Vec2> vec1 = opt.value();\n\n const vector<Vec2> vec2{a, vec1.front(), vec1.back(), b};\n\n for (int i = 0; i + 1 < vec2.size(); ++i) {\n res += self(self, c, vec2[i], vec2[i + 1]);\n }\n\n return res;\n };\n\n value_type sum = 0.0;\n\n for (int i = 0; i < n; ++i) {\n sum += cross_area(cross_area, circle, vertex[i], vertex[(i + 1) % n]);\n }\n\n sum *= 0.5;\n\n return fabs(sum);\n }\n\n Polygon convexHull() const { return convex_hull(vertex); }\n\n Polygon convexCut(const Line& line) const {\n assert(isConvex());\n\n vector<Vec2> res;\n\n for (int i = 0; i < n; ++i) {\n const Vec2 &now = vertex[i], &nxt = vertex[(i + 1) % n];\n\n if (ccw(line.begin, line.end, now) != -1) res.emplace_back(now);\n if (ccw(line.begin, line.end, now) * ccw(line.begin, line.end, nxt) < 0) {\n const auto& opt = Line(now, nxt).intersectsAt(line);\n if (opt.has_value()) res.emplace_back(opt.value());\n }\n }\n\n return Polygon(res);\n }\n\n value_type diameter() const {\n const auto convex = (isConvex() ? (*this) : convexHull()).vertex;\n\n const size_t n = convex.size();\n\n if (n == 2)\n return convex[0].distanceFrom(convex[1]);\n else {\n size_t i = 0, j = 0;\n\n for (size_t k = 0; k < n; ++k) {\n if (!(convex[i] < convex[k])) i = k;\n if (convex[j] < convex[k]) j = k;\n }\n\n value_type res = 0.0;\n\n const size_t si = i, sj = j;\n\n while (i != sj || j != si) {\n res = max(res, convex[i].distanceFrom(convex[j]));\n\n if ((convex[(i + 1) % n] - convex[i]).cross(convex[(j + 1) % n] - convex[j]) < 0) {\n i = (i + 1) % n;\n } else\n j = (j + 1) % n;\n }\n\n return res;\n }\n }\n\n friend istream& operator>>(istream& is, Polygon& rhs) {\n for (Vec2& e : rhs.vertex) is >> e;\n return is;\n }\n};\n\n// x座標でsort済み\nfloating_point_type distance_closest_pair(vector<Vec2>& points, int left, int right) {\n if (right - left <= 1) return 1e20;\n\n int mid = (left + right) / 2;\n floating_point_type x = points[mid].x;\n floating_point_type d = min(distance_closest_pair(points, left, mid), distance_closest_pair(points, mid, right));\n\n inplace_merge(points.begin() + left, points.begin() + mid, points.begin() + right,\n [](const Vec2& a, const Vec2& b) { return sgn(a.y - b.y) < 0; });\n\n vector<Vec2> a;\n\n for (int i = left; i < right; ++i) {\n if (sgn(fabs(points[i].x - x) - d) >= 0) continue;\n\n for (int j = static_cast<int>(a.size()) - 1; j >= 0; --j) {\n if (sgn(fabs((points[i] - a[j]).y) - d) >= 0) break;\n d = min(d, points[i].distanceFrom(a[j]));\n }\n\n a.emplace_back(points[i]);\n }\n\n return d;\n}\n\n// 垂直または水平線分の交点\n// begin < end\nvector<Vec2> intersections(const vector<Line>& lines) {\n const int n = static_cast<int>(lines.size());\n\n vector<pair<floating_point_type, int>> events;\n\n for (int i = 0; i < n; ++i) {\n const Line& line = lines[i];\n\n if (same(line.begin.y, line.end.y)) { // 水平\n events.emplace_back(make_pair(line.begin.x, i));\n events.emplace_back(make_pair(line.end.x, i + 2 * n));\n } else { // 垂直\n events.emplace_back(make_pair(line.begin.x, i + n));\n }\n }\n\n sort(events.begin(), events.end());\n\n set<floating_point_type> y_list;\n vector<Vec2> res;\n\n for (int i = 0; i < static_cast<int>(events.size()); ++i) {\n const int kind = events[i].second;\n const int id = kind % n;\n const Line& line = lines[id];\n\n if (kind < n) { // 左端\n y_list.insert(line.begin.y);\n } else if (kind < 2 * n) { // 垂直\n for (auto y : y_list) {\n if (line.begin.y - EPS <= y && y <= line.end.y + EPS) {\n res.emplace_back(Vec2(line.begin.x, y));\n }\n }\n } else { // 右端\n y_list.erase(line.end.y);\n }\n }\n\n return res;\n}\n} // namespace geometry\n\nusing namespace geometry;\nusing Point = Vector2D<ll>;\nvoid solve(int n, int m) {\n vector<vector<int>> a(n, vector<int>(6));\n rep(i, n) rep(j, 6) cin >> a[i][j];\n const int S = 360 * n;\n vector<vector<ll>> dp(m + 1, vector<ll>(S * 2 + 1, llINF));\n dp[0][S] = 0;\n rep(i, n) {\n int sum = 0, sqsum = 0;\n rep(j, 6) {\n sum += a[i][j];\n sqsum += a[i][j] * a[i][j];\n }\n sqsum *= 6;\n for (int c = m - 1; c >= 0; c--) {\n rep(s, S * 2 + 1) {\n if (dp[c][s] == llINF) continue;\n chmin(dp[c + 1][s + sum], dp[c][s] + sqsum + 2 * (s - S) * sum);\n chmin(dp[c + 1][s - sum], dp[c][s] + sqsum - 2 * (s - S) * sum);\n }\n }\n }\n cout << *min_element(all(dp[m])) << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m, k, p, q;\n string s;\n while (cin >> n >> m) {\n if (n == 0 && m == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 6550, "memory_kb": 23848, "score_of_the_acc": -0.9627, "final_rank": 14 }, { "submission_id": "aoj_1670_9323950", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define ull unsigned long long\n#define db double\n#define pii pair<int,int>\n#define pli pair<ll,int>\n#define pil pair<int,ll>\n#define pll pair<ll,ll>\n#define ti3 tuple<int,int,int>\n#define int128 __int128_t\n#define pii128 pair<int128,int128>\nconst int inf = 1 << 30;\nconst ll linf = (ll)4e18 + 10;\nconst db EPS = 1e-10;\nconst db pi = acos(-1);\ntemplate<class T> bool chmin(T& x, T y){\n if(x > y) {\n x = y;\n return true;\n } else return false;\n}\ntemplate<class T> bool chmax(T& x, T y){\n if(x < y) {\n x = y;\n return true;\n } else return false;\n}\n\n// overload macro\n#define CAT( A, B ) A ## B\n#define SELECT( NAME, NUM ) CAT( NAME, NUM )\n\n#define GET_COUNT( _1, _2, _3, _4, _5, _6 /* ad nauseam */, COUNT, ... ) COUNT\n#define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 6, 5, 4, 3, 2, 1 )\n\n#define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)\n\n// rep(overload)\n#define rep( ... ) VA_SELECT(rep, __VA_ARGS__)\n#define rep2(i, n) for (int i = 0; i < int(n); i++)\n#define rep3(i, a, b) for (int i = a; i < int(b); i++)\n#define rep4(i, a, b, c) for (int i = a; i < int(b); i += c)\n\n// repll(overload)\n#define repll( ... ) VA_SELECT(repll, __VA_ARGS__)\n#define repll2(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define repll3(i, a, b) for (ll i = a; i < (ll)(b); i++)\n#define repll4(i, a, b, c) for (ll i = a; i < (ll)(b); i += c)\n\n// rrep(overload)\n#define rrep( ... ) VA_SELECT(rrep, __VA_ARGS__)\n#define rrep2(i, n) for (int i = n - 1; i >= 0; i--)\n#define rrep3(i, a, b) for (int i = (b) - 1; i >= a; i--)\n#define rrep4(i, a, b, c) for (int i = (b) - 1; i >= a; i -= c)\n\n// rrepll(overload)\n#define rrepll( ... ) VA_SELECT(rrepll, __VA_ARGS__)\n#define rrepll2(i, n) for (ll i = (ll)(n) - 1; i >= 0ll; i--)\n#define rrepll3(i, a, b) for (ll i = (b) - 1; i >= (ll)(a); i--)\n#define rrepll4(i, a, b, c) for (ll i = (b) - 1; i >= (ll)(a); i -= c)\n\n// for_earh\n#define fore(e, v) for (auto&& e : v)\n\n// vector\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n\nint dp[61][30 * 60 * 60 * 6 + 1], a[61][6], b[61];\nbool solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) return false;\n int sum = 0;\n rep (i, n + 1) rep (j, n * 30 * 60 * 6 * 1) dp[i][j] = inf;\n dp[0][0] = 0;\n rep (i, n) {\n b[i] = 0;\n int val = 0;\n rep (j, 6) {\n cin >> a[i][j];\n val += a[i][j] * a[i][j] * 6;\n b[i] += a[i][j];\n }\n val -= b[i] * b[i];\n rrep (j, i + 1) rep(k, sum + 1) {\n chmin(dp[j + 1][k + b[i]], dp[j][k] + val);\n chmin(dp[j + 1][abs(k - b[i])], dp[j][k] + val);\n }\n sum += b[i];\n chmin(sum, n * 30 * 60 * 6);\n }\n int ans = inf;\n rep (i, sum + 1) {\n chmin(ans, i * i + dp[m][i]);\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n bool f = true;\n while (f) f = solve();\n return 0;\n}", "accuracy": 1, "time_ms": 2640, "memory_kb": 157868, "score_of_the_acc": -1.2545, "final_rank": 16 }, { "submission_id": "aoj_1670_9248586", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\nusing ll = long long;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nbool is_end = false;\n\nconst int nmax = 62;\nconst int BASE = 60 * 60;\nconst int amax = BASE * 2 + 1;\n\nconst int INF = 1e9;\n\nvoid solve()\n{\n int N, M; cin >> N >> M;\n if (N == 0 && M == 0)\n {\n is_end = true;\n return;\n }\n \n vector<int> linear(N, 0), square(N, 0);\n for (uint i = 0; i < N; ++i)\n {\n for (uint j = 0; j < 6; ++j)\n {\n int a; cin >> a;\n linear[i] += a;\n square[i] += a * a;\n }\n }\n \n vector<vector<int>> dp(nmax, vector<int>(amax, INF));\n vector<vector<int>> ndp = dp;\n \n dp[0][BASE + 0] = 0;\n for (uint i = 0; i < N; ++i)\n {\n for (uint st = 0; st <= M; ++st)\n {\n for (int sum = -BASE; sum < BASE; ++sum)\n {\n if (dp[st][BASE + sum] == INF) continue;\n int origin = dp[st][BASE + sum];\n \n chmin(ndp[st][BASE + sum], origin);\n // to A\n chmin(ndp[st + 1][BASE + sum + linear[i]], origin + 2 * linear[i] * sum + 6 * square[i]);\n // to B\n chmin(ndp[st + 1][BASE + sum - linear[i]], origin - 2 * linear[i] * sum + 6 * square[i]);\n }\n \n }\n swap(dp, ndp);\n }\n \n \n int res = INF;\n for (int a = -BASE; a < BASE; ++a)\n {\n chmin(res, dp[M][BASE + a]);\n }\n cout << res << endl;\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 3100, "memory_kb": 6308, "score_of_the_acc": -0.3242, "final_rank": 4 }, { "submission_id": "aoj_1670_9078245", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int n, int m) {\n vector<pair<int,int>> A(n);\n for(int i : rep(n)) {\n vector<int> d = in(6);\n int s1 = 0, s2 = 0;\n for(int k : rep(6)) {\n s1 += d[k];\n s2 += d[k] * d[k];\n }\n A[i] = {s1, s2 * 6 - s1 * s1};\n }\n const int e_max = 60 * 60;\n const i64 INF = 1e18;\n vector dp(m + 1, vector(e_max + 1, INF));\n dp[0][0] = 0;\n for(auto [mean, var] : A) {\n vector nt = dp;\n for(int c = 0; c < m; c++) {\n for(int e = 0; e <= e_max; e++) {\n if( e + mean <= e_max) chmin(nt[c + 1][ e + mean ], dp[c][e] + var);\n if(abs(e - mean) <= e_max) chmin(nt[c + 1][abs(e - mean)], dp[c][e] + var);\n }\n }\n dp = move(nt);\n }\n\n i64 ans = INF;\n for(int e = 0; e <= e_max; e++) chmin(ans, e * e + dp[m][e]);\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(), m = in();\n if(make_pair(n, m) == make_pair(0, 0)) return 0;\n print(solve(n, m));\n }\n}", "accuracy": 1, "time_ms": 1890, "memory_kb": 6664, "score_of_the_acc": -0.1433, "final_rank": 2 }, { "submission_id": "aoj_1670_8211958", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(1)[]{\n unsigned long N, M;\n cin >> N >> M;\n if(!(N + M))exit(0);\n vector<array<unsigned long, 6>> die(N);\n vector<pair<unsigned long, unsigned long>> mean_variance;\n mean_variance.reserve(N);\n for(auto&& [a, b, c, d, e, f] : die){\n cin >> a >> b >> c >> d >> e >> f;\n const auto S{a + b + c + d + e + f};\n mean_variance.emplace_back(S, (a * a + b * b + c * c + d * d + e * e + f * f) * 6 - S * S);\n }\n vector<vector<unsigned long>> dp(M + 1);\n for(unsigned long i{}; i <= M; ++i)\n dp[i] = vector<unsigned long>(i * 360 + 1, numeric_limits<unsigned long>::max() / 2);\n dp[0][0] = 0;\n unsigned long j{};\n for(const auto& [m, v] : mean_variance){\n ++j;\n for(unsigned long i{min(M, j)}; i--;){\n for(unsigned long j{}; j <= i * 360; ++j){\n dp[i + 1][j + m] = min(dp[i + 1][j + m], dp[i][j] + v);\n dp[i + 1][max(j, m) - min(j, m)] = min(dp[i + 1][max(j, m) - min(j, m)], dp[i][j] + v);\n }\n }\n }\n unsigned long ans{numeric_limits<unsigned long>::max()};\n for(unsigned long i{}, i_upto{size(dp.back())}; i < i_upto; ++i){\n ans = min(dp.back()[i] + i * i, ans);\n }\n cout << ans << endl;\n return ;\n }();\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 8388, "score_of_the_acc": -0.0137, "final_rank": 1 } ]
aoj_1678_cpp
Problem G Two Sets of Cards As an entertainment after the programming contest, you hold a game event for the participants. Two sets of cards, red ones and blue ones, are used in the game. Both of the two sets have the same number of cards as the number of the participants, and each card has one integer written on it. It is known that the contents of these two sets are the same; the integers on the red cards are the same as those on the blue cards as multisets. Even though, you don't know what integers are actually written on the cards. The same integer may be written on two or more cards of the same color, and the integers may be negative. You distributed one red card and one blue card to each of the participants. As the game result depends on the sum of the integers on the two cards distributed to each participant, you asked all the participants to declare their sums. Your task is to find a possible card distribution consistent with the participants' declarations, that is, a combination of integers on the cards distributed to each participant. Note that such a combination might not exist, as the participants may commit miscalculations. In such a case, report that there is no consistent combination. Input The input consists of multiple datasets, each in the following format. The number of datasets does not exceed 100. n s 1 s 2 ⋯ s n Here, n is the number of the participants, an integer between 1 and 70, inclusive. Each of s i ( i = 1, , n ) is the declared sum of the integers on the two cards distributed to the i -th participant, an integer between −150 and 150, inclusive. The end of the input is indicated by a line consisting of a zero. Output For each dataset, if there exists no combination of integers on the cards distributed to each participant that is consistent with the participants' declarations, output "No" in one line. Otherwise, output "Yes" in one line followed by one possible consistent combination in the following format. a 1 a 2 ⋯ a n b 1 b 2 ⋯ b n Here, for i = 1, , n, a i and b i represent the integers on the red and blue cards, respectively, distributed to the i -th participant. Each of a i and b i must be an integer between −10 9 and 10 9 , inclusive. It can be proved that, when one or more consistent combinations exist, there also exists a consistent combination satisfying this condition. If there are two or more such combinations, you may output any one of them. Sample Input 3 7 -2 3 3 4 8 8 4 1 2 4 8 6 -6 -2 3 2 9 -4 1 -100 0 Output for the Sample Input Yes 1 -3 6 6 1 -3 Yes 2 4 4 2 4 4 No Yes 3 -1 4 -1 5 -9 -9 -1 -1 3 4 5 Yes -50 -50
[ { "submission_id": "aoj_1678_10781175", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main()\n{\n\tint n;\n\tcin>>n;\n//\tn=10;\n\twhile(n)\n\t{\n//\t\tvector<int>a0(n+1),b0;\n//\t\tfor(int i=1;i<=n;i++)a0[i]=rand()%151-75;\n//\t\tb0=a0;\n//\t\tfor(int i=n;i>1;i--)\n//\t\t\tswap(b0[i],b0[rand()%n+1]);\n\t\tvector<int>s(n+1);\n\t\tint sum=0,ff=0,cnt1=0;\n\t\tfor(int i=1;i<=n;i++)\n\t\t{\n//\t\t\ts[i]=a0[i]+b0[i];\n//\t\t\tprintf(\"%d \",s[i]);\n\t\t\tscanf(\"%d\",&s[i]);\n\t\t\ts[i]+=150;\n\t\t\tsum+=s[i];\n\t\t\tif(s[i]%2==0)ff=1;\n\t\t\tif(s[i]%2==1)cnt1++;\n\t\t}\n\t\tvector<int>s0=s;\n\t\tif(ff)\n\t\t{\n\t\t\tif(sum%2==0)\n\t\t\t{\n\t\t\t\tputs(\"Yes\");\n\t\t\t\tif(!cnt1)\n\t\t\t\t{\n\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d%c\",s[i]/2-75,\" \\n\"[i==n]);\n\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d%c\",s[i]/2-75,\" \\n\"[i==n]);\n//\t\t\t\t\tfor(int i=1;i<=n;i++)assert(s[i]/2-75+s[i]/2-75==a0[i]+b0[i]);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tvector<int>f;\n\t\t\t\t\tfor(int i=1;i<=n;i++)if(s[i]%2!=0)f.push_back(i);\n\t\t\t\t\tfor(int i=1;i<=n;i++)if(s[i]%2==0)\n\t\t\t\t\t{\n\t\t\t\t\t\tint sum1=0;\n\t\t\t\t\t\tvector<int>g;\n\t\t\t\t\t\tfor(int j=f.size()/2;j<f.size();j++)\n\t\t\t\t\t\t\tg.push_back(f[j]);\n\t\t\t\t\t\tf.resize(f.size()/2);\n\t\t\t\t\t\tfor(int j=0;j<f.size();j+=2)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tsum1-=s[f[j]];\n\t\t\t\t\t\t\tif(j+1<g.size())sum1+=s[f[j+1]];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(int j=0;j<g.size();j+=2)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tsum1-=s[g[j]];\n\t\t\t\t\t\t\tif(j+1<g.size())sum1+=s[g[j+1]];\n\t\t\t\t\t\t}\n//\t\t\t\t\t\tprintf(\"%d\\n\",sum1);\n\t\t\t\t\t\tif(f.size()%2)sum1=-s[i]-sum1;\n\t\t\t\t\t\telse sum1=s[i]-sum1;\n//\t\t\t\t\t\tprintf(\"%d\\n\",sum1);\n\t\t\t\t\t\tf.push_back(i);\n\t\t\t\t\t\tg.push_back(i);\n\t\t\t\t\t\tvector<int>ansa(n+1),ansb(n+1);\n\t\t\t\t\t\tansa[f[0]]=ansb[g[0]]=sum1/2;\n\t\t\t\t\t\ts[f[0]]-=ansa[f[0]],s[g[0]]-=ansb[g[0]];\n\t\t\t\t\t\tfor(int i=1;i<f.size();i++)\n\t\t\t\t\t\t{\n//\t\t\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d \",s[i]);puts(\"\");\n\t\t\t\t\t\t\tansa[f[i]]=s[f[i-1]],ansb[f[i-1]]=s[f[i-1]];\n\t\t\t\t\t\t\ts[f[i-1]]-=ansb[f[i-1]],s[f[i]]-=ansa[f[i]];\n\t\t\t\t\t\t}\n//\t\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d \",s[i]);puts(\"\");\n\t\t\t\t\t\tfor(int i=1;i<g.size();i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tansb[g[i]]=s[g[i-1]],ansa[g[i-1]]=s[g[i-1]];\n\t\t\t\t\t\t\ts[g[i-1]]-=ansa[g[i-1]],s[g[i]]-=ansb[g[i]];\n\t\t\t\t\t\t}\n//\t\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d \",s[i]);puts(\"\");\n\t\t\t\t\t\tfor(int j=i+1;j<=n;j++)if(s0[j]%2==0)ansa[j]=ansb[j]=s[j]/2;\n\t\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d%c\",ansa[i]-75,\" \\n\"[i==n]);\n\t\t\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d%c\",ansb[i]-75,\" \\n\"[i==n]);\n//\t\t\t\t\t\tfor(int i=1;i<=n;i++)assert(ansa[i]-75+ansb[i]-75==a0[i]+b0[i]);\n\t\t\t\t\t\t\t\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse puts(\"No\");//,assert(0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tvector<vector<bitset<21001>>>dp(n+1,vector<bitset<21001>>(n*2+1));\n\t\t\tdp[0][0][0]=1;\n\t\t\tfor(int i=0;i<n;i++)if(s[i+1]%2)\n\t\t\t{\n\t\t\t\tdp[i+1][0]=dp[i][0];\n\t\t\t\tdp[i+1][1]=dp[i][1];\n\t\t\t\tfor(int j=2;j<n*2+1;j++)\n\t\t\t\t\tdp[i+1][j]=(dp[i][j]|(dp[i][j-2]<<s[i+1]));\n\t\t\t}\n\t\t\telse \n\t\t\t{\n\t\t\t\tdp[i+1][0]=dp[i][0];\n\t\t\t\tdp[i+1][1]=(dp[i][1]|(dp[i][0]<<s[i+1]/2));\n\t\t\t\tfor(int j=2;j<n*2+1;j++)\n\t\t\t\t\tdp[i+1][j]=(dp[i][j]|(dp[i][j-2]<<s[i+1])|(dp[i][j-1]<<s[i+1]/2));\n\t\t\t}\n\t\t\tif(sum%2||!dp[n][n][sum/2])puts(\"No\");//,assert(0);\n\t\t\telse\n\t\t\t{\n\t\t\t\tvector<int>b,c,vis(n+1);\n\t\t\t\tint j=sum/2,k=n;\n\t\t\t\tfor(int i=n;i>0;i--)\n\t\t\t\t\tif(j>=s[i]&&k>=2&&dp[i-1][k-2][j-s[i]])\n\t\t\t\t\t\tb.push_back(i),j-=s[i],k-=2;\n\t\t\t\t\telse if(dp[i-1][k][j])c.push_back(i);\n\t\t\t\t\telse vis[i]=1,j-=s[i]/2,k--;\n\t\t\t\tassert(b.size()==c.size());\n\t\t\t\tvector<int>ansa(n+1),ansb(n+1);\n\t\t\t\tfor(int i=1;i<=n;i++)if(vis[i])ansa[i]=s[i]/2,ansb[i]=s[i]/2;\n\t//\t\t\tfor(int i=1;i<=n;i++)printf(\"%d \",ansa[i]);puts(\"\");\n\t//\t\t\tfor(int i=1;i<=n;i++)printf(\"%d \",ansb[i]);puts(\"\");\n\t//\t\t\tfor(int x:b)printf(\"%d \",x);puts(\"\");\n\t//\t\t\tfor(int x:c)printf(\"%d \",x);puts(\"\");\n\t\t\t\tfor(int i=0;i<b.size();i++)\n\t\t\t\t{\n\t\t\t\t\tansa[b[i]]=ansb[c[i]]=s[b[i]];\n\t\t\t\t\ts[b[i]]-=ansa[b[i]],s[c[i]]-=ansb[c[i]];\n\t\t\t\t\tansa[c[i]]=ansb[b[(i+1)%b.size()]]=s[c[i]];\n\t\t\t\t\ts[c[i]]-=ansa[c[i]],s[b[(i+1)%b.size()]]-=ansb[b[(i+1)%b.size()]];\n\t\t\t\t}\n\t\t\t\tputs(\"Yes\");\n\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d%c\",ansa[i]-75,\" \\n\"[i==n]);\n\t\t\t\tfor(int i=1;i<=n;i++)printf(\"%d%c\",ansb[i]-75,\" \\n\"[i==n]);\n//\t\t\t\tfor(int i=1;i<=n;i++)assert(ansa[i]-75+ansb[i]-75==a0[i]+b0[i]);\n\t\t\t}\n\t\t}\n\t\tcin>>n;\n\t}\n\treturn 0;\n}\n/*\n 3\n 7 -2 3\n 3\n 4 8 8\n 4\n 1 2 4 8\n 6\n -6 -2 3 2 9 -4\n 1\n -100\n 0\n*/", "accuracy": 1, "time_ms": 630, "memory_kb": 29388, "score_of_the_acc": -0.0945, "final_rank": 1 }, { "submission_id": "aoj_1678_10683346", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<int>S(N);\n rep(i,0,N)cin>>S[i];\n int sum=0;\n rep(i,0,N)sum+=S[i];\n if(sum%2!=0){\n cout<<\"No\\n\";\n continue;\n }\n sum/=2;\n if(N%2==1){\n vector<int>A;\n rep(i,0,N){\n int cnt=sum;\n rep(j,0,N/2){\n cnt-=S[(i+2*(j+1))%N];\n }\n A.push_back(cnt);\n }\n cout<<\"Yes\"<<endl;\n rep(i,0,N)cout<<A[i]<<\" \";\n cout<<endl;\n rep(i,0,N)cout<<A[(i-1+N)%N]<<\" \";\n cout<<endl;\n }else{\n bool flag=false;\n rep(x,0,N){\n if(S[x]%2==0){\n vector<int>C;\n vector<int>P,Q;\n rep(i,0,N){\n if(i==x)continue;\n P.push_back(S[i]);\n Q.push_back(i);\n }\n rep(i,0,N-1){\n int cnt=sum-S[x]/2;\n rep(j,0,(N-1)/2){\n cnt-=S[Q[(i+2*(j+1))%(N-1)]];\n }\n C.push_back(cnt);\n }\n vector<int>D;\n rep(i,0,N-1)D.push_back(C[(i-1+N-1)%(N-1)]);\n vector<int>A(N),B(N);\n A[x]=S[x]/2,B[x]=S[x]/2;\n int now=0;\n rep(i,0,N){\n if(i==x)continue;\n A[i]=C[now],B[i]=D[now];\n now++;\n }\n cout<<\"Yes\"<<endl;\n rep(i,0,N)cout<<A[i]<<\" \";\n cout<<endl;\n rep(i,0,N)cout<<B[i]<<\" \";\n cout<<endl;\n flag=true;\n break;\n }\n }\n if(!flag){\n vector dp(N+1,vector<unordered_map<int,bool>>(N+1));\n dp[0][0][0]=true;\n rep(i,0,N){\n rep(k,0,i+1){\n for(auto[j,f]:dp[i][k]){\n dp[i+1][k+1][j+S[i]]=true;\n dp[i+1][k][j]=true;\n }\n }\n }\n if(!dp[N][N/2][sum]){\n cout<<\"No\"<<endl;\n }else{\n vector<int>V,W;\n int p=N/2,q=sum;\n rrep(i,0,N){\n if(dp[i][p][q]){\n W.push_back(i);\n continue;\n }\n if(p>0&&dp[i][p-1][q-S[i]]){\n p--;\n q-=S[i];\n V.push_back(i);\n continue;\n }\n }\n reverse(all(V));\n reverse(all(W));\n vector<int>A(N),B(N);\n A[V[0]]=0;\n rep(i,0,N){\n if(i%2==0){\n B[V[i/2]]=S[V[i/2]]-A[V[i/2]];\n A[W[i/2]]=B[V[i/2]];\n }else{\n B[W[i/2]]=S[W[i/2]]-A[W[i/2]];\n if(i!=N-1)A[V[(i+1)/2]]=B[W[i/2]];\n }\n }\n cout<<\"Yes\"<<endl;\n rep(i,0,N)cout<<A[i]<<\" \";\n cout<<endl;\n rep(i,0,N)cout<<B[i]<<\" \";\n cout<<endl;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 2810, "memory_kb": 130816, "score_of_the_acc": -1.2705, "final_rank": 5 }, { "submission_id": "aoj_1678_10668245", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vpdbg(a) rep(ii,a.size()){cout<<\"{\"<<a[ii].first<<\",\"<<a[ii].second<<\"} \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 4000000000000000000LL\n#define mod 998244353LL\n//#define mod 1000000007LL\n#define eps 0.000000001\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\n//#include<boost/multiprecision/cpp_int.hpp>\n//#define bbi boost::multiprecision::cpp_int\n//#include<atcoder/lazysegtree>\n\n\n//整数同士の累乗の計算をする。\nll power(ll A, ll B) {\n\tll result = 1;\n\tfor (ll i=0;i<B;i++){\n\t\tresult *= A;\n\t}\n\treturn result;\n}\n\n// nのk乗をmodで割った余りを計算\nll power_mod(ll n, ll k){\n\tlong long result = 1;\n\twhile (k > 0){\n\t\tif ((k&1) ==1)result=(result*n)%mod;\n\t\tn=n*n%mod;\n\t\tk >>= 1;\n\t}\n\treturn result;\n}\n\n\n//受け取った2次元文字の外側に、文字pをコーティングする。\nvector<string> pad(vector<string> &s,char p){\n\tll h=s.size();\n\tll w=s[0].size();\n\tvector<string> res(h+2,string(w+2,p));\n\trep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];\n\treturn res;\n}\n\n// Union-Find\nstruct UnionFind {\n\tvector<int> par, siz;\n\tUnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t// x を含むグループと y を含むグループとを併合する\n\tbool unite(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false; \n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\ntemplate <typename T>\nstruct offset_vector {\n vector<T> data;\n const int offset;\n\n /** @brief 負のインデックスをサポートするベクトル\n * @param n 正のインデックスのサイズ\n * @param offset 負のインデックスに拡張する数\n * @param init_val 初期値(デフォルトはT())\n */\n offset_vector(int n, int offset = 0, T init_val = T()) : offset(offset) {\n assert(0 <= n && \"offset_vectorの初期化時のnは0以上でなければなりません\");\n assert(0 <= offset && \"offset_vectorの初期化時のoffsetは0以上でなければなりません\");\n data.resize(n + offset, init_val);\n }\n\n T& operator[](int i) {\n assert(0 <= i + offset && \"offset_vectorへの負方向の範囲外参照です\");\n assert(i + offset < data.size() && \"offset_vectorへの正方向の範囲外参照です\");\n return data[i + offset];\n }\n\n auto begin() {\n return data.begin();\n }\n auto end() {\n return data.end();\n }\n auto rbegin() {\n return data.rbegin();\n }\n auto rend() {\n return data.rend();\n }\n auto size() const {\n return data.size();\n }\n size_t positive_size() const {\n return data.size() - offset;\n }\n auto empty() const {\n return data.empty();\n }\n auto front() {\n assert(!data.empty() && \"offset_vectorが空です\");\n return data.front();\n }\n auto back() {\n assert(!data.empty() && \"offset_vectorが空です\");\n return data.back();\n }\n\n void push_back(const T& value) {\n data.push_back(value);\n }\n void pop_back() {\n assert(offset < data.size() && \"offset_vectorのpop_backは、負の要素に対しては使用できません\");\n assert(!data.empty() && \"offset_vectorのpop_backは、空のベクトルに対しては使用できません\");\n data.pop_back();\n }\n\n friend ostream& operator<<(ostream& os, const offset_vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n if (i != 0) os << \" \";\n os << v.data[i];\n }\n os << \"\\n\";\n return os;\n }\n};\n\n//グリッド問題等用\nvl dx={1,0,-1,0};\nvl dy={0,1,0,-1};\n\n\nll solve(){\n\tll n;\n\tcin>>n;\n\tif(n==0){return 1;}\n\n\tvl a(n);\n\tvl odd;\n\tvl even;\n\tll allsum=0;\n\trep(i,n){\n\t\tcin>>a[i];\n\t\tallsum+=a[i];\n\t\tif(a[i]%2==0)even.push_back(i);\n\t\telse odd.push_back(i);\n\t}\n\n\tif(odd.size()%2==1){\n\t\tcout<<\"No\"<<endl;\n\t\treturn 0;\n\t}\n\n\t//1個でも偶数があればRotationで構築可能\n\tif(even.size()!=0){\n\t\tvl red(n);\n\t\tvl blue(n);\n\t\twhile(even.size()>1){\n\t\t\tred[even.back()]=a[even.back()]/2;\n\t\t\tblue[even.back()]=a[even.back()]/2;\n\t\t\teven.pop_back();\n\t\t}\n\t\todd.push_back(even[0]);\n\n\t\tvl abcd(odd.size());\n\t\t// abcd[0]を求める\n\t\tll sums=0;\n\t\trep(i,odd.size()){\n\t\t\tif(i%2==0){\n\t\t\t\tsums+=a[odd[i]];\n\t\t\t}else{\n\t\t\t\tsums-=a[odd[i]];\n\t\t\t}\n\t\t}\n\t\tabcd[0]=sums/2;\n\t\trep(i,odd.size()-1){\n\t\t\tabcd[i+1]=a[odd[i]]-abcd[i];\n\t\t}\n\n\t\trep(i,odd.size()){\n\t\t\tred[odd[i]]=abcd[i];\n\t\t\tblue[odd[i]]=abcd[(i+1)%odd.size()];\n\t\t}\n\t\tcout<<\"Yes\"<<endl;\n\t\tvdbg(red);\n\t\tvdbg(blue);\n\t\treturn 0;\n\t}\n\n\t\n\t//奇数のみの場合\n\tvector<vector<offset_vector<pair<bool,bool>>>> dp(n+1,vector<offset_vector<pair<bool,bool>>>(n/2+1,offset_vector<pair<bool,bool>>(150*n+1,150*n,{false,false})));\n\tdp[0][0][0]={true,false};\n\trep(i,n){\n\t\trep(j,n/2){\n\t\t\tloop(k,-150*i,150*i){\n\t\t\t\tif(!dp[i][j][k].first)continue;\n\n\t\t\t\t//使わない場合\n\t\t\t\tdp[i+1][j][k]={true,false};\n\n\t\t\t\t//使う場合\n\t\t\t\tdp[i+1][j+1][k+a[i]]={true,true};\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n\tif(!dp[n][n/2][allsum/2].first){\n\t\tcout<<\"No\"<<endl;\n\t\treturn 0;\n\t}\n\n\tvl g1;\n\tvl g2;\n\tll nowj=n/2;\n\tll nowk=allsum/2;\n\trrep(i,n){\n\t\tif(dp[i+1][nowj][nowk].second){\n\t\t\tnowj--;\n\t\t\tnowk-=a[i];\n\t\t\tg1.push_back(i);\n\t\t}else{\n\t\t\tg2.push_back(i);\n\t\t}\n\t}\n\t//vdbg(g1);\n\t//vdbg(g2);\n\n\tvl red(n);\n\tvl blue(n);\n\tvl abcd(n);\n\tabcd[0]=0;\n\n\trep(i,n-1){\n\t\tif(i%2==0){\n\t\t\tabcd[i+1]=a[g1[i/2]]-abcd[i];\n\t\t}else{\n\t\t\tabcd[i+1]=a[g2[i/2]]-abcd[i];\n\t\t}\n\t}\n\n\trep(i,n){\n\t\tif(i%2==0){\n\t\t\tred[g1[i/2]]=abcd[i];\n\t\t\tblue[g1[i/2]]=abcd[(i+1)%n];\n\t\t}else{\n\t\t\tred[g2[i/2]]=abcd[i];\n\t\t\tblue[g2[i/2]]=abcd[(i+1)%n];\n\t\t}\n\t}\n\tcout<<\"Yes\"<<endl;\n\tvdbg(red);\n\tvdbg(blue);\n\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(solve()==0);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2850, "memory_kb": 109544, "score_of_the_acc": -1.1797, "final_rank": 4 }, { "submission_id": "aoj_1678_10623963", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\n#define vl vector<ll>\n#define vll vector<ll>\n#define rep(i,n) for(int i = 0;i < n;i++)\n#define per(i,n) for(int i = n - 1;i >= 0;i--)\n#define fore(e,v) for(auto &&e,v)\n#define si(a) (int)(size(a))\n#define all(a) begin(a),end(a)\n#define lb(v,x) (lower_bound(all(v),x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T , typename S> bool chmin(T &a, const S& b){\n\treturn a > b ? a = b, 1:0;\n}\ntemplate<typename T , typename S> bool chmax(T &a, const S& b){\n\treturn a < b ? a = b, 1:0;\n}\n\nconst ll inf = 3e18;\n\nstruct _{\n\t_(){\n\t\tcin.tie(0) -> sync_with_stdio(0),cout.tie(0);\n\t}\n}__;\n\nbool solve(ll n,vll s,vll &a, vll &b){\n\tll sum = 0;\n\t{\n\t\trep(i,n)sum += s[i];\n\t\tif(sum % 2 != 0)return false;\n\t}\n\tif(n % 2 == 1){\n\t\tll sm = 0;\n\t\trep(i,n){\n\t\t\tif(i % 2 == 0)sm += s[i];\n\t\t\telse sm -= s[i];\n\t\t}\n\t\tassert(sum % 2 == 0);\n\t\tll x = sm / 2;\n\t\ta[0] = b[n - 1] = x;\n\t\trep(i,n - 1){\n\t\t\tll y = s[i] - x;\n\t\t\ta[i + 1] = b[i] = y;\n\t\t\tx = y;\n\t\t}\n\t\t// rep(i,n)assert(s[i] == a[i] + b[i]);\n\t\t// assert(s[n - 1] == a[n - 1] + b[n - 1]);\n\t\treturn true;\n\t}\n\trep(i,n)if(s[i] % 2 == 0){\n\t\tvll s2;\n\t\trep(j,n)if(j != i)s2.eb(s[j]);\n\t\tvll a2(n - 1),b2(n - 1);\n\t\tsolve(n - 1,s2,a2,b2);\n\t\tll k = 0;\n\t\trep(j,n){\n\t\t\tif(i == j){\n\t\t\t\ta[j] = b[j] = s[j] / 2;\n\t\t\t}else{\n\t\t\t\ta[j] = a2[k];\n\t\t\t\tb[j] = b2[k];\n\t\t\t\tk++;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\t\n\tvll s1,s2;\n\t{\n\t\tll base = 150 * n + 10;\n\t\tbool dp[n + 1][n + 1][2 * base];\n\t\trep(i,n + 1)rep(j,n + 1)rep(k,2 * base)dp[i][j][k] = 0;\n\t\tdp[0][0][base] = 1;\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\trep(sm,2 * base){\n\t\t\t\t\tif(dp[i][j][sm]){\n\t\t\t\t\t\tdp[i + 1][j][sm] = 1;\n\t\t\t\t\t\tif(0 <= sm + s[i] && sm + s[i] < 2 * base)dp[i + 1][j + 1][sm + s[i]] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(dp[n][n / 2][base + sum / 2] == 0)return false;\n\t\t// cout<<\"here\"<<endl;\n\t\t// cout<<base + sum / 2<<\" \"<<n<<\" \"<<n / 2<<\" \"<<dp[n][n / 2][base + sum / 2]<<endl;\n\t\tll j = n / 2,sm = base + sum / 2;\n\t\tper(i,n){\n\t\t\tif(dp[i][j][sm]){\n\t\t\t\ts2.eb(i);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tj--,sm -= s[i];\n\t\t\t\ts1.eb(i);\n\t\t\t}\n\t\t}\n\t\t// cout<<\"s1:\"; rep(i,n / 2)cout<<s1[i]<<\" \";cout<<endl;\n\t\t// cout<<\"s2:\"; rep(i,n / 2)cout<<s2[i]<<\" \";cout<<endl;\n\t\tassert(si(s1) == n / 2);\n\t\tassert(si(s2) == n / 2);\n\t\t{\n\t\t\tll ss = 0;\n\t\t\tfor(auto &&e : s1)ss += s[e];\n\t\t\tassert(ss == sum / 2);\n\t\t}\n\t}\n\tvll p(n);\n\t{\n\t\trep(i,n / 2){\n\t\t\tp[i * 2] = s1[i];\n\t\t\tp[i * 2 + 1] = s2[i];\n\t\t}\n\t}\n\n\tll x = 0;\n\ta[p[0]] = b[p[n - 1]] = x;\n\trep(i,n - 1){\n\t\tll y = s[p[i]] - x;\n\t\ta[p[i + 1]] = b[p[i]] = y;\n\t\tx = y;\n\t}\n\trep(i,n)assert(s[i] == a[i] + b[i]);\n\treturn true;\n}\n\nint main(){\n\twhile(1){\n\t\tll n;\n\t\tcin>>n;\n\t\tif(n == 0)break;\n\t\tvll s(n);\n\t\trep(i,n)cin>>s[i];\n\t\tvll a(n),b(n);\n\t\tif(solve(n,s,a,b)){\n\t\t\t\n\t\t\tcout<<\"Yes\"<<endl;\n\t\t\trep(i,n)cout<<a[i]<<\" \\n\"[i == n - 1];\n\t\t\trep(i,n)cout<<b[i]<<\" \\n\"[i == n - 1];\n\t\t\tcout<<flush;\n\n\t\t\t// rep(i,n)assert(s[i] == a[i] + b[i]);\n\t\t}else{\n\t\t\tcout<<\"No\"<<endl;\n\t\t}\n\n\t}\n}", "accuracy": 1, "time_ms": 1620, "memory_kb": 107000, "score_of_the_acc": -0.7818, "final_rank": 2 }, { "submission_id": "aoj_1678_10049475", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solving = true;\n\nvector<bool> knapsack(vector<int> v) {\n const int n = v.size();\n const int L = 150 * 70 / 2;\n const int S = accumulate(v.begin(), v.end(), 0);\n const pair<int, int> INF = make_pair(-1, -1);\n vector dp(n + 1, vector(n / 2 + 1, vector<pair<int, int>>(2 * L + 1, INF)));\n dp[1][0][v[0] + L] = make_pair(0, v[0]);\n dp[1][1][-v[0] + L] = make_pair(1, -v[0]);\n for(int i = 1; i < n; i++) {\n for(int j = 0; j <= n / 2; j++) {\n for(int s = -L; s <= L; s++) {\n if(dp[i][j][s + L] == INF) continue;\n int sp = s + v[i], sm = s - v[i];\n if(-L <= sp && sp <= L) dp[i + 1][j][sp + L] = make_pair(0, v[i]);\n if(-L <= sm && sm <= L && j < n / 2) dp[i + 1][j + 1][sm + L] = make_pair(1, -v[i]);\n }\n }\n }\n if(dp[n][n / 2][0 + L].first == -1) {\n vector<bool> ret;\n return ret;\n }\n vector<bool> ret(n);\n for(int i = n, j = n / 2, s = 0; i > 0; i--) {\n ret[i - 1] = dp[i][j][s + L].first;\n int j2 = j - dp[i][j][s + L].first, s2 = s - dp[i][j][s + L].second;\n j = j2, s = s2;\n }\n return ret;\n}\n\nvector<pair<int, int>> construct(vector<int> v) {\n const int n = v.size();\n vector<pair<int, int>> ret(n);\n ret[0] = make_pair(0, v[0]);\n for(int i = 1; i < n; i++) {\n const int x = ret[i - 1].second;\n ret[i] = make_pair(x, v[i] - x);\n }\n assert(ret[n - 1].second % 2 == 0);\n const int k = ret[n - 1].second / 2;\n for(int i = 0; i < n; i++) {\n ret[i].first += k * (i % 2 == 0 ? 1 : -1);\n ret[i].second += k * (i % 2 == 0 ? -1 : 1);\n }\n return ret;\n}\n\nvoid solve() {\n int n;\n cin >> n;\n if(n == 0) {\n solving = false;\n return;\n }\n int cnt_odd = 0;\n vector<int> s(n);\n for(int i = 0; i < n; i++) {\n cin >> s[i];\n if(abs(s[i]) % 2 == 1) cnt_odd++;\n }\n if(cnt_odd % 2 == 1) {\n cout << \"No\" << endl;\n return;\n }\n vector<int> ev, ei;\n vector<int> ov, oi;\n for(int i = 0; i < n; i++) {\n if(abs(s[i]) % 2 == 0) {\n ev.push_back(s[i]);\n ei.push_back(i);\n }else {\n ov.push_back(s[i]);\n oi.push_back(i);\n }\n }\n if(cnt_odd == n) {\n vector<bool> res = knapsack(ov);\n if(res.empty()) {\n cout << \"No\" << \"\\n\";\n return;\n }\n vector<int> v(ov.size()), id(oi.size());\n for(int i = 0, p = 0, q = 1; i < n; i++) {\n if(res[i]) {\n v[p] = ov[i], id[p] = oi[i], p += 2;\n }else {\n v[q] = ov[i], id[q] = oi[i], q += 2;\n }\n }\n vector<int> a(n), b(n);\n vector<pair<int, int>> res2 = construct(v);\n for(int i = 0; i < ov.size(); i++) {\n a[id[i]] = res2[i].first, b[id[i]] = res2[i].second;\n }\n for(int i = 0; i < ev.size(); i++) {\n a[ei[i]] = b[ei[i]] = s[ei[i]] / 2;\n }\n cout << \"Yes\" << \"\\n\";\n for(int i = 0; i < n; i++) cout << a[i] << \" \"; cout << \"\\n\";\n for(int i = 0; i < n; i++) cout << b[i] << \" \"; cout << \"\\n\";\n }else {\n vector<int> a(n), b(n);\n {\n vector<int> v = ov;\n v.push_back(ev[0]);\n vector<pair<int, int>> res = construct(v);\n for(int i = 0; i < ov.size(); i++) {\n a[oi[i]] = res[i].first, b[oi[i]] = res[i].second;\n }\n {\n a[ei[0]] = res.back().first, b[ei[0]] = res.back().second;\n }\n }\n {\n for(int i = 1; i < ev.size(); i++) {\n a[ei[i]] = b[ei[i]] = ev[i] / 2;\n }\n }\n cout << \"Yes\" << \"\\n\";\n for(int i = 0; i < n; i++) cout << a[i] << \" \"; cout << \"\\n\";\n for(int i = 0; i < n; i++) cout << b[i] << \" \"; cout << \"\\n\";\n }\n}\n\nint main() {\n while(solving) solve();\n}", "accuracy": 1, "time_ms": 3790, "memory_kb": 215808, "score_of_the_acc": -1.9906, "final_rank": 7 }, { "submission_id": "aoj_1678_9861500", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing i64 = long long;\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n\n int n;\n \n auto solve = [&]() {\n vector<int> s(n), a(n), b(n);\n for (int i = 0; i < n; i++) {\n cin >> s[i];\n }\n\n vector pos(2, vector<int>());\n for (int i = 0; i < n; i++) {\n pos[abs(s[i]) % 2].push_back(i);\n }\n\n if (pos[1].size() % 2 == 1) {\n cout << \"No\\n\";\n return;\n } else if (!pos[0].empty()) {\n int e = pos[0].back();\n pos[0].pop_back();\n pos[1].push_back(e);\n for (int i : pos[0]) {\n a[i] = b[i] = s[i] / 2;\n }\n int z = 0;\n for (int i : pos[1]) {\n z += s[i];\n }\n assert(z % 2 == 0);\n z /= 2;\n int sz = pos[1].size();\n for (int i = 0; i < sz; i++) {\n if (i == 0) {\n for (int k = 0; k < sz; k += 2) {\n a[pos[1][i]] += s[pos[1][k]];\n }\n a[pos[1][i]] -= z;\n } else {\n a[pos[1][i]] = s[pos[1][i - 1]] - a[pos[1][i - 1]];\n }\n }\n for (int i = 0; i < sz; i++) {\n b[pos[1][i]] = a[pos[1][(i + 1) % sz]];\n }\n } else {\n int sz = pos[1].size();\n const int V = 150, S = V * sz / 2;\n vector dp(sz + 1, vector(sz + 1, vector<int>(2 * S + 1)));\n dp[0][0][S] = true;\n for (int i = 0; i < sz; i++) {\n int v = s[pos[1][i]];\n for (int j = 0; j <= i; j++) {\n for (int k = 0; k <= 2 * S; k++) {\n if (dp[i][j][k]) {\n if (0 <= k + v && k + v <= 2 * S) {\n dp[i + 1][j + 1][k + v] = true;\n }\n if (0 <= k - v && k - v <= 2 * S) {\n dp[i + 1][j][k - v] = true;\n }\n }\n }\n }\n }\n if (!dp[sz][sz / 2][S]) {\n cout << \"No\\n\";\n return;\n }\n vector part(2, vector<int>());\n for (int i = sz - 1, j = sz / 2, k = S; i >= 0; i--) {\n int v = s[pos[1][i]];\n if (0 <= k - v && k - v <= 2 * S && j > 0 && dp[i][j - 1][k - v]) {\n j--;\n k -= v;\n part[0].push_back(pos[1][i]);\n } else if (0 <= k + v && k + v <= 2 * S && dp[i][j][k + v]) {\n k += v;\n part[1].push_back(pos[1][i]);\n } else {\n assert(false);\n }\n }\n assert(part[0].size() == part[1].size() && part[0].size() == sz / 2);\n for (int i = 0; i < sz; i++) {\n pos[1][i] = part[i % 2][i / 2];\n }\n for (int i = 1; i < sz; i++) {\n a[pos[1][i]] = s[pos[1][i - 1]] - a[pos[1][i - 1]];\n }\n for (int i = 0; i < sz; i++) {\n b[pos[1][i]] = a[pos[1][(i + 1) % sz]];\n }\n }\n\n cout << \"Yes\\n\";\n for (int i = 0; i < n; i++) {\n cout << a[i] << \" \\n\"[i + 1 == n];\n }\n for (int i = 0; i < n; i++) {\n cout << b[i] << \" \\n\"[i + 1 == n];\n }\n #ifdef LOCAL\n for (int i = 0; i < n; i++) {\n assert(a[i] + b[i] == s[i]);\n }\n assert(multiset<int>(a.begin(), a.end()) == multiset<int>(b.begin(), b.end()));\n #endif\n };\n\n while (cin >> n && n != 0) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 3320, "memory_kb": 213016, "score_of_the_acc": -1.8297, "final_rank": 6 }, { "submission_id": "aoj_1678_9694284", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\nusing namespace std;\n#undef long\n#define long long long\n#define vec vector\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<T> &a)\n{\n const int n = a.size();\n rep(i, n)\n {\n os << a[i];\n if (i + 1 != n)\n os << \" \";\n }\n return os;\n}\nvoid solve()\n{\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n vec<long> s(n);\n rep(i, n) cin >> s[i];\n long s_sowa = 0;\n for (long i : s)\n s_sowa += i;\n if (s_sowa % 2 != 0)\n {\n cout << \"No\" << endl;\n return;\n }\n auto f = [](vec<long> a) -> pair<vec<long>, vec<long>>\n {\n int n = a.size();\n vec<long> x(n), y(n);\n x[0] = 0;\n y[0] = a[0] - x[0];\n for (int i = 1; i < n; i++)\n {\n x[i] = y[i - 1];\n y[i] = a[i] - x[i];\n }\n x[0] = y[n - 1] / 2;\n y[0] = a[0] - x[0];\n for (int i = 1; i < n; i++)\n {\n x[i] = y[i - 1];\n y[i] = a[i] - x[i];\n }\n assert(x[0] == y[n - 1]);\n return {x, y};\n };\n if (n % 2 == 1)\n {\n auto [a, b] = f(s);\n cout << \"Yes\" << endl;\n cout << a << endl;\n cout << b << endl;\n return;\n }\n int gu_idx = -1;\n rep(i, n) if (s[i] % 2 == 0) gu_idx = i;\n if (gu_idx != -1)\n {\n vec<long> ss;\n rep(i, n) if (i != gu_idx) ss.push_back(s[i]);\n auto [aa, bb] = f(ss);\n vec<long> a, b;\n rep(i, n)\n {\n if (i == gu_idx)\n {\n a.push_back(s[i] / 2);\n b.push_back(s[i] / 2);\n }\n else\n {\n a.push_back(aa[i - (i > gu_idx)]);\n b.push_back(bb[i - (i > gu_idx)]);\n }\n }\n cout << \"Yes\" << endl;\n cout << a << endl;\n cout << b << endl;\n return;\n }\n vec<vec<vec<bool>>> dd(n + 1, vec<vec<bool>>(n / 2 + 1, vec<bool>(2 * n * 150 + 1)));\n dd[0][0][n * 150] = true;\n rep(j, n)\n {\n for (int i = n / 2; i >= 0; i--)\n {\n rep(k, 2 * n * 150 + 1)\n {\n if (!dd[j][i][k])\n continue;\n dd[j + 1][i][k] = true;\n }\n if (i != n / 2)\n {\n rep(k, 2 * n * 150 + 1)\n {\n if (!dd[j][i][k])\n continue;\n int to = k + s[j];\n if (to < 0 || to >= 2 * n * 150 + 1)\n continue;\n dd[j + 1][i + 1][to] = true;\n }\n }\n }\n }\n const long s_half = s_sowa / 2;\n if (!dd[n][n / 2][s_half + n * 150])\n {\n cout << \"No\" << endl;\n return;\n }\n vec<int> soe, soe2;\n int now_idx = n / 2;\n int now_sum = s_half + n * 150;\n for (int i = n - 1; i >= 0; i--)\n {\n if (now_idx >= 1 && dd[i][now_idx - 1][now_sum - s[i]])\n {\n soe.push_back(i);\n now_idx--;\n now_sum -= s[i];\n }\n else\n {\n soe2.push_back(i);\n }\n }\n assert(now_sum == n * 150);\n assert(now_idx == 0);\n assert(soe.size() == soe2.size());\n assert(soe.size() == n / 2);\n vec<long> ss;\n rep(i, n / 2)\n {\n ss.push_back(s[soe[i]]);\n ss.push_back(s[soe2[i]]);\n }\n auto [aa, bb] = f(ss);\n vec<long> a(n), b(n);\n rep(i, n / 2)\n {\n a[soe[i]] = aa[2 * i];\n b[soe[i]] = bb[2 * i];\n a[soe2[i]] = aa[2 * i + 1];\n b[soe2[i]] = bb[2 * i + 1];\n }\n cout << \"Yes\" << endl;\n cout << a << endl;\n cout << b << endl;\n auto aaa = a, bbb = b;\n rep(i, n) assert(aaa[i] + bbb[i] == s[i]);\n sort(aaa.begin(), aaa.end()), sort(bbb.begin(), bbb.end());\n rep(i, n) assert(aaa[i] == bbb[i]);\n return;\n}\nint main()\n{\n while (true)\n solve();\n}", "accuracy": 1, "time_ms": 3820, "memory_kb": 9944, "score_of_the_acc": -1, "final_rank": 3 } ]
aoj_1668_cpp
Problem E Tampered Records In ancient times, the International Collegiate Puzzle Contest (ICPC) was held annually. Each of the annual contests consisted of two rounds: the morning round and the afternoon round. In both of the two rounds, the participants posed one puzzle made on their own to all the other participants, and then tried to solve as many puzzles as possible posed by the other participants. The participants were ranked by the following rules. The participant who solved more puzzles in the afternoon round is ranked higher. For participants solving the same number of puzzles in the afternoon round, the one who solved more puzzles in the morning round is ranked higher. For participants solving completely the same numbers of puzzles in both rounds, the order is decided by lot. Recently, a historical document was found, which is said to be the standings of some year's ICPC. As in other similar documents, this document is assumed to list the number of puzzles solved by participants in each round, in the order of their ranks. However, the scholars suspect that someone tampered with the document overwriting some of the numbers. Your task is to find at least how many of the numbers were overwritten, assuming that the original document had records consistent with the rules described above. No parts of the document other than the numbers of puzzles solved were tampered with; addition and deletion of participants were impossible. Input The input consists of multiple datasets, each in the following format. n x 1 y 1 ⋮ x n y n n is the number of participants, an integer between 2 and 6000, inclusive. x i and y i are the numbers currently on the document representing the numbers of the puzzles solved in the morning and the afternoon rounds, respectively, by the participant whose rank was the i -th highest. x i and y i are non-negative integers less than n. Some of these numbers might differ from the original ones. The end of the input is indicated by a line consisting of a zero. The input consists of at most 100 datasets. The sum of n 's in all the datasets in the input is at most 10 5 . Output For each dataset, output in a line at least how many of the 2 n numbers in the document were overwritten. Note that the numbers before the overwriting were all non-negative integers less than n. Sample Input 2 1 0 1 1 3 2 2 2 0 0 1 4 0 3 0 2 3 1 2 0 5 0 3 0 3 1 2 4 2 4 2 3 1 1 2 2 2 2 4 0 0 1 1 2 2 3 3 0 Output for the Sample Input 1 1 0 1 2 4
[ { "submission_id": "aoj_1668_10683499", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include <atcoder/all>\nusing namespace atcoder;\n\nbool solve() {\n int N;\n cin >> N;\n if(N == 0) return false;\n using P = pair<int, int>;\n const P MINF = P{0, -1};\n vector<P> dp(N + 1, MINF);\n vector<P> ep(N + 1, MINF);\n dp[0] = P{N - 1, N - 1};\n for(int i = 0; i < N; ++i) {\n int x, y;\n cin >> x >> y;\n swap(x, y);\n for(int j = 0; j <= N; ++j) {\n const auto [p, q] = dp[j];\n // no changes\n if(P{x, y} <= dp[j]) {\n ep[j] = max(ep[j], P{x, y});\n }\n // chenge x\n if(j + 1 <= N) {\n if(P{p, y} <= dp[j]) {\n ep[j + 1] = max(ep[j + 1], P{p, y});\n }else {\n ep[j + 1] = max(ep[j + 1], P{p - 1, y});\n }\n }\n // change y\n if(j + 1 <= N && x <= p) {\n if(P{x, N - 1} <= dp[j]) {\n ep[j + 1] = max(ep[j + 1], P{x, N - 1});\n }else {\n ep[j + 1] = max(ep[j + 1], P{x, q});\n }\n }\n // change x, y\n if(j + 2 <= N) {\n ep[j + 2] = max(ep[j + 2], P{p, q});\n }\n }\n swap(dp, ep);\n fill(ep.begin(), ep.end(), MINF);\n }\n for(int i = 0; i <= N; ++i) {\n if(dp[i] != MINF) {\n cout << i << endl;\n break;\n }\n }\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 3712, "score_of_the_acc": -0.6915, "final_rank": 9 }, { "submission_id": "aoj_1668_10670107", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nbool check(int N,vector<vector<int>>X,vector<vector<int>>Y){\n vector<pair<int,int>>at(N*N+1);\n rep(i,0,N)rep(j,0,N){\n at[Y[i][j]]={i,j};\n }\n rep(i,0,N)rep(j,0,N)rep(k,0,N)rep(l,0,N){\n if(abs(i-k)+abs(j-l)==1){\n int a=X[i][j],b=X[k][l];\n auto[x,y]=at[a];\n auto[z,w]=at[b];\n if(abs(x-z)+abs(y-w)<N/2)return false;\n }\n }\n return true;\n}\n\nvoid Fmax(pair<int,int>&p,pair<int,int>q){\n if(q.second>p.second)p=q;\n if(q.second==p.second&&q.first>p.first)p=q;\n return;\n}\n\nint solve(int N,vector<int>X,vector<int>Y){\n // (Y[i],X[i]) が降順に並ぶ\n vector<pair<int,int>>dp(1);\n dp[0]={N-1,N-1};\n rep(i,0,N){\n vector<pair<int,int>>nxt(2*i+3,{-1,-1});\n rep(j,0,2*i+1){\n auto[x,y]=dp[j];\n if(x==-1)continue;\n // 変更なし\n if(y>Y[i]||(y==Y[i]&&x>=X[i])){\n Fmax(nxt[j],{X[i],Y[i]});\n }\n // xだけ変える\n if(y>Y[i]){\n Fmax(nxt[j+1],{N-1,Y[i]});\n }\n if(y==Y[i]){\n Fmax(nxt[j+1],{x,Y[i]});\n }\n // yだけ変える\n if(x>=X[i]){\n Fmax(nxt[j+1],{X[i],y});\n }\n if(y-1>=0){\n Fmax(nxt[j+1],{X[i],y-1});\n }\n // 両方変更(最大化)\n Fmax(nxt[j+2],{x,y});\n }\n swap(dp,nxt);\n }\n rep(j,0,2*N+1){\n if(dp[j].first!=-1){\n return j;\n }\n }\n return -1;\n}\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<int>X(N),Y(N);\n rep(i,0,N)cin>>X[i]>>Y[i];\n int ans=solve(N,X,Y);\n assert(ans!=-1);\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3940, "score_of_the_acc": -1.0086, "final_rank": 14 }, { "submission_id": "aoj_1668_10663114", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\n//O(N^2)許されるの...\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n vector<pii>yx(n);\n for(auto&[y,x]:yx)cin>>x>>y;\n\n const int inf = n-1;\n vector<pii> dp(n+1,{-1,-1});\n vector<pii> old(n+1,{-1,-1});\n dp[0]={inf,inf};\n\n rep(i,0,n){\n rep(j,0,n+1)old[j]=pii(-1,-1);\n swap(old,dp);\n rep(j,0,n+1)if(old[j]!=pii(-1,-1)){\n auto [yi,xi]=yx[i];\n auto [yd,xd]=old[j];\n auto chm=[&](int y,int x)->void{\n int cost=0;\n cost+=y!=yi;\n cost+=x!=xi;\n if(y>yd)return;\n if(y==yd&&x>xd)return;\n if(j+cost<n+1)\n chmax(dp[j+cost],{y,x});\n };\n\n vi ylist={inf,yi,yd,yd-1};\n vi xlist={inf,xi,xd,xd-1};\n for(auto y:ylist)\n for(auto x:xlist)\n chm(y,x);\n }\n }\n\n int ans=1010;\n rep(i,0,n+1){\n auto [x,y]=dp[i];\n if(x>=0&&y>=0){\n ans=i;\n break;\n }\n }\n \n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 6090, "memory_kb": 3712, "score_of_the_acc": -1.6121, "final_rank": 19 }, { "submission_id": "aoj_1668_10663090", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\n//O(N^2)許されるの...\nbool solve() {\n int n;cin>>n;\n if (n==0) return 0;\n vector<pii>yx(n);\n for(auto&[y,x]:yx)cin>>x>>y;\n\n const int inf = n-1;\n vector<pii> dp(n+1,{-1,-1});\n vector<pii> old(n+1,{-1,-1});\n dp[0]={inf,inf};\n\n rep(i,0,n){\n rep(j,0,n+1)old[j]={-1,-1};\n swap(old,dp);\n rep(j,0,n+1)if(old[j]!=pii(-1,-1)){\n auto [yi,xi]=yx[i];\n auto [yd,xd]=old[j];\n auto chm=[&](int y,int x)->void{\n int cost=0;\n cost+=y!=yi;\n cost+=x!=xi;\n if(y>yd)return;\n if(y==yd&&x>xd)return;\n if(j+cost<n+1)\n chmax(dp[j+cost],{y,x});\n };\n\n vi ylist={inf,yi,yd,yd-1};\n vi xlist={inf,xi,xd,xd-1};\n for(auto y:ylist)if(y<=yd)\n for(auto x:xlist)\n chm(y,x);\n \n }\n }\n\n int ans=1010;\n rep(i,0,n+1){\n auto [x,y]=dp[i];\n if(x>=0&&y>=0){\n ans=i;\n break;\n }\n }\n cout<<ans<<endl;\n return 1;\n}", "accuracy": 1, "time_ms": 5670, "memory_kb": 3840, "score_of_the_acc": -1.7267, "final_rank": 20 }, { "submission_id": "aoj_1668_10649649", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(true) {\n ll n;\n cin >> n;\n if(n == 0) break;\n vector<ll> x(n), y(n);\n rep(i, n) cin >> x[i] >> y[i];\n\n // 答えは高々 N (任意の人の午後を変えればいいだけ)\n // dp[i][j] : i 番目まで見て j 回改変したときの直近の値の最大値 (午後 * N + 午前) とすれば N^2 でできそう?\n\n // vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));\n vector<ll> dp(n + 1, -INF), next_dp(n + 1, -INF);\n dp[0] = (n - 1) * n + (n - 1);\n rep(i, n) {\n next_dp.assign(n + 1, -INF);\n rep(j, n) {\n if(dp[j] >= y[i] * n + x[i]) {\n next_dp[j] = max(next_dp[j], y[i] * n + x[i]);\n }\n\n if(j + 1 <= n) {\n ll cand = (dp[j] / n) * n + x[i];\n if(cand > dp[j]) cand = ((dp[j] / n) - 1) * n + x[i];\n ll cand2 = y[i] * n + (n - 1);\n if(cand2 <= dp[j]) cand = max(cand, cand2);\n ll cand3 = y[i] * n + dp[j] % n;\n if(cand3 <= dp[j]) cand = max(cand, cand3);\n\n if(cand >= 0) next_dp[j + 1] = max(next_dp[j + 1], cand);\n }\n\n if(j + 2 <= n) {\n next_dp[j + 2] = max(next_dp[j + 2], dp[j]);\n }\n }\n swap(dp, next_dp);\n }\n\n // rep(i, n + 1) {\n // rep(j, n + 1) cerr << dp[i][j] << \" \";\n // cerr << endl;\n // }\n // cerr << \"----------------\" << endl;\n\n ll ans = n;\n rep(i, n + 1) {\n if(dp[i] < 0) continue;\n ans = min(ans, i);\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 3900, "memory_kb": 3564, "score_of_the_acc": -0.9739, "final_rank": 13 }, { "submission_id": "aoj_1668_10611586", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\npair<int, int> comp(pair<int, int> a, pair<int, int> b) {\n return (a.second == b.second ? a.first > b.first : a.second > b.second) ? a : b;\n}\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<pair<int, int>> dp(1, {N - 1, N - 1});\n for (int i = 1, x, y; i <= N; i++) {\n cin >> x >> y;\n vector<pair<int, int>> ndp(2 * i + 1, {-1, -1});\n for (int j = 0; j + 2 <= 2 * i; j++) {\n auto [a, b] = dp[j];\n if (dp[j] == make_pair(-1, -1)) continue;\n // 0\n {\n if (comp(dp[j], make_pair(x, y)) == dp[j]) {\n ndp[j] = comp(ndp[j], make_pair(x, y));\n }\n }\n // 1\n {\n if (comp(dp[j], make_pair(a, y)) == dp[j]) {\n ndp[j + 1] = comp(ndp[j + 1], make_pair(a, y));\n }\n if (comp(dp[j], make_pair(N - 1, y)) == dp[j]) {\n ndp[j + 1] = comp(ndp[j + 1], make_pair(N - 1, y));\n }\n if (comp(dp[j], make_pair(x, b)) == dp[j]) {\n ndp[j + 1] = comp(ndp[j + 1], make_pair(x, b));\n }\n if (b > 0 && comp(dp[j], make_pair(x, b - 1)) == dp[j]) {\n ndp[j + 1] = comp(ndp[j + 1], make_pair(x, b - 1));\n }\n }\n // 2\n {\n ndp[j + 2] = comp(ndp[j + 2], dp[j]);\n }\n }\n dp = ndp;\n }\n for (int i = 0; i <= 2 * N; i++) {\n if (dp[i] != make_pair(-1, -1)) {\n cout << i << '\\n';\n break;\n }\n }\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 4990, "memory_kb": 3840, "score_of_the_acc": -1.5981, "final_rank": 18 }, { "submission_id": "aoj_1668_10610655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\ntemplate <class T>\nusing MaxHeap = std::priority_queue<T>;\ntemplate <class T>\nusing MinHeap = std::priority_queue<T, vector<T>, greater<T>>;\n#define rep2(i, n) for (ll i = 0; i < (n); i++)\n#define rep3(i, l, r) for (ll i = (l); i < (r); i++)\n#define rrep2(i, n) for (ll i = n; i-- > 0;)\n#define rrep3(i, r, l) for (ll i = (r); i-- > (l);)\n#define overload(a, b, c, d, ...) d\n#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)\n#define rrep(...) overload(__VA_ARGS__, rrep3, rrep2)(__VA_ARGS__)\n#define all(x) begin(x), end(x)\nbool chmin(auto& lhs, auto rhs) {\n return lhs > rhs ? lhs = rhs, 1 : 0;\n}\nbool chmax(auto& lhs, auto rhs) {\n return lhs < rhs ? lhs = rhs, 1 : 0;\n}\nstruct IOIO {\n IOIO() {\n std::cin.tie(0)->sync_with_stdio(0);\n }\n} ioio;\n\nvoid solve() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) return;\n vector<pair<int, int>> A(n);\n for (auto& [l, r]: A) cin >> r >> l;\n const int N = 2 * n;\n const pair<int, int> none = {-1, -1};\n vector<pair<int, int>> dp(N + 4, none);\n dp[0] = {n - 1, n - 1};\n rep(i, n) {\n auto [c, d] = A[i];\n rrep(j, N + 1) {\n if (dp[j] == none) continue;\n auto [a, b] = dp[j];\n\n chmax(dp[j + 2], dp[j]);\n\n if (b >= d) {\n chmax(dp[j + 1], pair<int, int>{a, d});\n } else if (a - 1 >= 0) {\n chmax(dp[j + 1], pair<int, int>{a - 1, d});\n }\n\n if (a > c) {\n chmax(dp[j + 1], pair<int, int>{c, n - 1});\n } else if (a == c) {\n chmax(dp[j + 1], pair<int, int>{c, b});\n }\n if (dp[j] >= A[i]) {\n dp[j] = A[i];\n } else {\n dp[j] = none;\n }\n }\n }\n rep(i, N + 1) {\n if (dp[i] != none) {\n cout << i << '\\n';\n break;\n }\n }\n }\n}\n\nint main() {\n int t = 1;\n\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 3712, "score_of_the_acc": -0.735, "final_rank": 10 }, { "submission_id": "aoj_1668_10609689", "code_snippet": "#include <iostream>\n#include <cassert>\n#include <vector>\n#include <algorithm>\nstruct grade {\n int morning = -1, afternoon = -1;\n bool valid() const {\n return morning != -1 and afternoon != -1;\n }\n};\nbool operator==(const grade& p, const grade& q) {\n return p.morning == q.morning and p.afternoon == q.afternoon;\n}\n// pがqより成績が良いか?(一緒も許容)\nbool compare(grade p, grade q) {\n if (p.afternoon != q.afternoon) return p.afternoon > q.afternoon; // 午後の成績\n else if (p.morning != q.morning) return p.morning > q.morning; // 午前の成績\n else return true; // 一緒の成績\n}\nvoid chmax(grade& p, grade q) {\n if (compare(q, p)) p = q;\n}\nint N;\nint solve() {\n // dp[i][j] := 上からi人まで矛盾なく並べる方法であって、j個の要素を変更したときの最下位の最高スコア\n std::vector<grade> dp(1, {N - 1, N - 1});\n for (int i = 1 ; i <= N ; i++) {\n std::vector<grade> next(2 * i + 1);\n int X, Y;\n std::cin >> X >> Y;\n for (int j = 0 ; j < std::ssize(dp) ; j++) {\n if (!dp[j].valid()) continue;\n // 一つも変えないケース\n if (compare(dp[j], {X, Y})) chmax(next[j], {X, Y});\n // 午後を変えるケース\n if (compare(dp[j], {X, dp[j].afternoon - 1})) chmax(next[j + 1], {X, dp[j].afternoon - 1});\n if (compare(dp[j], {X, dp[j].afternoon})) chmax(next[j + 1], {X, dp[j].afternoon});\n // 午前を変えるケース\n if (compare(dp[j], {N - 1, Y})) chmax(next[j + 1], {N - 1, Y});\n else if (compare(dp[j], {dp[j].morning, Y})) chmax(next[j + 1], {dp[j].morning, Y});\n // 両方変えるケース\n if (compare(dp[j], dp[j])) chmax(next[j + 2], dp[j]);\n }\n dp = std::move(next);\n }\n for (int i = 0 ; i < std::ssize(dp) ; i++) if (dp[i].valid()) {\n return i;\n }\n assert(false);\n return -1;\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n std::cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3820, "score_of_the_acc": -0.8268, "final_rank": 11 }, { "submission_id": "aoj_1668_10604139", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(long long i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(long long i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\n\n\nvoid solve(int &n) {\n vector<int> x(n), y(n);\n rep(i, 0, n) cin >> x[i] >> y[i];\n rep(i, 0, n) {\n swap(x[i], y[i]);\n x[i] = (n - 1) - x[i];\n y[i] = (n - 1) - y[i];\n }\n\n vector<pair<int, int>> dp(2 * n + 1, {intINF, intINF});\n dp[0] = {0, 0};\n\n rep(i, 0, n) {\n\n vector<pair<int, int>> ndp(2 * n + 1, {intINF, intINF});\n rep(j, 0, 2 * n + 1) {\n if(dp[j].first == intINF) continue;\n \n // 書き換えない\n if(pair<int, int>(x[i], y[i]) >= dp[j]) {\n chmin(ndp[j], {x[i], y[i]});\n }\n \n // xを書き換える\n if(j + 1 < sz(ndp)) {\n // dp[j].second <= y[i] -> 前と同じ\n if(dp[j].second <= y[i]) chmin(ndp[j + 1], {dp[j].first, y[i]});\n // dp[j].second > y[i] -> 前 + 1\n else if(dp[j].first + 1 < n) chmin(ndp[j + 1], {dp[j].first + 1, y[i]});\n }\n \n // yを書き換える\n // dp[j].first <= x[i]が前提\n if(j + 1 < sz(ndp) && dp[j].first <= x[i]) {\n if(dp[j].first == x[i]) chmin(ndp[j + 1], {x[i], dp[j].second});\n else chmin(ndp[j + 1], {x[i], 0});\n }\n\n // x,yを書き換える\n if(j + 2 < sz(ndp)) {\n chmin(ndp[j + 2], dp[j]);\n }\n }\n\n swap(ndp, dp);\n }\n\n rep(i, 0, 2 * n + 1) {\n if(dp[i].first != intINF) {\n cout << i << endl;\n return ;\n }\n }\n}\n\nint main() {\n int n;\n string s;\n while(true) {\n cin >> n;\n if(n == 0) return 0;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1600, "memory_kb": 3536, "score_of_the_acc": -0.4967, "final_rank": 6 }, { "submission_id": "aoj_1668_10449819", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\nusing pii = pair<int, int>;\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\ntemplate <typename T> void print(vector<T> A);\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (true) {\n auto comp = [](pii a, pii b) {\n if (a.second < b.second) {\n return true;\n } else if (a.second > b.second) {\n return false;\n }\n if (a.first <= b.first) {\n return true;\n }\n return false;\n };\n int N;\n cin >> N;\n if (N == 0) {\n break;\n }\n vector<pii> XY(N);\n for (auto &[x, y] : XY) {\n cin >> x >> y;\n }\n pii nul = {-1, -1}, max = {N - 1, N - 1};\n\n \n vector<pii> dp_1(2 * N + 3, nul);\n vector<pii> dp_2(2 * N + 3, nul);\n\n dp_1[0] = max;\n for (int i = 0; i < N; i++) {\n dp_2.assign(2*N+3,nul);\n for (int j = 0; j < 2 * N + 1; j++) {\n if (dp_1[j] == nul) {\n continue;\n }\n // if (j<4 && i==2)cout<<j<<' '<<dp_1[j].first<<' '<<dp_1[j].second<<endl;\n\n pii now = dp_1[j], nex = XY[i];\n // k=0\n if (comp(nex, now) && comp(dp_2[j], nex)) {\n dp_2[j] = nex;\n }\n\n // k=1\n nex.second = now.second;\n if (comp(nex, now) && comp(dp_2[j + 1], nex)) {\n dp_2[j + 1] = nex;\n }\n pii nex_2 = nex;\n nex_2.second -= 1;\n if (nex_2.second >= 0 && comp(nex_2, now) &&\n comp(dp_2[j + 1], nex_2)) {\n dp_2[j + 1] = nex_2;\n }\n\n pii nex_3 = XY[i];\n if (nex_3.second == now.second) {\n nex_3.first = now.first;\n } else if (nex_3.second < now.second) {\n nex_3.first = N - 1;\n }\n if (comp(nex_3, now) && comp(dp_2[j + 1], nex_3)) {\n dp_2[j + 1] = nex_3;\n }\n\n // k=2\n nex.first = now.first;\n if (comp(nex, now) && comp(dp_2[j + 2], nex)) {\n dp_2[j + 2] = nex;\n }\n }\n swap(dp_1,dp_2);\n }\n for (int j = 0; j < 2 * N + 1; j++) {\n if (dp_1[j] != nul) {\n cout << j << endl;\n break;\n }\n }\n }\n}\n\ntemplate <typename T> void print(vector<T> A) {\n for (int i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}", "accuracy": 1, "time_ms": 1410, "memory_kb": 3472, "score_of_the_acc": -0.3638, "final_rank": 3 }, { "submission_id": "aoj_1668_10444936", "code_snippet": "// AOJ 1668 Tampered Records\n// 2025.5.3\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) {\n\tint i; char b[30];\n\tif (!n) pc('0');\n\telse {\n\t\ti = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconst int MAXN = 6000;\nint dpA[2*MAXN+5], dpM[2*MAXN+5];\nint nxtA[2*MAXN+5], nxtM[2*MAXN+5];\n\ninline void try_update(int j, int na, int nm) {\n int &a = nxtA[j], &m = nxtM[j];\n if (a < na || (a == na && m < nm)) a = na, m = nm;\n}\n\nint main(){\n while (true) {\n int n = Cin();\n if (n == 0) break;\n vector<int> x(n), y(n);\n for (int i = 0; i < n; i++) x[i] = Cin(), y[i] = Cin();\n\n int dsz = 3;\n dpA[0] = y[0], dpM[0] = x[0];\n dpA[1] = n-1, dpM[1] = x[0];\n dpA[2] = n-1, dpM[2] = n-1;\n\n for (int i = 1; i < n; i++) {\n int yi = y[i], xi = x[i];\n int mi = min(dsz, 2*i + 1);\n int nsz = min(n + 1, 2*i + 3);\n\n for (int j = 0; j < nsz; j++) nxtA[j] = nxtM[j] = -1;\n for (int j = 0; j < mi; j++) {\n int a_last = dpA[j], m_last = dpM[j];\n if (a_last < 0) continue;\n\n if (a_last > yi || (a_last == yi && m_last >= xi))\n try_update(j, yi, xi);\n\n if (j + 1 <= n) {\n if (a_last >= yi) try_update(j+1, yi, m_last);\n if (a_last > yi) try_update(j+1, yi, n-1);\n if (m_last >= xi) try_update(j+1, a_last, xi);\n if (a_last > 0) try_update(j+1, a_last-1, xi);\n }\n\n if (j + 2 <= n) {\n try_update(j+2, a_last, m_last);\n if (a_last > 0) try_update(j+2, a_last-1, n-1);\n }\n }\n dsz = nsz;\n for (int j = 0; j < dsz; j++) {\n dpA[j] = nxtA[j];\n dpM[j] = nxtM[j];\n }\n }\n int ans = 2*n;\n for (int j = 0; j < dsz; j++) if (dpA[j] >= 0) { ans = j; break; }\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 3356, "score_of_the_acc": -0.1162, "final_rank": 2 }, { "submission_id": "aoj_1668_10418492", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// using mint = modint998244353;\n// using mint = modint1000000007;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nstruct Point {\n ll x, y;\n Point() {}\n Point(ll _x, ll _y): x(_x), y(_y) {}\n bool operator<(const Point& p) const {\n return (x == p.x ? y < p.y : x < p.x);\n }\n bool operator>(const Point& p) const {\n return (x == p.x ? y > p.y : x > p.x);\n }\n bool operator<=(const Point& p) const {\n return (x == p.x ? y <= p.y : x <= p.x);\n }\n bool operator>=(const Point& p) const {\n return (x == p.x ? y >= p.y : x >= p.x);\n }\n bool operator==(const Point& p) const {\n return (x == p.x && y == p.y);\n }\n};\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(true) {\n ll n;\n cin >> n;\n if(n == 0) break;\n vector<Point> points(n);\n rep(i, n) cin >> points[i].y >> points[i].x;\n\n // 答えは高々 N 以下\n // 基本的に上位から見て貪欲?\n // ただ、順位が 1 個上の午後に合わせたときに 1 個上を超す場合は午前も修正する必要あり\n // これがボトルネックになる場合は、最初から午後を操作していた方が良さそう\n\n // 上位から見て過去に x 回改ざんした時の、1 個上の値として存在する最大のスコアで DP ?\n // vector<vector<Point>> dp(n + 1, vector<Point>(n + 1, Point(-1, -1)));\n vector<Point> dp(n + 1, Point(-1, -1));\n dp[0] = Point(n - 1, n - 1);\n rep(i, n) {\n vector<Point> next(n + 1, Point(-1, -1));\n rep(j, n + 1) {\n if(dp[j] == Point(-1, -1)) continue;\n // 改竄をしない時\n if(dp[j] >= points[i]) {\n next[j] = max(next[j], points[i]);\n }\n\n // 1 つだけ改竄する時\n Point cand1 = points[i], cand2 = points[i];\n cand1.x = dp[j].x;\n if(cand1 > dp[j]) cand1.x--;\n if(cand1.x < 0) cand1 = Point(-1, -1);\n\n cand2.y = (cand2.x == dp[j].x ? dp[j].y : n - 1);\n if(cand2 > dp[j]) cand2 = Point(-1, -1);\n\n if(j + 1 < n + 1) next[j + 1] = max(next[j + 1], max(cand1, cand2));\n\n // 2 つ改竄する時\n if(j + 2 < n + 1) next[j + 2] = max(next[j + 2], dp[j]);\n }\n swap(dp, next);\n }\n\n // rep(i, n + 1) {\n // rep(j, n + 1) cerr << \"(\" << dp[i][j].x << \", \" << dp[i][j].y << \") \";\n // cerr << endl;\n // }\n rep(i, n + 1) {\n if(dp[i] == Point(-1, -1)) continue;\n cout << i << endl;\n break;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 3308, "score_of_the_acc": -0.0926, "final_rank": 1 }, { "submission_id": "aoj_1668_10389399", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\nvoid solve(ll n,vpll a){\n vll dp(2*n+1,-1LL<<60);\n dp[0] = n*n-1;\n rep(i,n){\n vll ndp(2*n+1,-1LL<<60);\n rep(j,2*n+1){\n if(dp[j] == (-1LL<<60)){continue;}\n if(dp[j] >= a[i].first + a[i].second * n){\n ndp[j] = max(ndp[j],a[i].first + a[i].second * n);\n }\n if(j+2 < 2*n+1){\n ndp[j+2] = max(ndp[j+2],dp[j]);\n }\n if(j+1 < 2*n+1){\n if(dp[j] % n >= a[i].first){\n ndp[j+1] = max(ndp[j+1],dp[j] / n * n + a[i].first);\n //print(i,j,dp[i][j] / n * n + a[i].first);\n }\n else if((dp[j] / n - 1) >= 0){\n ndp[j+1] = max(ndp[j+1],(dp[j] / n - 1) * n + a[i].first);\n //print(i,j,(dp[i][j] / n - 1) * n + a[i].first);\n }\n if(dp[j] / n > a[i].second){\n ndp[j+1] = max(ndp[j+1],a[i].second * n + n - 1);\n //print(i,j,a[i].second * n + n - 1);\n }\n else if(dp[j] / n == a[i].second){\n ndp[j+1] = max(ndp[j+1],dp[j]);\n //print(i,j,dp[i][j]);\n }\n else{\n ;\n }\n }\n }\n swap(dp,ndp);\n }\n //print(dp);\n rep(i,2*n+1){\n if(dp[i] != (-1LL<<60)){\n print(i);\n return;\n }\n }\n}\n\nint main(){\n while(1){\n LL(n);\n if(n == 0){break;}\n VPLL(a,n);\n solve(n,a);\n }\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 3968, "score_of_the_acc": -1.1493, "final_rank": 16 }, { "submission_id": "aoj_1668_10361216", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n\nusing namespace std;\n\nconstexpr int iINF = 1'000'000'000;\n\nint cmp (const pair<int, int>& x, const pair<int, int>& y) {\n if (x.second == y.second) {\n if (x.first == y.first) return 0;\n return x.first < y.first ? -1 : 1;\n }\n return x.second < y.second ? -1 : 1;\n}\n\nint main () {\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n\n vector<int> x(n), y(n);\n for (int i = 0; i < n; i++) {\n cin >> x[i] >> y[i];\n }\n\n // dp[i][j] := 昇順にi個処理して、変更j回をしたとき、作れる最も小さいスコア\n vector<pair<int, int>> dp(n + 1), ndp(n + 1);\n for (int i = 0; i < n + 1; i++) {\n dp[i] = make_pair(iINF, iINF);\n }\n dp[0] = make_pair(0, 0);\n\n for (int i = n - 1; 0 <= i; i--) {\n for (int j = 0; j < n + 1; j++) {\n ndp[j] = make_pair(iINF, iINF);\n }\n\n for (int j = 0; j < n + 1; j++) {\n if (dp[j] == make_pair(iINF, iINF)) continue;\n\n auto cur = make_pair(x[i], y[i]);\n // 変更0回\n if (cmp(dp[j], cur) <= 0) {\n if (cmp(cur, ndp[j]) <= 0) {\n ndp[j] = cur;\n }\n }\n\n if (j == n) continue;\n // 変更1回\n // どちらを変更するかで分ける。\n\n // 午前の部\n if (x[i] != 0\n && cmp(dp[j], make_pair(0, y[i])) <= 0\n && cmp(make_pair(0, y[i]), ndp[j + 1]) <= 0) {\n ndp[j + 1] = make_pair(0, y[i]);\n }\n if (x[i] != dp[j].first\n && cmp(dp[j], make_pair(dp[j].first, y[i])) <= 0\n && cmp(make_pair(dp[j].first, y[i]), ndp[j + 1]) <= 0) {\n ndp[j + 1] = make_pair(dp[j].first, y[i]);\n }\n\n // 午後の部\n if (y[i] != dp[j].second\n && cmp(dp[j], make_pair(x[i], dp[j].second)) <= 0\n && cmp(make_pair(x[i], dp[j].second), ndp[j + 1]) <= 0) {\n ndp[j + 1] = make_pair(x[i], dp[j].second);\n }\n if (y[i] != dp[j].second + 1\n && dp[j].second + 1 < n\n && cmp(dp[j], make_pair(x[i], dp[j].second + 1)) <= 0\n && cmp(make_pair(x[i], dp[j].second + 1), ndp[j + 1]) <= 0) {\n ndp[j + 1] = make_pair(x[i], dp[j].second + 1);\n }\n\n if (j == n - 1) continue;\n // 変更2回\n // この場合は前とピッタリ合わせればok\n if (dp[j].first != x[i] && dp[j].second != y[i]) {\n if (cmp(dp[j], ndp[j + 2]) <= 0) {\n ndp[j + 2] = dp[j];\n }\n }\n }\n\n swap(dp, ndp);\n }\n\n int ans = -1;\n for (int i = 0; i < n + 1; i++) {\n if (dp[i] != make_pair(iINF, iINF)) {\n ans = i;\n break;\n }\n }\n\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 3712, "score_of_the_acc": -0.6121, "final_rank": 8 }, { "submission_id": "aoj_1668_10038875", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep2(i, m, n) for (int i = (m); i < (n); ++i)\n#define rep(i, n) rep2(i, 0, n)\n#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)\n#define drep(i, n) drep2(i, n, 0)\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)\n#define REV(i, a, b) for (int i = (a); i >= (b); --i)\n#define CLR(a, b) memset((a), (b), sizeof(a))\n#define DUMP(x) cout << #x << \" = \" << (x) << endl;\n#define INF 1001001001001001001ll\n#define inf (int)1001001000\n#define MOD 998244353\n#define MOD1 1000000007\n#define PI 3.14159265358979\n#define Dval 1e-12\n#define fcout cout << fixed << setprecision(12)\n#define MP make_pair\n#define PB push_back\n#define fi first\n#define se second\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n\nusing ll = long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vl = vector<long long>;\nusing vs = vector<string>;\nusing vd = vector<double>;\nusing vld = vector<long double>;\nusing vc = vector<char>;\nusing vb = vector<bool>;\nusing vpii = vector<pair<int, int>>;\nusing vpil = vector<pair<int, long long>>;\nusing vpll = vector<pair<long long, long long>>;\nusing vvi = vector<vector<int>>;\nusing vvl = vector<vector<long long>>;\nusing vvd = vector<vector<double>>;\nusing vvld = vector<vector<long double>>;\nusing vvc = vector<vector<char>>;\nusing vvb = vector<vector<bool>>;\nusing vvpii = vector<vector<pair<int,int>>>;\nusing vvpll = vector<vector<pair<long long,long long>>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vvvl = vector<vector<vector<long long>>>;\nusing pii = pair<int, int>;\nusing pll = pair<long long, long long>;\n\nll gcd(ll x, ll y) {\tif (x == 0) return y;\treturn gcd(y%x, x);} \ntemplate<typename T>\nT POW(T x, ll n){T ret=1;\twhile(n>0){\t\tif(n&1) ret=ret*x;\t\tx=x*x;\t\tn>>=1;\t}\treturn ret;}\ntemplate<typename T>\nT modpow(T a, ll n, T p) {\tif(n==0) return (T)1; if (n == 1) return a % p; if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p; T t = modpow(a, n / 2, p); return (t * t) % p;}\ntemplate<typename T>\nT modinv(T a, T m) {\tif(m==0)return (T)1;\tT b = m, u = 1, v = 0;\twhile (b) {\t\tT t = a / b;\t\ta -= t * b; swap(a, b);\t\tu -= t * v; swap(u, v);\t}\tu %= m;\tif (u < 0) u += m;\treturn u;}\ntemplate<typename T>\nT REM(T a, T b){ return (a % b + b) % b;}\ntemplate<typename T>\nT QUO(T a, T b){ return (a - REM(a, b)) / b;}\n/* \nconst int MAXCOMB=510000;\nll MODCOMB = 998244353;\nll fac[MAXCOMB], finv[MAXCOMB], inv[MAXCOMB]; \nvoid COMinit() {\tfac[0] = fac[1] = 1;\tfinv[0] = finv[1] = 1;\tinv[1] = 1;\tfor (int i = 2; i < MAXCOMB; i++) {\t\tfac[i] = fac[i - 1] * i % MODCOMB;\t\tinv[i] = MODCOMB - inv[MODCOMB%i] * (MODCOMB / i) % MODCOMB;\t\tfinv[i] = finv[i - 1] * inv[i] % MODCOMB;\t}}\nll COM(ll n, ll k) {\tif (n < k) return 0;\tif (n < 0 || k < 0) return 0;\treturn fac[n] * (finv[k] * finv[n - k] % MODCOMB) % MODCOMB;}\nll com(ll n,ll m){ if(n<m || n<=0 ||m<0){\t\treturn 0;\t}\tif( m==0 || n==m){\t\treturn 1;\t}\tll k=1;\tfor(ll i=1;i<=m;i++){ k*=(n-i+1); \t k%=MODCOMB;\t k*=modinv(i,MODCOMB);\t k%=MODCOMB;\t}\treturn k;}\n*/\n/*\nconst int MAXCOMB=510000;\nstd::vector<mint> fac(MAXCOMB), finv(MAXCOMB), inv(MAXCOMB);\nvoid COMinit() {fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;for (int i = 2; i < MAXCOMB; i++) {fac[i] = fac[i - 1] * i;inv[i] = mint(0) - inv[mint::mod() % i] * (mint::mod() / i);finv[i] = finv[i - 1] * inv[i];}}\nmint COM(int n, int k) {if (n < k) return 0;if (n < 0 || k < 0) return 0;return fac[n] * finv[k] * finv[n - k];}\n*/\ntemplate <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false));}\ntemplate <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false));}\ntemplate <class T> T BS(vector<T> &vec, T key) { auto itr = lower_bound(vec.begin(), vec.end(), key); return distance(vec.begin(), itr); }\ntemplate<class T> pair<T,T> RangeBS(vector<T> &vec, T lowv, T highv){auto itr_l = lower_bound(vec.begin(), vec.end(), lowv); auto itr_r = upper_bound(vec.begin(), vec.end(), highv); return make_pair(distance(vec.begin(), itr_l), distance(vec.begin(), itr_r)-1);}\nvoid fail() { cout << \"-1\\n\"; exit(0); } void no() { cout << \"No\\n\"; exit(0); } void yes() { cout << \"Yes\\n\"; exit(0); }\ntemplate<class T> void er(T a) { cout << a << '\\n'; exit(0); }\nint dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1};\nbool range_in(int i, int j, int h, int w){ if(i<0 || j<0 || i>=h || j>=w) return false; return true;} \nint bitcount(int n){n=(n&0x55555555)+(n>>1&0x55555555); n=(n&0x33333333)+(n>>2&0x33333333); n=(n&0x0f0f0f0f)+(n>>4&0x0f0f0f0f); n=(n&0x00ff00ff)+(n>>8&0x00ff00ff); n=(n&0x0000ffff)+(n>>16&0x0000ffff); return n;}\n\ntemplate<typename T>\nstruct Edge{\n int from, to, index;\n T cost;\n Edge(int _to) : from(-1), to(_to), index(-1), cost(0) {}\n Edge(int _to, T _cost) : from(-1), to(_to), index(-1), cost(_cost) {}\n Edge(int _from, int _to, int _index) : from(_from), to(_to), index(_index), cost(0) {}\n Edge(int _from, int _to, int _index, T _cost) \n : from(_from), to(_to), index(_index), cost(_cost) {}\n bool operator<(const Edge<T>& other) const {\n return cost < other.cost; \n }\n Edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n};\nusing Graph = vector<vector<int>>; \ntemplate <typename T>\nusing WGraph = vector<vector<Edge<T>>>; \n\n int TESTFIN = 0;\n\nvoid solve(){\n int n;\n cin>>n;\n if(n==0){TESTFIN = 1; return;}\n vpii pr(n);\n rep(i,n)cin>>pr[i].se>>pr[i].fi;\n vvpii dp(2,vpii(n*2+1,{-1,-1}));\n for(int i=0;i<n;i++){\n if(i==0){\n dp[0][0] = pr[i];\n if(pr[i].fi==n-1 || pr[i].se==n-1) dp[0][1] = {n-1,n-1};\n else dp[0][1] = {n-1,pr[i].se};\n dp[0][2]= {n-1,n-1};\n continue;\n }\n for(int j=0;j<=n*2;j++){\n for(int k=0;k<=2;k++){\n if(j-k<0)continue;\n if(k==2){\n if(dp[0][j-k].fi!=-1){\n chmax(dp[1][j],dp[0][j-k]);\n }\n }\n if(k==1){\n if(dp[0][j-k].fi==-1)continue;\n int a = (dp[0][j-1].fi>=pr[i].fi);\n int b = (dp[0][j-1].se>=pr[i].se);\n if(a==1 && b==1){\n if(dp[0][j-1].fi==pr[i].fi){\n chmax(dp[1][j],dp[0][j-1]);\n }\n else chmax(dp[1][j],{dp[0][j-1].fi,pr[i].se});\n }\n if(a==1 && b==0){\n if(pr[i].fi<dp[0][j-1].fi-1)chmax(dp[1][j],{dp[0][j-1].fi-1,pr[i].se});\n else if(pr[i].fi==dp[0][j-1].fi)chmax(dp[1][j],dp[0][j-1]);\n else chmax(dp[1][j],{dp[0][j-1].fi-1,n-1});\n }\n if(a==0 && b==1){\n chmax(dp[1][j],{dp[0][j-1].fi,pr[i].se});\n }\n if(a==0 && b==0){ chmax(dp[1][j],{dp[0][j-1].fi-1,pr[i].se});}\n }\n if(k==0){\n if(dp[0][j-k].fi==-1)continue;\n \n if(dp[0][j]>=pr[i])chmax(dp[1][j],pr[i]);\n }\n }\n \n }\n rep(j,2*n+1)dp[0][j]=dp[1][j];\n rep(j,2*n+1)dp[1][j]={-1,-1};\n }\n for(int i=0;i<=n*2;i++){\n if(dp[0][i].fi!=-1){\n cout<<i<<endl;\n return;\n }\n }\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\twhile(TESTFIN==0) solve();\n}", "accuracy": 1, "time_ms": 5260, "memory_kb": 3584, "score_of_the_acc": -1.2613, "final_rank": 17 }, { "submission_id": "aoj_1668_9453085", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<int> A(N), B(N), C(N);\n rep(i,0,N) {\n int X, Y;\n cin >> X >> Y;\n X = N - 1 - X, Y = N - 1 - Y;\n swap(X,Y);\n A[i] = X, B[i] = Y, C[i] = A[i]*N+B[i];\n }\n int MAXS = N*2+1;\n vector<int> DP(MAXS,inf);\n DP[0] = 0;\n rep(i,0,N) {\n vector<int> NewDP(MAXS,inf);\n rep(j,0,MAXS) {\n if (DP[j] == inf) continue;\n if (DP[j] <= C[i]) chmin(NewDP[j],C[i]);\n if (DP[j] < N*(A[i]+1)) chmin(NewDP[j+1],max(DP[j],N*A[i]));\n if (DP[j] <= N*(N-1)+B[i]) chmin(NewDP[j+1],((DP[j]-B[i]+N-1)/N)*N+B[i]);\n chmin(NewDP[j+2],DP[j]);\n }\n rep(j,0,MAXS) DP[j] = NewDP[j];\n }\n rep(i,0,MAXS) {\n if (DP[i] < inf) {\n cout << i << endl;\n break;\n }\n }\n}\n}", "accuracy": 1, "time_ms": 1840, "memory_kb": 3492, "score_of_the_acc": -0.4754, "final_rank": 4 }, { "submission_id": "aoj_1668_9438352", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\nint main(){\n while(1){\n int N;cin>>N;\n if(!N)return 0;\n vector<pair<int,int>>A(N);\n cin>>A;\n REP(i,N)swap(A[i].F,A[i].S);\n vector<pair<int,int>>DP(N+1,make_pair(-1,-1));\n DP[0]=make_pair(N-1,N-1);\n REP(i,N){\n vector<pair<int,int>>EP(N+1,make_pair(-1,-1));\n REP(j,N+1){\n if(DP[j]>=A[i])chmax(EP[j],A[i]);\n if(j+2<=N)chmax(EP[j+2],DP[j]);\n if(j+1<=N){\n if(A[i].S<=DP[j].S)chmax(EP[j+1],make_pair(DP[j].F,A[i].S));\n else chmax(EP[j+1],make_pair(DP[j].F-1,A[i].S));\n if(DP[j].F==A[i].F)chmax(EP[j+1],DP[j]);\n else if(DP[j].F>A[i].F)chmax(EP[j+1],make_pair(A[i].F,N-1));\n }\n }\n DP=EP;\n }\n ll ans=N;\n REP(i,N+1)if(DP[i].F>=0)chmin(ans,i);\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1660, "memory_kb": 3528, "score_of_the_acc": -0.4959, "final_rank": 5 }, { "submission_id": "aoj_1668_9412273", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n \n#include <bits/stdc++.h>\n// #include <atcoder/all>\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vc<vv<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst double pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(long long y, long long x, long long H, long long W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\ntemplate<typename T> void view(const set<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\n\nint solve(int N){\n vi x(N), y(N); rep(i, N) cin >> x[i] >> y[i];\n pair<int, int> e = {-1, -1};\n vv<pair<int, int>> dp(2, vc<pair<int, int>>(2 * N + 1, e));\n int nw = 0, nxt = 1;\n dp[nw][0] = {N - 1, N - 1};\n rep(i, N){\n rep(j, 2 * N + 1) dp[nxt][j] = e;\n rep(j, 2 * N + 1){\n auto [ny, nx] = dp[nw][j];\n if (ny > y[i] || (ny == y[i] && nx >= x[i])) dp[nxt][j] = max(dp[nxt][j], {y[i], x[i]});\n if (j < 2 * N && ny == y[i]) dp[nxt][j + 1] = max(dp[nxt][j + 1], {y[i], nx});\n if (j < 2 * N && ny > y[i]) dp[nxt][j + 1] = max(dp[nxt][j + 1], {y[i], N - 1});\n if (j < 2 * N && nx >= x[i]) dp[nxt][j + 1] = max(dp[nxt][j + 1], {ny, x[i]});\n if (j < 2 * N && ny > 0) dp[nxt][j + 1] = max(dp[nxt][j + 1], {ny - 1, x[i]});\n if (j < 2 * N - 1) dp[nxt][j + 2] = max(dp[nxt][j + 2], dp[nw][j]);\n }\n swap(nw, nxt);\n }\n rep(j, 2 * N + 1) if (dp[nw][j] != e) return j;\n return -1;\n}\n\nint main(){\n vi ans;\n while (true){\n int N; cin >> N;\n if (N == 0) break;\n ans.push_back(solve(N));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 3280, "memory_kb": 3712, "score_of_the_acc": -1.0809, "final_rank": 15 }, { "submission_id": "aoj_1668_9407509", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\n\nint main(void) {\n while(1) {\n int n;\n cin >> n;\n\n if(n == 0) break;\n\n vector<P> a(n);\n rep(i, 0, n) {\n cin >> a[i].second;\n cin >> a[i].first;\n }\n\n vector<P> dp(2*n+1, {-1, -1});\n dp[0] = {n-1, n-1};\n\n rep(i, 0, n) {\n vector<P> ndp(2*n+1, {-1, -1});\n rep(j, 0, 2*n+1) {\n if(j-2 >= 0) ndp[j] = max(ndp[j], dp[j-2]);\n if(j-1 >= 0) {\n if(dp[j-1] >= P{dp[j-1].first, a[i].second}) ndp[j] = max(ndp[j], P{dp[j-1].first, a[i].second});\n if(dp[j-1] >= P{a[i].first, dp[j-1].second}) ndp[j] = max(ndp[j], P{a[i].first, dp[j-1].second});\n if(dp[j-1] >= P{a[i].first, n}) ndp[j] = max(ndp[j], P{a[i].first, n});\n if(dp[j-1].first-1>=0 and dp[j-1] >= P{dp[j-1].first-1, a[i].second}) ndp[j] = max(ndp[j], P{dp[j-1].first-1, a[i].second});\n }\n if(dp[j] >= a[i]) ndp[j] = max(ndp[j], a[i]);\n // cout << i << ' ' << j << ' ' << ndp[j].first << ' ' << ndp[j].second << '\\n';\n }\n swap(dp, ndp);\n }\n rep(i, 0, 2*n+1) if(dp[i] != P{-1, -1}) {\n cout << i << '\\n';\n break;\n }\n }\n}", "accuracy": 1, "time_ms": 3280, "memory_kb": 3640, "score_of_the_acc": -0.9718, "final_rank": 12 }, { "submission_id": "aoj_1668_9400719", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nint main(void) {\n while(1) {\n int n;\n cin >> n;\n if(n == 0) break;\n vector<pair<int, int>> p(n);\n rep(i, 0, n) {\n cin >> p[i].first >> p[i].second;\n swap(p[i].first, p[i].second);\n }\n vector<pair<int, int>> dp(2 * n + 1, {-1, -1});\n dp[0] = {n - 1, n - 1};\n rep(i, 1, n + 1) {\n vector<pair<int, int>> ndp(2 * n + 1, {-1, -1});\n rep(j, 0, 2 * n + 1) {\n if(p[i - 1] <= dp[j]) {\n ndp[j] = max(ndp[j], p[i - 1]);\n }\n if(j - 1 >= 0 and (dp[j - 1].first != -1 and dp[j - 1].second != -1)) {\n if(p[i - 1].second <= dp[j - 1].second) {\n if(p[i - 1].first == dp[j - 1].first) {\n ndp[j] = max(ndp[j], dp[j - 1]);\n } else {\n ndp[j] = max(ndp[j], {dp[j - 1].first, p[i - 1].second});\n }\n } else {\n if(p[i - 1].first == dp[j - 1].first) {\n ndp[j] = max(ndp[j], dp[j - 1]);\n } else {\n if(p[i - 1].first == dp[j - 1].first - 1) {\n ndp[j] = max(ndp[j], {p[i - 1].first, n - 1});\n } else {\n ndp[j] = max(ndp[j], {dp[j - 1].first - 1, p[i - 1].second});\n }\n }\n }\n }\n if(j - 2 >= 0 and (dp[j - 2].first != -1 and dp[j - 2].second != -1)) {\n ndp[j] = max(ndp[j], dp[j - 2]);\n }\n }\n swap(dp, ndp);\n }\n // rep(i, 0, n + 1) {\n // rep(j, 0, 2 * n + 1) {\n // cout << i << ' ' << j << ' ' << dp[i][j].first << ' ' << dp[i][j].second << '\\n';\n // }\n // }\n int ans = 2 * n + 1;\n rep(i, 0, 2 * n + 1) {\n if(dp[i].first != -1 and dp[i].second != -1) {\n ans = i;\n break;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 1890, "memory_kb": 3516, "score_of_the_acc": -0.5212, "final_rank": 7 } ]
aoj_1677_cpp
Problem F Billiards You are challenging a game with prizes, which is similar to billiards. This game uses a kind of billiard table, several balls of the same size, and a prize coin. The top of the table has a flat rectangular cloth-covered area bounded by elastic bumpers, called cushions. The balls can move on this area. The cushions limit the moves so that the centers of the balls are restricted to stay within a rectangular area of width a and length b, called the movable area. Let the coordinates of the four corners of the movable area be (0, 0), ( a, 0), ( a, b ), and (0, b ). At the start of a game, all the balls and the coin are placed on the table so that their centers are at distinct inner lattice points on the movable area, that is, not on its boundaries. As the balls and the coin are small enough, they do not touch each other unless a ball goes toward the center of another ball or the coin. The challenger chooses one of the balls, and strikes it with a cue to the direction with an angle of 45 degrees to the edges of the boundary of the movable area. The direction is such that both x- and y- coordinates increase. The struck ball will move straight until it reaches a boundary of the movable area, hits another ball, or drops into one of the holes placed at four corners. When the ball reaches a boundary, it bounces on the cushion at the reflection angle equal to its incident angle and continues its straight move. When it hits another ball or drops into a corner hole, the game is over. If it hits the coin before hitting another ball or dropping into a hole, the challenger will win the coin. Which ball should you strike to win the coin? You want to know all the balls bringing you the coin. Figure F-1 depicts the first and the second datasets of Sample Input. Figure F-1: The first and the second datasets of Sample Input For the first dataset, when the ball No. 2 is struck, it goes directly toward the coin. The ball No. 4 hits the coin after bouncing on the right-side cushion. As the ball No. 1 hits the ball No. 2, and the ball No. 3 drops in the upper right hole, striking them will not be awarded a coin. For the second dataset, when the only ball No. 1 is struck, after bouncing on the cushions 5 times, it will pass through its original position, and then after bouncing twice more, it reaches the coin. Input The input consists of multiple datasets, each in the following format. a b x y n x 1 y 1 ⋮ x n y n The first line has five integers. a and b are the width and the length of the movable area, respectively, satisfying 2 ≤ a ≤ 10 6 and 2 ≤ b ≤ 10 6 . x and y give the coordinates ( x, y ) of the position of the coin, satisfying 1 ≤ x < a and 1 ≤ y < b. n is the number of balls, satisfying 1 ≤ n ≤ 10 5 . The balls are numbered from 1 to n, and the i -th line in the following n lines has the c ...(truncated)
[ { "submission_id": "aoj_1677_10850999", "code_snippet": "// I made some changes to the original code.\n\n// #include<bits/stdc++.h>\n#include<iostream>\n#include<algorithm>\n#include<set>\n#include<tuple>\n#include<vector>\n#include<utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\n#define rep(i,n) for(int i = 0;i < n;i++)\n#define per(i,n) for(int i = n - 1;i >= 0;i--)\n#define fore(e,v) for(auto &&e,v)\n#define si(a) (int)(size(a))\n#define all(a) begin(a),end(a)\n#define lb(v,x) (lower_bound(all(v),x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T , typename S> bool chmin(T &a, const S& b){\n\treturn a > b ? a = b, 1:0;\n}\ntemplate<typename T , typename S> bool chmax(T &a, const S& b){\n\treturn a < b ? a = b, 1:0;\n}\n\nconst int inf = 3e8;\n\n// struct _{\n// \t_(){\n// \t\tcin.tie(0) -> sync_with_stdio(0),cout.tie(0);\n// \t}\n// }__;\n\nint main(){\n\t// cout << \"start\" << endl;\n\twhile(1){\n\t\tint a,b,x,y,n;\n\t\tcin>>a>>b>>x>>y>>n;\n\t\tif(a + b + x + y + n == 0)break;\n\t\tll base = 1e6 + 10;\n\t\tvector<pll>apb_a[2 * base], amb_a[2 * base];\n\t\trep(i,n){\n\t\t\tll x,y;\n\t\t\tcin>>x>>y;\n\n\t\t\tapb_a[x + y].eb(x,i);\n\t\t\tamb_a[base + x - y].eb(x,i);\n\t\t}\n\t\trep(i,2 * base){\n\t\t\tsort(all(apb_a[i]));\n\t\t\tsort(all(amb_a[i]));\n\t\t}\n\t\tll dx[] = {-1,1,-1,1};\n\t\tll dy[] = {-1,-1,1,1};\n\t\tset<ll> ans;\n\t\trep(d0,4){\n\t\t\tset<tuple<ll,ll,ll> > vis;\n\t\t\tll D = d0,X = x,Y = y;\n\t\t\tll pres = -1;\n\t\t\tauto push = [&](int i){\n\t\t\t\tif(pres != -1 && i != pres)return false;\n\t\t\t\tpres = i;\n\t\t\t\treturn true;\n\t\t\t};\n\t\t\twhile(1){\n\t\t\t\t// cout << \"continue\" << endl;\n\t\t\t\tif((X == 0 || X == a) && (Y == 0 || Y == b))break;\n\t\t\t\tif(vis.count({X,Y,D}))break;\n\t\t\t\tvis.insert({X,Y,D});\n\t\t\t\t// cout<<\"X,Y,D = \"<<X<<\" \"<<Y<<\" \"<<D<<endl;\n\t\t\t\tif(D == 0){\n\t\t\t\t\tll j = lb(amb_a[X - Y + base],make_pair(X,(ll)-1)) - 1;\n\t\t\t\t\t// cout<<\"j = \"<<j<<endl;\n\t\t\t\t\tif(j >= 0){\n\t\t\t\t\t\tif(!push(amb_a[X - Y + base][j].second))break;\n\t\t\t\t\t\tans.insert(amb_a[X - Y + base][j].second);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 1){\n\t\t\t\t\tll j = lb(apb_a[X + Y],make_pair(X,(ll)-1)) ;\n\t\t\t\t\tif(j < si(apb_a[X + Y])){\n\t\t\t\t\t\tif(!push(apb_a[X + Y][j].second))break;\n\t\t\t\t\t\tif(j + 1 < si(apb_a[X + Y]))break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 2){\n\t\t\t\t\tll j = lb(apb_a[X + Y],make_pair(X,(ll)-1)) - 1;\n\t\t\t\t\tif(j >= 0){\n\t\t\t\t\t\tif(!push(apb_a[X + Y][j].second))break;\n\t\t\t\t\t\tif(j - 1 >= 0)break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 3){\n\t\t\t\t\tll j = lb(amb_a[X - Y + base],make_pair(X,(ll)-1)) ;\n\t\t\t\t\tif(j < si(amb_a[X - Y + base])){\n\t\t\t\t\t\tif(!push(amb_a[X - Y + base][j].second))break;\n\t\t\t\t\t\tif(j + 1 < si(amb_a[X - Y + base]))break;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tint dd = inf;\n\t\t\t\tif(dx[D] == -1)chmin(dd,X);\n\t\t\t\telse chmin(dd,a - X);\n\t\t\t\tif(dy[D] == -1)chmin(dd,Y);\n\t\t\t\telse chmin(dd,b - Y);\n\n\t\t\t\tX += dx[D] * dd, Y += dy[D] * dd;\n\t\t\t\tif(X == 0 || X == a)D ^= 1;\n\t\t\t\tif(Y == 0 || Y == b)D ^= 2;\n\t\t\t}\n\t\t}\n\t\tif(si(ans) == 0)cout<<\"No\"<<endl;\n\t\telse{\n\t\t\tauto it = ans.begin();\n\t\t\trep(i,si(ans)){\n\t\t\t\tcout<<(*it) + 1<<\" \";\n\t\t\t\tit++;\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 1670, "memory_kb": 155872, "score_of_the_acc": -1.1562, "final_rank": 8 }, { "submission_id": "aoj_1677_10701314", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\nusing bint = __int128_t;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define TT template <typename T>\nTT using vec = vector<T>;\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\ntemplate <class T> bool chmin(T& x, T y) {\n\treturn x > y ? (x = y, true) : false;\n}\ntemplate <class T> bool chmax(T& x, T y) {\n\treturn x < y ? (x = y, true) : false;\n}\nstruct io_setup {\n\tio_setup() {\n\t\tios::sync_with_stdio(false);\n\t\tstd::cin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} io_setup;\n\nint A, B, X, Y, n;\nconstexpr int shf = 1e6 + 10;\nvector<vector<array<int, 2>>> stad(shf * 2), stsb(shf * 2);\narray<int, 2> XY;\narray<array<char, shf>, 4> wud;\narray<array<char, shf>, 4> hlr;\nvector<int> dx = {1, -1, 1, -1};\nvector<int> dy = {1, 1, -1, -1};\narray<int, 2> stxy;\n\nbool get_from(int a, int b, int dd) {\n\tset<array<int, 3>> st;\n\tint result = -1;\n\twhile (1) {\n\t\tif (st.count({a, b, dd})) {\n\t\t\tresult = 0;\n\t\t\tbreak;\n\t\t}\n\t\tst.insert({a, b, dd});\n\t\tif (dd == 1 || dd == 2) {\n\t\t\tif (!stad[a + b].empty()) {\n\t\t\t\tif (dd == 1) {\n\t\t\t\t\tauto itr = stad[a + b].rbegin();\n\t\t\t\t\tif (*itr == XY) {\n\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t} else if (*itr == stxy) {\n\t\t\t\t\t\titr++;\n\t\t\t\t\t\tif (itr != stad[a + b].rend() && *itr == XY) {\n\t\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tauto itr = stad[a + b].begin();\n\t\t\t\t\tif (*itr == XY) {\n\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t} else if (*itr == stxy) {\n\t\t\t\t\t\titr++;\n\t\t\t\t\t\tif (itr != stad[a + b].end() && *itr == XY) {\n\t\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif (!stsb[a - b + shf].empty()) {\n\t\t\t\tif (dd == 0) {\n\t\t\t\t\tauto itr = stsb[a - b + shf].begin();\n\t\t\t\t\tif (*itr == XY) {\n\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t} else if (*itr == stxy) {\n\t\t\t\t\t\titr++;\n\t\t\t\t\t\tif (itr != stsb[a - b + shf].end() && *itr == XY) {\n\t\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tauto itr = stsb[a - b + shf].rbegin();\n\t\t\t\t\tif (*itr == XY) {\n\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t} else if (*itr == stxy) {\n\t\t\t\t\t\titr++;\n\t\t\t\t\t\tif (itr != stsb[a - b + shf].rend() && *itr == XY) {\n\t\t\t\t\t\t\tresult = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresult = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tint tmp = 1e9;\n\t\t\tif (dx[dd] > 0) chmin(tmp, A - a);\n\t\t\telse chmin(tmp, a);\n\t\t\tif (dy[dd] > 0) chmin(tmp, B - b);\n\t\t\telse chmin(tmp, b);\n\t\t\ta += tmp * dx[dd];\n\t\t\tb += tmp * dy[dd];\n\t\t}\n\t\tif (a == 0 || a == A) {\n\t\t\tif (b == 0 || b == B) {\n\t\t\t\tresult = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdd ^= 1;\n\t\t\tif (hlr[dd][b] != -1) {\n\t\t\t\tresult = hlr[dd][b];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t} else {\n\t\t\tassert(b == 0 || b == B);\n\t\t\tif (a == 0 || a == A) {\n\t\t\t\tresult = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdd ^= 2;\n\t\t\tif (wud[dd][a] != -1) {\n\t\t\t\tresult = wud[dd][a];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tfor (auto [aa, bb, ddd] : st) {\n\t\tif (aa == 0 || aa == A) {\n\t\t\thlr[ddd][bb] = result;\n\t\t} else {\n\t\t\tassert(bb == 0 || bb == B);\n\t\t\twud[ddd][aa] = result;\n\t\t}\n\t}\n\treturn result;\n}\n\nint solve() {\n\tcin >> A >> B >> X >> Y >> n;\n\tif (A == 0) return 0;\n\tXY = {X, Y};\n\tvector<int> x(n), y(n);\n\trep(i, 0, n) cin >> x[i] >> y[i];\n\tx.push_back(X);\n\ty.push_back(Y);\n\trep(i, 0, 4) rep(j, 0, A + 1) wud[i][j] = -1;\n\trep(i, 0, 4) rep(j, 0, B + 1) hlr[i][j] = -1;\n\tvector<int> ndad, ndsb;\n\trep(i, 0, n + 1) {\n\t\tstad[x[i] + y[i]].push_back({x[i], y[i]});\n\t\tstsb[x[i] - y[i] + shf].push_back({x[i], y[i]});\n\t\tndad.push_back(x[i] + y[i]);\n\t\tndsb.push_back(x[i] - y[i] + shf);\n\t}\n\tsort(ndad.begin(), ndad.end());\n\tsort(ndsb.begin(), ndsb.end());\n\tndad.erase(unique(ndad.begin(), ndad.end()), ndad.end());\n\tndsb.erase(unique(ndsb.begin(), ndsb.end()), ndsb.end());\n\tfor (int ind : ndad) sort(stad[ind].begin(), stad[ind].end());\n\tfor (int ind : ndsb) sort(stsb[ind].begin(), stsb[ind].end());\n\tauto get_get_from = [&](int a, int b) -> bool {\n\t\tint dd = 0;\n\t\tauto itr = upper_bound(stsb[a - b + shf].begin(),\n\t\t\t\t\t\t\t stsb[a - b + shf].end(), stxy);\n\t\tif (itr != stsb[a - b + shf].end()) {\n\t\t\tif (*itr == XY) return 1;\n\t\t\treturn 0;\n\t\t}\n\t\t{\n\t\t\tint tmp = min(A - a, B - b);\n\t\t\ta += tmp;\n\t\t\tb += tmp;\n\t\t}\n\t\tif (a == 0 || a == A) {\n\t\t\tif (b == 0 || b == B) return 0;\n\t\t\tdd ^= 1;\n\t\t\tif (hlr[dd][b] != -1) return hlr[dd][b];\n\t\t\treturn hlr[dd][b] = get_from(a, b, dd);\n\t\t} else {\n\t\t\tassert(b == 0 || b == B);\n\t\t\tif (a == 0 || a == A) return 0;\n\t\t\tdd ^= 2;\n\t\t\tif (wud[dd][a] != -1) return wud[dd][a];\n\t\t\treturn wud[dd][a] = get_from(a, b, dd);\n\t\t}\n\t};\n\tvector<int> ans;\n\trep(i, 0, n) {\n\t\tstxy = {x[i], y[i]};\n\t\tif (get_get_from(x[i], y[i])) ans.push_back(i);\n\t}\n\tif (ans.empty()) {\n\t\tcout << \"No\" << endl;\n\t} else {\n\t\tfor (int val : ans) cout << val + 1 << \" \";\n\t\tcout << endl;\n\t}\n\tfor (int ind : ndad) stad[ind].clear();\n\tfor (int ind : ndsb) stsb[ind].clear();\n\treturn 1;\n}\n\nint main() {\n\twhile (solve());\n}", "accuracy": 1, "time_ms": 2340, "memory_kb": 244020, "score_of_the_acc": -1.8281, "final_rank": 14 }, { "submission_id": "aoj_1677_10685835", "code_snippet": "#include <bits/stdc++.h>\n#define fi first\n#define se second\n#define rep(i,s,n) for (int i = (s); i < (n); ++i)\n#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n#define len(x) (int)(x).size()\n#define dup(x,y) (((x)+(y)-1)/(y))\n#define pb push_back\n#define eb emplace_back\n#define Field(T) vector<vector<T>>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\ntemplate<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;\nusing P = pair<int,int>;\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\n\nbool solve() {\n int a, b, p, q, n;\n cin >> a >> b >> p >> q >> n;\n if (n == 0) return 1;\n vector<int> x(n), y(n);\n rep(i,0,n) cin >> x[i] >> y[i];\n rep(i,0,n) {\n x.eb(2*a-x[i]), y.eb(y[i]);\n x.eb(x[i]), y.eb(2*b-y[i]);\n x.eb(2*a-x[i]), y.eb(2*b-y[i]);\n }\n x.eb(0), x.eb(0), x.eb(a), x.eb(a);\n y.eb(0), y.eb(b), y.eb(0), y.eb(b);\n map<int,vector<P>> mp;\n rep(i,0,4*n+4) {\n mp[y[i]-x[i]].eb(x[i], i);\n }\n for (auto &[t, v] : mp) {\n sort(all(v));\n }\n function<int(map<int,vector<P>> &,int,int)> get = [&](map<int,vector<P>> &mp, int p, int q) {\n if (mp.count(q-p)) {\n vector<P> &v = mp[q-p];\n if (p > v[0].fi) {\n rrep(i,len(v),0) {\n if (p > v[i].fi) {\n return v[i].se;\n }\n }\n }\n }\n int k = q-p;\n int f = -1;\n rep(i,0,2*(a+b)+2) {\n if (k > 0) k -= 2*a;\n else k += 2*b;\n if (mp.count(k)) {\n vector<P> &v = mp[k];\n int idx = v.back().se;\n if (idx >= 4*n) return -1;\n if (f != -1) {\n if (idx >= n) return -1;\n if (f == idx) return idx;\n else return -1;\n }\n if (idx < n) return idx;\n f = (idx-n)/3;\n }\n }\n return -1;\n };\n vector<P> ps = {{p, q}, {2*a-p, q}, {p, 2*b-q}, {2*a-p, 2*b-q}};\n set<int> st;\n rep(i,0,4) {\n int idx = get(mp, ps[i].fi, ps[i].se);\n st.emplace(idx);\n }\n vector<int> ans;\n for (int e : st) {\n if (e == -1 || e >= n) continue;\n ans.eb(e+1);\n }\n if (ans.empty()) {\n cout << \"No\" << endl;\n return 0;\n }\n for (int e : ans) {\n cout << e << \" \";\n }\n cout << endl;\n return 0;\n}\n\nint main() {\n while(!solve());\n return 0;\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 48212, "score_of_the_acc": -0.4479, "final_rank": 3 }, { "submission_id": "aoj_1677_10682748", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int dx[]={1,1,-1,-1},dy[]={1,-1,-1,1};\n\nint main() {\n while(1){\n int A,B,CX,CY,N;\n cin>>A>>B>>CX>>CY>>N;\n if(A==0)return 0;\n vector<int>X(N),Y(N);\n vector<set<pair<int,int>>>se_plus(A+B+1),se_minus(A+B+1);\n rep(i,0,N){\n cin>>X[i]>>Y[i];\n se_plus[X[i]+Y[i]].insert({X[i],i});\n se_minus[X[i]-Y[i]+B].insert({X[i],i});\n }\n vector<bool>flag(N);\n rep(t,0,4){\n auto f=[&](int x,int y,int d)-> tuple<int,int,int,vector<int>> {\n int tx,ty,td;\n if(d==0){\n if(x+(B-y)<A)tx=x+(B-y),ty=B,td=1;\n else if(x+(B-y)==A)tx=A,ty=B,td=-1;\n else tx=A,ty=y+(A-x),td=3;\n }else if(d==1){\n if(x+y<A)tx=x+y,ty=0,td=0;\n else if(x+y==A)tx=A,ty=0,td=-1;\n else tx=A,ty=y-(A-x),td=2;\n }else if(d==2){\n if(x-y>0)tx=x-y,ty=0,td=3;\n else if(x-y==0)tx=0,ty=0,td=-1;\n else tx=0,ty=y-x,td=1;\n }else{\n if(x-(B-y)>0)tx=x-(B-y),ty=B,td=2;\n else if(x-(B-y)==0)tx=0,ty=B,td=-1;\n else tx=0,ty=y+x,td=0;\n }\n vector<int>tv;\n if(d==0){\n auto id=se_minus[x-y+B].lower_bound({x+1,0});\n while(id!=se_minus[x-y+B].end()&&(*id).first<=tx){\n tv.push_back((*id).second);\n id++;\n }\n }else if(d==1){\n auto id=se_plus[x+y].lower_bound({x+1,0});\n while(id!=se_plus[x+y].end()&&(*id).first<=tx){\n tv.push_back((*id).second);\n id++;\n }\n }else if(d==2){\n auto id=se_minus[x-y+B].lower_bound({x,-1});\n if(id!=se_minus[x-y+B].begin()){\n id--;\n while((*id).first>=tx){\n tv.push_back((*id).second);\n if(id==se_minus[x-y+B].begin())break;\n id--;\n }\n }\n }else{\n auto id=se_plus[x+y].lower_bound({x,-1});\n if(id!=se_plus[x+y].begin()){\n id--;\n while((*id).first>=tx){\n tv.push_back((*id).second);\n if(id==se_plus[x+y].begin())break;\n id--;\n }\n }\n }\n return make_tuple(tx,ty,td,tv);\n };\n int nx=CX,ny=CY,nd=t;\n vector<int>nv;\n set<int>se;\n rep(cnt,0,4*(A+B)+5){\n auto[mx,my,md,mv]=f(nx,ny,nd);\n if(nd==2){\n if(mv.size()){\n int ax=-1,ai=-1;\n for(auto i:mv){\n if(chmax(ax,X[i])){\n ai=i;\n }\n }\n if(se.empty()||((int)se.size()==1&&*se.begin()==ai)){\n flag[ai]=true;\n }\n break;\n }\n }\n for(auto x:mv)se.insert(x);\n if(se.size()>1)break;\n swap(nx,mx);\n swap(ny,my);\n swap(nd,md);\n swap(nv,mv);\n if(nd==-1)break;\n }\n }\n vector<int>ans;\n rep(i,0,N){\n if(flag[i])ans.push_back(i+1);\n }\n if(ans.empty())cout<<\"No\"<<endl;\n else{\n for(auto a:ans)cout<<a<<\" \";\n cout<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 219180, "score_of_the_acc": -0.8961, "final_rank": 5 }, { "submission_id": "aoj_1677_10682240", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O0\")\n#else\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\n// #include <bits/extc++.h>\nusing namespace std;\n\n// #include <atcoder/modint>\n// using mint = atcoder::modint998244353;\n// istream& operator>>(istream& in, mint& x) {\n// long long a;\n// in >> a;\n// x = a;\n// return in;\n// }\n// ostream& operator<<(ostream& out, const mint& x) {\n// return out << x.val();\n// }\n\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n\ntemplate <class T>\nconstexpr T infty = 0;\ntemplate <>\nconstexpr int infty<int> = 1'000'000'000;\ntemplate <>\nconstexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;\ntemplate <>\nconstexpr u32 infty<u32> = infty<int>;\ntemplate <>\nconstexpr u64 infty<u64> = infty<ll>;\ntemplate <>\nconstexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;\ntemplate <>\nconstexpr double infty<double> = infty<ll>;\ntemplate <>\nconstexpr long double infty<long double> = infty<ll>;\n\nusing pi = pair<int, int>;\nusing pl = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\ntemplate <class T>\nusing vc = vector<T>;\ntemplate <class T>\nusing vvc = vector<vc<T>>;\nusing vvi = vvc<int>;\nusing vvl = vvc<ll>;\ntemplate <class T>\nusing vvvc = vector<vvc<T>>;\ntemplate <class T>\nusing vvvvc = vector<vvvc<T>>;\ntemplate <class T>\nusing vvvvvc = vector<vvvvc<T>>;\ntemplate <class T>\nusing pqg = std::priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U>\nusing umap = unordered_map<T, U>;\n\n// template <typename K>\n// using tree = __gnu_pbds::tree<K, __gnu_pbds::null_type, std::less<>,\n// __gnu_pbds::rb_tree_tag,\n// __gnu_pbds::tree_order_statistics_node_update>;\n\n#define vv(type, name, h, ...) \\\n vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) \\\n vector<vector<vector<type>>> name( \\\n h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name( \\\n a, vector<vector<vector<type>>>( \\\n b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n\n// FOR(a) := for (ll _ = 0; _ < (ll)a; ++_)\n// FOR(i, a) := for (ll i = 0; i < (ll)a; ++i)\n// FOR(i, a, b) := for (ll i = a; i < (ll)b; ++i)\n// FOR(i, a, b, c) := for (ll i = a; i < (ll)b; i += (c))\n// FOR_R(a) := for (ll i = (a) - 1; i >= 0; --i)\n// FOR_R(i, a) := for (ll i = (a) - 1; i >= 0; --i)\n// FOR_R(i, a, b) := for (ll i = (b) - 1; i >= (ll)a; --i)\n#define FOR1(a) for (ll _ = 0; _ < (ll)a; ++_)\n#define FOR2(i, a) for (ll i = 0; i < (ll)a; ++i)\n#define FOR3(i, a, b) for (ll i = a; i < (ll)b; ++i)\n#define FOR4(i, a, b, c) for (ll i = a; i < (ll)b; i += (c))\n#define FOR1_R(a) for (ll i = (a) - 1; i >= 0; --i)\n#define FOR2_R(i, a) for (ll i = (a) - 1; i >= 0; --i)\n#define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= (ll)a; --i)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)\n#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)\n\n#define FOR_subset(t, s) \\\n for (int t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate <typename T>\nT floor(T a, T b) {\n return a / b - (a % b && (a ^ b) < 0);\n}\ntemplate <typename T>\nT ceil(T x, T y) {\n return floor(x + y - 1, y);\n}\ntemplate <typename T>\nT bmod(T x, T y) {\n return x - y * floor(x, y);\n}\ntemplate <typename T>\npair<T, T> divmod(T x, T y) {\n T q = floor(x, y);\n return {q, x - q * y};\n}\n\ntemplate <typename T, typename U>\nT POW(U x_, int n) {\n T x = x_;\n T ret = 1;\n while (n > 0) {\n if (n & 1) ret *= x;\n x *= x;\n n >>= 1;\n }\n return ret;\n}\n\ntemplate <typename T, typename U>\nT SUM(const vector<U> &A) {\n T sm = 0;\n for (auto &&a : A) sm += a;\n return sm;\n}\n\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) \\\n sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()\n\ntemplate <class T, class S>\ninline bool chmax(T &a, const S &b) {\n return (a < b ? a = b, 1 : 0);\n}\ntemplate <class T, class S>\ninline bool chmin(T &a, const S &b) {\n return (a > b ? a = b, 1 : 0);\n}\n\n// ? は -1\nvc<int> s_to_vi(const string &S, char first_char) {\n vc<int> A(S.size());\n FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }\n return A;\n}\n\ntemplate <typename T, typename U>\nvector<T> cumsum(vector<U> &A, int off = 1) {\n int N = A.size();\n vector<T> B(N + 1);\n FOR(i, N) { B[i + 1] = B[i] + A[i]; }\n if (off == 0) B.erase(B.begin());\n return B;\n}\n\ntemplate <typename T>\nvector<int> argsort(const vector<T> &A) {\n vector<int> ids(A.size());\n iota(all(ids), 0);\n sort(all(ids),\n [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });\n return ids;\n}\n\n// A[I[0]], A[I[1]], ...\ntemplate <typename T>\nvc<T> rearrange(const vc<T> &A, const vc<int> &I) {\n vc<T> B(I.size());\n FOR(i, I.size()) B[i] = A[I[i]];\n return B;\n}\n\ntemplate <class... T>\nconstexpr auto min(T... a) {\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate <class... T>\nconstexpr auto max(T... a) {\n return max(initializer_list<common_type_t<T...>>{a...});\n}\n\nvoid print() { cout << '\\n'; }\ntemplate <class T>\nvoid print(const T &a) {\n cout << a << '\\n';\n}\ntemplate <class T, class... Ts>\nvoid print(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\ntemplate <class T>\nvoid print(vector<T> &a) {\n for (int i = 0; i < (int)a.size(); ++i) {\n cout << a[i] << \" \\n\"[i == (int)a.size() - 1];\n }\n}\ntemplate <class T>\nvoid print(vector<T> &&a) {\n for (int i = 0; i < (int)a.size(); ++i) {\n cout << a[i] << \" \\n\"[i == (int)a.size() - 1];\n }\n}\ntemplate <class T>\nvoid print(vector<vector<T>> &a) {\n for (int i = 0; i < (int)a.size(); ++i) {\n for (int j = 0; j < (int)a[i].size(); ++j) {\n cout << a[i][j] << \" \\n\"[j == (int)a[i].size() - 1];\n }\n }\n}\ntemplate <class T>\nvoid print(vector<vector<T>> &&a) {\n for (int i = 0; i < (int)a.size(); ++i) {\n for (int j = 0; j < (int)a[i].size(); ++j) {\n cout << a[i][j] << \" \\n\"[j == (int)a[i].size() - 1];\n }\n }\n}\nvoid YESNO(bool b) { cout << (b ? \"YES\" : \"NO\") << endl; }\nvoid YesNo(bool b) { cout << (b ? \"Yes\" : \"No\") << endl; }\n\n// #ifdef LOCAL\n// https://zenn.dev/sassan/articles/19db660e4da0a4\n// #include \"/export/home/ICPC/ICPC05/workspace/cpp-dump/dump.hpp\"\n// #define dump(...) cpp_dump(__VA_ARGS__)\n// // CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val());\n// #else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n// #endif\n\n#include <atcoder/math>\n\n//----------------------------------------------------------------\nvoid solve() {\n ll A, B, X, Y, N;\n cin >> A >> B >> X >> Y >> N;\n if (A == 0 && B == 0 && X == 0 && Y == 0 && N == 0) {\n exit(0);\n }\n vl xs(N), ys(N);\n FOR(i, N) { cin >> xs[i] >> ys[i]; }\n vc<pl> ans(4, {infty<ll>, -1});\n FOR(l, 4) {\n FOR(i, N) {\n ll x = xs[i];\n ll y = ys[i];\n ll xx, yy;\n switch (l) {\n case 0: {\n xx = x;\n yy = y;\n break;\n }\n case 1: {\n xx = A + A - x;\n yy = y;\n break;\n }\n case 2: {\n xx = A + A - x;\n yy = B + B - y;\n break;\n }\n case 3: {\n xx = x;\n yy = B + B - y;\n break;\n }\n }\n ll corner = infty<ll>;\n {\n vl r = {A, B};\n vl m = {A - xx, B - yy};\n pl c = atcoder::crt(m, r);\n if (c != pl(0, 0)) corner = c.first;\n }\n FOR(k, 4) {\n vl r = {A + A, B + B};\n vl m;\n if (k == 0) {\n m = {X - xx, Y - yy};\n } else if (k == 1) {\n m = {A + A - X - xx, Y - yy};\n } else if (k == 2) {\n m = {A + A - X - xx, B + B - Y - yy};\n } else {\n m = {X - xx, B + B - Y - yy};\n }\n pl c = atcoder::crt(m, r);\n if (c == pl(0, 0)) continue;\n ll coin = c.first;\n if (corner < coin) continue;\n if (coin < ans[k].first) {\n if (l == 0) {\n ans[k] = {coin, i};\n } else {\n if (ans[k].second != i) {\n ans[k] = {coin, -1};\n }\n }\n }\n }\n }\n }\n vl res;\n for (auto p : ans) {\n if (p.second != -1) {\n res.emplace_back(p.second + 1);\n }\n }\n if (res.size() == 0) {\n print(\"No\");\n } else {\n UNIQUE(res);\n sort(all(res));\n FOR(i, res.size()) {\n cout << res[i] << \" \";\n }\n cout << endl;\n }\n}\n\nint main() {\n while (true) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 4948, "score_of_the_acc": -0.0226, "final_rank": 1 }, { "submission_id": "aoj_1677_10623782", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\n#define vl vector<ll>\n#define vll vector<ll>\n#define rep(i,n) for(int i = 0;i < n;i++)\n#define per(i,n) for(int i = n - 1;i >= 0;i--)\n#define fore(e,v) for(auto &&e,v)\n#define si(a) (int)(size(a))\n#define all(a) begin(a),end(a)\n#define lb(v,x) (lower_bound(all(v),x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T , typename S> bool chmin(T &a, const S& b){\n\treturn a > b ? a = b, 1:0;\n}\ntemplate<typename T , typename S> bool chmax(T &a, const S& b){\n\treturn a < b ? a = b, 1:0;\n}\n\nconst ll inf = 3e18;\n\nstruct _{\n\t_(){\n\t\tcin.tie(0) -> sync_with_stdio(0),cout.tie(0);\n\t}\n}__;\n\nint main(){\n\twhile(1){\n\t\tll a,b,x,y,n;\n\t\tcin>>a>>b>>x>>y>>n;\n\t\tif(a + b + x + y + n == 0)break;\n\t\tll base = 1e6 + 10;\n\t\tvector<pll>apb_a[2 * base], amb_a[2 * base];\n\t\trep(i,n){\n\t\t\tll x,y;\n\t\t\tcin>>x>>y;\n\n\t\t\tapb_a[x + y].eb(x,i);\n\t\t\tamb_a[base + x - y].eb(x,i);\n\t\t}\n\t\trep(i,2 * base){\n\t\t\tsort(all(apb_a[i]));\n\t\t\tsort(all(amb_a[i]));\n\t\t}\n\t\tll dx[] = {-1,1,-1,1};\n\t\tll dy[] = {-1,-1,1,1};\n\t\tset<ll> ans;\n\t\trep(d0,4){\n\t\t\tset<tuple<ll,ll,ll> > vis;\n\t\t\tll D = d0,X = x,Y = y;\n\t\t\tll pres = -1;\n\t\t\tauto push = [&](ll i){\n\t\t\t\tif(pres != -1 && i != pres)return false;\n\t\t\t\tpres = i;\n\t\t\t\treturn true;\n\t\t\t};\n\t\t\twhile(1){\n\t\t\t\tif((X == 0 || X == a) && (Y == 0 || Y == b))break;\n\t\t\t\tif(vis.count({X,Y,D}))break;\n\t\t\t\tvis.insert({X,Y,D});\n\t\t\t\t// cout<<\"X,Y,D = \"<<X<<\" \"<<Y<<\" \"<<D<<endl;\n\t\t\t\tif(D == 0){\n\t\t\t\t\tll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) - 1;\n\t\t\t\t\t// cout<<\"j = \"<<j<<endl;\n\t\t\t\t\tif(j >= 0){\n\t\t\t\t\t\tif(!push(amb_a[X - Y + base][j].second))break;\n\t\t\t\t\t\tans.insert(amb_a[X - Y + base][j].second);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 1){\n\t\t\t\t\tll j = lb(apb_a[X + Y],pair(X,(ll)-1)) ;\n\t\t\t\t\tif(j < si(apb_a[X + Y])){\n\t\t\t\t\t\tif(!push(apb_a[X + Y][j].second))break;\n\t\t\t\t\t\tif(j + 1 < si(apb_a[X + Y]))break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 2){\n\t\t\t\t\tll j = lb(apb_a[X + Y],pair(X,(ll)-1)) - 1;\n\t\t\t\t\tif(j >= 0){\n\t\t\t\t\t\tif(!push(apb_a[X + Y][j].second))break;\n\t\t\t\t\t\tif(j - 1 >= 0)break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 3){\n\t\t\t\t\tll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) ;\n\t\t\t\t\tif(j < si(amb_a[X - Y + base])){\n\t\t\t\t\t\tif(!push(amb_a[X - Y + base][j].second))break;\n\t\t\t\t\t\tif(j + 1 < si(amb_a[X - Y + base]))break;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tll dd = inf;\n\t\t\t\tif(dx[D] == -1)chmin(dd,X);\n\t\t\t\telse chmin(dd,a - X);\n\t\t\t\tif(dy[D] == -1)chmin(dd,Y);\n\t\t\t\telse chmin(dd,b - Y);\n\n\t\t\t\tX += dx[D] * dd, Y += dy[D] * dd;\n\t\t\t\tif(X == 0 || X == a)D ^= 1;\n\t\t\t\tif(Y == 0 || Y == b)D ^= 2;\n\t\t\t}\n\t\t}\n\t\tif(si(ans) == 0)cout<<\"No\"<<endl;\n\t\telse{\n\t\t\tauto it = ans.begin();\n\t\t\trep(i,si(ans)){\n\t\t\t\tcout<<(*it) + 1<<\" \";\n\t\t\t\tit++;\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 156124, "score_of_the_acc": -1.2115, "final_rank": 9 }, { "submission_id": "aoj_1677_10623780", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\n#define vl vector<ll>\n#define vll vector<ll>\n#define rep(i,n) for(int i = 0;i < n;i++)\n#define per(i,n) for(int i = n - 1;i >= 0;i--)\n#define fore(e,v) for(auto &&e,v)\n#define si(a) (int)(size(a))\n#define all(a) begin(a),end(a)\n#define lb(v,x) (lower_bound(all(v),x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T , typename S> bool chmin(T &a, const S& b){\n\treturn a > b ? a = b, 1:0;\n}\ntemplate<typename T , typename S> bool chmax(T &a, const S& b){\n\treturn a < b ? a = b, 1:0;\n}\n\nconst ll inf = 3e18;\n\n// struct _{\n// \t_(){\n// \t\tcin.tie(0) -> sync_with_stdio(0),cout.tie(0);\n// \t}\n// }__;\n\nint main(){\n\tll a,b,x,y,n;\n\twhile(cin>>a>>b>>x>>y>>n){\n\t\tif(a + b + x + y + n == 0)break;\n\t\tll base = 1e6 + 10;\n\t\tvector<pll>apb_a[2 * base], amb_a[2 * base];\n\t\trep(i,n){\n\t\t\tll x,y;\n\t\t\tcin>>x>>y;\n\n\t\t\tapb_a[x + y].eb(x,i);\n\t\t\tamb_a[base + x - y].eb(x,i);\n\t\t}\n\t\trep(i,2 * base){\n\t\t\tsort(all(apb_a[i]));\n\t\t\tsort(all(amb_a[i]));\n\t\t}\n\t\tll dx[] = {-1,1,-1,1};\n\t\tll dy[] = {-1,-1,1,1};\n\t\tset<ll> ans;\n\t\trep(d0,4){\n\t\t\tset<tuple<ll,ll,ll> > vis;\n\t\t\tll D = d0,X = x,Y = y;\n\t\t\tll pres = -1;\n\t\t\tauto push = [&](ll i){\n\t\t\t\tif(pres != -1 && i != pres)return false;\n\t\t\t\tpres = i;\n\t\t\t\treturn true;\n\t\t\t};\n\t\t\twhile(1){\n\t\t\t\tif((X == 0 || X == a) && (Y == 0 || Y == b))break;\n\t\t\t\tif(vis.count({X,Y,D}))break;\n\t\t\t\tvis.insert({X,Y,D});\n\t\t\t\t// cout<<\"X,Y,D = \"<<X<<\" \"<<Y<<\" \"<<D<<endl;\n\t\t\t\tif(D == 0){\n\t\t\t\t\tll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) - 1;\n\t\t\t\t\t// cout<<\"j = \"<<j<<endl;\n\t\t\t\t\tif(j >= 0){\n\t\t\t\t\t\tif(!push(amb_a[X - Y + base][j].second))break;\n\t\t\t\t\t\tans.insert(amb_a[X - Y + base][j].second);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 1){\n\t\t\t\t\tll j = lb(apb_a[X + Y],pair(X,(ll)-1)) ;\n\t\t\t\t\tif(j < si(apb_a[X + Y])){\n\t\t\t\t\t\tif(!push(apb_a[X + Y][j].second))break;\n\t\t\t\t\t\tif(j + 1 < si(apb_a[X + Y]))break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 2){\n\t\t\t\t\tll j = lb(apb_a[X + Y],pair(X,(ll)-1)) - 1;\n\t\t\t\t\tif(j >= 0){\n\t\t\t\t\t\tif(!push(apb_a[X + Y][j].second))break;\n\t\t\t\t\t\tif(j - 1 >= 0)break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(D == 3){\n\t\t\t\t\tll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) ;\n\t\t\t\t\tif(j < si(amb_a[X - Y + base])){\n\t\t\t\t\t\tif(!push(amb_a[X - Y + base][j].second))break;\n\t\t\t\t\t\tif(j + 1 < si(amb_a[X - Y + base]))break;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tll dd = inf;\n\t\t\t\tif(dx[D] == -1)chmin(dd,X);\n\t\t\t\telse chmin(dd,a - X);\n\t\t\t\tif(dy[D] == -1)chmin(dd,Y);\n\t\t\t\telse chmin(dd,b - Y);\n\n\t\t\t\tX += dx[D] * dd, Y += dy[D] * dd;\n\t\t\t\tif(X == 0 || X == a)D ^= 1;\n\t\t\t\tif(Y == 0 || Y == b)D ^= 2;\n\t\t\t}\n\t\t}\n\t\tif(si(ans) == 0)cout<<\"No\"<<endl;\n\t\telse{\n\t\t\tll m = si(ans);\n\t\t\tauto it = ans.begin();\n\t\t\trep(i,m){\n\t\t\t\tcout<<(*it) + 1<<\" \";\n\t\t\t\t// if(i == si(ans) - 1)cout<<endl;\n\t\t\t\t// else cout<<\" \";\n\t\t\t\tit++;\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1830, "memory_kb": 155828, "score_of_the_acc": -1.2284, "final_rank": 10 }, { "submission_id": "aoj_1677_10595220", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0; i<(int)n; i++)\n#define repi(i,m,n) for(int i=(int)m; i<=(int)n; i++)\n#define rrep(i,n) for(int i=(int)n-1; i>=0; i--)\n#define rrepi(i,m,n) for(int i=(int)n; i>=(int)m; i--)\n#define all(x) x.begin(),x.end()\ntemplate <typename T> inline bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; };\ntemplate <typename T> inline bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; };\ntemplate <typename T> void uniq(std::vector<T> &v){\n std::sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\nusing ll = long long;\n\nbool solve(){\n int a,b,x,y,n; cin >> a >> b >> x >> y >> n;\n if(a + b + x + y + n == 0) return false;\n vector<set<pair<int,int>>> s(2*a + 2*b + 1);\n \n vector<int> c(n+5),d(n+5);\n rep(i,n) cin >> c[i] >> d[i];\n c[n] = x; d[n] = y;\n c[n+1] = 0; d[n+1] = 0;\n c[n+2] = a; d[n+2] = 0;\n c[n+3] = 0; d[n+3] = b;\n c[n+4] = a; d[n+4] = b;\n\n rep(i,n+5){\n s[2*a - c[i] + d[i]].emplace(c[i] + d[i], i);\n s[2*a - (2*a - c[i]) + d[i]].emplace((2*a - c[i]) + d[i], i);\n s[2*a - c[i] + (2*b - d[i])].emplace(c[i] + (2*b - d[i]), i);\n s[2*a - (2*a - c[i]) + (2*b - d[i])].emplace((2*a - c[i]) + (2*b - d[i]), i);\n }\n\n vector<int> ans;\n rep(i,n){\n x = c[i]; y = d[i];\n s[2*a - (2*a - x) + y].erase(make_pair((2*a - x) + y, i));\n s[2*a - x + (2*b - y)].erase(make_pair(x + (2*b - y), i));\n s[2*a - (2*a - x) + (2*b - y)].erase(make_pair((2*a - x) + (2*b - y), i));\n\n while(1){\n int idx = 2*a - x + y, dist = x + y;\n auto p = s[idx].lower_bound(make_pair(dist, n));\n if(p == s[idx].end()){\n if(2*a - x < 2*b - y){\n y = (y + 2*a - x)%(2*b);\n x = 0;\n }\n else{\n x = (x + 2*b - y)%(2*a);\n y = 0;\n }\n } \n else{\n if((*p).second == n){\n ans.emplace_back(i+1);\n }\n break;\n }\n }\n\n x = c[i];y = d[i];\n\n s[2*a - (2*a - x) + y].emplace((2*a - x) + y, i);\n s[2*a - x + (2*b - y)].emplace(x + (2*b - y), i);\n s[2*a - (2*a - x) + (2*b - y)].emplace((2*a - x) + (2*b - y), i);\n }\n \n if(ans.size() == 0) cout << \"No\" << endl;\n else{\n uniq(ans);\n for(auto e : ans) cout << e << \" \";\n cout << endl;\n }\n \n return true;\n}\nint main(){\n while(solve());\n \n return 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 210032, "score_of_the_acc": -0.9891, "final_rank": 6 }, { "submission_id": "aoj_1677_10595217", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0; i<(int)n; i++)\n#define repi(i,m,n) for(int i=(int)m; i<=(int)n; i++)\n#define rrep(i,n) for(int i=(int)n-1; i>=0; i--)\n#define rrepi(i,m,n) for(int i=(int)n; i>=(int)m; i--)\n#define all(x) x.begin(),x.end()\ntemplate <typename T> inline bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; };\ntemplate <typename T> inline bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; };\ntemplate <typename T> void uniq(std::vector<T> &v){\n std::sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\nusing ll = long long;\n\nbool solve(){\n int a,b,x,y,n; cin >> a >> b >> x >> y >> n;\n if(a + b + x + y + n == 0) return false;\n int t = 2*a + 2*b - 1;\n vector<set<pair<int,int>>> s(t);\n \n vector<int> c(n+5),d(n+5);\n rep(i,n) cin >> c[i] >> d[i];\n c[n] = x; d[n] = y;\n c[n+1] = 0; d[n+1] = 0;\n c[n+2] = a; d[n+2] = 0;\n c[n+3] = 0; d[n+3] = b;\n c[n+4] = a; d[n+4] = b;\n\n rep(i,n+5){\n s[(-c[i] + d[i] + t)%t].emplace(c[i] + d[i], i);\n s[(t-(2*a - c[i]) + d[i])%t].emplace((2*a - c[i]) + d[i], i);\n s[(t - c[i] + (2*b - d[i]))%t].emplace(c[i] + (2*b - d[i]), i);\n s[(t - (2*a - c[i]) + (2*b - d[i]))%t].emplace((2*a - c[i]) + (2*b - d[i]), i);\n }\n\n vector<int> ans;\n rep(i,n){\n x = c[i]; y = d[i];\n s[(t - (2*a - x) + y)%t].erase(make_pair((2*a - x) + y, i));\n s[(t - x + (2*b - y))%t].erase(make_pair(x + (2*b - y), i));\n s[(t - (2*a - x) + (2*b - y))%t].erase(make_pair((2*a - x) + (2*b - y), i));\n\n while(1){\n int idx = (t - x + y)%t, dist = x + y;\n auto p = s[idx].upper_bound(make_pair(dist, n));\n if(p == s[idx].end()){\n if(2*a - x < 2*b - y){\n y = (y + 2*a - x)%(2*b);\n x = 0;\n }\n else{\n x = (x + 2*b - y)%(2*a);\n y = 0;\n }\n } \n else{\n if((*p).second == n){\n ans.emplace_back(i+1);\n }\n break;\n }\n }\n\n x = c[i];y = d[i];\n\n s[(t - (2*a - x) + y)%t].emplace((2*a - x) + y, i);\n s[(t - x + (2*b - y))%t].emplace(x + (2*b - y), i);\n s[(t - (2*a - x) + (2*b - y))%t].emplace((2*a - x) + (2*b - y), i);\n }\n \n if(ans.size() == 0) cout << \"No\" << endl;\n else{\n uniq(ans);\n for(auto e : ans) cout << e << \" \";\n cout << endl;\n }\n \n return true;\n}\nint main(){\n while(solve());\n \n return 0;\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 210216, "score_of_the_acc": -0.9943, "final_rank": 7 }, { "submission_id": "aoj_1677_10571221", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int a,int b,int cx,int cy,int n){\n vector<int> X(n),Y(n);\n int t = 2*(a+b)-1;\n vector<set<int>> s(t);\n map<pair<int,int>,int> mp;\n for (int i = 0; i < n; i++){\n int x,y,nx,ny;cin >> x >> y;\n\n X[i] = x;Y[i] = y;\n\n s[(x-y+t)%t].insert(x);\n mp[make_pair(x,y)] = i+1;\n\n nx = 2*a - x;\n s[(nx-y+t)%t].insert(nx);\n mp[make_pair(nx,y)] = i+1;\n\n ny = 2*b - y;\n s[(x-ny+t)%t].insert(x);\n mp[make_pair(x,ny)] = i+1;\n\n s[(nx-ny+t)%t].insert(nx);\n mp[make_pair(nx,ny)] = i+1;\n }\n\n s[(a-b+t)%t].insert(a);\n mp[make_pair(a,b)] = -1;\n s[(2*a-2*b+t)%t].insert(2*a);\n mp[make_pair(2*a,2*b)] = -1;\n s[(a-2*b+t)%t].insert(a);\n mp[make_pair(a,2*b)] = -1;\n s[(2*a-b+t)%t].insert(2*a);\n mp[make_pair(2*a,b)] = -1;\n\n s[(cx-cy+t)%t].insert(cx);\n mp[make_pair(cx,cy)] = 0;\n\n int nx = 2*a-cx,ny = 2*b-cy;\n s[(nx-cy+t)%t].insert(nx);\n mp[make_pair(nx,cy)] = 0;\n\n s[(cx-ny+t)%t].insert(cx);\n mp[make_pair(cx,ny)] = 0;\n\n s[(nx-ny+t)%t].insert(nx);\n mp[make_pair(nx,ny)] = 0;\n\n vector<int> ans;\n\n for (int i = 0; i < n; i++){\n int now = i+1;\n int x=X[i],y=Y[i];\n int idx = (x-y+t)%t;\n\n while(1){\n auto it = s[idx].upper_bound(x);\n if (it == s[idx].end()){\n if (2*a-x > 2*b-y){\n x += 2*b-y;\n y = 0;\n }\n else {\n y += 2*a-x;\n x = 0;\n }\n idx = (x-y+t)%t;\n continue;\n }\n else {\n nx = *it;\n ny = (nx - idx + t)%t;\n\n int tmp = mp[make_pair(nx,ny)];\n if (tmp == now){\n if (nx < a && ny < b)break;\n else {\n x = nx;\n y = ny;\n continue;\n }\n }\n\n if (tmp == 0){\n ans.push_back(now);\n break;\n }\n break;\n }\n }\n }\n sort(ans.begin(),ans.end());\n if (!ans.size())cout << \"No\";\n for (int i = 0; i < ans.size(); i++)cout << ans[i] << ' ';\n cout << endl;\n return;\n}\n\nint main(){\n while(1){\n int a,b,x,y,n;\n cin >> a >> b >> x >> y >> n;\n if (a==0)return 0;\n solve(a,b,x,y,n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 235124, "score_of_the_acc": -1.3881, "final_rank": 11 }, { "submission_id": "aoj_1677_10276508", "code_snippet": "#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\n//#include<boost/multiprecision/cpp_int.hpp>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (long long i=0;i<(ll)n;i++)\n#define loop(i,m,n) for(long long i=m;i<=(ll)n;i++)\n//#define bbi boost::multiprecision::cpp_int\n#define vl vector<long long>\n#define vvl vector<vector<long long>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 4000000000000000000LL\n#define mod 998244353LL\n//#define mod 1000000007LL\n\n\n//グリッド問題等用\nvl fdx={1,1,-1,-1};\nvl fdy={1,-1,1,-1};\n\n//乱数、ファイル入出力\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\nll solve(){\n\tll a,b,x,y,n;\n\tcin>>a>>b>>x>>y>>n;\n\tif(a==0)return 1;\n\t//rupはy切片、x座標に対してyがどれだけ大きいか。\n\tunordered_map<ll,set<pair<ll,ll>>> rup;\n\t//lupは、x+yが0,0よりどれだけ大きいか。\n\tunordered_map<ll,set<pair<ll,ll>>> lup;\n\n\tset<ll> ans;\n\n\trep(i,n){\n\t\tll c,d;\n\t\tcin>>c>>d;\n\t\trup[d-c].insert({c,i+1});\n\t\tlup[c+d].insert({c,i+1});\n\t}\n\n\trep(i,4){\n\t\tll dx=fdx[i];\n\t\tll dy=fdy[i];\n\t\tll coinx=x,coiny=y;\n\t\tset<ll> tmp;\n\t\trep(j,a+b+1){\n\t\t\t\n\t\t\tif(dx==dy){\n\t\t\t\tif(rup[coiny-coinx].size()!=0){\n\t\t\t\t\tif(dx==-1){\n\t\t\t\t\t\tauto it=rup[coiny-coinx].rbegin();\n\t\t\t\t\t\twhile(it!=rup[coiny-coinx].rend()&&it->first>coinx)it++;\n\t\t\t\t\t\tif(it!=rup[coiny-coinx].rend()){\n\t\t\t\t\t\t\ttmp.insert(it->second);\n\t\t\t\t\t\t\tif(tmp.size()==1){\n\t\t\t\t\t\t\t\tans.insert(it->second);\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}else{\n\t\t\t\t\t\tauto it=rup[coiny-coinx].begin();\n\t\t\t\t\t\twhile(it!=rup[coiny-coinx].end()&&it->first<coinx)it++;\n\t\t\t\t\t\tif(it!=rup[coiny-coinx].end()){\n\t\t\t\t\t\t\ttmp.insert(it->second);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\tif(lup[coiny+coinx].size()!=0){\n\t\t\t\t\tif(dx==-1){\n\t\t\t\t\t\tauto it=lup[coiny+coinx].rbegin();\n\t\t\t\t\t\twhile(it!=lup[coiny+coinx].rend()&&it->first>coinx)it++;\n\t\t\t\t\t\tif(it!=lup[coiny+coinx].rend()){\n\t\t\t\t\t\t\ttmp.insert(it->second);\n\t\t\t\t\t\t}\n\t\t\t\t\t}else{\n\t\t\t\t\t\tauto it=lup[coiny+coinx].begin();\n\t\t\t\t\t\twhile(it!=lup[coiny+coinx].end()&&it->first<coinx)it++;\n\t\t\t\t\t\tif(it!=lup[coiny+coinx].end()){\n\t\t\t\t\t\t\ttmp.insert(it->second);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t//壁にぶつかるまでの移動距離を求める。\n\t\t\tll idoux,idouy;\n\t\t\tif(dx==-1)idoux=coinx;\n\t\t\telse idoux=a-coinx;\n\t\t\tif(dy==-1)idouy=coiny;\n\t\t\telse idouy=b-coiny;\n\t\n\t\t\tcoinx+=dx*min(idoux,idouy);\n\t\t\tcoiny+=dy*min(idoux,idouy);\n\t\n\t\t\tif(idoux==idouy)break;\n\t\t\telse if(idoux<idouy)dx*=-1;\n\t\t\telse dy*=-1;\n\n\t\t\tif(tmp.size()==2)break;\n\t\t}\n\t}\n\tif(ans.size()==0){\n\t\tcout<<\"No\"<<endl;\n\t}else{\n\t\tsetdbg(ans);\n\t}\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(!solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 122388, "score_of_the_acc": -0.6134, "final_rank": 4 }, { "submission_id": "aoj_1677_9861697", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing i64 = long long;\n\narray<i64, 3> exgcd(i64 a, i64 b) {\n\tif (b == 0) { return {a, 1, 0}; }\n\tauto [g, x, y] = exgcd(b, a % b);\n\treturn {g, y, x - a / b * y};\n}\n\nbool crt(i64 &m1, i64 &r1, i64 m2, i64 r2) {\n if (m2 > m1) swap(m1, m2), swap(r1, r2);\n auto [g, a, b] = exgcd(m1, m2);\n if ((r2 - r1) % g != 0) return false;\n m2 /= g; i64 D = (r2 - r1) / g % m2 * a % m2;\n r1 += (D < 0 ? D + m2 : D) * m1; m1 *= m2;\n assert(r1 >= 0 && r1 < m1);\n return true;\n}\n\nmt19937 rng(58);\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n\n i64 a, b, X, Y, n;\n\n auto solve = [&]() {\n vector<pair<i64, i64>> balls(n);\n // set<pair<i64, i64>> vis{{X, Y}};\n for (auto &[x, y] : balls) {\n cin >> x >> y;\n // x = rng() % (a - 1) + 1;\n // y = rng() % (b - 1) + 1;\n // while (vis.count({x, y})) {\n // x = rng() % (a - 1) + 1;\n // y = rng() % (b - 1) + 1;\n // }\n // vis.emplace(x, y);\n }\n\n vector<int> ans;\n\n for (int px : {1, -1}) {\n for (int py : {1, -1}) {\n i64 sx = px == 1 ? X : a + a - X;\n i64 sy = py == 1 ? Y : b + b - Y;\n i64 rest = -1;\n int resi = -1;\n for (int i = 0; i < n; i++) {\n i64 m1 = 2 * a;\n i64 m2 = 2 * b; \n i64 r1 = sx - balls[i].first;\n r1 = (r1 % m1 + m1) % m1;\n i64 r2 = sy - balls[i].second;\n r2 = (r2 % m2 + m2) % m2;\n if (!crt(m1, r1, m2, r2)) {\n continue;\n }\n i64 t = r1;\n if (rest == -1 || t < rest) {\n rest = t;\n resi = i;\n }\n }\n if (resi != -1) {\n vector<pair<i64, i64>> holes{{0, 0}, {a, 0}, {0, b}, {a, b}};\n for (int i = 0; i < n; i++) {\n if (i == resi) {\n continue;\n }\n auto [x, y] = balls[i];\n holes.emplace_back(x, y);\n holes.emplace_back(a + a - x, y);\n holes.emplace_back(a + a - x, b + b - y);\n holes.emplace_back(x, b + b - y);\n }\n for (auto [x, y] : holes) {\n i64 m1 = 2 * a;\n i64 m2 = 2 * b; \n i64 r1 = sx - x;\n r1 = (r1 % m1 + m1) % m1;\n i64 r2 = sy - y;\n r2 = (r2 % m2 + m2) % m2;\n if (!crt(m1, r1, m2, r2)) {\n continue;\n }\n i64 t = r1;\n if (t < rest) {\n resi = -1;\n break;\n }\n }\n }\n if (resi != -1) {\n ans.push_back(resi);\n }\n }\n }\n\n if (ans.empty()) {\n cout << \"No\\n\";\n } else {\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n for (int i = 0; i < ans.size(); i++) {\n cout << ans[i] + 1 << ' ';\n }\n cout << '\\n';\n }\n };\n\n // while (true) {\n // a = rng() % 20 + 5;\n // b = rng() % 20 + 5;\n // X = rng() % (a - 1) + 1;\n // Y = rng() % (b - 1) + 1;\n // n = rng() % 10 + 1;\n // solve();\n // }\n \n while (cin >> a >> b >> X >> Y >> n && a != 0) {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 20760, "score_of_the_acc": -0.0842, "final_rank": 2 }, { "submission_id": "aoj_1677_9510608", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define ll long long\n#define elif else if\n\n#define repname(a, b, c, d, e, ...) e\n#define rep(...) repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define rep0(x) for (int rep_counter = 0; rep_counter < (x); ++rep_counter)\n#define rep1(i, x) for (int i = 0; i < (x); ++i)\n#define rep2(i, l, r) for (int i = (l); i < (r); ++i)\n#define rep3(i, l, r, c) for (int i = (l); i < (r); i += (c))\n\n#define all(A) A.begin(),A.end()\n\nvoid solve(ll A,ll B,ll SX,ll SY,ll N){\n map<ll,vector<ll>> M1,M2;\n \n map<pair<ll,ll>,ll> BALL;\n rep(i,N){\n ll x,y;\n cin>>x>>y;\n M1[x+y].push_back(x);\n M2[x-y].push_back(x);\n BALL[{x,y}]=i;\n }\n for(auto &m:M1)sort(all(m.second));\n for(auto &m:M2)sort(all(m.second));\n set<ll> AN;\n\n vector<ll> dx={1,-1,-1,1},dy={1,1,-1,-1};\n\n rep(dir,4){\n ll d=dir;\n ll x=SX;\n ll y=SY;\n map<pair<pair<ll,ll>,ll>,ll> seen;\n bool OK=1;\n ll C=-1;\n while(OK){\n ll dt=-1;\n if(seen[{{x,y},d}]>1){\n OK=0;\n break;\n }\n seen[{{x,y},d}]++;\n if(d==0){\n rep(_,2){\n auto p=upper_bound(all(M2[x-y]),x);\n if(p!=M2[x-y].end()){\n ll bx=*p;\n ll by=bx-x+y;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n C=bid;\n x=bx;\n y=by;\n }\n }\n dt=min(A-x,B-y);\n }\n else if(d==1){\n rep(_,2){\n auto p=lower_bound(all(M1[x+y]),x);\n if(p!=M1[x+y].begin()){\n ll bx=*(prev(p));\n ll by=x+y-bx;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n C=bid;\n x=bx;\n y=by;\n }\n }\n dt=min(x,B-y);\n }\n else if(d==2){\n rep(_,2){\n auto p=lower_bound(all(M2[x-y]),x);\n if(p!=M2[x-y].begin()){\n ll bx=*(prev(p));\n ll by=bx-x+y;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n else{\n \n if(OK){\n AN.insert(bid);\n OK=0;\n }\n break;\n }\n \n }\n }\n dt=min(x,y);\n }\n else{\n rep(_,2){\n auto p=upper_bound(all(M1[x+y]),x);\n if(p!=M1[x+y].end()){\n ll bx=*p;\n ll by=x+y-bx;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n C=bid;\n x=bx;\n y=by;\n }\n }\n dt=min(A-x,y);\n }\n ll nx=-1,ny=-1;\n x=x+dx[d]*dt;\n y=y+dy[d]*dt;\n if(max(min(x,A-x),min(y,B-y))==0){\n OK=0;\n break;\n }\n if(x==0||x==A){\n if(d==0)d=1;\n else if(d==1)d=0;\n else if(d==2)d=3;\n else if(d==3)d=2;\n }\n else{\n if(d==0)d=3;\n else if(d==3)d=0;\n else if(d==2)d=1;\n else if(d==1)d=2;\n }\n }\n \n }\n if(AN.size()==0){\n cout<<\"No\"<<\"\\n\";\n }\n else{\n vector<ll> ANV;\n for(auto s: AN)ANV.push_back(s);\n ll an=ANV.size();\n rep(i,an)cout<<ANV[i]+1<<(an==i-1?\"\":\" \");\n cout<<endl;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n ll A,B,SX,SY,N;\n while(cin>>A>>B>>SX>>SY>>N){\n if(A+B+SX+SY+N==0)return 0;\n solve(A,B,SX,SY,N);\n }\n}", "accuracy": 1, "time_ms": 2720, "memory_kb": 162184, "score_of_the_acc": -1.6577, "final_rank": 13 }, { "submission_id": "aoj_1677_9510607", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define ll long long\n#define elif else if\n\n#define repname(a, b, c, d, e, ...) e\n#define rep(...) repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define rep0(x) for (int rep_counter = 0; rep_counter < (x); ++rep_counter)\n#define rep1(i, x) for (int i = 0; i < (x); ++i)\n#define rep2(i, l, r) for (int i = (l); i < (r); ++i)\n#define rep3(i, l, r, c) for (int i = (l); i < (r); i += (c))\n\n#define all(A) A.begin(),A.end()\n\nvoid solve(ll A,ll B,ll SX,ll SY,ll N){\n map<ll,vector<ll>> M1,M2;\n \n map<pair<ll,ll>,ll> BALL;\n rep(i,N){\n ll x,y;\n cin>>x>>y;\n M1[x+y].push_back(x);\n M2[x-y].push_back(x);\n BALL[{x,y}]=i;\n }\n for(auto &m:M1)sort(all(m.second));\n for(auto &m:M2)sort(all(m.second));\n set<ll> AN;\n\n vector<ll> dx={1,-1,-1,1},dy={1,1,-1,-1};\n\n rep(dir,4){\n ll d=dir;\n ll x=SX;\n ll y=SY;\n map<pair<pair<ll,ll>,ll>,ll> seen;\n bool OK=1;\n ll C=-1;\n while(OK){\n ll dt=-1;\n if(seen[{{x,y},d}]>1){\n OK=0;\n break;\n }\n seen[{{x,y},d}]++;\n if(d==0){\n rep(_,2){\n auto p=upper_bound(all(M2[x-y]),x);\n if(p!=M2[x-y].end()){\n ll bx=*p;\n ll by=bx-x+y;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n C=bid;\n x=bx;\n y=by;\n }\n }\n dt=min(A-x,B-y);\n }\n else if(d==1){\n rep(_,2){\n auto p=lower_bound(all(M1[x+y]),x);\n if(p!=M1[x+y].begin()){\n ll bx=*(prev(p));\n ll by=x+y-bx;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n C=bid;\n x=bx;\n y=by;\n }\n }\n dt=min(x,B-y);\n }\n else if(d==2){\n rep(_,2){\n auto p=lower_bound(all(M2[x-y]),x);\n if(p!=M2[x-y].begin()){\n ll bx=*(prev(p));\n ll by=bx-x+y;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n else{\n \n if(OK){\n AN.insert(bid);\n OK=0;\n }\n break;\n }\n \n }\n }\n dt=min(x,y);\n }\n else{\n rep(_,2){\n auto p=upper_bound(all(M1[x+y]),x);\n if(p!=M1[x+y].end()){\n ll bx=*p;\n ll by=x+y-bx;\n ll bid=BALL[{bx,by}];\n if(C!=-1&&C!=bid){\n OK=0;\n break;\n }\n C=bid;\n x=bx;\n y=by;\n }\n }\n dt=min(A-x,y);\n }\n ll nx=-1,ny=-1;\n x=x+dx[d]*dt;\n y=y+dy[d]*dt;\n if(max(min(x,A-x),min(y,B-y))==0){\n OK=0;\n break;\n }\n if(x==0||x==A){\n if(d==0)d=1;\n else if(d==1)d=0;\n else if(d==2)d=3;\n else if(d==3)d=2;\n }\n else{\n if(d==0)d=3;\n else if(d==3)d=0;\n else if(d==2)d=1;\n else if(d==1)d=2;\n }\n }\n \n }\n if(AN.size()==0){\n cout<<\"No\"<<endl;\n }\n else{\n vector<ll> ANV;\n for(auto s: AN)ANV.push_back(s);\n ll an=ANV.size();\n rep(i,an)cout<<ANV[i]+1<<(an==i-1?\"\":\" \");\n cout<<endl;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n ll A,B,SX,SY,N;\n while(cin>>A>>B>>SX>>SY>>N){\n if(A+B+SX+SY+N==0)return 0;\n solve(A,B,SX,SY,N);\n }\n}", "accuracy": 1, "time_ms": 2670, "memory_kb": 162236, "score_of_the_acc": -1.6353, "final_rank": 12 } ]
aoj_1674_cpp
Problem C Honeycomb Distance A plane is tiled completely with regular hexagons. Each hexagon is called a cell. You can move in one step from a cell to one of its adjacent cells that share an edge. You want to know the minimum number of steps to move from the central cell to the specified cell. Each cell is specified by a pair of integers. The central cell is (0, 0). One of the directions perpendicular to the edges of the regular hexagons is defined to be the rightward direction. For each cell ( x, y ), its right adjacent cell is ( x + 1, y ) and its upper-right adjacent cell is ( x, y + 1). See the figure below. Write a program that computes the minimum number of steps required to move from the cell (0, 0) to the cell ( x, y ) for given integers x and y . Figure C-1: How to specify the cells Input The first line of the input contains only one positive integer n, which is the number of datasets. n does not exceed 100. Each of the following n lines contains one dataset. Each dataset consists of two integers x and y , which satisfy −1000 ≤ x ≤ 1000 and −1000 ≤ y ≤ 1000. The destination cell is ( x, y ). Output For each dataset, output one line containing the minimum number of steps required to move from the cell (0, 0) to the destination cell. Sample Input 7 0 0 0 1 1 0 2 1 2 -1 -3 2 -1 -3 Output for the Sample Input 0 1 1 3 2 3 4
[ { "submission_id": "aoj_1674_10687657", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pint = pair<int,int>;\nusing pll = pair<long long, long long>;\n\n#define rrep1(a) for(ll i = (ll)(a-1); i >= (ll)0 ; i--)\n#define rrep2(i, a) for(ll i = (ll)(a-1); i >= (ll)0; i--)\n#define rrep3(i, a, b) for(ll i = (ll)(a-1); i >=(b); i--)\n#define rrep4(i, a, b, c) for(ll i = (ll)(a-1); i >=(b); i -= (c))\n#define overload4(a, b, c, d, e, ...) e\n#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)\n\n#define rep1(a) for(ll i = 0; i < (ll)(a); i++)\n#define rep2(i, a) for(ll i = 0; i < (ll)(a); i++)\n#define rep3(i, a, b) for(ll i = (a); i < (ll)(b); i++)\n#define rep4(i, a, b, c) for(ll i = (a); i < (ll)(b); i += (c))\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n\n#define fi first\n#define se second\n#define pb push_back\n#define spa \" \"\n\n//!?!?\n#define O print\n\ninline void scan(){}\ntemplate<class Head,class... Tail>\ninline void scan(Head&head,Tail&... tail){std::cin>>head;scan(tail...);}\n#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)\n\n#pragma GCC diagnostic ignored \"-Wunused-value\"\nvoid print(){cout << '\\n';}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(std::cout << ... << (cout << ' ', b));cout << '\\n';}\n#pragma GCC diagnostic warning \"-Wunused-value\"\n\n//int INF = 2147483647; // おおよそ2*10^9\nll inf = 1LL<<60; //おおよそ10^18\n//ull UINF == おおよそ1.8*10^19\n\nconst vector<ll> dx = {1,1,0,0,-1,-1};\nconst vector<ll> dy = {0,-1,1,-1,0,1};\n\nint main(){\n LL(n);\n vector<pll> goal(n);\n rep(n) cin >> goal[i].fi >> goal[i].se;\n const ll size = 2010;\n vector<vector<ll>> field(size,vector<ll>(size, inf));\n vector<vector<bool>> visited(size,vector<bool>(size,false));\n field[size/2][size/2] = 0;\n queue<tuple<ll,ll,ll>> q;\n q.push({size/2,size/2,0});\n while(!q.empty()){\n auto [x, y, cost] = q.front();\n q.pop();\n if(visited[x][y]) continue;\n visited[x][y] = true;\n field[x][y] = min(field[x][y], cost);\n rep(dx.size()){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(nx < 0 || nx >= size || ny < 0 || ny >= size) continue;\n if(visited[nx][ny]) continue;\n q.push({nx,ny,cost + 1});\n }\n }\n rep(n){\n int ni = goal[i].fi + size/2;\n int nj = goal[i].se + size/2;\n cout << field[ni][nj] << endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 36352, "score_of_the_acc": -0.0845, "final_rank": 8 }, { "submission_id": "aoj_1674_10681271", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int MAX=3000;\nconst int INF=1<<30;\nconst int dx[]={0,1,1,0,-1,-1},dy[]={1,0,-1,-1,0,1};\nint dist[2*MAX+1][2*MAX+1];\n\nvoid solve(){\n rep(i,0,2*MAX+1)rep(j,0,2*MAX+1)dist[i][j]=INF;\n dist[MAX][MAX]=0;\n queue<tuple<int,int,int>>q;\n q.push({dist[MAX][MAX],MAX,MAX});\n while(q.size()){\n auto[d,x,y]=q.front();\n q.pop();\n rep(i,0,6){\n int ex=x+dx[i],ey=y+dy[i];\n if(0<=ex&&ex<=2*MAX&&0<=ey&&ey<=2*MAX&&chmin(dist[ex][ey],d+1)){\n q.push({dist[ex][ey],ex,ey});\n }\n }\n }\n}\n\nint main() {\n solve();\n int N;\n cin>>N;\n vector<int>X(N),Y(N);\n rep(i,0,N){\n cin>>X[i]>>Y[i];\n cout<<dist[X[i]+MAX][Y[i]+MAX]<<endl;\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 144456, "score_of_the_acc": -0.5889, "final_rank": 15 }, { "submission_id": "aoj_1674_10665452", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\n#define all(v) v.begin(), v.end()\n#define rall(a) a.rbegin(), a.rend()\n#define fi first\n#define se second\n#define endl \"\\n\"\n\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\n\ntemplate <typename T>\nusing vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます\ntemplate <typename T>\nusing vv = vc<vc<T>>;\n#define int long long\nusing ll = long long;\nll INF = 2e18;\n\n#ifdef ONLINE_JUDGE\n// ジャッジ環境ではデバッグマクロを無効化\n#define debug_x(x) \\\n do \\\n { \\\n } while (0)\n#define debug_vc(v) \\\n do \\\n { \\\n } while (0)\n#define debug_vv(v) \\\n do \\\n { \\\n } while (0)\n#else\n// ローカルではデバッグ出力\n#define debug_x(x) cout << #x << \" = \" << (x) << endl\n#define debug_vc(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) cout << v[i] << ' '; \\\n cout << endl; \\\n } // 一次元配列を出力する\n#define debug_vv(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) \\\n { \\\n rep(j, size(v[i])) { cout << v[i][j] << ' '; } \\\n cout << endl; \\\n } \\\n } // 二次元配列を出力する\n#endif\n\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = 1; i <= (n); ++i)\n#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define nfor(i, s, n) for (ll i = s; i < n; i++) // i=s,s+1...n-1 ノーマルfor\n#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる\n\nusing ld = long double;\nusing bl = bool;\n\ntemplate <class T>\nusing pq = priority_queue<T, vc<T>>; // 大きい順\ntemplate <class T>\nusing pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順\n\nusing vl = vc<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vv<vl>;\nusing vvvvl = vv<vvl>;\nusing vs = vc<string>;\nusing vvs = vv<string>;\nusing vb = vc<bl>;\nusing vvb = vv<bl>;\nusing vvvb = vv<vb>;\nusing vld = vc<ld>;\nusing vvld = vv<ld>;\nusing vvvld = vv<vld>;\nusing P = pair<ll, ll>;\nusing vP = vc<P>;\nusing vvP = vc<vP>;\nusing vvvP = vv<vP>;\n\n#define pb push_back\n\n#define YES cout << \"Yes\" << endl\n#define NO cout << \"No\" << endl\n#define YN \\\n { \\\n cout << \"Yes\" << endl; \\\n } \\\n else \\\n { \\\n cout << \"No\" << endl; \\\n } // if(a==b)YN;\n#define dame cout << -1 << endl\n\nstring dir = \"DRUL\"; // 第4象限の場合\n\nbool out_grid(ll i, ll j, ll h, ll w)\n{ // trueならcontinue\n return (!(0 <= i && i < h && 0 <= j && j < w));\n}\n\nvl di = {1, 0, -1, 0, -1, 1}; // vl di={1,1,0,-1,-1,-1,0,1};\nvl dj = {0, 1, 0, -1, 1, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};\n\nvoid solve()\n{\n ll x, y;\n cin >> x >> y;\n\n vvl dist(2011,vl(2011,-1));\n dist[1005][1005] = 0;\n queue<P> que;\n que.push({1005,1005});\n while(!que.empty()){\n auto [i,j] = que.front();\n que.pop();\n rep(k,6){\n ll ni = i + di[k];\n ll nj = j + dj[k];\n if(ni<0||ni>2010||nj<0||nj>2010)continue;\n if(dist[ni][nj]==-1){\n dist[ni][nj] = dist[i][j] + 1;\n if(ni==x+1005&&nj==y+1005){\n que = {};\n break;\n }\n que.push({ni,nj});\n }\n }\n }\n cout << dist[x+1005][y+1005] << endl;\n\n return;\n}\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ll n;\n cin >> n;\n rep(i, n)\n {\n solve();\n }\n return 0;\n}\n\n/*\nshift+alt+Fで成形\nf2で変数名変更\n\nスニペット\nbasic\n m:atcoder以外用\n ma:ABC/ARC用\n mahc:AHC用\nmath\n floor:床関数\n ceil:天井関数\n comv:二項係数\n modpow: a^b mod m\n GCD:最大公約数\n extGCD:拡張ユークリッド互除法\n PFD:素因数分解\n isprime:エラトステネスの篩\n matrixpow:行列累乗\ndata_structure\n seg:セグメント木\ntechnic\n Compress:座標圧縮\n runlength:ランレングス圧縮\ngraph\n warshall:ワーシャルフロイド法\n dfs:深さ優先探索\n bfs:幅優先探索\n LCA:最近共通祖先\n*/", "accuracy": 1, "time_ms": 3460, "memory_kb": 34992, "score_of_the_acc": -0.6809, "final_rank": 17 }, { "submission_id": "aoj_1674_10649279", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,N) for(i=0;i<N;i++)\n#define ll long long\n\ntypedef struct _pos{\n ll x,y;\n}pos;\npos operator+(pos a,pos b){\n return {a.x+b.x,a.y+b.y};\n}\nbool operator<(const pos a,const pos b){\n if(a.x>b.x)return true;\n if(a.x==b.x){\n if(a.y>b.y)return true;\n }\n return false;\n}\npos pos_move[6]={{0,1},{1,0},{1,-1},{0,-1},{-1,0},{-1,1}};\n\nusing ans_type = ll;\n\nint main(void){\n vector<ans_type>ans;\n ll i,j,k;\n\n ll dist[2009][2009];\n ll add=1000;\n\n ll t,T;\n cin>>T;\n rep(t,T){\n pos p;\n cin>>p.x>>p.y;\n\n if(p.x==0 && p.y==0){\n ans.push_back(0);\n continue;\n }\n \n ans_type a;\n queue<pos>q;\n \n rep(i,2009)rep(j,2009)dist[i][j]=INT64_MAX;\n\n q.push({0,0});\n dist[0+add][0+add]=0;\n\n bool end=0;\n while(dist[p.x+add][p.y+add]==INT64_MAX){\n pos now=q.front();q.pop();\n // clog<<now.x<<\" \"<<now.y<<endl;\n rep(i,6){\n pos next=now+pos_move[i];\n if(abs(next.x)>1000)continue;\n if(abs(next.y)>1000)continue;\n if(dist[now.x+add][now.y+add]+1<dist[next.x+add][next.y+add]){\n dist[next.x+add][next.y+add]=dist[now.x+add][now.y+add]+1;\n q.push(next);\n }\n }\n }\n a=dist[p.x+add][p.y+add];\n ans.push_back(a);\n }\n\n rep(i,ans.size())cout<<ans[i]<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 3260, "memory_kb": 35072, "score_of_the_acc": -0.6454, "final_rank": 16 }, { "submission_id": "aoj_1674_10636220", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <cassert>\n\nnamespace atcoder {\n\n // Implement (union by size) + (path compression)\n // Reference:\n // Zvi Galil and Giuseppe F. Italiano,\n // Data structures and algorithms for disjoint set union problems\n struct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n return _leader(a);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n\n int _leader(int a) {\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = _leader(parent_or_size[a]);\n }\n };\n\n} // namespace atcoder\n\nusing namespace std;\nusing namespace atcoder;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n#define ALL(a) (a).begin(), (a).end()\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define foreach(i, n) for (auto i : (n))\n#define chmax(a, b) a = max(a, b)\n#define chmin(a, b) a = min(a, b)\n\ntemplate <typename K, typename V>\nusing umap = std::unordered_map<K, V>;\ntemplate <typename K>\nusing uset = std::unordered_set<K>;\n\nusing grid = vector<vector<char>>;\nusing graph = vector<vector<int>>;\nusing vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vector<vector<int>>>;\nusing vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>;\nusing vld = vector<ld>; using vvld = vector<vector<ld>>;\nusing vb = vector<bool>; using vvb = vector<vector<bool>>;\n\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n;\n cin >> n;\n vi x(n), y(n);\n rep(i, n) {\n cin >> x[i] >> y[i]; \n x[i] += 1000;\n y[i] += 1000;\n }\n int dx[6] = {1, 0, 1, 0, -1, -1};\n int dy[6] = {0, 1, -1, -1, 0, 1};\n vvi dist(2001, vi(2001, -1));\n dist[1000][1000] = 0;\n queue<pair<int, int>> q;\n q.push({ 1000,1000 });\n while (!q.empty()) {\n pair<int, int> d = q.front();\n q.pop();\n rep(i, 6) {\n int nx = d.first + dx[i];\n int ny = d.second + dy[i];\n if (nx < 0 || 2000 < nx) continue;\n if (ny < 0 || 2000 < ny) continue;\n if (dist[nx][ny] != -1) continue;\n dist[nx][ny] = dist[d.first][d.second] + 1;\n q.push({ nx, ny });\n }\n }\n rep(i, n) {\n cout << dist[x[i]][y[i]] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 19128, "score_of_the_acc": -0.0023, "final_rank": 3 }, { "submission_id": "aoj_1674_10608596", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int n;\n cin >> n;\n vector<vector<int>> s(2002,vector<int>(2002,1e9));\n queue<pair<int,int>> que;\n s[1000][1000] = 0;\n que.push({1000,1000});\n while(!que.empty()){\n auto [x,y] = que.front();\n que.pop();\n if(x+1<2002&&s[x][y]+1<s[x+1][y]){\n s[x+1][y] = s[x][y] + 1;\n que.push({x+1,y});\n }\n if(x-1>=0&&s[x][y]+1<s[x-1][y]){\n s[x-1][y] = s[x][y] + 1;\n que.push({x-1,y});\n }\n if(y+1<2002&&s[x][y]+1<s[x][y+1]){\n s[x][y+1] = s[x][y] + 1;\n que.push({x,y+1});\n }\n if(y-1>=0&&s[x][y]+1<s[x][y-1]){\n s[x][y-1] = s[x][y] + 1;\n que.push({x,y-1});\n }\n if(x+1<2002&&y-1>=0&&s[x][y]+1<s[x+1][y-1]){\n s[x+1][y-1] = s[x][y] + 1;\n que.push({x+1,y-1});\n }\n if(x-1>=0&&y+1<2002&&s[x][y]+1<s[x-1][y+1]){\n s[x-1][y+1] = s[x][y] + 1;\n que.push({x-1,y+1});\n }\n }\n while(n--){\n int x,y;\n cin >> x >> y;\n cout << s[x+1000][y+1000] << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 19008, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1674_10587203", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing lint = long long;\n\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n vector<int> dx6 = {1, 1, 0, -1, -1, 0};\n vector<int> dy6 = {-1, 0, 1, 1, 0, -1};\n map<pair<int, int>, int> dist;\n dist[{0, 0}] = 0;\n queue<pair<int, int>> q;\n q.emplace(0, 0);\n bool fin = false;\n while (!q.empty()) {\n auto [i, j] = q.front();\n q.pop();\n rep(k, 6) {\n int ny = i + dy6[k];\n int nx = j + dx6[k];\n if (!(-1000 <= ny and ny <= 1000 and -1000 <= nx and nx <= 1000)) continue;\n if (!dist.contains({ny, nx})) {\n dist[{ny, nx}] = dist[{i, j}] + 1;\n q.emplace(ny, nx);\n }\n }\n }\n\n int n;\n in(n);\n rep(i, n) {\n int x, y;\n in(x, y);\n out(dist[{y, x}]);\n }\n}", "accuracy": 1, "time_ms": 3670, "memory_kb": 253696, "score_of_the_acc": -1.6484, "final_rank": 20 }, { "submission_id": "aoj_1674_10587109", "code_snippet": "#include <bits/stdc++.h>\nconst int OFFSET = 1000;\nint D[2010][2010];\nbool in(int x, int y) {\n return -1000 <= x and x <= 1000 and -1000 <= y and y <= 1000;\n}\nvoid set(int x, int y, int v) {\n assert(-1000 <= x and x <= 1000);\n assert(-1000 <= y and y <= 1000);\n D[x + OFFSET][y + OFFSET] = v;\n}\nint get(int x, int y) {\n assert(-1000 <= x and x <= 1000);\n assert(-1000 <= y and y <= 1000);\n return D[x + OFFSET][y + OFFSET];\n}\nconst int dx[]{0,-1,-1,0,1,1}, dy[]{1,1,0,-1,-1,0};\nint main() {\n for (int i = -1000 ; i <= 1000 ; i++) for (int j = -1000 ; j <= 1000 ; j++) {\n set(i, j, -1);\n }\n set(0, 0, 0);\n std::queue<std::pair<int, int>> que;\n que.push({0, 0});\n while (que.size()) {\n const auto [x, y] = que.front();\n que.pop();\n const int dist = get(x, y);\n for (int d = 0 ; d < 6 ; d++) {\n const int nx = x + dx[d], ny = y + dy[d];\n if (in(nx, ny) and get(nx, ny) == -1) {\n set(nx, ny, dist + 1);\n que.push({nx, ny});\n }\n }\n }\n int N;\n std::cin >> N;\n while (N--) {\n int x, y;\n std::cin >> x >> y;\n std::cout << get(x, y) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 19248, "score_of_the_acc": -0.0046, "final_rank": 5 }, { "submission_id": "aoj_1674_10566325", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int INF = int(1e9);\n vector<vector<int>> dist(2001, vector<int>(2001,INF));\n using P = pair<int, int>;\n queue<P> que;\n que.push({1000,1000});\n dist[1000][1000] = 0;\n int dx[] = {1,0,-1,-1,0,1};\n int dy[] = {0,1,1,0,-1,-1};\n\n while(que.size()) {\n auto [x,y] = que.front(); que.pop();\n for (int k=0; k<6; k++) {\n int nx = x+dx[k], ny = y+dy[k];\n if (nx<0 || 2001<=nx || ny<0 || 2001<=ny) continue;\n if (dist[x][y]+1 < dist[nx][ny]) {\n dist[nx][ny] = dist[x][y] + 1;\n que.push({nx, ny});\n }\n }\n }\n \n\n int N;\n cin >> N;\n while(N--) {\n int x, y;\n cin >> x >> y;\n x+=1000; y+=1000;\n cout << dist[x][y] << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 19328, "score_of_the_acc": -0.0032, "final_rank": 4 }, { "submission_id": "aoj_1674_10490183", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,1,1,0,-1,-1};\nvector<ll> _yo = {1,0,-1,-1,0,1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n\t\nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nvoid solve(ll x,ll y){\n\n}\n\nint main(){\n\tios::sync_with_stdio(false);cin.tie(nullptr);\n\n\tmap<pll,ll> mp;\n\tqueue<pll> que;\n\tmp[{0,0}] = 0;\n\tque.push({0,0});\n\twhile(!que.empty()){\n\t\tauto[nowy,nowx] = que.front();\n\t\tque.pop();\n\t\tif(abs(nowy) >1000 or abs(nowx)> 1000)continue;\n\t\trep(p,6){\n\t\t\tll ny = nowy + _ta[p];\n\t\t\tll nx = nowx + _yo[p];\n\t\t\tif(!mp.contains({ny,nx})){\n\t\t\t mp[{ny,nx}]= mp[{nowy,nowx}]+1;\n\t\t\t\tque.push({ny,nx});\n\t\t\t}\n\t\t}\n\n\t}\n\tLL(n);\n\trep(i,n){\n\t\tLL(x,y);\n\t\tcout << mp[{y,x}] << endl;\n\t}\n}", "accuracy": 1, "time_ms": 3300, "memory_kb": 254208, "score_of_the_acc": -1.5842, "final_rank": 19 }, { "submission_id": "aoj_1674_10490148", "code_snippet": "#include <iostream>\n#include <utility>\n#include <queue>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 1000001000;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\nint dx[6] = {1,0,-1,-1,0,1};\nint dy[6] = {0,1,+1,0,-1,-1};\n\nbool isin(int x, int y) {\n return -1000 <= x && x <= 1000 && -1000 <= y && y <= 1000;\n}\n\nvoid solve() {\n int x, y; cin >> x >> y;\n\n const int OFFSET = 1000;\n vector<vector<int>>dist(2001, vector<int>(2001,INTINF));\n dist[OFFSET+0][OFFSET+0] = 0;\n queue<pair<int,int>>que;\n que.push({0,0});\n while (!que.empty()) {\n pair<int,int> fr = que.front(); que.pop();\n if (fr.first == x && fr.second == y) break;\n for (int k=0; k<6; k++) {\n int nx = fr.first + dx[k];\n int ny = fr.second + dy[k];\n if (isin(nx,ny) && chmin(dist[OFFSET+nx][OFFSET+ny], dist[OFFSET+fr.first][OFFSET+fr.second]+1)) {\n que.push(make_pair(nx,ny));\n }\n }\n }\n\n cout << dist[OFFSET+x][OFFSET+y] << endl;\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n int n; cin >> n;\n for (int i=0; i<n; i++) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2390, "memory_kb": 19232, "score_of_the_acc": -0.4221, "final_rank": 14 }, { "submission_id": "aoj_1674_9861321", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing i64 = long long;\n\nconstexpr int V = 1000;\nint dis[2 * V + 1][2 * V + 1];\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n\n memset(dis, -1, sizeof(dis));\n vector<pair<int, int>> q{{0, 0}};\n dis[V][V] = 0;\n\n for (int i = 0; i < q.size(); i++) {\n auto [x, y] = q[i];\n for (auto [dx, dy] : {pair(1, 0), {0, 1}, {-1, 1}, {-1, 0}, {0, -1}, {1, -1}}) {\n int nx = x + dx, ny = y + dy;\n if (abs(nx) <= 1000 && abs(ny) <= 1000 && dis[nx + V][ny + V] == -1) {\n dis[nx + V][ny + V] = dis[x + V][y + V] + 1;\n q.emplace_back(nx, ny);\n }\n }\n }\n\n int t;\n cin >> t;\n\n auto solve = [&]() {\n int x, y;\n cin >> x >> y;\n cout << dis[x + V][y + V] << '\\n';\n };\n\n while (t--) {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 53396, "score_of_the_acc": -0.1462, "final_rank": 11 }, { "submission_id": "aoj_1674_9716514", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int C = 1100;\nconst int INF = 1e9;\nconst vector<int> dx = {0, 1, 1, 0, -1, -1};\nconst vector<int> dy = {1, 0, -1, -1, 0, 1};\nint main() {\n int MAX = 2100;\n int zero = 1010;\n vector<vector<int>> dist(MAX, vector<int> (MAX, INF));\n dist[zero][zero] = 0;\n queue<pair<int, int>> Q;\n Q.emplace(zero, zero);\n while (!Q.empty()) {\n auto [tx, ty] = Q.front();\n Q.pop();\n for (int i = 0; i < 6; i++) {\n int nx = tx + dx[i], ny = ty + dy[i];\n if (0 <= nx && nx <= zero * 2 && 0 <= ny && ny <= zero * 2 && dist[nx][ny] == INF) {\n dist[nx][ny] = dist[tx][ty] + 1;\n Q.emplace(nx, ny);\n }\n }\n }\n int n;\n cin >> n;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y;\n cout << dist[zero + x][zero + y] << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 20688, "score_of_the_acc": -0.0125, "final_rank": 6 }, { "submission_id": "aoj_1674_9695847", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, m, n) for (auto i = (m); i < (n); i++)\n#define rept(i, n) rep(i, 0, n)\n#define repr(i, m, n) for (auto i = (m); i-- > (n);)\n#define reptr(i, n) repr(i, 0, n)\n#define all(x) begin(x), end(x)\n#define sz(x) (int)size(x)\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vll = vector<ll>;\nusing vc = vector<char>;\nusing pii = pair<int, int>;\n\nuint dist[2001][2001];\nconst int dx[] = {1, 0, -1, -1, 0, 1};\nconst int dy[] = {0, 1, 1, 0, -1, -1};\npii que[2001 * 2001], *p = que, *q = que;\n\nvoid solve() {\n\tmemset(dist, -1, sizeof(dist));\n\tdist[1000][1000] = 0;\n\t*q++ = pii(1000, 1000);\n\twhile (p != q) {\n\t\tauto [x, y] = *p++;\n\t\trept(i, 6) {\n\t\t\tuint x2 = x + dx[i], y2 = y + dy[i];\n\t\t\tif (x2 < 2001 and y2 < 2001 and dist[x2][y2] > dist[x][y] + 1) {\n\t\t\t\tdist[x2][y2] = dist[x][y] + 1;\n\t\t\t\t*q++ = pii(x2, y2);\n\t\t\t}\n\t\t}\n\t}\n\n\tint n; scanf(\"%d\", &n);\n\twhile (n--) {\n\t\tint x, y; scanf(\"%d%d\", &x, &y);\n\t\tprintf(\"%d\\n\", dist[x+1000][y+1000]);\n\t}\n}\n\nint main() {\n\tcin.tie(0)->sync_with_stdio(0);\n\tint n = 1;\n\t// scanf(\"%d\", &n);\n\twhile (n--) solve();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 50500, "score_of_the_acc": -0.1339, "final_rank": 10 }, { "submission_id": "aoj_1674_9594057", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int di[6]{1, 1, 0, 0, -1, -1};\nconst int dj[6]{0, -1, -1, 1, 0, 1};\nint ans[2007][2007];\n\nvoid bfs() {\n queue<tuple<int, int, int>> q;\n q.emplace(1000, 1000, 0);\n memset(ans, -1, sizeof ans);\n ans[1000][1000] = 0;\n\n while (!q.empty()) {\n const auto [i, j, dist] = q.front();\n q.pop();\n\n for (int d = 0; d < 6; d++) {\n const int i2 = i + di[d];\n const int j2 = j + dj[d];\n\n if (i2 > 2000 || j2 > 2000) continue;\n if (i2 < 0 || j2 < 0) continue;\n if (ans[i2][j2] != -1) continue;\n\n ans[i2][j2] = dist + 1;\n q.emplace(i2, j2, dist + 1);\n }\n }\n}\n\nint main() {\n int t;\n cin >> t;\n\n bfs();\n\n while (t--) {\n int i, j;\n cin >> i >> j;\n i += 1000;\n j += 1000;\n cout << ans[i][j] << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 19240, "score_of_the_acc": -0.001, "final_rank": 2 }, { "submission_id": "aoj_1674_9503625", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint tx,ty;\nint dx[6]={1,0,-1,-1,0,1},dy[6]={0,1,1,0,-1,-1};\nvoid solve(){\n cin>>tx>>ty;\n vector dist(2<<10,vector<int>(2<<10,-1));\n int x0=1001,y0=1001;\n dist[x0][y0]=0;\n queue<pair<int,int>>que;\n que.push({x0,y0});\n while(!que.empty()){\n auto[x,y]=que.front();\n que.pop();\n for(int dir=0;dir<6;++dir){\n int xx=x+dx[dir],yy=y+dy[dir];\n if(!(0<=xx&&xx<=2*x0&&0<=yy&&yy<=2*y0))continue;\n if(dist[xx][yy]==-1){\n dist[xx][yy]=dist[x][y]+1;\n que.push({xx,yy});\n }\n }\n }\n cout<<dist[x0+tx][y0+ty]<<'\\n';\n}\nint main(){\n cin.tie(0)->sync_with_stdio(0);\n int n;cin>>n;\n while(n--)solve();\n}", "accuracy": 1, "time_ms": 5620, "memory_kb": 19820, "score_of_the_acc": -1.0035, "final_rank": 18 }, { "submission_id": "aoj_1674_9491640", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> P;\ntypedef vector<P> VP;\ntypedef vector<ll> VI;\ntypedef vector<VI> VVI;\n#define REP(i,n) for(ll i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n#define OUT(n) cout << n << \"\\n\"\n//constexpr ll MOD=998244353;\n//constexpr ll MOD=1000000007;\n//constexpr ll INF=2e18;\n\n\nint main() {\n ll N;\n cin >> N;\n\n VVI xy(2010, VI(2010, -1));\n\n xy[1000][1000] = 0;\n\n queue<int> q;\n q.push(10001000);\n int dx[] = {1, 0, -1, 0, -1, 1};\n int dy[] = {0, 1, 0, -1, 1, -1};\n while(!q.empty()){\n int x = q.front() / 10000;\n int y = q.front() % 10000;\n q.pop();\n REP(i, 6){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(nx < 0 || nx > 2000 || ny < 0 || ny > 2000) continue;\n if(xy[nx][ny] != -1) continue;\n xy[nx][ny] = xy[x][y] + 1;\n q.push(nx * 10000 + ny);\n }\n }\n\n REP(i, N){\n ll x, y;\n cin >> x >> y;\n OUT(xy[x + 1000][y + 1000]);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 35036, "score_of_the_acc": -0.0717, "final_rank": 7 }, { "submission_id": "aoj_1674_9491548", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF=9e18;\n\nint main() {\n ll N;\n cin>>N;\n vector<ll> x (N);\n vector<ll> y (N);\n\n for (ll a = 0; a < N; a++){\n cin>>x[a]>>y[a];\n }\n vector<vector<ll>> hai (2100,vector<ll>(2100,-1));\n hai[1000][1000]=0;\n queue<pair<ll,ll>>q;\n q.push({1000,1000});\n vector<ll> dx={0,1,0,-1,-1,1};\n vector<ll> dy={1,0,-1,0,1,-1};\n\n while(1){\n if(q.empty()){\n break;\n }\n ll nowx=q.front().first;\n ll nowy=q.front().second;\n ll kyori=hai[nowx][nowy];\n q.pop();\n for (ll a = 0; a < 6; a++){\n ll nextx=nowx+dx[a];\n ll nexty=nowy+dy[a];\n if(0<=nextx&&nextx<=2000&&0<=nexty&&nexty<=2000&&hai[nextx][nexty]==-1){\n hai[nextx][nexty]=kyori+1;\n q.push({nextx,nexty});\n }\n }\n\n }\n for (ll a = 0; a < N; a++){\n ll xxx=x[a]+1000;\n ll yyy=y[a]+1000;\n cout<<hai[xxx][yyy]<<endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 37984, "score_of_the_acc": -0.0861, "final_rank": 9 }, { "submission_id": "aoj_1674_9491510", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\nusing Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[6]={0,1,0,-1,1,-1}, dw[6]={1,0,-1,0,-1,1};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\nlong double EPS = 1e-6;\nlong double PI = acos(-1);\nconst ll INF=(1LL<<62);\nconst int MAX=(1<<30);\n//constexpr ll MOD=1000000000+7;\nconstexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing pii=pair<int,int>;\nusing pil=pair<int,ll>;\nusing pli=pair<ll,int>;\nusing pll=pair<ll,ll>;\nusing psi=pair<string,int>;\nusing pis=pair<int,string>;\nusing psl=pair<string,ll>;\nusing pls=pair<ll,string>;\nusing pss=pair<string,string>;\nusing pII=pair<Int,Int>;\n\ntemplate<class T>\nusing minimum_queue=priority_queue<T,vector<T>,greater<T>>;\n\nusing Graph=vector<vector<int>>;\n\ntemplate<class T >\nstruct Edge {\n int from, to;\n T cost;\n Edge(int from,int to,T cost):from(from),to(to),cost(cost){}\n Edge()=default;\n\n bool operator<(const Edge<T> &e){\n return cost<e.cost;\n }\n bool operator<=(const Edge<T> &e){\n return cost<=e.cost;\n }\n};\n\n\n//https://kenkoooo.hatenablog.com/entry/2016/11/30/163533\n//__int128 出力\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n//__int128 入力\ninline istream &operator>>(istream &is,Int &x) {\n auto parse=[](string &s){\n __int128 ret = 0;\n for (size_t i = 0; i < s.length(); i++){\n if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0';\n }\n if(s.size() and s[0]=='-') ret*=-1;\n return ret;\n };\n string s;\n is>>s;\n x=parse(s);\n\treturn is;\n}\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\ntemplate<typename T>\nvoid CinGraph(int M,WeightGraph<T> &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n T cost;\n cin>>s>>t>>cost;\n if(index1) s--,t--;\n g[s].emplace_back(t,cost);\n if(not directed) g[t].emplace_back(s,cost);\n }\n}\n\nvoid CinGraph(int M,Graph &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n cin>>s>>t;\n if(index1) s--,t--;\n g[s].push_back(t);\n if(not directed) g[t].push_back(s);\n }\n}\n\n\n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<T> &v) {\n for(size_t i=0;i<v.size();i++) is>>v[i];\n\treturn is;\n}\n \n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n is>>v[i];\n }\n return is;\n}\n//vector cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<T> &v) {\n bool sp=true;\n if(string(typeid(T).name())==\"c\") sp=false;\n for(size_t i=0;i<v.size();i++){\n if(i and sp) os<<\" \";\n os<<v[i];\n }\n return os;\n}\n//vector<vector> cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n os<<v[i];\n if(i+1!=v.size()) os<<\"\\n\";\n }\n return os;\n}\n\n//Graph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const Graph &g) {\n for(size_t i=0;i<g.size();i++){\n for(int to:g[i]){\n os<<i<<\"->\"<<to<<\" \";\n }\n os<<endl;\n }\n return os;\n}\n\n//WeightGraph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const WeightGraph<T> &g) {\n for(size_t i=0;i<g.size();i++){\n for(auto e:g[i]){\n os<<i<<\"->\"<<e.to<<\"(\"<<e.cost<<\") \";\n }\n os<<endl;\n }\n return os;\n}\n\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\ntemplate<typename V,typename T>\nvoid fill(V &v,const T value){\n v=value;\n}\n\ntemplate<typename V,typename T>\nvoid fill(vector<V> &vec,const T value){\n for(auto &v:vec) fill(v,value);\n}\n\n//pair cout\ntemplate<typename T, typename U>\ninline ostream &operator<<(ostream &os,const pair<T,U> &p) {\n\tos<<p.first<<\" \"<<p.second;\n\treturn os;\n}\n \n//pair cin\ntemplate<typename T, typename U>\ninline istream &operator>>(istream &is,pair<T,U> &p) {\n\tis>>p.first>>p.second;\n\treturn is;\n}\n \n//ソート\ntemplate<typename T>\ninline void vsort(vector<T> &v){\n sort(v.begin(),v.end());\n}\n \n//逆順ソート\ntemplate<typename T>\ninline void rvsort(vector<T> &v){\n\tsort(v.rbegin(),v.rend());\n}\n\n//1ビットの数を返す\ninline int popcount(int x){\n\treturn __builtin_popcount(x);\n}\n//1ビットの数を返す\ninline int popcount(ll x){\n\treturn __builtin_popcountll(x);\n}\ntemplate<typename T>\ninline void Compress(vector<T> &C){\n sort(C.begin(),C.end());\n C.erase(unique(C.begin(),C.end()),C.end());\n}\ntemplate<typename T>\ninline int lower_idx(const vector<T> &C,T value){\n return lower_bound(C.begin(),C.end(),value)-C.begin();\n}\ntemplate<typename T>\ninline int upper_idx(const vector<T> &C,T value){\n return upper_bound(C.begin(),C.end(),value)-C.begin();\n}\n//時計回りに90度回転\ntemplate<typename T>\ninline void rotate90(vector<vector<T>> &C){\n vector<vector<T>> D(C[0].size(),vector<T>(C.size()));\n for(int h=0;h<C.size();h++){\n for(int w=0;w<C[h].size();w++){\n D[w][C.size()-1-h]=C[h][w];\n }\n }\n C=D;\n}\n//0indexを想定\nbool OutGrid(ll h,ll w,ll H,ll W){\n return (h>=H or w>=W or h<0 or w<0);\n}\n\nvoid NO(){\n cout<<\"NO\"<<\"\\n\";\n}\nvoid YES(){\n cout<<\"YES\"<<\"\\n\";\n}\nvoid No(){\n cout<<\"No\"<<\"\\n\";\n}\nvoid Yes(){\n cout<<\"Yes\"<<\"\\n\";\n}\nnamespace overflow{\n template<typename T>\n T max(){\n return numeric_limits<T>::max();\n }\n template<typename T>\n T ADD(T a,T b){\n T res;\n return __builtin_add_overflow(a,b,&res)?max<T>():res;\n }\n template<typename T>\n T MUL(T a,T b){\n T res;\n return __builtin_mul_overflow(a,b,&res)?max<T>():res;\n }\n};\nusing namespace overflow;\nstruct mint{\n using u64=uint_fast64_t;\n u64 a;\n constexpr mint() :a(0){}\n constexpr mint(ll x) :a((x>=0)?(x%MOD):(x%MOD+MOD) ) {}\n\n inline constexpr mint operator+(const mint rhs)const noexcept{\n return mint(*this)+=rhs;\n }\n inline constexpr mint operator-(const mint rhs)const noexcept{\n return mint(*this)-=rhs;\n }\n inline constexpr mint operator*(const mint rhs)const noexcept{\n return mint(*this)*=rhs;\n }\n inline constexpr mint operator/(const mint rhs)const noexcept{\n return mint(*this)/=rhs;\n }\n inline constexpr mint operator+(const ll rhs) const noexcept{\n return mint(*this)+=mint(rhs);\n }\n inline constexpr mint operator-(const ll rhs)const noexcept{\n return mint(*this)-=mint(rhs);\n }\n inline constexpr mint operator*(const ll rhs)const noexcept{\n return mint(*this)*=mint(rhs);\n }\n inline constexpr mint operator/(const ll rhs)const noexcept{\n return mint(*this)/=mint(rhs);\n }\n\n inline constexpr mint &operator+=(const mint rhs)noexcept{\n a+=rhs.a;\n if(a>=MOD) a-=MOD;\n return *this;\n }\n inline constexpr mint &operator-=(const mint rhs)noexcept{\n if(rhs.a>a) a+=MOD;\n a-=rhs.a;\n return *this;\n }\n inline constexpr mint &operator*=(const mint rhs)noexcept{\n a=(a*rhs.a)%MOD;\n return *this;\n }\n inline constexpr mint &operator/=(mint rhs)noexcept{\n a=(a*rhs.inverse().a)%MOD;\n return *this;\n }\n inline constexpr mint &operator+=(const ll rhs)noexcept{\n return *this+=mint(rhs);\n }\n inline constexpr mint &operator-=(const ll rhs)noexcept{\n return *this-=mint(rhs);\n }\n inline constexpr mint &operator*=(const ll rhs)noexcept{\n return *this*=mint(rhs);\n }\n inline constexpr mint &operator/=(const ll rhs)noexcept{\n return *this/=mint(rhs);\n }\n\n inline constexpr mint operator=(const ll x)noexcept{\n a=(x>=0)?(x%MOD):(x%MOD+MOD);\n return *this;\n }\n\n inline constexpr bool operator==(const mint p)const noexcept{\n return a==p.a;\n }\n\n inline constexpr bool operator!=(const mint p)const noexcept{\n return a!=p.a;\n }\n\n inline constexpr mint pow(ll N) const noexcept{\n mint ans(1LL),p(a);\n while(N>0){\n if(bitUP(N,0)){\n ans*=p;\n }\n p*=p;\n N>>=1;\n }\n return ans;\n }\n inline constexpr mint inverse() const noexcept{\n return pow(MOD-2);\n }\n\n};\ninline constexpr mint operator+(const ll &a,const mint &b)noexcept{\n return mint(a)+=b;\n}\ninline constexpr mint operator-(const ll &a,const mint &b)noexcept{\n return mint(a)-=b;\n}\ninline constexpr mint operator*(const ll &a,const mint &b)noexcept{\n return mint(a)*=b;\n}\ninline constexpr mint operator/(const ll &a,const mint &b)noexcept{\n return mint(a)/=b;\n}\n//cout\ninline ostream &operator<<(ostream &os,const mint &p) {\n return os<<p.a;\n}\n\n//cin\ninline istream &operator>>(istream &is,mint &p) {\n ll t;\n is>>t;\n p=t;\n return is;\n}\n\nstruct Binominal{\n vector<mint> fac,finv,inv; //fac[n]:n! finv:(n!)の逆元\n int sz;\n Binominal(int n=10) :sz(1){\n if(n<=0) n=10;\n init(n);\n }\n inline void init(int n){\n fac.resize(n+1,1);\n finv.resize(n+1,1);\n inv.resize(n+1,1);\n for(int i=sz+1;i<=n;i++){\n fac[i]=fac[i-1]*i;\n inv[i]=MOD-inv[MOD%i]*(MOD/i);\n finv[i]=finv[i-1]*inv[i];\n }\n sz=n;\n }\n //nCk(n,k<=N) をO(1)で求める\n inline mint com(int n,int k){\n if(n<k) return mint(0);\n if(n<0 || k<0) return mint(0);\n if(n>sz) init(n);\n return fac[n]*finv[k]*finv[n-k];\n }\n //nCk(k<=N) をO(k) で求める \n inline mint rcom(ll n,int k){\n if(n<0 || k<0 || n<k) return mint(0);\n if(k>sz) init(k);\n mint ret(1);\n for(int i=0;i<k;i++){\n ret*=n-i;\n }\n ret*=finv[k];\n return ret;\n }\n\n //重複組み合わせ n種類のものから重複を許してk個を選ぶ\n //〇がn個,|がk個\n inline mint h(int n,int k){\n return com(n+k-1,k);\n }\n //順列の公式\n inline mint P(int n,int k){\n if(n<k) return 0;\n if(n>sz) init(n);\n return fac[n]*finv[n-k];\n }\n\n};\nvector<int> Subset(int S,bool zero=false,bool full=false){\n vector<int> ret;\n int now=(S-1)&S;\n if(full and S){\n ret.push_back(S);\n }\n do{\n ret.push_back(now);\n now=(now-1)&S;\n }while(now!=0);\n if(zero){\n ret.push_back(0);\n }\n return ret;\n}\ntemplate<typename T>\nT SUM(const vector<T> &v,int s,int t){\n chmax(s,0);\n chmin(t,int(v.size())-1);\n if(s>t) return 0;\n if(s==0) return v[t];\n else return v[t]-v[s-1];\n}\ntemplate<typename T>\nvoid buildSUM(vector<T> &v){\n for(size_t i=1;i<v.size();i++){\n v[i]+=v[i-1];\n }\n return;\n}\n\ntemplate<class T>\ninline bool operator==(const vector<T> &v,const T x)noexcept{\n for(T a:v){\n if(a!=x) return false;\n }\n return true;\n}\ntemplate<class T>\ninline bool operator!=(const vector<T> &v,const T x)noexcept{\n return not (v==x);\n}\n\ntemplate<class T>\ninline void operator-=(vector<T> &v,const T x)noexcept{\n for(T &a:v){\n a-=x;\n }\n}\n\ntemplate<class T>\ninline void operator+=(vector<T> &v,const T x)noexcept{\n for(T &a:v){\n a+=x;\n }\n}\n\ntemplate<class T>\ninline T extract(queue<T> &que){\n assert(que.size());\n T x=que.front();\n que.pop();\n return x;\n}\n\ntemplate<class T>\ninline T extract(priority_queue<T> &pq){\n assert(pq.size());\n T x=pq.top();\n pq.pop();\n return x;\n}\n\ntemplate<class T>\ninline T extract(minimum_queue<T> &pq){\n assert(pq.size());\n T x=pq.top();\n pq.pop();\n return x;\n}\n\ntemplate<class T>\ninline T extract(stack<T> &st){\n assert(st.size());\n T x=st.top();\n st.pop();\n return x;\n}\n\ntemplate<class S,class T>\ninline void append(vector<S> &u,const vector<T> &v){\n for(T x:v){\n u.push_back(x);\n }\n}\ninline ll mask(int x){\n return (1LL<<x)-1;\n}\n\nint half=2500;\nauto dist=vmake(5000,5000,MAX);\n\n\nvoid solve(){\n queue<pii> que;\n dist[half][half]=0;\n que.push({half,half});\n\n while(true){\n pii now=extract(que);\n int h=now.first,w=now.second;\n for(int i=0;i<6;i++){\n int nh=h+dh[i],nw=w+dw[i];\n if(OutGrid(nh,nw,5000,5000)) continue;\n if(dist[nh][nw]!=MAX) continue;\n dist[nh][nw]=dist[h][w]+1;\n que.push({nh,nw});\n }\n if(que.empty()) break;\n }\n\n int N;\n cin>>N;\n while(N--){\n int x,y;\n cin>>x>>y;\n cout<<dist[x+half][y+half]<<\"\\n\";\n }\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 101360, "score_of_the_acc": -0.4218, "final_rank": 13 }, { "submission_id": "aoj_1674_9491507", "code_snippet": "#ifdef DEBUG\n#include\"stdlibrary.h\"\n#else\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n// #include<ext/pb_ds/assoc_container.hpp>\n// #include<ext/pb_ds/tree_policy.hpp>\n// #include<ext/pb_ds/tag_and_trait.hpp>\n// using namespace __gnu_pbds;\n// #include<boost/multiprecision/cpp_int.hpp>\n// namespace multiprecisioninteger = boost::multiprecision;\n// using cint=multiprecisioninteger::cpp_int;\nusing ll=long long;\nusing datas=std::pair<ll,ll>;\nusing ddatas=std::pair<long double,long double>;\nusing tdata=std::pair<ll,datas>;\nusing vec=std::vector<ll>;\nusing mat=std::vector<vec>;\nusing pvec=std::vector<datas>;\nusing pmat=std::vector<pvec>;\n// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;\n// #define For(i,a,b) if(const std::int64_t _iterate_from=a,_iterate_to=b;_iterate_from<_iterate_to)for(const std::int64_t i:std::views::iota(_iterate_from,_iterate_to))\n#define For(i,a,b) for(ll i=a;i<b;++i)\n#define bFor(i,b,a) for(ll i=b-1;i>=(ll)a;--i)\n#define rep(i,N) For(i,0,N)\n#define rep1(i,N) For(i,1,N)\n#define brep(i,N) bFor(i,N,0)\n#define brep1(i,N) bFor(i,N,1)\n#define all(v) std::begin(v),std::end(v)\n#define allr(v) std::rbegin(v),std::rend(v)\n#define vsort(v) std::sort(all(v))\n#define vrsort(v) std::sort(allr(v))\n#define uniq(v) vsort(v),(v).erase(std::unique(all(v)),std::end(v))\n#define endl \"\\n\"\n#define popcount __builtin_popcountll\n#define print(x) std::cout<<x<<endl\n#define printyes print(\"Yes\")\n#define printno print(\"No\")\n#define printYES print(\"YES\")\n#define printNO print(\"NO\")\n#define output(v) do{bool f=0;for(auto outi:v){std::cout<<(f?\" \":\"\")<<outi;f=1;}std::cout<<endl;}while(0)\n#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)\n// constexpr ll mod=1000000007;\nconstexpr ll mod=998244353;\nconstexpr ll inf=1LL<<60;\nconstexpr long double eps=1e-9;\nconst long double PI=acosl(-1);\ntemplate<class T,auto x=T::mod()> std::ostream& operator<<(std::ostream& os,const T& v){return os<<v.val();}\ntemplate<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v);\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v);\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v);\ntemplate<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v);\ntemplate<class T,class E> std::ostream& operator<<(std::ostream& os,const std::pair<T,E>& p){return os<<\"(\"<<p.first<<\",\"<<p.second<<\")\";}\ntemplate<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T> inline bool chmax(T& a,const T b){bool x=a<b;if(x)a=b;return x;}\ntemplate<class T> inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;}\n#ifdef DEBUG\nvoid debugg(){std::cout<<endl;}\ntemplate<class T,class... Args>void debugg(const T& x,const Args&... args){std::cout<<\" \"<<x;debugg(args...);}\n#define debug(...) cout<<__LINE__<<\" [\"<<#__VA_ARGS__<<\"]:\",debugg(__VA_ARGS__)\n#else\n#define debug(...) (void(0))\n#endif\n\ninline void startupcpp(void) noexcept{\n cin.tie(0)->sync_with_stdio(0);\n cout<<fixed<<setprecision(15);\n}\n\nll modinv(ll a,const ll m=mod) noexcept{\n ll b=m,u=1,v=0,t;\n while(b){\n t=a/b;\n a-=t*b; swap(a,b);\n u-=t*v; swap(u,v);\n }\n return (u+m)%m;\n}\n\nll moddevide(const ll a,const ll b) noexcept{return (a*modinv(b))%mod;}\n\nvec modncrlistp,modncrlistm;\n\nll modncr(const ll n,const ll r){\n if(n<r||r<0)return 0;\n ll i,size=modncrlistp.size();\n if(size<=n){\n modncrlistp.resize(n+1);\n modncrlistm.resize(n+1);\n if(!size){\n modncrlistp[0]=modncrlistm[0]=1;\n size++;\n }\n For(i,size,n+1)modncrlistp[i]=modncrlistp[i-1]*i%mod;\n modncrlistm[n]=modinv(modncrlistp[n]);\n for(i=n;i>size;--i)modncrlistm[i-1]=modncrlistm[i]*i%mod;\n }\n return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;\n}\n\nll modpow(ll a,ll n,const ll m=mod){\n if(n<0)return 0;\n ll res=1;\n while(n>0){\n if(n&1)res=res*a%m;\n a=a*a%m;\n n>>=1;\n }\n return res;\n}\n\nconstexpr ll gcd(const ll a,const ll b) noexcept{return (!b)?abs(a):(a%b==0)?abs(b):gcd(b,a%b);}\nconstexpr ll lcm(const ll a,const ll b) noexcept{return a/gcd(a,b)*b;}\n\n// #include<atcoder/all>\n// using mint=atcoder::modint998244353;\n// void operator>>(istream& is,mint& v){long long x;is>>x;v=x;}\n\nll N,M,K,H,W,Q,A,B,C,D;\nstring s,t;\nll ans;\nint dist[4000][4000];\nint main(){\n startupcpp();\n// int codeforces;cin>>codeforces;while(codeforces--){\n constexpr ll dx[6]={1,0,-1,-1,0,1},dy[6]={0,1,1,0,-1,-1};\n queue<tuple<ll,ll,ll>> que;\n que.emplace(1,2000,2000);\n while(!que.empty()){\n auto [d,x,y]=que.front();que.pop();\n if(dist[x][y]!=0)continue;\n dist[x][y]=d;\n rep(i,6){\n ll nx=x+dx[i],ny=y+dy[i];\n if(nx<0||nx>=4000||ny<0||ny>=4000)continue;\n if(dist[nx][ny]==0)que.emplace(d+1,nx,ny);\n }\n }\n cin>>N;\n while(N--){\n cin>>A>>B;\n print(dist[A+2000][B+2000]-1);\n }\n// }\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 66860, "score_of_the_acc": -0.2375, "final_rank": 12 } ]
aoj_1675_cpp
Problem D A Bug That's Not a Pill Bug A bug that looks like a pill bug is walking on a plane. A Cartesian coordinate system is defined on the plane, in which its x- axis is directed from west to east, while its y- axis is directed from south to north. The bug is initially at a lattice point (a point where both the x- and y- coordinates are integers), facing the positive direction of x. Obstacles are placed at some of the lattice points on the plane. The bug continues to move according to the following rules, avoiding obstacles. The bug attempts to move from its current position to the adjacent lattice point in the direction it is currently facing. If that adjacent lattice point has no obstacles, the bug moves there without changing its direction. If that adjacent lattice point has an obstacle, the bug turns 90 degrees to the left, without changing its position. The initial position of the bug, the locations of the obstacles, and a moving distance are given. You are requested to find the position of the bug after it has moved exactly the given distance. Some of the datasets in Sample Input are illustrated below. The points marked with an "X" indicate the presence of obstacles. The left figure corresponds to the first two datasets of Sample Input. In these datasets, the initial position of the bug is (0, 1), and it moves to (1, 1) and then to (2, 1). Next, it attempts to move to (3, 1), but since there is an obstacle there, it turns to the positive direction of y and moves to (2, 2). The points the bug traverses are shown with orange circles. The right figure shows the next two datasets. Figure D-1: The first two datasets of Sample Input (left) and the next two datasets (right) Input The input consists of multiple datasets, each in the following format. The number of datasets does not exceed 200. n a b d x 1 y 1 ⋮ x n y n Here, n represents the number of obstacles, which is an integer (1 ≤ n ≤ 1000). Both a and b are integers between 0 and 100, inclusive, and the initial position of the bug is at coordinates ( a, b ). Additionally, d represents the distance to move, which is an integer (1 ≤ d ≤ 10 18 ). The i -th obstacle is located at coordinates ( x i , y i ) ( i = 1, ⋯, n ). x i and y i are integers between 0 and 100, inclusive. No two obstacles are at the same location, that is, if i ≠ j , then ( x i , y i ) ≠ ( x j , y j ). Additionally, no obstacle is located at the initial position ( a, b ) of the bug. It is guaranteed that the bug can move distance d or more under the given configuration. The end of the input is indicated by a line consisting of a zero. Output For each dataset, output two integers separated by a space on a single line representing the x- and y- coordinates of the position of the bug after it has moved distance d. Note that the x- and y- coordinates of the position of the bug can be negative. Sample Input 2 0 1 4 3 1 2 5 2 0 1 9 3 1 2 5 12 2 2 11 0 1 0 2 0 3 4 1 4 2 4 3 3 4 2 4 1 4 3 0 2 0 1 0 12 2 2 7791772263873 0 1 0 2 0 ...(truncated)
[ { "submission_id": "aoj_1675_10681419", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\nconst int MAX=100;\nconst ll INF=1LL<<60;\n\nint main() {\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n int A,B;\n ll D;\n cin>>A>>B>>D;\n vector<int>X(N),Y(N);\n vector flag(MAX+1,vector<bool>(MAX+1));\n rep(i,0,N){\n cin>>X[i]>>Y[i];\n flag[X[i]][Y[i]]=true;\n }\n vector dist(MAX+1,vector(MAX+1,vector<ll>(4,INF)));\n int dir=0;\n dist[A][B][dir]=D;\n while(1){\n int a=A+dx[dir],b=B+dy[dir];\n if(a<0||a>MAX||b<0||b>MAX){\n cout<<A+D*dx[dir]<<\" \"<<B+D*dy[dir]<<endl;\n break;\n }\n if(flag[a][b]){\n dir=(dir+1)%4;\n }else{\n if(D<=100){\n A=a,B=b;\n D--;\n }else{\n if(dist[a][b][dir]==INF){\n A=a,B=b;\n D--;\n dist[a][b][dir]=D;\n }else{\n A=a,B=b;\n D--;\n D%=(dist[a][b][dir]-D);\n }\n }\n }\n if(D==0){\n cout<<A<<\" \"<<B<<endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4224, "score_of_the_acc": -0.0121, "final_rank": 9 }, { "submission_id": "aoj_1675_10672241", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include <atcoder/all>\n// using namespace atcoder;\nstruct Init {\n Init() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << setprecision(13);\n }\n} init;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing VLL = vector<ll>;\nusing VVLL = vector<VLL>;\nusing PLL = pair<ll, ll>;\nusing VS = vector<string>;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n#define FOR(i, a, b) for (int i = int(a); i < int(b); ++i)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) x.rbegin(), x.rend()\n#define el '\\n'\n#define spa \" \"\n#define Yes cout << \"Yes\" << el\n#define No cout << \"No\" << el\n#define eps (1e-10)\n#define Equals(a, b) (fabs((a) - (b)) < eps)\n#define debug(x) cerr << #x << \" = \" << x << el\n\nconst double pi = 3.141592653589793238;\nconst int inf = 1073741823;\nconst ll infl = 1LL << 60;\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++)\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (T& in : v)\n is >> in;\n return is;\n}\nvoid print() {\n cout << el;\n}\ntemplate <typename T>\nvoid print(const T& t) {\n cout << t << el;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head& head, const Tail&... tail) {\n cout << head << spa;\n print(tail...);\n}\ntemplate <typename T1, typename T2>\ninline bool chmax(T1& a, T2 b) {\n bool compare = a < b;\n if (compare)\n a = b;\n return compare;\n}\ntemplate <typename T1, typename T2>\ninline bool chmin(T1& a, T2 b) {\n bool compare = a > b;\n if (compare)\n a = b;\n return compare;\n}\nvoid YesNo(bool b) {\n cout << (b ? \"Yes\" : \"No\") << el;\n}\n\nint main() {\n while (true) {\n ll n, a, b, d;\n cin >> n;\n if (n == 0)\n return 0;\n cin >> a >> b >> d;\n bool block[101][101] = {};\n while (n--) {\n int x, y;\n cin >> x >> y;\n block[x][y] = true;\n }\n ll x = a, y = b, m = 0;\n map<pair<pair<int, int>, int>, ll> ma;\n while (d--) {\n // print(\"d=\", d, \"(x, y, m)=\", x, y, m);\n if (ma.count({{x, y}, m})) {\n ll c = (ma[{{x, y}, m}] - d);\n d = d % c;\n ma.clear();\n }\n ma[{{x, y}, m}] = d;\n ll nx = x + dx[m];\n ll ny = y + dy[m];\n if (!(0 <= nx && nx <= 100 && 0 <= ny && ny <= 100)) {\n x += (d + 1) * dx[m];\n y += (d + 1) * dy[m];\n break;\n }\n\n while (block[nx][ny]) {\n m = (m + 1) % 4;\n nx = x + dx[m];\n ny = y + dy[m];\n if (!(0 <= nx && nx <= 100 && 0 <= ny && ny <= 100)) {\n x += (d + 1) * dx[m];\n y += (d + 1) * dy[m];\n break;\n }\n }\n\n x = nx;\n y = ny;\n }\n print(x, y);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3880, "score_of_the_acc": -0.0035, "final_rank": 8 }, { "submission_id": "aoj_1675_10666096", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\n#define all(v) v.begin(), v.end()\n#define rall(a) a.rbegin(), a.rend()\n#define fi first\n#define se second\n#define endl \"\\n\"\n\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\n\ntemplate <typename T>\nusing vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます\ntemplate <typename T>\nusing vv = vc<vc<T>>;\n#define int long long\nusing ll = long long;\nll INF = 2e18;\n\n#ifdef ONLINE_JUDGE\n// ジャッジ環境ではデバッグマクロを無効化\n#define debug_x(x) \\\n do \\\n { \\\n } while (0)\n#define debug_vc(v) \\\n do \\\n { \\\n } while (0)\n#define debug_vv(v) \\\n do \\\n { \\\n } while (0)\n#else\n// ローカルではデバッグ出力\n#define debug_x(x) cout << #x << \" = \" << (x) << endl\n#define debug_vc(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) cout << v[i] << ' '; \\\n cout << endl; \\\n } // 一次元配列を出力する\n#define debug_vv(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) \\\n { \\\n rep(j, size(v[i])) { cout << v[i][j] << ' '; } \\\n cout << endl; \\\n } \\\n } // 二次元配列を出力する\n#endif\n\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = 1; i <= (n); ++i)\n#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define nfor(i, s, n) for (ll i = s; i < n; i++) // i=s,s+1...n-1 ノーマルfor\n#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる\n\nusing ld = long double;\nusing bl = bool;\n\ntemplate <class T>\nusing pq = priority_queue<T, vc<T>>; // 大きい順\ntemplate <class T>\nusing pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順\n\nusing vl = vc<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vv<vl>;\nusing vvvvl = vv<vvl>;\nusing vs = vc<string>;\nusing vvs = vv<string>;\nusing vb = vc<bl>;\nusing vvb = vv<bl>;\nusing vvvb = vv<vb>;\nusing vld = vc<ld>;\nusing vvld = vv<ld>;\nusing vvvld = vv<vld>;\nusing P = pair<ll, ll>;\nusing vP = vc<P>;\nusing vvP = vc<vP>;\nusing vvvP = vv<vP>;\n\n#define pb push_back\n\n#define YES cout << \"Yes\" << endl\n#define NO cout << \"No\" << endl\n#define YN \\\n { \\\n cout << \"Yes\" << endl; \\\n } \\\n else \\\n { \\\n cout << \"No\" << endl; \\\n } // if(a==b)YN;\n#define dame cout << -1 << endl\n\nstring dir = \"DRUL\"; // 第4象限の場合\nvl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};\nvl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};\n\nbool out_grid(ll i, ll j, ll h, ll w)\n{ // trueならcontinue\n return (!(0 <= i && i < h && 0 <= j && j < w));\n}\n\nll n;\n\nvoid solve()\n{\n ll a, b, d;\n cin >> a >> b >> d;\n vl x(n), y(n);\n vvb isBlock(101, vb(101));\n ll ans_i = -1, ans_j = -1;\n rep(i, n)\n {\n cin >> x[i] >> y[i];\n isBlock[x[i]][y[i]] = true;\n }\n vvvl dist(101, vvl(101, vl(4, -1)));\n dist[a][b][0] = 0;\n queue<pair<P, ll>> que;\n que.push({{a, b}, 0});\n if(d==0){\n ans_i = a;\n ans_j = b;\n que = {};\n }\n while (!que.empty())\n {\n auto [co, cur_dir] = que.front();\n auto [i, j] = co;\n que.pop();\n ll ni = i + di[cur_dir];\n ll nj = j + dj[cur_dir];\n if (out_grid(ni, nj, 101, 101))\n {\n // ループしないで場外へ\n // cout << \"ループしない\" << endl;\n ll rest = d - dist[i][j][cur_dir];\n ans_i = i + rest * di[cur_dir];\n ans_j = j + rest * dj[cur_dir];\n }\n else\n {\n if (isBlock[ni][nj])\n {\n ll nex_dir = (cur_dir + 1) % 4;\n if (dist[i][j][nex_dir] == -1)\n {\n dist[i][j][nex_dir] = dist[i][j][cur_dir];\n if(dist[i][j][nex_dir] == d){\n ans_i = i;\n ans_j = j;\n break;\n }\n else{\n que.push({co, nex_dir});\n }\n }\n else\n {\n // cout << \"ループする(方向転換)\" << endl;\n // ループする\n ll span = dist[i][j][cur_dir] - dist[i][j][nex_dir];\n ll rest = d - dist[i][j][cur_dir];\n ll amari = rest % span;\n rep(ii, 101)\n {\n rep(jj, 101)\n {\n rep(k, 4)\n {\n if ((dist[ii][jj][k] - dist[i][j][nex_dir])>=0&&(dist[ii][jj][k] - dist[i][j][nex_dir]) % span == amari)\n {\n ans_i = ii;\n ans_j = jj;\n }\n }\n }\n }\n break;\n }\n }\n else\n {\n if (dist[ni][nj][cur_dir] == -1)\n {\n dist[ni][nj][cur_dir] = dist[i][j][cur_dir] + 1;\n if(dist[ni][nj][cur_dir] == d){\n ans_i = ni;\n ans_j = nj;\n }\n else{\n que.push({{ni, nj}, cur_dir});\n }\n }\n else\n {\n // ループする\n // cout << \"ループする(移動)\" << endl;\n ll span = dist[i][j][cur_dir] - dist[ni][nj][cur_dir] + 1;\n ll rest = d - dist[i][j][cur_dir];\n ll amari = rest % span;\n rep(ii, 101)\n {\n rep(jj, 101)\n {\n rep(k, 4)\n {\n if ((dist[ii][jj][k] - dist[ni][nj][cur_dir])>=0&&(dist[ii][jj][k] - dist[ni][nj][cur_dir] + 1) % span == amari)\n {\n ans_i = ii;\n ans_j = jj;\n }\n }\n }\n }\n break;\n }\n }\n }\n }\n cout << ans_i << ' ' << ans_j << endl;\n return;\n}\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true)\n {\n cin >> n;\n if (n == 0)\n break;\n solve();\n }\n return 0;\n}\n\n/*\nshift+alt+Fで成形\nf2で変数名変更\n\nスニペット\nbasic\n m:atcoder以外用\n ma:ABC/ARC用\n mahc:AHC用\nmath\n floor:床関数\n ceil:天井関数\n comv:二項係数\n modpow: a^b mod m\n GCD:最大公約数\n extGCD:拡張ユークリッド互除法\n PFD:素因数分解\n isprime:エラトステネスの篩\n matrixpow:行列累乗\ndata_structure\n seg:セグメント木\ntechnic\n Compress:座標圧縮\n runlength:ランレングス圧縮\ngraph\n warshall:ワーシャルフロイド法\n dfs:深さ優先探索\n bfs:幅優先探索\n LCA:最近共通祖先\n*/", "accuracy": 1, "time_ms": 60, "memory_kb": 4240, "score_of_the_acc": -0.0153, "final_rank": 15 }, { "submission_id": "aoj_1675_10659404", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<n;i++)\nusing ll = long long;\nusing ull = unsigned long long;\n\nbool solve(){\n int n;\n cin >> n;\n if(n == 0) return false;\n\n int a,b;\n ll d;\n cin >> a >> b >> d;\n\n vector<vector<bool>> block(101,vector<bool>(101,false));\n rep(i,n){\n int x,y;\n cin >> x >> y;\n block[x][y] = true;\n }\n\n\n int dx[] = {1,0,-1,0};\n int dy[] = {0,1,0,-1};\n ll cx = a;\n ll cy = b;\n vector<vector<vector<ll>>> seen(101,vector<vector<ll>>(101,vector<ll>(4,-1)));\n bool r = false;\n int dir = 0;\n while(d){\n if(\n 0 <= cx+dx[dir] && cx+dx[dir] <= 100 &&\n 0 <= cy+dy[dir] && cy+dy[dir] <= 100 &&\n block[cx+dx[dir]][cy+dy[dir]]\n ){\n dir++;\n dir %= 4;\n // cout << \"?\";\n }else{\n cx += dx[dir];\n cy += dy[dir];\n d--;\n if(\n cx + dx[dir] < 0 ||\n 100 < cx + dx[dir] ||\n cy + dy[dir] < 0 ||\n 100 < cy + dy[dir]\n ){\n cx += dx[dir] * d;\n cy += dy[dir] * d;\n break;\n }\n // cout << \"a\";\n }\n ll dd = seen[cx][cy][dir];\n if(dd != -1 && !r){\n ll ddd = dd - d;\n d %= ddd;\n r = true;\n // cout << \"!\";\n }\n seen[cx][cy][dir] = d;\n // cout << cx << \":\" << cy << endl;\n }\n\n cout << cx << \" \" << cy << endl;\n\n return true;\n}\n\nint main(){\n while(solve()){\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4096, "score_of_the_acc": -0.0151, "final_rank": 13 }, { "submission_id": "aoj_1675_10650061", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,N) for(i=0;i<N;i++)\n#define ll long long\n\ntypedef struct _pos{\n ll x,y;\n}pos;\npos operator+(pos a,pos b){\n return {a.x+b.x,a.y+b.y};\n}\npos pos_move[4]={{1,0},{0,1},{-1,0},{0,-1}};\n\nbool is_out(pos next){\n return (next.x<0 || 100<next.x || next.y<0 || 100<next.y);\n}\n\nint main(void){\n ll i,j,k;\n\n ll N;\n ll A,B,D;\n\n for(;;){\n cin>>N;\n if(N==0)break;\n cin>>A>>B>>D;\n\n bool wall[109][109]={0};\n ll vis[109][109][4];\n rep(i,109)rep(j,109)rep(k,4)vis[i][j][k]=-1;\n\n rep(i,N){\n ll x,y;\n cin>>x>>y;\n wall[x][y]=1;\n }\n\n pos now={A,B};\n ll d=0;\n vis[A][B][d]=0;\n while(D!=0){\n D--;\n pos next=now+pos_move[d];\n\n ll now_vis=vis[now.x][now.y][d];\n while(!is_out(next)){\n if(!wall[next.x][next.y])break;\n d=(d+1)%4;\n next=now+pos_move[d];\n }\n if(is_out(next)){\n next.x+=pos_move[d].x*D;\n next.y+=pos_move[d].y*D;\n now=next;\n break;\n }\n\n if(vis[next.x][next.y][d]!=-1){\n ll roop=now_vis+1-vis[next.x][next.y][d];\n // clog<<\"roop:\"<<roop<<\" (\"<<next.x<<\",\"<<next.y<<\")\"<<endl;\n D%=roop;\n }\n vis[next.x][next.y][d]=now_vis+1;\n now=next;\n }\n\n cout<<now.x<<\" \"<<now.y<<endl;\n if(now.x==-3 && now.y==72){\n clog<<N<<\" \"<<A<<\" \"<<B<<endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3588, "score_of_the_acc": 0, "final_rank": 2 }, { "submission_id": "aoj_1675_10610109", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int n;\n vector<pair<long long,long long>> lastans;\n while(true){\n cin >> n;\n if(n == 0) break;\n int a,b;\n long long d;\n cin >> a >> b >> d;\n map<pair<int,int>,bool> block;\n for(int i = 0; i < n; i++) {\n int x,y;\n cin >> x >> y;\n block[{x,y}] = true;\n }\n vector<pair<pair<long long,long long>,int>> path;\n map<vector<long long>,bool> visited;\n long long x =a, y = b, z=0,count=0;\n bool nx=false, ny=false,px=false,py=false,flag=false,flag2=false;\n vector<pair<int,int>> dir = {{1,0},{0,1},{-1,0},{0,-1}};\n while(true){\n if(count == d) {\n lastans.push_back({x,y});\n break;\n }\n if(visited[{x,y,z}]){\n flag = true;\n break;\n }\n if(x>100&&z!=2){\n flag2=true;\n break;\n }\n if(y>100&&z!=3){\n flag2=true;\n break;\n }\n if(x<0&&z!=0){\n flag2 = true;\n break;\n }\n if(y<0&&z!=1){\n flag2 = true;\n break;\n }\n visited[{x,y,z}] = true;\n path.push_back({{x,y},z});\n count++;\n for(int i=0;i<4;i++){\n int dx = dir[z].first, dy = dir[z].second;\n if(!block[{x+dx,y+dy}]) {\n x += dx;\n y += dy;\n break;\n }\n z ++;\n z %= 4;\n }\n }\n if(flag2){\n int dx= dir[z].first, dy = dir[z].second;\n lastans.push_back({x+dx*(d-count),y+dy*(d-count)});\n }else if(flag){\n long long index = 0;\n for(int i=0;i<count;i++){\n pair<long long,long long> p=make_pair(x,y);\n if(path[i].first==p&&path[i].second==z){\n break;\n }\n index++;\n }\n d-= index;\n d%=(count-index);\n lastans.push_back(path[index+d].first);\n }\n } \n for(int i = 0; i < lastans.size(); i++) {\n cout << lastans[i].first <<\" \"<< lastans[i].second<< endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4404, "score_of_the_acc": -0.0142, "final_rank": 12 }, { "submission_id": "aoj_1675_10600218", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,a,b) for(int i=(a);(i)<(b);(++i))\nusing ll=long long;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){return a>b?a=b,true:false;}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){return a<b?a=b,true:false;}\n\nint main(){\n cin.tie(nullptr)->ios::sync_with_stdio(false);\n while(true){\n ll n;\n cin>>n;\n if(n==0) return 0;\n ll a,b,d;\n cin>>a>>b>>d;\n vector<int> x(n),y(n);\n set<pair<int,int>> block;\n rep(i,0,n){\n cin>>x[i]>>y[i];\n block.insert({x[i],y[i]});\n }\n const int N=101;\n const ll inf=1ll<<60;\n const int dy[]={0,1,0,-1};\n const int dx[]={1,0,-1,0};\n vector dist(N,vector(N,vector<ll>(4,inf)));\n ll nowX=a,nowY=b;\n int dir=0;\n dist[nowX][nowY][dir]=0;\n rep(turn,0,d){\n const ll nowval=dist[nowX][nowY][dir];\n ll neX=nowX+dx[dir];\n ll neY=nowY+dy[dir];\n while(block.contains({neX,neY})){\n dir++;\n if(dir==4) dir-=4;\n neX=nowX+dx[dir];\n neY=nowY+dy[dir];\n }\n if(neX<0 or 100<neX or neY<0 or 100<neY){\n ll remD=d-turn-1;\n neX+=dx[dir]*remD;\n neY+=dy[dir]*remD;\n nowX=neX;\n nowY=neY;\n break;\n }\n // has loop\n if(dist[neX][neY][dir]!=inf){\n const ll len=nowval+1-dist[neX][neY][dir];\n if(len==0){\n nowX=neX;\n nowY=neY;\n break;\n }\n ll ansD=(d-turn-1)%len;\n ll X=neX;\n ll Y=neY;\n ll ndir=dir;\n while(ansD--){\n ll nX=X+dx[ndir];\n ll nY=Y+dy[ndir];\n while(block.contains({nX,nY})){\n ndir++;\n if(ndir==4) ndir-=4;\n nX=X+dx[ndir];\n nY=Y+dy[ndir];\n }\n X=nX;\n Y=nY;\n }\n nowX=X;\n nowY=Y;\n break;\n }else{\n dist[neX][neY][dir]=nowval+1;\n nowX=neX;\n nowY=neY;\n }\n }\n cout<<nowX<<\" \"<<nowY<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4352, "score_of_the_acc": -0.0151, "final_rank": 14 }, { "submission_id": "aoj_1675_10594486", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define pll pair<ll,ll>\n#define vll vector<ll>\n#define rep(i,n) for(int i = 0;i < (n);i++)\n#define fore(e,v) for(auto &&e : v)\n#define all(a) begin(a), end(a)\n#define si(a) (int)(a.size())\n#define lb(v,x) (lower_bound(all(v),x) - begin(v))\n#define eb emplace_back\n\ntemplate<typename T,typename S> bool chmin(T& a, const S& b){\n\treturn a > b ? a = b,1 : 0;\n}\ntemplate<typename T,typename S> bool chmax(T& a, const S& b){\n\treturn a < b ? a = b,1 : 0;\n}\nconst ll inf = 3e18;\nstruct _{\n\t_(){ cin.tie(0) -> sync_with_stdio(0), cout.tie(0);}\n}__;\n\nll dx[] = {1,0,-1,0};\nll dy[] = {0,1,0,-1};\n\nint main(){\n\twhile(1){\n\t\tll n;\n\t\tcin>>n;\n\t\tif(n == 0) break;\n\t\tll a,b,d;\n\t\tcin>>a>>b>>d;\n\t\tconst ll M = 101;\n\t\tvector<int> x_y[M],y_x[M];\n\t\trep(i,n){\n\t\t\tll x,y;\n\t\t\tcin>>x>>y;\n\t\t\tx_y[x].eb(y);\n\t\t\ty_x[y].eb(x);\n\t\t}\n\t\trep(i,M){\n\t\t\tsort(all(x_y[i]));\n\t\t\tsort(all(y_x[i]));\n\t\t}\n\t\tll rem[M][M][4];\n\t\trep(i,M)rep(j,M)rep(r,4)rem[i][j][r] = -1;\n\t\tqueue<pair<ll,pll> > que;\n\t\tauto push = [&](ll x,ll y,ll r){\n\t\t\tif(rem[x][y][r] > d){\n\t\t\t\tll shuki = rem[x][y][r] - d;\n\t\t\t\td %= shuki;\n\t\t\t}else{\n\t\t\t\trem[x][y][r] = d;\n\t\t\t}\n\t\t\tque.push(make_pair(r,make_pair(x,y)));\n\t\t};\n\t\tpush(a,b,0);\n\t\twhile(!que.empty()){\n\t\t\tauto [r,xy] = que.front();\n\t\t\tauto [x,y] = xy;\n\t\t\tque.pop();\n\t\t\t// cout<<\"x,y,r = \"<<x<<\" \"<<y<<\" \"<<r<<endl;\n\t\t\tll nx = -1,ny = -1, dd = 0;\n\t\t\tif(r == 0){\n\t\t\t\tll j = lb(y_x[y],x + 1);\n\t\t\t\tif(j == si(y_x[y])){\n\t\t\t\t\tdd = d;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tdd = y_x[y][j] - 1 - x;\n\t\t\t\t}\n\t\t\t}else if(r == 1){\n\t\t\t\tll j = lb(x_y[x],y + 1);\n\t\t\t\tif(j == si(x_y[x])){\n\t\t\t\t\tdd = d;\n\t\t\t\t}else{\n\t\t\t\t\tdd = x_y[x][j] - 1 - y;\n\t\t\t\t}\n\t\t\t}else if(r == 2){\n\t\t\t\tll j = lb(y_x[y],x);\n\t\t\t\tif(j == 0){\n\t\t\t\t\tdd = d;\n\t\t\t\t}else{\n\t\t\t\t\tdd = x - (y_x[y][j - 1] + 1);\n\t\t\t\t}\n\t\t\t}else if(r == 3){\n\t\t\t\tll j = lb(x_y[x],y);\n\t\t\t\tif(j == 0){\n\t\t\t\t\tdd = d;\n\t\t\t\t}else{\n\t\t\t\t\tdd = y - (x_y[x][j - 1] + 1);\n\t\t\t\t}\n\t\t\t}\n\t\t\tchmin(dd,d);\n\t\t\td -= dd;\n\t\t\tnx = x + dx[r] * dd;\n\t\t\tny = y + dy[r] * dd;\n\t\t\tif(d > 0){\n\t\t\t\tpush(nx,ny,(r + 1) % 4);\n\t\t\t}else{\n\t\t\t\tcout<<nx<<\" \"<<ny<<endl;\n\t\t\t}\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3740, "score_of_the_acc": -0.0019, "final_rank": 4 }, { "submission_id": "aoj_1675_10594299", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,n) for(ll i=0;i<n;i++)\n\nint di[]={1,0,-1,0};\nint dj[]={0,1,0,-1};\n\nvoid solve(int n){\n ll a,b,d; cin>>a>>b>>d;\n vector g(101,vector<int>(101));\n rep(i,n){\n int x,y; cin>>x>>y;\n g[x][y]=1;\n }\n\n ll ansx,ansy;\n vector visited(101,vector(101,vector<ll>(4,-1)));\n ll i=a,j=b,k=0;\n while(1){\n if(visited[i][j][k]!=-1){\n d=d%(visited[i][j][k]-d);\n if(d==0){\n ansx=i;\n ansy=j;\n break;\n }\n }\n visited[i][j][k]=d;\n\n ll nexti=i+di[k];\n ll nextj=j+dj[k];\n\n if(nexti<0 || nextj<0 || nexti>=101 || nextj>=101){\n ansx=i+di[k]*d;\n ansy=j+dj[k]*d;\n break;\n }\n\n if(g[nexti][nextj]==1){\n k=(k+1)%4;\n continue;\n }\n\n i=nexti;\n j=nextj;\n d--;\n\n if(d==0){\n ansx=i;\n ansy=j;\n break;\n }\n }\n cout << ansx << \" \" << ansy << endl;\n}\n\nint main(){\n while(1){\n int n; cin>>n;\n if(n==0) break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4224, "score_of_the_acc": -0.0136, "final_rank": 10 }, { "submission_id": "aoj_1675_10587595", "code_snippet": "#include <bits/stdc++.h>\nint N;\nlong long A, B, D, X[1000], Y[1000];\nconst int dx[]{1,0,-1,0},dy[]{0,1,0,-1};\nvoid solve() {\n std::set<std::pair<long long, long long>> blk;\n for (int i = 0 ; i < N ; i++) blk.insert({X[i], Y[i]});\n std::set<std::tuple<long long, long long, int>> set;\n int dir = 0;\n auto next_step = [&]() -> std::optional<std::pair<long long, long long>> {\n long long a = A, b = B;\n for (long long i = 0 ; i < std::min(120LL, D) ; i++) {\n const long long na = a + dx[dir], nb = b + dy[dir];\n if (blk.contains({na, nb})) {\n return std::pair{a, b};\n }\n a = std::move(na);\n b = std::move(nb);\n }\n return std::nullopt;\n };\n std::stack<std::pair<\n std::tuple<long long, long long, int>,\n long long>> stk;\n stk.push(std::pair{std::tuple{A, B, dir}, 0LL});\n set.insert({A, B, dir});\n bool fn = false;\n while (D) {\n assert(D > 0);\n const auto npos = next_step();\n if (npos) {\n const auto [na, nb] = npos.value();\n const long long d = std::abs(A - na) + std::abs(B - nb);\n D -= d;\n dir = (dir + 1) % 4;\n A = na;\n B = nb;\n if (!fn and set.contains({A, B, dir})) {\n fn = true;\n long long cyc = d; \n while (true) {\n assert(stk.size());\n if (stk.top().first == std::tuple{A, B, dir}) break;\n cyc += stk.top().second;\n stk.pop();\n }\n assert(cyc > 0);\n D %= cyc;\n }\n else {\n stk.push(std::pair{std::tuple{A, B, dir}, d});\n set.insert({A, B, dir});\n }\n }\n else {\n A += D * dx[dir];\n B += D * dy[dir];\n D = 0;\n break;\n }\n }\n std::cout << A << ' ' << B << '\\n';\n}\nint main() {\n while (true) {\n std::cin >> N;\n if (N == 0) break;\n std::cin >> A >> B >> D;\n for (int i = 0 ; i < N ; i++) std::cin >> X[i] >> Y[i];\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1675_10584289", "code_snippet": "#include <iostream>\n#include <array>\n\nvoid calc(int n) {\n int r = 0;\n std::int64_t a, b, d;\n std::cin >> a >> b >> d;\n std::array<std::array<bool, 101>, 101> obs{false};\n std::array<std::array<std::array<std::int64_t, 4>, 101>, 101> dist{};\n \n dist[b][a][r] = d;\n \n std::array<int, 4> mvx = { 1, 0, -1, 0};\n std::array<int, 4> mvy = { 0, 1, 0, -1};\n \n for (int i = 0; i < n; ++i) {\n int x, y;\n std::cin >> x >> y;\n obs[y][x] = true;\n }\n \n bool cycleFound = false;\n \n while (d) {\n int A, B;\n A = a + mvx[r];\n B = b + mvy[r];\n if ((A < 0 || 100 < A) || (B < 0 || 100 < B)) {\n a += d * mvx[r];\n b += d * mvy[r];\n d = 0;\n break;\n }\n if (obs[B][A]) {\n ++r;\n r = ((r % 4) + 4) % 4;\n } else {\n a = A;\n b = B;\n --d;\n if (dist[b][a][r] > 0 && !cycleFound) {\n int cycleDist = dist[b][a][r] - d;\n d %= cycleDist;\n cycleFound = true;\n }\n dist[b][a][r] = d;\n }\n }\n std::cout << a << ' ' << b << std::endl;\n}\n\nint main() {\n int n;\n \n while (true) {\n std::cin >> n;\n if (n == 0) {\n break;\n }\n calc(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3652, "score_of_the_acc": -0.0008, "final_rank": 3 }, { "submission_id": "aoj_1675_10580594", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\ntemplate <class T>\nusing MaxHeap = std::priority_queue<T>;\ntemplate <class T>\nusing MinHeap = std::priority_queue<T, vector<T>, greater<T>>;\n#define rep2(i, n) for (ll i = 0; i < (n); i++)\n#define rep3(i, l, r) for (ll i = (l); i < (r); i++)\n#define rrep2(i, n) for (ll i = n; i-- > 0;)\n#define rrep3(i, r, l) for (ll i = (r); i-- > (l);)\n#define overload(a, b, c, d, ...) d\n#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)\n#define rrep(...) overload(__VA_ARGS__, rrep3, rrep2)(__VA_ARGS__)\n#define all(x) begin(x), end(x)\nbool chmin(auto& lhs, auto rhs) {\n return lhs > rhs ? lhs = rhs, 1 : 0;\n}\nbool chmax(auto& lhs, auto rhs) {\n return lhs < rhs ? lhs = rhs, 1 : 0;\n}\nstruct IOIO {\n IOIO() {\n std::cin.tie(0)->sync_with_stdio(0);\n }\n} ioio;\n\nvoid solve() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) return;\n const int N = 103;\n const int OFFSET = 1;\n vector parent(N, vector(N, vector<tuple<int, int, int>>(4)));\n int si, sj;\n ll d;\n cin >> sj >> si >> d;\n si += OFFSET;\n sj += OFFSET;\n vector obs(N, vector<int>(N));\n rep(i, n) {\n ll x, y;\n cin >> x >> y;\n x += OFFSET;\n y += OFFSET;\n obs[y][x] = true;\n }\n const int DI[] = {1, 0, -1, 0};\n const int DJ[] = {0, -1, 0, 1};\n rep(i, N) rep(j, N) rep(d, 4) {\n parent[i][j][d] = {i, j, d};\n if (obs[i][j]) continue;\n if (i == 0 || j == 0 || i == N - 1 || j == N - 1) continue;\n rep(dd, 4) {\n int nd = (d + dd) % 4;\n int ni = i + DI[nd];\n int nj = j + DJ[nd];\n if (ni < 0 || nj < 0 || ni >= N || nj >= N) continue;\n if (obs[ni][nj]) continue;\n parent[i][j][d] = {ni, nj, nd};\n break;\n }\n }\n const int LOG = 60;\n vector to(LOG + 1, vector(N, vector(N, vector<tuple<int, int, int>>(4))));\n rep(i, N) rep(j, N) rep(k, 4) to[0][i][j][k] = parent[i][j][k];\n rep(log, LOG) {\n rep(i, N) rep(j, N) rep(k, 4) {\n auto [a, b, c] = to[log][i][j][k];\n to[log + 1][i][j][k] = to[log][a][b][c];\n }\n }\n tuple<ll, ll, ll> pos = {si, sj, 3};\n rep(i, LOG + 1) {\n if (d & (1LL << i)) {\n auto [a, b, c] = pos;\n pos = to[i][a][b][c];\n }\n }\n {\n tuple<ll, ll, ll> pos = {si, sj, 3};\n rep(i, min<ll>(d, 20)) {\n auto [a, b, c] = pos;\n pos = to[0][a][b][c];\n // cerr << a << ' ' << b << ' ' << c << endl;\n }\n }\n auto [i, j, dir] = pos;\n if (i == 0 || j == 0 || i == N - 1 || j == N - 1) {\n // i = 0, or j = 0 or ...になる最小時間\n ll ok = 1e18;\n ll ng = 0;\n while (ok - ng > 1) {\n ll key = (ok + ng) / 2;\n tuple<ll, ll, ll> pos = {si, sj, 3};\n rep(i, LOG + 1) {\n if (key & (1LL << i)) {\n auto [a, b, c] = pos;\n pos = to[i][a][b][c];\n }\n }\n auto [i, j, _] = pos;\n if (i == 0 || j == 0 || i == N - 1 || j == N - 1)\n ok = key;\n else\n ng = key;\n }\n // cerr << d << ' ' << d - ok << endl;\n d -= ok;\n assert(d >= 0);\n i += d * DI[dir];\n j += d * DJ[dir];\n cout << j - OFFSET << ' ' << i - OFFSET << '\\n';\n } else {\n // cerr << __LINE__ << endl;\n cout << j - OFFSET << ' ' << i - OFFSET << '\\n';\n }\n }\n}\n\nint main() {\n int t = 1;\n // cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 5510, "memory_kb": 61184, "score_of_the_acc": -1.5076, "final_rank": 19 }, { "submission_id": "aoj_1675_10576027", "code_snippet": "#include<bits/stdc++.h>\n#define fast std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr)\n#define eb emplace_back\n#define mp make_pair\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\nusing ll = long long;\nconstexpr ll inf = 2e18;\nconstexpr int mod = 998244353;\n#ifdef LOCAL\n#define debug(x) std::cerr << #x << \" = \" << (x) << std::endl;\n#else\n#define debug(x)\n#endif\nstatic void judge(bool c) {\n std::cout << (c ? \"Yes\" : \"No\") << '\\n';\n}\n\nll n,a,b,d,x[1000],y[1000];\nll dirx[4] = {1,0,-1,0},diry[4] = {0,1,0,-1};\nset<pair<ll,ll>> hit; \n\nint main() {\n fast;\n while(true){ \n cin >> n;\n if(n == 0) return 0; \n\n cin >> a >> b >> d;\n hit.clear(); \n for(int i = 0; i < n; i++) {\n ll obs_x, obs_y;\n cin >> obs_x >> obs_y;\n hit.insert(mp(obs_x, obs_y));\n }\n\n ll nowx = a, nowy = b;\n ll dir = 0; \n \n vector<tuple<ll,ll,ll>> path_log;\n map<tuple<ll,ll,ll>, ll> visited_step;\n\n path_log.eb(make_tuple(nowx, nowy, dir));\n visited_step[make_tuple(nowx, nowy, dir)] = 0; \n\n ll current_step = 0; \n\n bool loop_detected = false;\n ll loop_start_step = -1; \n ll loop_length = -1; \n\n const ll SIMULATION_LIMIT = 200000; \n\n for(ll i = 0; i < SIMULATION_LIMIT; ++i) { \n ll next_x = nowx + dirx[dir];\n ll next_y = nowy + diry[dir];\n\n if(hit.count(mp(next_x, next_y))) { \n dir = (dir + 1) % 4; \n } else { \n nowx = next_x;\n nowy = next_y;\n current_step++; \n \n if (visited_step.count(make_tuple(nowx, nowy, dir))) {\n loop_detected = true;\n loop_start_step = visited_step[make_tuple(nowx, nowy, dir)];\n loop_length = current_step - loop_start_step; \n break;\n }\n \n path_log.eb(make_tuple(nowx, nowy, dir));\n visited_step[make_tuple(nowx, nowy, dir)] = current_step;\n }\n\n if (current_step >= d) {\n break;\n }\n }\n\n if (loop_detected) {\n if (d < current_step) {\n auto final_state = path_log[d];\n cout << get<0>(final_state) << \" \" << get<1>(final_state) << endl;\n } else {\n ll remaining_d = d - loop_start_step;\n ll offset_in_loop = remaining_d % loop_length;\n \n auto final_state = path_log[loop_start_step + offset_in_loop];\n cout << get<0>(final_state) << \" \" << get<1>(final_state) << endl;\n }\n } else {\n if (current_step >= d) {\n auto final_state = path_log[d];\n cout << get<0>(final_state) << \" \" << get<1>(final_state) << endl;\n } else {\n ll remaining_move = d - current_step;\n nowx += dirx[dir] * remaining_move;\n nowy += diry[dir] * remaining_move;\n cout << nowx << \" \" << nowy << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 2350, "memory_kb": 25208, "score_of_the_acc": -0.6072, "final_rank": 18 }, { "submission_id": "aoj_1675_10575742", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n#define rep(i, a, b) for(int i = (a); i < (b); i++) // [a, b)を昇順に\n#define drep(i, a, b) for(int i = (b) - 1; i >= a; i--) // [a, b)を降順に\n#define all(v) v.begin(), v.end() // 昇順sortに\n#define rall(v) v.rbegin(), v.rend() // 降順sortに\n#define sz(x) (int)(x).size()\n#define decimal(n) cout << fixed << setprecision(n);\n#define debug(x) cerr << #x << \" = \" << x << endl;\n#define Yes cout << \"Yes\" << endl;\n#define No cout << \"No\" << endl;\n#define yesno(condition) { if (condition) cout << \"Yes\" << endl; else cout << \"No\" << endl; }\nusing vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;\nusing ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing lll = __int128;\n// using mint = modint998244353;\nconst ll MOD = 998244353;\n// using mint = modint;\n// const ll MOD = 998244353;\n// using mint = modint1000000007;\n// const ll MOD = 1000000007;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\nusing P = pair<ll, ll>; using T = tuple<ll, ll, ll>;\nusing vc = vector<char>;\nconst ll MAX = 5'000'009; const ll llINF = 2e18;\nconst int intINF = 2e9; const long double pi = acos(-1);\nll di[] = {0, -1, 0, 1, -1, -1, 1, 1};\nll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<class T> using pq = priority_queue<T, vector<T>>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\nbool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }\nbool is_multipul_overflow(ll a, ll b, ll c) {\n // a * b > cかを判定する\n if (a == 0 || b == 0) return false;\n return (a <= c / b ? false : true);\n}\n\n\n\nll dx[] = {1, 0, -1, 0};\nll dy[] = {0, 1, 0, -1};\n\nvoid solve(ll &n) {\n ll a, b, d;\n cin >> a >> b >> d;\n vl x(n), y(n);\n rep(i, 0, n) cin >> x[i] >> y[i];\n set<P> isblocked;\n rep(i, 0, n) {\n isblocked.insert({x[i], y[i]});\n }\n\n int mincoordinate = -1, maxcoordinate = 101;\n // x,yともに-1以下、101以上を超えたらその方向にまっすぐ進む\n // 前と同じ座標、同じ方向にたどり着いたらループしている\n\n auto dist = vector(maxcoordinate, vector(maxcoordinate, vector<int>(4, intINF)));\n ll rundist = 0;\n ll curx = a, cury = b, curdir = 0;\n\n // dist[curx][cury][curdir] = 0;\n\n while(rundist < d) {\n // 障害物に当たりえない範囲に来たので、残りの距離だけその方向に進む\n if(min(curx, cury) <= mincoordinate || max(curx, cury) >= maxcoordinate) {\n curx += (d - rundist) * dx[curdir];\n cury += (d - rundist) * dy[curdir];\n\n rundist += d - rundist;\n continue;\n }\n // 前と同じ座標、同じ方向に来たので、ループ分を飛ばす\n else if(dist[curx][cury][curdir] != intINF && d- rundist >= rundist - dist[curx][cury][curdir]) {\n ll loopdist = rundist - dist[curx][cury][curdir];\n\n // dを超えない範囲で、rundistにloopdistを足し続ける\n rundist += loopdist * ((d - rundist) / loopdist);\n continue;\n }\n\n // 距離を記録しておく\n dist[curx][cury][curdir] = rundist;\n\n\n ll nextx = curx + dx[curdir], nexty = cury + dy[curdir];\n\n // 次に障害物がないので、進む\n if(!isblocked.count({nextx, nexty})) {\n\n rundist++;\n\n swap(nextx, curx);\n swap(nexty, cury);\n }\n // 次に障害物があるので、回転する\n else {\n curdir++;\n if(curdir == 4) curdir = 0;\n }\n }\n\n cout << curx << \" \" << cury << endl;\n}\n\nint main() {\n ll n;\n while(true) {\n cin >> n;\n if(n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3868, "score_of_the_acc": -0.0139, "final_rank": 11 }, { "submission_id": "aoj_1675_10566881", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(int N) {\n int a, b;\n ll d;\n cin >> a >> b >> d;\n vector<vector<bool>> obj(101, vector<bool>(101, false));\n for (int i=0; i<N; i++) {\n int x, y;\n cin >> x >> y;\n obj[x][y] = true;\n }\n\n int dir = 0;\n int dirx[] = {1,0,-1,0};\n int diry[] = {0,1,0,-1};\n\n struct S {\n int right, up, left, down;\n };\n vector<vector<S>> dist(101, vector<S>(101, {-1,-1,-1,-1}));\n dist[a][b].right = 0;\n vector<tuple<int, int, S>> hist;\n hist.emplace_back(make_tuple(a, b, dist[a][b]));\n\n int x=a, y=b;\n int count = 0;\n while (true) {\n count++;\n int nx = x+dirx[dir], ny = y+diry[dir];\n while (0<=nx && nx<=100 && 0<=ny && ny<=100 && obj[nx][ny]) {\n // turn 90\n dir = (dir+1)%4;\n nx = x+dirx[dir]; ny = y+diry[dir];\n }\n //cerr << '(' << nx << ',' << ny << \")\\n\";\n if (nx<0 || 101<=nx || ny<0 || 101<=ny) {\n // get out\n ll fx = x + (d-count+1) * dirx[dir];\n ll fy = y + (d-count+1) * diry[dir];\n cout << fx << ' ' << fy << endl;\n return;\n }\n if (dir == 0) {\n if (dist[nx][ny].right != -1) {\n int loop_len = count - dist[nx][ny].right;\n int res = (d - count) % loop_len;\n int fx = get<0>(hist[dist[nx][ny].right+res]);\n int fy = get<1>(hist[dist[nx][ny].right+res]);\n cout << fx << ' ' << fy << endl;\n return;\n }\n dist[nx][ny].right = count;\n }\n else if (dir == 1) {\n if (dist[nx][ny].up != -1) {\n int loop_len = count - dist[nx][ny].up;\n int res = (d - count) % loop_len;\n int fx = get<0>(hist[dist[nx][ny].up+res]);\n int fy = get<1>(hist[dist[nx][ny].up+res]);\n cout << fx << ' ' << fy << endl;\n return;\n }\n dist[nx][ny].up = count;\n }\n else if (dir == 2) {\n if (dist[nx][ny].left != -1) {\n int loop_len = count - dist[nx][ny].left;\n int res = (d - count) % loop_len;\n int fx = get<0>(hist[dist[nx][ny].left+res]);\n int fy = get<1>(hist[dist[nx][ny].left+res]);\n cout << fx << ' ' << fy << endl;\n return;\n }\n dist[nx][ny].left = count;\n }\n else {\n if (dist[nx][ny].down != -1) {\n int loop_len = count - dist[nx][ny].down;\n int res = (d - count) % loop_len;\n int fx = get<0>(hist[dist[nx][ny].down+res]);\n int fy = get<1>(hist[dist[nx][ny].down+res]);\n cout << fx << ' ' << fy << endl;\n return;\n }\n dist[nx][ny].down = count;\n }\n hist.emplace_back(make_tuple(nx, ny, dist[nx][ny]));\n x = nx; y = ny;\n\n if (count == d) break;\n }\n\n cout << x << ' ' << y << endl;\n}\n\nint main() {\n while(true) {\n int N;\n cin >> N;\n if (N == 0) break;\n solve(N);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3840, "score_of_the_acc": -0.003, "final_rank": 7 }, { "submission_id": "aoj_1675_10490517", "code_snippet": "#line 1 \"D.cpp\"\n#include <iostream>\n#include <utility>\n#include <vector>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 1000001000;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\n#line 1 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n\n\n\n#ifdef LOCAL\n#define dbg(x) std::cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << std::endl\n#else\n#define dbg(x) true\n#endif\n\n#line 13 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n#include <set>\n\n\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {\n os << '(' << p.first << \", \" << p.second << ')';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << '[';\n for (int i=0; i<int(vec.size()); i++) {\n os << vec[i];\n if (i != int(vec.size())-1) os << \", \";\n }\n os << ']';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& st) {\n os << '{';\n auto itr = st.begin();\n while (itr != st.end()) {\n os << *itr;\n itr = next(itr);\n if (itr != st.end()) os << \", \";\n }\n os << '}';\n return os;\n}\n\n\n\n#line 15 \"D.cpp\"\n\nconst int dx[4] = {1,0,-1,0};\nconst int dy[4] = {0,1,0,-1};\n\nbool isin(int x, int y) {\n return 0 <= x && x <= 100 && 0 <= y && y <= 100;\n}\n\nint moove (vector<vector<vector<vector<int>>>>const& dp, int x, int y, int k, ll num) {\n assert(isin(x,y));\n assert(0 <= k && k < 4);\n\n dbg(num);\n for (int i=0; i<60 && (1LL<<i)<=num && num > 0; i++) {\n if ((1LL << i) & num) {\n int nxt_val = dp[i][k][x][y];\n if (nxt_val >= INTINF) return nxt_val;\n int nxt_k = nxt_val / 10201;\n int nxt_x = nxt_val % 10201 / 101;\n int nxt_y = nxt_val % 10201 % 101;\n\n k = nxt_k;\n x = nxt_x;\n y = nxt_y;\n num -= (1LL << i);\n }\n }\n dbg(num);\n assert(num == 0LL);\n return 10201 * k + 101 * x + y;\n}\n\nvoid solve(int n) {\n int a, b; cin >> a >> b;\n ll d; cin >> d;\n\n vector<vector<bool>>obs(101,vector<bool>(101,false));\n \n vector<pair<int,int>>xy(n); for (int i=0; i<n; i++) cin >> xy[i].first >> xy[i].second;\n\n for (int i=0; i<n; i++) obs[xy[i].first][xy[i].second] = true;\n dbg(obs);\n\n // dp[i][d][x][y] := (x,y)で方向dを向いているときに2^i回移動したらいる場所 ([0,100] * [0,100]の外にいるならINTINF, そうでないなら 10201 * d + x * 101 + y\n\n auto dp = vector(60, vector(4, vector(101, vector(101, -1))));\n\n // i=0パターン\n for (int k=0; k<4; k++) {\n for (int x=0; x<=100; x++) {\n for (int y=0; y<=100; y++) {\n if (obs[x][y]) continue;\n\n int nx = x + dx[k];\n int ny = y + dy[k];\n \n // (nx,ny)が何もない[0,100]*[0,100]\n if (isin(nx,ny) && !obs[nx][ny]) {\n dp[0][k][x][y] = 10201 * k + nx * 101 + ny;\n }\n // (nx,ny)が障害物のある[0,100]*[0,100]\n else if (isin(nx,ny) && obs[nx][ny]) {\n int nx1 = x + dx[(k+1)%4];\n int nx2 = x + dx[(k+2)%4];\n int nx3 = x + dx[(k+3)%4];\n \n int ny1 = y + dy[(k+1)%4];\n int ny2 = y + dy[(k+2)%4];\n int ny3 = y + dy[(k+3)%4];\n\n if (isin(nx1,ny1) && !obs[nx1][ny1]) dp[0][k][x][y] = 10201 * ((k+1)%4) + nx1 * 101 + ny1;\n else if(!isin(nx1,ny1)) dp[0][k][x][y] = INTINF + (k+1)%4;\n\n else if (isin(nx2,ny2) && !obs[nx2][ny2]) dp[0][k][x][y] = 10201 * ((k+2)%4) + nx2 * 101 + ny2;\n else if(!isin(nx2,ny2)) dp[0][k][x][y] = INTINF + (k+2)%4;\n\n else if (isin(nx3,ny3) && !obs[nx3][ny3]) dp[0][k][x][y] = 10201 * ((k+3)%4) + nx3 * 101 + ny3;\n else if(!isin(nx3,ny3)) dp[0][k][x][y] = INTINF + (k+3)%4;\n\n // 袋小路やん\n else {\n dp[0][k][x][y] = -1;\n }\n \n }\n // (nx,ny)が何もない nx < 0 || 100 < nx || ny < 0 || 100 < ny \n else if (nx < 0 || 100 < nx || ny < 0 || 100 < ny) {\n assert(!isin(nx,ny));\n dp[0][k][x][y] = INTINF + k;\n }\n else {\n dbg(k);\n dbg(x);\n dbg(y);\n dbg(nx);\n dbg(ny);\n assert(false);\n }\n }\n }\n }\n\n // dp[i][d][x][y] := (x,y)で方向dを向いているときに2^i回移動したらいる場所 ([0,100] * [0,100]の外にいるならINTINF, そうでないなら 10201 * d + x * 101 + y\n for (int i=0; i+1<60; i++) for (int k=0; k<4; k++) for (int x=0; x<=100; x++) for (int y=0; y<=100; y++) {\n if (obs[x][y]) continue;\n if (dp[i][k][x][y] == -1) continue;\n int nxt_val = dp[i][k][x][y];\n if (nxt_val >= INTINF) {\n dp[i+1][k][x][y] = nxt_val;\n continue;\n }\n int nxt_k = nxt_val / 10201;\n int nxt_x = nxt_val % 10201 / 101;\n int nxt_y = nxt_val % 10201 % 101;\n\n dp[i+1][k][x][y] = dp[i][nxt_k][nxt_x][nxt_y];\n }\n\n\n ll ok = 0;\n // ここ\n if (moove(dp,a,b,0,d) < INTINF) {\n int val = moove(dp,a,b,0,d);\n cout << val % 10201 / 101 << ' ' << val % 10201 % 101 << endl;\n return;\n }\n ll ng = d;\n\n while (abs(ng - ok) > 1) {\n ll mid = (ok + ng) / 2;\n if (moove(dp,a,b,0,mid) < INTINF) ok = mid;\n else ng = mid;\n }\n\n int val0 = moove(dp,a,b,0,ok);\n assert(val0 < INTINF);\n int k = val0 / 10201;\n ll x = val0 % 10201 / 101;\n ll y = val0 % 10201 % 101;\n\n if (!isin(x+dx[k], y+dy[k])) cout << x + ll(dx[k]) * (d-ok) << ' ' << y + ll(dy[k]) * (d-ok) << endl;\n else {\n k++;\n k %= 4;\n if (!isin(x+dx[k], y+dy[k])) cout << x + ll(dx[k]) * (d-ok) << ' ' << y + ll(dy[k]) * (d-ok) << endl;\n else {\n k++;\n k %= 4;\n if (!isin(x+dx[k], y+dy[k])) cout << x + ll(dx[k]) * (d-ok) << ' ' << y + ll(dy[k]) * (d-ok) << endl;\n else {\n k++;\n k %= 4;\n if (!isin(x+dx[k], y+dy[k])) cout << x + ll(dx[k]) * (d-ok) << ' ' << y + ll(dy[k]) * (d-ok) << endl;\n else assert(false);\n }\n }\n }\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n while (true) {\n int n; cin >> n;\n if (n == 0) return 0;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 14080, "score_of_the_acc": -0.3328, "final_rank": 17 }, { "submission_id": "aoj_1675_10490508", "code_snippet": "#line 1 \"D.cpp\"\n#include <iostream>\n#include <utility>\n#include <vector>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 1000001000;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\n#line 1 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n\n\n\n#ifdef LOCAL\n#define dbg(x) std::cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << std::endl\n#else\n#define dbg(x) true\n#endif\n\n#line 13 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n#include <set>\n\n\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {\n os << '(' << p.first << \", \" << p.second << ')';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << '[';\n for (int i=0; i<int(vec.size()); i++) {\n os << vec[i];\n if (i != int(vec.size())-1) os << \", \";\n }\n os << ']';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& st) {\n os << '{';\n auto itr = st.begin();\n while (itr != st.end()) {\n os << *itr;\n itr = next(itr);\n if (itr != st.end()) os << \", \";\n }\n os << '}';\n return os;\n}\n\n\n\n#line 15 \"D.cpp\"\n\nconst int dx[4] = {1,0,-1,0};\nconst int dy[4] = {0,1,0,-1};\n\nbool isin(int x, int y) {\n return 0 <= x && x <= 100 && 0 <= y && y <= 100;\n}\n\nint moove (vector<vector<vector<vector<int>>>>const& dp, int x, int y, int k, ll num) {\n assert(isin(x,y));\n assert(0 <= k && k < 4);\n\n dbg(num);\n for (int i=0; i<60 && (1LL<<i)<=num && num > 0; i++) {\n if ((1LL << i) & num) {\n int nxt_val = dp[i][k][x][y];\n if (nxt_val >= INTINF) return nxt_val;\n int nxt_k = nxt_val / 10201;\n int nxt_x = nxt_val % 10201 / 101;\n int nxt_y = nxt_val % 10201 % 101;\n\n k = nxt_k;\n x = nxt_x;\n y = nxt_y;\n num -= (1LL << i);\n }\n }\n dbg(num);\n assert(num == 0LL);\n return 10201 * k + 101 * x + y;\n}\n\nvoid solve(int n) {\n int a, b; cin >> a >> b;\n ll d; cin >> d;\n\n vector<vector<bool>>obs(101,vector<bool>(101,false));\n \n vector<pair<int,int>>xy(n); for (int i=0; i<n; i++) cin >> xy[i].first >> xy[i].second;\n\n for (int i=0; i<n; i++) obs[xy[i].first][xy[i].second] = true;\n dbg(obs);\n\n // dp[i][d][x][y] := (x,y)で方向dを向いているときに2^i回移動したらいる場所 ([0,100] * [0,100]の外にいるならINTINF, そうでないなら 10201 * d + x * 101 + y\n\n auto dp = vector(60, vector(4, vector(101, vector(101, -1))));\n\n // i=0パターン\n for (int k=0; k<4; k++) {\n for (int x=0; x<=100; x++) {\n for (int y=0; y<=100; y++) {\n if (obs[x][y]) continue;\n\n int nx = x + dx[k];\n int ny = y + dy[k];\n \n // (nx,ny)が何もない[0,100]*[0,100]\n if (isin(nx,ny) && !obs[nx][ny]) {\n dp[0][k][x][y] = 10201 * k + nx * 101 + ny;\n }\n // (nx,ny)が障害物のある[0,100]*[0,100]\n else if (isin(nx,ny) && obs[nx][ny]) {\n int nx1 = x + dx[(k+1)%4];\n int nx2 = x + dx[(k+2)%4];\n int nx3 = x + dx[(k+3)%4];\n \n int ny1 = y + dy[(k+1)%4];\n int ny2 = y + dy[(k+2)%4];\n int ny3 = y + dy[(k+3)%4];\n\n if (isin(nx1,ny1) && !obs[nx1][ny1]) dp[0][k][x][y] = 10201 * ((k+1)%4) + nx1 * 101 + ny1;\n else if(!isin(nx1,ny1)) dp[0][k][x][y] = INTINF + (k+1)%4;\n\n else if (isin(nx2,ny2) && !obs[nx2][ny2]) dp[0][k][x][y] = 10201 * ((k+2)%4) + nx2 * 101 + ny2;\n else if(!isin(nx2,ny2)) dp[0][k][x][y] = INTINF + (k+2)%4;\n\n else if (isin(nx3,ny3) && !obs[nx3][ny3]) dp[0][k][x][y] = 10201 * ((k+3)%4) + nx3 * 101 + ny3;\n else if(!isin(nx3,ny3)) dp[0][k][x][y] = INTINF + (k+3)%4;\n\n // 袋小路やん\n else {\n dp[0][k][x][y] = -1;\n }\n \n }\n // (nx,ny)が何もない nx < 0 || 100 < nx || ny < 0 || 100 < ny \n else if (nx < 0 || 100 < nx || ny < 0 || 100 < ny) {\n assert(!isin(nx,ny));\n dp[0][k][x][y] = INTINF + k;\n }\n else {\n dbg(k);\n dbg(x);\n dbg(y);\n dbg(nx);\n dbg(ny);\n assert(false);\n }\n }\n }\n }\n\n // dp[i][d][x][y] := (x,y)で方向dを向いているときに2^i回移動したらいる場所 ([0,100] * [0,100]の外にいるならINTINF, そうでないなら 10201 * d + x * 101 + y\n for (int i=0; i+1<60; i++) for (int k=0; k<4; k++) for (int x=0; x<=100; x++) for (int y=0; y<=100; y++) {\n if (obs[x][y]) continue;\n if (dp[i][k][x][y] == -1) continue;\n int nxt_val = dp[i][k][x][y];\n if (nxt_val >= INTINF) {\n dp[i+1][k][x][y] = nxt_val;\n continue;\n }\n int nxt_k = nxt_val / 10201;\n int nxt_x = nxt_val % 10201 / 101;\n int nxt_y = nxt_val % 10201 % 101;\n\n dp[i+1][k][x][y] = dp[i][nxt_k][nxt_x][nxt_y];\n }\n\n\n ll ok = 0;\n if (moove(dp,a,b,0,d) < INTINF) {\n int val = moove(dp,a,b,0,d);\n cout << val % 10201 / 101 << ' ' << val % 10201 % 101 << endl;\n return;\n }\n ll ng = d;\n\n while (abs(ng - ok) > 1) {\n ll mid = (ok + ng) / 2;\n if (moove(dp,a,b,0,mid) < INTINF) ok = mid;\n else ng = mid;\n }\n\n int val0 = moove(dp,a,b,0,ok);\n int val1 = moove(dp,a,b,0,ok+1);\n\n int k = val1 - INTINF;\n ll x = val0 % 10201 / 101 + dx[k];\n ll y = val0 % 10201 % 101 + dy[k];\n\t// cerr << x <<\" \" << y << endl;\n cout << x + ll(dx[k]) * (d - ok-1) << ' ' << y + ll(dy[k]) * (d - ok-1)<< endl;\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n while (true) {\n int n; cin >> n;\n if (n == 0) return 0;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1390, "memory_kb": 14080, "score_of_the_acc": -0.3313, "final_rank": 16 }, { "submission_id": "aoj_1675_10490451", "code_snippet": "//AC\n#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n#define int long long\n\nvoid solve(int n) {\n int direct = 1;\n int a, b, d;\n cin >> a >> b >> d;\n map<int, vector<int>> keyx, keyy;\n set<int> sx, sy;\n for (int i=0; i<n; ++i) {\n int x, y;\n cin >> x >> y;\n sx.insert(x);\n sy.insert(y);\n keyx[x].push_back(y);\n keyy[y].push_back(x);\n }\n\n for (auto x_: sx) {\n sort(keyx[x_].begin(), keyx[x_].end());\n }\n for (auto y_: sy) {\n sort(keyy[y_].begin(), keyy[y_].end());\n }\n\n map<tuple<int, int, int>, int> last_step;\n map<tuple<int, int, int>, int> existed;\n\n // for (int _=0; _<10; ++_) cout << \"=\";\n // cout << \"here~~\" << \"\\n\";\n\n int cnt = 0;\n while (d > 0) {\n if (existed[{a, b, direct}] == 1) {\n int len_cycle = last_step[{a, b, direct}] - d;\n d %= len_cycle;\n // cout << \"len cycle = \" << len_cycle << endl;\n // cout << \"changed d = \" << d << endl;\n }\n // cout << a << ' ' << b << ' ' << d << \" dir = \" << direct << endl;\n\n last_step[{a, b, direct}] = d;\n existed[{a, b, direct}]++;\n if (direct == 1) {\n //right\n //(a, b)にいるからkeyy[b]の配列でaよりも右にある最初の座標を取得する\n \n auto itr = upper_bound(keyy[b].begin(), keyy[b].end(), a);\n if (itr == keyy[b].end()) {\n //全部使い切る\n a += d;\n d = 0;\n } else {\n if (a + d < *itr) {\n //全部使い切る\n a += d;\n d = 0;\n } else {\n int can = *itr - a - 1;\n a += can;\n d -= can;\n }\n }\n } else if (direct == 0) {\n //up\n auto itr = upper_bound(keyx[a].begin(), keyx[a].end(), b);\n if (itr == keyx[a].end()) {\n //全部使い切る\n b += d;\n d = 0;\n } else {\n if (b + d < *itr) {\n //全部使い切る\n b += d;\n d = 0;\n } else {\n int can = *itr - b - 1;\n b += can;\n d -= can;\n }\n }\n } else if (direct == 3) {\n //left\n auto itr = lower_bound(keyy[b].begin(), keyy[b].end(), a);\n if (itr == keyy[b].begin()) {\n //全部使い切る\n a -= d;\n d = 0;\n } else {\n --itr;\n if (a - d > *itr) {\n //全部使い切る\n a -= d;\n d = 0;\n } else {\n int can = a - *itr- 1;\n a -= can;\n d -= can;\n }\n }\n } else {\n //down\n auto itr = lower_bound(keyx[a].begin(), keyx[a].end(), b);\n if (itr == keyx[a].begin()) {\n //全部使い切る\n b -= d;\n d = 0;\n } else {\n --itr;\n if (b - d > *itr) {\n //全部使い切る\n b -= d;\n d = 0;\n } else {\n int can = b - *itr- 1;\n b -= can;\n d -= can;\n }\n }\n\n }\n direct += 3;\n direct %= 4;\n }\n cout << a << ' ' << b << endl;\n}\n\nsigned main() {\n while(true) {\n int n;\n cin >> n;\n if (n == 0) break;\n solve(n);\n }\n}\n\n/*\nmemo\n\n\n2\n0 1 4\n3 1\n2 5\n\n2\n0 1 9\n3 1\n2 5\n\n12\n2 2 11\n0 1\n0 2\n0 3\n4 1\n4 2\n4 3\n3 4\n2 4\n1 4\n3 0\n2 0\n1 0\n\n12\n2 2 7791772263873\n0 1\n0 2\n0 3\n4 1\n4 2\n4 3\n3 4\n2 4\n1 4\n3 0\n2 0\n1 0\n\n3\n0 3 198\n99 2\n100 3\n99 4\n\n3\n0 3 1000000000000000000\n99 2\n100 3\n99 4\n\n1\n99 100 1\n100 100\n0\n\n*/", "accuracy": 1, "time_ms": 20, "memory_kb": 3712, "score_of_the_acc": -0.003, "final_rank": 5 }, { "submission_id": "aoj_1675_10490409", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n#define int long long\n\nvoid solve(int n) {\n int direct = 1;\n int a, b, d;\n cin >> a >> b >> d;\n map<int, vector<int>> keyx, keyy;\n set<int> sx, sy;\n for (int i=0; i<n; ++i) {\n int x, y;\n cin >> x >> y;\n sx.insert(x);\n sy.insert(y);\n keyx[x].push_back(y);\n keyy[y].push_back(x);\n }\n\n for (auto x_: sx) {\n sort(keyx[x_].begin(), keyx[x_].end());\n }\n for (auto y_: sy) {\n sort(keyy[y_].begin(), keyy[y_].end());\n }\n\n map<tuple<int, int, int>, int> last_step;\n map<tuple<int, int, int>, int> existed;\n\n // for (int _=0; _<10; ++_) cout << \"=\";\n // cout << \"here~~\" << \"\\n\";\n\n int cnt = 0;\n while (d > 0) {\n if (existed[{a, b, direct}] == 1) {\n int len_cycle = last_step[{a, b, direct}] - d;\n d %= len_cycle;\n // cout << \"len cycle = \" << len_cycle << endl;\n // cout << \"changed d = \" << d << endl;\n }\n // cout << a << ' ' << b << ' ' << d << \" dir = \" << direct << endl;\n\n last_step[{a, b, direct}] = d;\n existed[{a, b, direct}]++;\n if (direct == 1) {\n //right\n //(a, b)にいるからkeyy[b]の配列でaよりも右にある最初の座標を取得する\n \n auto itr = upper_bound(keyy[b].begin(), keyy[b].end(), a);\n if (itr == keyy[b].end()) {\n //全部使い切る\n a += d;\n d = 0;\n } else {\n if (a + d < *itr) {\n //全部使い切る\n a += d;\n d = 0;\n } else {\n int can = *itr - a - 1;\n a += can;\n d -= can;\n }\n }\n } else if (direct == 0) {\n //up\n auto itr = upper_bound(keyx[a].begin(), keyx[a].end(), b);\n if (itr == keyx[a].end()) {\n //全部使い切る\n b += d;\n d = 0;\n } else {\n if (b + d < *itr) {\n //全部使い切る\n b += d;\n d = 0;\n } else {\n int can = *itr - b - 1;\n b += can;\n d -= can;\n }\n }\n } else if (direct == 3) {\n //left\n auto itr = lower_bound(keyy[b].begin(), keyy[b].end(), a);\n if (itr == keyy[b].begin()) {\n //全部使い切る\n a -= d;\n d = 0;\n } else {\n --itr;\n if (a - d > *itr) {\n //全部使い切る\n a -= d;\n d = 0;\n } else {\n int can = a - *itr- 1;\n a -= can;\n d -= can;\n }\n }\n } else {\n //down\n auto itr = lower_bound(keyx[a].begin(), keyx[a].end(), b);\n if (itr == keyx[a].begin()) {\n //全部使い切る\n b -= d;\n d = 0;\n } else {\n --itr;\n if (b - d > *itr) {\n //全部使い切る\n b -= d;\n d = 0;\n } else {\n int can = b - *itr- 1;\n b -= can;\n d -= can;\n }\n }\n\n }\n direct += 3;\n direct %= 4;\n }\n cout << a << ' ' << b << endl;\n\n}\n\nsigned main() {\n while(true) {\n int n;\n cin >> n;\n if (n == 0) break;\n solve(n);\n }\n}\n\n/*\nmemo\n\n\n2\n0 1 4\n3 1\n2 5\n\n2\n0 1 9\n3 1\n2 5\n\n12\n2 2 11\n0 1\n0 2\n0 3\n4 1\n4 2\n4 3\n3 4\n2 4\n1 4\n3 0\n2 0\n1 0\n\n12\n2 2 7791772263873\n0 1\n0 2\n0 3\n4 1\n4 2\n4 3\n3 4\n2 4\n1 4\n3 0\n2 0\n1 0\n\n3\n0 3 198\n99 2\n100 3\n99 4\n\n3\n0 3 1000000000000000000\n99 2\n100 3\n99 4\n\n1\n99 100 1\n100 100\n0\n\n*/", "accuracy": 1, "time_ms": 20, "memory_kb": 3712, "score_of_the_acc": -0.003, "final_rank": 5 }, { "submission_id": "aoj_1675_10490333", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {1,0,-1,0,1,1,-1,-1};//x\nvector<ll> _yo = {0,1,0,-1,1,-1,1,-1};//y\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n\t\nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nvoid solve(ll n){\n\tLL(a,b,d);\n\tvpll xy(n);\n\trep(i,n){\n\t\tcin >> xy[i].first >> xy[i].second;\n\t}\t\n\tvvll obj(101,vll(101));\n\trep(i,n){\n\t\tauto [x,y] = xy[i];\n\t\tobj[x][y]++;\n\t}\n\tvector dp(61,vector(101,vector(101,vector<tpl3>(4,{INF,INF,INF}))));\n\n\trep(i,101)rep(j,101)if(obj[i][j]== 0){\n\t\trep(k,4){\n\t\t\tll ny = i +_ta[k];\n\t\t\tll nx = j +_yo[k];\n\t\t\tif(!isin(ny,nx,101,101)){\n\t\t\t\tdp[0][i][j][k] = {INF,INF,k};\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(obj[ny][nx] == 0){\n\t\t\t\tdp[0][i][j][k] = {ny,nx,k};\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tll nk =(k+1)%4;\n\t\t\tbool fin = false;\n\t\t\twhile(nk != k){\n\t\t\t\tny = i +_ta[nk];\n\t\t\t\tnx = j +_yo[nk];\n\t\t\t\tif(!isin(ny,nx,101,101)){\n\t\t\t\t\tdp[0][i][j][k] = {INF,INF,nk};\n\t\t\t\t\tfin = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(obj[ny][nx] == 0){\n\t\t\t\t\tdp[0][i][j][k] = {ny,nx,nk};\n\t\t\t\t\tfin = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnk++;\n\t\t\t\tnk %= 4;\n\t\t\t}\n\t\t\tif(!fin){\n\t\t\t\tdp[0][i][j][k] = {i,j,k};\n\t\t\t}\n\n\t\t}\n\t}\n\trep(z,60){\n\t\trep(i,101)rep(j,101)rep(k,4){\n\t\t\tauto [pi,pj,pk] = dp[z][i][j][k];\n\t\t\tif(pi == INF)continue;\n\t\t\tdp[z+1][i][j][k] = dp[z][pi][pj][pk];\n\t\t}\n\t}\n\t{\n\t\tll i = a,j = b;\n\t\tll k = 0;\n\t\trep(z,60){\n\t\t\tif(d >> z & 1){\n\t\t\t\tauto [ni,nj,nk] = dp[z][i][j][k];\n\t\t\t\ti = ni;\n\t\t\t\tj = nj;\n\t\t\t\tk = nk;\n\t\t\t\tif(i == INF )break;\n\t\t\t}\n\t\t}\n\t\tif(i != INF){\n\t\t\tcout << i << \" \" << j << endl; \n\t\t\treturn ;\n\t\t}\n\t}\n\tll ng = d;//条件を満たさない値\n\tll ok = 0;//条件を満たす値\n\tauto f = [&](ll mid)->bool{\n\t\tll i = a,j = b;\n\t\tll k = 0;\n\t\trep(z,60){\n\t\t\tif(mid>> z & 1){\n\t\t\t\tauto [ni,nj,nk] = dp[z][i][j][k];\n\t\t\t\ti = ni;\n\t\t\t\tj = nj;\n\t\t\t\tk = nk;\n\t\t\t\tif(i == INF )break;\n\t\t\t}\n\t\t}\n\t\treturn i != INF;\n\t};\n\twhile(abs(ok-ng) > 1){\n\t\tll mid = (ok + ng)/2;\n\t\tif(f(mid)){\n\t\t\tok = mid;\n\t\t}else{\n\t\t\tng = mid;\n\t\t}\n\t}\n\t//ok回までは行ける\n\tll i = a,j = b;\n\tll k = 0;\n\trep(z,60){\n\t\tif(ok >> z & 1){\n\t\t\tauto [ni,nj,nk] = dp[z][i][j][k];\n\t\t\ti = ni;\n\t\t\tj = nj;\n\t\t\tk = nk;\n\t\t}\n\t}\n\tll rem = d- ok;\n\t k = get<2>(dp[0][i][j][k]);\n\tcout << i + _ta[k]*rem << \" \" << j + _yo[k]*rem << endl;\n}\n\nint main(){\n\tios::sync_with_stdio(false);cin.tie(nullptr);\n\twhile(true){\n\t\tLL(n);\n\t\tif(n == 0)return 0;\n\t\tsolve(n);\n\t}\n}", "accuracy": 1, "time_ms": 6690, "memory_kb": 87768, "score_of_the_acc": -2, "final_rank": 20 } ]
aoj_2004_cpp
Data Center on Fire 今から少し未来の話である.高密度記憶および長期保存可能である記録デバイスが開発されてきたが,コンテンツが生み出される速度が異常なほどに速かったため,データセンターを設立した.データセンターを設立した当初は小さな建物だったが,増え続けるデータにあわせて何度も拡張工事をしたため,性能の異なるエレベータを何基も持つこととなった. ところが,データセンターで火災が発生した.火は一定時間だけ経過すると上の階ないし下の階に燃え広がる.また,火がついてから一定時間だけ経過するとその階は焼失してしまう.そこでエレベータを使ってデバイスを運び出すこととした.このエレベータは耐火性能が完璧であるため,階の焼失に関わらず任意の階へ移動できる.また,非常に強力な加速減速装置を持つことから,加速および減速にかかる時間は無視できるほど小さいため,一瞬にして定常速度に加速すること,あるいは停止することができる.また,緊急時においてはいわゆる「開」ボタンが機能しないようにプログラムされているため,エレベータの停止時間は運び出す(あるいは降ろす)デバイスの個数にかかわらず一定である.また,階の焼失前に到着したエレベータによって運び出されるデバイスについては,たとえエレベータの出発前に階が焼失したとしても,焼失の影響を受けない.ただし,エレベータに乗せることができなかったデバイスは当然ながら焼失する. エレベータはどの階から回収してよいか分からないため,デバイスのある最上階を目指すようにプログラムされている.ただし,エレベータ同士は通信可能であり,別のエレベータが目的階に到着した瞬間に情報が通信される.到着したエレベータに全てのデバイスが積みこめることがわかったときは,目的階よりも下にあり,回収可能なデバイスが残っている階のうちで最上階に目的階を変更する.また,焼失のために目的階に向かう必要性が失われたときも,同様にして目的階を変更する.目的階を変更するときに,移動方向を変更する必要があるときは,即座に移動方向を変更する.また,エレベータが満杯になってそれ以上のデバイスを積みこむことができないとき,あるいは回収可能なデバイスが残っていないときは 1 階を目指す. あなたの仕事は,上記の条件のもとで,退避させることのできたデバイスの個数および到着時刻を求めるプログラムを作成することである. Input 入力は複数のデータセットから構成される.それぞれのデータセットは次の形式で与えられる. N M d n 1 n 2 ... n N c 1 v 1 ts 1 x 1 c 2 v 2 ts 2 x 2 ... c M v M ts M x M k tx ty tz 記号の意味は次のとおりである.入力中の値はすべて整数である. N (2 <= N <= 30), M (1 <= M <= 10)はそれぞれビルの階数,エレベータの基数を表す. d (1000 <= d <= 10000)は階と階の間の距離を表す. n i (0 <= n i <= 100) は i 階にあるデバイスの個数を表す. c i (1 <= c i <= 50), v i (1 <= v i <= 2000), ts i (1 <= ts i <= 20), x i (1 <= x i <= N )はそれぞれ i 番目のエレベータの容量,速度,停止時間,初期位置をあらわす.初期位置は何階にあるかで表される. k (2 <= k <= N ), tx (30 <= tx <= 300), ty (30 <= ty <= 300), tz (30 <= tz <= 300)はそれぞれ火元の階,火がついてから焼失するまでの時間,上に燃え移るまでの時間,下に燃え移るまでの時間を表す. 入力の終了は 2 つの 0 を含む行によってあらわされる.これはデータセットの一部ではない. それぞれのデータセットは次の条件を満たすと仮定してよい. 単位時間の 1/1000 よりも短い時間の間に複数の階の消失が起こることはない. 単位時間の 1/1000 よりも短い時間の間に複数のエレベータが同一の階で 1 階より上にある階に到着することない. 単位時間の 1/1000 よりも短い時間の間にエレベータの到着とそのエレベータが到着しようとした階の焼失が起こることはない. Output それぞれのテストケースについて,回収されたデバイスの個数,および最後に回収されたデバイスが 1 階に到着してエレベータから降ろされるまでの時間を,単一の空白で区切って 1 行で出力しなさい.ただし,時間については,小数点以下に何個の数字を出力しても構わないが,0.001 を超える誤差を含めてはならない.また,1 階にあるデバイス以外は回収できなかったときは時間としてゼロを出力しなさい. Sample Input 5 2 5000 10 20 0 30 5 10 1000 6 1 20 500 8 1 3 40 25 30 0 0 Output for the Sample Input 50 84.000
[ { "submission_id": "aoj_2004_9173332", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 12\n\n\nenum Type{\n\tarrive,\n\tcarry_end,\n\tburn_start,\n\tburn_end,\n};\n\nstruct Box{\n\n\tint capa;\n\tdouble V,stop_time;\n};\n\nstruct Info{\n\tInfo(double arg_T,int arg_ind,int arg_toward,Type arg_type){\n\t\tT = arg_T;\n\t\tind = arg_ind;\n\t\ttoward = arg_toward;\n\t\ttype = arg_type;\n\t}\n\tInfo(){\n\n\t\tT = 0;\n\t\tind = toward = 0;\n\t\ttype = arrive;\n\t}\n\tbool operator<(const struct Info& arg) const{\n\n\t\treturn T > arg.T; //イベント発生時刻の昇順(PQ)\n\t}\n\n\tdouble T;\n\tint ind,toward;\n\tType type;\n};\n\nint N,M;\nint NUM[35],num_have[SIZE];\nbool is_up[SIZE],is_stop[SIZE],is_burn_start[35];\ndouble D;\nBox box[SIZE];\ndouble Y[SIZE],floorPOS[35];\n\nint K;\ndouble T_burn,T_up,T_under;\ndouble pre_T,now_T;\n\npriority_queue<Info> Q;\nbool end_FLG = false;\n\n\n//新しい目的階が決まった時に処理(Y[]の計算を前提としている)\nvoid calcElev(int to_floor){\n\n\tdouble toPOS = floorPOS[to_floor];\n\n\tfor(int i = 0; i < M; i++){\n\t\tif(is_stop[i])continue; //荷物の積み下ろし中\n\n\t\tdouble tmp_dist = fabs(toPOS-Y[i]);\n\n\t\tif(toPOS >= Y[i]){ //上にある時\n\n\t\t\tis_up[i] = true;\n\n\n\t\t}else{ //下にある時\n\n\t\t\tis_up[i] = false;\n\t\t}\n\n\t\tdouble tmp_T = now_T + tmp_dist/box[i].V;\n\n\t\tQ.push(Info(tmp_T,i,to_floor,arrive));\n\t}\n}\n\nvoid calcNowPos(){\n\n\tdouble diff = now_T-pre_T;\n\n\tfor(int i = 0; i < M; i++){\n\t\tif(is_stop[i])continue;\n\n\t\tif(is_up[i]){\n\n\t\t\tY[i] += box[i].V*diff;\n\t\t}else{\n\n\t\t\tY[i] -= box[i].V*diff;\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\t/* 初期化処理 */\n\twhile(!Q.empty()){\n\t\tQ.pop();\n\t}\n\n\tscanf(\"%lf\",&D);\n\n\tfloorPOS[1] = 0;\n\tNUM[1] = 0;\n\n\tfor(int i = 2; i <= N; i++){\n\n\t\tfloorPOS[i] = D*(i-1);\n\t}\n\n\tint next_floor = -1;\n\tint sum;\n\tscanf(\"%d\",&sum); //1階にある荷物は、運搬不要\n\n\tfor(int i = 2; i <= N; i++){\n\n\t\tscanf(\"%d\",&NUM[i]);\n\t\tif(NUM[i] > 0){\n\n\t\t\tnext_floor = i;\n\t\t}\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d %lf %lf %d\",&box[i].capa,&box[i].V,&box[i].stop_time,&tmp);\n\t\tY[i] = D*(tmp-1);\n\t}\n\n\tscanf(\"%d %lf %lf %lf\",&K,&T_burn,&T_up,&T_under);\n\n\tif(next_floor == -1){ //荷物なし\n\n\t\tprintf(\"%d 0.000000000000\\n\",sum);\n\t\treturn;\n\t}\n\n\tfor(int i = 1; i <= N; i++){\n\n\t\tis_burn_start[i] = false;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tis_stop[i] = false;\n\t\tnum_have[i] = 0;\n\t}\n\tpre_T = 0,now_T = 0;\n\n\tQ.push(Info(T_burn,K,-1,burn_end)); //K階が燃え尽きる時刻[\n\tis_burn_start[K] = true;\n\n\tif(K < N){\n\n\t\tQ.push(Info(T_up,K+1,-1,burn_start)); //K+1階が燃え始める時刻\n\t}\n\tif(K > 1){\n\n\t\tQ.push(Info(T_under,K-1,-1,burn_start)); //K-1階が燃え始める時刻\n\t}\n\n\t//各エレベーターの移動方向と、到着イベントを考える\n\tcalcElev(next_floor);\n\n\tdouble last_time = 0;\n\tend_FLG = false;\n\n\twhile(!Q.empty()){\n\n\t\tInfo info = Q.top();\n\t\tQ.pop();\n\n\t\tint pre_floor = info.toward;\n\n\t\tif(info.type == arrive && pre_floor != 1 && pre_floor != next_floor)continue; //■■現在は無効になった命令なので読み飛ばす\n\n\t\tpre_T = now_T;\n\t\tnow_T = info.T;\n\n\t\tcalcNowPos(); //各エレベーターの現在位置を計算\n\n\t\tif(info.type == arrive){\n\n\t\t\tif(pre_floor == 1){ //積み下ろしのために1階に来た場合\n\n\t\t\t\tif(num_have[info.ind] > 0){\n\n\t\t\t\t\tQ.push(Info(now_T+box[info.ind].stop_time,info.ind,1,carry_end));\n\t\t\t\t}\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//荷物を積むために、ある階に到着した場合\n\n\t\t\tis_stop[info.ind] = true; //移動指示を無視する\n\n\t\t\tQ.push(Info(now_T+box[info.ind].stop_time,info.ind,pre_floor,carry_end)); //積み込み終了イベントを追加\n\n\t\t\tint can_carry = box[info.ind].capa-num_have[info.ind]; //積み込める最大数\n\n\t\t\t//その階の荷物を全て積み切れるか判断する\n\n\t\t\tif(NUM[pre_floor] <= can_carry){ //全て積み込める\n\n\t\t\t\tnum_have[info.ind] += NUM[pre_floor];\n\t\t\t\tNUM[pre_floor] = 0;\n\n\t\t\t\tnext_floor -= 1;\n\n\t\t\t\twhile(next_floor > 0 && NUM[next_floor] == 0){\n\n\t\t\t\t\tnext_floor--;\n\t\t\t\t}\n\n\t\t\t\tif(next_floor == 0){\n\n\t\t\t\t\tnext_floor = 1; //■全ての荷物の運搬が完了\n\t\t\t\t\tend_FLG = true;\n\t\t\t\t}\n\n\t\t\t\t//他のエレベーターの、移動方向を変える\n\t\t\t\tcalcElev(next_floor);\n\n\t\t\t}else{ //積みきれない→満杯になる\n\n\t\t\t\tnum_have[info.ind] += can_carry;\n\t\t\t\tNUM[pre_floor] -= can_carry;\n\t\t\t}\n\n\t\t}else if(info.type == carry_end){ //積み込み終了、または積み下ろし完了\n\n\t\t\tif(pre_floor == 1){ //積み下ろしが完了した場合\n\n\t\t\t\tlast_time = now_T; //■■注意\n\t\t\t\tsum += num_have[info.ind];\n\t\t\t\tnum_have[info.ind] = 0;\n\n\t\t\t\tif(end_FLG)continue;\n\n\t\t\t\t//まだ荷物が残っている場合\n\t\t\t\tY[info.ind] = 0;\n\t\t\t\tis_stop[info.ind] = false;\n\n\t\t\t\tis_up[info.ind] = true;\n\n\t\t\t\tdouble add_T = floorPOS[next_floor]/box[info.ind].V;\n\n\t\t\t\tQ.push(Info(now_T+add_T,info.ind,next_floor,arrive));\n\n\n\t\t\t}else{ //積み込みが完了した場合\n\n\t\t\t\tif(end_FLG == true || num_have[info.ind] == box[info.ind].capa){ //満杯の場合→荷おろしのために1階に向かう\n\n\t\t\t\t\tdouble add_T = floorPOS[pre_floor]/box[info.ind].V;\n\n\t\t\t\t\tQ.push(Info(now_T+add_T,info.ind,1,arrive));\n\n\t\t\t\t}else{ //まだ荷物がある、かつ満杯でない場合→next_floorに向かう\n\n\t\t\t\t\tY[info.ind] = floorPOS[pre_floor];\n\t\t\t\t\tis_stop[info.ind] = false; //移動命令を受け付けるようにする\n\n\t\t\t\t\tis_up[info.ind] = (next_floor > pre_floor);\n\n\t\t\t\t\tdouble add_T = fabs(floorPOS[next_floor]-floorPOS[pre_floor])/box[info.ind].V;\n\t\t\t\t\tQ.push(Info(now_T+add_T,info.ind,next_floor,arrive));\n\t\t\t\t}\n\t\t\t}\n\n\t\t}else if(info.type == burn_start){ //ある階が燃え始めた場合\n\n\t\t\tQ.push(Info(now_T+T_burn,info.ind,-1,burn_end)); //焼失する時刻\n\t\t\tif(info.ind+1 <= N && !is_burn_start[info.ind+1]){\n\n\t\t\t\tQ.push(Info(now_T+T_up,info.ind+1,-1,burn_start));\n\t\t\t\tis_burn_start[info.ind+1] = true;\n\t\t\t}\n\t\t\tif(info.ind > 1 && !is_burn_start[info.ind-1]){\n\n\t\t\t\tQ.push(Info(now_T+T_under,info.ind-1,-1,burn_start));\n\t\t\t\tis_burn_start[info.ind-1] = true;\n\t\t\t}\n\n\t\t}else if(info.type == burn_end){ //焼失した場合\n\n\t\t\tNUM[info.ind] = 0; //運び入れられてない荷物を削除\n\n\t\t\tif(info.ind == next_floor){ //焼失した階が、現在の目的地になっている場合\n\n\t\t\t\tnext_floor -= 1;\n\t\t\t\twhile(next_floor > 0 && NUM[next_floor] == 0){\n\n\t\t\t\t\tnext_floor--;\n\t\t\t\t}\n\n\t\t\t\tif(next_floor == 0){\n\n\t\t\t\t\tnext_floor = 1; //■全ての荷物の運搬が完了\n\t\t\t\t\tend_FLG = true;\n\t\t\t\t}\n\t\t\t\t//他のエレベーターの、移動方向を変える\n\t\t\t\tcalcElev(next_floor);\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d %.12lf\\n\",sum,last_time);\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3236, "score_of_the_acc": -0.9775, "final_rank": 6 }, { "submission_id": "aoj_2004_9173321", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 12\n\n\nenum Type{\n\tarrive,\n\tcarry_end,\n\tburn_start,\n\tburn_end,\n};\n\nstruct Box{\n\n\tint capa;\n\tdouble V,stop_time;\n};\n\nstruct Info{\n\tInfo(double arg_T,int arg_ind,int arg_toward,Type arg_type){\n\t\tT = arg_T;\n\t\tind = arg_ind;\n\t\ttoward = arg_toward;\n\t\ttype = arg_type;\n\t}\n\tInfo(){\n\n\t\tT = 0;\n\t\tind = toward = 0;\n\t\ttype = arrive;\n\t}\n\tbool operator<(const struct Info& arg) const{\n\n\t\treturn T > arg.T; //イベント発生時刻の昇順(PQ)\n\t}\n\n\tdouble T;\n\tint ind,toward;\n\tType type;\n};\n\nint N,M;\nint NUM[35],num_have[SIZE];\nbool is_up[SIZE],is_stop[SIZE],is_burn_start[35],is_burn_end[35];\ndouble D;\nBox box[SIZE];\ndouble Y[SIZE],floorPOS[35];\n\nint K;\ndouble T_burn,T_up,T_under;\ndouble pre_T,now_T;\n\npriority_queue<Info> Q;\nbool end_FLG = false;\n\nbool DEBUG = true;\n\n//新しい目的階が決まった時に処理(Y[]の計算を前提としている)\nvoid calcElev(int to_floor){\n\n\tdouble toPOS = floorPOS[to_floor];\n\n\tfor(int i = 0; i < M; i++){\n\t\tif(is_stop[i])continue; //荷物の積み下ろし中\n\n\t\tdouble tmp_dist = fabs(toPOS-Y[i]);\n\n\t\tif(toPOS >= Y[i]){ //上にある時\n\n\t\t\tis_up[i] = true;\n\n\n\t\t}else{ //下にある時\n\n\t\t\tis_up[i] = false;\n\t\t}\n\n\t\tdouble tmp_T = now_T + tmp_dist/box[i].V;\n\t\t/*if(DEBUG){\n\n\t\t\tprintf(\"i:%d tmp_T:%.3lf\\n\",i,tmp_T);\n\t\t}*/\n\n\t\tQ.push(Info(tmp_T,i,to_floor,arrive));\n\t}\n}\n\nvoid calcNowPos(){\n\n\tdouble diff = now_T-pre_T;\n\n\tfor(int i = 0; i < M; i++){\n\t\tif(is_stop[i])continue;\n\n\t\tif(is_up[i]){\n\n\t\t\tY[i] += box[i].V*diff;\n\t\t}else{\n\n\t\t\tY[i] -= box[i].V*diff;\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\t/* 初期化処理 */\n\twhile(!Q.empty()){\n\t\tQ.pop();\n\t}\n\n\tscanf(\"%lf\",&D);\n\n\tfloorPOS[1] = 0;\n\tNUM[1] = 0;\n\n\tfor(int i = 2; i <= N; i++){\n\n\t\tfloorPOS[i] = D*(i-1);\n\t}\n\n\tint next_floor = -1;\n\tint sum;\n\tscanf(\"%d\",&sum); //1階にある荷物は、運搬不要\n\n\tfor(int i = 2; i <= N; i++){\n\n\t\tscanf(\"%d\",&NUM[i]);\n\t\tif(NUM[i] > 0){\n\n\t\t\tnext_floor = i;\n\t\t}\n\t}\n\n\n\tint tmp;\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d %lf %lf %d\",&box[i].capa,&box[i].V,&box[i].stop_time,&tmp);\n\t\tY[i] = D*(tmp-1);\n\t\t/*if(DEBUG){\n\n\t\t\tprintf(\"Y[%d]:%.3lf\\n\",i,Y[i]);\n\t\t}*/\n\t}\n\n\tscanf(\"%d %lf %lf %lf\",&K,&T_burn,&T_up,&T_under);\n\n\tif(next_floor == -1){ //荷物なし\n\n\t\t\tprintf(\"%d 0.000000000000\\n\",sum);\n\t\t\treturn;\n\t\t}\n\n\tfor(int i = 1; i <= N; i++){\n\n\t\tis_burn_start[i] = false;\n\t\tis_burn_end[i] = false;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tis_stop[i] = false;\n\t\tnum_have[i] = 0;\n\t}\n\tpre_T = 0,now_T = 0;\n\n\tQ.push(Info(T_burn,K,-1,burn_end)); //K階が燃え尽きる時刻[\n\tis_burn_start[K] = true;\n\n\tif(K < N){\n\n\t\tQ.push(Info(T_up,K+1,-1,burn_start)); //K+1階が燃え始める時刻\n\t}\n\tif(K > 1){\n\n\t\tQ.push(Info(T_under,K-1,-1,burn_start)); //K-1階が燃え始める時刻\n\t}\n\n\t//各エレベーターの移動方向と、到着イベントを考える\n\tcalcElev(next_floor);\n\n\tdouble last_time = 0;\n\tend_FLG = false;\n\n\twhile(!Q.empty()){\n\n\t\tInfo info = Q.top();\n\t\tQ.pop();\n\n\t\t/*if(DEBUG){\n\n\t\t\tprintf(\"\\n◎◎◎ T:%.3lf next_floor:%d\\n\",info.T,next_floor);\n\t\t\tfor(int i = 1; i <= N; i++){\n\n\t\t\t\tprintf(\" %d\",NUM[i]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\n\n\t\t\tswitch(info.type){\n\t\t\tcase arrive:\n\t\t\t\tprintf(\"到着\\n\");\n\t\t\t\tbreak;\n\t\t\tcase carry_end:\n\t\t\t\tprintf(\"carry_end\\n\");\n\t\t\t\tbreak;\n\t\t\tcase burn_start:\n\t\t\t\tprintf(\"延焼開始\\n\");\n\t\t\t\tbreak;\n\t\t\tcase burn_end:\n\t\t\t\tprintf(\"焼失\\n\");\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tprintf(\"ind:%d\\n\",info.ind);\n\t\t}*/\n\n\t\tint pre_floor = info.toward;\n\n\t\tif(info.type == arrive && pre_floor != 1 && pre_floor != next_floor)continue; //■■現在は無効になった命令なので読み飛ばす\n\n\t\tpre_T = now_T;\n\t\tnow_T = info.T;\n\n\n\t\t/*if(now_T-pre_T < EPS){\n\n\t\t\tprintf(\"BUG\\n\");\n\t\t\treturn;\n\t\t}*/\n\n\t\tcalcNowPos(); //各エレベーターの現在位置を計算\n\n\n\n\t\tif(info.type == arrive){\n\n\t\t\tif(pre_floor == 1){ //積み下ろしのために1階に来た場合\n\n\t\t\t\tif(num_have[info.ind] > 0){\n\n\t\t\t\t\tQ.push(Info(now_T+box[info.ind].stop_time,info.ind,1,carry_end));\n\t\t\t\t}\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t/*if(is_burn_end[pre_floor]){\n\n\t\t\t\tprintf(\"■焼失した階に到着 elev:%d pre_floor:%d next_floor:%d\\n\",info.ind,pre_floor,next_floor);\n\t\t\t\tif(!end_FLG){\n\n\t\t\t\t\tprintf(\"実終了\\n\");\n\t\t\t\t}else{\n\n\t\t\t\t\tprintf(\"終了\\n\");\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}*/\n\n\t\t\t//荷物を積むために、ある階に到着した場合\n\n\t\t\tis_stop[info.ind] = true; //移動指示を無視する\n\n\t\t\tQ.push(Info(now_T+box[info.ind].stop_time,info.ind,pre_floor,carry_end)); //積み込み終了イベントを追加\n\n\t\t\tint can_carry = box[info.ind].capa-num_have[info.ind]; //積み込める最大数\n\n\t\t\t//その階の荷物を全て積み切れるか判断する\n\n\t\t\tif(NUM[pre_floor] <= can_carry){ //全て積み込める\n\n\t\t\t\tnum_have[info.ind] += NUM[pre_floor];\n\t\t\t\tNUM[pre_floor] = 0;\n\n\t\t\t\t//printf(\"■■全て積み込めるので、next_floorを1引きます elev:%d 階:%d\\n\",info.ind,pre_floor);\n\n\t\t\t\tnext_floor -= 1;\n\n\t\t\t\twhile(next_floor > 0 && NUM[next_floor] == 0){\n\n\t\t\t\t\tnext_floor--;\n\t\t\t\t}\n\n\t\t\t\tif(next_floor == 0){\n\n\t\t\t\t\tnext_floor = 1; //■全ての荷物の運搬が完了\n\t\t\t\t\tend_FLG = true;\n\t\t\t\t}\n\n\t\t\t\t//他のエレベーターの、移動方向を変える\n\t\t\t\tcalcElev(next_floor);\n\n\t\t\t}else{ //積みきれない→満杯になる\n\n\t\t\t\tnum_have[info.ind] += can_carry;\n\t\t\t\tNUM[pre_floor] -= can_carry;\n\t\t\t}\n\n\t\t}else if(info.type == carry_end){ //積み込み終了、または積み下ろし完了\n\n\t\t\tif(pre_floor == 1){ //積み下ろしが完了した場合\n\n\t\t\t\t//printf(\"■■エレベーター%dが積み下ろしに来ました 荷物:%d next_floor:%d\\n\",info.ind,num_have[info.ind],next_floor);\n\n\t\t\t\tlast_time = now_T; //■■注意\n\t\t\t\tsum += num_have[info.ind];\n\t\t\t\tnum_have[info.ind] = 0;\n\n\t\t\t\tif(end_FLG)continue;\n\n\t\t\t\t//まだ荷物が残っている場合\n\t\t\t\tY[info.ind] = 0;\n\t\t\t\tis_stop[info.ind] = false;\n\n\t\t\t\tis_up[info.ind] = true;\n\n\t\t\t\tdouble add_T = floorPOS[next_floor]/box[info.ind].V;\n\n\t\t\t\tQ.push(Info(now_T+add_T,info.ind,next_floor,arrive));\n\n\n\t\t\t}else{ //積み込みが完了した場合\n\n\n\n\t\t\t\tif(end_FLG == true || num_have[info.ind] == box[info.ind].capa){ //満杯の場合→荷おろしのために1階に向かう\n\n\t\t\t\t\tdouble add_T = floorPOS[pre_floor]/box[info.ind].V;\n\n\t\t\t\t\tQ.push(Info(now_T+add_T,info.ind,1,arrive));\n\n\t\t\t\t}else{ //まだ荷物がある、かつ満杯でない場合→next_floorに向かう\n\n\t\t\t\t\tY[info.ind] = floorPOS[pre_floor];\n\t\t\t\t\tis_stop[info.ind] = false; //移動命令を受け付けるようにする\n\n\t\t\t\t\tis_up[info.ind] = (next_floor > pre_floor);\n\n\t\t\t\t\tdouble add_T = fabs(floorPOS[next_floor]-floorPOS[pre_floor])/box[info.ind].V;\n\t\t\t\t\tQ.push(Info(now_T+add_T,info.ind,next_floor,arrive));\n\t\t\t\t}\n\t\t\t}\n\n\t\t}else if(info.type == burn_start){ //ある階が燃え始めた場合\n\n\t\t\tQ.push(Info(now_T+T_burn,info.ind,-1,burn_end)); //焼失する時刻\n\t\t\tif(info.ind+1 <= N && !is_burn_start[info.ind+1]){\n\n\t\t\t\tQ.push(Info(now_T+T_up,info.ind+1,-1,burn_start));\n\t\t\t\tis_burn_start[info.ind+1] = true;\n\t\t\t}\n\t\t\tif(info.ind > 1 && !is_burn_start[info.ind-1]){\n\n\t\t\t\tQ.push(Info(now_T+T_under,info.ind-1,-1,burn_start));\n\t\t\t\tis_burn_start[info.ind-1] = true;\n\t\t\t}\n\n\t\t}else if(info.type == burn_end){ //焼失した場合\n\n\t\t\tNUM[info.ind] = 0; //運び入れられてない荷物を削除\n\n\t\t\tis_burn_end[info.ind] = true;\n\n\t\t\t//changeToward(info.ind); //この階に向かっているエレベーターがあれば、別のフロアに異動させる\n\t\t\t//printf(\"■■%d階が焼失 next_floor:%d\\n\",info.ind,next_floor);\n\n\t\t\tif(info.ind == next_floor){ //焼失した階が、現在の目的地になっている場合\n\n\t\t\t\t//printf(\"■■今、焼失階[%d]はnext_floorです\\n\",info.ind);\n\n\t\t\t\tnext_floor -= 1;\n\t\t\t\twhile(next_floor > 0 && NUM[next_floor] == 0){\n\n\t\t\t\t\tnext_floor--;\n\t\t\t\t}\n\n\t\t\t\tif(next_floor == 0){\n\n\t\t\t\t\tnext_floor = 1; //■全ての荷物の運搬が完了\n\t\t\t\t\tend_FLG = true;\n\t\t\t\t}\n\t\t\t\t//他のエレベーターの、移動方向を変える\n\t\t\t\tcalcElev(next_floor);\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d %.12lf\\n\",sum,last_time);\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.9413, "final_rank": 5 }, { "submission_id": "aoj_2004_2314325", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing db = double;\nusing ll = long long;\nusing vi = vector <int>;\n#define op operator\n#define pb push_back\n\nconst db eps = 1e-6L;\nint sgn(db a, db b = 0) {\n\ta -= b;\n\treturn (a > eps) - (a < -eps);\n}\n\nconst int N = 33;\nint n, m; db d;\nint device[N];\nint capacity[N], full_capacity[N];\nint speed[N];\ndb wait_time[N], full_wait_time[N];\ndb pos[N];\n\nint burn[N];\n\nint cnt;\ndb timer, last_collect_time;\n\nconst db inf = 1e10L;\nint goal;\ndb check(int i) {\n\tif(sgn(wait_time[i]) > 0) return wait_time[i];\n\tif(!capacity[i]) return (pos[i] - d) / speed[i];\n\tif(goal != -1) return abs(pos[i] - goal * d) / speed[i];\n\tif(sgn(pos[i], d) != 0) return (pos[i] - d) / speed[i];\n\treturn inf;\n}\n\nvoid solve(int i, db t) {\n\tif(sgn(wait_time[i]) > 0)\n\t\twait_time[i] -= t;\n\telse if(!capacity[i]) {\n\t\tpos[i] -= speed[i] * t;\n\t\tif(sgn(pos[i], d) == 0) {\n\t\t\twait_time[i] = full_wait_time[i];\n\t\t\tcapacity[i] = full_capacity[i];\n\t\t\tlast_collect_time = max(last_collect_time, timer + t + wait_time[i]);\n\t\t}\n\t} else if(goal != -1) {\n\t\tpos[i] += sgn(goal * d - pos[i]) * speed[i] * t;\n\t\tif(sgn(pos[i], goal * d) == 0) {\n\t\t\twait_time[i] = full_wait_time[i];\n\t\t\tint collect = min(capacity[i], device[goal]);\n\t\t\t//cout << capacity[i] << ' ' << device[goal] << '\\n';\n\t\t\tcapacity[i] -= collect;\n\t\t\tdevice[goal] -= collect;\n\t\t\tcnt += collect;\n\t\t}\n\t} else if(sgn(pos[i], d) != 0) {\n\t\tpos[i] -= speed[i] * t;\n\t\tif(sgn(pos[i], d) == 0 && capacity[i] != full_capacity[i]) {\n\t\t\twait_time[i] = full_wait_time[i];\n\t\t\tcapacity[i] = full_capacity[i];\n\t\t\tlast_collect_time = max(last_collect_time, timer + t + wait_time[i]);\n\t\t}\n\t}\n}\n\nint main() {\n\tcout << fixed << setprecision(9);\n\tios :: sync_with_stdio(0);\n\n\twhile(cin >> n >> m && n && m) {\n\t\tcin >> d;\n\t\tfor(int i = 1; i <= n; i ++)\n\t\t\tcin >> device[i];\n\t\tcnt = device[1]; device[1] = 0;\n\t\ttimer = last_collect_time = 0;\n\n\t\tfor(int i = 1; i <= m; i ++) {\n\t\t\tcin >> full_capacity[i] >> speed[i] >> full_wait_time[i] >> pos[i];\n\t\t\tcapacity[i] = full_capacity[i];\n\t\t\twait_time[i] = 0;\n\t\t\tpos[i] *= d;\n\t\t}\n\n\t\tint k, tx, ty, tz; cin >> k >> tx >> ty >> tz;\n\t\tburn[k] = tx;\n\t\tfor(int i = k + 1; i <= n; i ++)\n\t\t\tburn[i] = (i - k) * ty + tx;\n\t\tfor(int i = 1; i < k; i ++)\n\t\t\tburn[i] = (k - i) * tz + tx;\n\n\t\tfor(;;) {\n\t\t\t//cout << \"------\\n\";\n\t\t\tgoal = -1;\n\t\t\tfor(int i = 1; i <= n; i ++)\n\t\t\t\tif(device[i]) goal = i;\n\t\t\tdb nxt = inf;\n\t\t\t//cout << goal << ' ' << timer << '\\n';\n\t\t\t//for(int i = 1; i <= n; i ++) cout << device[i] << ' '; cout << '\\n';\n\t\t\tfor(int i = 1; i <= n; i ++)\n\t\t\t\tif(sgn(burn[i], timer) > 0)\n\t\t\t\t\tnxt = min(nxt, burn[i] - timer);\n\t\t\tfor(int i = 1; i <= m; i ++) {\n\t\t\t\tnxt = min(nxt, check(i));\n\t\t\t}\n\n\t\t\tif(nxt != inf)\n\t\t\t\tfor(int i = 1; i <= m; i ++)\n\t\t\t\t\tsolve(i, nxt);\n\t\t\telse\n\t\t\t\tbreak;\n\n\t\t\ttimer += nxt;\n\t\t\tfor(int i = 1; i <= n; i ++)\n\t\t\t\tif(sgn(burn[i], timer) == 0) device[i] = 0;\n\t\t}\n\t\tcout << cnt << ' ' << last_collect_time << '\\n';\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3288, "score_of_the_acc": -1.0625, "final_rank": 8 }, { "submission_id": "aoj_2004_1024860", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define chmin(a,b) (a) = min((a),(b))\n#define chmax(a,b) (a) = max((a),(b))\nusing namespace std;\ntypedef double D;\nconst D EPS = 1e-8;\n\nstruct elevator{\n int cap,rem;\n D v,ts,pos,stop;\n elevator(int a,D b,D c,D d):\n cap(a),v(b),ts(c),pos(d){rem = a; stop=-1;}\n \n D reachTime(D goal){\n if(stop>-EPS)return ts - stop;\n if(rem == 0)return pos/v;\n return abs(goal - pos)/v;\n }\n\n int move(D goal, D t){\n if(stop>-EPS){\n stop += t;\n if(EQ(stop,ts))stop = -1;\n return 0;\n }\n\n if(rem==0)goal = 0;\n pos += (goal>pos)?v*t:-v*t;\n if(EQ(goal,pos)){\n stop = 0;\n if(EQ(pos,0)){\n\tint res = cap-rem;\n\trem = cap;\n\treturn res;\n }\n }\n return 0;\n }\n};\n\nint main(){\n int N,M;\n\n while(cin >> N >> M, N){\n D d;\n cin >> d;\n bool flag = false;\n\n vector<int> device(N);\n rep(i,N)cin >> device[i];\n\n vector<elevator> el;\n rep(i,M){\n int c,v,ts,x;\n cin >> c >> v >> ts >> x;\n el.push_back(elevator(c,v,ts,d*(x-1)));\n }\n\n D k,tx,ty,tz;\n cin >> k >> tx >> ty >> tz; k--;\n vector<D> burn(N,-1);\n burn[k] = tx;\n int uf = k+1, df = k-1;\n D nxtu = ty, nxtd = tz;\n\n int ans = device[0];\n D curTime = 0, lastTime = 0;\n for(;;){\n int goal = -1;\n rep(i,N)if(device[i])goal = i;\n if(goal<1)break;\n\n D nxtTime = 1e9;\n rep(i,N){\n\tif(burn[i]>-EPS)chmin(nxtTime, burn[i]);\n }\n if(uf<N)chmin(nxtTime,nxtu);\n if(df>=0)chmin(nxtTime,nxtd);\n\n rep(i,M)chmin(nxtTime, el[i].reachTime(d*goal) );\n\n curTime += nxtTime;\n\n rep(i,N){\n\tif(burn[i]>-EPS){\n\t burn[i] -= nxtTime;\n\t if(EQ(burn[i],0)){\n\t device[i] = 0;\n\t burn[i] = -1;\n\t }\n\t}\n }\n\n nxtu -= nxtTime;\n if(EQ(nxtu,0)){\n\tif(uf<N)burn[uf] = tx;\n\tuf++; nxtu = ty;\n }\n\n nxtd -= nxtTime;\n if(EQ(nxtd,0)){\n\tif(df>=0)burn[df] = tx;\n\tdf--; nxtd = tz;\n }\n\n rep(i,M){\n\tint num = el[i].move(d*goal,nxtTime);\n\tif(num){\n\t ans += num; chmax(lastTime, curTime + el[i].ts);\n\t}\n\n\tif(EQ(el[i].pos,d*goal) && EQ(el[i].stop,0)){\n\t int tmp = min(el[i].rem, device[goal]);\n\t el[i].rem -= tmp; device[goal] -= tmp;\n\t if(tmp && goal)flag = true;\n\t}\n }\n }\n\n rep(i,M){\n if(el[i].rem != el[i].cap){\n\tans += el[i].cap - el[i].rem;\n\tD nxtTime = el[i].pos/el[i].v + el[i].ts;\n\tif(el[i].stop>-EPS)nxtTime += el[i].ts-el[i].stop;\n chmax(lastTime, curTime + nxtTime);\n }\n }\n cout << fixed << setprecision(6) << ans << \" \"\n \t << (flag?lastTime:0.0) << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1308, "score_of_the_acc": -0.3326, "final_rank": 4 }, { "submission_id": "aoj_2004_474233", "code_snippet": "#include <algorithm>\n#include <numeric>\n#include <queue>\n#include <iostream>\n#include <iomanip>\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < (n); ++i)\n\nstruct Event {\n double time;\n function<void ()> proc;\n\n Event(double time, function<void ()> proc): time(time), proc(proc) {}\n bool operator<(const Event& rhs) const { return rhs.time < time; }\n};\n\nstruct Elevator {\n int capacity, speed, stopTime, target, size, rev, dir;\n double pos, time;\n\n Elevator(): size(0), rev(0), dir(1), time(0) {}\n};\n\nint floorCount, elevatorCount, floorDistance;\nint initFireFloor, burningTime, upSpreadTime, downSpreadTime;\nvector<Elevator> elevators;\nvector<int> deviceCount;\nvector<bool> floorBurned;\npriority_queue<Event> Q;\ndouble ansTime;\nint ansCount;\n\nvoid changeTarget(double time, int elevator, int stopTime);\n\nint newTarget(int elevator, int currentTarget) {\n Elevator& e = elevators[elevator];\n if (e.size == e.capacity)\n return 0;\n for (int i = currentTarget - 1; i > 0; --i)\n if (!floorBurned[i] && deviceCount[i] > 0)\n return i;\n return 0;\n}\n\nvoid onReach(double time, int floor, int elevator, int rev) {\n Elevator& e = elevators[elevator];\n if (rev < e.rev) return;\n\n int n = min(deviceCount[floor], e.capacity - e.size);\n deviceCount[floor] -= n;\n e.size += n;\n if (floor == 0) {\n if (e.size > 0) {\n ansTime = max(ansTime, time + e.stopTime);\n ansCount += e.size;\n e.size = 0;\n e.target = floorCount;\n changeTarget(time, elevator, e.stopTime);\n }\n } else {\n if (deviceCount[floor] == 0) {\n REP(i, elevatorCount)\n if (i != elevator && elevators[i].target == floor)\n changeTarget(time, i, 0);\n }\n changeTarget(time, elevator, e.stopTime);\n }\n}\n\nvoid onBurned(double time, int floor) {\n floorBurned[floor] = true;\n REP(i, elevatorCount)\n if (elevators[i].target == floor)\n changeTarget(time, i, 0);\n}\n\nvoid changeTarget(double time, int elevator, int stopTime) {\n Elevator& e = elevators[elevator];\n int nextTarget = newTarget(elevator, e.target);\n if (e.target == nextTarget) return;\n e.target = nextTarget;\n e.pos += e.dir * (time - e.time) * e.speed;\n e.time = time + stopTime;\n double dist = e.target * floorDistance - e.pos;\n e.dir = dist >= 0 ? 1 : -1;\n e.rev += 1;\n double newTime = time + stopTime + fabs(dist) / e.speed;\n Q.push(Event(newTime, bind(onReach, newTime, e.target, elevator, e.rev)));\n}\n\nint main() {\n while (cin >> floorCount >> elevatorCount, floorCount | elevatorCount) {\n deviceCount.resize(floorCount);\n elevators.assign(elevatorCount, Elevator());\n floorBurned.assign(floorCount, false);\n\n cin >> floorDistance;\n REP(i, floorCount) cin >> deviceCount[i];\n REP(i, elevatorCount) {\n Elevator& e = elevators[i];\n cin >> e.capacity >> e.speed >> e.stopTime >> e.pos;\n e.pos = (e.pos - 1) * floorDistance;\n e.target = floorCount;\n }\n cin >> initFireFloor >> burningTime >> upSpreadTime >> downSpreadTime; --initFireFloor;\n\n ansTime = 0;\n ansCount = deviceCount[0];\n deviceCount[0] = 0;\n REP(floor, floorCount) {\n double t = floor >= initFireFloor ? upSpreadTime * (floor - initFireFloor) + burningTime\n : downSpreadTime * (initFireFloor - floor) + burningTime;\n Q.push(Event(t, bind(onBurned, t, floor)));\n }\n REP(i, elevatorCount) changeTarget(0, i, 0);\n\n for (; !Q.empty(); Q.pop()) { Q.top().proc(); }\n\n cout << setprecision(20);\n cout << ansCount << \" \" << ansTime << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1348, "score_of_the_acc": -0.2873, "final_rank": 3 }, { "submission_id": "aoj_2004_474223", "code_snippet": "#include <algorithm>\n#include <queue>\n#include <iostream>\n#include <numeric>\n#include <iomanip>\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < (n); ++i)\n\nstruct Event {\n double time;\n function<void ()> proc;\n\n Event(double time, function<void ()> proc): time(time), proc(proc) {}\n bool operator<(const Event& rhs) const { return rhs.time < time; }\n};\n\nint floorCount, elevatorCount, floorDistance;\nint initFireFloor, burningTime, upSpreadTime, downSpreadTime;\nvector<int> deviceCount;\nvector<int> elevatorSize;\nvector<int> elevatorCapacity;\nvector<int> elevatorSpeed;\nvector<int> elevatorStopTime;\nvector<double> elevatorPos;\nvector<double> elevatorTime;\nvector<int> elevatorRev;\nvector<int> elevatorTarget;\nvector<int> elevatorDir;\nvector<bool> floorBurned;\ndouble ansTime;\nint ansCount;\n\nvoid changeTarget(priority_queue<Event>& Q, double time, int elevator, int stopTime);\n\nint newTarget(int elevator, int currentTarget) {\n if (elevatorSize[elevator] == elevatorCapacity[elevator])\n return 0;\n for (int i = currentTarget - 1; i > 0; --i)\n if (!floorBurned[i] && deviceCount[i] > 0)\n return i;\n return 0;\n}\n\nvoid onReach(priority_queue<Event>& Q, double time, int floor, int elevator, int rev) {\n if (rev < elevatorRev[elevator]) return;\n\n int n = min(deviceCount[floor], elevatorCapacity[elevator] - elevatorSize[elevator]);\n deviceCount[floor] -= n;\n elevatorSize[elevator] += n;\n if (floor == 0) {\n if (elevatorSize[elevator] > 0) {\n ansTime = max(ansTime, time + elevatorStopTime[elevator]);\n ansCount += elevatorSize[elevator];\n elevatorSize[elevator] = 0;\n elevatorTarget[elevator] = floorCount;\n changeTarget(Q, time, elevator, elevatorStopTime[elevator]);\n }\n } else {\n if (deviceCount[floor] == 0) {\n REP(i, elevatorCount)\n if (i != elevator && elevatorTarget[i] == floor)\n changeTarget(Q, time, i, 0);\n }\n changeTarget(Q, time, elevator, elevatorStopTime[elevator]);\n }\n}\n\nvoid onBurned(priority_queue<Event>& Q, double time, int floor) {\n floorBurned[floor] = true;\n REP(i, elevatorCount)\n if (elevatorTarget[i] == floor)\n changeTarget(Q, time, i, 0);\n}\n\nvoid changeTarget(priority_queue<Event>& Q, double time, int elevator, int stopTime) {\n int nextTarget = newTarget(elevator, elevatorTarget[elevator]);\n if (elevatorTarget[elevator] == nextTarget) return;\n elevatorTarget[elevator] = nextTarget;\n elevatorPos[elevator] += elevatorDir[elevator] * (time - elevatorTime[elevator]) * elevatorSpeed[elevator];\n elevatorTime[elevator] = time + stopTime;\n double dist = elevatorTarget[elevator] * floorDistance - elevatorPos[elevator];\n elevatorDir[elevator] = dist >= 0 ? 1 : -1;\n elevatorRev[elevator] += 1;\n double newTime = time + stopTime + fabs(dist) / elevatorSpeed[elevator];\n Q.push(Event(newTime, bind(onReach, ref(Q), newTime, elevatorTarget[elevator], elevator, elevatorRev[elevator])));\n}\n\nint main() {\n while (cin >> floorCount >> elevatorCount, floorCount | elevatorCount) {\n deviceCount.resize(floorCount);\n elevatorCapacity.resize(elevatorCount);\n elevatorSpeed.resize(elevatorCount);\n elevatorStopTime.resize(elevatorCount);\n elevatorPos.resize(elevatorCount);\n\n cin >> floorDistance;\n REP(i, floorCount) cin >> deviceCount[i];\n REP(i, elevatorCount) cin >> elevatorCapacity[i] >> elevatorSpeed[i] >> elevatorStopTime[i] >> elevatorPos[i];\n REP(i, elevatorCount) elevatorPos[i] = (elevatorPos[i] - 1) * floorDistance;\n cin >> initFireFloor >> burningTime >> upSpreadTime >> downSpreadTime;\n --initFireFloor;\n\n floorBurned.assign(floorCount, false);\n elevatorSize.assign(elevatorCount, 0);\n elevatorTime.assign(elevatorCount, 0.0);\n elevatorRev.assign(elevatorCount, 0);\n elevatorDir.assign(elevatorCount, 1);\n elevatorTarget.assign(elevatorCount, floorCount);\n\n priority_queue<Event> Q;\n ansTime = 0;\n ansCount = deviceCount[0];\n deviceCount[0] = 0;\n REP(floor, floorCount) {\n double t = floor >= initFireFloor ? upSpreadTime * (floor - initFireFloor) + burningTime\n : downSpreadTime * (initFireFloor - floor) + burningTime;\n Q.push(Event(t, bind(onBurned, ref(Q), t, floor)));\n }\n REP(i, elevatorCount) changeTarget(Q, 0, i, 0);\n\n while (!Q.empty()) { Q.top().proc(); Q.pop(); }\n\n cout << setprecision(20);\n cout << ansCount << \" \" << ansTime << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1344, "score_of_the_acc": -0.2856, "final_rank": 2 }, { "submission_id": "aoj_2004_414010", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n \nconst int arrive = 0;\nconst int burnout = 1;\nconst int gfloor = 2;\n \nconst int dy[2] = {1, -1};\n \nclass Elevator\n{\npublic:\n int dev, cap, v, ts;\n double pos, stime;\n \n Elevator()\n {}\n \n Elevator(double pos, double stime, int dev, int cap, int v, int ts)\n :pos(pos),stime(stime),dev(dev),cap(cap),v(v),ts(ts)\n {}\n};\n \nclass Event\n{\npublic:\n int type, bpos;\n double time;\n \n Elevator e;\n \n Event(int type, int bpos, double time)\n :type(type), bpos(bpos), time(time)\n {}\n \n Event(int type, int bpos, double time, Elevator e)\n :type(type),bpos(bpos),time(time),e(e)\n {}\n \n bool operator<(const Event& e) const\n {\n return time > e.time;\n }\n};\n \nint N,M,K,T[3],devices[33];\nbool burn[33];\ndouble D[33];\n \nbool limit(Elevator& e)\n{\n return e.cap == e.dev;\n}\n \ndouble arvTime(int dst, Elevator& e, double t)\n{\n double res = (max(e.pos, D[dst]) - min(e.pos, D[dst])) / e.v;\n \n return t + res;\n}\n \nElevator arrived(Event& e)\n{\n int n = min(devices[e.bpos], e.e.cap - e.e.dev);\n devices[e.bpos] -= n;\n \n Elevator res = Elevator(D[e.bpos], e.time + e.e.ts, e.e.dev+n, e.e.cap, e.e.v, e.e.ts);\n return res;\n}\n \nEvent changeObj(Elevator& e, int d, double t)\n{\n double curpos = e.pos;\n double movet = max(e.stime, t);\n \n if(D[d] > e.pos) curpos += e.v * (movet-e.stime);\n else curpos -= e.v * (movet-e.stime);\n \n bool back = true;\n \n int dst = 1;\n for(int i = N; i >= 2; i--) {\n if(!burn[i] && devices[i] != 0) {\n dst = i;\n back = false;\n break;\n }\n }\n \n int type = arrive;\n if(limit(e) || back) {\n dst = 1;\n type = gfloor;\n }\n \n e.pos = curpos;\n double arv = arvTime(dst, e, movet);\n \n Elevator ne = Elevator(curpos, movet, e.dev, e.cap, e.v, e.ts);\n Event res = Event(type, dst, arv, ne);\n \n return res;\n}\n \n \nvoid finished(int p, double t, priority_queue<Event>& q)\n{\n priority_queue<Event> nq;\n while(!q.empty()) {\n Event e = q.top(); q.pop();\n \n if(e.type == arrive) {\n if(e.bpos == p) {\n Event ne = changeObj(e.e, e.bpos, t);\n nq.push(ne);\n }\n else {\n nq.push(e);\n }\n \n }\n else\n {\n nq.push(e);\n }\n }\n \n q = nq;\n}\n \nvoid burned(int p, double t, priority_queue<Event>& q)\n{\n if(burn[p]) return;\n burn[p] = true;\n \n priority_queue<Event> nq;\n while(!q.empty()) {\n Event e = q.top(); q.pop();\n \n if(e.type == arrive) {\n if(e.bpos == p) {\n Event ne = changeObj(e.e, e.bpos, t);\n nq.push(ne);\n }\n \n else {\n nq.push(e);\n }\n \n }\n if(e.type == burnout) {\n if(e.bpos != p)\n nq.push(e);\n }\n if(e.type == gfloor) {\n nq.push(e);\n }\n }\n \n q = nq;\n}\n \n \n \nvoid simulate(priority_queue<Event>& q)\n{\n int cnt=devices[1];\n double last=0;\n \n while(!q.empty()) {\n Event e = q.top(); q.pop();\n if(e.type == arrive) {\n double nt = e.time + e.e.ts;\n Elevator arv = arrived(e);\n q.push(changeObj(arv, e.bpos, nt));\n \n if(devices[e.bpos] == 0)\n finished(e.bpos, e.time, q);\n }\n if(e.type == burnout) {\n burned(e.bpos, e.time, q);\n }\n if(e.type == gfloor) {\n if(e.e.dev != 0) {\n double nt = e.time + e.e.ts;\n cnt += e.e.dev;\n e.e.dev = 0;\n e.e.pos = D[1];\n e.e.stime = nt;\n last = max(last, e.e.stime);\n \n q.push(changeObj(e.e, e.bpos, nt));\n }\n }\n }\n \n if(cnt==devices[1]) last = 0;\n printf(\"%d %.3lf\\n\", cnt, last);\n}\n \nint main()\n{\n while(cin >> N >> M, (N||M)) {\n memset(burn, 0, sizeof(burn));\n \n int dd;\n cin >> dd;\n D[1] = 0;\n for(int i = 1; i < N; i++)\n D[i+1] = D[i] + dd;\n \n for(int i = 1; i <= N; i++)\n cin >> devices[i];\n \n priority_queue<Event> q;\n for(int i = 0; i < M; i++) {\n int c,v,ts,x;\n cin >> c >> v >> ts >> x;\n Elevator e = Elevator(D[x], 0, 0, c, v, ts);\n q.push(changeObj(e, 1, 0));\n }\n \n \n cin >> K >> T[0] >> T[1] >> T[2];\n \n int S = 0;\n for(int i=K; i<=N; i++) {\n q.push(Event(burnout, i, S+T[0]));\n S += T[1];\n }\n \n S = 0;\n for(int i=K; i>=2; i--) {\n q.push(Event(burnout, i, S+T[0]));\n S += T[2];\n }\n \n simulate(q);\n\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 972, "score_of_the_acc": -1, "final_rank": 7 }, { "submission_id": "aoj_2004_102718", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nenum EventKind {\n Fire,\n ElevetorEnd\n};\n\nstruct Event {\n EventKind kind;\n double t;\n int floor;\n int index;\n Event() {;}\n Event(EventKind kind, double t, int floor, int index)\n : kind(kind), t(t), floor(floor), index(index) {;}\n bool operator<(const Event &rhs) const {\n if (t != rhs.t) { return t > rhs.t; }\n return floor < rhs.floor;;\n }\n};\n\nint n, m, k, tfloor, tup, tlower;\ndouble d;\nint device[50];\nint capacity[50];\ndouble v[50];\nint tstop[50];\ndouble elevetorFrom[50];\ndouble elevetorStart[50];\ndouble elevetorWait[50];\nint elevetorDir[50];\nint elevetorTo[50];\nint elevetorRide[50];\n\nvoid dfsFire(priority_queue<Event> &que, int floor, int parent, int t) {\n if (floor <= -1 || floor >= n) { return; }\n que.push(Event(Fire, t + tfloor, floor, 0));\n int nfloor = floor + 1;\n if (nfloor != parent) {\n dfsFire(que, nfloor, floor, t + tup);\n }\n nfloor = floor - 1;\n if (nfloor != parent) {\n dfsFire(que, nfloor, floor, t + tlower);\n }\n}\n\nvoid SetElevetor(priority_queue<Event> &que, int index, double t) {\n double pos = elevetorFrom[index] + v[index] * elevetorDir[index] * max(0.0, (t - elevetorStart[index] - elevetorWait[index]));\n assert(pos >= -EPS);\n elevetorFrom[index] = pos;\n elevetorWait[index] = max(0.0, elevetorWait[index] + elevetorStart[index] - t);\n elevetorStart[index] = t;\n if (elevetorRide[index] != capacity[index]) {\n for (int y = n - 1; y >= 0; y--) {\n if (device[y] == 0) { continue; }\n if (elevetorTo[index] == y) { return; }\n //cout << index << \" \" << elevetorTo[index] << \" \" << y << endl;\n elevetorDir[index] = y * d > pos ? 1 : -1;\n elevetorTo[index] = y;\n double nt = fabs(pos - elevetorTo[index] * d) / v[index] + t + elevetorWait[index];;\n //cout << index << \" \" << nt << \" \" << y << endl;\n que.push(Event(ElevetorEnd, nt, elevetorTo[index], index));\n return;\n }\n }\n if (elevetorTo[index] == 0) {\n elevetorDir[index] = 0;\n return;\n }\n elevetorDir[index] = -1;\n elevetorTo[index] = 0;\n double nt = fabs(pos - elevetorTo[index] * d) / v[index] + t + elevetorWait[index];\n //cout << index << \" \" << nt << \" \" << 0 << endl;\n que.push(Event(ElevetorEnd, nt, elevetorTo[index], index));\n return;\n}\n\nint main() {\n while (scanf(\"%d %d\", &n, &m),n|m) {\n int ans = 0;\n double anst = 0.0;\n priority_queue<Event> que;\n scanf(\"%lf\", &d);\n REP(i, n) { scanf(\"%d\", &device[i]); }\n ans += device[0];\n device[0] = 0;\n REP(i, m) {\n int y;\n scanf(\"%d %lf %d %d\", &capacity[i], &v[i], &tstop[i], &y);\n y--;\n elevetorFrom[i] = y * d;\n elevetorStart[i] = 0;\n elevetorWait[i] = 0;\n elevetorDir[i] = 1;\n elevetorTo[i] = -1;\n elevetorRide[i] = 0;\n SetElevetor(que, i, 0);\n }\n scanf(\"%d %d %d %d\", &k, &tfloor, &tup, &tlower);\n k--;\n dfsFire(que, k, k, 0);\n while (!que.empty()) {\n Event event = que.top();\n que.pop();\n if (event.kind == ElevetorEnd && event.floor != elevetorTo[event.index]) {\n continue;\n }\n //cout << event.kind << \" \" << event.t << \" \" << event.floor << \" \" << event.index << endl;\n if (event.kind == Fire) {\n //cout << event.t << endl;\n device[event.floor] = 0;\n } else if (event.kind == ElevetorEnd) {\n if (event.floor == 0) {\n ans += elevetorRide[event.index];\n if (elevetorRide[event.index] != 0) {\n anst = max(anst, event.t + tstop[event.index]);\n }\n elevetorFrom[event.index] = event.floor * d;\n elevetorStart[event.index] = event.t;\n elevetorRide[event.index] = 0;\n elevetorWait[event.index] = tstop[event.index];\n } else {\n //cout << event.kind << \" \" << event.t << \" \" << event.floor << \" \" << event.index << endl;\n int ride = min(capacity[event.index] - elevetorRide[event.index], device[event.floor]);\n device[event.floor] -= ride;\n elevetorFrom[event.index] = event.floor * d;\n elevetorStart[event.index] = event.t;\n elevetorRide[event.index] += ride;\n elevetorWait[event.index] = tstop[event.index];\n }\n } else {\n assert(false);\n }\n REP(i, m) { SetElevetor(que, i, event.t); }\n }\n printf(\"%d %.3lf\\n\", ans, anst);\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 972, "score_of_the_acc": -0.125, "final_rank": 1 } ]
aoj_2002_cpp
X-Ray Screening System これは 20XX 年の話である.再生発電網による安定したエネルギー供給,および液化合成燃料の発明によって航空旅客数は鰻登りに増加した.しかし,航空機によるテロリズムの脅威は依然として存在するため,高速でかつ高い信頼性をもつ自動手荷物検査システムの重要性が高まっている.検査官が全部の荷物を調べることは実際問題として困難であることから,自動検査において疑わしいと判断された手荷物だけを検査官で改めて調べるという仕組みを整えたいわけである. 航空機業界からの要請を受け,国際客室保護会社(International Cabin Protection Company)では,新たな自動検査システムを開発するため,最近の旅客の手荷物について調査した.調査の結果,最近の旅客の手荷物には次のような傾向があることが判明した. 手荷物は 1 辺だけが短い直方体のような形状をしている. 一般の乗客が手荷物に詰めて航空機に持ち込む品物としてはノートパソコン,音楽プレイヤー,携帯ゲーム機,トランプなどがあり,これらはいずれも長方形である. 個々の品物は,その長方形の辺が手荷物の辺と平行になるように詰めこまれる. 一方,テロリズムに用いられるような武器は,長方形とは非常に異なる形状をもつ. 上記の調査結果をふまえて,以下のような手荷物検査のためのモデルを考案した.それぞれの手荷物は X 線に対して透明である直方体の容器だとみなす.その中には X 線に対して不透明である複数の品物が入っている.ここで,直方体の 3 辺を x 軸,y 軸,z 軸とする座標系を考え,x 軸と平行な方向に X 線を照射して,y-z 平面に投影された画像を撮影する.撮影された画像は適当な大きさの格子に分割され,画像解析によって,それぞれの格子領域に映っている品物の材質が推定される.この会社には非常に高度の解析技術があり,材質の詳細な違いすらも解析することが可能であることから,品物の材質は互いに異なると考えることができる.なお,複数の品物が x 軸方向に関して重なっているときは,それぞれの格子領域について最も手前にある,すなわち x 座標が最小である品物の材質が得られる.また,2 つ以上の品物の x 座標が等しいことはないと仮定する. あなたの仕事は,画像解析の結果が与えられたときに,長方形ではない(武器であるかもしれない)品物が入っていることが断言できるか,あるいはその手荷物には長方形の品物以外のものは入っていないと推測されるかを判定するプログラムを作成することである. Input 入力の先頭行には単一の正の整数が含まれ,これはデータセットの個数を表す.それぞれのデータセットは次の形式で与えられる. H W 解析結果その1 解析結果その2 ... 解析結果その H H は画像の縦の大きさ, W は横の大きさを表す整数である (1 <= h , w <= 50).解析結果の各行は W 文字で構成されており, i 番目の文字は,行の左から i 番目にある格子領域における解析結果を表す.物質が検出された格子領域については,その材質が英大文字(A〜Z)によって表される.このとき,同じ材質であれば同じ文字が,また異なる材質であれば異なる文字が用いられる.物質が検出されなかった格子領域についてはドット(.)で表される. すべてのデータセットについて,材質の種類は 7 種類を超えないことが保証されている. Output それぞれのデータセットに対して,長方形以外の品物が確実に入っているときは「SUSPICIOUS」を,そうでないときは「SAFE」をそれぞれ 1 行に出力しなさい. Sample Input 6 1 1 . 3 3 ... .W. ... 10 10 .......... .DDDDCCC.. .DDDDCCC.. .DDDDCCC.. ADDDDCCC.. AAA..CCC.. AAABBBBC.. AAABBBB... ..BBBBB... .......... 10 10 .......... .DDDDDD... .DDDDCCC.. .DDDDCCC.. ADDDDCCC.. AAA..CCC.. AAABBBBC.. AAABBBB... ..BBBBB... .......... 10 10 R..E..C.T. R.EEE.C.T. .EEEEE.... EEEEEEE... .EEEEEEE.. ..EEEEEEE. ...EEEEEEE ....EEEEE. .....EEE.. ......E... 16 50 .................................................. .........AAAAAAAAAAAAAAAA......................... ....PPP...AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..... ....PPP...AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..... ....PPP...AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.... ....PPP..............AAAAA.AAAAAAAAAAAAAAAA....... ....PPP................A....AAA.AAAAAAAAAA........ ....PPP...........IIIIIAAIIAIII.AAAAAAAAAA........ ..CCCCCCCCCCCCC...IIIIIIAAAAAAAAAAAAAAAAAA........ ..CCCCCCCCCCCCC...IIIIIIIIIIIII...AAAAAAAAAA...... ....PPP............................AAAAAAAAAA..... MMMMPPPMMMMMMMMMMMMMMM.............AAAAAAAAAAA.... MMMMPPPMMMMMMMMMMMMMMM..............AAAAAAAAAAA... MMMMMMMMMMMMMMMMMMMMMM...............AAAAAAAAAA... MMMMMMMMMMMMMMMMMMMMMM...............AAAAAAAAAA... MMMMMMMMMMMMMMMMMMMMMM............................ Output for the Sample Input SAFE SAFE SAFE SUSPICIOUS SUSPICIOUS SUSPICIOUS
[ { "submission_id": "aoj_2002_10848556", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main()\n{\n\tint N, H, W;\n\tcin >> N;\n\twhile (N--) {\n\t\tcin >> H >> W;\n\t\tvector<int> used(26), xma(26), xmi(26, 100), yma(26), ymi(26, 100);\n\t\tvector<vector<char>> a(H, vector<char>(W));\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tcin >> a[i][j];\n\t\t\t\tif (a[i][j] != '.') {\n\t\t\t\t\tint p = a[i][j] - 'A';\n\t\t\t\t\tused[p] = 1;\n\t\t\t\t\txma[p] = max(xma[p], i);\n\t\t\t\t\txmi[p] = min(xmi[p], i);\n\t\t\t\t\tyma[p] = max(yma[p], j);\n\t\t\t\t\tymi[p] = min(ymi[p], j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>> d(26, vector<int>(26, 1000));\n\t\tfor (int i = 0; i < 26; i++) {\n\t\t\td[i][i] = 0;\n\t\t}\n\t\tbool sus = false;\n\t\tfor (int i = 0; i < 26; i++) {\n\t\t\tif (used[i]) {\n\t\t\t\tfor (int x = xmi[i]; x <= xma[i]; x++) {\n\t\t\t\t\tfor (int y = ymi[i]; y <= yma[i]; y++) {\n\t\t\t\t\t\tif (a[x][y] == '.') {\n\t\t\t\t\t\t\tsus = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (a[x][y] - 'A' != i) {\n\t\t\t\t\t\t\td[i][a[x][y] - 'A'] = -1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int k = 0; k < 26; k++) {\n\t\t\tfor (int i = 0; i < 26; i++) {\n\t\t\t\tfor (int j = 0; j < 26; j++) {\n\t\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 26; i++) {\n\t\t\tif (d[i][i] < 0) {\n\t\t\t\tsus = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << (sus ? \"SUSPICIOUS\" : \"SAFE\") << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.6765, "final_rank": 6 }, { "submission_id": "aoj_2002_10833933", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main()\n{\n\tint N, H, W;\n\tcin >> N;\n\twhile (N--) {\n\t\tcin >> H >> W;\n\t\tvector<int> used(26), xma(26), xmi(26, 100), yma(26), ymi(26, 100);\n\t\tvector<vector<char>> a(H, vector<char>(W));\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tcin >> a[i][j];\n\t\t\t\tif (a[i][j] != '.') {\n\t\t\t\t\tint p = a[i][j] - 'A';\n\t\t\t\t\tused[p] = 1;\n\t\t\t\t\txma[p] = max(xma[p], i);\n\t\t\t\t\txmi[p] = min(xmi[p], i);\n\t\t\t\t\tyma[p] = max(yma[p], j);\n\t\t\t\t\tymi[p] = min(ymi[p], j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>> d(26, vector<int>(26, 1000));\n\t\tfor (int i = 0; i < 26; i++) {\n\t\t\td[i][i] = 0;\n\t\t}\n\t\tbool sus = false;\n\t\tfor (int i = 0; i < 26; i++) {\n\t\t\tif (used[i]) {\n\t\t\t\tfor (int x = xmi[i]; x <= xma[i]; x++) {\n\t\t\t\t\tfor (int y = ymi[i]; y <= yma[i]; y++) {\n\t\t\t\t\t\tif (a[x][y] == '.') {\n\t\t\t\t\t\t\tsus = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (a[x][y] - 'A' != i) {\n\t\t\t\t\t\t\td[i][a[x][y] - 'A'] = -1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int k = 0; k < 26; k++) {\n\t\t\tfor (int i = 0; i < 26; i++) {\n\t\t\t\tfor (int j = 0; j < 26; j++) {\n\t\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 26; i++) {\n\t\t\tif (d[i][i] < 0) {\n\t\t\t\tsus = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << (sus ? \"SUSPICIOUS\" : \"SAFE\") << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.2157, "final_rank": 2 }, { "submission_id": "aoj_2002_10609913", "code_snippet": "#line 1 \"2006JAGC.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2006JAGC.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(...)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nint n = 26;\nint solve(){\n int h, w; cin >> h >> w;\n vector<string> s(h); rep(i,h) cin >> s[i];\n\n auto checker = [&](char c) -> vll {\n // 上に被っているものの配列を返す\n ll l=inf, r=-1, u=inf, d=-1;\n rep(i,h) {\n rep(j,w) {\n if(s[i][j] == c) {\n chmin(l,j);\n chmax(r,j);\n chmin(u,i);\n chmax(d,i);\n }\n }\n }\n if(l==inf) return {};\n\n vll res;\n rep2(i,u,d+1) {\n rep2(j,l,r+1) {\n if(s[i][j] == '.') return {-1}; // 構成不可能なときはこれ\n else if(s[i][j] != c) res.emplace_back(s[i][j] - 'A');\n }\n }\n sort(all(res));\n res.erase(unique(all(res)),res.end());\n return res;\n };\n\n vvll g(n);\n vll ins(n,0);\n\n rep(i,n) {\n g[i] = checker('A'+i);\n dbg(i, g[i]);\n if(g[i].size()>0 && g[i][0]==-1) return 1;\n for(auto ni: g[i]) ins[ni]++;\n }\n\n queue<int> que;\n vector<bool> seen(26,false);\n rep(i,n) {\n if(ins[i] == 0) {\n que.push(i);\n seen[i] = true;\n }\n }\n while(!que.empty()) {\n auto p = que.front();\n que.pop();\n for(auto ni: g[p]) {\n if(--ins[ni] == 0) {\n seen[ni] = true;\n que.push(ni);\n }\n }\n }\n\n bool all_seen = true;\n rep(i,n) all_seen = all_seen && seen[i];\n return !all_seen;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t; cin >> t;\n while(t--) {\n // 0ならsafe\n if(solve()==0) {\n cout << \"SAFE\\n\";\n } else {\n cout << \"SUSPICIOUS\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3396, "score_of_the_acc": -0.7843, "final_rank": 10 }, { "submission_id": "aoj_2002_10580781", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve() {\n const int N = 26;\n int H, W;\n cin >> H >> W;\n vector<string> S(H);\n for (int i = 0; i < H; i++) cin >> S[i];\n for (bool fn = true; fn; ) {\n fn = false;\n vector<int> lh(N, H + 1), rh(N, -1), lw(N, W + 1), rw(N, -1);\n vector<int> cnt(26);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (S[i][j] == '.' || S[i][j] == '#') continue;\n int p = S[i][j] - 'A';\n lh[p] = min(lh[p], i);\n rh[p] = max(rh[p], i + 1);\n lw[p] = min(lw[p], j);\n rw[p] = max(rw[p], j + 1);\n cnt[p]++;\n }\n }\n vector<int> del;\n for (int p = 0; p < 26; p++) {\n if (cnt[p]) {\n bool ok = true;\n for (int i = lh[p]; i < rh[p]; i++) {\n for (int j = lw[p]; j < rw[p]; j++) {\n if (S[i][j] != '#' && S[i][j] - 'A' != p) ok = false;\n }\n }\n if (ok) del.push_back(p);\n }\n }\n fn = del.size();\n for (int p : del) {\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (S[i][j] - 'A' == p) S[i][j] = '#';\n }\n }\n }\n }\n string ans = \"SAFE\";\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (S[i][j] != '.' && S[i][j] != '#') ans = \"SUSPICIOUS\";\n }\n }\n cout << ans << '\\n';\n}\n\nint main() {\n int T;\n cin >> T;\n while (T--) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.9314, "final_rank": 12 }, { "submission_id": "aoj_2002_10562173", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve();\nint main() {\n int T;\n cin >> T;\n while (T--) solve();\n return 0;\n}\n\nbool solve() {\n int H, W;\n cin >> H >> W;\n\n vector<string> S(H);\n string z = \"\";\n vector<int> lx, ly, rx, ry;\n for (int i = 0; i < H; i++) {\n cin >> S[i];\n for (int j = 0; j < W; j++) {\n if (S[i][j] != '.') {\n int k = z.find(S[i][j]);\n if (k == z.npos) {\n z.push_back(S[i][j]);\n lx.push_back(j);\n rx.push_back(j);\n ly.push_back(i);\n ry.push_back(i);\n } else {\n lx[k] = min(lx[k], j);\n rx[k] = max(rx[k], j);\n ly[k] = min(ly[k], i);\n ry[k] = max(ry[k], i);\n }\n }\n }\n }\n\n // n!\n int N = int(z.size());\n vector<int> order(N);\n iota(order.begin(), order.end(), 0);\n bool ans = false;\n\n do {\n vector<string> T(H, string(W, '.'));\n for (int kk = 0; kk < N; kk++) {\n int k = order[kk];\n for (int i = ly[k]; i <= ry[k]; i++) {\n for (int j = lx[k]; j <= rx[k]; j++) {\n T[i][j] = z[k];\n }\n }\n }\n if (S == T) {\n ans = true;\n break;\n }\n } while (next_permutation(order.begin(), order.end()));\n cout << (ans ? \"SAFE\" : \"SUSPICIOUS\") << endl;\n return true;\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 3456, "score_of_the_acc": -1.1122, "final_rank": 15 }, { "submission_id": "aoj_2002_10148382", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<set>\n#include<vector>\nusing namespace std;\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tint T;cin>>T;\n\tfor(;T--;)\n\t{\n\t\tint H,W;cin>>H>>W;\n\t\tvector<string>S(H);\n\t\tset<char>c;\n\t\tfor(int i=0;i<H;i++)\n\t\t{\n\t\t\tcin>>S[i];\n\t\t\tfor(int j=0;j<W;j++)if(S[i][j]!='.')c.insert(S[i][j]);\n\t\t}\n\t\tif(c.size()==0)\n\t\t{\n\t\t\tcout<<\"SAFE\"<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tvector<char>p;\n\t\tfor(auto e:c)p.push_back(e);\n\t\t// 図形の幅を見る\n\t\tvector<int>mnx(256,1e9),mny(256,1e9),mxx(256,0),mxy(256,0);\n\t\tfor(int i=0;i<p.size();i++)for(int x=0;x<H;x++)for(int y=0;y<W;y++)\n\t\t{\n\t\t\tif(S[x][y]==p[i])\n\t\t\t{\n\t\t\t\tmnx[p[i]]=min(mnx[p[i]],x),mny[p[i]]=min(mny[p[i]],y);\n\t\t\t\tmxx[p[i]]=max(mxx[p[i]],x),mxy[p[i]]=max(mxy[p[i]],y);\n\t\t\t}\n\t\t}\n\t\tbool safe=false;\n\t\tdo{\n\t\t\tvector<vector<bool> >vis(H,vector<bool>(W));\n\t\t\tbool ok=true;\n\t\t\tfor(int i=0;i<p.size();i++)\n\t\t\t{\n\t\t\t\t// 埋められるか見る\n\t\t\t\tfor(int x=mnx[p[i]];x<=mxx[p[i]];x++)for(int y=mny[p[i]];y<=mxy[p[i]];y++)\n\t\t\t\t{\n\t\t\t\t\tif(S[x][y]!=p[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tif(!vis[x][y]||S[x][y]=='.')ok=false;\n\t\t\t\t\t}\n\t\t\t\t\telse vis[x][y]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok)\n\t\t\t{\n\t\t\t\tsafe=true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}while(next_permutation(p.begin(),p.end()));\n\t\tcout<<(safe?\"SAFE\":\"SUSPICIOUS\")<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 2560, "memory_kb": 3484, "score_of_the_acc": -1.5426, "final_rank": 19 }, { "submission_id": "aoj_2002_9823736", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define vi vector<int>\n#define vl vector<long long>\n#define pii pair<int, int>\n#define pll pair<long long, long long>\n#define elif else if\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define all(v) (v).begin(), (v).end()\n#define rall(x) x.rbegin(), x.rend()\n#define el '\\n'\n#define Yes cout << \"Yes\" << el\n#define No cout << \"No\" << el\n#define YES cout << \"YES\" << el\n#define NO cout << \"NO\" << el\nconst double pi = 3.141592653589793238;\nconst int inf = 1073741823;\nconst ll infl = 1LL << 60;\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst int MOD = 998244353;\nconst array<int, 8> dx = {0, 0, -1, 1, -1, -1, 1, 1};\nconst array<int, 8> dy = {-1, 1, 0, 0, -1, 1, -1, 1};\ntemplate <typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b) {\n bool compare = a < b;\n if (compare) a = b;\n return compare;\n}\ntemplate <typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b) {\n bool compare = a > b;\n if (compare) a = b;\n return compare;\n}\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, std::pair<T1, T2> p) {\n os << \"{\" << p.first << \",\" << p.second << \"}\";\n return os;\n}\ntemplate <typename T>\ninline void print_vec(const vector<T> &v, bool split_line = false) {\n if (v.empty()) {\n cout << \"This vector is empty.\" << el;\n return;\n }\n constexpr bool isValue = is_integral<T>::value;\n for (int i = 0; i < (int)v.size(); i++) {\n if constexpr (isValue) {\n if ((v[i] == inf) || (v[i] == infl))\n cout << 'x' << \" \\n\"[split_line || i + 1 == (int)v.size()];\n else\n cout << v[i] << \" \\n\"[split_line || i + 1 == (int)v.size()];\n } else\n cout << v[i] << \" \\n\"[split_line || i + 1 == (int)v.size()];\n }\n}\ntemplate <typename T>\nvoid vin(vector<T> &v) {\n for (auto &element : v) {\n cin >> element;\n }\n}\ntemplate <typename T>\nT mod_pow(T x, T n, const T &p) {\n T ret = 1;\n while (n > 0) {\n if (n & 1) (ret *= x) %= p;\n (x *= x) %= p;\n n >>= 1;\n }\n return ret;\n}\ntemplate <typename T>\nT ipow(T x, T n) {\n T ret = 1;\n while (n > 0) {\n if (n & 1) ret *= x;\n x *= x;\n n >>= 1;\n }\n return ret;\n}\nstring solve(int h, int w, vector<string> s) {\n map<char, pii> minmax_x;\n map<char, pii> minmax_y;\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] == '.') {\n continue;\n }\n if (minmax_x.find(s[i][j]) == minmax_x.end()) {\n minmax_x[s[i][j]] = make_pair(inf, -inf);\n minmax_y[s[i][j]] = make_pair(inf, -inf);\n }\n chmin(minmax_x[s[i][j]].first, i);\n chmax(minmax_x[s[i][j]].second, i);\n chmin(minmax_y[s[i][j]].first, j);\n chmax(minmax_y[s[i][j]].second, j);\n }\n }\n vector<pair<char, char>> more_big;\n vector<char> per;\n\n for (auto k : minmax_x) {\n // cout << k << el;\n per.push_back(k.first);\n for (int i = k.second.first; i <= k.second.second; i++) {\n for (int j = minmax_y[k.first].first; j <= minmax_y[k.first].second;\n j++) {\n if (s[i][j] == '.') {\n return \"SUSPICIOUS\";\n } else if (s[i][j] != k.first) {\n more_big.push_back(make_pair(k.first, s[i][j]));\n }\n }\n }\n }\n\n sort(all(per));\n string result = \"SUSPICIOUS\";\n // print_vec(per);\n // print_vec(more_big);\n\n while (1) {\n bool ok = true;\n for (auto ij : more_big) {\n if (find(all(per), ij.first) < find(all(per), ij.second)) {\n } else {\n ok = false;\n }\n }\n if (ok) {\n result = \"SAFE\";\n break;\n }\n if (not next_permutation(all(per))) {\n break;\n }\n }\n\n return result;\n}\nint main() {\n int t;\n cin >> t;\n rep(i, t) {\n int h, w;\n cin >> h >> w;\n vector<string> s(h);\n rep(i, h) { cin >> s[i]; }\n cout << solve(h, w, s) << el;\n // set<char> tmp;\n // rep(i, h) {\n // rep(j, w) {\n // if (s[i][j] == '.') {\n // tmp.insert(s[i][j]);\n // }\n // }\n // }\n }\n // to do\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 3456, "score_of_the_acc": -1.0675, "final_rank": 14 }, { "submission_id": "aoj_2002_9593669", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\ninput top view of rect and non-rect objects\nsome rects may be partly masked by other\ntest 3:\nD -> A -> B -> C => SAFE\nD -> A -> B -> C -> D => SUSPICIOUS\n\nSUSPICIOUS due to\n1. cycle\n2. non-rect obj\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\nconst int MAX_N = 60;\nconst char FREE = '.';\nstatic char grid[MAX_N][MAX_N];\n\nconst int MAX_K = 26;\nconst int LETTER = 26;\nstatic bool visited[MAX_K];\nstatic int rec_stack[MAX_K];\ntypedef vector<vector<int> > Graph;\n\n//------------------------------------------------------------------------------\nbool cyclic_recursive(int N, Graph& G, int v)\n{\n if (!visited[v])\n {\n visited[v] = true;\n rec_stack[v] = true;\n for (int w: G[v])\n {\n if (!visited[w] && cyclic_recursive(N, G, w)) return true;\n else if (rec_stack[w]) return true;\n }\n }\n rec_stack[v] = false;\n return false;\n}\n\nbool cyclic(int N, Graph& G)\n{\n for (int v=0; v<N; ++v) { visited[v] = false; rec_stack[v] = false; }\n\n for (int v=0; v<N; ++v)\n {\n if (visited[v]) continue;\n else if (cyclic_recursive(N, G, v)) return true;\n }\n\n return false;\n}\n\nvoid debug_graph(int N, Graph& G)\n{\n for (int v=0; v<N; v++)\n {\n printf(\"%d: \", v); for (int w: G[v]) printf(\"%d \", w); printf(\"\\n\");\n }\n\n}\n//------------------------------------------------------------------------------\n// bounding box\nstruct BBox\n{\n char c;\n int m1, n1, m2, n2;\n BBox(): c('?'), m1(0), n1(0), m2(0), n2(0) {}\n BBox(char m, int a, int b, int c, int d): c(m), m1(a), n1(b), m2(c), n2(d) {}\n bool is_rect()\n {\n for (int m=m1; m<=m2; ++m)\n for (int n=n1; n<=n2; ++n)\n if (grid[m][n] == FREE)\n return false;\n return true;\n }\n void debug() const { printf(\"%c: (%d,%d), (%d,%d)\\n\", c, m1, n1, m2, n2); }\n};\n\ntypedef vector<BBox> BBoxes;\n//------------------------------------------------------------------------------\nBBox setup_bb(int M, int N, char c)\n{\n int m1 = INF;\n int m2 = -INF;\n int n1 = INF;\n int n2 = -INF;\n for (int m=0; m<M; ++m)\n for (int n=0; n<N; ++n)\n if (grid[m][n] == c)\n {\n m1 = min(m1, m);\n m2 = max(m2, m);\n n1 = min(n1, n);\n n2 = max(n2, n);\n }\n return BBox(c, m1, n1, m2, n2);\n}\nGraph setup_graph(int M, int N, map<char,int>& to_rect, int K, const BBoxes& BB)\n{\n Graph G(K);\n for (BBox B: BB)\n {\n set<int> vs;\n for (int m=B.m1; m<=B.m2; ++m)\n for (int n=B.n1; n<=B.n2; ++n)\n if (grid[m][n] != B.c)\n vs.insert(to_rect[grid[m][n]]);\n int w = to_rect[B.c];\n for (int v: vs) G[v].push_back(w);\n }\n return G;\n}\nbool solve(int M, int N)\n{\n //--------------------------------------------------------------------------\n // base cases:\n //--------------------------------------------------------------------------\n // init:\n vector<bool> marked(LETTER,false);\n for (int m=0; m<M; ++m)\n for (int i=0; i<N; ++i)\n if (grid[m][i] != FREE)\n marked[ grid[m][i]-'A' ] = true;\n\n vector<char> items;\n for (int c=0; c<LETTER; ++c)\n if (marked[c])\n items.push_back( c + 'A' );\n int K = items.size();\n if (K == 0) return true;\n //--------------------------------------------------------------------------\n // compute:\n map<char,int> to_rect;\n BBoxes BB(K);\n for (int k=0; k<K; ++k)\n {\n BB[k] = setup_bb(M, N, items[k]);\n if (DEBUG) BB[k].debug();\n\n if (!BB[k].is_rect()) return false;\n to_rect[items[k]] = k;\n }\n\n Graph G = setup_graph(M, N, to_rect, K, BB);\n if (DEBUG) debug_graph(K, G);\n if (cyclic(K, G))\n {\n if (DEBUG) printf(\"cyclic\\n\");\n return false;\n }\n return true;\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n //--------------------------------------------------------------------------\n /*\n int T, M, N, num;\n char c;\n cin >> T;\n while (T--)\n {\n cin >> M >> N;\n if (DEBUG) printf(\"M=%d, N=%d\\n\", M, N);\n for (int m=0; m<M; ++m)\n {\n for (int i=0; i<N; ++i)\n {\n cin >> c;\n grid[m][i] = c;\n }\n }\n if (DEBUG) {for (int m=0; m<M; ++m) { for (int n=0; n<N; ++n) printf(\"%c \",grid[m][n]); printf(\"\\n\"); }}\n if (solve(M, N)) printf(\"SAFE\\n\");\n else printf(\"SUSPICIOUS\\n\");\n }\n */\n int T, M, N, num;\n num = scanf(\"%d \", &T);\n while (T--)\n {\n num = scanf(\"%d %d \", &M, &N);\n for (int m=0; m<M; ++m) for (int i=0; i<N; ++i) num = scanf(\"%c \", &grid[m][i]);\n if (solve(M, N)) printf(\"SAFE\\n\");\n else printf(\"SUSPICIOUS\\n\");\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 10, "memory_kb": 3248, "score_of_the_acc": -0.4216, "final_rank": 4 }, { "submission_id": "aoj_2002_9459756", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nvoid Solve(int N, int M) {\n vector<string> S(N);\n rep(i,0,N) cin >> S[i];\n vector<int> lx(26), rx(26), ly(26), ry(26);\n while(1) {\n rep(i,0,26) lx[i] = inf, rx[i] = -inf, ly[i] = inf, ry[i] = -inf;\n rep(i,0,N) {\n rep(j,0,M) {\n if ('A' <= S[i][j] && S[i][j] <= 'Z') {\n chmin(lx[S[i][j]-'A'],i);\n chmax(rx[S[i][j]-'A'],i);\n chmin(ly[S[i][j]-'A'],j);\n chmax(ry[S[i][j]-'A'],j);\n }\n }\n }\n bool check = false;\n rep(i,0,26) {\n if (lx[i] == inf) continue;\n bool check2 = true;\n rep(j,lx[i],rx[i]+1) {\n rep(k,ly[i],ry[i]+1) {\n if (S[j][k] == '.') {\n cout << \"SUSPICIOUS\" << endl;\n return;\n }\n if (S[j][k] != (char)('A'+i) && S[j][k] != '#') check2 = false;\n }\n }\n if (!check2) continue;\n check = true;\n rep(j,lx[i],rx[i]+1) {\n rep(k,ly[i],ry[i]+1) {\n S[j][k] = '#';\n }\n }\n }\n if (!check) break;\n }\n bool ANS = true;\n rep(i,0,N) {\n rep(j,0,M) {\n if ('A' <= S[i][j] && S[i][j] <= 'Z') ANS = false;\n }\n }\n cout << (ANS ? \"SAFE\" : \"SUSPICIOUS\") << endl;\n return;\n}\n\nint main() {\n int T;\n cin >> T;\n rep(_,0,T) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n Solve(N,M);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3420, "score_of_the_acc": -0.8431, "final_rank": 11 }, { "submission_id": "aoj_2002_9456018", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#ifdef LOCAL_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n#include <stdio.h>\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <iomanip> //setprecision\n#include <map> // map\n#include <unordered_map> //unordered_map\n#include <queue> // queue, priority_queue\n#include <set> // set,multiset\n#include <stack> // stack\n#include <deque> // deque\n#include <math.h>//pow,,,\n#include <cmath>//abs,,,\n#include <bitset> // bitset\n#include <numeric> //accumulate,,,\n#include <sstream>\n#include <initializer_list>\n#include <random>\n#include <unordered_set>\n#include <time.h>\n#include <stdio.h>\n#include <string.h>\n#include <functional>\n#include <climits>\n#include <utility>\n#include <cassert>\n//#include <atcoder/all> // aclibraryを使う際は g++ a.cpp -O2 -std=c++17 -I . でコンパイルする\n//using namespace atcoder;\nusing namespace std;\nconst long long INF = 4000000000000000001; // 4*10^18\nconst int inf = 2001001001;\nconst long long MOD = 998244353;\nconst double pi = 3.141592653589;\nconst vector<int> dh = {1,0,-1,0} , dw = {0,1,0,-1};\nconst vector<int> dh8 = {-1,-1,-1,0,0,1,1,1} , dw8 = {-1,0,1,-1,1,-1,0,1};\n#define endl \"\\n\";\nlong long modpow(long long a, long long n, long long mod) {\n a %= mod;\n long long ret = 1;\n while (n > 0) {\n if (n & 1) ret = ret * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return ret % mod;\n}\nlong long modinv(long long a, long long m) {\n long long b = m, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n u %= m; \n if (u < 0) u += m;\n return u;\n}\nlong long gcd(long long a, long long b) {\n if (b == 0) return a; else return gcd(b, a % b);\n}\nlong long lcm(long long a, long long b) {\n return a / gcd(a, b) * b ;\n}\nlong long get_mod(long long res){\n if(res < 0) res += MOD;\n return res % MOD;\n}\ndouble DegToRad(double deg){\n return deg*(pi/180.0);\n}\ndouble RadToDeg(double rad){\n return rad/(pi/180.0);\n}\nint uniform_rand(int mini , int maxi){\n random_device seed;\n mt19937_64 engine(seed());\n uniform_real_distribution<> ran(mini , maxi+1);\n int ret = ran(engine);\n if(ret >= maxi+1) ret = maxi;\n return ret;\n}\n\n\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n // FILE* in = freopen(\"mojain/in1.txt\" , \"r\" , stdin); //2つ以上のケースを試したい場合はファイル名をループ回してin2.txt等で指定していく\n // FILE* out = freopen(\"mojaout/out1.txt\", \"w\", stdout);\n // ifstream ifs(\"in.txt\"); // 巨大ケースは in.txt に書いてifsで標準入力\n\n int N;cin >> N;\n for(int testcase = 0;testcase<N;testcase++){\n int H,W;cin >> H >> W;\n vector<vector<char>> ban(H,vector<char>(W));\n set<char> chars;\n for(int i = 0;i<H;i++){\n for(int j = 0;j<W;j++){\n cin >> ban.at(i).at(j);\n if(ban.at(i).at(j) != '.'){\n chars.insert(ban.at(i).at(j));\n }\n }\n }\n\n string allchar = \"\";\n for(char c : chars){\n allchar += c;\n }\n sort(allchar.begin(),allchar.end());\n\n vector<int> minh(26,inf) , maxh(26,-1) , minw(26,inf) , maxw(26,-1);\n for(int i = 0;i<H;i++){\n for(int j = 0;j<W;j++){\n char c = ban.at(i).at(j);\n if(c != '.'){\n int idx = c -= 'A';\n minh.at(idx) = min(minh.at(idx) , i);\n maxh.at(idx) = max(maxh.at(idx) , i);\n minw.at(idx) = min(minw.at(idx) , j);\n maxw.at(idx) = max(maxw.at(idx) , j);\n }\n }\n }\n\n do{\n vector<vector<char>> nban(H,vector<char>(W,'.'));\n for(char c : allchar){\n int sh,gh,sw,gw;\n sh = minh.at(c-'A'); gh = maxh.at(c-'A'); sw = minw.at(c-'A'); gw = maxw.at(c-'A');\n for(int h = sh;h<=gh;h++){\n for(int w = sw;w<=gw;w++){\n nban.at(h).at(w) = c;\n }\n }\n }\n\n if(ban == nban){\n cout << \"SAFE\" << endl;\n goto SKIP;\n }\n\n\n }while(next_permutation(allchar.begin(),allchar.end()));\n \n cout << \"SUSPICIOUS\" << endl;\n\n SKIP:;\n\n \n }\n\n \n\n \n \n\n \n\n \n\n}", "accuracy": 1, "time_ms": 1190, "memory_kb": 3204, "score_of_the_acc": -0.5648, "final_rank": 5 }, { "submission_id": "aoj_2002_9403124", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef LOCAL\n#include \"./debug.hpp\"\n#else\n#define debug(...)\n#define print_line\n#endif\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<ll>;\nusing pi = pair<int,int>;\nusing pl = pair<ll,ll>;\nconst int INF = 1e9 + 10;\nconst ll INFL = 4e18;\n#define rep(i, a, b) for (ll i = (a); i < (ll)(b); i++)\n#define all(x) x.begin(),x.end()\n\ntemplate <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }\ntemplate <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }\n\n// --------------------------------------------------------\n\nint main(){\n\tint T;cin>>T;\n\n\twhile(T--){\n\t\tint H,W;cin>>H>>W;\n\t\tvector<string>S(H);\n\t\tfor(int i=0;i<H;i++)cin>>S[i];\n\n\t\tset<char>st;\n\t\tmap<char,int>lo,hi,le,ri;\n\t\tfor(int i=0;i<H;i++){\n\t\t\tfor(int j=0;j<W;j++){\n\t\t\t\tif(S[i][j]=='.')continue;\n\t\t\t\tif(!st.count(S[i][j])){\n\t\t\t\t\tst.insert(S[i][j]);\n\t\t\t\t\tlo[S[i][j]]=i;\n\t\t\t\t\thi[S[i][j]]=i;\n\t\t\t\t\tle[S[i][j]]=j;\n\t\t\t\t\tri[S[i][j]]=j;\n\t\t\t\t}else{\n\t\t\t\t\tchmin(lo[S[i][j]],i);\n\t\t\t\t\tchmax(hi[S[i][j]],i);\n\t\t\t\t\tchmin(le[S[i][j]],j);\n\t\t\t\t\tchmax(ri[S[i][j]],j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap<char,set<char>>G;\n\t\tbool ans=true;\n\t\tfor(char c:st){\n\t\t\tfor(int i=lo[c];i<=hi[c];i++){\n\t\t\t\tfor(int j=le[c];j<=ri[c];j++){\n\t\t\t\t\tif(S[i][j]=='.')ans=false;\n\t\t\t\t\telse if(S[i][j]!=c)G[c].insert(S[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap<char,int>D;\n\t\tfor(char c:st)for(char nxt:G[c])D[nxt]++;\n\t\tqueue<char>Q;\n\t\tfor(char c:st)if(D[c]==0)Q.push(c);\n\t\tvector<char>to;\n\t\twhile(!Q.empty()){\n\t\t\tchar now=Q.front();Q.pop();\n\t\t\tto.push_back(now);\n\t\t\tfor(char nxt:G[now])if(D[nxt]>0){\n\t\t\t\tD[nxt]--;\n\t\t\t\tif(D[nxt]==0)Q.push(nxt);\n\t\t\t}\n\t\t}\n\n\t\tif(to.size()==st.size()&&ans)cout<<\"SAFE\"<<endl;\n\t\telse cout<<\"SUSPICIOUS\"<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3392, "score_of_the_acc": -0.7745, "final_rank": 9 }, { "submission_id": "aoj_2002_9402874", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\nusing pii =pair<int,int>;\n\nconstexpr ll mod = 998244353;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint dX[4]={1,0,-1,0};\nint dY[4]={0,-1,0,1};\n\n//library\n\n\n//end\n\n\nint main(){\n int n;\n cin>>n;\n for (int i00=0; i00<n; i00++){\n int h,w;\n cin>>h>>w;\n\n if (h==0)break;\n vector<string> F(h);\n\n for (int i=0; i<h; i++)cin>>F[i];\n\n //cout<<F[h-1]<<endl;\n map<char, int> dic;\n int k=0;\n for (int i=0; i<h; i++){\n for (int j=0; j<w; j++){\n if (F[i][j]=='.')continue;\n if (dic.count(F[i][j])==0) {\n dic[F[i][j]] = k;\n k++;\n }\n F[i][j]='0'+dic[F[i][j]];\n\n }\n }\n vector<int> vec(k);\n for (int i=0; i<k; i++)vec[i]=i;\n string ans=\"SUSPICIOUS\";\n do{\n //cout<<endl;\n vector<string>nF(h);\n //cout<<F[4];\n for (int i=0; i<h; i++)nF[i]=F[i];\n for (int i0=0; i0<k; i0++) {\n char c = '0' + vec[i0];\n //cout<<c;\n int mx = 1000, Mx = 0, my = 1000, My = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (F[i][j] == c) {\n mx = min(mx, i);\n Mx = max(Mx, i);\n my = min(my, j);\n My = max(My, j);\n\n }\n }\n }\n for (int i = mx; i <= Mx; i++) {\n for (int j = my; j <= My; j++) {\n if (F[i][j] == c || F[i][j] == '#') {\n F[i][j] = '#';\n continue;\n }\n goto go1;\n }\n\n }\n }\n\n ans=\"SAFE\";\n break;\n\n go1:;\n\n swap(nF,F);\n }while(next_permutation(vec.begin(),vec.end()));\n\n\n cout<<ans<<endl;\n\n\n\n\n\n\n }\n\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 3476, "score_of_the_acc": -1.2527, "final_rank": 18 }, { "submission_id": "aoj_2002_9315619", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0; i< (n); i++)\n#define mkp(i,j) make_pair((i),(j))\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 998244353;\nconst int inf = (1<<30);\n\n\nint main(){\n int q; cin>>q;\n rep(o,q){\n vector<vector<int>> c(8,vector<int>(4));//up,dw,le,ri\n rep(i,8){\n c[i][0] = inf;\n c[i][2] = inf; \n }\n map<char,int> ctoi;\n int h,w; cin>>h>>w;\n vector<vector<int>> table(h,vector<int>(w));\n int count = 1;\n\n rep(i,h){\n rep(j,w){\n char s; cin>>s;\n if(s == '.') table[i][j] = 0;\n else{\n int a = ctoi[s];\n if(a == 0){\n ctoi[s] = count;\n count++;\n }\n table[i][j] = ctoi[s];\n }\n }\n }\n rep(i,h){\n rep(j,w){\n int s = table[i][j];\n c[s][0] = min(i,c[s][0]);\n c[s][1] = max(i,c[s][1]);\n c[s][2] = min(j,c[s][2]);\n c[s][3] = max(j,c[s][3]);\n }\n }\n vector<int> p;\n for(int i = 1; i<count; i++) p.push_back(i);\n if(p.size() == 0){\n cout<<\"SAFE\"<<endl;\n continue;\n }\n bool ans = 0;\n do{\n set<int> v;\n bool res = 1;\n for(int s : p){\n for(int i = c[s][0]; i <= c[s][1]; i++){\n for(int j = c[s][2]; j<=c[s][3]; j++){\n if(table[i][j] == s) continue;\n if(v.find(table[i][j]) == v.end()){\n res = 0;\n //cout<<i<<\" \"<<j<<endl;\n }\n }\n }\n v.insert(s);\n }\n if(res == 1) ans = 1;\n\n }while(next_permutation(p.begin(),p.end()));\n \n cout<<(ans ? \"SAFE\" : \"SUSPICIOUS\")<<endl;\n }\n}", "accuracy": 1, "time_ms": 1670, "memory_kb": 3420, "score_of_the_acc": -1.1963, "final_rank": 16 }, { "submission_id": "aoj_2002_9259973", "code_snippet": "# include <bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define rep(i,n) for(ll i = 0;i <(ll)n;i++)\nconst ll INF = 1LL << 60;\n\nbool chmin(ll &a,ll b){\n if(a > b){\n a = b;\n return true;\n }else{\n return false;\n }\n}\n\nbool chmax(ll & a,ll b){\n if(a < b){\n a = b;\n return true;\n }else{\n return false;\n }\n}\n\nvoid solve(){\n ll h,w;cin >> h >> w;\n vector<string> s(h);\n set<char>ch;\n rep(i,h){\n cin >> s[i];\n rep(j,w){\n if(isupper(s[i][j])){\n ch.insert(s[i][j]);\n }\n }\n }\n ll siz = 26;\n if(siz == 0){\n cout << \"SAFE\" << endl;\n return ;\n }\n vector<ll> fin(siz,1);\n for(auto &p:ch){\n fin[p - 'A'] = 0;\n }\n vector<pair<ll,ll>> lr(siz,{INF,-INF}),ud(siz,{INF,-INF});\n auto getid = [](char x){\n return x - 'A';\n };\n\n rep(i,h){\n rep(j,w){\n if(isupper(s[i][j])){\n ll id =getid(s[i][j]);\n chmin(lr[id].first,j);\n chmax(lr[id].second,j);\n chmin(ud[id].first,i);\n chmax(ud[id].second,i);\n }\n }\n }\n while(true){\n bool isfin = true;\n rep(i,siz){\n if(fin[i])continue;\n bool able = true;\n for(ll x = lr[i].first;x <= lr[i].second;x++)for(ll y = ud[i].first ;y <= ud[i].second;y++){\n if(s[y][x] == char('A' + i) or s[y][x] == '*'){\n\n }else{\n able = false;\n }\n }\n if(able){\n for(ll x = lr[i].first;x <= lr[i].second;x++)for(ll y = ud[i].first ;y <= ud[i].second;y++){\n s[y][x] = '*';\n }\n fin[i] = 1;\n isfin = false;\n }\n }\n if(isfin){\n break;\n }\n }\n\n if(*min_element(fin.begin(),fin.end()) == 0){\n cout << \"SUSPICIOUS\" << endl;\n }else{\n cout << \"SAFE\" << endl;\n }\n}\n\n\nint main(){\n ll t;cin >> t;\n while(t--){\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": -0.7255, "final_rank": 8 }, { "submission_id": "aoj_2002_9259957", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,a,b) for(int i=a;i<b;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define dbgv(x); for(auto now : x) cout << now << \" \"; cout << endl;\nvoid chmin(int &a,int b){if(a > b) a = b;}\nvoid chmax(int &a,int b){if(a < b) a = b;}\nint main(){\n int n; cin >> n;\n while(n--){\n int h,w;\n cin >> h >> w;\n vector<pair<int,int>> mn(30,make_pair(-1,-1)),mx(30,make_pair(-1,-1));\n vector<string> s(h); rep(i,h) cin >> s[i];\n rep(i,h)rep(j,w)if(s[i][j] != '.'){\n if(mn[s[i][j]-'A'].first == -1){\n mn[s[i][j]-'A'].first = i;\n mn[s[i][j]-'A'].second = j;\n mx[s[i][j]-'A'].first = i;\n mx[s[i][j]-'A'].second = j;\n }else{\n chmin(mn[s[i][j]-'A'].first,i);\n chmin(mn[s[i][j]-'A'].second,j);\n chmax(mx[s[i][j]-'A'].first,i);\n chmax(mx[s[i][j]-'A'].second,j);\n }\n }\n vector<int> v;\n rep(i,30)if(mn[i].first != -1){\n v.push_back(i);\n }\n auto ch = [&](int x)->bool{\n auto [sx,sy] = mn[x];\n auto [tx,ty] = mx[x];\n for(int i = sx;i <= tx;i++)for(int j = sy;j <= ty;j++){\n if(s[i][j]-'A' != x && s[i][j] != '#') return false;\n }\n return true;\n };\n auto val = [&](int x)->void{\n auto [sx,sy] = mn[x];\n auto [tx,ty] = mx[x];\n for(int i = sx;i <= tx;i++)for(int j = sy;j <= ty;j++){\n s[i][j] = '#';\n }\n };\n if(v.size() == 0){\n cout << \"SAFE\" << endl;\n continue;\n }\n while(1){\n vector<int> nv;\n for(int x : v){\n if(ch(x)){\n val(x);\n }else{\n nv.push_back(x);\n }\n }\n if(nv.size() == v.size()){\n break;\n }\n swap(nv,v);\n }\n if(v.size() == 0){\n cout << \"SAFE\" << endl;\n }else{\n cout << \"SUSPICIOUS\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3360, "score_of_the_acc": -0.6961, "final_rank": 7 }, { "submission_id": "aoj_2002_9036256", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nint main(void) {\n int tc;\n cin >> tc;\n while(tc--) {\n int h, w;\n cin >> h >> w;\n vector<string> s(h);\n vector<char> c;\n vector<int> mi_x, ma_x, mi_y, ma_y;\n map<char, int> mp;\n rep(i, 0, h) {\n cin >> s[i];\n rep(j, 0, w) {\n if(s[i][j] == '.') continue;\n if(mp.find(s[i][j]) == mp.end()) {\n c.push_back(s[i][j]);\n mp[s[i][j]] = mp.size();\n mi_x.push_back(i);\n ma_x.push_back(i);\n mi_y.push_back(j);\n ma_y.push_back(j);\n } else {\n mi_x[mp[s[i][j]]] = min(mi_x[mp[s[i][j]]], (int)i);\n ma_x[mp[s[i][j]]] = max(ma_x[mp[s[i][j]]], (int)i);\n mi_y[mp[s[i][j]]] = min(mi_y[mp[s[i][j]]], (int)j);\n ma_y[mp[s[i][j]]] = max(ma_y[mp[s[i][j]]], (int)j);\n }\n }\n }\n vector<int> p(mp.size());\n rep(i, 0, (int)p.size()) {\n p[i] = i;\n }\n bool ans = false;\n do {\n vector<vector<char>> t(h, vector<char>(w, '.'));\n rep(i, 0, (int)p.size()) {\n rep(j, mi_x[p[i]], ma_x[p[i]] + 1) {\n rep(k, mi_y[p[i]], ma_y[p[i]] + 1) {\n t[j][k] = c[p[i]];\n }\n }\n }\n bool flag = true;\n rep(i, 0, h) {\n rep(j, 0, w) {\n if(s[i][j] != t[i][j]) {\n flag = false;\n }\n }\n }\n if(flag) {\n ans = true;\n }\n } while(next_permutation(p.begin(), p.end()));\n if(ans) {\n cout << \"SAFE\" << '\\n';\n } else {\n cout << \"SUSPICIOUS\" << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 2540, "memory_kb": 3352, "score_of_the_acc": -1.2148, "final_rank": 17 }, { "submission_id": "aoj_2002_8996050", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nstring solve(int H, int W){\n vc<string> S(H); rep(i, H) cin >> S[i];\n map<char, pair<int, int>> X, Y;\n rep(i, H) rep(j, W) if (S[i][j] != '.'){\n if (!X.count(S[i][j])){\n X[S[i][j]] = {inf, -1};\n Y[S[i][j]] = {inf, -1};\n }\n X[S[i][j]].first = min(X[S[i][j]].first, (int)i);\n X[S[i][j]].second = max(X[S[i][j]].second, (int)i);\n Y[S[i][j]].first = min(Y[S[i][j]].first, (int)j);\n Y[S[i][j]].second = max(Y[S[i][j]].second, (int)j);\n }\n vc<char> erased = {'.'};\n while (len(erased)){\n for (auto x : erased) if (X.count(x)){\n X.erase(x);\n Y.erase(x);\n }\n erased.clear();\n for (auto [k, v] : X){\n auto [si, gi] = v;\n auto [sj, gj] = Y[k];\n bool flag = true;\n srep(i, si, gi + 1) srep(j, sj, gj + 1) if (S[i][j] != k && S[i][j] != '#') flag = false;\n if (flag){\n srep(i, si, gi + 1) srep(j, sj, gj + 1) S[i][j] = '#';\n erased.push_back(k);\n }\n }\n }\n rep(i, H) rep(j, W) if (S[i][j] != '.' && S[i][j] != '#') return \"SUSPICIOUS\";\n return \"SAFE\";\n}\n\nint main(){\n int T; cin >> T;\n vc<string> ans;\n while (T--){\n int H, W; cin >> H >> W;\n ans.push_back(solve(H, W));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3236, "score_of_the_acc": -0.3922, "final_rank": 3 }, { "submission_id": "aoj_2002_8384593", "code_snippet": "#include<iostream>\nusing namespace std;\nconst int NUM = 8;\nconst int CONST = 51;\n\nclass Rectangle {\npublic:\n\tint left;\n\tint right;\n\tint top;\n\tint bottom;\n\tchar material;\n\n\tvoid check(int h, int w) {\n\t\tif (w < left){\n\t\t\tleft = w;\n\t\t}\n\t\tif (w > right){\n\t\t\tright = w;\n\t\t}\n\t\tif (h < top){\n\t\t\ttop = h;\n\t\t}\n\t\tif (h > bottom){\n\t\t\tbottom = h;\n\t\t}\n\t}\n} ;\n\nclass XRayScreening {\npublic:\n\tint hight, width;\n\tint mateN;\n\tchar ch;\n\n\tRectangle rct[NUM];\n\tint data[CONST][CONST] = {};\n\tbool position[NUM][NUM] = {};\n\n\tXRayScreening(int h, int w) {\n\t\thight = h;\n\t\twidth = w;\n\t\tmateN = 0;\n\t\tch = '\\0';\n\t}\n\tvoid set_data() {\n\t\tfor (int i = 1; i <= hight; ++i) {\n\t\t\tfor (int j = 1; j <= width; ++j) {\n\t\t\t\tcin >> ch;\n\n\t\t\t\tif (ch == '.') {\n\t\t\t\t\tdata[i][j] = 0;\n\t\t\t\t} else {\n\t\t\t\t\tint k = 1;\n\n\t\t\t\t\twhile (k <= mateN) {\n\t\t\t\t\t\tif (ch == rct[k].material) {\n\t\t\t\t\t\t\tdata[i][j] = k;\n\t\t\t\t\t\t\trct[k].check(i, j);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tk++;\n\t\t\t\t\t}\n\t\t\t\t\tif (k == mateN + 1) {\n\t\t\t\t\t\tdata[i][j] = k;\n\t\t\t\t\t\trct[k].material = ch;\n\t\t\t\t\t\trct[k].left = j;\n\t\t\t\t\t\trct[k].right = j;\n\t\t\t\t\t\trct[k].top = i;\n\t\t\t\t\t\trct[k].bottom = i;\n\t\t\t\t\t\tmateN++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tbool check_xray() {\n\t\tbool flag = true;\n\n\t\tfor (int i = 1; i <= mateN; i++) {\n\t\t\tfor (int j = rct[i].top; j <= rct[i].bottom; ++j) {\n\t\t\t\tfor (int k = rct[i].left; k <= rct[i].right; ++k) {\n\n\t\t\t\t\tif (data[j][k] == 0) {\n\t\t\t\t\t\tflag = false;\n\n\t\t\t\t\t} else if (data[j][k] != i) {\n\n\t\t\t\t\t\tif (position[i][data[j][k]] == false) {\n\t\t\t\t\t\t\tposition[i][data[j][k]] = true;\n\n\t\t\t\t\t\t\tfor (int l = 1; l <= mateN; l++) {\n\n\t\t\t\t\t\t\t\tposition[i][l] = (position[i][l] || position[data[j][k]][l]);\n\t\t\t\t\t\t\t\tposition[l][data[j][k]] = (position[l][i] || position[l][data[j][k]]);\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 1; i <= mateN; ++i) {\n\t\t\tif (position[i][i] == true)\n\t\t\t\tflag = false;\n\t\t}\n\t\treturn flag;\n\t}\n} ;\n\nint main() {\n\tint n, h, w;\n\tcin >> n;\n\n\n\tfor (int i = 0; i < n; ++i) {\n\t\tcin >> h >> w;\n\n\t\tXRayScreening xrs(h, w);\n\t\txrs.set_data();\n\n\t\tif (xrs.check_xray())\n\t\t\tcout << \"SAFE\\n\";\n\t\telse\n\t\t\tcout << \"SUSPICIOUS\\n\";\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3076, "score_of_the_acc": -0.0021, "final_rank": 1 }, { "submission_id": "aoj_2002_7999073", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N;\n cin>>N;\n while(N--){\n int H,W;\n cin>>H>>W;\n vector<vector<char>> A(H,vector<char>(W));\n map<char,array<int,4>>C;\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n cin>>A[i][j];\n if(A[i][j]!='.'){\n if(C.find(A[i][j])==C.end()){\n C[A[i][j]]={100,100,-1,-1};\n }\n C[A[i][j]][0]=min(C[A[i][j]][0],i);\n C[A[i][j]][1]=min(C[A[i][j]][1],j);\n C[A[i][j]][2]=max(C[A[i][j]][2],i);\n C[A[i][j]][3]=max(C[A[i][j]][3],j);\n }\n }\n }\n bool ans=!C.empty();\n vector<pair<char,array<int,4>>>S;\n for(auto i:C)S.push_back(i);\n do{\n bool now=true;\n vector<vector<bool>>ok(H,vector<bool>(W));\n for(int i=0;i<S.size();i++){\n for(int j=S[i].second[0];j<=S[i].second[2];j++){\n for(int k=S[i].second[1];k<=S[i].second[3];k++){\n if(!ok[j][k]&&A[j][k]!=S[i].first)now=false;\n ok[j][k]=true;\n }\n }\n }\n if(now)ans=false;\n }while(next_permutation(S.begin(),S.end()));\n cout<<(ans?\"SUSPICIOUS\\n\":\"SAFE\\n\");\n }\n}", "accuracy": 1, "time_ms": 4710, "memory_kb": 3416, "score_of_the_acc": -1.8333, "final_rank": 20 }, { "submission_id": "aoj_2002_7997549", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nbool is_end = false;\n\nvoid calc()\n{\n\tll H, W; cin >> H >> W;\n\tvector<vector<ll>> vec(H, vector<ll>(W));\n\tset<ll> P;\n\tfor (int i = 0; i < H; ++i)\n\t{\n\t\tstring S; cin >> S;\n\t\tfor (int j = 0; j < W; ++j)\n\t\t{\n\t\t\tif (S[j] == '.') {vec[i][j] = -1;}\n\t\t\telse {vec[i][j] = S[j] - 'A'; P.insert(vec[i][j]);}\n\t\t}\n\t}\n\t\n\tconst ll INF = 1e15;\n\tvector<ll> minx(26, INF), maxx(26, -1), miny(26, INF), maxy(26, -1);\n\tfor (ll i = 0; i < H; ++i)\n\t{\n\t\tfor (ll j = 0; j < W; ++j)\n\t\t{\n\t\t\tint v = vec[i][j];\n\t\t\tif (v == -1) {continue;}\n\t\t\t\n\t\t\tminx[v] = min(minx[v], i);\n\t\t\tmaxx[v] = max(maxx[v], i);\n\t\t\tminy[v] = min(miny[v], j);\n\t\t\tmaxy[v] = max(maxy[v], j);\n\t\t}\n\t}\n\tfor (int i = 0; i < 26; ++i)\n\t{\n\t\tif (minx[i] == INF) {minx[i] = -1;}\n\t\tif (miny[i] == INF) {miny[i] = -1;}\n\t}\n\n\tvector<vector<ll>> copy = vec;\n\tll done = 0;\n\twhile (1)\n\t{\n\t\tll color = -1;\n\t\tfor (auto p : P)\n\t\t{\n\t\t\tbool isok = true;\n\t\t\tfor (int x = minx[p]; x <= maxx[p]; ++x)\n\t\t\t{\n\t\t\t\tfor (int y = miny[p]; y <= maxy[p]; ++y)\n\t\t\t\t{\n\t\t\t\t\tif (copy[x][y] != p && copy[x][y] != 100) {isok = false;}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif (isok) {color = p;}\n\t\t}\n\t\t\n\t\tif (color == -1) {break;}\n\t\t\n\t\tP.erase(color);\n\t\tfor (int x = 0; x < H; ++x)\n\t\t{\n\t\t\tfor (int y = 0; y < W; ++y)\n\t\t\t{\n\t\t\t\tif (copy[x][y] == color)\n\t\t\t\t{\n\t\t\t\t\tcopy[x][y] = 100;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t}\n\t\n\tstring res = \"SUSPICIOUS\";\n\tif (P.size() == 0) {res = \"SAFE\";}\n\t\n\tcout << res << endl;\n\t\n\treturn;\n}\n\nint main()\n{\n\tll T; cin >> T;\n\twhile (T--)\n\t{\n\t\tcalc();\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3460, "score_of_the_acc": -0.9412, "final_rank": 13 } ]
aoj_2001_cpp
Amida, the City of Miracle 奇跡の都市アミダ(Amida, the City of Miracle)の市長は他の市のように選挙で選ぶのではない.かつて長い政権闘争と天変地異により疲弊したこの都市は,候補者全員を公平に選び,かつ幸運の星の下に生まれた者を市長にするために,候補者全員の運命をくじにゆだねることにしたのである.後にあみだくじと呼ばれるくじである. 選挙は以下のように行われる.候補者数と同数の長い縦線が引かれ,さらにいくつかの横線がそこに引かれる.横線は隣接した縦線の途中同士を結ぶように引かれる.いずれかの縦線の下には「当選」と書かれている.どの縦線が当選であるかは候補者からわからないように隠される.それぞれの候補者は縦線を 1 つ選択する.全ての候補者の選択が終わると,各候補者は線を上から下に向かってたどる.ただし,移動途中に横線が見つかったときは,横線の接続された反対側の縦線に移り,また下に向かってたどる.縦線の一番下までたどり着いたときに,そこに「当選」と書かれていた者が当選して,次期市長となる. この方法はうまくいった.幸運な市長の下に,争いも災害も少ない平和な時代が続いた.しかし近年,人口増加のためにこの手法に限界が見えてきた.候補者が増えたため,あみだくじが大規模になり,手作業による集計では非常に時間がかかるようになったのである.そこで市では,市役所のプログラマーであるあなたにあみだくじの電算化を依頼した. あなたの仕事は,あみだくじの構造と,選択された縦線の位置が与えられたときに,最終的にどの縦線にたどり着くかを求めるプログラムを書くことである. 以下の図は,サンプルとして与えた入力および出力の内容を示したものである. Input 入力は複数のデータセットからなる.1 つのデータセットは以下のように与えられる. n m a 横線1 横線2 横線3 ... 横線 m n , m , a はそれぞれ 2 <= n <= 100, 0 <= m <= 1000, 1 <= a <= n をみたす整数であり,それぞれ縦線の本数,横線の本数,調べる縦線の番号を表す. 横線のデータは以下のように与えられる. h p q h , p , q はそれぞれ 1 <= h <= 1000, 1 <= p < q <= n , を満たす整数であり, h はその横線の高さ, p , q はその横線につながっている 2 本の縦線の番号を表す. 1 つの縦線の同じ高さに,異なる横線が 2 つ以上つくことはない. 入力の終わりには,空白で区切られた 3 つのゼロのみからなる行がある。 Output それぞれのデータセットに対して 1 行ずつ,縦線 a の上端からたどったときの下端における縦線の番号を出力しなさい. Sample Input 4 4 1 3 1 2 2 2 3 3 3 4 1 3 4 0 0 0 Output for the Sample Input 4
[ { "submission_id": "aoj_2001_10849592", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n#define FOR(i,s,t) for (int i = s; i < t; i++)\n#define SZ(x) (int)x.size()\nusing LL = long long; using ll = LL;\nusing VI = vector<int>; using VL = vector<LL>;\nconst LL INF = 1e9; const LL LINF = 1e18;\n\ntypedef pair<ll, ll> pll;\nll solve(ll n,ll m,ll a) {\n\tll res = -1;\n\tvector<pair<ll, pll>> x;\n\tvector<ll> amida(n + 1, 0);\n\tiota(amida.begin(), amida.end(), 0);\n\tfor (int i = 0; i < m; i++) {\n\t\tll h, p, q; cin >> h >> p >> q;\n\t\tx.push_back({ h,{p,q} });\n\t}\n\tsort(x.begin(), x.end());\n\tfor (int i = 0; i < m; i++) {\n\t\tswap(amida[x[i].second.first], amida[x[i].second.second]);\n\t}\n\tres = amida[a];\n\treturn res;\n}\n\nint main() {\n\n\tint n, m, a;\n\twhile(cin >> n >> m >> a,n | m | a) {\n\t\tcout << solve(n, m, a) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3192, "score_of_the_acc": -0.0103, "final_rank": 3 }, { "submission_id": "aoj_2001_10582335", "code_snippet": "// AOJ 2001 - Amida, the City of Miracle\n// JAG Practice Contest for Japan Domestic 2006 - Problem B\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nbool solving() {\n int n, m, a; cin >> n >> m >> a;\n if(n == 0) return false;\n\n vector<pair<int, pair<int,int>>> line(m);\n for(int i=0; i<m; ++i) {\n cin >> line[i].first >> line[i].second.first >> line[i].second.second;\n }\n sort(line.begin(), line.end(), greater());\n\n for(int i=0; i<m; ++i) {\n if(a == line[i].second.first) {\n a = line[i].second.second;\n } else if(a == line[i].second.second) {\n a = line[i].second.first;\n }\n }\n cout << a << endl;\n return true;\n}\n\nint main() {\n while(solving()) {}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0241, "final_rank": 12 }, { "submission_id": "aoj_2001_10553833", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M, id;\n cin >> N >> M >> id;\n if (N == 0 && M == 0 && id == 0) return false;\n id--;\n vector<tuple<int, int, int>> amida;\n while (M--) {\n int h, p, q;\n cin >> h >> p >> q;\n amida.emplace_back(h, p - 1, q - 1);\n }\n sort(amida.begin(), amida.end());\n vector<int> A(N);\n iota(A.begin(), A.end(), 1);\n for (auto [_, p, q] : amida) {\n swap(A[p], A[q]);\n }\n cout << A[id] << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0241, "final_rank": 12 }, { "submission_id": "aoj_2001_10147489", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while (true) {\n int n, m, a;\n cin >> n >> m >> a;\n if (n == 0) {\n break;\n }\n vector<int> h(m), p(m), q(m);\n\n int mh = 0;\n for (int i = 0; i < m; i++) {\n cin >> h[i] >> p[i] >> q[i];\n mh = max(mh, h[i]);\n }\n\n vector<vector<int>> E(mh + 1, vector<int>(n + 1, -1));\n for (int i = 0; i < m; i++) {\n E[mh - h[i]][p[i]] = q[i];\n E[mh - h[i]][q[i]] = p[i];\n }\n\n int nw = a, nh = 0;\n while (true) {\n if (nh == mh) {\n break;\n }\n if (E[nh][nw] == -1) {\n nh++;\n } else {\n nw = E[nh][nw];\n nh++;\n }\n }\n cout << nw << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3652, "score_of_the_acc": -0.0344, "final_rank": 17 }, { "submission_id": "aoj_2001_9402726", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main() {\n while (1) {\n int n, m, a;\n cin >> n >> m >> a;\n if (n == 0 && m == 0 && a == 0) {\n break;\n }\n a--;\n vector<tuple<int, int, int>> A;\n for (int i = 0; i < m; i++) {\n int h, p, q;\n cin >> h >> p >> q;\n p--;\n q--;\n A.emplace_back(h, p, q);\n }\n sort(A.begin(), A.end());\n vector<int> C(n);\n iota(C.begin(), C.end(), 0);\n for (int i = 0; i < m; i++) {\n auto [h, p, q] = A[i];\n swap(C[p], C[q]);\n }\n cout << C[a] + 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.0187, "final_rank": 7 }, { "submission_id": "aoj_2001_9377674", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nint main(){\n while(true){\n int n,m,a;\n cin >> n >> m >> a;\n if(n==0&&m==0&&a==0)break;\n vector<tuple<int,int,int>>G(m);\n\n for(int i=0;i<m;i++){\n int h_,p_,q_;\n cin >> h_ >> p_ >> q_;\n G[i]={h_,p_,q_};\n }\n\n sort(G.rbegin(),G.rend());\n int pos=a;\n int idx=1000;\n for(int i=0;i<G.size();i++){\n if(get<0>(G[i])==idx)continue;\n if(get<1>(G[i])==pos){\n pos=get<2>(G[i]);\n idx=get<0>(G[i]);\n }else if(get<2>(G[i])==pos){\n pos=get<1>(G[i]);\n idx=get<0>(G[i]);\n }\n }\n cout << pos << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3340, "score_of_the_acc": -0.018, "final_rank": 6 }, { "submission_id": "aoj_2001_9299604", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst int inf = 1e9;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nint n,m,a;\n\nvoid solve() {\n vector<vector<int>> v (m,vector<int> (3));\n rep(i,m) {\n cin >> v[i][0] >> v[i][1] >> v[i][2];\n }\n \n sort(all(v));\n reverse(all(v));\n \n rep(i,m) {\n if (a == v[i][1]) {\n a = v[i][2];\n }\n else if (a == v[i][2]) {\n a = v[i][1];\n }\n }\n cout << a << endl;\n return;\n}\n\nint main() {\n while (true) {\n cin >> n >> m >> a;\n if (n == 0) {\n return 0;\n }\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3452, "score_of_the_acc": -0.0239, "final_rank": 11 }, { "submission_id": "aoj_2001_9208544", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nint ctoi(char c){return c-'0';}\nll ctoll(char c){return c-'0';}\n\nvoid solve(int n, int m, int a){\n set<tuple<int,int,int>>amida;\n rep(i,m){\n int h,p,q;\n cin >> h >> p >> q;\n p--;q--;\n amida.insert(make_tuple(-h,p,q));\n amida.insert(make_tuple(-h,q,p));\n }\n\n a--;\n int now = a;\n int takasa = -1001;\n for(auto v : amida){\n int h,from,to;\n tie(h,from,to) = v;\n if(h > takasa && now == from){\n takasa = h;\n now = to;\n }\n }\n cout << now+1 << endl;\n}\n\nint main(){\n while(true){\n int n,m,a;\n cin >> n >> m >> a;\n if(n==0&&m==0&&a==0)break;\n\n solve(n,m,a);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3444, "score_of_the_acc": -0.0235, "final_rank": 10 }, { "submission_id": "aoj_2001_8975184", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint solve(int N, int M, int A){\n vc<tuple<int, int, int>> edge(M);;\n rep(i, M){\n int h, p, q; cin >> h >> p >> q;\n p--; q--;\n edge[i] = {h, p, q};\n }\n sort(all(edge), [&](auto i, auto j){return i > j;});\n vi P(N); rep(i, N) P[i] = i + 1;\n for (auto [h, p, q] : edge) swap(P[p], P[q]);\n rep(i, N) if (P[i] == A) return i + 1;\n return -1;\n}\n\nint main(){\n vc<int> ans;\n while (true){\n int N, M, A; cin >> N >> M >> A;\n if (N == 0) break;\n ans.push_back(solve(N, M, A));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.0082, "final_rank": 2 }, { "submission_id": "aoj_2001_8964710", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(true) {\n int n, m, a;\n cin >> n >> m >> a;\n\n if (n == 0 && m == 0 && a == 0) {\n break;\n }\n\n int h[m], p[m], q[m];\n for (int i = 0; i < m; i++) {\n cin >> h[i] >> p[i] >> q[i];\n }\n\n int memo[1001][n+1];\n for (int i = 0; i < n+1; i++) {\n for (int j = 0; j < 1001; j++) {\n memo[j][i] = i;\n }\n }\n\n for (int i = 0; i < m; i++) {\n memo[h[i]][p[i]] = q[i];\n memo[h[i]][q[i]] = p[i];\n }\n\n int pos = a;\n for (int i = 1000; i >= 1; i--) {\n pos = memo[i][pos];\n }\n\n cout << pos << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3688, "score_of_the_acc": -0.0363, "final_rank": 18 }, { "submission_id": "aoj_2001_8964709", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(true) {\n int n, m, a;\n cin >> n >> m >> a;\n\n if (n == 0 && m == 0 && a == 0) {\n break;\n }\n\n int h[m], p[m], q[m];\n for (int i = 0; i < m; i++) {\n cin >> h[i] >> p[i] >> q[i];\n }\n\n int pos = a, high = 2000;\n while(true) {\n int nhigh = 0, idx = -1;\n for (int i = 0; i < m; i++) {\n if ((p[i] == pos || q[i] == pos) && nhigh < h[i] && h[i] < high) {\n idx = i;\n nhigh = h[i];\n } \n }\n if (idx == -1) {\n break;\n }\n high = nhigh;\n if (p[idx] == pos) {\n pos = q[idx];\n } else {\n pos = p[idx];\n }\n }\n\n cout << pos << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3212, "score_of_the_acc": -0.0113, "final_rank": 4 }, { "submission_id": "aoj_2001_8372758", "code_snippet": "#include <iostream>\nusing namespace std;\n\nclass Amida {\npublic:\n int longNum, travNum, selectNum;\n int **array;\n\n Amida(int n, int m, int a) {\n longNum = n;\n travNum = m;\n selectNum = a;\n\n array = new int* [1001];\n for (int i = 0; i <= 1000; ++i)\n array[i] = new int [longNum + 1];\n \n for (int i = 0; i <= 1000; ++i)\n for (int j = 0; j <= longNum; ++j)\n array[i][j] = 0;\n }\n void set_traverse() {\n int h, p, q;\n\n for (int i = 1; i <= travNum; ++i) {\n cin >> h >> p >> q;\n\n array[h][p] = q;\n array[h][q] = p;\n }\n }\n int selection() {\n for (int i = 1000; i > 0; --i) {\n\n if (array[i][selectNum]) {\n selectNum = array[i][selectNum];\n }\n } \n return selectNum;\n }\n} ;\n\nint main() {\n int n, m, a;\n cin >> n >> m >> a;\n while (n && m && a) {\n Amida ad(n, m, a);\n\n ad.set_traverse();\n\n cout << ad.selection() << '\\n';\n\n cin >> n >> m >> a;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 22072, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_2001_8345595", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int, int> Pi;\ntypedef pair<int, Pi> PPi;\nint main(){\n int n, m, a, h, p, q;\n while(cin >> n >> m >> a, n){\n vector<PPi> vec;\n int num[101];\n for(int i=1;i<=n;i++) num[i] = i;\n for(int i=0;i<m;i++){\n cin >> h >> p >> q;\n vec.push_back(PPi(h, Pi(p, q)));\n }\n sort(vec.begin(), vec.end());\n for(int i=0;i<m;i++){\n Pi pi = vec[i].second;\n swap(num[pi.first], num[pi.second]);\n }\n cout << num[a] << endl;\n }\n return(0);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2996, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2001_8222857", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <stdio.h>\n#include <math.h>\n#include <set>\n#include <map>\n#include <queue>\n#include <cassert>\n#include <numeric>\n#include <cstdio>\n\n\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define P pair<int, int>\n\n\nint main() {\n\tint n, m, a;\n\tint h, p, q;\n\tint position=0;\n\n\tcin >> n >> m >> a;\n\twhile (n != 0) {\n\t\tvector<vector<int>>table(n, vector<int>(1001,-1));\n\t\tfor (int i = 0;i < m;i++) {\n\t\t\tcin >> h >> p >> q;\n\t\t\tp--;q--;\n\t\t\ttable[p][h] = q;\n\t\t\ttable[q][h] = p;\n\t\t}\n\t\tfor (int i = 0;i < n;i++) {\n\t\t\treverse(table[i].begin(), table[i].end());\n\t\t}\n\t\tposition = a - 1;\n\t\tfor (int i = 0;i < 1001;i++) {\n\t\t\tif (table[position][i] != -1) {\n\t\t\t\tposition = table[position][i];\n\t\t\t}\n\t\t}\n\t\tcout << position + 1 << endl;\n\t\tcin >> n >> m >> a;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3516, "score_of_the_acc": -0.0273, "final_rank": 15 }, { "submission_id": "aoj_2001_8090917", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nint main() {\n while (1) {\n int n, m, a;\n cin >> n >> m >> a;\n\n if (n == 0 && m == 0 && a == 0) {\n break;\n }\n\n vector<vector<pair<int, int>>> amida(1001);\n\n for (int i = 0; i < m; i++) {\n int h, p, q;\n cin >> h >> p >> q;\n pair<int, int> tmp(p, q);\n amida[h].push_back(tmp);\n }\n\n int curr = a;\n for (int i = 1000; i > 0; i--) {\n int length = amida[i].size();\n for (int j = 0; j < length; j++) {\n if (curr == amida[i][j].first) {\n curr = amida[i][j].second;\n break;\n } else if (curr == amida[i][j].second) {\n curr = amida[i][j].first;\n break;\n }\n }\n }\n\n cout << curr << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.0201, "final_rank": 9 }, { "submission_id": "aoj_2001_8017655", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<queue>\n#include<set>\n\nusing namespace std;\nusing ll = long long;\n\nint n,m,a;\nvoid solve(){\n vector<pair<int,pair<int,int>>> g;\n for(int i = 0;i<m;i++){\n int u,v,h;\n cin>>h>>v>>u;\n u--;v--;\n g.push_back(make_pair(h,make_pair(u,v)));\n }\n sort(g.rbegin(),g.rend());\n a--;\n for(int i = 0;i<m;i++){\n if(g[i].second.first==a) a = g[i].second.second;\n else if(g[i].second.second==a) a= g[i].second.first;\n }\n cout<<a+1<<endl;\n \n}\n\nint main(){\n while(true){\n cin>>n>>m>>a;\n if(n==0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3468, "score_of_the_acc": -0.0247, "final_rank": 14 }, { "submission_id": "aoj_2001_7799496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main () {\n int n, m, a;\n cin >> n >> m >> a;\n \n while (n != 0) {\n int line = 1;\n vector<vector<int>> x(2000, vector<int>(n));\n for (int i = 0; i < m; i++) {\n int h, p, q;\n cin >> h >> p >> q;\n x[h][p - 1] = line;\n x[h][q - 1] = line;\n line++;\n }\n \n int now = a - 1;\n for (int i = 1999; i >= 0; i--) {\n if (x[i][now] != 0) {\n if (x[i][now - 1] == x[i][now]) now--;\n else if (x[i][now + 1] == x[i][now]) now++;\n }\n }\n \n cout << now + 1 << endl;\n \n cin >> n >> m >> a;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3900, "score_of_the_acc": -1.0474, "final_rank": 20 }, { "submission_id": "aoj_2001_7799356", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, N) for(int i = 0; i < N; i++)\nint main(){\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\twhile(1){\n\t\tint N, M, A; cin >> N >> M >> A;\n\t\tif(N == 0) break;\n\t\tif(M == 0){\n\t\t\tcout << A << endl;\n\t\t\tcontinue;\n\t\t}\n\t\tvector<int> h(M), p(M), q(M);\n\t\trep(i, M){\n\t\t\tcin >> h[i] >> p[i] >> q[i]; \n\t\t}\n\n\t\tvector<vector<int>> G(1010, vector<int>(110, -1));\n\t\trep(i, M){\n\t\t\tG[h[i]][p[i]] = q[i];\n\t\t\tG[h[i]][q[i]] = p[i];\n\t\t}\n\t\tfor(int i = 1005; i >= 0; i--){\n\t\t\tif(G[i][A] != -1){\n\t\t\t\tA = G[i][A];\n\t\t\t}\n\t\t}\n\t\tcout << A << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3548, "score_of_the_acc": -0.0289, "final_rank": 16 }, { "submission_id": "aoj_2001_7799343", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0; i< (n); i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 998244353;\nconst int inf = (1<<30);\n \nint main(){\n\twhile(1){\n\t\tint n,m,a;\n\t\tcin>>n>>m>>a;\n\t\tif(n == 0) break;\n\t\tvector<int> x(n);\n\t\trep(i,n) x[i] = i;\n\t\tvector<pair<int,P>> bou(m);\n\n\t\trep(i,m){\n\t\t\tint h,p,q;\n\t\t\tcin>>h>>p>>q;\n\t\t\tp--;q--;\n\t\t\tbou[i] = {h,{p,q}};\n\t\t}\n\t\tsort(bou.begin(),bou.end());\n\n\t\trep(i,m){\n\t\t\tP p = bou[i].second;\n\t\t\tswap(x[p.first] , x[p.second]);\n\t\t}\n\t\tcout<<x[a-1]+1<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.0187, "final_rank": 7 }, { "submission_id": "aoj_2001_7799308", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing tup = tuple<int, int, int>;\n\nint solve(int N, int M, int A) {\n vector<tup> P(M); \n for (int i = 0 ; i < M ; i++) {\n auto& [h, p, q] = P[i];\n cin >> h >> p >> q;\n }\n sort(P.begin(), P.end());\n reverse(P.begin(), P.end());\n int ans = A;\n for (auto [h, p, q] : P) {\n // cerr << h << ' ' << p << ' ' << q << endl;\n if (ans == p) {\n // cerr << \"ans hit\" << p << \" \" << \"move to\" << q << endl;\n ans = q;\n }\n else if (ans == q) {\n // cerr << \"ans hit\" << q << \" \" << \"move to\" << p << endl;\n ans = p;\n }\n }\n return ans;\n}\n\n\nint main() {\n while (1) {\n int N, M, A; cin >> N >> M >> A;\n if (!N) break;\n int ans = solve(N, M, A);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": -0.0166, "final_rank": 5 } ]
aoj_2007_cpp
Problem B: Make Purse Light Mr. Bill は店で買い物をしている。 彼の財布にはいくらかの硬貨(10 円玉、50 円玉、100 円玉、500 円玉)が入っているが、彼は今この小銭をできるだけ消費しようとしている。 つまり、適切な枚数の硬貨によって品物の代金を払うことで、釣り銭を受け取った後における硬貨の合計枚数を最小にしようとしている。 幸いなことに、この店の店員はとても律儀かつ親切であるため、釣り銭は常に最適な方法で渡される。 したがって、例えば 1 枚の 500 円玉の代わりに 5 枚の 100 円玉が渡されるようなことはない。 また、例えば 10 円玉を 5 枚出して、50 円玉を釣り銭として受け取ることもできる。 ただし、出した硬貨と同じ種類の硬貨が釣り銭として戻ってくるような払いかたをしてはいけない。 例えば、10 円玉を支払いの際に出したにも関わらず、別の 10 円玉が釣り銭として戻されるようでは、完全に意味のないやりとりが発生してしまうからである。 ところが Mr. Bill は計算が苦手なため、実際に何枚の硬貨を使用すればよいかを彼自身で求めることができなかった。 そこで、彼はあなたに助けを求めてきた。 あなたの仕事は、彼の財布の中にある硬貨の枚数と支払い代金をもとに、使用すべき硬貨の種類と枚数を求めるプログラムを書くことである。なお、店員はお釣りに紙幣を使用することはない。 Input 入力にはいくつかのテストケースが含まれる。 それぞれのテストケースは 2 行から構成される。 1 行目には Mr. Bill の支払い金額を円単位で表した 1 つの整数が含まれている。 2 行目には 4 つの整数が含まれており、それらは順に財布の中にある 10 円玉、50 円玉、100 円玉、500 円玉の枚数をそれぞれ表す。 支払い金額は常に 10 円単位である。 すなわち、支払い金額の一円の位は常に 0 である。 また財布の中に、同じ種類の硬貨は最大で 20 枚までしかないと仮定してよい。 支払いが不可能なケースが入力中に与えられることはない。 入力の終了は単一の 0 を含む行によって表される。 Output それぞれのテストケースについて、Mr. Bill が使用すべき硬貨の種類と枚数を出力せよ。 出力の各行には 2 つの整数 c i 、 k i が含まれる。これは支払いの際に c i 円玉を k i 枚使用することを意味している。 複数種類の硬貨を使用する場合は、 c i の小さいほうから順に、必要な行数だけ出力を行うこと。 下記の出力例を参考にせよ。 なお、出力には余計なスペースを含めてはならない。 連続するテストケースの間は空行で区切ること。 Sample Input 160 1 1 2 0 160 1 0 2 10 0 Output for the Sample Input 10 1 50 1 100 1 10 1 100 2
[ { "submission_id": "aoj_2007_10852642", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint main() {\n int n;\n int q = 0;\n while(cin >> n, n) {\n if(q != 0) {\n cout << endl;\n }\n q++;\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n int cnt = 1e9;\n int r1 = 0, r2 = 0, r3 = 0, r4 = 0;\n for(int i=0; i<=a; ++i) {\n for(int j=0; j<=b; ++j) {\n for(int k=0; k<=c; ++k) {\n for(int l=0; l<=d; ++l) {\n int total = 10 * i + 50 * j + 100 * k + 500 * l;\n if(total < n) {\n continue;\n }\n int m = total - n;\n if(m / 500 > 0 && l != 0) {\n continue;\n }\n int t = m / 500;\n m %= 500;\n if(m / 100 > 0 && k != 0) {\n continue;\n }\n t += m / 100;\n m %= 100;\n if(m / 50 > 0 && j != 0) {\n continue;\n }\n t += m / 50;\n m %= 50;\n if(m / 10 > 0 && i != 0) {\n continue;\n }\n t += m / 10;\n t = t + (a - i) + (b - j) + (c - k) + (d - l);\n if(t <= cnt) {\n r1 = i;\n r2 = j;\n r3 = k;\n r4 = l;\n cnt = t;\n }\n }\n }\n }\n }\n if(r1 > 0) {\n cout << 10 << ' ' << r1 << endl;\n }\n if(r2 > 0) {\n cout << 50 << ' ' << r2 << endl;\n }\n if(r3 > 0 ) {\n cout << 100 << ' ' << r3 << endl;\n }\n if(r4 > 0) {\n cout << 500 << ' ' << r4 << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3352, "score_of_the_acc": -0.6217, "final_rank": 8 }, { "submission_id": "aoj_2007_10611923", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvector<char> isprime;\nvoid init() {\n isprime.assign(1000005, 1);\n isprime[0] = 0;\n isprime[1] = 0;\n for(int i = 2; i < 1000005; i++) {\n if(isprime[i]) {\n for(int j = i * 2; j < 1000005; j += i) {\n isprime[j] = 0;\n }\n }\n }\n}\n\nll n, a, b, c, d;\nvoid input() { cin >> n >> a >> b >> c >> d; }\n\nvoid solve() {\n auto otsuri = [](ll a) {\n vector<ll> ret(4, 0);\n while(a >= 500) {\n ret[3]++;\n a -= 500;\n }\n while(a >= 100) {\n ret[2]++;\n a -= 100;\n }\n while(a >= 50) {\n ret[1]++;\n a -= 50;\n }\n while(a >= 10) {\n ret[0]++;\n a -= 10;\n }\n\n return ret;\n };\n\n vector<ll> best;\n ll ans = 100;\n\n vector<ll> v(4, 0), dep = {a, b, c, d};\n auto dfs = [&](auto self, int now) {\n if(now == 4) {\n ll sum = v[0]*10+v[1]*50+v[2]*100+v[3]*500;\n if(sum < n)\n return;\n vector<ll> w = otsuri(sum - n);\n ll cnt = 0;\n for(int i = 0; i < 4; i++) {\n if(v[i] && w[i])\n return;\n cnt += dep[i] - v[i] + w[i];\n }\n if(ans > cnt) {\n best = v;\n ans = cnt;\n }\n } else {\n for(int i = 0; i <= dep[now]; i++) {\n v[now] = i;\n self(self, now + 1);\n }\n }\n };\n dfs(dfs, 0);\n if(best[0])\n cout << \"10 \" << best[0] << endl;\n if(best[1])\n cout << \"50 \" << best[1] << endl;\n if(best[2])\n cout << \"100 \" << best[2] << endl;\n if(best[3])\n cout << \"500 \" << best[3] << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n init();\n input();\n while(1) { \n solve();\n input();\n if(n == 0)\n break;\n cout << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3812, "score_of_the_acc": -1.2615, "final_rank": 17 }, { "submission_id": "aoj_2007_10598923", "code_snippet": "// AOJ 2007 - Make Purse Light\n// JAG Practice Contest for Japan Domestic 2005 - Problem B\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> c{10, 50, 100, 500};\n\nint main() {\n for(int i=0; ; ++i) {\n int p; cin >> p;\n if(p == 0) break;\n if(i > 0) cout << endl;\n vector<int> h(4);\n for(int i=0; i<4; ++i) cin >> h[i];\n int least = 2e9;\n vector<int> k(4), l(4);\n for(k[0]=0; k[0]<=h[0]; ++k[0]) {\n for(k[1]=0; k[1]<=h[1]; ++k[1]) {\n for(k[2]=0; k[2]<=h[2]; ++k[2]) {\n for(k[3]=0; k[3]<=h[3]; ++k[3]) {\n int q = 0, cur = 0;\n for(int i=0; i<4; ++i) {\n q += c[i] * k[i];\n cur += h[i] - k[i];\n }\n if(q < p) continue;\n q -= p;\n for(int i=3; i>=0; --i) {\n cur += q / c[i];\n q %= c[i];\n }\n if(cur < least) {\n least = cur;\n l = k;\n }\n }\n }\n }\n }\n for(int i=0; i<4; ++i) {\n if(l[i]) cout << c[i] << ' ' << l[i] << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3456, "score_of_the_acc": -0.7715, "final_rank": 14 }, { "submission_id": "aoj_2007_10544919", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\nlong long mod_mul(long long a, long long b, long long mod) {\n return (__int128)a * b % mod;\n}\n\nlong long mod_pow(long long a, long long b, long long mod) {\n long long result = 1;\n a %= mod;\n while (b > 0) {\n if (b & 1) result = mod_mul(result, a, mod);\n a = mod_mul(a, a, mod);\n b >>= 1;\n }\n return result;\n}\n\nbool MillerRabin(ll x){\n \n if(x <= 1){return false;}\n if(x == 2){return true;}\n if((x & 1) == 0){return false;}\n vll test;\n if(x < (1 << 30)){\n test = {2, 7, 61};\n } \n else{\n test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n }\n ll s = 0;\n ll d = x - 1;\n while((d & 1) == 0){\n s++;\n d >>= 1;\n }\n for(ll i:test){\n if(x <= i){return true;}\n ll y = mod_pow(i,d,x);\n if(y == 1 || y == x - 1){continue;}\n ll c = 0;\n for(int j=0;j<s-1;j++){\n y = mod_mul(y,y,x);\n if(y == x - 1){\n c = 1;\n break;\n }\n }\n if(c == 0){\n return false;\n }\n }\n return true;\n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n ll t = 0;\n\n while(1){\n LL(x);\n if(x == 0){break;}\n if(t == 1){print();}\n t = 1;\n VLL(a,4);\n ll ans = 1LL<<60;\n vll c;\n rep(i,a[0]+1){\n rep(j,a[1]+1){\n rep(k,a[2]+1){\n rep(l,a[3]+1){\n vll b = a;\n b[0] -= i;\n b[1] -= j;\n b[2] -= k;\n b[3] -= l;\n ll v = 10 * i + 50 * j + 100 * k + 500 * l;\n ll u = v - x;\n if(u < 0){continue;}\n ll cnt = 0;\n while(u >= 500){\n cnt++;\n u -= 500;\n }\n while(u >= 100){\n cnt++;\n u -= 100;\n }\n while(u >= 50){\n cnt++;\n u -= 50;\n }\n while(u >= 10){\n cnt++;\n u -= 10;\n }\n if(ans > sum(b) + cnt){\n ans = sum(b) + cnt;\n c = b;\n //print(b,v);\n }\n \n }\n }\n }\n }\n vll d = {10,50,100,500};\n rep(i,4){\n c[i] = a[i] - c[i];\n if(c[i] >= 1){\n print(d[i],c[i]);\n }\n }\n\n }\n \n}", "accuracy": 1, "time_ms": 520, "memory_kb": 3456, "score_of_the_acc": -0.9274, "final_rank": 15 }, { "submission_id": "aoj_2007_10462464", "code_snippet": "#include <iostream> \nusing namespace std;\n\nint main(){\n int N,A,B,C,D,a,b,c,d,s,cha,r;\n int t=0;\n while(cin>>N>>A>>B>>C>>D && N>0){\n if(t==1){\n cout<<endl;\n }\n t=1;\n s=81;\n for (int i=0; i<=A; i++){\n for (int j=0; j<=B; j++){\n for (int k=0; k<=C; k++){\n for (int l=0; l<=D; l++){\n cha =(10*i)+(50*j)+(100*k)+(500*l)-N;\n if(cha>=0){\n r=0;\n r += cha/500;\n cha = cha % 500;\n r += cha/100;\n cha = cha % 100;\n\n r += cha/50;\n cha = cha % 50;\n r += cha/10;\n if(A+B+C+D-i-j-k-l+r < s){\n s=A+B+C+D-i-j-k-l+r;\n a=i;\n b=j;\n c=k;\n d=l;\n }\n }\n }\n }\n }\n }\n if(a>0){\n cout<<\"10 \"<<a<<endl;\n }\n if(b>0){\n cout<<\"50 \"<<b<<endl;\n }\n if(c>0){\n cout<<\"100 \"<<c<<endl;\n }\n if(d>0){\n cout<<\"500 \"<<d<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3356, "score_of_the_acc": -0.6433, "final_rank": 11 }, { "submission_id": "aoj_2007_9844652", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid solve(int m) {\n vector<int> c(4);\n cin >> c[0] >> c[1] >> c[2] >> c[3];\n const vector<int> v = {10, 50, 100, 500};\n int mn = 10000;\n vector<int> ans(4);\n vector<int> var(4);\n for (var[0] = 0; var[0] <= c[0]; var[0]++) {\n for (var[1] = 0; var[1] <= c[1]; var[1]++) {\n for (var[2] = 0; var[2] <= c[2]; var[2]++) {\n for (var[3] = 0; var[3] <= c[3]; var[3]++) {\n int p = 0;\n for (int q = 0; q < 4; q++) p += var[q] * v[q];\n if (p < m) continue;\n int o = p - m;\n vector<int> ret(4);\n bool ok = true;\n for (int q = 3; q >= 0; q--) {\n if (o / v[q] > 0 && var[q] > 0) ok = false;\n ret[q] = o / v[q] + c[q] - var[q];\n o %= v[q];\n }\n if (ok && mn > ret[0] + ret[1] + ret[2] + ret[3]) {\n mn = ret[0] + ret[1] + ret[2] + ret[3];\n for (int q = 0; q < 4; q++) {\n ans[q] = var[q];\n }\n }\n }\n }\n }\n }\n for (int i = 0; i < 4; i++) {\n if (ans[i]) {\n cout << v[i] << \" \" << ans[i] << endl;\n }\n }\n}\n\nint main() {\n int m;\n cin >> m;\n solve(m);\n cin >> m;\n while (m) {\n cout << endl;\n solve(m);\n cin >> m;\n }\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 3052, "score_of_the_acc": -0.6227, "final_rank": 9 }, { "submission_id": "aoj_2007_9448735", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nint main(){\n bool c=0;\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n if(c)cout<<endl;\n c=1;\n ll a,b,c,d;cin>>a>>b>>c>>d;\n vi ans(4),X={10,50,100,500};\n ll score=1e18;\n auto f=[](ll a){\n ll res=0;\n res+=a/500;a%=500;\n res+=a/100;a%=100;\n res+=a/50;a%=50;\n res+=a/10;\n return res;\n };\n REP(i,a+1)REP(j,b+1)REP(k,c+1)REP(l,d+1)if(10*i+50*j+100*k+500*l>=N){\n ll t=N-10*i-50*j-100*k-500*l;\n if(chmin(score,f(-t)+a-i+b-j+c-k+d-l))ans={i,j,k,l};\n }\n REP(i,4)if(ans[i])cout<<X[i]<<\" \"<<ans[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3324, "score_of_the_acc": -0.6354, "final_rank": 10 }, { "submission_id": "aoj_2007_9444649", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nint main() {\nfor (int _ = 0; ; _++) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n if (_ != 0) cout << endl;\n int X[4] = {10,50,100,500}, Y[4];\n cin >> Y[0] >> Y[1] >> Y[2] >> Y[3];\n int MIN = inf, ANS[4];\n rep(l,0,Y[3]+1) {\n rep(k,0,Y[2]+1) {\n rep(j,0,Y[1]+1) {\n rep(i,0,Y[0]+1) {\n int COUNT = X[0] * i + X[1] * j + X[2] * k + X[3] * l;\n if (COUNT >= N) {\n COUNT -= N;\n int Z[4];\n rep(m,0,3) {\n Z[m] = (COUNT % X[m+1]) / X[m];\n }\n Z[3] = COUNT / X[3];\n if (chmin(MIN,Z[0]+Z[1]+Z[2]+Z[3]+X[0]-i+X[1]-j+X[2]-k+X[3]-l)) {\n ANS[0] = i, ANS[1] = j, ANS[2] = k, ANS[3] = l;\n }\n }\n }\n }\n }\n }\n rep(i,0,4) {\n if (ANS[i] != 0) {\n cout << X[i] << ' ' << ANS[i] << endl;\n }\n }\n}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3184, "score_of_the_acc": -0.5065, "final_rank": 5 }, { "submission_id": "aoj_2007_9369520", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst vector<int> C = {10, 50, 100, 500};\nconst int INF = 1e9;\nint main() {\n int test = 0;\n while (1) {\n int n;\n cin >> n;\n if (n == 0) break;\n if (test) {\n cout << '\\n';\n }\n test++;\n vector<int> A(4);\n for (int i = 0; i < 4; i++) {\n cin >> A[i];\n }\n int MIN = INF;\n vector<int> ans(4);\n for (int i = 0; i <= A[0]; i++) {\n for (int j = 0; j <= A[1]; j++) {\n for (int k = 0; k <= A[2]; k++) {\n for (int l = 0; l <= A[3]; l++) {\n int sum = i * 10 + j * 50 + k * 100 + l * 500;\n sum -= n;\n if (sum < 0) continue;\n int cnt = A[0] - i + A[1] - j + A[2] - k + A[3] - l;\n for (int m = 3; m >= 0; m--) {\n cnt += sum / C[m];\n sum -= C[m] * (sum / C[m]);\n }\n if (cnt < MIN) {\n MIN = cnt;\n ans[0] = i;\n ans[1] = j;\n ans[2] = k;\n ans[3] = l;\n }\n }\n }\n }\n }\n for (int i = 0; i < 4; i++) {\n if (!ans[i]) continue;\n cout << C[i] << ' ' << ans[i] << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3136, "score_of_the_acc": -0.5266, "final_rank": 6 }, { "submission_id": "aoj_2007_9358626", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\n//オーバーフローしているか確認する\n//小さいケースで動いているかを確認する。\nvl coins = {10,50,100,500};\nvoid solve(){\n vector<vl> answer;\n while(true){\n ll X;cin>>X;\n if(X==0)break;\n ll a,b,c,d;cin>>a>>b>>c>>d;\n vl out = {a,b,c,d};\n vl F = {a,b,c,d};\n ll ans = INF;\n rep(A,a+1)rep(B,b+1)rep(C,c+1)rep(D,d+1){\n //それぞれA,B,C,D使った時の値\n ll now = 10*A+50*B+100*C+500*D;\n ll diff = now- X;\n if(diff < 0)continue;\n vl num(4); \n num[0] = a-A;\n num[1] = b-B;\n num[2] = c-C;\n num[3] = d-D;\n ll sum = 0;\n rep(k,4)sum+=num[k];\n for(int d=3;d>=0;d--){\n sum+=diff/coins[d];\n num[d]+=diff/coins[d];\n diff%=coins[d];\n }\n if(chmin(ans,sum))out={A,B,C,D};\n }\n answer.push_back(out);\n }\n const int n = answer.size();\n rep(i,n){\n rep(d,4){\n if(answer[i][d]==0)continue;\n cout<<coins[d]<<\" \"<<answer[i][d]<<endl;\n }\n if(i!=n-1)cout<<endl;\n }\n}", "accuracy": 1, "time_ms": 1340, "memory_kb": 3540, "score_of_the_acc": -1.3726, "final_rank": 18 }, { "submission_id": "aoj_2007_9344618", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst int inf = 1e9;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nint n;\n\nvoid solve() {\n int a,b,c,d;\n int aq,bq,cq,dq;\n cin >> a >> b >> c >> d;\n ll ans = INF;\n ll pay = INF;\n rep(aa,a+1) {\n rep(bb,b+1) {\n rep(cc,c+1) {\n rep(dd,d+1) {\n int sm = aa*10+bb*50+cc*100+dd*500;\n if (sm < n) {\n continue;\n }\n int now = 0;\n int change = sm-n;\n while (change >= 500) {\n change -= 500;\n now++;\n }\n while (change >= 100) {\n change -= 100;\n now++;\n }\n while (change >= 50) {\n change -= 50;\n now++;\n }\n now += change/10;\n now += a+b+c+d-aa-bb-cc-dd;\n if (ans > now) {\n ans = now;\n pay = aa+bb+cc+dd;\n aq=aa;bq=bb;cq=cc;dq=dd;\n }\n else if (ans == now) {\n if (pay > aa+bb+cc+dd) {\n pay = aa+bb+cc+dd;\n aq=aa;bq=bb;cq=cc;dq=dd;\n }\n }\n }\n }\n }\n }\n if (aq) {\n cout << 10 << ' ' << aq << endl;\n }\n if (bq) {\n cout << 50 << ' ' << bq << endl;\n }\n if (cq) {\n cout << 100 << ' ' << cq << endl;\n }\n if (dq) {\n cout << 500 << ' ' << dq << endl;\n }\n return;\n}\n\nint main() {\n bool q = false;\n while (true) {\n cin >> n;\n if (n == 0) {\n return 0;\n }\n if (q) {\n cout << endl;\n }\n solve();\n q = true;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3356, "score_of_the_acc": -0.6755, "final_rank": 12 }, { "submission_id": "aoj_2007_9307172", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<vector<pair<int,int>>>ans;\nbool solve()\n{\n auto calc=[](int x)->tuple<int,int,int,int>\n {\n int a=x/500;\n x%=500;\n int b=x/100;\n x%=100;\n int c=x/50;\n x%=50;\n int d=x/10;\n x%=10;\n assert(x==0);\n return make_tuple(d,c,b,a);\n };\n int X;\n cin>>X;\n if(X==0)return 0;\n int A,B,C,D;\n cin>>A>>B>>C>>D;\n int MIN=1<<30;\n for(int i=0;i<=A;i++)for(int j=0;j<=B;j++)for(int k=0;k<=C;k++)for(int l=0;l<=D;l++)\n {\n int pay=10*i+50*j+100*k+500*l;\n if(pay<X)continue;\n auto[a,b,c,d]=calc(pay-X);\n if(i&&a)continue;\n if(j&&b)continue;\n if(k&&c)continue;\n if(l&&d)continue;\n MIN=min(MIN,a+b+c+d-i-j-k-l);\n }\n for(int i=0;i<=A;i++)for(int j=0;j<=B;j++)for(int k=0;k<=C;k++)for(int l=0;l<=D;l++)\n {\n int pay=10*i+50*j+100*k+500*l;\n if(pay<X)continue;\n auto[a,b,c,d]=calc(pay-X);\n if(i&&a)continue;\n if(j&&b)continue;\n if(k&&c)continue;\n if(l&&d)continue;\n if(a+b+c+d-i-j-k-l==MIN)\n {\n vector<pair<int,int>>res;\n if(i)res.push_back(make_pair(10,i));\n if(j)res.push_back(make_pair(50,j));\n if(k)res.push_back(make_pair(100,k));\n if(l)res.push_back(make_pair(500,l));\n ans.push_back(res);\n return 1;\n }\n }\n return 1;\n}\nint main()\n{\n while(solve()){}\n for(int i=0;i<ans.size();i++)\n {\n if(i)cout<<'\\n';\n for(auto[v,c]:ans[i])cout<<v<<' '<<c<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3456, "score_of_the_acc": -0.7623, "final_rank": 13 }, { "submission_id": "aoj_2007_9305974", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,k,n) for (int i = k; i < (n); ++i)\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\nconst long long INF = 1e17;\n\nint main(){\n bool f = false;\n while(true){\n int sum,c[4],p[4],r[4];\n int d[4] = {10,50,100,500};\n cin >> sum;\n if(sum==0)break;\n if(f)cout << endl;\n f = true;\n for(int i=0;i<4;i++)cin >> c[i];\n\n int ans = 1e9;\n int ans_p[4];\n\n for(p[0]=0;p[0]<=c[0];p[0]++){\n for(p[1]=0;p[1]<=c[1];p[1]++){\n for(p[2]=0;p[2]<=c[2];p[2]++){\n for(p[3]=0;p[3]<=c[3];p[3]++){\n int total = 0;\n for(int i=0;i<4;i++){\n total += p[i]*d[i];\n }\n\n if(total<sum)continue;\n\n int res = total - sum;\n for(int i=3;i>=0;i--){\n r[i] = res/d[i];\n res %= d[i];\n }\n\n int have = 0;\n for(int i=3;i>=0;i--){\n have += c[i] - p[i] + r[i]; \n }\n\n if(ans>have){\n ans = have;\n for(int i=0;i<4;i++){\n ans_p[i] = p[i];\n }\n }\n\n\n }\n }\n }\n\n }\n\n for(int i=0;i<4;i++){\n if(ans_p[i]>0)cout << d[i] << ' ' << ans_p[i] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 3072, "score_of_the_acc": -0.5658, "final_rank": 7 }, { "submission_id": "aoj_2007_9275799", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n\ntemplate<class T> bool chmin(T &a, T b)\n{\n\tif(a>b)\n\t{\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nint N;\n\nbool solve()\n{\n\tif(N==0) return false;\n\tvector<int> A(4);\n\trep(i,4) cin >> A[i];\n\t\n\tint v = 10000;\n\tvector<int> ans(4);\n\trep(a,A[0]+1) rep(b,A[1]+1) rep(c,A[2]+1) rep(d,A[3]+1)\n\t{\n\t\tvector<int> C = {a,b,c,d};\n\t\tint r = a*10 + b*50 + c*100 + d*500 - N;\n\t\tif(r<0) continue;\n\t\tvector<int> B(4);\n\t\tB[3] = r/500;\n\t\tr %= 500;\n\t\tB[2] = r/100;\n\t\tr %= 100;\n\t\tB[1] = r/50;\n\t\tr %= 50;\n\t\tB[0] = r/10;\n\t\tassert(r%10==0);\n\t\trep(i,4) if(C[i]>0 && B[i]>0) continue;\n\t\tint cnt = 0;\n\t\trep(i,4) cnt += A[i]-C[i]+B[i];\n\t\tif(chmin(v, cnt)) ans = C;\n\t}\n\tvector<int> D = {10,50,100,500};\n\trep(i,4) if(ans[i]!=0) cout << D[i] << \" \" << ans[i] << '\\n';\n\tcin >> N;\n\tif(N==0) return false;\n\telse\n\t{\n\t\tcout << '\\n';\n\t\treturn true;\n\t}\n}\n\nint main()\n{\n\tcin >> N;\n\twhile(solve());\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3388, "score_of_the_acc": -0.9541, "final_rank": 16 }, { "submission_id": "aoj_2007_9262699", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vll = vector<ll>;\n#define all(v) (v).begin(),(v).end()\n#define rep(i,n) for(int i=0;i<n;i++)\n\nint main(){\n bool a=true;\n while(1){\n \n ll X;cin>>X;\n if(X==0) break;\n if(!a)cout<<endl;\n a=false;\n ll N=4;\n vll A(4);rep(i,N) cin>>A[i];\n \n vll ans(N);\n ll sum=-(1ll<<60);\n rep(i,A[0]+1) rep(j,A[1]+1) rep(k,A[2]+1) rep(l,A[3]+1){\n ll sum2=i*10+j*50+k*100+l*500;\n if(sum2<X) continue;\n ll rest=sum2-X;\n ll i2,j2,k2,l2;\n l2=rest/500;rest-=rest/500*500;\n k2=rest/100;rest-=rest/100*100;\n j2=rest/50;rest-=rest/50*50;\n i2=rest/10;rest-=rest/10*10;\n if((i2&&i)||(j2&&j)||(k2&&k)||(l2&&l)) continue;\n if(rest>0) continue;\n if(sum<(i-i2)+(j-j2)+(k-k2)+(l-l2)){\n ans={i,j,k,l};\n sum=(i-i2)+(j-j2)+(k-k2)+(l-l2);\n }\n\n }\n if(ans[0]>0) cout<<10<<\" \"<<ans[0]<<endl;\n if(ans[1]>0) cout<<50<<\" \"<<ans[1]<<endl;\n if(ans[2]>0) cout<<100<<\" \"<<ans[2]<<endl;\n if(ans[3]>0) cout<<500<<\" \"<<ans[3]<<endl;\n \n \n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3060, "score_of_the_acc": -0.4045, "final_rank": 3 }, { "submission_id": "aoj_2007_8976217", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nvc<pair<int, int>> solve(int N){\n vi coin(4); rep(i, 4) cin >> coin[i];\n vc<pair<int, int>> ans;\n int mini = inf, sm = 0; for (auto x : coin) sm += x;\n rep(a, coin[0] + 1) rep(b, coin[1] + 1) rep(c, coin[2] + 1) rep(d, coin[3] + 1){\n int tmp = sm - a - b - c - d;\n vc<pair<int, int>> cand;\n if (a > 0) cand.push_back({10, a});\n if (b > 0) cand.push_back({50, b});\n if (c > 0) cand.push_back({100, c});\n if (d > 0) cand.push_back({500, d});\n int val = a * 10 + b * 50 + c * 100 + d * 500;\n if (val < N) continue;\n while (val >= N + 500){\n val -= 500;\n tmp++;\n }\n while (val >= N + 100){\n val -= 100;\n tmp++;\n }\n while (val >= N + 50){\n val -= 50;\n tmp++;\n }\n while (val >= N + 10){\n val -= 10;\n tmp++;\n }\n if (tmp < mini){\n mini = tmp;\n ans = cand;\n }\n }\n return ans;\n}\n\nint main(){\n vc<vc<pair<int, int>>> ans;\n while (true){\n int N; cin >> N;\n if (N == 0) break;\n ans.push_back(solve(N));\n }\n rep(i, len(ans)){\n for (auto [a, b] : ans[i]) cout << a << \" \" << b << endl;\n if (i < len(ans) - 1) cout << endl;\n }\n // for (auto x : ans){\n // for (auto [a, b] : x) cout << a << \" \" << b << endl;\n \n // }\n}", "accuracy": 1, "time_ms": 2220, "memory_kb": 3348, "score_of_the_acc": -1.6184, "final_rank": 20 }, { "submission_id": "aoj_2007_8973059", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint count_change(int x, int &t10, int &t50, int &t100, int &t500) {\n t500 = x/500;\n x -= (x/500)*500;\n t100 = x/100;\n x -= (x/100)*100;\n t50 = x/50;\n x -= (x/50)*50;\n t10 = x/10;\n x -= (x/10)*10;\n\n return t10 + t50 + t100 + t500;\n}\n\nint main() {\n int cnt = 0;\n while (true) {\n int x;\n cin >> x;\n\n if (x == 0) {\n break;\n }\n\n int t10, t50, t100, t500;\n cin >> t10 >> t50 >> t100 >> t500;\n\n int y10, y50, y100, y500;\n y10 = y50 = y100 = y500 = 0;\n int memo = INT_MAX;\n for (int i = 0; i <= t10; i++) {\n for (int j = 0; j <= t50; j++) {\n for (int k = 0; k <= t100; k++) {\n for (int l = 0; l <= t500; l++) {\n int total = i*10+j*50+k*100+l*500;\n if (total < x) {\n continue;\n }\n int c10, c50, c100, c500;\n int cnt = count_change(total-x, c10, c50, c100, c500) + (t10-i) + (t50-j) + (t100-k) + (t500-l);\n if ((i && c10) || (j && c50) || (k && c100) || (l && c500)) {\n continue;\n }\n if (cnt < memo) {\n memo = cnt;\n y10 = i;\n y50 = j;\n y100 = k;\n y500 = l;\n }\n }\n }\n }\n }\n\n if (cnt) {\n cout << endl;\n }\n if (y10) {\n cout << 10 << \" \" << y10 << endl;\n }\n if (y50) {\n cout << 50 << \" \" << y50 << endl;\n }\n if (y100) {\n cout << 100 << \" \" << y100 << endl;\n }\n if (y500) {\n cout << 500 << \" \" << y500 << endl;\n }\n cnt++;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3052, "score_of_the_acc": -0.3933, "final_rank": 2 }, { "submission_id": "aoj_2007_8933788", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/home/github/algo/lib/template/template.hpp\"\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\ntemplate <class T, class U>\nconstexpr bool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nconstexpr bool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = M_PI;\n#line 3 \"/home/kuhaku/home/github/algo/lib/template/macro.hpp\"\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\n#line 3 \"/home/kuhaku/home/github/algo/lib/template/sonic.hpp\"\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(20);\n }\n\n constexpr void operator()() const {}\n} sonic;\n#line 5 \"/home/kuhaku/home/github/algo/lib/template/atcoder.hpp\"\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) {\n os << (it == v.begin() ? \"\" : \" \") << *it;\n }\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\ntemplate <typename T, typename... Args>\nauto make_vector(T x, int arg, Args... args) {\n if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);\n else return std::vector(arg, make_vector<T>(x, args...));\n}\nvoid Yes(bool is_correct = true) {\n std::cout << (is_correct ? \"Yes\" : \"No\") << '\\n';\n}\nvoid No(bool is_not_correct = true) {\n Yes(!is_not_correct);\n}\nvoid YES(bool is_correct = true) {\n std::cout << (is_correct ? \"YES\" : \"NO\") << '\\n';\n}\nvoid NO(bool is_not_correct = true) {\n YES(!is_not_correct);\n}\nvoid Takahashi(bool is_correct = true) {\n std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n';\n}\nvoid Aoki(bool is_not_correct = true) {\n Takahashi(!is_not_correct);\n}\n#line 3 \"a.cpp\"\n\nvector<int> charge(int x) {\n vector<int> res;\n vector<int> p = {500, 100, 50, 10};\n rep (i, 4) {\n res.emplace_back(x / p[i]);\n x -= res.back() * p[i];\n }\n reverse(all(res));\n return res;\n}\n\nint main(void) {\n int loop = 0;\n while (true) {\n int x;\n cin >> x;\n if (!x)\n break;\n if (loop++)\n cout << endl;\n vector<int> a(4);\n cin >> a;\n vector<int> ans(4);\n int eval = -Inf;\n vector<int> p = {10, 50, 100, 500};\n rep (i, a[0] + 1) {\n rep (j, a[1] + 1) {\n rep (k, a[2] + 1) {\n rep (l, a[3] + 1) {\n vector<int> b = {i, j, k, l};\n int sum = 0;\n rep (m, 4) sum += b[m] * p[m];\n if (x > sum)\n continue;\n auto c = charge(sum - x);\n bool f = true;\n rep (m, 4) f &= b[m] == 0 || c[m] == 0;\n int s = accumulate(all(b), 0), t = accumulate(all(c), 0);\n if (chmax(eval, s - t))\n ans = b;\n }\n }\n }\n }\n rep (i, 4) {\n if (ans[i])\n co(p[i], ans[i]);\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1980, "memory_kb": 3384, "score_of_the_acc": -1.5379, "final_rank": 19 }, { "submission_id": "aoj_2007_8861344", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n int N;\n cin >> N;\n while (N != 0) {\n int c[4], f[4] = {0, 0, 0, 0}, ans[4] = {0, 0, 0, 0};\n int sum = 0;\n for (int i = 0; i < 4; i++) {\n cin >> c[i];\n sum += c[i];\n }\n int mini = 1e+9;\n for (int pay0 = 0; pay0 <= c[0]; pay0++) {\n f[0] = pay0 > 0 ? 1 : 0;\n int m_pay0 = 10 * pay0;\n for (int pay1 = 0; pay1 <= c[1]; pay1++) {\n f[1] = pay1 > 0 ? 1 : 0;\n int m_pay1 = m_pay0 + 50 * pay1;\n for (int pay2 = 0; pay2 <= c[2]; pay2++) {\n f[2] = pay2 > 0 ? 1 : 0;\n int m_pay2 = m_pay1 + 100 * pay2;\n for (int pay3 = 0; pay3 <= c[3]; pay3++) {\n f[3] = pay3 > 0 ? 1 : 0;\n int m_pay3 = m_pay2 + 500 * pay3;\n int n_pay = pay0 + pay1 + pay2 + pay3;\n if (m_pay3 >= N) {\n int ch[4] = {0, 0, 0, 0};\n int rem = m_pay3 - N;\n for (int i = 3; i >= 0; i--) {\n int coin_val = i == 0 ? 10 : i == 1 ? 50 : i == 2 ? 100 : 500;\n ch[i] = rem / coin_val;\n rem = rem % coin_val;\n }\n int n_charge = ch[0] + ch[1] + ch[2] + ch[3];\n if (mini > sum - n_pay + n_charge) {\n if (!(f[0] == 1 && ch[0] > 0) && !(f[1] == 1 && ch[1] > 0) && !(f[2] == 1 && ch[2] > 0) && !(f[3] == 1 && ch[3] > 0)) {\n ans[0] = pay0; ans[1] = pay1; ans[2] = pay2; ans[3] = pay3;\n mini = sum - n_pay + n_charge;\n }\n }\n }\n }\n }\n }\n }\n int coin_vals[4] = {10, 50, 100, 500};\n for (int i = 0; i < 4; i++) {\n if (ans[i] > 0) {\n cout << coin_vals[i] << \" \" << ans[i] << endl;\n }\n }\n cin >> N;\n if (N != 0) {\n cout << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3084, "score_of_the_acc": -0.4426, "final_rank": 4 }, { "submission_id": "aoj_2007_8818928", "code_snippet": "#include <cstdio>\n#define MAX 4\n#define INF 1e+9\n#define pii pair<int,int>\n#define mp make_pair\n\nint payment(int *p) {\n return 10 * p[0] + 50 * p[1] + 100 * p[2] + 500 * p[3];\n}\n\nvoid charge(int m, int *ans) {\n ans[3] = m / 500;\n m -= ans[3] * 500;\n ans[2] = m / 100;\n m -= ans[2] * 100;\n ans[1] = m / 50;\n m -= ans[1] * 50;\n ans[0] = m / 10;\n}\n\nbool judge(int *f, int *ch) {\n for (int i = 0; i < MAX; ++i)\n if (f[i] == 1 && ch[i] > 0)\n return false;\n return true;\n}\n\nint main() {\n int N;\n scanf(\"%d\", &N);\n while (1) {\n int c[MAX], sum = 0, f[MAX] = {0, 0, 0, 0}, mini = INF, pay[MAX], ans[MAX];\n for (int i = 0; i < MAX; ++i) {\n scanf(\"%d\", &c[i]);\n sum += c[i];\n }\n for (pay[0] = 0; pay[0] <= c[0]; ++pay[0]) {\n if (pay[0] > 0)\n f[0] = 1;\n for (pay[1] = 0; pay[1] <= c[1]; ++pay[1]) {\n if (pay[1] > 0)\n f[1] = 1;\n for (pay[2] = 0; pay[2] <= c[2]; ++pay[2]) {\n if (pay[2] > 0)\n f[2] = 1;\n for (pay[3] = 0; pay[3] <= c[3]; ++pay[3]) {\n if (pay[3] > 0)\n f[3] = 1;\n int m_pay = payment(pay);\n if (m_pay >= N) {\n int ch[MAX];\n charge(m_pay - N, ch);\n if (mini > sum - pay[0] - pay[1] - pay[2] - pay[3] + ch[0] + ch[1] + ch[2] + ch[3]) {\n if (judge(f, ch)) {\n for (int i = 0; i < MAX; ++i)\n ans[i] = pay[i];\n mini = sum - pay[0] - pay[1] - pay[2] - pay[3] + ch[0] + ch[1] + ch[2] + ch[3];\n }\n }\n }\n f[3] = 0;\n }\n f[2] = 0;\n }\n f[1] = 0;\n }\n f[0] = 0;\n }\n int deno[] = {10, 50, 100, 500};\n for (int i = 0; i < MAX; ++i) {\n if (ans[i] > 0)\n printf(\"%d %d\\n\", deno[i], ans[i]);\n }\n scanf(\"%d\", &N);\n if (!N)\n break;\n printf(\"\\n\");\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 2596, "score_of_the_acc": -0.0459, "final_rank": 1 } ]
aoj_2005_cpp
Water Pipe Construction 21XX 年,ついに人類は火星への移住計画を開始させた.火星への移住第一陣に選抜されたあなたは,火星行政中央局(the Administrative Center of Mars)に配属され,火星で起こる様々な問題に対処している.行政中央局の目下の最大の問題は自立した需要供給サイクルの確保である.月からの援助物資が届くまでには数ヶ月単位の時間がかかるため,基本的には火星内の需要は火星内で解決する必要がある.それに加えて,循環システムを確立するまでは資源を極力節約することが求められる. 行政中央局は極地にある氷の採掘を開始した.太陽光でこれを溶かし,水として個々の基地に供給することが最終目標である.最初の段階として,水源のある基地から2つの主要基地までの導水管を敷設することにした.また,現時点ではいくつかの基地とそれらを結ぶ道路以外には開拓されておらず,未開拓のところに導水管を敷設するには多大なるコストと燃料を要するため,道路に沿って導水管を敷設することにした.さらに,技術上の制約のため,彼らの導水管は常に一方向だけに水が流れる. あなたの仕事は,これらの条件のもとで,導水管の敷設にかかるコストを最小にするプログラムを作成することである. Input 入力は複数のデータセットから構成される. データセットの最初の行は 5 つの整数からなる.これらは順に,火星に存在する基地の数 n (3 <= n <= 100),基地を結ぶ道路の数 m (2 <= m <= 1000),水源となる基地の番号 s , 導水管の目的地となる 2 つの主要基地の番号 g 1 , g 2 を表す.基地の番号は 1 から n までの整数で表される. s , g 1 , g 2 は互いに異なる. 続く m 行には,導水管を敷設可能な道路の情報が与えられる.各行はある 2 つの基地間における道路の情報を表しており,3 つの整数 b 1 , b 2 , c (1 <= c <= 1000) からなる.ここに b 1 , b 2 は道路の始端および終端の基地の番号を表し,これらは互いに異なる. c は基地 b 1 から基地 b 2 に向かう導水管を敷設するためにかかるコストである. 全てのデータセットについて,水源から目的地まで水を供給する経路は常に存在すると仮定してよい.また,ある 2 つの基地間には高々 1 つの道路しか存在しないことから,コストの情報はそれぞれの方向について高々 1 回しか与えられない. 入力の終了はスペース文字で区切られた 5 つのゼロが含まれる行で表される. Output それぞれのデータセットに対して,導水管を敷設するコストの最小値を 1 行に出力せよ. Sample Input 4 5 1 3 4 1 2 5 2 3 5 2 4 5 1 3 8 1 4 8 0 0 0 0 0 Output for the Sample Input 15
[ { "submission_id": "aoj_2005_10852719", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VS = vector<string>; using LL = long long;\nusing VI = vector<int>; using VVI = vector<VI>;\nusing PII = pair<int, int>; using PLL = pair<LL, LL>;\nusing VL = vector<LL>; using VVL = vector<VL>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)\n#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)\n#define debug(x) cerr << #x << \": \" << x << endl\nconst int INF = 1e9; const LL LINF = 1e16;\nconst LL MOD = 1000000007; const double PI = acos(-1.0);\nint DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };\n\n/* ----- 2018/07/23 Problem: AOJ 2005 / Link: https://onlinejudge.u-aizu.ac.jp/challenges/search/volumes/2005 ----- */\n/* ------問題------\n\n21XX 年,ついに人類は火星への移住計画を開始させた.火星への移住第一陣に選抜されたあなたは,火星行政中央局(the Administrative Center of Mars)に配属され,火星で起こる様々な問題に対処している.行政中央局の目下の最大の問題は自立した需要供給サイクルの確保である.月からの援助物資が届くまでには数ヶ月単位の時間がかかるため,基本的には火星内の需要は火星内で解決する必要がある.それに加えて,循環システムを確立するまでは資源を極力節約することが求められる.\n行政中央局は極地にある氷の採掘を開始した.太陽光でこれを溶かし,水として個々の基地に供給することが最終目標である.最初の段階として,水源のある基地から2つの主要基地までの導水管を敷設することにした.また,現時点ではいくつかの基地とそれらを結ぶ道路以外には開拓されておらず,未開拓のところに導水管を敷設するには多大なるコストと燃料を要するため,道路に沿って導水管を敷設することにした.さらに,技術上の制約のため,彼らの導水管は常に一方向だけに水が流れる.\nあなたの仕事は,これらの条件のもとで,導水管の敷設にかかるコストを最小にするプログラムを作成することである.\n\n-----問題ここまで----- */\n/* -----解説等-----\n\n日本語が悪いのでSやGiから水が分かれるとも取れる。\n×彼らの導水管は常に一方向だけに水が流れる\n○頂点を流れる水は常に一方向である\n頂点について一方向だと、分岐点を全探索するのみ。\n\n----解説ここまで---- */\n\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(false);\n\n\n\tint N, M, S, G1, G2;\n\twhile (cin >> N >> M >> S >> G1 >> G2,N) {\n\t\tconst int intINF = 2e9 / 3;\n\t\tVVI dist(N, VI(N,intINF ));\n\t\tS--, G1--, G2--;\n\t\tFOR(i, 0, N)dist[i][i] = 0;\n\t\tFOR(i, 0, M) {\n\t\t\tint a, b, c; cin >> a >> b >> c; a--; b--;\n\t\t\tdist[a][b] = c;\n\t\t}\n\t\tFOR(k, 0, N)FOR(i, 0, N)FOR(j, 0, N)dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n\t\tint ans = INF;\n\t\tFOR(i, 0, N) {\n\t\t\tans = min(ans, dist[S][i] + dist[i][G1] + dist[i][G2]);\n\t\t}\n\t\tcout << ans << \"\\n\";\n\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3436, "score_of_the_acc": -0.4426, "final_rank": 5 }, { "submission_id": "aoj_2005_10668080", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M, S, T1, T2;\n cin >> N >> M >> S >> T1 >> T2;\n if (N == 0) return false;\n S--, T1--, T2--;\n const long long inf = 1e18;\n vector<vector<long long>> D(N, vector<long long>(N, inf));\n vector<vector<long long>> E(N, vector<long long>(N, inf));\n for (int u, v, w; M--; ) {\n cin >> u >> v >> w;\n u--, v--;\n D[u][v] = w;\n E[v][u] = w;\n }\n for (int i = 0; i < N; i++) {\n D[i][i] = 0;\n E[i][i] = 0;\n }\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n D[i][j] = min(D[i][j], D[i][k] + D[k][j]);\n E[i][j] = min(E[i][j], E[i][k] + E[k][j]);\n }\n }\n }\n long long ans = inf;\n for (int i = 0; i < N; i++) {\n ans = min(ans, E[i][S] + D[i][T1] + D[i][T2]);\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3712, "score_of_the_acc": -1.096, "final_rank": 19 }, { "submission_id": "aoj_2005_10610437", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\ntuple<vl, vi> dijkstra(int s, int N, vv<pair<int, ll>> &g){\n using P = pair<ll, int>;\n vl dist(N, INF);\n vi bef(N);\n priority_queue<P, vc<P>, greater<P>> q;\n dist[s] = 0;\n q.push({0, s});\n while (!q.empty()){\n auto [c, now] = q.top();q.pop();\n if (dist[now] < c) continue;\n for (auto&& [nxt, nc]: g[now]) if (dist[nxt] > c + nc){\n dist[nxt] = c + nc;\n bef[nxt] = now;\n q.push({dist[nxt], nxt}); \n }\n }\n return {dist, bef};\n}\n\nint N, M, s, g1, g2;\n\nvoid solve(){\n vv<pair<int, ll>> g(N), rg(N);\n rep(i, M){\n int a, b, w; cin >> a >> b >> w; a--; b--;\n g[a].push_back({b, w});\n rg[b].push_back({a, w});\n }\n auto [dist_s, _s] = dijkstra(s, N, g);\n auto [dist_g1, _g1] = dijkstra(g1, N, rg);\n auto [dist_g2, _g2] = dijkstra(g2, N, rg);\n ll ans = INF;\n rep(p, N) if (dist_s[p] < INF && dist_g1[p] < INF && dist_g2[p] < INF){\n ans = min(ans, dist_s[p] + dist_g1[p] + dist_g2[p]);\n }\n cout << ans << endl;\n}\nint main(){\n while (true){\n cin >> N >> M >> s >> g1 >> g2;\n s--;\n g1--;\n g2--;\n if (N == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.0712, "final_rank": 3 }, { "submission_id": "aoj_2005_10579430", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n// for AOJ or ICPC or etc..\ntemplate<class Tp>\nbool zero (const Tp &x) {\n return x == 0;\n}\n\ntemplate<class Tp, class... Args>\nbool zero (const Tp &x, const Args& ...args) {\n return zero(x) and zero(args...);\n}\n\n\n/*隣接行列を更新してくDPのイメージ\nつながってない辺は重みINF、繋がってたら辺の重みで初期化\nO(V^3)\n*/\nint warshall_floyd(vector<vector<ll>> &dist){\n int size = (int)dist.size();\n rep(k,size){\n rep(i,size){\n rep(j,size){\n if(i == j){\n chmin(dist[i][j],0LL);\n }\n if(dist[i][k] != INF && dist[k][j] != INF){\n chmin(dist[i][j],dist[i][k] + dist[k][j]);\n }\n }\n }\n }\n //負の閉路判定あれば−1大丈夫なら1\n rep(i,size){\n if(dist[i][i]<0){\n return -1;\n }\n }\n return 1;\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n,m,s,g1,g2);\n if(zero(n,m,s,g1,g2))break;\n s--;g1--;g2--;\n vvll g(n,vll(n,INF));\n rep(i,m){\n LL(a,b,c);\n a--;b--;\n chmin(g[a][b],c);\n }\n warshall_floyd(g);\n // iまで一緒\n ll ans = INF;\n\n rep(i,n){\n chmin(ans,g[s][i] + g[i][g1] + g[i][g2]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3840, "score_of_the_acc": -0.7207, "final_rank": 15 }, { "submission_id": "aoj_2005_10506814", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 40;\n\nbool solve(){\n int n, m, s, g1, g2; cin >> n >> m >> s >> g1 >> g2, --s, --g1, --g2;\n if(n == 0) return false;\n vector G(n, vector<ll>(n, INF));\n for(int i = 0; i < m; ++i){\n int b1, b2, c; cin >> b1 >> b2 >> c, --b1, --b2;\n G[b1][b2] = c;\n }\n\n for(int i = 0; i < n; ++i) G[i][i] = 0;\n for(int k = 0; k < n; ++k){\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < n; ++j){\n if(G[i][j] > G[i][k] + G[k][j]){\n G[i][j] = G[i][k] + G[k][j];\n }\n }\n }\n }\n ll ans = INF;\n for(int i = 0; i < n; ++i){\n ll tmp = G[s][i] + G[i][g1] + G[i][g2];\n if(ans > tmp) ans = tmp;\n }\n cout << ans << endl;\n return true;\n}\n\nint main(){\n while(1){\n if(!solve()) break;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3320, "score_of_the_acc": -0.8201, "final_rank": 17 }, { "submission_id": "aoj_2005_10160829", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst ll INF = 1e18;\nint n, m, s, g1, g2;\nvector<vector<pair<ll, ll>>> E;\n\nvector<ll> dijk(int s) {\n vector<ll> dist(n, INF);\n priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n pq.emplace(0, s);\n dist[s] = 0;\n while (!pq.empty()) {\n auto [cost, u] = pq.top();\n pq.pop();\n if (dist[u] != cost)\n continue;\n for (auto [v, w] : E[u]) {\n if (dist[v] > cost + w) {\n dist[v] = cost + w;\n pq.emplace(dist[v], v);\n }\n }\n }\n return dist;\n}\n\nint main() {\n while (true) {\n n = m = s = g1 = g2 = 0;\n cin >> n >> m >> s >> g1 >> g2, s--, g1--, g2--;\n if (n == 0) {\n break;\n }\n\n E.clear();\n E.resize(n);\n for (int i = 0; i < m; i++) {\n int a, b, c;\n cin >> a >> b >> c, a--, b--;\n E[a].emplace_back(b, c);\n }\n\n auto d1 = dijk(s);\n\n ll ans = INF;\n for (int i = 0; i < n; i++) {\n auto d2 = dijk(i);\n ans = min(ans, d1[i] + d2[g1] + d2[g2]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3516, "score_of_the_acc": -0.458, "final_rank": 7 }, { "submission_id": "aoj_2005_10160806", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst ll INF = 1e18;\n\nvector<ll> dijk(int s, int n, int m, int g1, int g2,\n vector<vector<pair<ll, ll>>> &E) {\n vector<ll> dist(n, INF);\n priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;\n pq.emplace(0, s);\n dist[s] = 0;\n while (!pq.empty()) {\n auto [cost, u] = pq.top();\n pq.pop();\n if (dist[u] != cost)\n continue;\n for (auto [v, w] : E[u]) {\n if (dist[v] > cost + w) {\n dist[v] = cost + w;\n pq.emplace(dist[v], v);\n }\n }\n }\n return dist;\n}\n\nint main() {\n while (true) {\n int n, m, s, g1, g2;\n cin >> n >> m >> s >> g1 >> g2, s--, g1--, g2--;\n if (n == 0) {\n break;\n }\n vector<vector<pair<ll, ll>>> E(n);\n for (int i = 0; i < m; i++) {\n int a, b, c;\n cin >> a >> b >> c, a--, b--;\n E[a].emplace_back(b, c);\n }\n\n auto d1 = dijk(s, n, m, g1, g2, E);\n\n ll ans = INF;\n for (int i = 0; i < n; i++) {\n auto d2 = dijk(i, n, m, g1, g2, E);\n ans = min(ans, d1[i] + d2[g1] + d2[g2]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3220, "score_of_the_acc": -0.4008, "final_rank": 4 }, { "submission_id": "aoj_2005_10027255", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\nusing Mat = vector<vector<int>>;\n\nvoid wf(Mat &a, int n) {\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n a[i][j] = min(a[i][j], a[i][k] + a[k][j]);\n }\n }\n }\n}\n\nconstexpr int inf = 1 << 29;\nvoid solve(int n, int m, int s, int g1, int g2) {\n s--,g1--,g2--;\n Mat dist(n, vector<int>(n, inf));\n for (int i = 0; i < n; i++) dist[i][i] = 0;\n while(m--) {\n int u, v, c;\n cin >> u >> v >> c;\n u--,v--;\n dist[u][v] = c;\n }\n wf(dist, n);\n int ans = inf;\n for (int i = 0; i < n; i++) {\n ans = min(ans, dist[s][i] + dist[i][g1] + dist[i][g2]);\n }\n cout << ans << endl;\n}\n\nint main() {\n int n, m, s, g1, g2;\n cin >> n >> m >> s >> g1 >> g2;\n do {\n solve(n, m, s, g1, g2);\n cin >> n >> m >> s >> g1 >> g2;\n }while (n != 0);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3300, "score_of_the_acc": -0.6163, "final_rank": 10 }, { "submission_id": "aoj_2005_9484712", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct MaxFlow {\n struct Edge {\n int to, rev;\n ll cap;\n };\n int V;\n vector<vector<Edge>> G;\n vector<int> itr, level;\n using P = pair<int, int>;\n vector<P> es;\n\n public:\n MaxFlow() {}\n MaxFlow(int V) : V(V) {\n G.assign(V, vector<Edge>());\n }\n int add_vertex() {\n G.push_back(vector<Edge>());\n return V++;\n }\n void add_edge(int from, int to, ll cap) {\n int fid = SZ(G[from]), tid = SZ(G[to]);\n if (from == to)\n tid++;\n es.push_back({from, fid});\n G[from].push_back({to, tid, cap});\n G[to].push_back({from, fid, 0});\n }\n struct Type {\n int from, to;\n ll cap, recap;\n };\n Type get_edge(int i) {\n auto [from, pos] = es[i];\n auto e = G[from][pos];\n auto re = G[e.to][e.rev];\n return Type{from, e.to, e.cap, re.cap};\n }\n void bfs(int s) {\n level.assign(V, -1);\n queue<int> q;\n level[s] = 0;\n q.push(s);\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto &e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n q.push(e.to);\n }\n }\n }\n }\n ll dfs(int v, int t, ll f) {\n if (v == t)\n return f;\n for (int &i = itr[v]; i < (int)G[v].size(); i++) {\n Edge &e = G[v][i];\n if (e.cap > 0 && level[v] < level[e.to]) {\n ll d = dfs(e.to, t, min(f, e.cap));\n if (d > 0) {\n e.cap -= d, G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n ll run(int s, int t) {\n ll ret = 0, f;\n while (bfs(s), level[t] >= 0) {\n itr.assign(V, 0);\n while ((f = dfs(s, t, INF)) > 0)\n ret += f;\n }\n return ret;\n }\n vector<int> cut() {\n vector<int> ret(V);\n rep(v, 0, V) if (level[v] < 0) ret[v] = 1;\n return ret;\n }\n};\n\n/**\n * @brief Maximum Flow\n */\n\nvector<int> Dijkstra(int N, vector<vector<pair<int,int>>>& G, int S) {\n vector<int> D(N, inf);\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> PQ;\n D[S] = 0;\n PQ.push({0,S});\n while(!PQ.empty()) {\n pair<int,int> P = PQ.top();\n PQ.pop();\n if (D[P.second] < P.first) continue;\n for (pair<int,int> E : G[P.second]) {\n int ND = P.first + E.second;\n if (ND < D[E.first]) {\n D[E.first] = ND;\n PQ.push({ND,E.first});\n }\n }\n }\n return D;\n}\n\nint main() {\nwhile(1) {\n int N, M, S, G1, G2;\n cin >> N >> M >> S >> G1 >> G2;\n if (N == 0) return 0;\n S--, G1--, G2--;\n vector<vector<pair<int,int>>> A(N), B(N);\n rep(i,0,M) {\n int X, Y, Z;\n cin >> X >> Y >> Z;\n X--, Y--;\n A[X].push_back({Y,Z});\n B[Y].push_back({X,Z});\n }\n vector<int> D1 = Dijkstra(N,A,S), D2 = Dijkstra(N,B,G1), D3 = Dijkstra(N,B,G2);\n ll ANS = INF;\n rep(i,0,N) {\n chmin(ANS,(ll)D1[i] + (ll)D2[i] + (ll)D3[i]);\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0464, "final_rank": 2 }, { "submission_id": "aoj_2005_9395368", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#include <bits/stdc++.h>\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint solve(int N, int M, int S, int G1, int G2){\n vvi dist(N, vi(N, inf));\n rep(i, N) dist[i][i] = 0;\n rep(i, M){\n int u, v, c; cin >> u >> v >> c; u--; v--;\n dist[u][v] = c;\n }\n for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++){\n if (dist[i][k] != inf && dist[k][j] != inf){\n if (dist[i][j] > dist[i][k] + dist[k][j]){\n dist[i][j] = dist[i][k] + dist[k][j];\n }\n }\n }\n int ans = inf;\n rep(i, N) if (dist[S][i] != inf && dist[i][G1] != inf && dist[i][G2] != inf){\n ans = min(ans, dist[S][i] + dist[i][G1] + dist[i][G2]);\n }\n return ans;\n}\nint main(){\n vi ans;\n while (true){\n int N, M, S, G1, G2; cin >> N >> M >> S >> G1 >> G2;\n S--; G1--; G2--;\n if (N == 0) break;\n ans.push_back(solve(N, M, S, G1, G2));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3296, "score_of_the_acc": -0.8155, "final_rank": 16 }, { "submission_id": "aoj_2005_9366067", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\nusing pii =pair<int,int>;\n\nconstexpr ll mod = 1000000007;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint dX[4]={1,0,-1,0};\nint dY[4]={0,-1,0,1};\n\n\n\n//library\n\n//end\n\nusing Graph =vector<vector<int>>;\nint big=500000000;\n\nint main(){\n\n while (true){\n int n,m,s,g1,g2;\n cin>>n>>m>>s>>g1>>g2;\n if (n==0)break;\n vector<vector<int>> E(n+1,vector<int>(n+1,big));\n for (int i=0; i<n+1; i++){\n E[i][i]=0;\n }\n for (int i=0; i<m; i++){\n int b1,b2,c;\n cin>>b1>>b2>>c;\n E[b1][b2]=c;\n }\n for (int k=0; k<n+1; k++){\n for (int i=0; i<n+1; i++){\n for (int j=0; j<n+1; j++){\n E[i][j]=min(E[i][j],E[i][k]+E[k][j]);\n }\n }\n }\n\n int ans=big;\n for (int v=1; v<n+1; v++){\n ans=min(ans,E[s][v]+E[v][g1]+E[v][g2]);\n }\n\n cout<<ans<<endl;\n\n }\n\n return 0;\n\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3532, "score_of_the_acc": -0.6611, "final_rank": 12 }, { "submission_id": "aoj_2005_9342712", "code_snippet": "#include <bits/stdc++.h>\n//#include <ranges>\n#define _USE_MATH_DEFINES\n//#include <atcoder/all>\n//#include <atcoder/modint>\n//using namespace atcoder;\n//using mint = modint998244353;\n//using mint = modint1000000007;\nusing namespace std;\n//using mint = modint;\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)\n#define rrep(i,a,b) for(int i=a;i>=b;i--)\n#define fore(i,a) for(auto &i:a)\n#define V vector<ll>\n#define Vi vector<int>\n#define Vd vector<double>\n#define Vb vector<bool>\n#define Vs vector<string>\n#define Vc vector<char>\n#define VV vector<V>\nusing P = pair<ll,ll>;\nusing G = vector<vector<int>>;\n#define VP vector<P>\ntemplate<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define INF 1LL << 60\n#define inf 1e9\ntemplate <typename T>\nbool chmax(T &a, const T& b) {\n if (a < b) {\n a = b; // aをbで更新\n return true;\n }\nreturn false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T& b) {\n if (a > b) {\n a = b; // aをbで更新\n return true;\n }\n return false;\n}\n\nlong long combi(long long n, long long k) {\n if (n == k || k == 0)\n return 1;\n else {\n return combi(n - 1, k - 1) + combi(n - 1, k);\n }\n}\n//整数かどうか\nbool isNumber(const string& str)\n{\n for (const char &c : str) {\n if (std::isdigit(c) == 0) return false;\n }\n return true;\n}\n///*\n//最大公約数\nll gcd(ll a, ll b){\n if(b==0){\n return a;\n }else{\n return gcd(b, a%b);\n }\n}\n//最小公倍数\nll lcm(ll a, ll b){\n ll g=gcd(a,b);\n return a/g*b;\n}\n//*/\n//int di[] = {-1,0,1,0};\n//int dj[] = {0,-1,0,1};\n\n//s = regex_replace(s, regex(\"あ\"), \"う\");\n/*stiring で char を検索するときは\n s.find(c)!=string::npos\n */\n\n\n/*//各桁の和\n int wa(int n){\n int sum =0;\n while(n>0){\n sum += n%10;\n n/=10;\n }\n return sum;\n}\n*/\n/*\n//階乗\n int ki(int i){\n int k = 1;\n for(int j = 1; j<=i; j++){\n k *= j;\n }\n return k;\n }\n*/\n/*log_x(b)\ndouble logN(double x, double b) {\n return log(x) / log(b);\n}\n*/\n//\n/*//エラトステネスの篩 main関数内にsolve();を書き込む!!\nconst ll N = 101010;//求める範囲\nVb isp(N+1,true);\nvoid solve(){\n isp[0] = false;\n isp[1] = false;\n for(ll i = 2; i+i<=N;i++){\n if(isp[i])for(ll j = 2; i*j<=N;j++)isp[i*j] = false;\n }\n}\n//\n*/\n//\n/*\n//約数列挙 O(√n)\nvector<long long> divisor(long long n) {\n vector<long long> ret;\n for (long long i = 1; i * i <= n; i++) {\n if (n % i == 0) {\n ret.push_back(i);\n if (i * i != n) ret.push_back(n / i);\n }\n }\n sort(ret.begin(), ret.end()); // 昇順に並べる\n return ret;\n}\n//\n*/\n//\n/*\n//素因数分解O(√n)\nmap< ll, ll > prime_factor(ll n) {\n map< ll, ll > ret;\n for(ll i = 2; i * i <= n; i++) {\n while(n % i == 0) {\n ret[i]++;\n n /= i;\n }\n }\n if(n != 1) ret[n] = 1;\n return ret;\n}\n//\n*/\n\n/*\nll modpow(ll x, ll n, ll mod){\n while(n){\n ll resu = 1;\n if(n&1)res = (res * x) %mod;\n x = (x*x)%mod;\n n>>=1;\n }\n return res;\n}\n*/\n/*\n//最小二乗法\n//aのb乗をmで割ったあまりを返す関数\n//変数aはa^1→a^2→a^4→a^8→…と変化\nll power(ll a,ll b, ll m){\n ll p = a,ans = 1;\n rep(i,60){\n ll wari = (1LL<<i);\n if((b/wari)%2==1){\n ans=(ans*p)%m;\n }\n p=(p*p)%m;\n }\n return ans;\n}\n*/\n//\n/*\ntemplate <typename T> bool next_combination(const T first, const T last, int k) {\n const T subset = first + k;\n // empty container | k = 0 | k == n \n if (first == last || first == subset || last == subset) {\n return false;\n }\n T src = subset;\n while (first != src) {\n src--;\n if (*src < *(last - 1)) {\n T dest = subset;\n while (*src >= *dest) {\n dest++;\n }\n iter_swap(src, dest);\n rotate(src + 1, dest + 1, last);\n rotate(subset, subset + (last - dest) - 1, last);\n return true;\n }\n }\n // restore\n rotate(first, subset, last);\n return false;\n}\n//\n*/\nint ctoi(char c){\n if(c=='1')return 1;\n else if(c=='2')return 2;\n else if(c=='3')return 3;\n else if(c=='4')return 4;\n else if(c=='5')return 5;\n else if(c=='6')return 6;\n else if(c=='7')return 7;\n else if(c=='8')return 8;\n else if(c=='9')return 9;\n else if(c=='0')return 0;\n else return -inf;\n}\n//vector<int> dx = {1,0,-1,0,1,-1,-1,1};\n//vector<int> dy = {0,1,0,-1,1,1,-1,-1};\nint dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };\n//int dx[8]={0,-1,-1,-1,0,1,1,1},dy[8]={1,1,0,-1,-1,-1,0,1};\n//#define mod 998244353\n//cout << mint.val() << endl;\n//cout << fixed << setprecision(15) << y << endl;\n\n//bit s に i番目のビットを立てる\n#define bittate(s,i) s | (1LL<<i)\n//bit sから i番目のビットを消す\n#define bitkeshi(s,i) s^(1LL<<i)\n//bit s が i番目のビットを含んでいるか\n#define bitcheck(s,i) (s>>i)&1LL\n//string str(bitset<32>(value).to_string<char, char_traits<char>, allocator<char> >());\n#define ppc(n) __popcount(n)\nstring tobin(ll n){\n string re((bitset<64>(n).to_string<char, char_traits<char>, allocator<char> >()));\n return re;\n}\n#define yes \"Yes\"\n#define no \"No\"\n#define Yes \"YES\"\n#define No \"NO\"\n\n\n\n\nint main(){\n int n,m,s,g1,g2;\n while(cin >> n >> m >> s >> g1 >> g2){\n if(n==0)return 0;\n s--;g1--;g2--;\n VV dis(n,V(n,INF));\n rep(i,m){\n int b1,b2,c;\n cin >> b1 >> b2 >> c;\n b1--;b2--;\n dis[b1][b2]=c;\n }\n rep(i,n)dis[i][i]=0;\n rep(i,n)rep(j,n)rep(k,n){\n dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);\n }\n ll ans=INF;\n rep(i,n){\n chmin(ans,dis[s][i]+dis[i][g1]+dis[i][g2]);\n }\n cout << ans << endl;\n\n }\n\n\n\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3532, "score_of_the_acc": -0.6611, "final_rank": 12 }, { "submission_id": "aoj_2005_9334789", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing ll = long long;\n#define MOD 1000000007\n#define Mod 998244353\nconst int MAX = 1000000005;\nconst long long INF = 1000000000000000005LL;\nusing namespace std;\n//using namespace atcoder;\n\nvector<ll> dijkstra (int st, int N, vector<vector<pair<int, ll>>> &G) {\n\tvector<ll> dist(N, MAX);\n\tpriority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;\n\tdist[st] = 0;\n\tpq.push(make_pair(0, st));\n\twhile (!pq.empty()) {\n\t\tll d;\n\t\tint v;\n\t\ttie(d, v) = pq.top();\n\t\tpq.pop();\n\t\tif (d != dist[v]) continue;\n\t\tfor (auto nv : G[v]) {\n\t\t\tif (dist[nv.first] <= dist[v]+nv.second) continue;\n\t\t\tdist[nv.first] = dist[v]+nv.second;\n\t\t\tpq.push(make_pair(dist[nv.first], nv.first));\n\t\t}\n\t}\n\treturn dist;\n}\n\nbool solve() {\n int n, m, s, g1, g2;\n cin >> n >> m >> s >> g1 >> g2;\n if (n == 0) return false;\n s--; g1--; g2--;\n vector<vector<pair<int, ll>>> G(n);\n for (int i = 0; i < m; i++) {\n int u, v;\n ll c;\n cin >> u >> v >> c;\n u--; v--;\n G[u].push_back({v, c});\n }\n vector<vector<ll>> dist(n);\n for (int i = 0; i < n; i++) {\n dist[i] = dijkstra(i, n, G);\n }\n ll ans = INF;\n for (int i = 0; i < n; i++) {\n ans = min(ans, dist[s][i] + dist[i][g1] + dist[i][g2]);\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(0);cin.tie();\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3592, "score_of_the_acc": -0.4728, "final_rank": 8 }, { "submission_id": "aoj_2005_9234939", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int n, m, s, g1, g2;\n std::cin >> n >> m >> s >> g1 >> g2;\n if (n == 0) return 1;\n s--; g1--; g2--;\n const int INF = 1e8;\n std::vector dist(n, std::vector<int>(n, INF));\n for (int i = 0; i < m; i++) {\n int u, v, c;\n std::cin >> u >> v >> c;\n u--; v--;\n dist[u][v] = c;\n }\n for (int i = 0; i < n; i++) dist[i][i] = 0;\n\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n\n int ans = INF;\n for (int i = 0; i < n; i++) {\n ans = std::min(ans, dist[s][i] + dist[i][g1] + dist[i][g2]);\n }\n std::cout << ans << '\\n';\n\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3216, "score_of_the_acc": -0.6, "final_rank": 9 }, { "submission_id": "aoj_2005_9036359", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nint main(void) {\n while(true) {\n int n, m, s, g1, g2;\n cin >> n >> m >> s >> g1 >> g2;\n if(n == 0) break;\n s--;\n g1--;\n g2--;\n vector<vector<int>> d(n, vector<int>(n, 1e8));\n rep(i, 0, n) d[i][i] = 0;\n rep(i, 0, m) {\n int a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n d[a][b] = c;\n }\n rep(k, 0, n) {\n rep(i, 0, n) {\n rep(j, 0, n) {\n d[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n }\n }\n }\n int ans = 1e8;\n rep(i, 0, n) {\n ans = min(ans, d[s][i] + d[i][g1] + d[i][g2]);\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3544, "score_of_the_acc": -0.6635, "final_rank": 14 }, { "submission_id": "aoj_2005_7959062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M, s, g1, g2;\n cin >> N >> M >> s >> g1 >> g2;\n if (!N) return false;\n\n vector<vector<pair<int, int>>> G1(N), G2(N);\n for (int _ = 0 ; _ < M ; _++) {\n int a, b, c; cin >> a >> b >> c;\n a--; b--;\n G1[a].emplace_back(b, c);\n G2[b].emplace_back(a, c);\n }\n \n auto dij = [&](vector<vector<pair<int, int>>> G, int st) -> vector<int> {\n const int inf = (int)1e9;\n vector<int> res(N, inf);\n res[st] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;\n que.push({ 0, st });\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (res[v] < d) continue;\n for (auto [x, w] : G[v]) {\n if (d + w < res[x]) {\n res[x] = d + w;\n que.push({ res[x], x });\n }\n }\n }\n return res;\n };\n\n vector<vector<int>> A(3, vector<int>{});\n A[0] = dij(G1, s - 1);\n A[1] = dij(G2, g1 - 1);\n A[2] = dij(G2, g2 - 1);\n assert(A[0].size() == (size_t)N);\n assert(A[1].size() == (size_t)N);\n assert(A[1].size() == (size_t)N);\n\n long long ans = (long long)1e18;\n for (int i = 0 ; i < N ; i++) {\n long long v = 0;\n for (int j = 0 ; j < 3 ; j++) v += A[j][i];\n ans = min(ans, v);\n }\n\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3416, "score_of_the_acc": -0.0387, "final_rank": 1 }, { "submission_id": "aoj_2005_7147137", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, m, s, g1, g2;\n while(cin >> n >> m >> s >> g1 >> g2, n){\n vector<vector<int>> A(n, vector<int>(n, 1 << 29));\n s--, g1--, g2--;\n int u, v, c;\n for(int i = 0; i < m; i++){\n cin >> u >> v >> c;\n u--, v--;\n A[u][v] = c;\n }\n for(int i = 0; i < n; i++)A[i][i] = 0;\n for(int k = 0; k < n; k++){\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n A[i][j] = min(A[i][j], A[i][k] + A[k][j]);\n }\n }\n }\n int ans = 1 << 30;\n for(int i = 0; i < n; i++){\n ans = min(ans, A[s][i] + A[i][g1] + A[i][g2]);\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3448, "score_of_the_acc": -0.4449, "final_rank": 6 }, { "submission_id": "aoj_2005_7140080", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)(N); ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)\n#define per(i, N) for(int i = (N)-1; i >= 0; --i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n, k) ((n) >> (k))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\nusing pointld = struct{ ld x, y; };\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vvec<T>>;\ntemplate<class T> using vvvvec = vvec<vvec<T>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr ll MOD = (1) ? 998244353 : 1e9+7;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};\nconstexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing Pair = pair<ll, ll>;\nusing Tpl = tuple<char, int, int>;\n\nclass Warshall_Floyd{\n public:\n vector<vector<ll>> dist;\n int size;\n\n public:\n Warshall_Floyd(int N) : size(N), dist(N, vector<ll>(N, LINF)){\n for(int i = 0; i < N; i++) dist[i][i] = 0;\n }\n void add_edge(int u, int v, int w){\n dist[u][v] = w;\n }\n bool solve(){\n for(int k = 0; k < size; k++){\n for(int i = 0; i < size; i++){\n for(int j = 0; j < size; j++){\n if(dist[i][k] == LINF || dist[k][j] == LINF) continue;\n chmin(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n for(int i = 0; i < size; i++)\n if(dist[i][i] < 0) return false;\n return true;\n }\n int find_dist(int u, int v){\n return dist[u][v];\n }\n bool need_edge(int u, int v, int w){\n for(int i = 0; i < size; i++){\n if(i == u || i == v) continue;\n if(w >= dist[u][i] + dist[i][v]) return false;\n }\n return true;\n }\n};\n\nvoid Main(){\n while(1){\n int N, M, S, G1, G2; cin >> N >> M >> S >> G1 >> G2;\n if(N == 0) break;\n S--, G1--, G2--;\n\n Warshall_Floyd wf(N);\n vvec<int> G(N);\n rep(i, M){\n int a, b, c; cin >> a >> b >> c; a--, b--;\n G[a].eb(b);\n wf.add_edge(a, b, c);\n }\n\n wf.solve();\n \n ll ans = LINF;\n rep(i, N){\n ll val = wf.dist[S][i] + wf.dist[i][G1] + wf.dist[i][G2];\n chmin(ans, val);\n }\n\n cout << ans << endl;\n }\n}\n \nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3356, "score_of_the_acc": -0.6271, "final_rank": 11 }, { "submission_id": "aoj_2005_6960513", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nconst ll INF=1e8;\nint main(void){\n ll n,m,x,y,z,q,w,e;\n while(cin>>n>>m>>x>>y>>z,n++){\n ll dp[n][n]={};\n for(ll i=0;i<n;i++){\n for(ll j=0;j<n;j++){\n if(i!=j){\n dp[i][j]=INF;\n }\n }\n }\n for(ll i=0;i<m;i++){\n cin>>q>>w>>e;\n dp[q][w]=e;\n }\n for(ll k=0;k<n;k++){\n for(ll i=0;i<n;i++){\n for(ll j=0;j<n;j++){\n dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);\n }\n }\n }\n ll ans=INF;\n for(ll i=0;i<n;i++){\n ans=min(ans,dp[x][i]+dp[i][y]+dp[i][z]);\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3484, "score_of_the_acc": -0.8519, "final_rank": 18 }, { "submission_id": "aoj_2005_6793631", "code_snippet": "// I SELL YOU...! \n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<functional>\n#include<queue>\n#include<chrono>\n#include<iomanip>\n#include<map>\n#include<set>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll,ll>;\nusing TP = tuple<ll,ll,ll>;\nvoid init_io(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(18);\n}\nconst ll INF = 1e9;\nvoid solve() {\n ll n,m,s,g1,g2;\n cin >> n >> m >> s >> g1 >> g2;\n if ((n|m|s|g1|g2) == 0) return;\n s--;\n g1--;\n g2--;\n vector<vector<ll>> d(n, vector<ll>(n, INF));\n for(int i=0;i<m;i++) {\n ll b1,b2,c;\n cin >> b1 >> b2 >> c;\n b1--;\n b2--;\n d[b1][b2] = c;\n }\n for (int i=0;i<n;i++) {\n d[i][i] = 0;\n }\n for (int k=0;k<n;k++) {\n for (int i=0;i<n;i++) {\n for (int j=0;j<n;j++) {\n d[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n }\n }\n }\n ll ans = INF;\n for (int i=0;i<n;i++) {\n ans = min(ans, d[s][i] + d[i][g1] + d[i][g2]);\n }\n cout << ans << endl;\n solve();\n}\nsigned main(){\n init_io();\n solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 8384, "score_of_the_acc": -1.4, "final_rank": 20 } ]
aoj_2003_cpp
Railroad Conflict Nate U. Smith は大都市圏の鉄道会社を経営している.この大都市圏には,彼の鉄道会社以外にライバルの鉄道会社がある.2 社は互いに競合関係にある. この大都市圏では,列車の運行を容易にするため,全ての路線は両端の駅を結んだ線分を経路としている.また,踏切等の設置をさけるため,全ての路線は高架または地下に敷設されている.既存路線は,全線にわたって高架を走るか,または全線にわたって地下を走るかのいずれかである. ところで,最近,この大都市圏にある 2 つの街区 A,B がさかんに開発されていることから,Nate の会社ではこれらの街区の間に新しい路線を設けることを決断した.この新路線は,従来の路線と同様に A,B を両端とする線分を経路としたいが,経路の途中には別の路線があることから,高架あるいは地下のいずれかに全線を敷設することは不可能である.そこで,路線の一部を高架に,残りの部分を地下に敷設することにした.このとき,新路線が自社の既存路線と交わるところでは,新路線と既存路線との間の乗り継ぎを便利にするため,既存路線が高架ならば新路線も高架に,既存路線が地下ならば新路線も地下を走るようにする.また,新路線が他社の既存路線と交わるところでは,他社による妨害をさけるため,既存路線が高架ならば新路線は地下に,既存路線が地下ならば新路線は高架を走るようにする.A 駅および B 駅をそれぞれ高架あるいは地下のいずれに設けるかは特に問わない. 当然のことながら,新路線が高架から地下に,あるいは地下から高架に移るところには出入口を設けなければならない.ところが,出入口を設けるためには費用がかかるため,新路線に設ける出入口の個数は最小限に抑えたい.それには,プログラマであるあなたの助けを要すると考えた Nate は,あなたを彼の会社に呼び出した. あなたの仕事は,A 駅,B 駅の位置,そして既存路線に関する情報が与えられたときに,新路線において最低限設けなければならない出入口の個数を求めるプログラムを作成することである. 以下の図は,サンプルとして与えた入力および出力の内容を示したものである. Input 入力の先頭行には単一の正の整数が含まれ,これはデータセットの個数を表す.それぞれのデータセットは次の形式で与えられる. xa ya xb yb n xs 1 ys 1 xt 1 yt 1 o 1 l 1 xs 2 ys 2 xt 2 yt 2 o 2 l 2 ... xs n ys n xt n yt n o n l n ただし,( xa , ya ) および ( xb , yb ) はそれぞれ A 駅,B 駅の座標を表す. N は既存路線の数を表す 100 以下の正の整数である.( xs i , ys i ) および ( ys i , yt i ) は i 番目の既存路線における始点および終点の座標を表す. o i は i 番目の既存路線の所有者を表す整数である.これは 1 または 0 のいずれかであり,1 はその路線が自社所有であることを,0 は他社所有であることをそれぞれ表す. l i は i 番目の既存路線が高架または地下のいずれにあるかを表す整数である.これも 1 または 0 のいずれかであり,1 はその路線が高架にあることを,0 は地下にあることをそれぞれ表す. 入力中に現れる x 座標および y 座標の値は -10000 から 10000 の範囲にある(両端も範囲に含まれる).また,それぞれのデータセットにおいて,新路線を含めた全ての路線に対して,以下の条件を満たすと仮定してよい.ここで 2 点が非常に近いとは,その 2 点間の距離が 10 -9 以下であることを表す. ある交点の非常に近くに別の交点があることはない. ある路線の端点の非常に近くを別の路線が通ることはない. 2 つの路線の一部または全部が重なることはない. Output それぞれのデータセットに対して,最低限設けなければならない出入口の個数を 1 行に出力しなさい. Sample Input 2 -10 1 10 1 4 -6 2 -2 -2 0 1 -6 -2 -2 2 1 0 6 2 2 -2 0 0 6 -2 2 2 1 1 8 12 -7 -3 8 4 -5 4 -2 1 1 4 -2 4 9 1 0 6 9 6 14 1 1 -7 6 7 6 0 0 1 0 1 10 0 0 -5 0 -5 10 0 1 -7 0 7 0 0 1 -1 0 -1 -5 0 1 Output for the Sample Input 1 3
[ { "submission_id": "aoj_2003_10851339", "code_snippet": "#define _USE_MATH_DEFIMES\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <ctime>\n#include <deque>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <random>\n#include <regex>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\nconst int MOD = 1'000'000'007;\nconst int MOD2 = 998'244'353;\nconst int INF = 1'000'000'000; //1e9\nconst int NIL = -1;\nconst long long LINF = 1'000'000'000'000'000'000; // 1e18\nconst long double EPS = 1E-10;\n\ntemplate<class T, class S> inline bool chmax(T &a, const S &b){\n if(a < b){\n a = b; return true;\n }\n return false;\n}\ntemplate<class T, class S> inline bool chmin(T &a, const S &b){\n if(b < a){\n a = b; return true;\n }\n return false;\n}\n\n\n\n\n\n\nclass Point2D{\n long double x, y;\npublic:\n Point2D(long double X=0, long double Y=0): x(X), y(Y){}\n Point2D(const Point2D& v): x(v.x), y(v.y){}\n template<class T>\n Point2D(const std::vector<T> &v){\n assert(v.size() == 2);\n x = v[0]; y = v[1];\n }\n template<class T, class T2>\n Point2D(const std::pair<T, T2> &p){\n x = p.first; y = p.second;\n }\n\n void setX(long double X){x = X;}\n long double getX(){return x;}\n void setY(long double Y){y = Y;}\n long double getY(){return y;}\n\n Point2D operator+() const{return *this;}\n Point2D operator-() const{return Point2D(-x, -y);}\n\n Point2D& operator=(const Point2D& v){\n x = v.x; y = v.y;\n return *this;\n }\n\n Point2D& operator+=(const Point2D& v){\n x += v.x; y += v.y;\n return *this;\n }\n\n Point2D& operator-=(const Point2D& v){\n x -= v.x; y -= v.y;\n return *this;\n }\n\n Point2D& operator*=(const long double a){\n x *= a; y *= a;\n return *this;\n }\n\n Point2D& operator/=(const long double a){\n x /= a; y /= a;\n return *this;\n }\n\n Point2D operator+(const Point2D& v) const{\n Point2D tmp(*this);\n return tmp += v;\n }\n\n Point2D operator-(const Point2D& v) const{\n Point2D tmp(*this);\n return tmp -= v;\n }\n\n Point2D operator*(const long double a) const{\n Point2D tmp(*this);\n return tmp *= a;\n }\n\n friend Point2D operator*(long double a, const Point2D v){\n return v * a;\n }\n\n Point2D operator/(const long double a) const{\n Point2D tmp(*this);\n return tmp /= a;\n }\n\n long double norm(){return x*x + y*y;}\n friend long double norm(Point2D v){return v.norm();}\n long double abs(){return sqrt(norm());}\n friend long double abs(Point2D v){return v.abs();}\n\n bool operator<(const Point2D &v) const{\n return x != v.x ? x < v.x : y < v.y;\n }\n\n bool operator>(const Point2D &v) const{\n return x != v.x ? x > v.x : y > v.y;\n }\n\n bool operator==(const Point2D &v) const{\n return std::abs(x - v.x) < EPS\n && std::abs(y - v.y) < EPS;\n }\n\n bool operator<=(const Point2D &v) const{\n return (*this < v) || (*this == v);\n }\n\n bool operator>=(const Point2D &v) const{\n return (*this > v) || (*this == v);\n }\n\n long double dot(Point2D b){\n return x*b.y + y*b.y;\n }\n\n friend long double dot(Point2D a, Point2D b){\n return a.dot(b);\n }\n\n long double cross(Point2D b){\n return x*b.y - y*b.x;\n }\n\n friend long double cross(Point2D a, Point2D b){\n return a.cross(b);\n }\n\n friend long double distance(Point2D a, Point2D b){\n return (a - b).abs();\n }\n\n friend bool isParallel(Point2D a, Point2D b){\n return std::abs(a.cross(b)) < EPS;\n }\n\n friend bool isOrthogonal(Point2D a, Point2D b){\n return std::abs(a.dot(b)) < EPS;\n }\n\n int ccw(Point2D b){\n if(cross(b) > EPS) return 1;//反時計\n if(cross(b) < -EPS) return -1;//時計\n if(dot(b) < -EPS) return 2;//逆向き\n if(norm() < b.norm()) return -2;//同じ向きbが大きい\n return 0;//同じ向きaが大きい\n }\n\n friend int ccw(Point2D a, Point2D b){\n return a.ccw(b);//同じ向きaが大きい\n }\n\n int ccw(Point2D p1, Point2D p2){\n return (p1 - *this).ccw(p2 - *this);\n }\n\n friend int ccw(Point2D p0, Point2D p1, Point2D p2){\n return p0.ccw(p1, p2);\n }\n\n friend bool intersect(Point2D p1, Point2D p2, Point2D p3, Point2D p4){\n return(p1.ccw(p2, p3) * p1.ccw(p2, p4) <= 0 &&\n p3.ccw(p4, p1) * p3.ccw(p4, p2) <= 0);\n }\n \n std::vector<Point2D> convex_hll(std::vector<Point2D> &v){\n int n(v.size()), k(0);\n auto comp = [](Point2D a, Point2D b){\n if(a.x != b.x) return a.x < b.x;\n else return a.y < b.y;\n };\n std::sort(std::begin(v), std::end(v), comp);\n std::vector<Point2D> ret(2*n);\n //下側\n for(int i(0); i < n; ret[k++] = v[i++]){\n while(k >= 2 && ret[k-2].ccw(ret[k-2], v[i]) <= 0) --k;\n }\n //上側\n for(int i(n-2), t = k + 1; i >= 0;ret[k++] = v[i--]){\n while(k >= t && ret[k-2].ccw(ret[k-2], v[i]) <= 0) --k;\n }\n ret.resize(k-1);\n return ret;\n }\n};\nusing Vector2D = Point2D;\n\nclass Line2D{\n Point2D p1, p2;\npublic:\n Line2D(){}\n Line2D(Point2D a, Point2D b): p1(a), p2(b){}\n\n void setP1(const Point2D p){p1 = p;}\n Point2D getP1(){return p1;}\n void setP2(const Point2D p){p2 = p;}\n Point2D getP2(){return p2;}\n\n Point2D project(Point2D p){\n Point2D base(p2 - p1);\n long double r(dot(p - p1, base) / norm(base));\n return p1 + base * r;\n }\n\n friend Point2D project(Line2D l, Point2D p){\n return l.project(p);\n }\n\n friend Point2D reflect(Line2D l, Point2D p){\n return p + 2.0 * (l.project(p) - p);\n }\n\n friend bool isParallel(Line2D s1, Line2D s2){\n return isParallel(s1.p2 - s1.p1, s2.p2- s2.p1);\n }\n\n friend bool isOrthogonal(Line2D s1, Line2D s2){\n return isOrthogonal(s1.p2 - s1.p1, s2.p2- s2.p1);\n }\n\n friend bool intersect(Line2D s1, Line2D s2){\n return intersect(s1.p1, s1.p2, s2.p1, s2.p2);\n }\n\n friend Point2D getCrossPoint(Line2D s1, Line2D s2){\n Vector2D base(s2.p2 - s2.p1);\n long double d1(std::abs(cross(base, s1.p1 - s2.p1)));\n long double d2(std::abs(cross(base, s1.p2 - s2.p1)));\n long double t(d1 / (d1 + d2));\n return s1.p1 + (s1.p2 - s1.p1) * t;\n }\n};\nusing Segment2D = Line2D;\n\nint main(){\n int Q; std::cin >> Q;\n while(Q--){\n long double xa, ya, xb, yb;\n std::cin >> xa >> ya >> xb >> yb;\n Point2D A(xa, ya), B(xb, yb);\n Segment2D AB(A, B);\n std::vector<std::pair<long double, int>> cross;\n int n; std::cin >> n;\n {\n long double xs, ys, xt, yt;\n int o, l;\n while(n--){\n std::cin >> xs >> ys >> xt >> yt >> o >> l;\n Point2D S(xs, ys), T(xt, yt);\n Segment2D ln(S, T);\n if(intersect(AB, ln)){\n auto t(getCrossPoint(AB, ln));\n cross.emplace_back(abs(t - A), o ^ l);\n }\n }\n }\n std::sort(std::begin(cross), std::end(cross));\n if(cross.empty()){\n std::cout << 0 << \"\\n\";\n continue;\n }\n int cur(cross[0].second), ans(0);\n for(int i(1), i_len(cross.size()); i < i_len; ++i){\n if(cur != cross[i].second){\n cur = cross[i].second; ++ans;\n }\n }\n std::cout << ans << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3368, "score_of_the_acc": -1.5657, "final_rank": 11 }, { "submission_id": "aoj_2003_10645501", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// 書きかけの幾何ライブラリ\n// バグがあるかも\n\nnamespace geometry {\n\nusing Real = long double;\nconstexpr Real eps = 1e-12;\nconstexpr Real PI = acosl(-1);\nconstexpr Real INF_REAL = numeric_limits<Real>::max();\n\nbool equal_eps(Real a, Real b) {\n return abs(a - b) < eps;\n}\n\nint sgn(Real a) {\n return equal_eps(a, 0) ? 0 : a > 0 ? 1 : -1;\n}\n\nstruct Point {\n Real x, y;\n constexpr Point() : Point(0, 0) {}\n constexpr Point(Real _x, Real _y) : x(_x), y(_y) {}\n constexpr Point(const pair<Real, Real> &p) : x(p.first), y(p.second) {}\n Point &operator+=(const Point &p) {\n x += p.x;\n y += p.y;\n return *this;\n }\n Point &operator-=(const Point &p) {\n x -= p.x;\n y -= p.y;\n return *this;\n }\n Point &operator*=(const Real &r) {\n x *= r;\n y *= r;\n return *this;\n }\n Point &operator/=(const Real &r) {\n assert(!equal_eps(r, 0));\n x /= r;\n y /= r;\n return *this;\n }\n Point operator+(const Point &p) const { return Point(*this) += p; }\n Point operator-(const Point &p) const { return Point(*this) -= p; }\n Point operator*(const Real &r) const { return Point(*this) *= r; }\n Point operator/(const Real &r) const { return Point(*this) /= r; }\n Point operator-() const { return Point() - *this; }\n bool operator==(const Point &p) const {\n return equal_eps(x, p.x) && equal_eps(y, p.y);\n }\n bool operator!=(const Point &p) const {\n return !equal_eps(x, p.x) || !equal_eps(y, p.y);\n }\n Point rotate(Real rad) const {\n Real c = cosl(rad), s = sinl(rad);\n Real tx = x * c - y * s;\n Real ty = x * s + y * c;\n return Point(tx, ty);\n }\n Point unit() const {\n return (*this) / metric();\n }\n Real Re() const { return x; }\n Real Im() const { return y; }\n Real norm() const { return x * x + y * y; }\n Real metric() const { return sqrtl(norm()); }\n Real arg() const { return atan2l(y, x); }\n Real arg(Point q1, Point q2) const {\n q1 -= *this, q2 -= *this;\n return abs(q1.arg() - q2.arg());\n }\n friend Point operator*(Real r, const Point &p) {\n return p * r;\n }\n friend Point operator/(Real r, const Point &p) {\n return p / r;\n }\n};\n\nReal Re(const Point &p) { return p.Re(); }\nReal Im(const Point &p) { return p.Im(); }\nReal dot(const Point &p, const Point &q) {\n return p.x * q.x + p.y * q.y;\n}\nReal cross(const Point &p, const Point &q) {\n return p.x * q.y - p.y * q.x;\n}\nReal norm(const Point &p) {\n return p.norm();\n}\nReal metric(const Point &p) {\n return p.metric();\n}\nReal metric(const Point &p, const Point &q) {\n return metric(p - q);\n}\nReal arg(const Point &p) {\n return p.arg();\n}\n\nbool equal_point(const Point &p, const Point &q) {\n return equal_eps(p.x, q.x) && equal_eps(p.y, q.y);\n}\n\nconstexpr Point INF_POINT = Point(INF_REAL, INF_REAL);\n\n// a を基準にした b と c の位置関係\nint ccw(const Point &a, const Point &b, const Point &c) {\n Point p = b - a, q = c - a;\n if (cross(p, q) > eps) return 1; // a, b, c の順で反時計回り\n if (cross(p, q) < -eps) return -1; // a, b, c の順で時計回り\n if (min(norm(p), norm(q)) < eps * eps) return 0; // a = c or b = c\n if (dot(p, q) < eps) return 2; // c, a, b の順で一直線\n if (norm(p) < norm(q)) return -2; // a, b, c の順で一直線\n return 0; // a, c, b の順で一直線\n}\n\nistream &operator>>(istream &is, Point& p) {\n Real x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\n\n} // namespace geometry\n\nnamespace geometry {\n\nstruct Line {\n // Line : ax + by = c\n Real a, b, c;\n constexpr Line() = default;\n constexpr Line(Real _a, Real _b, Real _c) : a(_a), b(_b), c(_c) {}\n constexpr Line(const Point &p, const Point &q) : a(q.y - p.y), b(p.x - q.x), c(p.x * q.y - p.y * q.x) {}\n constexpr Line(Real _a, const Point &p) : a(_a), b(-1), c(_a * p.x - p.y) {}\n bool online(const Point &p) const {\n return equal_eps(a * p.x + b * p.y, c);\n }\n Real get_x(Real y) const {\n if (equal_eps(a, 0)) return INF_REAL;\n return (c - b * y) / a;\n }\n Real get_y(Real x) const {\n if (equal_eps(b, 0)) return INF_REAL;\n return (c - a * x) / b;\n }\n Line perpendicular(const Point &p) const {\n if (equal_eps(a, 0)) return Line(1, 0, p.x);\n if (equal_eps(0, b)) return Line(0, 1, p.y);\n return Line(b / a, p);\n }\n Point crossppoint(const Line &l) const {\n if (equal_eps(a * l.b, l.a * b)) return INF_POINT;\n Real d = a * l.b - l.a * b;\n return Point(l.b * c - b * l.c, l.c * a - c * l.a) / d;\n }\n Point projection(const Point &p) const {\n return crossppoint(perpendicular(p));\n }\n Point reflection(const Point &p) const {\n Point q = projection(p);\n return 2 * q - p;\n }\n Point unit() const {\n if (equal_eps(b, 0)) {\n return Point(0, 1);\n }\n Point p(0, get_y(0)), q(1, get_y(1));\n return (p - q) / metric(p - q);\n }\n bool is_orthogonal(const Line &l) const {\n return equal_eps(a * l.a + b * l.b, 0);\n }\n bool is_parallel(const Line &l) const {\n return equal_eps(a * l.b, l.a * b);\n }\n Real dist(const Point &p) const {\n Point q = projection(p);\n return metric(p - q);\n }\n};\n\nbool is_orthogonal(const Line &l1, const Line &l2) {\n return l1.is_orthogonal(l2);\n}\nbool is_parallel(const Line &l1, const Line &l2) {\n return l1.is_parallel(l2);\n}\nPoint crosspoint(const Line &l1, const Line &l2) {\n return l1.crossppoint(l2);\n}\n\n} // namespace geometry\n\nnamespace geometry {\n\nstruct Segment {\n Line l;\n Point p, q;\n Segment() = default;\n Segment(const Point &_p, const Point &_q) : l(_p, _q), p(_p), q(_q) {}\n bool cross(const Segment &s) const {\n bool f1 = ccw(p, q, s.p) * ccw(p, q, s.q) <= 0;\n bool f2 = ccw(s.p, s.q, p) * ccw(s.p, s.q, q) <= 0;\n return f1 && f2;\n }\n Point crosspoint(const Segment &s) const {\n if (cross(s)) return l.crossppoint(s.l);\n return INF_POINT;\n }\n Real dist(const Point &r) const {\n if (dot(q - p, r - p) < eps) return (r - p).metric();\n if (dot(p - q, r - q) < eps) return (r - q).metric();\n return l.dist(r);\n }\n Real dist(const Segment &s) const {\n if (cross(s)) return 0;\n Real res = INF_REAL;\n res = min(res, dist(s.p));\n res = min(res, dist(s.q));\n res = min(res, s.dist(p));\n res = min(res, s.dist(q));\n return res;\n }\n};\n\nbool cross(const Segment &s, const Segment &t) {\n return s.cross(t);\n}\n\nPoint crosspoint(const Segment &s, const Segment &t) {\n return s.crosspoint(t);\n}\n\nReal dist(const Segment &s, const Segment &t) {\n return s.dist(t);\n}\n\n} // nemespace geometry\nusing namespace geometry;\n\nvoid solve() {\n Point a, b;\n cin >> a >> b;\n Segment S(a, b);\n auto get = [&] (Real t) -> Point {\n return a + t * (b - a);\n };\n auto time = [&] (const Point p) -> Real {\n Real l = 0, r = 1;\n for (int t = 0; t < 100; t++) {\n Real m = (l + r) / 2;\n auto q = get(m);\n (metric(a, q) <= metric(a, p) ? l : r) = m;\n }\n return r;\n };\n int N;\n cin >> N;\n vector<pair<Real, bool>> Ts;\n while (N--) {\n Point s, t;\n int o, l;\n cin >> s >> t >> o >> l;\n Segment T(s, t);\n auto p = S.crosspoint(T);\n if (!equal_point(p, INF_POINT)) {\n Ts.emplace_back(time(p), o ^ l);\n }\n }\n sort(Ts.begin(), Ts.end());\n int ans = 0, k = -1;\n for (auto [_, f] : Ts) {\n if (k != -1 && k != f) ans++;\n k = f;\n }\n cout << ans << '\\n';\n}\n\nint main() {\n int T;\n cin >> T;\n while (T--) solve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3584, "score_of_the_acc": -1.8384, "final_rank": 20 }, { "submission_id": "aoj_2003_10579495", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\n\n//参考 https://github.com/saphmitchy/deliair-lib\n// 型名\n// R:Real, P:Point, L:Line, S:Segment, C:Circle, VP:vector<Point>\n\n#define X(p) real(p)\n#define Y(p) imag(p)\n\nusing R = ld;\nusing P = complex<R>;\nusing VP = vector<P>;\n\n//const R EPS = 1e-9; // ここは適宜調節する,いつものやつから消す\nconst R pi = acos(-1.0);\n\nint sgn(R a) {\n return (a < -EPS) ? -1 : (a > EPS) ? 1 : 0;\n} // 符号関数\n\nbool eq(R a, R b) {//実数の一致判定\n return sgn(b - a) == 0;\n}\n\nP operator*(P p, R d) {//ベクトルのd倍\n return P(X(p) * d, Y(p) * d);\n}\n\nP operator/(P p, R d) {//ベクトルの1/d倍\n return p * (1 / d);\n}\n\nistream &operator>>(istream &is, P &p) {\n // R a, b; // 入力が小数\n int a, b; // 入力が整数\n is >> a >> b;\n p = P(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, P p) {\n return os << X(p) << ' ' << Y(p);\n}\n\nR getarg(P b,P a){//ベクトルbはベクトルaを何radian回転させる必要があるか\n assert(sgn(abs(a)) != 0);//長さが0はだめ\n return arg(b/a);\n}\n\nbool cp_x(P p, P q) {//ベクトルの比較x軸で比較->y軸で比較\n if (!eq(X(p), X(q)))\n return X(p) < X(q);\n return Y(p) < Y(q);\n}\n\nbool cp_y(P p, P q) {//ベクトルの比較y軸で比較->x軸で比較\n if (!eq(Y(p), Y(q)))\n return Y(p) < Y(q);\n return X(p) < X(q);\n}\n\nstruct L {//直線ab\n P a, b;\n L() {}\n L(P a, P b) : a(a), b(b) {}\n\n // 入出力(必要なら)\n friend ostream &operator<<(ostream &os, L &l) {\n return os << l.a << ' ' << l.b;\n }\n friend istream &operator>>(istream &is, L &l) {\n return is >> l.a >> l.b;\n }\n};\n\nstruct S : L {//線分ab\n S() {}\n S(P a, P b) : L(a, b) {}\n};\n\nstruct C {//中心p 半径rの円\n P p;\n R r;\n C() {}\n C(P p, R r) : p(p), r(r) {}\n};\n\nP rot(P p, R t) {//ベクトルの回転\n return p * P(cos(t), sin(t));\n}\n\n//2つのベクトルの内積\nR dot(P p, P q) {\n return X(p) * X(q) + Y(p) * Y(q);\n}\n\n//2つのベクトルの外積\nR det(P p, P q) {\n return X(p) * Y(q) - Y(p) * X(q);\n}\n\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=jp\nint ccw(P a, P b, P c) { // 線分 ab に対する c の位置関係 \n b -= a, c -= a;//ベクトルab,ベクトルacにした\n if (sgn(det(b, c)) == 1)//外積右ねじ正\n return +1; // COUNTER_CLOCKWISE a,b,cが反時計回り\n if (sgn(det(b, c)) == -1)\n return -1; // CLOCKWISE\n if (dot(b, c) < 0.0)\n return +2; // ONLINE_BACK\n if (norm(b) < norm(c))\n return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool para(L a, L b) { // 平行判定\n return eq(det(a.b - a.a, b.b - b.a), 0.0);\n}\n\nbool orth(L a, L b) { // 垂直判定\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\n\nP proj(L l, P p) { // 垂線の足\n R t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\n\n// これいる?\n// P proj(S s, P p) {\n// R t = dot(p - s.a, s.b - s.a) / norm(s.b - s.a);\n// return s.a + (s.b - s.a) * t;\n// }\n\nP refl(L l, P p) { // 線対称の位置にある点\n return p + (proj(l, p) - p) * 2.0;\n}\n\nbool inter(L l, P p) { // 交点を持つか判定\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\nbool inter(S s, P p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool inter(L l, L m) {\n if (!eq(det(l.b - l.a, m.b - m.a), 0.0))\n return true;\n return eq(det(l.b - l.a, m.b - l.a), 0.0);\n}\n\nbool inter(L l, S s) {\n return sgn(det(l.b - l.a, s.a - l.a) * det(l.b - l.a, s.b - l.a)) <= 0;\n}\n\nbool inter(S s, L l) {\n return inter(l, s);\n}\n\nbool inter(S s, S t) {\n if (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) > 0)\n return false;\n return ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nR dist(P p, P q) {\n return abs(q - p);\n}\n\nR dist(L l, P p) {\n return abs(p - P(proj(l, p)));\n}\n\n//R dist(S s, P p) {\n// P h = proj(s, p);\n// if (inter(s, h))\n// return abs(h - p);\n// return min(abs(s.a - p), abs(s.b - p));\n//}\n\nR dist(S s, P p) {\n P h = proj(s, p);\n if(dot(p-s.a,s.b-s.a) <= 0) return abs(s.a-p);\n if(dot(p-s.b,s.a-s.b) <= 0) return abs(s.b-p);\n return abs(p - h);\n}\n\nR dist(L l, L m) {\n return inter(l, m) ? 0.0 : dist(l, m.a);\n}\n\nR dist(S s, S t) {\n if (inter(s, t))\n return 0.0;\n return min({dist(s, t.a), dist(s, t.b), dist(t, s.a), dist(t, s.b)});\n}\n\nR dist(L l, S s) {\n if (inter(l, s))\n return 0.0;\n return min(dist(l, s.a), dist(l, s.b));\n}\n\nR dist(S s, L l) {\n return dist(l, s);\n}\n\nbool inter(C c, L l) {\n return sgn(c.r - dist(l, c.p)) >= 0;\n}\n\nbool inter(C c, P p) {\n return eq(abs(p - c.p), c.r);\n}\n\n// 共通接線の本数\n// 交点なし:4\n// 外接:3\n// 2点で交わる:2\n// 内接:1\n// 一方がもう一方を内包:0\nint inter(C c1, C c2) {\n if (c1.r < c2.r)\n swap(c1, c2);\n R d = abs(c1.p - c2.p);\n int a = sgn(d - c1.r - c2.r);\n if (a >= 0)\n return 3 + a;\n return 1 + sgn(d - c1.r + c2.r);\n}\n\nVP crosspoint(L l, L m) {\n VP ret;\n if (!inter(l, m))\n return ret;\n R A = det(l.b - l.a, m.b - m.a);\n R B = det(l.b - l.a, l.b - m.a);\n if (eq(A, 0.0) && eq(B, 0.0)) {\n ret.emplace_back(m.a);\n } else {\n ret.emplace_back(m.a + (m.b - m.a) * B / A);\n }\n return ret;\n}\n\nVP crosspoint(S s, S t) {\n return inter(s, t) ? crosspoint(L(s), L(t)) : VP();\n}\n\nVP crosspoint(C c, L l) {//円と直線の交点\n P h = proj(l, c.p);\n P e = (l.b - l.a) / abs(l.b - l.a);\n VP ret;\n if (!inter(c, l))\n return ret;\n if (eq(dist(l, c.p), c.r)) {\n ret.emplace_back(h);\n } else {\n R b = sqrt(c.r * c.r - norm(h - c.p));\n ret.push_back(h + e * b), ret.push_back(h - e * b);\n }\n return ret;\n}\n\nVP crosspoint(C c, S s) {//円と線分の交点\n P h = proj(s, c.p);\n P e = (s.b - s.a) / abs(s.b - s.a);\n VP ret;\n if (!inter(c, s))\n return ret;\n if (eq(dist(s, c.p), c.r)) {\n ret.emplace_back(h);\n } else {\n R b = sqrt(c.r * c.r - norm(h - c.p));\n if(ccw(s.a,s.b,h - e * b) == 0){//s.aに近い方から線分上なら追加する\n ret.push_back(h - e * b);\n }\n if(ccw(s.a,s.b,h + e * b)==0){\n ret.push_back(h + e * b);\n }\n }\n return ret;\n}\n\nVP crosspoint(C c1, C c2) {//2つの円の交わる点\n R d = abs(c1.p - c2.p);\n R a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n R t = atan2(Y(c2.p) - Y(c1.p), X(c2.p) - X(c1.p));\n VP ret;\n if (inter(c1, c2) % 4 == 0) // 交わらないとき\n return ret;\n if (eq(a, 0.0)) {\n ret.emplace_back(P(c1.p + rot(P(c1.r, 0.0), t)));\n } else {\n P p1 = c1.p + rot(P(c1.r, 0.0), t + a);\n P p2 = c1.p + rot(P(c1.r, 0.0), t - a);\n ret.emplace_back(p1), ret.emplace_back(p2);\n }\n return ret;\n}\n\nVP cut(VP p, L l, bool border = true) { // 直線が多角形に切り取られる区間\n int n = sz(p);\n p.emplace_back(p[0]), p.emplace_back(p[1]);\n VP ret;\n rep(i, n) {\n if (!eq(dist(l, p[i]), 0) && !eq(dist(l, p[i + 1]), 0)) {\n S s(p[i], p[i + 1]);\n if (eq(dist(l, s), 0)) {\n auto res = crosspoint(l, s);\n ret.emplace_back(res[0]);\n }\n }\n if (eq(dist(l, p[i + 1]), 0)) {\n if ((eq(dist(l, p[i]), 0) || eq(dist(l, p[i + 2]), 0)) && !border)\n continue;\n S s(p[i], p[i + 2]);\n if (eq(dist(l, s), 0))\n ret.emplace_back(p[i + 1]);\n }\n }\n return ret;\n}\n\nVP rectangle(S s, R r) { // sを軸とした幅rの長方形\n P d = (s.a - s.b) * P(0, 1);\n d *= r / sqrt(norm(d));\n return VP{s.a + d, s.a - d, s.b - d, s.b + d};\n}\n\nL vertical_bisector(P p, P q) { // 垂直二等分線\n L l;\n l.a = (p + q) * 0.5;\n l.b = l.a + rot(q - p, pi * 0.5);\n return l;\n}\n\nL angle_bisector(P a,P b,P c){//角abcの二等分線(角bの2等分線)\n L l;\n l.a = b;\n R ang = atan2(Y(c-b),X(c-b)) - atan2(Y(a-b),X(a-b));//なす角\n ang/=2.0;\n l.b = l.a + rot(a-b,ang);\n return l;\n}\n\nC Apollonius(P p, P q, R a, R b) { // アポロニウスの円\n P p1 = (p * b + q * a) / (a + b), p2 = (-p * b + q * a) / (a - b);\n C c;\n c.p = (p1 + p2) * 0.5;\n c.r = abs(p1 - p2) * 0.5;\n return c;\n}\n\nR area(VP p) { // 多角形の面積\n R ret = 0.0;\n int n = sz(p);\n rep(i, n) ret += det(p[i], p[(i + 1) % n]);\n return abs(ret * 0.5);\n}\n\nint in_polygon(VP p, P q) { // IN:2, ON:1, OUT:0\n int n = sz(p);\n int ret = 0;\n rep(i, n) {\n P a = p[i] - q, b = p[(i + 1) % n] - q;\n if (eq(det(a, b), 0.0) && sgn(dot(a, b)) <= 0)\n return 1;\n if (Y(a) > Y(b))\n swap(a, b);\n if (sgn(Y(a)) <= 0 && sgn(Y(b)) == 1 && sgn(det(a, b)) == 1)\n ret ^= 2;\n }\n return ret;\n}\n\nVP tangent(C c, P p) { // 点 p を通る円 c の接線と c の接点\n return crosspoint(c, C(p, sqrt(norm(p - c.p) - c.r * c.r)));\n}\n\nvector<L> tangent(C c1, C c2) { // 共通接線\n vector<L> ret;\n if (c1.r < c2.r)\n swap(c1, c2);\n R r = abs(c2.p - c1.p);\n if (eq(r, 0.0))\n return ret;\n P u = (c2.p - c1.p) / r;\n P v = rot(u, pi * 0.5);\n for (R s : {1.0, -1.0}) {\n R h = (c1.r + c2.r * s) / r;\n if (eq(abs(h), 1.0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if (abs(h) < 1.0) {\n P uu = u * h, vv = v * sqrt(1.0 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\nVP convex_hull(VP p) { // 凸包\n sort(all(p), cp_x);\n p.erase(unique(all(p)), end(p));\n int n = sz(p), k = 0;\n if (n == 1)\n return p;\n VP ch(2 * n);\n for (int i = 0; i < n; ch[k++] = p[i++]) {\n while (k >= 2 && sgn(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) <= 0)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while (k >= t && sgn(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) <= 0)\n k--;\n }\n ch.resize(k - 1);\n return ch;\n}\n\nR closest_pair(VP p) { // 最近点対の距離\n if (sz(p) <= 1)\n return 1e18;\n sort(all(p), cp_x);\n VP memo(sz(p));\n\n function<R(int, int)> rec = [&](int l, int r) {\n if (r - l <= 1)\n return R(1e18);\n int m = (l + r) >> 1;\n R x = X(p[m]);\n R ret = min(rec(l, m), rec(m, r));\n inplace_merge(p.begin() + l, p.begin() + m, p.begin() + r, cp_y);\n int cnt = 0;\n reps(i, l, r) {\n if (abs(X(p[i]) - x) >= ret)\n continue;\n rep(j, cnt) {\n P d = p[i] - memo[cnt - j - 1];\n if (Y(d) >= ret)\n break;\n chmin(ret, abs(d));\n }\n memo[cnt++] = p[i];\n }\n return ret;\n };\n\n return rec(0, sz(p));\n}\n\nR farthest_pair(VP p) {//最遠点対の距離\n VP ps = convex_hull(p);\n ll n = ps.size();\n if(n == 2){//凸包が潰れてる\n return dist(ps[0],ps[1]);\n }\n ll i = 0,j = 0;\n rep(k,n){//x軸方向に最も遠い点対を求める\n if(cp_x(ps[k],ps[i]))i = k;\n if(cp_x(ps[j],ps[k]))j = k;\n }\n R ret = 0;\n ll si = i,sj = j;\n while(i != sj || j != si){//180度反転しきるまで\n ret = max(ret,dist(ps[i],ps[j]));\n if(det(ps[(i+1)%n] - ps[i],ps[(j+1)%n]-ps[j]) < 0){\n i = (i + 1) % n;\n }else{\n j = (j + 1) % n;\n }\n }\n return ret;\n}\n\n// 原点, 点 a, 点 b とで囲まれる領域の面積 (三角形 ver と扇型 ver)\nR calc_element(P a, P b, R cr,bool triangle){\n if(triangle)return det(a,b)/2;\n else{\n P tmp = b * (P(X(a),-Y(a)));\n R ang = atan2(Y(tmp),X(tmp));\n return cr * cr * ang/2;\n }\n}\n\n// 円 C と、三角形 ((0, 0), ia, ib) との共通部分の面積\nR common_area(C c, P ia,P ib){\n P a = ia - c.p , b = ib - c.p;\n if(eq(abs(a-b),0))return 0;\n bool isin_a = (sgn(c.r - abs(a))>= 0);\n bool isin_b = (sgn(c.r - abs(b))>= 0);\n if(isin_a && isin_b)return calc_element(a,b,c.r,true);//aもbも円の中\n\n C oc(P(0,0),c.r);\n S seg(a,b);\n VP cr = crosspoint(oc,seg);\n if(cr.empty())return calc_element(a,b,c.r,false);\n P s = cr[0],t = cr.back();\n return calc_element(s,t,c.r,true) + calc_element(a,s,c.r,isin_a) + calc_element(t,b,c.r,isin_b);\n\n}\n\n\nR common_area(C c, VP vp){// 円cと多角形の共通部分の面積\n R ret = 0;\n ll n = vp.size();\n rep(i,n){\n ret += common_area(c,vp[i],vp[(i+1)%n]);\n }\n return ret;\n}\n\nR common_area(C p, C q) {// 円と円の共通部分の面積\n R d = abs(p.p - q.p);\n if (d >= p.r + q.r - EPS) return 0;\n else if (d <= abs(p.r - q.r) + EPS) return min(p.r, q.r) * min(p.r, q.r) * pi;\n R pcos = (p.r*p.r + d*d - q.r*q.r) / (p.r*d*2);\n R pang = acosl(pcos);\n R parea = p.r*p.r*pang - p.r*p.r*sin(pang*2)/2;\n R qcos = (q.r*q.r + d*d - p.r*p.r) / (q.r*d*2);\n R qang = acosl(qcos);\n R qarea = q.r*q.r*qang - q.r*q.r*sin(qang*2)/2;\n return parea + qarea;\n}\n\nvector<VP> divisions(vector<L> lf, R lim = 1e9) {\n vector<L> ls;\n each(l, lf) {\n bool ok = true;\n each(m, ls) {\n if (para(l, m) & inter(l, m.a)) {\n ok = false;\n break;\n }\n }\n if (ok)\n ls.emplace_back(l);\n }\n VP lc{P(-lim, -lim), P(lim, -lim), P(lim, lim), P(-lim, lim)};\n rep(i, 4) ls.emplace_back(lc[i], lc[(i + 1) % 4]);\n int m = sz(ls);\n VP ps;\n vector<vector<int>> lp(m);\n rep(i, m) {\n reps(j, i + 1, m) {\n each(p, crosspoint(ls[i], ls[j])) {\n if (max(abs(X(p)), abs(Y(p))) < lim + EPS) {\n lp[i].emplace_back(sz(ps)), lp[j].emplace_back(sz(ps));\n ps.emplace_back(p);\n }\n }\n }\n }\n int n = sz(ps);\n vector<int> id(n, -1), to;\n vector<R> rg;\n vector<vector<pair<R, int>>> li(n);\n rep(i, m) {\n sort(all(lp[i]), [&ps](int a, int b) { return cp_x(ps[a], ps[b]); });\n vector<int> q;\n rep(j, sz(lp[i])) {\n int me = id[lp[i][j]], st = j;\n auto np = ps[lp[i][j]];\n while (j + 1 < sz(lp[i])) {\n if (abs(ps[lp[i][j + 1]] - np) < EPS) {\n j++;\n if (id[lp[i][j]] != -1)\n me = id[lp[i][j]];\n } else\n break;\n }\n if (me == -1)\n me = lp[i][st];\n reps(k, st, j + 1) id[lp[i][k]] = me;\n q.emplace_back(me);\n }\n rep(i, sz(q) - 1) {\n P d = ps[q[i + 1]] - ps[q[i]];\n R s = atan2(Y(d), X(d)), t = atan2(-Y(d), -X(d));\n int x = q[i], y = q[i + 1];\n li[x].emplace_back(s, sz(to));\n li[x].emplace_back(s + pi * 2, sz(to));\n to.emplace_back(y), rg.emplace_back(t);\n li[y].emplace_back(t, sz(to));\n li[y].emplace_back(t + pi * 2, sz(to));\n to.emplace_back(x), rg.emplace_back(s);\n }\n }\n rep(i, n) sort(all(li[i]));\n vector<bool> u(sz(to), false);\n vector<VP> ret;\n rep(i, n) {\n each(l, li[i]) {\n int ns = l.second;\n if (u[ns])\n continue;\n VP nv;\n int no = ns;\n bool ok = true;\n while (1) {\n if (sz(nv) > 1) {\n P x = nv[sz(nv) - 2], y = nv[sz(nv) - 1], z = ps[to[no]];\n int c = ccw(x, y, z);\n if (c == 1)\n ok = false;\n if (c != -1)\n nv.pop_back();\n }\n nv.emplace_back(ps[to[no]]);\n u[no] = true;\n no = upper_bound(all(li[to[no]]), pair(rg[no] + EPS, -1))->second;\n if (no == ns)\n break;\n }\n if (ok)\n ret.emplace_back(nv);\n }\n }\n return ret;\n}\n\n//ref https://github.com/drken1215/algorithm/blob/master/Geometry/arg_sort.cpp\n//verify https://atcoder.jp/contests/abc139/submissions/me\n//点列を偏角ソート\nvoid arg_sort(VP &v){\n //原点=0,(pi,2pi] = -1 (0pi,pi] = 1\n auto sign = [&](const P &p){\n if(sgn(X(p)) == 0 && sgn(Y(p)) == 0){\n return 0;\n }else if(sgn(Y(p)) == -1 || (sgn(Y(p)) == 0 && sgn(X(p)) == 1)){\n return -1;\n }else{\n return 1;\n }\n };\n auto cp = [&](const P &p,const P &q){\n if(sign(p) != sign(q)){\n return sign(p) < sign(q);\n }else{//外積>0で判定\n //同じ向きにあるときは未定義、それぞれ決める\n return X(p) * Y(q) - Y(p) * X(q) > 0;\n }\n };\n sort(v.begin(),v.end(),cp);\n}\n\n\nvoid solve(){\n S ab;\n {\n LL(xa,ya,xb,yb);\n ab = S(P(xa,ya),P(xb,yb));\n }\n LL(n);\n vector<pair<ld,ll>> crospt;\n rep(i,n){\n LL(xs,ys,xt,yt,o,l);\n S st =S(P(xs,ys),P(xt,yt));\n VP ret = crosspoint(ab,st);\n each(p,ret){\n ld d = dist(p,ab.a);\n if(o == 1){\n //自分\n crospt.emplace_back(d,l);\n }else {\n crospt.emplace_back(d,l^1);\n }\n }\n }\n sort(all(crospt));\n ll siz = sz(crospt);\n\n vvll dp(siz+1,vll(2,INF));\n dp[0][0] =0;\n dp[0][1] =0;\n rep(i,siz){\n rep(j,2)if(crospt[i].second == j){\n rep(k,2){\n if(j == k){\n chmin(dp[i+1][j],dp[i][k]);\n }else{\n chmin(dp[i+1][j],dp[i][k] + 1);\n }\n }\n }\n }\n cout << min(dp[siz][0],dp[siz][1]) << endl;\n\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n LL(t);\n while(t--){\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_2003_9726195", "code_snippet": "#include <bits/stdc++.h>\n using namespace std;\n \n template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\n template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n #define rep(i,n) for (int i = 0; i < (n); ++i)\n\n typedef long long ll;\n typedef unsigned long long ull;\n using P=pair<ll,ll>;\n using T=tuple<int,int,int>;\n const int INF=1001001001;\n const ll INFL=1e18;\n const int mod=998244353;\n\n\t#define EPS (1e-10)\n\t#define equals(a,b)(fabs((a)-(b))<EPS)\n\n\tclass Point{\n\t\tpublic:\n\t\tdouble x,y;\n\n\t\tPoint(double x=0,double y=0):x(x),y(y){}\n\n\t\tPoint operator + (Point p){return Point(x+p.x,y+p.y);}\n\t\tPoint operator - (Point p){return Point(x-p.x,y-p.y);}\n\t\tPoint operator * (double a){return Point(a*x,a*y);}\n\t\tPoint operator / (double a){return Point(x/a,y/a);}\n\n\t\tdouble abs(){return sqrt(norm());}\n\t\tdouble norm(){return x*x+y*y;}\n\n\t\tbool operator < (const Point &p) const{\n\t\t\treturn x!=p.x ? x < p.x: y < p.y;\n\t\t}\n\t\tbool operator == (const Point &p) const{\n\t\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) <EPS;\n\t\t}\n\t};\n\ttypedef Point Vector;\n\tstruct Segment{\n\t\tPoint p1,p2;\n\t\tSegment(Point p1,Point p2):p1(p1),p2(p2){}\n\t};\n\ttypedef Segment Line;\n\tclass Circle{\n\tpublic:\n\t\tPoint c;\n\t\tdouble r;\n\t\tCircle(Point c=Point(),double r=0.0):c(c),r(r){}\n\t};\n\ttypedef vector<Point>Polygon;\n\t\n\tdouble dot(Vector a,Vector b){\n\t\treturn a.x*b.x+a.y*b.y;\n\t}\n\tdouble cross(Vector a,Vector b){\n\t\treturn a.x*b.y-a.y*b.x;\n\t}\n\tPoint rot90(const Point& p){return Point(-p.y,p.x);}\n\tPoint getCrossPoint(Line s1,Line s2){//2線分の交点\n\t\tVector base=s2.p2-s2.p1;\n\t\tdouble d1=abs(cross(base,s1.p1-s2.p1));\n\t\tdouble d2=abs(cross(base,s1.p2-s2.p1));\n\t\tdouble t=d1/(d1+d2);\n\t\treturn s1.p1+(s1.p2-s1.p1)*t;\n\t}\n\tPoint intersection(Line s1,Line s2){//2直線の交点 http://www.deqnotes.net/acmicpc/2d_geometry/lines\n\t\tVector a=s1.p2-s1.p1,b=s2.p2-s2.p1;\n\t\treturn s1.p1+a*cross(b,s2.p1-s1.p1)/cross(b,a);\n\t}\n\tPoint circumcenter(Point a,Point b,Point c){\n\t\tLine ab((a+b)/2,(a+b)/2+rot90(a-b));\n\t\tLine bc((b+c)/2,(b+c)/2+rot90(b-c));\n\t\treturn getCrossPoint(ab,bc);\n\t}\n\tint ccw(Point p0,Point p1,Point p2){\n\t\tVector a=p1-p0;\n\t\tVector b=p2-p0;\n\t\tif(cross(a,b)>EPS){return 1;}\n\t\tif(cross(a,b)<-EPS){return -1;}\n\t\tif(dot(a,b)<-EPS){return 2;}\n\t\tif(a.norm()<b.norm()){return -2;}\n\t\treturn 0;\n\t}\n\tdouble getarea(vector<Point>p){ //https://imagingsolution.net/math/calc_n_point_area/\n\t\tint n=p.size();\n\t\tdouble s=0;\n\t\trep(i,n){\n\t\t\ts+=cross(p[i],p[(i+1)%n]);\n\t\t}\n\t\ts=abs(s);\n\t\ts/=2;\n\t\treturn s;\n\t}\n\tvector<Point> ConvexCut(vector<Point>& s,Segment t){\n\t\tvector<Point>ans;\n\t\tint n=s.size();\n\t\trep(j,n){\n\t\t\tPoint p=s[j],q=s[(j+1)%n];\n\t\t\tdouble p_c=cross(t.p2-t.p1,p-t.p1);\n\t\t\tdouble q_c=cross(t.p2-t.p1,q-t.p1);\n\t\t\tSegment now=Segment(p,q);\n\n\t\t\tif(p_c+EPS>0){\n\t\t\t\tans.push_back(p);\n\t\t\t}\n\t\t\tif(ccw(t.p1,t.p2,now.p1)*ccw(t.p1,t.p2,now.p2)==-1){\n\t\t\t\tauto e=intersection(t,now);\n\t\t\t\tans.push_back(e);\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n\n\tbool intersect(Point p1,Point p2,Point p3,Point p4){\n\t\treturn (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n\t}\n\n\tbool intersect(Segment s1,Segment s2){\n\t\treturn intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n\t}\n\n void solve(){\n\t\tPoint xa,xb;\n\t\tcin>>xa.x>>xa.y>>xb.x>>xb.y;\n\t\tSegment nw(xa,xb);\n\t\tint n;\n\t\tcin>>n;\n\t\tvector<tuple<Point,int,int>>cs;\n\t\trep(i,n){\n\t\t\tPoint s,t;\n\t\t\tint o,l;\n\t\t\tcin>>s.x>>s.y>>t.x>>t.y>>o>>l;\n\t\t\tSegment v(s,t);\n\t\t\tif(intersect(nw,v)){\n\t\t\t\tauto cv=intersection(nw,v);\n\t\t\t\tcs.push_back(make_tuple(cv,o,l));\n\t\t\t}\n\t\t}\n\t\tsort(cs.begin(),cs.end());\n\t\tint ans=INF;\n\t\trep(z,2){\n\t\t\tint pre=z,cnt=0;\n\t\t\tfor(auto[v,o,l]:cs){\n\t\t\t\tif(!o){\n\t\t\t\t\tif(l!=pre){\n\t\t\t\t\t\tcnt++;\n\t\t\t\t\t\tpre^=1;\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tif(l==pre){\n\t\t\t\t\t\tcnt++;\n\t\t\t\t\t\tpre^=1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tchmin(ans,cnt);\n\t\t}\n\t\tcout<<ans<<endl;\n }\n\n int main(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\t\t\n\t\tint t;\n\t\tcin>>t;\n\t\trep(z,t){\n\t\t\tsolve();\n\t\t}\n\t\t\n return 0;\n }", "accuracy": 1, "time_ms": 20, "memory_kb": 3712, "score_of_the_acc": -1.25, "final_rank": 6 }, { "submission_id": "aoj_2003_9722118", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nconst double EPS = 1e-9;\n\n\n//определяет расположение точки (xp, yp) относительно отрезка ((x1, y1), (x2, y2))\nint func(double x1, double y1, double x2, double y2, double xp, double yp) {\n double vec_pow = (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1);\n double scal_pow = (x2 - x1) * (xp - x1) + (y2 - y1) * (yp - y1);\n double mod1 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));\n double mod2 = sqrt((xp - x1) * (xp - x1) + (yp - y1) * (yp - y1));\n\n //(xp, yp) справа от вектора {(x1, y1), (x2, y2)}\n if (vec_pow > EPS) {\n return 1;\n }\n\n //(xp, yp) слева от вектора {(x1, y1), (x2, y2)}\n if (vec_pow < -EPS) {\n return -1;\n }\n\n //тогда (xp, yp) и вектор {(x1, y1), (x2, y2)} лежат на одной прямой\n\n //(xp, yp) расположенна сзади точки (x1, y1)\n if (scal_pow < -EPS) {\n return 2;\n }\n\n //(xp, yp) расположена дальше (x2, y2)\n if (mod1 < mod2) {\n return -2;\n }\n\n //(xp, yp) лежит на отрезке ((x1, y1), (x2, y2))\n return 0;\n}\n\n//проверка пересекаются ли отрезки ((x1, y1), (x2, y2)) и ((x3, y3), (x4, y4))\nbool is_crossed(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {\n bool res = func(x1, y1, x2, y2, x3, y3) * func(x1, y1, x2, y2, x4, y4) <= 0 &&\n func(x3, y3, x4, y4, x1, y1) * func(x3, y3, x4, y4, x2, y2) <= 0;\n\n return res;\n}\n\n//находит точку пересечения прямых заданых двумя точками\npair<double, double> find_point(\n double x1, double y1,\n double x2, double y2,\n double x3, double y3,\n double x4, double y4) {\n pair<double, double> point;\n point.first = ((x2 - x1) * (y3 * (x4 - x3) + x3 * (y3 - y4)) - (x4 - x3) * (y1 * (x2 - x1) + x1 * (y1 - y2))) /\n ((y2 - y1) * (x4 - x3) - (y4 - y3) * (x2 - x1));\n if (x1 == x2) {\n point.second = ((point.first - x3) * (y4 - y3) + y3 * (x4 - x3)) / (x4 - x3);\n } else {\n point.second = ((point.first - x1) * (y2 - y1) + y1 * (x2 - x1)) / (x2 - x1);\n }\n\n return point;\n}\n\ndouble dist(double x1, double y1, double x2, double y2) {\n return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));\n}\n\nsigned main() {\n int q;\n cin >> q;\n while (q--) {\n double xa, ya, xb, yb;\n cin >> xa >> ya >> xb >> yb;\n int n;\n cin >> n;\n\n vector<pair<double, bool>> points;\n\n for (int i = 1; i <= n; i++) {\n double xs, ys, xf, yf;\n bool o, l;\n cin >> xs >> ys >> xf >> yf >> o >> l;\n if (is_crossed(xa, ya, xb, yb, xs, ys, xf, yf)) {\n pair<double, double> point = find_point(xa, ya, xb, yb, xs, ys, xf, yf);\n double distance = dist(point.first, point.second, xa, ya);\n // 1 1 -> 1\n // 1 0 -> 0\n // 0 1 -> 0\n // 0 0 -> 1\n // cout << i << \" \" << distance << \" \" << o << \" \" << l << \" \" << (o == l) << endl;\n points.push_back(make_pair(distance, o == l));\n }\n }\n\n if (points.size() == 0) {\n cout << 0 << endl;\n continue;\n }\n\n sort(points.begin(), points.end());\n\n int ans = 0;\n\n bool state = points[0].second;\n for (int i = 1; i < points.size(); i++) {\n if (state != points[i].second) {\n state = points[i].second;\n ans++;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3464, "score_of_the_acc": -1.6869, "final_rank": 15 }, { "submission_id": "aoj_2003_9722113", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nconst double EPS = 0.000001;\n\n\n//определяет расположение точки (xp, yp) относительно отрезка ((x1, y1), (x2, y2))\nint func(double x1, double y1, double x2, double y2, double xp, double yp) {\n double vec_pow = (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1);\n double scal_pow = (x2 - x1) * (xp - x1) + (y2 - y1) * (yp - y1);\n double mod1 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));\n double mod2 = sqrt((xp - x1) * (xp - x1) + (yp - y1) * (yp - y1));\n\n //(xp, yp) справа от вектора {(x1, y1), (x2, y2)}\n if (vec_pow > EPS) {\n return 1;\n }\n\n //(xp, yp) слева от вектора {(x1, y1), (x2, y2)}\n if (vec_pow < -EPS) {\n return -1;\n }\n\n //тогда (xp, yp) и вектор {(x1, y1), (x2, y2)} лежат на одной прямой\n\n //(xp, yp) расположенна сзади точки (x1, y1)\n if (scal_pow < -EPS) {\n return 2;\n }\n\n //(xp, yp) расположена дальше (x2, y2)\n if (mod1 < mod2) {\n return -2;\n }\n\n //(xp, yp) лежит на отрезке ((x1, y1), (x2, y2))\n return 0;\n}\n\n//проверка пересекаются ли отрезки ((x1, y1), (x2, y2)) и ((x3, y3), (x4, y4))\nbool is_crossed(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {\n bool res = func(x1, y1, x2, y2, x3, y3) * func(x1, y1, x2, y2, x4, y4) <= 0 &&\n func(x3, y3, x4, y4, x1, y1) * func(x3, y3, x4, y4, x2, y2) <= 0;\n\n return res;\n}\n\n//находит точку пересечения прямых заданых двумя точками\npair<double, double> find_point(\n double x1, double y1,\n double x2, double y2,\n double x3, double y3,\n double x4, double y4) {\n pair<double, double> point;\n point.first = ((x2 - x1) * (y3 * (x4 - x3) + x3 * (y3 - y4)) - (x4 - x3) * (y1 * (x2 - x1) + x1 * (y1 - y2))) /\n ((y2 - y1) * (x4 - x3) - (y4 - y3) * (x2 - x1));\n if (x1 == x2) {\n point.second = ((point.first - x3) * (y4 - y3) + y3 * (x4 - x3)) / (x4 - x3);\n } else {\n point.second = ((point.first - x1) * (y2 - y1) + y1 * (x2 - x1)) / (x2 - x1);\n }\n\n return point;\n}\n\ndouble dist(double x1, double y1, double x2, double y2) {\n return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));\n}\n\nsigned main() {\n int q;\n cin >> q;\n while (q--) {\n double xa, ya, xb, yb;\n cin >> xa >> ya >> xb >> yb;\n int n;\n cin >> n;\n\n vector<pair<double, bool>> points;\n\n for (int i = 1; i <= n; i++) {\n double xs, ys, xf, yf;\n bool o, l;\n cin >> xs >> ys >> xf >> yf >> o >> l;\n if (is_crossed(xa, ya, xb, yb, xs, ys, xf, yf)) {\n pair<double, double> point = find_point(xa, ya, xb, yb, xs, ys, xf, yf);\n double distance = dist(point.first, point.second, xa, ya);\n // 1 1 -> 1\n // 1 0 -> 0\n // 0 1 -> 0\n // 0 0 -> 1\n // cout << i << \" \" << distance << \" \" << o << \" \" << l << \" \" << (o == l) << endl;\n points.push_back(make_pair(distance, o == l));\n }\n }\n\n if (points.size() == 0) {\n cout << 0 << endl;\n continue;\n }\n\n sort(points.begin(), points.end());\n // cout << points.size() << endl;\n // for (int i = 0; i < points.size(); i++) {\n // cout << points[i].first << ' ' << points[i].second << endl;\n // }\n int ans = 0;\n\n bool state = points[0].second;\n for (int i = 1; i < points.size(); i++) {\n if (state != points[i].second) {\n state = points[i].second;\n ans++;\n }\n }\n cout << ans << endl;\n }\n}\n\n// 1\n// 0 0 2 2\n// 2\n// 0 1 1 0 1 1\n// 0 2 2 0 0 1\n//\n// 1\n// -10 1 10 1\n// 4\n// -6 2 -2 -2 0 1\n// -6 -2 -2 2 1 0\n// 6 2 2 -2 0 0\n// 6 -2 2 2 1 1", "accuracy": 1, "time_ms": 50, "memory_kb": 3416, "score_of_the_acc": -1.6263, "final_rank": 12 }, { "submission_id": "aoj_2003_9722112", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nconst double EPS = 0.000001;\n\nstruct PointData {\n bool is_underground;\n double distance;\n\n PointData(double arg_distance, bool arg_is_underground) {\n distance = arg_distance;\n is_underground = arg_is_underground;\n }\n\n bool operator<(const PointData &other) const {\n return distance > other.distance;\n }\n};\n\n//определяет расположение точки (xp, yp) относительно отрезка ((x1, y1), (x2, y2))\nint func(double x1, double y1, double x2, double y2, double xp, double yp) {\n double vec_pow = (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1);\n double scal_pow = (x2 - x1) * (xp - x1) + (y2 - y1) * (yp - y1);\n double mod1 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));\n double mod2 = sqrt((xp - x1) * (xp - x1) + (yp - y1) * (yp - y1));\n\n //(xp, yp) справа от вектора {(x1, y1), (x2, y2)}\n if (vec_pow > EPS) {\n return 1;\n }\n\n //(xp, yp) слева от вектора {(x1, y1), (x2, y2)}\n if (vec_pow < -EPS) {\n return -1;\n }\n\n //тогда (xp, yp) и вектор {(x1, y1), (x2, y2)} лежат на одной прямой\n\n //(xp, yp) расположенна сзади точки (x1, y1)\n if (scal_pow < -EPS) {\n return 2;\n }\n\n //(xp, yp) расположена дальше (x2, y2)\n if (mod1 < mod2) {\n return -2;\n }\n\n //(xp, yp) лежит на отрезке ((x1, y1), (x2, y2))\n return 0;\n}\n\n//проверка пересекаются ли отрезки ((x1, y1), (x2, y2)) и ((x3, y3), (x4, y4))\nbool is_crossed(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {\n bool res = func(x1, y1, x2, y2, x3, y3) * func(x1, y1, x2, y2, x4, y4) <= 0 &&\n func(x3, y3, x4, y4, x1, y1) * func(x3, y3, x4, y4, x2, y2) <= 0;\n\n return res;\n}\n\n//находит точку пересечения прямых заданых двумя точками\npair<double, double> find_point(\n double x1, double y1,\n double x2, double y2,\n double x3, double y3,\n double x4, double y4) {\n pair<double, double> point;\n point.first = ((x2 - x1) * (y3 * (x4 - x3) + x3 * (y3 - y4)) - (x4 - x3) * (y1 * (x2 - x1) + x1 * (y1 - y2))) /\n ((y2 - y1) * (x4 - x3) - (y4 - y3) * (x2 - x1));\n if (x1 == x2) {\n point.second = ((point.first - x3) * (y4 - y3) + y3 * (x4 - x3)) / (x4 - x3);\n } else {\n point.second = ((point.first - x1) * (y2 - y1) + y1 * (x2 - x1)) / (x2 - x1);\n }\n\n return point;\n}\n\ndouble dist(double x1, double y1, double x2, double y2) {\n return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));\n}\n\nsigned main() {\n int q;\n cin >> q;\n while (q--) {\n double xa, ya, xb, yb;\n cin >> xa >> ya >> xb >> yb;\n int n;\n cin >> n;\n\n priority_queue<PointData> points;\n\n for (int i = 1; i <= n; i++) {\n double xs, ys, xf, yf;\n bool o, l;\n cin >> xs >> ys >> xf >> yf >> o >> l;\n if (is_crossed(xa, ya, xb, yb, xs, ys, xf, yf)) {\n pair<double, double> point = find_point(xa, ya, xb, yb, xs, ys, xf, yf);\n double distance = dist(point.first, point.second, xa, ya);\n // 1 1 -> 1\n // 1 0 -> 0\n // 0 1 -> 0\n // 0 0 -> 1\n // cout << i << \" \" << distance << \" \" << o << \" \" << l << \" \" << (o == l) << endl;\n points.push(PointData(distance, !(o == l)));\n }\n }\n\n if (points.empty()) {\n cout << 0 << endl;\n continue;\n }\n\n int ans = 0;\n\n bool state = points.top().is_underground;\n while (!points.empty()) {\n if (state != points.top().is_underground) {\n state = points.top().is_underground;\n ans++;\n }\n points.pop();\n }\n cout << ans << endl;\n }\n}\n\n// 1\n// 0 0 2 2\n// 2\n// 0 1 1 0 1 1\n// 0 2 2 0 0 1\n//\n// 1\n// -10 1 10 1\n// 4\n// -6 2 -2 -2 0 1\n// -6 -2 -2 2 1 0\n// 6 2 2 -2 0 0\n// 6 -2 2 2 1 1", "accuracy": 1, "time_ms": 50, "memory_kb": 3448, "score_of_the_acc": -1.6667, "final_rank": 13 }, { "submission_id": "aoj_2003_9722100", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\ntypedef long long int ll;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000001\nusing namespace std;\n\nstruct Info{\n\tdouble x,y;\n};\n\nstruct Data{\n\tData(double arg_distance,bool arg_is_under_ground){\n\t\tdistance = arg_distance;\n\t\tis_under_ground = arg_is_under_ground;\n\t}\n\tbool operator<(const struct Data &arg) const{\n\t\treturn distance > arg.distance;\n\t};\n\n\tdouble distance;\n\tbool is_under_ground;\n};\n\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\nInfo calc(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){\n\tInfo ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\ndouble calc_dist(double x1,double y1,double x2,double y2){\n\treturn sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));\n}\n\nvoid func(){\n\n\tdouble start_x,start_y,goal_x,goal_y;\n\tscanf(\"%lf %lf %lf %lf\",&start_x,&start_y,&goal_x,&goal_y);\n\n\tint N;\n\tscanf(\"%d\",&N);\n\n\tpriority_queue<Data> Q;\n\n\tdouble x1,y1,x2,y2;\n\tint Owner,L;\n\n\tInfo ret;\n\tdouble tmp_dist;\n\n\tfor(int loop = 0; loop < N; loop++){\n\t\tscanf(\"%lf %lf %lf %lf %d %d\",&x1,&y1,&x2,&y2,&Owner,&L);\n\n\t\tif(func(x1,y1,x2,y2,start_x,start_y)*func(x1,y1,x2,y2,goal_x,goal_y) <= 0 &&\n\t\t\t\tfunc(start_x,start_y,goal_x,goal_y,x1,y1) * func(start_x,start_y,goal_x,goal_y,x2,y2) <= 0){\n\t\t\tret = calc(start_x,start_y,goal_x,goal_y,x1,y1,x2,y2);\n\t\t\ttmp_dist = calc_dist(start_x,start_y,ret.x,ret.y);\n\t\t\tif(Owner == 1){\n\t\t\t\tif(L == 1){\n\t\t\t\t\tQ.push(Data(tmp_dist,false));\n\t\t\t\t}else{\n\t\t\t\t\tQ.push(Data(tmp_dist,true));\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\tif(L == 1){\n\t\t\t\t\tQ.push(Data(tmp_dist,true));\n\t\t\t\t}else{\n\t\t\t\t\tQ.push(Data(tmp_dist,false));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(Q.empty()){\n\t\tprintf(\"0\\n\");\n\t\treturn;\n\t}\n\n\tbool under_ground = Q.top().is_under_ground;\n\tQ.pop();\n\n\tint ans = 0;\n\n\twhile(!Q.empty()){\n\t\tif(under_ground == Q.top().is_under_ground){\n\t\t\t//Do nothing\n\t\t}else{\n\t\t\tans++;\n\t\t\tunder_ground = Q.top().is_under_ground;\n\t\t}\n\t\tQ.pop();\n\t}\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\tint num;\n\tscanf(\"%d\",&num);\n\n\tfor(int i = 0; i < num; i++)func();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2920, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2003_9640611", "code_snippet": "#include <iostream>\n#include <complex>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\ndouble det(const complex<double> &a, const complex<double> &b) {\n return (conj(b) * a).imag();\n}\n\ndouble ratio(complex<double> s1, complex<double> d1, complex<double> s2, complex<double> d2) {\n return det(s2 - s1, d2) / det(d1, d2);\n}\n\nint main() {\n int c;\n cin >> c;\n while (c--) {\n int xa, ya, xb, yb, m, o, l;\n cin >> xa >> ya >> xb >> yb >> m;\n complex<double> a(xa, ya), b(xb, yb);\n vector<pair<double, int>> cross;\n for (int i = 0; i < m; i++) {\n cin >> xa >> ya >> xb >> yb >> o >> l;\n complex<double> c(xa, ya), d(xb, yb);\n double s = ratio(a, b - a, c, d - c), t = ratio(c, d - c, a, b - a);\n if (0 < s && s < 1 && 0 < t && t < 1) cross.push_back(make_pair(s, o ^ l));\n }\n sort(cross.begin(), cross.end());\n int ans = 0;\n int n = cross.size();\n if (n > 1) {\n for (int i = 0; i < n - 1; i++)\n if (cross[i].second != cross[i + 1].second) ans++;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3324, "score_of_the_acc": -1.2601, "final_rank": 7 }, { "submission_id": "aoj_2003_9520390", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nusing T = double;\nconst T eps = 1e-12;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, .0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, .0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * 2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), .0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), .0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return .0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, 0.) and eq(d2, 0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / 2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return .0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = .0;\n if (eq(f, .0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = .0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return .0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / 2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / 2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / 2.);\n res += b.r * b.r * (bth - sin(bth * 2) / 2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), .0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / 2., (a + b) / 2. + (b - a) * Point(0, 1));\n Line l2((b + c) / 2., (b + c) / 2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, 0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, 1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nint _;\ncin >> _;\nwhile(_--) {\n double xa, ya, xb, yb;\n cin >> xa >> ya >> xb >> yb;\n Point pa(xa,ya), pb(xb,yb);\n Segment S(pa,pb);\n vector<pair<double,int>> V;\n int N;\n cin >> N;\n rep(i,0,N) {\n double x1, y1, x2, y2;\n int O, L;\n cin >> x1 >> y1 >> x2 >> y2 >> O >> L;\n int A = (O+L)%2;\n Point p1(x1,y1), p2(x2,y2);\n Segment T(p1,p2);\n if (isIntersect(S,T)) {\n Point P = Intersection(S,T);\n V.push_back(pair<double,int>{abs(pa-P), A});\n }\n }\n sort(ALL(V));\n int ANS = 0;\n for (int i = 0; i + 1 < (int)(V.size()); i++) {\n if (V[i].second != V[i+1].second) ANS++;\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3484, "score_of_the_acc": -1.7121, "final_rank": 17 }, { "submission_id": "aoj_2003_9418833", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = 1e9 + 10;\nconst ll INFL = 4e18;\n\nconst long double EPS = 1e-9;\n\nbool almostEqual(long double a, long double b) { return std::abs(a - b) < EPS; }\n\nbool lessThan(long double a, long double b) {\n return a < b && !almostEqual(a, b);\n}\n\nbool greaterThan(long double a, long double b) {\n return a > b && !almostEqual(a, b);\n}\n\nbool lessThanOrEqual(long double a, long double b) {\n return a < b || almostEqual(a, b);\n}\n\nbool greaterThanOrEqual(long double a, long double b) {\n return a > b || almostEqual(a, b);\n}\n\nstruct Point {\n long double x, y;\n\n Point() = default;\n Point(long double x, long double y) : x(x), y(y) {}\n\n Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }\n\n Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }\n\n Point operator*(long double k) const { return Point(x * k, y * k); }\n\n Point operator/(long double k) const { return Point(x / k, y / k); }\n\n long double dot(const Point &p) const { return x * p.x + y * p.y; }\n\n long double cross(const Point &p) const { return x * p.y - y * p.x; }\n\n long double cross(const Point &p1, const Point &p2) const {\n return (p1.x - x) * (p2.y - y) - (p1.y - y) * (p2.x - x);\n }\n\n long double norm() const { return x * x + y * y; }\n\n long double abs() const { return std::sqrt(norm()); }\n\n long double arg() const { return atan2(y, x); }\n\n bool operator==(const Point &p) const {\n return almostEqual(x, p.x) && almostEqual(y, p.y);\n }\n\n friend istream &operator>>(istream &is, Point &p) {\n return is >> p.x >> p.y;\n }\n};\n\nstruct Line {\n Point a, b;\n\n Line() = default;\n Line(const Point &_a, const Point &_b) : a(_a), b(_b) {}\n Line(const long double &A, const long double &B, const long double &C) {\n if (almostEqual(A, 0)) {\n assert(!almostEqual(B, 0));\n a = Point(0, C / B);\n b = Point(1, C / B);\n } else if (almostEqual(B, 0)) {\n a = Point(C / A, 0);\n b = Point(C / A, 1);\n } else if (almostEqual(C, 0)) {\n a = Point(0, C / B);\n b = Point(1, (C - A) / B);\n } else {\n a = Point(0, C / B);\n b = Point(C / A, 0);\n }\n }\n\n bool operator==(const Line &l) const { return a == l.a && b == l.b; }\n\n friend istream &operator>>(istream &is, Line &l) {\n return is >> l.a >> l.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n using Line::Line;\n};\n\nstruct Circle {\n Point center;\n long double r;\n Circle() = default;\n Circle(long double x, long double y, long double r) : center(x, y), r(r) {}\n Circle(Point _center, long double r) : center(_center), r(r) {}\n\n bool operator==(const Circle &C) const {\n return center == C.center && r == C.r;\n }\n\n friend istream &operator>>(istream &is, Circle &C) {\n return is >> C.center >> C.r;\n }\n};\n\nenum Orientation {\n COUNTER_CLOCKWISE,\n CLOCKWISE,\n ONLINE_BACK,\n ONLINE_FRONT,\n ON_SEGMENT\n};\n\nOrientation ccw(const Point &p0, const Point &p1, const Point &p2) {\n Point a = p1 - p0;\n Point b = p2 - p0;\n long double cross_product = a.cross(b);\n\n if (greaterThan(cross_product, 0))\n return COUNTER_CLOCKWISE;\n if (lessThan(cross_product, 0))\n return CLOCKWISE;\n if (lessThan(a.dot(b), 0))\n return ONLINE_BACK;\n if (lessThan(a.norm(), b.norm()))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nstd::string orientationToString(Orientation o) {\n switch (o) {\n case COUNTER_CLOCKWISE:\n return \"COUNTER_CLOCKWISE\";\n case CLOCKWISE:\n return \"CLOCKWISE\";\n case ONLINE_BACK:\n return \"ONLINE_BACK\";\n case ONLINE_FRONT:\n return \"ONLINE_FRONT\";\n case ON_SEGMENT:\n return \"ON_SEGMENT\";\n default:\n return \"UNKNOWN\";\n }\n}\n\nPoint projection(const Point &p1, const Point &p2, const Point &p) {\n Point base = p2 - p1;\n long double r = (p - p1).dot(base) / base.norm();\n return p1 + base * r;\n}\n\nPoint projection(const Line &l, const Point &p) {\n Point base = l.b - l.a;\n long double r = (p - l.a).dot(base) / base.norm();\n return l.a + base * r;\n}\n\nPoint reflection(const Point &p1, const Point &p2, const Point &p) {\n Point proj = projection(p1, p2, p);\n return proj * 2 - p;\n}\n\nPoint reflection(const Line &l, const Point &p) {\n Point proj = projection(l, p);\n return proj * 2 - p;\n}\n\nbool isParallel(const Line &l1, const Line &l2) {\n return almostEqual((l1.b - l1.a).cross(l2.b - l2.a), 0);\n}\n\nbool isOrthogonal(const Line &l1, const Line &l2) {\n return almostEqual((l1.b - l1.a).dot(l2.b - l2.a), 0);\n}\n\nbool isParallel(const Segment &l1, const Segment &l2) {\n return almostEqual((l1.b - l1.a).cross(l2.b - l2.a), 0);\n}\n\nbool isOrthogonal(const Segment &l1, const Segment &l2) {\n return almostEqual((l1.b - l1.a).dot(l2.b - l2.a), 0);\n}\n\nbool isParallel(const Line &l1, const Segment &l2) {\n return almostEqual((l1.b - l1.a).cross(l2.b - l2.a), 0);\n}\n\nbool isOrthogonal(const Line &l1, const Segment &l2) {\n return almostEqual((l1.b - l1.a).dot(l2.b - l2.a), 0);\n}\n\nbool isParallel(const Segment &l1, const Line &l2) {\n return almostEqual((l1.b - l1.a).cross(l2.b - l2.a), 0);\n}\n\nbool isOrthogonal(const Segment &l1, const Line &l2) {\n return almostEqual((l1.b - l1.a).dot(l2.b - l2.a), 0);\n}\n\nbool isPointOnLine(const Point &p, const Line &l) {\n return almostEqual((l.b - l.a).cross(p - l.a), 0.0);\n}\n\nbool isPointOnSegment(const Point &p, const Segment &s) {\n return lessThanOrEqual(std::min(s.a.x, s.b.x), p.x) &&\n lessThanOrEqual(p.x, std::max(s.a.x, s.b.x)) &&\n lessThanOrEqual(std::min(s.a.y, s.b.y), p.y) &&\n lessThanOrEqual(p.y, std::max(s.a.y, s.b.y)) &&\n almostEqual((s.b - s.a).cross(p - s.a), 0.0);\n}\n\nbool isIntersecting(const Segment &s1, const Segment &s2) {\n Point p0p1 = s1.b - s1.a;\n Point p0p2 = s2.a - s1.a;\n Point p0p3 = s2.b - s1.a;\n Point p2p3 = s2.b - s2.a;\n Point p2p0 = s1.a - s2.a;\n Point p2p1 = s1.b - s2.a;\n\n long double d1 = p0p1.cross(p0p2);\n long double d2 = p0p1.cross(p0p3);\n long double d3 = p2p3.cross(p2p0);\n long double d4 = p2p3.cross(p2p1);\n\n if (lessThan(d1 * d2, 0) && lessThan(d3 * d4, 0)) {\n return true;\n }\n\n if (almostEqual(d1, 0.0) && isPointOnSegment(s2.a, s1))\n return true;\n if (almostEqual(d2, 0.0) && isPointOnSegment(s2.b, s1))\n return true;\n if (almostEqual(d3, 0.0) && isPointOnSegment(s1.a, s2))\n return true;\n if (almostEqual(d4, 0.0) && isPointOnSegment(s1.b, s2))\n return true;\n\n return false;\n}\n\nPoint getIntersection(const Segment &s1, const Segment &s2) {\n assert(isIntersecting(s1, s2));\n auto cross = [](Point p, Point q) { return p.x * q.y - p.y * q.x; };\n\n Point base = s2.b - s2.a;\n long double d1 = std::abs(cross(base, s1.a - s2.a));\n long double d2 = std::abs(cross(base, s1.b - s2.a));\n long double t = d1 / (d1 + d2);\n\n return s1.a + (s1.b - s1.a) * t;\n}\n\nlong double distancePointToSegment(const Point &p, const Segment &s) {\n Point proj = projection(s.a, s.b, p);\n if (isPointOnSegment(proj, s)) {\n return (p - proj).abs();\n } else {\n return std::min((p - s.a).abs(), (p - s.b).abs());\n }\n}\n\nlong double distanceSegmentToSegment(const Segment &s1, const Segment &s2) {\n if (isIntersecting(s1, s2)) {\n return 0.0;\n }\n return std::min(\n {distancePointToSegment(s1.a, s2), distancePointToSegment(s1.b, s2),\n distancePointToSegment(s2.a, s1), distancePointToSegment(s2.b, s1)});\n}\n\nlong double getPolygonArea(const vector<Point> &points) {\n int n = points.size();\n long double area = 0.0;\n for (int i = 0; i < n; ++i) {\n int j = (i + 1) % n;\n area += points[i].x * points[j].y;\n area -= points[i].y * points[j].x;\n }\n return std::abs(area) / 2.0;\n}\n\nbool isConvex(const vector<Point> &points) {\n int n = points.size();\n bool has_positive = false, has_negative = false;\n for (int i = 0; i < n; ++i) {\n int j = (i + 1) % n;\n int k = (i + 2) % n;\n Point a = points[j] - points[i];\n Point b = points[k] - points[j];\n long double cross_product = a.cross(b);\n if (greaterThan(cross_product, 0))\n has_positive = true;\n if (lessThan(cross_product, 0))\n has_negative = true;\n }\n return !(has_positive && has_negative);\n}\n\nbool isPointOnPolygon(const vector<Point> &polygon, const Point &p) {\n int n = polygon.size();\n for (int i = 0; i < n; ++i) {\n Point a = polygon[i];\n Point b = polygon[(i + 1) % n];\n Segment s(a, b);\n if (isPointOnSegment(p, s)) {\n return true;\n }\n }\n return false;\n}\n\nbool isPointInsidePolygon(const vector<Point> &polygon, const Point &p) {\n int n = polygon.size();\n bool inPolygon = false;\n for (int i = 0; i < n; ++i) {\n Point a = polygon[i];\n Point b = polygon[(i + 1) % n];\n if (greaterThan(a.y, b.y))\n swap(a, b);\n if (lessThanOrEqual(a.y, p.y) && lessThan(p.y, b.y) &&\n greaterThan((b - a).cross(p - a), 0)) {\n inPolygon = !inPolygon;\n }\n }\n return inPolygon;\n}\n\nvector<Point> convexHull(vector<Point> &points,\n bool include_collinear = false) {\n int n = points.size();\n if (n <= 1)\n return points;\n\n sort(points.begin(), points.end(),\n [](const Point &l, const Point &r) -> bool {\n if (almostEqual(l.y, r.y)) {\n return lessThan(l.x, r.x);\n }\n return lessThan(l.y, r.y);\n });\n if (n == 2)\n return points;\n\n vector<Point> upper = {points[0], points[1]},\n lower = {points[0], points[1]};\n for (int i = 2; i < n; ++i) {\n while ((int)upper.size() >= 2 &&\n ccw(upper.end()[-2], upper.end()[-1], points[i]) != CLOCKWISE) {\n if (ccw(upper.end()[-2], upper.end()[-1], points[i]) ==\n ONLINE_FRONT &&\n include_collinear)\n break;\n upper.pop_back();\n }\n upper.push_back(points[i]);\n while ((int)lower.size() >= 2 && ccw(lower.end()[-2], lower.end()[-1],\n points[i]) != COUNTER_CLOCKWISE) {\n if (ccw(lower.end()[-2], lower.end()[-1], points[i]) ==\n ONLINE_FRONT &&\n include_collinear)\n break;\n lower.pop_back();\n }\n lower.push_back(points[i]);\n }\n reverse(upper.begin(), upper.end());\n upper.pop_back();\n lower.pop_back();\n lower.insert(lower.end(), upper.begin(), upper.end());\n return lower;\n}\n\nlong double convexHullDiameter(const vector<Point> &hull) {\n int n = hull.size();\n if (n == 1)\n return 0;\n\n int k = 1;\n long double max_dist = 0;\n\n for (int i = 0; i < n; ++i) {\n while (true) {\n int j = (k + 1) % n;\n Point dist1 = hull[i] - hull[j], dist2 = hull[i] - hull[k];\n max_dist = max(max_dist, dist1.abs());\n max_dist = max(max_dist, dist2.abs());\n if (dist1.abs() > dist2.abs()) {\n k = j;\n } else {\n break;\n }\n }\n Point dist = hull[i] - hull[k];\n max_dist = max(max_dist, dist.abs());\n }\n\n return max_dist;\n}\n\nvector<Point> cutPolygon(const vector<Point> &g, const Line &l) {\n auto isLeft = [](const Point &p1, const Point &p2, const Point &p) -> bool {\n return (p2 - p1).cross(p - p1) > 0;\n };\n vector<Point> newPolygon;\n int n = g.size();\n for (int i = 0; i < n; ++i) {\n const Point &cur = g[i];\n const Point &next = g[(i + 1) % n];\n if (isLeft(l.a, l.b, cur)) {\n newPolygon.push_back(cur);\n }\n if ((isLeft(l.a, l.b, cur) && !isLeft(l.a, l.b, next)) ||\n (!isLeft(l.a, l.b, cur) && isLeft(l.a, l.b, next))) {\n long double A1 = (next - cur).cross(l.a - cur);\n long double A2 = (next - cur).cross(l.b - cur);\n Point intersection = l.a + (l.b - l.a) * (A1 / (A1 - A2));\n newPolygon.push_back(intersection);\n }\n }\n return newPolygon;\n}\n\nlong double closestPair(vector<Point> &points, int l, int r) {\n if (r - l <= 1)\n return numeric_limits<long double>::max();\n int mid = (l + r) >> 1;\n long double x = points[mid].x;\n long double d =\n min(closestPair(points, l, mid), closestPair(points, mid, r));\n auto iti = points.begin(), itl = iti + l, itm = iti + mid, itr = iti + r;\n inplace_merge(itl, itm, itr,\n [](const Point &lhs, const Point &rhs) -> bool {\n return lessThan(lhs.y, rhs.y);\n });\n\n vector<Point> nearLine;\n for (int i = l; i < r; ++i) {\n if (greaterThanOrEqual(fabs(points[i].x - x), d))\n continue;\n int sz = nearLine.size();\n for (int j = sz - 1; j >= 0; --j) {\n Point dv = points[i] - nearLine[j];\n if (dv.y >= d)\n break;\n d = min(d, dv.abs());\n }\n nearLine.push_back(points[i]);\n }\n return d;\n}\n\nint countIntersections(vector<Segment> segments) {\n struct Event {\n long double x;\n int type; // 0: horizontal start, 1: vertical, 2: horizontal end\n long double y1, y2;\n\n Event(long double x, int type, long double y1, long double y2)\n : x(x), type(type), y1(y1), y2(y2) {}\n\n bool operator<(const Event &other) const {\n if (x == other.x)\n return type < other.type;\n return x < other.x;\n }\n };\n vector<Event> events;\n\n sort(segments.begin(), segments.end(),\n [](const Segment &lhs, const Segment &rhs) -> bool {\n return lessThan(min(lhs.a.x, lhs.b.x), min(rhs.a.x, rhs.b.x));\n });\n\n for (const auto &seg : segments) {\n if (seg.a.y == seg.b.y) {\n long double y = seg.a.y;\n long double x1 = min(seg.a.x, seg.b.x);\n long double x2 = max(seg.a.x, seg.b.x);\n events.emplace_back(x1, 0, y, y);\n events.emplace_back(x2, 2, y, y);\n } else {\n long double x = seg.a.x;\n long double y1 = min(seg.a.y, seg.b.y);\n long double y2 = max(seg.a.y, seg.b.y);\n events.emplace_back(x, 1, y1, y2);\n }\n }\n\n sort(events.begin(), events.end());\n\n set<long double> activeSegments;\n int intersectionCount = 0;\n\n for (const auto &event : events) {\n if (event.type == 0) {\n activeSegments.insert(event.y1);\n } else if (event.type == 2) {\n activeSegments.erase(event.y1);\n } else if (event.type == 1) {\n auto lower = activeSegments.lower_bound(event.y1);\n auto upper = activeSegments.upper_bound(event.y2);\n intersectionCount += distance(lower, upper);\n }\n }\n\n return intersectionCount;\n}\n\nint countCirclesIntersection(const Circle &c1, const Circle &c2) {\n long double d =\n sqrt((c1.center.x - c2.center.x) * (c1.center.x - c2.center.x) +\n (c1.center.y - c2.center.y) * (c1.center.y - c2.center.y));\n long double r1 = c1.r, r2 = c2.r;\n\n if (greaterThan(d, r1 + r2)) {\n return 4;\n } else if (almostEqual(d, r1 + r2)) {\n return 3;\n } else if (greaterThan(d, fabs(r1 - r2))) {\n return 2;\n } else if (almostEqual(d, fabs(r1 - r2))) {\n return 1;\n } else {\n return 0;\n }\n}\n\nCircle getInCircle(const Point &A, const Point &B,\n const Point &C) {\n long double a = (B - C).abs();\n long double b = (A - C).abs();\n long double c = (A - B).abs();\n\n long double s = (a + b + c) / 2;\n long double area = sqrt(s * (s - a) * (s - b) * (s - c));\n long double r = area / s;\n\n long double cx = (a * A.x + b * B.x + c * C.x) / (a + b + c);\n long double cy = (a * A.y + b * B.y + c * C.y) / (a + b + c);\n\n return Circle{Point(cx, cy), r};\n}\n\nCircle getCircumCircle(const Point &A, const Point &B,\n const Point &C) {\n long double D =\n 2 * (A.x * (B.y - C.y) + B.x * (C.y - A.y) + C.x * (A.y - B.y));\n long double Ux = ((A.x * A.x + A.y * A.y) * (B.y - C.y) +\n (B.x * B.x + B.y * B.y) * (C.y - A.y) +\n (C.x * C.x + C.y * C.y) * (A.y - B.y)) /\n D;\n long double Uy = ((A.x * A.x + A.y * A.y) * (C.x - B.x) +\n (B.x * B.x + B.y * B.y) * (A.x - C.x) +\n (C.x * C.x + C.y * C.y) * (B.x - A.x)) /\n D;\n\n Point center(Ux, Uy);\n long double radius = (center - A).abs();\n\n return Circle{center, radius};\n}\n\nvector<Point> getCircleLineIntersection(const Circle &c, Point p1, Point p2) {\n long double cx = c.center.x, cy = c.center.y, r = c.r;\n long double dx = p2.x - p1.x;\n long double dy = p2.y - p1.y;\n long double a = dx * dx + dy * dy;\n long double b = 2 * (dx * (p1.x - cx) + dy * (p1.y - cy));\n long double c_const =\n (p1.x - cx) * (p1.x - cx) + (p1.y - cy) * (p1.y - cy) - r * r;\n\n long double discriminant = b * b - 4 * a * c_const;\n vector<Point> intersections;\n\n if (almostEqual(discriminant, 0)) {\n long double t = -b / (2 * a);\n long double ix = p1.x + t * dx;\n long double iy = p1.y + t * dy;\n intersections.emplace_back(ix, iy);\n intersections.emplace_back(ix, iy);\n } else if (discriminant > 0) {\n long double sqrt_discriminant = sqrt(discriminant);\n long double t1 = (-b + sqrt_discriminant) / (2 * a);\n long double t2 = (-b - sqrt_discriminant) / (2 * a);\n\n long double ix1 = p1.x + t1 * dx;\n long double iy1 = p1.y + t1 * dy;\n long double ix2 = p1.x + t2 * dx;\n long double iy2 = p1.y + t2 * dy;\n\n intersections.emplace_back(ix1, iy1);\n intersections.emplace_back(ix2, iy2);\n }\n\n if (almostEqual(intersections[0].x, intersections[1].x)) {\n if (greaterThan(intersections[0].y, intersections[1].y)) {\n swap(intersections[0], intersections[1]);\n }\n } else if (greaterThan(intersections[0].x, intersections[1].x)) {\n swap(intersections[0], intersections[1]);\n }\n return intersections;\n}\n\nvector<Point> getCirclesIntersect(const Circle &c1, const Circle &c2) {\n long double x1 = c1.center.x, y1 = c1.center.y, r1 = c1.r;\n long double x2 = c2.center.x, y2 = c2.center.y, r2 = c2.r;\n\n long double d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));\n\n if (d > r1 + r2 || d < abs(r1 - r2)) {\n return {}; // No intersection\n }\n\n long double a = (r1 * r1 - r2 * r2 + d * d) / (2 * d);\n long double h = sqrt(r1 * r1 - a * a);\n\n long double x0 = x1 + a * (x2 - x1) / d;\n long double y0 = y1 + a * (y2 - y1) / d;\n\n long double rx = -(y2 - y1) * (h / d);\n long double ry = (x2 - x1) * (h / d);\n\n Point p1(x0 + rx, y0 + ry);\n Point p2(x0 - rx, y0 - ry);\n\n vector<Point> intersections;\n intersections.push_back(p1);\n intersections.push_back(p2);\n\n if (almostEqual(intersections[0].x, intersections[1].x)) {\n if (greaterThan(intersections[0].y, intersections[1].y)) {\n swap(intersections[0], intersections[1]);\n }\n } else if (greaterThan(intersections[0].x, intersections[1].x)) {\n swap(intersections[0], intersections[1]);\n }\n return intersections;\n}\n\nvector<Point> getTangentLinesFromPoint(const Circle &c, const Point &p) {\n long double cx = c.center.x, cy = c.center.y, r = c.r;\n long double px = p.x, py = p.y;\n\n long double dx = px - cx;\n long double dy = py - cy;\n long double d = (p - c.center).abs();\n\n if (lessThan(d, r)) {\n return {}; // No tangents if the point is inside the circle\n } else if (almostEqual(d, r)) {\n return {p};\n }\n\n long double a = r * r / d;\n long double h = sqrt(r * r - a * a);\n\n long double cx1 = cx + a * dx / d;\n long double cy1 = cy + a * dy / d;\n\n vector<Point> tangents;\n\n tangents.emplace_back(cx1 + h * dy / d, cy1 - h * dx / d);\n tangents.emplace_back(cx1 - h * dy / d, cy1 + h * dx / d);\n\n if (almostEqual(tangents[0].x, tangents[1].x)) {\n if (greaterThan(tangents[0].y, tangents[1].y)) {\n swap(tangents[0], tangents[1]);\n }\n } else if (greaterThan(tangents[0].x, tangents[1].x)) {\n swap(tangents[0], tangents[1]);\n }\n\n return tangents;\n}\n\nvector<Segment> getCommonTangentsLine(const Circle &c1,\n const Circle &c2) {\n long double x1 = c1.center.x, y1 = c1.center.y, r1 = c1.r;\n long double x2 = c2.center.x, y2 = c2.center.y, r2 = c2.r;\n\n long double dx = x2 - x1;\n long double dy = y2 - y1;\n long double d = sqrt(dx * dx + dy * dy);\n\n vector<Segment> tangents;\n\n if (almostEqual(d, 0) && almostEqual(r1, r2)) {\n return tangents;\n }\n\n if (greaterThanOrEqual(d, r1 + r2)) {\n long double a = atan2(dy, dx);\n for (int sign : {-1, 1}) {\n long double theta = acos((r1 + r2) / d);\n long double cx1 = x1 + r1 * cos(a + sign * theta);\n long double cy1 = y1 + r1 * sin(a + sign * theta);\n long double cx2 = x2 + r2 * cos(a + sign * theta);\n long double cy2 = y2 + r2 * sin(a + sign * theta);\n tangents.emplace_back(Segment{Point(cx1, cy1), Point(cx2, cy2)});\n if (almostEqual(d, r1 + r2))\n break;\n }\n }\n\n if (greaterThanOrEqual(d, fabs(r1 - r2))) {\n long double a = atan2(dy, dx);\n for (int sign : {-1, 1}) {\n long double theta = acos((r1 - r2) / d);\n long double cx1 = x1 + r1 * cos(a + sign * theta);\n long double cy1 = y1 + r1 * sin(a + sign * theta);\n long double cx2 = x2 - r2 * cos(a + sign * theta);\n long double cy2 = y2 - r2 * sin(a + sign * theta);\n tangents.emplace_back(Segment{Point(cx1, cy1), Point(cx2, cy2)});\n if (almostEqual(d, fabs(r1 - r2)))\n break;\n }\n }\n\n sort(tangents.begin(), tangents.end(),\n [&](const Segment &s1, const Segment &s2) {\n if (almostEqual(s1.a.x, s2.a.x)) {\n return lessThan(s1.a.y, s2.a.y);\n } else {\n return lessThan(s1.a.x, s2.a.x);\n }\n });\n return tangents;\n}\n\nusing ld = long double;\n\nint main() {\n int T;\n cin >> T;\n\n while (T--) {\n Point A, B;\n cin >> A >> B;\n int N;\n cin >> N;\n Segment AB(A, B);\n vector<tuple<ld, ld, int, int>> T;\n for (int i = 0; i < N; i++) {\n Point C, D;\n cin >> C >> D;\n int o, l;\n cin >> o >> l;\n Segment CD(C, D);\n if (isIntersecting(AB, CD)) {\n Point P = getIntersection(AB, CD);\n auto [c, d] = P;\n T.push_back({c, d, o, l});\n }\n }\n sort(T.begin(), T.end());\n\n int ans = INF;\n for (int i = 0; i < 2; i++) {\n int now = i;\n int res = 0;\n for (auto [x, y, o, l] : T) {\n if (o) {\n if (now != l) {\n res++;\n now = l;\n }\n } else {\n if (now == l) {\n res++;\n now = 1 - l;\n }\n }\n }\n ans = min(ans, res);\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3448, "score_of_the_acc": -1.6667, "final_rank": 13 }, { "submission_id": "aoj_2003_9395602", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#include <bits/stdc++.h>\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\ntypedef double D;\ntypedef complex<D> P; // Point\ntypedef pair<P, P> L; // Line\ntypedef vector<P> VP;\nconst D EPS = 1e-9; // 許容誤差。問題によって変える\n#define X real()\n#define Y imag()\n#define LE(n,m) ((n) < (m) + EPS)\n#define GE(n,m) ((n) + EPS > (m))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\nD dot(P a, P b) {\n return (conj(a)*b).X;\n}\n// 外積 cross(a,b) = |a||b|sinθ\nD cross(P a, P b) {\n return (conj(a)*b).Y;\n}\n\n// 点の進行方向\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n/* 交差判定 直線・線分は縮退してはならない。接する場合は交差するとみなす。isecはintersectの略 */\n\n// 直線と点\nbool isecLP(P a1, P a2, P b) {\n return abs(ccw(a1, a2, b)) != 1; // return EQ(cross(a2-a1, b-a1), 0); と等価\n}\n\n// 直線と直線\nbool isecLL(P a1, P a2, P b1, P b2) {\n return !isecLP(a2-a1, b2-b1, 0) || isecLP(a1, b1, b2);\n}\n\n// 直線と線分\nbool isecLS(P a1, P a2, P b1, P b2) {\n return cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS;\n}\n\n// 線分と線分\nbool isecSS(P a1, P a2, P b1, P b2) {\n return ccw(a1, a2, b1)*ccw(a1, a2, b2) <= 0 &&\n ccw(b1, b2, a1)*ccw(b1, b2, a2) <= 0;\n}\n\n// 線分と点\nbool isecSP(P a1, P a2, P b) {\n return !ccw(a1, a2, b);\n}\n\n\n/* 距離 各直線・線分は縮退してはならない */\n\n// 点pの直線aへの射影点を返す\nP proj(P a1, P a2, P p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\n// 点pの直線aへの反射点を返す\nP reflection(P a1, P a2, P p) {\n return 2.0*proj(a1, a2, p) - p;\n}\n\nD distLP(P a1, P a2, P p) {\n return abs(proj(a1, a2, p) - p);\n}\n\nD distLL(P a1, P a2, P b1, P b2) {\n return isecLL(a1, a2, b1, b2) ? 0 : distLP(a1, a2, b1);\n}\n\nD distLS(P a1, P a2, P b1, P b2) {\n return isecLS(a1, a2, b1, b2) ? 0 : min(distLP(a1, a2, b1), distLP(a1, a2, b2));\n}\n\nD distSP(P a1, P a2, P p) {\n P r = proj(a1, a2, p);\n if (isecSP(a1, a2, r)) return abs(r-p);\n return min(abs(a1-p), abs(a2-p));\n}\n\nD distSS(P a1, P a2, P b1, P b2) {\n if (isecSS(a1, a2, b1, b2)) return 0;\n return min(min(distSP(a1, a2, b1), distSP(a1, a2, b2)),\n min(distSP(b1, b2, a1), distSP(b1, b2, a2)));\n}\n\n// 2直線の交点\nP crosspointLL(P a1, P a2, P b1, P b2) {\n D d1 = cross(b2-b1, b1-a1);\n D d2 = cross(b2-b1, a2-a1);\n if (EQ(d1, 0) && EQ(d2, 0)) return a1; // same line\n if (EQ(d2, 0)) throw \"kouten ga nai\"; // 交点がない\n return a1 + d1/d2 * (a2-a1);\n}\n\nint solve(){\n P a, b;\n D ax, ay, bx, by; cin >> ax >> ay >> bx >> by;\n a = {ax, ay};\n b = {bx, by};\n int N; cin >> N;\n vc<tuple<P, P, int>> lines;\n vc<pair<P, int>> cross;\n rep(i, N){\n P s, t;\n D sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty;\n s = {sx, sy};\n t = {tx, ty};\n int o, l; cin >> o >> l;\n if (o) l ^= 1;\n lines.push_back({s, t, l});\n if (isecSS(a, b, s, t)){\n cross.push_back({crosspointLL(a, b, s, t), l});\n }\n }\n sort(all(cross), [&](auto p1, auto p2){return (EQ(p1.first.real(), p2.first.real()) ? p1.first.imag() < p2.first.imag() : p1.first.real() < p2.first.real());});\n int ret = 0;\n rep(i, len(cross) - 1) if (cross[i].second != cross[i + 1].second) ret++;\n return ret;\n}\nint main(){\n vi ans;\n int T; cin >> T;\n rep(i, T) ans.push_back(solve());\n \n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3528, "score_of_the_acc": -1.7677, "final_rank": 18 }, { "submission_id": "aoj_2003_9359411", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\nusing pii =pair<int,int>;\n\nconstexpr ll mod = 1000000007;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint dX[4]={1,0,-1,0};\nint dY[4]={0,-1,0,1};\n\n\n\n//library\n\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nconst double EPS = 1e-10;\nconst double PI = asinl(1) * 2;\n\nstruct Point{\n double x,y;\n Point(){}\n Point(double x,double y):x(x),y(y){}\n Point operator+(Point p) {return Point(x+p.x,y+p.y);}\n Point operator-(Point p) {return Point(x-p.x,y-p.y);}\n Point operator*(double k){return Point(x*k,y*k);}\n Point operator/(double k){return Point(x/k,y/k);}\n double norm(){return x*x+y*y;}\n double abs(){return sqrt(norm());}\n\n bool operator<(const Point &p) const{\n return x!=p.x?x<p.x:y<p.y;\n //grid-point only\n //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0;\n }\n\n bool operator==(const Point &p) const{\n return fabs(x-p.x)<EPS and fabs(y-p.y)<EPS;\n }\n};\n\nbool sort_x(Point a,Point b){\n return a.x!=b.x?a.x<b.x:a.y<b.y;\n}\n\nbool sort_y(Point a,Point b){\n return a.y!=b.y?a.y<b.y:a.x<b.x;\n}\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Segment{\n Point p1,p2;\n Segment(){}\n Segment(Point p1, Point p2):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nstruct Circle{\n Point c;\n double r;\n Circle(){}\n Circle(Point c,double r):c(c),r(r){}\n};\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nPoint orth(Point p){return Point(-p.y,p.x);}\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\ndouble arg(Vector p){\n return atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n return Point(cos(r)*a,sin(r)*a);\n}\n\n// COUNTER CLOCKWISE\nstatic const int CCW_COUNTER_CLOCKWISE = 1;\nstatic const int CCW_CLOCKWISE = -1;\nstatic const int CCW_ONLINE_BACK = 2;\nstatic const int CCW_ONLINE_FRONT = -2;\nstatic const int CCW_ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a = p1-p0;\n Vector b = p2-p0;\n if(cross(a,b) > EPS) return CCW_COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS) return CCW_CLOCKWISE;\n if(dot(a,b) < -EPS) return CCW_ONLINE_BACK;\n if(a.norm()<b.norm()) return CCW_ONLINE_FRONT;\n return CCW_ON_SEGMENT;\n}\n\nbool intersectSS(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 and\n ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0 );\n}\n\nbool intersectSS(Segment s1,Segment s2){\n return intersectSS(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool intersectPS(Polygon p,Segment l){\n int n=p.size();\n for(int i=0;i<n;++i)\n if(intersectSS(Segment(p[i],p[(i+1)%n]),l)) return 1;\n return 0;\n}\n\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistanceSS(Segment s1,Segment s2){\n if(intersectSS(s1,s2)) return 0.0;\n return min(\n min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),\n min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));\n}\n\n// intercsect of circles\nstatic const int ICC_SEPERATE = 4;\nstatic const int ICC_CIRCUMSCRIBE = 3;\nstatic const int ICC_INTERSECT = 2;\nstatic const int ICC_INSCRIBE = 1;\nstatic const int ICC_CONTAIN = 0;\n\nint intersectCC(Circle c1,Circle c2){\n if(c1.r<c2.r) swap(c1,c2);\n double d=abs(c1.c-c2.c);\n double r=c1.r+c2.r;\n if(equals(d,r)) return ICC_CIRCUMSCRIBE;\n if(d>r) return ICC_SEPERATE;\n if(equals(d+c2.r,c1.r)) return ICC_INSCRIBE;\n if(d+c2.r<c1.r) return ICC_CONTAIN;\n return ICC_INTERSECT;\n}\n\nbool intersectSC(Segment s,Circle c){\n return getDistanceSP(s,c.c)<=c.r;\n}\n\nint intersectCS(Circle c,Segment s){\n if(norm(project(s,c.c)-c.c)-c.r*c.r>EPS) return 0;\n double d1=abs(c.c-s.p1),d2=abs(c.c-s.p2);\n if(d1<c.r+EPS and d2<c.r+EPS) return 0;\n if((d1<c.r-EPS and d2>c.r+EPS)\n or (d1>c.r+EPS and d2<c.r-EPS)) return 1;\n Point h=project(s,c.c);\n if(dot(s.p1-h,s.p2-h)<0) return 2;\n return 0;\n}\n\nPoint getCrossPointSS(Segment s1,Segment s2){\n for(int k=0;k<2;++k){\n if(getDistanceSP(s1,s2.p1)<EPS) return s2.p1;\n if(getDistanceSP(s1,s2.p2)<EPS) return s2.p2;\n swap(s1,s2);\n }\n Vector base=s2.p2-s2.p1;\n double d1=fabs(cross(base,s1.p1-s2.p1));\n double d2=fabs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}\n\nPoint getCrossPointLL(Line l1,Line l2){\n double a=cross(l1.p2-l1.p1,l2.p2-l2.p1);\n double b=cross(l1.p2-l1.p1,l1.p2-l2.p1);\n if(fabs(a)<EPS and fabs(b)<EPS) return l2.p1;\n return l2.p1+(l2.p2-l2.p1)*(b/a);\n}\n\nPolygon getCrossPointCL(Circle c,Line l){\n Polygon ps;\n Point pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n if(equals(getDistanceLP(l,c.c),c.r)){\n ps.emplace_back(pr);\n return ps;\n }\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n ps.emplace_back(pr+e*base);\n ps.emplace_back(pr-e*base);\n return ps;\n}\n\nPolygon getCrossPointCS(Circle c,Segment s){\n Line l(s);\n Polygon res=getCrossPointCL(c,l);\n if(intersectCS(c,s)==2) return res;\n if(res.size()>1u){\n if(dot(l.p1-res[0],l.p2-res[0])>0) swap(res[0],res[1]);\n res.pop_back();\n }\n return res;\n}\n\n\nPolygon getCrossPointCC(Circle c1,Circle c2){\n Polygon p(2);\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n p[0]=c1.c+polar(c1.r,t+a);\n p[1]=c1.c+polar(c1.r,t-a);\n return p;\n}\n\n// IN:2 ON:1 OUT:0\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;++i){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.y>b.y) swap(a,b);\n if(a.y < EPS and EPS < b.y and cross(a,b) > EPS )\n x = !x;\n }\n return (x?2:0);\n}\n\n// BEGIN IGNORE\nPolygon andrewScan(Polygon s){\n Polygon u,l;\n if(s.size()<3) return s;\n sort(s.begin(),s.end());\n u.push_back(s[0]);\n u.push_back(s[1]);\n l.push_back(s[s.size()-1]);\n l.push_back(s[s.size()-2]);\n for(int i=2;i<(int)s.size();++i){\n for(int n=u.size();\n n>=2 and ccw(u[n-2],u[n-1],s[i])!=CCW_CLOCKWISE;\n n--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n for(int i=s.size()-3;i>=0;i--){\n for(int n=l.size();\n n>=2 and ccw(l[n-2],l[n-1],s[i])!=CCW_CLOCKWISE;\n n--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n reverse(l.begin(),l.end());\n for(int i=u.size()-2;i>=1;i--) l.push_back(u[i]);\n return l;\n}\n// END IGNORE\n\nPolygon convex_hull(Polygon ps){\n int n=ps.size();\n sort(ps.begin(),ps.end(),sort_y);\n int k=0;\n Polygon qs(n*2);\n for(int i=0;i<n;++i){\n while(k>1 and cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0)\n k--;\n qs[k++]=ps[i];\n }\n for(int i=n-2,t=k;i>=0;i--){\n while(k>t and cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0)\n k--;\n qs[k++]=ps[i];\n }\n qs.resize(k-1);\n return qs;\n}\n\ndouble diameter(Polygon s){\n Polygon p=s;\n int n=p.size();\n if(n==2) return abs(p[0]-p[1]);\n int i=0,j=0;\n for(int k=0;k<n;++k){\n if(p[i]<p[k]) i=k;\n if(!(p[j]<p[k])) j=k;\n }\n double res=0;\n int si=i,sj=j;\n while(i!=sj or j!=si){\n res=max(res,abs(p[i]-p[j]));\n if(cross(p[(i+1)%n]-p[i],p[(j+1)%n]-p[j])<0.0){\n i=(i+1)%n;\n }else{\n j=(j+1)%n;\n }\n }\n return res;\n}\n\nbool isConvex(Polygon p){\n bool f=1;\n int n=p.size();\n for(int i=0;i<n;++i){\n int t=ccw(p[(i+n-1)%n],p[i],p[(i+1)%n]);\n f&=t!=CCW_CLOCKWISE;\n }\n return f;\n}\n\ndouble area(Polygon s){\n double res=0;\n for(int i=0;i<(int)s.size();++i){\n res+=cross(s[i],s[(i+1)%s.size()])/2.0;\n }\n return res;\n}\n\ndouble area(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n if(c1.r+c2.r<=d+EPS) return 0;\n if(d<=fabs(c1.r-c2.r)){\n double r=min(c1.r,c2.r);\n return PI*r*r;\n }\n double res=0;\n for(int k=0;k<2;++k){\n double rc=(d*d+c1.r*c1.r-c2.r*c2.r)/(2*d*c1.r);\n double th=acosl(rc)*2;\n res+=(th-sinl(th))*c1.r*c1.r/2;\n swap(c1,c2);\n }\n return res;\n}\n\nPolygon convexCut(Polygon p,Line l){\n Polygon q;\n for(int i=0;i<(int)p.size();++i){\n Point a=p[i],b=p[(i+1)%p.size()];\n if(ccw(l.p1,l.p2,a)!=-1) q.push_back(a);\n if(ccw(l.p1,l.p2,a)*ccw(l.p1,l.p2,b)<0)\n q.push_back(getCrossPointLL(Line(a,b),l));\n }\n return q;\n}\n\nLine bisector(Point p1,Point p2){\n Circle c1=Circle(p1,abs(p1-p2)),c2=Circle(p2,abs(p1-p2));\n Polygon p=getCrossPointCC(c1,c2);\n if(cross(p2-p1,p[0]-p1)>0) swap(p[0],p[1]);\n return Line(p[0],p[1]);\n}\n\nVector translate(Vector v,double theta){\n Vector res;\n res.x=cos(theta)*v.x-sin(theta)*v.y;\n res.y=sin(theta)*v.x+cos(theta)*v.y;\n return res;\n}\n\nvector<Line> corner(Line l1,Line l2){\n vector<Line> res;\n if(isParallel(l1,l2)){\n double d=getDistanceLP(l1,l2.p1)/2.0;\n Vector v1=l1.p2-l1.p1;\n v1=v1/v1.abs()*d;\n Point p=l2.p1+translate(v1,90.0*(PI/180.0));\n double d1=getDistanceLP(l1,p);\n double d2=getDistanceLP(l2,p);\n if(fabs(d1-d2)>d){\n p=l2.p1+translate(v1,-90.0*(PI/180.0));\n }\n res.push_back(Line(p,p+v1));\n }else{\n Point p=getCrossPointLL(l1,l2);\n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n v1=v1/v1.abs();\n v2=v2/v2.abs();\n res.push_back(Line(p,p+(v1+v2)));\n res.push_back(\n Line(p,p+translate(v1+v2,90.0*(PI/180.0))));\n }\n return res;\n}\n\nPolygon tangent(Circle c1,Point p2){\n Circle c2=Circle(p2,sqrt(norm(c1.c-p2)-c1.r*c1.r));\n Polygon p=getCrossPointCC(c1,c2);\n sort(p.begin(),p.end());\n return p;\n}\n\nvector<Line> tangent(Circle c1,Circle c2){\n vector<Line> ls;\n if(c1.r<c2.r) swap(c1,c2);\n double g=norm(c1.c-c2.c);\n if(equals(g,0)) return ls;\n Point u=(c2.c-c1.c)/sqrt(g);\n Point v=orth(u);\n for(int s=1;s>=-1;s-=2){\n double h=(c1.r+s*c2.r)/sqrt(g);\n if(equals(1-h*h,0)){\n ls.emplace_back(c1.c+u*c1.r,c1.c+(u+v)*c1.r);\n }else if(1-h*h>0){\n Point uu=u*h,vv=v*sqrt(1-h*h);\n ls.emplace_back(\n c1.c+(uu+vv)*c1.r,c2.c-(uu+vv)*c2.r*s);\n ls.emplace_back(\n c1.c+(uu-vv)*c1.r,c2.c-(uu-vv)*c2.r*s);\n }\n }\n\n return ls;\n}\n\ndouble closest_pair(Polygon &a,int l=0,int r=-1){\n if(r<0){\n r=a.size();\n sort(a.begin(),a.end(),sort_x);\n }\n if(r-l<=1) return abs(a[0]-a[1]);\n int m=(l+r)>>1;\n double x=a[m].x;\n double d=min(closest_pair(a,l,m),closest_pair(a,m,r));\n inplace_merge(a.begin()+l,a.begin()+m,a.begin()+r,sort_y);\n\n Polygon b;\n for(int i=l;i<r;++i){\n if(fabs(a[i].x-x)>=d) continue;\n for(int j=0;j<(int)b.size();++j){\n double dy=a[i].y-next(b.rbegin(),j)->y;\n if(dy>=d) break;\n d=min(d,abs(a[i]-*next(b.rbegin(),j)));\n }\n b.emplace_back(a[i]);\n }\n return d;\n}\n\nvector<vector<int>>\nsegmentArrangement(vector<Segment> &ss, Polygon &ps){\n int n=ss.size();\n for(int i=0;i<n;++i){\n ps.emplace_back(ss[i].p1);\n ps.emplace_back(ss[i].p2);\n for(int j=i+1;j<n;++j)\n if(intersectSS(ss[i],ss[j]))\n ps.emplace_back(getCrossPointSS(ss[i],ss[j]));\n }\n sort(ps.begin(),ps.end());\n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n\n vector<vector<int> > G(ps.size());\n for(int i=0;i<n;++i){\n vector<pair<double,int> > ls;\n for(int j=0;j<(int)ps.size();++j)\n if(getDistanceSP(ss[i],ps[j])<EPS)\n ls.emplace_back(make_pair(norm(ss[i].p1-ps[j]),j));\n\n sort(ls.begin(),ls.end());\n for(int j=0;j+1<(int)ls.size();++j){\n int a=ls[j].second,b=ls[j+1].second;\n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n }\n for(auto &v:G){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n }\n return G;\n}\n\nstruct EndPoint{\n Point p;\n int seg,st;\n EndPoint(){}\n EndPoint(Point p,int seg,int st):p(p),seg(seg),st(st){}\n bool operator<(const EndPoint &ep)const{\n if(p.y==ep.p.y) return st<ep.st;\n return p.y<ep.p.y;\n }\n};\n\nint manhattan_intersection(\n vector<Segment> ss,const int INF){\n const int BTM = 0;\n const int LFT = 1;\n const int RGH = 2;\n const int TOP = 3;\n\n int n=ss.size();\n vector<EndPoint> ep;\n for(int i=0;i<n;++i){\n if(ss[i].p1.y==ss[i].p2.y){\n if(ss[i].p1.x>ss[i].p2.x) swap(ss[i].p1,ss[i].p2);\n ep.emplace_back(ss[i].p1,i,LFT);\n ep.emplace_back(ss[i].p2,i,RGH);\n }else{\n if(ss[i].p1.y>ss[i].p2.y) swap(ss[i].p1,ss[i].p2);\n ep.emplace_back(ss[i].p1,i,BTM);\n ep.emplace_back(ss[i].p2,i,TOP);\n }\n }\n sort(ep.begin(),ep.end());\n\n set<int> bt;\n bt.insert(INF);\n\n int cnt=0;\n for(int i=0;i<n*2;++i){\n if(ep[i].st==TOP){\n bt.erase(ep[i].p.x);\n }else if(ep[i].st==BTM){\n bt.emplace(ep[i].p.x);\n }else if(ep[i].st==LFT){\n auto b=bt.lower_bound(ss[ep[i].seg].p1.x);\n auto e=bt.upper_bound(ss[ep[i].seg].p2.x);\n cnt+=distance(b,e);\n }\n }\n\n return cnt;\n}\n\ndouble area(Polygon ps,Circle c){\n if(ps.size()<3u) return 0;\n function<double(Circle, Point, Point)> dfs=\n [&](Circle c,Point a,Point b){\n Vector va=c.c-a,vb=c.c-b;\n double f=cross(va,vb),res=0;\n if(equals(f,0.0)) return res;\n if(max(abs(va),abs(vb))<c.r+EPS) return f;\n Vector d(dot(va,vb),cross(va,vb));\n if(getDistanceSP(Segment(a,b),c.c)>c.r-EPS)\n return c.r*c.r*atan2(d.y,d.x);\n auto u=getCrossPointCS(c,Segment(a,b));\n if(u.empty()) return res;\n if(u.size()>1u and dot(u[1]-u[0],a-u[0])>0)\n swap(u[0],u[1]);\n u.emplace(u.begin(),a);\n u.emplace_back(b);\n for(int i=1;i<(int)u.size();++i)\n res+=dfs(c,u[i-1],u[i]);\n return res;\n };\n double res=0;\n for(int i=0;i<(int)ps.size();++i)\n res+=dfs(c,ps[i],ps[(i+1)%ps.size()]);\n return res/2;\n}\n\n//end\n\n\n\nusing Graph =vector<vector<ll>>;\n\nint main(){\n int n;\n cin>>n;\n\n for (int i0=0; i0<n; i0++){\n double xa,ya,xb,yb;\n cin>>xa>>ya>>xb>>yb;\n Segment S0={Point(xa,ya),Point(xb,yb)};\n\n int m;\n cin>>m;\n vector<pair<Point,int>> P;\n\n for (int i=0; i<m; i++){\n double xs,ys,xt,yt;\n int o,l;\n cin>>xs>>ys>>xt>>yt>>o>>l;\n\n int f=(o^l);\n Segment S1={Point(xs,ys),Point(xt,yt)};\n if (intersectSS(S0,S1)){\n P.emplace_back(getCrossPointSS(S0,S1),f);\n }\n }\n auto comp=[&](auto& a, auto&b){\n if (a.first.x==b.first.x){\n return a.first.y<b.first.y;\n }\n return a.first.x<b.first.x;\n };\n sort(P.begin(),P.end(),comp);\n int ans=0;\n for (int i=1; i<P.size(); i++){\n if (P[i].second!=P[i-1].second)ans++;\n }\n cout<<ans<<endl;\n\n\n }\n\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3552, "score_of_the_acc": -1.798, "final_rank": 19 }, { "submission_id": "aoj_2003_9345416", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing ll = long long;\n#define MOD 1000000007\n#define Mod 998244353\nconst int MAX = 1000000005;\nconst long long INF = 1000000000000000005LL;\nusing namespace std;\n//using namespace atcoder;\n\n\nconst double EPS = 1e-9;\n\nstruct Point {\n\tdouble x, y;\n\t\n\tPoint operator+(Point& other) {\n\t\treturn {x + other.x, y + other.y};\n\t}\n\tPoint operator-(Point& other) {\n\t\treturn {x - other.x, y - other.y};\n\t}\n};\n\nstruct Segment {\n\tPoint a, b;\n};\n\ndouble norm(Point P) {return sqrt(P.x*P.x + P.y*P.y);}\ndouble arg(Point P) {return atan2(P.y, P.x);}\n\ndouble dot(Point P, Point Q) {\n\treturn P.x*Q.x + P.y*Q.y;\n}\n\ndouble cross(Point P, Point Q) {\n\treturn P.x*Q.y - P.y*Q.x;\n}\n\nint ccw(Point a, Point b, Point c) {\n\tif (cross(b - a, c - a) > EPS) return 1;\n\tif (cross(b - a, c - a) < -EPS) return -1;\n\tif (dot(b - a, c - a) < -EPS) return 2;\n\tif (norm(b - a) + EPS < norm(c - a)) return -2;\n\treturn 0;\n}\n\nbool intersect(Segment s1, Segment s2) {\n bool b1 = (ccw(s1.a, s1.b, s2.a)*ccw(s1.a, s1.b, s2.b) <= 0);\n bool b2 = (ccw(s2.a, s2.b, s1.a)*ccw(s2.a, s2.b, s1.b) <= 0);\n return b1 && b2;\n}\n\nPoint cross_point(Segment s1, Segment s2) {\n\tPoint a = s1.a, b = s1.b, c = s2.a, d = s2.b;\n\tPoint p;\n\tp.x = a.x + cross(a-c, d-c)/cross(d-c, b-a) * (b-a).x;\n\tp.y = a.y + cross(a-c, d-c)/cross(d-c, b-a) * (b-a).y;\n\treturn p;\n}\n\nstruct CPoint {\n Point p;\n int o, l;\n};\n\nbool solve() {\n Point a, b;\n cin >> a.x >> a.y >> b.x >> b.y;\n Segment s1 = {a, b};\n int n;\n cin >> n;\n vector<CPoint> crosses;\n for (int i = 0; i < n; i++) {\n Point c, d;\n int o, l;\n cin >> c.x >> c.y >> d.x >> d.y >> o >> l;\n Segment s2 = {c, d};\n if (!intersect(s1, s2)) continue;\n crosses.push_back({cross_point(s1, s2), o, l});\n }\n sort(crosses.begin(), crosses.end(), [&](CPoint& a, CPoint& b) -> bool {\n if (fabs(a.p.x - b.p.x) < EPS) return a.p.y < b.p.y;\n return a.p.x < b.p.x;\n });\n int ans = 0;\n for (int i = 1; i < crosses.size(); i++) {\n int pl = (crosses[i-1].o ? crosses[i-1].l : 1-crosses[i-1].l);\n int nl = (crosses[i].o ? crosses[i].l : 1-crosses[i].l);\n ans += (pl != nl);\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie();\n int t;\n cin >> t;\n while (t--) {solve();}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3480, "score_of_the_acc": -0.9571, "final_rank": 2 }, { "submission_id": "aoj_2003_9333562", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cmath>\n#include <iterator>\n#include <utility>\n#include <vector>\n\nusing Real = long double;\nconstexpr Real EPS = 1e-10;\nconst Real PI = std::acos(-1);\n\nint sign(Real x) {\n return x < -EPS ? -1 : x > EPS ? 1 : 0;\n}\n\nbool eq(Real lhs, Real rhs) {\n return sign(rhs - lhs) == 0;\n}\n\nbool lte(Real lhs, Real rhs) {\n return sign(rhs - lhs) >= 0;\n}\n\nbool lt(Real lhs, Real rhs) {\n return sign(rhs - lhs) > 0;\n}\n\nbool gte(Real lhs, Real rhs) {\n return lte(rhs, lhs);\n}\n\nbool gt(Real lhs, Real rhs) {\n return lt(rhs, lhs);\n}\n\nstruct Point {\n Real x;\n Real y;\n Point(Real x = 0, Real y = 0): x(x), y(y) {}\n Point& operator+=(Point rhs) {\n this->x += rhs.x;\n this->y += rhs.y;\n return *this;\n }\n Point& operator-=(Point rhs) {\n this->x -= rhs.x;\n this->y -= rhs.y;\n return *this;\n }\n Point& operator*=(Real k) {\n this->x *= k;\n this->y *= k;\n return *this;\n }\n Point& operator/=(Real k) {\n this->x /= k;\n this->y /= k;\n return *this;\n }\n friend Point operator+(Point lhs, Point rhs) {\n return lhs += rhs;\n }\n friend Point operator-(Point lhs, Point rhs) {\n return lhs -= rhs;\n }\n friend Point operator*(Point p, Real k) {\n return p *= k;\n }\n friend Point operator/(Point p, Real k) {\n return p /= k;\n }\n friend Point operator*(Real k, Point p) {\n return p * k;\n }\n friend Point operator/(Real k, Point p) {\n return p / k;\n }\n friend bool operator==(Point lhs, Point rhs) {\n return eq(lhs.x, rhs.x) && eq(lhs.y, rhs.y);\n }\n friend bool operator<(Point lhs, Point rhs) {\n if (eq(lhs.x, rhs.x)) return lt(lhs.y, rhs.y);\n return lt(lhs.x, rhs.x);\n }\n friend bool operator>(Point lhs, Point rhs) {\n return rhs < lhs;\n }\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = Points;\nusing Vector = Point;\n\nReal norm(Vector p) {\n return p.x * p.x + p.y * p.y;\n}\n\nReal abs(Vector p) {\n return std::sqrt(norm(p));\n}\n\nReal dot(Vector lhs, Vector rhs) {\n return lhs.x * rhs.x + lhs.y * rhs.y;\n}\n\nReal cross(Vector lhs, Vector rhs) {\n return lhs.x * rhs.y - lhs.y * rhs.x;\n}\n\nint ccw(Point p0, Point p1, Point p2) {\n Vector a = p1 - p0;\n Vector b = p2 - p0;\n\n if (cross(a, b) > EPS) return 1;\n\n if (cross(a, b) < -EPS) return -1;\n\n if (dot(a, b) < 0) return 2;\n\n if (norm(a) < norm(b)) return -2;\n\n return 0;\n}\n\nstruct Circle {\n Real r;\n Point c;\n Circle() {}\n Circle(Real r, Point c): r(r), c(c) {}\n};\n\nstruct Segment {\n Segment() {}\n Segment(Point p0, Point p1): p0(p0), p1(p1) {}\n Point p0, p1;\n};\n\nstruct Line {\n Line() {}\n Line(Point p0, Point p1): p0(p0), p1(p1) {}\n explicit Line(Segment s): p0(s.p0), p1(s.p1) {}\n Point p0, p1;\n};\n\nReal distance(Point lhs, Point rhs) {\n return abs(rhs - lhs);\n}\n\nReal distance(Line l, Point p) {\n return std::abs(cross(l.p1 - l.p0, p - l.p0)) / abs(l.p1 - l.p0);\n}\n\nReal distance(Segment s, Point p) {\n if (dot(s.p1 - s.p0, p - s.p0) < 0) {\n return distance(p, s.p0);\n }\n if (dot(s.p0 - s.p1, p - s.p1) < 0) {\n return distance(p, s.p1);\n }\n return distance(Line(s), p);\n}\n\nbool intersect(Segment lhs, Segment rhs) {\n return ccw(lhs.p0, lhs.p1, rhs.p0) * ccw(lhs.p0, lhs.p1, rhs.p1) <= 0\n && ccw(rhs.p0, rhs.p1, lhs.p0) * ccw(rhs.p0, rhs.p1, lhs.p1) <= 0;\n}\n\nbool intersect(Segment s, Point p) {\n return ccw(s.p0, s.p1, p) == 0;\n}\n\nReal distance(Segment lhs, Segment rhs) {\n if (intersect(lhs, rhs)) {\n return Real(0);\n }\n return std::min({distance(lhs, rhs.p0), distance(lhs, rhs.p1), distance(rhs, lhs.p0), distance(rhs, lhs.p1)});\n}\n\nbool parallel(Vector lhs, Vector rhs) {\n return eq(cross(lhs, rhs), 0);\n}\n\nbool parallel(Segment lhs, Segment rhs) {\n return parallel(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nbool parallel(Line lhs, Line rhs) {\n return parallel(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nPoint crosspoint(Line lhs, Line rhs) {\n Real a = cross(lhs.p1 - lhs.p0, rhs.p1 - rhs.p0);\n Real b = cross(lhs.p1 - lhs.p0, lhs.p1 - rhs.p0);\n return rhs.p0 + (rhs.p1 - rhs.p0) * b / a;\n}\n\nbool intersect(Line l, Segment s) {\n return ccw(l.p0, l.p1, s.p0) * ccw(l.p0, l.p1, s.p1) <= 0;\n}\n\nbool intersect(Circle c, Line l) {\n return lte(distance(l, c.c), c.r);\n}\n\nbool intersect(Circle c, Point p) {\n return eq(distance(c.c, p), c.r);\n}\n\nint intersect(Circle lhs, Circle rhs) {\n if (gt(lhs.r, rhs.r)) std::swap(lhs, rhs);\n Real d = distance(lhs.c, rhs.c);\n if (lt(lhs.r + rhs.r, d)) return 4;\n if (eq(lhs.r + rhs.r, d)) return 3;\n if (gt(lhs.r + d, rhs.r)) return 2;\n if (eq(lhs.r + d, rhs.r)) return 1;\n return 0;\n}\n\nbool orthogonal(Vector lhs, Vector rhs) {\n return eq(dot(lhs, rhs), 0);\n}\n\nbool orthogonal(Segment lhs, Segment rhs) {\n return orthogonal(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nbool orthogonal(Line lhs, Line rhs) {\n return orthogonal(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nPoint crosspoint(Segment lhs, Segment rhs) {\n Real d0 = distance(Line(lhs.p0, lhs.p1), rhs.p0);\n Real d1 = distance(Line(lhs.p0, lhs.p1), rhs.p1);\n return rhs.p0 + (rhs.p1 - rhs.p0) * (d0 / (d0 + d1));\n}\n\nReal arg(Vector p) {\n return std::atan2(p.y, p.x);\n}\n\nVector polar(Real a, Real r) {\n return Point(std::cos(r) * a, std::sin(r) * a);\n}\n\nPoint rotate(Point p, Real t) {\n Point v = polar(1, t);\n return Point(p.x * v.x - p.y * v.y, p.x * v.y + p.y * v.x);\n}\n\nReal angle(Point p0, Point p1, Point p2) {\n Real a = arg(p0 - p1);\n Real b = arg(p2 - p1);\n if (gt(a, b)) std::swap(a, b);\n return std::min(b - a, 2 * PI - (b - a));\n}\n\nPoints crosspoint(Circle lhs, Circle rhs) {\n Real d = abs(lhs.c - rhs.c);\n if (eq(d, lhs.r + rhs.r)) return {lhs.c + lhs.r * (rhs.c - lhs.c) / d};\n Real a = std::acos((lhs.r * lhs.r + d * d - rhs.r * rhs.r) / (2 * lhs.r * d));\n Real t = arg(rhs.c - lhs.c);\n return {lhs.c + polar(lhs.r, t + a), lhs.c + polar(lhs.r, t - a)};\n}\n\nPoint projection(Segment s, Point p) {\n Vector a = p - s.p0;\n Vector b = s.p1 - s.p0;\n Real t = dot(a, b) / norm(b);\n return s.p0 + t * b;\n}\n\nPoint projection(Line l, Point p) {\n Vector a = p - l.p0;\n Vector b = l.p1 - l.p0;\n Real t = dot(a, b) / norm(b);\n return l.p0 + t * b;\n}\n\nPoint reflection(Segment s, Point p) {\n return p + (projection(s, p) - p) * Real(2);\n}\n\nPoint reflection(Line l, Point p) {\n return p + (projection(l, p) - p) * Real(2);\n}\n\nPoints crosspoint(Circle c, Line l) {\n Vector p = projection(l, c.c);\n Real t = std::sqrt(c.r * c.r - norm(c.c - p));\n if (eq(t, 0)) return {p};\n Vector e = (l.p1 - l.p0) / abs(l.p1 - l.p0);\n return {p + t * e, p + -t * e};\n}\n\nReal area(Polygon poly) {\n int n = poly.size();\n Real result = 0;\n for (int i = 0; i < n; i++) {\n result += cross(poly[i], poly[(i + 1) % n]);\n }\n return result / 2;\n}\n\nReal perimeter(Polygon poly) {\n int n = poly.size();\n Real result = 0;\n for (int i = 0; i < n; i++) {\n result += abs(poly[i] - poly[(i + 1) % n]);\n }\n return result;\n}\n\nbool convex(Polygon poly) {\n int n = poly.size();\n for (int i = 0; i < n; i++) {\n if (ccw(poly[i], poly[(i + 1) % n], poly[(i + 2) % n]) == -1) return false;\n }\n return true;\n}\n\nint contain(Polygon poly, Point p) {\n int n = poly.size();\n bool parity = false;\n for (int i = 0; i < n; i++) {\n Point a = poly[i] - p;\n Point b = poly[(i + 1) % n] - p;\n if (gt(a.y, b.y)) std::swap(a, b);\n if (lte(a.y, 0) && lt(0, b.y) && lt(cross(a, b), 0)) parity ^= true;\n if (eq(cross(a, b), 0) && lte(dot(a, b), 0)) return 1;\n }\n return (parity ? 2 : 0);\n}\n\nPolygon convex_hull(Points points) {\n std::sort(points.begin(), points.end(), [](Point lhs, Point rhs) {\n if (eq(lhs.x, rhs.x)) return lt(lhs.y, rhs.y);\n return lt(lhs.x, rhs.x);\n });\n int n = points.size();\n Points lower, upper;\n lower.push_back(points[n - 1]);\n lower.push_back(points[n - 2]);\n upper.push_back(points[0]);\n upper.push_back(points[1]);\n for (int i = n - 3; i >= 0; i--) {\n for (int m = lower.size(); m >= 2 && ccw(lower[m - 2], lower[m - 1], points[i]) >= 0; m--) {\n lower.pop_back();\n }\n lower.push_back(points[i]);\n }\n for (int i = 2; i < n; i++) {\n for (int m = upper.size(); m >= 2 && ccw(upper[m - 2], upper[m - 1], points[i]) >= 0; m--) {\n upper.pop_back();\n }\n upper.push_back(points[i]);\n }\n std::reverse(lower.begin(), lower.end());\n std::copy(std::next(upper.rbegin()), std::prev(upper.rend()), std::back_inserter(lower));\n return lower;\n}\n\nPolygon convex_cut(Polygon poly, Line l) {\n int n = poly.size();\n Polygon res;\n for (int i = 0; i < n; i++) {\n Point a = poly[i];\n Point b = poly[(i + 1) % n];\n if (ccw(l.p0, l.p1, a) != -1) res.push_back(a);\n if (ccw(l.p0, l.p1, a) * ccw(l.p0, l.p1, b) == -1) {\n res.push_back(crosspoint(l, Line(a, b)));\n }\n }\n return res;\n}\n\nLine bisector(Point p0, Point p1, Point p2) {\n return Line(p1, p1 + polar(1, angle(p0, p1, p2) / 2));\n}\n\nCircle incircle(Point p0, Point p1, Point p2) {\n Point c = crosspoint(bisector(p0, p1, p2), bisector(p1, p2, p0));\n Real r = std::abs(2 * area({p0, p1, p2}) / perimeter({p0, p1, p2}));\n return Circle(r, c);\n}\n\nLine perpendicular(Line l, Point p) {\n Vector v = l.p1 - l.p0;\n return Line(p, p + Vector(-v.y, v.x));\n}\n\nLine perpendicular_bisector(Segment s) {\n return perpendicular(Line(s), (s.p0 + s.p1) / 2);\n}\n\nCircle circumscribed_circle(Point p0, Point p1, Point p2) {\n Point c = crosspoint(perpendicular_bisector(Segment(p0, p1)), perpendicular_bisector(Segment(p0, p2)));\n return Circle(distance(p0, c), c);\n}\n\nReal area(Circle c) {\n return c.r * c.r * PI;\n}\n\nPoints tangent(Circle c, Point p) {\n return crosspoint(c, Circle(std::sqrt(norm(c.c - p) - c.r * c.r), p));\n}\n\nint solve() {\n Point start, goal;\n std::cin >> start.x >> start.y;\n std::cin >> goal.x >> goal.y;\n Segment segment(start, goal);\n int n;\n std::cin >> n;\n std::vector<std::pair<Point, int>> cp;\n for (int i = 0; i < n; i++) {\n Point p, q;\n std::cin >> p.x >> p.y;\n std::cin >> q.x >> q.y;\n Segment s(p, q);\n int a, b;\n std::cin >> a >> b;\n if (intersect(segment, s)) {\n cp.emplace_back(crosspoint(segment, s), a ^ b);\n }\n }\n std::sort(cp.begin(), cp.end());\n int ans = 0;\n for (int l = 0, r = 0; l < (int)cp.size(); l = r) {\n while (r < (int)cp.size() && cp[l].second == cp[r].second) r++;\n ans++;\n }\n std::cout << std::max(0, ans - 1) << std::endl;\n return 0;\n}\n\nint main() {\n int n;\n std::cin >> n;\n while (n--) solve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3480, "score_of_the_acc": -1.7071, "final_rank": 16 }, { "submission_id": "aoj_2003_9327227", "code_snippet": "#include <bits/stdc++.h>\n\n//#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)\n//#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)\n//#define all(v) (v).begin(), (v).end()\n//#define rall(v) (v).rbegin(), (v).rend()\n//#define bit(n, k) (((n) >> (k)) & 1)\n//#define pcnt(n) (bitset<64>(n).count())\n//#define endl '\\n'\n//#define fi first\n//#define se second\n//#define mkpr make_pair\n//#define mktpl make_tuple\n//#define eb emplace_back\n//template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& V) {\n// for (auto& it : V) is >> it;\n// return is;\n//}\n//template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) {\n// for (int i = 0 ; i < (int)V.size() ; i++) {\n// os << V[i] << (i + 1 == (int)V.size() ? \"\" : \" \");\n// }\n// return os;\n//}\n//using namespace std;\n\n\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nusing Real = long double;\n\nnamespace internal {\n\nReal EPS{1e-12};\nconstexpr i32 negative{-1};\nconstexpr i32 zero{};\nconstexpr i32 positive{1};\n\n} // namespace internal\n\nReal& Eps() {\n return internal::EPS;\n}\n\ni32 Sign(Real value) {\n if (value < -Eps()) return internal::negative;\n if (value > Eps()) return internal::positive;\n return internal::zero;\n}\n\nbool Zero(Real value) {\n return Sign(value) == internal::zero;\n}\n\nbool Positive(Real value) {\n return Sign(value) == internal::positive;\n}\n\nbool Negative(Real value) {\n return Sign(value) == internal::negative;\n}\n\nbool Equal(Real a, Real b) {\n return Zero(a - b);\n}\n\nbool Smaller(Real a, Real b) {\n return Negative(a - b);\n}\n\nbool Bigger(Real a, Real b) {\n return Positive(a - b);\n}\n\nReal Square(Real value) {\n return (Zero(value) ? value : value * value);\n}\n\nReal Sqrt(Real value) {\n assert(!Negative(value));\n return (Zero(value) ? value : sqrtl(value));\n}\n\nReal Abs(Real value) {\n return (Negative(value) ? -value : value);\n}\n\n} // namespace geometryR2\n \n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nconstexpr Real PI{acosl(-1)};\nconstexpr Real TAU{static_cast<Real>(2) * PI};\n\nconstexpr Real ArcToRadian(Real arc) {\n return (arc * PI) / static_cast<Real>(180);\n}\n\nconstexpr Real RadianToArc(Real radian) {\n return (radian * static_cast<Real>(180)) / PI;\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Point {\nprivate:\n Real x_{}, y_{};\npublic:\n /* constructor */\n Point() = default;\n Point(Real x, Real y) : x_{x}, y_{y} {}\n\n /* getter, setter */\n Real x() const {\n return x_;\n }\n Real& x() {\n return x_;\n }\n Real y() const {\n return y_;\n }\n Real& y() {\n return y_;\n }\n\n /* operator */\n Point& operator+=(const Point& rhs) {\n x_ += rhs.x();\n y_ += rhs.y();\n return *this;\n }\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point{lhs} += rhs;\n }\n Point operator+() const {\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n x_ -= rhs.x();\n y_ -= rhs.y();\n return *this;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point{lhs} -= rhs;\n }\n Point operator-() const {\n return Point{} - *this;\n }\n Point& operator*=(Real k) {\n x_ *= k;\n y_ *= k;\n return *this;\n }\n friend Point operator*(Real k, const Point& p) {\n return Point{p} *= k;\n }\n friend Point operator*(const Point& p, Real k) {\n return Point{p} *= k;\n }\n Point& operator/=(Real k) {\n assert(!Zero(k));\n x_ /= k;\n y_ /= k;\n return *this;\n }\n friend Point operator/(Real k, const Point& p) {\n return Point{p} /= k;\n }\n friend Point operator/(const Point& p, Real k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& lhs, const Point& rhs) {\n return Equal(lhs.x(), rhs.x()) and Equal(lhs.y(), rhs.y());\n }\n friend bool operator!=(const Point& lhs, const Point& rhs) {\n return !Equal(lhs.x(), rhs.x()) or !Equal(lhs.y(), rhs.y());\n }\n friend bool operator<(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and Smaller(lhs.y(), rhs.y()));\n }\n friend bool operator<=(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and (Smaller(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend bool operator>(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and Bigger(lhs.y(), rhs.y()));\n }\n friend bool operator>=(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and (Bigger(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x_ >> p.y_;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x_ << ',' << p.y_ << ')';\n return os;\n }\n \n /* member function */\n Real normSquare() const {\n return Square(x_) + Square(y_);\n }\n Real norm() const {\n return Sqrt(normSquare());\n }\n void normalize() {\n assert((*this) != Point{});\n (*this) /= norm(); \n }\n Point normalized() const {\n Point res{*this};\n res.normalize();\n return res;\n }\n Point rotated(Real radian) const {\n return Point{\n x_ * cosl(radian) - y_ * sinl(radian),\n x_ * sinl(radian) + y_ * cosl(radian)\n };\n }\n void rotate(Real radian) {\n *this = rotated(radian); \n }\n Point rotatedByArc(Real arc) const {\n return rotated(ArcToRadian(arc));\n }\n void rotateByArc(Real arc) {\n *this = rotatedByArc(arc);\n }\n Real argument() const {\n return (Negative(y_) ? TAU : static_cast<Real>(0)) + atan2l(y_, x_);\n }\n Real argumentByArc() const {\n return RadianToArc(argument());\n }\n\n /* friend function */\n friend Real Dot(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.x() + lhs.y() * rhs.y();\n }\n friend Real Cross(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.y() - lhs.y() * rhs.x();\n }\n friend Real Argument(const Point& lhs, const Point& rhs) {\n return rhs.argument() - lhs.argument();\n }\n friend bool ArgComp(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.argument(), rhs.argument());\n }\n};\n\nusing Vector = Point;\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nenum RELATION {\n // p0 -> p1 -> p2の順で直線上に並んでいる\n ONLINE_FRONT = -2,\n // (p1 - p0) -> (p2 - p0)が時計回りになっている\n CLOCKWISE,\n // p0 -> p2 -> p1の順で直線上に並んでいる\n ON_SEGMENT,\n // (p1 - p0) -> (p2 - p0)が反時計回りになっている\n COUNTER_CLOCKWISE,\n // p2 -> p0 -> p1、またはp1 -> p0 -> p2の順で直線上に並んでいる\n ONLINE_BACK\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (Smaller(a.normSquare(), b.normSquare())) return ONLINE_FRONT;\n return ON_SEGMENT;\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.norm();\n}\n\nReal DistanceSquare(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.normSquare();\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Segment {\nprivate:\n Point p0_{}, p1_{};\npublic:\n /* constructor */\n Segment() = default;\n Segment(const Point& p0, const Point& p1) : p0_{p0}, p1_{p1} {}\n Segment(Real x0, Real y0, Real x1, Real y1) : p0_{x0, y0}, p1_{x1, y1} {}\n\n /* getter setter */\n const Point& p0() const {\n return p0_;\n }\n Point& p0() {\n return p0_;\n }\n const Point& p1() const {\n return p1_;\n }\n Point& p1() {\n return p1_;\n }\n\n /* member function */\n bool valid() const {\n return p0_ != p1_;\n }\n bool straddle(const Segment& s) const {\n return Relation(p0_, p1_, s.p0()) * Relation(p0_, p1_, s.p1()) <= 0;\n }\n Real length() const {\n assert(valid());\n return Distance(p0_, p1_);\n }\n Point midpoint() const {\n assert(valid());\n return p0_ + Vector{p1_ - p0_} / static_cast<Real>(2);\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nbool Intersect(const Segment& s0, const Segment& s1) {\n assert(s0.valid());\n assert(s1.valid());\n return s0.straddle(s1) and s1.straddle(s0);\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nbool Parallel(const Segment& s0, const Segment& s1) {\n assert(s0.valid());\n assert(s1.valid());\n return Zero(Cross(s0.p1() - s0.p0(), s1.p1() - s1.p0()));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nPoint CrossPoint(const Segment& s0, const Segment& s1) {\n assert(s0.valid());\n assert(s1.valid());\n assert(Intersect(s0, s1));\n if (Parallel(s0, s1)) {\n if (s0.p0() == s1.p0()) {\n if (Relation(s0.p0(), s0.p1(), s1.p1()) == ONLINE_BACK) {\n return s0.p0();\n }\n else {\n assert(false);\n }\n }\n else if (s0.p0() == s1.p1()) {\n if (Relation(s0.p0(), s0.p1(), s1.p0()) == ONLINE_BACK) {\n return s0.p0();\n }\n else {\n assert(false);\n }\n }\n else if (s0.p1() == s1.p0()) {\n if (Relation(s0.p1(), s0.p0(), s1.p1()) == ONLINE_BACK) {\n return s0.p1();\n }\n else {\n assert(false);\n }\n }\n else if (s0.p1() == s1.p1()) {\n if (Relation(s0.p1(), s0.p0(), s1.p0()) == ONLINE_BACK) {\n return s0.p1();\n }\n else {\n assert(false);\n }\n }\n else {\n assert(false);\n }\n }\n else {\n Vector base{s0.p1() - s0.p0()};\n Real baseNorm{base.norm()};\n Real r1{Abs(Cross(base, s1.p0() - s0.p0())) / baseNorm};\n Real r2{Abs(Cross(base, s1.p1() - s0.p0())) / baseNorm};\n return s1.p0() + (s1.p1() - s1.p0()) * (r1 / (r1 + r2));\n }\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p, const Segment& s) {\n assert(s.valid());\n if (Negative(Dot(s.p1() - s.p0(), p - s.p0()))) {\n return Distance(p, s.p0());\n }\n if (Negative(Dot(s.p0() - s.p1(), p - s.p1()))) {\n return Distance(p, s.p1());\n }\n return Abs(Cross(s.p1() - s.p0(), p - s.p0())) / s.length();\n}\n\nbool PointOnSegment(const Point& p, const Segment& s) {\n assert(s.valid());\n return Zero(Distance(p, s));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\nusing namespace zawa;\nusing namespace geometryR2;\n\nvoid solve() {\n Segment AB;\n std::cin >> AB.p0() >> AB.p1();\n int N;\n std::cin >> N;\n using item = std::tuple<Real, int, int>;\n std::vector<item> P;\n P.reserve(N);\n for (int i{} ; i < N ; i++) {\n Segment ST;\n std::cin >> ST.p0() >> ST.p1();\n int o, l;\n std::cin >> o >> l;\n if (!AB.valid()) {\n continue;\n }\n if (ST.valid()) {\n if (!Intersect(AB, ST)) continue;\n Point cp{CrossPoint(AB, ST)};\n P.emplace_back(Distance(AB.p0(), cp), o, l);\n }\n else {\n if (Zero(Distance(ST.p0(), AB))) {\n Point cp{ST.p0()};\n P.emplace_back(Distance(AB.p0(), cp), o, l);\n }\n }\n }\n if (!AB.valid()) {\n std::cout << 0 << '\\n';\n return;\n }\n std::sort(P.begin(), P.end());\n // 地下....0\n // 高架....1\n int ans[2]{ 0, 0 }, cur[2]{ 0, 1 };\n for (const auto& [d, o, l] : P) {\n for (int i{} ; i < 2 ; i++) {\n if (o == 1) { // 自社\n if (cur[i] != l) {\n cur[i] ^= 1;\n ans[i]++;\n }\n }\n else if (o == 0) { // 他社\n if (cur[i] == l) {\n cur[i] ^= 1;\n ans[i]++;\n }\n }\n else {\n assert(false);\n }\n }\n }\n std::cout << std::min(ans[0], ans[1]) << '\\n';\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n\n int T;\n std::cin >> T;\n while (T--) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3464, "score_of_the_acc": -1.1869, "final_rank": 5 }, { "submission_id": "aoj_2003_9298539", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nvoid chmax(T& p, T q) { if (p < q)p = q; };\n\nusing point=pair<double,double>;\n\ndouble EPS=1e-12;\nvoid solve() {\n cout<<fixed<<setprecision(14);\n point P,Q;\n cin>>P.first>>P.second>>Q.first>>Q.second;\n double dx=P.first-Q.first;\n double dy=P.second-Q.second;\n vector<pair<double,int>> C;\n ll N;\n cin>>N;\n rep(i,N){\n point S,T;\n int f,g;\n cin>>S.first>>S.second>>T.first>>T.second;\n cin>>f>>g;\n double DX=S.first-T.first;\n double DY=S.second-T.second;\n\n if(abs(dy*DX-dx*DY)<EPS)continue;\n\n double RX=T.first-Q.first;\n double RY=T.second-Q.second;\n double a=(RX*DY-RY*DX)/(dx*DY-dy*DX);\n double b;\n if(DX!=0)b=(a*dx-RX)/DX;\n else b=(a*dy-RY)/DY;\n if(a>1.0||a<0.0||b<0.0||b>1.0)continue;\n C.push_back({a,f^g});\n }\n sort(all(C));\n int an=0;\n ll CN=C.size();\n rep(i,CN-1)if(C[i].second!=C[i+1].second)an++;\n cout<<an<<endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N;\n cin>>N;\n rep(i,N){\n if(N==0)return 0;\n solve();\n }\n \n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3436, "score_of_the_acc": -1.1515, "final_rank": 4 }, { "submission_id": "aoj_2003_9266346", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing R = long double;\nusing Point = std::pair<R, R>;\nusing Segment = std::pair<Point, Point>;\n\nstd::istream& operator>>(std::istream& is, Point& p) {\n is >> p.first >> p.second;\n return is;\n}\nstd::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\n\nconst R EPS{1e-11};\nbool Positive(R v) {\n return v > EPS;\n}\nbool Negative(R v) {\n return v < -EPS;\n}\nbool Zero(R v) {\n return !Positive(v) and !Negative(v);\n}\nbool Smaller(R a, R b) { // <\n return Negative(a - b);\n}\nbool Bigger(R a, R b) {\n return Positive(a - b);\n}\nbool Equal(R a, R b) {\n return Zero(a - b);\n}\nR Square(R v) {\n return (Zero(v) ? 0.0l : v * v);\n}\n\nPoint operator+(const Point& p, const Point& q) {\n return Point{p.first + q.first, p.second + q.second};\n}\nPoint operator-(const Point& p, const Point& q) {\n return Point{p.first - q.first, p.second - q.second};\n}\nPoint operator*(const Point& p, R v) {\n return Point{p.first * v, p.second * v};\n}\nPoint operator/(const Point& p, R v) {\n assert(!Zero(v));\n return Point{p.first / v, p.second / v};\n}\n\nR NormSquare(const Point& p) {\n return Square(p.first) + Square(p.second);\n}\nR Norm(const Point& p) {\n return sqrtl(NormSquare(p));\n}\nR DistanceSquare(const Point& p, const Point& q) {\n return NormSquare(p - q);\n}\n\nR Cross(const Point& p, const Point& q) {\n return p.first * q.second - p.second * q.first;\n}\nR Dot(const Point& p, const Point& q) {\n return p.first * q.first + p.second * q.second;\n}\n\nenum RELATION {\n ONLINE_FRONT = -2,\n CLOCKWISE,\n ON_SEGMENT,\n COUNTER_CLOCKWISE,\n ONLINE_BACK\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (Smaller(NormSquare(a), NormSquare(b))) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool Straddle(const Segment& s, const Segment& t) {\n return Relation(s.first, s.second, t.first) * Relation(s.first, s.second, t.second) <= 0;\n}\n\nbool Intersect(const Segment& s, const Segment& t) {\n return Straddle(s, t) and Straddle(t, s);\n}\n\nPoint CrossPoint(const Segment& s, const Segment& t) {\n assert(Intersect(s, t));\n Point base{s.second - s.first};\n R baseNorm{Norm(base)};\n R r1{std::abs(Cross(base, t.first - s.first)) / baseNorm};\n R r2{std::abs(Cross(base, t.second - s.first)) / baseNorm};\n return t.first + (t.second - t.first) * (r1 / (r1 + r2));\n}\n\nvoid solve() {\n Point A, B;\n std::cin >> A >> B;\n Segment S{A, B};\n int N;\n std::cin >> N;\n std::vector<std::pair<R, std::pair<int, int>>> C;\n C.reserve(N);\n for (int i{} ; i < N ; i++) {\n Point p, q;\n std::cin >> p >> q;\n Segment T{p, q};\n int o, l;\n std::cin >> o >> l;\n if (!Intersect(S, T)) continue;\n Point crossPoint{CrossPoint(S, T)};\n C.emplace_back(DistanceSquare(A, crossPoint), std::make_pair(o, l));\n }\n std::sort(C.begin(), C.end());\n std::pair<int, int> score[2];\n score[0] = std::pair<int, int>{ 0, 0 };\n score[1] = std::pair<int, int>{ 0, 1 };\n for (const auto& [_, P] : C) {\n auto [o, l]{P};\n for (int i{} ; i < 2 ; i++) {\n if (o == 0) { // other\n if (l == score[i].second) {\n score[i].first++;\n score[i].second ^= 1;\n }\n }\n else if (o == 1) { // my\n if (l != score[i].second) {\n score[i].first++;\n score[i].second ^= 1;\n }\n }\n else {\n assert(false);\n }\n }\n }\n std::cout << std::min(score[0].first, score[1].first) << '\\n';\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n int T;\n std::cin >> T;\n while (T--) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3564, "score_of_the_acc": -1.3131, "final_rank": 8 }, { "submission_id": "aoj_2003_9036290", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nusing Real = long double;\nconst Real EPS = 1e-8, PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << \" \" << p.imag();\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(p.real() * d, p.imag() * d);\n}\nPoint operator/(const Point& p, const Real& d) {\n return Point(p.real() / d, p.imag() / d);\n}\nPoint rot(const Point& p, Real theta) {\n return p * Point(cos(theta), sin(theta));\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nReal dist(const Point& p1, const Point& p2) {\n return abs(p1 - p2);\n}\nbool comp_x(const Point& p1, const Point& p2) {\n return eq(p1.real(), p2.real()) ? p1.imag() < p2.imag() : p1.real() < p2.real();\n}\nbool comp_y(const Point& p1, const Point& p2) {\n return eq(p1.imag(), p2.imag()) ? p1.real() < p2.real() : p1.imag() < p2.imag();\n}\nbool comp_arg(const Point& p1, const Point& p2) {\n return arg(p1) < arg(p2);\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nReal closest_pair(vector<Point> ps) {\n if((int)ps.size() <= 1) return 1e18;\n sort(ps.begin(), ps.end(), comp_x);\n vector<Point> memo(ps.size());\n auto func = [&](auto& func, int l, int r) -> Real {\n if(r - l <= 1) return 1e18;\n int m = (l + r) >> 1;\n Real x = ps[m].real();\n Real res = min(func(func, l, m), func(func, m, r));\n inplace_merge(ps.begin() + l, ps.begin() + m, ps.begin() + r, comp_y);\n int cnt = 0;\n for(int i = l; i < r; ++i) {\n if(abs(ps[i].real() - x) >= res) continue;\n for(int j = 0; j < cnt; ++j) {\n Point d = ps[i] - memo[cnt - j - 1];\n if(d.imag() >= res) break;\n res = min(res, abs(d));\n }\n memo[cnt++] = ps[i];\n }\n return res;\n };\n return func(func, 0, (int)ps.size());\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_orthogonal(const Line& a, const Line& b) {\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\nPoint projection(const Line& l, const Point& p) {\n Real t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\nPoint reflection(const Line& l, const Point& p) {\n return p + (projection(l, p) - p) * 2.0;\n}\nbool is_intersect_lp(const Line& l, const Point& p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\nbool is_intersect_sp(const Segment& s, const Point& p) {\n return ccw(s.a, s.b, p) == 0;\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nbool is_intersect_ls(const Line& l, const Segment& s) {\n return sign(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a)) <= 0;\n}\nbool is_intersect_sl(const Segment& s, const Line& l) {\n return is_intersect_ls(l, s);\n}\nbool is_intersect_ss(const Segment& s1, const Segment& s2) {\n if(ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) > 0) return false;\n return ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.push_back(l2.a);\n } else {\n res.push_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nvector<Point> intersection_ss(const Segment& s1, const Segment& s2) {\n return is_intersect_ss(s1, s2) ? intersection_ll(Line(s1), Line(s2)) : vector<Point>();\n}\nReal dist_lp(const Line& l, const Point& p) {\n return abs(p - projection(l, p));\n}\nReal dist_sp(const Segment& s, const Point& p) {\n Point h = projection(s, p);\n if(is_intersect_sp(s, h)) return abs(h - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\nReal dist_ll(const Line& l1, const Line& l2) {\n if(is_intersect_ll(l1, l2)) return 0.0;\n return dist_lp(l1, l2.a);\n}\nReal dist_ss(const Segment& s1, const Segment& s2) {\n if(is_intersect_ss(s1, s2)) return 0.0;\n return min({dist_sp(s1, s2.a), dist_sp(s1, s2.b), dist_sp(s2, s1.a), dist_sp(s2, s1.b)});\n}\nReal dist_ls(const Line& l, const Segment& s) {\n if(is_intersect_ls(l, s)) return 0.0;\n return min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\nReal dist_sl(const Segment& s, const Line& l) {\n return dist_ls(l, s);\n}\nstruct Point2 {\n Point p;\n int o, l;\n};\nint main(void) {\n int tc;\n cin >> tc;\n while(tc--) {\n Point a, b;\n cin >> a >> b;\n Segment c = {a, b};\n int n;\n cin >> n;\n vector<Segment> seg(n);\n vector<int> o(n), l(n);\n vector<Point2> intersection;\n rep(i, 0, n) {\n Point s, t;\n cin >> s >> t;\n seg[i] = {s, t};\n cin >> o[i] >> l[i];\n o[i] = 1 - o[i];\n vector<Point> inter = intersection_ss(c, seg[i]);\n if(inter.empty()) continue;\n intersection.push_back({inter[0], o[i], l[i]});\n }\n if(intersection.empty()) {\n cout << 0 << '\\n';\n continue;\n }\n auto comp = [&](const Point2& p, const Point2& q) -> bool {\n return abs(p.p - a) < abs(q.p - a);\n };\n sort(intersection.begin(), intersection.end(), comp);\n int ans = 0;\n int cur = intersection[0].l xor intersection[0].o;\n rep(i, 1, (int)intersection.size()) {\n int nex = intersection[i].l xor intersection[i].o;\n if(nex != cur) {\n ans++;\n cur = nex;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3636, "score_of_the_acc": -1.404, "final_rank": 10 }, { "submission_id": "aoj_2003_8495849", "code_snippet": "#line 1 \"2003.test.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/2003\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Real.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp\"\n\n#include <cstdint>\n#include <cstddef>\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Real.hpp\"\n\n#include <cmath>\n#include <cassert>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nusing Real = long double;\nconstexpr Real EPS{1e-12};\n\nnamespace internal {\n\nconstexpr i32 negative{-1};\nconstexpr i32 zero{};\nconstexpr i32 positive{1};\n\n} // namespace internal\n\nconstexpr i32 Sign(Real value) {\n if (value < -EPS) return internal::negative;\n if (value > EPS) return internal::positive;\n return internal::zero;\n}\n\nconstexpr bool Zero(Real value) {\n return Sign(value) == internal::zero;\n}\n\nconstexpr bool Positive(Real value) {\n return Sign(value) == internal::positive;\n}\n\nconstexpr bool Negative(Real value) {\n return Sign(value) == internal::negative;\n}\n\nconstexpr bool Equal(Real a, Real b) {\n return Zero(a - b);\n}\n\nconstexpr bool Smaller(Real a, Real b) {\n return Negative(a - b);\n}\n\nconstexpr bool Bigger(Real a, Real b) {\n return Positive(a - b);\n}\n\nconstexpr Real Square(Real value) {\n return (Zero(value) ? value : value * value);\n}\n\nconstexpr Real Sqrt(Real value) {\n assert(!Negative(value));\n return (Zero(value) ? value : sqrtl(value));\n}\n\nconstexpr Real Abs(Real value) {\n return (Negative(value) ? -value : value);\n}\n\n} // namespace geometryR2\n \n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Segment.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nconstexpr Real PI{acosl(-1)};\nconstexpr Real TAU{static_cast<Real>(2) * PI};\n\nconstexpr Real ArcToRadian(Real arc) {\n return (arc * PI) / static_cast<Real>(180);\n}\n\nconstexpr Real RadianToArc(Real radian) {\n return (radian * static_cast<Real>(180)) / PI;\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n#include <iostream>\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Point {\nprivate:\n Real x_{}, y_{};\npublic:\n /* constructor */\n Point() = default;\n Point(Real x, Real y) : x_{x}, y_{y} {}\n\n /* getter, setter */\n Real x() const {\n return x_;\n }\n Real& x() {\n return x_;\n }\n Real y() const {\n return y_;\n }\n Real& y() {\n return y_;\n }\n\n /* operator */\n Point& operator+=(const Point& rhs) {\n x_ += rhs.x();\n y_ += rhs.y();\n return *this;\n }\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point{lhs} += rhs;\n }\n Point operator+() const {\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n x_ -= rhs.x();\n y_ -= rhs.y();\n return *this;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point{lhs} -= rhs;\n }\n Point operator-() const {\n return Point{} - *this;\n }\n Point& operator*=(Real k) {\n x_ *= k;\n y_ *= k;\n return *this;\n }\n friend Point operator*(Real k, const Point& p) {\n return Point{p} *= k;\n }\n friend Point operator*(const Point& p, Real k) {\n return Point{p} *= k;\n }\n Point& operator/=(Real k) {\n assert(!Zero(k));\n x_ /= k;\n y_ /= k;\n return *this;\n }\n friend Point operator/(Real k, const Point& p) {\n return Point{p} /= k;\n }\n friend Point operator/(const Point& p, Real k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& lhs, const Point& rhs) {\n return Equal(lhs.x(), rhs.x()) and Equal(lhs.y(), rhs.y());\n }\n friend bool operator!=(const Point& lhs, const Point& rhs) {\n return !Equal(lhs.x(), rhs.x()) or !Equal(lhs.y(), rhs.y());\n }\n friend bool operator<(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and Smaller(lhs.y(), rhs.y()));\n }\n friend bool operator<=(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and (Smaller(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend bool operator>(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and Bigger(lhs.y(), rhs.y()));\n }\n friend bool operator>=(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and (Bigger(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x_ >> p.y_;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x_ << ',' << p.y_ << ')';\n return os;\n }\n \n /* member function */\n Real normSquare() const {\n return Square(x_) + Square(y_);\n }\n Real norm() const {\n return Sqrt(normSquare());\n }\n void normalize() {\n assert((*this) != Point{});\n (*this) /= norm(); \n }\n Point normalized() const {\n Point res{*this};\n res.normalize();\n return res;\n }\n Point rotated(Real radian) const {\n return Point{\n x_ * cosl(radian) - y_ * sinl(radian),\n x_ * sinl(radian) + y_ * cosl(radian)\n };\n }\n void rotate(Real radian) {\n *this = rotated(radian); \n }\n Point rotatedByArc(Real arc) const {\n return rotated(ArcToRadian(arc));\n }\n void rotateByArc(Real arc) {\n *this = rotatedByArc(arc);\n }\n Real argument() const {\n return (Negative(y_) ? TAU : static_cast<Real>(0)) + atan2l(y_, x_);\n }\n Real argumentByArc() const {\n return RadianToArc(argument());\n }\n\n /* friend function */\n friend Real Dot(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.x() + lhs.y() * rhs.y();\n }\n friend Real Cross(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.y() - lhs.y() * rhs.x();\n }\n friend Real Argument(const Point& lhs, const Point& rhs) {\n return rhs.argument() - lhs.argument();\n }\n friend bool ArgComp(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.argument(), rhs.argument());\n }\n};\n\nusing Vector = Point;\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Relation.hpp\"\n\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Relation.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nenum RELATION {\n // p0 -> p1 -> p2の順で直線上に並んでいる\n ONLINE_FRONT = -2,\n // (p1 - p0) -> (p2 - p0)が時計回りになっている\n CLOCKWISE,\n // p0 -> p2 -> p1の順で直線上に並んでいる\n ON_SEGMENT,\n // (p1 - p0) -> (p2 - p0)が反時計回りになっている\n COUNTER_CLOCKWISE,\n // p2 -> p0 -> p1、またはp1 -> p0 -> p2の順で直線上に並んでいる\n ONLINE_BACK\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (Smaller(a.normSquare(), b.normSquare())) return ONLINE_FRONT;\n return ON_SEGMENT;\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndPoint.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndPoint.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.norm();\n}\n\nReal DistanceSquare(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.normSquare();\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Segment.hpp\"\n\n#include <algorithm>\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Segment.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Segment {\nprivate:\n Point p0_{}, p1_{};\npublic:\n /* constructor */\n Segment() = default;\n Segment(const Point& p0, const Point& p1) : p0_{p0}, p1_{p1} {}\n Segment(Real x0, Real y0, Real x1, Real y1) : p0_{x0, y0}, p1_{x1, y1} {}\n\n /* getter setter */\n const Point& p0() const {\n return p0_;\n }\n Point& p0() {\n return p0_;\n }\n const Point& p1() const {\n return p1_;\n }\n Point& p1() {\n return p1_;\n }\n\n /* member function */\n bool valid() const {\n return p0_ != p1_;\n }\n bool straddle(const Segment& s) const {\n return Relation(p0_, p1_, s.p0()) * Relation(p0_, p1_, s.p1()) <= 0;\n }\n Real length() const {\n assert(valid());\n return Distance(p0_, p1_);\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndSegment.hpp\"\n\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndSegment.hpp\"\n\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndSegment.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p, const Segment& s) {\n assert(s.valid());\n if (Negative(Dot(s.p1() - s.p0(), p - s.p0()))) {\n return Distance(p, s.p0());\n }\n if (Negative(Dot(s.p0() - s.p1(), p - s.p1()))) {\n return Distance(p, s.p1());\n }\n return Abs(Cross(s.p1() - s.p0(), p - s.p0())) / s.length();\n}\n\nbool PointOnSegment(const Point& p, const Segment& s) {\n assert(s.valid());\n return Zero(Distance(p, s));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Intersect/SegmentAndSegment.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Intersect/SegmentAndSegment.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Intersect/SegmentAndSegment.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nbool Intersect(const Segment& s0, const Segment& s1) {\n assert(s0.valid());\n assert(s1.valid());\n return s0.straddle(s1) and s1.straddle(s0);\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/CrossPoint/SegmentAndSegment.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Parallel/SegmentAndSegment.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Parallel/SegmentAndSegment.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Parallel/SegmentAndSegment.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nbool Parallel(const Segment& s0, const Segment& s1) {\n assert(s0.valid());\n assert(s1.valid());\n return Zero(Cross(s0.p1() - s0.p0(), s1.p1() - s1.p0()));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/CrossPoint/SegmentAndSegment.hpp\"\n\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/CrossPoint/SegmentAndSegment.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nPoint CrossPoint(const Segment& s0, const Segment& s1) {\n assert(s0.valid());\n assert(s1.valid());\n assert(Intersect(s0, s1));\n if (Parallel(s0, s1)) {\n if (s0.p0() == s1.p0()) {\n if (Relation(s0.p0(), s0.p1(), s1.p1()) == ONLINE_BACK) {\n return s0.p0();\n }\n else {\n assert(false);\n }\n }\n else if (s0.p0() == s1.p1()) {\n if (Relation(s0.p0(), s0.p1(), s1.p0()) == ONLINE_BACK) {\n return s0.p0();\n }\n else {\n assert(false);\n }\n }\n else if (s0.p1() == s1.p0()) {\n if (Relation(s0.p1(), s0.p0(), s1.p1()) == ONLINE_BACK) {\n return s0.p1();\n }\n else {\n assert(false);\n }\n }\n else if (s0.p1() == s1.p1()) {\n if (Relation(s0.p1(), s0.p0(), s1.p0()) == ONLINE_BACK) {\n return s0.p1();\n }\n else {\n assert(false);\n }\n }\n else {\n assert(false);\n }\n }\n else {\n Vector base{s0.p1() - s0.p0()};\n Real baseNorm{base.norm()};\n Real r1{Abs(Cross(base, s1.p0() - s0.p0())) / baseNorm};\n Real r2{Abs(Cross(base, s1.p1() - s0.p0())) / baseNorm};\n return s1.p0() + (s1.p1() - s1.p0()) * (r1 / (r1 + r2));\n }\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 9 \"2003.test.cpp\"\n\n#line 12 \"2003.test.cpp\"\n#include <vector>\n\nusing namespace zawa::geometryR2;\n\nstruct item {\n Segment seg{};\n int have{};\n int h{};\n item() = default;\n};\n\nvoid solve() {\n Segment s;\n std::cin >> s.p0() >> s.p1(); \n int n; std::cin >> n;\n std::vector<item> a(n);\n for (auto& i : a) {\n std::cin >> i.seg.p0() >> i.seg.p1();\n std::cin >> i.have >> i.h;\n }\n std::vector<Point> cross(n);\n std::vector<int> index;\n for (int i{} ; i < n ; i++) {\n if (a[i].seg.valid()) {\n if (!Intersect(s, a[i].seg)) continue;\n cross[i] = CrossPoint(s, a[i].seg);\n index.emplace_back(i);\n }\n else {\n if (!PointOnSegment(a[i].seg.p0(), s)) continue;\n cross[i] = a[i].seg.p0();\n index.emplace_back(i);\n }\n }\n std::sort(index.begin(), index.end(), [&](int i, int j) -> bool {\n return Smaller(Distance(s.p0(), cross[i]), Distance(s.p0(), cross[j]));\n });\n int dp[2]{0,0};\n for (auto i : index) {\n int nxt[2]{100000,100000};\n if (a[i].have == 0) {\n if (a[i].h == 0) {\n nxt[1] = std::min(dp[0] + 1, dp[1]);\n }\n else {\n nxt[0] = std::min(dp[0], dp[1] + 1);\n }\n }\n else {\n if (a[i].h == 0) {\n nxt[0] = std::min(dp[0], dp[1] + 1);\n }\n else {\n nxt[1] = std::min(dp[0] + 1, dp[1]);\n }\n }\n dp[0] = nxt[0];\n dp[1] = nxt[1];\n }\n int ans{std::min(dp[0], dp[1])};\n std::cout << ans << '\\n';\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n int n; std::cin >> n;\n for (int i{} ; i < n ; i++) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3580, "score_of_the_acc": -1.3333, "final_rank": 9 } ]
aoj_2009_cpp
Problem D: Area Separation Mr. Yamada Springfield Tanaka は、国家区画整理事業局局長補佐代理という大任を任されていた。 現在、彼の国は大規模な区画整理の最中であり、この区画整理をスムーズに終わらせることができたならば彼の昇進は間違いなしであるとされている。 ところが、そんな彼の出世を快く思わない者も多くいるのだ。 そんな人間の 1 人に、Mr.Sato Seabreeze Suzuki がいた。 彼は、ことあるごとに Mr. Yamada の足を引っ張ろうと画策してきた。 今回も Mr. Sato は足を引っ張るために、実際の区画整理を担当している組織に圧力をかけて、区画整理の結果を非常に分かりにくいものにしてしまった。 そのため、Mr. Yamada に渡された結果は、 ある正方形の土地をどの直線で分割したかという情報のみになっていた。 最低限、その正方形の土地がいくつに分割されたかだけでも分からなければ、Mr. Yamada は昇進どころか解雇されること間違いなしである。 あなたの仕事は、(-100,-100)、(100,-100)、(100,100)、(-100,100) を頂点とする正方形領域が、与えられた n 本の直線によっていくつに分割されているかを調べるプログラムを書いて、Mr. Yamada を解雇の危機から救うことである。 Input 入力は複数のテストケースからなる。 それぞれのテストケースの最初の行では、直線数を表す整数 n が与えられる(1 <= n <= 100)。 その後の n 行にはそれぞれ 4 つの整数 x 1 、 y 1 、 x 2 、 y 2 が含まれる。 これらの整数は直線上の相異なる 2 点 ( x 1 , y 1 ) と ( x 2 , y 2 ) を表す。 与えられる 2 点は常に正方形の辺上の点であることが保証されている。 与えられる n 本の直線は互いに異なり、直線同士が重なることはない。 また、直線が正方形の辺と重なることもない。 入力の終了は n = 0 で表される。 Output それぞれのテストケースについて、 n 本の直線によって分割された領域の数を 1 行で出力せよ。 なお、距離が 10 -10 未満の 2 点は一致するとみなしてよい。 また、|PQ| < 10 -10 、|QR| < 10 -10 でかつ |PR| >= 10 -10 となるような交点の組 P、Q、R は存在しない。 Sample Input 2 -100 -20 100 20 -20 -100 20 100 2 -100 -20 -20 -100 20 100 100 20 0 Output for the Sample Input 4 3
[ { "submission_id": "aoj_2009_10854083", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\ntypedef complex<double> P;\nconst double EPS = 1e-12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\ndouble cross(P a, P b) {\n\treturn (conj(a)*b).imag();\n}\n\nP crosspointLL(P a1, P a2, P b1, P b2) {\n\tdouble d1 = cross(b2 - b1, b1 - a1);\n\tdouble d2 = cross(b2 - b1, a2 - a1);\n\tassert(!(EQ(d1, 0) && EQ(d2, 0)));\n\tif (EQ(d2, 0)) return P(-1000, -1000);\n\treturn a1 + d1 / d2 * (a2 - a1);\n}\n\nint main() {\n\twhile(true) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tvector<P> line_start(n), line_end(n);\n\t\tfor (int i = 0; i<n; i++) {\n\t\t\tint x1, y1, x2, y2;\n\t\t\tcin >> x1 >> y1 >> x2 >> y2;\n\t\t\tline_start[i] = P(x1, y1);\n\t\t\tline_end[i] = P(x2, y2);\n\t\t}\n\n\t\tint ans = 1;\n\t\tfor (int i = 0; i<n; i++) {\n\t\t\tvector<P> cross_points;\n\t\t\tfor (int j = 0; j<i; j++) {\n\t\t\t\tP cp = crosspointLL(line_start[i], line_end[i], line_start[j], line_end[j]);\n\t\t\t\tif (cp.real() < -100 + EPS || cp.real() + EPS > 100 || cp.imag() < -100 + EPS || cp.imag() + EPS > 100) continue;\n\t\t\t\tbool exist = false;\n\t\t\t\tfor (auto p : cross_points) {\n\t\t\t\t\tif (EQ(cp, p)) {\n\t\t\t\t\t\texist = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!exist) {\n\t\t\t\t\tcross_points.push_back(cp);\n\t\t\t\t}\n\t\t\t}\n\t\t\tans += 1 + cross_points.size();\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3348, "score_of_the_acc": -0.1852, "final_rank": 15 }, { "submission_id": "aoj_2009_9626418", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef pair<ll,ll> pi;\n#define FOR(i,l,r) for(auto i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define F first\n#define S second\npair<ld,ld>intersection(pi x,pi y,pi z,pi w){\n ll a=x.S-y.S,b=y.F-x.F,p=a*x.F+b*x.S;\n ll c=z.S-w.S,d=w.F-z.F,q=c*z.F+d*z.S;\n ll det=a*d-b*c;\n if(!det)return make_pair(-1000,-1000);\n return make_pair((ld)(d*p-b*q)/det,(ld)(-c*p+a*q)/det);\n}\nint main(){\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n vector<pair<pi,pi>>A(N);\n REP(i,N)cin>>A[i].F.F>>A[i].F.S>>A[i].S.F>>A[i].S.S;\n ll ans=1;\n REP(i,N){\n set<pair<ld,ld>>P;\n REP(j,i){\n auto p=intersection(A[j].F,A[j].S,A[i].F,A[i].S);\n if(abs(p.F)>=100||abs(p.S)>=100)continue;\n P.insert(p);\n }\n ans+=P.size()+1;\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3360, "score_of_the_acc": -0.093, "final_rank": 9 }, { "submission_id": "aoj_2009_9475689", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nusing T = double;\nconst T eps = 1e-12;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, .0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, .0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * 2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), .0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), .0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return .0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, 0.) and eq(d2, 0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / 2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return .0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = .0;\n if (eq(f, .0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = .0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return .0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / 2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / 2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / 2.);\n res += b.r * b.r * (bth - sin(bth * 2) / 2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), .0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / 2., (a + b) / 2. + (b - a) * Point(0, 1));\n Line l2((b + c) / 2., (b + c) / 2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, 0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, 1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<Point> VP1, VP2;\n vector<Segment> SL;\n vector<Line> L;\n rep(i,0,N) {\n double A, B, C, D;\n cin >> A >> B >> C >> D;\n Point P1(A,B), P2(C,D);\n VP1.push_back(P1), VP2.push_back(P2);\n Segment l(P1,P2);\n SL.push_back(l);\n Line ll(P1,P2);\n L.push_back(ll);\n }\n int ANS = 1;\n rep(i,0,N) {\n vector<pair<double,double>> VD;\n VD.push_back({VP1[i].real(),VP1[i].imag()});\n VD.push_back({VP2[i].real(),VP2[i].imag()});\n rep(j,0,i) {\n if (!isIntersect(SL[i],SL[j])) continue;\n Point PP = Intersection(L[i],L[j]);\n VD.push_back({PP.real(),PP.imag()});\n }\n UNIQUE(VD);\n ANS += (int)(VD.size()) - 1;\n rep(j,0,(int)(VD.size())-1) {\n if ((VD[j].first-VD[j+1].first)*(VD[j].first-VD[j+1].first)+(VD[j].second-VD[j+1].second)*(VD[j].second-VD[j+1].second)<1e-10) ANS--;\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3568, "score_of_the_acc": -0.1444, "final_rank": 14 }, { "submission_id": "aoj_2009_9365049", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n#define rrep(i,start,end) for (long long i = start;i >= (long long)(end);i--)\n#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)\n#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)\n#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef vector<long long> vll;\ntypedef vector<pair<long long ,long long>> vpll;\ntypedef vector<vector<long long>> vvll;\ntypedef set<ll> sll;\ntypedef map<long long , long long> mpll;\ntypedef pair<long long ,long long> pll;\ntypedef tuple<long long , long long , long long> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (int)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\n// for AOJ or ICPC or etc..\n// 全部が0だったらtrueを返す\ntemplate<class Tp> bool zero (const Tp &x) {return x == 0;}\ntemplate<class Tp, class... Args> bool zero (const Tp &x, const Args& ...args) {return zero(x) and zero(args...);}\n\n\n\n//参考 https://github.com/saphmitchy/deliair-lib\n// 型名\n// R:Real, P:Point, L:Line, S:Segment, C:Circle, VP:vector<Point>\n\n#define X(p) real(p)\n#define Y(p) imag(p)\n\nusing R = ld;\nusing P = complex<R>;\nusing VP = vector<P>;\n\n//const R EPS = 1e-9; // ここは適宜調節する,いつものやつから消す\nconst R pi = acos(-1.0);\n\nint sgn(R a) {\n return (a < -EPS) ? -1 : (a > EPS) ? 1 : 0;\n} // 符号関数\n\nbool eq(R a, R b) {//実数の一致判定\n return sgn(b - a) == 0;\n}\n\nP operator*(P p, R d) {//ベクトルのd倍\n return P(X(p) * d, Y(p) * d);\n}\n\nP operator/(P p, R d) {//ベクトルの1/d倍\n return p * (1 / d);\n}\n\nistream &operator>>(istream &is, P &p) {\n // R a, b; // 入力が小数\n int a, b; // 入力が整数\n is >> a >> b;\n p = P(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, P p) {\n return os << X(p) << ' ' << Y(p);\n}\n\nR getarg(P b,P a){//ベクトルbはベクトルaを何radian回転させる必要があるか\n assert(sgn(abs(a)) != 0);//長さが0はだめ\n return arg(b/a);\n}\n\nbool cp_x(P p, P q) {//ベクトルの比較x軸で比較->y軸で比較\n if (!eq(X(p), X(q)))\n return X(p) < X(q);\n return Y(p) < Y(q);\n}\n\nbool cp_y(P p, P q) {//ベクトルの比較y軸で比較->x軸で比較\n if (!eq(Y(p), Y(q)))\n return Y(p) < Y(q);\n return X(p) < X(q);\n}\n\nstruct L {//直線ab\n P a, b;\n L() {}\n L(P a, P b) : a(a), b(b) {}\n\n // 入出力(必要なら)\n friend ostream &operator<<(ostream &os, L &l) {\n return os << l.a << ' ' << l.b;\n }\n friend istream &operator>>(istream &is, L &l) {\n return is >> l.a >> l.b;\n }\n};\n\nstruct S : L {//線分ab\n S() {}\n S(P a, P b) : L(a, b) {}\n};\n\nstruct C {//中心p 半径rの円\n P p;\n R r;\n C() {}\n C(P p, R r) : p(p), r(r) {}\n};\n\nP rot(P p, R t) {//ベクトルの回転\n return p * P(cos(t), sin(t));\n}\n\n//2つのベクトルの内積\nR dot(P p, P q) {\n return X(p) * X(q) + Y(p) * Y(q);\n}\n\n//2つのベクトルの外積\nR det(P p, P q) {\n return X(p) * Y(q) - Y(p) * X(q);\n}\n\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=jp\nint ccw(P a, P b, P c) { // 線分 ab に対する c の位置関係 \n b -= a, c -= a;//ベクトルab,ベクトルacにした\n if (sgn(det(b, c)) == 1)//外積右ねじ正\n return +1; // COUNTER_CLOCKWISE a,b,cが反時計回り\n if (sgn(det(b, c)) == -1)\n return -1; // CLOCKWISE\n if (dot(b, c) < 0.0)\n return +2; // ONLINE_BACK\n if (norm(b) < norm(c))\n return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool para(L a, L b) { // 平行判定\n return eq(det(a.b - a.a, b.b - b.a), 0.0);\n}\n\nbool orth(L a, L b) { // 垂直判定\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\n\nP proj(L l, P p) { // 垂線の足\n R t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\n\n// これいる?\n// P proj(S s, P p) {\n// R t = dot(p - s.a, s.b - s.a) / norm(s.b - s.a);\n// return s.a + (s.b - s.a) * t;\n// }\n\nP refl(L l, P p) { // 線対称の位置にある点\n return p + (proj(l, p) - p) * 2.0;\n}\n\nbool inter(L l, P p) { // 交点を持つか判定\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\nbool inter(S s, P p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool inter(L l, L m) {\n if (!eq(det(l.b - l.a, m.b - m.a), 0.0))\n return true;\n return eq(det(l.b - l.a, m.b - l.a), 0.0);\n}\n\nbool inter(L l, S s) {\n return sgn(det(l.b - l.a, s.a - l.a) * det(l.b - l.a, s.b - l.a)) <= 0;\n}\n\nbool inter(S s, L l) {\n return inter(l, s);\n}\n\nbool inter(S s, S t) {\n if (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) > 0)\n return false;\n return ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nR dist(P p, P q) {\n return abs(q - p);\n}\n\nR dist(L l, P p) {\n return abs(p - P(proj(l, p)));\n}\n\nR dist(S s, P p) {\n P h = proj(s, p);\n if (inter(s, h))\n return abs(h - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\nR dist(L l, L m) {\n return inter(l, m) ? 0.0 : dist(l, m.a);\n}\n\nR dist(S s, S t) {\n if (inter(s, t))\n return 0.0;\n return min({dist(s, t.a), dist(s, t.b), dist(t, s.a), dist(t, s.b)});\n}\n\nR dist(L l, S s) {\n if (inter(l, s))\n return 0.0;\n return min(dist(l, s.a), dist(l, s.b));\n}\n\nR dist(S s, L l) {\n return dist(l, s);\n}\n\nbool inter(C c, L l) {\n return sgn(c.r - dist(l, c.p)) >= 0;\n}\n\nbool inter(C c, P p) {\n return eq(abs(p - c.p), c.r);\n}\n\n// 共通接線の本数\n// 交点なし:4\n// 外接:3\n// 2点で交わる:2\n// 内接:1\n// 一方がもう一方を内包:0\nint inter(C c1, C c2) {\n if (c1.r < c2.r)\n swap(c1, c2);\n R d = abs(c1.p - c2.p);\n int a = sgn(d - c1.r - c2.r);\n if (a >= 0)\n return 3 + a;\n return 1 + sgn(d - c1.r + c2.r);\n}\n\nVP crosspoint(L l, L m) {\n VP ret;\n if (!inter(l, m))\n return ret;\n R A = det(l.b - l.a, m.b - m.a);\n R B = det(l.b - l.a, l.b - m.a);\n if (eq(A, 0.0) && eq(B, 0.0)) {\n ret.emplace_back(m.a);\n } else {\n ret.emplace_back(m.a + (m.b - m.a) * B / A);\n }\n return ret;\n}\n\nVP crosspoint(S s, S t) {\n return inter(s, t) ? crosspoint(L(s), L(t)) : VP();\n}\n\nVP crosspoint(C c, L l) {//円と直線の交点\n P h = proj(l, c.p);\n P e = (l.b - l.a) / abs(l.b - l.a);\n VP ret;\n if (!inter(c, l))\n return ret;\n if (eq(dist(l, c.p), c.r)) {\n ret.emplace_back(h);\n } else {\n R b = sqrt(c.r * c.r - norm(h - c.p));\n ret.push_back(h + e * b), ret.push_back(h - e * b);\n }\n return ret;\n}\n\nVP crosspoint(C c, S s) {//円と線分の交点\n P h = proj(s, c.p);\n P e = (s.b - s.a) / abs(s.b - s.a);\n VP ret;\n if (!inter(c, s))\n return ret;\n if (eq(dist(s, c.p), c.r)) {\n ret.emplace_back(h);\n } else {\n R b = sqrt(c.r * c.r - norm(h - c.p));\n if(ccw(s.a,s.b,h - e * b) == 0){//s.aに近い方から線分上なら追加する\n ret.push_back(h - e * b);\n }\n if(ccw(s.a,s.b,h + e * b)==0){\n ret.push_back(h + e * b);\n }\n }\n return ret;\n}\n\nVP crosspoint(C c1, C c2) {//2つの円の交わる点\n R d = abs(c1.p - c2.p);\n R a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n R t = atan2(Y(c2.p) - Y(c1.p), X(c2.p) - X(c1.p));\n VP ret;\n if (inter(c1, c2) % 4 == 0) // 交わらないとき\n return ret;\n if (eq(a, 0.0)) {\n ret.emplace_back(P(c1.p + rot(P(c1.r, 0.0), t)));\n } else {\n P p1 = c1.p + rot(P(c1.r, 0.0), t + a);\n P p2 = c1.p + rot(P(c1.r, 0.0), t - a);\n ret.emplace_back(p1), ret.emplace_back(p2);\n }\n return ret;\n}\n\nVP cut(VP p, L l, bool border = true) { // 直線が多角形に切り取られる区間\n int n = sz(p);\n p.emplace_back(p[0]), p.emplace_back(p[1]);\n VP ret;\n rep(i, n) {\n if (!eq(dist(l, p[i]), 0) && !eq(dist(l, p[i + 1]), 0)) {\n S s(p[i], p[i + 1]);\n if (eq(dist(l, s), 0)) {\n auto res = crosspoint(l, s);\n ret.emplace_back(res[0]);\n }\n }\n if (eq(dist(l, p[i + 1]), 0)) {\n if ((eq(dist(l, p[i]), 0) || eq(dist(l, p[i + 2]), 0)) && !border)\n continue;\n S s(p[i], p[i + 2]);\n if (eq(dist(l, s), 0))\n ret.emplace_back(p[i + 1]);\n }\n }\n return ret;\n}\n\nVP rectangle(S s, R r) { // sを軸とした幅rの長方形\n P d = (s.a - s.b) * P(0, 1);\n d *= r / sqrt(norm(d));\n return VP{s.a + d, s.a - d, s.b - d, s.b + d};\n}\n\nL vertical_bisector(P p, P q) { // 垂直二等分線\n L l;\n l.a = (p + q) * 0.5;\n l.b = l.a + rot(q - p, pi * 0.5);\n return l;\n}\n\nL angle_bisector(P a,P b,P c){//角abcの二等分線(角bの2等分線)\n L l;\n l.a = b;\n R ang = atan2(Y(c-b),X(c-b)) - atan2(Y(a-b),X(a-b));//なす角\n ang/=2.0;\n l.b = l.a + rot(a-b,ang);\n return l;\n}\n\nC Apollonius(P p, P q, R a, R b) { // アポロニウスの円\n P p1 = (p * b + q * a) / (a + b), p2 = (-p * b + q * a) / (a - b);\n C c;\n c.p = (p1 + p2) * 0.5;\n c.r = abs(p1 - p2) * 0.5;\n return c;\n}\n\nR area(VP p) { // 多角形の面積\n R ret = 0.0;\n int n = sz(p);\n rep(i, n) ret += det(p[i], p[(i + 1) % n]);\n return abs(ret * 0.5);\n}\n\nint in_polygon(VP p, P q) { // IN:2, ON:1, OUT:0\n int n = sz(p);\n int ret = 0;\n rep(i, n) {\n P a = p[i] - q, b = p[(i + 1) % n] - q;\n if (eq(det(a, b), 0.0) && sgn(dot(a, b)) <= 0)\n return 1;\n if (Y(a) > Y(b))\n swap(a, b);\n if (sgn(Y(a)) <= 0 && sgn(Y(b)) == 1 && sgn(det(a, b)) == 1)\n ret ^= 2;\n }\n return ret;\n}\n\nVP tangent(C c, P p) { // 点 p を通る円 c の接線と c の接点\n return crosspoint(c, C(p, sqrt(norm(p - c.p) - c.r * c.r)));\n}\n\nvector<L> tangent(C c1, C c2) { // 共通接線\n vector<L> ret;\n if (c1.r < c2.r)\n swap(c1, c2);\n R r = abs(c2.p - c1.p);\n if (eq(r, 0.0))\n return ret;\n P u = (c2.p - c1.p) / r;\n P v = rot(u, pi * 0.5);\n for (R s : {1.0, -1.0}) {\n R h = (c1.r + c2.r * s) / r;\n if (eq(abs(h), 1.0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if (abs(h) < 1.0) {\n P uu = u * h, vv = v * sqrt(1.0 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\nVP convex_hull(VP p) { // 凸包\n sort(all(p), cp_x);\n p.erase(unique(all(p)), end(p));\n int n = sz(p), k = 0;\n if (n == 1)\n return p;\n VP ch(2 * n);\n for (int i = 0; i < n; ch[k++] = p[i++]) {\n while (k >= 2 && sgn(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) <= 0)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while (k >= t && sgn(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) <= 0)\n k--;\n }\n ch.resize(k - 1);\n return ch;\n}\n\nR closest_pair(VP p) { // 最近点対の距離\n if (sz(p) <= 1)\n return 1e18;\n sort(all(p), cp_x);\n VP memo(sz(p));\n\n function<R(int, int)> rec = [&](int l, int r) {\n if (r - l <= 1)\n return R(1e18);\n int m = (l + r) >> 1;\n R x = X(p[m]);\n R ret = min(rec(l, m), rec(m, r));\n inplace_merge(p.begin() + l, p.begin() + m, p.begin() + r, cp_y);\n int cnt = 0;\n reps(i, l, r) {\n if (abs(X(p[i]) - x) >= ret)\n continue;\n rep(j, cnt) {\n P d = p[i] - memo[cnt - j - 1];\n if (Y(d) >= ret)\n break;\n chmin(ret, abs(d));\n }\n memo[cnt++] = p[i];\n }\n return ret;\n };\n\n return rec(0, sz(p));\n}\n\nR farthest_pair(VP p) {//最遠点対の距離\n VP ps = convex_hull(p);\n ll n = ps.size();\n if(n == 2){//凸包が潰れてる\n return dist(ps[0],ps[1]);\n }\n ll i = 0,j = 0;\n rep(k,n){//x軸方向に最も遠い点対を求める\n if(cp_x(ps[k],ps[i]))i = k;\n if(cp_x(ps[j],ps[k]))j = k;\n }\n R ret = 0;\n ll si = i,sj = j;\n while(i != sj || j != si){//180度反転しきるまで\n ret = max(ret,dist(ps[i],ps[j]));\n if(det(ps[(i+1)%n] - ps[i],ps[(j+1)%n]-ps[j]) < 0){\n i = (i + 1) % n;\n }else{\n j = (j + 1) % n;\n }\n }\n return ret;\n}\n\n// 原点, 点 a, 点 b とで囲まれる領域の面積 (三角形 ver と扇型 ver)\nR calc_element(P a, P b, R cr,bool triangle){\n if(triangle)return det(a,b)/2;\n else{\n P tmp = b * (P(X(a),-Y(a)));\n R ang = atan2(Y(tmp),X(tmp));\n return cr * cr * ang/2;\n }\n}\n\n// 円 C と、三角形 ((0, 0), ia, ib) との共通部分の面積\nR common_area(C c, P ia,P ib){\n P a = ia - c.p , b = ib - c.p;\n if(eq(abs(a-b),0))return 0;\n bool isin_a = (sgn(c.r - abs(a))>= 0);\n bool isin_b = (sgn(c.r - abs(b))>= 0);\n if(isin_a && isin_b)return calc_element(a,b,c.r,true);//aもbも円の中\n\n C oc(P(0,0),c.r);\n S seg(a,b);\n VP cr = crosspoint(oc,seg);\n if(cr.empty())return calc_element(a,b,c.r,false);\n P s = cr[0],t = cr.back();\n return calc_element(s,t,c.r,true) + calc_element(a,s,c.r,isin_a) + calc_element(t,b,c.r,isin_b);\n\n}\n\n\nR common_area(C c, VP vp){// 円cと多角形の共通部分の面積\n R ret = 0;\n ll n = vp.size();\n rep(i,n){\n ret += common_area(c,vp[i],vp[(i+1)%n]);\n }\n return ret;\n}\n\nR common_area(C p, C q) {// 円と円の共通部分の面積\n R d = abs(p.p - q.p);\n if (d >= p.r + q.r - EPS) return 0;\n else if (d <= abs(p.r - q.r) + EPS) return min(p.r, q.r) * min(p.r, q.r) * pi;\n R pcos = (p.r*p.r + d*d - q.r*q.r) / (p.r*d*2);\n R pang = acosl(pcos);\n R parea = p.r*p.r*pang - p.r*p.r*sin(pang*2)/2;\n R qcos = (q.r*q.r + d*d - p.r*p.r) / (q.r*d*2);\n R qang = acosl(qcos);\n R qarea = q.r*q.r*qang - q.r*q.r*sin(qang*2)/2;\n return parea + qarea;\n}\n\nvector<VP> divisions(vector<L> lf, R lim = 1e9) {\n vector<L> ls;\n each(l, lf) {\n bool ok = true;\n each(m, ls) {\n if (para(l, m) & inter(l, m.a)) {\n ok = false;\n break;\n }\n }\n if (ok)\n ls.emplace_back(l);\n }\n VP lc{P(-lim, -lim), P(lim, -lim), P(lim, lim), P(-lim, lim)};\n rep(i, 4) ls.emplace_back(lc[i], lc[(i + 1) % 4]);\n int m = sz(ls);\n VP ps;\n vector<vector<int>> lp(m);\n rep(i, m) {\n reps(j, i + 1, m) {\n each(p, crosspoint(ls[i], ls[j])) {\n if (max(abs(X(p)), abs(Y(p))) < lim + EPS) {\n lp[i].emplace_back(sz(ps)), lp[j].emplace_back(sz(ps));\n ps.emplace_back(p);\n }\n }\n }\n }\n int n = sz(ps);\n vector<int> id(n, -1), to;\n vector<R> rg;\n vector<vector<pair<R, int>>> li(n);\n rep(i, m) {\n sort(all(lp[i]), [&ps](int a, int b) { return cp_x(ps[a], ps[b]); });\n vector<int> q;\n rep(j, sz(lp[i])) {\n int me = id[lp[i][j]], st = j;\n auto np = ps[lp[i][j]];\n while (j + 1 < sz(lp[i])) {\n if (abs(ps[lp[i][j + 1]] - np) < EPS) {\n j++;\n if (id[lp[i][j]] != -1)\n me = id[lp[i][j]];\n } else\n break;\n }\n if (me == -1)\n me = lp[i][st];\n reps(k, st, j + 1) id[lp[i][k]] = me;\n q.emplace_back(me);\n }\n rep(i, sz(q) - 1) {\n P d = ps[q[i + 1]] - ps[q[i]];\n R s = atan2(Y(d), X(d)), t = atan2(-Y(d), -X(d));\n int x = q[i], y = q[i + 1];\n li[x].emplace_back(s, sz(to));\n li[x].emplace_back(s + pi * 2, sz(to));\n to.emplace_back(y), rg.emplace_back(t);\n li[y].emplace_back(t, sz(to));\n li[y].emplace_back(t + pi * 2, sz(to));\n to.emplace_back(x), rg.emplace_back(s);\n }\n }\n rep(i, n) sort(all(li[i]));\n vector<bool> u(sz(to), false);\n vector<VP> ret;\n rep(i, n) {\n each(l, li[i]) {\n int ns = l.second;\n if (u[ns])\n continue;\n VP nv;\n int no = ns;\n bool ok = true;\n while (1) {\n if (sz(nv) > 1) {\n P x = nv[sz(nv) - 2], y = nv[sz(nv) - 1], z = ps[to[no]];\n int c = ccw(x, y, z);\n if (c == 1)\n ok = false;\n if (c != -1)\n nv.pop_back();\n }\n nv.emplace_back(ps[to[no]]);\n u[no] = true;\n no = upper_bound(all(li[to[no]]), pair(rg[no] + EPS, -1))->second;\n if (no == ns)\n break;\n }\n if (ok)\n ret.emplace_back(nv);\n }\n }\n return ret;\n}\n//ref https://github.com/drken1215/algorithm/blob/master/Geometry/arg_sort.cpp\n//verify https://atcoder.jp/contests/abc139/submissions/me\n//点列を偏角ソート\nvoid arg_sort(VP &v){\n //原点=0,(pi,2pi] = -1 (0pi,pi] = 1\n auto sign = [&](const P &p){\n if(sgn(X(p)) == 0 && sgn(Y(p)) == 0){\n return 0;\n }else if(sgn(Y(p)) == -1 || (sgn(Y(p)) == 0 && sgn(X(p)) == 1)){\n return -1;\n }else{\n return 1;\n }\n };\n auto cp = [&](const P &p,const P &q){\n if(sign(p) != sign(q)){\n return sign(p) < sign(q);\n }else{//外積>0で判定\n //同じ向きのときは未定義必要に応じて決める\n return X(p) * Y(q) - Y(p) * X(q) > 0;\n }\n };\n sort(v.begin(),v.end(),cp);\n}\n\n\n// 変数をちゃんと全部受け取る!\nvoid solve(ll n){\n vector<L> vl;\n rep(i,n){\n LL(x1,y1,x2,y2);\n L l(P(x1,y1),P(x2,y2));\n vl.push_back(l);\n }\n P p1(-100,-100),p2(100,-100),p3(100,100),p4(-100,100);\n L l1(p1,p2),l2(p2,p3),l3(p3,p4),l4(p4,p1);\n vl.push_back(l1);vl.push_back(l2);vl.push_back(l3);vl.push_back(l4);\n\n vector<VP> div = divisions(vl,1e10);\n VP poli = {p1,p2,p3,p4};\n ll cnt = 0;\n rep(i,div.size()){\n bool is_in = true;\n each(p,div[i]){\n if(in_polygon(poli,p) == 0){\n is_in = false;\n }\n }\n if(is_in)cnt++;\n \n }\n cout << cnt << endl;\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n);//変数数調整\n if(zero(n))break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 7028, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2009_9339754", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nconst double EPS = 1e-10;\nconst double PI = asinl(1) * 2;\n\nstruct Point{\n double x,y;\n Point(){}\n Point(double x,double y):x(x),y(y){}\n Point operator+(Point p) {return Point(x+p.x,y+p.y);}\n Point operator-(Point p) {return Point(x-p.x,y-p.y);}\n Point operator*(double k){return Point(x*k,y*k);}\n Point operator/(double k){return Point(x/k,y/k);}\n double norm(){return x*x+y*y;}\n double abs(){return sqrt(norm());}\n\n bool operator<(const Point &p) const{\n return x!=p.x?x<p.x:y<p.y;\n //grid-point only\n //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0;\n }\n\n bool operator==(const Point &p) const{\n return fabs(x-p.x)<EPS and fabs(y-p.y)<EPS;\n }\n};\n\nbool sort_x(Point a,Point b){\n return a.x!=b.x?a.x<b.x:a.y<b.y;\n}\n\nbool sort_y(Point a,Point b){\n return a.y!=b.y?a.y<b.y:a.x<b.x;\n}\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Segment{\n Point p1,p2;\n Segment(){}\n Segment(Point p1, Point p2):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nstruct Circle{\n Point c;\n double r;\n Circle(){}\n Circle(Point c,double r):c(c),r(r){}\n};\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nPoint orth(Point p){return Point(-p.y,p.x);}\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\ndouble arg(Vector p){\n return atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n return Point(cos(r)*a,sin(r)*a);\n}\n\n// COUNTER CLOCKWISE\nstatic const int CCW_COUNTER_CLOCKWISE = 1;\nstatic const int CCW_CLOCKWISE = -1;\nstatic const int CCW_ONLINE_BACK = 2;\nstatic const int CCW_ONLINE_FRONT = -2;\nstatic const int CCW_ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a = p1-p0;\n Vector b = p2-p0;\n if(cross(a,b) > EPS) return CCW_COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS) return CCW_CLOCKWISE;\n if(dot(a,b) < -EPS) return CCW_ONLINE_BACK;\n if(a.norm()<b.norm()) return CCW_ONLINE_FRONT;\n return CCW_ON_SEGMENT;\n}\n\nbool intersectSS(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 and\n ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0 );\n}\n\nbool intersectSS(Segment s1,Segment s2){\n return intersectSS(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool intersectPS(Polygon p,Segment l){\n int n=p.size();\n for(int i=0;i<n;++i)\n if(intersectSS(Segment(p[i],p[(i+1)%n]),l)) return 1;\n return 0;\n}\n\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistanceSS(Segment s1,Segment s2){\n if(intersectSS(s1,s2)) return 0.0;\n return min(\n min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),\n min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));\n}\n\n// intercsect of circles\nstatic const int ICC_SEPERATE = 4;\nstatic const int ICC_CIRCUMSCRIBE = 3;\nstatic const int ICC_INTERSECT = 2;\nstatic const int ICC_INSCRIBE = 1;\nstatic const int ICC_CONTAIN = 0;\n\nint intersectCC(Circle c1,Circle c2){\n if(c1.r<c2.r) swap(c1,c2);\n double d=abs(c1.c-c2.c);\n double r=c1.r+c2.r;\n if(equals(d,r)) return ICC_CIRCUMSCRIBE;\n if(d>r) return ICC_SEPERATE;\n if(equals(d+c2.r,c1.r)) return ICC_INSCRIBE;\n if(d+c2.r<c1.r) return ICC_CONTAIN;\n return ICC_INTERSECT;\n}\n\nbool intersectSC(Segment s,Circle c){\n return getDistanceSP(s,c.c)<=c.r;\n}\n\nint intersectCS(Circle c,Segment s){\n if(norm(project(s,c.c)-c.c)-c.r*c.r>EPS) return 0;\n double d1=abs(c.c-s.p1),d2=abs(c.c-s.p2);\n if(d1<c.r+EPS and d2<c.r+EPS) return 0;\n if((d1<c.r-EPS and d2>c.r+EPS)\n or (d1>c.r+EPS and d2<c.r-EPS)) return 1;\n Point h=project(s,c.c);\n if(dot(s.p1-h,s.p2-h)<0) return 2;\n return 0;\n}\n\nPoint getCrossPointSS(Segment s1,Segment s2){\n for(int k=0;k<2;++k){\n if(getDistanceSP(s1,s2.p1)<EPS) return s2.p1;\n if(getDistanceSP(s1,s2.p2)<EPS) return s2.p2;\n swap(s1,s2);\n }\n Vector base=s2.p2-s2.p1;\n double d1=fabs(cross(base,s1.p1-s2.p1));\n double d2=fabs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}\n\nPoint getCrossPointLL(Line l1,Line l2){\n double a=cross(l1.p2-l1.p1,l2.p2-l2.p1);\n double b=cross(l1.p2-l1.p1,l1.p2-l2.p1);\n if(fabs(a)<EPS and fabs(b)<EPS) return l2.p1;\n return l2.p1+(l2.p2-l2.p1)*(b/a);\n}\n\nPolygon getCrossPointCL(Circle c,Line l){\n Polygon ps;\n Point pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n if(equals(getDistanceLP(l,c.c),c.r)){\n ps.emplace_back(pr);\n return ps;\n }\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n ps.emplace_back(pr+e*base);\n ps.emplace_back(pr-e*base);\n return ps;\n}\n\nPolygon getCrossPointCS(Circle c,Segment s){\n Line l(s);\n Polygon res=getCrossPointCL(c,l);\n if(intersectCS(c,s)==2) return res;\n if(res.size()>1u){\n if(dot(l.p1-res[0],l.p2-res[0])>0) swap(res[0],res[1]);\n res.pop_back();\n }\n return res;\n}\n\n\nPolygon getCrossPointCC(Circle c1,Circle c2){\n Polygon p(2);\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n p[0]=c1.c+polar(c1.r,t+a);\n p[1]=c1.c+polar(c1.r,t-a);\n return p;\n}\n\n// IN:2 ON:1 OUT:0\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;++i){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.y>b.y) swap(a,b);\n if(a.y < EPS and EPS < b.y and cross(a,b) > EPS )\n x = !x;\n }\n return (x?2:0);\n}\n\n// BEGIN IGNORE\nPolygon andrewScan(Polygon s){\n Polygon u,l;\n if(s.size()<3) return s;\n sort(s.begin(),s.end());\n u.push_back(s[0]);\n u.push_back(s[1]);\n l.push_back(s[s.size()-1]);\n l.push_back(s[s.size()-2]);\n for(int i=2;i<(int)s.size();++i){\n for(int n=u.size();\n n>=2 and ccw(u[n-2],u[n-1],s[i])!=CCW_CLOCKWISE;\n n--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n for(int i=s.size()-3;i>=0;i--){\n for(int n=l.size();\n n>=2 and ccw(l[n-2],l[n-1],s[i])!=CCW_CLOCKWISE;\n n--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n reverse(l.begin(),l.end());\n for(int i=u.size()-2;i>=1;i--) l.push_back(u[i]);\n return l;\n}\n// END IGNORE\n\nPolygon convex_hull(Polygon ps){\n int n=ps.size();\n sort(ps.begin(),ps.end(),sort_y);\n int k=0;\n Polygon qs(n*2);\n for(int i=0;i<n;++i){\n while(k>1 and cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0)\n k--;\n qs[k++]=ps[i];\n }\n for(int i=n-2,t=k;i>=0;i--){\n while(k>t and cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0)\n k--;\n qs[k++]=ps[i];\n }\n qs.resize(k-1);\n return qs;\n}\n\ndouble diameter(Polygon s){\n Polygon p=s;\n int n=p.size();\n if(n==2) return abs(p[0]-p[1]);\n int i=0,j=0;\n for(int k=0;k<n;++k){\n if(p[i]<p[k]) i=k;\n if(!(p[j]<p[k])) j=k;\n }\n double res=0;\n int si=i,sj=j;\n while(i!=sj or j!=si){\n res=max(res,abs(p[i]-p[j]));\n if(cross(p[(i+1)%n]-p[i],p[(j+1)%n]-p[j])<0.0){\n i=(i+1)%n;\n }else{\n j=(j+1)%n;\n }\n }\n return res;\n}\n\nbool isConvex(Polygon p){\n bool f=1;\n int n=p.size();\n for(int i=0;i<n;++i){\n int t=ccw(p[(i+n-1)%n],p[i],p[(i+1)%n]);\n f&=t!=CCW_CLOCKWISE;\n }\n return f;\n}\n\ndouble area(Polygon s){\n double res=0;\n for(int i=0;i<(int)s.size();++i){\n res+=cross(s[i],s[(i+1)%s.size()])/2.0;\n }\n return res;\n}\n\ndouble area(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n if(c1.r+c2.r<=d+EPS) return 0;\n if(d<=fabs(c1.r-c2.r)){\n double r=min(c1.r,c2.r);\n return PI*r*r;\n }\n double res=0;\n for(int k=0;k<2;++k){\n double rc=(d*d+c1.r*c1.r-c2.r*c2.r)/(2*d*c1.r);\n double th=acosl(rc)*2;\n res+=(th-sinl(th))*c1.r*c1.r/2;\n swap(c1,c2);\n }\n return res;\n}\n\nPolygon convexCut(Polygon p,Line l){\n Polygon q;\n for(int i=0;i<(int)p.size();++i){\n Point a=p[i],b=p[(i+1)%p.size()];\n if(ccw(l.p1,l.p2,a)!=-1) q.push_back(a);\n if(ccw(l.p1,l.p2,a)*ccw(l.p1,l.p2,b)<0)\n q.push_back(getCrossPointLL(Line(a,b),l));\n }\n return q;\n}\n\nLine bisector(Point p1,Point p2){\n Circle c1=Circle(p1,abs(p1-p2)),c2=Circle(p2,abs(p1-p2));\n Polygon p=getCrossPointCC(c1,c2);\n if(cross(p2-p1,p[0]-p1)>0) swap(p[0],p[1]);\n return Line(p[0],p[1]);\n}\n\nVector translate(Vector v,double theta){\n Vector res;\n res.x=cos(theta)*v.x-sin(theta)*v.y;\n res.y=sin(theta)*v.x+cos(theta)*v.y;\n return res;\n}\n\nvector<Line> corner(Line l1,Line l2){\n vector<Line> res;\n if(isParallel(l1,l2)){\n double d=getDistanceLP(l1,l2.p1)/2.0;\n Vector v1=l1.p2-l1.p1;\n v1=v1/v1.abs()*d;\n Point p=l2.p1+translate(v1,90.0*(PI/180.0));\n double d1=getDistanceLP(l1,p);\n double d2=getDistanceLP(l2,p);\n if(fabs(d1-d2)>d){\n p=l2.p1+translate(v1,-90.0*(PI/180.0));\n }\n res.push_back(Line(p,p+v1));\n }else{\n Point p=getCrossPointLL(l1,l2);\n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n v1=v1/v1.abs();\n v2=v2/v2.abs();\n res.push_back(Line(p,p+(v1+v2)));\n res.push_back(\n Line(p,p+translate(v1+v2,90.0*(PI/180.0))));\n }\n return res;\n}\n\nPolygon tangent(Circle c1,Point p2){\n Circle c2=Circle(p2,sqrt(norm(c1.c-p2)-c1.r*c1.r));\n Polygon p=getCrossPointCC(c1,c2);\n sort(p.begin(),p.end());\n return p;\n}\n\nvector<Line> tangent(Circle c1,Circle c2){\n vector<Line> ls;\n if(c1.r<c2.r) swap(c1,c2);\n double g=norm(c1.c-c2.c);\n if(equals(g,0)) return ls;\n Point u=(c2.c-c1.c)/sqrt(g);\n Point v=orth(u);\n for(int s=1;s>=-1;s-=2){\n double h=(c1.r+s*c2.r)/sqrt(g);\n if(equals(1-h*h,0)){\n ls.emplace_back(c1.c+u*c1.r,c1.c+(u+v)*c1.r);\n }else if(1-h*h>0){\n Point uu=u*h,vv=v*sqrt(1-h*h);\n ls.emplace_back(\n c1.c+(uu+vv)*c1.r,c2.c-(uu+vv)*c2.r*s);\n ls.emplace_back(\n c1.c+(uu-vv)*c1.r,c2.c-(uu-vv)*c2.r*s);\n }\n }\n\n return ls;\n}\n\ndouble closest_pair(Polygon &a,int l=0,int r=-1){\n if(r<0){\n r=a.size();\n sort(a.begin(),a.end(),sort_x);\n }\n if(r-l<=1) return abs(a[0]-a[1]);\n int m=(l+r)>>1;\n double x=a[m].x;\n double d=min(closest_pair(a,l,m),closest_pair(a,m,r));\n inplace_merge(a.begin()+l,a.begin()+m,a.begin()+r,sort_y);\n\n Polygon b;\n for(int i=l;i<r;++i){\n if(fabs(a[i].x-x)>=d) continue;\n for(int j=0;j<(int)b.size();++j){\n double dy=a[i].y-next(b.rbegin(),j)->y;\n if(dy>=d) break;\n d=min(d,abs(a[i]-*next(b.rbegin(),j)));\n }\n b.emplace_back(a[i]);\n }\n return d;\n}\n\nvector<vector<int>>\nsegmentArrangement(vector<Segment> &ss, Polygon &ps){\n int n=ss.size();\n for(int i=0;i<n;++i){\n ps.emplace_back(ss[i].p1);\n ps.emplace_back(ss[i].p2);\n for(int j=i+1;j<n;++j)\n if(intersectSS(ss[i],ss[j]))\n ps.emplace_back(getCrossPointSS(ss[i],ss[j]));\n }\n sort(ps.begin(),ps.end());\n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n\n vector<vector<int> > G(ps.size());\n for(int i=0;i<n;++i){\n vector<pair<double,int> > ls;\n for(int j=0;j<(int)ps.size();++j)\n if(getDistanceSP(ss[i],ps[j])<EPS)\n ls.emplace_back(make_pair(norm(ss[i].p1-ps[j]),j));\n\n sort(ls.begin(),ls.end());\n for(int j=0;j+1<(int)ls.size();++j){\n int a=ls[j].second,b=ls[j+1].second;\n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n }\n for(auto &v:G){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n }\n return G;\n}\n\nstruct EndPoint{\n Point p;\n int seg,st;\n EndPoint(){}\n EndPoint(Point p,int seg,int st):p(p),seg(seg),st(st){}\n bool operator<(const EndPoint &ep)const{\n if(p.y==ep.p.y) return st<ep.st;\n return p.y<ep.p.y;\n }\n};\n\nint manhattan_intersection(\n vector<Segment> ss,const int INF){\n const int BTM = 0;\n const int LFT = 1;\n const int RGH = 2;\n const int TOP = 3;\n\n int n=ss.size();\n vector<EndPoint> ep;\n for(int i=0;i<n;++i){\n if(ss[i].p1.y==ss[i].p2.y){\n if(ss[i].p1.x>ss[i].p2.x) swap(ss[i].p1,ss[i].p2);\n ep.emplace_back(ss[i].p1,i,LFT);\n ep.emplace_back(ss[i].p2,i,RGH);\n }else{\n if(ss[i].p1.y>ss[i].p2.y) swap(ss[i].p1,ss[i].p2);\n ep.emplace_back(ss[i].p1,i,BTM);\n ep.emplace_back(ss[i].p2,i,TOP);\n }\n }\n sort(ep.begin(),ep.end());\n\n set<int> bt;\n bt.insert(INF);\n\n int cnt=0;\n for(int i=0;i<n*2;++i){\n if(ep[i].st==TOP){\n bt.erase(ep[i].p.x);\n }else if(ep[i].st==BTM){\n bt.emplace(ep[i].p.x);\n }else if(ep[i].st==LFT){\n auto b=bt.lower_bound(ss[ep[i].seg].p1.x);\n auto e=bt.upper_bound(ss[ep[i].seg].p2.x);\n cnt+=distance(b,e);\n }\n }\n\n return cnt;\n}\n\ndouble area(Polygon ps,Circle c){\n if(ps.size()<3u) return 0;\n function<double(Circle, Point, Point)> dfs=\n [&](Circle c,Point a,Point b){\n Vector va=c.c-a,vb=c.c-b;\n double f=cross(va,vb),res=0;\n if(equals(f,0.0)) return res;\n if(max(abs(va),abs(vb))<c.r+EPS) return f;\n Vector d(dot(va,vb),cross(va,vb));\n if(getDistanceSP(Segment(a,b),c.c)>c.r-EPS)\n return c.r*c.r*atan2(d.y,d.x);\n auto u=getCrossPointCS(c,Segment(a,b));\n if(u.empty()) return res;\n if(u.size()>1u and dot(u[1]-u[0],a-u[0])>0)\n swap(u[0],u[1]);\n u.emplace(u.begin(),a);\n u.emplace_back(b);\n for(int i=1;i<(int)u.size();++i)\n res+=dfs(c,u[i-1],u[i]);\n return res;\n };\n double res=0;\n for(int i=0;i<(int)ps.size();++i)\n res+=dfs(c,ps[i],ps[(i+1)%ps.size()]);\n return res/2;\n}\n\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\nusing pii =pair<int,int>;\n\nconstexpr ll mod = 998244353;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint dX[4]={1,0,-1,0};\nint dY[4]={0,-1,0,1};\n\n\nusing Graph =vector<vector<ll>>;\n\n\nint main(){\n while (true){\n int n;\n cin>>n;\n if (n==0)break;\n vector<Segment> Segs;\n int ans=1;\n for (int i=0; i<n; i++){\n double x1,y1,x2,y2;\n cin>>x1>>y1>>x2>>y2;\n Point p1={x1,y1};\n Point p2={x2,y2};\n Segment s={p1,p2};\n\n vector<Point> vec;\n vec.push_back(p1);\n vec.push_back(p2);\n for (auto v: Segs){\n if (intersectSS(v,s)){\n vec.push_back(getCrossPointSS(v,s));\n\n }\n }\n int sans=vec.size()-1;\n sort(vec.begin(),vec.end(),[](auto& a, auto& b){\n if (a.x==b.x)return a.y<b.y;\n return a.x<b.x;});\n int cnt=1;\n for (int j=1; j<vec.size(); j++){\n if (vec[j]==vec[j-1]){\n cnt++;\n }\n else{\n sans-=cnt-1;\n cnt=1;\n }\n }\n sans-=cnt-1;\n\n ans+=sans;\n //cout<<' '<<ans<<endl;\n Segs.emplace_back(p1,p2);\n\n\n\n }\n cout<<ans<<endl;\n\n\n\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3480, "score_of_the_acc": -0.1227, "final_rank": 12 }, { "submission_id": "aoj_2009_9334075", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cmath>\n#include <iterator>\n#include <utility>\n#include <vector>\n\nusing Real = long double;\nconstexpr Real EPS = 1e-10;\nconst Real PI = std::acos(-1);\n\nint sign(Real x) {\n return x < -EPS ? -1 : x > EPS ? 1 : 0;\n}\n\nbool eq(Real lhs, Real rhs) {\n return sign(rhs - lhs) == 0;\n}\n\nbool lte(Real lhs, Real rhs) {\n return sign(rhs - lhs) >= 0;\n}\n\nbool lt(Real lhs, Real rhs) {\n return sign(rhs - lhs) > 0;\n}\n\nbool gte(Real lhs, Real rhs) {\n return lte(rhs, lhs);\n}\n\nbool gt(Real lhs, Real rhs) {\n return lt(rhs, lhs);\n}\n\nstruct Point {\n Real x;\n Real y;\n Point(Real x = 0, Real y = 0): x(x), y(y) {}\n Point& operator+=(Point rhs) {\n this->x += rhs.x;\n this->y += rhs.y;\n return *this;\n }\n Point& operator-=(Point rhs) {\n this->x -= rhs.x;\n this->y -= rhs.y;\n return *this;\n }\n Point& operator*=(Real k) {\n this->x *= k;\n this->y *= k;\n return *this;\n }\n Point& operator/=(Real k) {\n this->x /= k;\n this->y /= k;\n return *this;\n }\n friend Point operator+(Point lhs, Point rhs) {\n return lhs += rhs;\n }\n friend Point operator-(Point lhs, Point rhs) {\n return lhs -= rhs;\n }\n friend Point operator*(Point p, Real k) {\n return p *= k;\n }\n friend Point operator/(Point p, Real k) {\n return p /= k;\n }\n friend Point operator*(Real k, Point p) {\n return p * k;\n }\n friend Point operator/(Real k, Point p) {\n return p / k;\n }\n friend bool operator==(Point lhs, Point rhs) {\n return eq(lhs.x, rhs.x) && eq(lhs.y, rhs.y);\n }\n friend bool operator<(Point lhs, Point rhs) {\n if (eq(lhs.x, rhs.x)) return lt(lhs.y, rhs.y);\n return lt(lhs.x, rhs.x);\n }\n friend bool operator>(Point lhs, Point rhs) {\n return rhs < lhs;\n }\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = Points;\nusing Vector = Point;\n\nReal norm(Vector p) {\n return p.x * p.x + p.y * p.y;\n}\n\nReal abs(Vector p) {\n return std::sqrt(norm(p));\n}\n\nReal dot(Vector lhs, Vector rhs) {\n return lhs.x * rhs.x + lhs.y * rhs.y;\n}\n\nReal cross(Vector lhs, Vector rhs) {\n return lhs.x * rhs.y - lhs.y * rhs.x;\n}\n\nint ccw(Point p0, Point p1, Point p2) {\n Vector a = p1 - p0;\n Vector b = p2 - p0;\n\n if (cross(a, b) > EPS) return 1;\n\n if (cross(a, b) < -EPS) return -1;\n\n if (dot(a, b) < 0) return 2;\n\n if (norm(a) < norm(b)) return -2;\n\n return 0;\n}\n\nstruct Circle {\n Real r;\n Point c;\n Circle() {}\n Circle(Real r, Point c): r(r), c(c) {}\n};\n\nstruct Segment {\n Segment() {}\n Segment(Point p0, Point p1): p0(p0), p1(p1) {}\n Point p0, p1;\n};\n\nstruct Line {\n Line() {}\n Line(Point p0, Point p1): p0(p0), p1(p1) {}\n explicit Line(Segment s): p0(s.p0), p1(s.p1) {}\n Point p0, p1;\n};\n\nReal distance(Point lhs, Point rhs) {\n return abs(rhs - lhs);\n}\n\nReal distance(Line l, Point p) {\n return std::abs(cross(l.p1 - l.p0, p - l.p0)) / abs(l.p1 - l.p0);\n}\n\nReal distance(Segment s, Point p) {\n if (dot(s.p1 - s.p0, p - s.p0) < 0) {\n return distance(p, s.p0);\n }\n if (dot(s.p0 - s.p1, p - s.p1) < 0) {\n return distance(p, s.p1);\n }\n return distance(Line(s), p);\n}\n\nbool intersect(Segment lhs, Segment rhs) {\n return ccw(lhs.p0, lhs.p1, rhs.p0) * ccw(lhs.p0, lhs.p1, rhs.p1) <= 0\n && ccw(rhs.p0, rhs.p1, lhs.p0) * ccw(rhs.p0, rhs.p1, lhs.p1) <= 0;\n}\n\nbool intersect(Segment s, Point p) {\n return ccw(s.p0, s.p1, p) == 0;\n}\n\nReal distance(Segment lhs, Segment rhs) {\n if (intersect(lhs, rhs)) {\n return Real(0);\n }\n return std::min({distance(lhs, rhs.p0), distance(lhs, rhs.p1), distance(rhs, lhs.p0), distance(rhs, lhs.p1)});\n}\n\nbool parallel(Vector lhs, Vector rhs) {\n return eq(cross(lhs, rhs), 0);\n}\n\nbool parallel(Segment lhs, Segment rhs) {\n return parallel(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nbool parallel(Line lhs, Line rhs) {\n return parallel(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nPoint crosspoint(Line lhs, Line rhs) {\n Real a = cross(lhs.p1 - lhs.p0, rhs.p1 - rhs.p0);\n Real b = cross(lhs.p1 - lhs.p0, lhs.p1 - rhs.p0);\n return rhs.p0 + (rhs.p1 - rhs.p0) * b / a;\n}\n\nbool intersect(Line l, Segment s) {\n return ccw(l.p0, l.p1, s.p0) * ccw(l.p0, l.p1, s.p1) <= 0;\n}\n\nbool intersect(Circle c, Line l) {\n return lte(distance(l, c.c), c.r);\n}\n\nbool intersect(Circle c, Point p) {\n return eq(distance(c.c, p), c.r);\n}\n\nbool intersect(Circle c, Segment s) {\n return lte(distance(s, c.c), c.r);\n}\n\nint intersect(Circle lhs, Circle rhs) {\n if (gt(lhs.r, rhs.r)) std::swap(lhs, rhs);\n Real d = distance(lhs.c, rhs.c);\n if (lt(lhs.r + rhs.r, d)) return 4;\n if (eq(lhs.r + rhs.r, d)) return 3;\n if (gt(lhs.r + d, rhs.r)) return 2;\n if (eq(lhs.r + d, rhs.r)) return 1;\n return 0;\n}\n\nbool orthogonal(Vector lhs, Vector rhs) {\n return eq(dot(lhs, rhs), 0);\n}\n\nbool orthogonal(Segment lhs, Segment rhs) {\n return orthogonal(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nbool orthogonal(Line lhs, Line rhs) {\n return orthogonal(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nPoint crosspoint(Segment lhs, Segment rhs) {\n Real d0 = distance(Line(lhs.p0, lhs.p1), rhs.p0);\n Real d1 = distance(Line(lhs.p0, lhs.p1), rhs.p1);\n return rhs.p0 + (rhs.p1 - rhs.p0) * (d0 / (d0 + d1));\n}\n\nReal arg(Vector p) {\n return std::atan2(p.y, p.x);\n}\n\nVector polar(Real a, Real r) {\n return Point(std::cos(r) * a, std::sin(r) * a);\n}\n\nPoint rotate(Point p, Real t) {\n Point v = polar(1, t);\n return Point(p.x * v.x - p.y * v.y, p.x * v.y + p.y * v.x);\n}\n\nReal angle(Point p0, Point p1, Point p2) {\n Real a = arg(p0 - p1);\n Real b = arg(p2 - p1);\n if (gt(a, b)) std::swap(a, b);\n return std::min(b - a, 2 * PI - (b - a));\n}\n\nPoints crosspoint(Circle lhs, Circle rhs) {\n Real d = abs(lhs.c - rhs.c);\n if (eq(d, lhs.r + rhs.r)) return {lhs.c + lhs.r * (rhs.c - lhs.c) / d};\n Real a = std::acos((lhs.r * lhs.r + d * d - rhs.r * rhs.r) / (2 * lhs.r * d));\n Real t = arg(rhs.c - lhs.c);\n return {lhs.c + polar(lhs.r, t + a), lhs.c + polar(lhs.r, t - a)};\n}\n\nPoint projection(Segment s, Point p) {\n Vector a = p - s.p0;\n Vector b = s.p1 - s.p0;\n Real t = dot(a, b) / norm(b);\n return s.p0 + t * b;\n}\n\nPoint projection(Line l, Point p) {\n Vector a = p - l.p0;\n Vector b = l.p1 - l.p0;\n Real t = dot(a, b) / norm(b);\n return l.p0 + t * b;\n}\n\nPoint reflection(Segment s, Point p) {\n return p + (projection(s, p) - p) * Real(2);\n}\n\nPoint reflection(Line l, Point p) {\n return p + (projection(l, p) - p) * Real(2);\n}\n\nPoints crosspoint(Circle c, Line l) {\n Vector p = projection(l, c.c);\n Real t = std::sqrt(c.r * c.r - norm(c.c - p));\n if (eq(t, 0)) return {p};\n Vector e = (l.p1 - l.p0) / abs(l.p1 - l.p0);\n return {p + t * e, p + -t * e};\n}\n\nReal area(Polygon poly) {\n int n = poly.size();\n Real result = 0;\n for (int i = 0; i < n; i++) {\n result += cross(poly[i], poly[(i + 1) % n]);\n }\n return result / 2;\n}\n\nReal perimeter(Polygon poly) {\n int n = poly.size();\n Real result = 0;\n for (int i = 0; i < n; i++) {\n result += abs(poly[i] - poly[(i + 1) % n]);\n }\n return result;\n}\n\nbool convex(Polygon poly) {\n int n = poly.size();\n for (int i = 0; i < n; i++) {\n if (ccw(poly[i], poly[(i + 1) % n], poly[(i + 2) % n]) == -1) return false;\n }\n return true;\n}\n\nint contain(Polygon poly, Point p) {\n int n = poly.size();\n bool parity = false;\n for (int i = 0; i < n; i++) {\n Point a = poly[i] - p;\n Point b = poly[(i + 1) % n] - p;\n if (gt(a.y, b.y)) std::swap(a, b);\n if (lte(a.y, 0) && lt(0, b.y) && lt(cross(a, b), 0)) parity ^= true;\n if (eq(cross(a, b), 0) && lte(dot(a, b), 0)) return 1;\n }\n return (parity ? 2 : 0);\n}\n\nPolygon convex_hull(Points points) {\n std::sort(points.begin(), points.end(), [](Point lhs, Point rhs) {\n if (eq(lhs.x, rhs.x)) return lt(lhs.y, rhs.y);\n return lt(lhs.x, rhs.x);\n });\n int n = points.size();\n Points lower, upper;\n lower.push_back(points[n - 1]);\n lower.push_back(points[n - 2]);\n upper.push_back(points[0]);\n upper.push_back(points[1]);\n for (int i = n - 3; i >= 0; i--) {\n for (int m = lower.size(); m >= 2 && ccw(lower[m - 2], lower[m - 1], points[i]) >= 0; m--) {\n lower.pop_back();\n }\n lower.push_back(points[i]);\n }\n for (int i = 2; i < n; i++) {\n for (int m = upper.size(); m >= 2 && ccw(upper[m - 2], upper[m - 1], points[i]) >= 0; m--) {\n upper.pop_back();\n }\n upper.push_back(points[i]);\n }\n std::reverse(lower.begin(), lower.end());\n std::copy(std::next(upper.rbegin()), std::prev(upper.rend()), std::back_inserter(lower));\n return lower;\n}\n\nPolygon convex_cut(Polygon poly, Line l) {\n int n = poly.size();\n Polygon res;\n for (int i = 0; i < n; i++) {\n Point a = poly[i];\n Point b = poly[(i + 1) % n];\n if (ccw(l.p0, l.p1, a) != -1) res.push_back(a);\n if (ccw(l.p0, l.p1, a) * ccw(l.p0, l.p1, b) == -1) {\n res.push_back(crosspoint(l, Line(a, b)));\n }\n }\n return res;\n}\n\nLine bisector(Point p0, Point p1, Point p2) {\n return Line(p1, p1 + polar(1, angle(p0, p1, p2) / 2));\n}\n\nCircle incircle(Point p0, Point p1, Point p2) {\n Point c = crosspoint(bisector(p0, p1, p2), bisector(p1, p2, p0));\n Real r = std::abs(2 * area({p0, p1, p2}) / perimeter({p0, p1, p2}));\n return Circle(r, c);\n}\n\nLine perpendicular(Line l, Point p) {\n Vector v = l.p1 - l.p0;\n return Line(p, p + Vector(-v.y, v.x));\n}\n\nLine perpendicular_bisector(Segment s) {\n return perpendicular(Line(s), (s.p0 + s.p1) / 2);\n}\n\nCircle circumscribed_circle(Point p0, Point p1, Point p2) {\n Point c = crosspoint(perpendicular_bisector(Segment(p0, p1)), perpendicular_bisector(Segment(p0, p2)));\n return Circle(distance(p0, c), c);\n}\n\nReal area(Circle c) {\n return c.r * c.r * PI;\n}\n\nPoints tangent(Circle c, Point p) {\n return crosspoint(c, Circle(std::sqrt(norm(c.c - p) - c.r * c.r), p));\n}\n\nconst Real L = 100;\n\nbool on_square(Point p) {\n return eq(p.x, L) || eq(p.x, -L) || eq(p.y, L) || eq(p.y, -L);\n}\n\nint solve() {\n int n;\n std::cin >> n;\n if (n == 0) return 1;\n using Segments = std::vector<Segment>;\n Segments segments;\n int ans = 1;\n for (int i = 0; i < n; i++) {\n Point p, q;\n std::cin >> p.x >> p.y;\n std::cin >> q.x >> q.y;\n Segment s(p, q);\n Points cp;\n for (auto& t: segments) {\n if (intersect(s, t)) {\n cp.push_back(crosspoint(s, t));\n }\n }\n std::sort(cp.begin(), cp.end());\n cp.erase(std::unique(cp.begin(), cp.end()), cp.end());\n int cur = 0;\n for (auto& p: cp) {\n if (!on_square(p)) cur++;\n }\n ans += cur + 1;\n segments.push_back(s);\n }\n std::cout << ans << std::endl;\n return 0;\n}\n\nint main() {\n while (!solve())\n ;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3552, "score_of_the_acc": -0.1405, "final_rank": 13 }, { "submission_id": "aoj_2009_9311420", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, n) for(int i = a; i < n; i++)\n#define rrep(i, a, n) for(int i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nnamespace geometry {\n using D = long double;\n using Point = std::complex<D>;\n const D EPS = 1e-10;\n const D PI = std::acos(D(-1));\n\n istream &operator>>(istream &is, Point &p) {\n D a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n }\n\n // d 倍する\n Point operator*(Point p, D d) {\n return Point(p.real() * d, p.imag() * d);\n }\n // 等しいかどうか(誤差で判定)\n inline bool equal(D a, D b) { return fabs(a - b) < EPS; }\n\n // 単位ベクトル\n Point unit_vector(Point a) { return a / abs(a); };\n\n // 法線ベクトル (逆向きがよければ (0, -1) をかける)\n Point normal_vector(Point a, D dir=1) { return a * Point(0, dir); }\n\n // 内積:a・b = |a||b|cosΘ\n D dot(Point a, Point b){\n return (a.real() * b.real() + a.imag() * b.imag());\n }\n\n // 外積:a×b = |a||b|sinΘ (外積の大きさではないか?)\n D cross(Point a, Point b){\n return (a.real() * b.imag() - a.imag() * b.real());\n }\n\n // 反時計回りに theta 回転\n Point rotate(Point a, D theta) {\n D c = cos(theta), s = sin(theta);\n return Point(c * a.real() - s * a.imag(), s * a.real() + c * a.imag());\n }\n\n // 直線 \n struct Line {\n Point a, b;\n Line() = default;\n Line(Point a_, Point b_) : a(a_), b(b_) { assert(a_ != b_); };\n // Ax+By=C\n Line(D A, D B, D C){\n if(equal(A, 0)){\n a = Point(0, C/B), b = Point(1, C/B);\n }else if(equal(B, 0)){\n b = Point(C/A, 0), a = Point(C/A, 1);\n }else{\n a = Point(0, C/B), b = Point(C/A, 0);\n }\n }\n };\n\n // 線分(Line と同じ)\n struct Segment : Line {\n Segment() = default;\n Segment(Point a_, Point b_) : Line(a_, b_) {};\n };\n\n\n // 射影:直線(線分)に 点p から引いた垂線の足を求める\n Point projection(Line l, Point p){\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n Point projection(Segment l, Point p){\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n // 反射:直線を対象軸として 点p と線対称の位置にある点を求める\n Point reflection(Line l, Point p){\n return p + (projection(l, p)-p) * 2.0;\n }\n\n // 3点 a, b, c の位置関係\n // reference: https://sen-comp.hatenablog.com/entry/2020/03/12/145742#iSP3%E7%82%B9%E3%81%AE%E4%BD%8D%E7%BD%AE%E9%96%A2%E4%BF%82\n int ccw(Point a, Point b, Point c){\n b -= a, c -= a;\n // 点 a, b, c が\n if(cross(b, c) > EPS) return 1; // 反時計回りのとき\n if(cross(b, c) < -EPS) return -1; // 時計回りのとき\n \n // 同一直線上にある場合\n if(dot(b, c) < 0) return 2; // c, a, b の順\n if(norm(b) < norm(c)) return -2; // a, b, c の順\n return 0; // a, c, b の順\n }\n \n // 垂直(内積 == 0)\n bool is_vertical(Line a, Line b){\n return equal(dot(a.b - a.a, b.b - b.a), 0);\n }\n\n // 平行(外積 == 0)\n bool is_parallel(Line a, Line b){\n return equal(cross(a.b - a.a, b.b - b.a), 0);\n }\n\n // 線分の交差判定(線分 s に対して, 線分 t の端点が反対側にあればよい)\n bool is_intersect(Segment s, Segment t){\n return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0) && \n (ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n }\n\n // 交点(交差する前提)\n Point cross_point(Line s, Line t){\n D d1 = cross(s.b - s.a, t.b - t.a);\n D d2 = cross(s.b - s.a, s.b - t.a);\n // s, t が一致する場合(適当な1点を返す)\n if(equal(abs(d1), 0) && equal(abs(d2), 0)) return t.a; \n \n return t.a + (t.b - t.a) * (d2/d1);\n }\n Point cross_point(Segment s, Segment t) {\n assert(is_intersect(s, t)); // 交差する前提\n return cross_point(Line(s), Line(t));\n }\n\n // 線分と点の距離(点p から線分のどこかへの最短距離)\n D dist_segment_point(Segment l, Point p){\n if(dot(l.b - l.a, p - l.a) < EPS) abs(p - l.a);\n if(dot(l.a - l.b, p - l.b) < EPS) abs(p - l.b);\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n // 線分と線分の距離\n D dist_segment_segment(Segment s, Segment t){\n if(is_intersect(s, t)) return 0.0;\n D res = min({\n dist_segment_point(s, t.a),\n dist_segment_point(s, t.b),\n dist_segment_point(t, s.a),\n dist_segment_point(t, s.b),\n });\n return res;\n }\n\n // Todo : 円, 多角形\n};\nusing namespace geometry;\n\n\nint main(){\n while(1){\n int n; cin >> n;\n if(n == 0) return 0;\n int ans = n+1;\n vector<Line> lines;\n rep(i, 0, n){\n Point p1, p2; cin >> p1 >> p2;\n Line newl(p1, p2);\n vector<Point> points;\n for(auto l: lines){\n if(is_parallel(l, newl)) continue;\n Point crossp = cross_point(l, newl);\n if(abs(crossp.imag())>=100 || abs(crossp.real())>=100) continue;\n bool flg = true;\n for(auto p:points){\n if(abs(p-crossp) <= 1e-10){\n flg = false;\n break;\n }\n }\n if(flg) ans++;\n points.push_back(crossp);\n }\n lines.push_back(newl);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3492, "score_of_the_acc": -0.8399, "final_rank": 19 }, { "submission_id": "aoj_2009_9311415", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, n) for(int i = a; i < n; i++)\n#define rrep(i, a, n) for(int i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nnamespace geometry {\n using D = long double;\n using Point = std::complex<D>;\n const D EPS = 1e-10;\n const D PI = std::acos(D(-1));\n\n istream &operator>>(istream &is, Point &p) {\n D a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n }\n\n // d 倍する\n Point operator*(Point p, D d) {\n return Point(p.real() * d, p.imag() * d);\n }\n // 等しいかどうか(誤差で判定)\n inline bool equal(D a, D b) { return fabs(a - b) < EPS; }\n\n // 単位ベクトル\n Point unit_vector(Point a) { return a / abs(a); };\n\n // 法線ベクトル (逆向きがよければ (0, -1) をかける)\n Point normal_vector(Point a, D dir=1) { return a * Point(0, dir); }\n\n // 内積:a・b = |a||b|cosΘ\n D dot(Point a, Point b){\n return (a.real() * b.real() + a.imag() * b.imag());\n }\n\n // 外積:a×b = |a||b|sinΘ (外積の大きさではないか?)\n D cross(Point a, Point b){\n return (a.real() * b.imag() - a.imag() * b.real());\n }\n\n // 反時計回りに theta 回転\n Point rotate(Point a, D theta) {\n D c = cos(theta), s = sin(theta);\n return Point(c * a.real() - s * a.imag(), s * a.real() + c * a.imag());\n }\n\n // 直線 \n struct Line {\n Point a, b;\n Line() = default;\n Line(Point a_, Point b_) : a(a_), b(b_) { assert(a_ != b_); };\n // Ax+By=C\n Line(D A, D B, D C){\n if(equal(A, 0)){\n a = Point(0, C/B), b = Point(1, C/B);\n }else if(equal(B, 0)){\n b = Point(C/A, 0), a = Point(C/A, 1);\n }else{\n a = Point(0, C/B), b = Point(C/A, 0);\n }\n }\n };\n\n // 線分(Line と同じ)\n struct Segment : Line {\n Segment() = default;\n Segment(Point a_, Point b_) : Line(a_, b_) {};\n };\n\n\n // 射影:直線(線分)に 点p から引いた垂線の足を求める\n Point projection(Line l, Point p){\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n Point projection(Segment l, Point p){\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n // 反射:直線を対象軸として 点p と線対称の位置にある点を求める\n Point reflection(Line l, Point p){\n return p + (projection(l, p)-p) * 2.0;\n }\n\n // 3点 a, b, c の位置関係\n // reference: https://sen-comp.hatenablog.com/entry/2020/03/12/145742#iSP3%E7%82%B9%E3%81%AE%E4%BD%8D%E7%BD%AE%E9%96%A2%E4%BF%82\n int ccw(Point a, Point b, Point c){\n b -= a, c -= a;\n // 点 a, b, c が\n if(cross(b, c) > EPS) return 1; // 反時計回りのとき\n if(cross(b, c) < -EPS) return -1; // 時計回りのとき\n \n // 同一直線上にある場合\n if(dot(b, c) < 0) return 2; // c, a, b の順\n if(norm(b) < norm(c)) return -2; // a, b, c の順\n return 0; // a, c, b の順\n }\n \n // 垂直(内積 == 0)\n bool is_vertical(Line a, Line b){\n return equal(dot(a.b - a.a, b.b - b.a), 0);\n }\n\n // 平行(外積 == 0)\n bool is_parallel(Line a, Line b){\n return equal(cross(a.b - a.a, b.b - b.a), 0);\n }\n\n // 線分の交差判定(線分 s に対して, 線分 t の端点が反対側にあればよい)\n bool is_intersect(Segment s, Segment t){\n return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0) && \n (ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n }\n\n // 交点(交差する前提)\n Point cross_point(Line s, Line t){\n D d1 = cross(s.b - s.a, t.b - t.a);\n D d2 = cross(s.b - s.a, s.b - t.a);\n // s, t が一致する場合(適当な1点を返す)\n if(equal(abs(d1), 0) && equal(abs(d2), 0)) return t.a; \n \n return t.a + (t.b - t.a) * (d2/d1);\n }\n Point cross_point(Segment s, Segment t) {\n assert(is_intersect(s, t)); // 交差する前提\n return cross_point(Line(s), Line(t));\n }\n\n // 線分と点の距離(点p から線分のどこかへの最短距離)\n D dist_segment_point(Segment l, Point p){\n if(dot(l.b - l.a, p - l.a) < EPS) abs(p - l.a);\n if(dot(l.a - l.b, p - l.b) < EPS) abs(p - l.b);\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n // 線分と線分の距離\n D dist_segment_segment(Segment s, Segment t){\n if(is_intersect(s, t)) return 0.0;\n D res = min({\n dist_segment_point(s, t.a),\n dist_segment_point(s, t.b),\n dist_segment_point(t, s.a),\n dist_segment_point(t, s.b),\n });\n return res;\n }\n\n // Todo : 円, 多角形\n};\nusing namespace geometry;\n\n\nint main(){\n while(1){\n int n; cin >> n;\n if(n == 0) return 0;\n int ans = n+1;\n vector<Line> lines;\n rep(i, 0, n){\n Point p1, p2; cin >> p1 >> p2;\n Line newl(p1, p2);\n vector<Point> points;\n for(auto l: lines){\n if(is_parallel(l, newl)) continue;\n Point crossp = cross_point(l, newl);\n if(abs(crossp.imag())>=100-1e-10 || abs(crossp.real())>=100-1e-10) continue;\n bool flg = true;\n for(auto p:points){\n if(abs(p-crossp) <= 1e-10){\n flg = false;\n break;\n }\n }\n if(flg) ans++;\n points.push_back(crossp);\n }\n lines.push_back(newl);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3476, "score_of_the_acc": -0.7883, "final_rank": 18 }, { "submission_id": "aoj_2009_9307481", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nnamespace geometry\n{\n using ld=long double;\n const double EPS=1e-8;\n const double PI=acos(-1);\n const double INF_DOUBLE=DBL_MAX;\n bool same(double a, double b){return abs(a-b)<EPS;}\n struct point\n {\n double x,y;\n point()=default;\n point(double x, double y):x(x),y(y){}\n\n double norm(){return sqrt(x*x+y*y);}\n double arg(){return atan2(y,x);}\n double arg(point p, point q) // *this=Oとして∠POQを[0,π]で返す\n {\n p-=*this,q-=*this;\n double res=abs(p.arg()-q.arg());\n if(res>PI+EPS)res=2*PI-res;\n return res;\n }\n double operator*(const point& p){return x*p.x+y*p.y;} // 内積を求める\n double operator^(const point& p){return x*p.y-y*p.x;} // 外積を求める\n\n bool operator==(const point& p){return same(x,p.x)&&same(y,p.y);}\n bool operator!=(const point& p){return !same(x,p.x)||!same(y,p.y);}\n point operator+(const point& p){return point{x+p.x,y+p.y};}\n point operator-(const point& p){return point{x-p.x,y-p.y};}\n point operator*(const double& k){return point{x*k,y*k};}\n point operator/(const double& k){return point{x/k,y/k};}\n point& operator+=(const point& p){return *this=(*this)+p;}\n point& operator-=(const point& p){return *this=(*this)-p;}\n point& operator*=(const double& k){return *this=(*this)*k;}\n point& operator/=(const double& k){return *this=(*this)/k;}\n friend point operator*(double k, point p){return p*k;}\n void rotate(double theta) // theta回転する、thetaは弧度法\n {\n double x_=x*cos(theta)-y*sin(theta);\n double y_=x*sin(theta)+y*cos(theta);\n x=x_,y=y_;\n }\n int ccw(point q, point r) // this=:pを中心としてq,rとの位置関係\n {\n q-=*this,r-=*this;\n if((q^r)>EPS)return 1; // p,q,rが反時計回り\n if(-(q^r)>EPS)return -1; // p,q,rが時計回り\n if(-(q*r)>EPS)return 2; // r,p,qの順で同一直線上\n if(q.norm()<r.norm())return -2; // p,q,rの順で同一直線上\n return 0; // p,r,qの順で同一直線上\n }\n };\n ostream &operator<<(ostream& os, point p)\n {\n os << '(' << p.x << ',' << p.y << ')';\n return os;\n }\n const point INF_POINT=point(INF_DOUBLE,INF_DOUBLE);\n struct line // ax+by=cの形で持つ\n {\n double a,b,c;\n line()=default;\n line(double a, double b, double c):a(a),b(b),c(c){}\n // 2点\n line(point p, point q):a(q.y-p.y),b(p.x-q.x),c(p.x*q.y-p.y*q.x){}\n line(double a, point p):a(a),b(-1),c(a*p.x-p.y){} // 傾きと1点\n bool online(point p){return same(a*p.x+b*p.y,c);} // 点が線上にあるか\n // 代入\n double y(double x)\n {\n if(same(b,0))return INF_DOUBLE;\n return (c-a*x)/b;\n }\n double x(double y)\n {\n if(same(a,0))return INF_DOUBLE;\n return (c-b*y)/a;\n }\n point uni()\n {\n if(same(b,0)){return point(0,1);}\n point p(0,this->y(0)),q(1,this->y(1));\n return (p-q)/(p-q).norm();\n }\n line normal(point p) // pを通りthisに垂直な直線\n {\n if(same(a,0))return line(1,0,p.x);\n if(same(b,0))return line(0,1,p.y);\n return line(b/a,p);\n }\n point crosspoint(line l) // 2直線の交点\n {\n if(same(a*l.b,b*l.a))return INF_POINT;\n double den=a*l.b-b*l.a;\n return point((c*l.b-b*l.c)/den,(a*l.c-c*l.a)/den);\n }\n point projection(point p) // pからthisに下した垂線の足\n {\n return this->crosspoint(this->normal(p));\n }\n point reflection(point p) // thisについてpと対称な点\n {\n point q=this->projection(p);\n return 2*q-p;\n }\n double dist(point p){return (p-this->projection(p)).norm();} // 点と直線の距離\n double angle(line l) // 2直線のなす角\n {\n \n if(same(a*l.a+b*l.b,0))return PI/2; // 直行\n if(same(a*l.b,b*l.a))return INF_DOUBLE; // 平行\n double tan=(a*l.b-b*l.a)/(a*l.a+b*l.b);\n if(-tan>EPS)tan=-tan;\n return atan(tan);\n }\n };\n struct segment\n {\n line l;\n point p,q;\n segment()=default;\n segment(point p, point q):l(line(p,q)),p(p),q(q){}\n bool cross(segment s) // 線分の交差判定\n {\n return p.ccw(q,s.p)*p.ccw(q,s.q)<=0&&s.p.ccw(s.q,p)*s.p.ccw(s.q,q)<=0;\n }\n point crosspoint(segment s) // 線部同士の交点\n {\n if(this->cross(s))return l.crosspoint(s.l);\n return INF_POINT;\n }\n double dist(point r) // 点と線分の距離\n {\n if((q-p)*(r-p)<EPS){return (r-p).norm();}\n if((p-q)*(r-q)<EPS){return (r-q).norm();}\n return l.dist(r);\n }\n double dist(segment s) // 線分同士の距離\n {\n if(this->cross(s)){return 0;}\n double res=this->dist(s.p);\n res=min(res,this->dist(s.q));\n res=min(res,s.dist(p));\n res=min(res,s.dist(q));\n return res;\n }\n };\n struct polygon\n {\n int n;\n vector<point>vertex;\n polygon()=default;\n polygon(vector<point>v):vertex(v),n(v.size()){}\n point operator[](int i){return vertex[i];}\n double area() // 面積\n {\n double res=vertex.back()^vertex[0];\n for(int i=1;i<n;i++)res+=vertex[i-1]^vertex[i];\n return res/2;\n }\n bool is_convex() // 凸性判定\n {\n int pre,cur,nxt;\n for(int i=1;i<=n;i++)\n {\n pre=i-1;\n cur=i%n;\n nxt=(i+1)%n;\n if(vertex[pre].ccw(vertex[cur],vertex[nxt])==-1){return 0;}\n }\n return 1;\n }\n int contain(point p)// 多角形に点が含まれているか、内部:2、辺上:1、外部:0\n {\n bool in=0;\n for(int i=0;i<n;i++)\n {\n point q=vertex[i]-p,r=vertex[(i+1)%n]-p;\n if(q.y>r.y)swap(q,r);\n if(q.y<EPS&&EPS<r.y&&-(q^r)>EPS)in=!in;\n if(same(q^r,0)&&(q*r)<=0)return 1;\n }\n return (in?2:0);\n }\n double diam() // 凸包の直径=最遠頂点対間距離\n {\n cout << \"ok diam\\n\";\n int is=0,js=0;\n for(int i=1;i<n;i++)\n {\n cout << i << endl;\n if(vertex[i].y>vertex[is].y)is=i;\n if(vertex[i].y<vertex[js].y)js=i;\n }\n double res=(vertex[is]-vertex[js]).norm();\n int i,j,max_i,max_j;\n i=max_i=is;\n j=max_j=js;\n do\n {\n point pi=vertex[(i+1)%n]-vertex[i];\n point pj=vertex[(j+1)%n]-vertex[j];\n if((pi^pj)>EPS)j=(j+1)%n;\n else i=(i+1)%n;\n if((vertex[i]-vertex[j]).norm()>res)\n {\n res=(vertex[i]-vertex[j]).norm();\n max_i=i,max_j=j;\n }\n }while(i!=is||j!=js);\n return res;\n }\n void relabeling() // 最も下、最も左の頂点から\n {\n double min_x=INF_DOUBLE,min_y=INF_DOUBLE;\n int idx=0;\n for(int i=0;i<n;i++)\n {\n if(vertex[i].y<min_y)\n {\n min_x=vertex[i].x;\n min_y=vertex[i].y;\n idx=i;\n }\n else if(same(vertex[i].y,min_y)&&vertex[i].x<min_x)\n {\n min_x=vertex[i].x;\n min_y=vertex[i].y;\n idx=i;\n }\n }\n vector<point>tmp(n);\n for(int i=0;i<n;i++)tmp[i]=vertex[(idx+i)%n];\n vertex=tmp;\n }\n };\n polygon convex_hull(vector<point>v) // 凸包\n {\n int n=v.size(),k=0;\n sort(v.begin(),v.end(),[](const point& p, const point& q)\n {\n return p.x!=q.x?p.x<q.x:p.y<q.y;\n });\n vector<point>ch(2*n);\n for(int i=0;i<n;ch[k++]=v[i++])\n {\n // 同一直線上の点を含めないときは上、含めるときは下\n // while(k>=2&&((ch[k-1]-ch[k-2])^(v[i]-ch[k-1]))<EPS)k--;\n while(k>=2&&-((ch[k-1]-ch[k-2])^(v[i]-ch[k-1]))>EPS)k--;\n }\n for(int i=n-2,t=k+1;i>=0;ch[k++]=v[i--])\n {\n // while(k>=2&&((ch[k-1]-ch[k-2])^(v[i]-ch[k-1]))<EPS)k--;\n while(k>=2&&-((ch[k-1]-ch[k-2])^(v[i]-ch[k-1]))>EPS)k--;\n }\n ch.resize(k-1);\n return polygon(ch);\n }\n double closest_pair(vector<point> v) // 最近点対をO(N(logN)^2)で求める\n {\n sort(v.begin(),v.end(),[](const point& p, const point& q)\n {\n return p.x<q.x;\n });\n auto rec=[&](const auto& rec, int l, int r) -> double\n {\n if(r-l<=1)return INF_DOUBLE;\n int m=(r+l)/2;\n double x=v[m].x;\n double d=min(rec(rec,l,m),rec(rec,m,r));\n inplace_merge(v.begin()+l,v.begin()+m,v.begin()+r,[&](const point& p, const point& q)\n {\n return p.y<q.y;\n });\n vector<point>v_;\n for(int i=l;i<r;i++)\n {\n if(d-abs(v[i].x-x)<=EPS)continue;\n for(int j=v_.size()-1;j>=0;j--)\n {\n if(d-abs(v[i].y-v_[j].y)<=EPS)break;\n d=min(d,(v[i]-v_[j]).norm());\n }\n v_.push_back(v[i]);\n }\n return d;\n };\n return rec(rec,0,v.size());\n }\n struct circle\n {\n point p;\n double r;\n circle()=default;\n circle(point p, double r):p(p),r(r){}\n circle(point p_, point q_, point r_, bool in=0) // 三角形の外/内接円\n {\n if(in)\n {\n double a=(q_-r_).norm(),b=(r_-p_).norm(),c=(p_-q_).norm();\n p=a*p_+b*q_+c*r_;\n p/=(a+b+c);\n r=line(p_,q_).dist(p);\n }\n else\n {\n point pq=p_-q_,qr=q_-r_;\n double d=pq^qr;\n double tmp1=p_.norm()*p_.norm()-q_.norm()*q_.norm();\n double tmp2=q_.norm()*q_.norm()-r_.norm()*r_.norm();\n p=(0.5/d)*(tmp1*qr-tmp2*pq);\n swap(p.x,p.y);\n p.y=-p.y;\n r=(p-p_).norm();\n }\n }\n int count_common_tangent(circle c) // 共通接線の本数を返す\n {\n double d=(p-c.p).norm();\n if(d>r+c.r+EPS)return 4;\n if(same(d,r+c.r))return 3;\n if(same(d,abs(r-c.r)))return 1;\n if(d<abs(r-c.r)-EPS)return 0;\n return 2;\n }\n vector<point>crosspoint(line l) // 円と直線の交点\n {\n double d=l.dist(p);\n vector<point>res;\n if(d>r+EPS)return res;\n point h=l.projection(p);\n if(same(d,r))\n {\n res.push_back(h);\n return res;\n }\n point e=l.uni();\n double tmp=sqrt(r*r-d*d);\n res.push_back(h-tmp*e);\n res.push_back(h+tmp*e);\n return res;\n }\n vector<point>crosspoint(circle c) // 2円の交点\n {\n int t=this->count_common_tangent(c);\n double d=(p-c.p).norm();\n vector<point>res;\n if(t==0||t==4){return res;}\n if(t==3)\n {\n double s=r/(r+c.r);\n res.push_back(p+(c.p-p)*s);\n return res;\n }\n if(t==1)\n {\n if(c.r<r-EPS)res.push_back(p+(c.p-p)*(r/d));\n else res.push_back(c.p+(p-c.p)*(c.r/d));\n return res;\n }\n double a_=2*(p.x-c.p.x);\n double b_=2*(p.y-c.p.y);\n double c_=(p.x+c.p.x)*(p.x-c.p.x)+(p.y+c.p.y)*(p.y-c.p.y)+(r+c.r)*(c.r-r);\n return this->crosspoint(line(a_,b_,c_));\n }\n vector<point>tangent(point p_) // 点から引いた円の接点\n {\n double d=(p-p_).norm();\n if(d<r-EPS)return{};\n if(same(d,r))return{p_};\n return this->crosspoint(circle(p_,sqrt(d*d-r*r)));\n }\n vector<line> common_tangent(circle c) // 2円の共通接線\n {\n int t=this->count_common_tangent(c);\n double d=(p-c.p).norm();\n vector<line>res;\n if(t==0)return res;\n if(t==1)\n {\n point q=(this->crosspoint(c))[0];\n res.push_back(line(p,q).normal(q));\n }\n if(t==2)\n {\n //\n }\n }\n };\n}\nusing namespace geometry;\nbool solve()\n{\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<segment>lines(N);\n int ans=1;\n for(int i=0;i<N;i++)\n {\n point P,Q;\n cin>>P.x>>P.y>>Q.x>>Q.y;\n lines[i]=segment(P,Q);\n int cnt=1;\n vector<point>CP;\n for(int j=0;j<i;j++)\n {\n if(!lines[i].cross(lines[j]))continue;\n auto X=lines[i].crosspoint(lines[j]);\n if(same(X.x,100))continue;\n if(same(X.x,-100))continue;\n if(same(X.y,100))continue;\n if(same(X.y,-100))continue;\n if([&]\n {\n for(auto Y:CP)if(same(X.x,Y.x)&&same(X.y,Y.y))return 0;\n return 1;\n }())CP.push_back(X);\n }\n ans+=CP.size()+1;\n }\n cout<<ans<<'\\n';\n return 1;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.1167, "final_rank": 11 }, { "submission_id": "aoj_2009_9117409", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\n\nusing P = complex<ld>;\nld eps = 1e-10;\nll solve(int N){\n vc<P> S(N), T(N); rep(i, N){\n ld a, b, c, d; cin >> a >> b >> c >> d;\n S[i] = {a, b};\n T[i] = {c, d};\n }\n ll ans = 1;\n auto cross = [&](P a, P b){return (conj(a) * b).imag();};\n rep(i, N){\n vc<P> point;\n ll cnt = 0;\n rep(j, i){\n P iv = T[i] - S[i];\n P jv = T[j] - S[j];\n if (abs(cross(iv, jv)) < eps) continue;\n P a = S[i] + iv * cross(jv, S[j] - S[i]) / cross(jv, iv);\n if (abs(a.real()) - 100 > -eps || abs(a.imag()) - 100 > -eps) continue;\n bool flag = true;\n rep(x, len(point)) if (abs(a.real() - point[x].real()) < eps && abs(a.imag() - point[x].imag()) < eps){\n flag = false;\n break;\n }\n if (flag){\n cnt++;\n point.push_back(a);\n }\n }\n ans += cnt + 1;\n }\n return ans;\n}\n\nint main(){\n vc<ll> ans;\n while (true){\n int N; cin >> N;\n if (N == 0) break;\n ans.push_back(solve(N));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3412, "score_of_the_acc": -0.1058, "final_rank": 10 }, { "submission_id": "aoj_2009_9030409", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nusing Real = long double;\nconst Real EPS = 1e-10, PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << \" \" << p.imag();\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(p.real() * d, p.imag() * d);\n}\nPoint operator/(const Point& p, const Real& d) {\n return Point(p.real() / d, p.imag() / d);\n}\nPoint rot(const Point& p, Real theta) {\n return p * Point(cos(theta), sin(theta));\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nReal dist(const Point& p1, const Point& p2) {\n return abs(p1 - p2);\n}\nbool comp_x(const Point& p1, const Point& p2) {\n return eq(p1.real(), p2.real()) ? p1.imag() < p2.imag() : p1.real() < p2.real();\n}\nbool comp_y(const Point& p1, const Point& p2) {\n return eq(p1.imag(), p2.imag()) ? p1.real() < p2.real() : p1.imag() < p2.imag();\n}\nbool comp_arg(const Point& p1, const Point& p2) {\n return arg(p1) < arg(p2);\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nReal closest_pair(vector<Point> ps) {\n if((int)ps.size() <= 1) return 1e18;\n sort(ps.begin(), ps.end(), comp_x);\n vector<Point> memo(ps.size());\n auto func = [&](auto& func, int l, int r) -> Real {\n if(r - l <= 1) return 1e18;\n int m = (l + r) >> 1;\n Real x = ps[m].real();\n Real res = min(func(func, l, m), func(func, m, r));\n inplace_merge(ps.begin() + l, ps.begin() + m, ps.begin() + r, comp_y);\n int cnt = 0;\n for(int i = l; i < r; ++i) {\n if(abs(ps[i].real() - x) >= res) continue;\n for(int j = 0; j < cnt; ++j) {\n Point d = ps[i] - memo[cnt - j - 1];\n if(d.imag() >= res) break;\n res = min(res, abs(d));\n }\n memo[cnt++] = ps[i];\n }\n return res;\n };\n return func(func, 0, (int)ps.size());\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_orthogonal(const Line& a, const Line& b) {\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\nPoint projection(const Line& l, const Point& p) {\n Real t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\nPoint reflection(const Line& l, const Point& p) {\n return p + (projection(l, p) - p) * 2.0;\n}\nbool is_intersect_lp(const Line& l, const Point& p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\nbool is_intersect_sp(const Segment& s, const Point& p) {\n return ccw(s.a, s.b, p) == 0;\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nbool is_intersect_ls(const Line& l, const Segment& s) {\n return sign(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a)) <= 0;\n}\nbool is_intersect_sl(const Segment& s, const Line& l) {\n return is_intersect_ls(l, s);\n}\nbool is_intersect_ss(const Segment& s1, const Segment& s2) {\n if(ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) > 0) return false;\n return ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.push_back(l2.a);\n } else {\n res.push_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nvector<Point> intersection_ss(const Segment& s1, const Segment& s2) {\n return is_intersect_ss(s1, s2) ? intersection_ll(Line(s1), Line(s2)) : vector<Point>();\n}\nReal dist_lp(const Line& l, const Point& p) {\n return abs(p - projection(l, p));\n}\nReal dist_sp(const Segment& s, const Point& p) {\n Point h = projection(s, p);\n if(is_intersect_sp(s, h)) return abs(h - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\nReal dist_ll(const Line& l1, const Line& l2) {\n if(is_intersect_ll(l1, l2)) return 0.0;\n return dist_lp(l1, l2.a);\n}\nReal dist_ss(const Segment& s1, const Segment& s2) {\n if(is_intersect_ss(s1, s2)) return 0.0;\n return min({dist_sp(s1, s2.a), dist_sp(s1, s2.b), dist_sp(s2, s1.a), dist_sp(s2, s1.b)});\n}\nReal dist_ls(const Line& l, const Segment& s) {\n if(is_intersect_ls(l, s)) return 0.0;\n return min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\nReal dist_sl(const Segment& s, const Line& l) {\n return dist_ls(l, s);\n}\nint main(void) {\n while(1) {\n int n;\n cin >> n;\n if(n == 0) break;\n vector<Segment> s(n);\n int ans = 1;\n rep(i, 0, n) {\n Point p1, p2;\n cin >> p1 >> p2;\n Point dir = p2 - p1;\n s[i] = Segment(p1 + EPS * dir, p2 - EPS * dir);\n vector<Point> cs;\n rep(j, 0, i) {\n vector<Point> c = intersection_ss(s[i], s[j]);\n if(c.empty()) continue;\n bool flag = true;\n rep(k, 0, (int)cs.size()) {\n if(eq(cs[k].real(), c[0].real()) and eq(cs[k].imag(), c[0].imag())) {\n flag = false;\n }\n }\n if(flag) {\n cs.push_back(c[0]);\n }\n }\n ans += cs.size() + 1;\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3480, "score_of_the_acc": -0.2655, "final_rank": 17 }, { "submission_id": "aoj_2009_8861715", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <set>\n\n#define EPS 1e-10\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n\nusing namespace std;\n\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(const P &p) const { return P(x + p.x, y + p.y); }\n P operator-(const P &p) const { return P(x - p.x, y - p.y); }\n bool operator<(const P &p) const {\n return x != p.x ? x < p.x : y < p.y;\n }\n};\n\ntypedef pair<P, P> Segment;\n\ndouble cross(const P &a, const P &b) { return a.x * b.y - a.y * b.x; }\ndouble norm(const P &p) { return p.x * p.x + p.y * p.y; }\ndouble dot(const P &a, const P &b) { return a.x * b.x + a.y * b.y; }\n\nint ccw(const P &p, const P &q, const P &r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool isIntersect(const P &p1, const P &p2, const P &p3, const P &p4) {\n return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);\n}\n\nP crosspoint(const P &p1, const P &p2, const P &p3, const P &p4) {\n double A = cross(p2 - p1, p4 - p3);\n double B = cross(p2 - p1, p2 - p3) / A;\n return P(p3.x + (p4.x - p3.x) * B, p3.y + (p4.y - p3.y) * B);\n}\n\nint main() {\n int n;\n vector<Segment> pos;\n Segment pesh;\n\n while (cin >> n && n) {\n int field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> pesh.F.x >> pesh.F.y >> pesh.S.x >> pesh.S.y;\n pos.push_back(pesh);\n set<P> exist;\n int x_counter = 0;\n for (int j = 0; j < pos.size() - 1; j++) {\n if (isIntersect(pos[j].F, pos[j].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S)) {\n P xp = crosspoint(pos[j].F, pos[j].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S);\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS)\n continue;\n if (exist.insert(xp).second) {\n x_counter++;\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3064, "score_of_the_acc": -0.0198, "final_rank": 4 }, { "submission_id": "aoj_2009_8508036", "code_snippet": "#line 1 \"2009.test.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/2009\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp\"\n\n#include <cstdint>\n#include <cstddef>\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp\"\n\n#include <iostream>\n#include <iomanip>\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Real.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Real.hpp\"\n\n#include <cmath>\n#include <cassert>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nusing Real = long double;\nconstexpr Real EPS{1e-12};\n\nnamespace internal {\n\nconstexpr i32 negative{-1};\nconstexpr i32 zero{};\nconstexpr i32 positive{1};\n\n} // namespace internal\n\nconstexpr i32 Sign(Real value) {\n if (value < -EPS) return internal::negative;\n if (value > EPS) return internal::positive;\n return internal::zero;\n}\n\nconstexpr bool Zero(Real value) {\n return Sign(value) == internal::zero;\n}\n\nconstexpr bool Positive(Real value) {\n return Sign(value) == internal::positive;\n}\n\nconstexpr bool Negative(Real value) {\n return Sign(value) == internal::negative;\n}\n\nconstexpr bool Equal(Real a, Real b) {\n return Zero(a - b);\n}\n\nconstexpr bool Smaller(Real a, Real b) {\n return Negative(a - b);\n}\n\nconstexpr bool Bigger(Real a, Real b) {\n return Positive(a - b);\n}\n\nconstexpr Real Square(Real value) {\n return (Zero(value) ? value : value * value);\n}\n\nconstexpr Real Sqrt(Real value) {\n assert(!Negative(value));\n return (Zero(value) ? value : sqrtl(value));\n}\n\nconstexpr Real Abs(Real value) {\n return (Negative(value) ? -value : value);\n}\n\n} // namespace geometryR2\n \n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nconstexpr Real PI{acosl(-1)};\nconstexpr Real TAU{static_cast<Real>(2) * PI};\n\nconstexpr Real ArcToRadian(Real arc) {\n return (arc * PI) / static_cast<Real>(180);\n}\n\nconstexpr Real RadianToArc(Real radian) {\n return (radian * static_cast<Real>(180)) / PI;\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Point {\nprivate:\n Real x_{}, y_{};\npublic:\n /* constructor */\n Point() = default;\n Point(Real x, Real y) : x_{x}, y_{y} {}\n\n /* getter, setter */\n Real x() const {\n return x_;\n }\n Real& x() {\n return x_;\n }\n Real y() const {\n return y_;\n }\n Real& y() {\n return y_;\n }\n\n /* operator */\n Point& operator+=(const Point& rhs) {\n x_ += rhs.x();\n y_ += rhs.y();\n return *this;\n }\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point{lhs} += rhs;\n }\n Point operator+() const {\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n x_ -= rhs.x();\n y_ -= rhs.y();\n return *this;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point{lhs} -= rhs;\n }\n Point operator-() const {\n return Point{} - *this;\n }\n Point& operator*=(Real k) {\n x_ *= k;\n y_ *= k;\n return *this;\n }\n friend Point operator*(Real k, const Point& p) {\n return Point{p} *= k;\n }\n friend Point operator*(const Point& p, Real k) {\n return Point{p} *= k;\n }\n Point& operator/=(Real k) {\n assert(!Zero(k));\n x_ /= k;\n y_ /= k;\n return *this;\n }\n friend Point operator/(Real k, const Point& p) {\n return Point{p} /= k;\n }\n friend Point operator/(const Point& p, Real k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& lhs, const Point& rhs) {\n return Equal(lhs.x(), rhs.x()) and Equal(lhs.y(), rhs.y());\n }\n friend bool operator!=(const Point& lhs, const Point& rhs) {\n return !Equal(lhs.x(), rhs.x()) or !Equal(lhs.y(), rhs.y());\n }\n friend bool operator<(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and Smaller(lhs.y(), rhs.y()));\n }\n friend bool operator<=(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and (Smaller(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend bool operator>(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and Bigger(lhs.y(), rhs.y()));\n }\n friend bool operator>=(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and (Bigger(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x_ >> p.y_;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x_ << ',' << p.y_ << ')';\n return os;\n }\n \n /* member function */\n Real normSquare() const {\n return Square(x_) + Square(y_);\n }\n Real norm() const {\n return Sqrt(normSquare());\n }\n void normalize() {\n assert((*this) != Point{});\n (*this) /= norm(); \n }\n Point normalized() const {\n Point res{*this};\n res.normalize();\n return res;\n }\n Point rotated(Real radian) const {\n return Point{\n x_ * cosl(radian) - y_ * sinl(radian),\n x_ * sinl(radian) + y_ * cosl(radian)\n };\n }\n void rotate(Real radian) {\n *this = rotated(radian); \n }\n Point rotatedByArc(Real arc) const {\n return rotated(ArcToRadian(arc));\n }\n void rotateByArc(Real arc) {\n *this = rotatedByArc(arc);\n }\n Real argument() const {\n return (Negative(y_) ? TAU : static_cast<Real>(0)) + atan2l(y_, x_);\n }\n Real argumentByArc() const {\n return RadianToArc(argument());\n }\n\n /* friend function */\n friend Real Dot(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.x() + lhs.y() * rhs.y();\n }\n friend Real Cross(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.y() - lhs.y() * rhs.x();\n }\n friend Real Argument(const Point& lhs, const Point& rhs) {\n return rhs.argument() - lhs.argument();\n }\n friend bool ArgComp(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.argument(), rhs.argument());\n }\n};\n\nusing Vector = Point;\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Polygon.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Relation.hpp\"\n\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Relation.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nenum RELATION {\n // p0 -> p1 -> p2の順で直線上に並んでいる\n ONLINE_FRONT = -2,\n // (p1 - p0) -> (p2 - p0)が時計回りになっている\n CLOCKWISE,\n // p0 -> p2 -> p1の順で直線上に並んでいる\n ON_SEGMENT,\n // (p1 - p0) -> (p2 - p0)が反時計回りになっている\n COUNTER_CLOCKWISE,\n // p2 -> p0 -> p1、またはp1 -> p0 -> p2の順で直線上に並んでいる\n ONLINE_BACK\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (Smaller(a.normSquare(), b.normSquare())) return ONLINE_FRONT;\n return ON_SEGMENT;\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Polygon.hpp\"\n\n#include <algorithm>\n#line 10 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Polygon.hpp\"\n#include <vector>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Polygon {\nprivate:\n std::vector<Point> data_;\npublic:\n /* member */\n usize size() const {\n return data_.size();\n }\n\n /* constructor */\n Polygon() = default;\n Polygon(const Polygon& polygon) : data_{polygon.data_} {}\n Polygon(const std::vector<Point>& data) : data_{data} {}\n Polygon(usize n) : data_{n} {\n assert(n >= static_cast<usize>(3));\n }\n\n /* operator[] */\n Point& operator[](usize i) {\n assert(i < size());\n return data_[i];\n }\n const Point& operator[](usize i) const {\n assert(i < size());\n return data_[i];\n }\n Polygon& operator=(const Polygon& polygon) {\n data_ = polygon.data_;\n return *this;\n }\n friend std::istream& operator>>(std::istream& is, Polygon& polygon) {\n for (size_t i{} ; i < polygon.size() ; i++) {\n is >> polygon[i];\n }\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Polygon& polygon) {\n for (usize i{} ; i < polygon.size() ; i++) {\n std::cout << polygon[i] << (i + 1 == polygon.size() ? \"\" : \" \");\n }\n return os;\n }\n\n /* member function */\n void orderRotate(usize i) {\n assert(i < size());\n std::rotate(data_.begin(), data_.begin() + i, data_.end());\n }\n void headMinimize() {\n auto index{std::distance(data_.begin(), std::min_element(data_.begin(), data_.end()))};\n orderRotate(index);\n }\n bool isConvex() const {\n assert(size() >= static_cast<usize>(3));\n for (usize i{} ; i < size() ; i++) {\n if (Relation(data_[i], data_[i+1==size()?0:i+1], data_[i+2>=size()?i+2-size():i+2])\n == CLOCKWISE) {\n return false;\n }\n }\n return true;\n }\n Real area() const {\n assert(size() >= static_cast<usize>(3));\n Real res{};\n for (usize i{1} ; i < size() ; i++) {\n res += Cross(data_[i] - data_[0], data_[i+1==size()?0:i+1] - data_[0]);\n }\n return res / static_cast<Real>(2);\n }\n void pushBack(const Point& p) {\n data_.push_back(p);\n }\n void emplaceBack(Real x, Real y) {\n data_.emplace_back(x, y);\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Line.hpp\"\n\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Line.hpp\"\n\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Line.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Line {\nprivate:\n Point p0_{}, p1_{};\npublic:\n /* constructor */\n Line() = default;\n Line(const Point& p0, const Point& p1) : p0_{p0}, p1_{p1} {}\n // y = ax + b \n Line(Real a, Real b) : p0_{static_cast<Real>(0), b}, p1_{static_cast<Real>(1), a + b} {}\n\n /* getter, setter */\n const Point& p0() const {\n return p0_;\n }\n Point& p0() {\n return p0_;\n }\n const Point& p1() const {\n return p1_;\n }\n Point& p1() {\n return p1_;\n }\n\n /* operator */\n friend bool operator==(const Line& l0, const Line& l1) {\n return Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0())) and Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l0.p0()));\n }\n friend bool operator!=(const Line& l0, const Line& l1) {\n return !Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0())) or !Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l0.p0()));\n }\n\n /* member function */\n bool valid() const {\n return p0_ != p1_;\n }\n Vector slope() const {\n assert(valid());\n return Vector{p1() - p0()}.normalized();\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Intersect/LineAndLine.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Intersect/LineAndLine.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Intersect/LineAndLine.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nbool Intersect(const Line& l0, const Line& l1) {\n assert(l0.valid());\n assert(l1.valid());\n if (!Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0()))) {\n return true;\n }\n else if (!Zero(Cross(l0.p1() - l0.p0(), l1.p0() - l0.p0()))) {\n return false;\n }\n else {\n return true;\n }\n}\n\n} // namespace geometryR2\n\n} // namespace \n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/CrossPoint/LineAndLine.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/CrossPoint/LineAndLine.hpp\"\n\n#line 8 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/CrossPoint/LineAndLine.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nPoint CrossPoint(const Line& l0, const Line& l1) {\n assert(l0.valid());\n assert(l1.valid());\n assert(Intersect(l0, l1));\n assert(l0 != l1);\n return l0.p0() + (l0.p1() - l0.p0()) * \n (Cross(l1.p0() - l0.p0(), l1.p1() - l1.p0()) / Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0()));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Contain/PolygonContainsPoint.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Contain/State.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nenum ContainState {\n INSIDE,\n ONLINE,\n OUTSIDE\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Contain/PolygonContainsPoint.hpp\"\n\n#line 11 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Contain/PolygonContainsPoint.hpp\"\n#include <utility>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nContainState PolygonContainsPoint(const Polygon& polygon, const Point& p) {\n usize n{polygon.size()};\n assert(n >= static_cast<usize>(3));\n bool odd{};\n for (usize i{} ; i < n ; i++) {\n if (polygon[i] == p) {\n return ONLINE;\n }\n if (Relation(polygon[i], polygon[i+1==n?0:i+1], p) == ON_SEGMENT) {\n return ONLINE;\n }\n Vector a{polygon[i] - p}, b{polygon[i+1==n?0:i+1] - p};\n if (Bigger(a.y(), b.y())) std::swap(a, b);\n odd ^= !Positive(a.y()) and Positive(b.y()) and Positive(Cross(a, b));\n }\n return (odd ? INSIDE : OUTSIDE);\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 10 \"2009.test.cpp\"\n\n#line 13 \"2009.test.cpp\"\n#include <set>\n\nusing namespace zawa;\nusing namespace geometryR2;\n\nbool contain(const Point& p) {\n static Polygon sq(4);\n sq[0] = Point{-100, -100};\n sq[1] = Point{100, -100};\n sq[2] = Point{100, 100};\n sq[3] = Point{-100, 100};\n return PolygonContainsPoint(sq, p) == INSIDE;\n}\n\nbool solve() {\n int n; std::cin >> n;\n if (n == 0) return false;\n std::vector<Line> l;\n l.reserve(n);\n int ans{1};\n for (int _{} ; _ < n ; _++) {\n Line a;\n std::cin >> a.p0() >> a.p1();\n std::set<Point> set;\n for (const auto line : l) {\n if (!Intersect(line, a)) continue;\n Point p{CrossPoint(line, a)};\n if (contain(p)) set.insert(p);\n }\n ans += set.size() + 1;\n l.push_back(a); \n }\n std::cout << ans << '\\n';\n return true;\n}\n\nint main() {\n SetFastIO();\n for (int i{} ; solve() ; i++) {\n\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3400, "score_of_the_acc": -0.1981, "final_rank": 16 }, { "submission_id": "aoj_2009_8215905", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n\n#define EPS 1e-10\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n\n P operator-(P p) const { return P(x - p.x, y - p.y); }\n bool operator==(const P &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; }\n};\n\ntypedef std::pair<P, P> Segment;\n\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\n\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS) return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS) return CLOCKWISE;\n if (dot(a, b) < -EPS) return ONLINE_BACK;\n if (norm(a) < norm(b)) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool isIntersect(P p1, P p2, P p3, P p4) {\n return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);\n}\n\nP crosspoint(P p1, P p2, P p3, P p4) {\n double A = cross(p2 - p1, p4 - p3);\n double B = cross(p2 - p1, p2 - p3);\n return P(p3.x + (p4.x - p3.x) * (B / A), p3.y + (p4.y - p3.y) * (B / A));\n}\n\nint main() {\n int n;\n while (std::cin >> n && n) {\n int field = 1;\n std::vector<Segment> pos;\n for (int i = 0; i < n; i++) {\n Segment pesh;\n std::cin >> pesh.first.x >> pesh.first.y >> pesh.second.x >> pesh.second.y;\n pos.push_back(pesh);\n std::vector<P> exist;\n int x_counter = 0;\n for (int i = 0; i < int(pos.size()) - 1; i++) {\n if (isIntersect(pos[i].first, pos[i].second, pos.back().first, pos.back().second)) {\n P xp = crosspoint(pos[i].first, pos[i].second, pos.back().first, pos.back().second);\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS) continue;\n if (std::find(exist.begin(), exist.end(), xp) == exist.end()) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n std::cout << field << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3056, "score_of_the_acc": -0.0178, "final_rank": 3 }, { "submission_id": "aoj_2009_8215902", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <set>\n#include <vector>\n#define EPS 1e-10\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\nusing namespace std;\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(P p) { return P(x + p.x, y + p.y); }\n P operator-(P p) { return P(x - p.x, y - p.y); }\n P operator*(P p) { return P(x * p.x, y * p.y); }\n P operator/(P p) { return P(x / p.x, y / p.y); }\n bool operator==(const P &p) const {\n return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);\n }\n bool operator<(const P &p) const {\n return x != p.x ? fabs(x - p.x) < EPS : fabs(y - p.y) < EPS;\n }\n};\ntypedef pair<P, P> Segment;\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\ndouble abs(P p) { return sqrt(norm(p)); }\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\nbool isIntersect(P p1, P p2, P p3, P p4) {\n return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);\n}\nP crosspoint(P p1, P p2, P p3, P p4) {\n double A = cross(p2 - p1, p4 - p3);\n double B = cross(p2 - p1, p2 - p3);\n P pro = p4 - p3;\n pro.x = pro.x * (B / A);\n pro.y = pro.y * (B / A);\n return p3 + pro;\n}\nint main() {\n int n, xx, yy, xxx, yyy, field;\n vector<Segment> pos;\n Segment pesh;\n vector<P> exist;\n P xp;\n while (cin >> n && n) {\n field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> xx >> yy >> xxx >> yyy;\n pesh.F.x = xx;\n pesh.F.y = yy;\n pesh.S.x = xxx;\n pesh.S.y = yyy;\n pos.push_back(pesh);\n int x_counter = 0;\n bool new_x = true;\n exist.clear();\n for (int i = 0; i < pos.size() - 1; i++) {\n new_x = true;\n if (isIntersect(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S)) {\n xp = crosspoint(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S);\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS)\n continue;\n for (int j = 0; j < exist.size(); j++) {\n if (exist[j] == xp) {\n new_x = false;\n break;\n }\n }\n if (new_x) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2992, "score_of_the_acc": -0.002, "final_rank": 2 }, { "submission_id": "aoj_2009_8215901", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#define EPS 1e-10\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\nusing namespace std;\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(P p) { return P(x + p.x, y + p.y); }\n P operator-(P p) { return P(x - p.x, y - p.y); }\n P operator*(P p) { return P(x * p.x, y * p.y); }\n P operator/(P p) { return P(x / p.x, y / p.y); }\n bool operator==(const P &p) const {\n return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);\n }\n bool operator<(const P &p) const {\n return x != p.x ? fabs(x - p.x) < EPS : fabs(y - p.y) < EPS;\n }\n};\ntypedef pair<P, P> Segment;\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\ndouble abs(P p) { return sqrt(norm(p)); }\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\nbool isIntersect(P p1, P p2, P p3, P p4) {\n return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);\n}\nP crosspoint(P p1, P p2, P p3, P p4) {\n double A = cross(p2 - p1, p4 - p3);\n double B = cross(p2 - p1, p2 - p3);\n P pro = p4 - p3;\n pro.x = pro.x * (B / A);\n pro.y = pro.y * (B / A);\n return p3 + pro;\n}\nint main() {\n int n, xx, yy, xxx, yyy, field;\n vector<Segment> pos;\n Segment pesh;\n vector<P> exist;\n P xp;\n while (cin >> n && n) {\n field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> xx >> yy >> xxx >> yyy;\n pesh.F.x = xx;\n pesh.F.y = yy;\n pesh.S.x = xxx;\n pesh.S.y = yyy;\n pos.push_back(pesh);\n int x_counter = 0;\n bool new_x = true;\n exist.clear();\n for (int i = 0; i < pos.size() - 1; i++) {\n new_x = true;\n if (isIntersect(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S)) {\n xp = crosspoint(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S);\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS)\n continue;\n for (int j = 0; j < exist.size(); j++) {\n if (exist[j] == xp) {\n new_x = false;\n break;\n }\n }\n if (new_x) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2984, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2009_8116170", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#define EPS 1e-10\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\nusing namespace std;\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(P p) const { return P(x + p.x, y + p.y); }\n P operator-(P p) const { return P(x - p.x, y - p.y); }\n P operator*(P p) const { return P(x * p.x, y * p.y); }\n P operator/(P p) const { return P(x / p.x, y / p.y); }\n bool operator==(const P &p) const {\n return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);\n }\n bool operator<(const P &p) const {\n return x != p.x ? x < p.x : y < p.y;\n }\n};\ntypedef pair<P, P> Segment;\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\ndouble abs(P p) { return sqrt(norm(p)); }\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\nbool isIntersect(Segment s1, Segment s2) {\n return (ccw(s1.F, s1.S, s2.F) * ccw(s1.F, s1.S, s2.S) <= 0 &&\n ccw(s2.F, s2.S, s1.F) * ccw(s2.F, s2.S, s1.S) <= 0);\n}\nP crosspoint(Segment s1, Segment s2) {\n double A = cross(s1.S - s1.F, s2.S - s2.F);\n double B = cross(s1.S - s1.F, s1.S - s2.F);\n P pro = s2.S - s2.F;\n pro.x = pro.x * (B / A);\n pro.y = pro.y * (B / A);\n return s2.F + pro;\n}\nint main() {\n int n, xx, yy, xxx, yyy, field;\n vector<Segment> pos;\n Segment pesh;\n vector<P> exist;\n P xp;\n while (cin >> n && n) {\n field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> xx >> yy >> xxx >> yyy;\n pesh.F.x = xx;\n pesh.F.y = yy;\n pesh.S.x = xxx;\n pesh.S.y = yyy;\n pos.push_back(pesh);\n int x_counter = 0;\n bool new_x = true;\n exist.clear();\n for (int i = 0; i < pos.size() - 1; i++) {\n new_x = true;\n if (isIntersect(pos[i], pos.back())) {\n xp = crosspoint(pos[i], pos.back());\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS) continue;\n if (find(exist.begin(), exist.end(), xp) != exist.end()) {\n new_x = false;\n }\n if (new_x) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3120, "score_of_the_acc": -0.0336, "final_rank": 8 }, { "submission_id": "aoj_2009_8116168", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#define EPS 1e-10\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\nusing namespace std;\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(P p) const { return P(x + p.x, y + p.y); }\n P operator-(P p) const { return P(x - p.x, y - p.y); }\n P operator*(P p) const { return P(x * p.x, y * p.y); }\n P operator/(P p) const { return P(x / p.x, y / p.y); }\n bool operator==(const P &p) const {\n return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);\n }\n bool operator<(const P &p) const {\n return x != p.x ? x < p.x : y < p.y;\n }\n};\ntypedef pair<P, P> Segment;\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\ndouble abs(P p) { return sqrt(norm(p)); }\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\nbool isIntersect(Segment s1, Segment s2) {\n return (ccw(s1.F, s1.S, s2.F) * ccw(s1.F, s1.S, s2.S) <= 0 &&\n ccw(s2.F, s2.S, s1.F) * ccw(s2.F, s2.S, s1.S) <= 0);\n}\nP crosspoint(Segment s1, Segment s2) {\n double A = cross(s1.S - s1.F, s2.S - s2.F);\n double B = cross(s1.S - s1.F, s1.S - s2.F);\n P pro = s2.S - s2.F;\n pro.x = pro.x * (B / A);\n pro.y = pro.y * (B / A);\n return s2.F + pro;\n}\nint main() {\n int n, xx, yy, xxx, yyy, field;\n vector<Segment> pos;\n Segment pesh;\n vector<P> exist;\n P xp;\n while (cin >> n && n) {\n field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> xx >> yy >> xxx >> yyy;\n pesh.F.x = xx;\n pesh.F.y = yy;\n pesh.S.x = xxx;\n pesh.S.y = yyy;\n pos.push_back(pesh);\n int x_counter = 0;\n bool new_x = true;\n exist.clear();\n for (int i = 0; i < pos.size() - 1; i++) {\n new_x = true;\n if (isIntersect(pos[i], pos.back())) {\n xp = crosspoint(pos[i], pos.back());\n if (fabs(100 - fabs(xp.x)) < EPS || fabs(100 - fabs(xp.y)) < EPS)\n continue;\n for (int j = 0; j < exist.size(); j++) {\n if (exist[j] == xp) {\n new_x = false;\n break;\n }\n }\n if (new_x) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3104, "score_of_the_acc": -0.0297, "final_rank": 6 }, { "submission_id": "aoj_2009_8113152", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#define EPS 1e-10\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 0\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\nusing namespace std;\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(P p) { return P(x + p.x, y + p.y); }\n P operator-(P p) { return P(x - p.x, y - p.y); }\n P operator*(P p) { return P(x * p.x, y * p.y); }\n P operator/(P p) { return P(x / p.x, y / p.y); }\n bool operator==(const P &p) const {\n return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);\n }\n bool operator<(const P &p) const {\n return x != p.x ? fabs(x - p.x) < EPS : fabs(y - p.y) < EPS;\n }\n};\ntypedef pair<P, P> Segment;\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\ndouble abs(P p) { return sqrt(norm(p)); }\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\nbool isIntersect(P p1, P p2, P p3, P p4) {\n return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);\n}\nP crosspoint(P p1, P p2, P p3, P p4) {\n double A = cross(p2 - p1, p4 - p3);\n double B = cross(p2 - p1, p2 - p3);\n P pro = p4 - p3;\n pro.x = pro.x * (B / A);\n pro.y = pro.y * (B / A);\n return p3 + pro;\n}\nint main() {\n int n, xx, yy, xxx, yyy, field;\n vector<Segment> pos;\n Segment pesh;\n vector<P> exist;\n P xp;\n while (cin >> n && n) {\n field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> xx >> yy >> xxx >> yyy;\n pesh.F.x = xx;\n pesh.F.y = yy;\n pesh.S.x = xxx;\n pesh.S.y = yyy;\n pos.push_back(pesh);\n int x_counter = 0;\n bool new_x = true;\n exist.clear();\n for (int i = 0; i < pos.size() - 1; i++) {\n new_x = true;\n if (isIntersect(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S)) {\n xp = crosspoint(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S);\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS)\n continue;\n for (int j = 0; j < exist.size(); j++) {\n if (exist[j] == xp) {\n new_x = false;\n break;\n }\n }\n if (new_x) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3104, "score_of_the_acc": -0.0297, "final_rank": 6 }, { "submission_id": "aoj_2009_8113147", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#define EPS 1e-9\n#define F first\n#define S second\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1\n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\nusing namespace std;\nstruct P {\n double x, y;\n P(double x = 0, double y = 0) : x(x), y(y) {}\n P operator+(P p) { return P(x + p.x, y + p.y); }\n P operator-(P p) { return P(x - p.x, y - p.y); }\n P operator*(P p) { return P(x * p.x, y * p.y); }\n P operator/(P p) { return P(x / p.x, y / p.y); }\n bool operator==(const P &p) const {\n return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);\n }\n bool operator<(const P &p) const {\n return x != p.x ? fabs(x - p.x) < EPS : fabs(y - p.y) < EPS;\n }\n};\ntypedef pair<P, P> Segment;\ndouble dot(P a, P b) { return a.x * b.x + a.y * b.y; }\ndouble cross(P a, P b) { return a.x * b.y - a.y * b.x; }\ndouble norm(P p) { return p.x * p.x + p.y * p.y; }\ndouble abs(P p) { return sqrt(norm(p)); }\nint ccw(P p, P q, P r) {\n P a = q - p;\n P b = r - p;\n if (cross(a, b) > EPS)\n return COUNTER_CLOCKWISE;\n if (cross(a, b) < -EPS)\n return CLOCKWISE;\n if (dot(a, b) < -EPS)\n return ONLINE_BACK;\n if (norm(a) < norm(b))\n return ONLINE_FRONT;\n return ON_SEGMENT;\n}\nbool isIntersect(P p1, P p2, P p3, P p4) {\n return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);\n}\nP crosspoint(P p1, P p2, P p3, P p4) {\n double A = cross(p2 - p1, p4 - p3);\n double B = cross(p2 - p1, p2 - p3);\n P pro = p4 - p3;\n pro.x = pro.x * (B / A);\n pro.y = pro.y * (B / A);\n return p3 + pro;\n}\nint main() {\n int n, xx, yy, xxx, yyy, field;\n vector<Segment> pos;\n Segment pesh;\n vector<P> exist;\n P xp;\n while (cin >> n && n) {\n field = 1;\n pos.clear();\n for (int i = 0; i < n; i++) {\n cin >> xx >> yy >> xxx >> yyy;\n pesh.F.x = xx;\n pesh.F.y = yy;\n pesh.S.x = xxx;\n pesh.S.y = yyy;\n pos.push_back(pesh);\n int x_counter = 0;\n bool new_x = true;\n exist.clear();\n for (int i = 0; i < pos.size() - 1; i++) {\n new_x = true;\n if (isIntersect(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S)) {\n xp = crosspoint(pos[i].F, pos[i].S, pos[pos.size() - 1].F,\n pos[pos.size() - 1].S);\n if (100 - fabs(xp.x) < EPS || 100 - fabs(xp.y) < EPS)\n continue;\n for (int j = 0; j < exist.size(); j++) {\n if (exist[j] == xp) {\n new_x = false;\n break;\n }\n }\n if (new_x) {\n x_counter++;\n exist.push_back(xp);\n }\n }\n }\n field += x_counter + 1;\n }\n cout << field << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3084, "score_of_the_acc": -0.0247, "final_rank": 5 } ]
aoj_2008_cpp
Problem C: Dragon Fantasy 永遠に続くと思われていた平和は突然終わりを告げた。 はるか昔に封印されていた魔王がついに復活を遂げたのである。 だが、今まさに世界が闇に覆われようとしているとき、1 人の勇者が現れた。 そしてその勇者は、世界各地に散らばっている伝説のクリスタルを集める旅に出た。 伝説によると、全てのクリスタルを集めることができたならば、どのような願いでもかなえられるという伝説の龍神を呼び出すことができると伝えられている。 その龍神の力を借りれば魔王を倒すことも可能なはずだ。 クリスタルは世界各地に散らばっている。 勇者はそれらを 1 つずつ、自らの手で集めていく必要がある。 なぜなら、勇者以外の者がクリスタルを手に入れたならば、魔王の手先によって奪われてしまうかもしれないからである。 勇者は、1 日あたりユークリッド距離で 1 だけ移動することができる。 しかし、ここで 1 つの重大な問題がある。 魔王は、常に闇の瘴気をまき散らしており、その瘴気に汚染された場所は人が立ち入ることのできない死の大地と化してしまう。 たとえ勇者であってもその場所に立ち入ることはできない。 さらに、その瘴気は時間とともに同心円状に広まっていくため、 時間の経過とともに勇者の移動できる領域は減少していってしまう。 瘴気は 1 日あたりユークリッド距離 1 だけ広がることが確認されている。 そして、その境界線上にあるクリスタルを勇者は取ることはできない。また、復活したばかりの魔王は力を蓄えるために動かないことも分かっている。 勇者は一刻も早くクリスタルを手に入れなければならない。 しかし、もし全てのクリスタルを手に入れることが不可能であるならば、別の手段を考えなければならないだろう。 そこで、あなたには勇者の初期位置と魔王の復活した場所、そしてクリスタルの存在する位置から、全てのクリスタルを手に入れることができるかどうかを調べるプログラムを作っていただきたい。 ちなみに、勇者は疲れない。 そして眠らない。 全てのクリスタルを集めて世界を救うまでは絶えず動き続け、クリスタルを集めるのだ!! Input 入力は複数のテストケースから構成される。 各テストケースの先頭の行では 5 つの整数 n (0 < n <= 20)、 h x 、 h y 、 d x 、 d y が与えられる。 n はクリスタルの個数、( h x , h y ) は魔王が復活した瞬間での勇者の位置、( d x , d y ) は魔王が復活した位置である。 後に続く n 行には、各クリスタルの位置を表す 2 つの整数 c x 、 c y が与えられる。 入力は n = h x = h y = d x = d y = 0 のときに終了し、これはテストケースには含まない。 入力で与えられる全ての座標は絶対値が 1000 以下の整数であることが保証されている。 Output 各テストケースについて、全てのクリスタルを集めることができるならば「YES」と、そうでないならば「NO」と出力せよ。 Sample Input 2 0 0 10 10 1 1 4 4 2 0 0 10 10 1 1 6 6 2 0 0 10 10 1 1 5 5 0 0 0 0 0 Output for the Sample Input YES NO NO
[ { "submission_id": "aoj_2008_9626361", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long double ld;\n#define FOR(i,l,r) for(auto i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define F first\n#define S second\nint main(){\n while(1){\n int N;cin>>N;\n vector<pair<int,int>>C(N+2);\n cin>>C[N].first>>C[N].second;\n cin>>C[N+1].first>>C[N+1].second;\n if(!N)return 0;\n REP(i,N)cin>>C[i].first>>C[i].second;\n auto f=[](pair<int,int>x,pair<int,int>y){\n return sqrt((ld)(x.F-y.F)*(x.F-y.F)+(x.S-y.S)*(x.S-y.S));\n };\n bool ans=0;\n int st=0,cnt=0;\n ld t=0;\n function<void(int)>dfs=[&](int v){\n if(t>=f(C[N+1],C[v]))return;\n if(cnt==N){ans=1;return;}\n REP(i,N)if((st>>i)%2==0&&t+f(C[i],C[v])>=f(C[i],C[N+1]))return;\n REP(i,N)if((st>>i)%2==0){\n cnt++;\n st+=1<<i;\n t+=f(C[v],C[i]);\n dfs(i);\n if(ans)return;\n cnt--;\n st-=1<<i;\n t-=f(C[v],C[i]);\n }\n };\n dfs(N);\n if(ans)cout<<\"YES\"<<endl;\n else cout<<\"NO\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3312, "score_of_the_acc": -0.0625, "final_rank": 8 }, { "submission_id": "aoj_2008_9381049", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst double eps = 1e-10;\nbool dfs(double x2, double y2, vector<pair<double, double>> &c, vector<bool> &used, int id, double d) {\n int n = c.size();\n bool res = false;\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n if (used[i]) {\n cnt++;\n } else {\n double nd = d + hypot(c[i].first - c[id].first, c[i].second - c[id].second);\n double md = hypot(c[i].first - x2, c[i].second - y2);\n if (nd >= md - eps) {\n return false;\n }\n }\n }\n if (cnt == n) return true;\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n double nd = d + hypot(c[id].first - c[i].first, c[id].second - c[i].second);\n double md = hypot(c[i].first - x2, c[i].second - y2);\n if (nd < md) {\n used[i] = true;\n res |= dfs(x2, y2, c, used, i, nd);\n used[i] = false; \n }\n if (res) return res;\n }\n }\n return res;\n}\nint main() {\n while (1) {\n int n, x1, y1, x2, y2;\n cin >> n >> x1 >> y1 >> x2 >> y2;\n if (n == 0 && x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0) {\n break;\n }\n vector<pair<double, double>> c(n);\n for (int i = 0; i < n; i++) {\n cin >> c[i].first >> c[i].second;\n }\n vector<bool> used(n, false);\n bool ans = false;\n for (int i = 0; i < n; i++) {\n double d = hypot(x1 - c[i].first, y1 - c[i].second);\n double md = hypot(x2 - c[i].first, y2 - c[i].second);\n if (d + eps < md) {\n used[i] = true;\n ans |= dfs(x2, y2, c, used, i, d);\n used[i] = false;\n }\n if (ans) break;\n }\n cout << (ans ? \"YES\" : \"NO\") << endl;\n }\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3424, "score_of_the_acc": -0.1051, "final_rank": 15 }, { "submission_id": "aoj_2008_4949850", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nconst double pi = 3.141592653589793238462643383279;\nusing namespace std;\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a) * (a))\n#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(c) sort((c).begin(), (c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\nconst double EPS = 1e-8, PI = acos(-1);\n//ここから編集\nusing Point = complex< double >;\nbool eq(double a, double b){ return fabs(a-b) < EPS; }\nnamespace std {\n bool operator<(const Point a, const Point b) {\n if(eq(a.real(),b.real())) return a.imag() < b.imag();\n return a.real() < b.real();\n }\n}\n\nistream &operator>> (istream &is, Point &p) {\n double a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<< (ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nbool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n}\n\n// rotate Φ(rad)\n// x = r * cos(θ + Φ)\n// = r * cos(θ) * cos(Φ) - r * sin(θ) * sin(Φ)\n// = x * cos(Φ) - y * sin(Φ) (∵ cos(θ) = x/r, sin(θ) = y/r) \nPoint rotate(double phi, const Point &p) {\n double x = p.real(), y = p.imag();\n return Point(x * cos(phi) - y * sin(phi), x * sin(phi) + y * cos(phi));\n}\n\ndouble radian_to_degree(double r) {\n return (r * 180.0 / PI);\n}\n\ndouble degree_to_radian(double d) {\n return (d * PI / 180.0);\n}\n\nstruct Line{\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b){}\n\n Line(double A, double B, double C){\n //ax + by = c\n if(eq(A, 0)){\n a = Point(0, C/B), b = Point(1, C/B);\n }else if(eq(B, 0)){\n a = Point(C/A, 0), b = Point(C/A, 1);\n }else{\n a = Point(0, C/B), b = Point(C/A, 0);\n }\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n friend ostream &operator<<(ostream &os, Line &a) {\n return os << a.a << \" to \" << a.b;\n }\n};\n\nstruct Segment: Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n double r;\n\n Circle() = default;\n\n Circle(Point p, double r): p(p), r(r){}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\ndouble cross(const Point &a, const Point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\ndouble dot(const Point& a, const Point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n//https://mathtrain.jp/projection\nPoint projection(const Line &l, const Point &p){\n double t = dot(p - l.a, l.a-l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p){\n double t = dot(p - l.a, l.b-l.a) / norm(l.a - l.b);\n return l.a + (l.b - l.a) * t;\n}\n\nPoint reflection(const Line &l, const Point &p){\n return p + (projection(l, p) - p) * 2.0;\n}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if(cross(b-a, c-a) > EPS) return 1; // \"COUNTER_CLOCKWISE\"\n if(cross(b-a, c-a) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b-a, c-a) < -EPS) return 2; // \"ONLINE_BACK\" c-a-b\n if(norm(b-a) < norm(c-a) - EPS) return -2; // \"ONLINE_FRONT\" a-b-c\n return 0; // \"ON_SEGMENT\" a-c-b\n}\n\nbool parallel(const Line &a, const Line &b){\n return eq(cross(a.a-a.b, b.a-b.b), 0.0);\n}\nbool orthogonal(const Line &a, const Line &b){\n return eq(dot(a.a-a.b, b.a-b.b), 0.0);\n}\nenum { OUT, ON, IN };\n\nint contains(const Polygon& Q, const Point& p){\n bool in = false;\n for(int i=0; i<Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i+1)%Q.size()]-p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\nbool intersect(const Segment &s, const Point &p){\n return ccw(s.a, s.b, p) == 0;\n}\n\n//直線の交差判定\nbool intersect(const Line &a, const Line &b) {\n if(parallel(a, b)) return 0;\n return 1;\n}\n\n//線分が重なる/端点で交わるときtrue\nbool intersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 && ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\n\nbool intersect(const Line &l, const Segment &s) {\n \n}\n\nPoint crosspoint(const Line &a, const Line &b){\n double d = cross(b.b-b.a, a.b-a.a);\n if(eq(d, 0.0)) return Point(1e9, 1e9); \n \n return a.a + (a.b - a.a) * cross(b.b-b.a, b.b-a.a) / d;\n}\n\nPoint crosspoint(const Segment &a, const Segment &b){\n return crosspoint(Line(a.a, a.b), Line(b.a, b.b));\n}\ndouble distance(const Point &a, const Point &b){\n return abs(a - b);\n}\ndouble distance(const Line &l, const Point &p){\n return abs( cross(p - l.a, l.b-l.a) / abs(l.b-l.a) );\n}\ndouble distance(const Segment &s, const Point &p){\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r-p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n// double distance(const Line &a, const Line &b){\n\n// }\n// double distance(const Line &a, const Segment &b){\n\n// }\ndouble distance(const Segment &a, const Segment &b) {\n return intersect(a, b) ? 0 : min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\n/* 2円の交点 */\nvector<Point> crosspointCC(Circle C1, Circle C2){\n vector<Point> ps;\n Point ab = C2.p - C1.p;\n double d = abs(ab);\n double rc = (C1.r * C1.r + d * d - C2.r * C2.r) / (2 * d);\n if(eq(d, 0) || C1.r < abs(rc)) return ps;\n\n double rs = sqrt(C1.r * C1.r - rc*rc);\n\n Point abN = ab * Point(0, rs/d);\n Point cp = C1.p + rc / d * ab;\n ps.push_back(cp + abN);\n if(!eq(norm(abN), 0))ps.push_back(cp-abN);\n return ps;\n}\n\nvector<Point> crosspointCL(Circle C, Line l){\n Point p = projection(l, C.p);\n\n Point e = (l.b-l.a)/abs(l.b-l.a);\n if(eq(distance(l, C.p), C.r)) {\n return vector<Point>{p, p};\n }\n double base = sqrt(C.r*C.r-norm(p-C.p));\n \n return vector<Point>{p+e*base, p-e*base};\n}\n\n/* 円同士の共通部分の面積を求める */\n// verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_I date: 2020/10/11\nlong double AreaofCC(Circle& C1, Circle& C2){\n if(C1.r > C2.r) swap(C1, C2);\n long double nor = norm(C1.p - C2.p);\n long double dist = sqrtl(nor);\n \n if(C1.r + C2.r < dist+EPS) return 0;\n\n if(dist + C1.r < C2.r + EPS) return C1.r * C1.r * PI;\n\n long double theta1 = acosl((nor + C1.r*C1.r - C2.r * C2.r)/(2*C1.r*dist));\n\n long double theta2 = acosl((nor + C2.r*C2.r - C1.r * C1.r)/(2*C2.r*dist));\n \n return (theta1 - sinl(theta1+ theta1) *0.5) * C1.r * C1.r + (theta2 - sinl(theta2+theta2) *0.5) * C2.r * C2.r;\n}\n\n\nPolygon convex_hull(Polygon &p)\n{\n int n = (int)p.size(), k = 0;\n if (n <= 2)\n return p;\n sort(p.begin(), p.end());\n vector<Point> ch(n * 2);\n\n for (int i = 0; i < n; ch[k++] = p[i++])\n {\n while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n {\n while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\ndouble closestpair(Points &a, int l, int r){\n if(r-l<=1) return 1e20;\n int mid = (l+r)/2;\n\n double X = a[mid].real();\n double d = min(closestpair(a, l, mid), closestpair(a, mid, r));\n inplace_merge(a.begin()+l, a.begin()+mid, a.begin()+r, [](const Point& pa, const Point& pb){\n return pa.imag() < pb.imag();\n });\n\n Points b;\n for(int i=l; i<r; i++){\n if(abs(a[i].real()-X) >= d) continue;\n for(int j=b.size()-1; j>=0; j--){\n if(abs((a[i]-b[j]).imag()) >= d) break;\n d = min(d, abs(a[i]-b[j]));\n }\n b.push_back(a[i]);\n }\n return d;\n}\n\n/* 円の交差判定 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/finder.jsp?course=CGL date:2020/10/11 */\nint intersectionCC(Circle c1, Circle c2){\n if(c1.r > c2.r) swap(c1, c2);\n double d1 = abs(c1.p-c2.p); \n double d2 = c1.r + c2.r;\n if(d1 > d2) return 4; /* 互いに交点を持たない */\n else if(d1 == d2) return 3; /* 外接する場合 */\n else{\n if(c2.r == c1.r + d1) return 1; /* 内接する場合 */\n else if(c2.r > c1.r + d1) return 0; /* 包含 */\n else return 2; /* 交点を2つ持つ */\n }\n}\n\n/* 点集合が反時計回りに与えられる場合のみ */\ndouble Area(Points &g){\n double res = 0;\n int n = g.size();\n REP(i,n){\n res += cross(g[i], g[(i+1)%n]);\n }\n return res/2.0;\n}\n\n/* 内接円 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B&lang=ja date: 2020/10/11 */\npair<Point, double> incircle_of_a_Triangle(Points &p){\n\n double a = abs(p[1]-p[2]), b = abs(p[2]-p[0]), c = abs(p[0]-p[1]);\n double s = (a+b+c) / 2.0;\n double S = sqrtl(s*(s-a)*(s-b)*(s-c));\n double r = S/s; \n \n Point pp = (a*p[0]+b*p[1]+c*p[2])/(a+b+c);\n return make_pair(pp, r);\n} \n\n/* 外接円 */\npair<Point, double> circumscribed_circle_of_a_triangle(Points &p){\n\n Point m1((p[0]+p[1])/2.0), m2((p[1]+p[2])/2.0);\n Point v((p[1]-p[0]).imag(), (p[0]-p[1]).real()), w((p[1]-p[2]).imag(), (p[2]-p[1]).real());\n\n Line l1(m1, Point(v+m1)), l2(m2, Point(w+m2));\n\n Point x = crosspoint(l1, l2);\n double r = abs(x-p[0]);\n return make_pair(x, r);\n}\n\n/* ある点pを通る円cの接線 */\nPoints tangent_to_a_circle(Point p, Circle C){\n Points ps;\n double d = abs(C.p-p);\n if(eq(d,0)){\n ps.push_back(p);\n }else if(d>EPS){\n \n double d2 = sqrt(d*d-C.r*C.r);\n \n long double theta = acosl(d2/d);\n //cout << theta << endl;\n Point pp = C.p - p;\n Point p2 = rotate(-theta, pp);\n\n Point e = p2/abs(p2);\n Point ppp = e*d2;\n ps.push_back(p + ppp);\n p2 = rotate(theta, pp);\n e = p2/abs(p2);\n ppp = e*d2;\n ps.push_back(p + ppp);\n \n }\n return ps;\n}\n\nLines tangent(Circle C1, Circle C2){\n Lines ls;\n if(C1.r < C2.r) swap(C1, C2);\n double d = norm(C1.p - C2.p);\n if(eq(d, 0)) return ls;\n Point u = (C2.p - C1.p) / sqrt(d);\n Point v = rotate(pi/2, u);\n\n for(double s: {-1, 1}) {\n double h = (C1.r + s * C2.r) / sqrt(d);\n if(eq(1-h*h, 0)) {\n ls.emplace_back(C1.p + u * C1.r, C1.p + (u+v) * C1.r);\n }else if(1-h*h>0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ls.emplace_back(C1.p + (uu+vv) * C1.r, C2.p - (uu+vv) * C2.r * s);\n ls.emplace_back(C1.p + (uu-vv) * C1.r, C2.p - (uu-vv) * C2.r * s);\n }\n }\n return ls;\n}\n\nusing ld = long double;\nld dist(ld sx, ld sy, ld tx, ld ty){\n return sqrtl((sx-tx)*(sx-tx) + (sy-ty)*(sy-ty));\n}\nint n;\nld hx, hy, dx, dy;\nvector<double> cx(25), cy(25);\nbool dfs(ld x, ld y, ld t, int bit){\n \n if(bit == (1<<n)-1){\n return true;\n }\n \n REP(i,n){\n if(bit >> i & 1) continue;\n if(t + dist(x, y, cx[i], cy[i]) + EPS > dist(dx, dy, cx[i], cy[i])) return false;\n }\n\n for(int i=0; i<n; i++){\n if(bit >> i & 1) continue;\n\n ld d = dist(x, y, cx[i], cy[i]);\n ld mao = dist(dx, dy, cx[i], cy[i]);\n if(d+t + EPS > mao) return false;\n if(dfs(cx[i], cy[i], t+d, bit | (1 << i))) return true;\n }\n return false;\n}\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(6);\n \n \n while(cin >> n >> hx >> hy >> dx >> dy){\n if(n==0&&hx==0&&hy==0&&dx==0&&dy==0) break;\n REP(i,n){\n cin >> cx[i] >> cy[i];\n }\n \n if(dfs(hx, hy, 0.0, 0)) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3288, "score_of_the_acc": -0.0683, "final_rank": 11 }, { "submission_id": "aoj_2008_4804706", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <queue>\nusing namespace std;\nconst double EPS = 1e-8;\nint n;\nint x[22],y[22];\ndouble di[22][22];\ndouble dist(double sx, double sy, double gx, double gy){\n return sqrt((gx-sx)*(gx-sx) +(gy-sy)*(gy-sy));\n}\nbool used[21];\nbool dfs(int c, double d, int m){\n if(n == m) return true;\n for(int i=1; i<n; i++){\n if(used[i]) continue;\n if(d +di[c][i] +EPS > di[i][n]){\n return false;\n }\n }\n for(int i=1; i<n; i++){\n if(used[i]) continue;\n used[i] = true;\n if(dfs(i, d+di[c][i], m+1)){\n return true;\n }\n used[i] = false;\n }\n return false;\n}\n\nint main(){\n while(1){\n int sx,sy,dx,dy;\n cin >> n >> sx >> sy >> dx >> dy;\n if(n == 0) break;\n\n x[0] = sx;\n y[0] = sy;\n for(int i=0; i<n; i++){\n cin >> x[i+1] >> y[i+1];\n }\n n++;\n x[n] = dx;\n y[n] = dy;\n for(int i=1; i<n; i++){\n used[i] = false;\n }\n for(int i=0; i<n+1; i++){\n for(int j=0; j<n+1; j++){\n di[i][j] = di[j][i] = dist(x[i],y[i],x[j],y[j]);\n }\n }\n if(dfs(0, 0, 1)){\n cout << \"YES\" << endl;\n }else{\n cout << \"NO\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3112, "score_of_the_acc": -0.028, "final_rank": 5 }, { "submission_id": "aoj_2008_3618932", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nint N,M,K,L,R,H,W;\n\nconst long long int MOD=1000000007;\nconst float EPS=1e-8;\n\nfloat dis(float a,float c,float b,float d){\n\treturn sqrt((a-b)*(a-b)+(c-d)*(c-d));\n}\n\nfloat dp[1<<20][20];\n\nint main(){\n\n\tint N;\n\tfloat sx,sy,gx,gy;\n\twhile(cin>>N>>sx>>sy>>gx>>gy,N){\n\t\tvector<float>y(N);\n\t\tvector<float>x(N);\n\t\tvector<float>devil(N);\n\t\tbool f = true;\n\t\tfor(int i=0;i<N;i++){\n\t\t\tcin>>x[i]>>y[i];\n\t\t\tdevil[i]=dis(gx,gy,x[i],y[i]);\n\t\t\tif (devil[i] < dis(sx,sy,x[i],y[i])) {\n\t\t\t\tf = false;\n\t\t\t}\n\t\t}\n\t\tif (!f) {\n\t\t\tcout << \"NO\\n\";\n\t\t\tcontinue;\n\t\t}\n\t\tvector<vector<float>>dist(N,vector<float>(N));\n\t\tfor(int i=0;i<N;i++){\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tdist[i][j]=dis(x[i],y[i],x[j],y[j]);\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<1<<N;i++){\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tdp[i][j]=MOD;\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<N;i++){\n\t\t\tif(devil[i]-EPS>dis(sx,sy,x[i],y[i])){\n\t\t\t\tdp[1<<i][i]=dis(sx,sy,x[i],y[i]);\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<1<<N;i++){\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tif(!((i>>j)&1))continue;\n\t\t\t\tif(dp[i][j]>=1000000)continue;\n\t\t\t\tbool hoge = false;\n\t\t\t\tfor(int k=0;k<N;k++){\n\t\t\t\t\tif((i>>k)&1)continue;\n\t\t\t\t\tif(dp[i][j]+dist[j][k] > devil[k] + EPS)hoge = true;\n\t\t\t\t}\n\n\t\t\t\tif(hoge)continue;\n\t\t\t\tfor(int k=0;k<N;k++){\n\t\t\t\t\tif((i>>k)&1)continue;\n\t\t\t\t\tfloat nx=dp[i][j]+dist[j][k];\n\t\t\t\t\tif(nx<devil[k]-EPS){\n\t\t\t\t\t\tdp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],nx);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfloat ans=MOD;\n\t\tfor(int i=0;i<N;i++){\n\t\t\tans=min(ans,dp[(1<<N)-1][i]);\n\t\t}\n\t\tif(ans>=10000000){\n\t\t\tcout<<\"NO\\n\";\n\t\t}\n\t\telse{\n\t\t\tcout<<\"YES\\n\";\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6630, "memory_kb": 85184, "score_of_the_acc": -1.9577, "final_rank": 20 }, { "submission_id": "aoj_2008_3427289", "code_snippet": "#include <string>\n#include <vector>\n#include <iostream>\n#include <queue>\n#include <functional>\n#include <algorithm>\n#include <random>\n#include <cmath>\n#include <climits>\n#include <iomanip>\n#include <cfloat>\n#include <set>\n#include <map>\n#include <tuple>\nstruct Point {\n\tfloat x, y;\n\tfloat distance(const Point &that) const {\n\t\treturn std::sqrt((x - that.x) * (x - that.x) + (y - that.y) * (y - that.y));\n\t}\n};\n\nvoid solve() {\n\tint n;\n\tfloat hx, hy, dx, dy; std::cin >> n >> hx >> hy >> dx >> dy;\n\tstd::vector<std::vector<float>> memo(20, std::vector<float>(1 << 20, FLT_MAX));\n\twhile (n != 0) {\n\t\tauto is_end = false;\n\t\tPoint start{ hx, hy }, mao{ dx, dy };\n\t\tfor (auto &m : memo)std::fill(m.begin(), m.end(), FLT_MAX);\n\t\tstd::vector<Point> crystal(n); for (auto &c : crystal) std::cin >> c.x >> c.y;\n\t\tstd::priority_queue<std::tuple<int, int, int>, std::vector<std::tuple<int, int, int>>, std::function<bool(const std::tuple<int, int, int> &, const std::tuple<int, int, int> &)>> queue([n, &memo](const std::tuple<int, int, int> &a, const std::tuple<int, int, int> &b) {return (std::get<2>(a) == std::get<2>(b)) ? memo[std::get<0>(a)][std::get<1>(a)] > memo[std::get<0>(b)][std::get<1>(b)] : std::get<2>(a) < std::get<2>(b); });\n\t\tfor (auto i = 0; i < n && !is_end; ++i) {\n\t\t\tif (start.distance(crystal[i]) < mao.distance(crystal[i])) {\n\t\t\t\tmemo[i][1 << i] = start.distance(crystal[i]);\n\t\t\t\tqueue.push(std::make_tuple(i, 1 << i, 1));\n\t\t\t}\n\t\t\telse {\n\t\t\t\tstd::cout << \"NO\\n\";\n\t\t\t\tis_end = true;\n\t\t\t}\n\t\t}\n\t\tbool can = true;\n\t\twhile (!queue.empty() && !is_end) {\n\t\t\tauto current = std::get<0>(queue.top());\n\t\t\tauto flag = std::get<1>(queue.top());\n\t\t\tauto count = std::get<2>(queue.top());\n\t\t\tauto distance = memo[current][flag];\n\t\t\tqueue.pop();\n\t\t\t\tcan = true;\n\t\t\t\tfor (auto i = 0; i < n && can; ++i) {\n\t\t\t\t\tif ((flag & (1 << i)) == 0 && distance + crystal[current].distance(crystal[i]) >= mao.distance(crystal[i])) {\n\t\t\t\t\t\tcan = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (can) for (auto i = 0; i < n && !is_end; ++i) {\n\t\t\t\t\tif ((flag & (1 << i)) == 0) {\n\t\t\t\t\t\tif (memo[i][flag | (1 << i)] > distance + crystal[current].distance(crystal[i])) {\n\t\t\t\t\t\t\tif (count + 1 == n) {\n\t\t\t\t\t\t\t\tstd::cout << \"YES\\n\";\n\t\t\t\t\t\t\t\tis_end = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmemo[i][flag | (1 << i)] = distance + crystal[current].distance(crystal[i]);\n\t\t\t\t\t\t\tqueue.push(std::make_tuple(i, flag | (1 << i), count + 1));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t\tif (!is_end) {\n\t\t\tif (std::any_of(memo.begin(), memo.end(), [n](const std::vector<float> &v) {return v[(1 << n) - 1] != FLT_MAX; })) {\n\t\t\t\tstd::cout << \"YES\\n\";\n\t\t\t}\n\t\t\telse {\n\t\t\t\tstd::cout << \"NO\\n\";\n\t\t\t}\n\t\t}\n\t\tstd::cin >> n >> hx >> hy >> dx >> dy;\n\t}\n}\nint main() {\n\tsolve();\n}", "accuracy": 1, "time_ms": 2310, "memory_kb": 88900, "score_of_the_acc": -1.3384, "final_rank": 19 }, { "submission_id": "aoj_2008_2989954", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\nint n;\nint x[20],y[20];\nint sx,sy,mx,my;\nbool c[20];\nbool f(int u,double d)\n{\n\tint cnt=0;\n\tfor(int i=0;i<n;i++)\n\t{\n\t\tif(c[i])\n\t\t{\n\t\t\tcnt++;\n\t\t\tcontinue;\n\t\t}\n\t\tdouble a=hypot(x[u]-x[i],y[u]-y[i])+d;\n\t\tdouble b=hypot(mx-x[i],my-y[i]);\n\t\tif(b<=a)return false;\n\t}\n\tif(cnt==n)return true;\n\tfor(int i=0;i<n;i++)\n\t{\n\t\tif(c[i])continue;\n\t\tdouble a=hypot(x[u]-x[i],y[u]-y[i])+d;\n\t\tdouble b=hypot(mx-x[i],my-y[i]);\n\t\tif(b>a)\n\t\t{\n\t\t\tc[i]=1;\n\t\t\tif(f(i,a))return true;\n\t\t\tc[i]=0;\n\t\t}\n\t}\n\treturn false;\n}\nmain()\n{\n\twhile(cin>>n>>sx>>sy>>mx>>my,n)\n\t{\n\t\tfor(int i=0;i<n;i++)cin>>x[i]>>y[i];\n\t\tfor(int i=0;i<n;i++)c[i]=0;\n\t\tbool fl=0;\n\t\tfor(int i=0;!fl&&i<n;i++)\n\t\t{\n\t\t\tdouble a=hypot(sx-x[i],sy-y[i]);\n\t\t\tdouble b=hypot(mx-x[i],my-y[i]);\n\t\t\tif(a<b)\n\t\t\t{\n\t\t\t\tc[i]=1;\n\t\t\t\tfl|=f(i,a);\n\t\t\t\tc[i]=0;\n\t\t\t}\n\t\t\telse break;\n\t\t}\n\t\tcout<<(fl?\"YES\":\"NO\")<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 2996, "score_of_the_acc": -0.1033, "final_rank": 14 }, { "submission_id": "aoj_2008_2979966", "code_snippet": "#include<stdio.h>\n#include<math.h>\n#include<algorithm>\nusing namespace std;\ndouble eps=1e-9;\nint x[23];\nint y[23];\ndouble dist[23][23];\nint n;\nint solve(int a,int b,double s){\n if(a==(1<<n)-1)return 1;\n for(int i=0;i<n;i++){//枝切り\n if(a&(1<<i))continue;\n if(s+dist[b][i]+eps>dist[n+1][i])return 0;\n }\n for(int i=0;i<n;i++){\n if(a&(1<<i))continue;\n \n if(solve(a+(1<<i),i,s+dist[b][i]))return 1;\n }\n return 0;\n}\nint main(){\n int a,hx,hy,dx,dy;\n while(scanf(\"%d%d%d%d%d\",&a,&hx,&hy,&dx,&dy),a){\n n=a;\n for(int i=0;i<a;i++)scanf(\"%d%d\",x+i,y+i);\n x[a]=hx;x[a+1]=dx;\n y[a]=hy;y[a+1]=dy;\n for(int i=0;i<a+2;i++)for(int j=0;j<a+2;j++)\n dist[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));\n if(solve(0,a,0))printf(\"YES\\n\");\n else printf(\"NO\\n\");\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 2572, "score_of_the_acc": -0.0234, "final_rank": 4 }, { "submission_id": "aoj_2008_2587950", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Info{\n\tint x,y;\n};\n\nint N,POW[21];\nInfo start,enemy,info[21];\ndouble dist_table[21][21],poison_TIME[20];\nbool FLG;\n\ndouble calc_Time(Info from,Info to){\n\treturn sqrt((from.x-to.x)*(from.x-to.x)+(from.y-to.y)*(from.y-to.y));\n}\n\nvoid dfs(int node_id,int count,double total_time,int state){\n\n\tif(count == N){\n\t\tFLG = true;\n\t}\n\tif(FLG)return;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(state & (1 << i)){\n\t\t\t//Do nothing\n\t\t}else{\n\t\t\tdouble next_time = total_time+dist_table[node_id][i];\n\t\t\tif(next_time >= poison_TIME[i])return;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(state & (1 << i)){\n\t\t\t//Do nothing\n\t\t}else{\n\t\t\tdouble next_time = total_time+dist_table[node_id][i];\n\t\t\tint next_state = state + POW[i];\n\t\t\tdfs(i,count+1,next_time,next_state);\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d %d\",&info[i].x,&info[i].y);\n\t\tpoison_TIME[i] = calc_Time(info[i],enemy);\n\t}\n\tinfo[N].x = start.x;\n\tinfo[N].y = start.y;\n\n\tdouble tmp;\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = i+1; k <= N; k++){\n\t\t\ttmp = calc_Time(info[i],info[k]);\n\t\t\tdist_table[i][k] = tmp;\n\t\t\tdist_table[k][i] = tmp;\n\t\t}\n\t}\n\n\tFLG = false;\n\n\tdfs(N,0,0,0);\n\n\tif(FLG){\n\t\tprintf(\"YES\\n\");\n\t}else{\n\t\tprintf(\"NO\\n\");\n\t}\n}\n\nint main(void){\n\n\tfor(int i = 0; i <= 20; i++)POW[i] = pow(2,i);\n\n while(true){\n \tscanf(\"%d %d %d %d %d\",&N,&start.x,&start.y,&enemy.x,&enemy.y);\n\n \tif(N == 0)break;\n\n \tfunc();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3284, "score_of_the_acc": -0.0285, "final_rank": 6 }, { "submission_id": "aoj_2008_2415567", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<string>\n\nusing namespace std;\n\n#define REP(i,n) for(int i = 0;i < n ;i++)\n#define EPS 1e-8\n\nint n,hx,hy,dx,dy;\nint x[30],y[30];\n\nbool flag;\n\nvoid dfs(int unvisit,int cx,int cy,double t)\n{\n\tif(flag) return;\n\tif(unvisit == 0)flag = true;\n\t\n\telse\n\t{\n\t\tREP(i,n)\n\t\t{\n\t\t\tif(!((unvisit >> i) & 1))continue;\n\t\t\tif(hypot(dx-x[i],dy-y[i]) <= t + hypot(cx-x[i],cy-y[i]))return;\n\t\t}\n\t\t\n\t\tREP(i,n)\n\t\t{\n\t\t\tif(!((unvisit >> i) & 1))continue;\n\t\t\tdfs((unvisit & ~(1 << i)),x[i],y[i],t + hypot(cx-x[i],cy-y[i]));\n\t\t}\n\t}\n}\n\nstring solve()\n{\n\tflag = false;\n\tdfs((1<<n)-1,hx,hy,0);\n\treturn (flag)?\"YES\":\"NO\";\n}\n\nint main()\n{\n\twhile(cin >> n >> hx >> hy >> dx >> dy,n)\n\t{\n\t\tREP(i,n)\n\t\t{\n\t\t\tcin >> x[i] >> y[i];\n\t\t}\n\t\t\n\t\tcout << solve() << endl;\n\t\t\n\t}\n\t\n\treturn 0;\n\t\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 3000, "score_of_the_acc": -0.0865, "final_rank": 13 }, { "submission_id": "aoj_2008_1877564", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <string>\n#include <cmath>\n#include <algorithm>\n#include <functional>\ninline double dist(int x, int y) {\n\treturn std::sqrt(x * x + y * y);\n}\ninline double dist(std::pair<int, int> pos) {\n\treturn std::sqrt(pos.first * pos.first + pos.second * pos.second);\n}\ninline double dist(int x, int y, std::pair<int, int> pos) {\n\treturn std::sqrt((x - pos.first) * (x - pos.first) + (y - pos.second) * (y - pos.second));\n}\n\nstd::vector<std::pair<int, int>> crystal;\nint n, x, y, mx, my;\nbool Solve(double day = 0, std::vector<std::pair<int, int>> got = {});\nbool isClear = false;\nint main()\n{\n\tstd::vector<std::string> results;\n\twhile (1) {\n\t\tstd::cin >> n >> x >> y >> mx >> my;\n\t\tif (!x && !y && !n && !mx && !my) break;\n\t\tcrystal.clear();\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint cx, cy;\n\t\t\tstd::cin >> cx >> cy;\n\t\t\tcx -= mx; cy -= my;\n\t\t\tcrystal.push_back(std::make_pair(cx, cy));\n\t\t}\n\t\tx -= mx; y -= my;\n\t\tmx = 0; my = 0;\n\t\tisClear = false;\n\t\tSolve();\n\t\tresults.push_back(isClear ? \"YES\" : \"NO\");\n\t}\n\n\tfor (auto& x : results) {\n\t\tstd::cout << x << std::endl;\n\t}\n\n\treturn 0;\n}\n\nbool Solve(double day, std::vector<std::pair<int, int>> got)\n{\n\tif (isClear) return true;\n\tbool ok = false;\n\tint posX, posY;\n\tif (got.empty()) {\n\t\tposX = x;\n\t\tposY = y;\n\t}\n\telse {\n\t\tposX = got.back().first;\n\t\tposY = got.back().second;\n\t}\n\t//??¨??£??????????????´?????¨??????????????????????????£?????????\n\tfor (auto& x : crystal) {\n\t\t//???????????????????¨????(?????¢???????????????????????????)\n\t\tdouble pass = dist(posX, posY, x);\n\t\tdouble next = day + pass;\n\t\t//?????£?????????crystal?????????????????????????????????????????????\n\t\tif (next < dist(x)) {\n\t\t\t//??´?°????????????¨??????????¨????\n\t\t\tif (std::find(got.begin(), got.end(), x) == got.end()) {\n\t\t\t\tgot.push_back(x);\n\t\t\t\tif (got.size() == n) {\n\t\t\t\t\tisClear = true;\n\t\t\t\t}\n\t\t\t\tok = ok | Solve(next, got);\n\t\t\t}\n\t\t}\n\t}\n\treturn ok;\n}", "accuracy": 1, "time_ms": 2750, "memory_kb": 3104, "score_of_the_acc": -0.4292, "final_rank": 17 }, { "submission_id": "aoj_2008_1875051", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX 21\n#define EPS 1e-9\n#define INF 1e9\n \nstruct Point{\n double x, y;\n}; \n \nint N;\nPoint h, d, crystal[MAX];\n \ndouble dist(Point a, Point b){\n return sqrt(pow(a.x-b.x,2) + pow(a.y-b.y,2));\n}\n \nbool rec(int S, int v, double total_dist){\n if(S == (1 << N) - 1){\n\treturn true;\n }\n for(int u = 0 ; u < N ; u++){\n\tif(!(S >> u & 1)){\n\t if(total_dist + dist(crystal[v],crystal[u]) >= dist(d, crystal[u])){\n\t\treturn false;\n\t }\n\t}\n }\n for(int u = 0 ; u < N ; u++){\n\tif(!(S >> u & 1)){\n\t if(rec(S | 1 << u, u, total_dist + dist(crystal[v],crystal[u]))){\n\t\treturn true;\n\t }\n\t}\n }\n}\n \nbool solve(){\n for(int i = 0 ; i < N ; i++){\n\tif(rec(0,i,dist(h,crystal[i]))){\n\t return true;\n\t}\n } \n return false;\n}\n \nint main(){\n while(cin >> N >> h.x >> h.y >> d.x >> d.y, (N + h.x + h.y + d.x + d.y)){\n\tfor(int i = 0 ; i < N ; i++){\n\t cin >> crystal[i].x >> crystal[i].y;\n\t}\n\tcout << (solve() ? \"YES\" : \"NO\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 3160, "score_of_the_acc": -0.0853, "final_rank": 12 }, { "submission_id": "aoj_2008_1861551", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n;\nvector<int> x, y;\nvector<int> viewed;\n\ndouble dist(int a, int b){\n return sqrt((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b]));\n}\n\nbool dfs(int v, double t, int c){\n if(c == n) return true;\n\n for(int i=0;i<n;i++){\n if(viewed[i]) continue;\n if(t + dist(v, i) >= dist(i, n+1)) return false;\n }\n\n for(int i=0;i<n;i++){\n if(viewed[i]) continue;\n double nt = t + dist(v, i);\n if(nt < dist(i, n+1)){\n viewed[i] = true;\n if(dfs(i, nt, c+1)) return true;\n viewed[i] = false;\n }\n }\n\n return false;\n}\n\nint main(){\n while(1){\n cin >> n;\n if(n == 0) break;\n x.assign(n+2, 0);\n y.assign(n+2, 0);\n cin >> x[n] >> y[n] >> x[n+1] >> y[n+1];\n for(int i=0;i<n;i++){\n cin >> x[i] >> y[i];\n }\n viewed.assign(n, 0);\n if(dfs(n, 0, 0)){\n cout << \"YES\" << endl;\n }else{\n cout << \"NO\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 1188, "score_of_the_acc": -0.0567, "final_rank": 7 }, { "submission_id": "aoj_2008_1861542", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n;\nvector<int> x, y;\nvector<bool> viewed;\n\ndouble dist(int a, int b){\n return sqrt((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b]));\n}\n\nbool dfs(int v, double t, int c){\n if(c == n) return true;\n\n for(int i=0;i<n;i++){\n if(viewed[i]) continue;\n if(t + dist(v, i) >= dist(i, n+1)) return false;\n }\n\n for(int i=0;i<n;i++){\n if(viewed[i]) continue;\n double nt = t + dist(v, i);\n if(nt < dist(i, n+1)){\n viewed[i] = true;\n if(dfs(i, nt, c+1)) return true;\n viewed[i] = false;\n }\n }\n\n return false;\n}\n\nint main(){\n while(1){\n cin >> n;\n if(n == 0) break;\n x.assign(n+2, 0);\n y.assign(n+2, 0);\n cin >> x[n] >> y[n] >> x[n+1] >> y[n+1];\n for(int i=0;i<n;i++){\n cin >> x[i] >> y[i];\n }\n viewed.assign(n, false);\n if(dfs(n, 0, 0)){\n cout << \"YES\" << endl;\n }else{\n cout << \"NO\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3080, "score_of_the_acc": -0.0644, "final_rank": 9 }, { "submission_id": "aoj_2008_1819062", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n\n#define EPS\t1.0e-10\n#define MAX_CRYSTAL\t32\n#define rep(idx, max)\tfor(int idx = 0, idx##Max = max; idx < idx##Max; idx ++)\n\ntypedef struct _POINT\n{\n\tint x, y;\n\n\t_POINT() {}\n\t_POINT(int _x, int _y) : x(_x), y(_y) {}\n} POINT;\n\nint g_crystalNum;\nPOINT\tg_hero, g_devil;\nPOINT\tg_crystals[MAX_CRYSTAL];\n\nint\tg_bSolved;\nint\tg_bReached[MAX_CRYSTAL];\nint\tg_reachedCount;\n\ndouble Distance(const POINT &src1, const POINT &src2)\n{\n\tdouble x = src1.x - src2.x;\n\tdouble y = src1.y - src2.y;\n\treturn( ::sqrt(x * x + y * y) );\n}\n\nvoid Solve(const POINT &pt, const double time)\n{\n\t//std::cout << time << std::endl;\n\tif (g_bSolved) { return; }\n\n\tif (g_reachedCount == g_crystalNum)\n\t{\n\t\tg_bSolved = true;\n\t\treturn;\n\t}\n\n\trep(n, g_crystalNum)\n\t{\n\t\tif (g_bReached[n]) { continue; }\n\t\tdouble r = time + ::Distance(pt, g_crystals[n]);\t// Elapsed time of hero\n//\t\tif (r - ::Distance(g_devil, g_crystals[n]) > EPS) { return; }\t// Could not reached (You are too late :P)\n\t\tif (::Distance(g_devil, g_crystals[n]) < r + EPS) { return; }\t// Could not reached (You are too late :P)\n\t}\n\n\trep(n, g_crystalNum)\n\t{\n\t\tif (g_bReached[n]) { continue; }\n\n\t\tg_bReached[n] = true;\n\t\tg_reachedCount++;\n\t\t::Solve(g_crystals[n], time + ::Distance(pt, g_crystals[n]));\n\t\tg_bReached[n] = false;\n\t\tg_reachedCount--;\n\t}\n}\n\nint main(int nArgs, char **lplpszArgs)\n{\n\twhile (std::cin >> g_crystalNum)\n\t{\n\t\tstd::cin >> g_hero.x >> g_hero.y >> g_devil.x >> g_devil.y;\n\t\tif (g_crystalNum == 0) { break; }\n\t\trep(i, g_crystalNum)\n\t\t{\n\t\t\tPOINT &pt = g_crystals[i];\n\t\t\tstd::cin >> pt.x >> pt.y;\n\t\t}\n\n\t\t//::printf(\"Hero: (%d, %d), Devil: (%d, %d)\\n\", g_hero.x, g_hero.y, g_devil.x, g_devil.y);\n\n\t\tg_bSolved = false;\n\t\t::memset(g_bReached, 0, sizeof(g_bReached));\n\t\tg_reachedCount = 0;\n\t\t::Solve(g_hero, 0);\n\t\tstd::cout << (g_bSolved ? \"YES\" : \"NO\") << std::endl;\n\t}\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 1152, "score_of_the_acc": -0.0655, "final_rank": 10 }, { "submission_id": "aoj_2008_1783503", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nvi x,y;\nint n,a,b,c,d;\nbool f(int q,int nx,int ny,double t){\n\tif(q==(1<<n)-1)return 1;\n\trep(i,n)if((q&1<<i)==0&&(hypot(nx-x[i],ny-y[i])+t+EPS>hypot(c-x[i],d-y[i])))return 0;\n\trep(i,n)if((q&1<<i)==0&&f(q^1<<i,x[i],y[i],t+hypot(nx-x[i],ny-y[i])))return 1;\n\treturn 0;\n}\nint main(){\n\twhile(cin>>n,n){\n\t\tcin>>a>>b>>c>>d;\n\t\ty=x=vi(n);\n\t\trep(i,n)cin>>x[i]>>y[i];\n\t\tcout<<(f(0,a,b,0)?\"YES\":\"NO\")<<endl;\n\t}\n\tcin>>n>>n>>n>>n;\n}", "accuracy": 1, "time_ms": 970, "memory_kb": 1220, "score_of_the_acc": -0.1351, "final_rank": 16 }, { "submission_id": "aoj_2008_1764610", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <algorithm>\n\nusing namespace std;\n\nint n,hx,hy,dx,dy;\n\nfloat dist[21][21];\nstruct Crystal{\n int x,y;\n float dist;\n bool visited;\n};\nCrystal crystals[21];//[0]????????????????????????????????????\n\nbool rec(int s,float t){\n crystals[s].visited=true;\n bool b=true;\n for(int i=1;i<=n;i++){\n if(crystals[i].visited) continue;\n if(dist[i][s]+t<crystals[i].dist){\n if(rec(i,dist[i][s]+t)){\n //cout << s << \",\";\n return true;\n }else{\n b=false;\n }\n }else{\n //???????????????????????????????????£??????\n crystals[s].visited=false;\n return false;\n }\n }\n //??¨???????????????????????????????????????\n //cout << s << \",\";\n crystals[s].visited=false;\n return b;\n}\n\nint main(void){\n while(cin >> n >> crystals[0].x >> crystals[0].y >> dx >> dy,n){\n for(int i=1;i<=n;i++){\n cin >> crystals[i].x >> crystals[i].y;\n crystals[i].dist=sqrt(pow(crystals[i].x-dx,2)+pow(crystals[i].y-dy,2));\n }\n sort(crystals+1,crystals+n+1,[](const Crystal& a, const Crystal& b){return (a.dist==b.dist)?(a.x<b.x):(a.dist)<(b.dist);});\n for(int i=0;i<=n;i++){\n crystals[i].visited=false;\n for(int k=i+1;k<=n;k++){\n dist[i][k]=dist[k][i]=sqrt(pow(crystals[i].x-crystals[k].x,2)+pow(crystals[i].y-crystals[k].y,2));\n }\n }\n if(rec(0,0)){\n cout << \"YES\" << endl;\n }else{\n cout << \"NO\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3084, "score_of_the_acc": -0.0231, "final_rank": 3 }, { "submission_id": "aoj_2008_1714399", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n\n#define rep(i, n) for(int i = 0; i < (n); ++i)\n\nusing namespace std;\n\nint n;\ndouble x[22];\ndouble y[22];\ndouble d[22][22];\n\nbool dfs(int k, int x, double c){\n\tif(x == (1 << n) - 1){\n\t\treturn true;\n\t}\n\trep(i, n){\n\t\tif(\n\t\t\t!(x & (1 << i)) &&\n\t\t\tc + d[k][i] >= d[i][n + 1]\n\t\t){\n\t\t\treturn false;\n\t\t}\n\t}\n\trep(i, n){\n\t\tif(\n\t\t\t!(x & (1 << i)) &&\n\t\t\tdfs(i, x | (1 << i), c + d[k][i])\n\t\t){\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint main(){\n\twhile(1){\n\t\tscanf(\"%d\", &n);\n\t\tif(n == 0){\n\t\t\tbreak;\n\t\t}\n\t\tscanf(\"%lf%lf%lf%lf\", x + n, y + n, x + n + 1, y + n + 1);\n\t\trep(i, n){\n\t\t\tscanf(\"%lf%lf\", x + i, y + i);\n\t\t}\n\t\trep(i, n + 2){\n\t\t\trep(j, n + 2){\n\t\t\t\tdouble dx = x[i] - x[j];\n\t\t\t\tdouble dy = y[i] - y[j];\n\t\t\t\td[i][j] = sqrt(dx * dx + dy * dy);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%s\\n\", dfs(n, 0, 0) ? \"YES\" : \"NO\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1052, "score_of_the_acc": -0.0077, "final_rank": 1 }, { "submission_id": "aoj_2008_1392435", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <functional>\n\nusing namespace std;\n\ntypedef pair<int, int> P;\ntypedef pair<double, int> R;\ntypedef pair<P, R> Q;\n\nint main() {\n\tint n, hx, hy, dx, dy;\n\tdouble r;\n\twhile (cin >> n >> hx >> hy >> dx >> dy) {\n\t\tif (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0) {\n\t\t\tbreak;\n\t\t}\n\t\thx -= dx;\n\t\thy -= dy;\n\t\tvector<P> data;\n\t\tint x, y;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcin >> x >> y;\n\t\t\tx -= dx;\n\t\t\ty -= dy;\n\t\t\tdata.push_back(P(x, y));\n\t\t}\n\t\tvector< vector<double> > data2(n, vector<double>(n));\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = i+1; j < n; j++) {\n\t\t\t\tdata2[i][j] = data2[j][i] = sqrt(pow(data[i].first-data[j].first, 2)+pow(data[i].second-data[j].second, 2));\n\t\t\t}\n\t\t}\n\t\tvector<double> data3(n);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tdata3[i] = sqrt(pow(data[i].first, 2)+pow(data[i].second, 2));\n\t\t}\n\t\tqueue<Q> q;\n\t\tq.push(Q(P(hx, hy), R(0.0, 0)));\n\t\tbool hantei = false;\n\t\twhile (!q.empty()) {\n\t\t\tQ p = q.front();\n\t\t\tq.pop();\n\t\t\tP xy = p.first;\n\t\t\tR limit = p.second;\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tbool hantei2 = true;\n\t\t\t\tif ((limit.second & (1<<i)) == 0) {\n\t\t\t\t\tdouble dead = limit.first+sqrt(pow(data[i].first-xy.first, 2)+pow(data[i].second-xy.second, 2));\n\t\t\t\t\tif (data3[i] > dead) {\n\t\t\t\t\t\tint k = limit.second | (1<<i);\n\t\t\t\t\t\tif (k == (1<<n)-1) {\n\t\t\t\t\t\t\thantei = true;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\t\t\t\tif ((k & (1<<j)) == 0) {\n\t\t\t\t\t\t\t\tif (data3[j] <= dead+data2[i][j]) {\n\t\t\t\t\t\t\t\t\thantei2 = false;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t} \n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (hantei2) {\n\t\t\t\t\t\t\tq.push(Q(data[i], R(dead, k)));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (hantei) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (hantei) {\n\t\t\tcout << \"YES\" << endl;\n\t\t} else {\n\t\t\tcout << \"NO\" << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4340, "memory_kb": 13744, "score_of_the_acc": -0.7938, "final_rank": 18 }, { "submission_id": "aoj_2008_1373912", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nconst int INF = 1<<28;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n\n\nint T, n, m;\ndouble x[21], y[21], t[21], hx, hy, sx ,sy;\n\ndouble hypot(double x, double y){\n\treturn sqrt(x*x+y*y);\n}\nint solve(double sx, double sy, int b, double ct){\n\tif(b == (1<<n)-1) return 1;\n\tREP(i, n)if(0 == ((b>>i)&1)){\n\t\tdouble nt = ct + hypot(x[i]-sx, y[i]-sy);\n\t\tif(t[i]-EPS < nt) return 0;\n\t}\n\tREP(i, n)if(0 == ((b>>i)&1)){\n\t\tdouble nt = ct + hypot(x[i]-sx, y[i]-sy);\n\t\tif(t[i]-EPS < nt) continue;\n\t\tif(solve(x[i], y[i], b | (1<<i), nt)) return 1;\n\t}\n\treturn 0;\n}\n\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> sx >> sy >> hx >> hy, n){\n\t\tREP(i, n){\n\t\t\tcin >> x[i] >> y[i];\n\t\t\tt[i] = hypot(x[i]-hx, y[i] - hy);\n\t\t}\n\t\tcout << (solve(sx, sy, 0, 0) ? \"YES\" : \"NO\") << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 1228, "score_of_the_acc": -0.0188, "final_rank": 2 } ]
aoj_2010_cpp
Problem E: Poor Mail Forwarding Masa 君が住んでいる地域の郵便システムは少々変わっている。 この地域では、各々の郵便局に対して 1 から順に連続した異なる番号がつけられており、ある郵便局に届けられた郵便物は、その郵便局からいくつかの郵便局を経由して目的の郵便局に届けられる。 郵便物の「転送」は、特定の郵便局の間でしか行われない。 ただし、転送が行われる郵便局同士の間では、転送は双方向に行われる。 ある郵便局から宛先まで郵便物を転送するための経路が複数存在する場合は、 宛先までの距離が最短となるような経路を使用する。 したがって、転送先はそのような最短経路における次の郵便局となる。 もし、総距離が最短となる経路が複数存在する場合は、直接の転送先となる郵便局の番号が若いほうに転送する。 ところで、この地域では深刻な労働力不足に悩まされており、郵便局間の転送を行う転送員が各郵便局に 1 人ずつしかいない。 彼らががんばって往復しているのである。 当然、ある郵便局に郵便物が届いた時点でその郵便局に所属する転送員が出払っていた場合は、その郵便局からの転送は一時的にストップする。 彼らは自分の所属する郵便局にいるときに、その郵便局から転送されていない郵便物が存在すれば即座に出発し、転送先に届けた後は即座に戻ってくる。 彼が転送先の郵便局から帰ってくるとき、彼の郵便局に転送される郵便物がその郵便局にあったとしても、それを運んでくることはしない (その郵便物の転送はその郵便局の転送員の仕事である)。 転送する際に、複数の郵便物が溜まっていた場合は、その郵便局に最も早く届けられた郵便物から転送を行う。 ここで、同じ時刻に届いた郵便物が複数あったときは、直接の転送先となる郵便局の番号が若いほうから転送する。 なお、同じ転送先となる郵便物があればそれも一緒に転送する。 また、転送員の出発時刻と同じ時刻に、同じ郵便局に転送されるべき郵便物が到着した場合は、それも一緒に転送される。 なお、彼らは全員、距離 1 の移動に時間 1 を要する。 あなたには、郵便局間の転送に関わる情報と、各郵便局に郵便物が届いた時刻のリストが与えられる。 このとき、郵便物の動きをシミュレートし、各郵便物が宛先の郵便局に届いた時刻を報告せよ。 問題のシミュレーション中で必要となる時刻はすべて 0 以上 2 31 未満の範囲におさまると仮定してよい。 Input 入力は複数のデータセットからなり、入力の終了は 0 0 とだけ書かれた 1 行で表される。 それぞれのデータセットの書式は以下の通りである。 データセットの最初の行には、整数 n (2 <= n <= 32) と m (1 <= m <= 496) が 1 文字のスペースで区切られて順に与えられる。 n は郵便局の総数を、 m は郵便局間の直接の転送経路の数を表す。 ある 2 つの郵便局間に 2 つ以上の直接転送経路が存在することはない。 続く m 行に、局間の直接転送経路の情報を記した 3 つの整数が書かれる。 1 つ目と 2 つ目が転送経路の両端の郵便局の番号 (1 以上 n 以下) であり、最後の整数がその 2 局間の距離を表す。 距離は 1 以上 10000 未満である。 次の行には、処理すべき郵便物の数を表す整数 l (1 <= l <= 1000) が書かれ、 それに続く l 行に各郵便物の詳細が書かれる。 その各行の先頭には 3 つの整数が書かれている。 これらは順に、発送元の郵便局の番号、宛先の郵便局の番号、発送元の郵便局に届いた時刻である。 行の最後に、郵便物のラベルを表す文字列が書かれる。 このラベルは、長さが 1 文字以上 50 文字以内であり、 アルファベット、数字、ハイフン ('-')、アンダースコア ('_') のみからなる。 これらの各郵便物の情報は、発送元の郵便局に到着した時刻順に書かれている。 同じ時刻のときは、順番は特に規定されていない。 また、同じラベルの郵便物や、発送元と宛先が同じ郵便物または配達するための経路が存在しない郵便物は存在しない。 入力行中の各数値や文字列の区切りは、全て 1 文字のスペースである。 Output 各々のデータセットに対して l 行の出力を行う。 それぞれの出力は、以下の書式に従う。 各々の郵便物について、その郵便物が届いた時刻を 1 行に出力する。その行には、最初に郵便物のラベルを出力し、次に 1 文字のスペースを挟んで、最後に郵便物が宛先の郵便局に到着した時刻を出力する。 これらは到着時刻順に出力する。同時刻に到着した郵便物が複数ある場合は、そのラベルの ASCII コードの昇順に出力する。 各々のデータセットに対応する出力の間に、空行が 1 つ挿入される。 Sample Input 4 5 1 2 10 1 3 15 2 3 5 2 4 3 3 4 10 2 1 4 10 LoveLetter 2 3 20 Greetings 3 3 1 2 1 2 3 1 3 1 1 3 1 2 1 BusinessMailC 2 3 1 BusinessMailB 3 1 1 BusinessMailA 0 0 Output for the Sample Input Greetings 25 LoveLetter 33 BusinessMailA 2 BusinessMailB 2 BusinessMailC 2
[ { "submission_id": "aoj_2010_10204478", "code_snippet": "// AOJ #2010\n// Poor Mail Forwarding 2025.2.8\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n const int INF = 1000000000;\n \nstruct Mail {\n int src, dest;\n ll start_time;\n string label;\n ll delivered_time;\n};\n \nstruct Event {\n ll time;\n int type;\n int post;\n int mail_id;\n};\n \nstruct EventComparator {\n bool operator()(const Event &a, const Event &b) const {\n if(a.time != b.time) return a.time > b.time;\n if(a.type != b.type) return a.type > b.type;\n return a.post > b.post;\n }\n};\n \nstruct WaitingMail {\n ll arrivalTime;\n int next;\n int mail_id;\n};\n \nstruct WaitingMailComparator {\n bool operator()(const WaitingMail &a, const WaitingMail &b) const {\n if(a.arrivalTime != b.arrivalTime) return a.arrivalTime < b.arrivalTime;\n if(a.next != b.next) return a.next < b.next;\n return a.mail_id < b.mail_id;\n }\n};\n \nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n bool firstDataset = true;\n while(true){\n int n, m;\n if(!(cin >> n >> m)) break;\n if(n==0 && m==0) break;\n \n vector<vector<int>> graph(n+1, vector<int>(n+1, INF));\n for (int i = 1; i <= n; i++){\n graph[i][i] = 0;\n }\n for (int i = 0; i < m; i++){\n int u, v, w;\n cin >> u >> v >> w;\n graph[u][v] = w;\n graph[v][u] = w;\n }\n \n int l;\n cin >> l;\n vector<Mail> mails(l);\n for (int i = 0; i < l; i++){\n cin >> mails[i].src >> mails[i].dest >> mails[i].start_time >> mails[i].label;\n mails[i].delivered_time = -1;\n }\n \n vector<vector<int>> dist(n+1, vector<int>(n+1, INF));\n for (int i = 1; i <= n; i++){\n for (int j = 1; j <= n; j++){\n dist[i][j] = graph[i][j];\n }\n }\n for (int k = 1; k <= n; k++){\n for (int i = 1; i <= n; i++){\n for (int j = 1; j <= n; j++){\n if(dist[i][k] < INF && dist[k][j] < INF)\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n \n vector<vector<int>> nextHop(n+1, vector<int>(n+1, -1));\n for (int i = 1; i <= n; i++){\n for (int j = 1; j <= n; j++){\n if(i == j) continue;\n int chosen = -1;\n for (int k = 1; k <= n; k++){\n if(k == i) continue;\n if(graph[i][k] < INF){\n if(dist[k][j] < INF && dist[i][j] == graph[i][k] + dist[k][j]){\n if(chosen == -1 || k < chosen)\n chosen = k;\n }\n }\n }\n nextHop[i][j] = chosen;\n }\n }\n \n vector<vector<WaitingMail>> waiting(n+1);\n vector<bool> forwarderFree(n+1, true);\n \n priority_queue<Event, vector<Event>, EventComparator> eventQueue;\n for (int i = 0; i < l; i++){\n Event ev;\n ev.time = mails[i].start_time;\n ev.type = 0;\n ev.post = mails[i].src;\n ev.mail_id = i;\n eventQueue.push(ev);\n }\n \n while(!eventQueue.empty()){\n ll currentTime = eventQueue.top().time;\n vector<Event> currentEvents;\n while(!eventQueue.empty() && eventQueue.top().time == currentTime) {\n currentEvents.push_back(eventQueue.top());\n eventQueue.pop();\n }\n sort(currentEvents.begin(), currentEvents.end(), [](const Event &a, const Event &b){\n if(a.type != b.type) return a.type < b.type;\n return a.post < b.post;\n });\n \n for(auto &ev : currentEvents) {\n if(ev.type == 0) {\n int post = ev.post;\n int mailId = ev.mail_id;\n if(post == mails[mailId].dest) {\n mails[mailId].delivered_time = currentTime;\n } else {\n WaitingMail wm;\n wm.arrivalTime = currentTime;\n wm.next = nextHop[post][ mails[mailId].dest ];\n wm.mail_id = mailId;\n waiting[post].push_back(wm);\n }\n } else {\n int post = ev.post;\n forwarderFree[post] = true;\n }\n }\n \n for (int post = 1; post <= n; post++){\n if(forwarderFree[post] && !waiting[post].empty()){\n sort(waiting[post].begin(), waiting[post].end(), WaitingMailComparator());\n \n int target = waiting[post][0].next;\n \n vector<int> batchMailIds;\n vector<WaitingMail> remaining;\n for(auto &wm : waiting[post]){\n if(wm.next == target)\n batchMailIds.push_back(wm.mail_id);\n else\n remaining.push_back(wm);\n }\n waiting[post] = remaining;\n \n int travelTime = graph[post][target];\n \n for(auto &mailId : batchMailIds){\n Event newEv;\n newEv.time = currentTime + travelTime;\n newEv.type = 0;\n newEv.post = target;\n newEv.mail_id = mailId;\n eventQueue.push(newEv);\n }\n \n forwarderFree[post] = false;\n Event retEv;\n retEv.time = currentTime + 2LL * travelTime;\n retEv.type = 1;\n retEv.post = post;\n retEv.mail_id = -1;\n eventQueue.push(retEv);\n }\n }\n }\n \n vector<int> order(l);\n for (int i = 0; i < l; i++){\n order[i] = i;\n }\n sort(order.begin(), order.end(), [&](int a, int b) {\n if(mails[a].delivered_time != mails[b].delivered_time)\n return mails[a].delivered_time < mails[b].delivered_time;\n return mails[a].label < mails[b].label;\n });\n \n if(!firstDataset) cout << endl;\n firstDataset = false;\n \n for(auto idx : order){\n cout << mails[idx].label << \" \" << mails[idx].delivered_time << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3524, "score_of_the_acc": -0.6882, "final_rank": 7 }, { "submission_id": "aoj_2010_9626575", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef pair<ll,ll> pi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define F first\n#define S second\n#define sz(A) ((ll)(A.size()))\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\nint main(){\n bool cc=0;\n while(1){\n ll N,M;cin>>N>>M;\n if(!N)return 0;\n if(cc)cout<<\"\\n\";\n cc=1;\n vvi D(N,vi(N,1e18));\n vvi E(N,vi(N,1e18));\n REP(i,M){\n ll u,v,c;cin>>u>>v>>c;u--;v--;\n E[u][v]=E[v][u]=D[u][v]=D[v][u]=c;\n }\n REP(i,N)D[i][i]=0;\n REP(i,N)REP(j,N)REP(k,N)D[j][k]=min(D[j][k],D[j][i]+D[i][k]);\n vvi F(N,vi(N,-1));\n REP(i,N)REP(j,N)if(i!=j&&D[i][j]<1e17){\n F[i][j]=0;\n while(D[i][j]!=E[i][F[i][j]]+D[F[i][j]][j])F[i][j]++;\n }\n ll _;cin>>_;\n min_priority_queue<pair<ll,pair<string,pi>>>Q;\n while(_--){\n ll u,v,t;string s;cin>>u>>v>>t>>s;u--;v--;\n Q.emplace(make_pair(t,make_pair(s,pi(u,v))));\n }\n vector<pair<ll,string>>ans;\n vector<vector<vector<pair<ll,string>>>>letters(N,vector<vector<pair<ll,string>>>(N));\n vector<bool>officer(N,1);\n while(sz(Q)){\n ll t=Q.top().F;\n string s=Q.top().S.F;\n auto[u,v]=Q.top().S.S;\n Q.pop();\n if(s==\"|\"){\n pi p=pi(1e18,0);\n REP(v,N)for(auto[nt,ns]:letters[u][v])p=min(p,pi(nt,F[u][v]));\n if(p.F>1e17)officer[u]=1;\n else{\n REP(v,N)if(F[u][v]==p.S){\n for(auto[nt,ns]:letters[u][v])Q.emplace(make_pair(t+E[u][p.S],make_pair(ns,pi(p.S,v))));\n letters[u][v].clear();\n }\n Q.emplace(make_pair(t+2*E[u][p.S],make_pair(\"|\",pi(u,u))));\n }\n }\n else{\n if(u==v)ans.emplace_back(make_pair(t,s));\n else{\n if(officer[u]){\n officer[u]=0;\n Q.emplace(make_pair(t,make_pair(\"|\",pi(u,u))));\n }\n letters[u][v].emplace_back(make_pair(t,s));\n }\n }\n }\n REP(i,sz(ans))cout<<ans[i].S<<\" \"<<ans[i].F<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3996, "score_of_the_acc": -0.8468, "final_rank": 10 }, { "submission_id": "aoj_2010_4849361", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<LL,LL> P;\ntypedef pair<LL,int> LP;\nconst LL INF=1LL<<40;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nvector<vector<LL>> dis,path,to;\nvector<tuple<LL,LL,LL,string>> mail;\ntypedef tuple<LL,LL,LL> T;\npriority_queue<T,vector<T>,greater<T>> q1;\nbool emp[50];\nbool used[50];\n\nint main(){\n LL n,m,q;\n int i,j,k;\n LL a,b,c;\n LL p;\n string sa;\n int cnt=0;\n while(cin>>n>>m){\n if(!n)break;\n if(cnt)cout<<endl;\n cnt++;\n dis.assign(n,vector<LL>(n,INF));\n path.assign(n,vector<LL>(n,INF));\n to.assign(n,vector<LL>(n,-1));\n for(i=0;i<m;i++){\n cin>>a>>b>>c;\n a--,b--;\n dis[a][b]=c;\n dis[b][a]=c;\n path[a][b]=c;\n path[b][a]=c;\n }\n for(i=0;i<n;i++){\n dis[i][i]=0;\n }\n for(k=0;k<n;k++){\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);\n }\n }\n }\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n for(k=0;k<n;k++){\n if(path[i][k]<INF && dis[i][j]==path[i][k]+dis[k][j])break;\n }\n to[i][j]=k;\n }\n }\n\n mail.clear();\n cin>>q;\n for(i=0;i<q;i++){\n cin>>a>>b>>c>>sa;\n a--,b--;\n mail.push_back(make_tuple(a,b,c,sa));\n q1.push(make_tuple(c,a,i));\n }\n memset(emp,false,sizeof(emp));\n memset(used,false,sizeof(used));\n vector<P> st[40][40];\n vector<pair<LL,string>> vs;\n while(!q1.empty()){\n p=get<0>(q1.top());\n while(!q1.empty() && p==get<0>(q1.top())){\n tie(ignore,a,b)=q1.top(),q1.pop();\n if(b==-1){\n emp[a]=false,used[a]=true;\n continue;\n }\n c=get<1>(mail[b]);\n if(a==c){\n vs.push_back(make_pair(p,get<3>(mail[b])));\n continue;\n }\n st[a][to[a][c]].push_back(make_pair(p,b));\n used[a]=true;\n }\n for(i=0;i<n;i++){\n if(!used[i] || emp[i])continue;\n used[i]=false;\n a=INF,k=-1;\n for(j=0;j<n;j++){\n if(st[i][j].empty())continue;\n if(a>st[i][j][0].first){\n a=st[i][j][0].first,k=j;\n }\n }\n if(k==-1)continue;\n for(auto node:st[i][k]){\n int id=node.second;\n q1.push(make_tuple(p+path[i][k],k,id));\n }\n st[i][k].clear();\n emp[i]=true;\n q1.push(make_tuple(p+path[i][k]*2,i,-1));\n }\n }\n sort(vs.begin(),vs.end());\n for(auto node:vs){\n cout<<node.second<<\" \"<<node.first<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3340, "score_of_the_acc": -0.6263, "final_rank": 4 }, { "submission_id": "aoj_2010_4811722", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\n//0...荷物届く\n//1...人が帰る\nstruct ste {\n\tint tm,typ, id;\n};\nbool operator<(const ste& a, const ste& b) {\n\tif (a.tm != b.tm)return a.tm > b.tm;\n\tif (a.typ != b.typ)return a.typ > b.typ;\n\telse return a.id > b.id;\n}\nint n, m;\nint dist[32][32];\nstruct edge {\n\tint to, cost;\n};\nvoid solve() {\n\trep(i, n)rep(j, n) {\n\t\tif (i == j)dist[i][j] = 0;\n\t\telse dist[i][j] = mod;\n\t}\n\tvector<vector<edge>> G(n);\n\trep(i, m) {\n\t\tint a, b; cin >> a >> b;\n\t\ta--; b--;\n\t\tint c; cin >> c;\n\t\tdist[a][b] = dist[b][a] = min(dist[a][b],c);\n\t\tG[a].push_back({ b,c });\n\t\tG[b].push_back({ a,c });\n\t}\n\trep(k, n)rep(i, n)rep(j, n) {\n\t\tdist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n\t}\n\tint l; cin >> l; vector<queue<int>> tos(l);\n\t\n\t//配達員の存在\n\tvector<bool> exi(n, true);\n\t//荷物の存在状況\n\tvector<vector<P>> bags(n);\n\n\tpriority_queue<ste> q;\n\tvector<string> names(l);\n\t//荷物の前の位置\n\tvector<int> pre(l);\n\trep(i, l) {\n\t\tint a, b, t; string s;\n\t\tcin >> a >> b >> t >> s;\n\t\tnames[i] = s;\n\t\ta--; b--;\n\t\tint d = dist[a][b];\n\t\tint cur = a;\n\t\ttos[i].push(cur);\n\t\t//cout << \"?! \" << cur << \"\\n\";\n\t\twhile (cur != b) {\n\t\t\tint chk = mod;\n\t\t\tfor (edge e : G[cur]) {\n\t\t\t\tif (e.cost + dist[e.to][b] == d) {\n\t\t\t\t\tchk = min(chk, e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t\td -= dist[cur][chk];\n\t\t\tcur = chk;\n\t\t\ttos[i].push(cur);\n\t\t\t//cout << \"?! \" << cur << \"\\n\";\n\t\t}\n\t\tq.push({ t,0,i });\n\t\tpre[i] = -1;\n\t}\n\tvector<string> ans;\n\tvector<int> anstime;\n\twhile (!q.empty()) {\n\t\t//cout << \"start\\n\";\n\t\tint t = q.top().tm;\n\t\tvector<string> curans;\n\t\twhile (q.size() && q.top().tm == t) {\n\t\t\tste s = q.top(); q.pop();\n\t\t\t//cout << \"? \"<<s.tm << \" \" << s.typ << \" \" << s.id << \"\\n\";\n\t\t\tif (s.typ == 0) {\n\t\t\t\tint id = s.id;\n\t\t\t\tint cur = tos[id].front();\n\t\t\t\tint pr = pre[id];\n\t\t\t\t//cout << \"shogeki\" << cur << \" \" << pr << \"\\n\";\n\t\t\t\tif (pr >= 0) {\n\t\t\t\t\t//cout << \"nande \" << pr << \"\\n\";\n\t\t\t\t\tq.push({ t + dist[cur][pr],1,pr });\n\t\t\t\t}\n\t\t\t\tif (tos[id].size() == 1) {\n\t\t\t\t\tcurans.push_back(names[id]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tbags[cur].push_back({ t,id });\n\t\t\t\ttos[id].pop();\n\t\t\t\t\n\t\t\t}\n\t\t\telse {\n\t\t\t\t//cout << \"??????? \" << s.id << \"\\n\";\n\t\t\t\texi[s.id] = true;\n\t\t\t}\n\t\t}\n\t\tsort(all(curans));\n\t\tfor (string s : curans) {\n\t\t\tans.push_back(s);\n\t\t\tanstime.push_back(t);\n\t\t}\n\n\t\t//cout << \"!!! \" << exi[1] << \" \" << bags[1].size() << \"\\n\";\n\t\trep(i, n) {\n\t\t\tif (exi[i] && bags[i].size()) {\n\t\t\t\tint mi = bags[i][0].first;\n\t\t\t\tint to = mod;\n\t\t\t\trep(j, bags[i].size()) {\n\t\t\t\t\tif (bags[i][j].first == mi) {\n\t\t\t\t\t\tint id = bags[i][j].second;\n\t\t\t\t\t\tto = min(to, tos[id].front());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t//cout << \"wow \" << i << \" \" << to << \"\\n\";\n\t\t\t\tint len = bags[i].size();\n\t\t\t\tvector<P> nex;\n\t\t\t\trep(j,len) {\n\t\t\t\t\tint id = bags[i][j].second;\n\t\t\t\t\tif (tos[id].front() == to) {\n\t\t\t\t\t\tq.push({ t + dist[i][to],0,id });\n\t\t\t\t\t\tpre[id] = i;\n\t\t\t\t\t}\n\t\t\t\t\telse nex.push_back(bags[i][j]);\n\t\t\t\t}\n\t\t\t\tswap(bags[i], nex);\n\t\t\t\texi[i] = false;\n\t\t\t}\n\t\t}\n\t\t//cout << pre[1] << \" \" << bags[1].size() << \"\\n\";\n\t\t//cout << pre[0] << \" \" << pre[1] << \"\\n\";\n\t}\n\trep(i, ans.size())cout << ans[i] <<\" \"<<anstime[i]<< endl;\n\t\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(8);\n\t//init_f(); \n\t//init();\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\tbool f = true;\n\twhile (cin >> n >> m, n) {\n\t\tif (!f)cout << endl;\n\t\telse f = false;\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3860, "score_of_the_acc": -0.8011, "final_rank": 9 }, { "submission_id": "aoj_2010_4773554", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <utility>\nusing namespace std;\nconst int inf = 1e9;\n\nstruct transfer{\n int from, to, direct_to, time;\n string name;\n transfer(int from, int to, int direct_to, int time, string name):\n from(from), to(to), direct_to(direct_to), time(time), name(name){}\n transfer(){}\n bool operator <(const transfer &a) const{\n return (time!=a.time)? time<a.time: direct_to<a.direct_to;\n }\n};\n\nint main(){\n bool is_first_dataset = true;\n while(1){\n int n,m;\n cin >> n >> m;\n if(n == 0) break;\n if(is_first_dataset){\n is_first_dataset = false;\n }else{\n cout << endl;\n }\n\n vector<vector<int>> dist_0(n, vector<int>(n, inf));\n for(int i=0; i<n; i++){\n dist_0[i][i] = 0;\n }\n for(int i=0; i<m; i++){\n int a,b,c;\n cin >> a >> b >> c;\n a--; b--;\n dist_0[a][b] = dist_0[b][a] = c;\n }\n auto dist = dist_0;\n for(int k=0; k<n; k++){\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n }\n vector<vector<int>> direct(n, vector<int>(n));\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(i == j){\n direct[i][j] = i;\n continue;\n }\n for(int k=0; k<n; k++){\n if(k == i) continue;\n if(dist_0[i][k] +dist[k][j] == dist[i][j]){\n direct[i][j] = k;\n break;\n }\n }\n }\n }\n\n int q;\n cin >> q;\n vector<vector<transfer>> tr(n);\n priority_queue<int> event;\n for(int i=0; i<q; i++){\n int from,to,time;\n string name;\n cin >> from >> to >> time >> name;\n from--; to--;\n tr[from].emplace_back(from, to, direct[from][to], time, name);\n event.push(-time);\n }\n\n for(int i=0; i<n; i++){\n sort(tr[i].begin(), tr[i].end());\n }\n vector<int> rettime(n, 0);\n event.push(0);\n\n vector<pair<int,string>> ans;\n while(!event.empty()){\n int t = event.top();\n while(!event.empty() and event.top() == t){\n event.pop();\n }\n t = -t;\n for(int i=0; i<n; i++){\n if(t < rettime[i]) continue;\n if(tr[i].empty() or tr[i][0].time > t) continue;\n int dto = tr[i][0].direct_to;\n rettime[i] = t +2*dist[i][dto];\n event.push(-rettime[i]);\n event.push(-(t +dist[i][dto]));\n for(int j=(int)tr[i].size()-1; j>=0; j--){\n if(tr[i][j].direct_to == dto and t >= tr[i][j].time){\n if(tr[i][j].to == tr[i][j].direct_to){\n ans.emplace_back(t +dist[i][dto], tr[i][j].name);\n }else{\n int to = tr[i][j].to;\n tr[dto].emplace_back(dto, to, direct[dto][to], t+dist[i][dto], tr[i][j].name);\n }\n tr[i].erase(tr[i].begin() +j);\n } \n }\n sort(tr[dto].begin(), tr[dto].end());\n }\n }\n sort(ans.begin(), ans.end());\n for(auto p: ans){\n cout << p.second << \" \" << p.first << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3344, "score_of_the_acc": -0.8777, "final_rank": 11 }, { "submission_id": "aoj_2010_4154542", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long double EPS = 1e-10;\nconst int INF = 1e9;\nconst long double PI = acos(-1.0L);\n//const ll mod = 1000000007;\n\nstruct query {\n int type;\n int pos;\n string name;\n};\n\nbool operator< (query a, query b) {\n return a.type < b.type;\n}\n\nint N, M;\nint dist[32][32];\nint Next[32][32];\nint Connected[32][32];\nbool Exist[32];\nvector<vector<string>> QUEUE[32];\nmap<int, vector<query>> mp;\nmap<string, int> ans;\nmap<string, int> destination;\ntypedef pair<int, query> iq;\ntypedef pair<int, string> is;\n\nvoid Add(query q) {\n //cerr << \"Add: \" << q.pos << \" \" << q.name << endl;\n int to = Next[q.pos][destination[q.name]];\n for(int i = 0; i < QUEUE[q.pos].size(); i++) {\n if(to == Next[q.pos][destination[QUEUE[q.pos][i][0]]]) {\n QUEUE[q.pos][i].push_back(q.name);\n return;\n }\n }\n QUEUE[q.pos].push_back({q.name});\n}\n\nvoid Serve(int pos, int timer) {\n if(!Exist[pos]) return;\n if(QUEUE[pos].empty()) return;\n for(string tmp : QUEUE[pos][0]) {\n int to = Next[pos][destination[tmp]];\n int newtimer = timer + dist[pos][to];\n query q;\n q.type = 1;\n q.name = tmp;\n q.pos = to;\n //cerr << \"Serve: from \" << pos << \" to \" << to << \" \" << tmp << \" when \" << newtimer << endl;\n mp[newtimer].push_back(q);\n q.type = 0;\n q.name = \"\";\n q.pos = pos;\n newtimer += dist[pos][to];\n mp[newtimer].push_back(q);\n }\n QUEUE[pos].erase(QUEUE[pos].begin());\n Exist[pos] = false;\n}\n\nvoid solve() {\n mp.clear();\n ans.clear();\n destination.clear();\n for(int i = 0; i < N; i++) {\n Exist[i] = true;\n QUEUE[i].clear();\n }\n for(int i = 0; i < N; i++) {\n for(int j = 0; j < N; j++) {\n dist[i][j] = INF;\n Connected[i][j] = 1e9;\n }\n dist[i][i] = 0;\n Next[i][i] = -1;\n Connected[i][i] = 0;\n }\n for(int i = 0; i < M; i++) {\n int a, b, l;\n cin >> a >> b >> l;\n a--;\n b--;\n dist[a][b] = l;\n dist[b][a] = l;\n Connected[a][b] = l;\n Connected[b][a] = l;\n }\n int Q;\n cin >> Q;\n while(Q--) {\n int a, b, t;\n string S;\n cin >> a >> b >> t >> S;\n a--;\n b--;\n destination[S] = b;\n query q;\n q.pos = a;\n q.name = S;\n q.type = 1;\n mp[t].push_back(q);\n }\n for(int i = 0; i < N; i++) {\n for(int j = 0; j < N; j++) {\n for(int k = 0; k < N; k++) {\n chmin(dist[j][k], dist[j][i] + dist[i][k]);\n }\n }\n }\n for(int from = 0; from < N; from++) {\n for(int to = 0; to < N; to++) {\n if(from == to) continue;\n int tmp = -1;\n for(int i = 0; i < N; i++) {\n if(Connected[from][i] == 1e9) continue;\n if(i == from) continue;\n if(tmp == -1 or Connected[from][tmp] + dist[tmp][to] > Connected[from][i] + dist[i][to]) \n tmp = i;\n }\n Next[from][to] = tmp;\n }\n }\n for(auto itr = mp.begin(); itr != mp.end(); itr++) {\n auto tmp = *itr;\n //cerr << \"------\" << tmp.first << \"------\" << endl;\n vector<iq> vec;\n for(auto q : tmp.second) {\n //cerr << q.type << \" \" << q.name << \" \" << q.pos << endl;\n if(q.type == 0) {\n //cerr << \"clerk of \" << q.pos << \" comes back\" << endl;\n Exist[q.pos] = true;\n }\n if(q.type == 1) {\n //cerr << q.name << \" \" << destination[q.name] << endl;\n if(destination[q.name] == q.pos) {\n ans[q.name] = tmp.first;\n continue;\n }\n vec.push_back({Next[q.pos][destination[q.name]], q});\n }\n }\n sort(vec.begin(), vec.end());\n for(auto tmp : vec) {\n Add(tmp.second);\n }\n for(int i = 0; i < N; i++) {\n Serve(i, tmp.first);\n }\n }\n vector<is> ANS;\n for(auto tmp : ans) {\n ANS.push_back({tmp.second, tmp.first});\n }\n sort(ANS.begin(), ANS.end());\n for(auto tmp : ANS) {\n cout << tmp.second << \" \" << tmp.first << endl;\n }\n /*\n for(int i = 0; i < N; i++) {\n for(int j = 0; j < N; j++) {\n cerr << Next[i][j] << \" \";\n }\n cerr << endl;\n }\n */\n}\n\nint main() {\n //cout.precision(10);\n cin.tie(0);\n ios::sync_with_stdio(false);\n int testcase = 0;\n while(cin >> N >> M) {\n if(N == 0) break;\n if(testcase != 0) cout << endl;\n testcase++;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4016, "score_of_the_acc": -1.1035, "final_rank": 14 }, { "submission_id": "aoj_2010_3570843", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\nconstexpr int inf = numeric_limits<int>::max();\n\nstruct edge {\n int from, to, cost;\n edge(int f, int t, int c) : from(f), to(t), cost(c) {}\n};\n\nusing edges = vector<edge>;\nusing graph = vector<edges>;\n\nvoid add_edge(graph& g, int from, int to, int cost) {\n g[from].emplace_back(from, to, cost);\n g[to].emplace_back(to, from, cost);\n}\n\nvector<int> dijkstra(graph const& g, int s) {\n const int n = g.size();\n vector<int> dist(n, inf / 2);\n dist[s] = 0;\n priority_queue<pii, vector<pii>, greater<>> que;\n que.emplace(0, s);\n while(!que.empty()) {\n int cur_d, v; tie(cur_d, v) = que.top();\n que.pop();\n if(cur_d > dist[v]) continue;\n for(auto const& e : g[v]) {\n if(dist[e.to] > dist[v] + e.cost) {\n dist[e.to] = dist[v] + e.cost;\n que.emplace(dist[e.to], e.to);\n }\n }\n }\n return dist;\n}\n\nedge calc_next_v(graph const& g, int s, int t) {\n const auto from_s = dijkstra(g, s);\n const auto from_t = dijkstra(g, t);\n edge res{s, inf, -1};\n for(auto const& e : g[s]) {\n if(from_s[e.from] + e.cost + from_t[e.to] == from_s[t] && res.to > e.to) {\n res = e;\n }\n }\n return res;\n}\n\nstruct packet {\n string name;\n int to;\n packet(string n, int t) : name(n), to(t) {}\n};\n\nstruct event {\n int from, time;\n packet pkt;\n event(int f, int t, packet p) : from(f), time(t), pkt(p) {}\n bool operator>(const event& e) const {\n return time > e.time;\n }\n};\n\nstruct postman {\n int idx, cur, dst, remain, ecost;\n vector<packet> ps;\n postman(int i) : idx(i), cur(i), dst(-1), remain(0), ecost(-1) {}\n\n void move(int cur_t, int t, vector<vector<event>>& tasks, vector<pair<int, string>>& ans) {\n while(t != 0 && dst != -1) {\n const int use = min(remain, t);\n remain -= use, t -= use;\n cur_t += use;\n if(remain == 0) {\n cur = dst;\n dst = -1;\n for(auto const& p : ps) {\n if(p.to == cur) {\n ans.emplace_back(cur_t, p.name);\n } else {\n tasks[cur].emplace_back(cur, cur_t, p);\n }\n }\n ps.clear();\n if(cur != idx) { // return to original\n dst = idx;\n remain = ecost;\n }\n }\n }\n assert(t >= 0);\n }\n};\n\nint main() {\n int n, m;\n bool first = true;\n while(cin >> n >> m, n) {\n if(!first) cout << '\\n';\n first = false;\n\n graph g(n);\n for(int i = 0; i < m; ++i) {\n int f, t, cost; cin >> f >> t >> cost;\n f--, t--;\n add_edge(g, f, t, cost);\n }\n vector<vector<edge>> sg(n);\n for(int s = 0; s < n; ++s) {\n for(int t = 0; t < n; ++t) {\n sg[s].push_back(calc_next_v(g, s, t));\n }\n }\n int l; cin >> l;\n vector<event> ev;\n for(int i = 0; i < l; ++i) {\n int s, t, time; cin >> s >> t >> time;\n string name; cin >> name;\n s--, t--;\n ev.emplace_back(s, time, packet{name, t});\n }\n sort(begin(ev), end(ev), greater<>{});\n\n vector<postman> man;\n for(int i = 0; i < n; ++i) {\n man.emplace_back(i);\n }\n int cur_t = 0;\n vector<vector<event>> tasks(n);\n vector<pair<int, string>> ans;\n auto is_finished = [&] () {\n if(!ev.empty()) return false;\n for(auto const& t : tasks) {\n if(!t.empty()) return false;\n }\n for(auto const& p : man) {\n if(p.dst != -1) return false;\n }\n return true;\n };\n while(!is_finished()) {\n //cout << \"cur_t: \" << cur_t << endl;\n // determine next time\n int nxt_t = inf;\n for(auto const& p : man) {\n if(p.dst == -1) continue;\n nxt_t = min(nxt_t, cur_t + p.remain);\n }\n if(!ev.empty()) nxt_t = min(nxt_t, ev.back().time);\n //cout << \"ev.sz: \" << ev.size() << endl;\n //cout << \"elapsed: \" << nxt_t - cur_t << endl;\n\n // first, resolve task\n while(!ev.empty() && ev.back().time == nxt_t) {\n tasks[ev.back().from].push_back(ev.back());\n ev.pop_back();\n }\n\n // second, resolve postman\n for(int i = 0; i < n; ++i) {\n man[i].move(cur_t, nxt_t - cur_t, tasks, ans);\n }\n for(int i = 0; i < n; ++i) {\n // returned to original office\n if(man[i].dst != -1) continue;\n if(!tasks[i].empty()) {\n vector<event> nxt_ts;\n int min_time = inf, min_to = inf, ecost = -1;\n for(auto const& t : tasks[i]) {\n if(make_pair(min_time, min_to) > make_pair(t.time, sg[i][t.pkt.to].to)) {\n min_time = t.time;\n min_to = sg[i][t.pkt.to].to;\n ecost = sg[i][t.pkt.to].cost;\n }\n }\n assert(ecost >= 1);\n man[i].remain = man[i].ecost = ecost;\n man[i].dst = min_to;\n for(auto const& t : tasks[i]) {\n if(sg[i][t.pkt.to].to == min_to) {\n man[i].ps.push_back(t.pkt);\n } else {\n nxt_ts.push_back(t);\n }\n }\n tasks[i] = move(nxt_ts);\n }\n }\n cur_t = nxt_t;\n }\n\n //cout << \"finished: \" << endl;\n sort(begin(ans), end(ans));\n for(auto const& p : ans) {\n cout << p.second << \" \" << p.first << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3384, "score_of_the_acc": -1.6411, "final_rank": 18 }, { "submission_id": "aoj_2010_3363032", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nInt e[40][40],g[40][40];\nsigned main(){\n Int n,m;\n Int first=0;\n while(cin>>n>>m,n||m){\n if(exchange(first,1)) cout<<endl;\n memset(e,-1,sizeof(e));\n for(Int i=0;i<m;i++){\n Int a,b,c;\n cin>>a>>b>>c;\n a--;b--;\n e[a][b]=e[b][a]=c;\n }\n \n Int l;\n cin>>l;\n vector<Int> xs(l),ys(l),ts(l);\n vector<string> ss(l);\n for(Int i=0;i<l;i++){\n cin>>xs[i]>>ys[i]>>ts[i]>>ss[i];\n xs[i]--;\n ys[i]--;\n }\n\n const Int INF = 1e15;\n for(Int i=0;i<40;i++)\n for(Int j=0;j<40;j++)\n g[i][j]=~e[i][j]?e[i][j]:INF;\n \n for(Int i=0;i<40;i++) g[i][i]=0;\n\n for(Int k=0;k<n;k++)\n for(Int i=0;i<n;i++)\n for(Int j=0;j<n;j++)\n chmin(g[i][j],g[i][k]+g[k][j]);\n\n using S = pair<Int, string>; \n vector<S> ans;\n \n using T = tuple<Int, Int, Int, Int>;\n priority_queue<T, vector<T>, greater<T> > pq;\n for(Int i=0;i<l;i++) pq.emplace(ts[i],xs[i],ts[i],i);\n\n vector<Int> ws(n,0);\n while(!pq.empty()){\n T pt=pq.top();\n Int t,v,a,k;\n tie(t,v,a,k)=pt;\n if(t<ws[v]){\n pq.pop();\n pq.emplace(ws[v],v,a,k);\n continue;\n }\n \n vector<Int> idxs,arrs;\n while(!pq.empty()){\n T qt=pq.top();\n Int tt,vv,aa,idx;\n tie(tt,vv,aa,idx)=qt;\n if(t!=tt||v!=vv) break;\n pq.pop();\n idxs.emplace_back(idx);\n arrs.emplace_back(aa);\n }\n ws[v]=t;\n \n Int arr=arrs[0];\n vector<Int> nxts(idxs.size());\n for(Int i=0;i<(Int)idxs.size();i++){\n chmin(arr,arrs[i]);\n Int y=ys[idxs[i]];\n Int u=-1;\n for(Int j=0;j<n;j++){\n if(e[v][j]<0) continue;\n if(e[v][j]+g[j][y]==g[v][y]){\n u=j;\n break;\n }\n }\n nxts[i]=u;\n }\n \n Int nxt=n;\n for(Int i=0;i<(Int)idxs.size();i++)\n if(arrs[i]==arr) chmin(nxt,nxts[i]);\n\n ws[v]+=e[v][nxt]*2;\n for(Int i=0;i<(Int)idxs.size();i++){\n if(nxts[i]==nxt){\n if(ys[idxs[i]]==nxt){\n ans.emplace_back(t+e[v][nxt],ss[idxs[i]]);\n }else{\n pq.emplace(t+e[v][nxt],nxt,t+e[v][nxt],idxs[i]);\n }\n }else{\n pq.emplace(ws[v],v,arrs[i],idxs[i]);\n }\n }\n }\n \n sort(ans.begin(),ans.end());\n for(auto p:ans) cout<<p.second<<\" \"<<p.first<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": -0.6169, "final_rank": 3 }, { "submission_id": "aoj_2010_3064411", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n//#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n#define BIG_NUM INT_MAX\n#define NUM 32\n#define SIZE 1000\n\nenum Type{\n\tHuman,\n\tLetter,\n};\n\nstruct Edge{\n\tEdge(int arg_to,int arg_dist){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t}\n\tint to,dist;\n};\n\nstruct Porter{\n\tint home_office;\n\tvector<int> Bag;\n};\n\nstruct Item{\n\tstring name;\n\tint final_office;\n};\n\nstruct Data{\n\tData(int arg_item_id,int arg_arrive_time,int arg_next_loc){\n\t\titem_id = arg_item_id;\n\t\tarrive_time = arg_arrive_time;\n\t\tnext_loc = arg_next_loc;\n\t}\n\tbool operator<(const struct Data& arg) const{\n\t\tif(arrive_time != arg.arrive_time){\n\t\t\treturn arrive_time < arg.arrive_time;\n\t\t}else{\n\t\t\treturn next_loc < arg.next_loc;\n\t\t}\n\t}\n\tint item_id,arrive_time,next_loc;\n};\n\nstruct Office{\n\tbool can_port;\n\tint pre_time; //ポーターが前回帰ってきた時刻\n\tvector<Data> V;\n};\n\nstruct Info{\n\tInfo(Type arg_type,int arg_time,int arg_id,int arg_arrive_loc){\n\t\ttype = arg_type;\n\t\ttime = arg_time;\n\t\tid = arg_id;\n\t\tarrive_loc = arg_arrive_loc;\n\t}\n\tbool operator<(const struct Info &arg) const{ //時刻の昇順(PQ)\n\t\treturn time > arg.time;\n\t}\n\tType type;\n\tint time,id,arrive_loc;\n};\n\nstruct FINISH{\n\tFINISH(string arg_name,int arg_time){\n\t\tname = arg_name;\n\t\ttime = arg_time;\n\t}\n\tbool operator<(const struct FINISH &arg) const{\n\t\tif(time != arg.time){ //時刻または名前の昇順(PQ)\n\t\t\treturn time > arg.time;\n\t\t}else{\n\t\t\treturn name > arg.name;\n\t\t}\n\t}\n\tstring name;\n\tint time;\n};\n\nint N,E;\nint min_dist[NUM][NUM];\nint next_table[NUM][NUM];\nint num_item;\nvector<Edge> G[NUM];\nOffice office[NUM];\nPorter porter[NUM];\nItem item[SIZE];\npriority_queue<Info> EVENT;\npriority_queue<FINISH> ANS;\nint case_num;\n\n\n//転送先へ最短経路で届ける際の、次の中継点を求める\nint find_next(int current,int goal){\n\n\tint minimum = min_dist[current][goal],min_index = BIG_NUM,mid;\n\n\tfor(int i = 0; i < G[current].size(); i++){\n\t\tmid = G[current][i].to;\n\t\tif(G[current][i].dist+min_dist[mid][goal] != minimum)continue;\n\n\t\tmin_index = min(min_index,mid);\n\t}\n\n\treturn min_index;\n}\n\n//bool DEBUG = false;\n\nvoid func(){\n\n\tif(case_num > 0)printf(\"\\n\");\n\n\tfor(int i = 0; i < N; i++){\n\t\tG[i].clear();\n\t\toffice[i].V.clear();\n\t\tporter[i].Bag.clear();\n\t}\n\n\twhile(!EVENT.empty())EVENT.pop();\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(i != k){\n\t\t\t\tmin_dist[i][k] = BIG_NUM;\n\t\t\t}else{\n\t\t\t\tmin_dist[i][k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint from,to,dist;\n\n\tfor(int loop = 0; loop < E; loop++){\n\n\t\tscanf(\"%d %d %d\",&from,&to,&dist);\n\t\tfrom--;\n\t\tto--;\n\n\t\tmin_dist[from][to] = dist;\n\t\tmin_dist[to][from] = dist;\n\n\t\tG[from].push_back(Edge(to,dist));\n\t\tG[to].push_back(Edge(from,dist));\n\t}\n\n\t//全点対間最短経路\n\tfor(int mid = 0; mid < N; mid++){\n\t\tfor(int start = 0; start < N; start++){\n\t\t\tif(min_dist[start][mid] == BIG_NUM)continue;\n\t\t\tfor(int goal = 0; goal < N; goal++){\n\t\t\t\tif(min_dist[mid][goal] == BIG_NUM)continue;\n\t\t\t\tmin_dist[start][goal] = min(min_dist[start][goal],min_dist[start][mid]+min_dist[mid][goal]);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int a = 0; a < N; a++){\n\t\tfor(int b = 0; b < N; b++){\n\t\t\tif(a == b)continue;\n\t\t\tnext_table[a][b] = find_next(a,b);\n\t\t}\n\t}\n\n\tscanf(\"%d\",&num_item);\n\n\tint tmp_time;\n\n\tfor(int i = 0; i < num_item; i++){\n\n\t\tcin >> from >> to >> tmp_time >> item[i].name;\n\n\t\tfrom--;\n\t\tto--;\n\n\t\titem[i].final_office = to;\n\n\t\tEVENT.push(Info(Letter,tmp_time,i,from));\n\t}\n\n\t//配達人を、自分の本拠地に配置する\n\tfor(int i = 0; i < N; i++){\n\n\t\tporter[i].home_office = i;\n\n\t\toffice[i].can_port = true;\n\t\toffice[i].pre_time = 0;\n\t}\n\n\tint current_time,tmp_id,tmp_loc,letter_id,tmp_next_loc;\n\n\tint num_finish = 0;\n\tvector<Data> work;\n\n\twhile(!EVENT.empty()){\n\n\t\tcurrent_time = EVENT.top().time;\n\n\t\t//同時刻のイベントを同時に処理する\n\t\twhile(EVENT.empty() == false && EVENT.top().time == current_time){\n\n\t\t\tif(EVENT.top().type == Human){\n\n\t\t\t\ttmp_id = EVENT.top().id;\n\n\t\t\t\tif(porter[tmp_id].home_office == EVENT.top().arrive_loc){ //本拠地に戻った場合\n\n\t\t\t\t\toffice[porter[tmp_id].home_office].can_port = true;\n\t\t\t\t\toffice[porter[tmp_id].home_office].pre_time = EVENT.top().time; //戻った時刻を記録\n\n\t\t\t\t}else{ //別のofficeに到着した場合[★荷物を渡す★]\n\n\t\t\t\t\ttmp_loc = EVENT.top().arrive_loc;\n\t\t\t\t\ttmp_time = EVENT.top().time;\n\n\t\t\t\t\tfor(int i = 0; i < porter[tmp_id].Bag.size(); i++){\n\n\t\t\t\t\t\tletter_id = porter[tmp_id].Bag[i];\n\n\t\t\t\t\t\tif(item[letter_id].final_office == tmp_loc){ //★最終目的地に届いた場合\n\n\t\t\t\t\t\t\tnum_finish++;\n\n\t\t\t\t\t\t\tANS.push(FINISH(item[letter_id].name,tmp_time));\n\t\t\t\t\t\t\tif(num_finish == num_item)break;\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t//次の転送先を先に計算しておく\n\t\t\t\t\t\ttmp_next_loc = next_table[tmp_loc][item[letter_id].final_office];\n\t\t\t\t\t\tif(tmp_next_loc == tmp_loc){\n\t\t\t\t\t\t\tprintf(\"BUGG!!\\n\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\toffice[tmp_loc].V.push_back(Data(letter_id,tmp_time,tmp_next_loc));\n\t\t\t\t\t}\n\t\t\t\t\tporter[tmp_id].Bag.clear();\n\t\t\t\t\tif(num_finish == num_item)break;\n\n\t\t\t\t\t//★porterが本拠地に戻る★\n\t\t\t\t\tEVENT.push(Info(Human,tmp_time+min_dist[tmp_loc][porter[tmp_id].home_office],tmp_id,porter[tmp_id].home_office));\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tletter_id = EVENT.top().id;\n\t\t\t\ttmp_loc = EVENT.top().arrive_loc;\n\t\t\t\ttmp_time = EVENT.top().time;\n\n\t\t\t\t//転送先を計算\n\t\t\t\ttmp_next_loc = next_table[tmp_loc][item[letter_id].final_office];\n\t\t\t\toffice[tmp_loc].V.push_back(Data(letter_id,tmp_time,tmp_next_loc));\n\t\t\t}\n\n\t\t\tEVENT.pop();\n\t\t}\n\t\tif(num_finish == num_item)break;\n\n\t\t//home_officeにいるporterがいたら、Letterを持ち出す\n\n\t\tint min_time,min_next;\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tif(office[i].can_port == false || office[i].V.size() == 0)continue;\n\n\t\t\t/*\n\t\t\t *最も早く、officeに届いた郵便物を探す。\n\t\t\t *複数あるなら、届先の番号が若い順\n\t\t\t */\n\n\t\t\tsort(office[i].V.begin(),office[i].V.end());\n\n\t\t\tmin_time = office[i].V[0].arrive_time; //★到着が早い順にpushされているはず★\n\t\t\tmin_next = office[i].V[0].next_loc;\n\n\t\t\tfor(int k = 1; k < office[i].V.size(); k++){\n\t\t\t\tif(min_time < office[i].V[k].arrive_time)break;\n\t\t\t\tif(min_time == office[i].V[k].arrive_time){\n\t\t\t\t\tmin_next = min(min_next,office[i].V[k].next_loc);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//届先がmin_nextのletterを、porterに持たせる。porterは出発させる\n\t\t\tfor(int k = 0; k < office[i].V.size(); k++){\n\n\t\t\t\tif(office[i].V[k].next_loc == min_next){ //ポーターのバッグに詰める\n\n\t\t\t\t\tporter[i].Bag.push_back(office[i].V[k].item_id);\n\n\t\t\t\t}else{ //今回は運ばない\n\n\t\t\t\t\twork.push_back(office[i].V[k]);\n\t\t\t\t}\n\t\t\t}\n\t\t\toffice[i].V.clear();\n\n\t\t\tif(work.size() > 0){\n\n\t\t\t\tfor(int k = 0; k < work.size(); k++){\n\t\t\t\t\toffice[i].V.push_back(work[k]);\n\t\t\t\t}\n\t\t\t\twork.clear();\n\t\t\t}\n\t\t\t//ポーターの移動\n\t\t\toffice[i].can_port = false;\n\n\t\t\tif(office[i].pre_time <= min_time){ //★porterが待つ場合\n\n\t\t\t\tEVENT.push(Info(Human,min_time+min_dist[i][min_next],i,min_next));\n\n\t\t\t}else{ //★porterが待たない場合\n\n\t\t\t\tEVENT.push(Info(Human,office[i].pre_time+min_dist[i][min_next],i,min_next));\n\t\t\t}\n\t\t}\n\t}\n\n\twhile(!ANS.empty()){\n\n\t\tprintf(\"%s %d\\n\",ANS.top().name.c_str(),ANS.top().time);\n\t\tANS.pop();\n\t}\n\n\tcase_num++;\n}\n\nint main(){\n\n\tcase_num = 0;\n\n\twhile(true){\n\n\t\t//if(DEBUG)break;\n\n\t\tscanf(\"%d %d\",&N,&E);\n\t\tif(N == 0 && E == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3488, "score_of_the_acc": -0.6761, "final_rank": 5 }, { "submission_id": "aoj_2010_3064409", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n//#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n#define BIG_NUM INT_MAX\n#define NUM 32\n#define SIZE 1000\n\nenum Type{\n\tHuman,\n\tLetter,\n};\n\nstruct Edge{\n\tEdge(int arg_to,int arg_dist){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t}\n\tint to,dist;\n};\n\nstruct Porter{\n\tint home_office;\n\tvector<int> Bag;\n};\n\nstruct Item{\n\tstring name;\n\tint final_office;\n};\n\nstruct Data{\n\tData(int arg_item_id,int arg_arrive_time,int arg_next_loc){\n\t\titem_id = arg_item_id;\n\t\tarrive_time = arg_arrive_time;\n\t\tnext_loc = arg_next_loc;\n\t}\n\tbool operator<(const struct Data& arg) const{\n\t\tif(arrive_time != arg.arrive_time){\n\t\t\treturn arrive_time < arg.arrive_time;\n\t\t}else{\n\t\t\treturn next_loc < arg.next_loc;\n\t\t}\n\t}\n\tint item_id,arrive_time,next_loc;\n};\n\nstruct Office{\n\tbool can_port;\n\tint pre_time; //ポーターが前回帰ってきた時刻\n\tvector<Data> V;\n};\n\nstruct Info{\n\tInfo(Type arg_type,int arg_time,int arg_id,int arg_arrive_loc){\n\t\ttype = arg_type;\n\t\ttime = arg_time;\n\t\tid = arg_id;\n\t\tarrive_loc = arg_arrive_loc;\n\t}\n\tbool operator<(const struct Info &arg) const{ //時刻の昇順(PQ)\n\t\treturn time > arg.time;\n\t}\n\tType type;\n\tint time,id,arrive_loc;\n};\n\nstruct FINISH{\n\tFINISH(string arg_name,int arg_time){\n\t\tname = arg_name;\n\t\ttime = arg_time;\n\t}\n\tbool operator<(const struct FINISH &arg) const{\n\t\tif(time != arg.time){ //時刻または名前の昇順(PQ)\n\t\t\treturn time > arg.time;\n\t\t}else{\n\t\t\treturn name > arg.name;\n\t\t}\n\t}\n\tstring name;\n\tint time;\n};\n\nint N,E;\nint min_dist[NUM][NUM];\nint next_table[NUM][NUM];\nint num_item;\nvector<Edge> G[NUM];\nOffice office[NUM];\nPorter porter[NUM];\nItem item[SIZE];\npriority_queue<Info> EVENT;\npriority_queue<FINISH> ANS;\nint case_num;\n\n\n//転送先へ最短経路で届ける際の、次の中継点を求める\nint find_next(int current,int goal){\n\n\tint minimum = min_dist[current][goal],min_index = BIG_NUM,mid;\n\n\tfor(int i = 0; i < G[current].size(); i++){\n\t\tmid = G[current][i].to;\n\t\tif(G[current][i].dist+min_dist[mid][goal] != minimum)continue;\n\n\t\tmin_index = min(min_index,mid);\n\t}\n\n\treturn min_index;\n}\n\n//bool DEBUG = false;\n\nvoid func(){\n\n\tif(case_num > 0)printf(\"\\n\");\n\n\tfor(int i = 0; i < N; i++){\n\t\tG[i].clear();\n\t\toffice[i].V.clear();\n\t\tporter[i].Bag.clear();\n\t}\n\n\twhile(!EVENT.empty())EVENT.pop();\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(i != k){\n\t\t\t\tmin_dist[i][k] = BIG_NUM;\n\t\t\t}else{\n\t\t\t\tmin_dist[i][k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint from,to,dist;\n\n\tfor(int loop = 0; loop < E; loop++){\n\n\t\tscanf(\"%d %d %d\",&from,&to,&dist);\n\t\tfrom--;\n\t\tto--;\n\n\t\tmin_dist[from][to] = dist;\n\t\tmin_dist[to][from] = dist;\n\n\t\tG[from].push_back(Edge(to,dist));\n\t\tG[to].push_back(Edge(from,dist));\n\t}\n\n\t//全点対間最短経路\n\tfor(int mid = 0; mid < N; mid++){\n\t\tfor(int start = 0; start < N; start++){\n\t\t\tif(min_dist[start][mid] == BIG_NUM)continue;\n\t\t\tfor(int goal = 0; goal < N; goal++){\n\t\t\t\tif(min_dist[mid][goal] == BIG_NUM)continue;\n\t\t\t\tmin_dist[start][goal] = min(min_dist[start][goal],min_dist[start][mid]+min_dist[mid][goal]);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int a = 0; a < N; a++){\n\t\tfor(int b = 0; b < N; b++){\n\t\t\tif(a == b)continue;\n\t\t\tnext_table[a][b] = find_next(a,b);\n\t\t}\n\t}\n\n\tscanf(\"%d\",&num_item);\n\n\tint tmp_time;\n\n\tfor(int i = 0; i < num_item; i++){\n\n\t\tcin >> from >> to >> tmp_time >> item[i].name;\n\n\t\tfrom--;\n\t\tto--;\n\n\t\titem[i].final_office = to;\n\n\t\tEVENT.push(Info(Letter,tmp_time,i,from));\n\t}\n\n\t//配達人を、自分の本拠地に配置する\n\tfor(int i = 0; i < N; i++){\n\n\t\tporter[i].home_office = i;\n\n\t\toffice[i].can_port = true;\n\t\toffice[i].pre_time = 0;\n\t}\n\n\tint current_time,tmp_id,tmp_loc,letter_id,tmp_next_loc;\n\n\tint num_finish = 0;\n\tvector<Data> work;\n\n\twhile(!EVENT.empty()){\n\n\t\tcurrent_time = EVENT.top().time;\n\n\t\t//同時刻のイベントを同時に処理する\n\t\twhile(EVENT.empty() == false && EVENT.top().time == current_time){\n\n\t\t\tif(EVENT.top().type == Human){\n\n\t\t\t\ttmp_id = EVENT.top().id;\n\n\t\t\t\tif(porter[tmp_id].home_office == EVENT.top().arrive_loc){ //本拠地に戻った場合\n\n\t\t\t\t\toffice[porter[tmp_id].home_office].can_port = true;\n\t\t\t\t\toffice[porter[tmp_id].home_office].pre_time = EVENT.top().time; //戻った時刻を記録\n\n\t\t\t\t}else{ //別のofficeに到着した場合[★荷物を渡す★]\n\n\t\t\t\t\ttmp_loc = EVENT.top().arrive_loc;\n\t\t\t\t\ttmp_time = EVENT.top().time;\n\n\t\t\t\t\tfor(int i = 0; i < porter[tmp_id].Bag.size(); i++){\n\n\t\t\t\t\t\tletter_id = porter[tmp_id].Bag[i];\n\n\t\t\t\t\t\tif(item[letter_id].final_office == tmp_loc){ //★最終目的地に届いた場合\n\n\t\t\t\t\t\t\tnum_finish++;\n\n\t\t\t\t\t\t\tANS.push(FINISH(item[letter_id].name,tmp_time));\n\t\t\t\t\t\t\tif(num_finish == num_item)break;\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t//次の転送先を先に計算しておく\n\t\t\t\t\t\ttmp_next_loc = next_table[tmp_loc][item[letter_id].final_office];\n\t\t\t\t\t\tif(tmp_next_loc == tmp_loc){\n\t\t\t\t\t\t\tprintf(\"BUGG!!\\n\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\toffice[tmp_loc].V.push_back(Data(letter_id,tmp_time,tmp_next_loc));\n\t\t\t\t\t}\n\t\t\t\t\tporter[tmp_id].Bag.clear();\n\t\t\t\t\tif(num_finish == num_item)break;\n\n\t\t\t\t\t//★porterが本拠地に戻る★\n\t\t\t\t\tEVENT.push(Info(Human,tmp_time+min_dist[tmp_loc][porter[tmp_id].home_office],tmp_id,porter[tmp_id].home_office));\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tletter_id = EVENT.top().id;\n\t\t\t\ttmp_loc = EVENT.top().arrive_loc;\n\t\t\t\ttmp_time = EVENT.top().time;\n\n\t\t\t\t//転送先を計算\n\t\t\t\ttmp_next_loc = next_table[tmp_loc][item[letter_id].final_office];\n\t\t\t\toffice[tmp_loc].V.push_back(Data(letter_id,tmp_time,tmp_next_loc));\n\t\t\t}\n\n\t\t\tEVENT.pop();\n\t\t}\n\t\tif(num_finish == num_item)break;\n\n\t\t//home_officeにいるporterがいたら、Letterを持ち出す\n\n\t\tint min_time,min_next;\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tif(office[i].can_port == false || office[i].V.size() == 0)continue;\n\n\t\t\t/*\n\t\t\t *最も早く、officeに届いた郵便物を探す。\n\t\t\t *複数あるなら、届先の番号が若い順\n\t\t\t */\n\n\t\t\tsort(office[i].V.begin(),office[i].V.end());\n\n\t\t\tmin_time = office[i].V[0].arrive_time; //★到着が早い順にpushされているはず★\n\t\t\tmin_next = office[i].V[0].next_loc;\n\n\t\t\tfor(int k = 1; k < office[i].V.size(); k++){\n\t\t\t\tif(min_time < office[i].V[k].arrive_time)break;\n\t\t\t\tif(min_time == office[i].V[k].arrive_time){\n\t\t\t\t\tmin_next = min(min_next,office[i].V[k].next_loc);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//届先がmin_nextのletterを、porterに持たせる。porterは出発させる\n\t\t\tfor(int k = 0; k < office[i].V.size(); k++){\n\n\t\t\t\t//if(office[i].V[k].arrive_time == min_time && office[i].V[k].next_loc == min_next){ //ポーターのバッグに詰める\n\t\t\t\tif(office[i].V[k].next_loc == min_next){ //ポーターのバッグに詰める\n\n\t\t\t\t\tporter[i].Bag.push_back(office[i].V[k].item_id);\n\n\t\t\t\t}else{ //今回は運ばない\n\n\t\t\t\t\twork.push_back(office[i].V[k]);\n\t\t\t\t}\n\t\t\t}\n\t\t\toffice[i].V.clear();\n\n\t\t\tif(work.size() > 0){\n\n\t\t\t\tfor(int k = 0; k < work.size(); k++){\n\t\t\t\t\toffice[i].V.push_back(work[k]);\n\t\t\t\t}\n\t\t\t\twork.clear();\n\t\t\t}\n\t\t\t//ポーターの移動\n\t\t\toffice[i].can_port = false;\n\n\t\t\tif(office[i].pre_time <= min_time){ //★porterが待つ場合\n\n\t\t\t\tEVENT.push(Info(Human,min_time+min_dist[i][min_next],i,min_next));\n\n\t\t\t}else{ //★porterが待たない場合\n\n\t\t\t\tEVENT.push(Info(Human,office[i].pre_time+min_dist[i][min_next],i,min_next));\n\t\t\t}\n\t\t}\n\t}\n\n\twhile(!ANS.empty()){\n\n\t\tprintf(\"%s %d\\n\",ANS.top().name.c_str(),ANS.top().time);\n\t\tANS.pop();\n\t}\n\n\tcase_num++;\n}\n\nint main(){\n\n\tcase_num = 0;\n\n\twhile(true){\n\n\t\t//if(DEBUG)break;\n\n\t\tscanf(\"%d %d\",&N,&E);\n\t\tif(N == 0 && E == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3504, "score_of_the_acc": -0.6815, "final_rank": 6 }, { "submission_id": "aoj_2010_2962981", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n\n#include <iostream>\n#include <complex>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#define debug(x) cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << endl;\n\n#define mod 1000000007 //1e9+7(prime number)\n#define INF 1000000000 //1e9\n#define LLINF 2000000000000000000LL //2e18\n#define SIZE 50\n\nbool firstcase = true;\n\nstruct DATA{\n ll t;\n int id;\n int now;\n int next;\n int goal;\n\n const bool operator<(const DATA &B) const{\n if(t != B.t) return t > B.t;\n if(next != B.next) return next > B.next;\n return false;\n }\n\n DATA(ll t, int id, int now, int next, int goal):\n t(t), id(id), now(now), next(next), goal(goal){}\n};\n\nint dist[40][40];\nint connect[40][40];\nint n;\n\nint getNext(int a, int b){\n if (a == b) return a;\n for(int i=0;i<n;i++){\n if(connect[a][i] && connect[a][i] == dist[a][i] && dist[a][i] + dist[i][b] == dist[a][b])\n return i;\n }\n\n assert(false);\n return -1;\n}\n\nbool solve(){\n int m, q;\n string text[1000];\n \n priority_queue<DATA> pq;\n\n scanf(\"%d%d\", &n, &m);\n \n if(n == 0) return false;\n if(firstcase) firstcase = false;\n else puts(\"\");\n \n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n dist[i][j] = INF;\n connect[i][j] = false;\n }\n dist[i][i] = 0;\n }\n \n \n for(int i=0;i<m;i++){\n int a, b, c; \n scanf(\"%d%d%d\", &a, &b, &c);\n a--; b--;\n\n dist[a][b] = dist[b][a] = c;\n connect[a][b] = connect[b][a] = c;\n }\n\n for(int k=0;k<n;k++)\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n \n scanf(\"%d\", &q);\n\n for(int i=0;i<q;i++){\n int a, b, t;\n char temp[60];\n scanf(\"%d%d%d%s\", &a, &b, &t, temp);\n a--; b--;\n text[i] = temp;\n \n pq.push(DATA(t, i, a, getNext(a,b), b));\n }\n\n bool outside[40] = {};\n ll sent[40][40] = {};\n priority_queue<pair<ll,int> > que[40];\n queue<DATA> post[40][40];\n vector<pair<ll,string> > ans;\n\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n sent[i][j] = -1;\n \n while(pq.size()){\n queue<int> work;\n ll t = pq.top().t;\n \n while(pq.size() && pq.top().t == t){\n auto p = pq.top(); pq.pop();\n if(p.id != -1){\n if(p.now == p.goal){\n ans.push_back({p.t, text[p.id]});\n continue;\n }\n \n post[p.now][p.next].push(p);\n que[p.now].push({-p.t, -p.next});\n // cerr << \"[post] \" << t << \" : \" << p.now << \" -> \" << p.next << \" -> \" << p.goal << endl;\n }else{\n assert(outside[p.now]);\n outside[p.now] = false;\n // cerr << \"[back] \" << t << \" : \" << p.now << endl;\n }\n\n work.push(p.now);\n }\n \n\n while(work.size()){\n int now = work.front(); work.pop();\n if(outside[now]) continue;\n \n while(que[now].size() && -que[now].top().first <= t){\n auto p = que[now].top(); que[now].pop();\n\n if(-p.first <= sent[now][-p.second]) continue;\n \n //cerr << now << \" -> \" << -p.second << endl;\n \n while(post[now][-p.second].size()){\n auto p2 = post[now][-p.second].front(); post[now][-p.second].pop();\n \n p2.t = t + dist[now][p2.next];\n p2.now = p2.next;\n p2.next = getNext(p2.next, p2.goal);\n pq.push(p2);\n }\n \n outside[now] = true;\n pq.push(DATA((ll)t + dist[now][-p.second]*2, -1, now, -1, -1));\n sent[now][-p.second] = t;\n break;\n }\n\n }\n }\n\n sort(ans.begin(), ans.end());\n\n assert(ans.size() == q);\n \n for(int i=0;i<q;i++){\n cout << ans[i].second << \" \" << ans[i].first << endl;\n }\n \n return true;\n}\n\n\nint main(){\n while(solve());\n \n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4452, "score_of_the_acc": -1.5, "final_rank": 17 }, { "submission_id": "aoj_2010_2935347", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <utility>\n#include <cassert>\n#include <cstdio>\n\nusing namespace std;\nconst int INF = 1e9;\n\nstruct mail{\n int time, to, cur, next, id;\n};\n\nbool operator < (const mail &a, const mail &b){\n if(a.time != b.time) return a.time > b.time;\n if(a.next != b.next) return a.next > b.next;\n return a.cur > b.cur;\n}\n\nint main(){\n int n, m, c = 0;\n while(cin >> n >> m, n){\n if(c) printf(\"\\n\");\n else ++c;\n vector< vector<int> > D(n, vector<int>(n, INF)), E(n, vector<int>(n, -1));\n for(int i = 0; i < m; ++i){\n int u, v, d;\n cin >> u >> v >> d;\n --u;--v;\n D[u][v] = d;\n D[v][u] = d;\n E[u][v] = d;\n E[v][u] = d;\n }\n\n for(int i = 0; i < n; ++i) D[i][i] = 0;\n for(int k = 0; k < n; ++k){\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < n; ++j){\n D[i][j] = min(D[i][j], D[i][k] + D[k][j]);\n }\n }\n }\n\n vector< vector<int> > next(n, vector<int>(n, -1));\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < n; ++j){\n for(int k = 0; k < n; ++k){\n if((D[i][k] + D[k][j] != D[i][j]) || (D[i][k] != E[i][k])) continue;\n next[i][j] = k;\n break;\n }\n }\n }\n\n int l;\n cin >> l;\n vector<string> name(l);\n priority_queue<mail> pque;\n \n for(int i = 0; i < l; ++i){\n int from, to, time;\n cin >> from >> to >> time >> name[i];\n pque.push((mail){time, to-1, from-1, next[from-1][to-1], i});\n }\n \n vector<int> T(n, -1);\n vector< vector<int> > U(n, vector<int>(n, -1));\n vector< pair<int, string> > A;\n \n while(!pque.empty()){\n mail x = pque.top();\n pque.pop();\n \n if(U[x.cur][x.next] < x.time){\n T[x.cur] = max(T[x.cur], x.time);\n U[x.cur][x.next] = T[x.cur];\n T[x.cur] = max(T[x.cur], U[x.cur][x.next] + 2*D[x.cur][x.next]);\n }\n x.time = U[x.cur][x.next] + D[x.cur][x.next];\n x.cur = x.next;\n x.next = next[x.cur][x.to];\n\n if(x.cur == x.to){\n A.push_back(make_pair(x.time, name[x.id]));\n }else{\n pque.push(x);\n }\n }\n \n sort(A.begin(), A.end());\n for(int i = 0; i < l; ++i){\n cout << A[i].second << \" \" << A[i].first << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": -0.5753, "final_rank": 1 }, { "submission_id": "aoj_2010_2935340", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <utility>\n#include <cassert>\n#include <cstdio>\n\nusing namespace std;\nconst int INF = 1e9;\n\nstruct mail{\n int time, to, cur, next, id;\n};\n\nbool operator < (const mail &a, const mail &b){\n if(a.time != b.time) return a.time > b.time;\n if(a.next != b.next) return a.next > b.next;\n return a.cur > b.cur;\n}\n\nint main(){\n int n, m, c = 0;\n while(cin >> n >> m, n){\n if(c) printf(\"\\n\");\n else ++c;\n vector< vector<int> > D(n, vector<int>(n, INF)), E(n, vector<int>(n, 0));\n\n \n for(int i = 0; i < m; ++i){\n int u, v, d;\n cin >> u >> v >> d;\n --u;--v;\n D[u][v] = d;\n D[v][u] = d;\n E[u][v] = d;\n E[v][u] = d;\n }\n\n for(int i = 0; i < n; ++i) D[i][i] = 0;\n for(int k = 0; k < n; ++k){\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < n; ++j){\n D[i][j] = min(D[i][j], D[i][k] + D[k][j]);\n\n }\n }\n }\n\n vector< vector<int> > next(n, vector<int>(n, -1));\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < n; ++j){\n for(int k = 0; k < n; ++k){\n if(i == k || (D[i][k] + D[k][j] != D[i][j]) || (D[i][k] != E[i][k])) continue;\n next[i][j] = k;\n break;\n }\n }\n }\n\n int l;\n cin >> l;\n vector<string> name(l);\n priority_queue<mail> pque;\n \n for(int i = 0; i < l; ++i){\n int from, to, time;\n cin >> from >> to >> time >> name[i];\n pque.push((mail){time, to-1, from-1, next[from-1][to-1], i});\n }\n \n vector<int> T(n, -1);\n vector< vector<int> > U(n, vector<int>(n, -1));\n vector< pair<int, string> > A;\n \n while(!pque.empty()){\n mail x = pque.top();\n pque.pop();\n \n if(U[x.cur][x.next] < x.time){\n T[x.cur] = max(T[x.cur], x.time);\n U[x.cur][x.next] = T[x.cur];\n T[x.cur] = max(T[x.cur], U[x.cur][x.next] + 2*D[x.cur][x.next]);\n }\n x.time = U[x.cur][x.next] + D[x.cur][x.next];\n x.cur = x.next;\n x.next = next[x.cur][x.to];\n\n if(x.cur == x.to){\n A.push_back(make_pair(x.time, name[x.id]));\n }else{\n pque.push(x);\n }\n }\n \n sort(A.begin(), A.end());\n for(int i = 0; i < l; ++i){\n cout << A[i].second << \" \" << A[i].first << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3192, "score_of_the_acc": -0.5766, "final_rank": 2 }, { "submission_id": "aoj_2010_2917514", "code_snippet": "// g++ -std=c++11 a.cpp\n#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm> \n#include<map>\n#include<set>\n#include<unordered_map>\n#include<utility>\n#include<cmath>\n#include<random>\n#include<cstring>\n#include<queue>\n#include<stack>\n#include<bitset>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#include<assert.h>\n#include<typeinfo>\n#define loop(i,a,b) for(ll i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define FOR(i,a) for(auto i:a)\n#define pb push_back\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n#define show2d(v) rep(i,v.size()){rep(j,v[i].size())cout<<\" \"<<v[i][j];cout<<endl;}\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\n#define int ll\ntypedef ll Def;\ntypedef pair<Def,Def> pii;\ntypedef vector<Def> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef vector<string> vs;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef pair<Def,pii> pip;\ntypedef vector<pip>vip;\n#define mt make_tuple\ntypedef tuple<int,int,int,int> tp;\ntypedef vector<tp> vt;\ntemplate<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}\ntemplate<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}\n//template<class C>constexpr int size(const C &c){return (int)c.size();}\n//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}\nconst double PI=acos(-1);\nconst double EPS=1e-9;\nDef inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;\nint dx[]={0,1,0,-1};\nint dy[]={1,0,-1,0};\nsigned main(){\n\tint n,m,co=0;\n\twhile(cin>>n>>m,n){\n\t\tif(co++)cout<<endl;\n\t\tvvi cost(n,vi(n,inf));\n\t\trep(i,n)cost[i][i]=0;\n\t\tvvp G(n);\n\t\trep(i,m){\n\t\t\tint a,b,c;cin>>a>>b>>c;\n\t\t\ta--;b--;\n\t\t\tcost[a][b]=cost[b][a]=c;\n\t\t\tG[a].pb({b,c});\n\t\t\tG[b].pb({a,c});\n\t\t}\n\t\trep(i,n)sort(all(G[i]));\n\t\trep(k,n)rep(i,n)rep(j,n)\n\t\t\tcost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);\n\n\t\tvvi go(n,vi(n,inf));\n\t\trep(i,n)rep(j,n)if(i!=j)rep(k,G[i].size())if(cost[i][j]==G[i][k].second+cost[G[i][k].first][j]){\n\t\t\tgo[i][j]=G[i][k].first;break;\n\t\t}\n\n\t\tint q;\n\t\tcin>>q;\n\t\tvi s(q),t(q),hu(n),ob(q);\n\t\tvs name(q);\n\t\trep(i,q)cin>>s[i]>>t[i]>>ob[i]>>name[i];\n\t\trep(i,q)s[i]--,t[i]--;\n\t\twhile(1){\n\t\t\tint mi=inf,from=-1;\n\t\t\trep(i,q)if(s[i]!=t[i]){\n\t\t\t\tint tim=max(ob[i],hu[s[i]]);\n\t\t\t\tif(mi>tim)mi=tim,from=s[i];\n\t\t\t}\n\t\t\tif(from==-1)break;\n\n\t\t\tint mi_t=inf,to=-1;\n\t\t\trep(i,q)if(s[i]!=t[i]&s[i]==from){\n\t\t\t\tif(mi_t>ob[i])mi_t=ob[i],to=go[s[i]][t[i]];\n\t\t\t\telse if(mi_t==ob[i])to=min(to,go[s[i]][t[i]]);\n\t\t\t}\n\n\t\t\thu[from]=mi+2*cost[from][to];\n\t\t\trep(i,q)if(ob[i]<=mi&&s[i]==from&&go[s[i]][t[i]]==to){\n\t\t\t\tob[i]=mi+cost[from][to];\n\t\t\t\ts[i]=to;\n\t\t\t}\n\t\t}\n\t\tvector<pair<int,string> >out(q);\n\t\trep(i,q)out[i]={ob[i],name[i]};\n\t\tsort(all(out));\n\t\trep(i,q)cout<<out[i].second<<\" \"<<out[i].first<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3228, "score_of_the_acc": -1.0887, "final_rank": 13 }, { "submission_id": "aoj_2010_2452046", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nll INF =(1LL<<31);\n\ntypedef pair<ll,ll> Pii;\ntypedef pair<ll,string> Pis;\ntypedef pair< Pii , Pis > PP;\n\nll N,M,K;\nll G[32][32];\nll g[32][32];\n\nset< PP > u[32][32];\n\nll mem[32][32];\n\nll search(ll from,ll to){\n if(mem[from][to]!=-1)return mem[from][to];\n ll mini=INF, res=-1;\n for(int i=0;i<N;i++){\n if(from==i || g[from][i]==INF)continue;\n if(mini>g[from][i]+G[i][to]){\n mini=g[from][i]+G[i][to];\n res=i;\n }\n }\n assert(res!=-1);\n return mem[from][to]=res;\n}\n\nvoid init(){\n for(int i=0;i<32;i++){\n for(int j=0;j<32;j++){\n g[i][j]=(i==j?0:INF);\n G[i][j]=(i==j?0:INF);\n mem[i][j]=-1;\n u[i][j].clear();\n }\n }\n}\n\nint main(){\n ll cnt=0;\n while(1){\n\n init();\n cin>>N>>M;\n if(N==0&&M==0)break;\n if(cnt)cout<<endl;\n cnt++;\n \n for(int i=0;i<M;i++){\n ll a,b,c;\n cin>>a>>b>>c;\n a--,b--;\n G[a][b]=G[b][a]=c;\n }\n \n for(int i=0;i<N;i++)\n for(int j=0;j<N;j++)\n g[i][j]=G[i][j];\n \n for(int k=0;k<N;k++)\n for(int i=0;i<N;i++)\n for(int j=0;j<N;j++)\n G[i][j]=min(G[i][j],G[i][k]+G[k][j]);\n \n cin>>K;\n\n map<ll, vector<PP> > mp;\n set<ll> st;\n \n for(int i=0;i<K;i++){\n ll from,to,ti;\n string name;\n cin>>from>>to>>ti>>name;\n from--,to--;\n mp[ti].push_back( PP( Pii(from,to) , Pis(0,name)) );\n st.insert(ti);\n }\n\n vector< Pis > ans;\n vector<ll> v(N,0);\n ll last=-1;\n \n st.insert(0);\n while(!st.empty()){\n\n ll p=*st.begin();\n assert( last < p ); last=p;\n \n if(mp.count(p)){\n for(int i=0;i<(int)mp[p].size();i++){\n PP pp=mp[p][i];\n ll from=pp.first.first;\n ll to=pp.first.second;\n string name=pp.second.second;\n ll key=search(from,to);\n PP tmp=PP( Pii(p, key ) , Pis(to,name) );\n u[from][key].insert(tmp);\n }\n // mp[p].clear();\n }\n \n for(int i=0;i<N;i++){\n if(v[i]>p)continue;\n \n PP target=PP( Pii(INF,0) , Pis(0,\"\") );\n \n for(int j=0;j<N;j++){\n if(u[i][j].empty())continue;\n PP pp=*u[i][j].begin();\n target=min(target,pp);\n }\n if(target.first.first==INF)continue;\n\n ll nex=target.first.second;\n \n set< PP > :: iterator it;\n for( it=u[i][nex].begin(); it!= u[i][nex].end() ; it++ ){\n PP pp=*it;\n ll to=pp.second.first;\n string name=pp.second.second;\n\n if(to==nex){\n ans.push_back( Pis( p+g[i][nex] , name ) );\n }else{\n mp[ p+g[i][nex] ].push_back( PP(Pii(nex,to),Pis(0,name)) );\n }\n }// iterator\n \n u[i][nex].clear();\n v[i]=p+g[i][nex]*2;\n st.insert(p+g[i][nex]);\n st.insert(p+g[i][nex]*2);\n }// i\n \n st.erase(p);\n }// while ( st.empty() )\n \n sort(ans.begin(),ans.end());\n for(int i=0;i<(int)ans.size();i++){\n Pis p=ans[i];\n cout<<p.second<<' '<<p.first<<endl;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3508, "score_of_the_acc": -1.4328, "final_rank": 16 }, { "submission_id": "aoj_2010_2412323", "code_snippet": "#include <bits/stdc++.h>\n#define INF 10000000\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,string> P2;\n\nstruct edge{\n\tint t,c;\n\tedge(){}\n\tedge(int tt,int cc){\n\t\tt=tt;\n\t\tc=cc;\n\t}\n};\n\nstruct data{\n\tint f,id;\n\tint ti,gt,next;\n\tdata(){}\n\tdata(int ff,int ii,int tt,int gg,int nn){\n\t\tf=ff;\n\t\tid=ii;\n\t\tti=tt;\n\t\tgt=gg;\n\t\tnext=nn;\n\t}\n\tbool operator<(const data& d)const{\n\t\tif(ti==d.ti){\n\t\t\tif(gt==d.gt)return next>d.next;\n\t\t\treturn gt>d.gt;\n\t\t}\n\t\treturn ti>d.ti;\n\t}\n};\n\nint n,m,l;\nvector<edge> G[1001];\n\nint dist[33];\nint dijk(int f,int t){\n\tfill(dist,dist+n,INF);\n\tpriority_queue<P,vector<P>,greater<P> > que;\n\tque.push(P(0,t));\n\tint res=n;\n\tdist[t]=0;\n\twhile(que.size()){\n\t\tP p=que.top();\n\t\tque.pop();\n\t\tif(dist[p.second]<p.first)continue;\n\t\tfor(int j=0;j<G[p.second].size();j++){\n\t\t\tedge e=G[p.second][j];\n\t\t\tif(e.t==f && dist[e.t]==dist[p.second]+e.c)res=min(res,p.second);\n\t\t\tif(dist[e.t]>dist[p.second]+e.c){\n\t\t\t\tif(e.t==f)res=p.second;\n\t\t\t\tdist[e.t]=dist[p.second]+e.c;\n\t\t\t\tque.push(P(dist[e.t],e.t));\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\n\npriority_queue<data> que[35];\nstring str[1001];\nint lf[1001],lt[1001],ltime[1001];\nint next[35];\nint ntime[35];\npriority_queue<P,vector<P>,greater<P> > backer;\nvector<P2> res;\n\nint main(void){\n\tbool space=false;\n\twhile(1){\n\t\tscanf(\"%d%d\",&n,&m);\n\t\tif(n==0 && m==0)break;\n\t\tif(space)cout << endl;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tG[i].clear();\n\t\t}\n\t\tspace=true;\n\t\tfor(int i=0;i<m;i++){\n\t\t\tint f,t,c;\n\t\t\tscanf(\"%d%d%d\",&f,&t,&c);\n\t\t\tf--;\n\t\t\tt--;\n\t\t\tG[f].push_back(edge(t,c));\n\t\t\tG[t].push_back(edge(f,c));\n\t\t}\n\t\tfor(int i=0;i<n;i++){\n\t\t\twhile(que[i].size())que[i].pop();\n\t\t}\n\t\twhile(backer.size())backer.pop();\n\t\tscanf(\"%d\",&l);\n\t\tfor(int i=0;i<l;i++){\n\t\t\tcin >> lf[i] >> lt[i] >> ltime[i] >> str[i];\n\t\t\tlf[i]--;\n\t\t\tlt[i]--;\n\t\t\tque[lf[i]].push(data(-1,i,ltime[i],ltime[i],dijk(lf[i],lt[i])));\n\t\t}\n\t\tmemset(next,0,sizeof(next));\n\t\tmemset(ntime,0,sizeof(ntime));\n\t\tres.clear();\n\t\twhile(1){\n\t\t\tbool flag=false;\n\t\t\tint best=0,bt=0;\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tif(que[i].size()>0){\n\t\t\t\t\tdata p=que[i].top();\n\t\t\t\t\tif(!flag){\n\t\t\t\t\t\tbest=p.ti;\n\t\t\t\t\t\tbt=i;\n\t\t\t\t\t}\n\t\t\t\t\tflag=true;\n\t\t\t\t\tif(best>p.ti){\n\t\t\t\t\t\tbt=i;\n\t\t\t\t\t\tbest=p.ti;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!flag)break;\n\t\t\tdata v=que[bt].top();\n\t\t\tque[bt].pop();\n\t\t\twhile(backer.size()){\n\t\t\t\tP p=backer.top();\n\t\t\t\tif(p.first<=v.ti){\n\t\t\t\t\tbacker.pop();\n\t\t\t\t\tnext[p.second]=0;\n\t\t\t\t}else break;\n\t\t\t}\n\t\t\tif(bt==lt[v.id]){\n\t\t\t\tres.push_back(P2(v.ti,str[v.id]));\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(next[bt]==-1){\n\t\t\t\tv.ti=ntime[bt];\n\t\t\t\tque[bt].push(v);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint nex=dijk(bt,lt[v.id]);\n\t\t\tint now=v.ti;\n\t\t\tint plu=0;\n\t\t\tfor(int i=0;i<G[bt].size();i++){\n\t\t\t\tif(nex==G[bt][i].t)plu=G[bt][i].c;\n\t\t\t}\n\t\t\tnext[bt]=-1;\n\t\t\tntime[bt]=v.ti+plu*2;\n\t\t\tbacker.push(P(v.ti+plu*2,bt));\n\t\t\tv.ti+=plu;\n\t\t\tque[nex].push(data(bt,v.id,v.ti,v.ti,(nex==lt[v.id])?-1:dijk(nex,lt[v.id])));\n\t\t\tqueue<data> tmp;\n\t\t\twhile(que[bt].size()){\n\t\t\t\tdata pp=que[bt].top();\n\t\t\t\tif(pp.ti>now)break;\n\t\t\t\tque[bt].pop();\n\t\t\t\tif(pp.next==nex){\n\t\t\t\t\tque[nex].push(data(bt,pp.id,v.ti,v.ti,(nex==lt[pp.id])?-1:dijk(nex,lt[pp.id])));\n\t\t\t\t}else{\n\t\t\t\t\ttmp.push(pp);\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile(tmp.size()){\n\t\t\t\tdata pp=tmp.front();\n\t\t\t\tque[bt].push(pp);\n\t\t\t\ttmp.pop();\n\t\t\t}\n\t\t}\n\t\tsort(res.begin(),res.end());\n\t\tfor(int i=0;i<res.size();i++){\n\t\t\tcout << res[i].second << \" \" << res[i].first << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3424, "score_of_the_acc": -1.6546, "final_rank": 19 }, { "submission_id": "aoj_2010_2399833", "code_snippet": "#ifndef _WIN32\n#include<iostream>\n#endif // !_WIN32\n\n#include<algorithm>\n#include<vector>\n#include<string>\n#include<iomanip>\n#include<map>\n#include<stack>\n#include<queue>\n#include<utility>\n#include<assert.h>\nusing namespace std;\ntypedef long long LL;\n#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\ntypedef pair<LL, string> P;\ntypedef vector<int> V;\ntypedef vector<V> VV;\n//input\nint n, m, l;\nint c[32][32];\nstring label[1123];\nint bg_pos[1123];\nint ed_pos[1123];\nLL bg_time[1123];\n\n\nLL d[32][32];\nint nxt[32][32];\n\nconst LL INF = 1e15;\n\nint exist[32];\nvector<VV> post(32,VV(32));\nLL min_time[32][32];\ntypedef pair<LL, int> pli;\ntemplate<typename T>\nbool chmin(T &a, T b) { bool f = a > b; if (f)a = b; return f; }\nmap<LL, vector<pair<int,int>>> sc;\nvoid sync(LL now_time,vector<P>& answer) {\n\tREP(from, n)if(exist[from]) {\n\t\tpli res(INF,-1);\n\t\tREP(j, n)chmin(res, { min_time[from][j],j });\n\t\tint to = res.second;\n\t\tif (to != -1) {\n\t\t\texist[from] = 0;\n\t\t\tLL next_time = now_time + c[from][to];\n\t\t\tV &now_post = post[from][to];\n\t\t\twhile (now_post.size()) {\n\t\t\t\tint now_item = now_post.back();\n\t\t\t\tif (ed_pos[now_item] == to)\n\t\t\t\t\tanswer.push_back({ next_time,label[now_item] });\n\t\t\t\telse {\n\t\t\t\t\tsc[next_time].push_back({ to,now_item });\n\t\t\t\t\t//cout << label[now_item] << \" \" << from << \"->\" << to << endl;\n\t\t\t\t}\n\t\t\t\tnow_post.pop_back();\n\t\t\t}\n\t\t\tmin_time[from][to] = INF;\n\t\t\tnext_time += c[to][from];\n\t\t\tsc[next_time].push_back({ from,-1 });\n\t\t}\n\t}\n}\nLL clean(LL now_time) {\n\tauto it = sc.upper_bound(now_time);\n\tif (it == sc.end())return -1;\n\tnow_time = it->first;\n\twhile (it->second.size()) {\n\t\tint id = it->second.back().second;\n\t\tint pos = it->second.back().first;\n\t\tif (id == -1)exist[pos] = 1;\n\t\telse {\n\t\t\tint to = nxt[pos][ed_pos[id]];\n\t\t\tchmin(min_time[pos][to], now_time);\n\t\t\tpost[pos][to].push_back(id);\n\t\t}\n\t\tit->second.pop_back();\n\t}\n\treturn now_time;\n}\n\nvoid init() {\n\tsc.clear();\n\tREP(i, 32)REP(j, 32)c[i][j] = -1;\n\tREP(i, 32)REP(j, 32) {\n\t\td[i][j] = INF;\n\t\tnxt[i][j] = -1;\n\t}\n\tREP(i, 32) {\n\t\texist[i] = 1;\n\t\tREP(j,32)post[i][j].clear();\n\t\tREP(j,32)min_time[i][j] = INF;\n\t}\n}\n\nvoid set_nxt(){\n\tREP(t, n) {\n\t\tbool update = true;\n\t\td[t][t] = 0;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tREP(i, n)REP(j, n) if(c[i][j]!=-1){\n\t\t\t\tLL nxt_cost = d[i][t] + c[i][j];\n\t\t\t\tif (d[j][t] > nxt_cost) {\n\t\t\t\t\tupdate = true;\n\t\t\t\t\td[j][t] = nxt_cost;\n\t\t\t\t\tnxt[j][t] = i;\n\t\t\t\t}\n\t\t\t\tif (d[j][t] == nxt_cost) {\n\t\t\t\t\tnxt[j][t] = min(nxt[j][t], i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\tint xxx = 0;\n\twhile (cin >> n >> m, n + m) {\n\t\tif (xxx)cout << endl;\n\t\txxx = 1;\n\t\tinit();\n\t\tREP(i, m) {\n\t\t\tint a, b, x;\n\t\t\tcin >> a >> b >> x;\n\t\t\ta--, b--;\n\t\t\tc[a][b] = c[b][a] = x;\n\t\t}\n\t\tvector<P> res;\n\t\tset_nxt();\n\t\tcin >> l;\n\t\tREP(i, l) {\n\t\t\tcin >> bg_pos[i] >> ed_pos[i] >> bg_time[i] >> label[i];\n\t\t\tbg_pos[i]--;\n\t\t\ted_pos[i]--;\n\t\t\tsc[bg_time[i]].push_back({ bg_pos[i],i });\n\t\t}\n\t\tLL nt = -1;\n\t\twhile (true) {\n\t\t\tnt = clean(nt);\n\t\t\tif (nt == -1)break;\n\t\t\tsync(nt, res);\n\t\t}\n\t\tassert(res.size() == l);\n\t\tsort(ALL(res));\n\t\tfor (auto &it : res)cout << it.second << \" \" << it.first << endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3576, "score_of_the_acc": -1.2056, "final_rank": 15 }, { "submission_id": "aoj_2010_1993166", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\nconst int MOD = 1000000007;\nconst int INF = MOD + 1;\nconst ld EPS = 1e-12;\ntemplate<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }\ntemplate<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }\n\n/*--------------------template--------------------*/\n\n\nint n, m, l;\nvector<vi> dist, nx;\ntypedef pair<int, string> Item;\n\nstruct Event\n{\n\tint time, office;\n\tint eve; // 0 -> ??°??£?????????, 1 -> ???????????????????????????\n\tItem item;\n\n\tbool operator < (const Event& e) const \n\t{ \n\t\tif (time != e.time) return time > e.time;\n\t\telse return eve > e.eve;\n\t}\n};\n\nvector<Item> getItem(int office, set<pair<Item, int>> &que)\n{\n\tll time = LLONG_MAX / 100; int to = 10000;\n\tfor (auto i : que)\n\t{\n\t\tif (i.second < time)\n\t\t{\n\t\t\ttime = i.second;\n\t\t\tto = nx[office][i.first.first];\n\t\t}\n\t\telse if (i.second == time)\n\t\t{\n\t\t\tchmin(to, nx[office][i.first.first]);\n\t\t}\n\t}\n\tvector<Item> res;\n\tvector<pair<Item, int>> er;\n\tfor (auto i : que)\n\t{\n\t\tif (nx[office][i.first.first] == to)\n\t\t{\n\t\t\ter.push_back(i);\n\t\t\tres.push_back(i.first);\n\t\t}\n\t}\n\tfor (auto i : er) que.erase(i);\n\treturn res;\n}\n\nint main()\n{\n\tcin.sync_with_stdio(false); cout << fixed << setprecision(10);\n\tbool start = true;\n\twhile (cin >> n >> m, n)\n\t{\n\t\tif (!start) cout << endl;\n\t\tstart = false;\n\t\tdist.clear(), nx.clear();\n\t\tdist.resize(n, vi(n, INF));\n\t\tnx.resize(n, vi(n));\n\t\tREP(i, n)REP(j, n)\n\t\t{\n\t\t\tif (i == j) dist[i][j] = 0;\n\t\t\tnx[i][j] = j;\n\t\t}\n\t\tREP(i, m)\n\t\t{\n\t\t\tint a, b, c; cin >> a >> b >> c;\n\t\t\ta--; b--;\n\t\t\tdist[a][b] = c;\n\t\t\tdist[b][a] = c;\n\t\t}\n\t\tREP(k, n)REP(i, n)REP(j, n)\n\t\t{\n\t\t\tif (dist[i][j] > dist[i][k] + dist[k][j])\n\t\t\t{\n\t\t\t\tdist[i][j] = dist[i][k] + dist[k][j];\n\t\t\t\tnx[i][j] = nx[i][k];\n\t\t\t}\n\t\t\telse if (k != i && dist[i][j] == dist[i][k] + dist[k][j])\n\t\t\t{\n\t\t\t\tnx[i][j] = min(nx[i][j], nx[i][k]);\n\t\t\t}\n\t\t}\n\t\n\t\tpriority_queue<Event> events;\n\t\tcin >> l;\n\t\tREP(i, l)\n\t\t{\n\t\t\tint a, b, t; string s;\n\t\t\tcin >> a >> b >> t >> s;\n\t\t\ta--; b--;\n\t\t\tevents.push(Event{ t,a,1,Item(b, s) });\n\t\t}\n\t\tvector<bool> officer(n, true);\n\t\tvector<set<pair<Item, int>>> que(n); //????????¨??????\n\n\t\tint now = -1;\n\t\tvs reach;\n\t\twhile (1)\n\t\t{\n\t\t\tif (events.empty())\n\t\t\t{\n\t\t\t\tbool f = true;\n\t\t\t\tREP(i, que.size()) if (!que.empty()) f = false;\n\t\t\t\tif(f) break;\n\t\t\t}\n\t\t\tif (events.empty() || events.top().time != now)\n\t\t\t{\n\t\t\t\tbool f = true;\n\t\t\t\tsort(ALL(reach));\n\t\t\t\tfor(auto i: reach)\n\t\t\t\t{ \n\t\t\t\t\tcout << i << \" \" << now << endl;\n\t\t\t\t}\n\t\t\t\treach.clear();\n\t\t\t\tREP(i, n)\n\t\t\t\t{\n\t\t\t\t\tif (!officer[i] || que[i].empty()) continue;\n\t\t\t\t\tf = false;\n\t\t\t\t\tvector<Item> send = getItem(i, que[i]);\n\t\t\t\t\tint next_office = nx[i][get<0>(send[0])];\n\t\t\t\t\tint d = dist[i][next_office];\n\t\t\t\t\tofficer[i] = false;\n\t\t\t\t\tevents.push(Event{ now + d * 2 ,i, 0 });\n\t\t\t\t\tfor (auto item : send)\n\t\t\t\t\t{\n\t\t\t\t\t\tevents.push(Event{ now + d, next_office, 1, item });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (f)\n\t\t\t\t{\n\t\t\t\t\tif (events.empty()) break;\n\t\t\t\t\telse now = events.top().time;\n\t\t\t\t}\n\t\t\t\telse continue;\n\t\t\t}\n\n\t\t\tEvent tmp = events.top();\n\t\t\tevents.pop();\n\t\t\tif (tmp.eve == 0)\n\t\t\t{\n\t\t\t\tofficer[tmp.office] = true;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (tmp.item.first == tmp.office)\n\t\t\t\t{\n\t\t\t\t\treach.push_back(tmp.item.second);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tque[tmp.office].emplace(tmp.item, tmp.time);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3408, "score_of_the_acc": -0.8992, "final_rank": 12 }, { "submission_id": "aoj_2010_1988814", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nstruct root {\n\tint start;\n\tint goal;\n\tint time;\n\tvector<int>ways;\n\troot():ways() {\n\t\tstart = 0;\n\t\tgoal = 0;\n\t}\n\troot(const int start_, const int goal_, const int time_, const vector<int>ways_)\n\t\t:start(start_), goal(goal_), time(time_), ways(ways_) {\n\n\t}\n};\nstruct edge {\n\tint src;\n\tint dst;\n\tint cost;\n};\n\nstruct query {\n\tint type;\n\tint id;\n\tint num;\n\tint next;\n\tint nextnext;\n\tint time;\n\tquery(int type_, int id_, int num_, const int next_,const int nextnext_, int time_) :type(type_), id(id_), num(num_), next(next_),nextnext(nextnext_), time(time_) {\n\n\t}\n};\nclass Compare {\npublic:\n\tbool operator()(const query&l, const query&r) {\n\t\treturn l.time==r.time?l.type==r.type?l.nextnext>r.nextnext:l.type<r.type:l.time> r.time;\n\t}\n};\t//aa?????????????????¶\nstruct letter {\n\tint id;\n\tstring name;\n\tvector<int>ways;\n};\nint main() {\n\tint aa = 0;\n\twhile (1) {\n\t\tint N, M; cin >> N >> M;\n\t\tif (!N)break;\n\t\tif(aa)\tcout << endl;\n\n\t\taa++;\n\t\t\n\t\tvector<vector<root>>roots(N, vector<root>(N));\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\t\n\t\t\t\troots[i][j].start = i;\n\t\t\t\troots[i][j].goal = j;\n\t\t\t\tif (i == j) {\n\t\t\t\t\troots[i][j].time = 0;\n\t\t\t\t\troots[i][j].ways = vector<int>{};\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\troots[i][j].time = 1e9;\n\t\t\t\t}\n\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tint a, b, c; cin >> a >> b >> c; a--; b--;\n\t\t\troots[a][b] = root( a,b,c,vector<int>{b} );\n\t\t\troots[b][a] = root(b, a, c, vector<int>{a});\n\t\t}\n\t\tfor (int k = 0; k < N; ++k) {\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\t\tif (i == k || j == k||i==j)continue;\n\t\t\t\t\tif (roots[i][k].time < 1e8&&roots[k][j].time < 1e8) {\n\t\t\t\t\t\tconst int nexttime = roots[i][k].time + roots[k][j].time;\n\t\t\t\t\t\tbool ok = true;\n\t\t\t\t\t\tvector<int>preways(roots[i][j].ways);\n\t\t\t\t\t\tvector<int>nextways(roots[i][k].ways);\n\t\t\t\t\t\tnextways.insert(nextways.end(), roots[k][j].ways.begin(), roots[k][j].ways.end());\n\t\t\t\t\t\tif (nexttime > roots[i][j].time)ok = false;\n\t\t\t\t\t\telse if (nexttime == roots[i][j].time) {\n\n\t\t\t\t\t\t\tfor (int a= 0; a < preways.size(); ++a) {\n\t\t\t\t\t\t\t\tif (preways[a] > nextways[a]) {\n\t\t\t\t\t\t\t\t\tok = true;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse if (preways[a] < nextways[a]) {\n\t\t\t\t\t\t\t\t\tok = false;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tok = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (ok) {\n\t\t\t\t\t\t\troots[i][j].time = nexttime;\n\t\t\t\t\t\t\troots[i][j].ways = nextways;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tif (roots[i][j].time < 1e8) {\n\t\t\t\t\troots[i][j].ways.insert(roots[i][j].ways.begin(), i);\n\t\t\t\t\troots[i][j].ways.emplace_back(-1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\tint L; cin >> L;\n\t\tpriority_queue<query, vector<query>, Compare>que;\n\t\tmap<int,letter>mp;\n\t\tfor (int i = 0; i < L; ++i) {\n\t\t\tint a, b, c; string d; cin >> a >> b >> c >> d; a--; b--;\n\t\t\tmp[i] = letter{ i,d,roots[a][b].ways };\n\t\t\tint nextnext = roots[a][b].ways[1];\n\t\t\tque.push(query(1,i,0,roots[a][b].ways[0],nextnext,c ));\n\t\t}\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tque.push(query{ 0,0,0,i,0,0 });\n\t\t}\n\t\tvector<pair< int,string>>anss;\n\t\t//vector<vector<vector<int>>>waits(N,vector<vector<int>>(N,vector<int>()));\n\t\tvector<deque<vector<tuple<int,int,int>>>>nexts(N);\n\t\tvector<int>exists(N);\n\t\t\n\t\twhile (!que.empty()) {\n\t\t\tquery q(que.top());\n\t\t\tque.pop();\n\n\t\t\tconst int nowtime = q.time;\n\n\t\t\tconst int nowplace = q.next;\n\t\t\t//??°??£?????????\n\t\t\tif (q.type == 0) {\n\t\t\t\tassert(exists[nowplace] == false);\n\t\t\t\tif (!nexts[nowplace].empty()) {\n\t\t\t\t\tauto carrys(nexts[nowplace].front());\n\t\t\t\t\tconst int nextplace = get<2>(carrys[0]);\n\t\t\t\t\tfor (auto carry : carrys) {\n\t\t\t\t\t\tint aid = get<0>(carry);\n\t\t\t\t\t\tint anum = get<1>(carry);\n\t\t\t\t\t\tconst int needtime = roots[nowplace][nextplace].time;\n\t\t\t\t\t\tconst int nexttime = nowtime + needtime;\n\t\t\t\t\t\tconst int backtime = nexttime + needtime;\n\t\t\t\t\t\tque.push(query(1,aid,anum + 1,nextplace,mp[aid].ways[anum+2],nexttime ));\n\t\t\t\t\t\texists[nowplace] = false;\n\t\t\t\t\t}\n\t\t\t\t\tque.push(query(0, 0, 0, nowplace, 0,nowtime+2*roots[nowplace][nextplace].time));\n\t\t\t\t\tnexts[nowplace].pop_front();\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tassert(!exists[nowplace]);\n\t\t\t\t\texists[nowplace] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\t//???????????\\???\n\t\t\telse {\n\t\t\t\tif (q.nextnext==-1) {\n\t\t\t\t\tanss.push_back(make_pair(q.time, mp[q.id].name));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tconst int nextplace = mp[q.id].ways[q.num + 1];\n\t\t\t\t\tif (!exists[nowplace]) {\n\t\t\t\t\t\tbool flag = true;\n\t\t\t\t\t\tfor (int i = 0; i < nexts[nowplace].size(); ++i) {\n\t\t\t\t\t\t\tif (get<2>(nexts[nowplace][i][0]) == nextplace) {\n\t\t\t\t\t\t\t\tnexts[nowplace][i].push_back(make_tuple(q.id, q.num, nextplace));\n\t\t\t\t\t\t\t\tflag = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (flag) {\n\t\t\t\t\t\t\tnexts[nowplace].push_back(vector<tuple<int,int,int>>(1, make_tuple(q.id, q.num, nextplace)));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tbool flag = true;\n\t\t\t\t\t\tfor (int i = 0; i < nexts[nowplace].size(); ++i) {\n\t\t\t\t\t\t\tif (get<2>(nexts[nowplace][i][0]) == nextplace) {\n\t\t\t\t\t\t\t\tnexts[nowplace][i].push_back(make_tuple(q.id, q.num, nextplace));\n\t\t\t\t\t\t\t\tflag = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (flag) {\n\t\t\t\t\t\t\tnexts[nowplace].push_back(vector<tuple<int, int, int>>(1, make_tuple(q.id, q.num, nextplace)));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tque.push(query(0, 0, 0, nowplace, 0, nowtime));\n\t\t\t\t\t\texists[nowplace] = false;\n\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\tsort(anss.begin(), anss.end());\n\t\tfor (auto ans : anss) {\n\t\t\tcout << ans.second << \" \" << ans.first << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3452, "score_of_the_acc": -1.664, "final_rank": 20 }, { "submission_id": "aoj_2010_1347324", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 32;\n\nconst int INF = 1 << 30;\n\nconst string evnms[] = { \"E_LT_ARV\", \"E_PM_BK\", \"E_PM_DL\", \"E_PM_ST\" };\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef pair<int,string> pis;\ntypedef vector<int> vi;\ntypedef vector<pis> vpis;\ntypedef queue<pii> qpii;\n\nstruct Letter {\n int t, nexthop, dst;\n string label;\n Letter() {}\n Letter(int _t, int _nexthop, int _dst, string _label) {\n t = _t, nexthop = _nexthop, dst = _dst, label = _label;\n }\n\n bool operator<(const Letter& lt) const {\n return t < lt.t || (t == lt.t && nexthop < lt.nexthop);\n }\n\n void print() {\n printf(\" Letter: t=%d, nexthop=%d, dst=%d, labe=%s\\n\",\n\t t, nexthop, dst, label.c_str());\n }\n};\n\ntypedef deque<Letter> dql;\n\nstruct PostMan {\n int id, st;\n dql mls;\n\n void print() {\n printf(\" PostMan: id=%d, st=%d, mls=%lu\\n\", id, st, mls.size());\n for (int i = 0; i < mls.size(); i++) mls[i].print();\n }\n};\n\nenum { M_WT, M_ON };\nenum { E_LT_ARV, E_PM_BK, E_PM_DL, E_PM_ST };\n\nstruct Event {\n int t, st;\n int p0, p1;\n string label;\n PostMan *pm;\n\n Event() {}\n Event(int _t, int _st, int _p0, int _p1, string _label) {\n t = _t, st = _st, p0 = _p0, p1 = _p1;\n label = _label;\n pm = NULL;\n }\n Event(int _t, int _st, int _p0, int _p1, PostMan *_pm) {\n t = _t, st = _st, p0 = _p0, p1 = _p1;\n label = \"\";\n pm= _pm;\n }\n \n bool operator>(const Event& ev) const {\n return t > ev.t || (t == ev.t && st > ev.st);\n }\n\n void print() {\n if (st == E_LT_ARV) {\n printf(\"Event: t=%d, st=%s, %d->%d, label=%s\\n\",\n\t t, evnms[st].c_str(), p0, p1, label.c_str());\n }\n else {\n printf(\"Event: t=%d, st=%s, %d->%d, pm=%d\\n\",\n\t t, evnms[st].c_str(), p0, p1, pm->id);\n pm->print();\n }\n }\n};\n\n/* global variables */\n\nint n, m, l;\nvi nbrs[MAX_N];\nint costs[MAX_N][MAX_N];\nint dists[MAX_N], nhops[MAX_N];\nint rt[MAX_N][MAX_N];\n\ndql posts[MAX_N];\nPostMan pmen[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (bool first = true;; first = false) {\n cin >> n >> m;\n if (n == 0) break;\n \n if (! first) cout << endl;\n\n for (int i = 0; i < n; i++) nbrs[i].clear();\n \n for (int i = 0; i < m; i++) {\n int a, b, c;\n cin >> a >> b >> c;\n a--, b--;\n nbrs[a].push_back(b);\n nbrs[b].push_back(a);\n costs[a][b] = costs[b][a] = c;\n }\n\n for (int i = 0; i < n; i++) {\n sort(nbrs[i].begin(), nbrs[i].end());\n for (int j = 0; j < n; j++) rt[i][j] = -1;\n }\n\n for (int st = 0; st < n; st++) {\n for (int i = 0; i < n; i++) dists[i] = INF;\n dists[st] = 0;\n nhops[st] = -1;\n\n qpii q;\n q.push(pii(0, st));\n\n while (! q.empty()) {\n\tpii u = q.front();\n\tq.pop();\n\n\tint ud = u.first;\n\tint ui = u.second;\n\tif (ud != dists[ui]) continue;\n\n\tint unhop = nhops[ui];\n\tvi& nbru = nbrs[ui];\n\n\tfor (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n\t int vi = *vit;\n\t int vd = ud + costs[ui][vi];\n\t if (dists[vi] > vd) {\n\t dists[vi] = vd;\n\t nhops[vi] = (unhop >= 0) ? unhop : vi;\n\t q.push(pii(vd, vi));\n\t }\n\t else if (dists[vi] == vd && nhops[vi] > unhop) {\n\t dists[vi] = vd;\n\t nhops[vi] = unhop;\n\t q.push(pii(vd, vi));\n\t }\n\t}\n }\n\n for (int gl = 0; gl < n; gl++) rt[st][gl] = nhops[gl];\n }\n\n if (false) {\n for (int i = 0; i < n; i++) {\n\tfor (int j = 0; j < n; j++) cout << rt[i][j] << ' ';\n\tcout << endl;\n }\n }\n\n for (int i = 0; i < n; i++) {\n posts[i].clear();\n pmen[i].id = i, pmen[i].st = M_WT;\n pmen[i].mls.clear();\n }\n\n priority_queue<pis,vector<pis>,greater<pis> > arrived;\n priority_queue<Event,vector<Event>,greater<Event> > pqe;\n\n cin >> l;\n \n for (int i = 0; i < l; i++) {\n int p0, p1, t;\n string label;\n cin >> p0 >> p1 >> t >> label;\n p0--, p1--;\n \n pqe.push(Event(t, E_LT_ARV, p0, p1, label));\n }\n\n while (! pqe.empty()) {\n Event ev = pqe.top();\n pqe.pop();\n //ev.print();\n \n if (ev.st == E_LT_ARV) {\n\tint nexthop = rt[ev.p0][ev.p1];\n\tposts[ev.p0].push_back(Letter(ev.t, nexthop, ev.p1, ev.label));\n\n\tPostMan *pm = &pmen[ev.p0];\n\tif (pm->st == M_WT) {\n\t pm->st = M_ON;\n\t pqe.push(Event(ev.t, E_PM_ST, ev.p0, ev.p0, pm));\n\t}\n }\n\n else if (ev.st == E_PM_ST) {\n\tdql& post = posts[ev.p0];\n\tsort(post.begin(), post.end());\n\n\tLetter lt0 = post.front();\n\tpost.pop_front();\n\tPostMan *pm = ev.pm;\n\tpm->mls.push_back(lt0);\n\n\tfor (dql::iterator lit = post.begin(); lit != post.end();) {\n\t if (lit->nexthop == lt0.nexthop) {\n\t pm->mls.push_back(*lit);\n\t lit = post.erase(lit);\n\t }\n\t else lit++;\n\t}\n\n\tpqe.push(Event(ev.t + costs[ev.p0][lt0.nexthop],\n\t\t E_PM_DL, ev.p0, lt0.nexthop, pm));\n }\n\n else if (ev.st == E_PM_DL) {\n\tPostMan *pm = ev.pm;\n\tdql& mls = pm->mls;\n\t\n\tfor (dql::iterator lit = mls.begin(); lit != mls.end(); lit++) {\n\t lit->nexthop = rt[ev.p1][lit->dst];\n\t if (lit->nexthop == -1)\n\t arrived.push(pis(ev.t, lit->label));\n\t else\n\t pqe.push(Event(ev.t, E_LT_ARV, ev.p1, lit->dst, lit->label));\n\t}\n\n\tpm->mls.clear();\n\n\tpqe.push(Event(ev.t + costs[ev.p1][ev.p0],\n\t\t E_PM_BK, ev.p1, ev.p0, pm));\n }\n\n else if (ev.st == E_PM_BK) {\n\tPostMan *pm = ev.pm;\n\tif (posts[pm->id].empty())\n\t pm->st = M_WT;\n\telse {\n\t pqe.push(Event(ev.t, E_PM_ST, ev.p1, ev.p1, pm));\n\t}\n }\n }\n\n //cout << arrived.size() << endl;\n\n while (! arrived.empty()) {\n pis lt = arrived.top();\n arrived.pop();\n\n cout << lt.second << ' ' << lt.first << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1476, "score_of_the_acc": -0.75, "final_rank": 8 } ]
aoj_2011_cpp
Problem F: Gather the Maps! はるか昔、八尾氏が残したとされる伝説の秘宝が八王子のどこかに眠っているという。 その在処を示すとされる宝の地図は、いくつかの断片に分割された状態で、八尾氏の n 人の子孫達によって受け継がれている。 今、八尾氏の子孫達は協力してその秘宝を手に入れようとしていた。 ところが、秘宝の在処を指し示す宝の地図の一部分だけでは秘宝を見つけることができない。 そこで、八尾氏の子孫達は全員で集まって地図を 1 ヶ所に集めようとした。 ところが、いざ実行に移そうとしてもなかなか予定が合わずに集まることができない。 しかしこの秘宝に関する情報は、一族において秘密裏に伝えられてきた貴重な情報である。 漏洩の危険性を考慮すると、公共の通信手段を用いて地図をやりとりすることなど問題外である。 そこで、子孫同士が直接会って地図を手渡すということを繰り返すことで、ある 1 人の子孫のところに地図を集めることにした。 なお、1 人が 1 日に会える人数に制限はないが、互いにスケジュールが空いていることが必要である。 あなたの仕事は、それぞれの子孫に対するスケジュールの空いている日のリストから、地図を集めるには最低で何日必要かを求めるプログラムを書くことである。 ちなみに、八尾氏一族の結束は非常に固い。 最終的に地図全体を手にした子孫が、他の子孫を裏切って秘宝を持ち逃げすれば、一族から制裁を受けることになる。その制裁はきわめて恐ろしいものであるため、実際にその子孫が秘宝を持ち逃げすることは事実上不可能である。 Input 入力は複数のデータセットからなる。 それぞれのデータセットは複数の行からなる。 その最初の行には、地図の断片を持った者の人数を表す整数 n (1 < n <= 50) が記述されている。 続く n 行には、それぞれの子孫のスケジュールが書かれている。 i 行目は i 人目の子孫のスケジュールが表しており、いくつかの整数が 1 文字のスペースを区切りとして書かれている。 最初の整数 f i (0 <= f i <= 30) は、その子孫のスケジュールが空いている日の日数を表す整数である。 続く f i 個の整数は、スケジュールが空いている日付を表す。 これらの日付は互いに異なり、全て 1 以上 30 以下である。 入力の最後に 0 のみを含んだ 1 行がある。 Output 各データセットに対して、1 つの整数を 1 行に出力せよ。 もし、30 日以内に地図を集めることができる場合は、地図を集めるのに最低限必要となる日数を、集めることができない場合は -1 を出力せよ。 追記 : 上記の「地図を集めるのに最低限必要となる日数」は 1 日を起点として最も早く全ての地図が集まる日付を意味する. Sample Input 4 1 1 2 2 3 2 1 2 3 3 4 5 0 Output for the Sample Input 3
[ { "submission_id": "aoj_2011_10853823", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main()\n{\n\tint n;\n\twhile (cin >> n, n) {\n\t\tvector<vector<int>> v(31);\n\t\tfor (int i = 0, f; i < n; i++) {\n\t\t\tcin >> f;\n\t\t\tfor (int j = 0, d; j < f; j++) {\n\t\t\t\tcin >> d;\n\t\t\t\tv[d - 1].push_back(i);\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>> dp(n, vector<int>(n));\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tdp[i][i] = 1;\n\t\t}\n\t\tint res = -1;\n\t\tfor (int i = 0; i <= 30; i++) {\n\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\tbool flag = true;\n\t\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\t\tif (dp[k][j] == 0) {\n\t\t\t\t\t\tflag = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (flag) {\n\t\t\t\t\tres = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (res != -1) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (v[i].size() > 1) {\n\t\t\t\tvector<int> su(n);\n\t\t\t\tfor (auto m : v[i]) {\n\t\t\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\t\t\tsu[j] = max(su[j], dp[m][j]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : v[i]) {\n\t\t\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\t\t\tdp[m][j] = max(dp[m][j], su[j]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << res << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3192, "score_of_the_acc": -0.2151, "final_rank": 3 }, { "submission_id": "aoj_2011_10610398", "code_snippet": "#line 1 \"2005JAGF.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2005JAGF.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(x)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nll pc(ll val) {\n ll res = 0;\n\n rep(i,60) {\n if(((val>>i)&1)==1) res++;\n }\n\n return res;\n}\n\nint solve(){\n int n; cin >> n;\n if(n==0) return 1;\n vector<vector<bool>> has_time(n,vector<bool>(31, false));\n rep(i,n) {\n int k;\n cin >> k;\n rep(_,k) {\n int t; cin >> t;\n t--;\n has_time[i][t] = true;\n }\n }\n\n vll dp(n,0);\n rep(i,n) dp[i] = 1ll << i;\n\n rep(i,31) {\n ll nxt = 0;\n rep(j,n) {\n if(has_time[j][i]) nxt = nxt | dp[j];\n }\n\n if(pc(nxt) == n) {\n cout << i+1 << '\\n';\n return 0;\n }\n\n vll ndp = dp;\n rep(j,n) if(has_time[j][i]) ndp[j] = nxt;\n\n dp = ndp;\n dbg(dp);\n\n }\n cout << \"-1\\n\";\n return 0;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3396, "score_of_the_acc": -0.6148, "final_rank": 7 }, { "submission_id": "aoj_2011_10562370", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve();\nint main() {\n while (solve());\n return 0;\n}\n\nusing ll = long long;\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<int> F(N);\n for (int i = 0; i < N; i++) {\n int ff;\n cin >> ff;\n for (int j = 0; j < ff; j++) {\n int f;\n cin >> f;\n f--;\n F[i] |= 1 << f;\n }\n }\n vector<ll> dp(N);\n // dp=各要素が獲得できる地図集合\n for (ll i = 0; i < N; i++) dp[i] |= 1LL << i;\n for (int d = 0; d < 30; d++) {\n ll x = 0;\n for (int i = 0; i < N; i++) {\n if (F[i] >> d & 1) {\n x |= dp[i];\n }\n }\n if (popcount((unsigned long long)x) == N) {\n cout << d + 1 << endl;\n return true;\n }\n for (int i = 0; i < N; i++) {\n if (F[i] >> d & 1) dp[i] = x;\n }\n }\n cout << -1 << endl;\n return true;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_2011_10562364", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve();\nint main() {\n while (solve());\n return 0;\n}\n\nstruct UF {\n vector<int> p, s;\n UF(int n) : p(n, 0), s(n, 1) { iota(p.begin(), p.end(), 0); }\n int leader(int u) {\n if (p[u] == u) return u;\n return p[u] = leader(p[u]);\n }\n void merge(int u, int v) {\n u = leader(u), v = leader(v);\n if (u == v) return;\n int ns = s[u] + s[v];\n p[u] = v;\n s[v] = ns;\n }\n int size(int a) { return s[leader(a)]; }\n};\n\nusing ll = long long;\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<int> F(N);\n for (int i = 0; i < N; i++) {\n int ff;\n cin >> ff;\n for (int j = 0; j < ff; j++) {\n int f;\n cin >> f;\n f--;\n F[i] |= 1 << f;\n }\n }\n vector<ll> dp(N);\n // dp=各要素が獲得できる地図集合\n for (ll i = 0; i < N; i++) dp[i] |= 1LL << i;\n for (int d = 0; d < 30; d++) {\n ll x = 0;\n for (int i = 0; i < N; i++) {\n if (F[i] >> d & 1) {\n x |= dp[i];\n }\n }\n if (popcount((unsigned long long)x) == N) {\n cout << d + 1 << endl;\n return true;\n }\n for (int i = 0; i < N; i++) {\n if (F[i] >> d & 1) dp[i] = x;\n }\n }\n cout << -1 << endl;\n return true;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_2011_9626468", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef pair<ll,ll> pi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define F first\n#define S second\n#define sz(A) ((ll)(A.size()))\nstruct dsu{\n vector<int>nec;\n dsu(int N):nec(N,-1){}\n int leader(int v){while(nec[v]>=0)v=nec[v];return v;}\n int merge(int u,int v){\n assert(0<=u&&u<nec.size());\n assert(0<=v&&v<nec.size());\n //cout<<u<<\" \"<<v<<endl;\n u=leader(u);\n v=leader(v);\n if(u==v)return u;\n if(nec[u]<nec[v])swap(u,v);\n nec[v]+=nec[u];\n nec[u]=v;\n return v;\n }\n bool same(int u,int v){return leader(u)==leader(v);}\n int size(int v){return -nec[leader(v)];}\n};\nint main(){\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n vvi E(31);\n REP(i,N){\n ll f;cin>>f;\n while(f--){ll t;cin>>t;E[t].emplace_back(i);}\n }\n REP(i,31){\n bool ok=0;\n REP(v,N){\n dsu D(N);\n RREP(j,i+1){\n bool ex=0;\n REP(k,E[j].size())if(D.same(E[j][k],v))ex=1;\n if(ex)REP(k,E[j].size()-1)D.merge(E[j][k+1],E[j][k]);\n }\n if(D.size(0)==N)ok=1;\n }\n if(ok){cout<<i<<endl;break;}\n if(i==30)cout<<-1<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3456, "score_of_the_acc": -0.912, "final_rank": 11 }, { "submission_id": "aoj_2011_9565895", "code_snippet": "#include <bits/stdc++.h>\n using namespace std;\n #include <ext/pb_ds/assoc_container.hpp>\n using namespace __gnu_pbds;\n \n template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\n template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n #define rep(i,n) for (int i = 0; i < (n); ++i)\n\n typedef long long ll;\n typedef unsigned long long ull;\n typedef long double ld;\n using P=pair<ll,ll>;\n using T=tuple<int,int,int>;\n const ll INF=1001001001; \n const ll INFL=1e18;\n const ll mod=998244353;\n\n struct Unionfind{\n vector<int>d;\n Unionfind(int n):d(n,-1){}\n int root(int x){\n if(d[x]<0){return x;}\n return d[x]=root(d[x]);\n }\n bool unite(int x,int y){\n x=root(x);y=root(y);\n if(x==y){return false;}\n if(d[x]>d[y]){swap(x,y);}\n d[x]+=d[y];\n d[y]=x;\n return true;\n }\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -d[root(x)];}\n };\n\n void solve(int n){\n vector<ll>ok(35);\n rep(i,n){\n int d;\n cin>>d;\n rep(j,d){\n int x;\n cin>>x;\n ok[x]|=1ll<<i;\n }\n }\n vector<ll>can(n);\n rep(i,n)can[i]|=1ll<<i;\n for(int d=1;d<=30;d++){\n rep(i,n){\n rep(j,i){\n if(ok[d]>>i&1 && ok[d]>>j&1){\n ll I=can[i],J=can[j];\n can[j]|=I;\n can[i]|=J;\n }\n }\n }\n rep(i,n){\n if(can[i]==(1ll<<n)-1){\n cout<<d<<endl;\n return;\n }\n }\n }\n cout<<-1<<endl;\n }\n\n int main(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n int n;\n while(1){\n cin>>n;\n if(n==0)break;\n solve(n);\n }\n \n return 0;\n }", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_2011_9565820", "code_snippet": "#include <bits/stdc++.h>\n using namespace std;\n #include <ext/pb_ds/assoc_container.hpp>\n using namespace __gnu_pbds;\n \n template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\n template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n #define rep(i,n) for (int i = 0; i < (n); ++i)\n\n typedef long long ll;\n typedef unsigned long long ull;\n typedef long double ld;\n using P=pair<ll,ll>;\n using T=tuple<int,int,int>;\n const ll INF=1001001001; \n const ll INFL=1e18;\n const ll mod=998244353;\n\n struct Unionfind{\n vector<int>d;\n Unionfind(int n):d(n,-1){}\n int root(int x){\n if(d[x]<0){return x;}\n return d[x]=root(d[x]);\n }\n bool unite(int x,int y){\n x=root(x);y=root(y);\n if(x==y){return false;}\n if(d[x]>d[y]){swap(x,y);}\n d[x]+=d[y];\n d[y]=x;\n return true;\n }\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -d[root(x)];}\n };\n\n void solve(int n){\n vector<vector<int>>ok(35);\n rep(i,n){\n int d;\n cin>>d;\n rep(j,d){\n int x;\n cin>>x;\n ok[x].push_back(i);\n }\n }\n vector<int>mx(n);\n rep(i,n){\n vector<int>dist(n,INF);\n dist[i]=0;\n for(int d=1;d<=30;d++){\n int mn=INF;\n for(int j:ok[d]){\n chmin(mn,dist[j]);\n }\n if(mn<=d){\n for(int j:ok[d]){\n chmin(dist[j],d);\n }\n }\n \n }\n rep(j,n){\n chmax(mx[j],dist[j]);\n }\n }\n int ans=INF;\n rep(i,n){\n chmin(ans,mx[i]);\n }\n if(ans==INF)ans=-1;\n cout<<ans<<endl;\n }\n\n int main(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n int n;\n while(1){\n cin>>n;\n if(n==0)break;\n solve(n);\n }\n \n return 0;\n }", "accuracy": 1, "time_ms": 20, "memory_kb": 3584, "score_of_the_acc": -1.0092, "final_rank": 15 }, { "submission_id": "aoj_2011_9481964", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<vector<int>> V(30);\n rep(i,0,N) {\n int K;\n cin >> K;\n rep(j,0,K) {\n int A;\n cin >> A;\n A--;\n V[A].push_back(i);\n }\n }\n if (N == 1) {\n cout << 0 << endl;\n continue;\n }\n vector<ll> DP(N);\n rep(i,0,N) DP[i] = 1LL << i;\n ll MAX = (1LL << N) - 1;\n bool ans = false;\n rep(i,0,30) {\n ll COUNT = 0;\n for (int A : V[i]) COUNT |= DP[A];\n for (int A : V[i]) DP[A] = COUNT;\n rep(j,0,N) {\n if (DP[j] == MAX) {\n cout << i+1 << endl;\n ans = true;\n break;\n }\n }\n if (ans) break;\n }\n if (!ans) cout << -1 << endl;\n\n}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3252, "score_of_the_acc": -0.3288, "final_rank": 4 }, { "submission_id": "aoj_2011_9403167", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef LOCAL\n#include \"./debug.hpp\"\n#else\n#define debug(...)\n#define print_line\n#endif\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<ll>;\nusing pi = pair<int,int>;\nusing pl = pair<ll,ll>;\nconst int INF = 1e9 + 10;\nconst ll INFL = 4e18;\n#define rep(i, a, b) for (ll i = (a); i < (ll)(b); i++)\n#define all(x) x.begin(),x.end()\n\ntemplate <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }\ntemplate <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }\n\n// --------------------------------------------------------\n\nconst int MX=31;\n\nint main(){\n\twhile(true){\n\t\tint N;cin>>N;\n\t\tif(N==0)break;\n\t\tvector<vector<int>>D(MX);\n\t\tfor(int i=0;i<N;i++){\n\t\t\tint m;cin>>m;\n\t\t\tfor(int j=0;j<m;j++){\n\t\t\t\tint d;cin>>d;\n\t\t\t\tD[d].push_back(i);\n\t\t\t}\n\t\t}\n\n\t\tint ans=INF;\n\t\tvector<set<int>>st(N);\n\t\tfor(int i=0;i<N;i++)st[i].insert(i);\n\t\tfor(int i=0;i<MX;i++){\n\t\t\tfor(int j=0;j<(int)D[i].size();j++){\n\t\t\t\tfor(int k=j+1;k<(int)D[i].size();k++){\n\t\t\t\t\tfor(int x:st[D[i][j]])st[D[i][k]].insert(x);\n\t\t\t\t\tfor(int x:st[D[i][k]])st[D[i][j]].insert(x);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tdebug(st[j]);\n\t\t\t\tif(st[j].size()==N)ans=i;\n\t\t\t}\n\t\t\tif(ans!=INF)break;\n\t\t}\n\n\t\tif(ans==INF)ans=-1;\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 3508, "score_of_the_acc": -1.8443, "final_rank": 20 }, { "submission_id": "aoj_2011_9402807", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,k,n) for (int i = k; i < (n); ++i)\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\nconst long long INF = 1e17;\n\nint main(){\n while(true){\n int n;\n cin >> n;\n if(n==0){\n break;\n }\n\n //dp[i] = \n //人iに最適に地図を集めたときの断片の集合\n vector<vector<int>> dp(n+10);\n\n for(int i=1;i<=n;i++){\n dp[i].push_back(i);\n }\n\n vector<vector<int>> free(n+10);\n vector<vector<int>> date(40);\n for(int i=1;i<=n;i++){\n int f;\n cin >> f;\n for(int j=0;j<f;j++){\n int x;\n cin >> x;\n free[i].push_back(x);\n date[x].push_back(i);\n }\n }\n\n int ans = -1;\n for(int i=1;i<=30;i++){\n //j->kにあげる\n for(int j=0;j<date[i].size();j++){\n for(int k=0;k<date[i].size();k++){\n if(j==k)continue;\n //dp[from]に含まれて、\n //dp[to]に含まれない要素を\n //dp[to]に追加 \n for(auto x:dp[date[i][j]]){\n bool have = false;\n for(auto y:dp[date[i][k]]){\n if(x==y)have = true;\n }\n if(!have)dp[date[i][k]].push_back(x);\n }\n }\n }\n\n bool ok = false;\n for(int j=1;j<=n;j++){\n if(dp[j].size()==n){\n ans = i;\n ok = true;\n break;\n }\n }\n if(ok){\n break;\n }\n }\n\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 3432, "score_of_the_acc": -1.6243, "final_rank": 18 }, { "submission_id": "aoj_2011_9381240", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main() {\n while (1) {\n int n;\n cin >> n;\n if (n == 0) {\n break;\n }\n vector<int> f(n);\n vector<vector<int>> e(n);\n for (int i = 0; i < n; i++) {\n cin >> f[i];\n e[i].resize(f[i]);\n for (int j = 0; j < f[i]; j++) {\n cin >> e[i][j];\n e[i][j]--;\n }\n }\n vector<vector<int>> S(31);\n //S[i] はi(0-idx)日目の集まれる人の集合\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < f[i]; j++) {\n S[e[i][j]].push_back(i);\n }\n }\n vector dp(31, vector(n, vector<int> (n, 0)));\n for (int i = 0; i < n; i++) {\n dp[0][i][i] = 1;\n }\n for (int i = 0; i < 30; i++) {\n for (int j = 0; j < n; j++) {\n dp[i + 1][j] = dp[i][j];\n }\n int N = S[i].size();\n vector<int> tmp(n, 0);\n for (int j = 0; j < N; j++) {\n //S[i][j]\n for (int k = 0; k < n; k++) {\n tmp[k] |= dp[i][S[i][j]][k];\n }\n }\n for (int j = 0; j < N; j++) {\n for (int k = 0; k < n; k++) {\n dp[i + 1][S[i][j]][k] |= tmp[k];\n }\n }\n }\n int ans = 31;\n vector<int> Check(n, 1);\n for (int i = 0; i < 31; i++) {\n bool ok = false;\n for (int j = 0; j < n; j++) {\n if (dp[i][j] == Check) {\n ans = i;\n ok = true;\n break;\n }\n }\n if (ok) break;\n }\n if (ans == 31) {\n ans = -1;\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3584, "score_of_the_acc": -1.0367, "final_rank": 16 }, { "submission_id": "aoj_2011_9369429", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nvoid solve(){\n while(true){\n int n;cin>>n;\n if(n==0)return;\n vvl A(n);\n vvl day(31);\n rep(i,n){\n ll f;cin>>f;\n A[i].resize(f);\n rep(j,f)cin>>A[i][j],A[i][j]--,day[A[i][j]].push_back(i);\n }\n vector<bitset<60>> p(n);\n rep(i,n)p[i].set(i,1);\n ll ans = INF;\n rep(i,31){\n bitset<60> now;\n for(auto a:day[i])now|=p[a];\n for(auto a:day[i])p[a]|=now;\n dbg(day[i]);\n dbg(now);\n rep(j,n){\n if(p[j].count()==n){\n chmin(ans,(ll)i+1);\n }\n }\n }\n cout<<(ans==INF?-1:ans)<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3452, "score_of_the_acc": -0.7295, "final_rank": 9 }, { "submission_id": "aoj_2011_9308905", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\n\nconstexpr ll mod = 998244353;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\n\nbool is_ok(ll S,int n){\n for (ll k=0; k<n; k++){\n if ((S&((ll)1<<k))==0)return false;\n }\n return true;\n}\n\nint main(){\n while (true){\n int n;\n cin>>n;\n if (n==0)break;\n\n vector<vector<ll>> dp(32,vector<ll>(n));\n vector<vector<int>> vec(31);\n for (int i=0; i<n; i++){\n int m;\n cin>>m;\n for (int j=0; j<m; j++) {\n int t;\n cin >> t;\n vec[t].push_back(i);\n }\n }\n\n for (ll i=0; i<n; i++){\n dp[0][i]=((ll)1<<i);\n \n }\n\n int ans=-1;\n for (int t=0; t<31; t++){\n ll S=0;\n for (auto v: vec[t]){\n\n S=(S|dp[t][v]);\n }\n\n\n if (is_ok(S,n)){\n ans=t;\n break;\n }\n for (auto v: vec[t]){\n dp[t][v]=S;\n }\n for (int v=0; v<n; v++){\n dp[t+1][v]=dp[t][v];\n }\n }\n cout<<ans<<endl;\n\n }\n\n\n\n\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3440, "score_of_the_acc": -0.7141, "final_rank": 8 }, { "submission_id": "aoj_2011_9307401", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<int>can_go[30];\nbool solve()\n{\n int N;\n cin>>N;\n if(N==0)return 0;\n for(int i=0;i<30;i++)can_go[i].clear();\n for(int i=0;i<N;i++)\n {\n int K;\n cin>>K;\n for(;K--;)\n {\n int a;\n cin>>a;\n can_go[a-1].push_back(i);\n }\n }\n vector<set<int>>dp(N);\n for(int i=0;i<N;i++)dp[i].insert(i);\n int day=0;\n bool fn=false;\n while(day<30)\n {\n auto V=can_go[day];\n for(int u:V)\n {\n for(int v:V)\n {\n for(int w:dp[v])dp[u].insert(w);\n }\n if(dp[u].size()==N)fn=true;\n }\n day++;\n if(fn)break;\n }\n cout<<(fn?day:-1)<<'\\n';\n return 1;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 950, "memory_kb": 3500, "score_of_the_acc": -1.6903, "final_rank": 19 }, { "submission_id": "aoj_2011_9287636", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define rep(i,n) for(ll i = 0;i < (ll)(n);i++)\n\n\nvoid solve(ll n){\n vector<vector<ll>> day(30);\n rep(i,n){\n ll c;cin >> c;\n rep(j,c){\n ll v;cin >> v;\n v--;\n day[v].push_back(i);\n }\n }\n vector<ll> ticket(n);\n rep(i,n){\n ticket[i] = 1LL << i;\n }\n rep(i,30){\n ll ntk = 0;\n for(ll &p:day[i]){\n ntk |= ticket[p];\n }\n if(ntk == (1LL << n)-1){\n cout << i+1 << endl;\n return ;\n }\n for(ll &p:day[i]){\n ticket[p] = ntk;\n }\n }\n cout << -1 << endl;\n return ;\n}\n\nint main(){\n while(true){\n ll n;cin >> n;\n if(n == 0)break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3096, "score_of_the_acc": -0.0092, "final_rank": 1 }, { "submission_id": "aoj_2011_9259571", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int MAX = 1000000005;\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n vector<int> f(n);\n vector<vector<int>> s(n);\n for (int i = 0; i < n; i++) {\n cin >> f[i];\n for (int j = 0; j < f[i]; j++) {\n int d;\n cin >> d;\n s[i].push_back(d);\n }\n }\n vector<vector<int>> free(31);\n for (int i = 1; i <= 30; i++) {\n for (int j = 0; j < n; j++) {\n for (int d : s[j]) {\n if (d == i) free[i].push_back(j);\n }\n }\n }\n vector<long long> dp(n);\n for (int i = 0; i < n; i++) dp[i] = (1LL << i);\n for (int i = 1; i <= 30; i++) {\n for (int j : free[i]) {\n for (int k : free[i]) {\n if (j == k) continue;\n dp[j] |= dp[k];\n }\n }\n for (int j = 0; j < n; j++) {\n if (dp[j] == (1LL << n) - 1) {\n cout << i << endl;\n return true;\n }\n }\n }\n cout << -1 << endl;\n\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie();\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3360, "score_of_the_acc": -0.5502, "final_rank": 6 }, { "submission_id": "aoj_2011_9253564", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,a,b) for(int i=a;i<b;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define dbgv(x); for(auto now : x) cout << now << \" \"; cout << endl;\n\nstruct UnionFind {\n vector<int> par;\n UnionFind(int n=0): par(n,-1) {}\n int find(int x) {\n if(par[x] < 0) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y){\n x = find(x); y = find(y);\n if(x==y) return false;\n if(par[x] > par[y]) swap(x,y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n bool same(int x, int y) {return find(x) == find(y);}\n int size(int x) {return -par[find(x)];}\n};\n\nint main(){\n while (1){\n int n; cin >> n;\n if(n == 0) break;\n vector<set<int>> v(n);\n rep(i,n){\n v[i].insert(i);\n }\n vector<vector<int>> days(30);\n rep(i,n){\n int k; cin >> k;\n rep(j,k){\n int x; cin >> x;\n x--;\n days[x].push_back(i);\n }\n }\n bool ok = false;\n for(int i = 0;i < 30;i++){\n set<int> now;\n for(int idx : days[i]){\n for(int test : v[idx]) now.insert(test);\n }\n if(now.size() == n){\n cout << i+1 << endl;\n ok = true;\n break;\n }\n for(int idx : days[i]) v[idx]= now;\n }\n if(!ok) cout << -1 << endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3284, "score_of_the_acc": -0.4678, "final_rank": 5 }, { "submission_id": "aoj_2011_9111948", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\n\nint n;\n\nvoid solve() {\n vector<set<int>> st (n);\n rep(i,n) {\n st[i].insert(i);\n }\n vector<vector<int>> date (30);\n int x,d;\n rep(i,n) {\n cin >> x;\n rep(j,x) {\n cin >> d;\n date[d-1].push_back(i);\n }\n }\n set<int> mx;\n rep(i,30) {\n mx.clear();\n for (auto w : date[i]) {\n for (auto e : st[w]) {\n mx.insert(e);\n }\n }\n for (auto w : date[i]) {\n for (auto e : mx) {\n st[w].insert(e);\n }\n }\n rep(j,n) {\n if (st[j].size() == n) {\n cout << i+1 << endl;\n return;\n }\n }\n }\n cout << -1 << endl;\n return;\n}\n\nint main() {\n while (true) {\n cin >> n; \n if (!n) {\n return 0;\n }\n solve();\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3580, "score_of_the_acc": -1.1294, "final_rank": 17 }, { "submission_id": "aoj_2011_9004442", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nint main(void) {\n while(true) {\n int n;\n cin >> n;\n if(n == 0) break;\n vector<vector<int>> p(30);\n rep(i, 0, n) {\n int f;\n cin >> f;\n rep(j, 0, f) {\n int d;\n cin >> d;\n d--;\n p[d].push_back(i);\n }\n }\n vector<ll> m(n);\n rep(i, 0, n) {\n m[i] = 1ll << i;\n }\n int ans = -1;\n rep(i, 0, 30) {\n ll mask = 0;\n rep(j, 0, (int)p[i].size()) {\n mask |= m[p[i][j]];\n }\n if(mask + 1 == (1ll << n)) {\n ans = i + 1;\n break;\n }\n rep(j, 0, (int)p[i].size()) {\n m[p[i][j]] = mask;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.1393, "final_rank": 2 }, { "submission_id": "aoj_2011_8988232", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint solve(int N){\n int D = 31;\n vvi day(31);\n vvi g(N * D);\n rep(i, N) rep(j, D - 1) g[i * D + j + 1].push_back(i * D + j);\n rep(p, N){\n int M; cin >> M;\n rep(i, M){\n int d; cin >> d;\n day[d].push_back(p);\n }\n }\n rep(i, D) if (len(day[i])){\n rep(b, len(day[i])) rep(a, b){\n g[day[i][a] * D + i].push_back(day[i][b] * D + i);\n g[day[i][b] * D + i].push_back(day[i][a] * D + i);\n }\n deque<int> q;\n vi seen(N * D, 0);\n q.push_back(day[i][0] * D + i);\n seen[day[i][0] * D + i] = 1;\n set<int> canget = {day[i][0]};\n while (!q.empty()){\n auto v = q.front(); q.pop_front();\n for (auto u : g[v]) if (!seen[u]){\n canget.insert(u / D);\n seen[u] = 1;\n q.push_back(u);\n }\n }\n if (len(canget) == N) return i;\n }\n return -1;\n}\n\nint main(){\n vc<int> ans;\n while(true){\n int N; cin >> N;\n if (N == 0) break;\n ans.push_back(solve(N));\n }\n for (auto x : ans) cout << fixed << setprecision(4) << x << endl;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3380, "score_of_the_acc": -0.7379, "final_rank": 10 } ]
aoj_2013_cpp
Osaki 大崎 English text is not available in this practice contest. 山手線は東京 23 区内に敷設されている環状鉄道路線である.総路線距離は 34.5km であり,1 周にはおよそ 1 時間を要する.駅は全部で 29 駅存在する.ラインカラーはウグイス色である.ピーク時の混雑率は 200% を超え,日本の鉄道路線の中で最も混雑している路線の 1 つである.最も混雑する時間帯では 3 分に 1 本の列車が走っており,東京に初めて来た人々はその光景に驚くものである. 鉄子さんは山手線を愛してやまない生粋の鉄道好きである.ある日,彼女は愛読書である JR 時刻表を読みながら次の疑問に至った.「山手線では一日に何台の車両が使われているのだろう?」 彼女は時刻表から運行に最低限必要な車両の台数を割り出そうとした.しかし,列車の本数が非常に多かったので,彼女一人の力では到底数え切れそうにない.そこで彼女は優秀なプログラマーであるあなたに助けを求めた. あなたの仕事は与えられた時刻表から山手線の運行に要する車両の最低台数を求めるプログラムを書くことである.山手線は環状路線であることから,その時刻表は便宜上「大崎駅」を始発駅および終着駅として表記されることが多い.そのため,彼女から渡された時刻表にも各列車の大崎駅における発時刻および着時刻のみが記されている. なお,実際の山手線では起こりえないが,状況設定を簡単にするため,ここでは大崎駅の到着直後に列車が大崎駅を出発可能であると考えることにする.また,鉄子さんが写した時刻に誤りがあったり,鉄子さんの妄想によって勝手に加えられた列車が時刻表に紛れ込んだりしている場合もあるが,あなたはそれらを見抜くことができないので,あくまでも書かれたとおりの時刻に対して台数を求めなければならない. きちんと動作するプログラムを書けば,彼女が列車内デートに誘ってくれるかもしれない.もっとも,誘いに乗るか断るかはあなた次第であるが. Input 入力は複数のデータセットから構成される.各データセットは次の形式になっている. n hh : mm : ss hh : mm : ss hh : mm : ss hh : mm : ss ... hh : mm : ss hh : mm : ss 1 行目の整数 n は時刻表に含まれる列車の本数である.この値は 10,000 を超えないことが保証されている.2 行目から n + 1 行目までの n 行には各列車の大崎駅の発時刻および着時刻がこの順番で与えられ,発時刻と着時刻の間は 1 つの空白で区切られている.各時刻は hh : mm : ss の形式で表現され, hh が時, mm が分, ss が秒を表している.それぞれの値の範囲は 0 ≦ hh < 24, 0 ≦ mm < 60, 0 ≦ ss < 60である.これらの数値は全て 2 桁となるように,必要に応じて先頭に 0 が付け加えられている. 夜の 24:00 をまたいで運行されるような列車は含まれない.したがって,常に発時刻は常に着時刻よりも前の時刻である. 入力の終了は n = 0 によって示される.これはデータセットには含まれない. Output 各データセットに対して,最低限必要となる車両の台数を 1 行に出力しなさい. Sample Input 3 05:47:15 09:54:40 12:12:59 12:13:00 16:30:20 21:18:53 6 00:00:00 03:00:00 01:00:00 03:00:00 02:00:00 03:00:00 03:00:00 04:00:00 03:00:00 05:00:00 03:00:00 06:00:00 0 Output for the Sample Input 1 3
[ { "submission_id": "aoj_2013_11060394", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; ++i)\n\nint time_to_int(string s) {\n int h = stoi(s.substr(0, 2));\n int m = stoi(s.substr(3, 2));\n int sec = stoi(s.substr(6, 2));\n return h * 3600 + m * 60 + sec;\n}\n\nvoid solve(int n) {\n int M = 86400;\n vector<int> S(M + 1);\n rep(i, n) {\n string s, e;\n cin >> s >> e;\n int s_int = time_to_int(s);\n int e_int = time_to_int(e);\n S[s_int]++;\n S[e_int]--;\n }\n\n rep(i, M) { S[i + 1] += S[i]; }\n\n int max_cnt = 0;\n rep(i, M + 1) max_cnt = max(max_cnt, S[i]);\n\n cout << max_cnt << endl;\n}\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3700, "score_of_the_acc": -0.1289, "final_rank": 5 }, { "submission_id": "aoj_2013_11008969", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <map>\n \nusing namespace std;\n\nint main(){\n while(true){\n int n;\n cin >> n;\n if(n == 0)break;\n vector<int> imos(86411);\n\n for(int i=0; i<n; i++){\n string s, g;\n cin >> s >> g;\n int startSec = stoi(s.substr(0, 2)) * 3600 + stoi(s.substr(3,2)) * 60 + stoi(s.substr(6,2));\n int goalSec = stoi(g.substr(0, 2)) * 3600 + stoi(g.substr(3,2)) * 60 + stoi(g.substr(6,2));\n imos[startSec]++;\n imos[goalSec]--;\n }\n // 05:47:15 09:54:40\n\n int result = 0;\n for(int i=1; i<=86410; i++){\n imos[i] += imos[i-1];\n result = max(result, imos[i]);\n }\n cout << result << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3476, "score_of_the_acc": -0.1333, "final_rank": 6 }, { "submission_id": "aoj_2013_10974226", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, n) for (int i = a; i < n; ++i)\n#define rrep(i, a, n) for (int i = n - 1; i >= a; --i)\nusing ll = long long;\nusing ld = long double;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vp = vector<pl>;\nusing vvp = vector<vp>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vs = vector<string>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vd = vector<ld>;\nusing vvd = vector<vd>;\nconst int mod = 998244353;\nconst ll hashh = 2147483647;\n\nint main()\n{\n while (1)\n {\n ll n;\n cin >> n;\n if (n == 0)\n {\n break;\n }\n vl dp(100000);\n rep(i, 0, n)\n {\n ll a, b;\n string s, t;\n cin >> s >> t;\n a = stoll(s.substr(0, 2)) * 3600 + stoll(s.substr(3, 2)) * 60 + stoll(s.substr(6, 2));\n b = stoll(t.substr(0, 2)) * 3600 + stoll(t.substr(3, 2)) * 60 + stoll(t.substr(6, 2));\n dp[a]++;\n dp[b]--;\n }\n ll ans = 0;\n rep(i, 1, 100000)\n {\n dp[i] += dp[i - 1];\n }\n rep(i, 0, 100000)\n {\n ans = max(ans, dp[i]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3960, "score_of_the_acc": -0.229, "final_rank": 13 }, { "submission_id": "aoj_2013_10948053", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n\n\n\n\n// いもす法の最大値を出力すればよい。\n\n\n\n\nint main() {\n while(true) {\n int n; cin >> n;\n if(n == 0) break;\n vector<int> train(10e5+9);\n for(int i = 0; i < n; i++) {\n string s, t; cin >> s >> t;\n int sh = int(s[0] - '0') * 10 + int(s[1] - '0'), th = int(t[0] - '0') * 10 + int(t[1] - '0');\n int sm = int(s[3] - '0') * 10 + int(s[4] - '0'), tm = int(t[3] - '0') * 10 + int(t[4] - '0');\n int ss = int(s[6] - '0') * 10 + int(s[7] - '0'), ts = int(t[6] - '0') * 10 + int(t[7] - '0');\n\n int time_s = ss + sm*60 + sh * 3600, time_t = ts + 60 * tm + 3600 * th;\n train[time_s]++; train[time_t]--;\n }\n\n for(int i = 0; i < 10e5+5; i++) train[i+1] += train[i];\n int ans = 0;\n for(int i = 0; i < 10e5+5; i++) ans = max(ans, train[i]);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 7420, "score_of_the_acc": -1.0413, "final_rank": 18 }, { "submission_id": "aoj_2013_10885923", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nusing Graph = vector<vector<int>>;\nconst int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconst int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};\n\n#define rep(i, a, b) for(int i = a; i < b; i++)\n#define all(v) v.begin(), v.end()\n#define YESNO(flag) cout << (flag ? \"Yes\" : \"No\") << \"\\n\"\n#define inf 1070000000\n#define INF 4500000000000000000\n//#define MOD 998244353\n#define MOD 1000000007\n#define pi 3.14159265358979\n\ntemplate<class T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n//xのn乗\nll llpow(ll x, ll n) {\n ll ans = 1;\n while(n > 0) {\n if(n & 1) ans *= x;\n x *= x;\n n >>= 1; \n }\n return ans;\n}\nll modpow(ll x, ll n, ll mod) {\n ll ans = 1;\n while(n > 0) {\n if(n & 1) ans = ans * x % mod;\n x = x * x % mod;\n n >>= 1;\n }\n return ans;\n}\n//mod pでのaの逆元\nll inverse(ll a, ll p) {\n return modpow(a, p-2, p);\n}\n//nの階乗\nll fact(ll n) {\n if(n == 0) return 1;\n else return n * fact(n-1);\n}\nll modfact(ll n, ll mod) {\n if(n == 0) return 1;\n else return (n * modfact(n-1, mod)) % mod;\n}\n//nCr=n!/r!(n-r)! mod p\nll comb(ll n, ll r, ll mod) {\n ll a = modfact(n, mod), b = modfact(r, mod), c = modfact(n-r, mod);\n b = inverse(b, mod); c = inverse(c, mod);\n return (((a*b)%mod)*c)%mod;\n}\n\n//1+2+...+n = n(n+1)/2\nll triangle(ll n) {\n ll x = n*(n+1);\n return x/2;\n}\n//素数判定\nbool is_prime(long long x) {\n if(x == 1) return false;\n \n for(long long i = 2; i*i <= x; i++) {\n if(x % i == 0) return false;\n }\n return true;\n}\n\n//座標圧縮\nvector<int> compress(vector<int> A) {\n vector<int> B = A;\n\n sort(B.begin(), B.end());\n B.erase(unique(B.begin(), B.end()), B.end());\n\n vector<int> res(A.size());\n for(int i = 0; i < (int)A.size(); i++) {\n res[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();\n }\n return res;\n}\n\n//正方形グリッドの右回転\nvoid rotate_right(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[N-1-j][i];\n }\n S = T;\n}\n//左回転\nvoid rotate_left(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[j][N-1-i];\n }\n S = T;\n}\n\n//2点(a,b)と(x,y)の距離\ndouble dist(ll a, ll b, ll x, ll y) {\n return sqrt((a-x)*(a-x) + (b-y)*(b-y));\n}\nint man(int a, int b, int x, int y) {\n return abs(a-x) + abs(b-y);\n}\n\n//桁数\nint digit(ll N) {\n if(N <= 9) return 1;\n\n return 1 + digit(N / 10);\n}\n//A進数文字列NをB進数に変換. A,Bは10以下\nstring base_change(string N, ll A, ll B) {\n ll X = stoll(N), Y = 0;\n\n ll i = 1;\n while(X != 0) {\n Y += (X % 10) * i;\n i *= A;\n X /= 10;\n }\n\n X = Y, Y = 0;\n i = 1;\n while(X != 0) {\n Y += (X % B) * i;\n i *= 10;\n X /= B;\n }\n\n return to_string(Y);\n}\n//回文判定\nbool palin(string S) {\n rep(i,0,S.size()) {\n if(S[i] != S[S.size()-1-i]) return false;\n }\n return true;\n}\n\n//hh:mm:ss to second\nint convert_to_time(string S) {\n ll h = 10*(S[0]-'0') + (S[1]-'0');\n ll m = 10*(S[3]-'0') + (S[4]-'0');\n ll s = 10*(S[6]-'0') + (S[7]-'0');\n \n return h*3600+m*60+s;\n}\n\n//DFS\nvoid dfs(const Graph &G, int v, vector<bool> &seen) { \n seen[v] = true;\n \n for (auto next_v : G[v]) { \n if (!seen[next_v]) {\n dfs(G, next_v, seen);\n }\n }\n}\nvoid griddfs(const vector<vector<char>> &G, int x, int y, vector<vector<bool>> &seen) {\n int H = G.size(), W = G[0].size();\n seen[x][y] = true;\n \n for (int i = 0; i < 8; i++) { \n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if (!seen[nx][ny] and G[nx][ny] != '#') griddfs(G, nx, ny, seen);\n }\n}\n//BFS\nvoid bfs(const Graph &G, int v, vector<int> &dist) {\n rep(i,0,dist.size()) dist[i] = inf;\n dist[v] = 0;\n queue<int> q;\n q.push(v);\n while(!q.empty()) {\n int u = q.front();\n q.pop();\n for(auto next_u : G[u]) {\n if(dist[next_u] == inf) {\n dist[next_u] = dist[u] + 1;\n q.push(next_u);\n }\n }\n }\n}\nvoid gridbfs(const vector<vector<char>> &G, int x, int y, vector<vector<int>> &dist) {\n int H = G.size(), W = G[0].size();\n rep(i,0,H) rep(j,0,W) dist[i][j] = inf;\n dist[x][y] = 0;\n queue<pair<int, int>> q;\n q.push(make_pair(x, y));\n while(!q.empty()) {\n int x = q.front().first, y = q.front().second;\n q.pop();\n for(int i = 0; i < 4; i++) {\n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if(dist[nx][ny] == inf and G[nx][ny] != '#') {\n dist[nx][ny] = dist[x][y] + 1;\n q.push(make_pair(nx, ny));\n }\n }\n }\n}\n//ワーシャル・フロイド\nvoid floyd(vector<vector<ll>> &G) {\n int N = G.size();\n for (int k = 0; k < N; k++){\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if(G[i][j] > G[i][k] + G[k][j]) {\n G[i][j] = G[i][k] + G[k][j];\n }\n }\n }\n }\n}\n\n//Union-Find\nvector<int> par(0);\nint root(int x) {\n if(par[x] == x) {\n return x;\n }else {\n return par[x] = root(par[x]);\n }\n}\nbool same(int x, int y) {\n return root(x) == root(y);\n}\nvoid unite(int x, int y) {\n x = root(x);\n y = root(y);\n if(x == y) return;\n par[x] = y;\n}\n\nvoid solve() { \n}\n\nint main() {\n while(1) {\n int N; cin >> N;\n if(N == 0) break;\n\n vector<int> A(86500, 0);\n rep(i,0,N) {\n string S, T; cin >> S >> T;\n int a = convert_to_time(S), b = convert_to_time(T);\n A[a]++; A[b]--;\n }\n\n rep(i,1,86500) A[i] += A[i-1];\n int ans = 0;\n rep(i,0,86500) chmax(ans, A[i]);\n cout << ans << \"\\n\"; \n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3772, "score_of_the_acc": -0.1381, "final_rank": 10 }, { "submission_id": "aoj_2013_10880522", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n// using mint = modint998244353;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll,ll>;\nusing vstr = vector<string>;\nusing vint = vector<int>;\nusing vvi = vector<vint>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vpii = vector<pii>;\nusing vvpii = vector<vpii>;\nusing vpll = vector<pll>;\n#define mkpr make_pair\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define Rep(i,a,b) for(int i=a;i<b;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define printv(x); for(auto now : x) cout << now << \" \"; cout << endl;\n#define yes(q) cout << ((q) ? \"Yes\" : \"No\") << endl;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\nconst int INF =1001001001;\nconst ll INFL = 4e18;\n\nll f(string s){\n ll ret=0;\n\n ll hour =stoll(s.substr(0,2));\n ll minute =stoll(s.substr(3,2));\n ll second =stoll(s.substr(6,2));\n ret=hour*3600+minute*60+second;\n\n\n return ret;\n}\nint main()\n{\n while(1){\n int n;\n cin>>n;\n if(n==0)break;\n vll s(24*3600+1);\n rep(i,n){\n string st,ed;\n cin>>st>>ed;\n s[f(st)]--;\n s[f(ed)]++;\n }\n rep(i,24*3600)s[i+1]+=s[i];\n ll ans=0;\n for(auto x:s)chmin(ans,x);\n cout<<-ans<<endl;\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3944, "score_of_the_acc": -0.1603, "final_rank": 11 }, { "submission_id": "aoj_2013_10877428", "code_snippet": "#line 1 \"main.cpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n#line 8 \"Library/macros.hpp\"\n\nusing ll = long long;\nusing ld = long double;\n\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rep1(i,n) for(ll i = 1; i <= (n); ++i)\n#define rrep(i,n) for(ll i = n-1; i >= 0; --i)\n#define all(x) (x).begin(), (x).end()\n#define rall(a) a.rbegin(),a.rend()\n\ninline void Yes() { std::cout << \"Yes\" << std::endl; }\ninline void No() { std::cout << \"No\" << std::endl; }\ninline void YN(bool flag) {flag ? Yes() : No();}\n\ntemplate<class T> bool chmin(T& a,T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T> bool chmax(T& a,T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<typename T> using vc = std::vector<T>;\ntemplate<class T> using P = std::pair<T, T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<class T> using PQ = std::priority_queue<T, vc<T>>;\ntemplate<class T> using PQg = std::priority_queue<T, vc<T>, std::greater<T>>;\ninline const std::vector<ll> dx = {1, 0, -1, 0};\ninline const std::vector<ll> dy = {0, 1, 0, -1};\ninline constexpr ll INF = 2e18;\n\n//グリッドを90度回転\ntemplate <typename T>\nvv<T> rotate(const vv<T>& v) {\n if(v.empty() || v[0].empty()) return {};\n\n size_t n = v.size();\n size_t m = v[0].size();\n\n vv<T> nx(m,vc<T>(n));\n\n for (size_t i = 0; i < n; ++i)for (size_t j = 0; j < m; ++j) {\n nx[j][n-1-i] = v[i][j];\n }\n return nx;\n}\n\ntemplate <typename T>\nvv<T> counter_rotate(const vv<T>& v) {\n if(v.empty() || v[0].empty()) return {};\n\n size_t n = v.size();\n size_t m = v[0].size();\n\n vv<T> nx(m,vc<T>(n));\n\n for (size_t i = 0; i < n; ++i)for (size_t j = 0; j < m; ++j) {\n nx[m-1-j][i] = v[i][j];\n }\n return nx;\n}\n#line 4 \"main.cpp\"\n\n/*考察 */\nint main() {\n while(true) {\n ll n;cin>>n;\n if(!n) break;\n vc<ll> sum(250000);\n rep(i,n) {\n string op,ed;\n cin >> op >> ed;\n string nop,ned;\n rep(i,9) {\n if(i%3==2) continue;\n nop += op[i]; ned += ed[i];\n }\n ll s = stoll(nop),e = stoll(ned);\n sum[s]++;sum[e]--;\n }\n rep(i,250000) sum[i+1] += sum[i];\n cout << *max_element(all(sum)) << endl;\n}\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 5088, "score_of_the_acc": -0.4076, "final_rank": 16 }, { "submission_id": "aoj_2013_10865660", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint change_to_second(string s) {\n int h = stoi(s.substr(0, 2));\n int m = stoi(s.substr(3,2));\n int sec = stoi(s.substr(6,2));\n return h * 3600 + m * 60 + sec;\n}\n\nint main(){\n vector<int> res_list;\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n vector<int> imos(86400, 0);\n for (int i = 0; i < n; ++i) {\n string s1, s2; cin >> s1 >> s2;\n int t1 = change_to_second(s1);\n int t2 = change_to_second(s2);\n imos[t1] += 1;\n imos[t2] -= 1;\n }\n int res = imos[0];\n for (int i = 1; i < imos.size(); ++i) {\n imos[i] += imos[i-1];\n res = max(res, imos[i]);\n }\n res_list.push_back(res);\n }\n for (int res: res_list) cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3748, "score_of_the_acc": -0.135, "final_rank": 9 }, { "submission_id": "aoj_2013_10848991", "code_snippet": "#include <bits/stdc++.h>\n#include <cstdio>\n\nusing namespace std;\n\nint toSec(int, int, int);\nint toSec(int hh, int mm, int ss) {\n return hh*60*60 + mm*60 + ss;\n}\n\nint main(void)\n{\n int N;\n while (scanf(\"%d\", &N), N) {\n vector<pair<int, int>> times;\n for (int i = 0; i < N; i++) {\n pair<int, int> p;\n int h, m, s;\n scanf(\"%d:%d:%d\", &h, &m, &s);\n p.first = toSec(h, m, s);\n p.second = 1;\n times.push_back(p);\n scanf(\"%d:%d:%d\", &h, &m, &s);\n p.first = toSec(h, m, s);\n p.second = -1;\n times.push_back(p);\n }\n sort(times.begin(), times.end());\n\n int ma = 0;\n int curr = 0;\n for (auto const &p: times) {\n assert(curr >= 0);\n curr += p.second;\n ma = max(ma, curr);\n }\n assert(curr == 0);\n printf(\"%d\\n\", ma);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3652, "score_of_the_acc": -0.056, "final_rank": 2 }, { "submission_id": "aoj_2013_10847031", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint wa[250000];\n\nint main() {\n while(true){\n int N;\n cin >> N;\n if(N == 0){\n return 0;\n }\n for(int i = 0; i <= 240000; i++){\n wa[i] = 0;\n }\n string l, r;\n for(int i = 0; i < N; i++){\n cin >> l >> r;\n int L = 0, R = 0;\n L += 10000 * stoi(l.substr(0, 2));\n L += 100 * stoi(l.substr(3, 2));\n L += stoi(l.substr(6, 2));\n R += 10000 * stoi(r.substr(0, 2));\n R += 100 * stoi(r.substr(3, 2));\n R += stoi(r.substr(6, 2));\n\n //cout << L << \" \" << R << endl;\n\n wa[L]++; wa[R]--;\n }\n\n for(int i = 1; i <= 240000; i++){\n wa[i] = wa[i-1] + wa[i];\n }\n\n int ans = 0;\n for(int i = 0; i <= 240000; i++){\n ans = max(ans, wa[i]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4144, "score_of_the_acc": -0.386, "final_rank": 15 }, { "submission_id": "aoj_2013_10803702", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <map>\n#include <bitset>\n#include <cmath>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <cassert>\n#include <unordered_set>\n#include <unordered_map>\n#include <cmath>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define all(v) v.begin(), v.end()\n\nll llpow(ll a, ll t)\n{\n ll res = 1;\n rep(i, t) res *= a;\n return res;\n}\n\nll inf = std::numeric_limits<ll>::max();\n\nint main()\n{\n vector<ll> ans;\n while (true)\n {\n ll n;\n cin >> n;\n if (!n)\n {\n break;\n }\n vector<vector<ll>> vec(n);\n rep(i, n)\n {\n string s, t;\n cin >> s >> t;\n ll snum = 0;\n snum += (s[0] - '0') * 36000;\n snum += (s[1] - '0') * 3600;\n snum += (s[3] - '0') * 600;\n snum += (s[4] - '0') * 60;\n snum += (s[6] - '0') * 10;\n snum += s[7] - '0';\n ll tnum = 0;\n tnum += (t[0] - '0') * 36000;\n tnum += (t[1] - '0') * 3600;\n tnum += (t[3] - '0') * 600;\n tnum += (t[4] - '0') * 60;\n tnum += (t[6] - '0') * 10;\n tnum += t[7] - '0';\n vec[i] = {snum, tnum};\n }\n vector<ll> sum(86400, 0);\n rep(i, n)\n {\n ll s = vec[i][0];\n ll t = vec[i][1];\n sum[s]++;\n sum[t]--;\n }\n rep(i, 86400)\n {\n if (!i)\n {\n continue;\n }\n sum[i] += sum[i - 1];\n }\n ll mx = 0;\n rep(i, 86400)\n {\n mx = max(mx, sum[i]);\n }\n ans.push_back(mx);\n }\n for (auto num : ans)\n {\n cout << num << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4332, "score_of_the_acc": -0.2436, "final_rank": 14 }, { "submission_id": "aoj_2013_10710410", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define ld long double\n#define INF (ll)1e18\n#define moda(i,mod) ((ll)i%(ll)mod+(ll)mod)%(ll)mod\n#define ll long long\nint main(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n vector<ll> s;\n while(1){\n ll n;cin>>n;if(n==0)break;\n vector<ll> f(1e6,0);\n while(n--){\n for(ll i=0;i<2;i++){\n char s;ll a,b,c;cin>>a>>s>>b>>s>>c;\n if(i==0)f[(a*60+b)*60+c]++;\n else f[(a*60+b)*60+c]--;\n }\n }\n ll ans=0;\n for(ll i=0;i<f.size()-1;i++){\n f[i+1]+=f[i];\n ans=max(ans,f[i]);\n }s.push_back(ans);\n }\n for(auto i:s)cout<<i<<endl;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 11240, "score_of_the_acc": -1.3667, "final_rank": 19 }, { "submission_id": "aoj_2013_10680952", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\n\n\n\n\n\n\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\n while(1){\n LL(n);\n if(n == 0){break;}\n auto f = [&](string s){\n ll res = 0;\n res += 3600 * 10 * (ll)(s[0] - '0');\n res += 3600 * 1 * (ll)(s[1] - '0');\n res += 60 * 10 * (ll)(s[3] - '0');\n res += 60 * 1 * (ll)(s[4] - '0');\n res += 1 * 10 * (ll)(s[6] - '0');\n res += 1 * 1 * (ll)(s[7] - '0');\n return res;\n };\n vvll x(100000);\n rep(i,n){\n STR(s,t);\n ll a = f(s);\n ll b = f(t);\n x[a].push_back(-1);\n x[b].push_back(1);\n }\n ll ans = 0;\n ll temp = 0;\n rep(i,100000){\n sort(rall(x[i]));\n for(ll y:x[i]){\n temp += y;\n if(temp < 0){\n ans++;\n temp++;\n }\n }\n }\n print(ans);\n }\n \n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6392, "score_of_the_acc": -0.4422, "final_rank": 17 }, { "submission_id": "aoj_2013_10615201", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n;\nvector<string> s, t;\nvoid input() {\n cin >> n;\n s.resize(n);\n t.resize(n);\n for(int i = 0; i < n; i++)\n cin >> s[i] >> t[i];\n}\nvoid solve() {\n vector<a2> eve;\n auto to_int = [](string s) {\n ll ret = 0;\n ret += 3600 * stoi(s.substr(0, 2));\n ret += 60 * stoi(s.substr(3, 2));\n ret += 1 * stoi(s.substr(6, 2));\n return ret;\n };\n for(auto &x : s) {\n eve.push_back({to_int(x), 1});\n }\n for(auto &x : t) {\n eve.push_back({to_int(x), -1});\n }\n\n sort(eve.begin(), eve.end());\n\n ll ans = 0, cnt = 0;\n for(auto &x : eve) {\n cnt += x[1];\n chmax(ans, cnt);\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4260, "score_of_the_acc": -0.1343, "final_rank": 8 }, { "submission_id": "aoj_2013_10604362", "code_snippet": "#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n//https://boostjp.github.io/tips/multiprec-int.html\n\n#define YES cout<<\"Yes\"<<endl\n#define NO cout<<\"No\"<<endl\n#define YN {cout<<\"Yes\"<<endl;}else{cout<<\"No\"<<endl;}// if(a==b)YN;\n#define NO2 cout<<-1<<endl\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define rrep(i, n) for (int i=int(n)-1; i>=0; --i)\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n\nint toSec(string str) {\n int h = (str[0]-'0')*10 + (str[1]-'0');\n int m = (str[3]-'0')*10 + (str[4]-'0');\n int s = (str[6]-'0')*10 + (str[7]-'0');\n return h*3600 + m*60 + s;\n}\n\nvoid solve(int N) {\n vector<int> pref(86409);\n rep(i,N) {\n string S,E;\n cin >> S >> E;\n int start = toSec(S), end = toSec(E);\n start++; end++;\n pref[start]++; pref[end]--;\n }\n rep(i, pref.size()-1) pref[i+1] += pref[i];\n int ans = 0;\n rep(i, pref.size()) ans = max(ans, pref[i]);\n cout << ans << endl;\n}\n\nint main() {\n while(true) {\n int N;\n cin >> N;\n if (N == 0) break;\n solve(N);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3480, "score_of_the_acc": -0.1005, "final_rank": 4 }, { "submission_id": "aoj_2013_10595447", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cmath>\n#include <algorithm>\n#include <set>\n#include <map>\n#include <queue>\n#include <iomanip>\n#include <functional>\n#include <stack>\n\nusing namespace std;\nusing l = long long;\nusing ul = unsigned long long;\n\nconst int inf = 2147483647; // 2e9 1 << 30\nconst l INF = 9223372036854775807; // 9e18 1LL << 60\n\n#define r(i, n) for (l i = 0; i < n; ++i)\n#define r1(i, n) for (l i = 1; i < n; ++i)\n#define r0(i) for (l i = -1; i < 2; ++i)\n#define pll pair<l, l>\n\n\nvoid YesNo(bool s=false) {\n if (s) cout<<\"Yes\"<<endl;\n else cout<<\"No\"<<endl;\n}\n\nvoid nans(l ans) {\n if (ans==INF) cout<<-1<<endl;\n else cout<<ans<<endl;\n}\n\nstruct UnionFind {\n vector<l> parent, rank, size;\n l tree_count;\n\n UnionFind(l n) : parent(n), rank(n, 0), size(n, 1), tree_count(n) {\n for (l i = 0; i < n; i++) parent[i] = i;\n }\n \n l find(l x) {\n if (parent[x] == x) return x;\n return parent[x] = find(parent[x]); // パス圧縮\n }\n bool unite(pll xy) {\n l rx = find(xy.first);\n l ry = find(xy.second);\n if (rx == ry) return false;\n if (rank[rx] < rank[ry]) swap(rx, ry);\n parent[ry] = rx;\n size[rx] += size[ry];\n if (rank[rx] == rank[ry]) rank[rx]++;\n\n tree_count--;\n\n return true;\n }\n \n bool same(pll xy) {\n return find(xy.first) == find(xy.second);\n }\n \n l getSize(l x) {\n return size[find(x)];\n }\n};\n\n\nvoid demo() {\n l n;cin>>n;\n\n if (n==0) exit(0);\n \n vector<l> t(24*3600);\n\n r(i, n) {\n int sh, sm, ss, gh, gm, gs;\n\n scanf(\"%d:%d:%d %d:%d:%d\", &sh, &sm, &ss, &gh, &gm, &gs);\n\n t[sh*3600+sm*60+ss]++;\n t[gh*3600+gm*60+gs]--;\n }\n\n r1(i, 24*3600) t[i] += t[i-1];\n\n l ans=0;\n\n r(i, 24*3600) ans=max(ans, t[i]);\n\n cout<<ans<<endl;\n}\n\nint main() {\n while (true) demo();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4052, "score_of_the_acc": -0.1742, "final_rank": 12 }, { "submission_id": "aoj_2013_10557081", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using namespace atcoder;\n// if using this, use \"g++ *.cpp -I ~/atcoder/ac...\" to complile\n\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define rep(i, n) for (int i = 0; i < (n); i++)\nvector<int> dx = {0, 1, 0, -1};\nvector<int> dy = {1, 0, -1, 0};\nconst int IINF = 1e9;\nconst ll LINF = 1ll<<60; \nconst ll MOD = 1e9+7;\n\n// main\nint main()\n{\n\twhile (1) {\n\t\tint n; cin >> n;\n\t\tif (n == 0) break;\n\n\t\tvector<int> v(24*60*60, 0);\n\t\trep(i, n) {\n\t\t\tint h1, h2, m1, m2, s1, s2;\n\t\t\tscanf(\"%d:%d:%d\", &h1, &m1, &s1);\n\t\t\tscanf(\"%d:%d:%d\", &h2, &m2, &s2);\n\n\t\t\tv[h1*60*60+m1*60+s1]++;\n\t\t\tv[h2*60*60+m2*60+s2]--;\n\t\t}\n\n\t\trep(i, 24*60*60-1) v[i+1] += v[i];\n\n\t\tint ans = 0;\n\t\trep(i, 24*60*60) ans = max(ans, v[i]);\n\n\t\tcout << ans << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3612, "score_of_the_acc": -0.0842, "final_rank": 3 }, { "submission_id": "aoj_2013_10495302", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing lll = __int128;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nbool solve(){\n ll N;cin>>N;\n if(N==0) return 1;\n ll K=3600*24+3;\n vll imos(K);\n rep(i,N){\n rep(j,2){\n string S;cin>>S;\n ll p=((S[0]-'0')*10+S[1]-'0')*3600+((S[3]-'0')*10+S[4]-'0')*60+((S[6]-'0')*10+S[7]-'0');\n // cout<<p<<\" \"<<S<<endl;\n if(j==0) imos[p]++;\n else imos[p]--;\n }\n }\n rep(i,K-1) imos[i+1]+=imos[i];\n cout<<*max_element(all(imos))<<endl;\n return 0;\n}\n\n\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3892, "score_of_the_acc": -0.0536, "final_rank": 1 }, { "submission_id": "aoj_2013_10433363", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n\nint main(){\n vector<int> ans(0);\n \n while(true){\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<int> table(1e6 + 10, 0);\n for(int i = 0; i < n; i++){\n string s, g;\n cin >> s >> g;\n\n int st = stoi(s.substr(0, 2))*3600 + stoi(s.substr(3, 2))*60 + stoi(s.substr(6, 2)); \n int gt = stoi(g.substr(0, 2))*3600 + stoi(g.substr(3, 2))*60 + stoi(g.substr(6, 2)); \n\n table.at(st)++;\n table.at(gt)--;\n }\n\n for(int i = 1; i <= 86400; i++){\n table.at(i) += table.at(i-1);\n }\n\n ans.push_back(*max_element(table.begin(), table.end()));\n }\n\n for(int a : ans){\n cout << a << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 7100, "score_of_the_acc": -1.4668, "final_rank": 20 }, { "submission_id": "aoj_2013_10428480", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using namespace atcoder;\n// if using this, use \"g++ *.cpp -I ~/atcoder/ac...\" to complile\n\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define rep(i, n) for (int i = 0; i < (n); i++)\nvector<int> dx = {0, 1, 0, -1};\nvector<int> dy = {1, 0, -1, 0};\nconst int IINF = 1e9;\nconst ll LINF = 1ll<<60; \n\n\n// main\nint main()\n{\n\twhile (1) {\n\t\tint n;\n\t\tcin >> n;\n\n\t\tif (n == 0) break;\n\n\t\tvector<int> sum(24*60*60+5, 0);\n\t\trep(i, n) {\n\t\t\tchar h1, h2, m1, m2, s1, s2;\n\t\t\tint h, m, sc;\n\t\t\tint tmp = 0;\n\t\t\tchar colon = ':';\n\t\t\tcin >> h1 >> h2 >> colon;\n\t\t\tcin >> m1 >> m2 >> colon;\n\t\t\tcin >> s1 >> s2;\n\n\t\t\ttmp += (60*60)*((h1-'0')*10+(h2-'0'));\n\t\t\ttmp += 60*((m1-'0')*10+(m2-'0'));\n\t\t\ttmp += (s1-'0')*10+(s2-'0');\n\t\n\t\t\tsum[tmp]++;\n\n\t\t\ttmp = 0;\n\t\t\tcin >> h1 >> h2 >> colon;\n\t\t\tcin >> m1 >> m2 >> colon;\n\t\t\tcin >> s1 >> s2;\n\n\t\t\ttmp += (60*60)*((h1-'0')*10+(h2-'0'));\n\t\t\ttmp += 60*((m1-'0')*10+(m2-'0'));\n\t\t\ttmp += (s1-'0')*10+(s2-'0');\n\n\t\t\tsum[tmp]--;\n\t\t}\n\n\t\tfor (int i = 1; i < sum.size(); i++) {\n\t\t\tsum[i] += sum[i-1];\n\t\t}\n\n\t\tint ans = 0;\n\t\tfor (auto val: sum) {\n\t\t\tans = max(ans, val);\n\t\t}\n\n\t\tcout << ans << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3476, "score_of_the_acc": -0.1333, "final_rank": 6 } ]
aoj_2014_cpp
Surrounding Area 土地囲い English text is not available in this practice contest. ある 2 人の不動産屋が,客船に乗って南の島に向かっていた.青い空,さわやかな風….2 人は他の乗客とともに船旅を楽しんでいた.ところが,ある日突然発生した竜巻によって客船が沈んでしまった.他の乗客は救助隊によって救助されたが,どういうわけかこの 2 人だけ見落とされてしまった.数日の漂流の後,彼らはある無人島に流れ着いた.この無人島は長方形であり,以下の図のように格子に区切られていた. 図 C-1: 無人島の形状 彼らはとても欲深かったので,助けを呼ぶことではなく,この無人島の土地を売ることを考えていた.そして,彼らは土地を折半するということはせず,土地の奪い合いを始めた.彼らはそれぞれ自分の土地にしたいところを,一方は黒い杭で,もう一方は白い杭で囲み始めた.全ての杭は格子の中央に打たれ,また 1 つの格子に複数の杭が打たれることはなかった.しばらくすると双方とも疲れ果てて,杭を打つことを止めた. あなたの仕事は,黒い杭および白い杭で囲まれた土地の面積を求めるプログラムを書くことである.ただし,格子 ( i , j ) が黒い杭に囲まれているとは,直感的には「格子 ( i , j ) から上下左右に任意に移動したとき,最初に当たる杭は常に黒い杭である」ことを意味する.正確には以下のように定義される. 黒い杭に「拡大隣接した」格子を次のとおりに定める.白い杭についても同様とする. 格子 ( i , j ) に杭が存在せず,さらに格子 ( i , j ) に隣接する格子のいずれかに黒い杭が存在するならば,格子 ( i , j ) は黒い杭に拡大隣接している. 格子 ( i , j ) に杭が存在せず,さらに格子 ( i , j ) に隣接する格子のいずれかが黒い杭に拡大隣接しているならば,格子 ( i , j ) は黒い杭に拡大隣接している.このルールは再帰的に適用される. このとき,格子 ( i , j ) が黒い杭に拡大隣接していて,さらに白い杭に隣接していないとき,またそのときに限って,格子 ( i , j ) が黒い杭に囲まれているという.また逆に,格子 ( i , j ) が白い杭に拡大隣接していて,さらに黒い杭に隣接していないとき,またそのときに限って,格子 ( i , j ) が白い杭に囲まれているという. Input 入力は複数のデータセットから構成される.各データセットは以下のような構成になっている. w h a 1,1 a 2,1 a 3,1 ... a w ,1 a 1,2 a 2,2 a 3,2 ... a w ,2 ... a 1, h a 2, h a 3, h ... a w , h w は土地の横幅, h は土地の縦幅を表す.これらは 1 ≦ w , h ≦ 50 を満たす.各 a i , j は格子 ( i , j ) の状態を表す 1 つの半角文字であり,「 B 」は黒い杭が打たれていることを,「 W 」は白い杭が打たれていることを,「 . 」(ピリオド)はいずれの杭も打たれていないことをそれぞれ示す. w = h = 0 は入力の終端を表しており,データセットには含まれない. Output 各データセットについて,黒い杭が囲んだ土地の大きさ,および白い杭が囲んだ土地の大きさを 1 つの空白で区切って 1 行に出力しなさい. Sample Input 10 10 .....W.... ....W.W... ...W...W.. ....W...W. .....W...W ......W.W. BBB....W.. ..B..BBBBB ..B..B.... ..B..B..W. 5 3 ...B. ...BB ..... 1 1 . 0 0 Output for the Sample Input 6 21 12 0 0 0
[ { "submission_id": "aoj_2014_10588904", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int H, W;\n cin >> W >> H;\n if (H == 0 && W == 0) return false;\n vector<string> S(H);\n for (int i = 0; i < H; i++) cin >> S[i];\n int D[5] = {0, 1, 0, -1, 0};\n int ansB = 0, ansW = 0;\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (S[i][j] != '.') continue;\n vector<vector<bool>> vis(H, vector<bool>(W, false));\n vis[i][j] = true;\n queue<pair<int, int>> q;\n q.emplace(i, j);\n bool fnB = false, fnW = false;\n while (q.size()) {\n auto [r, c] = q.front();\n q.pop();\n if (S[r][c] != '.') {\n fnB = fnB || S[r][c] == 'B';\n fnW = fnW || S[r][c] == 'W';\n continue;\n }\n for (int d = 0; d < 4; d++) {\n int nr = r + D[d], nc = c + D[d + 1];\n if (0 <= nr && nr < H && 0 <= nc && nc < W && !vis[nr][nc]) {\n vis[nr][nc] = true;\n q.emplace(nr, nc);\n }\n }\n }\n if (fnB && fnW) continue;\n ansB += fnB;\n ansW += fnW;\n }\n }\n cout << ansB << ' ' << ansW << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3456, "score_of_the_acc": -0.2075, "final_rank": 10 }, { "submission_id": "aoj_2014_10147572", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint dx[] = {-1, 0, 1, 0};\nint dy[] = {0, 1, 0, -1};\n\nint main() {\n while (true) {\n int w, h;\n cin >> w >> h;\n if (w == 0 && h == 0)break;\n vector<vector<char>> grid(h, vector<char>(w));\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n cin >> grid[i][j];\n }\n }\n\n int answ = 0, ansb = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (grid[i][j] != '.')\n continue;\n queue<pair<int, int>> q;\n vector<vector<int>> dist(h, vector<int>(w, -1));\n q.emplace(i, j);\n dist[i][j] = 0;\n bool ww = false, bb = false;\n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n for (int dir = 0; dir < 4; dir++) {\n int nx = x + dx[dir], ny = y + dy[dir];\n if (nx < 0 || nx >= h || ny < 0 || ny >= w)\n continue;\n if (grid[nx][ny] == 'W')\n ww = true;\n if (grid[nx][ny] == 'B')\n bb = true;\n if (grid[nx][ny] != '.')\n continue;\n if (dist[nx][ny] == -1) {\n dist[nx][ny] = dist[x][y] + 1;\n q.emplace(nx, ny);\n }\n }\n }\n if (ww && !bb)\n answ++;\n if (!ww && bb)\n ansb++;\n }\n }\n cout << ansb << \" \" << answ << endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3112, "score_of_the_acc": -0.1899, "final_rank": 7 }, { "submission_id": "aoj_2014_9844756", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n\nusing namespace std;\n\nvoid solve (int w, int h) {\n vector<string> a(h);\n for (auto &i: a) cin >> i;\n const vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};\n int ab = 0, aw = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (a[i][j] != '.') continue;\n bool bf = false, wf = false;\n queue<pair<int, int>> q;\n q.emplace(i, j);\n vector visited(h, vector<bool>(w));\n visited[i][j] = true;\n while(!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n if (a[x][y] == 'B') {\n bf = true;\n continue;\n }\n if (a[x][y] == 'W') {\n wf = true;\n continue;\n }\n for (int k = 0; k < 4; k++) {\n int nx = x + dx[k], ny = y + dy[k];\n if (0 <= nx && nx < h && 0 <= ny && ny < w && !visited[nx][ny]) {\n q.emplace(nx, ny);\n visited[nx][ny] = true;\n }\n }\n }\n if (bf && !wf) ab++;\n if (wf && !bf) aw++;\n }\n }\n cout << ab << \" \" << aw << endl;\n}\n\nint main() {\n int w, h;\n cin >> w >> h;\n while (w && h) {\n solve(w, h);\n cin >> w >> h;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3300, "score_of_the_acc": -0.2712, "final_rank": 16 }, { "submission_id": "aoj_2014_9124441", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nchar judge = 'N';\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\nvoid ju(vector<vector<char>> &G, ll x, ll y, ll h, ll w,\n vector<vector<bool>> &seen) {\n seen[x][y] = true;\n if (G[x][y] != '.') {\n if (judge == 'N') {\n judge = G[x][y];\n return;\n } else if (judge != G[x][y]) {\n judge = 'E';\n return;\n } else if (judge == G[x][y]) {\n return;\n }\n }\n\n for (ll i = 0; i < 4; ++i) {\n ll nx = x + dx[i];\n ll ny = y + dy[i];\n\n if (nx >= 0 && nx < h && ny >= 0 && ny < w && !seen[nx][ny]) {\n ju(G, nx, ny, h, w, seen);\n }\n }\n}\n\nvoid solve() {\n while (1) {\n ll h, w;\n cin >> w >> h;\n if (h == 0) return;\n vector<vector<char>> S(h, vector<char>(w));\n for (ll i = 0; i < h; ++i) {\n for (ll j = 0; j < w; ++j) {\n cin >> S[i][j];\n }\n }\n\n ll white = 0;\n ll black = 0;\n for (ll i = 0; i < h; ++i) {\n for (ll j = 0; j < w; ++j) {\n vector<vector<bool>> seen(h, vector<bool>(w));\n judge = 'N';\n if (S[i][j] != '.') continue;\n ju(S, i, j, h, w, seen);\n if (judge == 'W') ++white;\n else if (judge == 'B') ++black;\n }\n }\n\n cout << black << ' ' << white << endl;\n }\n}\n\nint main() {\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3592, "score_of_the_acc": -0.3212, "final_rank": 18 }, { "submission_id": "aoj_2014_9124437", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nchar judge = 'N';\nint dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1};\n\nvoid ju(vector<vector<char>> &G, ll x, ll y, ll h, ll w, vector<vector<bool>> &seen) {\n seen[x][y] = true;\n\n if (G[x][y] != '.' && judge == 'N') {\n judge = G[x][y];\n return;\n } else if (G[x][y] != '.' && judge != G[x][y]) {\n judge = 'E';\n return;\n } else if (G[x][y] != '.' && judge == G[x][y]) {\n return;\n }\n\n for (ll i = 0; i < 4; ++i) {\n ll nx = x + dx[i];\n ll ny = y + dy[i];\n\n if (nx >= 0 && nx < h && ny >= 0 && ny < w && !seen[nx][ny]) {\n ju(G,nx,ny,h,w,seen);\n }\n }\n}\n\n\nvoid solve() {\n while (1) {\n ll h,w;\n cin >> w >> h;\n if (h == 0) return;\n vector<vector<char>> S(h, vector<char>(w));\n for (ll i = 0; i < h; ++i) {\n for (ll j = 0; j < w; ++j) {\n cin >> S[i][j];\n }\n }\n\n ll white = 0;\n ll black = 0;\n for (ll i = 0; i < h; ++i) {\n for (ll j = 0; j < w; ++j) {\n vector<vector<bool>> seen(h, vector<bool>(w));\n judge = 'N';\n if (S[i][j] != '.') continue;\n ju(S,i,j,h,w,seen);\n if (judge == 'W') ++white;\n else if (judge == 'B') ++black;\n judge = 'N';\n }\n }\n\n cout << black << ' ' << white << endl;\n }\n}\n\nint main() {\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3644, "score_of_the_acc": -0.3111, "final_rank": 17 }, { "submission_id": "aoj_2014_8976473", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nvi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};\npair<int, int> solve(int H, int W){\n vc<string> S(H);rep(i, H) cin >> S[i];\n int l = 0, r = 0;\n rep(i, H) rep(j, W) if (S[i][j] == '.'){\n bool b = false, w = false;\n vvi seen(H, vi(W, 0));\n seen[i][j] = 1;\n deque<pair<int, int>> q;\n q.push_back({i, j});\n while (!q.empty()){\n auto [ni, nj] = q.front(); q.pop_front();\n rep(k, 4){\n int nxi = ni + dx[k], nxj = nj + dy[k];\n if (inside(nxi, nxj, H, W) && !seen[nxi][nxj]){\n seen[nxi][nxj] = 1;\n if (S[nxi][nxj] == '.'){\n q.push_back({nxi, nxj});\n }\n if (S[nxi][nxj] == 'B') b = true;\n if (S[nxi][nxj] == 'W') w = true;\n }\n }\n }\n if (b && !w) l++;\n if (!b && w) r++;\n }\n return {l, r};\n}\n\nint main(){\n vc<pair<int, int>> ans;\n while (true){\n int W, H; cin >> W >> H;\n if (W == 0) break;\n ans.push_back(solve(H, W));\n }\n for (auto x : ans) cout << x.first << \" \" << x.second << endl;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3420, "score_of_the_acc": -0.1957, "final_rank": 8 }, { "submission_id": "aoj_2014_7149969", "code_snippet": "#include \"bits/stdc++.h\"\n#pragma GCC optimize(\"Ofast\")\n\n// Begin Header {{{\n#pragma region\nusing namespace std;\n\n#ifndef DEBUG\n#define dump(...)\n#endif\n\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)\n#define repc(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)\n#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)\n#define var(Type, ...) \\\n Type __VA_ARGS__; \\\n input(__VA_ARGS__)\n#define let const auto\n\nconstexpr size_t operator\"\"_zu(unsigned long long value) {\n return value;\n};\nconstexpr intmax_t operator\"\"_jd(unsigned long long value) {\n return value;\n};\nconstexpr uintmax_t operator\"\"_ju(unsigned long long value) {\n return value;\n};\n\nconstexpr int INF = 0x3f3f3f3f;\nconstexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;\n\nusing usize = size_t;\nusing imax = intmax_t;\nusing uimax = uintmax_t;\nusing ld = long double;\n\ntemplate <class T, class Compare = less<>>\nusing MaxHeap = priority_queue<T, vector<T>, Compare>;\ntemplate <class T, class Compare = greater<>>\nusing MinHeap = priority_queue<T, vector<T>, Compare>;\n\ninline void input() {}\ntemplate <class Head, class... Tail>\ninline void input(Head&& head, Tail&&... tail) {\n cin >> head;\n input(forward<Tail>(tail)...);\n}\n\ntemplate <class Container, class Value = typename Container::value_type,\n enable_if_t<!is_same<Container, string>::value, nullptr_t> = nullptr>\ninline istream& operator>>(istream& is, Container& vs) {\n for (auto& v: vs) is >> v;\n return is;\n}\n\ntemplate <class T, class U>\ninline istream& operator>>(istream& is, pair<T, U>& p) {\n is >> p.first >> p.second;\n return is;\n}\n\ninline void output() {\n cout << \"\\n\";\n}\ntemplate <class Head, class... Tail>\ninline void output(Head&& head, Tail&&... tail) {\n cout << head;\n if (sizeof...(tail)) cout << \" \";\n output(forward<Tail>(tail)...);\n}\n\ntemplate <class Container, class Value = typename Container::value_type,\n enable_if_t<!is_same<Container, string>::value, nullptr_t> = nullptr>\ninline ostream& operator<<(ostream& os, const Container& vs) {\n static constexpr const char* delim[] = {\" \", \"\"};\n for (auto it = begin(vs); it != end(vs); ++it) {\n os << delim[it == begin(vs)] << *it;\n }\n return os;\n}\n\ntemplate <class Iterator>\ninline void join(const Iterator& Begin, const Iterator& End, const string& delim = \"\\n\",\n const string& last = \"\\n\") {\n for (auto it = Begin; it != End; ++it) {\n cout << ((it == Begin) ? \"\" : delim) << *it;\n }\n cout << last;\n}\n\ntemplate <class T>\ninline vector<T> makeVector(const T& init_value, size_t sz) {\n return vector<T>(sz, init_value);\n}\n\ntemplate <class T, class... Args>\ninline auto makeVector(const T& init_value, size_t sz, Args... args) {\n return vector<decltype(makeVector<T>(init_value, args...))>(\n sz, makeVector<T>(init_value, args...));\n}\n\ntemplate <class Func>\nclass FixPoint : Func {\npublic:\n explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}\n\n template <class... Args>\n constexpr decltype(auto) operator()(Args&&... args) const {\n return Func::operator()(*this, std::forward<Args>(args)...);\n }\n};\n\ntemplate <class Func>\nstatic inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {\n return FixPoint<Func> {forward<Func>(f)};\n}\n\ntemplate <class Container>\nstruct reverse_t {\n Container& c;\n reverse_t(Container& c) : c(c) {}\n auto begin() {\n return c.rbegin();\n }\n auto end() {\n return c.rend();\n }\n};\n\ntemplate <class Container>\nauto reversed(Container& c) {\n return reverse_t<Container>(c);\n}\n\ntemplate <class T>\ninline bool chmax(T& a, const T& b) noexcept {\n return b > a && (a = b, true);\n}\n\ntemplate <class T>\ninline bool chmin(T& a, const T& b) noexcept {\n return b < a && (a = b, true);\n}\n\ntemplate <class T>\ninline T diff(const T& a, const T& b) noexcept {\n return a < b ? b - a : a - b;\n}\n\ntemplate <class T>\ninline T sigma(const T& l, const T& r) noexcept {\n return (l + r) * (r - l + 1) / 2;\n}\n\ntemplate <class T>\ninline T ceildiv(const T& n, const T& d) noexcept {\n return (n + d - 1) / d;\n}\n\nvoid operator|=(vector<bool>::reference lhs, const bool rhs) {\n lhs = lhs | rhs;\n}\n\nvoid operator&=(vector<bool>::reference lhs, const bool rhs) {\n lhs = lhs & rhs;\n}\n\nvoid operator^=(vector<bool>::reference lhs, const bool rhs) {\n lhs = lhs ^ rhs;\n}\n\nvoid ioinit() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n cerr << fixed << setprecision(10);\n clog << fixed << setprecision(10);\n}\n#pragma endregion\n// }}} End Header\n\nint main() {\n ioinit();\n\n size_t W, H;\n while (input(W, H), W) {\n vector<string> ss(H);\n input(ss);\n const string T = \"BW\";\n const auto f = [&](int sr, int sc, int wi) {\n queue<pair<int, int>> que;\n que.emplace(sr, sc);\n auto seen = makeVector<bool>(false, H, W);\n seen[sr][sc] = true;\n constexpr array<int, 8> dr = {0, 1, 0, -1, -1, 1, 1, -1};\n constexpr array<int, 8> dc = {1, 0, -1, 0, 1, 1, -1, -1};\n const auto inside = [](int r, int c, int R, int C) {\n return (0 <= r && r < R && 0 <= c && c < C);\n };\n bool valid = false;\n while (!que.empty()) {\n const auto [r, c] = que.front();\n que.pop();\n rep(dir, 0, 4) {\n int nr = r + dr[dir];\n int nc = c + dc[dir];\n if (!inside(nr, nc, H, W)) continue;\n if (seen[nr][nc]) continue;\n if (ss[nr][nc] == T[1 - wi]) return false;\n if (ss[nr][nc] == T[wi]) valid = true;\n if (ss[nr][nc] == '.') {\n seen[nr][nc] = true;\n que.emplace(nr, nc);\n }\n }\n }\n return valid;\n };\n int cb = 0, cw = 0;\n rep(sr, 0, H) rep(sc, 0, W) {\n if (ss[sr][sc] == '.') {\n cb += f(sr, sc, 0);\n cw += f(sr, sc, 1);\n }\n }\n output(cb, cw);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3348, "score_of_the_acc": -0.3499, "final_rank": 19 }, { "submission_id": "aoj_2014_6032110", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <utility>\n#include <string>\n#include <vector>\n#include <set>\n#include <stack>\n#include <queue>\n#include <map>\n#include <math.h>\n#include <string.h>\n#include <iomanip>\n#include <numeric>\n#include <cstdlib>\n#include <cstdint>\n#include <cmath>\n#include <functional>\n#include <limits>\n#include <cassert>\n#include <bitset>\n#include <chrono>\n#include <random>\n#include <memory>\n \n/* macro */\n \n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)\n#define all(v) (v).begin(),(v).end()\n#define rall(v) (v).rbegin(),(v).rend()\n#define Bit(n) (1LL<<(n))\n \n/* macro end */\n \n/* template */\n \nnamespace ebi {\n \n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n \nvoid debug_out() { std::cerr << std::endl; }\n \ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n \ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {\n return os << pa.first << \" \" << pa.second;\n}\n \ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, std::vector<T> vec) {\n for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n \nusing size_t = std::size_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\nusing ld = long double;\n \ntemplate<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }\n \ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b){\n a = b;\n return true;\n }\n return false;\n}\n \ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b){\n a = b;\n return true;\n }\n return false;\n}\n \ntemplate <class T>\nT gcd(T a, T b){\n if( b==0 ) return a;\n else return gcd(b, a%b);\n}\n \ntemplate <class T>\nT lcm(T a, T b) {\n a /= gcd(a, b);\n return a*b;\n}\n \ntemplate<class T>\nT Pow(T x, i64 n) {\n T res = 1;\n while(n>0){\n if(n&1) res = res *x;\n x = x*x;\n n>>=1;\n }\n return res;\n}\n \ntemplate<class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n \nconstexpr i64 LNF = std::numeric_limits<i64>::max()/4;\n \nconstexpr int INF = std::numeric_limits<int>::max()/2;\n \nconst std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};\nconst std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};\n \n}\n \n/* template end */\n\nnamespace ebi {\n\nvoid main_() {\n int w,h;\n while(std::cin >> w >> h, !(w == 0 && h == 0)) {\n std::vector<std::string> a(h);\n rep(i,0,h) std::cin >> a[i];\n std::vector seen(h, std::vector<int>(w, -1));\n std::vector table(h, std::vector<int>(w, 0));\n auto dfs = [&](auto &&self, int y, int x, int val) -> void {\n if(a[y][x] != '.') return;\n table[y][x] |= val;\n seen[y][x] = 1;\n rep(i,0,4) {\n int ny = y + dy[i];\n int nx = x + dx[i];\n if(0 <= ny && ny < h && 0 <= nx && nx < w && seen[ny][nx] < 0) {\n self(self, ny, nx, val);\n }\n }\n };\n rep(i,0,h) {\n rep(j,0,w) {\n int val = 0;\n if(a[i][j] == '.') {\n continue;\n }\n else if(a[i][j] == 'W') {\n val = 2;\n }\n else {\n val = 1;\n }\n seen = std::vector(h, std::vector<int>(w, -1));\n rep(k,0,4) {\n int ny = i + dy[k];\n int nx = j + dx[k];\n if(0 <= ny && ny < h && 0 <= nx && nx < w) {\n dfs(dfs, ny, nx, val);\n }\n }\n }\n }\n int black = 0, white = 0;\n rep(i,0,h) {\n rep(j,0,w) {\n if(table[i][j] == 1) {\n black++;\n }\n else if(table[i][j] == 2) {\n white++;\n }\n }\n }\n std::cout << black << \" \" << white << '\\n';\n }\n}\n\n}\n \nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while(t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3444, "score_of_the_acc": -0.1184, "final_rank": 4 }, { "submission_id": "aoj_2014_5975909", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nbool gridIn(int y, int x, int H, int W) {\n if (y < 0 || y >= H || x < 0 || x >= W)\n return false;\n return true;\n}\n\nint main() {\n int H, W;\n while (cin >> W >> H, H) {\n vector<string> S(H);\n rep(i, H) cin >> S[i];\n\n int ansB = 0, ansW = 0;\n rep(i, H) rep(j, W) {\n if (S[i][j] != '.') continue;\n\n bool isB = false, isW = false;\n queue<pair<int, int>> que;\n que.push({i, j});\n vector<vector<bool>> seen(H, vector<bool>(W));\n seen[i][j] = true;\n\n while (!que.empty()) {\n auto now = que.front();\n que.pop();\n\n rep(i, 4) {\n int ny = now.first + dy[i];\n int nx = now.second + dx[i];\n\n if (gridIn(ny, nx, H, W) && !seen[ny][nx]) {\n if (S[ny][nx] == '.') {\n que.push({ny, nx});\n seen[ny][nx] = true;\n } else if (S[ny][nx] == 'W')\n isW = true;\n else\n isB = true;\n }\n }\n }\n\n if (isB == isW) continue;\n if (isB) ansB++;\n if (isW) ansW++;\n }\n cout << ansB << \" \" << ansW << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3368, "score_of_the_acc": -0.2392, "final_rank": 14 }, { "submission_id": "aoj_2014_5781844", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dx[] = {1,-1,0,0};\nint dy[] = {0,0,1,-1};\n\nint main() {\n while (true) {\n int w,h;\n cin >> w >> h;\n if(w == 0) {\n return 0;\n }\n vector<vector<char>>a(h,vector<char>(w));\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n cin >> a[i][j];\n }\n }\n int B = 0,W = 0;\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n if(a[i][j] == '.') {\n queue<pair<int,int>>que;\n que.push({i,j});\n vector<vector<int>>dist(h,vector<int>(w,-1));\n dist[i][j] = 0;\n int b = 0,ww = 0;\n while (!que.empty()) {\n pair<int,int> x = que.front();\n que.pop();\n for(int k = 0; k < 4; k++) {\n int nx = x.first+dx[k];\n int ny = x.second+dy[k];\n if(nx >= 0 && nx < h && ny >= 0 && ny < w) {\n if(a[nx][ny] == '.') {\n if(dist[nx][ny] == -1) {\n dist[nx][ny] = 0;\n que.push({nx,ny});\n }\n }\n else {\n if(a[nx][ny] == 'B') {\n b++;\n }\n else {\n ww++;\n }\n }\n }\n }\n }\n if(b && !ww) {\n B++;\n }\n if(ww && !b) {\n W++;\n }\n }\n }\n }\n cout << B << \" \" << W << endl;\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3540, "score_of_the_acc": -0.1758, "final_rank": 5 }, { "submission_id": "aoj_2014_5537654", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <functional>\n#include <bitset>\n#include <cstddef>\n#include <type_traits>\n#include <vector>\n\nusing namespace std;\nconst long long int INF = numeric_limits<long long int>::max() / 4;\nconst int inf = numeric_limits<int>::max() / 4;\nconst long long int MOD1000000007 = 1000000007;\nconst long long int MOD998244353 = 998244353;\nconst double MATH_PI = 3.1415926535897932;\n\ntemplate<typename T1, typename T2>\ninline void chmin(T1 &a, const T2 &b) { if (a > b) a = b; }\n\ntemplate<typename T1, typename T2>\ninline void chmax(T1 &a, const T2 &b) { if (a < b) a = b; }\n\n#define lint long long int\n#define ALL(a) a.begin(),a.end()\n#define RALL(a) a.rbegin(),a.rend()\n#define rep(i, n) for(int i=0;i<(int)(n);i++)\n#define VI vector<int>\n#define VLL vector<long long>\n#define VC vector<char>\n#define VB vector<bool>\n#define PI pair<int, int>\n#define PLL pair<long long, long long>\n#define VPI vector<pair<int, int>>\n#define VPLL vector<pair<long long, long long>>\n#define VVI vector<vector<int>>\n#define VVPI vecor<vector<pair<int, int>>>\n#define VVPILL vector<vector<pair<int, long long>>>\n\n#define SUM(v) accumulate(ALL(v), 0LL)\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n\n\nint dfs(int i, int j, vector<vector<char>> &g, vector<vector<int>> &color) {\n int h = g.size();\n int w = g[0].size();\n int di[] = {0, -1, 0, 1};\n int dj[] = {1, 0, -1, 0};\n\n // 何らかの手段で、find_B と find_W を更新する\n bool find_B = false;\n bool find_W = false;\n // 4 方向見てる\n for (int dir = 0; dir < 4; dir++) {\n int ni = i + di[dir];\n int nj = j + dj[dir];\n\n // 再帰関数を読んで、探索\n if (ni < 0 or nj < 0 or ni >= h or nj >= w) continue;\n // 訪問済みなら continue\n if (color[ni][nj] != 0) {\n int c = color[ni][nj];\n if (c == 3 or c == 1) {\n // B が見つかってる\n find_B = true;\n }\n if (c == 3 or c == 2) {\n // W が見つかってる\n find_W = true;\n }\n continue;\n }\n if (g[ni][nj] == 'B') {\n find_B = true;\n } else if (g[ni][nj] == 'W') {\n find_W = true;\n } else {\n color[ni][nj] = 4;\n int c = dfs(ni, nj, g, color);\n if (c == 3 or c == 1) {\n // B が見つかってる\n find_B = true;\n }\n if (c == 3 or c == 2) {\n // W が見つかってる\n find_W = true;\n }\n }\n }\n\n if (find_B and find_W) {\n color[i][j] = 3;\n } else if (find_B) {\n color[i][j] = 1;\n } else if (find_W) {\n color[i][j] = 2;\n } else {\n // assert(false);\n }\n return color[i][j];\n}\nbool solve() {\n int h, w;\n cin >> h >> w;\n swap(h, w);\n if (h == 0) return false;\n\n vector g(h, vector<char>(w));\n\n bool all_dot = true;\n rep (i, h) {\n rep (j, w) {\n cin >> g[i][j];\n if (g[i][j] != '.') all_dot = false;\n }\n }\n\n if (all_dot) {\n cout << 0 << \" \" << 0 << endl;\n return true;\n }\n\n // 0 := 未訪問\n // 1 := B だけ見つかる\n // 2 := W だけ見つかる\n // 3 := BW 共に見つかる\n // 4 := 探索中 (now loading)\n int cnt_b = 0;\n int cnt_w = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (g[i][j] == '.') {\n vector color(h, vector<int>(w, 0));\n color[i][j] = 4;\n int c = dfs(i, j, g, color);\n if (c == 1) {\n cnt_b++;\n }\n if (c == 2) {\n cnt_w++;\n }\n }\n }\n }\n cout << cnt_b << \" \" << cnt_w << endl;\n\n return true;\n}\n\nint main() {\n\n while (solve()) {}\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3396, "score_of_the_acc": -0.1064, "final_rank": 3 }, { "submission_id": "aoj_2014_5243872", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//#include <atcoder/all>\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vpll = vector<pair<ll, ll>>;\n#define rep(i,n) for (ll i = 0; i < (n); i++)\n#define all(v) (v).begin(), (v).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\n#define pb push_back\n#define endl '\\n'\n#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);\nconst int inf = 1<<30;\nconst ll INF = 1ll<<60;\n//const ll mod = 1000000007;\nconst ll mod = 998244353;\nconst ll dx[4] = {1, 0, -1, 0};\nconst ll dy[4] = {0, 1, 0, -1};\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\n\ntemplate<class T> void disp1(vector<T> &array, bool blank=1) {\n rep(i, array.size()) cout << ((i&&blank) ? \" \" : \"\") << array[i];\n cout << endl;\n //cout << \" \";\n}\ntemplate<class T> void disp2(vector<vector<T>> &array, bool blank=1) {\n rep(i, array.size()) disp1(array[i], blank);\n}\n\nll rem(ll a, ll b) { return (a % b + b) % b; } // 余りを0~b-1に丸める\n// 小数の入力が文字で与えられたとき、10^n倍したものを返す\nll x10(string str_f, ll n) {\n if (str_f[0] == '-') return -x10(str_f.substr(1), n);\n ll times = 1; rep(i,n) times *= 10; // times = 10^n\n int id = -1; rep(i,str_f.size()) if (str_f[i] == '.') id = i;\n if (id == -1) { return stoll(str_f)*times; }\n string str_int = str_f.substr(0, id), str_dec = str_f.substr(id+1);\n while (str_dec.size() < n) str_dec += '0';\n return stoll(str_int)*times + stoll(str_dec);\n}\n// A^N (mod)\nll modpow(ll A, ll N, ll mod) {\n ll RES = 1;\n while (N) {\n if (N & 1) RES = RES * A % mod;\n A = A * A % mod;\n N >>= 1;\n }\n return RES;\n}\n\n//using mint = modint998244353;\n\nvvll bfs(vector<string> &s, vvll sta, ll x, ll y) {\n ll h = sz(s), w = sz(s[0]);\n vvll seen(h, vll(w, 0));\n queue<pll> que;\n que.push({x, y});\n while (que.size()) {\n pll p = que.front(); que.pop();\n ll cx = p.fi, cy = p.se;\n if (seen[cx][cy]) continue;\n seen[cx][cy] = 1;\n rep(i,4) {\n ll nx = cx+dx[i], ny = cy+dy[i];\n if (0 <= nx && nx < h && 0 <= ny && ny < w && seen[nx][ny] == 0 && s[nx][ny] == '.') {\n sta[nx][ny]++;\n que.push({nx, ny});\n }\n }\n }\n return sta;\n}\n\nint main() {\n ll w, h;\n while (cin >> w >> h, w) {\n vector<string> s(h);\n // bla[i][j] = (i,j)に到達可能なマスの中にBがあれば1, なければ0\n vvll bla(h, vll(w, 0)), whi(h, vll(w, 0));\n rep(i,h) cin >> s[i];\n rep(i,h) rep(j,w) {\n if (s[i][j] == 'W') {\n whi = bfs(s, whi, i, j);\n } else if (s[i][j] == 'B') {\n bla = bfs(s, bla, i, j);\n }\n }\n ll bb = 0, ww = 0;\n rep(i,h) rep(j,w) {\n if (whi[i][j] > 0 && bla[i][j] == 0) ww++;\n if (bla[i][j] > 0 && whi[i][j] == 0) bb++;\n }\n printf(\"%lld %lld\\n\", bb, ww);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3164, "score_of_the_acc": -0.2131, "final_rank": 11 }, { "submission_id": "aoj_2014_4961902", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld;\n#define ALL(x) (x).begin(), (x).end()\n#define SZ(x) ((lint)(x).size())\n#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)\n#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)\n#define REP(i, n) FOR(i,0,n)\n#define IREP(i, n) IFOR(i,0,n)\n#define endk '\\n'\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\nlint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); }\nlint ceil(lint a, lint b) { return (a + b - 1) / b; }\nlint digit(lint a) { return (lint)log10(a); }\nconst lint MOD = 998244353, INF = 1e18;\nlint dx[8] = { 0, -1, 1, 0, 1, -1, 1, -1 }, dy[8] = { 1, 0, 0, -1, -1, -1, 1, 1 };\nvoid YN(bool flag) { cout << (flag ? \"Yes\" : \"No\") << endk; }\ntypedef pair<long double, lint> Pa;\ntypedef pair<lint, plint> tlint;\nstruct point {\n\tlint r;\n\tlint x, y;\n};\n\nlint W, H;\nint main() {\n\twhile (true) {\n\t\tcin >> W >> H;\n\t\tif (W == 0 && H == 0) break;\n\t\tvector<vector<lint> > mp(H, vector<lint>(W));\n\t\tvector<vector<lint> > dp(H, vector<lint>(W));\n\t\tREP(i, H) {\n\t\t\tREP(j, W) {\n\t\t\t\tchar c;\n\t\t\t\tcin >> c;\n\t\t\t\tif (c == '.') mp[i][j] = 0;\n\t\t\t\tif (c == 'B') mp[i][j] = 1;\n\t\t\t\tif (c == 'W') mp[i][j] = 2;\n\t\t\t}\n\t\t}\n\t\tREP(i, H) {\n\t\t\tREP(j, W) {\n\t\t\t\tif (!mp[i][j]) continue;\n\t\t\t\tqueue<plint> que;\n\t\t\t\tque.push({ i, j });\n\t\t\t\twhile (!que.empty()) {\n\t\t\t\t\tplint curr = que.front(); que.pop();\n\t\t\t\t\tREP(k, 4) {\n\t\t\t\t\t\tlint nh = curr.first + dy[k], nw = curr.second + dx[k];\n\t\t\t\t\t\tif (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;\n\t\t\t\t\t\tif (mp[nh][nw] != 0) continue;\n\t\t\t\t\t\tif ((dp[nh][nw] & mp[i][j]) != 0) continue;\n\t\t\t\t\t\tdp[nh][nw] |= mp[i][j];\n\t\t\t\t\t\tque.push({ nh, nw });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tlint cnt[2] = { 0 };\n\t\tREP(i, H) {\n\t\t\tREP(j, W) {\n\t\t\t\tif (dp[i][j] == 1) cnt[0]++;\n\t\t\t\tif (dp[i][j] == 2) cnt[1]++;\n\t\t\t}\n\t\t}\n\t\tcout << cnt[0] << \" \" << cnt[1] << endk;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3192, "score_of_the_acc": -0.0025, "final_rank": 2 }, { "submission_id": "aoj_2014_4946668", "code_snippet": "#include <bits/stdc++.h>\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n//#include <boost/multiprecision/cpp_int.hpp>\nusing namespace std;\n\n//using namespace boost::multiprecision;\n//#include<atcoder/all>\n//using namespace atcoder;\n\nusing dou =long double;\nstring yes=\"yes\";\nstring Yes=\"Yes\";\nstring YES=\"YES\";\nstring no=\"no\";\nstring No=\"No\";\nstring NO=\"NO\";\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> PL;\n\nconst ll mod = 998244353ll;\n//const ll mod = 1000000007ll;\n//const ll mod = 100000;\n\n\nstruct mint {\n ll x; // typedef long long ll;\n mint(ll x=0):x((x%mod+mod)%mod){}\n mint operator-() const { return mint(-x);}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += mod-a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}\n mint operator+(const mint a) const { return mint(*this) += a;}\n mint operator-(const mint a) const { return mint(*this) -= a;}\n mint operator*(const mint a) const { return mint(*this) *= a;}\n mint pow(ll t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\n // for prime modhttps://atcoder.jp/contests/abc166/submit?taskScreenName=abc166_f\n mint inv() const { return pow(mod-2);}\n mint& operator/=(const mint a) { return *this *= a.inv();}\n mint operator/(const mint a) const { return mint(*this) /= a;}\n};\n\nistream& operator>>(istream& is, const mint& a) { return is >> a.x;}\nostream& operator<<(ostream& os, const mint& a) { return os << a.x;}\n\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n#define brep(n) for(int bit=0;bit<(1<<n);bit++)\n#define bbrep(n) for(int bbit=0;bbit<(1<<n);bbit++)\n#define erep(i,container) for (auto &i : container)\n#define itrep(i,container) for (auto i : container)\n#define irep(i, n) for(ll i = n-1; i >= (ll)0ll; i--)\n#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)\n#define reprep(i,j,h,w) rep(i,h)rep(j,w)\n#define repreprep(i,j,k,h,w,n) rep(i,h)rep(j,w)rep(k,n)\n#define all(x) (x).begin(),(x).end()\n#define rall(x) (x).rbegin(),(x).rend()\n#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];\n#define pb push_back\n#define pf push_front\n#define query int qq;std::cin >> qq;rep(qqq,qq)\n#define lb lower_bound\n#define ub upper_bound\n#define fi first\n#define se second\n#define itn int\n#define mp make_pair\n//#define sum(a) accumulate(all(a),0ll)\n#define keta fixed<<setprecision\n#define vout(a) erep(qxqxqx,a)std::cout << qxqxqx << ' ';std::cout << std::endl;\n#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))\n//#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))\n#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));\n#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));\n#define case std::cout <<\"Case #\" <<qqq+1<<\":\"\n#define RES(a,i,j) a.resize(i);rep(ii,i)a[ii].resize(j);\n#define RESRES(a,i,j,k) a.resize(i);rep(ii,i)a[ii].resize(j);reprep(ii,jj,i,j){dp[ii][jj].resize(k)};\n#define res resize\n#define as assign\n#define ffor for(;;)\n#define ppri(a,b) std::cout << a<<\" \"<<b << std::endl\n#define pppri(a,b,c) std::cout << a<<\" \"<<b <<\" \"<< c<<std::endl\n#define ppppri(a,b,c,d) std::cout << a<<\" \"<<b <<\" \"<< c<<' '<<d<<std::endl\n#define aall(x,n) (x).begin(),(x).begin()+(n)\n#define SUM(a) accumulate(all(a),0ll) \n#define stirng string\n#define gin(a,b) int a,b;std::cin >> a>>b;a--;b--;\n#define popcount __builtin_popcount\n#define permu(a) next_permutation(all(a))\n#define aru(a,d) a.find(d)!=a.end()\n\n//#define grid_input(a,type) int h,w;std::cin >> h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];\n\n//typedef long long T;\nll ceili(ll a,ll b){\n return ((a+b-1)/b);\n}\nconst int INF = 2'000'000'000;\nconst ll INF64 =32233720854775807ll;\n//const ll INF64 = 9223372036854775807ll;\n\n//const ll INF64 = 243'000'000'000'000'000'0;\n\n\n//const ll MOD = 100'000'000'7ll;\nconst ll MOD = 998244353ll;\n//const ll MOD = 1000003ll;\nconst ll OD = 1000000000000007ll;\nconst dou pi=3.141592653589793;\n\nlong long modpow(long long a, long long n) { //累乗の剰余\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % MOD;\n a = a * a % MOD;\n n >>= 1;\n }\n return res;\n}\n\n//メモ\n//ゲーム(Grundy数とか)の復習をする\n//リスニング力をどうにかする\n//モノイドをやる!!!!\n//特に理由がなければllを使おうキャンペーン\n//個数制限付きナップサックの復習\n//戻すDP\n//周期性の解析結果の考察をする\n//いい加減英語スライドを作り始める\n//改良モデルを深堀りすると良さそう\n//被服調査票を提出する\n//PGバトル 10/24 13:00~\n//学科棟まで書類を受け取りに行く\n//全方位木DPとスライド最小値\n//やること困ったらSRM 500番台やるといいかも\n\n\nint h,w;\nbool hani(int i,int j){//grid上探索において範囲内かどうかをreturn\n if(i>=0&&i<h&&j>=0&&j<w)return true;\n else return false;\n}\n\nstd::vector<int> d{-1,0,1,0,-1};\nint main(){\n ffor{\n std::cin >> w>>h;\n if(w==0)break;\n vvector(a,char,h,w,0);\n reprep(i,j,h,w)std::cin >> a[i][j];\n reprep(i,j,h,w){\n if(a[i][j]=='.'){\n queue<P>q;\n q.push(P(i,j));\n bool ww=0,b=0;\n while(!q.empty()){\n P p=q.front();\n a[p.fi][p.se]='0';\n q.pop();\n rep(ii,4){\n int y=p.fi+d[ii];\n int x=p.se+d[ii+1];\n if(!hani(y,x))continue;\n if(a[y][x]=='.'){\n q.push(P(y,x));\n a[y][x]='0';\n }\n if(a[y][x]=='W'){\n ww=1;\n }\n if(a[y][x]=='B'){\n b=1;\n }\n }\n \n }\n \n if(ww||b){\n if(!(ww&&b)){\n char c;\n if(ww)c='w';\n else c='b';\n reprep(ii,jj,h,w)if(a[ii][jj]=='0')a[ii][jj]=c;\n }\n }\n reprep(ii,jj,h,w)if(a[ii][jj]=='0')a[ii][jj]='1';\n \n \n }\n }\n int ww=0,b=0;\n // ppri(h,w);\n reprep(i,j,h,w){\n if(a[i][j]=='w')ww++;\n if(a[i][j]=='b')b++;\n }\n \n //std::cout << std::endl;\n ppri(b,ww);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3156, "score_of_the_acc": -0.0018, "final_rank": 1 }, { "submission_id": "aoj_2014_4925784", "code_snippet": "#include <bits/stdc++.h>\nstruct __fastio__ { __fastio__ () { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } __fast_io__;\ntemplate<class T, class U> bool chmin (T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax (T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> void print (const T &value) { std::cout << value << '\\n'; }\ntemplate<class T, class... Args> void print (const T &value, const Args& ...args) { std::cout << value << ' '; print(args...); }\ntemplate<class T> void input (T &x) { std::cin >> x; }\ntemplate<class T> void input (std::vector<T> &v) { for (T &e : v) input(e); }\ntemplate<class T, class... Args> void input (T &x, Args&... args) { input(x); input(args...); }\ntemplate<class T> bool zero (const T &x) { return (x == (T)0); }\ntemplate<class T, class... Args> bool zero (const T &x, const Args& ...args) { return (x == 0 and zero(args...)); }\ntemplate<class T> using matrix = std::vector<std::vector<T>>;\nusing i32 = std::int_fast32_t;\nusing u32 = std::uint_fast32_t;\nusing i64 = std::int_fast64_t;\nusing u64 = std::uint_fast64_t;\nusing isize = std::ptrdiff_t;\nusing usize = std::size_t;\n\nconst std::pair<isize, isize> delta[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n\nvoid solve (isize w, isize h) {\n\tstd::vector<std::string> s(h);\n\tfor (auto &e : s) std::cin >> e;\n\t\n\tauto inside = [&] (isize i, isize j) {\n\t\treturn (0 <= i and i < h and 0 <= j and j < w);\n\t};\n\tauto bfs = [&] (isize si, isize sj) -> i32 {\n\t\tstd::queue<std::pair<isize, isize>> que({{si, sj}});\n\t\tstd::vector<std::vector<bool>> seen(h, std::vector<bool>(w, false));\n\t\tbool black = false, white = false;\n\t\twhile (not que.empty()) {\n\t\t\tauto [x, y] = que.front(); que.pop();\n\t\t\tif (seen[x][y]) continue;\n\t\t\tseen[x][y] = true;\n\t\t\tfor (auto [dx, dy] : delta) {\n\t\t\t\tisize i = x + dx, j = y + dy;\n\t\t\t\tif (not inside(i, j) or seen[i][j]) continue;\n\t\t\t\tif (s[i][j] == '.') que.emplace(i, j);\n\t\t\t\tif (s[i][j] == 'B') black = true;\n\t\t\t\tif (s[i][j] == 'W') white = true;\n\t\t\t}\n\t\t}\n\t\tif (black and white) return 0;\n\t\tif (black) return 1;\n\t\tif (white) return 2;\n\t\treturn 3;\n\t};\n\t\n\tusize black = 0, white = 0;\n\tfor (isize i = 0; i < h; i++) for (isize j = 0; j < w; j++) {\n\t\tif (s[i][j] == '.') {\n\t\t\ti32 type = bfs(i, j);\n\t\t\tif (type == 1) black++;\n\t\t\tif (type == 2) white++;\n\t\t}\n\t}\n\t\n\tstd::cout << black << ' ' << white << '\\n';\n\treturn;\n}\n\nint main() {\n\twhile (true) {\n\t\tisize w, h;\n\t\tstd::cin >> w >> h;\n\t\tif (w == 0 and h == 0) break;\n\t\telse solve(w, h);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3416, "score_of_the_acc": -0.2623, "final_rank": 15 }, { "submission_id": "aoj_2014_4922032", "code_snippet": "#include<iostream>\n#include<fstream>\n#include<bitset>\n#include<string>\n#include<vector>\n#include<list>\n#include<algorithm>\n#include<cmath>\n#include<map>\n#include<set>\n#include<iomanip>\n#include<queue>\n#include<stack>\n#include<numeric>\n#include<utility>\n#include<regex>\n#include<climits>\n#include<ctime>\n#include<functional>\n\nvoid Init() {\n\tstd::cin.tie(0); std::ios::sync_with_stdio(false);\n\tstruct Init_caller { Init_caller() { Init(); } }caller;\n}\n\nusing namespace std;\n\n#define int long long\n#define rep(i,n) for(LL i=0;i<(n);i++)\n#define rep1(i,n) for(LL i=1;i<(n);i++)\n#define rrep(i,n) for(LL i=(n)-1;i>=0;i--)\n#define all(a) (a).begin(),(a).end()\n#define check() cout<<\"! ! !\"<<endl\n#define endl \"\\n\"\n#define _y() cout<<\"Yes\"<<endl\n#define _Y() cout<<\"YES\"<<endl\n#define _n() cout<<\"No\"<<endl\n#define _N() cout<<\"NO\"<<endl\n\n\nconstexpr long double PI = 3.141592653589793238;\nconstexpr long long INF = (long long)1e15;\nconstexpr long long MOD = 1e9 + 7;\n\n\nusing LL = long long;\nusing st = string;\ntemplate<typename T>using vec = vector<T>;\ntemplate<typename T>using vvec = vec<vec<T>>;\ntemplate<typename T>using vvvec = vec<vvec<T>>;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vs = vector<st>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing pi = pair<int, int>;\nusing ppi = pair<pi, int>;\nusing vpi = vector<pi>;\nusing vppi = vector<ppi>;\nusing mii = map<int, int>;\nusing mpii = map<pi, int>;\nusing mib = map<int, bool>;\nusing mci = map<char, int>;\ntemplate<typename T>using pq_greater = priority_queue<T>;\ntemplate<typename T>using pq_smaller = priority_queue<T, vec<T>, greater<T>>;\n\n//0,1,2\n//3,4,5\n//6,7,8\nint dx4[] = { 0, -1,1,0 };\nint dy4[] = { -1,0,0,1 };\nint dx9[] = { -1,0,1,-1,0,1,-1,0,1 };\nint dy9[] = { -1,-1,-1,0,0,0,1,1,1 };\n\ntemplate<typename T>\nistream &operator>>(istream &in, vector<T> &v) {\n\trep(i, v.size()) {\n\t\tin >> v[i];\n\t}\n\treturn in;\n}\n\ntemplate<typename T, typename U>\nistream &operator>>(istream&in, pair<T, U> &p) {\n\n\tin >> p.first >> p.second;\n\treturn in;\n}\n\ntemplate<typename T>\nostream &operator<<(ostream &out, vector<T> &v) {\n\tout << \"{ \";\n\trep(i, v.size()) {\n\t\tout << v[i] << \" \";\n\t}\n\tout << \" }\";\n\treturn out;\n}\n\ntemplate<typename T, typename U>\nostream &operator<<(ostream&out, pair<T, U> &p) {\n\n\tout << \"(\" << p.first << \",\" << p.second << \")\" << endl;\n\treturn out;\n}\n\nstruct mint {\n\tint x;\n\tmint(int x = 0) :x((x%MOD + MOD) % MOD) {}\n\n\tmint operator-() const { return mint(-x); }\n\tmint& operator+=(const mint a) {\n\t\tif ((x += a.x) >= MOD) x -= MOD;\n\t\treturn *this;\n\t}\n\tmint& operator-=(const mint a) {\n\t\tif ((x += MOD - a.x) >= MOD) x -= MOD;\n\t\treturn *this;\n\t}\n\tmint& operator*=(const mint a) {\n\t\t(x *= a.x) %= MOD;\n\t\treturn *this;\n\t}\n\tmint operator+(const mint a) const {\n\t\tmint res(*this);\n\t\treturn res += a;\n\t}\n\tmint operator-(const mint a) const {\n\t\tmint res(*this);\n\t\treturn res -= a;\n\t}\n\tmint operator*(const mint a) const {\n\t\tmint res(*this);\n\t\treturn res *= a;\n\t}\n\tmint pow(int t) const {\n\t\tif (!t) return 1;\n\t\tmint a = pow(t >> 1);\n\t\ta *= a;\n\t\tif (t & 1) a *= *this;\n\t\treturn a;\n\t}\n\n\tmint inv() const {\n\t\treturn pow(MOD - 2);\n\t}\n\tmint& operator/=(const mint a) {\n\t\treturn (*this) *= a.inv();\n\t}\n\tmint operator/(const mint a) const {\n\t\tmint res(*this);\n\t\treturn res /= a;\n\t}\n\n\tfriend ostream& operator<<(ostream& out, const mint& m) {\n\t\tout << m.x;\n\t\treturn out;\n\t}\n\n};\n\nvoid y_n(bool p) {\n\tp ? _y() : _n();\n}\n\nvoid Y_N(bool p) {\n\tp ? _Y() : _N();\n}\n\n//MAX,maxpos,MIN,minpos\ntemplate<typename T>\npair<pair<T, int>, pair<T, int>> vmaxmin(vector<T> v) {\n\tint MAX = -INF;\n\tint MIN = INF;\n\tint maxpos = 0;\n\tint minpos = 0;\n\trep(i, v.size()) {\n\t\tif (MAX < v[i]) {\n\t\t\tmaxpos = i;\n\t\t\tMAX = v[i];\n\t\t}\n\t\tif (MIN > v[i]) {\n\t\t\tminpos = i;\n\t\t\tMIN = v[i];\n\t\t}\n\t}\n\treturn { {MAX,maxpos},{MIN,minpos} };\n}\n\ntemplate<typename T>\nT vsum(vector<T> v) {\n\tT sum = 0;\n\trep(i, v.size()) {\n\t\tsum += v[i];\n\t}\n\treturn sum;\n}\n\nint gcd(int a, int b) {\n\tif (b == 0) {\n\t\tswap(a, b);\n\t}\n\tint r;\n\twhile ((r = a % b) != 0) {\n\t\ta = b;\n\t\tb = r;\n\t}\n\treturn b;\n}\n\nint lcm(int a, int b) {\n\treturn (a / gcd(a, b) * b);\n}\n\nbool is_square(int n) {\n\tif ((int)sqrt(n)*(int)sqrt(n) == n) {\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\n\nbool is_prime(int n) {\n\tif (n <= 1) {\n\t\treturn false;\n\t}\n\telse {\n\t\tfor (int i = 2; i*i <= n; i++) {\n\t\t\tif (n % i == 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\treturn true;\n}\n\nvoid dec_num(int n, vi &v) {\n\tint a = 2;\n\tv.push_back(1);\n\tv.push_back(n);\n\twhile (a*a <= n) {\n\t\tif (n%a == 0) {\n\t\t\tv.push_back(a);\n\t\t\tv.push_back(n / a);\n\t\t}\n\t\ta++;\n\t}\n\tsort(all(v));\n\tv.erase(unique(all(v)), v.end());\n}\n\n\nvoid dec_prime(int n, vi &v) {\n\t//v.push_back(1);\n\tint a = 2;\n\twhile (a*a <= n) {\n\t\tif (n % a == 0) {\n\t\t\tv.push_back(a);\n\t\t\tn /= a;\n\t\t}\n\t\telse {\n\t\t\ta++;\n\t\t}\n\t}\n\tv.push_back(n);\n}\n\n//n= ...*pow(p_i,m[p_i])*...\nvoid dec_prime_e(int n, map<int, int> &m) {\n\tfor (int i = 2; i*i <= n; i++) {\n\t\tif (n%i == 0) {\n\t\t\tint e = 0;\n\t\t\twhile (n%i == 0) {\n\t\t\t\te++;\n\t\t\t\tn /= i;\n\t\t\t}\n\t\t\tm[i] += e;\n\t\t}\n\t}\n\tif (n != 1) m[n]++;\n}\n\n\nint sieve_prime(int a, int b) {\n\tif (a > b)swap(a, b);\n\tvb s(b + 1, true);\n\tint cnt_a = 0, cnt_b = 0;\n\tfor (int i = 2; i <= b; i++) {\n\t\tfor (int j = 2; i*j <= b; j++) {\n\t\t\ts[i*j] = false;\n\t\t}\n\t}\n\t//return s;\n\tfor (int i = 2; i <= b; i++) {\n\t\tif (s[i]) {\n\t\t\t//cout << i << \" \";\n\t\t\tif (i < a) {\n\t\t\t\tcnt_a++;\n\t\t\t}\n\t\t\tcnt_b++;\n\t\t}\n\t}\n\treturn cnt_b - cnt_a;\n\n}\n\n\nint factorial(int n) {\n\tint a = 1, ret = 1;\n\twhile (a < n) {\n\t\ta++;\n\t\tret *= a;\n\t\t//ret %= MOD;\n\t}\n\treturn ret;\n}\n\nint bit_count(int n) {\n\tint k = (int)log2(n) + 1;\n\tint ret = 0;\n\trep(i, k) {\n\t\tif (n&(1LL << i))ret++;\n\t}\n\treturn ret;\n}\n\n//a^(a+1)^(a+2)^...^b\nint XOR(int a, int b) {\n\tif (a == 0) {\n\t\tswitch (b % 4) {\n\t\tcase 0:\treturn b;\n\t\tcase 1:\treturn 1;\n\t\tcase 2:\treturn (b + 1);\n\t\tcase 3:\treturn 0;\n\t\t}\n\t}\n\tint A = XOR(0, a - 1);\n\tint B = XOR(0, b);\n\treturn (A ^ B);\n}\n\n\nconst int COMBMAX = 4000;\nint comb[COMBMAX + 5][COMBMAX + 5];\n\nvoid init_comb() {\n\tcomb[0][0] = 1;\n\trep(i, COMBMAX) {\n\t\trep(j, i + 1) {\n\t\t\tcomb[i + 1][j] += comb[i][j];\n\t\t\t//comb[i + 1][j] %= MOD;\n\t\t\tcomb[i + 1][j + 1] += comb[i][j];\n\t\t\t//comb[i + 1][j + 1] %= MOD;\n\t\t}\n\t}\n}\n\nint combination(int n, int k) {\n\tif (k<0 || k>n)return 0;\n\telse return comb[n][k];\n}\n\n\nconst int COMBMODMAX = 5000000;\nint fac[COMBMODMAX + 5], facinv[COMBMODMAX + 5], inv[COMBMODMAX + 5];\n\nvoid init_comb_mod() {\n\tfac[0] = fac[1] = 1;\n\tfacinv[0] = facinv[1] = 1;\n\tinv[1] = 1;\n\tfor (int i = 2; i < COMBMODMAX; i++) {\n\t\tfac[i] = fac[i - 1] * i%MOD;\n\t\tinv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n\t\tfacinv[i] = facinv[i - 1] * inv[i] % MOD;\n\t}\n}\n\n//nCk (mod p)\n//nHk = n+k-1Ck\nint comb_mod(int n, int k) {\n\tif (n < k)return 0;\n\tif (n < 0 || k < 0)return 0;\n\treturn fac[n] * (facinv[k] * facinv[n - k] % MOD) % MOD;\n}\n\n//x^n (mod p)\nint pow_mod(int x, int n, int p) {\n\tif (n == 0)return 1;\n\tint res = pow_mod(x*x%p, n / 2, p);\n\tif (n % 2 == 1)res = res * x % p;\n\treturn res;\n}\n\nstruct _p {\n\tint x, y;\n\n};\n\nstruct _t{\n\tint x, y, z;\n\n};\n\nstruct edge {\n\tint to;\n\tint cost;\n};\n\n\nusing ve = vector<edge>;\nusing vve = vector<ve>;\n\n\n/*****************************************************************************/\n\nsigned main() {\n\n\tint w,h;\n\twhile (cin >> w >> h, w || h) {\n\n\t\tvvc f(h, vc(w));\n\t\t\n\t\tcin >> f;\n\n\t\tvvi B(h, vi(w, 0));\n\t\tvvi W(h, vi(w, 0));\n\t\trep(i, h) {\n\t\t\trep(j, w) {\n\t\t\t\tif (f[i][j] != '.') {\n\t\t\t\t\tqueue<_t>q;\n\t\t\t\t\tq.push({ j,i,f[i][j] == 'B' ? 1 : 0 });\n\t\t\t\t\tvvb visited(h, (vb(w, false)));\n\t\t\t\t\tvisited[i][j] = true;\n\t\t\t\t\twhile (!q.empty()) {\n\t\t\t\t\t\t_t t = q.front();\n\t\t\t\t\t\tq.pop();\n\t\t\t\t\t\trep(k, 4) {\n\t\t\t\t\t\t\tint nx = t.x + dx4[k], ny = t.y + dy4[k];\n\t\t\t\t\t\t\tif (nx < 0 || nx >= w || ny < 0 || ny >= h || visited[ny][nx] || f[ny][nx]!='.')continue;\n\t\t\t\t\t\t\tvisited[ny][nx] = true;\n\t\t\t\t\t\t\tq.push({ nx,ny,t.z });\n\t\t\t\t\t\t\tif (t.z == 1) {\n\t\t\t\t\t\t\t\tB[ny][nx] = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tW[ny][nx] = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//cout << B << endl << W;\n\t\tint a = 0, b = 0;\n\t\trep(i, h) {\n\t\t\trep(j, w) {\n\t\t\t\tif (B[i][j] && !W[i][j])a++;\n\t\t\t\tif (!B[i][j] && W[i][j])b++;\n\t\t\t}\n\t\t}\n\t\tcout << a << \" \" << b << endl;\n\t}\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3156, "score_of_the_acc": -0.224, "final_rank": 13 }, { "submission_id": "aoj_2014_4886067", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n#define rep(i,n) for(int i=0; i<(int)(n); i++)\nusing P = pair<int, int>;\n\nint dy[] = {1, 0, -1, 0};\nint dx[] = {0, 1, 0, -1};\n\nint calc(const vector<vector<char>> &grid, int sy, int sx)\n{\n if(grid[sy][sx] != '.') return 0;\n int h = grid.size(), w = grid[0].size();\n vector<vector<int>> checked(h, vector<int>(w, 0));\n\n bool br = false, wh = false;\n queue<P> que;\n que.push(P(sy, sx));\n checked[sy][sx] = 1;\n while(!que.empty() && (!br || !wh))\n {\n auto p = que.front();\n que.pop();\n int y = p.first, x = p.second;\n rep(i,4)\n {\n int ny = y+dy[i], nx = x+dx[i];\n if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;\n if(checked[ny][nx]) continue;\n checked[ny][nx] = 1;\n if(grid[ny][nx] == '.')\n {\n que.push(P(ny, nx));\n }\n else if(grid[ny][nx] == 'B')\n {\n br = true;\n }\n else if(grid[ny][nx] == 'W')\n {\n wh = true;\n }\n }\n }\n if(br == wh) return 0;\n else if(br) return 1;\n else if(wh) return 2;\n}\n\nint solve()\n{\n int w, h;\n cin >> w >> h;\n if(w == 0) return 0;\n vector<vector<char>> grid(h, vector<char>(w));\n rep(i,h) rep(j,w) cin >> grid[i][j];\n int br = 0, wh = 0;\n rep(i,h)\n {\n rep(j,w)\n {\n int f = calc(grid, i, j);\n if(f == 1) br++;\n if(f == 2) wh++;\n }\n }\n cout << br << \" \" << wh << endl;\n\n return 1;\n}\n\nint main()\n{\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3060, "score_of_the_acc": -0.1889, "final_rank": 6 }, { "submission_id": "aoj_2014_4886058", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n#define rep(i,n) for(int i=0; i<(int)(n); i++)\nusing P = pair<int, int>;\n\nint dy[] = {1, 0, -1, 0};\nint dx[] = {0, 1, 0, -1};\n\nint calc(const vector<vector<char>> &grid, int sy, int sx)\n{\n if(grid[sy][sx] != '.') return 0;\n int h = grid.size(), w = grid[0].size();\n vector<vector<int>> checked(h, vector<int>(w, 0));\n\n bool br = false, wh = false;\n queue<P> que;\n que.push(P(sy, sx));\n checked[sy][sx] = 1;\n while(!que.empty())\n {\n auto p = que.front();\n que.pop();\n int y = p.first, x = p.second;\n rep(i,4)\n {\n int ny = y+dy[i], nx = x+dx[i];\n if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;\n if(checked[ny][nx]) continue;\n checked[ny][nx] = 1;\n if(grid[ny][nx] == '.')\n {\n que.push(P(ny, nx));\n }\n else if(grid[ny][nx] == 'B')\n {\n br = true;\n }\n else if(grid[ny][nx] == 'W')\n {\n wh = true;\n }\n }\n }\n if(br == wh) return 0;\n else if(br) return 1;\n else if(wh) return 2;\n}\n\nint solve()\n{\n int w, h;\n cin >> w >> h;\n if(w == 0) return 0;\n vector<vector<char>> grid(h, vector<char>(w));\n rep(i,h) rep(j,w) cin >> grid[i][j];\n int br = 0, wh = 0;\n rep(i,h)\n {\n rep(j,w)\n {\n int f = calc(grid, i, j);\n if(f == 1) br++;\n if(f == 2) wh++;\n }\n }\n cout << br << \" \" << wh << endl;\n\n return 1;\n}\n\nint main()\n{\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3112, "score_of_the_acc": -0.201, "final_rank": 9 }, { "submission_id": "aoj_2014_4879707", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n// #define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) {\n os << ',';\n }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\n//-------------------------------------\n\nbool aoj = true;\n\nvoid solve() {\n int w, h;\n cin >> w >> h;\n if(w == 0) {\n aoj = false;\n return;\n }\n vector<string> G(h);\n for(int i = 0; i < h; i++) {\n cin >> G[i];\n }\n\n vector black(h, vector(w, false));\n vector white(h, vector(w, false));\n\n auto bfs = [&](int si, int sj, vector<vector<bool>> &used) {\n queue<pair<int, int>> que;\n if(used[si][sj]) {\n return;\n }\n que.emplace(si, sj);\n while(que.size()) {\n auto [nowi, nowj] = que.front();\n que.pop();\n used[nowi][nowj] = true;\n\n for(int r = 0; r < 4; r++) {\n int nxti = nowi + dy[r];\n int nxtj = nowj + dx[r];\n if(0 <= nxti && nxti < h && 0 <= nxtj && nxtj < w) {\n if(G[nxti][nxtj] == '.' && !used[nxti][nxtj]) {\n que.emplace(nxti, nxtj);\n }\n }\n }\n }\n };\n\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n if(G[i][j] == 'B') {\n bfs(i, j, black);\n } else if(G[i][j] == 'W') {\n bfs(i, j, white);\n }\n }\n }\n\n int ans[2] = {0, 0};\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n if(black[i][j] && !white[i][j]) {\n ans[0]++;\n } else if(!black[i][j] && white[i][j]) {\n ans[1]++;\n }\n if(G[i][j] == 'B') {\n ans[0]--;\n } else if(G[i][j] == 'W') {\n ans[1]--;\n }\n }\n }\n cout << ans[0] << \" \" << ans[1] << endl;\n}\n\nint main() {\n while(aoj) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 55868, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2014_4847850", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define REP(i, n) for(int (i)=0; (i)< (n); ++i)\n#define REPR(i, n) for(int (i)=(n); (i)>=0; --i)\n#define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i)\nconstexpr int INF = 1e9;\n//constexpr ll INF = 1LL << 61;\nconstexpr int mod = 1e9+7;\n\nint dx[] = {1, 0, -1, 0};\nint dy[] = {0, 1, 0, -1};\nvoid solve(int w, int h){\n vector<string> a(h);\n REP(i, h){\n cin >> a[i];\n }\n int black =0, white=0;\n auto check = [&](int sy, int sx){\n queue<pair<int, int>> que;\n vector<vector<bool>> homon(h, vector<bool>(w, false)); \n que.push({sy, sx});\n homon[sy][sx] = true;\n int res = -1;\n while(!que.empty()){\n auto [y, x] = que.front(); que.pop();\n REP(i, 4){\n int ny = y + dy[i], nx = x + dx[i];\n if(ny < 0 || h <= ny || nx < 0 || w <= nx) continue;\n if(homon[ny][nx]) continue;\n if(res == -1 && a[ny][nx] == 'B'){\n res = 1;\n }\n else if(res==-1 && a[ny][nx] == 'W'){\n res =2;\n }\n else if((res == 1 && a[ny][nx] == 'W') || (res == 2 && a[ny][nx] == 'B')){\n res = 0;\n return res;\n }\n if(a[ny][nx] == '.'){\n que.push({ny, nx});\n homon[ny][nx] = true;\n }\n }\n }\n return res;\n };\n REP(cy, h){\n REP(cx, w){\n if(a[cy][cx] != '.') continue;\n int res = check(cy, cx);\n if(res ==1){\n black++;\n }\n else if(res == 2){\n white++;\n }\n }\n }\n cout << black << \" \" << white << endl;\n}\n\nint main(){\n while(true){\n int w, h;\n cin >> w >> h;\n if(w==0 && h==0){\n break;\n }\n solve(w, h);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3268, "score_of_the_acc": -0.215, "final_rank": 12 } ]
aoj_2012_cpp
Space Coconut Crab 宇宙ヤシガニ English text is not available in this practice contest. ケン・マリンブルーは,宇宙ヤシガニを求めて全銀河を旅するスペースハンターである.宇宙ヤシガニは,宇宙最大とされる甲殻類であり,成長後の体長は 400 メートル以上,足を広げれば 1,000 メートル以上にもなると言われている.既に多数の人々が宇宙ヤシガニを目撃しているが,誰一人として捕らえることに成功していない. ケンは,長期間の調査によって,宇宙ヤシガニの生態に関する重要な事実を解明した.宇宙ヤシガニは,驚くべきことに,相転移航法と呼ばれる最新のワープ技術と同等のことを行い,通常空間と超空間の間を往来しながら生きていた.さらに,宇宙ヤシガニが超空間から通常空間にワープアウトするまでには長い時間がかかり,またワープアウトしてからしばらくは超空間に移動できないことも突き止めた. そこで,ケンはついに宇宙ヤシガニの捕獲に乗り出すことにした.戦略は次のとおりである.はじめに,宇宙ヤシガニが通常空間から超空間に突入する際のエネルギーを観測する.このエネルギーを e とするとき,宇宙ヤシガニが超空間からワープアウトする座標 ( x , y , z ) は以下の条件を満たすことがわかっている. x , y , z はいずれも非負の整数である. x + y 2 + z 3 = e である. 上記の条件の下で x + y + z の値を最小にする. これらの条件だけでは座標が一意に決まるとは限らないが, x + y + z の最小値を m としたときに,ワープアウトする座標が平面 x + y + z = m 上にあることは確かである.そこで,この平面上に十分な大きさのバリアを張る.すると,宇宙ヤシガニはバリアの張られたところにワープアウトすることになる.バリアの影響を受けた宇宙ヤシガニは身動きがとれなくなる.そこをケンの操作する最新鋭宇宙船であるウェポン・ブレーカー号で捕獲しようという段取りである. バリアは一度しか張ることができないため,失敗するわけにはいかない.そこでケンは,任務の遂行にあたって計算機の助けを借りることにした.あなたの仕事は,宇宙ヤシガニが超空間に突入する際のエネルギーが与えられたときに,バリアを張るべき平面 x + y + z = m を求めるプログラムを書くことである.用意されたテストケースの全てに対して正しい結果を出力したとき,あなたのプログラムは受け入れられるであろう. Input 入力は複数のデータセットで構成される.各データセットは 1 行のみからなり,1 つの正の整数 e ( e ≦ 1,000,000) が含まれる.これは,宇宙ヤシガニが超空間に突入した際のエネルギーを表す.入力は e = 0 の時に終了し,これはデータセットには含まれない. Output 各データセットについて, m の値を 1 行に出力しなさい.出力には他の文字を含めてはならない. Sample Input 1 2 4 27 300 1250 0 Output for the Sample Input 1 2 2 3 18 44
[ { "submission_id": "aoj_2012_10848222", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,s,t) for(int i = s; i < t ; i++)\nusing LL = long long; using VL = vector<LL>;\ntemplate<class T> using V = vector<T>; template<class T> using VV = V<V<T>>;\nconst LL LINF = 1e18;\n\nvoid solve() {\n\tLL N;\n\twhile (cin >> N, N) {\n\t\tLL ans = LINF;\n\t\tfor (LL z = 0; z*z*z <= N; z++) {\n\t\t\tfor (LL y = 0; y*y <= N; y++) {\n\t\t\t\tLL x = N - z * z*z - y * y;\n\t\t\t\tif (x >= 0 && x<=N) {\n\t\t\t\t\tans = min(ans, x + y + z);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\n}\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(false);\n\n\tsolve();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3216, "score_of_the_acc": -1.0684, "final_rank": 5 }, { "submission_id": "aoj_2012_10611812", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n;\nvector<a2> v;\nvoid input() { cin >> n; }\n\nvoid solve() {\n ll ans = 1e18;\n for(int i = 0; i <= 100; i++) {\n for(int j = 0; j <= 1000; j++) {\n if(i * i * i + j * j <= n) {\n chmin(ans, i + j + (n - i * i * i - j * j));\n }\n }\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3256, "score_of_the_acc": -1.207, "final_rank": 9 }, { "submission_id": "aoj_2012_10581096", "code_snippet": "// AOJ 2012 - Space Coconut Grab\n// JAG Practice Contest for Japan Domestic 2007 - Problem A\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nbool solving() {\n int e; cin >> e;\n if(e == 0) return false;\n \n int m = 2e9;\n for(int z=0; z*z*z <= e; ++z) {\n for(int y=0; y*y + z*z*z <= e; ++y) {\n int x = e - y*y - z*z*z;\n m = min(m, x+y+z);\n }\n }\n cout << m << endl;\n return true;\n}\n\nint main() {\n while(solving()) {}\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3456, "score_of_the_acc": -1.4, "final_rank": 18 }, { "submission_id": "aoj_2012_10544774", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\nlong long mod_mul(long long a, long long b, long long mod) {\n return (__int128)a * b % mod;\n}\n\nlong long mod_pow(long long a, long long b, long long mod) {\n long long result = 1;\n a %= mod;\n while (b > 0) {\n if (b & 1) result = mod_mul(result, a, mod);\n a = mod_mul(a, a, mod);\n b >>= 1;\n }\n return result;\n}\n\nbool MillerRabin(ll x){\n \n if(x <= 1){return false;}\n if(x == 2){return true;}\n if((x & 1) == 0){return false;}\n vll test;\n if(x < (1 << 30)){\n test = {2, 7, 61};\n } \n else{\n test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n }\n ll s = 0;\n ll d = x - 1;\n while((d & 1) == 0){\n s++;\n d >>= 1;\n }\n for(ll i:test){\n if(x <= i){return true;}\n ll y = mod_pow(i,d,x);\n if(y == 1 || y == x - 1){continue;}\n ll c = 0;\n for(int j=0;j<s-1;j++){\n y = mod_mul(y,y,x);\n if(y == x - 1){\n c = 1;\n break;\n }\n }\n if(c == 0){\n return false;\n }\n }\n return true;\n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n while(1){\n LL(e);\n if(e == 0){break;}\n ll m = 1LL<<60;\n rep(z,200){\n ll u = z * z * z;\n if(u > e){break;}\n rep(y,2000){\n ll v = y * y;\n if(u + v > e){break;}\n ll x = e - u - v;\n if(x >= 0){\n m = min(m,x+y+z);\n }\n }\n }\n print(m);\n }\n \n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3456, "score_of_the_acc": -1.4333, "final_rank": 19 }, { "submission_id": "aoj_2012_10533617", "code_snippet": "#if __has_include(<yoniha/all.h>)\n#include <yoniha/all.h>\nusing namespace atcoder;\n#else\n#include <bits/stdc++.h>\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n#endif\nusing namespace std;\n\n#define int long long\n#define all(x) (x).begin(), (x).end()\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rrep(i, n) for(int i = (int)(n - 1); i >= 0; i--)\ntemplate <typename T> bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate <typename T> bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\n\n// using mint = modint;\n\nsigned main(){\n auto solve = [](int e){\n int ans = 1ll << 60;\n for(int z = 0; z * z * z <= e; z++) for(int y = 0; y * y + z * z * z <= e; y++){\n int x = e - y * y - z * z * z;\n chmin(ans, x + y + z);\n }\n cout << ans << '\\n';\n };\n while(true){\n int e; cin >> e;\n if(e == 0) break;\n solve(e);\n }\n}\n/*\n指数部が大きい文字から探索するとよい\nzの全探索が10^2、yが10^4になる\n*/", "accuracy": 1, "time_ms": 140, "memory_kb": 3436, "score_of_the_acc": -1.3474, "final_rank": 14 }, { "submission_id": "aoj_2012_9502120", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\n// エネルギー関数\nint space_crab_energy(int x, int y, int z) {\n return x + y*y + z*z*z;\n}\n\nint main() {\n while (true) {\n int e;\n cin >> e;\n if (e == 0) break; // 入力が0の場合は終了\n\n int min_m = e + 1; // 初期値として十分大きな値\n\n // y の範囲を sqrt(e) で制限\n int max_y = static_cast<int>(sqrt(e)) + 1;\n\n for (int y = 0; y <= max_y; ++y) {\n int y2 = y * y;\n\n // z の範囲を cbrt(e - y^2) で制限\n int max_z = static_cast<int>(cbrt(e - y2)) + 1;\n\n for (int z = 0; z <= max_z; ++z) {\n int z3 = z * z * z;\n int x = e - (y2 + z3);\n\n if (x >= 0) { // x が負でない場合\n int current_sum = x + y + z;\n // space_crab_energy 関数を使って条件をチェック\n if (space_crab_energy(x, y, z) == e) {\n min_m = min(min_m, current_sum); // 最小値を更新\n }\n }\n }\n }\n\n cout << min_m << endl; // 結果を出力\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3224, "score_of_the_acc": -1.3895, "final_rank": 17 }, { "submission_id": "aoj_2012_9500682", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\n// エネルギー関数\nint space_crab_energy(int x, int y, int z) {\n return x + y*y + z*z*z;\n}\n\nint main() {\n while (true) {\n int e;\n cin >> e;\n if (e == 0) break;\n\n int min_m = e + 1; // 初期値として十分大きな値\n\n // y の範囲を sqrt(e) で制限\n int max_y = static_cast<int>(sqrt(e)) + 1;\n\n for (int y = 0; y <= max_y; ++y) {\n int y2 = y * y;\n\n // z の範囲を cbrt(e - y^2) で制限\n int max_z = static_cast<int>(cbrt(e - y2)) + 1;\n\n for (int z = 0; z <= max_z; ++z) {\n int z3 = z * z * z;\n int x = e - (y2 + z3);\n\n if (x >= 0) {\n int current_sum = x + y + z;\n min_m = min(min_m, current_sum);\n }\n }\n }\n\n cout << min_m << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3260, "score_of_the_acc": -1.3509, "final_rank": 15 }, { "submission_id": "aoj_2012_9500671", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nint main() {\n int e;\n while (cin >> e && e != 0) {\n int min_sum = e + 1; // `x + y + z` の初期値として十分大きい値\n\n // y の上限を sqrt(e) に設定\n int max_y = static_cast<int>(sqrt(e)) + 1;\n\n for (int y = 0; y <= max_y; ++y) {\n int y2 = y * y;\n\n // z の上限を cbrt(e - y^2) に設定\n int max_z = static_cast<int>(cbrt(e - y2)) + 1;\n\n for (int z = 0; z <= max_z; ++z) {\n int z3 = z * z * z;\n int x = e - (y2 + z3);\n\n if (x >= 0) {\n int current_sum = x + y + z;\n min_sum = min(min_sum, current_sum);\n }\n }\n }\n\n cout << min_sum << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3240, "score_of_the_acc": -1.2649, "final_rank": 10 }, { "submission_id": "aoj_2012_9397585", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\nint main() {\n while (1) {\n long long e;\n cin >> e;\n if (e == 0) {\n break;\n }\n int ans = INF;\n for (int z = 0; z <= 100; z++) {\n for (int y = 0; y <= 1000; y++) {\n int x = e - y * y - z * z * z;\n if (x < 0) break;\n ans = min(ans, x + y + z);\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3356, "score_of_the_acc": -1.3368, "final_rank": 12 }, { "submission_id": "aoj_2012_9356749", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\n//オーバーフローしているか確認する\n//小さいケースで動いているかを確認する。\nvoid solve(){\n while(true){\n ll e;cin>>e;\n if(e==0)return;\n dbg(e);\n ll ans = INF;\n for(ll b=0;b*b<=e;b++){\n for(ll c=0;b*b+c*c*c<=e;c++){\n if(b*b+c*c*c<=e){\n ll a = (e-b*b-c*c*c);\n assert(a+b*b+c*c*c==e);\n chmin(ans,b+c+a);\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3308, "score_of_the_acc": -1.0772, "final_rank": 6 }, { "submission_id": "aoj_2012_9347468", "code_snippet": "#include<iostream>\nusing namespace std;\n\nint main(){\n while(true){\n int e;\n cin >> e;\n if(e == 0) break;\n int a= 1000000;\n int z=0,x=0;\n while(z*z*z <= e){\n int y=0;\n while((z*z*z) + (y*y) <= e){\n x = e - (z*z*z) - (y*y);\n if(a > x+y+z) a = x+y+z;\n y++;\n }\n z++;\n }\n cout << a << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3076, "score_of_the_acc": -0.4667, "final_rank": 1 }, { "submission_id": "aoj_2012_9347187", "code_snippet": "#include<bits//stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(true){\n int e;\n cin >> e;\n if(e==0) break;\n int sum = 1<<30;\n for(int z = 0; z*z*z <= e; z++){\n for(int y = 0; (z*z*z)+(y*y) <= e; y++){\n int x = e-(z*z*z)-(y*y);\n if(sum>x+y+z) sum=x+y+z;\n }\n }\n cout << sum << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3440, "score_of_the_acc": -1.5579, "final_rank": 20 }, { "submission_id": "aoj_2012_9347184", "code_snippet": "#include<bits//stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(true){\n int e;\n cin >> e;\n if(e==0) break;\n int sum = 1<<30;\n for(int z = 0; z*z*z <= e; z++){\n for(int y = 0; (z*z*z)+(y*y) <= e; y++){\n int x = e-(z*z*z)-(y*y);\n if(sum>x+y+z) sum=x+y+z;\n }\n }\n cout << sum << endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3356, "score_of_the_acc": -1.3368, "final_rank": 12 }, { "submission_id": "aoj_2012_9347179", "code_snippet": "#include<bits//stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(true){\n int e;\n cin >> e;\n if(e==0) return 0;\n int sum = 1<<30;\n for(int z = 0; z*z*z <= e; z++){\n for(int y = 0; (z*z*z)+(y*y) <= e; y++){\n int x = e-(z*z*z)-(y*y);\n if(sum>x+y+z) sum=x+y+z;\n }\n }\n cout << sum << endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3336, "score_of_the_acc": -1.2842, "final_rank": 11 }, { "submission_id": "aoj_2012_9347176", "code_snippet": "#include<bits//stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(true){\n int e;\n cin >> e;\n if(e==0) return 0;\n int ans = 1<<30;\n for(int z = 0; z*z*z <= e; z++){\n for(int y = 0; (z*z*z)+(y*y) <= e; y++){\n int x = e-(z*z*z)-(y*y);\n if(ans>x+y+z) ans=x+y+z;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3136, "score_of_the_acc": -0.7579, "final_rank": 4 }, { "submission_id": "aoj_2012_9346635", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,j,n) for (long long i = j; i < (int)(n); i++)\n#define REP(i,j,n) for (long long i = j; i <= (int)(n); i++)\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n#define all(x) (x.begin(), x.end())\ntemplate<typename T1, typename T2> inline bool chmax(T1 &a, T2 b){\n bool compare = (a < b);\n if(compare) a = b;\n return compare;\n}\ntemplate<typename T1, typename T2> inline bool chmin(T1 &a, T2 b){\n bool compare = (a > b);\n if(compare) a = b;\n return compare;\n}\nstring substrback(string s,size_t p,size_t l){\n\t//s=文字列 p=後ろから数えて何文字目 l=pから何文字目まで\n\tconst size_t strl = s.length();\n\treturn s.substr(strl - p,l);\n}\n//pair型のfirstで比較\nbool comparePairs(const pair<ll,ll> &a, const pair<ll,ll> &b){\n\tif(a.first != b.first)\n\treturn a.first<b.first;\n\treturn a.second < b.second;\n}\n//pair型のsecondで比較\nbool comparePairs2(const pair<ll,ll> &a, const pair<ll,ll> &b){\n\tif(a.second != b.second)\n\treturn a.second<b.second;\n\treturn a.first < b.first;\n}\n//pair型の1.second と2.firstをつなげていく\nbool customsort(const pair<ll,ll> &a,const pair<ll,ll> &b){\n\tif(a.second == b.first){\n\t\treturn true;\n\t}\n\treturn false;\n}\nbool customsort2(const pair<ll, ll> &a, const pair<ll, ll> &b) {\n // 特定の値がfirstに出た場合、そのsecond値を先頭に持ってくる\n if (a.first == -1) {\n return true;\n } else if (b.first == -1) {\n return false;\n }\n return a.first < b.first;\n}\n\n//nの階乗\null facto(ll n){\n\tif (n==0 || n ==1){\n\t\treturn 1;\n\t} else {\n\t\treturn n *facto(n-1);\n\t}\n}\n//nのΣ\nll sum(ll n) {\n\tll sum =0;\n\tsum = n*(n+1)/2;\n\treturn sum;\n}\nstruct V {\n\tint a, b;\n};\nbool acom(const V& a, const V& b) {\n\treturn a.a < b.a;\n}\nbool bcom(const V& a, const V& b) {\n\treturn a.b < b.b;\n}\nbool isPrime(ll num){\n\tif(num==1)return false;\n\tif(num==2)return true;\n\tif(num % 2 == 0)return false;\n\tfor(ll i =3;i*i<=num;i+=2){\n\t\tif(num % i == 0)return false;\n\t}\n\treturn true;\n}\nint gcd(int a, int b){\n\tif(a%b==0) return b;\n\telse return gcd(b, a%b);\n}\n/*\nint main() {\n\tll n,m;\n\tcin>>n>>m;\n\tfor (ll i = (ll) sqrt(m) + 1; i >= 2; i--) {\n\t\twhile(true) {\n\t\t\tif (n % i == 0 && m % i == 0) {\n\t\t\t\tm /= i;\n\t\t\t\tn /= i;\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tll g = gcd(n,m);\n\tn/=g; m/=g;\n\t//cout<<n<<\" \"<<m<<endl;\n\tint maxn =0; int maxm = 0;\n\tint n1 =n; int m1 =m;\n\twhile(n1>0){\n\t\tchmax(maxn,n1%10);\n\t\tn1/=10;\n\t}\n\twhile(m1>0){\n\t\tchmax(maxm,m1%10);\n\t\tm1/=10;\n\t}\n\tll ans = max(n,m);\n\tif(ans>10)ans==10;\n\tcout<<ans<<endl;\n}\n*/\n//atodekesite\nunordered_set<ll> primes;\nvoid bunkai(ll num) {\n\tcout<<num<<endl;\n\tif(num==1)return;\n\tif(num==2) {\n\t\tprimes.insert(2);\n\t\tbunkai(num/2);\n\t}\n\tif(num % 2 == 0)return;\n\tfor(ll i =3;i*i<=num;i+=2){\n\t\tif(num % i == 0) {\n\t\t\tprimes.insert(i);\n\t\t\tbunkai(num/i);\n\t\t} else {\n\t\t\treturn;\n\t\t}\n\t}\n}\nint main(){\n\twhile(true){\n\t\tll n;\n\t\tcin>>n;\n\t\tll ans = 10000000000;\n\t\tif(n==0)return 0;\n\t\tfor(ll i=0;i*i*i<=n;i++){\n\t\t\tfor(int j=0;i*i*i+j*j<=n;j++){\n\t\t\t\tchmin(ans,i+j+(n-(i*i*i+j*j)));\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t\t\n\t}\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3324, "score_of_the_acc": -1.3526, "final_rank": 16 }, { "submission_id": "aoj_2012_9346556", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)\n#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n, k) (((n) >> (k)) & 1)\n#define pcnt(n) (bitset<64>(n).count())\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\ntemplate <class T> std::istream& operator>>(std::istream& is, std::vector<T>& V) {\n for (auto& it : V) is >> it;\n return is;\n}\ntemplate <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) {\n for (int i = 0 ; i < (int)V.size() ; i++) {\n os << V[i] << (i + 1 == (int)V.size() ? \"\" : \" \");\n }\n return os;\n}\nusing namespace std;\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while(true){\n int e;\n cin >> e;\n if(e==0) return 0;\n int ans = 1<<30;\n for(int z = 0; z*z*z <= e; z++){\n for(int y = 0; (z*z*z)+(y*y) <= e; y++){\n int x = e-(z*z*z)-(y*y);\n if(ans>x+y+z) ans=x+y+z;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3356, "score_of_the_acc": -1.1035, "final_rank": 8 }, { "submission_id": "aoj_2012_9346525", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\nusing ll = long long;\n#define all(x) x.begin(),x.end()\ntemplate <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }\ntemplate <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }\n\nint main() {\n while(true){\n int n;\n cin>>n;\n int ans=1e9;\n if(n==0)break;\n for(int z=0;z*z*z<=n;z++){\n for(int y=0;y*y+z*z*z<=n;y++){\n int x=n-z*z*z-y*y;\n chmin(ans,x+y+z);\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3124, "score_of_the_acc": -0.7263, "final_rank": 3 }, { "submission_id": "aoj_2012_9329185", "code_snippet": "#include <iostream>\n#include <string>\n#include <utility>\n#include <limits.h>\n#include <cstdlib>\n#include <algorithm>\n#include <iomanip>\n#include <math.h>\n#include <queue>\n#include <stack>\n#include <list>\n#include <map>\nusing namespace std;\n\nint main(){\n int e;\n while(1){\n cin >> e;\n if(e == 0)return 0;\n\n int m = INT_MAX;\n int x = 0,y = 0,z = 0;\n\n for(z = 0; z *z*z <= e; z++){\n int nz = e - z*z*z;\n for(y = 0; y*y <= nz; y++){\n x = nz-y*y;\n if(x >= 0){\n m = min(m,x+y+z);\n }\n }\n }\n\n cout << m << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3364, "score_of_the_acc": -1.0912, "final_rank": 7 }, { "submission_id": "aoj_2012_9329108", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(true){\n set<int>s;\n int e;\n cin >> e;\n if(e==0)break;\n float z=0;\n while(z*z*z <= e){\n float y = floor(sqrt(e-z*z*z));\n float x = e-z*z*z-y*y;\n s.insert(x+y+z);\n z++;\n }\n cout << *s.begin() << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3348, "score_of_the_acc": -0.7158, "final_rank": 2 } ]
aoj_2015_cpp
Square Route スクウェア・ルート English text is not available in this practice contest. このたび新しい豪邸を建てることを決めた大富豪の品田氏は,どの街に新邸を建てようかと悩んでいる.実は,品田氏は正方形がとても好きという変わった人物であり,そのため少しでも正方形の多い街に住みたいと思っている. 品田氏は,碁盤目状に道路の整備された街の一覧を手に入れて,それぞれの街について,道路により形作られる正方形の個数を数えることにした.ところが,道と道の間隔は一定とは限らないため,手作業で正方形を数えるのは大変である.そこであなたには,碁盤目状の道路の情報が与えられたときに,正方形の個数を数えるプログラムを書いて欲しい. Input 入力は複数のデータセットから構成されており,各データセットは以下のような構成になっている. N M h 1 h 2 ... h N w 1 w 2 ... w M 1 行目には 2 つの正の整数 N , M (1 ≦ N , M ≦ 1500) が与えられる.続く N 行 h 1 , h 2 , ..., h N (1 ≦ h i ≦ 1000)は,道路と道路の南北方向の間隔を表す.ここで h i は北から i 番目の道路と北から i + 1 番目の道路の間隔である.同様に,続く M 行 w 1 , ..., w M (1 ≦ w i ≦ 1000)は,道路と道路の東西方向の間隔を表す.ここで w i は西から i 番目の道路と西から i + 1 番目の道路の間隔である.道路自体の幅は十分細いため考慮する必要はない. 図 D-1: 最初のデータセット N = M = 0 は入力の終端を示しており,データセットには含めない. Output 各データセットに対して,正方形の個数を 1 行に出力しなさい.たとえば,Sample Input の最初のデータセットにおいては,以下のとおり 6 個の正方形を含むので,このデータセットに対する出力は 6 となる. 図 D-2: 最初のデータセットに含まれる正方形 Sample Input 3 3 1 1 4 2 3 1 1 2 10 10 10 0 0 Output for the Sample Input 6 2
[ { "submission_id": "aoj_2015_10848499", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nint main(){\n int n,m;\n while(cin>>n>>m,n){\n vector<int>h(n),w(m);\n for(int i=0;i<n;++i){\n cin>>h[i];\n }\n for(int i=0;i<m;++i){\n cin>>w[i];\n }\n vector<int>comb1(1500001,0);\n long long ans=0;\n for(int i=0;i<n;++i){\n for(int j=i;j<n;++j){\n int buf=0;\n for(int k=i;k<=j;++k){\n buf+=h[k];\n }\n comb1[buf]++;\n }\n }\n for(int i=0;i<m;++i){\n for(int j=i;j<m;++j){\n int buf=0;\n for(int k=i;k<=j;++k){\n buf+=w[k];\n }\n ans+=comb1[buf];\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 2290, "memory_kb": 9252, "score_of_the_acc": -1, "final_rank": 11 }, { "submission_id": "aoj_2015_10779429", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// y 1-100000 c 1000000000 y < 100\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n const int MAX_SUM = 1500000; // 1500 * 1000\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n\n // vector を使って出現回数をカウントする\n vector<int> n_freq(MAX_SUM + 1, 0);\n vector<int> m_freq(MAX_SUM + 1, 0);\n\n vector<int> n_prefix(n + 1, 0);\n vector<int> m_prefix(m + 1, 0);\n\n for (int i = 0; i < n; i++) {\n int x;\n cin >> x;\n n_prefix[i + 1] = n_prefix[i] + x;\n }\n for (int i = 0; i < m; i++) {\n int x;\n cin >> x;\n m_prefix[i + 1] = m_prefix[i] + x;\n }\n\n // 高さ方向の連続区間の和をカウント\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j <= n; j++) {\n int sum = n_prefix[j] - n_prefix[i];\n n_freq[sum]++;\n }\n }\n\n // 幅方向の連続区間の和をカウント\n for (int i = 0; i < m; i++) {\n for (int j = i + 1; j <= m; j++) {\n int sum = m_prefix[j] - m_prefix[i];\n m_freq[sum]++;\n }\n }\n\n // 正方形の個数を計算\n long long result = 0;\n for (int s = 1; s <= MAX_SUM; s++) {\n if (n_freq[s] && m_freq[s]) {\n result += (long long)n_freq[s] * m_freq[s];\n }\n }\n\n cout << result << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 15196, "score_of_the_acc": -0.2623, "final_rank": 3 }, { "submission_id": "aoj_2015_10731714", "code_snippet": "#include <iostream> \n#include <vector> \n#include <algorithm> \nusing namespace std;\nint square(int n, int m) {\n int h,w;\n vector<int> H(n);\n vector<int> W(m);\n for(int i=0; i<n;i++){\n cin>>H.at(i);\n }\n for(int i=0; i<m; i++){\n cin>>W.at(i);\n }\n for(int i=0; i<n; i++){\n h=H.at(i);\n for(int j=i+1; j<n; j++){\n h+=H.at(j);\n H.push_back(h);\n }\n }\n for(int i=0; i<m; i++){\n w=W.at(i);\n for(int j=i+1; j<m; j++){\n w+=W.at(j);\n W.push_back(w);\n }\n }\n sort(H.begin(), H.end());\n sort(W.begin(), W.end());\n int sum=0;\n h=H.size();\n w=W.size();\n int a=0;\n int b=0;\n while(a<h && b<w){\n if(H.at(a)<W.at(b)){\n a++;\n }\n else if(H.at(a)>W.at(b)){\n b++;\n }\n else{\n int s=1;\n int t=1;\n while(a<h-1 && H.at(a)==H.at(a+1)){\n s++;\n a++;\n }\n while(b<w-1 && W.at(b)==W.at(b+1)){\n t++;\n b++;\n }\n sum+=(s*t);\n a++;\n b++;\n }\n }\n return sum;\n}\n\nint main() {\n int n,m;\n while(cin>>n>>m && n!=0){\n cout<<square(n,m)<<endl;\n }\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 18600, "score_of_the_acc": -0.4892, "final_rank": 7 }, { "submission_id": "aoj_2015_10685676", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <math.h>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <functional>\n#include <cassert>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define all(a) a.begin(), a.end()\n#define arr(a) a.rbegin(), a.rend()\ntemplate<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}\ntemplate<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}\nusing ll = long long;\n\n//Konishii\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true) {\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) break;\n vector<int> h(n), w(m);\n rep(i, n) cin >> h[i];\n rep(i, m) cin >> w[i];\n\n int ma = 1500 * 1000;\n vector<int> cnth(ma + 10, 0), cntw(ma + 10, 0);\n vector<int> pfh(n + 1, 0), pfw(m + 1, 0);\n rep(i, n) pfh[i+1] += pfh[i] + h[i];\n rep(i, m) pfw[i+1] += pfw[i] + w[i];\n\n rep(i, n) {\n for(int j = i; j < n; j++) {\n int H = pfh[j+1] - pfh[i];\n cnth[H]++;\n }\n }\n rep(i, m) {\n for(int j = i; j < m; j++) {\n int W = pfw[j+1] - pfw[i];\n cntw[W]++;\n }\n }\n ll ans = 0;\n\n rep(i, ma + 1) {\n ans += ll(cnth[i]) * cntw[i];\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 21172, "score_of_the_acc": -0.367, "final_rank": 5 }, { "submission_id": "aoj_2015_10637451", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n, m;\nvector<ll> v, w;\nvoid input() {\n cin >> n >> m;\n v.resize(n);\n w.resize(m);\n for(auto &x : v)\n cin >> x;\n for(auto &x : w)\n cin >> x;\n}\nvoid solve() {\n map<ll, a2> mp;\n for(int i = 0; i < n; i++) {\n ll now = 0;\n for(int j = i; j < n; j++) {\n now += v[j];\n mp[now][0]++;\n }\n }\n for(int i = 0; i < m; i++) {\n ll now = 0;\n for(int j = i; j < m; j++) {\n now += w[j];\n mp[now][1]++;\n }\n }\n\n ll ans = 0;\n for(auto &x : mp) {\n ans += x.second[0] * x.second[1];\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1850, "memory_kb": 27068, "score_of_the_acc": -1.302, "final_rank": 19 }, { "submission_id": "aoj_2015_10451692", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing ull=unsigned long long;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)\nconstexpr int maxd=1500000;\nconstexpr int maxN=1500;\nint N,M,L,te1;\narray<int,maxd+1>h0,w0;\narray<int,maxN>h,w;\nsigned main() {\n cin.tie(0)->sync_with_stdio(0);\n cout<<fixed<<setprecision(16);\n for(;cin>>N>>M,N;){\n L=min(N,M);\n memset(h0.data(),0,(L*1000+1)*4);\n memset(w0.data(),0,(L*1000+1)*4);\n rep(i,N)cin>>h[i];\n rep(i,M)cin>>w[i];\n rep(i,N){\n te1=0;\n for(int j=i;j<N;j++){\n\tte1+=h[j];\n\th0[te1]++;\n }\n }\n rep(i,M){\n te1=0;\n for(int j=i;j<M;j++){\n\tte1+=w[j];\n\tw0[te1]++;\n }\n }\n te1=0;\n rep(i,L*1000+1)te1+=h0[i]*w0[i];\n cout<<te1<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 15180, "score_of_the_acc": -0.165, "final_rank": 2 }, { "submission_id": "aoj_2015_10147749", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while (true) {\n ll N, M;\n cin >> N >> M;\n if (N==0 && M==0)break;\n\n vector<ll> h(N), w(M);\n for (int i = 0; i < N; i++)\n cin >> h[i];\n for (int i = 0; i < M; i++)\n cin >> w[i];\n\n vector<ll> prefh(N + 1, 0), prefw(M + 1, 0);\n for (int i = 0; i < N; i++) {\n prefh[i + 1] = prefh[i] + h[i];\n }\n for (int i = 0; i < M; i++) {\n prefw[i + 1] = prefw[i] + w[i];\n }\n\n vector<ll> eh, ew;\n for (int i = 0; i <= N; i++) {\n for (int j = i + 1; j <= N; j++) {\n eh.push_back(prefh[j] - prefh[i]);\n }\n }\n for (int i = 0; i <= M; i++) {\n for (int j = i + 1; j <= M; j++) {\n ew.push_back(prefw[j] - prefw[i]);\n }\n }\n sort(eh.begin(), eh.end());\n sort(ew.begin(), ew.end());\n\n ll ans = 0;\n for (int i = 0; i < eh.size(); i++) {\n auto itl = lower_bound(ew.begin(), ew.end(), eh[i]);\n auto itr = upper_bound(ew.begin(), ew.end(), eh[i]);\n ans += itr - itl;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 44124, "score_of_the_acc": -1.3009, "final_rank": 18 }, { "submission_id": "aoj_2015_9625736", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while(true) {\n ll H, W;\n cin >> H >> W;\n\n if(!H) { break; }\n\n vector<ll> h(H), w(W), sh{0}, sw{0};\n for(auto &i : h) {\n cin >> i;\n sh.emplace_back(sh.back() + i);\n }\n for(auto &i : w) {\n cin >> i;\n sw.emplace_back(sw.back() + i);\n }\n\n map<ll, ll> mh, mw;\n for(ll r = 0; r <= H; r++) {\n for(ll l = 0; l < r; l++) { mh[sh[r] - sh[l]]++; }\n }\n for(ll r = 0; r <= W; r++) {\n for(ll l = 0; l < r; l++) { mw[sw[r] - sw[l]]++; }\n }\n\n ll ans = 0;\n for(auto &i : mh) { ans += i.second * mw[i.first]; }\n\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 1640, "memory_kb": 45184, "score_of_the_acc": -1.7137, "final_rank": 20 }, { "submission_id": "aoj_2015_9453100", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<ll> A(N+1), B(M+1);\n A[0] = B[0] = 0;\n rep(i,0,N) {\n ll X;\n cin >> X;\n A[i+1] = A[i]+X;\n }\n rep(i,0,M) {\n ll X;\n cin >> X;\n B[i+1] = B[i]+X;\n }\n map<ll,ll> mp;\n rep(i,0,N+1) {\n rep(j,i+1,N+1) {\n mp[A[j]-A[i]]++;\n }\n }\n ll ANS = 0;\n rep(i,0,M+1) {\n rep(j,i+1,M+1) {\n ANS += mp[B[j]-B[i]];\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 27232, "score_of_the_acc": -1.1348, "final_rank": 13 }, { "submission_id": "aoj_2015_9427788", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#ifdef LOCAL_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n#include <stdio.h>\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <iomanip> //setprecision\n#include <map> // map\n#include <unordered_map> //unordered_map\n#include <queue> // queue, priority_queue\n#include <set> // set,multiset\n#include <stack> // stack\n#include <deque> // deque\n#include <math.h>//pow,,,\n#include <cmath>//abs,,,\n#include <bitset> // bitset\n#include <numeric> //accumulate,,,\n#include <sstream>\n#include <initializer_list>\n#include <random>\n#include <unordered_set>\n#include <time.h>\n#include <stdio.h>\n#include <string.h>\n#include <functional>\n#include <climits>\n#include <utility>\n#include <cassert>\n//#include <atcoder/all> // aclibraryを使う際は g++ a.cpp -O2 -std=c++17 -I . でコンパイルする\n//using namespace atcoder;\nusing namespace std;\nconst long long INF = 4000000000000000001; // 4*10^18\nconst int inf = 2001001001;\nconst long long MOD = 998244353;\nconst double pi = 3.141592653589;\nconst vector<int> dh = {1,0,-1,0} , dw = {0,1,0,-1};\nconst vector<int> dh8 = {-1,-1,-1,0,0,1,1,1} , dw8 = {-1,0,1,-1,1,-1,0,1};\n#define endl \"\\n\";\nlong long modpow(long long a, long long n, long long mod) {\n a %= mod;\n long long ret = 1;\n while (n > 0) {\n if (n & 1) ret = ret * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return ret % mod;\n}\nlong long modinv(long long a, long long m) {\n long long b = m, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n u %= m; \n if (u < 0) u += m;\n return u;\n}\nlong long gcd(long long a, long long b) {\n if (b == 0) return a; else return gcd(b, a % b);\n}\nlong long lcm(long long a, long long b) {\n return a / gcd(a, b) * b ;\n}\nlong long get_mod(long long res){\n if(res < 0) res += MOD;\n return res % MOD;\n}\ndouble DegToRad(double deg){\n return deg*(pi/180.0);\n}\ndouble RadToDeg(double rad){\n return rad/(pi/180.0);\n}\nint uniform_rand(int mini , int maxi){\n random_device seed;\n mt19937_64 engine(seed());\n uniform_real_distribution<> ran(mini , maxi+1);\n int ret = ran(engine);\n if(ret >= maxi+1) ret = maxi;\n return ret;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n // FILE* in = freopen(\"mojain/in1.txt\" , \"r\" , stdin); //2つ以上のケースを試したい場合はファイル名をループ回してin2.txt等で指定していく\n // FILE* out = freopen(\"mojaout/out1.txt\", \"w\", stdout);\n // ifstream ifs(\"in.txt\"); // 巨大ケースは in.txt に書いてifsで標準入力\n\n while(true){\n int N,M;cin >> N >> M;\n if(N == 0){\n break;\n }\n\n vector<int> H(N) , W(M);\n vector<int> ruiH(N+1) , ruiW(M+1);\n for(int i = 0;i<N;i++){\n cin >> H.at(i);\n ruiH.at(i+1) = ruiH.at(i) + H.at(i);\n }\n \n for(int i = 0;i<M;i++){\n cin >> W.at(i);\n ruiW.at(i+1) = ruiW.at(i) + W.at(i);\n }\n\n map<int,int> WM;\n for(int i = 0;i<M+1;i++){\n for(int j = i+1;j<M+1;j++){\n WM[ruiW.at(j)-ruiW.at(i)]++;\n }\n }\n\n int ans = 0;\n for(int i = 0;i<N+1;i++){\n for(int j = i+1;j<N+1;j++){\n ans += WM[ruiH.at(j)-ruiH.at(i)];\n }\n }\n\n cout << ans << endl;\n\n\n\n\n\n\n\n\n \n \n\n \n\n }\n \n\n \n\n}", "accuracy": 1, "time_ms": 1380, "memory_kb": 21188, "score_of_the_acc": -0.9313, "final_rank": 10 }, { "submission_id": "aoj_2015_9370255", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,k,n) for (int i = k; i < (n); ++i)\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\nconst long long INF = 1e17;\n\nint main(){\n while(true){\n int n,m;\n cin >> n >> m;\n if(n==0){\n break;\n }\n vector<int> h(n);\n for(int i=0;i<n;i++){\n cin >> h[i];\n }\n\n vector<int> w(m);\n for(int i=0;i<m;i++){\n cin >> w[i];\n }\n\n //sum_h[i-1]:h[0]からh[i]の和\n vector<int> sum_h(n+10);\n //sum_w[i-1]:w[0]からw[i]の和\n vector<int> sum_w(m+10);\n\n for(int i=1;i<=n;i++){\n sum_h[i] = sum_h[i-1] + h[i-1];\n }\n\n for(int i=1;i<=m;i++){\n sum_w[i] = sum_w[i-1] + w[i-1];\n }\n\n //高さ別度数分布\n vector<ll> h_cnt(2000000);\n //幅別度数分布\n vector<ll> w_cnt(2000000);\n\n for(int l=0;l<n;l++){\n for(int r=l+1;r<=n;r++){\n h_cnt[sum_h[r]-sum_h[l]]++;\n }\n }\n\n for(int l=0;l<m;l++){\n for(int r=l+1;r<=m;r++){\n w_cnt[sum_w[r]-sum_w[l]]++;\n }\n }\n\n ll ans = 0LL;\n\n for(int i=0;i<2000000;i++){\n ans += h_cnt[i]*w_cnt[i];\n }\n\n cout << ans << endl;\n\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 34140, "score_of_the_acc": -0.9085, "final_rank": 9 }, { "submission_id": "aoj_2015_9364436", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nvoid solve(){\n while(true){\n int N,M;cin>>N>>M;\n if(N==0&&M==0)return;\n map<ll,ll> mp;\n vl h(N);rep(i,N)cin>>h[i];\n vl w(M);rep(i,M)cin>>w[i];\n vl S(N+1),T(M+1);\n rep(i,N)S[i+1]+=S[i]+h[i];\n rep(i,M)T[i+1]+=T[i]+w[i];\n ll ans = 0;\n rep(l,N)for(int r=l+1;r<=N;r++)mp[S[r]-S[l]]++;\n rep(l,M)for(int r=l+1;r<=M;r++)ans+=mp[T[r]-T[l]];\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1470, "memory_kb": 27284, "score_of_the_acc": -1.1406, "final_rank": 15 }, { "submission_id": "aoj_2015_9359238", "code_snippet": "// g++-13 1.cpp -std=c++17 -O2 -I .\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n#include<ext/pb_ds/tag_and_trait.hpp>\nusing namespace __gnu_pbds;\n\nusing ll = long long;\nusing ld = long double;\n \nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vst = vector<string>;\nusing vvst = vector<vst>;\n \n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define pq_big(T) priority_queue<T,vector<T>,less<T>>\n#define pq_small(T) priority_queue<T,vector<T>,greater<T>>\n#define all(a) a.begin(),a.end()\n#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)\n#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end())\n\nrandom_device seed;\nmt19937_64 randint(seed());\n\nll grr(ll mi, ll ma) { // [mi, ma)\n return mi + randint() % (ma - mi);\n}\n\nvoid solve(int n,int m){\n vi h(n+1,0);\n vi w(m+1,0);\n rep(i,0,n){\n int x;cin>>x;\n h[i+1]=h[i]+x;\n }\n rep(i,0,m){\n int x;cin>>x;\n w[i+1]=w[i]+x;\n }\n\n map<int,ll> mp;\n rep(i,0,n+1){\n rep(j,i+1,n+1){\n mp[h[j]-h[i]]++;\n }\n }\n\n ll ans=0;\n rep(i,0,m+1){\n rep(j,i+1,m+1){\n ans+=mp[w[j]-w[i]];\n }\n }\n\n cout<<ans<<endl;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while(true){\n int n,m;cin>>n>>m;\n if(n==0&&m==0)return 0;\n solve(n,m);\n }\n}", "accuracy": 1, "time_ms": 1500, "memory_kb": 27196, "score_of_the_acc": -1.1514, "final_rank": 16 }, { "submission_id": "aoj_2015_9358372", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n while (true) {\n ll N, M, ans = 0;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n break;\n }\n vector<ll> h(N + 1, 0), w(M + 1, 0);\n vector<ll> hz(N + 1, 0), wz(M + 1, 0);\n\n for (ll i = 1; i <= N; i++) cin >> h[i];\n for (ll i = 1; i <= M; i++) cin >> w[i];\n\n for (ll i = 1; i <= N; i++) {\n hz[i] = hz[i - 1] + h[i];\n }\n\n for (ll i = 1; i <= M; i++) {\n wz[i] = wz[i - 1] + w[i];\n }\n\n unordered_map<ll, ll> sum_count_h;\n for (ll i = 1; i <= N; i++) {\n for (ll j = 0; j < i; j++) {\n sum_count_h[hz[i] - hz[j]]++;\n }\n }\n\n for (ll i = 1; i <= M; i++) {\n for (ll j = 0; j < i; j++) {\n ans += sum_count_h[wz[i] - wz[j]];\n }\n }\n\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 18788, "score_of_the_acc": -0.424, "final_rank": 6 }, { "submission_id": "aoj_2015_9340627", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nbool solve(){\n ll N,M;cin>>N>>M;\n if(N==0&&M==0) return 0;\n vll A(N),B(M);\n rep(i,N) cin>>A[i];\n rep(i,M) cin>>B[i];\n\n vll rw(1),rw2(1);\n rep(i,N) rw.push_back(rw[i]+A[i]);\n rep(i,M) rw2.push_back(rw2[i]+B[i]);\n\n map<ll,ll> mp;\n rep(i,M) rep2(j,i+1,M+1){\n mp[rw2[j]-rw2[i]]++;\n }\n\n ll ans=0;\n rep(i,N) rep2(j,i+1,N+1){\n ans+=mp[rw[j]-rw[i]];\n }\n cout<<ans<<endl;\n\n return 1;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(solve());\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 27292, "score_of_the_acc": -1.1364, "final_rank": 14 }, { "submission_id": "aoj_2015_9336149", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int n, m;\n std::cin >> n >> m;\n if (n == 0) return 1;\n std::vector<long long> a(n + 1), b(m + 1);\n for (int i = 0; i < n; i++) {\n long long v;\n std::cin >> v;\n a[i + 1] = a[i] + v;\n }\n for (int i = 0; i < m; i++) {\n long long v;\n std::cin >> v;\n b[i + 1] = b[i] + v;\n }\n std::map<long long, long long> mp;\n for (int i = 0; i <= n; i++) {\n for (int j = 0; j < i; j++) {\n mp[a[i] - a[j]]++;\n }\n }\n long long ans = 0;\n for (int i = 0; i <= m; i++) {\n for (int j = 0; j < i; j++) {\n ans += mp[b[i] - b[j]];\n }\n }\n std::cout << ans << std::endl;\n return 0;\n}\n\nint main() {\n // int n;\n // std::cin >> n;\n // std::cin.ignore();\n // while (n--) solve();\n while (!solve());\n}", "accuracy": 1, "time_ms": 1520, "memory_kb": 27168, "score_of_the_acc": -1.1594, "final_rank": 17 }, { "submission_id": "aoj_2015_9336032", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main()\n{\n while (1)\n {\n\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0)\n break;\n vector<ll> h(n + 1);\n vector<ll> w(m + 1);\n for (int i = 1; i <= n; i++)\n cin >> h[i];\n for (int i = 1; i <= m; i++)\n cin >> w[i];\n\n for (int i = 1; i <= n; i++)\n h[i] += h[i - 1];\n for (int i = 1; i <= m; i++)\n w[i] += w[i - 1];\n\n map<ll, ll> x;\n for (int i = 0; i < n; i++)\n {\n for (int j = i + 1; j <= n; j++)\n {\n x[h[j] - h[i]]++;\n }\n }\n ll ans = 0;\n\n for (int i = 0; i < m; i++)\n {\n for (int j = i + 1; j <= m; j++)\n {\n ans += x[w[j] - w[i]];\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 27148, "score_of_the_acc": -1.1324, "final_rank": 12 }, { "submission_id": "aoj_2015_9336010", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,j,n) for (int i = j; i < (int)(n); i++)\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n#define all(x) (x.begin(), x.end())\ntemplate<typename T1, typename T2> inline bool chmax(T1 &a, T2 b){\n bool compare = (a < b);\n if(compare) a = b;\n return compare;\n}\ntemplate<typename T1, typename T2> inline bool chmin(T1 &a, T2 b){\n bool compare = (a > b);\n if(compare) a = b;\n return compare;\n}\nstring substrback(string s,size_t p,size_t l){\n\t//s=文字列 p=後ろから数えて何文字目 l=pから何文字目まで\n\tconst size_t strl = s.length();\n\treturn s.substr(strl - p,l);\n}\n//pair型のfirstで比較\nbool comparePairs(const pair<ll,ll> &a, const pair<ll,ll> &b){\n\tif(a.first != b.first)\n\treturn a.first<b.first;\n\treturn a.second < b.second;\n}\n//pair型のsecondで比較\nbool comparePairs2(const pair<ll,ll> &a, const pair<ll,ll> &b){\n\tif(a.second != b.second)\n\treturn a.second<b.second;\n\treturn a.first < b.first;\n}\n//pair型の1.second と2.firstをつなげていく\nbool customsort(const pair<ll,ll> &a,const pair<ll,ll> &b){\n\tif(a.second == b.first){\n\t\treturn true;\n\t}\n\treturn false;\n}\nbool customsort2(const pair<ll, ll> &a, const pair<ll, ll> &b) {\n // 特定の値がfirstに出た場合、そのsecond値を先頭に持ってくる\n if (a.first == -1) {\n return true;\n } else if (b.first == -1) {\n return false;\n }\n return a.first < b.first;\n}\n\n//nの階乗\null facto(ll n){\n\tif (n==0 || n ==1){\n\t\treturn 1;\n\t} else {\n\t\treturn n *facto(n-1);\n\t}\n}\n//nのΣ\nll sum(ll n) {\n\tll sum =0;\n\tsum = n*(n+1)/2;\n\treturn sum;\n}\nstruct V {\n\tint a, b;\n};\nbool acom(const V& a, const V& b) {\n\treturn a.a < b.a;\n}\nbool bcom(const V& a, const V& b) {\n\treturn a.b < b.b;\n}\n//やりましょう!!\nbool solve() {\n\tint n, m;\n\tcin>>n>>m;\n\tif(n==0&&m==0) return false;\n\tvector<int> h(n+1);\n\tvector<int> w(m+1);\n\tint current = 0;\n\trep(i,0,n) {\n\t\tint x;\n\t\tcin>>x;\n\t\th[i]=current;\n\t\tcurrent+=x;\n\t}\n\th[n]=current;\n\tcurrent = 0;\n\trep(i,0,m) {\n\t\tint x;\n\t\tcin>>x;\n\t\tw[i]=current;\n\t\tcurrent+=x;\n\t}\n\tw[m]=current;\n\tvector<int> ruih, ruiw;\n\tfor (int width = 1; width <= n; width++) {\n\t\tfor (int start = 0; start <= n-width; start++) {\n\t\t\truih.push_back(h[start+width] - h[start]);\n\t\t}\n\t}\n\tfor (int width = 1; width <= m; width++) {\n\t\tfor (int start = 0; start <= m-width; start++) {\n\t\t\truiw.push_back(w[start+width] - w[start]);\n\t\t}\n\t}\n\tsort(ruiw.begin(), ruiw.end());\n\tll ans = 0;\n\trep(i,0,ruih.size()) {\n\t\tauto start = lower_bound(ruiw.begin(), ruiw.end(), ruih[i]);\n\t\tif(start==ruiw.end()) continue; \n\t\tauto end = upper_bound(ruiw.begin(), ruiw.end(), ruih[i]);\n\t\tans+=(end-start);\n\t}\n\tcout<<ans<<endl;\n\treturn true;\n}\nint main() {\n\twhile(solve());\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 22872, "score_of_the_acc": -0.5685, "final_rank": 8 }, { "submission_id": "aoj_2015_9335952", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing lint = long long;\n\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n while (1) {\n int n, m, ans = 0;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n vector<int> h(n), w(m), hs(n + 1, 0), ws(m + 1, 0), t(1500001, 0), y(1500001, 0);\n rep(i, n) {\n cin >> h[i];\n }\n rep(i, m) {\n cin >> w[i];\n }\n rep(i, n) {\n hs[i + 1] = hs[i] + h[i];\n }\n rep(i, m) {\n ws[i + 1] = ws[i] + w[i];\n }\n rep(i, n) {\n rep(j, i + 1, n + 1) {\n t[hs[j] - hs[i]]++;\n }\n }\n rep(i, m) {\n rep(j, i + 1, m + 1) {\n y[ws[j] - ws[i]]++;\n }\n }\n rep(i, 1, 1500001) {\n ans += t[i] * y[i];\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 14968, "score_of_the_acc": -0.2824, "final_rank": 4 }, { "submission_id": "aoj_2015_9335901", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)\n#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n, k) (((n) >> (k)) & 1)\n#define pcnt(n) (bitset<64>(n).count())\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\ntemplate <class T> std::istream& operator>>(std::istream& is, std::vector<T>& V) {\n for (auto& it : V) is >> it;\n return is;\n}\ntemplate <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) {\n for (int i = 0 ; i < (int)V.size() ; i++) {\n os << V[i] << (i + 1 == (int)V.size() ? \"\" : \" \");\n }\n return os;\n}\nusing namespace std;\n\nusing ll = long long;\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while(1){\n int N, M; cin >> N >> M;\n if(N == 0 && M == 0) break;\n\n vector<ll> H(N), W(M); cin >> H >> W;\n vector<ll> sumH(N + 1), sumW(M + 1);\n rep(i, N){\n sumH[i + 1] = sumH[i] + H[i];\n }\n rep(i, M){\n sumW[i + 1] = sumW[i] + W[i];\n }\n\n vector<ll> num(1000010, 0);\n rep(i, N){\n for(int j = i + 1; j <= N; j++){\n num[sumH[j] - sumH[i]]++;\n }\n }\n ll ans = 0;\n rep(i, M){\n for(int j = i + 1; j <= M; j++){\n ans += num[sumW[j] - sumW[i]];\n }\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 11016, "score_of_the_acc": -0.0579, "final_rank": 1 } ]
aoj_2017_cpp
Karakuri Doll からくり人形 English text is not available in this practice contest. からくり人形師の JAG 氏は,長年の研究の果てに,伝統技術と最新技術を融合させたすばらしいお茶汲み人形の開発に成功した.このお茶汲み人形は,下の図のように格子に区切られた家の中で,台所(K)でお茶の入った湯飲みを乗せるとそれを主人(M)の居場所まで持っていき,主人が飲み終わった湯飲みを乗せると台所まで戻っていくという仕事をする.ただし,この人形は台所と主人の間の往復経路を自分で見つけられるほど賢くはない.使用者はこの人形にプログラムをあらかじめ入力して,人形がきちんと往復できるようにする必要がある. 図 F-1: 家の構造の例 人形が受理する命令は「左に曲がる」と「右に曲がる」の 2 つであり,人形に入力可能なプログラムは命令の有限な列である.人形は台所を出発した後,壁(図中「#」で表されたマス)にぶつかるまで前進し,壁にぶつかると最初の命令に従って左または右に方向転換する.以降,再び壁にぶつかるまで前進し,ぶつかったら次の命令を実行し,以下同様に物事が進む.方向転換してもなお壁にぶつかって前進できないときは,その場所から移動しないまま次の命令を実行する.壁にぶつかったとき,実行する命令が残っていなければ人形は停止する.これをプログラムの順実行と呼ぶ. 主人の居場所から台所に戻るときは,プログラムの命令列を逆順に実行し,さらに左右反対に方向転換する.これをプログラムの逆実行と呼ぶ.命令の実行順序および方向転換以外の挙動は順実行と同じである. たとえば,上の図の家で,この人形に「右,右,左」というプログラムを与えると,往路では地点 a で右,地点 b で右,地点 c で左にそれぞれ方向転換して主人の居場所に到着する.また,復路では地点 c で右,地点 b で左,地点 a で左にそれぞれ方向転換して台所に帰還する.一方,「左,左」というプログラムを与えると,人形はまず地点 a で左に方向転換する.直進しようとするが,そのまま左側の壁にぶつかるため,地点 a にとどまったまま 2 番目の命令にしたがって左に方向転換する.そして直進して台所まで戻ってきたところで壁にぶつかり,命令が残っていないので停止する. ところで,JAG 氏は大きな過ちを犯していた.実は,どのようなプログラムを与えても主人の居場所までたどり着けない場合が存在するのである.また,主人の居場所に着いたとしても,湯飲みを台所まで持って帰れない場合もある. あなたの仕事は,ある家の中でこの人形を使ったときに,この人形が主人の居場所に到着できるか,そして台所に帰還できるかを判断するプログラムを書くことである. Input 入力は複数のデータセットからなり,最後に 0 0 と書かれた行が入力の終わりを示す.データセットの数は 128 個以下である. 各データセットの最初の行には,家の大きさを表す 2 つの整数 W , H がスペース区切りで書かれる.これらの整数は 3 ≦ W ≦ 64, 3 ≦ H ≦ 16 を満たす.続く H 行は部屋の構造を意味する.各行は W 文字から構成され,「 # 」は壁を,「 . 」(ピリオド)は床を,「 K 」は台所を,「 M 」は主人の居場所をそれぞれ示す.これら以外の文字は含まれない. この家の最外部は,常に壁(「 # 」)であることが保証されている.また「 K 」と「 M 」は,各データセットについて常に 1 つずつ存在して,3 方向が壁(「 # 」),残りの 1 方向が床(「 . 」)であると仮定してよい. Output 各データセットに対して,以下の記述のとおりにメッセージを 1 行に出力しなさい. どのようなプログラムを与えても主人の居場所に到着しない場合は「He cannot bring tea to his master.」 主人の居場所には到着するが,その後に台所に帰還するようにプログラムできない場合は「He cannot return to the kitchen.」 主人の居場所に到着して,台所に帰還するようにプログラムできる場合は「He can accomplish his mission.」 ここで,主人の居場所に到着するとは,人形を台所に置いて壁のない方向に向けた状態から,プログラムを順実行したときに,停止時点において主人の居場所に人形が存在することを指す.台所に帰還するとは,人形を主人の居場所に置いて壁のない方向に向けた状態から,プログラムを逆実行したときに,停止時点において台所に人形が存在することを指す.停止時点における人形の向きは問わない.また,往路,復路ともに,経路の途中で台所または主人を通過しても構わない.ただし,経路の途中で台所または主人を通過したとしても,停止時点において人形がそれ以外の場所にいるときは,主人の居場所に到着した,あるいは台所に帰還したとはみなされない. Sample Input 5 3 ##### #K.M# ##### 9 5 ######### #.....### #.###..M# #K####### ######### 9 5 ######### #K......# ####.#### ####M#### ######### 9 5 ######### #M......# ####.#### ####K#### ######### 7 9 ####### #####M# #####.# #.....# #.###.# #.....# #.##### #K##### ####### 7 6 ####### #####.# ##....# #K..#.# ###M#.# ####### 7 8 ####### ##...## ###.#M# #.....# #.#...# #.#..## #.K#### ####### 9 6 ######### ###..##.# ##......# #K....#.# ##..#M#.# ######### 9 6 ######### #.####### #....#M.# #.#...#.# ###K#...# ######### 12 7 ############ ###...####.# ##K#...M##.# ##.....#...# #........#.# ###..#...#.# ############ 23 16 ####################### #########...########### ##########.###.######## ##########.....######## ##########.#.#.###...## ########.#.#.######.### ########............### ########.###.######.### ############.######.### #K...........######.### ####.#######.######.### # ...(truncated)
[ { "submission_id": "aoj_2017_9409644", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint dx[] = {+1, 0, -1, 0};\nint dy[] = {0, +1, 0, -1};\n\nstring solve(const int w, const int h) {\n vector<string> a = in(h);\n int sx, sy, sd, tx, ty, td;\n for(int x : rep(h)) for(int y : rep(w)) {\n if(a[x][y] == 'K') tie(sx, sy) = make_pair(x, y), a[x][y] = '.';\n if(a[x][y] == 'M') tie(tx, ty) = make_pair(x, y), a[x][y] = '.';\n }\n for(int d : rep(4)) {\n if(a[sx + dx[d]][sy + dy[d]] == '.') sd = d;\n if(a[tx + dx[d]][ty + dy[d]] == '.') td = d;\n }\n\n const int n = h * w * 4;\n auto f = [&](int x, int y, int d) { return (x * w + y) * 4 + d; };\n auto f_inv = [&](int v) { return make_tuple((v / 4) / w, (v / 4) % w, v % 4); };\n\n vector<int> nxt(n, -1);\n for(int x : rep(h)) for(int y : rep(w)) for(int d : rep(4)) {\n if(a[x][y] == '.') {\n int nx = x, ny = y;\n while(a[nx + dx[d]][ny + dy[d]] == '.') nx += dx[d], ny += dy[d];\n nxt[f(x, y, d)] = f(nx, ny, d);\n }\n }\n bool one_way = [&] {\n vector vis(n, 0);\n queue<int> que;\n que.push(nxt[f(sx, sy, sd)]);\n while(not que.empty()) {\n int v = que.front(); que.pop();\n if(vis[v]) continue;\n vis[v] = true;\n auto [x, y, d] = f_inv(v);\n if(make_pair(x, y) == make_pair(tx, ty)) return true;\n int nv = nxt[f(x, y, (d + 1) % 4)];\n if(not vis[nv]) que.push(nv);\n nv = nxt[f(x, y, (d + 3) % 4)];\n if(not vis[nv]) que.push(nv);\n }\n return false;\n }();\n if(not one_way) return \"He cannot bring tea to his master.\"s;\n\n vector prev(2, vector(n, vector<int>()));\n for(int x : rep(h)) for(int y : rep(w)) for(int d : rep(4)) {\n if(a[x][y] == '.' and a[x + dx[d]][y + dy[d]] == '#') {\n prev[0][nxt[f(x, y, (d + 1) % 4)]].push_back(f(x, y, d));\n prev[1][nxt[f(x, y, (d + 3) % 4)]].push_back(f(x, y, d));\n }\n }\n bool two_way = [&] {\n vector vis(n, vector(n, 0));\n queue<pair<int,int>> que;\n for(int d : rep(4)) {\n if(a[sx + dx[d]][sy + dy[d]] == '#') {\n que.push({nxt[f(sx, sy, sd)], f(sx, sy, d)});\n }\n }\n while(not que.empty()) {\n auto [u, v] = que.front(); que.pop();\n if(vis[u][v]) continue;\n vis[u][v] = true;\n auto [ux, uy, ud] = f_inv(u);\n auto [vx, vy, vd] = f_inv(v);\n if(make_pair(ux, uy) == make_pair(tx, ty) and v == nxt[f(tx, ty, td)]) return true;\n int nu = nxt[f(ux, uy, (ud + 1) % 4)];\n for(int nv : prev[1][v]) if(not vis[nu][nv]) que.push({nu, nv});\n nu = nxt[f(ux, uy, (ud + 3) % 4)];\n for(int nv : prev[0][v]) if(not vis[nu][nv]) que.push({nu, nv});\n }\n return false;\n }();\n if(not two_way) return \"He cannot return to the kitchen.\"s;\n return \"He can accomplish his mission.\"s;\n}\n\nint main() {\n while(true) {\n int w = in(), h = in();\n if(make_pair(w, h) == make_pair(0, 0)) return 0;\n print(solve(w, h));\n }\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 69144, "score_of_the_acc": -0.7642, "final_rank": 16 }, { "submission_id": "aoj_2017_7861243", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T>\nbool chmax(T& a, const T& b) {\n return a < b ? (a = b, 1) : 0;\n}\ntemplate <typename T>\nbool chmin(T& a, const T& b) {\n return a > b ? (a = b, 1) : 0;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n const vector<int> di = {1, 0, -1, 0};\n const vector<int> dj = {0, 1, 0, -1};\n\n while (true) {\n int W, H;\n cin >> W >> H;\n if (W == 0 && H == 0) break;\n\n vector<string> grid(H);\n for (auto& x : grid) cin >> x;\n\n int M = 4 * W * H;\n\n auto encode = [&](int i, int j, int d) { return 4 * (W * i + j) + d; };\n auto decode = [&](int x) {\n return make_tuple(x / 4 / W, x / 4 % W, x % 4);\n };\n\n auto check = [&](int i, int j, int d) {\n int ni = i + di[d], nj = j + dj[d];\n return (0 <= ni && ni < H && 0 <= nj && nj < W &&\n grid[ni][nj] != '#');\n };\n\n vector<int> to(M);\n rep(i, 0, H) rep(j, 0, W) {\n if (grid[i][j] == '#') continue;\n rep(d, 0, 4) {\n int ni = i, nj = j;\n while (check(ni, nj, d)) {\n ni += di[d];\n nj += dj[d];\n }\n to[encode(i, j, d)] = encode(ni, nj, d);\n }\n }\n vector<vector<int>> nxt(2, vector<int>(M));\n vector<vector<vector<int>>> G_rev(2, vector<vector<int>>(M));\n rep(i, 0, H) rep(j, 0, W) {\n if (grid[i][j] == '#') continue;\n rep(d, 0, 4) {\n if (check(i, j, d)) continue;\n rep(k, 0, 2) {\n int nd = (d + (2 * k - 1) + 4) % 4;\n nxt[k][encode(i, j, d)] = to[encode(i, j, nd)];\n G_rev[k][to[encode(i, j, nd)]].push_back(encode(i, j, d));\n }\n }\n }\n\n stack<pair<int, int>> st;\n set<pair<int, int>> visited;\n\n stack<int> st1;\n set<int> visited1;\n\n int gi, gj;\n\n rep(i, 0, H) rep(j, 0, W) {\n if (grid[i][j] == 'K') {\n rep(d1, 0, 4) rep(d2, 0, 4) {\n if (check(i, j, d1)) {\n int x1 = encode(i, j, d1);\n int x2 = encode(i, j, d2);\n\n st.push({to[x1], x2});\n visited.insert({to[x1], x2});\n\n st1.push(to[x1]);\n visited1.insert(to[x1]);\n }\n }\n }\n if (grid[i][j] == 'M') {\n gi = i;\n gj = j;\n }\n }\n\n bool ok2 = false;\n\n while (!st.empty()) {\n auto [x1, x2] = st.top();\n st.pop();\n auto [i1, j1, d1] = decode(x1);\n auto [i2, j2, d2] = decode(x2);\n bool b1 = i1 == gi && j1 == gj;\n bool b2 = false;\n rep(d, 0, 4) {\n if (check(gi, gj, d) && to[encode(gi, gj, d)] == x2) {\n b2 = true;\n }\n }\n if (b1 && b2) {\n ok2 = true;\n break;\n }\n rep(k, 0, 2) {\n int z1 = nxt[k][x1];\n for (auto z2 : G_rev[k ^ 1][x2]) {\n if (!visited.count({z1, z2})) {\n st.push({z1, z2});\n visited.insert({z1, z2});\n }\n }\n }\n }\n if (ok2) {\n cout << \"He can accomplish his mission.\\n\";\n continue;\n }\n\n bool ok1 = false;\n\n while (!st1.empty()) {\n int x = st1.top();\n st1.pop();\n auto [i, j, d] = decode(x);\n if (i == gi && j == gj) {\n ok1 = true;\n break;\n }\n rep(k, 0, 2) {\n int z = nxt[k][x];\n if (!visited1.count(z)) {\n st1.push(z);\n visited1.insert(z);\n }\n }\n }\n\n if (ok1)\n cout << \"He cannot return to the kitchen.\";\n else\n cout << \"He cannot bring tea to his master.\";\n // cout << endl;\n cout << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 8068, "score_of_the_acc": -0.032, "final_rank": 1 }, { "submission_id": "aoj_2017_7137068", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define rep2(i, a, b) for(ll i = (a); i < (b); i++)\n#define vi vector<int>\n#define si(c) int(c.size())\n\nusing namespace std;\n\nvi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};\n\nint main() {\n while(true) {\n int w, h;\n cin >> w >> h;\n if(!h) exit(0);\n\n vector a(h, vi(w, 1));\n int sx[2], sy[2], dir[2];\n rep(i, h) rep(j, w) {\n char c;\n cin >> c;\n if(c == 'K') sx[0] = i, sy[0] = j;\n if(c == 'M') sx[1] = i, sy[1] = j;\n if(c == '#') a[i][j] = 0;\n }\n rep(i, 2) rep(j, 4) if(a[sx[i] + dx[j]][sy[i] + dy[j]]) dir[i] = j;\n\n auto id = [&](int i, int j, int dir) { return (i * w + j) * 4 + dir; };\n int N = h * w * 4, SX = id(sx[0], sy[0], dir[0]), SY = id(sx[1], sy[1], dir[1]);\n vector vis(N, vi(N));\n vector to(N, 0);\n vector cand(2, vector(N, vi()));\n queue<pair<int, int>> q;\n bool tomas = false, flag = false;\n\n rep(i, h) rep(j, w) rep(d, 4) {\n if(!a[i][j]) continue;\n int ni = i, nj = j;\n while(a[i + dx[d]][j + dy[d]]) i += dx[d], j += dy[d];\n to[id(ni, nj, d)] = id(i, j, d);\n i = ni, j = nj;\n }\n\n auto nd = [](int d, int t) { return (d + 1 + t * 2) % 4; };\n\n rep(i, h) rep(j, w) rep(d, 4) {\n if(!a[i][j]) continue;\n if(a[i + dx[d]][j + dy[d]]) continue;\n rep(t, 2) { cand[t][to[id(i, j, nd(d, t))]].emplace_back(id(i, j, d)); }\n }\n\n rep(d, 4) {\n if(a[sx[0] + dx[d]][sy[0] + dy[d]]) continue;\n vis[to[SX]][id(sx[0], sy[0], d)] = true;\n q.emplace(to[SX], id(sx[0], sy[0], d));\n }\n\n while(!empty(q)) {\n auto [x, y] = q.front();\n if(x / 4 == SY / 4 and y == to[SY]) {\n flag = true;\n break;\n }\n q.pop();\n rep(t, 2) {\n int nx = to[x / 4 * 4 + nd(x % 4, t)];\n for(auto ny : cand[t ^ 1][y]) {\n if(!vis[nx][ny]) vis[nx][ny] = true, q.emplace(nx, ny);\n }\n }\n }\n if(!flag) {\n queue<int> q;\n q.emplace(to[SX]);\n vi vis(N);\n vis[to[SX]] = true;\n while(!empty(q)) {\n auto x = q.front();\n if(x / 4 == SY / 4) tomas = true;\n q.pop();\n rep(t, 2) {\n int nx = to[x / 4 * 4 + nd(x % 4, t)];\n if(!vis[nx]) vis[nx] = true, q.emplace(nx);\n }\n }\n }\n\n if(flag) {\n cout << \"He can accomplish his mission.\";\n } else if(tomas) {\n cout << \"He cannot return to the kitchen.\";\n } else {\n cout << \"He cannot bring tea to his master.\";\n }\n cout << endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 69156, "score_of_the_acc": -0.6654, "final_rank": 14 }, { "submission_id": "aoj_2017_7137067", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define rep2(i, a, b) for(ll i = (a); i < (b); i++)\n#define vi vector<int>\n#define si(c) int(c.size())\n\nusing namespace std;\n\nvi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};\n\nint main() {\n while(true) {\n int w, h;\n cin >> w >> h;\n if(!h) exit(0);\n\n vector a(h, vi(w, 1));\n int sx[2], sy[2];\n rep(i, h) rep(j, w) {\n char c;\n cin >> c;\n if(c == 'K') sx[0] = i, sy[0] = j;\n if(c == 'M') sx[1] = i, sy[1] = j;\n if(c == '#') a[i][j] = 0;\n }\n int dir[2] = {};\n rep(i, 2) rep(j, 4) if(a[sx[i] + dx[j]][sy[i] + dy[j]]) dir[i] = j;\n\n auto id = [&](int i, int j, int dir) { return (i * w + j) * 4 + dir; };\n int N = h * w * 4;\n vector vis(N, vi(N));\n\n bool tomas = false;\n bool flag = false;\n\n vector to(N, 0);\n rep(i, h) rep(j, w) rep(d, 4) {\n if(!a[i][j]) continue;\n int ni = i, nj = j;\n while(a[i + dx[d]][j + dy[d]]) i += dx[d], j += dy[d];\n to[id(ni, nj, d)] = id(i, j, d);\n i = ni, j = nj;\n }\n int SX = id(sx[0], sy[0], dir[0]), SY = id(sx[1], sy[1], dir[1]);\n\n vector cand(2, vector(N, vi()));\n\n auto nd = [](int d, int t) { return (d + 1 + t * 2) % 4; };\n\n rep(i, h) rep(j, w) rep(d, 4) {\n if(!a[i][j]) continue;\n if(a[i + dx[d]][j + dy[d]]) continue;\n rep(t, 2) { cand[t][to[id(i, j, nd(d, t))]].emplace_back(id(i, j, d)); }\n }\n\n queue<pair<int, int>> q;\n rep(d, 4) {\n if(a[sx[0] + dx[d]][sy[0] + dy[d]]) continue;\n vis[to[SX]][id(sx[0], sy[0], d)] = true;\n q.emplace(to[SX], id(sx[0], sy[0], d));\n }\n\n while(!empty(q)) {\n auto [x, y] = q.front();\n if(x / 4 == SY / 4 and y == to[SY]) {\n flag = true;\n break;\n }\n q.pop();\n rep(t, 2) {\n int nx = to[x / 4 * 4 + nd(x % 4, t)];\n for(auto ny : cand[t ^ 1][y]) {\n if(!vis[nx][ny]) vis[nx][ny] = true, q.emplace(nx, ny);\n }\n }\n }\n if(!flag) {\n queue<int> q;\n q.emplace(to[SX]);\n vi vis(N);\n vis[to[SX]] = true;\n while(!empty(q)) {\n auto x = q.front();\n if(x / 4 == SY / 4) tomas = true;\n q.pop();\n rep(t, 2) {\n int nx = to[x / 4 * 4 + nd(x % 4, t)];\n if(!vis[nx]) vis[nx] = true, q.emplace(nx);\n }\n }\n }\n\n if(flag) {\n cout << \"He can accomplish his mission.\";\n } else if(tomas) {\n cout << \"He cannot return to the kitchen.\";\n } else {\n cout << \"He cannot bring tea to his master.\";\n }\n cout << endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 69192, "score_of_the_acc": -0.6658, "final_rank": 15 }, { "submission_id": "aoj_2017_7137064", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define rep2(i, a, b) for(ll i = (a); i < (b); i++)\n#define vi vector<int>\n#define si(c) int(c.size())\n\nusing namespace std;\n\nint main() {\n while(true) {\n int w, h;\n cin >> w >> h;\n if(!h) exit(0);\n\n vector a(h, vi(w, 1));\n int sx[2], sy[2];\n rep(i, h) rep(j, w) {\n char c;\n cin >> c;\n if(c == 'K') sx[0] = i, sy[0] = j;\n if(c == 'M') sx[1] = i, sy[1] = j;\n if(c == '#') a[i][j] = 0;\n }\n int dir[2] = {};\n vector<pair<int, int>> dx{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n rep(i, 2) {\n rep(j, 4) {\n auto [x, y] = dx[j];\n int nx = sx[i] + x, ny = sy[i] + y;\n if(a[nx][ny]) dir[i] = j;\n }\n }\n\n auto id = [&](int i, int j, int dir) { return (i * w + j) * 4 + dir; };\n int N = h * w * 4;\n vector vis(N, vi(N));\n\n bool tomas = false;\n bool flag = false;\n\n vector to(N, 0);\n rep(i, h) rep(j, w) rep(d, 4) {\n if(!a[i][j]) continue;\n int ni = i, nj = j;\n while(a[i + dx[d].first][j + dx[d].second]) i += dx[d].first, j += dx[d].second;\n to[id(ni, nj, d)] = id(i, j, d);\n assert(a[ni][nj]);\n i = ni, j = nj;\n }\n int SX = id(sx[0], sy[0], dir[0]), SY = id(sx[1], sy[1], dir[1]);\n\n vector<vi> rcand(N), lcand(N);\n rep(i, h) rep(j, w) rep(d, 4) {\n if(!a[i][j]) continue;\n if(a[i + dx[d].first][j + dx[d].second]) continue;\n int nd = (d + 1) % 4;\n rcand[to[id(i, j, nd)]].emplace_back(id(i, j, d));\n nd = (d + 3) % 4;\n lcand[to[id(i, j, nd)]].emplace_back(id(i, j, d));\n }\n\n queue<pair<int, int>> q;\n rep(d, 4) {\n if(a[sx[0] + dx[d].first][sy[0] + dx[d].second]) continue;\n vis[to[SX]][id(sx[0], sy[0], d)] = true;\n q.emplace(to[SX], id(sx[0], sy[0], d));\n }\n\n auto f = [&](int i) { cout << i / 4 / w << \" \" << i / 4 % w << \" \" << i % 4 << \" \"; };\n\n while(!empty(q)) {\n auto [x, y] = q.front();\n // f(x), f(y), cout << endl;\n if(x / 4 == SY / 4 and y == to[SY]) {\n flag = true;\n break;\n }\n q.pop();\n {\n int nx = x / 4 * 4 + (x % 4 + 1) % 4;\n nx = to[nx];\n for(auto ny : lcand[y]) {\n if(!vis[nx][ny]) vis[nx][ny] = true, q.emplace(nx, ny);\n }\n }\n {\n int nx = x / 4 * 4 + (x % 4 + 3) % 4;\n nx = to[nx];\n for(auto ny : rcand[y]) {\n if(!vis[nx][ny]) vis[nx][ny] = true, q.emplace(nx, ny);\n }\n }\n }\n if(!flag) {\n queue<int> q;\n q.emplace(to[SX]);\n vi vis(N);\n vis[to[SX]] = true;\n while(!empty(q)) {\n auto x = q.front();\n if(x / 4 == SY / 4) tomas = true;\n q.pop();\n for(auto d : {1, 3}) {\n int nx = x / 4 * 4 + (x % 4 + d) % 4;\n nx = to[nx];\n if(!vis[nx]) vis[nx] = true, q.emplace(nx);\n }\n }\n }\n\n if(flag) {\n cout << \"He can accomplish his mission.\";\n } else if(tomas) {\n cout << \"He cannot return to the kitchen.\";\n } else {\n cout << \"He cannot bring tea to his master.\";\n }\n cout << endl;\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 69212, "score_of_the_acc": -0.6604, "final_rank": 13 }, { "submission_id": "aoj_2017_6773636", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=300005;\nconst ll INF=1LL<<62;\n\nbool dp[16][64][4][16][64][4][2];\nbool can[16][64][4][2];\npair<int,int> to[16][64][4];\n\nvector<int> dh={0,1,0,-1},dw={1,0,-1,0};\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int H,W;cin>>W>>H;\n if(H==0) break;\n vector<string> S(H);\n for(int i=0;i<H;i++) cin>>S[i];\n int sh=-1,sw=-1,sd=-1,gh=-1,gw=-1,gd=-1;\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n if(S[i][j]=='K'){\n sh=i;\n sw=j;\n for(int k=0;k<4;k++){\n if(S[i+dh[k]][j+dw[k]]!='#'){\n //cout<<i+dh[k]<<\" \"<<j+dw[k]<<\" \"<<S[i+dh[k]][j+dw[k]]<<\" \"<<k<<endl;\n sd=k;\n }\n }\n }\n if(S[i][j]=='M'){\n gh=i;\n gw=j;\n for(int k=0;k<4;k++){\n if(S[i+dh[k]][j+dw[k]]!='#') gd=k;\n }\n }\n }\n }\n for(int a=0;a<H;a++) for(int b=0;b<W;b++) for(int c=0;c<4;c++) for(int d=0;d<H;d++) for(int e=0;e<W;e++) for(int f=0;f<4;f++) for(int g=0;g<2;g++) dp[a][b][c][d][e][f][g]=false;\n \n //cout<<sh<<\" \"<<sw<<\" \"<<sd<<\" \"<<gh<<\" \"<<gw<<\" \"<<gd<<endl;\n \n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n if(S[i][j]=='#') continue;\n for(int k=0;k<4;k++){\n int toh1=i,tow1=j;\n while(S[toh1+dh[k]][tow1+dw[k]]!='#'){\n toh1+=dh[k];\n tow1+=dw[k];\n }\n to[i][j][k]=mp(toh1,tow1);\n }\n }\n }\n \n queue<tuple<int,int,int,int,int,int,int>> Q;\n for(int k=0;k<4;k++){\n if(k!=sd){\n dp[sh][sw][sd][sh][sw][k][0]=true;\n Q.push({sh,sw,sd,sh,sw,k,0});\n }\n }\n while(!Q.empty()){\n auto [h1,w1,d1,h2,w2,d2,f]=Q.front();Q.pop();\n if(f==0){\n assert(S[h2+dh[d2]][w2+dw[d2]]=='#');\n auto [toh1,tow1]=to[h1][w1][d1];\n int toh2=h2,tow2=w2;\n while(1){\n if(!dp[toh1][tow1][d1][toh2][tow2][d2][1]){\n dp[toh1][tow1][d1][toh2][tow2][d2][1]=true;\n Q.push({toh1,tow1,d1,toh2,tow2,d2,1});\n }\n toh2-=dh[d2];\n tow2-=dw[d2];\n \n if(S[toh2][tow2]=='#') break;\n }\n }else{\n assert(S[h1+dh[d1]][w1+dw[d1]]=='#');\n for(int k=1;k<4;k+=2){\n int tod1=(d1+k)%4,tod2=(d2+k)%4;\n if(S[h2+dh[tod2]][w2+dw[tod2]]!='#') continue;\n if(!dp[h1][w1][tod1][h2][w2][tod2][0]){\n dp[h1][w1][tod1][h2][w2][tod2][0]=true;\n Q.push({h1,w1,tod1,h2,w2,tod2,0});\n }\n }\n }\n }\n \n bool f=false;\n \n for(int k=0;k<4;k++){\n if(gd==k) continue;\n f|=dp[gh][gw][k][gh][gw][gd][1];\n }\n \n if(f){\n cout<<\"He can accomplish his mission.\\n\";\n continue;\n }\n \n for(int a=0;a<H;a++) for(int b=0;b<W;b++) for(int c=0;c<4;c++) for(int g=0;g<2;g++) can[a][b][c][g]=false;\n \n queue<tuple<int,int,int,int>> QQ;\n QQ.push({sh,sw,sd,0});\n while(!QQ.empty()){\n auto [h,w,d,f]=QQ.front();QQ.pop();\n if(f==0){\n auto [toh,tow]=to[h][w][d];\n if(!can[toh][tow][d][1]){\n can[toh][tow][d][1]=true;\n QQ.push({toh,tow,d,1});\n }\n }else{\n for(int k=1;k<4;k+=2){\n int tod=(d+k)%4;\n if(!can[h][w][tod][0]){\n can[h][w][tod][0]=true;\n QQ.push({h,w,tod,0});\n }\n }\n }\n }\n \n if(can[gh][gw][gd^2][1]) cout<<\"He cannot return to the kitchen.\\n\";\n else cout<<\"He cannot bring tea to his master.\\n\";\n }\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 36612, "score_of_the_acc": -0.4167, "final_rank": 10 }, { "submission_id": "aoj_2017_6107336", "code_snippet": "#include <cassert>\n#include <iostream>\n#include <string>\n#include <map>\n#include <set>\n#include <tuple>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nconst int MAX_H = 20;\nconst int MAX_W = 70;\nconst int dx[] = {-1, 0, 1, 0};\nconst int dy[] = {0, 1, 0, -1};\nconst int UP = 0;\nconst int RIGHT = 1;\nconst int DOWN = 2;\nconst int LEFT = 3;\nconst int DIRECT[] = {UP, RIGHT, DOWN, LEFT};\n// x0, y0, d0, x1, y1, d1\nusing Pos = tuple<int, int, int, int, int, int>;\n\nint h, w;\nstring field[MAX_H];\nmap<Pos, Pos> used;\n\nstring solve() {\n for(int i = 0; i < h; i++) {\n cin >> field[i];\n }\n used.clear();\n int mx = -1, my = -1, md = -1;\n int kx = -1, ky = -1, kd = -1;\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n if(field[i][j] == 'K' || field[i][j] == 'M') {\n int d = -1;\n for(int k = 0; k < 4; k++) {\n int nx = i + dx[k], ny = j + dy[k];\n if(field[nx][ny] != '#') {\n d = k;\n break;\n }\n }\n assert(d != -1);\n if(field[i][j] == 'K') {\n kx = i;\n ky = j;\n kd = d;\n } else {\n mx = i;\n my = j;\n md = d;\n }\n }\n }\n }\n assert(mx != -1);\n assert(my != -1);\n queue<Pos> q;\n for(int i = 0; i < 4; i++) {\n q.push(make_tuple(mx, my, md, mx, my, i));\n used[make_tuple(mx, my, md, mx, my, i)] = \n make_tuple(-1, -1, -1, -1, -1, -1);\n }\n bool can_return = false, can_reach = false;\n while(!q.empty()) {\n auto now = q.front(); q.pop();\n int x0 = get<0>(now), y0 = get<1>(now), d0 = get<2>(now),\n x1 = get<3>(now), y1 = get<4>(now), d1 = get<5>(now);\n // cerr << x0 << \" \" << y0 << \" \" << d0 << \" \"\n // << x1 << \" \" << y1 << \" \" << d1 << endl;\n assert(field[x0][y0] != '#');\n assert(field[x1][y1] != '#');\n while(field[x0 + dx[d0]][y0 + dy[d0]] != '#') {\n x0 += dx[d0]; y0 += dy[d0];\n }\n if(x1 == kx && y1 == ky) {\n can_reach = true;\n }\n while(field[x1][y1] != '#') {\n for(int k = 1; k < 4; k += 2) {\n int nd0 = (d0 + k) % 4, nd1 = (d1 + k) % 4;\n if(x0 == kx && y0 == ky && x1 == kx && y1 == ky && d1 == (kd + 2) % 4) {\n can_return = true;\n // auto f = now;\n // while(get<0>(f) != -1) {\n // cout << get<0>(f) << \" \" << get<1>(f) << \" \" << get<2>(f) << \" \"\n // << get<3>(f) << \" \" << get<4>(f) << \" \" << get<5>(f) << endl;\n // f = used[f];\n // }\n break;\n }\n // cerr << x1 - dx[nd1] << \" \" << y1 - dy[nd1] << endl;\n if(field[x1 - dx[nd1]][y1 - dy[nd1]] == '#') {\n Pos nxt = make_tuple(x0, y0, nd0, x1, y1, nd1);\n if(!used.count(nxt)) {\n used[nxt] = now;\n q.push(nxt);\n }\n }\n }\n x1 += dx[d1];\n y1 += dy[d1];\n }\n }\n if(can_return) return \"He can accomplish his mission.\";\n else if(can_reach) return \"He cannot return to the kitchen.\";\n else return \"He cannot bring tea to his master.\";\n}\n\nint main() {\n vector<string> ans;\n while(1) {\n cin >> w >> h;\n if(w == 0 && h == 0) break;\n ans.push_back(solve());\n }\n for(auto &i : ans) cout << i << endl;\n}", "accuracy": 1, "time_ms": 2680, "memory_kb": 29764, "score_of_the_acc": -0.9577, "final_rank": 17 }, { "submission_id": "aoj_2017_6035963", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nstruct Graph {\n int V;\n vector<vector<int>> G;\n Graph() {}\n Graph(int V) : V(V) { G.resize(V); }\n void add_edge(int a, int b) { G[a].push_back(b); }\n size_t size() const { return G.size(); }\n const vector<int> &operator[](int id) const { return G[id]; }\n};\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, -1, 0, 1};\n\nbool solve() {\n int h, w;\n cin >> w >> h;\n\n if (h == 0 && w == 0) return false;\n\n vector<string> f(h);\n cin >> f;\n\n auto out_of_borders = [&](int x, int y) {\n return (x < 0 || x >= h || y < 0 || y >= w);\n };\n\n auto go = vect(h, vect(w, vect(4, pii(-1, -1))));\n auto rev = vect(h, vect(w, vect(4, vector<pii>())));\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (f[i][j] == '#') continue;\n for (int d = 0; d < 4; d++) {\n int x = i, y = j;\n while (!out_of_borders(x + dx[d], y + dy[d]) &&\n f[x + dx[d]][y + dy[d]] != '#') {\n x += dx[d];\n y += dy[d];\n }\n go[i][j][d] = pii(x, y);\n rev[x][y][d].emplace_back(i, j);\n }\n }\n }\n\n auto encode = [&](int ax, int ay, int ad, int bx, int by, int bd) {\n return (4 * w * ax + 4 * ay + ad) * 4 * w * h + (4 * w * bx + 4 * by + bd);\n };\n\n auto decode = [&](int id, int &ax, int &ay, int &ad, int &bx, int &by,\n int &bd) {\n bd = id % 4, id /= 4;\n by = id % w, id /= w;\n bx = id % h, id /= h;\n ad = id % 4, id /= 4;\n ay = id % w, id /= w;\n ax = id % h, assert(id / h == 0);\n return;\n };\n\n int sx = -1, sy = -1, sd = -1, gx = -1, gy = -1, gd = -1;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (f[i][j] == 'K') {\n sx = i, sy = j;\n for (int d = 0; d < 4; d++) {\n int ni = i + dx[d], nj = j + dy[d];\n if (!out_of_borders(ni, nj) && f[ni][nj] != '#') sd = d;\n }\n }\n if (f[i][j] == 'M') {\n gx = i, gy = j;\n for (int d = 0; d < 4; d++) {\n int ni = i + dx[d], nj = j + dy[d];\n if (!out_of_borders(ni, nj) && f[ni][nj] != '#') gd = d;\n }\n }\n }\n }\n dmp(sx, sy, sd, gx, gy, gd);\n\n for (int d = 0; d < 4; d++) {\n if (d == sd) continue;\n auto [ssx, ssy] = go[sx][sy][sd];\n int S = encode(ssx, ssy, sd, sx, sy, d);\n\n vector<int> dist;\n dist.assign(4 * h * w * 4 * h * w, numeric_limits<int>::max());\n dist[S] = 0;\n queue<int> q;\n q.push(S);\n while (!q.empty()) {\n auto v = q.front();\n q.pop();\n int ai, aj, ad, bi, bj, bd;\n decode(v, ai, aj, ad, bi, bj, bd);\n {\n int nai = ai + dx[ad], naj = aj + dy[ad];\n assert(!out_of_borders(nai, naj) && f[nai][naj] == '#');\n int nbi = bi + dx[bd], nbj = bj + dy[bd];\n assert(!out_of_borders(nbi, nbj) && f[nbi][nbj] == '#');\n }\n // turn right and go straight\n {\n int nad = (ad + 1) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int nbd = (bd + 1) % 4;\n for (auto [nbi, nbj] : rev[bi][bj][bd]) {\n int nnbi = nbi + dx[nbd], nnbj = nbj + dy[nbd];\n assert(!out_of_borders(nnbi, nnbj));\n if (f[nnbi][nnbj] != '#') continue;\n int u = encode(nai, naj, nad, nbi, nbj, nbd);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n }\n // turn left and go straight\n {\n int nad = (ad + 3) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int nbd = (bd + 3) % 4;\n for (auto [nbi, nbj] : rev[bi][bj][bd]) {\n int nnbi = nbi + dx[nbd], nnbj = nbj + dy[nbd];\n assert(!out_of_borders(nnbi, nnbj));\n if (f[nnbi][nnbj] != '#') continue;\n int u = encode(nai, naj, nad, nbi, nbj, nbd);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n }\n }\n\n auto [ggx, ggy] = go[gx][gy][gd];\n for (int e = 0; e < 4; e++) {\n int id = encode(gx, gy, e, ggx, ggy, gd);\n if (dist[id] == 0) {\n cout << \"He can accomplish his mission.\" << endl;\n return true;\n }\n }\n }\n\n {\n auto [ssx, ssy] = go[sx][sy][sd];\n int S = encode(ssx, ssy, sd, 0, 0, 0);\n vector<int> dist;\n dist.assign(4 * h * w * 4 * h * w, numeric_limits<int>::max());\n dist[S] = 0;\n queue<int> q;\n q.push(S);\n while (!q.empty()) {\n auto v = q.front();\n q.pop();\n int ai, aj, ad, bi, bj, bd;\n decode(v, ai, aj, ad, bi, bj, bd);\n {\n int nai = ai + dx[ad], naj = aj + dy[ad];\n assert(!out_of_borders(nai, naj) && f[nai][naj] == '#');\n }\n // turn right and go straight\n {\n int nad = (ad + 1) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int u = encode(nai, naj, nad, 0, 0, 0);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n // turn left and go straight\n {\n int nad = (ad + 3) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int u = encode(nai, naj, nad, 0, 0, 0);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n }\n int T = encode(gx, gy, (gd + 2) % 4, 0, 0, 0);\n if (dist[T] == 0) {\n cout << \"He cannot return to the kitchen.\" << endl;\n return true;\n }\n }\n\n cout << \"He cannot bring tea to his master.\" << endl;\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 101532, "score_of_the_acc": -1.0734, "final_rank": 19 }, { "submission_id": "aoj_2017_6035961", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nstruct Graph {\n int V;\n vector<vector<int>> G;\n Graph() {}\n Graph(int V) : V(V) { G.resize(V); }\n void add_edge(int a, int b) { G[a].push_back(b); }\n size_t size() const { return G.size(); }\n const vector<int> &operator[](int id) const { return G[id]; }\n};\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, -1, 0, 1};\n\nbool solve() {\n int h, w;\n cin >> w >> h;\n\n if (h == 0 && w == 0) return false;\n\n vector<string> f(h);\n cin >> f;\n\n auto out_of_borders = [&](int x, int y) {\n return (x < 0 || x >= h || y < 0 || y >= w);\n };\n\n auto go = vect(h, vect(w, vect(4, pii(-1, -1))));\n auto rev = vect(h, vect(w, vect(4, vector<pii>())));\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (f[i][j] == '#') continue;\n for (int d = 0; d < 4; d++) {\n int x = i, y = j;\n while (!out_of_borders(x + dx[d], y + dy[d]) &&\n f[x + dx[d]][y + dy[d]] != '#') {\n x += dx[d];\n y += dy[d];\n }\n go[i][j][d] = pii(x, y);\n rev[x][y][d].emplace_back(i, j);\n }\n }\n }\n\n auto encode = [&](int ax, int ay, int ad, int bx, int by, int bd) {\n return (4 * w * ax + 4 * ay + ad) * 4 * w * h + (4 * w * bx + 4 * by + bd);\n };\n\n auto decode = [&](int id, int &ax, int &ay, int &ad, int &bx, int &by,\n int &bd) {\n bd = id % 4, id /= 4;\n by = id % w, id /= w;\n bx = id % h, id /= h;\n ad = id % 4, id /= 4;\n ay = id % w, id /= w;\n ax = id % h, assert(id / h == 0);\n return;\n };\n\n int sx = -1, sy = -1, sd = -1, gx = -1, gy = -1, gd = -1;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (f[i][j] == 'K') {\n sx = i, sy = j;\n for (int d = 0; d < 4; d++) {\n int ni = i + dx[d], nj = j + dy[d];\n if (!out_of_borders(ni, nj) && f[ni][nj] != '#') sd = d;\n }\n }\n if (f[i][j] == 'M') {\n gx = i, gy = j;\n for (int d = 0; d < 4; d++) {\n int ni = i + dx[d], nj = j + dy[d];\n if (!out_of_borders(ni, nj) && f[ni][nj] != '#') gd = d;\n }\n }\n }\n }\n dmp(sx, sy, sd, gx, gy, gd);\n\n // Graph g(4 * h * w * 4 * h * w);\n // for (int ai = 0; ai < h; ai++) {\n // for (int aj = 0; aj < w; aj++) {\n // for (int ad = 0; ad < 4; ad++) {\n // for (int bi = 0; bi < h; bi++) {\n // for (int bj = 0; bj < w; bj++) {\n // for (int bd = 0; bd < 4; bd++) {\n // int u = encode(ai, aj, ad, bi, bj, bd);\n // // turn right\n // {\n // int nad = (ad + 1) % 4;\n // auto [nai, naj] = go[ai][aj][nad];\n // int nbd = (bd + 1) % 4;\n // for (auto [nbi, nbj] : rev[bi][bj][bd]) {\n // int v = encode(nai, naj, nad, nbi, nbj, nbd);\n // g.add_edge(u, v);\n // }\n // }\n // // turn left\n // {\n // int nad = (ad + 3) % 4;\n // auto [nai, naj] = go[ai][aj][nad];\n // int nbd = (bd + 3) % 4;\n // for (auto [nbi, nbj] : rev[bi][bj][bd]) {\n // int v = encode(nai, naj, nad, nbi, nbj, nbd);\n // g.add_edge(u, v);\n // }\n // }\n // }\n // }\n // }\n // }\n // }\n // }\n\n for (int d = 0; d < 4; d++) {\n if (d == sd) continue;\n auto [ssx, ssy] = go[sx][sy][sd];\n int S = encode(ssx, ssy, sd, sx, sy, d);\n\n vector<int> dist;\n dist.assign(4 * h * w * 4 * h * w, numeric_limits<int>::max());\n dist[S] = 0;\n queue<int> q;\n q.push(S);\n while (!q.empty()) {\n auto v = q.front();\n q.pop();\n int ai, aj, ad, bi, bj, bd;\n decode(v, ai, aj, ad, bi, bj, bd);\n {\n int nai = ai + dx[ad], naj = aj + dy[ad];\n assert(!out_of_borders(nai, naj) && f[nai][naj] == '#');\n int nbi = bi + dx[bd], nbj = bj + dy[bd];\n assert(!out_of_borders(nbi, nbj) && f[nbi][nbj] == '#');\n }\n // turn right and go straight\n {\n int nad = (ad + 1) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int nbd = (bd + 1) % 4;\n for (auto [nbi, nbj] : rev[bi][bj][bd]) {\n int nnbi = nbi + dx[nbd], nnbj = nbj + dy[nbd];\n assert(!out_of_borders(nnbi, nnbj));\n if (f[nnbi][nnbj] != '#') continue;\n int u = encode(nai, naj, nad, nbi, nbj, nbd);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n }\n // turn left and go straight\n {\n int nad = (ad + 3) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int nbd = (bd + 3) % 4;\n for (auto [nbi, nbj] : rev[bi][bj][bd]) {\n int nnbi = nbi + dx[nbd], nnbj = nbj + dy[nbd];\n assert(!out_of_borders(nnbi, nnbj));\n if (f[nnbi][nnbj] != '#') continue;\n int u = encode(nai, naj, nad, nbi, nbj, nbd);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n }\n }\n\n auto [ggx, ggy] = go[gx][gy][gd];\n for (int e = 0; e < 4; e++) {\n int id = encode(gx, gy, e, ggx, ggy, gd);\n if (dist[id] == 0) {\n cout << \"He can accomplish his mission.\" << endl;\n return true;\n }\n }\n }\n\n {\n auto [ssx, ssy] = go[sx][sy][sd];\n int S = encode(ssx, ssy, sd, 0, 0, 0);\n vector<int> dist;\n dist.assign(4 * h * w * 4 * h * w, numeric_limits<int>::max());\n dist[S] = 0;\n queue<int> q;\n q.push(S);\n while (!q.empty()) {\n auto v = q.front();\n q.pop();\n int ai, aj, ad, bi, bj, bd;\n decode(v, ai, aj, ad, bi, bj, bd);\n {\n int nai = ai + dx[ad], naj = aj + dy[ad];\n assert(!out_of_borders(nai, naj) && f[nai][naj] == '#');\n }\n // turn right and go straight\n {\n int nad = (ad + 1) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int u = encode(nai, naj, nad, 0, 0, 0);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n // turn left and go straight\n {\n int nad = (ad + 3) % 4;\n auto [nai, naj] = go[ai][aj][nad];\n int u = encode(nai, naj, nad, 0, 0, 0);\n if (dist[u] != 0) {\n dist[u] = 0;\n q.push(u);\n }\n }\n }\n int T = encode(gx, gy, (gd + 2) % 4, 0, 0, 0);\n if (dist[T] == 0) {\n cout << \"He cannot return to the kitchen.\" << endl;\n return true;\n }\n }\n\n cout << \"He cannot bring tea to his master.\" << endl;\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 101396, "score_of_the_acc": -1.0692, "final_rank": 18 }, { "submission_id": "aoj_2017_5987029", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.10.21 18:34:32 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region modint\n\nconst int mod = mod_1000000007;\n\ntemplate <int mod>\nstruct ModInt {\n\tint x;\n\tModInt() : x(0) {}\n\tModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\tModInt &operator+=(const ModInt &p) {\n\t\tif((x += p.x) >= mod) x -= mod;\n\t\treturn *this;\n\t}\n\tModInt &operator-=(const ModInt &p) {\n\t\tif((x += mod - p.x) >= mod) x -= mod;\n\t\treturn *this;\n\t}\n\tModInt &operator*=(const ModInt &p) {\n\t\tx = (int)(1LL * x * p.x % mod);\n\t\treturn *this;\n\t}\n\tModInt &operator/=(const ModInt &p) {\n\t\t*this *= p.inverse();\n\t\treturn *this;\n\t}\n\tModInt operator-() const { return ModInt(-x); }\n\tModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n\tModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n\tModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n\tModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n\tbool operator==(const ModInt &p) const { return x == p.x; }\n\tbool operator!=(const ModInt &p) const { return x != p.x; }\n\tModInt inverse() const {\n\t\tint a = x, b = mod, u = 1, v = 0, t;\n\t\twhile(b > 0) {\n\t\t\tt = a / b;\n\t\t\tswap(a -= t * b, b);\n\t\t\tswap(u -= t * v, v);\n\t\t}\n\t\treturn ModInt(u);\n\t}\n\tModInt pow(long long n) const {\n\t\tModInt ret(1), mul(x);\n\t\twhile(n > 0) {\n\t\t\tif(n & 1) ret *= mul;\n\t\t\tmul *= mul;\n\t\t\tn >>= 1;\n\t\t}\n\t\treturn ret;\n\t}\n\tfriend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }\n\tfriend istream &operator>>(istream &is, ModInt &a) {\n\t\tlong long t;\n\t\tis >> t;\n\t\ta = ModInt<mod>(t);\n\t\treturn (is);\n\t}\n\tstatic int get_mod() { return mod; }\n};\nusing mint = ModInt<mod>;\n\n#pragma endregion\n\nint cnt = 0;\nstring accomplish = \"He can accomplish his mission.\";\nstring can_master_cannot_kitchen = \"He cannot return to the kitchen.\";\nstring cannot_bring = \"He cannot bring tea to his master.\";\n\nstring solve(int w) {\n\tint h;\n\tcin >> h;\n\n\tVEC(string, s, h);\n\n\tint kx, ky, mx, my;\n\trep(i, h) rep(j, w) {\n\t\tif(s[i][j] == 'K') {\n\t\t\tkx = i;\n\t\t\tky = j;\n\t\t} else if(s[i][j] == 'M') {\n\t\t\tmx = i;\n\t\t\tmy = j;\n\t\t}\n\t}\n\n\tauto wall = [&s](int x, int y) { return s[x][y] == '#'; };\n\n\tusing state = tuple<int, int, int, int, int, int>;\n\n\tqueue<state> q;\n\tset<state> se;\n\n\trep(i, 4) rep(j, 4) {\n\t\tif(wall(kx + dx[i], ky + dy[i])) continue;\n\t\tstate st(kx, ky, kx, ky, i, j);\n\t\tq.push(st);\n\t\tse.insert(st);\n\t}\n\n\t// after change his direction, before move foward\n\n\tauto add_state = [&q, &se](int gx, int gy, int tx, int ty, int gdir, int tdir) {\n\t\tstate st(gx, gy, tx, ty, gdir, tdir);\n\t\tif(!se.count(st)) {\n\t\t\tse.insert(st);\n\t\t\tq.push(st);\n\t\t}\n\t\treturn;\n\t};\n\n\tdebug(s);\n\n\twhile(!q.empty()) {\n\t\tint gx, gy, tx, ty, gdir, tdir;\n\t\ttie(gx, gy, tx, ty, gdir, tdir) = q.front();\n\t\tq.pop();\n\n\t\tif(cnt == 8) debug(gx, gy, tx, ty, gdir, tdir);\n\n\t\tint tback = (tdir + 2) % 4;\n\t\t{\n\t\t\tint tbx, tby;\n\t\t\ttbx = tx + dx[tback];\n\t\t\ttby = ty + dy[tback];\n\t\t\tif(!wall(tbx, tby)) continue;\n\t\t}\n\n\t\twhile(!wall(gx + dx[gdir], gy + dy[gdir])) {\n\t\t\tgx += dx[gdir];\n\t\t\tgy += dy[gdir];\n\t\t}\n\n\t\twhile(!wall(tx, ty)) {\n\t\t\tif(gx == mx && gy == my) {\n\t\t\t\tif(tx == mx && ty == my && !wall(mx + dx[tback], my + dy[tback])) {\n\t\t\t\t\treturn accomplish;\n\t\t\t\t}\n\t\t\t}\n\t\t\tadd_state(gx, gy, tx, ty, (gdir + 1) % 4, (tdir + 1) % 4);\n\t\t\tadd_state(gx, gy, tx, ty, (gdir + 3) % 4, (tdir + 3) % 4);\n\t\t\ttx += dx[tdir];\n\t\t\tty += dy[tdir];\n\t\t}\n\t}\n\n\tse.clear();\n\n\trep(i, 4) {\n\t\tif(wall(kx + dx[i], ky + dy[i])) continue;\n\t\tstate st(kx, ky, kx, ky, i, i);\n\t\tadd_state(kx, ky, kx, ky, i, i);\n\t}\n\n\twhile(!q.empty()) {\n\t\tint gx, gy, tx, ty, gdir, tdir;\n\t\ttie(gx, gy, ignore, ignore, gdir, ignore) = q.front();\n\t\tq.pop();\n\n\t\tif(cnt == 8) debug(gx, gy, tx, ty, gdir, tdir);\n\n\t\t// int tback = (tdir + 2) % 4;\n\t\t// {\n\t\t// \tint tbx, tby;\n\t\t// \ttbx = tx + dx[tback];\n\t\t// \ttby = ty + dy[tback];\n\t\t// \tif(!wall(tbx, tby)) continue;\n\t\t// }\n\n\t\twhile(!wall(gx + dx[gdir], gy + dy[gdir])) {\n\t\t\tgx += dx[gdir];\n\t\t\tgy += dy[gdir];\n\t\t}\n\n\t\t// while(!wall(tx, ty)) {\n\t\tif(gx == mx && gy == my) return can_master_cannot_kitchen;\n\t\t// \t\tif(tx == mx && ty == my && !wall(mx + dx[tback], my + dy[tback])) {\n\t\t// \t\t\treturn accomplish;\n\t\t// \t\t}\n\t\t// \t}\n\t\tadd_state(gx, gy, 0, 0, (gdir + 1) % 4, 0);\n\t\tadd_state(gx, gy, 0, 0, (gdir + 3) % 4, 0);\n\t\t// \ttx += dx[tdir];\n\t\t// \tty += dy[tdir];\n\t\t// }\n\t}\n\n\treturn cannot_bring;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n && n) {\n\t\tcnt++;\n\t\tcout << solve(n) << dl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3700, "memory_kb": 47216, "score_of_the_acc": -1.4292, "final_rank": 20 }, { "submission_id": "aoj_2017_5936878", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\n#include<complex>\n#include<numeric>\n#include<functional>\n#include<unordered_map>\n#include<unordered_set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,LL> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n\tfor(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n\tfor(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n\tif(vec_n==-1)vec_n=vec_s.size();\n\tfor(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n\tif(vec_n==-1)vec_n=vec_s.size();\n\tfor(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\ntemplate<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){\n\tint n=v1.size();\n\tfor(int i=0;i<n;i++){\n\t\tif(i)os<<\" \";\n\t\tos<<v1[i];\n\t}\n\treturn os;\n}\ntemplate<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){\n\tos<<p.first<<\" \"<<p.second;\n\treturn os;\n}\ntemplate<typename T> istream& operator>>(istream& is,vector<T>& v1){\n\tint n=v1.size();\n\tfor(int i=0;i<n;i++)is>>v1[i];\n\treturn is;\n}\ntemplate<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){\n\tis>>p.first>>p.second;\n\treturn is;\n}\n\nint n,m;\nnamespace sol{\n\ttypedef vector<char> V1;\n\ttypedef vector<V1> V2;\n\ttypedef vector<V2> V3;\n\ttypedef vector<V3> V4;\n\ttypedef vector<V4> V5;\n\ttypedef vector<V5> V6;\n\tconst int move[4][2]={{1,0},{0,1},{-1,0},{0,-1}};\n\n\tint check_to(int x,int y,vector<string>& grid){\n\t\tfor(int i=0;i<4;i++){\n\t\t\tif(grid[x+move[i][0]][y+move[i][1]]!='#')return i;\n\t\t}\n\t\tassert(false);\n\t\treturn -1;\n\t}\n\tbool dp[16][64][16][64][4][4];\n\n\tvoid solve(){\n\t\tint i,j,k;\n\t\tint a,b,c,d,e;\n\t\tint x,y;\n\t\tint p,q;\n\t\tint sto,eto;\n\t\tint sx,sy,ex,ey;\n\t\tvector<string> grid(n);\n\t\tcin>>grid;\n\t\tV3 tx(n,V2(m,V1(4)));\n\t\tV3 ty(n,V2(m,V1(4)));\n\t\tmemset(dp,false,sizeof(dp));\n\t\tfor(i=0;i<n;i++){\n\t\t\tfor(j=0;j<m;j++){\n\t\t\t\tfor(k=0;k<4;k++){\n\t\t\t\t\tx=i,y=j;\n\t\t\t\t\tfor(;grid[x][y]!='#';x+=move[k][0],y+=move[k][1]);\n\t\t\t\t\tx-=move[k][0],y-=move[k][1];\n\t\t\t\t\ttx[i][j][k]=x;\n\t\t\t\t\tty[i][j][k]=y;\n\t\t\t\t}\n\t\t\t\tif(grid[i][j]=='K'){\n\t\t\t\t\tsto=check_to(i,j,grid);\n\t\t\t\t\tsx=i,sy=j;\n\t\t\t\t}\n\t\t\t\tif(grid[i][j]=='M'){\n\t\t\t\t\teto=check_to(i,j,grid);\n\t\t\t\t\tex=i,ey=j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tqueue<tuple<int,int,int,int,int,int>> q1;\n\t\tfor(i=0;i<4;i++){\n\t\t\tx=tx[sx][sy][sto],y=ty[sx][sy][sto];\n\t\t\tdp[x][y][sx][sy][sto][i]=true;\n\t\t\tq1.push({x,y,sx,sy,sto,i});\n\t\t}\n\t\twhile(!q1.empty()){\n\t\t\ttie(x,y,a,b,c,d)=q1.front(),q1.pop();\n\t\t\tp=a+move[d][0],q=b+move[d][1];\n\t\t\tif(grid[p][q]!='#' && !dp[x][y][p][q][c][d]){\n\t\t\t\tdp[x][y][p][q][c][d]=true;\n\t\t\t\tq1.push({x,y,p,q,c,d});\n\t\t\t}\n\t\t\tauto func=[&](int g,int h) -> void{\n\t\t\t\tg=(g+4)%4,h=(h+4)%4;\n\t\t\t\te=(h+2)%4;\n\t\t\t\tp=a+move[e][0],q=b+move[e][1];\n\t\t\t\tif(grid[p][q]!='#')return;\n\t\t\t\tp=tx[x][y][g],q=ty[x][y][g];\n\t\t\t\tif(!dp[p][q][a][b][g][h]){\n\t\t\t\t\tdp[p][q][a][b][g][h]=true;\n\t\t\t\t\tq1.push({p,q,a,b,g,h});\n\t\t\t\t}\n\t\t\t};\n\t\t\tfunc(c+1,d+1);\n\t\t\tfunc(c-1,d-1);\n\t\t}\n\t\tfor(i=0;i<4;i++){\n\t\t\tj=(eto+2)%4;\n\t\t\tif(dp[ex][ey][ex][ey][i][j]){\n\t\t\t\tcout<<\"He can accomplish his mission.\"<<endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\t\n\t\tV3 dp2(n,V2(m,V1(4)));\n\t\tqueue<tuple<int,int,int>> q2;\n\t\tdp2[sx][sy][sto]=1;\n\t\tq2.push({sx,sy,sto});\n\t\twhile(!q2.empty()){\n\t\t\ttie(a,b,c)=q2.front(),q2.pop();\n\t\t\tfor(i=-1;i<=1;i+=2){\n\t\t\t\td=(c+i+4)%4;\n\t\t\t\tx=tx[a][b][d],y=ty[a][b][d];\n\t\t\t\tif(dp2[x][y][d]==0){\n\t\t\t\t\tdp2[x][y][d]=1;\n\t\t\t\t\tq2.push({x,y,d});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(i=0;i<4;i++){\n\t\t\tif(dp2[ex][ey][i]){\n\t\t\t\tcout<<\"He cannot return to the kitchen.\"<<endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tcout<<\"He cannot bring tea to his master.\"<<endl;\n\t}\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\twhile(cin>>m>>n){\n\t\tif(n==0 && m==0)return 0;\n\t\tsol::solve();\n\t}\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 20240, "score_of_the_acc": -0.1938, "final_rank": 5 }, { "submission_id": "aoj_2017_5916731", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> dx = {1, 0, -1, 0};\nvector<int> dy = {0, 1, 0, -1};\nint main(){\n while (true){\n int W, H;\n cin >> W >> H;\n if (W == 0 && H == 0){\n break;\n }\n vector<vector<char>> c(H, vector<char>(W));\n for (int i = 0; i < H; i++){\n for (int j = 0; j < W; j++){\n cin >> c[i][j];\n }\n }\n vector<vector<int>> id(H, vector<int>(W));\n vector<int> X, Y;\n int cnt = 0;\n for (int i = 0; i < H; i++){\n for (int j = 0; j < W; j++){\n if (c[i][j] != '#'){\n id[i][j] = cnt;\n X.push_back(i);\n Y.push_back(j);\n cnt++;\n }\n }\n }\n int K, M;\n for (int i = 0; i < cnt; i++){\n if (c[X[i]][Y[i]] == 'K'){\n K = i;\n }\n if (c[X[i]][Y[i]] == 'M'){\n M = i;\n }\n }\n int s = 0;\n for (int i = 0; i < 4; i++){\n if (c[X[K] + dx[i]][Y[K] + dy[i]] == '.'){\n s = i;\n }\n }\n int t = 0;\n for (int i = 0; i < 4; i++){\n if (c[X[M] + dx[i]][Y[M] + dy[i]] == '.'){\n t = i;\n }\n }\n vector<vector<vector<vector<bool>>>> used(4, vector<vector<vector<bool>>>(4, vector<vector<bool>>(cnt + 1, vector<bool>(cnt, false))));\n queue<tuple<int, int, int, int>> Q;\n for (int i = 0; i < 4; i++){\n used[s][i][K][K] = true;\n Q.push(make_tuple(s, i, K, K));\n }\n bool ok1 = false, ok2 = false;\n while (!Q.empty()){\n int d1 = get<0>(Q.front());\n int d2 = get<1>(Q.front());\n int v1 = get<2>(Q.front());\n int v2 = get<3>(Q.front());\n Q.pop();\n int x1 = X[v1];\n int y1 = Y[v1];\n while (c[x1 + dx[d1]][y1 + dy[d1]] != '#'){\n x1 += dx[d1];\n y1 += dy[d1];\n }\n if (id[x1][y1] == M){\n ok2 = true;\n }\n int d1l = (d1 + 1) % 4;\n int d1r = (d1 + 3) % 4;\n if (!used[d1l][0][id[x1][y1]][cnt]){\n used[d1l][0][id[x1][y1]][cnt] = true;\n Q.push(make_tuple(d1l, 0, id[x1][y1], cnt));\n }\n if (!used[d1r][0][id[x1][y1]][cnt]){\n used[d1r][0][id[x1][y1]][cnt] = true;\n Q.push(make_tuple(d1r, 0, id[x1][y1], cnt));\n }\n if (v2 != cnt){\n int x2 = X[v2];\n int y2 = Y[v2];\n int d2l = (d2 + 1) % 4;\n int d2r = (d2 + 3) % 4;\n while (true){\n if (c[x2 + dx[d2r]][y2 + dy[d2r]] == '#'){\n if (id[x1][y1] == M && id[x2][y2] == M && d2 == (t ^ 2)){\n ok1 = true;\n }\n if (!used[d1l][d2l][id[x1][y1]][id[x2][y2]]){\n used[d1l][d2l][id[x1][y1]][id[x2][y2]] = true;\n Q.push(make_tuple(d1l, d2l, id[x1][y1], id[x2][y2]));\n }\n }\n if (c[x2 + dx[d2l]][y2 + dy[d2l]] == '#'){\n if (id[x1][y1] == M && id[x2][y2] == M && d2 == (t ^ 2)){\n ok1 = true;\n }\n if (!used[d1r][d2r][id[x1][y1]][id[x2][y2]]){\n used[d1r][d2r][id[x1][y1]][id[x2][y2]] = true;\n Q.push(make_tuple(d1r, d2r, id[x1][y1], id[x2][y2]));\n }\n }\n if (c[x2 + dx[d2]][y2 + dy[d2]] == '#'){\n break;\n }\n x2 += dx[d2];\n y2 += dy[d2];\n }\n }\n }\n if (ok1){\n cout << \"He can accomplish his mission.\" << endl;\n } else if (ok2){\n cout << \"He cannot return to the kitchen.\" << endl;\n } else {\n cout << \"He cannot bring tea to his master.\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 6368, "score_of_the_acc": -0.0565, "final_rank": 2 }, { "submission_id": "aoj_2017_4774219", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\nchar grid[16][64];\nbool used[16][64][4][16][64][4];\nvector<string> output{\n \"He can accomplish his mission.\",\n \"He cannot return to the kitchen.\",\n \"He cannot bring tea to his master.\",\n};\n\nvoid dfs(int fy, int fx, int fd, int ry, int rx, int rd){\n if(used[fy][fx][fd][ry][rx][rd]) return;\n used[fy][fx][fd][ry][rx][rd] = true;\n for(int diff=1; diff<=3; diff+=2){\n int nfy = fy;\n int nfx = fx;\n int nfd = (fd +diff) %4;\n while(grid[nfy+dy[nfd]][nfx+dx[nfd]] != '#'){\n nfy += dy[nfd];\n nfx += dx[nfd];\n }\n if(ry == 0){\n dfs(nfy, nfx, nfd, 0, 0, 0);\n continue;\n }\n int nry = ry;\n int nrx = rx;\n int nrd = (rd +diff) %4;\n while(grid[nry][nrx] != '#'){\n if(grid[nry+dy[nrd]][nrx+dx[nrd]] == '#'){\n dfs(nfy, nfx, nfd, nry, nrx, nrd);\n }\n nry -= dy[rd];\n nrx -= dx[rd];\n }\n }\n}\n\nint main(){\n while(1){\n int w,h;\n cin >> w >> h;\n if(w == 0) break;\n\n memset(used, 0, sizeof(used));\n int sy,sx,gy,gx;\n sy = sx = gy = gx = -1;\n for(int i=0; i<h; i++){\n for(int j=0; j<w; j++){\n cin >> grid[i][j];\n if(grid[i][j] == 'K'){\n sy = i;\n sx = j;\n }\n if(grid[i][j] == 'M'){\n gy = i;\n gx = j;\n }\n }\n }\n\n int dir_go=-1, dir_ret=-1;\n for(int d=0; d<4; d++){\n if(grid[sy+dy[d]][sx+dx[d]] != '#'){\n dir_go = d;\n }\n if(grid[gy+dy[d]][gx+dx[d]] != '#'){\n dir_ret = d;\n }\n }\n int fsy = sy;\n int fsx = sx;\n while(grid[fsy+dy[dir_go]][fsx+dx[dir_go]] != '#'){\n fsy += dy[dir_go];\n fsx += dx[dir_go];\n }\n int fgy = gy;\n int fgx = gx;\n while(grid[fgy+dy[dir_ret]][fgx+dx[dir_ret]] != '#'){\n fgy += dy[dir_ret];\n fgx += dx[dir_ret];\n }\n \n dfs(fsy, fsx, dir_go, sy, sx, (dir_go+1)%4);\n dfs(fsy, fsx, dir_go, sy, sx, (dir_go+2)%4);\n dfs(fsy, fsx, dir_go, sy, sx, (dir_go+3)%4);\n dfs(fsy, fsx, dir_go, 0, 0, 0);\n bool ok = false;\n for(int i=0; i<4; i++){\n if(used[gy][gx][i][fgy][fgx][dir_ret]){\n ok = true;\n }\n }\n if(ok){\n cout << output[0] << endl;\n continue;\n }\n if(used[gy][gx][(dir_ret+2)%4][0][0][0]){\n cout << output[1] << endl;\n }else{\n cout << output[2] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 20092, "score_of_the_acc": -0.1809, "final_rank": 4 }, { "submission_id": "aoj_2017_4167539", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\nconst ll INF = (1e+18) + 7;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-6;\nconst ld pi = acos(-1.0);\n//typedef vector<vector<ll>> mat;\ntypedef vector<int> vec;\n\nll mod_pow(ll a, ll n) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * a%mod;\n\t\ta = a * a%mod; n >>= 1;\n\t}\n\treturn res;\n}\n\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n%mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, int n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a*a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 2000500;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nint dx[4] = { 0,1,0,-1 };\nint dy[4] = { 1,0,-1,0 };\n\nint w, h;\nchar mp[16][64];\nbool exi[16][64][4][16][64][4];\n\nbool canr[16][64][4];\nstruct status {\n\tint x1, y1, d1, x2, y2, d2;\n};\nvoid solve() {\n\trep(i, h)rep(j, w) {\n\t\tcin >> mp[i][j];\n\t}\n\trep(i, h)rep(j, w)rep(k, 4)rep(x, h)rep(y, w)rep(l, 4) {\n\t\texi[i][j][k][x][y][l] = false;\n\t}\n\tqueue<status> q;\n\trep(i, h)rep(j, w)if (mp[i][j] == 'K') {\n\t\tint d = 0;\n\t\trep(k, 4) {\n\t\t\tint nx = i + dx[k];\n\t\t\tint ny = j + dy[k];\n\t\t\tif (mp[nx][ny] != '#') {\n\t\t\t\td = k;\n\t\t\t}\n\t\t}\n\t\trep(k, 4) {\n\t\t\tif (k == (2^d))continue;\n\t\t\texi[i][j][d][i][j][k] = true;\n\t\t\tq.push({ i,j,d,i,j,k });\n\t\t}\n\t}\n\twhile (!q.empty()) {\n\t\tstatus s = q.front(); q.pop();\n\t\tint x1 = s.x1;\n\t\tint y1 = s.y1;\n\t\tint d1 = s.d1;\n\t\t/*if (s.x1 == 6 && s.y1 == 10) {\n\t\t\tcout << s.d1 << endl;\n\t\t}*/\n\t\t/*if (s.x2 == 4 && s.y2 == 5 && s.d2 == 3) {\n\t\t\tcout << s.x1 << \" \" << s.y1 << \" \" << s.d1 << endl;\n\t\t}*/\n\t\t//cout << s.x1 << \" \" << s.y1 << \" \" << s.d1 << endl;\n\t\t//cout << s.x1 << \" \" << s.y1 << \" \" << s.d1 << \" \" << s.x2 << \" \" << s.y2 << \" \" << s.d2 << endl;\n\t\twhile (mp[x1 + dx[d1]][y1 + dy[d1]] != '#') {\n\t\t\tx1 += dx[d1]; y1 += dy[d1];\n\t\t}\n\t\tint x2 = s.x2;\n\t\tint y2 = s.y2;\n\t\tint d2 = s.d2;\n\t\t//assert(mp[x1][y1] != '#');\n\t\t//assert(mp[x2][y2] != '#');\n\t\twhile (mp[x2][y2] != '#') {\n\t\t\t//turn right\n\t\t\tint dd1 = (d1 + 1) % 4;\n\t\t\tint dd2 = (d2 + 1) % 4;\n\t\t\tif (mp[x2 + dx[dd2 ^ 2]][y2 + dy[dd2 ^ 2]] == '#') {\n\t\t\t\tif (!exi[x1][y1][dd1][x2][y2][dd2]) {\n\t\t\t\t\texi[x1][y1][dd1][x2][y2][dd2] = true;\n\t\t\t\t\tq.push({ x1,y1,dd1,x2,y2,dd2 });\n\t\t\t\t}\n\t\t\t}\n\t\t\t//turn left\n\t\t\tdd1 = (d1 + 3) % 4;\n\t\t\tdd2 = (d2 + 3) % 4;\n\t\t\tif (mp[x2 + dx[dd2 ^ 2]][y2 + dy[dd2 ^ 2]] == '#') {\n\t\t\t\tif (!exi[x1][y1][dd1][x2][y2][dd2]) {\n\t\t\t\t\texi[x1][y1][dd1][x2][y2][dd2] = true;\n\t\t\t\t\tq.push({ x1,y1,dd1,x2,y2,dd2 });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tx2 += dx[d2];\n\t\t\ty2 += dy[d2];\n\t\t}\n\t}\n\tint gx, gy;\n\tint gd = 0;\n\trep(i, h)rep(j, w) {\n\t\tif (mp[i][j] == 'M') {\n\t\t\tgx = i, gy = j;\n\t\t\trep(k, 4) {\n\t\t\t\tif (mp[i + dx[k]][j + dy[k]] != '#') {\n\t\t\t\t\tgd = k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvector<status> vf,vs;\n\trep(k, 4) {\n\t\tint cx = gx, cy = gy;\n\t\twhile (mp[cx][cy] != '#') {\n\t\t\tvf.push_back({ cx,cy,k ^ 2,0,0,0 });\n\t\t\t/*if (k == gd) {\n\t\t\t\tvs.push_back({ 0,0,0,cx,cy , k ^ 2 });\n\t\t\t}*/\n\n\t\t\tcx += dx[k];\n\t\t\tcy += dy[k];\n\t\t}\n\t\tcx -= dx[k]; cy -= dy[k];\n\t\tif (k == gd) {\n\t\t\tvs.push_back({ 0,0,0,cx,cy,k ^ 2 });\n\t\t}\n\t}\n\tfor (status s2 : vs) {\n\t\t//cout << s2.x2 << \" \" << s2.y2 << \" \" << s2.d2 << endl;\n\t}\n\tfor (status s1 : vf)for (status s2 : vs) {\n\t\tif (exi[s1.x1][s1.y1][s1.d1][s2.x2][s2.y2][s2.d2]) {\n\t\t\tcout << \"He can accomplish his mission.\" << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\trep(i, h)rep(j, w)rep(k, 4) {\n\t\tcanr[i][j][k] = false;\n\t}\n\trep(i, h)rep(j, w){\n\t\tif (mp[i][j] == 'K') {\n\t\t\tint d = 0;\n\t\t\trep(k, 4) {\n\t\t\t\tint nx = i + dx[k];\n\t\t\t\tint ny = j + dy[k];\n\t\t\t\tif (mp[nx][ny] != '#')d = k;\n\t\t\t}\n\t\t\tcanr[i][j][d] = true;\n\t\t\tq.push({ i,j,d });\n\t\t}\n\t}\n\twhile (!q.empty()) {\n\t\tstatus s = q.front(); q.pop();\n\t\tint x = s.x1;\n\t\tint y = s.y1;\n\t\tint d = s.d1;\n\t\twhile (mp[x + dx[d]][y + dy[d]] != '#') {\n\t\t\tx += dx[d];\n\t\t\ty += dy[d];\n\t\t}\n\t\tint dd = (d + 1) % 4;\n\t\tif (!canr[x][y][dd]) {\n\t\t\tcanr[x][y][dd] = true;\n\t\t\tq.push({ x,y,dd });\n\t\t}\n\t\tdd = (d + 3) % 4;\n\t\tif (!canr[x][y][dd]) {\n\t\t\tcanr[x][y][dd] = true;\n\t\t\tq.push({ x,y,dd });\n\t\t}\n\t}\n\tif (canr[gx][gy][gd ^ 1] || canr[gx][gy][gd ^ 3]) {\n\t\tcout << \"He cannot return to the kitchen.\" << endl;\n\t\treturn;\n\t}\n\tcout << \"He cannot bring tea to his master.\" << endl;\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f(); //init();\n\t//int t; cin >> t; rep(i, t)solve();\n\twhile(cin>>w>>h,w)solve();\n\t//solve();\n\tstop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 50908, "score_of_the_acc": -0.533, "final_rank": 12 }, { "submission_id": "aoj_2017_4105489", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nconst int D = 4,H = 16, W = 64;\nbool dp[D][H][W][D][H][W];\nchar st[H][W];\n\nbool rc[D][H][W];\n\nusing P = pair<int, int>;\nusing T = tuple<int, int, int>;\nP mv[D][H][W];\n\nstruct ST{\n int k1,y1,x1,k2,y2,x2;\n ST(){}\n ST(int k1,int y1,int x1,int k2,int y2,int x2):\n k1(k1),y1(y1),x1(x1),k2(k2),y2(y2),x2(x2){}\n};\n\nsigned main(){\n int w,h;\n while(cin>>w>>h,w){\n vector<string> tt(h);\n for(int i=0;i<h;i++) cin>>tt[i];\n memset(st,'#',sizeof(st));\n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++)\n st[i][j]=tt[i][j];\n h=H;w=W;\n\n int sy=-1,sx=-1,gy=-1,gx=-1;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(st[i][j]=='K') sy=i,sx=j;\n if(st[i][j]=='M') gy=i,gx=j;\n }\n }\n\n int dy[]={0,1,0,-1};\n int dx[]={1,0,-1,0};\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(st[i][j]=='#') continue;\n for(int k=0;k<4;k++){\n int y=i,x=j;\n while(st[y+dy[k]][x+dx[k]]!='#'){\n y+=dy[k];\n x+=dx[k];\n }\n mv[k][i][j]=P(y,x);\n }\n }\n }\n\n {\n memset(rc,0,sizeof(rc));\n queue<T> que;\n for(int k=0;k<4;k++){\n if(st[sy+dy[k]][sx+dx[k]]=='.'){\n int y,x;\n tie(y,x)=mv[k][sy][sx];\n rc[k][y][x]=1;\n que.emplace(k,y,x);\n }\n }\n\n while(!que.empty()){\n int k,y,x;\n tie(k,y,x)=que.front();que.pop();\n assert(st[y+dy[k]][x+dx[k]]=='#');\n for(int dk=1;dk<4;dk+=2){\n int nk=(k+dk)%4;\n int ny,nx;\n tie(ny,nx)=mv[nk][y][x];\n if(rc[nk][ny][nx]) continue;\n rc[nk][ny][nx]=1;\n que.emplace(nk,ny,nx);\n }\n }\n\n int flg=0;\n for(int k=0;k<4;k++)\n flg|=rc[k][gy][gx];\n if(!flg){\n cout<<\"He cannot bring tea to his master.\"<<endl;\n continue;\n }\n }\n\n int tap=0,uku=0;\n for(int k=0;k<4;k++){\n if(st[sy+dy[k]][sx+dx[k]]=='.')\n tap=k;\n if(st[gy+dy[k]][gx+dx[k]]=='.')\n uku=k;\n }\n memset(dp,0,sizeof(dp));\n queue<ST> que;\n auto push=\n [&](int k1,int y1,int x1,int k2,int y2,int x2){\n if(dp[k1][y1][x1][k2][y2][x2]) return;\n dp[k1][y1][x1][k2][y2][x2]=1;\n que.emplace(k1,y1,x1,k2,y2,x2);\n };\n\n for(int k=0;k<4;k++){\n if(st[sy+dy[k]][sx+dx[k]]!='#') continue;\n push(tap,mv[tap][sy][sx].first,mv[tap][sy][sx].second,k,sy,sx);\n }\n\n while(!que.empty()){\n ST s=que.front();que.pop();\n assert(st[s.y1][s.x1]!='#');\n assert(st[s.y2][s.x2]!='#');\n assert(st[s.y1+dy[s.k1]][s.x1+dx[s.k1]]=='#');\n assert(st[s.y2+dy[s.k2]][s.x2+dx[s.k2]]=='#');\n\n for(int dk=1;dk<4;dk+=2){\n int nk1=(s.k1+dk)%4;\n int ny1,nx1;\n tie(ny1,nx1)=mv[nk1][s.y1][s.x1];\n\n int nk2=(s.k2+dk)%4;\n int ny2=s.y2,nx2=s.x2;\n while(st[ny2][nx2]!='#'){\n if(st[ny2+dy[nk2]][nx2+dx[nk2]]=='#')\n push(nk1,ny1,nx1,nk2,ny2,nx2);\n ny2-=dy[s.k2];\n nx2-=dx[s.k2];\n }\n }\n }\n\n int flg=0;\n for(int k=0;k<4;k++)\n flg|=dp[k][gy][gx][uku][mv[uku][gy][gx].first][mv[uku][gy][gx].second];\n if(flg) cout<<\"He can accomplish his mission.\"<<endl;\n else cout<<\"He cannot return to the kitchen.\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 19768, "score_of_the_acc": -0.1634, "final_rank": 3 }, { "submission_id": "aoj_2017_3837851", "code_snippet": "#include<bits/stdc++.h>\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-8)\n#define Eps (1e-12)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nint dx[4] = {0,1,0,-1};\nint dy[4] = {1,0,-1,0};\n#define track() cout<<\"######\"<<endl;\n\nint h,w;\nstring grid[16];\nbool used[16][64][4][16][64][4];\nbool flag1,flag2;\nint sy,sx,sd,ty,tx,td;\n\nint up(int d){return ((d+0)%4);}\nint left(int d){return ((d+1)%4);}\nint down(int d){return ((d+2)%4);}\nint right(int d){return ((d+3)%4);}\n\nvector<pair<pii,pii> > steps;\n\nvoid rec(int ky,int kx,int kd,int my,int mx,int md){\n\n if(used[ky][kx][kd][my][mx][md])return;\n used[ky][kx][kd][my][mx][md] = true;\n\n int nky=ky,nkx=kx;\n while(1){\n if(grid[nky+dy[kd]][nkx+dx[kd]]=='#')break;\n nky = nky + dy[kd];\n nkx = nkx + dx[kd];\n }\n if(my==0){\n if(nky==ty && nkx==tx)flag1=true;\n rec(nky,nkx,left(kd),0,0,0);\n rec(nky,nkx,right(kd),0,0,0);\n return;\n }\n\n int nmy=my,nmx=mx; \n while(1){\n if(grid[nmy+dy[ left(md) ]][nmx+dx[ left(md) ]]=='#' &&\n grid[nmy+dy[ right(md) ]][nmx+dx[ right(md) ]]=='.' ){\n rec(nky,nkx,right(kd),nmy,nmx,right(md));\n }\n if(grid[nmy+dy[ right(md) ]][nmx+dx[ right(md) ]]=='#' &&\n grid[nmy+dy[ left(md) ]][nmx+dx[ left(md) ]]=='.' ){\n rec(nky,nkx,left(kd),nmy,nmx,left(md));\n } \n\n if(grid[nmy+dy[ down(md) ]][nmx+dx[ down(md) ]]=='#'){ \n if(grid[nmy+dy[ right(md) ]][nmx+dx[ right(md) ]]=='#' &&\n grid[nmy+dy[ left(md) ]][nmx+dx[ left(md) ]]=='#' ){\n rec(nky,nkx,right(kd),nmy,nmx,right(md));\n rec(nky,nkx,left(kd),nmy,nmx,left(md));\n }\n }\n \n if(grid[nmy+dy[ md ]][nmx+dx[ md ]]=='#'){ \n if(grid[nmy+dy[ right(md) ]][nmx+dx[ right(md) ]]=='#' &&\n grid[nmy+dy[ left(md) ]][nmx+dx[ left(md) ]]=='#' ){\n rec(nky,nkx,right(kd),nmy,nmx,right(md));\n rec(nky,nkx,left(kd),nmy,nmx,left(md));\n }\n break;\n }\n nmy = nmy + dy[md];\n nmx = nmx + dx[md];\n }\n if(nky==ty && nkx==tx && nmy==ty && nmx==tx && md==(td+2)%4)flag2 = true;\n}\n\nstring solve(){\n memset(used,0,sizeof(used));\n flag1 = false;\n flag2 = false;\n FOR(i,0,h){\n FOR(j,0,w){\n if(grid[i][j]=='K'){\n sy = i;\n sx = j;\n grid[i][j]='.';\n FOR(k,0,4)\n if(grid[sy+dy[k]][sx+dx[k]]=='.')sd = k;\n }\n if(grid[i][j]=='M'){\n ty = i;\n tx = j;\n grid[i][j]='.';\n FOR(k,0,4)\n if(grid[ty+dy[k]][tx+dx[k]]=='.')td = k;\n }\n }\n }\n FOR(i,0,4)rec(sy,sx,sd,sy,sx,i);\n if(flag2)return \"He can accomplish his mission.\";\n rec(sy,sx,sd,0,0,0);\n if(flag1)return \"He cannot return to the kitchen.\";\n return \"He cannot bring tea to his master.\";\n}\n\nint main()\n{\n while(cin>>w>>h && w){\n FOR(i,0,h)cin>>grid[i];\n cout<<solve()<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 20460, "score_of_the_acc": -0.2328, "final_rank": 8 }, { "submission_id": "aoj_2017_3651589", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\n\n// direction\n// 0: N, 1: E, 2:S, 3:W\nconstexpr int dx[4] = {0, 1, 0, -1};\nconstexpr int dy[4] = {-1, 0, 1, 0};\nconstexpr int dd[2] = {1, 3}; // turn right, left\n\nbool reach1[16][64][4]; // only go to kitchen\nbool reach[16][64][4][16][64][4];\n\nint main() {\n int w, h;\n while(cin >> w >> h, w) {\n memset(reach1, false, sizeof(reach1));\n memset(reach, false, sizeof(reach));\n vector<string> v(h);\n for(auto& s : v) cin >> s;\n\n int sy = -1, sx = -1, sd = -1, gy = -1, gx = -1;\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(v[i][j] == '#') continue;\n if(v[i][j] == 'K') {\n sy = i, sx = j;\n for(int d = 0; d < 4; ++d) {\n if(v[sy + dy[d]][sx + dx[d]] == '#') continue;\n sd = d;\n }\n }\n if(v[i][j] == 'M') {\n gy = i, gx = j;\n }\n }\n }\n\n function<bool(int, int, int)> go_to_kitchen = [&] (int y, int x, int d) {\n if(reach1[y][x][d]) return false;\n reach1[y][x][d] = true;\n if(y == gy && x == gx) return true;\n bool res = false;\n if(v[y + dy[d]][x + dx[d]] != '#') {\n res = go_to_kitchen(y + dy[d], x + dx[d], d);\n } else {\n res = go_to_kitchen(y, x, (d + 1) % 4);\n res |= go_to_kitchen(y, x, (d + 3) % 4);\n }\n return res;\n };\n function<bool(int, int, int, int, int, int)> dfs = [&] (int y1, int x1, int d1, int y2, int x2, int d2) {\n if(reach[y1][x1][d1][y2][x2][d2]) return false;\n //cout << y1 << ' ' << x1 << ' ' << d1 << ' ' << y2 << ' ' << x2 << ' ' << d2 << endl;\n reach[y1][x1][d1][y2][x2][d2] = true;\n if(gy == y2 && gx == x2 && gy == y1 && gx == x1\n && v[y2 - dy[d2]][x2 - dx[d2]] != '#' && v[y1 + dy[d1]][x1 + dx[d1]] == '#') {\n return true;\n }\n bool res = false;\n if(v[y1 + dy[d1]][x1 + dx[d1]] != '#') {\n res = dfs(y1 + dy[d1], x1 + dx[d1], d1, y2, x2, d2);\n } else { // can turn\n for(int i = 0; i < 2; ++i) {\n const int nd1 = (d1 + dd[i]) % 4, nd2 = (d2 + dd[i]) % 4;\n if(v[y2 - dy[nd2]][x2 - dx[nd2]] != '#') continue; // front must be wall\n res |= dfs(y1, x1, nd1, y2, x2, nd2);\n }\n }\n if(v[y2 + dy[d2]][x2 + dx[d2]] != '#') {\n res |= dfs(y1, x1, d1, y2 + dy[d2], x2 + dx[d2], d2);\n }\n return res;\n };\n\n bool ac = false;\n for(int d = 0; d < 4; ++d) {\n ac |= dfs(sy, sx, sd, sy, sx, d);\n }\n\n if(ac) {\n cout << \"He can accomplish his mission.\" << endl;\n } else if(go_to_kitchen(sy, sx, sd)) {\n cout << \"He cannot return to the kitchen.\" << endl;\n } else {\n cout << \"He cannot bring tea to his master.\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 950, "memory_kb": 25436, "score_of_the_acc": -0.4235, "final_rank": 11 }, { "submission_id": "aoj_2017_3651480", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\n\n// direction\n// 0: N, 1: E, 2:S, 3:W\nconstexpr int dx[4] = {0, 1, 0, -1};\nconstexpr int dy[4] = {-1, 0, 1, 0};\nconstexpr int dd[2] = {1, 3}; // turn right, left\n\ntemplate<typename T>\nstd::vector<T> table(int n, T v) { return std::vector<T>(n, v); }\n\ntemplate <class... Args>\nauto table(int n, Args... args) {\n auto val = table(args...);\n return std::vector<decltype(val)>(n, std::move(val));\n}\n\nbool reach1[16][64][4]; // only go to kitchen\nbool reach[16][64][4][16][64][4];\n\nint main() {\n int w, h;\n while(cin >> w >> h, w) {\n memset(reach1, false, sizeof(reach1));\n memset(reach, false, sizeof(reach));\n vector<string> v(h);\n for(auto& s : v) cin >> s;\n\n auto can_move_to = [&] (int y, int x) {\n return 0 <= y && y < h && 0 <= x && x < w && v[y][x] != '#';\n };\n auto from = table(h, w, 4, vector<pii>{});\n auto to = table(h, w, 4, make_pair(-1, -1));\n int sy = -1, sx = -1, sd = -1, gy = -1, gx = -1, gd = -1;\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(v[i][j] == '#') continue;\n for(int d = 0; d < 4; ++d) {\n int y = i, x = j;\n while(can_move_to(y + dy[d], x + dx[d])) {\n y += dy[d];\n x += dx[d];\n }\n from[y][x][d].emplace_back(i, j);\n to[i][j][d] = make_pair(y, x);\n }\n\n if(v[i][j] == 'K') {\n sy = i, sx = j;\n for(int d = 0; d < 4; ++d) {\n if(!can_move_to(sy + dy[d], sx + dx[d])) continue;\n sd = d;\n }\n }\n if(v[i][j] == 'M') {\n gy = i, gx = j;\n for(int d = 0; d < 4; ++d) {\n if(!can_move_to(gy + dy[d], gx + dx[d])) continue;\n gd = d;\n }\n }\n }\n }\n\n function<bool(int, int, int)> go_to_kitchen = [&] (int y, int x, int d) {\n if(reach1[y][x][d]) return false;\n reach1[y][x][d] = true;\n if(y == gy && x == gx) return true;\n bool res = false;\n if(v[y + dy[d]][x + dx[d]] != '#') {\n res = go_to_kitchen(y + dy[d], x + dx[d], d);\n } else {\n res = go_to_kitchen(y, x, (d + 1) % 4);\n res |= go_to_kitchen(y, x, (d + 3) % 4);\n }\n return res;\n };\n\n queue<tuple<int, int, int, int, int, int>> que;\n for(int d = 0; d < 4; ++d) {\n reach[sy][sx][sd][sy][sx][d] = true;\n que.emplace(sy, sx, sd, sy, sx, d);\n }\n while(!que.empty()) {\n int y1, x1, d1, y2, x2, d2;\n tie(y1, x1, d1, y2, x2, d2) = que.front();\n que.pop();\n const int ny1 = to[y1][x1][d1].first, nx1 = to[y1][x1][d1].second;\n for(auto const& p : from[y2][x2][d2]) {\n const int ny2 = p.first, nx2 = p.second;\n if(v[ny2 + dy[(d2 + 2) % 4]][nx2 + dx[(d2 + 2) % 4]] == '#') {\n reach[ny1][nx1][d1][ny2][nx2][d2] = true; // terminate\n }\n for(int i = 0; i < 2; ++i) {\n const int nd1 = (d1 + dd[i]) % 4, nd2 = (d2 + dd[i]) % 4;\n if(reach[ny1][nx1][nd1][ny2][nx2][nd2]) continue;\n if(v[ny2 + dy[nd2]][nx2 + dx[nd2]] != '#') continue; // cannot turn at this point\n reach[ny1][nx1][nd1][ny2][nx2][nd2] = true;\n que.emplace(ny1, nx1, nd1, ny2, nx2, nd2);\n }\n }\n }\n\n bool f2 = false;\n for(int d1 = 0; d1 < 4; ++d1) {\n f2 |= reach[gy][gx][d1][gy][gx][gd];\n }\n\n if(f2) {\n cout << \"He can accomplish his mission.\" << endl;\n } else if(go_to_kitchen(sy, sx, sd)) {\n cout << \"He cannot return to the kitchen.\" << endl;\n } else {\n cout << \"He cannot bring tea to his master.\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 19924, "score_of_the_acc": -0.2244, "final_rank": 7 }, { "submission_id": "aoj_2017_3651450", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\n\n// direction\n// 0: N, 1: E, 2:S, 3:W\nconstexpr int dx[4] = {0, 1, 0, -1};\nconstexpr int dy[4] = {-1, 0, 1, 0};\nconstexpr int dd[2] = {1, 3}; // turn right, left\n\ntemplate<typename T>\nstd::vector<T> table(int n, T v) { return std::vector<T>(n, v); }\n\ntemplate <class... Args>\nauto table(int n, Args... args) {\n auto val = table(args...);\n return std::vector<decltype(val)>(n, std::move(val));\n}\n\nbool reach[16][64][4][16][64][4];\n\nint main() {\n int w, h;\n while(cin >> w >> h, w) {\n memset(reach, false, sizeof(reach));\n vector<string> v(h);\n for(auto& s : v) cin >> s;\n\n auto can_move_to = [&] (int y, int x) {\n return 0 <= y && y < h && 0 <= x && x < w && v[y][x] != '#';\n };\n auto from = table(h, w, 4, vector<pii>{});\n auto to = table(h, w, 4, make_pair(-1, -1));\n int sy = -1, sx = -1, sd = -1, gy = -1, gx = -1, gd = -1;\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(v[i][j] == '#') continue;\n for(int d = 0; d < 4; ++d) {\n int y = i, x = j;\n while(can_move_to(y + dy[d], x + dx[d])) {\n y += dy[d];\n x += dx[d];\n }\n from[y][x][d].emplace_back(i, j);\n to[i][j][d] = make_pair(y, x);\n }\n\n if(v[i][j] == 'K') {\n sy = i, sx = j;\n for(int d = 0; d < 4; ++d) {\n if(!can_move_to(sy + dy[d], sx + dx[d])) continue;\n sd = d;\n }\n }\n if(v[i][j] == 'M') {\n gy = i, gx = j;\n for(int d = 0; d < 4; ++d) {\n if(!can_move_to(gy + dy[d], gx + dx[d])) continue;\n gd = d;\n }\n }\n }\n }\n\n bool f1 = false;\n { // only go to master\n auto reach = table(h, w, 4, false);\n queue<tuple<int, int, int>> que;\n que.emplace(sy, sx, sd);\n reach[sy][sx][sd] = true;\n while(!que.empty()) {\n int y, x, d; tie(y, x, d) = que.front();\n que.pop();\n const int ny = to[y][x][d].first, nx = to[y][x][d].second;\n for(int i = 0; i < 2; ++i) {\n const int nd = (d + dd[i]) % 4;\n if(reach[ny][nx][nd]) continue;\n reach[ny][nx][nd] = true;\n que.emplace(ny, nx, nd);\n }\n }\n for(int i = 0; i < 4; ++i) {\n f1 |= reach[gy][gx][i];\n }\n }\n queue<tuple<int, int, int, int, int, int>> que;\n for(int d = 0; d < 4; ++d) {\n reach[sy][sx][sd][sy][sx][d] = true;\n que.emplace(sy, sx, sd, sy, sx, d);\n }\n while(!que.empty()) {\n int y1, x1, d1, y2, x2, d2;\n tie(y1, x1, d1, y2, x2, d2) = que.front();\n que.pop();\n const int ny1 = to[y1][x1][d1].first, nx1 = to[y1][x1][d1].second;\n for(auto const& p : from[y2][x2][d2]) {\n const int ny2 = p.first, nx2 = p.second;\n if(v[ny2 + dy[(d2 + 2) % 4]][nx2 + dx[(d2 + 2) % 4]] == '#') {\n reach[ny1][nx1][d1][ny2][nx2][d2] = true; // terminate\n }\n for(int i = 0; i < 2; ++i) {\n const int nd1 = (d1 + dd[i]) % 4, nd2 = (d2 + dd[i]) % 4;\n if(reach[ny1][nx1][nd1][ny2][nx2][nd2]) continue;\n if(v[ny2 + dy[nd2]][nx2 + dx[nd2]] != '#') continue; // cannot turn at this point\n reach[ny1][nx1][nd1][ny2][nx2][nd2] = true;\n que.emplace(ny1, nx1, nd1, ny2, nx2, nd2);\n }\n }\n }\n\n bool f2 = false;\n for(int d1 = 0; d1 < 4; ++d1) {\n f2 |= reach[gy][gx][d1][gy][gx][gd];\n }\n\n if(f2) {\n cout << \"He can accomplish his mission.\" << endl;\n } else if(f1) {\n cout << \"He cannot return to the kitchen.\" << endl;\n } else {\n cout << \"He cannot bring tea to his master.\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 19880, "score_of_the_acc": -0.2239, "final_rank": 6 }, { "submission_id": "aoj_2017_3651440", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\n\n// direction\n// 0: N, 1: E, 2:S, 3:W\nconstexpr int dx[4] = {0, 1, 0, -1};\nconstexpr int dy[4] = {-1, 0, 1, 0};\nconstexpr int dd[2] = {1, 3}; // turn right, left\n\ntemplate<typename T>\nstd::vector<T> table(int n, T v) { return std::vector<T>(n, v); }\n\ntemplate <class... Args>\nauto table(int n, Args... args) {\n auto val = table(args...);\n return std::vector<decltype(val)>(n, std::move(val));\n}\n\nbool reach[16][64][4][16][64][4];\n\nint main() {\n int w, h;\n while(cin >> w >> h, w) {\n fill((bool*)reach, (bool*)reach + 64 * 64 * 64 * 64, false);\n vector<string> v(h);\n for(auto& s : v) cin >> s;\n\n auto can_move_to = [&] (int y, int x) {\n return 0 <= y && y < h && 0 <= x && x < w && v[y][x] != '#';\n };\n auto from = table(h, w, 4, vector<pii>{});\n auto to = table(h, w, 4, make_pair(-1, -1));\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(v[i][j] == '#') continue;\n for(int d = 0; d < 4; ++d) {\n int y = i, x = j;\n while(can_move_to(y + dy[d], x + dx[d])) {\n y += dy[d];\n x += dx[d];\n }\n from[y][x][d].emplace_back(i, j);\n to[i][j][d] = make_pair(y, x);\n }\n }\n }\n\n int sy = -1, sx = -1, sd = -1, gy = -1, gx = -1, gd = -1;\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(v[i][j] == 'K') {\n sy = i, sx = j;\n for(int d = 0; d < 4; ++d) {\n if(can_move_to(sy + dy[d], sx + dx[d])) {\n sd = d;\n }\n }\n }\n if(v[i][j] == 'M') {\n gy = i, gx = j;\n for(int d = 0; d < 4; ++d) {\n if(can_move_to(gy + dy[d], gx + dx[d])) {\n gd = d;\n }\n }\n }\n }\n }\n\n bool f1 = false;\n { // only go to master\n auto reach = table(h, w, 4, false);\n queue<tuple<int, int, int>> que;\n que.emplace(sy, sx, sd);\n reach[sy][sx][sd] = true;\n while(!que.empty()) {\n int y, x, d; tie(y, x, d) = que.front();\n que.pop();\n const int ny = to[y][x][d].first, nx = to[y][x][d].second;\n for(int i = 0; i < 2; ++i) {\n const int nd = (d + dd[i]) % 4;\n if(reach[ny][nx][nd]) continue;\n reach[ny][nx][nd] = true;\n que.emplace(ny, nx, nd);\n }\n }\n for(int i = 0; i < 4; ++i) {\n f1 |= reach[gy][gx][i];\n }\n }\n queue<tuple<int, int, int, int, int, int>> que;\n for(int d = 0; d < 4; ++d) {\n reach[sy][sx][sd][sy][sx][d] = true;\n que.emplace(sy, sx, sd, sy, sx, d);\n }\n while(!que.empty()) {\n int y1, x1, d1, y2, x2, d2;\n tie(y1, x1, d1, y2, x2, d2) = que.front();\n que.pop();\n const int ny1 = to[y1][x1][d1].first, nx1 = to[y1][x1][d1].second;\n for(auto const& p : from[y2][x2][d2]) {\n const int ny2 = p.first, nx2 = p.second;\n if(v[ny2 + dy[(d2 + 2) % 4]][nx2 + dx[(d2 + 2) % 4]] == '#') {\n reach[ny1][nx1][d1][ny2][nx2][d2] = true; // terminate\n }\n for(int i = 0; i < 2; ++i) {\n const int nd1 = (d1 + dd[i]) % 4, nd2 = (d2 + dd[i]) % 4;\n if(reach[ny1][nx1][nd1][ny2][nx2][nd2]) continue;\n if(v[ny2 + dy[nd2]][nx2 + dx[nd2]] != '#') continue; // cannot turn at this point\n reach[ny1][nx1][nd1][ny2][nx2][nd2] = true;\n que.emplace(ny1, nx1, nd1, ny2, nx2, nd2);\n }\n }\n }\n\n bool f2 = false;\n for(int d1 = 0; d1 < 4; ++d1) {\n f2 |= reach[gy][gx][d1][gy][gx][gd];\n }\n\n if(f2) {\n cout << \"He can accomplish his mission.\" << endl;\n } else if(f1) {\n cout << \"He cannot return to the kitchen.\" << endl;\n } else {\n cout << \"He cannot bring tea to his master.\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 19976, "score_of_the_acc": -0.3605, "final_rank": 9 } ]
aoj_2016_cpp
Lifeguard in the Pool プールの監視員 English text is not available in this practice contest. Horton Moore はプールの監視員として働いている.彼が見回りのためにプールの縁を歩いていたところ,プールの中で一人の少女がおぼれていることに気づいた.もちろん,彼は直ちに救助に向かわなければならない.しかも,少女の身に何かあっては大変であるから,少しでも早く少女のもとにたどり着きたい. あなたの仕事は,プールの形状(頂点数が 3〜10 の凸多角形),地上および水中における監視員の単位距離あたりの移動時間,そして監視員と少女の初期位置が与えられたときに,監視員が少女のところに到着するまでにかかる最短の時間を求めるプログラムを書くことである. Input 入力は複数のデータセットの並びからなる.入力の終わりは 1 つの 0 だけを含む行によって示される. 各データセットは次の形式になっている. n x 1 y 1 x 2 y 2 ... x n y n tg tw xs ys xt yt それぞれの記号の意味は次のとおりである. n は凸多角形をしたプールの頂点数を示す.これは 3 以上 10 以下の整数である. ( x i , y i ) はプールの i 番目の頂点の座標を示す.それぞれの座標値は絶対値が 100 以下の整数である.頂点は反時計回りの順番で与えられる. tg は監視員が地上において移動するときにかかる単位距離あたりの時間を表す. tw は監視員が水中において移動するときにかかる単位距離あたりの時間を表す.これらはいずれも整数であり,さらに 1 ≦ tg < tw ≦ 100 を満たす. ( xs , ys ) は監視員の初期位置の座標を表す.この座標はプールのちょうど辺上にある. ( xt , yt ) は少女の初期位置の座標を表す.この座標はプールの内側にある. 同一の行にある数値と数値の間は 1 個の空白で区切られている. この問題において,監視員および少女は点であるとみなす.また,監視員がプールの辺に沿って移動するときは地上を移動しているとみなす.監視員は,地上から水中に一瞬で入ることが,また水中から地上に一瞬で出ることができると仮定して構わない.監視員が地上から水中に,あるいは水中から地上に移るとき,監視員は同じ座標にとどまると考えること.したがって,たとえばプールの縁から離れたところに飛び込むことによって水中での移動距離を減らすことはできない. Output 各データセットに対して,監視員が少女の元に到着するまでにかかる最短の時間を 1 行に出力しなさい.解答の誤差は 0.00000001 (10 −8 ) を超えてはならない.精度に関する条件を満たしていれば,小数点以下は何桁数字を出力しても構わない. Sample Input 4 0 0 10 0 10 10 0 10 10 12 0 5 9 5 4 0 0 10 0 10 10 0 10 10 12 0 0 9 1 4 0 0 10 0 10 10 0 10 10 12 0 1 9 1 8 2 0 4 0 6 2 6 4 4 6 2 6 0 4 0 2 10 12 3 0 3 5 0 Output for the Sample Input 108.0 96.63324958071081 103.2664991614216 60.0
[ { "submission_id": "aoj_2016_10848573", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,a,b) for (int i=(a);i<=(b);++i)\n#define ROF(i,a,b) for (int i=(a);i>=(b);--i)\n#define fi first\n#define se second\n#define pb push_back\n#define mp make_pair\n#define pDI pair<double,int>\ninline int read(){\n\tint x=0,f=1; char ch=getchar();\n\twhile (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }\n\twhile (ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); }\n\treturn x*f;\n}\n\nconst int MAXN=300005;\nconst double PI=3.141592653589793238462,eps=1e-12,INF=1e20;\n#define sqr(x) ((x)*(x))\ninline int sgn(double x){ if (fabs(x)<eps) return 0; return x>0?1:-1; }\nstruct P{\n\tdouble x,y,k;\n\tP(double x=0,double y=0):x(x),y(y){}\n\tP operator +(const P& p)const{ return P(x+p.x,y+p.y); }\n\tP operator -(const P& p)const{ return P(x-p.x,y-p.y); }\n\tP operator *(const double& f)const{ return P(x*f,y*f); }\n\tP operator /(const double& f)const{ return P(x/f,y/f); }\n\tdouble len(){ return sqrt(x*x+y*y); }\n\tdouble operator ^(const P& p)const{ return x*p.y-y*p.x; }\n\tdouble operator *(const P& p)const{ return x*p.x+y*p.y; }\n\tbool operator <(const P& p)const{ return k<p.k; }\n}A[MAXN],B,C,D,F[MAXN],U,s,t;\ninline double dot(P a,P b,P o){ return (a-o)*(b-o); }\ninline double cross(P a,P b,P o){ return (a-o)^(b-o); }\ninline P ulen(P p){ return p/p.len(); }\ninline P projection(P l1,P l2,P p){ return l2+(l1-l2)*dot(l1,p,l2)/sqr(l1-l2); }\n\nstruct list{\n\tint l,lt[MAXN],nt[MAXN],x[MAXN];\n\tdouble y[MAXN];\n\tvoid clear(){ l=0; memset(lt,0,sizeof(lt)); }\n\tvoid addedge(int a,int b,double c){\n\t\t//if (l&1) printf(\"%d-%d %.5f\\n\",a,b,c);\n\t\tnt[++l]=lt[a]; lt[a]=l; x[l]=b; y[l]=c;\n\t}\n}E;\n\nint n,m,l;\ndouble tg,tw,tanalpha,dis[MAXN];\npriority_queue<pDI >Q;\ndouble dijkstra(int S,int T){\n\tFOR(i,1,T) dis[i]=INF;\n\tQ.push(mp(dis[S]=0,S));\n\twhile (!Q.empty()) {\n\t\tpDI z=Q.top(); Q.pop();\n\t\tdouble x=-z.fi; int y=z.se;\n\t\tif (dis[y]<x-eps) continue;\n\t\tfor (int i=E.lt[y];i;i=E.nt[i]) {\n\t\t\tint j=E.x[i];\n\t\t\tif (dis[j]>x+E.y[i]) Q.push(mp(-(dis[j]=x+E.y[i]),j));\n\t\t}\n\t}\n\treturn dis[T];\n}\nint main(){\n\twhile (n=read(),n!=0) {\n\t\tE.clear(),m=0;\n\t\tFOR(i,1,n) scanf(\"%lf%lf\",&A[i].x,&A[i].y);\n\t\tA[n+1]=A[1]; F[0]=A[0]=A[n];\n\t\tscanf(\"%lf%lf\",&tg,&tw);\n\t\t//printf(\"%.5f\\n\",acos(tg/tw)/PI*180.0);\n\t\tscanf(\"%lf%lf%lf%lf\",&s.x,&s.y,&t.x,&t.y);\n\t\tif (tw-tg<eps) { printf(\"%.12f\\n\",(s-t).len()*tw); continue; }\n\t\ttanalpha=tg/sqrt(sqr(tw)-sqr(tg));\n\t\tFOR(i,1,n) {\n\t\t\t//printf(\"%d:\\n\",i);\n\t\t\tdouble l1;\n\t\t\tl=m+1;\n\t\t\tU=ulen(A[i+1]-A[i]);\n\t\t\t//printf(\"%.5f %.5f\\n\",U.x,U.y);\n\t\t\tB=projection(A[i],A[i+1],s);\n\t\t\tl1=(s-B).len()*tanalpha;\n\t\t\tC=B+U*l1;\n\t\t\tD=B-U*l1;\n\t\t\t//printf(\"%.5f\\n\",l1);\n\t\t\t//printf(\"S-B=(%.5f,%.5f)C=(%.5f,%.5f)D=(%.5f,%.5f)\\n\",B.x,B.y,C.x,C.y,D.x,D.y);\n\t\t\tif (sgn(dot(A[i],A[i+1],C))<0) F[++m]=C,F[m].k=(C-A[i]).len();\n\t\t\tif (sgn(dot(A[i],A[i+1],D))<0) F[++m]=D,F[m].k=(D-A[i]).len();\n\t\t\tB=projection(A[i],A[i+1],t);\n\t\t\tl1=(t-B).len()*tanalpha;\n\t\t\tC=B+U*l1;\n\t\t\tD=B-U*l1;\n\t\t\t//printf(\"%.5f\\n\",l1);\n\t\t\t//printf(\"T-B=(%.5f,%.5f)C=(%.5f,%.5f)D=(%.5f,%.5f)\\n\",B.x,B.y,C.x,C.y,D.x,D.y);\n\t\t\tif (sgn(dot(A[i],A[i+1],C))<0) F[++m]=C,F[m].k=(C-A[i]).len();\n\t\t\tif (sgn(dot(A[i],A[i+1],D))<0) F[++m]=D,F[m].k=(D-A[i]).len();\n\t\t\tF[++m]=A[i+1],F[m].k=(A[i+1]-A[i]).len();\n\t\t\tsort(F+l,F+m+1);\n\t\t\tFOR(j,l,m)\n\t\t\tif (j!=1) {\n\t\t\t\tE.addedge(j-1,j,(F[j]-F[j-1]).len()*tg);\n\t\t\t\tE.addedge(j,j-1,(F[j]-F[j-1]).len()*tg);\n\t\t\t}\n\t\t}\n\t\tE.addedge(1,m,(F[m]-F[1]).len()*tg);\n\t\tE.addedge(m,1,(F[m]-F[1]).len()*tg);\n\t\tF[++m]=s; F[++m]=t;\n\t\tFOR(i,1,m) {\n\t\t\tif (i!=m-1) E.addedge(i,m-1,(F[m-1]-F[i]).len()*tw);\n\t\t\tif (i!=m-1) E.addedge(m-1,i,(F[m-1]-F[i]).len()*tw);\n\t\t\tif (i!=m) E.addedge(i,m,(F[m]-F[i]).len()*tw);\n\t\t\tif (i!=m) E.addedge(m,i,(F[m]-F[i]).len()*tw);\n\t\t}\n\t\t//FOR(i,1,m) printf(\"ID:%d (%.5f,%.5f)\\n\",i,F[i].x,F[i].y);\n\t\tprintf(\"%.12f\\n\",dijkstra(m-1,m));\n\t}\n\t\n\treturn 0;\n}\n\n/*\n4\n0 0 10 0 10 10 0 10\n10\n12\n0 5\n9 5\n4\n0 0 10 0 10 10 0 10\n10\n12\n0 0\n9 1\n4\n0 0 10 0 10 10 0 10\n10\n12\n0 1\n9 1\n8\n2 0 4 0 6 2 6 4 4 6 2 6 0 4 0 2\n10\n12\n3 0\n3 5\n0\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 21728, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_2016_9391778", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\n#include<complex>\nconst int INF = 1000000000;\nconst ll LINF = 1001002003004005006ll;\nint dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 };\n\nstruct IOSetup {\n IOSetup() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n }\n} iosetup;\n\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n return os;\n}\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v)is >> x;\n return is;\n}\n\n// line 1 \"Geometry/template.cpp\"\n// Real\nusing Real = double;\nconst Real EPS = 1e-6;\nconst Real pi = acosl(-1);\n\n// Point\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, Point& p) {\n return os << fixed << setprecision(12) << p.real() << ' ' << p.imag();\n}\ninline bool eq(Real a, Real b) {\n return fabs(a - b) < EPS;\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\n// Line\nstruct Line {\n Point p1, p2;\n Line() = default;\n Line(Point p1, Point p2) :p1(p1), p2(p2) {}\n //Ax + By = C\n Line(Real A, Real B, Real C) {\n if (eq(A, 0)) p1 = Point(0, C / B), p2 = Point(1, C / B);\n else if (eq(B, 0))p1 = Point(C / A, 0), p2 = Point(C / A, 1);\n else p1 = Point(0, C / B), p2 = Point(C / A, 0);\n }\n};\n\n// Segment\nstruct Segment :Line {\n Segment() = default;\n Segment(Point p1, Point p2) :Line(p1, p2) {}\n};\nReal dot(Point a, Point b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\nReal cross(Point a, Point b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return 1;//COUNTER CLOCKWISE\n else if (cross(b, c) < -EPS) return -1;//CLOCKWISE\n else if (dot(b, c) < 0) return 2;//c--a--b ONLINE BACK\n else if (norm(b) < norm(c)) return -2;//a--b--c ONLINE FRONT\n else return 0;//a--c--b ON SEGMENT\n}\nPoint projection(Segment l, Point p) {\n Real k = dot(l.p1 - l.p2, p - l.p1) / norm(l.p1 - l.p2);\n return l.p1 + (l.p1 - l.p2) * k;\n}\nbool intersect(Segment s, Point p) {\n return ccw(s.p1, s.p2, p) == 0;\n}\nReal dis(Point a, Point b) {\n return abs(a - b);\n}\nReal dis(Segment s, Point p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\n// //AtoBで途中から水でPへ\npair<double, Point> min_point(Point P, Point A, Point B, double G, double W) {\n double res = min(dis(P, A) * W, dis(P, B) * W + dis(A, B) * G);\n double L = 0.0, R = 1.0;\n Point D = B - A;\n rep(i, 100) {\n double c1 = (L * 2.0 + R) / 3.0;\n double c2 = (L + 2.0 * R) / 3.0;\n Point P1, P2;\n\n P1 = { A.real() + c1 * D.real(),A.imag() + c1 * D.imag() };\n P2 = { A.real() + c2 * D.real(),A.imag() + c2 * D.imag() };\n double d1 = dis(P, P1) * W + dis(A, P1) * G;\n double d2 = dis(P, P2) * W + dis(A, P2) * G;\n if (d1 < d2) {\n R = c2;\n }\n else {\n L = c1;\n }\n }\n Point P1;\n P1 = Point({ A.real() + L * D.real(),A.imag() + L * D.imag() });\n return { dis(P,P1) * W + dis(A,P1) * G,P1 };\n}\n\nvoid solve(ll N) {\n vector<Point> P(N);\n rep(i, N) {\n cin >> P[i];\n }\n double G, W;\n cin >> G >> W;\n cout << fixed << setprecision(14);\n Point S, T;\n cin >> S >> T;\n double an = dis(S, T) * W;\n double bn=1e18;\n rep(_, 2) {\n ll bs = 0;\n rep(i, N) {\n if (dis(Segment(P[i], P[(i + 1) % N]), S) < EPS * 2.0) {\n bs = i;\n break;\n }\n }\n double RUN = 0.0;\n for (ll i = bs; i < bs + N; i++) {\n if (i == bs) {\n pair<double, Point> dP = min_point(T, S, P[(i + 1) % N], G, W);\n double d = dP.first;\n Point H = dP.second;\n an = min(an, d);\n RUN += dis(S, P[(i + 1) % N]) * G;\n }\n else {\n pair<double, Point> dP = min_point(T, P[i%N], P[(i + 1) % N], G, W);\n double d = dP.first;\n Point H = dP.second;\n an = min(an, d + RUN);\n RUN += dis(P[i%N], P[(i + 1) % N]) * G;\n\n for(ll j=bs+1;j<=i;j++){\n pair<double, Point> dQ = min_point(S, P[(j+1)%N], P[j%N], G, W);\n \n double d2 = dQ.first;\n Point H2 = dQ.second;\n if(j==i){\n if(dis(H,P[i%N])+dis(H2,P[(i+1)%N])<dis(P[i%N],P[(i+1)%N]))continue;\n double dn=d+d2-dis(P[i%N],P[(i+1)%N])*G;\n \n an=min(an,dn);\n }\n else{\n double ru=0.0;\n for(ll k=j+1;k<i;k++){\n ru+=dis(P[k%N],P[(k+1)%N])*G;\n }\n double dn=d+d2+ru;\n an=min(an,d+d2+ru); \n }\n \n }\n }\n \n }\n reverse(all(P));\n }\n\n cout << an << endl;\n return;\n}\n\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N;\n while (cin >> N, N != 0)solve(N);\n\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3400, "score_of_the_acc": -0.1344, "final_rank": 12 }, { "submission_id": "aoj_2016_7243201", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstruct Point {\n\tdouble px, py;\n};\n\nPoint operator+(const Point &a1, const Point &a2) {\n\treturn Point{ a1.px + a2.px, a1.py + a2.py };\n}\n\nPoint operator-(const Point &a1, const Point &a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nPoint operator*(const Point &a1, const double &a2) {\n\treturn Point{ a1.px * a2, a1.py * a2 };\n}\n\ndouble ABS(Point A1) {\n\treturn sqrt(A1.px * A1.px + A1.py * A1.py);\n}\n\ndouble crs(Point A1, Point A2) {\n\treturn A1.px * A2.py - A1.py * A2.px;\n}\n\nint N, Guard_Idx;\nPoint G[309], S, T;\ndouble TG, TW, dist_sum[309], dist_pos[309];\ndouble dist[309][309];\n\nPoint GetPoint(int idx, double prog) {\n\tPoint P1 = G[(idx + 0) % N];\n\tPoint P2 = G[(idx + 1) % N];\n\treturn P1 + ((P2 - P1) * prog);\n}\n\nvoid solve() {\n\t// Initialize\n\tdist_sum[0] = 0;\n\tfor (int i = 0; i < N; i++) dist_pos[i] = ABS(G[i] - G[(i + 1) % N]);\n\tfor (int i = 0; i < N; i++) dist_sum[i + 1] = dist_sum[i] + dist_pos[i];\n\tG[N] = S;\n\tG[N + 1] = T;\n\t\n\t// Get Place of S\n\tvector<pair<int, double>> Candidate;\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint P1 = G[(i + 0) % N];\n\t\tPoint P2 = G[(i + 1) % N];\n\t\tPoint P3 = S;\n\t\tif (abs(crs(P2 - P1, P3 - P1)) < 1.0e-6) {\n\t\t\tGuard_Idx = i;\n\t\t\tCandidate.push_back(make_pair(i, ABS(P3 - P1) / ABS(P2 - P1)));\n\t\t\tbreak;\n\t\t}\n\t}\n\t\n\t// Find Candidate\n\tfor (int i = 0; i < N; i++) Candidate.push_back(make_pair(i, 0.0));\n\tfor (int i = 0; i <= N + 1; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tint idx1 = (j + 0) % N;\n\t\t\tint idx2 = (j + 1) % N;\n\t\t\tif (idx1 == i || idx2 == i) continue;\n\t\t\t\n\t\t\t// Ternary Search 1\n\t\t\tif (true) {\n\t\t\t\tdouble cl = 0.0, cr = 1.0, c1, c2;\n\t\t\t\tfor (int k = 0; k < 90; k++) {\n\t\t\t\t\tc1 = (cl + cl + cr) / 3.0;\n\t\t\t\t\tc2 = (cl + cr + cr) / 3.0;\n\t\t\t\t\tPoint P1 = G[i];\n\t\t\t\t\tPoint P2 = GetPoint(j, c1);\n\t\t\t\t\tPoint P3 = GetPoint(j, c2);\n\t\t\t\t\tdouble dist1 = ABS(P2 - P1) * TW + (1.0 - c1) * dist_pos[j] * TG;\n\t\t\t\t\tdouble dist2 = ABS(P3 - P1) * TW + (1.0 - c2) * dist_pos[j] * TG;\n\t\t\t\t\tif (dist1 > dist2) { cl = c1; }\n\t\t\t\t\telse { cr = c2; }\n\t\t\t\t}\n\t\t\t\t// cout << \"# \" << i << \" \" << j << \" \" << cl << endl;\n\t\t\t\tCandidate.push_back(make_pair(j, cl));\n\t\t\t}\n\t\t\t\n\t\t\t// Ternary Search 2\n\t\t\tif (true) {\n\t\t\t\tdouble cl = 0.0, cr = 1.0, c1, c2;\n\t\t\t\tfor (int k = 0; k < 90; k++) {\n\t\t\t\t\tc1 = (cl + cl + cr) / 3.0;\n\t\t\t\t\tc2 = (cl + cr + cr) / 3.0;\n\t\t\t\t\tPoint P1 = G[i];\n\t\t\t\t\tPoint P2 = GetPoint(j, c1);\n\t\t\t\t\tPoint P3 = GetPoint(j, c2);\n\t\t\t\t\tdouble dist1 = ABS(P2 - P1) * TW + c1 * dist_pos[j] * TG;\n\t\t\t\t\tdouble dist2 = ABS(P3 - P1) * TW + c2 * dist_pos[j] * TG;\n\t\t\t\t\tif (dist1 > dist2) { cl = c1; }\n\t\t\t\t\telse { cr = c2; }\n\t\t\t\t}\n\t\t\t\t// cout << \"# \" << i << \" \" << j << \" \" << cl << endl;\n\t\t\t\tCandidate.push_back(make_pair(j, cl));\n\t\t\t}\n\t\t}\n\t}\n\tint NumCand = Candidate.size();\n\tCandidate.push_back(make_pair(-1.0, -1.0));\n\t\n\t// Get Distance\n\tfor (int i = 0; i <= NumCand; i++) {\n\t\tfor (int j = 0; j <= NumCand; j++) {\n\t\t\tPoint P1 = T; if (i < NumCand) P1 = GetPoint(Candidate[i].first, Candidate[i].second);\n\t\t\tPoint P2 = T; if (j < NumCand) P2 = GetPoint(Candidate[j].first, Candidate[j].second);\n\t\t\tdist[i][j] = ABS(P1 - P2) * TW;\n\t\t\tif (i < NumCand && j < NumCand) {\n\t\t\t\tdouble r1 = dist_sum[Candidate[i].first] + dist_pos[Candidate[i].first] * Candidate[i].second;\n\t\t\t\tdouble r2 = dist_sum[Candidate[j].first] + dist_pos[Candidate[j].first] * Candidate[j].second;\n\t\t\t\tdouble ground = min(abs(r1 - r2), dist_sum[N] - abs(r1 - r2));\n\t\t\t\tdist[i][j] = min(dist[i][j], ground * TG);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// Warshall-Floyd\n\tfor (int k = 0; k <= NumCand; k++) {\n\t\tfor (int i = 0; i <= NumCand; i++) {\n\t\t\tfor (int j = 0; j <= NumCand; j++) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n\t\t}\n\t}\n\t\n\t// Output\n\tprintf(\"%.12lf\\n\", dist[0][NumCand]);\n}\n\nint main() {\n\twhile (true) {\n\t\t// Input\n\t\tcin >> N;\n\t\tfor (int i = 0; i < N; i++) cin >> G[i].px >> G[i].py;\n\t\tif (N == 0) break;\n\t\tcin >> TG >> TW;\n\t\tcin >> S.px >> S.py;\n\t\tcin >> T.px >> T.py;\n\t\t\n\t\t// Solve\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 4092, "score_of_the_acc": -0.3311, "final_rank": 16 }, { "submission_id": "aoj_2016_5959532", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\nusing Real = double;\nusing Point = complex< double >;\nusing Polygon = vector< Point >;\nconst Real EPS = 1e-10, PI = acos(-1);\nconstexpr int COUNTER_CLOCKWISE = +1;\nconstexpr int CLOCKWISE = -1;\nconstexpr int ONLINE_BACK = +2; // c-a-b\nconstexpr int ONLINE_FRONT = -2; // a-b-c\nconstexpr int ON_SEGMENT = 0; // a-c-b\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nPoint operator/(const Point &p, const Real &d) {\n return Point(real(p) / d, imag(p) / d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b; is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, const Point &p) {\n return os << fixed << setprecision(10) << real(p) << \" \" << imag(p);\n}\n\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\n// 符号\ninline int sign(const Real &r) {\n return r <= -EPS ? -1 : r >= EPS ? 1 : 0;\n}\n\n// a = b ? \ninline bool equals(const Real &a, const Real &b) {\n return sign(a - b) == 0;\n}\n\n\n// 単位ベクトル\nPoint unitVec(const Point &a){ return a / abs(a); }\n\n// 法線ベクトル 半時計周り\nPoint normVec(const Point &a){ return a * Point(0, 1); }\n//Point normVec(const Point &a){ return a * Point(0,-1); }\n\n// 内積\nReal dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n\n// 外積\nReal cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nPoint rotate(const Point &p, const Real &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal Radian_To_Degree(const Real &radian){ return radian * 180.0 / PI; }\nReal Degree_To_Radian(const Real &degree){ return degree * PI / 180.0; }\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n\n // Ax + By = C\n Line(Real A, Real B, Real C) {\n if(equals(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n }else if(equals(B,0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n }else{\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.p << \" \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.p >> c.r;\n }\n};\n\n// 直線lに点pから引いた垂線の足を求める\nPoint projection(const Line &l, const Point &p) {\n Real t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n Real t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// 直線lに関して点pと対称な点を求める\nPoint reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * 2.0;\n}\n\n// 点の回転方向\n// 点aを基準に点b,cの位置関係\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if(sign(cross(b, c)) == +1) return COUNTER_CLOCKWISE; // 反時計回り\n if(sign(cross(b, c)) == -1) return CLOCKWISE; // 時計回り\n if(sign(dot(b, c)) == -1) return ONLINE_BACK; // c - a - b\n if(norm(b) < norm(c)) return ONLINE_FRONT; // a - b - c\n return ON_SEGMENT; // a - c - b\n}\n\n// 2直線の直交判定\nbool is_orthogonal(const Line &a, const Line &b) {\n return equals(dot(a.b - a.a, b.b - b.a), 0);\n}\n\n// 2直線の平行判定\nbool is_parallel(const Line &a, const Line &b) {\n return equals(cross(a.b - a.a, b.b - b.a), 0);\n}\n\n// 線分の交差判定\nbool is_intersect_ss(const Segment &s, const Segment &t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 直線の交差判定\nbool is_intersect_ll(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(equals(abs(A), 0) && equals(abs(B), 0)) return true;\n return !is_parallel(l, m);\n}\n\n// 線分に点が乗っているか\nbool is_intersect_sp(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == ON_SEGMENT;\n}\n\n// 点と点の距離\nReal distance_pp(const Point &p1, const Point &p2) {\n return sqrt(pow(real(p1)-real(p2), 2) + pow(imag(p1)-imag(p2), 2));\n}\n\n// 点と直線の距離\nReal distance_lp(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\n// 直線と直線の距離\nReal distance_ll(const Line &l, const Line &m) {\n return is_intersect_ll(l, m) ? 0 : distance_lp(l, m.a);\n}\n\n// 点と線分の距離\nReal distance_sp(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if(is_intersect_sp(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nReal distance_ss(const Segment &a, const Segment &b) {\n if(is_intersect_ss(a, b)) return 0;\n return min({distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b)});\n}\n\n// 交点\nPoint cross_point_ll(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(equals(abs(A), 0) && equals(abs(B), 0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// 円と円の位置関係\nint intersect(Circle c1, Circle c2) {\n if(c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r < d) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(c1.r - c2.r < d) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\nReal area(const Polygon &p){\n Real A = 0;\n for(int i = 0; i < p.size(); i++){\n A += cross(p[i], p[(i + 1) % p.size()]);\n }\n return A * 0.5;\n}\n\nbool is_convex(const Polygon &p){\n for(int i = 0; i < p.size(); i++){\n if(ccw(p[(i + p.size() - 1) % p.size()], p[i], p[(i + 1) % p.size()]) == -1) return false;\n }\n return true;\n}\n\nenum { OUT, ON, IN };\nint contains(const Polygon &Q, const Point &p){\n bool in = false;\n for(int i = 0; i < Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\nPolygon convex_hull(Polygon &p, bool strict = true){\n int n = (int)p.size(), k = 0;\n if(n <= 2) return p;\n sort(p.begin(), p.end());\n Polygon ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]){\n while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]){\n while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\nReal convex_diameter(const Polygon &p){\n int n = (int)p.size();\n int is = 0, js = 0;\n for(int i = 1; i < n; i++){\n if(p[i].imag() > p[is].imag()) is = i;\n if(p[i].imag() < p[js].imag()) js = i;\n }\n Real maxdis = norm(p[is] - p[js]);\n int maxi, maxj, i, j;\n i = maxi = is;\n j = maxj = js;\n do {\n if(cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0){\n j = (j + 1) % n;\n }else{\n i = (i + 1) % n;\n }\n\n if(norm(p[i] - p[j]) > maxdis){\n maxdis = norm(p[i] - p[j]);\n maxi = i;\n maxj = j;\n }\n\n }while(i != is || j != js);\n return sqrt(maxdis);\n}\n\nPolygon convex_cut(const Polygon &U, Line l){\n Polygon ret;\n for(int i = 0; i < U.size(); i++){\n Point now = U[i], nxt = U[(i + 1) % U.size()];\n if(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0){\n ret.push_back(cross_point_ll(Line(now, nxt), l));\n }\n }\n return ret;\n}\n\nReal ClosetPair(Polygon& ps){\n if(ps.size() <= 1) throw (0);\n sort(begin(ps), end(ps));\n\n auto compare_y = [&](const Point &a, const Point &b) {\n return a.imag() < b.imag();\n };\n Polygon beet(ps.size());\n const double INF = 1e18;\n\n function< double(int, int) > rec = [&](int left, int right) {\n if(right - left <= 1) return INF;\n int mid = (left + right) >> 1;\n auto x = ps[mid].real();\n auto ret = min(rec(left, mid), rec(mid, right));\n inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n int ptr = 0;\n for(int i = left; i < right; i++) {\n if(fabs((ps[i].real()) - x) >= ret) continue;\n for(int j = 0; j < ptr; j++) {\n auto luz = ps[i] - beet[ptr - j - 1];\n if(luz.imag() >= ret) break;\n ret = min(ret, abs(luz));\n }\n beet[ptr++] = ps[i];\n }\n return ret;\n };\n return rec(0, (int) ps.size());\n}\n\npair< Point, Point > cross_point_cc(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\nconst int BOTTOM = 0;\nconst int LEFT = 1;\nconst int RIGHT = 2;\nconst int TOP = 3;\n\nclass endPoint {\n public:\n Point p;\n int seg; // id of Point\n int st; // kind of Point\n endPoint(){}\n endPoint(Point inp, int inseg, int inst):p(inp),seg(inseg),st(inst){}\n bool operator<(const endPoint &ep)const {\n if(p.imag() == ep.p.imag()) return st < ep.st;\n else return p.imag() < ep.p.imag();\n }\n};\n\nendPoint EP[220000];\nint Manhattan_Intersections(vector< Segment > s){\n int n = s.size();\n double hoge;\n\n for(int i = 0, k = 0; i < n; i++){\n if(s[i].a.imag() == s[i].b.imag()){\n if(s[i].a.real() > s[i].b.real()){\n hoge = s[i].a.real();\n s[i].a.real(s[i].b.real());\n s[i].b.real(hoge);\n }\n }else if(s[i].a.imag() > s[i].b.imag()){\n hoge = s[i].a.imag();\n s[i].a.imag(s[i].b.imag());\n s[i].b.imag(hoge);\n }\n\n if(s[i].a.imag() == s[i].b.imag()){\n EP[k++] = endPoint(s[i].a, i, LEFT);\n EP[k++] = endPoint(s[i].b, i, RIGHT);\n }else{\n EP[k++] = endPoint(s[i].a, i, BOTTOM);\n EP[k++] = endPoint(s[i].b, i, TOP);\n }\n }\n\n sort(EP, EP + 2 * n);\n set<int> BT;\n BT.insert(1000000001);\n int cnt = 0;\n for(int i = 0; i < 2 * n; i++){\n if(EP[i].st == TOP) BT.erase(EP[i].p.real());\n else if(EP[i].st == BOTTOM) BT.insert(EP[i].p.real());\n else if(EP[i].st == LEFT){\n set<int>::iterator b = lower_bound(BT.begin(), BT.end(), s[EP[i].seg].a.real());\n set<int>::iterator e = upper_bound(BT.begin(), BT.end(), s[EP[i].seg].b.real());\n cnt += distance(b, e);\n }\n }\n\n return cnt;\n}\n\n// 内接円\nCircle Inscribed_Circle(const Point& a, const Point& b, const Point& c){\n double A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point x = Point((a * A + b * B + c * C) / (A + B + C));\n double r = distance_sp(Segment(a,b),x);\n return Circle(x, r);\n}\n\n// 外接円\nCircle Circumscribed_Circle(const Point& a, const Point& b, const Point& c){\n Point m1((a+b)/2.0), m2((b+c)/2.0);\n Point v((b-a).imag(), (a-b).real()), w((b-c).imag(), (c-b).real());\n Line s(m1, Point(m1+v)), t(m2, Point(m2+w));\n const Point x = cross_point_ll(s, t);\n return Circle(x, abs(a-x));\n}\n\npair< Point, Point > cross_point_cl(const Circle &c, const Line &l) {\n Point pr = projection(l, c.p);\n if(equals(abs(pr - c.p), c.r)) return {pr, pr};\n Point e = (l.b - l.a) / abs(l.b - l.a);\n auto k = sqrt(norm(c.r) - norm(pr - c.p));\n return {pr - e * k, pr + e * k};\n}\n\n// 点pを通る円の接線\npair<Point,Point> tangent_cp(const Circle &c, const Point &p) {\n return cross_point_cc(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\n\n// 二つの円の共通接線\nvector< Line > tangent_cc(Circle c1, Circle c2){\n vector< Line > ret;\n if(c1.r < c2.r) swap(c1,c2);\n double g = norm(c1.p-c2.p);\n if(equals(g,0.0)) return ret;\n Point u = (c2.p-c1.p)/sqrt(g);\n Point v = rotate(u, PI * 0.5);\n for(int s:{-1,1}){\n double h = (c1.r+ s*c2.r)/sqrt(g);\n if(equals(1- h*h, 0)){\n ret.emplace_back(c1.p+u*c1.r, c1.p+(u+v)*c1.r);\n }else if(1-h*h > 0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ret.emplace_back(c1.p+(uu+vv)*c1.r, c2.p-(uu+vv)*c2.r*s);\n ret.emplace_back(c1.p+(uu-vv)*c1.r, c2.p-(uu-vv)*c2.r*s);\n }\n }\n return ret;\n}\n\nReal area_poly_c(const Polygon &p, const Circle &c) {\n if(p.size() < 3) return 0.0;\n function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if(equals(f, 0.0)) return ret;\n if(max(abs(va), abs(vb)) < c.r + EPS) return f;\n if(distance_sp(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n auto u = cross_point_cl(c, Segment(a, b));\n vector< Point > tot{a, u.first, u.second, b};\n for(int i = 0; i + 1 < tot.size(); i++) {\n ret += cross_area(c, tot[i], tot[i + 1]);\n }\n return ret;\n };\n Real A = 0;\n for(int i = 0; i < p.size(); i++) {\n A += cross_area(c, p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\nReal area_cc(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r <= d + EPS) return 0.0;\n if(d <= fabs(c1.r - c2.r) + EPS) {\n Real r = min(c1.r, c2.r);\n return r * r * PI;\n }\n Real rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n Real theta = acos(rc / c1.r);\n Real phi = acos((d - rc) / c2.r);\n return (c1.r * c1.r*theta + c2.r*c2.r*phi - d*c1.r*sin(theta));\n}\n\n// g <- pair < v , cost > \ntemplate < class T >\nvector<T> Dijkstra(vector<vector<pair<int,T>>> &g, int s) {\n vector<T> dist(g.size(), -1);\n priority_queue<pair<T,int>> Q;\n Q.emplace(0, s);\n while(!Q.empty()){\n T cost = Q.top().first;\n int v = Q.top().second;\n Q.pop();\n cost = - cost;\n if(dist[v] == -1){\n dist[v] = cost;\n for(auto e : g[v]){\n if(dist[e.first] == -1){\n Q.emplace(-(cost + e.second), e.first);\n }\n }\n }\n }\n return dist;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n while(true) {\n int n; cin >> n; if(n == 0) break;\n Polygon p(n);\n rep(i,n) cin >> p[i];\n Real tg,tw; cin >> tg >> tw;\n Point s,t; cin >> s >> t;\n\n auto f = [&](Segment l, Point q) {\n Point lo = l.a, hi = l.b;\n rep(_,50) {\n Point m1 = (lo + lo + hi) / 3.0;\n Point m2 = (lo + hi + hi) / 3.0;\n auto get = [&](Point x) { return abs(x - l.a) * tg + abs(x - q) * tw; };\n if(get(m1) > get(m2)) lo = m1; else hi = m2;\n }\n return lo;\n };\n\n vector< Point > v; v.push_back(s); v.push_back(t);\n rep(i,n){\n Segment l(p[i], p[(i + 1) % n]), r(p[(i + 1) % n], p[i]);\n rep(j,n) if(i != j) {\n v.push_back(f(l, p[j]));\n v.push_back(f(r, p[j])); \n }\n v.push_back(f(l, s)); v.push_back(f(r, s));\n v.push_back(f(r, t)); v.push_back(f(l, t));\n }\n\n int M = v.size();\n vector<vector< Real >> d(M, vector< Real >(M, 1e9));\n rep(i,M)rep(j,M) {\n d[i][j] = d[j][i] = abs(v[i] - v[j]) * tw;\n rep(k,n) {\n if(is_intersect_sp(Segment(p[k], p[(k + 1) % n]), v[i])\n && is_intersect_sp(Segment(p[k], p[(k + 1) % n]), v[j]))\n d[i][j] = d[j][i] = min(d[i][j], abs(v[i] - v[j]) * tg);\n }\n }\n \n rep(k,M)rep(i,M)rep(j,M) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n cout.precision(17);\n cout << d[0][1] << endl;\n }\n}", "accuracy": 1, "time_ms": 2250, "memory_kb": 9000, "score_of_the_acc": -0.7346, "final_rank": 17 }, { "submission_id": "aoj_2016_5959510", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\nusing Real = double;\nusing Point = complex< double >;\nusing Polygon = vector< Point >;\nconst Real EPS = 1e-10, PI = acos(-1);\nconstexpr int COUNTER_CLOCKWISE = +1;\nconstexpr int CLOCKWISE = -1;\nconstexpr int ONLINE_BACK = +2; // c-a-b\nconstexpr int ONLINE_FRONT = -2; // a-b-c\nconstexpr int ON_SEGMENT = 0; // a-c-b\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nPoint operator/(const Point &p, const Real &d) {\n return Point(real(p) / d, imag(p) / d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b; is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, const Point &p) {\n return os << fixed << setprecision(10) << real(p) << \" \" << imag(p);\n}\n\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\n// 符号\ninline int sign(const Real &r) {\n return r <= -EPS ? -1 : r >= EPS ? 1 : 0;\n}\n\n// a = b ? \ninline bool equals(const Real &a, const Real &b) {\n return sign(a - b) == 0;\n}\n\n\n// 単位ベクトル\nPoint unitVec(const Point &a){ return a / abs(a); }\n\n// 法線ベクトル 半時計周り\nPoint normVec(const Point &a){ return a * Point(0, 1); }\n//Point normVec(const Point &a){ return a * Point(0,-1); }\n\n// 内積\nReal dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n\n// 外積\nReal cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nPoint rotate(const Point &p, const Real &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal Radian_To_Degree(const Real &radian){ return radian * 180.0 / PI; }\nReal Degree_To_Radian(const Real &degree){ return degree * PI / 180.0; }\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n\n // Ax + By = C\n Line(Real A, Real B, Real C) {\n if(equals(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n }else if(equals(B,0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n }else{\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.p << \" \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.p >> c.r;\n }\n};\n\n// 直線lに点pから引いた垂線の足を求める\nPoint projection(const Line &l, const Point &p) {\n Real t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n Real t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// 直線lに関して点pと対称な点を求める\nPoint reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * 2.0;\n}\n\n// 点の回転方向\n// 点aを基準に点b,cの位置関係\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if(sign(cross(b, c)) == +1) return COUNTER_CLOCKWISE; // 反時計回り\n if(sign(cross(b, c)) == -1) return CLOCKWISE; // 時計回り\n if(sign(dot(b, c)) == -1) return ONLINE_BACK; // c - a - b\n if(norm(b) < norm(c)) return ONLINE_FRONT; // a - b - c\n return ON_SEGMENT; // a - c - b\n}\n\n// 2直線の直交判定\nbool is_orthogonal(const Line &a, const Line &b) {\n return equals(dot(a.b - a.a, b.b - b.a), 0);\n}\n\n// 2直線の平行判定\nbool is_parallel(const Line &a, const Line &b) {\n return equals(cross(a.b - a.a, b.b - b.a), 0);\n}\n\n// 線分の交差判定\nbool is_intersect_ss(const Segment &s, const Segment &t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 直線の交差判定\nbool is_intersect_ll(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(equals(abs(A), 0) && equals(abs(B), 0)) return true;\n return !is_parallel(l, m);\n}\n\n// 線分に点が乗っているか\nbool is_intersect_sp(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == ON_SEGMENT;\n}\n\n// 点と点の距離\nReal distance_pp(const Point &p1, const Point &p2) {\n return sqrt(pow(real(p1)-real(p2), 2) + pow(imag(p1)-imag(p2), 2));\n}\n\n// 点と直線の距離\nReal distance_lp(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\n// 直線と直線の距離\nReal distance_ll(const Line &l, const Line &m) {\n return is_intersect_ll(l, m) ? 0 : distance_lp(l, m.a);\n}\n\n// 点と線分の距離\nReal distance_sp(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if(is_intersect_sp(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nReal distance_ss(const Segment &a, const Segment &b) {\n if(is_intersect_ss(a, b)) return 0;\n return min({distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b)});\n}\n\n// 交点\nPoint cross_point_ll(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(equals(abs(A), 0) && equals(abs(B), 0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// 円と円の位置関係\nint intersect(Circle c1, Circle c2) {\n if(c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r < d) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(c1.r - c2.r < d) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\nReal area(const Polygon &p){\n Real A = 0;\n for(int i = 0; i < p.size(); i++){\n A += cross(p[i], p[(i + 1) % p.size()]);\n }\n return A * 0.5;\n}\n\nbool is_convex(const Polygon &p){\n for(int i = 0; i < p.size(); i++){\n if(ccw(p[(i + p.size() - 1) % p.size()], p[i], p[(i + 1) % p.size()]) == -1) return false;\n }\n return true;\n}\n\nenum { OUT, ON, IN };\nint contains(const Polygon &Q, const Point &p){\n bool in = false;\n for(int i = 0; i < Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\nPolygon convex_hull(Polygon &p, bool strict = true){\n int n = (int)p.size(), k = 0;\n if(n <= 2) return p;\n sort(p.begin(), p.end());\n Polygon ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]){\n while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]){\n while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\nReal convex_diameter(const Polygon &p){\n int n = (int)p.size();\n int is = 0, js = 0;\n for(int i = 1; i < n; i++){\n if(p[i].imag() > p[is].imag()) is = i;\n if(p[i].imag() < p[js].imag()) js = i;\n }\n Real maxdis = norm(p[is] - p[js]);\n int maxi, maxj, i, j;\n i = maxi = is;\n j = maxj = js;\n do {\n if(cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0){\n j = (j + 1) % n;\n }else{\n i = (i + 1) % n;\n }\n\n if(norm(p[i] - p[j]) > maxdis){\n maxdis = norm(p[i] - p[j]);\n maxi = i;\n maxj = j;\n }\n\n }while(i != is || j != js);\n return sqrt(maxdis);\n}\n\nPolygon convex_cut(const Polygon &U, Line l){\n Polygon ret;\n for(int i = 0; i < U.size(); i++){\n Point now = U[i], nxt = U[(i + 1) % U.size()];\n if(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0){\n ret.push_back(cross_point_ll(Line(now, nxt), l));\n }\n }\n return ret;\n}\n\nReal ClosetPair(Polygon& ps){\n if(ps.size() <= 1) throw (0);\n sort(begin(ps), end(ps));\n\n auto compare_y = [&](const Point &a, const Point &b) {\n return a.imag() < b.imag();\n };\n Polygon beet(ps.size());\n const double INF = 1e18;\n\n function< double(int, int) > rec = [&](int left, int right) {\n if(right - left <= 1) return INF;\n int mid = (left + right) >> 1;\n auto x = ps[mid].real();\n auto ret = min(rec(left, mid), rec(mid, right));\n inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n int ptr = 0;\n for(int i = left; i < right; i++) {\n if(fabs((ps[i].real()) - x) >= ret) continue;\n for(int j = 0; j < ptr; j++) {\n auto luz = ps[i] - beet[ptr - j - 1];\n if(luz.imag() >= ret) break;\n ret = min(ret, abs(luz));\n }\n beet[ptr++] = ps[i];\n }\n return ret;\n };\n return rec(0, (int) ps.size());\n}\n\npair< Point, Point > cross_point_cc(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\nconst int BOTTOM = 0;\nconst int LEFT = 1;\nconst int RIGHT = 2;\nconst int TOP = 3;\n\nclass endPoint {\n public:\n Point p;\n int seg; // id of Point\n int st; // kind of Point\n endPoint(){}\n endPoint(Point inp, int inseg, int inst):p(inp),seg(inseg),st(inst){}\n bool operator<(const endPoint &ep)const {\n if(p.imag() == ep.p.imag()) return st < ep.st;\n else return p.imag() < ep.p.imag();\n }\n};\n\nendPoint EP[220000];\nint Manhattan_Intersections(vector< Segment > s){\n int n = s.size();\n double hoge;\n\n for(int i = 0, k = 0; i < n; i++){\n if(s[i].a.imag() == s[i].b.imag()){\n if(s[i].a.real() > s[i].b.real()){\n hoge = s[i].a.real();\n s[i].a.real(s[i].b.real());\n s[i].b.real(hoge);\n }\n }else if(s[i].a.imag() > s[i].b.imag()){\n hoge = s[i].a.imag();\n s[i].a.imag(s[i].b.imag());\n s[i].b.imag(hoge);\n }\n\n if(s[i].a.imag() == s[i].b.imag()){\n EP[k++] = endPoint(s[i].a, i, LEFT);\n EP[k++] = endPoint(s[i].b, i, RIGHT);\n }else{\n EP[k++] = endPoint(s[i].a, i, BOTTOM);\n EP[k++] = endPoint(s[i].b, i, TOP);\n }\n }\n\n sort(EP, EP + 2 * n);\n set<int> BT;\n BT.insert(1000000001);\n int cnt = 0;\n for(int i = 0; i < 2 * n; i++){\n if(EP[i].st == TOP) BT.erase(EP[i].p.real());\n else if(EP[i].st == BOTTOM) BT.insert(EP[i].p.real());\n else if(EP[i].st == LEFT){\n set<int>::iterator b = lower_bound(BT.begin(), BT.end(), s[EP[i].seg].a.real());\n set<int>::iterator e = upper_bound(BT.begin(), BT.end(), s[EP[i].seg].b.real());\n cnt += distance(b, e);\n }\n }\n\n return cnt;\n}\n\n// 内接円\nCircle Inscribed_Circle(const Point& a, const Point& b, const Point& c){\n double A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point x = Point((a * A + b * B + c * C) / (A + B + C));\n double r = distance_sp(Segment(a,b),x);\n return Circle(x, r);\n}\n\n// 外接円\nCircle Circumscribed_Circle(const Point& a, const Point& b, const Point& c){\n Point m1((a+b)/2.0), m2((b+c)/2.0);\n Point v((b-a).imag(), (a-b).real()), w((b-c).imag(), (c-b).real());\n Line s(m1, Point(m1+v)), t(m2, Point(m2+w));\n const Point x = cross_point_ll(s, t);\n return Circle(x, abs(a-x));\n}\n\npair< Point, Point > cross_point_cl(const Circle &c, const Line &l) {\n Point pr = projection(l, c.p);\n if(equals(abs(pr - c.p), c.r)) return {pr, pr};\n Point e = (l.b - l.a) / abs(l.b - l.a);\n auto k = sqrt(norm(c.r) - norm(pr - c.p));\n return {pr - e * k, pr + e * k};\n}\n\n// 点pを通る円の接線\npair<Point,Point> tangent_cp(const Circle &c, const Point &p) {\n return cross_point_cc(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\n\n// 二つの円の共通接線\nvector< Line > tangent_cc(Circle c1, Circle c2){\n vector< Line > ret;\n if(c1.r < c2.r) swap(c1,c2);\n double g = norm(c1.p-c2.p);\n if(equals(g,0.0)) return ret;\n Point u = (c2.p-c1.p)/sqrt(g);\n Point v = rotate(u, PI * 0.5);\n for(int s:{-1,1}){\n double h = (c1.r+ s*c2.r)/sqrt(g);\n if(equals(1- h*h, 0)){\n ret.emplace_back(c1.p+u*c1.r, c1.p+(u+v)*c1.r);\n }else if(1-h*h > 0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ret.emplace_back(c1.p+(uu+vv)*c1.r, c2.p-(uu+vv)*c2.r*s);\n ret.emplace_back(c1.p+(uu-vv)*c1.r, c2.p-(uu-vv)*c2.r*s);\n }\n }\n return ret;\n}\n\nReal area_poly_c(const Polygon &p, const Circle &c) {\n if(p.size() < 3) return 0.0;\n function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if(equals(f, 0.0)) return ret;\n if(max(abs(va), abs(vb)) < c.r + EPS) return f;\n if(distance_sp(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n auto u = cross_point_cl(c, Segment(a, b));\n vector< Point > tot{a, u.first, u.second, b};\n for(int i = 0; i + 1 < tot.size(); i++) {\n ret += cross_area(c, tot[i], tot[i + 1]);\n }\n return ret;\n };\n Real A = 0;\n for(int i = 0; i < p.size(); i++) {\n A += cross_area(c, p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\nReal area_cc(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r <= d + EPS) return 0.0;\n if(d <= fabs(c1.r - c2.r) + EPS) {\n Real r = min(c1.r, c2.r);\n return r * r * PI;\n }\n Real rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n Real theta = acos(rc / c1.r);\n Real phi = acos((d - rc) / c2.r);\n return (c1.r * c1.r*theta + c2.r*c2.r*phi - d*c1.r*sin(theta));\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n while(true) {\n int n; cin >> n; if(n == 0) break;\n Polygon p(n);\n rep(i,n) cin >> p[i];\n Real tg,tw; cin >> tg >> tw;\n Point s,t; cin >> s >> t;\n\n auto f = [&](Segment l, Point q) {\n Point lo = l.a, hi = l.b;\n rep(_,50) {\n Point m1 = (lo + lo + hi) / 3.0;\n Point m2 = (lo + hi + hi) / 3.0;\n auto get = [&](Point x) { return abs(x - l.a) * tg + abs(x - q) * tw; };\n if(get(m1) > get(m2)) lo = m1; else hi = m2;\n }\n return lo;\n };\n\n vector< Point > v; v.push_back(s); v.push_back(t);\n rep(i,n)rep(j,n) if(i != j) {\n v.push_back(f(Segment(p[i], p[(i + 1) % n]), p[j]));\n v.push_back(f(Segment(p[(i + 1) % n], p[i]), p[j]));\n }\n\n rep(i,n) {\n v.push_back(f(Segment(p[i], p[(i + 1) % n]), s));\n v.push_back(f(Segment(p[i], p[(i + 1) % n]), t));\n v.push_back(f(Segment(p[(i + 1) % n], p[i]), s));\n v.push_back(f(Segment(p[(i + 1) % n], p[i]), t));\n }\n\n int M = v.size();\n vector<vector< Real >> d(M, vector< Real >(M, 1e9));\n rep(i,M)rep(j,M) {\n d[i][j] = d[j][i] = abs(v[i] - v[j]) * tw;\n rep(k,n) {\n if(is_intersect_sp(Segment(p[k], p[(k + 1) % n]), v[i])\n && is_intersect_sp(Segment(p[k], p[(k + 1) % n]), v[j]))\n d[i][j] = d[j][i] = min(d[i][j], abs(v[i] - v[j]) * tg);\n }\n }\n \n rep(k,M)rep(i,M)rep(j,M) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n cout.precision(17);\n cout << d[0][1] << endl;\n }\n}", "accuracy": 1, "time_ms": 2280, "memory_kb": 9100, "score_of_the_acc": -0.7443, "final_rank": 18 }, { "submission_id": "aoj_2016_5340952", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <cmath>\n#include <queue>\nusing namespace std;\n\nconst double EPS = 1e-10;\nconst double INF = 1e12;\n#define X real()\n#define Y imag()\n#define EQ(n,m) (abs((n)-(m)) < EPS)\ntypedef complex<double> P;\ntypedef vector<P> VP;\nnamespace std{\n bool operator < (const P& a, const P& b){\n return (a.X!=b.X) ? a.X<b.X : a.Y<b.Y;\n }\n}\nbool ItsSP(P a, P b, P c){\n return (abs(a-c) +abs(b-c) -abs(b-a)) < EPS;\n}\nvoid tri_search(P a, P b, P c, P d, double tg, double tw, VP &ret){\n for(int i=0; i<2; i++){\n P v[4] = {a,b,c,d};\n if(i==0) reverse(v, v+4);\n for(int j=0; j<2; j++){\n P g = v[2+j];\n P beg=v[0], end=v[1];\n for(int rep=0; rep<50; rep++){\n P dir = end-beg;\n P mid1 = beg +dir*(1.0/3);\n P mid2 = beg +dir*(2.0/3);\n if(tg*abs(mid1-v[0]) +tw*abs(g-mid1) > tg*abs(mid2-v[0]) +tw*abs(g-mid2)){\n beg = mid1;\n }else{\n end = mid2;\n }\n }\n ret.push_back(beg);\n }\n }\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n VP v, poly;\n v.reserve(400);\n for(int i=0; i<n; i++){\n int x,y;\n cin >> x >> y;\n v.push_back(P(x,y));\n poly.push_back(P(x,y));\n }\n\n int tg,tw, sx,sy, gx,gy;\n cin >> tg >> tw;\n cin >> sx >> sy >> gx >> gy;\n P s(sx,sy), g(gx,gy);\n\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n tri_search(v[i], v[(i+1)%n], v[j], v[(j+1)%n], tg, tw, v);\n tri_search(v[(i+1)%n], v[i], v[(j+1)%n], v[j], tg, tw, v);\n }\n tri_search(v[i], v[(i+1)%n], s, s, tg, tw, v);\n tri_search(v[(i+1)%n], v[i], s, s, tg, tw, v);\n tri_search(v[i], v[(i+1)%n], g, g, tg, tw, v);\n tri_search(v[(i+1)%n], v[i], g, g, tg, tw, v);\n }\n\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n v.push_back(s);\n v.push_back(g);\n int sv = v.size();\n vector<vector<double> > adj(sv, vector<double>(sv, INF));\n for(int i=0; i<sv; i++){\n for(int j=i+1; j<sv; j++){\n bool onedge = false;\n for(int e=0; e<n; e++){\n if(ItsSP(poly[e], poly[(e+1)%n], v[i]) && ItsSP(poly[e], poly[(e+1)%n], v[j])){\n onedge = true;\n break;\n }\n }\n if(onedge){\n adj[i][j] = adj[j][i] = tg *abs(v[i]-v[j]);\n }else{\n adj[i][j] = adj[j][i] = tw *abs(v[i]-v[j]);\n }\n }\n } \n\n double ans = INF;\n priority_queue<pair<double, int> > wait;\n vector<double> mincost(sv, INF);\n wait.push(make_pair(0,sv-2));\n mincost[sv-2] = 0;\n while(!wait.empty()){\n int p = wait.top().second;\n double cost = -wait.top().first;\n wait.pop();\n if(cost > mincost[p]) continue;\n if(p == sv-1){\n ans = cost;\n break;\n }\n for(int i=0; i<sv; i++){\n double newcost = cost+adj[p][i];\n if(newcost < mincost[i]){\n wait.push(make_pair(-newcost, i));\n mincost[i] = newcost;\n }\n }\n } \n\n cout << fixed;\n cout << setprecision(12);\n cout << ans << string(1000, '0') << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 3248, "score_of_the_acc": -0.1856, "final_rank": 14 }, { "submission_id": "aoj_2016_4824034", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 12\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n \t/*\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Data{\n\n\tint p[2];\n};\n\nstruct State{\n\n\tState(int arg_node_id,double arg_sum_dist){\n\t\tnode_id = arg_node_id;\n\t\tsum_dist = arg_sum_dist;\n\t}\n\n\tint node_id;\n\tdouble sum_dist;\n};\n\nint N;\ndouble TG,TW;\ndouble min_dist[SIZE][SIZE];\nPoint start,goal;\nData line[SIZE];\n\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n//切片を求める関数\ndouble calc_add(Line line){\n\n\tdouble slope = calc_slope(line);\n\n\tif(fabs(slope-DBL_MAX) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(slope) < EPS){\n\n\t\treturn line.p[0].y;\n\n\t}else{\n\n\t\treturn line.p[0].y-slope*line.p[0].x;\n\t}\n}\n\n//線分のx座標を指定した時の座標を求める\nPoint calc_pos(Line line,double x){\n\n\tPoint ret;\n\n\tdouble slope = calc_slope(line);\n\tif(fabs(slope-DBL_MAX) < EPS){\n\n\t\tret = Point(DBL_MAX,DBL_MAX);\n\t\treturn ret;\n\t}\n\n\tdouble add = calc_add(line);\n\n\tif(fabs(slope) < EPS){\n\n\t\tret = Point(x,add);\n\n\t}else{\n\n\t\tret = Point(x,slope*x+add);\n\t}\n\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tPolygon first_P;\n\n\tdouble x,y;\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tfirst_P.push_back(Point(x,y));\n\t}\n\n\tscanf(\"%lf %lf\",&TG,&TW);\n\tscanf(\"%lf %lf\",&start.x,&start.y);\n\tscanf(\"%lf %lf\",&goal.x,&goal.y);\n\n\tPolygon P;\n\n\tbool FLG = false;\n\tfor(int i = 0; i < N; i++){\n\t\tif(first_P[i] == start){\n\t\t\tFLG = true;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif(FLG){ //startが端点\n\n\t\tP = first_P;\n\t}else{\n\n\t\tP.push_back(first_P[0]);\n\n\t\tfor(int i = 1; i <= N; i++){\n\t\t\tLine tmp_line = Line(first_P[i-1],first_P[i%N]);\n\t\t\tif(getDistanceSP(tmp_line,start) < EPS){\n\n\t\t\t\tP.push_back(start);\n\t\t\t}\n\t\t\tif(i == N)break;\n\t\t\tP.push_back(first_P[i]);\n\t\t}\n\n\t\tN++;\n\t}\n\n\tint num_line = 0;\n\tfor(int i = 0; i < N; i++){\n\n\t\tline[num_line].p[0] = i;\n\t\tline[num_line++].p[1] = (i+1)%N;\n\t}\n\n\t//starのindexを求める\n\tint start_index = -1;\n\tfor(int i = 0; i < N; i++){\n\n\t\tif(P[i] == start){\n\n\t\t\tstart_index = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(i != k){\n\t\t\t\tmin_dist[i][k] = BIG_NUM;\n\t\t\t}else{\n\t\t\t\tmin_dist[i][k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\t//隣接する点間にエッジを張る\n\tfor(int i = 0; i < num_line; i++){\n\t\tint a = line[i].p[0];\n\t\tint b = line[i].p[1];\n\n\t\tdouble tmp_dist = calc_dist(P[a],P[b])*TG;\n\n\t\tmin_dist[a][b] = min(min_dist[a][b],tmp_dist);\n\t\tmin_dist[b][a] = min(min_dist[b][a],tmp_dist);\n\t}\n\n\n\t//スタートから直後に飛び込んで,水中から入次し、かつ線分の端まで行く場合のコスト\n\tfor(int i = 0; i < num_line; i++){\n\t\tif(line[i].p[0] == start_index || line[i].p[1] == start_index)continue;\n\n\t\tLine tmp_line = Line(P[line[i].p[0]],P[line[i].p[1]]);\n\t\tPoint tmp_p = calc_Reflection_Point(tmp_line,start);\n\t\tLine work_line = Line(start,tmp_p);\n\n\t\tif(!is_Cross(tmp_line,work_line))continue;\n\n\t\tPoint cross_p = calc_Cross_Point(tmp_line,work_line);\n\n\t\tdouble A = calc_dist(start,cross_p);\n\t\tdouble x = (A*TG)/(sqrt(TW*TW-TG*TG));\n\n\t\tdouble len_0 = calc_dist(cross_p,P[line[i].p[0]]);\n\n\t\tif(x <= len_0){\n\t\t\tdouble dist_0 = sqrt(A*A+x*x)*TW+(len_0-x)*TG;\n\t\t\tmin_dist[start_index][line[i].p[0]] = min(min_dist[start_index][line[i].p[0]],dist_0);\n\t\t\tmin_dist[line[i].p[0]][start_index] = min(min_dist[line[i].p[0]][start_index],dist_0);\n\t\t}\n\n\t\tdouble len_1 = calc_dist(cross_p,P[line[i].p[1]]);\n\n\t\tif(x <= len_1){\n\t\t\tdouble dist_1 = sqrt(A*A+x*x)*TW+(len_1-x)*TG;\n\n\t\t\tmin_dist[start_index][line[i].p[1]] = min(min_dist[start_index][line[i].p[1]],dist_1);\n\t\t\tmin_dist[line[i].p[1]][start_index] = min(min_dist[line[i].p[1]][start_index],dist_1);\n\t\t}\n\t}\n\n\tdouble ans = calc_dist(start,goal)*TW; //水中を直進\n\n\tfor(int mid = 0; mid < N; mid++){\n\t\tfor(int st = 0; st < N; st++){\n\t\t\tif(fabs(min_dist[st][mid]-BIG_NUM) < EPS)continue;\n\t\t\tfor(int gl = 0; gl < N; gl++){\n\t\t\t\tif(fabs(min_dist[mid][gl]-BIG_NUM) < EPS)continue;\n\n\t\t\t\tmin_dist[st][gl] = min(min_dist[st][gl],min_dist[st][mid]+min_dist[mid][gl]);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble L,R;\n\tdouble tmp_ans;\n\tPoint work1,work2;\n\n\t//辺の途中に水中からの入次はなく、辺の途中で水中に飛び込む場合\n\tfor(int i = 0; i < num_line; i++){\n\n\t\tint a = line[i].p[0];\n\t\tint b = line[i].p[1];\n\n\t\tLine tmp_line = Line(P[a],P[b]);\n\n\t\tdouble slope = calc_slope(tmp_line);\n\t\ttmp_ans = BIG_NUM;\n\n\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tL = min(P[a].y,P[b].y);\n\t\t\t\tR = max(P[a].y,P[b].y);\n\n\t\t\t\tdouble X = P[a].x;\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = Point(X,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = Point(X,mid2);\n\n\t\t\t\t\tdouble t1 = calc_dist(P[a],work1)*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\tdouble t2 = calc_dist(P[a],work2)*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\ttmp_ans = min(tmp_ans,min_dist[start_index][a]+min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\n\t\t\t}else{ //その他\n\n\t\t\t\tL = min(P[a].x,P[b].x);\n\t\t\t\tR = max(P[a].x,P[b].x);\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = calc_pos(tmp_line,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = calc_pos(tmp_line,mid2);\n\n\t\t\t\t\tdouble t1 = calc_dist(P[a],work1)*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\tdouble t2 = calc_dist(P[a],work2)*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\ttmp_ans = min(tmp_ans,min_dist[start_index][a]+min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\t\t\t}\n\t\t\tswap(a,b);\n\t\t}\n\t}\n\n\t//水中から入次する辺と飛び込む辺が同じ場合\n\tfor(int i = 0; i < num_line; i++){\n\t\tif(line[i].p[0] == start_index || line[i].p[1] == start_index)continue;\n\n\t\tLine tmp_line = Line(P[line[i].p[0]],P[line[i].p[1]]);\n\t\tPoint tmp_p = calc_Reflection_Point(tmp_line,start);\n\t\tLine work_line = Line(start,tmp_p);\n\n\t\tif(!is_Cross(tmp_line,work_line))continue;\n\n\t\tPoint cross_p = calc_Cross_Point(tmp_line,work_line);\n\n\t\tint a = line[i].p[0];\n\t\tint b = line[i].p[1];\n\n\t\tdouble A = calc_dist(start,cross_p);\n\t\tdouble x = (A*TG)/(sqrt(TW*TW-TG*TG));\n\n\t\tdouble slope = calc_slope(tmp_line);\n\t\tdouble tmp_ans = BIG_NUM;\n\n\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tL = min(P[a].y,cross_p.y);\n\t\t\t\tR = max(P[a].y,cross_p.y);\n\n\t\t\t\tdouble X = P[a].x;\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = Point(X,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = Point(X,mid2);\n\n\t\t\t\t\tdouble t1,t2;\n\t\t\t\t\tif(calc_dist(cross_p,work1)+EPS < x){\n\n\t\t\t\t\t\tt1 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt1 = sqrt(A*A+x*x)*TW+((calc_dist(work1,cross_p)-x))*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\t}\n\t\t\t\t\tif(calc_dist(cross_p,work2)+EPS < x){\n\n\t\t\t\t\t\tt2 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt2 = sqrt(A*A+x*x)*TW+((calc_dist(work2,cross_p)-x))*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_ans = min(tmp_ans,min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\n\t\t\t}else{\n\n\t\t\t\tL = min(P[a].x,cross_p.x);\n\t\t\t\tR = max(P[a].x,cross_p.x);\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = calc_pos(tmp_line,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = calc_pos(tmp_line,mid2);\n\n\t\t\t\t\tdouble t1,t2;\n\t\t\t\t\tif(calc_dist(cross_p,work1)+EPS < x){\n\n\t\t\t\t\t\tt1 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt1 = sqrt(A*A+x*x)*TW+((calc_dist(work1,cross_p)-x))*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\t}\n\t\t\t\t\tif(calc_dist(cross_p,work2)+EPS < x){\n\n\t\t\t\t\t\tt2 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt2 = sqrt(A*A+x*x)*TW+((calc_dist(work2,cross_p)-x))*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_ans = min(tmp_ans,min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\n\t\t\t}\n\t\t\tswap(a,b);\n\t\t}\n\t}\n\n\tprintf(\"%.12lf\\n\",ans);\n\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3216, "score_of_the_acc": -0.1049, "final_rank": 9 }, { "submission_id": "aoj_2016_4824030", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 12\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n \t/*\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Data{\n\n\tint p[2];\n};\n\nstruct State{\n\n\tState(int arg_node_id,double arg_sum_dist){\n\t\tnode_id = arg_node_id;\n\t\tsum_dist = arg_sum_dist;\n\t}\n\n\tint node_id;\n\tdouble sum_dist;\n};\n\nint N;\ndouble TG,TW;\ndouble min_dist[SIZE][SIZE];\nPoint start,goal;\nData line[SIZE];\n\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n//切片を求める関数\ndouble calc_add(Line line){\n\n\tdouble slope = calc_slope(line);\n\n\tif(fabs(slope-DBL_MAX) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(slope) < EPS){\n\n\t\treturn line.p[0].y;\n\n\t}else{\n\n\t\treturn line.p[0].y-slope*line.p[0].x;\n\t}\n}\n\n//線分のx座標を指定した時の座標を求める\nPoint calc_pos(Line line,double x){\n\n\tPoint ret;\n\n\tdouble slope = calc_slope(line);\n\tif(fabs(slope-DBL_MAX) < EPS){\n\n\t\tret = Point(DBL_MAX,DBL_MAX);\n\t\treturn ret;\n\t}\n\n\tdouble add = calc_add(line);\n\n\tif(fabs(slope) < EPS){\n\n\t\tret = Point(x,add);\n\n\t}else{\n\n\t\tret = Point(x,slope*x+add);\n\t}\n\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tPolygon first_P;\n\n\tdouble x,y;\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tfirst_P.push_back(Point(x,y));\n\t}\n\n\tscanf(\"%lf %lf\",&TG,&TW);\n\tscanf(\"%lf %lf\",&start.x,&start.y);\n\tscanf(\"%lf %lf\",&goal.x,&goal.y);\n\n\tPolygon P;\n\n\tbool FLG = false;\n\tfor(int i = 0; i < N; i++){\n\t\tif(first_P[i] == start){\n\t\t\tFLG = true;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif(FLG){ //startが端点\n\n\t\t//printf(\"startは点\\n\");\n\n\t\tP = first_P;\n\t}else{\n\n\t\tP.push_back(first_P[0]);\n\n\t\tfor(int i = 1; i <= N; i++){\n\t\t\tLine tmp_line = Line(first_P[i-1],first_P[i%N]);\n\t\t\tif(getDistanceSP(tmp_line,start) < EPS){\n\n\t\t\t\tP.push_back(start);\n\t\t\t}\n\t\t\tif(i == N)break;\n\t\t\tP.push_back(first_P[i]);\n\t\t}\n\n\t\tN++;\n\t}\n\n\tint num_line = 0;\n\tfor(int i = 0; i < N; i++){\n\n\t\tline[num_line].p[0] = i;\n\t\tline[num_line++].p[1] = (i+1)%N;\n\t}\n\n\t//starのindexを求める\n\tint start_index = -1;\n\tfor(int i = 0; i < N; i++){\n\n\t\tif(P[i] == start){\n\n\t\t\tstart_index = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t//printf(\"start_index:%d\\n\",start_index);\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(i != k){\n\t\t\t\tmin_dist[i][k] = BIG_NUM;\n\t\t\t}else{\n\t\t\t\tmin_dist[i][k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\t//隣接する点間にエッジを張る\n\tfor(int i = 0; i < num_line; i++){\n\t\tint a = line[i].p[0];\n\t\tint b = line[i].p[1];\n\n\t\tdouble tmp_dist = calc_dist(P[a],P[b])*TG;\n\n\t\tmin_dist[a][b] = min(min_dist[a][b],tmp_dist);\n\t\tmin_dist[b][a] = min(min_dist[b][a],tmp_dist);\n\t}\n\n\n\t//スタートから直後に飛び込んで,水中から入次し、かつ線分の端まで行く場合のコスト\n\tfor(int i = 0; i < num_line; i++){\n\t\tif(line[i].p[0] == start_index || line[i].p[1] == start_index)continue;\n\n\t\tLine tmp_line = Line(P[line[i].p[0]],P[line[i].p[1]]);\n\t\tPoint tmp_p = calc_Reflection_Point(tmp_line,start);\n\t\tLine work_line = Line(start,tmp_p);\n\n\t\tif(!is_Cross(tmp_line,work_line))continue;\n\n\t\tPoint cross_p = calc_Cross_Point(tmp_line,work_line);\n\n\t\tdouble A = calc_dist(start,cross_p);\n\t\tdouble x = (A*TG)/(sqrt(TW*TW-TG*TG));\n\n\t\tdouble len_0 = calc_dist(cross_p,P[line[i].p[0]]);\n\n\t\tif(x <= len_0){\n\t\t\tdouble dist_0 = sqrt(A*A+x*x)*TW+(len_0-x)*TG;\n\t\t\tmin_dist[start_index][line[i].p[0]] = min(min_dist[start_index][line[i].p[0]],dist_0);\n\t\t\tmin_dist[line[i].p[0]][start_index] = min(min_dist[line[i].p[0]][start_index],dist_0);\n\t\t}\n\n\t\tdouble len_1 = calc_dist(cross_p,P[line[i].p[1]]);\n\n\t\tif(x <= len_1){\n\t\t\tdouble dist_1 = sqrt(A*A+x*x)*TW+(len_1-x)*TG;\n\n\t\t\tmin_dist[start_index][line[i].p[1]] = min(min_dist[start_index][line[i].p[1]],dist_1);\n\t\t\tmin_dist[line[i].p[1]][start_index] = min(min_dist[line[i].p[1]][start_index],dist_1);\n\t\t}\n\t}\n\n\tdouble ans = calc_dist(start,goal)*TW; //水中を直進\n\n\t//printf(\"最初のans:%.5lf\\n\",ans);\n\n\tfor(int mid = 0; mid < N; mid++){\n\t\tfor(int st = 0; st < N; st++){\n\t\t\tif(fabs(min_dist[st][mid]-BIG_NUM) < EPS)continue;\n\t\t\tfor(int gl = 0; gl < N; gl++){\n\t\t\t\tif(fabs(min_dist[mid][gl]-BIG_NUM) < EPS)continue;\n\n\t\t\t\tmin_dist[st][gl] = min(min_dist[st][gl],min_dist[st][mid]+min_dist[mid][gl]);\n\t\t\t}\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < N; i++){\n\t\tif(i == start_index){\n\n\t\t\tprintf(\"start\\n\");\n\t\t}\n\t\tprintf(\"min_dist[%d]:%.5lf\\n\",i,min_dist[start_index][i]);\n\n\t}*/\n\tdouble L,R;\n\tdouble tmp_ans;\n\tPoint work1,work2;\n\n\t//辺の途中に水中からの入次はなく、辺の途中で水中に飛び込む場合\n\tfor(int i = 0; i < num_line; i++){\n\n\t\tint a = line[i].p[0];\n\t\tint b = line[i].p[1];\n\n\t\t//printf(\"a:%d b:%d\\n\",a,b);\n\n\t\tLine tmp_line = Line(P[a],P[b]);\n\n\t\tdouble slope = calc_slope(tmp_line);\n\t\ttmp_ans = BIG_NUM;\n\n\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\t//if(min_dist[start_index][a] >= ans)continue;\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tL = min(P[a].y,P[b].y);\n\t\t\t\tR = max(P[a].y,P[b].y);\n\n\t\t\t\tdouble X = P[a].x;\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = Point(X,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = Point(X,mid2);\n\n\t\t\t\t\tdouble t1 = calc_dist(P[a],work1)*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\tdouble t2 = calc_dist(P[a],work2)*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\ttmp_ans = min(tmp_ans,min_dist[start_index][a]+min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\n\t\t\t}else{ //その他\n\n\t\t\t\tL = min(P[a].x,P[b].x);\n\t\t\t\tR = max(P[a].x,P[b].x);\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = calc_pos(tmp_line,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = calc_pos(tmp_line,mid2);\n\n\t\t\t\t\tdouble t1 = calc_dist(P[a],work1)*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\tdouble t2 = calc_dist(P[a],work2)*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\ttmp_ans = min(tmp_ans,min_dist[start_index][a]+min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\t\t\t}\n\t\t\tswap(a,b);\n\t\t}\n\t}\n\n\t//printf(\"ans2:%.12lf\\n\",ans);\n\n\t//水中から入次する辺と飛び込む辺が同じ場合\n\tfor(int i = 0; i < num_line; i++){\n\t\tif(line[i].p[0] == start_index || line[i].p[1] == start_index)continue;\n\n\t\tLine tmp_line = Line(P[line[i].p[0]],P[line[i].p[1]]);\n\t\tPoint tmp_p = calc_Reflection_Point(tmp_line,start);\n\t\tLine work_line = Line(start,tmp_p);\n\n\t\tif(!is_Cross(tmp_line,work_line))continue;\n\n\t\tPoint cross_p = calc_Cross_Point(tmp_line,work_line);\n\n\t\tint a = line[i].p[0];\n\t\tint b = line[i].p[1];\n\n\t\tdouble A = calc_dist(start,cross_p);\n\t\tdouble x = (A*TG)/(sqrt(TW*TW-TG*TG));\n\n\t\tdouble slope = calc_slope(tmp_line);\n\t\tdouble tmp_ans = BIG_NUM;\n\n\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tL = min(P[a].y,cross_p.y);\n\t\t\t\tR = max(P[a].y,cross_p.y);\n\n\t\t\t\tdouble X = P[a].x;\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = Point(X,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = Point(X,mid2);\n\n\t\t\t\t\tdouble t1,t2;\n\t\t\t\t\tif(calc_dist(cross_p,work1)+EPS < x){\n\n\t\t\t\t\t\tt1 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt1 = sqrt(A*A+x*x)*TW+((calc_dist(work1,cross_p)-x))*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\t}\n\t\t\t\t\tif(calc_dist(cross_p,work2)+EPS < x){\n\n\t\t\t\t\t\tt2 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt2 = sqrt(A*A+x*x)*TW+((calc_dist(work2,cross_p)-x))*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_ans = min(tmp_ans,min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\n\t\t\t}else{\n\n\t\t\t\tL = min(P[a].x,cross_p.x);\n\t\t\t\tR = max(P[a].x,cross_p.x);\n\n\t\t\t\tfor(int loop = 0; loop < 100; loop++){\n\n\t\t\t\t\tdouble mid1 = (2.0*L+R)/3.0;\n\t\t\t\t\twork1 = calc_pos(tmp_line,mid1);\n\n\t\t\t\t\tdouble mid2 = (1.0*L+2.0*R)/3.0;\n\t\t\t\t\twork2 = calc_pos(tmp_line,mid2);\n\n\t\t\t\t\tdouble t1,t2;\n\t\t\t\t\tif(calc_dist(cross_p,work1)+EPS < x){\n\n\t\t\t\t\t\tt1 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt1 = sqrt(A*A+x*x)*TW+((calc_dist(work1,cross_p)-x))*TG+calc_dist(work1,goal)*TW;\n\t\t\t\t\t}\n\t\t\t\t\tif(calc_dist(cross_p,work2)+EPS < x){\n\n\t\t\t\t\t\tt2 = BIG_NUM;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tt2 = sqrt(A*A+x*x)*TW+((calc_dist(work2,cross_p)-x))*TG+calc_dist(work2,goal)*TW;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_ans = min(tmp_ans,min(t1,t2));\n\n\t\t\t\t\tif(t1 <= t2){\n\n\t\t\t\t\t\tR = mid2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tL = mid1;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans = min(ans,tmp_ans);\n\n\t\t\t}\n\t\t\tswap(a,b);\n\t\t}\n\t}\n\n\tprintf(\"%.12lf\\n\",ans);\n\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3288, "score_of_the_acc": -0.1084, "final_rank": 11 }, { "submission_id": "aoj_2016_2611053", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <cmath>\n#include <queue>\nusing namespace std;\n\nconst double EPS = 1e-10;\nconst double INF = 1e12;\n#define X real()\n#define Y imag()\n#define EQ(n,m) (abs((n)-(m)) < EPS)\ntypedef complex<double> P;\ntypedef vector<P> VP;\nnamespace std{\n bool operator < (const P& a, const P& b){\n return (a.X!=b.X) ? a.X<b.X : a.Y<b.Y;\n }\n}\nbool ItsSP(P a, P b, P c){\n return (abs(a-c) +abs(b-c) -abs(b-a)) < EPS;\n}\nvoid tri_search(P a, P b, P c, P d, double tg, double tw, VP &ret){\n for(int i=0; i<2; i++){\n P v[4] = {a,b,c,d};\n if(i==0) reverse(v, v+4);\n for(int j=0; j<2; j++){\n P g = v[2+j];\n P beg=v[0], end=v[1];\n for(int rep=0; rep<50; rep++){\n P dir = end-beg;\n P mid1 = beg +dir*(1.0/3);\n P mid2 = beg +dir*(2.0/3);\n if(tg*abs(mid1-v[0]) +tw*abs(g-mid1) > tg*abs(mid2-v[0]) +tw*abs(g-mid2)){\n beg = mid1;\n }else{\n end = mid2;\n }\n }\n ret.push_back(beg);\n }\n }\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n VP v, poly;\n v.reserve(400);\n for(int i=0; i<n; i++){\n int x,y;\n cin >> x >> y;\n v.push_back(P(x,y));\n poly.push_back(P(x,y));\n }\n\n int tg,tw, sx,sy, gx,gy;\n cin >> tg >> tw;\n cin >> sx >> sy >> gx >> gy;\n P s(sx,sy), g(gx,gy);\n\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n tri_search(v[i], v[(i+1)%n], v[j], v[(j+1)%n], tg, tw, v);\n tri_search(v[(i+1)%n], v[i], v[(j+1)%n], v[j], tg, tw, v);\n }\n tri_search(v[i], v[(i+1)%n], s, s, tg, tw, v);\n tri_search(v[(i+1)%n], v[i], s, s, tg, tw, v);\n tri_search(v[i], v[(i+1)%n], g, g, tg, tw, v);\n tri_search(v[(i+1)%n], v[i], g, g, tg, tw, v);\n }\n\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n v.push_back(s);\n v.push_back(g);\n int sv = v.size();\n vector<vector<double> > adj(sv, vector<double>(sv, INF));\n for(int i=0; i<sv; i++){\n for(int j=i+1; j<sv; j++){\n bool onedge = false;\n for(int e=0; e<n; e++){\n if(ItsSP(poly[e], poly[(e+1)%n], v[i]) && ItsSP(poly[e], poly[(e+1)%n], v[j])){\n onedge = true;\n break;\n }\n }\n if(onedge){\n adj[i][j] = adj[j][i] = tg *abs(v[i]-v[j]);\n }else{\n adj[i][j] = adj[j][i] = tw *abs(v[i]-v[j]);\n }\n }\n } \n\n double ans = INF;\n priority_queue<pair<double, int> > wait;\n vector<double> mincost(sv, INF);\n wait.push(make_pair(0,sv-2));\n mincost[sv-2] = 0;\n while(!wait.empty()){\n int p = wait.top().second;\n double cost = -wait.top().first;\n wait.pop();\n if(cost > mincost[p]) continue;\n if(p == sv-1){\n ans = cost;\n break;\n }\n for(int i=0; i<sv; i++){\n double newcost = cost+adj[p][i];\n if(newcost < mincost[i]){\n wait.push(make_pair(-newcost, i));\n mincost[i] = newcost;\n }\n }\n } \n\n cout << fixed;\n cout << setprecision(12);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 3380, "score_of_the_acc": -0.192, "final_rank": 15 }, { "submission_id": "aoj_2016_2143392", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \ntypedef long double ldouble;\nconst ldouble eps = 0.000000001;\nbool eq(ldouble a,ldouble b){return (-eps<a-b && a-b<eps);}\n \nstruct Point{\n ldouble x,y;\n Point operator + (const Point p)const{\n return (Point){x+p.x,y+p.y};\n }\n Point operator - (const Point p)const{\n return (Point){x-p.x,y-p.y};\n }\n Point operator * (const ldouble r)const{\n return (Point){x*r,y*r};\n }\n};\nbool eq(Point a,Point b){return ( eq(a.x,b.x) && eq(a.y,b.y) );}\n \ntypedef Point Vector;\n \nldouble dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}\nldouble cross(Vector a,Vector b){return a.x*b.y-b.x*a.y;}\nldouble norm(Vector v){return dot(v,v);}\nldouble abs(Vector v){return sqrt(norm(v));}\n \n \nPoint project(Point a,Point b,Point p){\n Point base=b-a;\n ldouble r=dot(p-a,base)/norm(base);\n return a+base*r;\n}\n \nint n;\nldouble A,B;\nvector<Point> t;\nPoint s,g;\nldouble ans;\n \nldouble func0(double H,double W){\n ldouble a=1.0;\n ldouble b=-2.0*W;\n ldouble c=W*W-(A*A*H*H)/(B*B-A*A);\n if(b*b-4.0*a*c<0)return min(H*B+W*A, sqrt(H*H+W*W)*B);\n ldouble t = (-b+sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t>W){\n t=(-b-sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t<0)return 0;\n }\n return t;\n}\nldouble func02(Point a,Point b,Point g){\n Point D=project(a,b,g);\n return func0( abs(g-D) , abs(a-D) );\n}\nldouble func(ldouble H,ldouble W){\n ldouble t=func0(H,W);\n ldouble ff = t*A+B*sqrt((W-t)*(W-t)+H*H );\n return ff;\n} \nldouble func2(Point a,Point b,Point g){\n Point D=project(a,b,g);\n return func( abs(g-D) , abs(a-D) );\n}\n \nvoid solve(){\n vector<Point> T;\n for(int i=0;i<n;i++){\n T.push_back(t[i]);\n if(i+1<n){\n ldouble tt = func02(t[i],t[i+1],g);\n Vector base=t[i+1]-t[i]; \n Point next = t[i]+ base*( tt /abs(base) );\n if(!eq(next,t[i])&&!eq(next,t[i+1])\n && eq(abs(t[i]-next)+abs(t[i+1]-next),abs(t[i]-t[i+1]))\n )T.push_back(next);\n }\n }\n \n t=T;\n n=t.size();\n vector<ldouble> u(n); \n u[0]=0;\n u[1]=abs(t[0]-t[1])*A;\n for(int i=2;i<n;i++){\n u[i]=u[i-1]+abs(t[i]-t[i-1])*A;\n for(int j=1;j<i;j++){\n u[i]=min(u[i],u[j-1]+func2(t[j-1],t[j],t[i]));\n u[i]=min(u[i],u[j-1]+func2(t[i],t[i-1],t[j-1]));\n }\n }\n for(int i=0;i+1<n;i++){\n ldouble na = u[i]+func2(t[i],t[i+1],g);\n ans=min(ans,na);\n }\n}\n \nint main(){\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n while(1){\n vector<Point> nt;\n cin>>n;\n if(n==0)break;\n t.clear();t.resize(n);\n for(int i=0;i<n;i++)cin>>t[i].x>>t[i].y;\n cin>>A>>B>>s.x>>s.y>>g.x>>g.y;\n int flg=0,f=-1;\n for(int i=0;i<n;i++){\n int j=(i+1)%n; \n if(eq(s,t[i]))flg=1;\n if(eq( abs(s-t[i])+abs(s-t[j]) , abs(t[i]-t[j]) )){\n f=j;\n break;\n }\n }\n if(flg==0)nt.push_back(s);\n else f=(f+n-1)%n;\n for(int j=0;j<n;j++)nt.push_back(t[(f+j)%n]);\n n=nt.size();\n t=nt;\n ans=abs(s-g)*B;\n solve();\n reverse(t.begin()+1,t.end());\n solve();\n double ans2=ans;\n printf(\"%.14f\\n\",ans2);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3156, "score_of_the_acc": -0.0972, "final_rank": 4 }, { "submission_id": "aoj_2016_2122049", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-8)\n#define mod 1000000007\n#define pi acos(-1)\n#define phi (1.0+sqrt(5))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return (!equals(x,p.x) ? x-p.x<-eps : y-p.y<-eps);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nint n;\ndouble tg,tw;\nPolygon p;\nvector<Point> vp;\nvector<pid> g[MAX];\nPoint s,t;\nint S,T;\n\nvoid init(){\n p.clear();\n vp.clear();\n FOR(i,0,MAX)g[i].clear();\n}\n\nPoint getPoint(Point a,Point b,Point c){\n Vector v=b-a;\n v=v/abs(v);\n double L=0.0,R=abs(b-a);\n FOR(i,0,40){\n double m1=(L*phi+R)/(1.0+phi);\n double m2=(L+R*phi)/(1.0+phi);\n double res1=(abs(v*m1))*tg+abs(a+v*m1-c)*tw;\n double res2=(abs(v*m2))*tg+abs(a+v*m2-c)*tw;\n if(res1<res2)R=m2;\n else L=m1;\n }\n return a+v*L;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[S]=0;\n pq.push(mp(0,S));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n if(d[u.s]-u.f<-eps)continue;\n if(u.s==T)return d[u.s];\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i].f;\n double cost=d[u.s]+g[u.s][i].s;\n if(cost-d[next]<-eps){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n FOR(i,0,n){\n vp.pb(getPoint(p[i],p[(i+1)%n],s));\n vp.pb(getPoint(p[i],p[(i+1)%n],t));\n vp.pb(getPoint(p[(i+1)%n],p[i],s));\n vp.pb(getPoint(p[(i+1)%n],p[i],t));\n }\n vp.pb(s);\n FOR(i,0,n){\n Segment s(p[i],p[(i+1)%n]);\n vector<pdi> list;\n FOR(j,0,vp.size()){\n if(ccw(s.p1,s.p2,vp[j])==0)list.pb(mp(abs(s.p1-vp[j]),j));\n }\n sort(all(list));\n FOR(j,0,list.size()-1){\n int a=list[j].s,b=list[j+1].s;\n g[a].pb(mp(b,abs(vp[a]-vp[b])*tg));\n g[b].pb(mp(a,abs(vp[a]-vp[b])*tg));\n }\n }\n vp.pb(t);\n FOR(i,0,vp.size()){\n if(vp[i]==s)S=i;\n if(vp[i]==t)T=i;\n }\n FOR(i,0,vp.size())if(i!=T)g[i].pb(mp(T,abs(vp[i]-t)*tw));\n FOR(i,0,vp.size())if(i!=S)g[S].pb(mp(i,abs(vp[i]-s)*tw));\n\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n vp.pb(Point(x,y));\n }\n cin>>tg>>tw;\n cin>>s.x>>s.y>>t.x>>t.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3144, "score_of_the_acc": -0.0982, "final_rank": 5 }, { "submission_id": "aoj_2016_2121020", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-10)\n#define mod 1000000007\n#define pi acos(-1)\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return (!equals(x,p.x) ? x-p.x<-eps : y-p.y<-eps);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nint n;\ndouble tg,tw;\nPolygon p;\nvector<Point> vp;\nvector<pid> g[MAX];\nPoint s,t;\nint S,T;\n\nvoid init(){\n p.clear();\n vp.clear();\n FOR(i,0,MAX)g[i].clear();\n}\n\nPoint getPoint(Point a,Point b,Point c){\n Vector v=b-a;\n v=v/abs(v);\n double L=0.0,R=abs(b-a);\n FOR(i,0,50){\n double m1=(L+L+R)/3.0;\n double m2=(L+R+R)/3.0;\n double res1=(abs(v*m1))*tg+abs(a+v*m1-c)*tw;\n double res2=(abs(v*m2))*tg+abs(a+v*m2-c)*tw;\n if(res1-res2<-eps)R=m2;\n else L=m1;\n }\n return a+v*L;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[S]=0;\n pq.push(mp(0,S));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n if(d[u.s]-u.f<-eps)continue;\n if(u.s==T)return d[u.s];\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i].f;\n double cost=d[u.s]+g[u.s][i].s;\n if(cost-d[next]<-eps){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n FOR(i,0,n){\n vp.pb(getPoint(p[i],p[(i+1)%n],s));\n vp.pb(getPoint(p[i],p[(i+1)%n],t));\n vp.pb(getPoint(p[(i+1)%n],p[i],s));\n vp.pb(getPoint(p[(i+1)%n],p[i],t));\n }\n vp.pb(s);\n FOR(i,0,n){\n Segment s(p[i],p[(i+1)%n]);\n vector<pdi> list;\n FOR(j,0,vp.size()){\n if(ccw(s.p1,s.p2,vp[j])==0)list.pb(mp(abs(s.p1-vp[j]),j));\n }\n sort(all(list));\n FOR(j,0,list.size()-1){\n int a=list[j].s,b=list[j+1].s;\n g[a].pb(mp(b,abs(vp[a]-vp[b])*tg));\n g[b].pb(mp(a,abs(vp[a]-vp[b])*tg));\n }\n }\n vp.pb(t);\n FOR(i,0,vp.size()){\n if(vp[i]==s)S=i;\n if(vp[i]==t)T=i;\n }\n FOR(i,0,vp.size())if(i!=T)g[i].pb(mp(T,abs(vp[i]-t)*tw));\n FOR(i,0,vp.size())if(i!=S)g[S].pb(mp(i,abs(vp[i]-s)*tw));\n\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n vp.pb(Point(x,y));\n }\n cin>>tg>>tw;\n cin>>s.x>>s.y>>t.x>>t.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3192, "score_of_the_acc": -0.1021, "final_rank": 6 }, { "submission_id": "aoj_2016_2120996", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-8)\n#define mod 1000000007\n#define pi acos(-1)\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return (!equals(x,p.x) ? x-p.x<-eps : y-p.y<-eps);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nvector<Point> unique(vector<Point> vp){\n vector<Point> res;\n if(vp.empty())return res;\n sort(all(vp));\n res.pb(vp[0]);\n FOR(i,1,vp.size())if(!(res.back()==vp[i]))res.pb(vp[i]);\n return res;\n}\n\nint n;\ndouble tg,tw;\nPolygon p;\nvector<Point> vp;\nvector<pid> g[MAX];\nPoint s,t;\nint S,T;\n\nvoid init(){\n p.clear();\n vp.clear();\n FOR(i,0,MAX)g[i].clear();\n}\n\nPoint getPoint(Point a,Point b,Point c){\n Vector v=b-a;\n v=v/abs(v);\n double L=0.0,R=abs(b-a);\n FOR(i,0,50){\n double m1=(L+L+R)/3.0;\n double m2=(L+R+R)/3.0;\n double res1=(abs(v*m1))*tg+abs(a+v*m1-c)*tw;\n double res2=(abs(v*m2))*tg+abs(a+v*m2-c)*tw;\n if(res1<res2)R=m2;\n else L=m1;\n }\n return a+v*L;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[S]=0;\n pq.push(mp(0,S));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n if(d[u.s]-u.f<-eps)continue;\n if(u.s==T)return d[u.s];\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i].f;\n double cost=d[u.s]+g[u.s][i].s;\n if(cost-d[next]<-eps){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n FOR(i,0,n){\n vp.pb(getPoint(p[i],p[(i+1)%n],s));\n vp.pb(getPoint(p[i],p[(i+1)%n],t));\n vp.pb(getPoint(p[(i+1)%n],p[i],s));\n vp.pb(getPoint(p[(i+1)%n],p[i],t));\n }\n vp.pb(s);\n // vp=unique(vp);\n FOR(i,0,n){\n Segment s(p[i],p[(i+1)%n]);\n vector<pdi> list;\n FOR(j,0,vp.size()){\n if(ccw(s.p1,s.p2,vp[j])==0)list.pb(mp(abs(s.p1-vp[j]),j));\n }\n sort(all(list));\n FOR(j,0,list.size()-1){\n int a=list[j].s,b=list[j+1].s;\n g[a].pb(mp(b,abs(vp[a]-vp[b])*tg));\n g[b].pb(mp(a,abs(vp[a]-vp[b])*tg));\n }\n }\n vp.pb(t);\n FOR(i,0,vp.size()){\n if(vp[i]==s)S=i;\n if(vp[i]==t)T=i;\n }\n FOR(i,0,vp.size())if(i!=T)g[i].pb(mp(T,abs(vp[i]-t)*tw));\n FOR(i,0,vp.size())if(i!=S)g[S].pb(mp(i,abs(vp[i]-s)*tw));\n\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n vp.pb(Point(x,y));\n }\n cin>>tg>>tw;\n cin>>s.x>>s.y>>t.x>>t.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3300, "score_of_the_acc": -0.1074, "final_rank": 10 }, { "submission_id": "aoj_2016_2120979", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-8)\n#define mod 1000000007\n#define pi acos(-1)\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\n \nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nvector<Point> unique(vector<Point> vp){\n vector<Point> res;\n if(vp.empty())return res;\n res.pb(vp[0]);\n FOR(i,0,vp.size())if(!(res.back()==vp[i]))res.pb(vp[i]);\n return res;\n}\n\nint n;\ndouble tg,tw;\nPolygon p;\nvector<Point> vp;\nvector<pid> g[MAX];\nPoint s,t;\nint S,T;\n\nvoid init(){\n p.clear();\n vp.clear();\n FOR(i,0,MAX)g[i].clear();\n}\n\nPoint getPoint(Point a,Point b,Point c){\n Vector v=b-a;\n v=v/abs(v);\n double L=0.0,R=abs(b-a);\n FOR(i,0,50){\n double m1=(L+L+R)/3.0;\n double m2=(L+R+R)/3.0;\n double res1=(abs(v*m1))*tg+abs(a+v*m1-c)*tw;\n double res2=(abs(v*m2))*tg+abs(a+v*m2-c)*tw;\n if(res1<res2)R=m2;\n else L=m1;\n }\n return a+v*L;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[S]=0;\n pq.push(mp(0,S));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n if(d[u.s]-u.f<-eps)continue;\n if(u.s==T)return d[u.s];\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i].f;\n double cost=d[u.s]+g[u.s][i].s;\n if(cost-d[next]<-eps){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n FOR(i,0,n){\n vp.pb(getPoint(p[i],p[(i+1)%n],s));\n vp.pb(getPoint(p[i],p[(i+1)%n],t));\n vp.pb(getPoint(p[(i+1)%n],p[i],s));\n vp.pb(getPoint(p[(i+1)%n],p[i],t));\n }\n vp.pb(s);\n vp=unique(vp);\n FOR(i,0,n){\n Segment s(p[i],p[(i+1)%n]);\n vector<pdi> list;\n FOR(j,0,vp.size()){\n if(ccw(s.p1,s.p2,vp[j])==0)list.pb(mp(abs(s.p1-vp[j]),j));\n }\n sort(all(list));\n FOR(j,0,list.size()-1){\n int a=list[j].s,b=list[j+1].s;\n g[a].pb(mp(b,abs(vp[a]-vp[b])*tg));\n g[b].pb(mp(a,abs(vp[a]-vp[b])*tg));\n }\n }\n vp.pb(t);\n FOR(i,0,vp.size()){\n if(vp[i]==s)S=i;\n if(vp[i]==t)T=i;\n }\n FOR(i,0,vp.size())if(i!=T)g[i].pb(mp(T,abs(vp[i]-t)*tw));\n FOR(i,0,vp.size())if(i!=S)g[S].pb(mp(i,abs(vp[i]-s)*tw));\n\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n vp.pb(Point(x,y));\n }\n cin>>tg>>tw;\n cin>>s.x>>s.y>>t.x>>t.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3228, "score_of_the_acc": -0.1039, "final_rank": 7 }, { "submission_id": "aoj_2016_2120975", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-8)\n#define mod 1000000007\n#define pi acos(-1)\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\n \nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nvector<Point> unique(vector<Point> vp){\n vector<Point> res;\n if(vp.empty())return res;\n res.pb(vp[0]);\n FOR(i,0,vp.size())if(!(res.back()==vp[i]))res.pb(vp[i]);\n return res;\n}\n\nint n;\ndouble tg,tw;\nPolygon p;\nvector<Point> vp;\nvector<pid> g[MAX];\nPoint s,t;\nint S,T;\n\nvoid init(){\n p.clear();\n vp.clear();\n FOR(i,0,MAX)g[i].clear();\n}\n\nPoint getPoint(Point a,Point b,Point c){\n Vector v=b-a;\n v=v/abs(v);\n double L=0.0,R=abs(b-a);\n FOR(i,0,100){\n double m1=(L+L+R)/3.0;\n double m2=(L+R+R)/3.0;\n double res1=(abs(v*m1))*tg+abs(a+v*m1-c)*tw;\n double res2=(abs(v*m2))*tg+abs(a+v*m2-c)*tw;\n if(res1<res2)R=m2;\n else L=m1;\n }\n return a+v*L;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[S]=0;\n pq.push(mp(0,S));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n if(d[u.s]-u.f<-eps)continue;\n if(u.s==T)return d[u.s];\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i].f;\n double cost=d[u.s]+g[u.s][i].s;\n if(cost-d[next]<-eps){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n FOR(i,0,n){\n vp.pb(getPoint(p[i],p[(i+1)%n],s));\n vp.pb(getPoint(p[i],p[(i+1)%n],t));\n vp.pb(getPoint(p[(i+1)%n],p[i],s));\n vp.pb(getPoint(p[(i+1)%n],p[i],t));\n }\n vp.pb(s);\n vp=unique(vp);\n FOR(i,0,n){\n Segment s(p[i],p[(i+1)%n]);\n vector<pdi> list;\n FOR(j,0,vp.size()){\n if(ccw(s.p1,s.p2,vp[j])==0)list.pb(mp(abs(s.p1-vp[j]),j));\n }\n sort(all(list));\n FOR(j,0,list.size()-1){\n int a=list[j].s,b=list[j+1].s;\n g[a].pb(mp(b,abs(vp[a]-vp[b])*tg));\n g[b].pb(mp(a,abs(vp[a]-vp[b])*tg));\n }\n }\n vp.pb(t);\n FOR(i,0,vp.size()){\n if(vp[i]==s)S=i;\n if(vp[i]==t)T=i;\n }\n FOR(i,0,vp.size())if(i!=T)g[i].pb(mp(T,abs(vp[i]-t)*tw));\n FOR(i,0,vp.size())if(i!=S)g[S].pb(mp(i,abs(vp[i]-s)*tw));\n\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n vp.pb(Point(x,y));\n }\n cin>>tg>>tw;\n cin>>s.x>>s.y>>t.x>>t.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3148, "score_of_the_acc": -0.1047, "final_rank": 8 }, { "submission_id": "aoj_2016_1779670", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\n#include <queue>\n\nusing namespace std;\n\ninline long double add(long double a, long double b){\n return abs(a+b)<(1e-11)*(abs(a)+abs(b)) ? 0.0 : a+b;\n}\n\nstruct vec{\n long double x,y;\n vec operator-(vec b){\n return (vec){add(x,-b.x),add(y,-b.y)};\n }\n vec operator+(vec b){\n return (vec){add(x,b.x),add(y,b.y)};\n }\n vec operator*(long double d){\n return (vec){x*d,y*d};\n }\n bool operator==(vec b){\n return x==b.x&&y==b.y;\n }\n bool operator!=(vec b){\n return x!=b.x||y!=b.y;\n }\n long double dot(vec v){\n return add(x*v.x,y*v.y);\n }\n long double cross(vec v){\n return add(x*v.y,-y*v.x);\n }\n long double norm(){\n return sqrt(x*x+y*y);\n }\n};\n\nint ccw(vec& a, vec& b, vec& c){\n vec ab = b-a, ac = c-a;\n long double o = ab.cross(ac);\n if(o>0) return 1; //CCW\n if(o<0) return -1; //CW\n if(ab.dot(ac)<0){\n return 2; //C-A-B\n }else{\n if(ab.dot(ab)<ac.dot(ac)){\n return -2; //A-B-C\n }else{\n return 0; //A-C-B\n }\n }\n}\n\nvoid print_path(vector<pair<int,vec>>&P);\nlong double t_sum(vector<pair<int,vec>>&P, long double tg, long double tw);\n\nvec ternarysearch(vec& p0, vec& p1, vec& p2, long double tg, long double tw){\n //???p0??????p2??????????????????p2p3?????????????????£?????????????????§????????????????????????????????????\n long double left=1.0/3.0,right=2.0/3.0;\n long double t,t1,t2;\n vec P1,P2;\n while(true){\n P1=p1*(1-left)+p2*left;\n P2=p1*(1-right)+p2*right;\n t1=(p0-P1).norm()*tw+(p2-P1).norm()*tg;\n t2=(p0-P2).norm()*tw+(p2-P2).norm()*tg;\n if(abs(t1-t2)<1e-11){\n t=(left+right)/2;\n if(1.0-t<1e-8){\n return p2;\n }else if(t<1e-8){\n return p1;\n }else{\n return P1;\n }\n }\n if(t1<t2){\n right=(2*left+right)/3;\n left=2*left-right;\n }else{\n left=(2*right+left)/3;\n right=2*right-left;\n }\n }\n}\n\nstruct Segment{\n int begin,end;\n vector<int> P;\n Segment(int a, int b){\n begin=a, end=b;\n P.push_back(a);\n P.push_back(b);\n }\n};\n\nstruct Edge{\n int to;\n long double t;\n Edge(int a, long double b){\n to=a, t=b;\n }\n};\n\nstruct Point{\n vec v;\n vector<Edge> E;\n Point(vec v){\n this->v=v;\n }\n};\n\nlong double solve(vector<vec>& v, vec& S, vec& T, long double tg, long double tw){\n int n = v.size();\n vector<Point> V;\n for(int i=0;i<n;i++){\n if((v[i]-S).norm()<1e-8){//S?????????????????????\n V.push_back(Point(S));\n for(int j=i+1;j!=i;j=(j+1)%n){\n V.push_back(Point(v[j]));\n }\n break;\n }else if(!ccw(v[i],v[(i+1)%n],S)){//S??????????????????\n V.push_back(Point(S));\n i=(i+1)%n;\n for(int j=0;j<n;j++){\n V.push_back(Point(v[(i+j)%n]));\n }\n break;\n }\n }\n n=V.size();\n vector<Segment> L;\n for(int i=0;i<V.size();i++){\n L.push_back(Segment(i,(i+1)%n));\n }\n V.push_back(T);\n n=V.size();\n int iT=V.size()-1;\n for(int i=0;i<n;i++){\n for(auto& s:L){\n if(s.begin==i||s.end==i) continue;\n vec M;\n M=ternarysearch(V[i].v,V[s.begin].v,V[s.end].v,tg,tw);\n if(M!=V[s.begin].v&&M!=V[s.end].v){\n V.push_back(M);\n V.back().E.push_back(Edge(i,(V[i].v-M).norm()*tw));\n V[i].E.push_back(Edge(V.size()-1,(V[i].v-M).norm()*tw));\n s.P.push_back(V.size()-1);\n }\n M=ternarysearch(V[i].v,V[s.end].v,V[s.begin].v,tg,tw);\n if(M!=V[s.begin].v&&M!=V[s.end].v){\n V.push_back(M);\n V.back().E.push_back(Edge(i,(V[i].v-M).norm()*tw));\n V[i].E.push_back(Edge(V.size()-1,(V[i].v-M).norm()*tw));\n s.P.push_back(V.size()-1);\n }\n }\n }\n //????§???¢?????????+S+T????£?????????¶\n for(int i=0;i<=iT;i++){\n for(int j=i+2;j<=iT;j++){\n V[i].E.push_back(Edge(j,(V[i].v-V[j].v).norm()*tw));\n V[j].E.push_back(Edge(i,(V[i].v-V[j].v).norm()*tw));\n }\n }\n\n //????§???¢????????????????????????????????¶???\n for(auto& s:L){\n for(auto& p1:s.P){\n for(auto& p2:s.P){\n if(p1!=p2){\n V[p1].E.push_back(Edge(p2,(V[p1].v-V[p2].v).norm()*tg));\n V[p2].E.push_back(Edge(p1,(V[p1].v-V[p2].v).norm()*tg));\n }\n }\n }\n }\n //?§???????0????????????iT?????????????????????\n typedef pair<long double, int> Q;\n priority_queue<Q, vector<Q>, greater<Q>> que;\n long double minimum_time[V.size()];\n fill(minimum_time,minimum_time+V.size(),1e10);\n int s=0;\n minimum_time[s]=0;\n que.push(Q(0,s));\n while(!que.empty()){\n Q q=que.top();\n que.pop();\n int i = q.second;\n if(i==iT) return minimum_time[i];\n if(minimum_time[i]<q.first) continue;\n for(auto& e:V[i].E){\n if(minimum_time[e.to]>minimum_time[i]+e.t){\n minimum_time[e.to]=minimum_time[i]+e.t;\n que.push(Q(minimum_time[e.to],e.to));\n }\n }\n }\n return 0;\n}\n\nint main(){\n int n;\n long double tg,tw;\n vec S,T;\n vector<vec> V;\n\n while(cin >> n, n!=0){\n V.clear();\n for(int i=0;i<n;i++){\n long double x,y;\n cin >> x >> y;\n V.push_back((vec){.x=x,.y=y});\n }\n cin >> tg >> tw;\n cin >> S.x >> S.y;\n cin >> T.x >> T.y;\n\n cout.precision(8);\n cout << fixed;\n cout << solve(V,S,T,tg,tw) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3204, "score_of_the_acc": -0.1454, "final_rank": 13 }, { "submission_id": "aoj_2016_1630949", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <complex>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef complex<double> P;\n \n#define REP(i,n) for(ll i=0;i<(n);++i)\n#define DEBUG(x) cout<<#x<<\": \"<<(x)<<endl\n \n#define N_MAX 10\nint n;\nP ps[N_MAX+1];\nint tg,tw;\nP start,goal;\ndouble pslen[N_MAX+1];\ndouble pslensum[N_MAX*2+1];\n\nint pai,pbi,sti;\nP pa,pb;\n\ndouble minestval = 1e18;\nint minestpai = -1,minestpbi = -1;\nbool hack = false;\n \ndouble calc(){\n double result = 0.0;\n if(pai != sti){\n result += abs(pa-start)*tw;\n }\n if(pai == sti){\n if(sti != pbi){\n double tmp = 0.0;\n if(sti<pbi)\n tmp += (pslensum[pbi-1] - pslensum[sti])*tg;\n else\n tmp += (pslensum[pbi+n-1] - pslensum[sti])*tg;\n tmp += abs(ps[sti+1]-start)*tg;\n tmp += abs(pb-ps[pbi])*tg;\n\n double tmp2 = 0.0;\n if(sti>pbi)\n tmp2 += (pslensum[sti-1] - pslensum[pbi])*tg;\n else\n tmp2 += (pslensum[sti+n-1] - pslensum[pbi])*tg;\n tmp2 += abs(start-ps[sti])*tg;\n tmp2 += abs(ps[pbi+1]-pb)*tg;\n\n result += min(tmp,tmp2);\n }else{\n result += abs(pb-start)*tg;\n }\n }else{\n if(pai != pbi){\n double tmp = 0.0;\n if(pai<pbi)\n tmp += (pslensum[pbi-1] - pslensum[pai])*tg;\n else\n tmp += (pslensum[pbi+n-1] - pslensum[pai])*tg;\n tmp += abs(ps[pai+1]-pa)*tg;\n tmp += abs(pb-ps[pbi])*tg;\n\n double tmp2 = 0.0;\n if(pai>pbi)\n tmp2 += (pslensum[pai-1] - pslensum[pbi])*tg;\n else\n tmp2 += (pslensum[pai+n-1] - pslensum[pbi])*tg;\n tmp2 += abs(pa-ps[pai])*tg;\n tmp2 += abs(ps[pbi+1]-pb)*tg;\n\n result += min(tmp,tmp2);\n }else{\n result += abs(pb-pa)*tg;\n }\n }\n result += abs(goal-pb)*tw;\n if(!hack && result < minestval){\n minestval = result;\n minestpai = pai;\n minestpbi = pbi;\n }\n return result;\n}\n\nint T = 10;\n\ndouble solve2(){\n double result = 1e18;\n REP(i,n){\n if(hack && i!=minestpbi) continue;\n pbi = i;\n double left = 0.0;\n double right = 1.0;\n REP(_,T){\n double midl = (left*2.0+right)/3.0;\n pb = ps[i] + (ps[i+1]-ps[i])*midl;\n double resl = calc();\n double midr = (left+right*2.0)/3.0;\n pb = ps[i] + (ps[i+1]-ps[i])*midr;\n double resr = calc();\n if(resl<resr)right = midr;\n else left = midl;\n }\n pb = ps[i]+(ps[i+1]-ps[i])*left;\n result = min(result,calc());\n }\n return result;\n}\n \ndouble solve(){\n // determine PointA\n double result = 1e18;\n REP(i,n){\n if(hack && i!=minestpai) continue;\n // edge from ps[i] to ps[i+1]\n pai = i;\n double left = 0.0;\n double right = 1.0;\n REP(_,T){\n double midl = (left*2.0+right)/3.0;\n pa = ps[i] + (ps[i+1]-ps[i])*midl;\n double resl = solve2();\n double midr = (left+right*2.0)/3.0;\n pa = ps[i] + (ps[i+1]-ps[i])*midr;\n double resr = solve2();\n if(resl<resr)right = midr;\n else left = midl;\n }\n pa = ps[i]+(ps[i+1]-ps[i])*left;\n result = min(result,solve2());\n }\n return result;\n}\n \nint main(){\n cout.precision(20);\n while(true){\n // input\n cin>>n;\n if(!n)break;\n int x,y;\n REP(i,n){\n cin>>x>>y;\n ps[i] = P(x,y);\n }\n ps[n] = ps[0];\n cin>>tg>>tw;\n cin>>x>>y;\n start = P(x,y);\n cin>>x>>y;\n goal = P(x,y);\n \n REP(i,n) pslen[i] = abs(ps[i+1]-ps[i]);\n REP(i,2*n+1){\n pslensum[i] = pslen[i%n];\n if(i!=0) pslensum[i] += pslensum[i-1];\n }\n REP(i,n){\n if( abs((conj(ps[i+1]-ps[i])*(start-ps[i])).imag()) < 1e-9 ){\n sti = i;\n break;\n }\n }\n minestval = 1e18;\n T = 20;\n hack = false;\n solve();\n T = 100;\n hack = true;\n double result = solve();\n cout<<fixed<<result<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6330, "memory_kb": 1260, "score_of_the_acc": -1.0033, "final_rank": 20 }, { "submission_id": "aoj_2016_1628837", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\n#include<vector>\n#include<queue>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\nint iSP(Pt a, Pt b, Pt c) {\n\tint s = sig((b - a).det(c - a));\n\tif (s) return s;\n\tif (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b\n\tif (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c\n\treturn 0;\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\nPt hLP(Pt a,Pt b,Pt c){\n\treturn pLL(a,b,c,c+(b-a)*Pt(0,1));\n}\n//inside: +1 on: 0 outside: -1\nint sGP(int n, Pt p[], Pt a) {\n\tint side = -1, i;\n\tp[n] = p[0];\n\tfor (i = 0; i < n; ++i) {\n\t\tPt p0 = p[i] - a, p1 = p[i + 1] - a;\n\t\tif (sig(p0.det(p1)) == 0 && sig(p0.dot(p1)) <= 0) return 0;\n\t\tif (p0.y > p1.y) swap(p0, p1);\n\t\tif (sig(p0.y) <= 0 && 0 < sig(p1.y) && sig(p0.det(p1)) > 0) side = -side;\n\t}\n\treturn side;\n}\n\nPt p[20];\nPt h[20];\ndouble ijk[1100];\nint v[1100];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tdouble tg,tw;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble X,Y;\n\t\t\tscanf(\"%lf%lf\",&X,&Y);\n\t\t\tp[i]=Pt(X,Y);\n\t\t}\n\t\tp[a]=p[0];\n\t\tscanf(\"%lf%lf\",&tg,&tw);\n\t\tPt s,t;\n\t\tdouble X,Y;\n\t\tscanf(\"%lf%lf\",&X,&Y);\n\t\ts=Pt(X,Y);\n\t\tscanf(\"%lf%lf\",&X,&Y);\n\t\tt=Pt(X,Y);\n\t\tdouble th=asin(tg/tw);\n\t\tfor(int i=0;i<a;i++){\n\t\t\th[i]=hLP(p[i],p[i+1],t);\n\t\t}\n\t\tvector<Pt> hb;\n\t\thb.push_back(s);\n\t\thb.push_back(t);\n\t\tfor(int i=0;i<a;i++){\n\t\t\thb.push_back(p[i]);\n\t\t}\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j||i==j+1)continue;\n\t\t\t\tPt tmp=hLP(p[j],p[j+1],p[i]);\n\t\t\t\tdouble dist=(tmp-p[i]).ABS();\n\t\t\t\tdouble len=dist*tan(th);\n\t\t\t\tPt vec=p[j+1]-p[j];\n\t\t\t\tvec=vec/(vec.ABS())*len;\n\t\t\t\tPt t1=tmp+vec;\n\t\t\t\tPt t2=tmp-vec;\n\t\t\t\tif(iSP(p[j],t1,p[j+1])==2)hb.push_back(t1);\n\t\t\t\tif(iSP(p[j],t2,p[j+1])==2)hb.push_back(t2);\n\t\t\t}\n\t\t}\n\t\tfor(int j=0;j<a;j++){\n\t\t\tPt tmp=hLP(p[j],p[j+1],t);\n\t\t\tdouble dist=(tmp-t).ABS();\n\t\t\tdouble len=dist*tan(th);\n\t\t\tPt vec=p[j+1]-p[j];\n\t\t\tvec=vec/(vec.ABS())*len;\n\t\t\tPt t1=tmp+vec;\n\t\t\tPt t2=tmp-vec;\n\t\t\tif(iSP(p[j],t1,p[j+1])==2)hb.push_back(t1);\n\t\t\tif(iSP(p[j],t2,p[j+1])==2)hb.push_back(t2);\n\t\t}\n\t\tfor(int j=0;j<a;j++){\n\t\t\tPt tmp=hLP(p[j],p[j+1],s);\n\t\t\tdouble dist=(tmp-s).ABS();\n\t\t\tdouble len=dist*tan(th);\n\t\t\tPt vec=p[j+1]-p[j];\n\t\t\tvec=vec/(vec.ABS())*len;\n\t\t\tPt t1=tmp+vec;\n\t\t\tPt t2=tmp-vec;\n\t\t\tif(iSP(p[j],t1,p[j+1])==2)hb.push_back(t1);\n\t\t\tif(iSP(p[j],t2,p[j+1])==2)hb.push_back(t2);\n\t\t}\n\t\tint sz=hb.size();\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tv[i]=0;\n\t\t\tijk[i]=999999999;\n\t\t}\n\t\tijk[0]=0;\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tint at=0;\n\t\t\tdouble tmp=999999999;\n\t\t\tfor(int j=0;j<sz;j++){\n\t\t\t\tif(!v[j]&&tmp>ijk[j]){\n\t\t\t\t\tat=j;tmp=ijk[j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tv[at]=1;\n\t\t\tfor(int j=0;j<sz;j++){\n\t\t\t\tif(v[j])continue;\n\t\t\t\tPt M=(hb[at]+hb[j])/2;\n\t\t\t\tdouble toc=ijk[at];\n\t\t\t\tif(sGP(a,p,M)==0)toc+=(hb[at]-hb[j]).ABS()*tg;\n\t\t\t\telse toc+=(hb[at]-hb[j]).ABS()*tw;\n\t\t\t\tijk[j]=min(ijk[j],toc);\n\t\t\t}\n\t\t}\n\t\t//for(int i=0;i<sz;i++)printf(\"(%f %f): %f\\n\",hb[i].x,hb[i].y,ijk[i]);\n\t\tprintf(\"%.12f\\n\",ijk[1]);\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1192, "score_of_the_acc": -0.0032, "final_rank": 1 }, { "submission_id": "aoj_2016_1628573", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \ntypedef long double ldouble;\nconst ldouble eps = 0.000000001;\nbool eq(ldouble a,ldouble b){return (-eps<a-b && a-b<eps);}\n\nstruct Point{\n ldouble x,y;\n Point operator + (const Point p)const{\n return (Point){x+p.x,y+p.y};\n }\n Point operator - (const Point p)const{\n return (Point){x-p.x,y-p.y};\n }\n Point operator * (const ldouble r)const{\n return (Point){x*r,y*r};\n }\n};\nbool eq(Point a,Point b){return ( eq(a.x,b.x) && eq(a.y,b.y) );}\n \ntypedef Point Vector;\n\nldouble dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}\nldouble cross(Vector a,Vector b){return a.x*b.y-b.x*a.y;}\nldouble norm(Vector v){return dot(v,v);}\nldouble abs(Vector v){return sqrt(norm(v));}\n \n \nPoint project(Point a,Point b,Point p){\n Point base=b-a;\n ldouble r=dot(p-a,base)/norm(base);\n return a+base*r;\n}\n\nint n;\nldouble A,B;\nvector<Point> t;\nPoint s,g;\nldouble ans;\n \nldouble func0(double H,double W){\n ldouble a=1.0;\n ldouble b=-2.0*W;\n ldouble c=W*W-(A*A*H*H)/(B*B-A*A);\n if(b*b-4.0*a*c<0)return min(H*B+W*A, sqrt(H*H+W*W)*B);\n ldouble t = (-b+sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t>W){\n t=(-b-sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t<0)return 0;\n }\n return t;\n}\nldouble func02(Point a,Point b,Point g){\n Point D=project(a,b,g);\n return func0( abs(g-D) , abs(a-D) );\n}\nldouble func(ldouble H,ldouble W){\n ldouble t=func0(H,W);\n ldouble ff = t*A+B*sqrt((W-t)*(W-t)+H*H );\n return ff;\n} \nldouble func2(Point a,Point b,Point g){\n Point D=project(a,b,g);\n return func( abs(g-D) , abs(a-D) );\n}\n \nvoid solve(){\n vector<Point> T;\n for(int i=0;i<n;i++){\n T.push_back(t[i]);\n if(i+1<n){\n ldouble tt = func02(t[i],t[i+1],g);\n Vector base=t[i+1]-t[i]; \n Point next = t[i]+ base*( tt /abs(base) );\n if(!eq(next,t[i])&&!eq(next,t[i+1])\n && eq(abs(t[i]-next)+abs(t[i+1]-next),abs(t[i]-t[i+1]))\n )T.push_back(next);\n }\n }\n \n t=T;\n n=t.size();\n vector<ldouble> u(n); \n u[0]=0;\n u[1]=abs(t[0]-t[1])*A;\n for(int i=2;i<n;i++){\n u[i]=u[i-1]+abs(t[i]-t[i-1])*A;\n for(int j=1;j<i;j++){\n u[i]=min(u[i],u[j-1]+func2(t[j-1],t[j],t[i]));\n u[i]=min(u[i],u[j-1]+func2(t[i],t[i-1],t[j-1]));\n }\n }\n for(int i=0;i+1<n;i++){\n ldouble na = u[i]+func2(t[i],t[i+1],g);\n ans=min(ans,na);\n }\n}\n \nint main(){\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n while(1){\n vector<Point> nt;\n cin>>n;\n if(n==0)break;\n t.clear();t.resize(n);\n for(int i=0;i<n;i++)cin>>t[i].x>>t[i].y;\n cin>>A>>B>>s.x>>s.y>>g.x>>g.y;\n int flg=0,f=-1;\n for(int i=0;i<n;i++){\n int j=(i+1)%n; \n if(eq(s,t[i]))flg=1;\n if(eq( abs(s-t[i])+abs(s-t[j]) , abs(t[i]-t[j]) )){\n f=j;\n break;\n }\n }\n if(flg==0)nt.push_back(s);\n else f=(f+n-1)%n;\n for(int j=0;j<n;j++)nt.push_back(t[(f+j)%n]);\n n=nt.size();\n t=nt;\n ans=abs(s-g)*B;\n solve();\n reverse(t.begin()+1,t.end());\n solve();\n double ans2=ans;\n printf(\"%.14f\\n\",ans2);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1312, "score_of_the_acc": -0.0106, "final_rank": 3 }, { "submission_id": "aoj_2016_1628564", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long double ldouble;\n\nconst ldouble eps = 0.000000001;\n\nbool eq(ldouble a,ldouble b){\n return (-eps<a-b && a-b<eps);\n}\n\n\nstruct Point{\n ldouble x,y;\n Point operator + (const Point p)const{\n return (Point){x+p.x,y+p.y};\n }\n Point operator - (const Point p)const{\n return (Point){x-p.x,y-p.y};\n }\n Point operator * (const ldouble r)const{\n return (Point){x*r,y*r};\n }\n};\nbool eq(Point a,Point b){\n return ( eq(a.x,b.x) && eq(a.y,b.y) );\n}\n\ntypedef Point Vector;\n\nldouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\nldouble cross(Vector a,Vector b){\n return a.x*b.y-b.x*a.y;\n}\nldouble norm(Vector v){\n return dot(v,v);\n}\n\nldouble abs(Vector v){\n return sqrt(norm(v));\n}\n\n\nPoint project(Point a,Point b,Point p){\n Point base=b-a;\n ldouble r=dot(p-a,base)/norm(base);\n return a+base*r;\n}\n\nldouble calc(ldouble a,ldouble b,ldouble c){\n return (-b + sqrt(b*b-4.0*a*c))/ (2.0*a);\n}\n\n\n\nint n;\nldouble A,B;\nvector<Point> t;\nPoint s,g;\nldouble ans;\n\nldouble func0(double H,double W){\n ldouble a=1.0;\n ldouble b=-2.0*W;\n ldouble c=W*W-(A*A*H*H)/(B*B-A*A);\n if(b*b-4.0*a*c<0)return min(H*B+W*A, sqrt(H*H+W*W)*B);\n ldouble t = (-b+sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t>W){\n t=(-b-sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t<0)return min(H*B+W*A, sqrt(H*H+W*W)*B);\n }\n return t;\n}\nldouble func02(Point a,Point b,Point g){\n Point D=project(a,b,g);\n return func0( abs(g-D) , abs(a-D) );\n}\n\nldouble func(ldouble H,ldouble W){\n ldouble a=1.0;\n ldouble b=-2.0*W;\n ldouble c=W*W-(A*A*H*H)/(B*B-A*A);\n if(b*b-4.0*a*c<0)return min(H*B+W*A, sqrt(H*H+W*W)*B);\n ldouble t = (-b+sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t>W){\n t=(-b-sqrt(b*b-4.0*a*c))/(2.0*a);\n if(t<0)return min(H*B+W*A, sqrt(H*H+W*W)*B);\n }\n ldouble ff = t*A+B*sqrt((W-t)*(W-t)+H*H );\n return ff;\n}\n\nldouble func2(Point a,Point b,Point g){\n Point D=project(a,b,g);\n return func( abs(g-D) , abs(a-D) );\n}\n\nvoid solve(){\n\n\n vector<Point> T;\n\n for(int i=0;i<n;i++){\n T.push_back(t[i]);\n if(i+1<n){\n ldouble tt = func02(t[i],t[i+1],g);\n Vector base=t[i+1]-t[i]; \n Point next = t[i]+ base*( tt /abs(base) );\n if(!eq(next,t[i])&&!eq(next,t[i+1])\n && eq(abs(t[i]-next)+abs(t[i+1]-next),abs(t[i]-t[i+1]))\n )T.push_back(next);\n }\n }\n \n t=T;\n n=t.size();\n vector<ldouble> u(n); \n u[0]=0;\n u[1]=abs(t[0]-t[1])*A;\n\n for(int i=2;i<n;i++){\n u[i]=u[i-1]+abs(t[i]-t[i-1])*A;\n for(int j=1;j<i;j++){\n u[i]=min(u[i],u[j-1]+func2(t[j-1],t[j],t[i]));\n u[i]=min(u[i],u[j-1]+func2(t[i],t[i-1],t[j-1]));\n }\n }\n for(int i=0;i+1<n;i++){\n /*\n cout<<i<<endl;\n cout<<(int)t[i].x<<' '<<(int)t[i].y<<endl;\n cout<<(int)t[i+1].x<<' '<<(int)t[i+1].y<<endl;\n cout<<(double)u[i]<<' '<<(double)u[i+1]<<endl; \n cout<<(double)abs(t[i+1]-t[i])*(double)A<<endl;\n cout<<endl;\n */\n ldouble na = u[i]+func2(t[i],t[i+1],g);\n ans=min(ans,na);\n }\n\n}\n\nint main(){ \n while(1){\n vector<Point> nt;\n cin>>n;\n if(n==0)break;\n t.clear();t.resize(n);\n for(int i=0;i<n;i++)cin>>t[i].x>>t[i].y;\n cin>>A>>B>>s.x>>s.y>>g.x>>g.y;\n\n int flg=0,f=-1;\n for(int i=0;i<n;i++){\n int j=(i+1)%n; \n if(eq(s,t[i]))flg=1;\n if(eq( abs(s-t[i])+abs(s-t[j]) , abs(t[i]-t[j]) )){\n f=j;\n break;\n }\n }\n \n if(flg==0)nt.push_back(s);\n else f=(f+n-1)%n;\n for(int j=0;j<n;j++)nt.push_back(t[(f+j)%n]);\n n=nt.size();\n t=nt;\n ans=abs(s-g)*B;\n solve();\n reverse(t.begin()+1,t.end());\n solve();\n double ans2=ans;\n printf(\"%.14f\\n\",ans2);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1288, "score_of_the_acc": -0.0094, "final_rank": 2 } ]
aoj_2019_cpp
Princess' Marriage お姫様の嫁入り English text is not available in this practice contest. ある貧乏な国のおてんばで勇敢なお姫様は,ギャンブルの配当がパリミュチュエル方式で決定される事を知ることにより,ギャンブルについて詳しくなった気がしてギャンブルでの勝利を確信した.その結果,今までよりも更に多額のお金をつぎ込み,国民が納めた税金をすべて失うほどの負けを喫してしまった.この事態を重く受け止めた王様は,お姫様を隣の国へ嫁がせることにした.こうすることにより,お姫様を日頃の行いを反省させ,同時に隣国との交友を深めて財政援助をしてもらおうと考えたからである. お姫様と隣国の王子様はお互いの事が気に入り,また両国の王様間でも政略結婚に関する同意がなされた.お姫様はなけなしのお金を手に意気揚々と隣の国へ出かけた.一方で,お姫様が嫁ぐ動機は王様の一方的な利益追及のためであり,快くないと考える隣国の王子様の側近は,お姫様を亡き者にするために道の途中に無数の刺客達を放った. お姫様が通る道はすでに決められている.お姫様の通る道には合計 L 個の宿場がある.便宜上,出発地点および到着地点も宿場とし,各宿場を S 1 , S 2 , ... S L と呼ぶことにする.お姫様は最初に S 1 におり,昇順に ( S 2 , S 3 ... という順番で) 宿場を訪れて,最終的に S L へ行くものとする.宿場では金銭を払って護衛を雇うことができ,お金がある限り好きな距離だけ契約してお姫様を守らせることができる.護衛を雇う費用は,距離1につき金1である.お姫様は通る区間内を部分的に守ってもらうことも出来ることに注意せよ. S i と S i+1 間の距離は D i , S i と S i+1 間で距離1につき刺客に襲われる回数の期待値は P i で与えられている. お姫様が予算 M を持っていて,刺客に襲われる回数の期待値が最小になるように護衛を雇ったときの,目的地までに刺客に襲われる回数の期待値を求めよ. Input 入力は複数のデータセットからなる.各データセットは以下のような形式をとる. N M D 1 P 1 D 2 P 2 ... D N P N 各データセットの最初の行には二つの整数が与えられ,それぞれ区間の数 N (1≦ N ≦10,000) と,お姫差が持つ予算 M (0≦ M ≦1,000,000,000) を表す.次の N 行はお姫様が通る道の情報を表す.各行は二つの整数が含まれており, i 行目は区間の距離 D i (1≦ D i ≦10,000) とその間を1単位距離移動したときに襲われる回数の期待値 P i (0≦ P i <=10) からなる. 入力の終端は N =0, M =0 となるデータセットであらわされる.このデータセットに対しては計算結果を出力してはならない. Output 各データセット毎に,お姫様が目的地までに刺客に襲われる回数の期待値を出力せよ. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input 5 140
[ { "submission_id": "aoj_2019_10731946", "code_snippet": "//問題:https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015&lang=jp\n//判定:AC\n\n//学習コメント:\n//辺の長さが同じ正方形を同時に数える処理に少し手間取った。\n\n#include <iostream> \n#include <vector> \n#include <algorithm> \n#include <utility>\nusing namespace std;\n\nint Prin(int n,int m){\n vector< pair<int,int> >A(n);\n int d,p;\n int ex=0;\n for(int i=0; i<n; i++){\n cin>>d>>p;\n ex+=d*p;\n A.at(i)=make_pair(p,d);\n }\n sort(A.begin(), A.end());\n reverse(A.begin(), A.end());\n int t=0;\n while(m>0){\n if(m>A.at(t).second){\n m-=A.at(t).second;\n ex-=(A.at(t).first*A.at(t).second);\n t++;\n }\n else{\n ex-=(A.at(t).first*m);\n m=0;\n }\n if(t==n){\n break;\n }\n }\n return ex;\n}\n\nint main(){\n int n,m;\n while(cin>>n>>m && n!=0){\n cout<<Prin(n,m)<<endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3268, "score_of_the_acc": -0.3083, "final_rank": 6 }, { "submission_id": "aoj_2019_10724403", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N,M;\npair<int,int> PD[10010];\n\nint main(){\n while(cin >> N >> M && N) {\n int d,p;\n for (int i = 0; i < N; ++i) {\n cin >> d >> p;\n PD[i] = make_pair(p,d);\n }\n sort(PD, PD + N, greater<pair<int,int>>());\n\n int S = 0;\n for (int i = 0; i < N; ++i) {\n S += PD[i].first * PD[i].second;\n }\n for (int i = 0; i < N; ++i) {\n if (M <= 0) break;\n int guarded = min(M, PD[i].second);\n S -= guarded * PD[i].first;\n M -= guarded;\n }\n cout << S << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3420, "score_of_the_acc": -0.5062, "final_rank": 10 }, { "submission_id": "aoj_2019_10612941", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return false;\n vector<int> D(N), P(N);\n long long ans = 0;\n for (int i = 0; i < N; i++) {\n cin >> D[i] >> P[i];\n ans += D[i] * P[i];\n }\n vector<int> ord(N);\n iota(ord.begin(), ord.end(), 0);\n sort(ord.begin(), ord.end(), [&] (int i, int j) {\n return P[i] > P[j];\n });\n for (int i : ord) {\n int d = min(D[i], M);\n D[i] -= d, M -= d;\n ans -= d * P[i];\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.6927, "final_rank": 15 }, { "submission_id": "aoj_2019_10611804", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n, m;\nvector<a2> v;\nvoid input() {\n cin >> n >> m;\n v.resize(n);\n for(auto &x : v)\n cin >> x[1] >> x[0];\n}\n\nvoid solve() {\n ll ans = 0;\n for(auto &x : v)\n ans += x[0] * x[1];\n sort(v.rbegin(), v.rend());\n\n for(int i = 0; i < n && m; i++) {\n ll sub = min(m, v[i][1]);\n ans -= v[i][0] * sub;\n m -= sub;\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3196, "score_of_the_acc": -0.1875, "final_rank": 2 }, { "submission_id": "aoj_2019_10606116", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < n; ++i)\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\nint solve() {\n ll n, m;\n cin >> n >> m;\n\tif(n == 0 && m == 0){return 1;}\n vector<ll> d(n),p(n);\n vector<pair<ll,ll>> d_p(n);\n rep(i, n){\n cin >> d[i] >> p[i];\n d_p[i].first = p[i];\n d_p[i].second = d[i];\n }\n sort(d_p.rbegin(),d_p.rend());\n ll ans = 0;\n rep(i, n){\n if(d_p[i].second <= m){\n m -= d_p[i].second;\n d_p[i].second = 0;\n }else{\n d_p[i].second -= m;\n m = 0;\n break;\n }\n }\n rep(i, n){\n ans += d_p[i].first * d_p[i].second;\n }\n cout << ans << endl;\n return 0;\n}\nint main() {\n while(solve() == 0);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3332, "score_of_the_acc": -0.3916, "final_rank": 7 }, { "submission_id": "aoj_2019_10580113", "code_snippet": "// AOJ 2019 - Princess's Marriage\n// JAG Practice Contest for Japan Domestic 2008 - Problem B\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nbool solving() {\n int N, M; cin >> N >> M;\n if(N == 0) return false;\n\n vector<pair<int, int>> segment(N);\n for(int i=0; i<N; ++i) {\n int d, p; cin >> d >> p;\n segment[i] = {-p, d};\n }\n sort(segment.begin(), segment.end());\n\n long long ex = 0;\n for(int i=0; i<N; ++i) {\n if(segment[i].second <= M) {\n M -= segment[i].second;\n } else {\n ex -= segment[i].first * (segment[i].second - M);\n M = 0;\n }\n }\n cout << ex << endl;\n return true;\n}\n\nint main() {\n while(solving()) {}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.6927, "final_rank": 15 }, { "submission_id": "aoj_2019_10544651", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\nlong long mod_mul(long long a, long long b, long long mod) {\n return (__int128)a * b % mod;\n}\n\nlong long mod_pow(long long a, long long b, long long mod) {\n long long result = 1;\n a %= mod;\n while (b > 0) {\n if (b & 1) result = mod_mul(result, a, mod);\n a = mod_mul(a, a, mod);\n b >>= 1;\n }\n return result;\n}\n\nbool MillerRabin(ll x){\n \n if(x <= 1){return false;}\n if(x == 2){return true;}\n if((x & 1) == 0){return false;}\n vll test;\n if(x < (1 << 30)){\n test = {2, 7, 61};\n } \n else{\n test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n }\n ll s = 0;\n ll d = x - 1;\n while((d & 1) == 0){\n s++;\n d >>= 1;\n }\n for(ll i:test){\n if(x <= i){return true;}\n ll y = mod_pow(i,d,x);\n if(y == 1 || y == x - 1){continue;}\n ll c = 0;\n for(int j=0;j<s-1;j++){\n y = mod_mul(y,y,x);\n if(y == x - 1){\n c = 1;\n break;\n }\n }\n if(c == 0){\n return false;\n }\n }\n return true;\n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n while(1){\n LL(n,m);\n if(n == 0 && m == 0){break;}\n vpll a;\n rep(i,n){\n LL(x,y);\n a.push_back({y,x});\n }\n sort(rall(a));\n ll ans = 0;\n for(auto [x,y]:a){\n ll z = min(y,m);\n y -= z;\n m -= z;\n ans += x * y;\n }\n print(ans);\n }\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3820, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_2019_10536889", "code_snippet": "#include <bits/stdc++.h>\n#include <vector>\nusing namespace std;\nint main() {\n int N,M,D,P,u,ans;\n cin >> N >> M;\n while(N!=0 or M!=0){\n ans = 0;\n vector<int>sumP(11,0);\n for(int i=0;i<N;i++){\n cin >> D >> P;\n sumP[P]+=D;\n ans += D * P;\n }\n \n u= 10;\n while(M > 0){\n ans -= min(M,sumP[u]) * u;\n M = M - sumP[u];\n u--;\n if(u == -1){\n ans = 0;\n break;\n }\n }\n cout << ans << endl;\n cin >> N >> M;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3436, "score_of_the_acc": -0.527, "final_rank": 11 }, { "submission_id": "aoj_2019_10498312", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <string>\n#include <functional>\nusing namespace std;\nusing ll = long long;\nusing lb = long double;\n\nint main() {\n int n, m;\n while (true) {\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n // sect: vector of pair<dist, exp>\n vector<pair<int, int>> sect;\n for (int i = 0; i < n; i++) {\n int d, p;\n cin >> d >> p;\n sect.push_back(make_pair(d, p));\n }\n\n while (m > 0) {\n int maxind = 0;\n // Search for sect where its dist is still above zero.\n while (maxind < n && sect[maxind].first == 0) maxind++;\n // If there's none, budget cannot be used anymore.\n if (maxind == n) break;\n int max = sect[maxind].second;\n // Search for max value of exp per dist, excluding its dist has already reached zero.\n for (int i = 0; i < n; i++) {\n if (sect[i].first != 0 && sect[i].second > max) {\n max = sect[i].second;\n maxind = i;\n }\n }\n // Decrease budget and maxind's dist. Dist reaches zero if whole of the section is paid from the budget.\n if (m - sect[maxind].first < 0) { //If budget won't pay whole of the section, minus dist by budget and let budget be zero.\n sect[maxind].first -= m;\n m = 0;\n } else { //If buget will pay whole of the section, minus budget by whole dist and let dist of the section be zero.\n m -= sect[maxind].first;\n sect[maxind].first = 0;\n }\n }\n //Calc dist * exp per dist. If dist is zero, whole of the section is paid from the budget. so exp is zero.\n int ans = 0;\n for (int i = 0; i < n; i++) {\n ans += sect[i].first * sect[i].second;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3500, "score_of_the_acc": -1.5833, "final_rank": 20 }, { "submission_id": "aoj_2019_10495790", "code_snippet": "#ifdef YONIHA\n#include <yoniha/all.h>\nusing namespace atcoder;\n#elifdef ATCODER\n#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace atcoder;\n#else\n#include <bits/stdc++.h>\n#endif\nusing namespace std;\n#define int long long\n#define all(x) (x).begin(), (x).end()\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rrep(i, n) for(int i = (int)(n - 1); i >= 0; i--)\ntemplate <typename T> bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate <typename T> bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vector<bool>>;\nusing vs = vector<string>;\nusing pii = pair<int, int>;\n/*\nusing mint = modint;\nusing vm = vector<mint>;\nusing vvm = vector<vector<mint>>;\n*/\n\nvoid solve(int n, int m){\n vector<pii> dp(n);\n for(auto&& [d, p] : dp) cin >> d >> p;\n sort(all(dp), [](pii x, pii y) {return x.second > y.second;});\n int ans = 0;\n for(auto [d, p] : dp){\n int use_money = min(m, d);\n m -= use_money;\n ans += (d - use_money) * p;\n }\n cout << ans << '\\n';\n}\n\nsigned main(){\n while(true){\n int n, m; cin >> n >> m;\n if(n == 0 && m == 0) return 0;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3620, "score_of_the_acc": -0.7666, "final_rank": 18 }, { "submission_id": "aoj_2019_10461040", "code_snippet": "// 問題:https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2019\n// 判定:\n\n//学習コメント:\n\n#include <bits/stdc++.h>\n#include <vector>\nusing namespace std;\nint main() {\n int N,M,D,P,u,ans;\n cin >> N >> M;\n while(N!=0 or M!=0){\n ans = 0;\n vector<int>sumP(11,0);\n for(int i=0;i<N;i++){\n cin >> D >> P;\n sumP[P]+=D;\n ans += D * P;\n }\n u= 10;\n while(M > 0){\n ans -= min(M,sumP[u]) * u;\n M = M - sumP[u];\n u--;\n if(u == -1){\n ans = 0;\n break;\n }\n }\n cout << ans << endl;\n cin >> N >> M;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3052, "score_of_the_acc": -0.027, "final_rank": 1 }, { "submission_id": "aoj_2019_9406018", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main() {\n while (1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n break;\n }\n vector<int> D(N), P(N);\n for (int i = 0; i < N; i++) {\n cin >> D[i] >> P[i];\n }\n vector<int> id(N, 0);\n iota(id.begin(), id.end(), 0);\n sort(id.begin(), id.end(), [&] (int a, int b) {\n if (P[a] == P[b]) {\n return D[a] < D[b];\n }\n return P[a] > P[b];\n });\n long long ans = 0;\n for (int i = 0; i < N; i++) {\n ans += (long long)D[i] * P[i];\n }\n for (int i = 0; i < N; i++) {\n if (M >= D[id[i]]) {\n ans -= P[id[i]] * D[id[i]];\n M -= D[id[i]];\n } else { \n ans -= P[id[i]] * M;\n M = 0;\n break;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3364, "score_of_the_acc": -0.4333, "final_rank": 9 }, { "submission_id": "aoj_2019_9364882", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nvoid solve(){\n while(true){\n int N,M;cin>>N>>M;\n if(N==0&&M==0)return;\n vector<pl> P(N);\n rep(i,N){\n ll a,b;cin>>a>>b;\n P[i] = {b,a};\n }\n sort(all(P),greater<pl>());\n ll ans =0;\n for(auto [p,d]:P){\n if(d <= M){\n M-=d;\n }else{\n d-=M;\n M = 0;\n ans+=d*p;\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3528, "score_of_the_acc": -0.6198, "final_rank": 13 }, { "submission_id": "aoj_2019_9347611", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = (int)1e9 + 1001010;\nconst ll llINF = (long long)4e18 + 22000020;\nconst string endn = \"\\n\";\ntemplate <class T> inline auto vector2(size_t i, size_t j, const T &init = T()) {return vector(i, vector<T>(j, init));}\nconst string ELEM_SEPARATION = \" \", VEC_SEPARATION = endn;\ntemplate<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}\ntemplate<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(const auto &I : A){o << I << (--i ? ELEM_SEPARATION : \"\");} return o;}\ntemplate<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(const auto &I : A){o << I << (--i ? VEC_SEPARATION : \"\");} return o;}\ntemplate<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}\ntemplate<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}\ntemplate<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}\ntemplate<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}\nll floor(ll a, ll b){if (b < 0) a = -a, b = -b; if(a >= 0) return a / b; else return (a + 1) / b - 1;}\nll ceil(ll a, ll b){if (b < 0) a = -a, b = -b; if(a > 0) return (a - 1) / b + 1; else return a / b;}\nll bit(unsigned long long val, unsigned long long digit){return (val >> digit) & 1;}\n#ifdef DEBUG\n#include <debug_slephy.cpp>\n#else\n#define debug(...)\n#endif\n// ================================== ここまでテンプレ ==================================\n\nvoid solve(){\n ll n, m; cin >> n >> m;\n if(n == 0 && m == 0) exit(0);\n vector<pair<ll, ll>> dp(n);\n for(int i = 0; i < n; i++) cin >> dp[i].first >> dp[i].second;\n\n ll ans = 0;\n for(auto [d, p] : dp){\n ans += d * p;\n }\n\n sort(dp.begin(), dp.end(),\n [](const auto &p, const auto &q){\n const auto &[d1, p1] = p;\n const auto &[d2, p2] = q;\n if(p1 != p2) return p1 > p2;\n else return d1 < d2;\n }\n );\n\n ll remain = m;\n for(auto [d, p] : dp){\n ll g = min(remain, d);\n remain -= g;\n ans -= g * p;\n }\n cout << ans << endl;\n}\n\nint main(int argc, char *argv[]){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while(true) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3536, "score_of_the_acc": -0.6302, "final_rank": 14 }, { "submission_id": "aoj_2019_9333570", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nint main(){\n while(true){\n ll N,M;\n cin >> N >> M;\n if(N==0&&M==0)break;\n vector<pair<int,int>>pair(N);\n for(ll i=0;i<N;i++){\n ll d,p;\n cin >> d >> p;\n pair[i].first=p;\n pair[i].second=d;\n // firstが金,secondが距離\n }\n sort(pair.rbegin(),pair.rend());\n int ans=0;\n for(int k=0;k<N;k++){\n if(M-pair[k].second>=0){\n M=M-pair[k].second;\n }else{\n ans+=(pair[k].second-M)*pair[k].first;\n M=0;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3440, "score_of_the_acc": -0.5322, "final_rank": 12 }, { "submission_id": "aoj_2019_9306576", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,k,n) for (int i = k; i < (n); ++i)\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\nconst long long INF = 1e17;\n\nint main(){\n while(true){\n int n,m;\n cin >> n >> m;\n if(n==0){\n break;\n }\n \n vector<int> d(n),p(n);\n vector<int> denger(11);\n for(int i=0;i<n;i++){\n cin >> d[i] >> p[i];\n denger[p[i]] += d[i];\n }\n\n for(int i=10;i>=1;i--){\n if(m-denger[i]>0){\n m -= denger[i];\n denger[i] = 0;\n }\n else{\n denger[i] -= m;\n m = 0;\n }\n }\n\n int sum = 0;\n int len = 0;\n for(int i=10;i>=1;i--){\n sum += denger[i] * i;\n len += denger[i];\n }\n\n cout << sum << endl;\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3184, "score_of_the_acc": -0.1989, "final_rank": 3 }, { "submission_id": "aoj_2019_9291612", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nbool f(P A,P B){\n return A.second>B.second;\n}\n\nbool solve(){\n ll N,M;cin>>N>>M;\n if(N==0&&M==0) return 0;\n\n vp AB(N);rep(i,N) cin>>AB[i].first>>AB[i].second;\n ll ans=0;\n rep(i,N) ans+=AB[i].first*AB[i].second;\n sort(all(AB),f);\n rep(i,N){\n if(AB[i].first<=M) M-=AB[i].first,ans-=AB[i].first*AB[i].second;\n else ans-=M*AB[i].second,M=0;\n }\n\n cout << ans << endl;\n\n\n return 1;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(solve());\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3612, "score_of_the_acc": -0.7292, "final_rank": 17 }, { "submission_id": "aoj_2019_9263028", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\n\nll n,m;\n\nvoid solve() {\n vector<ll> v (11,0);\n int x,y;\n rep(i,n) {\n cin >> x >> y;\n v[y] += x;\n }\n ll ans = 0;\n rep(i,11) {\n if (v[10-i] <= m) {\n m -= v[10-i];\n }\n else {\n ans += (v[10-i]-m)*(10-i);\n m = 0;\n }\n }\n cout << ans << endl;\n return;\n}\n\nint main() {\n while (true) {\n cin >> n >> m;\n if (n == 0) {\n return 0;\n }\n solve();\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3348, "score_of_the_acc": -0.4124, "final_rank": 8 }, { "submission_id": "aoj_2019_9126268", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n,m;\n\tpair<int,int> a[10000];\n\tint d,p;\n\tint ans=0,cn;\n\twhile(1){\n\t\tcin>>n>>m;\n\t\tif(n==0&&m==0)break;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tcin>>d>>p;\n\t\t\ta[i]=make_pair(p,d);\n\t\t\tans+=d*p;\n\t\t}\n\t\tsort(a,a+n);\n\t\treverse(a,a+n);\n\t\tcn=0;\n\t\twhile(m>0){\n\t\t\tif(m<a[cn].second){\n\t\t\t\tans-=m*a[cn].first;\n\t\t\t\tm=0;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tans-=a[cn].first*a[cn].second;\n\t\t\t\tm-=a[cn].second;\n\t\t\t}\n\t\t\tcn++;\n\t\t\tif(cn==n)break;\n\t\t}\n\t\tcout<<ans<<endl;\n\t\tans=0;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3212, "score_of_the_acc": -0.2354, "final_rank": 4 }, { "submission_id": "aoj_2019_8976190", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nll solve(int N, ll M){\n vc<pair<ll, ll>> pd(N);\n rep(i, N) cin >> pd[i].second >> pd[i].first;\n sort(all(pd), [&](auto i, auto j){return i > j;});\n ll ans = 0;\n for (auto [p, d] : pd){\n ll tmp = min(d, M);\n d -= tmp;\n M -= tmp;\n ans += p * d;\n }\n return ans;\n}\n\nint main(){\n vc<ll> ans;\n while (true){\n int N; ll M; cin >> N >> M;\n if (N == 0) break;\n ans.push_back(solve(N, M));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3220, "score_of_the_acc": -0.2458, "final_rank": 5 } ]
aoj_2022_cpp
Princess, a Cryptanalyst お姫様の暗号解読 English text is not available in this practice contest. ある貧乏な国のおてんばで勇敢なお姫様は,お忍びで出かけた町である日本語で書かれた古文書を手に入れた.お姫様は日本語ができるため,さっそくこの古文書を読んでみた.すると,驚くべき事が分かった.この古文書は,古の秘宝の在処を示していたのである.ところが,秘宝の在処は暗号になっており,容易には分からないようになっていた.そこでお姫様は,従者であるあなたに暗号の解読を手伝うように命令を下した. あなたはお姫様を助けるため昼夜を問わず調査を行った.その結果,Shortest Secret String (SSS) と呼ばれる文字列が解読に重要な役割を果たすことがわかった.ここで SSS とは, N 個の単語全部を部分文字列として含み,なおかつその長さが最小であるような文字列である. あなたの仕事は, N 個の単語から SSS を見つけ出す事である. Input 入力は複数のデータセットで与えられる.データセットの初めの一行は,データセットに含まれる単語の数 N (1≦ N ≦10)が与えられる.続く N 行に単語が与えられる.最後のデータセットの後に,0のみを含む一行が与えられる. なお,入力で与えられる単語はアルファベット小文字のみからなり,長さは高々10であることが保証されている. Output 各データセットについてSSS を一行に出力せよ.また,複数ある場合は辞書式順序で最小のものを出力せよ. Sample Input 4 apple length things thin 2 icp cpc 3 zeta eta alphabet 2 until till 0 Output for the Sample Input applengthings icpc zetalphabet untill
[ { "submission_id": "aoj_2022_5538521", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 110;\nint main(){\n while (true){\n int N;\n cin >> N;\n if (N == 0){\n break;\n }\n vector<string> S(N);\n for (int i = 0; i < N; i++){\n cin >> S[i];\n }\n sort(S.begin(), S.end());\n S.erase(unique(S.begin(), S.end()), S.end());\n N = S.size();\n vector<string> T;\n for (int i = 0; i < N; i++){\n bool ok = true;\n int L1 = S[i].size();\n for (int j = 0; j < N; j++){\n if (i != j){\n int L2 = S[j].size();\n for (int k = 0; k <= L2 - L1; k++){\n if (S[j].substr(k, L1) == S[i]){\n ok = false;\n }\n }\n }\n }\n if (ok){\n T.push_back(S[i]);\n }\n }\n N = T.size();\n vector<vector<int>> d(N, vector<int>(N));\n for (int i = 0; i < N; i++){\n for (int j = 0; j < N; j++){\n int L1 = T[i].size();\n int L2 = T[j].size();\n for (int k = 0; k <= min(L1, L2); k++){\n if (T[i].substr(L1 - k) == T[j].substr(0, k)){\n d[i][j] = L2 - k;\n }\n }\n }\n }\n vector<vector<string>> dp(1 << N, vector<string>(N, string(INF, ' ')));\n for (int i = 0; i < N; i++){\n dp[1 << i][i] = T[i];\n }\n for (int i = 0; i < (1 << N); i++){\n for (int j = 0; j < N; j++){\n if ((i >> j & 1) == 1){\n for (int k = 0; k < N; k++){\n if ((i >> k & 1) == 0){\n int i2 = i | (1 << k);\n string t = dp[i][j] + T[k].substr(T[k].size() - d[j][k]);\n if (dp[i2][k].size() > t.size() || dp[i2][k].size() == t.size() && dp[i2][k] > t){\n dp[i2][k] = t;\n }\n }\n }\n }\n }\n }\n string ans(INF, ' ');\n for (int i = 0; i < N; i++){\n \tif (ans.size() > dp[(1 << N) - 1][i].size() || ans.size() == dp[(1 << N) - 1][i].size() && ans > dp[(1 << N) - 1][i]){\n \t ans = dp[(1 << N) - 1][i];\n \t}\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5040, "score_of_the_acc": -0.0323, "final_rank": 5 }, { "submission_id": "aoj_2022_4931111", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dp[1<<14][14];\nint len[14][14];\n\nint main(){\n int n;\n while(cin >> n,n){\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n dp[i][j]=1e9;\n }\n }\n vector<string> s(n);\n for(int i=0;i<n;i++){\n cin >> s[i];\n }\n sort(s.begin(), s.end());\n s.erase(unique(s.begin(), s.end()),s.end());\n n=s.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i==j)len[i][j]=s[j].size();\n else{\n int cnt=0;\n int p=s[j].size();\n for(int k=0;k<min(s[i].size(),s[j].size());k++){\n if(s[j].substr(0,k+1)==s[i].substr((int)s[i].size()-1-k))cnt=k+1;\n }\n len[i][j]=p-cnt;\n }\n }\n }\n vector<bool> notuse(n,false);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i==j)continue;\n for(int k=0;k+(int)(s[j].size())-1<s[i].size();k++){\n if(s[i].substr(k,(int)(s[j].size()))==s[j])notuse[j]=true;\n }\n }\n }\n int aa=0;\n for(int i=0;i<n;i++){\n if(notuse[i])aa+=(1<<i);\n }\n for(int i=0;i<n;i++){\n if(notuse[i])continue;\n dp[aa+(1<<i)][i]=(int)s[i].size();\n }\n int res=1e9;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n if(dp[i][j]==1e9)continue;\n for(int k=0;k<n;k++){\n if((1<<k)&i)continue;\n dp[i+(1<<k)][k]=min(dp[i+(1<<k)][k],dp[i][j]+len[j][k]);\n }\n }\n }\n for(int i=0;i<n;i++){\n res=min(res,dp[(1<<n)-1][i]);\n }\n vector<string> V;\n auto con=[&](vector<int> id)->string{\n reverse(id.begin(), id.end());\n string ans=s[id[0]];\n for(int i=1;i<id.size();i++){\n int p=id[i-1],c=id[i];\n for(int j=(int)s[c].size()-len[p][c];j<(int)s[c].size();j++){\n ans+=s[c][j];\n }\n }\n return ans;\n };\n auto dfs=[&](auto dfs,vector<int> &v,int bit,int last)->void{\n if(bit-(1<<last)==aa){\n V.push_back(con(v)); return;\n }\n for(int i=0;i<n;i++){\n if((1<<i)&aa)continue;\n if(i==last)continue;\n if((1<<i)&bit){\n if(dp[bit-(1<<last)][i]+len[i][last]==dp[bit][last]){\n v.push_back(i);\n dfs(dfs,v,bit-(1<<last),i);\n v.pop_back();\n }\n }\n }\n return;\n };\n for(int i=0;i<n;i++){\n if((1<<i)&aa)continue;\n if(dp[(1<<n)-1][i]==res){\n vector<int> v={i};\n dfs(dfs,v,(1<<n)-1,i);\n }\n }\n sort(V.begin(), V.end());\n cout << V[0] << endl;\n }\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 51140, "score_of_the_acc": -0.8672, "final_rank": 16 }, { "submission_id": "aoj_2022_4931110", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dp[1<<14][14];\nint len[14][14];\n\nint main(){\n int n;\n while(cin >> n,n){\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n dp[i][j]=1e9;\n }\n }\n vector<string> s(n);\n for(int i=0;i<n;i++){\n cin >> s[i];\n }\n sort(s.begin(), s.end());\n s.erase(unique(s.begin(), s.end()),s.end());\n n=s.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i==j)len[i][j]=s[j].size();\n else{\n int cnt=0;\n int p=s[j].size();\n for(int k=0;k<min(s[i].size(),s[j].size());k++){\n if(s[j].substr(0,k+1)==s[i].substr((int)s[i].size()-1-k))cnt=k+1;\n }\n len[i][j]=p-cnt;\n }\n }\n }\n vector<bool> notuse(n,false);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i==j)continue;\n for(int k=0;k+(int)(s[j].size())-1<s[i].size();k++){\n if(s[i].substr(k,(int)(s[j].size()))==s[j])notuse[j]=true;\n }\n }\n }\n int aa=0;\n for(int i=0;i<n;i++){\n if(notuse[i])aa+=(1<<i);\n }\n for(int i=0;i<n;i++){\n if(notuse[i])continue;\n dp[aa+(1<<i)][i]=(int)s[i].size();\n }\n int res=1e9;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n if(dp[i][j]==1e9)continue;\n for(int k=0;k<n;k++){\n if((1<<k)&i)continue;\n dp[i+(1<<k)][k]=min(dp[i+(1<<k)][k],dp[i][j]+len[j][k]);\n }\n }\n }\n for(int i=0;i<n;i++){\n res=min(res,dp[(1<<n)-1][i]);\n }\n vector<string> V;\n auto con=[&](vector<int> id)->string{\n reverse(id.begin(), id.end());\n string ans=s[id[0]];\n for(int i=1;i<id.size();i++){\n int p=id[i-1],c=id[i];\n for(int j=(int)s[c].size()-len[p][c];j<(int)s[c].size();j++){\n ans+=s[c][j];\n }\n }\n return ans;\n };\n auto dfs=[&](auto dfs,vector<int> &v,int bit,int last)->void{\n if(bit-(1<<last)==aa){\n V.push_back(con(v)); return;\n }\n for(int i=0;i<n;i++){\n if((1<<i)&aa)continue;\n if(i==last)continue;\n if((1<<i)&bit){\n if(dp[bit-(1<<last)][i]+len[i][last]==dp[bit][last]){\n v.push_back(i);\n dfs(dfs,v,bit-(1<<last),i);\n v.pop_back();\n }\n }\n }\n return;\n };\n for(int i=0;i<n;i++){\n if((1<<i)&aa)continue;\n if(dp[(1<<n)-1][i]==res){\n vector<int> v={i};\n dfs(dfs,v,(1<<n)-1,i);\n }\n }\n // for(int _=0;_<n;_++){\n // if(res==dp[(1<<n)-1][_]){\n // int curid=_,cur=(1<<n)-1;\n // vector<int> id;\n // while(1){\n // id.push_back(curid);\n // int nx=pre[cur][curid];\n // if(nx<0)break;\n // cur-=(1<<curid);\n // curid=nx;\n // }\n // reverse(id.begin(), id.end());\n // string ans=s[id[0]];\n // for(int i=1;i<id.size();i++){\n // int p=id[i-1];\n // int c=id[i];\n // for(int j=(int)s[c].size()-len[p][c];j<(int)s[c].size();j++){\n // ans+=s[c][j];\n // }\n // }\n // v.push_back(ans);\n // }\n // }\n\n sort(V.begin(), V.end());\n cout << V[0] << endl;\n }\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 51168, "score_of_the_acc": -0.8677, "final_rank": 17 }, { "submission_id": "aoj_2022_4911241", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nconst double pi = 3.141592653589793238462643383279;\nusing namespace std;\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a) * (a))\n#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(c) sort((c).begin(), (c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\nconst double EPS = 1e-10, PI = acos(-1);\n//ここから編集\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(){}\n RollingHash(string s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = MOD;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n RollingHash& operator=(const RollingHash&) { return *this; }\n};\n\nbool max(const string& s, const string& t){\n \n if(s.size() < t.size()) return false;\n else if(s.size() > t.size()) return true;\n for(int i=0; i<s.size(); i++){\n if(s[i] > t[i]) return true;\n else if(s[i] < t[i]) return false;\n }\n return false;\n}\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n \n int n; \n while(cin >> n, n){\n vector<string> v(n);\n REP(i,n) cin >> v[i];\n REP(i,n) {\n RollingHash rh(v[i]);\n \n }\n vector<vector<string>> dp(n, vector<string>(1<<n, \"~\"));\n dp[0][0] = \"\";\n for(int i=0; i<n; i++){\n dp[i][(1<<i)] = v[i];\n }\n REP(bit,(1<<n)){\n\n for(int i=0; i<n; i++){\n if(dp[i][bit] == \"~\") continue;\n if(bit >> i & 1){\n\n for(int j=0; j<n; j++){\n if(!(bit >> j & 1)){\n\n /* dp[i][bit]にvjをくっつける */\n string s = dp[i][bit];\n string t = v[j];\n RollingHash rh(s);\n RollingHash rh2(t);\n int mx = 0;\n \n for(int k=1; k<=min(s.size(), t.size()); k++){\n if(rh.get(s.size()-k, s.size()) == rh2.get(0, k)) mx = max(mx, k);\n }\n \n if(rh.count(t)){\n /* dp[j][bit | (1 << j)]とdp[i][bit]の大きさを比較 */\n\n if(dp[j][bit | (1 << j)] == \"~\") dp[j][bit | (1 << j)] = dp[i][bit];\n else{ \n if(max(dp[j][bit | (1 << j)], dp[i][bit])){\n dp[j][bit|(1<<j)] = dp[i][bit];\n }\n }\n }else{\n string nx = s + t.substr(mx, t.size()-mx);\n /* dp[j][bit | (1 << j)]とnxの大きさを比較 */\n\n if(dp[j][bit | (1 << j)] == \"~\") dp[j][bit | (1 << j)] = nx;\n else{\n if(max(dp[j][bit | (1 << j)], nx)){\n dp[j][bit|(1<<j)] = nx;\n }\n }\n }\n }\n }\n }\n }\n }\n string ans = \"~\";\n for(int i=0; i<n; i++){\n if(ans == \"~\") ans = dp[i][(1<<n)-1];\n else if(max(ans, dp[i][(1<<n)-1])){\n ans = dp[i][(1<<n)-1];\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 3516, "score_of_the_acc": -0.0724, "final_rank": 9 }, { "submission_id": "aoj_2022_4804724", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nbool contains(string a, string b){\n int n = a.length();\n int m = b.length();\n if(n < m) return false;\n for(int i=0; i<n-m+1; i++){\n if(b == a.substr(i, m)){\n return true;\n }\n }\n return false;\n}\nint match(string a, string b){\n int n = a.length();\n int m = b.length();\n for(int i=0; i<n; i++){\n if(m < n-i) continue;\n if(a.substr(i, n-i) == b.substr(0, n-i)){\n return n-i;\n }\n }\n return 0;\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<string> s(n);\n for(int i=0; i<n; i++){\n cin >> s[i];\n }\n for(int i=n-1; i>=0; i--){\n for(int j=0; j<n; j++){\n if(i == j) continue;\n if(contains(s[j], s[i])){\n s.erase(s.begin()+i);\n n--;\n break;\n }\n }\n }\n int sum = 0;\n for(auto str: s){\n sum += str.length();\n }\n vector<vector<int>> a(n, vector<int>(n));\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n a[i][j] = match(s[i], s[j]);\n }\n }\n vector<vector<string>> dp(1<<n, vector<string>(n, \"\"));\n for(int i=0; i<n; i++){\n dp[1<<i][i] = s[i];\n }\n for(int i=0; i<(1<<n); i++){\n for(int j=0; j<n; j++){\n if(!(i>>j&1)) continue;\n for(int k=0; k<n; k++){\n if(i>>k&1) continue;\n int ni = i | (1<<k);\n string ns = dp[i][j] +s[k].substr(a[j][k], (int)s[k].length()-a[j][k]);\n if(dp[ni][k] == \"\") dp[ni][k] = ns;\n if(dp[ni][k] == \"\" or ns.length() < dp[ni][k].length() or\n (ns.length() == dp[ni][k].length() and ns < dp[ni][k])){\n dp[ni][k] = ns;\n }\n }\n }\n }\n string ans(100, 'z');\n for(string str: dp.back()){\n if(str == \"\") continue;\n if(str.length() < ans.length() or (str.length()==ans.length() and str<ans)){\n ans = str;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3440, "score_of_the_acc": -0.0071, "final_rank": 1 }, { "submission_id": "aoj_2022_3875221", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nbool contains(string str, string pre){\n int s = str.length(), p = pre.length();\n if(s < p) return false;\n\n for(int i = 0; i <= s - p; i++){\n int j;\n for(j = 0; j < p; j++){ \n if(str[i+j] != pre[j]) break;\n }\n if(j == p) return true;\n }\n\n return false;\n}\n\ntypedef unsigned long long ull;\null x = 1000000007;\n\nint f(string s, string t){\n int k = min(s.length(), t.length()), slen = s.length();\n int ret = 0;\n ull a = 0, b = 0, tmp = 1;\n for(int i = 1; i <= k; i++){\n a = a + s[slen-i]*tmp;\n b = b*x + t[i-1];\n if(a == b) ret = i;\n tmp *= x;\n }\n return ret;\n}\n\nint main(){\n int n;\n while(cin >> n, n){\n vector<string> v;\n for(int i = 0; i < n; i++){\n string s; cin >> s;\n v.push_back(s);\n }\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n n = v.size();\n vector<string> x;\n for(int i = 0; i < n; i++){\n bool valid = true;\n for(int j = 0; j < n; j++){\n if(i == j) continue;\n if(contains(v[j], v[i])) valid = false;\n }\n if(valid) x.push_back(v[i]);\n }\n int n = x.size();\n vector<vector<string>> dp(n, vector<string>(1<<n, \"\"));\n for(int i = 0; i < n; i++) dp[i][1<<i] = x[i];\n for(int s = 1; s < 1<<n; s++){\n for(int i = 0; i < n; i++){\n if(((s>>i)&1) == 0) continue;\n for(int j = 0; j < n; j++){\n if((s>>j)&1) continue;\n string tmp = dp[i][s];\n tmp += x[j].substr(f(dp[i][s], x[j]));\n int nbit = s + (1<<j);\n if(dp[j][nbit] == \"\" || tmp.length() < dp[j][nbit].length() || (tmp.length()==dp[j][nbit].length() && tmp < dp[j][nbit])){\n dp[j][nbit] = tmp;\n }\n }\n }\n }\n string ans = dp[0].back();\n for(int i = 1; i < n; i++){\n if(ans.length() > dp[i].back().length() || (ans.length()==dp[i].back().length() && ans > dp[i].back())){\n ans = dp[i].back();\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3412, "score_of_the_acc": -0.0095, "final_rank": 2 }, { "submission_id": "aoj_2022_3504590", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) repi(i,0,n)\n#define repi(i,a,b) for(int i=(int)(a);i < (int)(b);++i)\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class Key,class Value> ostream& operator << (ostream &s, map<Key,Value> M)\n{ for ( auto itr = begin(M); itr != end(M); ++itr) { s << itr->first << \":\" << itr->second; } return s;}\nvoid print(const std::vector<int>& v)\n{\n std::for_each(v.begin(), v.end(), [](int x) {\n std::cout << x << \" \";\n });\n std::cout << std::endl;\n}\nbool is_sub(string s1,string s2) {\n int s1_size = s1.size();\n int s2_size = s2.size();\n if (s1_size > s2_size) {\n rep(i,s1_size-s2_size + 1) {\n bool flg = true;\n rep(j,s2_size) {\n if (s1[i+j] != s2[j]) {\n flg = false;\n break;\n }\n }\n if (flg) return true;\n }\n return false;\n }\n else {\n rep(i,s2_size-s1_size + 1) {\n bool flg = true;\n rep(j,s1_size) {\n if (s2[i+j] != s1[j]) {\n flg = false;\n break;\n }\n }\n if (flg) return true;\n }\n return false;\n }\n}\n\nint main() {\n while (true) {\n int n = 0;\n cin >> n;\n if (n == 0) {\n break;\n }\n long init = 0;\n vector<string> S(n);\n rep(i,n) cin >> S[i];\n rep(i,n-1) {\n repi(j,i+1,n) {\n // cout << is_sub(S[i],S[j]) << \" \" << i << \" \" << j << endl;\n if( is_sub(S[i],S[j]) ) {\n if (S[i].size() > S[j].size()) init |= (1 << j);\n else init |= (1 << i);\n }\n }\n }\n\n\n vector<int> v;\n rep(i,n) {\n if (!((init >> i) & 1)) v.push_back(i);\n }\n //cout << v << endl;\n // cout << v << endl;\n\n // suffix,prefix計算\n vector<vector<string>> preffix(n),suffix(n);\n rep(i,n) {\n if (((init >> i) & 1)) continue;\n preffix[i] = vector<string>(S[i].size()+1,\"\");\n suffix[i] = vector<string>(S[i].size()+1,\"\");\n rep(j, S[i].size()) {\n repi(k,j+1,S[i].size()+1) {\n preffix[i][k] += S[i][j];\n suffix[i][k] = S[i][S[i].size()-1-j] + suffix[i][k];\n }\n }\n }\n\n int string_num = v.size();\n string ans = \"77\";\n \n do {\n string cur = S[v[0]];\n rep(i, string_num-1) {\n int s_size = min(S[v[i]].size(),S[v[i+1]].size());\n rep(j,s_size) {\n //cout << suffix[v[i]][s_size-1-j] << \" \" << preffix[v[i+1]][s_size-1-j] << endl;;\n if (suffix[v[i]][s_size-1-j] == preffix[v[i+1]][s_size-1-j]) {\n //cout << cur << \" \" << suffix[v[i+1]][S[v[i+1]].size()-j] << endl;\n \n int match = s_size - 1 - j;\n cur += suffix[v[i+1]][S[v[i+1]].size()-match];\n break;\n }\n }\n }\n if (ans == \"77\" || ans.size() > cur.size() || ( ans.size() == cur.size() && ans > cur)) ans = cur;\n } while (next_permutation(v.begin(), v.end()));\n \n cout << ans << endl;\n\n\n \n\n \n \n }\n}", "accuracy": 1, "time_ms": 4390, "memory_kb": 3160, "score_of_the_acc": -0.624, "final_rank": 15 }, { "submission_id": "aoj_2022_3324384", "code_snippet": "#include <bits/stdc++.h>\n\n#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)\n#define RFOR(i,a) for(int i=(a); i >= 0; --i)\n#define FOE(i,a) for(auto i : a)\n#define ALL(c) (c).begin(), (c).end()\n#define RALL(c) (c).rbegin(), (c).rend()\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl;\n#define SUM(x) std::accumulate(ALL(x), 0LL)\n#define MIN(v) *std::min_element(v.begin(), v.end())\n#define MAX(v) *std::max_element(v.begin(), v.end())\n#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())\n#define BIT_ON(bit, i) (bit & (1LL << i))\n\ntypedef long long LL;\ntemplate<typename T> using V = std::vector<T>;\ntemplate<typename T> using VV = std::vector<std::vector<T>>;\ntemplate<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate<typename T> using VVVV = std::vector<std::vector<std::vector<std::vector<T>>>>;\n\ntemplate<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }\ntemplate<class T> inline void print(T x) { std::cout << x << std::endl; }\ntemplate<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) { if (i != 0) {std::cout << \" \";} std::cout << v[i];} std::cout << \"\\n\"; }\ntemplate<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }\ntemplate<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }\ntemplate<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }\ntemplate<class T> inline std::vector<T> unique(std::vector<T> v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n return v;\n}\n\nconst int INF = 1 << 30;\nconst double EPS = 1e-9;\nconst std::string YES = \"YES\", Yes = \"Yes\", NO = \"NO\", No = \"No\";\nconst std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 }; // 4近傍(右, 下, 左, 上)\nconst std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };\n\nusing namespace std;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n int N;\n cin >> N;\n if (N == 0) {\n break;\n }\n\n V<string> tmp;\n FOR(i, 0, N) {\n string S;\n cin >> S;\n tmp.emplace_back(S);\n }\n\n tmp = unique(tmp);\n\n V<string> v;\n FOR(i, 0, tmp.size()) {\n string s = tmp[i];\n bool ok = true;\n\n FOR(j, 0, tmp.size()) {\n string t = tmp[j];\n if (i == j) {\n continue;\n }\n\n if (t.find(s) != string::npos) {\n ok = false;\n }\n }\n if (ok) {\n v.emplace_back(s);\n }\n }\n VV<string> memo(v.size(), V<string>(v.size(), \"\"));\n FOR(i, 0, v.size()) {\n string s = v[i];\n FOR(j, 0, v.size()) {\n string t = v[j];\n\n int same = 0;\n for (int n = v[j].size(); n >= 0; --n) {\n\n bool ok = true;\n FOR(k, 0, n) {\n if (s[s.size() - n + k] != t[k]) {\n ok = false;\n break;\n }\n }\n if (ok) {\n same = n;\n break;\n }\n }\n string ans = \"\";\n FOR(k, same, t.size()) {\n ans += t[k];\n }\n memo[i][j] = ans;\n }\n }\n\n string ans = \"\";\n FOR(i, 0, 110) {\n ans += \"z\";\n }\n\n V<int> idx(v.size());\n iota(ALL(idx), 0);\n\n do {\n string t = v[idx[0]];\n FOR(i, 1, v.size()) {\n t += memo[idx[i - 1]][idx[i]];\n if (t.size() > ans.size()) {\n break;\n }\n }\n\n if (t.size() == ans.size()) {\n ans = min(ans, t);\n }\n else if (t.size() < ans.size()) {\n ans = t;\n }\n } while(next_permutation(ALL(idx)));\n\n print(ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2000, "memory_kb": 3192, "score_of_the_acc": -0.2846, "final_rank": 13 }, { "submission_id": "aoj_2022_3324348", "code_snippet": "#include <bits/stdc++.h>\n\n#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)\n#define RFOR(i,a) for(int i=(a); i >= 0; --i)\n#define FOE(i,a) for(auto i : a)\n#define ALL(c) (c).begin(), (c).end()\n#define RALL(c) (c).rbegin(), (c).rend()\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl;\n#define SUM(x) std::accumulate(ALL(x), 0LL)\n#define MIN(v) *std::min_element(v.begin(), v.end())\n#define MAX(v) *std::max_element(v.begin(), v.end())\n#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())\n#define BIT_ON(bit, i) (bit & (1LL << i))\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\n\ntypedef long long LL;\ntemplate<typename T> using V = std::vector<T>;\ntemplate<typename T> using VV = std::vector<std::vector<T>>;\ntemplate<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate<typename T> using VVVV = std::vector<std::vector<std::vector<std::vector<T>>>>;\n\ntemplate<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }\ntemplate<class T> inline void print(T x) { std::cout << x << std::endl; }\ntemplate<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) { if (i != 0) {std::cout << \" \";} std::cout << v[i];} std::cout << \"\\n\"; }\ntemplate<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }\ntemplate<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }\ntemplate<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }\n\nconst int INF = 1 << 30;\nconst double EPS = 1e-9;\nconst std::string YES = \"YES\", Yes = \"Yes\", NO = \"NO\", No = \"No\";\nconst std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 }; // 4近傍(右, 下, 左, 上)\nconst std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };\n\nusing namespace std;\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n int N;\n cin >> N;\n if (N == 0) {\n break;\n }\n\n V<string> tmp;\n FOR(i, 0, N) {\n string S;\n cin >> S;\n tmp.emplace_back(S);\n }\n\n sort(ALL(tmp));\n tmp.erase(unique(ALL(tmp)), tmp.end());\n\n V<string> v;\n FOR(i, 0, tmp.size()) {\n string s = tmp[i];\n bool ok = true;\n\n FOR(j, 0, tmp.size()) {\n string t = tmp[j];\n if (i == j) {\n continue;\n }\n\n if (t.find(s) != string::npos) {\n ok = false;\n }\n }\n if (ok) {\n v.emplace_back(s);\n }\n }\n VV<string> memo(v.size(), V<string>(v.size(), \"\"));\n FOR(i, 0, v.size()) {\n string s = v[i];\n FOR(j, 0, v.size()) {\n string t = v[j];\n\n int same = 0;\n for (int n = v[j].size(); n >= 0; --n) {\n\n bool ok = true;\n FOR(k, 0, n) {\n if (s[s.size() - n + k] != t[k]) {\n ok = false;\n break;\n }\n }\n if (ok) {\n same = n;\n break;\n }\n }\n string ans = \"\";\n FOR(k, same, t.size()) {\n ans += t[k];\n }\n memo[i][j] = ans;\n }\n }\n\n string ans = \"\";\n FOR(i, 0, 110) {\n ans += \"z\";\n }\n\n V<int> idx(v.size());\n iota(ALL(idx), 0);\n\n do {\n string t = v[idx[0]];\n FOR(i, 1, v.size()) {\n t += memo[idx[i - 1]][idx[i]];\n if (t.size() > ans.size()) {\n break;\n }\n }\n\n if (t.size() == ans.size()) {\n ans = min(ans, t);\n }\n else if (t.size() < ans.size()) {\n ans = t;\n }\n } while(next_permutation(ALL(idx)));\n\n print(ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2020, "memory_kb": 3188, "score_of_the_acc": -0.2874, "final_rank": 14 }, { "submission_id": "aoj_2022_2542383", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Info{\n\tshort state;\n\tshort num_used,total_length;\n\tll order;\n};\n\nint N,match_len[10][10],POW[12];\nchar first_words[10][11],words[10][11];\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nint strCmp2(char* left,char* right){\n\tint i;\n\n\tif(strCmp(left,right))return 3;\n\n\tfor(i=0;left[i] != '\\0' && right[i] != '\\0'; i++){\n\t\tif(left[i] != right[i]){\n\t\t\tif(left[i] < right[i])return 1;\n\t\t\telse{\n\t\t\t\treturn 2;\n\t\t\t}\n\t\t}\n\t}\n\tif(left[i] == '\\0')return 1;\n\telse{\n\t\treturn 2;\n\t}\n}\n\nvoid func(){\n\n\tint words_index = 0,first_length[N],second_length[N];\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%s\",first_words[i]);\n\t\tfor(tmp = 0; first_words[i][tmp] != '\\0';tmp++);\n\t\tfirst_length[i] = tmp;\n\t}\n\n\tint i_index,k_index,delete_num = 0;\n\tbool FLG;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tFLG = true;\n\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(first_words[k][0] == '\\0')continue;\n\t\t\tif(i != k && first_length[i] <= first_length[k]){\n\n\t\t\t\tfor(int start = 0; start + first_length[i] <= first_length[k]; start++){\n\n\t\t\t\t\tfor(i_index = 0,k_index = 0; i_index < first_length[i]\n\t\t\t\t\t\t\t&& first_words[i][i_index] == first_words[k][start+k_index]; i_index++,k_index++);\n\n\t\t\t\t\tif(i_index == first_length[i]){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}\n\t\t\tif(!FLG)break;\n\t\t}\n\n\t\tif(FLG){\n\t\t\tsecond_length[words_index] = first_length[i];\n\t\t\tstrcpy(words[words_index++],first_words[i]);\n\t\t}else{\n\t\t\tfirst_words[i][0] = '\\0';\n\t\t\tdelete_num++;\n\t\t}\n\t}\n\n\tN -= delete_num;\n\n\tint common,tmp_common;\n\n\tfor(int left = 0;left < N; left++){\n\t\tfor(int right = 0; right < N; right++){\n\t\t\tif(left != right){\n\n\t\t\t\tcommon = 0;\n\n\t\t\t\tfor(int start = 0; start < second_length[left]; start++){\n\t\t\t\t\tfor(tmp_common = 0; tmp_common < second_length[right] && start+tmp_common < second_length[left] &&\n\t\t\t\t\t\t\twords[left][start+tmp_common] == words[right][tmp_common]; tmp_common++);\n\n\t\t\t\t\tif(start+tmp_common == second_length[left]){\n\t\t\t\t\t\tcommon = tmp_common;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tmatch_len[left][right] = common;\n\t\t\t}\n\t\t}\n\t}\n\n\tqueue<Info> Q;\n\n\tfor(int i = 0; i < N; i++){\n\t\tInfo new_info;\n\t\tnew_info.state = POW[i];\n\t\tnew_info.num_used = 1;\n\t\tnew_info.order = i;\n\t\tnew_info.total_length = second_length[i];\n\t\tQ.push(new_info);\n\t}\n\n\tint ans = BIG_NUM;\n\n\tchar ans_word[101],tmp_word[101];\n\tint work[N];\n\tstack<int> S;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.front().num_used == N){\n\n\t\t\tif(ans >= Q.front().total_length ){\n\n\t\t\t\tll work_num = Q.front().order;\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tS.push(work_num%10);\n\t\t\t\t\twork_num /= 10;\n\t\t\t\t}\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\twork[i] = S.top();\n\t\t\t\t\tS.pop();\n\t\t\t\t}\n\n\t\t\t\tstrcpy(tmp_word,words[work[0]]);\n\t\t\t\tint index = second_length[work[0]];\n\n\t\t\t\tint pre = work[0];\n\n\t\t\t\tfor(int i = 1; i < N; i++){\n\t\t\t\t\tfor(int k = match_len[pre][work[i]];words[work[i]][k] != '\\0'; k++){\n\t\t\t\t\t\ttmp_word[index++] = words[work[i]][k];\n\t\t\t\t\t}\n\t\t\t\t\tpre = work[i];\n\t\t\t\t}\n\n\t\t\t\ttmp_word[Q.front().total_length] = '\\0';\n\n\t\t\t\tif(ans > Q.front().total_length){\n\t\t\t\t\tans = Q.front().total_length;\n\t\t\t\t\tstrcpy(ans_word,tmp_word);\n\t\t\t\t}else{\n\t\t\t\t\tif(strCmp2(ans_word,tmp_word) == 2){\n\t\t\t\t\t\tstrcpy(ans_word,tmp_word);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}else if(Q.front().total_length >= ans){\n\t\t\tQ.pop();\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tif(!(Q.front().state & (1 << i))){\n\t\t\t\t\tInfo new_info;\n\t\t\t\t\tnew_info.state = Q.front().state+POW[i];\n\t\t\t\t\tnew_info.num_used = Q.front().num_used+1;\n\t\t\t\t\tnew_info.order = 10*Q.front().order+i;\n\t\t\t\t\tnew_info.total_length = Q.front().total_length+second_length[i]\n\t\t\t\t\t\t\t\t-match_len[Q.front().order%10][i];\n\t\t\t\t\tQ.push(new_info);\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"%s\\n\",ans_word);\n}\n\nint main() {\n\n\tfor(int i = 0; i < 12; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 63124, "score_of_the_acc": -1.0725, "final_rank": 20 }, { "submission_id": "aoj_2022_2521145", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nvector<string>s;\nint n;\nstring t,ans;\nmain(){\n\twhile(cin>>n,n){\n\t\tset<string>ss;\n\t\tans=\"zzzzzzzzzzzzzzzzzzzzzzzzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";\n\t\tint p=0;\n\t\tvector<int>v;\n\t\ts.clear();\n\t\tr(i,n){\n\t\t\tcin>>t;\n\t\t\tif(!ss.count(t)){\n\t\t\t\ts.push_back(t);\n\t\t\t\tss.insert(t);\n\t\t\t}\n\t\t}\n\t\tr(i,s.size())r(j,s.size())if(i!=j&&s[i].size()>=s[j].size()){\n\t\t\tr(k,s[i].size()){\n\t\t\t\tint sum=0;\n\t\t\t\tif(k+s[j].size()<=s[i].size()){\n\t\t\t\tfor(int l=k;l-k<s[j].size();l++)\n\t\t\t\t\tif(s[i][l]==s[j][l-k])sum++;\n\t\t\t\tif(sum==s[j].size()){\n\t\t\t\t\tv.push_back(j);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}}\n\t\t}\n\t\tsort(v.begin(),v.end());\n\t\tv.erase(unique(v.begin(),v.end()),v.end());\n\t\tr(i,v.size()){\n\t\t\ts.erase(s.begin()+v[i]+p);\n\t\t\tp--;n--;\n\t\t}\n\t\tsort(s.begin(),s.end());\n\t\tdo{\n\t\t\tt=\"\";\n\t\t\tt+=s[0];\n\t\t\tfor(int i=1;i<s.size();i++){\n\t\t\t\tint sum=0;\n\t\t\t\tfor(int j=s[i-1].size()-1;j>=0;j--){\n\t\t\t\t\tint sum2=0,cnt=0;\n\t\t\t\t\tfor(int k=0;;k++){\n\t\t\t\t\t\tif(k+j>=s[i-1].size())break;\n\t\t\t\t\t\tif(s[i][k]==s[i-1][j+k])sum2++;\n\t\t\t\t\t\telse goto L;\n\t\t\t\t\t}\n\t\t\t\t\tif(!cnt)sum=max(sum,sum2);L:;\n\t\t\t\t}\n\t\t\t\tt+=s[i].substr(sum);\n\t\t\t}\n\t\t\tif(ans.size()>t.size())ans=t;\n\t\t\telse if(ans.size()==t.size()) ans=min(ans,t);\n\t\t}while(next_permutation(s.begin(),s.end()));\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 6770, "memory_kb": 3180, "score_of_the_acc": -0.9629, "final_rank": 18 }, { "submission_id": "aoj_2022_2414407", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int,int>;\nusing ll = long long;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)\n#define all(v) v.begin(),v.end()\n//#define debug(x) cerr << #x << \" : \" << x << endl\n#define debug(...)\n\ntemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; }\n// vector\ntemplate<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }\ntemplate<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << \"\\t\" << t; return os << endl; }\n// pair\ntemplate<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << \"<\" << v.first << \", \" << v.second << \">\"; }\n\nconst int INF = 1 << 30;\nconst ll INFL = 1LL << 60;\n\n\nclass Solver {\n public:\n int N;\n vector<string> S;\n bool contain(const string &s, const string &word) {\n rep(i, s.size()) {\n bool ok = true;\n rep(j, word.size()) {\n if(i + j >= s.size() or s[i + j] != word[j]) {\n ok = false; break;\n }\n }\n if(ok) return true;\n }\n return false;\n }\n vector<string> merge(const string &s, const string &word) {\n vector<string> res;\n repeat(i, s.size() - word.size(), s.size()) {\n if(i < 0) continue;\n bool ok = true;\n rep(j, word.size()) if(i + j < s.size() and s[i + j] != word[j]) ok = false;\n if(ok) {\n int same = s.size() - i;\n res.emplace_back(s + word.substr(same));\n }\n }\n res.emplace_back(s + word);\n return res;\n }\n string calc(const string &s, int flg) {\n debug(s);\n string res;\n rep(i, N) if(not (flg & (1 << i))) {\n auto t = merge(s, S[i]);\n for(string &tt : t) { \n string res2 = calc(tt, flg | (1 << i));\n if(res.size() == 0 or res2.size() < res.size()) res = res2;\n else if(res2.size() == res.size()) set_min(res, res2);\n }\n }\n if(res.size() > 0) return res;\n return s;\n }\n \n bool solve() {\n cin >> N;\n if(N == 0) return false; \n S.resize(N); cin >> S;\n vector<string> T;\n rep(i, N) {\n bool ok =true;\n rep(j, N) if(i != j) {\n if(S[i] == S[j]) {\n if(i > j) {\n ok = false;\n break;\n }\n }\n else if(contain(S[j], S[i])) {\n ok = false;\n break;\n } \n }\n if(ok) T.push_back(S[i]);\n }\n debug(N); debug(S); debug(T);\n swap(S, T);\n N = S.size();\n cout << calc(\"\", 0) << endl;\n return true;\n }\n};\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(Solver().solve());\n return 0;\n}", "accuracy": 1, "time_ms": 7040, "memory_kb": 3104, "score_of_the_acc": -1.0001, "final_rank": 19 }, { "submission_id": "aoj_2022_2413694", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <array>\n#include <set>\n#include <map>\n#include <queue>\n#include <tuple>\n#include <unordered_set>\n#include <unordered_map>\n#include <functional>\n#include <cassert>\n#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))\n#define repeat_from(i, m, n) for (int i = (m); (i) < int(n); ++(i))\n#define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i))\n#define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= int(m); --(i))\n#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)\n#define unittest_name_helper(counter) unittest_ ## counter\n#define unittest_name(counter) unittest_name_helper(counter)\n#define unittest __attribute__((constructor)) void unittest_name(__COUNTER__) ()\nusing ll = long long;\nusing namespace std;\ntemplate <class T> inline void setmax(T & a, T const & b) { a = max(a, b); }\ntemplate <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }\ntemplate <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }\ntemplate <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }\n\nbool is_suffix(string const & a, string const & b) {\n if (a.length() > b.length()) return false;\n return b.compare(b.length() - a.length(), a.length(), a) == 0;\n}\nvoid setshort(string & a, string const & b) {\n if (a.empty() or b.length() < a.length() or (a.length() == b.length() and b < a)) {\n a = b;\n }\n}\n\nint main() {\nassert (string(\"puyopuyo\").find(\"yop\") != string::npos);\nassert (string(\"puyopuyo\").find(\"pop\") == string::npos);\nassert (string(\"puyopuyo\").find(\"aaa\") == string::npos);\nassert (string(\"puyo\").find(\"puyopuyo\") == string::npos);\n while (true) {\n // input\n int n; cin >> n;\n if (n == 0) break;\n vector<string> word(n); repeat (i, n) cin >> word[i];\n // solve\n vector<string> dp(1 << n);\n function<void (string const &, int)> go = [&](string const & s, int used) {\nrepeat (i, n) if (used & (1 << i)) {\nassert (s.find(word[i]) != string::npos);\n}\n repeat (i, n) if (not (used & (1 << i))) {\n if (s.find(word[i]) != string::npos) {\n used |= 1 << i;\n }\n }\n setshort(dp[used], s);\n repeat (i, n) if (not (used & (1 << i))) {\n repeat_from (sep, 1, word[i].length()) {\n if (is_suffix(word[i].substr(0, sep), s)) {\n go(s + word[i].substr(sep), used | (1 << i));\n }\n }\n }\n };\n repeat (i, n) {\n go(word[i], 1 << i);\n }\nrepeat (a, 1 << n) if (not dp[a].empty()) {\nrepeat (i, n) if (a & (1 << i)) {\nassert (dp[a].find(word[i]) != string::npos);\n}\n}\n// repeat (iteration, 3)\n repeat (a, 1 << n) if (not dp[a].empty()) {\n repeat (b, 1 << n) {\n if ((b | a) == a) {\n setshort(dp[b], dp[a]);\n }\n }\n }\n repeat_from (a, 1, 1 << n) {\n assert (not dp[a].empty());\n repeat (b, 1 << n) if (not dp[b].empty()) {\n if (not (a & b)) {\n setshort(dp[a | b], dp[a] + dp[b]);\n setshort(dp[a | b], dp[b] + dp[a]);\n }\n }\n }\nrepeat (a, 1 << n) if (not dp[a].empty()) {\nrepeat (i, n) if (a & (1 << i)) {\nassert (dp[a].find(word[i]) != string::npos);\n}\n}\n // output\nrepeat (i, n) {\nassert (dp[(1 << n) - 1].find(word[i]) != string::npos);\n}\n cout << dp[(1 << n) - 1] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3100, "score_of_the_acc": -0.0114, "final_rank": 3 }, { "submission_id": "aoj_2022_2142742", "code_snippet": "#include<bits/stdc++.h>\n#define MOD 1000000007\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3f\n#define EPS (1e-10)\n#define rep(i,n)for(int i=0;i<n;i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int>P;\n\nstring s[10];\nstring dp[1 << 10][10];\nint main() {\n\tint n;\n\tstring inf; rep(i, 100)inf += 'z';\n\twhile (scanf(\"%d\", &n), n) {\n\t\trep(i, n)cin >> s[i];\n\t\trep(i, 1 << n)rep(j, n)dp[i][j] = inf;\n\t\trep(i, n)dp[1 << i][i] = s[i];\n\t\trep(i, 1 << n) {\n\t\t\trep(j, n) {\n\t\t\t\tif (!(i >> j & 1))continue;\n\t\t\t\tif (dp[i][j] == inf)continue;\n\t\t\t\trep(k, n) {\n\t\t\t\t\tif (i >> k & 1)continue;\n\t\t\t\t\tstring t = dp[i | 1 << k][k];\n\t\t\t\t\tstring p = dp[i][j];\n\t\t\t\t\tint d = s[k].size();\n\t\t\t\t\twhile ((int)(p + s[k].substr(d)).find(s[k]) == -1)d--;\n\t\t\t\t\tp += s[k].substr(d);\n\t\t\t\t\tif (t.size() > p.size())dp[i | 1 << k][k] = p;\n\t\t\t\t\telse if (t.size() == p.size())dp[i | 1 << k][k] = min(dp[i | 1 << k][k], p);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring ans = inf;\n\t\trep(i, n) {\n\t\t\tstring t = dp[(1 << n) - 1][i];\n\t\t\tif (ans.size() > t.size())ans = t;\n\t\t\telse if (ans.size() == t.size())ans = min(ans, t);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3700, "score_of_the_acc": -0.047, "final_rank": 7 }, { "submission_id": "aoj_2022_2137607", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\nusing namespace std;\nint x[14][14], n; string S[14];\nstring dp[14][16384];\nbool erased[14]; vector<int>vec;\nint SSS(string S, string T) {\n\tfor (int i = min(S.size(), T.size()); i >= 0; i--) {\n\t\tif (S.substr(S.size() - i, i) == T.substr(0, i))return i;\n\t}\n}\nint main() {\n\twhile (true) {\n\t\tcin >> n; vec.clear(); if (n == 0)break; for (int i = 0; i < n; i++)cin >> S[i];\n\t\tfor (int i = 0; i < 14; i++) {\n\t\t\terased[i] = false;\n\t\t\tfor (int j = 0; j < 16384; j++) { dp[i][j] = \"$\"; }\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (S[i].size() > S[j].size()) {\n\t\t\t\t\tfor (int k = 0; k <= S[i].size() - S[j].size(); k++) {\n\t\t\t\t\t\tif (S[i].substr(k, S[j].size()) == S[j])erased[j] = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; i++) { if (erased[i] == false)vec.push_back(i); }\n\t\tn = vec.size(); for (int i = 0; i < n; i++)S[i] = S[vec[i]];\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (i != j)x[i][j] = SSS(S[i], S[j]);\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; i++) { dp[i][(1 << i)] = S[i]; }\n\t\tfor (int j = 0; j < (1 << n); j++) {\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tif (dp[i][j] == \"$\")continue;\n\t\t\t\tint bit[14]; for (int k = 0; k < n; k++)bit[k] = (j / (1 << k)) % 2;\n\t\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\t\tif (bit[k] == 1)continue;\n\t\t\t\t\tstring W1 = dp[i][j] + S[k].substr(x[i][k], S[k].size() - x[i][k]);\n\t\t\t\t\tif (dp[k][j + (1 << k)] == \"$\") { dp[k][j + (1 << k)] = W1; }\n\t\t\t\t\tif (dp[k][j + (1 << k)].size() > W1.size()) { dp[k][j + (1 << k)] = W1; }\n\t\t\t\t\tif (dp[k][j + (1 << k)].size() == W1.size() && dp[k][j + (1 << k)] > W1) { dp[k][j + (1 << k)] = W1; }\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring minx = dp[0][(1 << n) - 1];\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (minx.size() > dp[i][(1 << n) - 1].size()) { minx = dp[i][(1 << n) - 1]; }\n\t\t\tif (minx.size() == dp[i][(1 << n) - 1].size() && minx > dp[i][(1 << n) - 1]) { minx = dp[i][(1 << n) - 1]; }\n\t\t}\n\t\tcout << minx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 15836, "score_of_the_acc": -0.2506, "final_rank": 12 }, { "submission_id": "aoj_2022_2085814", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef long long ll;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n\ntypedef pair<int,int> pi;\n\nint n;\nvector<string> s,t;\nint T;\n\nconst int INF=12345678;\nint dp[15][1<<15];\npi par[15][1<<15];\n\n// i,j???????????????????????±?????¨???????????????\nint com[15][15];\n\nstring make_string(int now, int mask)\n{\n pi p=par[now][mask];\n if(p.fi==-1) return t[now];\n return make_string(p.fi,p.se)+t[now].substr(com[p.fi][now]);\n}\n\nint main()\n{\n cin.tie(0);ios::sync_with_stdio(false);\n\n while(cin >>n,n)\n {\n s=vector<string>(n);\n rep(i,n) cin >>s[i];\n\n vector<bool> use(n,true);\n rep(i,n)rep(j,n)\n {\n if(i==j) continue;\n\n int I=s[i].size(), J=s[j].size();\n if(I>J) continue;\n\n if(I==J)\n {\n if(s[i]==s[j] && i<j) use[i]=false;\n }\n else\n {\n rep(k,J-I+1)\n {\n if(s[i]==s[j].substr(k,I))\n {\n // cout <<s[i]<<\" \"<<s[j].substr(k,I)<<endl;\n use[i]=false;\n }\n }\n }\n }\n\n t.clear();\n rep(i,n) if(use[i]) t.pb(s[i]);\n T=t.size();\n // printf(\"T=%d\\n\", T);\n\n\n memset(com,0,sizeof(com));\n rep(i,T)rep(j,T)\n {\n if(i==j) continue;\n int I=t[i].size(), J=t[j].size();\n\n // cout <<i<<\" \"<<j<<endl;\n // cout <<t[i] << \" \"<<t[j]<<endl;\n rep(k,min(I,J))\n {\n int len=k+1;\n // cout <<\"len= \"<<len;\n // cout <<t[i].substr(I-len,len)<<\" \"<<t[j].substr(0,len)<<'\\n';\n if(t[i].substr(I-len,len)==t[j].substr(0,len)) com[i][j]=len;\n }\n\n }\n\n fill(dp[0],dp[15],INF);\n\n // ??????\n rep(i,T)\n {\n dp[i][1<<i]=t[i].size();\n par[i][1<<i]=pi(-1,-1);\n }\n\n for(int mask=1; mask<(1<<T); ++mask)\n {\n rep(before,T)if(mask>>before&1)\n {\n rep(i,T)if(!(mask>>i&1))\n {\n int add=t[i].size()-com[before][i];\n if(dp[i][mask+(1<<i)]>dp[before][mask]+add)\n {\n dp[i][mask+(1<<i)]=dp[before][mask]+add;\n par[i][mask+(1<<i)]=pi(before,mask);\n }\n else if(dp[i][mask+(1<<i)]==dp[before][mask]+add)\n {\n string old_s=make_string(i,mask+(1<<i));\n string new_s=make_string(before,mask)+t[i].substr(com[before][i]);\n\n if(new_s<old_s) par[i][mask+(1<<i)] = pi(before,mask);\n }\n }\n }\n }\n\n int min_len=INF;\n rep(i,T)\n {\n // printf(\" dp? %d\\n\", dp[i][(1<<T)-1]);\n min_len=min(min_len, dp[i][(1<<T)-1]);\n }\n string ans=\"#\";\n rep(i,T)\n {\n if(dp[i][(1<<T)-1]==min_len)\n {\n if(ans==\"#\") ans=make_string(i,(1<<T)-1);\n else ans=min(ans,make_string(i,(1<<T)-1));\n }\n }\n\n // cout << \"Scenario #\" << sce+1 << '\\n';\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 5236, "score_of_the_acc": -0.0441, "final_rank": 6 }, { "submission_id": "aoj_2022_2083338", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n// does s contain t?\n/*\nbool contain(const string& s, const string& t)\n{\n if(s.size() < t.size()) return false;\n int idx = 0;\n bool flag = false;\n for(int i = 0; i < t.size(); i++) {\n flag = false;\n for(int j = idx; j < s.size(); j++) {\n if(t[i] == s[j]) idx = j+1, flag = true;\n if(flag) break;\n }\n if(!flag) break;\n }\n return flag;\n}\n*/\n\nbool cmp(const string& left, const string& right)\n{\n if(left.size() != right.size()) return left.size() < right.size();\n for(int i = 0; i < left.size(); i++) {\n if(left[i] != right[i]) return left[i] < right[i];\n }\n return false;\n}\n\n// additional string to let s contain t.\n/*\nstring addstr(const string& s, const string& t)\n{\n int idx = 0;\n for(int i = 0; i < t.size(); i++) {\n bool flag = false;\n for(int j = idx; j < s.size(); j++) {\n if(t[i] == s[j]) idx = j+1, flag = true;\n if(flag) break;\n }\n if(!flag) return t.substr(i);\n }\n return \"\";\n}\n*/\nstring addstr(string s, string t)\n{\n for(int i = 0; i < s.size(); i++) {\n if(s.substr(i) == t.substr(0, s.size()-i)) return t.substr(s.size()-i);\n }\n return t;\n}\n\nstring inf()\n{\n string ret = \"\";\n for(int i = 0; i < 1000; i++) ret += \"~\";\n return ret;\n}\n\nint main()\n{\n int N;\n while(cin >> N, N) {\n vector<string> S(N);\n for(int i = 0; i < N; i++) cin >> S[i];\n sort(S.begin(), S.end(), cmp);\n S.erase(unique(S.begin(), S.end()), S.end());\n\n vector<string> T;\n for(int i = 0; i < S.size(); i++) {\n bool flag = false;\n for(int j = i+1; j < S.size(); j++) {\n\tif(S[j].find(S[i]) != string::npos) flag = true;\n\tif(flag) break;\n }\n if(!flag) T.push_back(S[i]);\n }\n \n int M = T.size();\n string add[11][11];\n for(int i = 0; i < M; i++) {\n for(int j = 0; j < M; j++) {\n\tadd[i][j] = addstr(T[i], T[j]);\n }\n }\n \n string dp[1<<11][11];\n fill(dp[0], dp[1<<11], inf());\n for(int i = 0; i < M; i++) dp[1<<i][i] = T[i];\n \n string ans = inf();\n for(int i = 0; i < 1<<M; i++) {\n for(int j = 0; j < M; j++) {\n\tif(dp[i][j] == inf()) continue;\n\tif(i == (1<<M)-1) {\n\t ans = min(ans, dp[i][j], cmp);\n\t}\n\tfor(int k = 0; k < M; k++) {\n\t if((i >> k) & 1) continue;\n\t dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + add[j][k], cmp);\n\t}\n }\n }\n \n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3508, "score_of_the_acc": -0.0708, "final_rank": 8 }, { "submission_id": "aoj_2022_2053451", "code_snippet": "#include <string>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nint n; vector<string> s;\nint main() {\n\twhile (cin >> n, n) {\n\t\ts.resize(n);\n\t\tfor (int i = 0; i < n; i++) cin >> s[i];\n\t\tvector<int> flag(n, 0); // bit / i -> j\n\t\tvector<vector<int> > cost(n, vector<int>(n)); // i -> j\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (s[i].find(s[j]) != string::npos) {\n\t\t\t\t\tflag[i] |= 1 << j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tfor (int k = 0; k <= s[i].size(); k++) {\n\t\t\t\t\tstring ret = s[i].substr(0, k) + s[j];\n\t\t\t\t\tif (ret.substr(0, s[i].size()) == s[i]) {\n\t\t\t\t\t\tcost[i][j] = ret.size() - s[i].size();\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<vector<string> > dp(1 << n, vector<string>(n, string(101, 'z')));\n\t\tdp[0][0] = \"\";\n\t\tfor (int i = 1; i < 1 << n; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (!(i & (1 << j))) continue;\n\t\t\t\tint p = i - (1 << j);\n\t\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\t\tstring con = (p != 0 ? s[j].substr(s[j].size() - cost[k][j], cost[k][j]) : s[j]);\n\t\t\t\t\tif (dp[i][j].size() > dp[p][k].size() + con.size()) dp[i][j] = dp[p][k] + con;\n\t\t\t\t\telse if (dp[i][j].size() == dp[p][k].size() + con.size() && dp[i][j] > dp[p][k] + con) dp[i][j] = dp[p][k] + con;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring ret(101, 'z');\n\t\tfor (int i = 1; i < 1 << n; i++) {\n\t\t\tint bit = i;\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (i & (1 << j)) bit |= flag[j];\n\t\t\t}\n\t\t\tif (bit == (1 << n) - 1) {\n\t\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\t\tif (ret.size() > dp[i][j].size()) ret = dp[i][j];\n\t\t\t\t\telse if (ret.size() == dp[i][j].size() && ret > dp[i][j]) ret = dp[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3584, "score_of_the_acc": -0.0152, "final_rank": 4 }, { "submission_id": "aoj_2022_2042733", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long // <-----!!!!!!!!!!!!!!!!!!!\n\n#define rep(i,n) for (int i=0;i<(n);i++)\n#define rep2(i,a,b) for (int i=(a);i<(b);i++)\n#define rrep(i,n) for (int i=(n)-1;i>=0;i--)\n#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define printV(v) for(auto x : v){cout << x << \" \";} cout << endl\n#define printVS(vs) for(auto x : vs){cout << x << endl;}\n#define printVV(vv) for(auto v : vv){for(auto&& x : v){cout << x << \" \";}cout << endl;}\n#define printP(p) cout << p.first << \" \" << p.second << endl\n#define printVP(vp) for(auto p : vp) printP(p);\n\ntypedef long long ll;\ntypedef pair<int, int> Pii;\ntypedef tuple<int, int, int> TUPLE;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<Pii> vp;\ntypedef vector<vector<int>> Graph;\nconst int inf = 1e9;\nconst int mod = 1e9 + 7;\n\nint n;\nvector<string> s;\nvvi d;\nvvi dp;\n\nint common(string s, string t) {\n for (int k = min(s.size(), t.size()); k >= 0; k--) {\n if (s.substr((int)s.size() - k) == t.substr(0, k)) {\n return k;\n }\n }\n}\n\nstring concat(string s, string t) {\n int k = common(s, t);\n return s + t.substr(k);\n}\n\nstring mymin(string s, string t) {\n if (s.size() < t.size()) return s;\n if (s.size() > t.size()) return t;\n return s < t ? s : t;\n}\n\nstring dfs(int state, int j, string str) {\n if (__builtin_popcount(state) == 1) return concat(s[j], str);\n\n string ret(101, 'z');\n rep(i, n) {\n if (!((state >> i) & 1)) continue;\n if (dp[state - (1 << j)][i] + d[i][j] == dp[state][j]) {\n ret = mymin(ret, dfs(state - (1 << j), i, concat(s[i], str)));\n }\n }\n\n return ret;\n}\n\n\n\nsigned main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n\n int t = 0;\n while (cin >> n, n) {\n s.clear();\n s.resize(n);\n rep(i, n) cin >> s[i];\n\n vector<bool> alive(n, true);\n rep(i, n) {\n rep2(j, i + 1, n) {\n if (s[i] == s[j]) {\n alive[j] = false;\n }\n }\n }\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n if (s[i] != s[j] && s[i].find(s[j]) != string::npos) {\n alive[j] = false;\n }\n }\n }\n\n {\n vector<string> t;\n rep(i, n) {\n if (alive[i]) {\n t.emplace_back(s[i]);\n }\n }\n s = t;\n n = s.size();\n }\n\n d.clear();\n d.resize(n, vi(n));\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n d[i][j] = common(s[i], s[j]);\n }\n }\n\n dp.clear();\n dp.resize(1 << n, vi(n, -1));\n rep(i, n) {\n dp[1 << i][i] = 0;\n }\n rep(state, 1 << n) {\n rep(i, n) {\n if (dp[state][i] == -1) continue;\n rep(j, n) {\n if ((state >> j) & 1) continue;\n dp[state | (1 << j)][j] = max(dp[state | (1 << j)][j], dp[state][i] + d[i][j]);\n }\n }\n }\n\n int mx = *max_element(all(dp[(1 << n) - 1]));\n string ans(101, 'z');\n rep(j, n) {\n if (dp[(1 << n) - 1][j] == mx) {\n ans = mymin(ans, dfs((1 << n) - 1, j, s[j]));\n }\n }\n cout << ans << endl;\n }\n\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3272, "score_of_the_acc": -0.128, "final_rank": 11 }, { "submission_id": "aoj_2022_2042729", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long // <-----!!!!!!!!!!!!!!!!!!!\n\n#define rep(i,n) for (int i=0;i<(n);i++)\n#define rep2(i,a,b) for (int i=(a);i<(b);i++)\n#define rrep(i,n) for (int i=(n)-1;i>=0;i--)\n#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define printV(v) for(auto x : v){cout << x << \" \";} cout << endl\n#define printVS(vs) for(auto x : vs){cout << x << endl;}\n#define printVV(vv) for(auto v : vv){for(auto&& x : v){cout << x << \" \";}cout << endl;}\n#define printP(p) cout << p.first << \" \" << p.second << endl\n#define printVP(vp) for(auto p : vp) printP(p);\n\ntypedef long long ll;\ntypedef pair<int, int> Pii;\ntypedef tuple<int, int, int> TUPLE;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<Pii> vp;\ntypedef vector<vector<int>> Graph;\nconst int inf = 1e9;\nconst int mod = 1e9 + 7;\n\nint n;\nvector<string> s(n);\nvvi d(n, vi(n));\nvvi dp(1 << n, vi(n, -1));\n\nint common(string s, string t) {\n for (int k = min(s.size(), t.size()); k >= 0; k--) {\n if (s.substr((int)s.size() - k) == t.substr(0, k)) {\n return k;\n }\n }\n}\n\nstring concat(string s, string t) {\n int k = common(s, t);\n return s + t.substr(k);\n}\n\nstring mymin(string s, string t) {\n if (s.size() < t.size()) return s;\n if (s.size() > t.size()) return t;\n return s < t ? s : t;\n}\n\nstring dfs(int state, int j, string str) {\n // cout << state << \" \" << j << \" \" << str << endl;\n // if (state == 0) return str;\n if (__builtin_popcount(state) == 1) return concat(s[j], str);\n\n string ret(101, 'z');\n rep(i, n) {\n if (!((state >> i) & 1)) continue;\n if (dp[state - (1 << j)][i] + d[i][j] == dp[state][j]) {\n ret = mymin(ret, dfs(state - (1 << j), i, concat(s[i], str)));\n }\n }\n\n return ret;\n}\n\n\n\nsigned main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n\n int t = 0;\n while (cin >> n, n) {\n s.clear();\n s.resize(n);\n rep(i, n) cin >> s[i];\n\n // cout << t++ << endl;\n // printVS(s);\n\n vector<bool> alive(n, true);\n rep(i, n) {\n rep2(j, i + 1, n) {\n if (s[i] == s[j]) {\n alive[j] = false;\n }\n }\n }\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n if (s[i] != s[j] && s[i].find(s[j]) != string::npos) {\n alive[j] = false;\n }\n }\n }\n\n {\n vector<string> t;\n rep(i, n) {\n if (alive[i]) {\n t.emplace_back(s[i]);\n }\n }\n s = t;\n n = s.size();\n }\n\n // cout << n << endl;\n // printVS(s);\n\n d.clear();\n d.resize(n, vi(n));\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n d[i][j] = common(s[i], s[j]);\n }\n }\n // printVV(d);\n // cout << endl;\n\n dp.clear();\n dp.resize(1 << n, vi(n, -1));\n rep(i, n) {\n dp[1 << i][i] = 0;\n }\n rep(state, 1 << n) {\n rep(i, n) {\n if (dp[state][i] == -1) continue;\n rep(j, n) {\n if ((state >> j) & 1) continue;\n dp[state | (1 << j)][j] = max(dp[state | (1 << j)][j], dp[state][i] + d[i][j]);\n }\n }\n }\n // printVV(dp);\n // cout << endl;\n\n int mx = *max_element(all(dp[(1 << n) - 1]));\n string ans(101, 'z');\n rep(j, n) {\n if (dp[(1 << n) - 1][j] == mx) {\n ans = mymin(ans, dfs((1 << n) - 1, j, s[j]));\n }\n }\n // cout << \"* ans = \" << ans << endl;\n cout << ans << endl;\n }\n\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3248, "score_of_the_acc": -0.1276, "final_rank": 10 } ]
aoj_2027_cpp
Problem E: Reading a Chord In this problem, you are required to write a program that enumerates all chord names for given tones. We suppose an ordinary scale that consists of the following 12 tones: C, C # , D, D # , E, F, F # , G, G # , A, A # , B Two adjacent tones are different by a half step; the right one is higher. Hence, for example, the tone G is higher than the tone E by three half steps. In addition, the tone C is higher than the tone B by a half step. Strictly speaking, the tone C of the next octave follows the tone B, but octaves do not matter in this problem. A chord consists of two or more different tones, and is called by its chord name . A chord may be represented by multiple chord names, but each chord name represents exactly one set of tones. In general, a chord is represented by a basic chord pattern ( base chord ) and additional tones ( tension ) as needed. In this problem, we consider five basic patterns as listed below, and up to one additional tone. Figure 1: Base chords (only shown those built on C) The chords listed above are built on the tone C, and thus their names begin with C. The tone that a chord is built on (the tone C for these chords) is called the root of the chord. A chord specifies its root by an absolute tone, and its other components by tones relative to its root. Thus we can obtain another chord by shifting all components of a chord. For example, the chord name D represents a chord consisting of the tones D, F # and A, which are the shifted-up tones of C, E and G (which are components of the chord C) by two half steps. An additional tone, a tension, is represented by a number that may be preceding a plus or minus sign, and designated parentheses after the basic pattern. The figure below denotes which number is used to indicate each tone. Figure 2: Tensions for C chords For example, C(9) represents the chord C with the additional tone D, that is, a chord that consists of C, D, E and G. Similarly, C(+11) represents the chord C plus the tone F # . The numbers that specify tensions denote relative tones to the roots of chords like the compo- nents of the chords. Thus change of the root also changes the tones specified by the number, as illustrated below. Figure 3: Tensions for E chords +5 and -5 are the special tensions. They do not indicate to add another tone to the chords, but to sharp (shift half-step up) or flat (shift half-step down) the fifth tone (the tone seven half steps higher than the root). Therefore, for example, C(+5) represents a chord that consists of C, E and G # , not C, E, G and G # . Figure 4 describes the syntax of chords in Backus-Naur Form. Now suppose we find chord names for the tones C, E and G. First, we easily find the chord C consists of the tones C, E and G by looking up the base chord table shown above. Therefore ‘C’ should be printed. We have one more chord name for those tones. The chord Em obviously represents the set of tones E, G and B. Here, if you sharp the tone B, we have ...(truncated)
[ { "submission_id": "aoj_2027_3269182", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nstring root_sts = \"C ,C#,D ,D#,E ,F ,F#,G ,G#,A ,A#,B \";\n\nint get_num(string name) {\n\n\tint k= root_sts.find(name);\n\tstring l= root_sts.substr(0,k);\n\treturn count(l.begin(),l.end(),',');\n}\nbool check(vector<int>a, vector<int>b) {\n\tfor (auto& k : a) {\n\t\tk%=12;\n\t}\n\tfor (auto& k : b) {\n\t\tk %= 12;\n\t}\n\tsort(a.begin(),a.end());\n\tsort(b.begin(),b.end());\n\treturn a==b;\n}\n\nint main() {\n\tint Q;cin>>Q;\n\twhile (Q--) {\n\t\tint N;cin>>N;\n\t\tvector<int>nums(N);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring name;cin>>name;nums[i]=get_num(name);\n\t\t}\n\t\tsort(nums.begin(),nums.end());\n\n\t\tvector<vector<int>>components = {\n\t\t\tvector<int>{0,4,7},\n\t\t\tvector<int>{0,4,7,10},\n\n\t\t\tvector<int>{0,4,7,11},\n\n\t\t\tvector<int>{0,3,7},\n\t\t\tvector<int>{0,3,7,10},\n\n\t\t};\n\t\tvector<string>chord_names = {\n\t\t\t\"\",\n\t\t\t\"7\",\n\t\t\t\"M7\",\n\t\t\t\"m\",\n\t\t\t\"m7\"\n\t\t};\n\t\tvector<string>anss;\n\t\tfor (int root = 0; root < 12; ++root) {\n\t\t\tstring root_name = root_sts.substr(root * 3, 2);\n\n\t\t\tif(root_name[1]==' ')root_name.pop_back();\n\t\t\tfor (int chord = 0; chord < 5; ++chord) {\n\t\t\t\tstring chord_name=chord_names[chord];\n\t\t\t\tstring tension_name;\n\t\t\t\t{\n\t\t\t\t\tvector<string>tension_bases{\n\t\t\t\t\t\t\"5\",\n\t\t\t\t\t\t\"9\"\n\t\t\t\t\t\t,\"11\",\n\t\t\t\t\t\t\"13\"\n\t\t\t\t\t};\n\t\t\t\t\tfor (int tension_part = 0; tension_part < 4; ++tension_part) {\n\t\t\t\t\t\tfor (int plus = -1; plus <= 1; plus++) {\n\t\t\t\t\t\t\tstring minus;\n\t\t\t\t\t\t\tif (plus == 1) {\n\t\t\t\t\t\t\t\tminus=\"+\";\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if (plus == -1) {\n\t\t\t\t\t\t\t\tminus=\"-\";\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tstring tension_name=\"(\"+\n\t\t\t\t\t\t\t\tminus+tension_bases[tension_part]\n\t\t\t\t\t\t\t\t+\")\";\n\t\t\t\t\t\t\tvector<int>voices(components[chord]);\n\t\t\t\t\t\t\tif (tension_part == 0) {\n\t\t\t\t\t\t\t\tif (plus == 0) {\n\t\t\t\t\t\t\t\t\ttension_name=\"\";\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tvoices[2]+=plus;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tif (tension_part == 1) {\n\t\t\t\t\t\t\t\t\tvoices.push_back(2+plus);\n\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse if (tension_part == 2) {\n\t\t\t\t\t\t\t\t\tvoices.push_back(5+plus);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse if (tension_part == 3) {\n\t\t\t\t\t\t\t\t\tvoices.push_back(9+plus);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tfor(auto &vo:voices)vo+=root;\n\t\t\t\t\t\t\tif (check(voices, nums)) {\n\t\t\t\t\t\t\t\tstring ans=root_name\n\t\t\t\t\t\t\t\t\t+chord_name+tension_name;\n\t\t\t\t\t\t\t\tanss.push_back(ans);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (anss.empty()) {\n\t\t\tcout<<\"UNKNOWN\"<<endl;\n\t\t}\n\t\telse {\n\t\t\tfor (int i = 0; i < anss.size(); ++i) {\n\t\t\t\tcout<<anss[i];\n\t\t\t\tif(i==anss.size()-1)cout<<endl;\n\t\t\t\telse cout<<\" \";\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 3016, "score_of_the_acc": -1.6417, "final_rank": 10 }, { "submission_id": "aoj_2027_3118618", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 12\n\nstruct Info{\n\tInfo(int arg_tone_id,int arg_code_id,int arg_tension_op,int arg_tension_value,bool arg_is_tension){\n\t\ttone_id = arg_tone_id;\n\t\tcode_id = arg_code_id;\n\t\ttension_op = arg_tension_op;\n\t\ttension_value = arg_tension_value;\n\t\tis_tension = arg_is_tension;\n\t}\n\tint tone_id,code_id,tension_op,tension_value;\n\tbool is_tension;\n};\n\nint POW[NUM];\nint tension_array[4] = {9,11,13,5};\nint diff_table[3][3] = {{1,2,3},{4,5,6},{8,9,10}};\nchar tone_name[NUM][3] = {\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\nchar code_name[NUM][3] = {\"@\",\"7\",\"M7\",\"m\",\"m7\"};\nvector<int> component[5];\nvector<Info> ANS;\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\n//該当するtoneのidを返却する\nint get_id(char buf[3]){\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tif(strCmp(tone_name[i],buf)){\n\t\t\treturn i;\n\t\t}\n\t}\n\n\treturn -1;\n}\n\nint get_state(bool check[NUM]){\n\n\tint ret = 0;\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tif(check[i]){\n\t\t\tret += POW[i];\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid print_Info(Info info){\n\n\tprintf(\"%s\",tone_name[info.tone_id]);\n\n\tif(info.code_id != 0){\n\n\t\tprintf(\"%s\",code_name[info.code_id]);\n\t}\n\tif(!info.is_tension)return;\n\n\tprintf(\"(\");\n\n\tif(info.tension_op != 0){\n\n\t\tif(info.tension_op > 0){\n\n\t\t\tprintf(\"+\");\n\n\t\t}else{\n\n\t\t\tprintf(\"-\");\n\t\t}\n\t}\n\tprintf(\"%d\",info.tension_value);\n\tprintf(\")\");\n}\n\nvoid dfs(int ans_state){\n\n\tint tmp,tmp_state,tmp_value,diff,tmp_loc;\n\tbool check[12],pre;\n\n\tfor(int base_tone = 0; base_tone < NUM; base_tone++){\n\t\tfor(int cord = 0; cord < 5; cord++){\n\n\t\t\tfor(int i = 0; i < NUM; i++){\n\t\t\t\tcheck[i] = false;\n\t\t\t}\n\t\t\tfor(int i = 0; i < component[cord].size(); i++){\n\t\t\t\tcheck[(base_tone+component[cord][i])%12] = true;\n\t\t\t}\n\n\t\t\ttmp_state = get_state(check);\n\n\t\t\tif(tmp_state == ans_state){\n\t\t\t\tANS.push_back(Info(base_tone,cord,0,0,false)); //テンション無しで見つかればcontinue\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//テンションを全探索\n\t\t\tfor(int t = 0; t < 4; t++){\n\n\t\t\t\ttmp_value = tension_array[t];\n\n\t\t\t\tif(tmp_value != 5){\n\t\t\t\t\t//各数字について、マイナス、ノーマル、プラスの3つ\n\t\t\t\t\tfor(int a = 0; a < 3; a++){\n\n\t\t\t\t\t\tdiff = diff_table[t][a]; //base_toneからの相対位置\n\t\t\t\t\t\ttmp_loc = (base_tone+diff)%12;\n\t\t\t\t\t\tpre = check[tmp_loc];\n\t\t\t\t\t\tcheck[tmp_loc] = true;\n\t\t\t\t\t\ttmp_state = get_state(check);\n\t\t\t\t\t\tif(tmp_state == ans_state){\n\t\t\t\t\t\t\tANS.push_back(Info(base_tone,cord,a-1,tmp_value,true));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcheck[tmp_loc] = pre;\n\t\t\t\t\t}\n\n\t\t\t\t}else{ //tmp_value == 5\n\n\t\t\t\t\t//★base_toneより7半音高い音を上下させる\n\t\t\t\t\ttmp_loc = (base_tone+7)%12;\n\t\t\t\t\tcheck[tmp_loc] = false;\n\n\t\t\t\t\t//マイナス\n\t\t\t\t\tpre = check[(tmp_loc+11)%12];\n\t\t\t\t\tcheck[(tmp_loc+11)%12] = true; //-1の代わりに11\n\t\t\t\t\ttmp_state = get_state(check);\n\t\t\t\t\tif(tmp_state == ans_state){\n\t\t\t\t\t\tANS.push_back(Info(base_tone,cord,-1,tmp_value,true));\n\t\t\t\t\t}\n\t\t\t\t\tcheck[(tmp_loc+11)%12] = pre;\n\n\t\t\t\t\t//プラス\n\t\t\t\t\tpre = check[(tmp_loc+1)%12];\n\t\t\t\t\tcheck[(tmp_loc+1)%12] = true;\n\t\t\t\t\ttmp_state = get_state(check);\n\t\t\t\t\tif(tmp_state == ans_state){\n\t\t\t\t\t\tANS.push_back(Info(base_tone,cord,1,tmp_value,true));\n\t\t\t\t\t}\n\t\t\t\t\tcheck[(tmp_loc+1)%12] = pre;\n\n\t\t\t\t\tcheck[tmp_loc] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\tint num;\n\tchar buf[3];\n\n\tscanf(\"%d\",&num);\n\n\tint tmp_state = 0;\n\n\tfor(int loop = 0; loop < num; loop++){\n\t\tscanf(\"%s\",buf);\n\t\ttmp_state += POW[get_id(buf)];\n\t}\n\n\tANS.clear();\n\n\tdfs(tmp_state);\n\n\tif(ANS.size() == 0){\n\t\tprintf(\"UNKNOWN\\n\");\n\t\treturn;\n\t}\n\n\tprint_Info(ANS[0]);\n\tfor(int i = 1; i < ANS.size(); i++){\n\t\tprintf(\" \");\n\t\tprint_Info(ANS[i]);\n\t}\n\n\tprintf(\"\\n\");\n}\n\n\nint main(){\n\n\t//何もなし\n\tcomponent[0].push_back(0);\n\tcomponent[0].push_back(4);\n\tcomponent[0].push_back(7);\n\n\t//7\n\tcomponent[1].push_back(0);\n\tcomponent[1].push_back(4);\n\tcomponent[1].push_back(7);\n\tcomponent[1].push_back(10);\n\n\t//M7\n\tcomponent[2].push_back(0);\n\tcomponent[2].push_back(4);\n\tcomponent[2].push_back(7);\n\tcomponent[2].push_back(11);\n\n\t//m\n\tcomponent[3].push_back(0);\n\tcomponent[3].push_back(3);\n\tcomponent[3].push_back(7);\n\n\t//m7\n\tcomponent[4].push_back(0);\n\tcomponent[4].push_back(3);\n\tcomponent[4].push_back(7);\n\tcomponent[4].push_back(10);\n\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < NUM; i++){\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\tint num_case;\n\n\tscanf(\"%d\",&num_case);\n\n\tfor(int loop = 0; loop < num_case; loop++){\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3164, "score_of_the_acc": -1.0164, "final_rank": 7 }, { "submission_id": "aoj_2027_2285885", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nint main() {\n string s[12]={\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n string t[5]={\"\",\"7\",\"M7\",\"m\",\"m7\"};\n string r[11]={\"\",\"-9\",\"9\",\"+9\",\"-11\",\"11\",\"+11\",\"\",\"-13\",\"13\",\"+13\"};\n int d[5][4]={{0,4,7,0},{0,4,7,10},{0,4,7,11},{0,3,7,0},{0,3,7,10}};\n int T;\n cin >> T;\n while(T--) {\n int n;\n cin >> n;\n vector<int> a(12,0);\n for(int i=0; i<n; i++) {\n string e;\n cin >> e;\n for(int j=0; j<12; j++) {\n if(s[j]==e) a[j]=1;\n }\n }\n vector<string> ans;\n for(int i=0; i<12; i++) {\n for(int j=0; j<5; j++) {\n vector<int> b(12,0);\n for(int k=0; k<4; k++) b[(i+d[j][k])%12]=1;\n if(a==b) {\n ans.push_back(s[i]+t[j]);\n continue;\n }\n for(int k=1; k<11; k++) {\n if(k==7) continue;\n int x=b[(i+k)%12];\n b[(i+k)%12]=1;\n if(a==b) ans.push_back(s[i]+t[j]+\"(\"+r[k]+\")\");\n b[(i+k)%12]=x;\n }\n b[(i+7)%12]=0;b[(i+6)%12]=1;\n if(a==b) ans.push_back(s[i]+t[j]+\"(-5)\");\n b[(i+6)%12]=0;b[(i+8)%12]=1;\n if(a==b) ans.push_back(s[i]+t[j]+\"(+5)\");\n }\n }\n if(ans.size()) {\n for(int i=0; i<ans.size(); i++) {\n if(i) cout << \" \";\n cout << ans[i];\n }\n cout << endl;\n } else cout << \"UNKNOWN\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3148, "score_of_the_acc": -0.9949, "final_rank": 6 }, { "submission_id": "aoj_2027_2285652", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n string s[12]={\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n string t[5]={\"\",\"7\",\"M7\",\"m\",\"m7\"};\n string r[11]={\"\",\"-9\",\"9\",\"+9\",\"-11\",\"11\",\"+11\",\"\",\"-13\",\"13\",\"+13\"};\n int d[5][4]={{0,4,7,-1},{0,4,7,10},{0,4,7,11},{0,3,7,-1},{0,3,7,10}};\n int T;\n cin >> T;\n while(T--) {\n int n;\n cin >> n;\n vector<int> a(12,0);\n for(int i=0; i<n; i++) {\n string e;\n cin >> e;\n for(int j=0; j<12; j++) {\n if(s[j]==e) {\n a[j]=1;\n break;\n }\n }\n }\n vector<string> ans;\n for(int i=0; i<12; i++) {\n for(int j=0; j<5; j++) {\n vector<int> b(12,0);\n for(int k=0; k<4; k++) {\n if(d[j][k]==-1) break;\n b[(i+d[j][k])%12]=1;\n }\n if(a==b) {\n ans.push_back(s[i]+t[j]);\n continue;\n }\n for(int k=1; k<11; k++) {\n if(k==7) continue;\n int x=b[(i+k)%12];\n b[(i+k)%12]=1;\n if(a==b) ans.push_back(s[i]+t[j]+\"(\"+r[k]+\")\");\n b[(i+k)%12]=x;\n }\n b[(i+7)%12]=0;b[(i+6)%12]=1;\n if(a==b) ans.push_back(s[i]+t[j]+\"(-5)\");\n b[(i+6)%12]=0;b[(i+8)%12]=1;\n if(a==b) ans.push_back(s[i]+t[j]+\"(+5)\");\n }\n }\n if(ans.size()) {\n for(int i=0; i<ans.size(); i++) {\n if(i) cout << \" \";\n cout << ans[i];\n }\n cout << endl;\n } else cout << \"UNKNOWN\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3132, "score_of_the_acc": -0.9899, "final_rank": 5 }, { "submission_id": "aoj_2027_911715", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<sstream>\n\nusing namespace std;\n\nstruct Code{\n string name;\n vector<string> sound;\n};\n\nconst int KIND = 12;\nstring sound[] = {\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n\nvector<string> make_vec(const string& s){\n stringstream ss(s);\n vector<string> res;\n string str;\n while(ss >> str) res.push_back(str);\n return res;\n}\n\nint get_index(const string& s){\n for(int i = 0; i < KIND; i++) if(sound[i] == s) return i;\n return -1;\n}\n\nCode shift(Code c, int n){\n c.name = sound[n] + c.name.substr(1);\n \n vector<string> tmp(c.sound.size());\n for(int i = 0; i < (int)tmp.size(); i++){\n int idx = get_index(c.sound[i]);\n tmp[i] = sound[(idx+n)%KIND];\n }\n sort(tmp.begin(),tmp.end());\n c.sound = tmp;\n return c;\n}\n\nstring to_str(int n){\n ostringstream oss;\n oss << n;\n return oss.str();\n}\n\nvector<string> get_sound(int n){\n if(n == 5) return make_vec(\"F# G G#\");\n if(n == 9) return make_vec(\"C# D D#\");\n if(n == 11) return make_vec(\"E F F#\");\n if(n == 13) return make_vec(\"G# A A#\");\n return make_vec(\"ERROR\");\n}\n\nCode change_tension(Code c, int n, int type){\n vector<string> v = get_sound(n);\n int pos = -1;\n for(int i = 0; i < (int)c.sound.size(); i++)\n for(int j = 0; j < (int)v.size(); j++)\n if(c.sound[i] == v[j]) pos = i;\n \n string op = \"\";\n if(type == 0) op = \"-\";\n if(type == 2) op = \"+\";\n\n c.name += \"(\" + op + to_str(n) + \")\";\n \n if(n == 5){\n if(pos == -1) c.sound.push_back(v[type]);\n else c.sound[pos] = v[type];\n\n }else{\n if(pos == -1 || c.sound[pos] != v[type]) c.sound.push_back(v[type]);\n }\n // if(pos == -1 || c.sound[pos] != v[type]) c.sound.push_back(v[type]);\n\n sort(c.sound.begin(), c.sound.end());\n return c;\n}\n\nvoid print(const vector<Code>& v){\n cout << \"--------- begin -----------\" << endl;\n \n for(int i = 0; i < (int)v.size(); i++){\n cout << v[i].name << endl;\n for(int j = 0; j < (int)v[i].sound.size(); j++) cout << v[i].sound[j] << \" \";\n cout << endl;\n }\n cout << \"--------- end -----------\" << endl;\n}\n\nvector<Code> make(){\n vector<Code> res, tmp;\n Code base[5];\n base[0] = (Code){\"C\", make_vec(\"C E G\")};\n base[1] = (Code){\"C7\",make_vec(\"C E G A#\")};\n base[2] = (Code){\"CM7\", make_vec(\"C E G B\")};\n base[3] = (Code){\"Cm\", make_vec(\"C D# G\")};\n base[4] = (Code){\"Cm7\", make_vec(\"C D# G A#\")};\n\n for(int i = 0; i < 5; i++){\n sort(base[i].sound.begin(), base[i].sound.end());\n res.push_back(base[i]);\n tmp.push_back(base[i]);\n }\n \n int tens[] = {5,9,11,13};\n for(int i = 0; i < 5; i++)\n for(int j = 0; j < 4; j++)\n for(int k = 0; k < 3; k++){\n\tCode nex = change_tension(base[i], tens[j], k);\n\tif(nex.sound != base[i].sound){\n\t res.push_back(nex);\n\t tmp.push_back(nex);\n\t}\n }\n \n for(int i = 0; i < (int)tmp.size(); i++)\n for(int j = 1; j < 12; j++) res.push_back(shift(tmp[i], j));\n \n for(int i = 0; i < (int)res.size(); i++) sort(res[i].sound.begin(), res[i].sound.end());\n return res;\n}\n\nvector<string> solve(const vector<Code>& cand, const vector<string>& v){\n\n vector<string> res;\n for(int i = 0; i < (int)cand.size(); i++)\n if(cand[i].sound == v) res.push_back(cand[i].name);\n return res;\n}\n\nvoid TEST(const vector<Code>& cand){\n\n for(int i = 0; i < (int)cand.size(); i++)\n if(cand[i].name == \"Dm7(9)\"){\n cout << cand[i].name << endl;\n for(int j = 0; j < (int)cand[i].sound.size(); j++)\n\tcout << cand[i].sound[j] << \" \" ;\n cout << endl;\n }\n exit(0);\n}\n\nint main(){\n\n vector<Code> cand = make();\n // TEST(cand);\n \n int T;\n cin >> T;\n while(T--){\n int n;\n cin >> n;\n vector<string> v(n);\n for(int i = 0; i < n; i++) cin >> v[i];\n sort(v.begin(), v.end());\n vector<string> ans = solve(cand, v);\n if(ans.size() == 0) cout << \"UNKNOWN\" << endl;\n else{\n for(int i = 0; i < (int)ans.size(); i++){\n\tif(i) cout << \" \";\n\tcout << ans[i];\n }\n cout << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1380, "score_of_the_acc": -0.4362, "final_rank": 3 }, { "submission_id": "aoj_2027_667266", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <list>\n#include <cmath>\n#include <fstream>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <set>\n#include <map>\n#include <complex>\n#include <iterator>\n#include <cstdlib>\n#include <cstring>\n#include <sstream>\n#include <stack>\n#include <climits>\n#include <deque>\n#include <bitset>\n#include <cassert>\n#include <ctime>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int dy[]={-1,0,1,0},dx[]={0,1,0,-1};\n// adjust problem by problem\nconst double EPS=1e-8;\nconst double PI=acos(-1.0);\n#ifdef __GNUC__\nint popcount(int n){return __builtin_popcount(n);}\nint popcount(ll n){return __builtin_popcountll(n);}\n#endif\n#ifndef __GNUC__\ntemplate<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}\n#endif\ntemplate<class T>int SIZE(T a){return a.size();}\ntemplate<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}\ntemplate<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}\ntemplate<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}\ntemplate<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}\ntemplate<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}\nbool EQ(double a,double b){return abs(a-b)<EPS;}\nvoid fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}\nvector<string> split(string str,char del){\n vector<string> res;\n for(int i=0,s=0;i<SIZE(str);i++){\n if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}\n else if(i==SIZE(str)-1){res.push_back(str.substr(s));}\n }\n return res;\n}\n\n\nint main(){\n map<string,int> toneToIdx;\n string idxToTone[]={\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n // コードの名前とそのcomponent\n for(int i=0;i<12;i++)toneToIdx[idxToTone[i]]=i;\n map<string,set<int> > chords;\n {\n set<int> st;\n st.insert(toneToIdx[\"C\"]);\n st.insert(toneToIdx[\"E\"]);\n st.insert(toneToIdx[\"G\"]);\n chords[\"C\"]=st;\n }\n {\n set<int> st;\n st.insert(toneToIdx[\"C\"]);\n st.insert(toneToIdx[\"E\"]);\n st.insert(toneToIdx[\"G\"]);\n st.insert(toneToIdx[\"A#\"]);\n chords[\"C7\"]=st;\n }\n {\n set<int> st;\n st.insert(toneToIdx[\"C\"]);\n st.insert(toneToIdx[\"E\"]);\n st.insert(toneToIdx[\"G\"]);\n st.insert(toneToIdx[\"B\"]);\n chords[\"CM7\"]=st;\n }\n {\n set<int> st;\n st.insert(toneToIdx[\"C\"]);\n st.insert(toneToIdx[\"D#\"]);\n st.insert(toneToIdx[\"G\"]);\n chords[\"Cm\"]=st;\n }\n {\n set<int> st;\n st.insert(toneToIdx[\"C\"]);\n st.insert(toneToIdx[\"D#\"]);\n st.insert(toneToIdx[\"G\"]);\n st.insert(toneToIdx[\"A#\"]);\n chords[\"Cm7\"]=st;\n }\n\n typedef pair<string,string> pss;\n pss pars[]={pss(\"-9\",\"C#\"),pss(\"9\",\"D\"),pss(\"+9\",\"D#\")\n ,pss(\"-11\",\"E\"),pss(\"11\",\"F\"),pss(\"+11\",\"F#\")\n ,pss(\"-13\",\"G#\"),pss(\"13\",\"A\"),pss(\"+13\",\"A#\")\n ,pss(\"-5\",\"\"),pss(\"+5\",\"\"),pss(\"\",\"\")}; \n int N;\n cin>>N;\n while(N--){\n int M;\n set<int> inSets;\n vector<string> ress;\n cin>>M;\n for(int i=0;i<M;i++){\n string s;\n cin>>s;\n inSets.insert(toneToIdx[s]);\n //cout<<toneToIdx[s]<<\" \";\n }\n //cout<<endl;\n // すべてのtoneをrootに\n for(int i=0;i<12;i++){\n // 5種類のコードを使用\n for(map<string,set<int> >::iterator it=chords.begin();it!=chords.end();it++){\n // 括弧の中身をどうするか\n for(int j=0;j<12;j++){\n string r=idxToTone[i]+it->first.substr(1);\n if(pars[j].first!=\"\")r+=\"(\"+pars[j].first+\")\";\n //cout<<r<<endl;\n set<int> nowSt;\n for(set<int>::iterator iit=it->second.begin();iit!=it->second.end();iit++)\n nowSt.insert((*iit+i)%12);\n if(pars[j].first==\"\"){}\n else if(pars[j].first[1]=='5'){\n // replace\n int tgt=(i+7)%12;\n // assert(it->second.size()==nowSt.size());\n // for(set<int>::iterator iit=nowSt.begin();iit!=nowSt.end();iit++){\n // cout<<*iit<<\" \";\n // }\n // cout<<endl;\n nowSt.erase(tgt);\n if(pars[j].first[0]=='+')nowSt.insert((tgt+1)%12);\n else nowSt.insert((tgt+11)%12);\n }\n // 普通に処理\n else{\n if(nowSt.count((i+toneToIdx[pars[j].second])%12))continue;\n nowSt.insert((i+toneToIdx[pars[j].second])%12);\n }\n if(inSets==nowSt){\n ress.push_back(r);\n }\n }\n }\n }\n if((int)ress.size()==0)cout<<\"UNKNOWN\"<<endl;\n else{\n for(int i=0;i<(int)ress.size();i++){\n cout<<ress[i];\n if(i==(int)ress.size()-1)cout<<endl;\n else cout<<\" \";\n }\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 1232, "score_of_the_acc": -1.3894, "final_rank": 9 }, { "submission_id": "aoj_2027_615742", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nstring tone[12] = {\n \"C\",\n \"C#\",\n \"D\",\n \"D#\",\n \"E\",\n \"F\",\n \"F#\",\n \"G\",\n \"G#\",\n \"A\",\n \"A#\",\n \"B\"\n};\nint to_int(string s){\n REP(i, 12) if(s == tone[i]) return i;\n assert(false);\n return -1;\n}\nstring to_str(int n){\n assert(n >= 0 && n < 12);\n return tone[n];\n}\nstring code_name[5] = {\n \"\",\n \"7\",\n \"M7\",\n \"m\",\n \"m7\"\n};\nint code_tone[][5] = {\n {0, 4, 7, -1, -1},\n {0, 4, 7, 10, -1},\n {0, 4, 7, 11, -1},\n {0, 3, 7, -1, -1},\n {0, 3, 7, 10, -1}\n};\nstring tension_name[12] = {\n \"\",\n \"(-9)\",\n \"(9)\",\n \"(+9)\",\n \"(-11)\",\n \"(11)\",\n \"(+11)\",\n \"\",\n \"(-13)\",\n \"(13)\",\n \"(+13)\",\n \"\",\n};\nstring special_tension[2] = {\n \"(-5)\",\n \"(+5)\"\n};\n\nvector<string> solve(vector<string> tone_str){\n vector<string> answer;\n vector<int> tones;\n REP(i, tone_str.size()) tones.push_back(to_int(tone_str[i]));\n REP(i, tones.size()){\n int offset = tones[i];\n vector<int> relative = tones;\n REP(i, relative.size()) relative[i] = (relative[i] - offset + 12) % 12;\n sort(relative.begin(), relative.end());\n REP(j, 5){\n vector<int> code;\n for(int k = 0; code_tone[j][k] != -1; k++){\n code.push_back(code_tone[j][k]);\n }\n if(code == relative) answer.push_back(to_str(offset) + code_name[j]);\n for(int k = 0; k < 2; k++){\n vector<int> code2 = code;\n code2.erase(remove(code2.begin(), code2.end(), 7)); code2.push_back(7 + (2 * k - 1));\n sort(code2.begin(), code2.end());\n if(code2 == relative) answer.push_back(to_str(offset) + code_name[j] + special_tension[k]);\n }\n for(int k = 0; k < 12; k++) if(tension_name[k] != \"\"){\n vector<int> code2 = code;\n code2.push_back(k);\n sort(code2.begin(), code2.end());\n if(code2 == relative) answer.push_back(to_str(offset) + code_name[j] + tension_name[k]);\n }\n }\n }\n return answer;\n}\n\nint main(){\n int T;\n cin >> T;\n while(T--){\n int R; cin >> R;\n vector<string> tone_str(R);\n REP(i, R) cin >> tone_str[i];\n vector<string> codes = solve(tone_str);\n\n if(codes.empty()) cout << \"UNKNOWN\" << endl;\n\n REP(i, codes.size()){\n cout << codes[i];\n if(i == codes.size() - 1) cout << endl;\n else cout << \" \";\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1240, "score_of_the_acc": -0.4411, "final_rank": 4 }, { "submission_id": "aoj_2027_343031", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nint main() {\n string tones[12] = {\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n int component[5][3] = {{4,7,-1},{4,7,10},{4,7,11},{3,7,-1},{3,7,10}};\n string compo[5] = {\"\",\"7\",\"M7\",\"m\",\"m7\"};\n int tension[12] = {0,1,2,3,4,5,6,-1,1,8,9,10};\n string tens[12] = {\"\",\"(-9)\",\"(9)\",\"(+9)\",\"(-11)\",\"(11)\",\"(+11)\",\"(-5)\",\"(+5)\",\"(-13)\",\"(13)\",\"(+13)\"};\n\n map<set<int>, vector<string> > mp;\n REP(i, 12) {\n REP(j, 5) {\n vector<int> tmp;\n tmp.push_back(i);\n REP(k, 3) {\n if (component[j][k] == -1) break;\n tmp.push_back((i+component[j][k])%12);\n }\n map<set<int>,bool> memo;\n REP(k,12) {\n vector<int> tmp2(tmp);\n if (k == 0);\n else if (k == 7 || k == 8) // +-5\n tmp2[2] = (tmp2[2]+tension[k]+12)%12;\n else\n tmp2.push_back((i+tension[k])%12);\n set<int> tmpset(ALL(tmp2));\n if (memo[tmpset]) continue;\n memo[tmpset] = 1;\n string s = tones[i];\n s += compo[j];\n s += tens[k];\n mp[tmpset].push_back(s);\n // cout << s << endl;\n // FOR(it, tmpset) cout << tones[*it] << \" \";\n // cout << endl;\n }\n }\n }\n map<string, int> mp2;\n REP(i, 12) {\n mp2[tones[i]] = i;\n }\n int N;\n cin >> N;\n while(N--) {\n int n;\n cin >> n;\n set<int> se;\n REP(i,n) {\n string s;\n cin >> s;\n se.insert(mp2[s]);\n }\n vector<string> v = mp[se];\n if (v.size() == 0) {\n cout << \"UNKNOWN\" << endl;\n } else {\n REP(i, v.size()) {\n if (i) cout << \" \";\n cout << v[i];\n }\n cout << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2027_111439", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<map>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define ALL(C) (C).begin(),(C).end()\n\nconst int CONST = 12;\n\nbool have_same(vector<int> &a){\n rep(i,a.size())REP(j,i+1,a.size())if (a[i] == a[j])return true;\n return false;\n}\n\nvoid generate(vector<string> & a,vector<vector<int> > &b){\n vector<int> base[5];\n string opdata[5]={\"\",\"7\",\"M7\",\"m\",\"m7\"};\n base[0].pb(0);base[0].pb(4);base[0].pb(7);\n base[1].pb(0);base[1].pb(4);base[1].pb(7);base[1].pb(10);\n base[2].pb(0);base[2].pb(4);base[2].pb(7);base[2].pb(11);\n base[3].pb(0);base[3].pb(3);base[3].pb(7);\n base[4].pb(0);base[4].pb(3);base[4].pb(7);base[4].pb(10);\n\n vector<int> pat;\n\n string option=\"\";\n string ori[]={\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n string add[]={\"-9\",\"9\",\"+9\",\"-11\",\"11\",\"+11\",\"-13\",\"13\",\"+13\"};\n int addp[]={1,2,3,4,5,6,8,9,10};\n string h[]={\"-5\",\"+5\"};\n int hp[]={11,1};\n \n\n rep(k,5){\n pat=base[k];\n option=opdata[k];\n rep(i,12){ \n vector<int> cpy=pat;\n rep(j,pat.size())cpy[j]=(cpy[j]+i)%CONST;\n a.pb(ori[i]+option);\n b.pb(cpy);\n vector<int> cpy2=cpy;\n cpy2.pb(-1);\n rep(j,9){\n\tcpy2[cpy2.size()-1]=(i+addp[j])%CONST;\n\tif (have_same(cpy2))continue;\n\ta.pb(ori[i]+option+\"(\"+add[j]+\")\");\n\tb.pb(cpy2);\n }\n \n int base=cpy[2];\n rep(j,2){\n\t//cpy[cpy.size()-1]=(base+hp[j])%CONST;\n\tcpy[2]=(base+hp[j])%CONST;\n\ta.pb(ori[i]+option+\"(\"+h[j]+\")\");\n\tb.pb(cpy);\n }\n }\n }\n \n rep(i,a.size()){\n // cout << a[i] << \" \";// << b[i].size() << endl;\n // rep(j,b[i].size())cout << b[i][j]<<\" \";cout <<endl;\n sort(ALL(b[i]));\n }\n}\n\n\nbool is_same_vector(vector<int> &a,vector<int> &b){\n if (a.size() != b.size())return false;\n rep(i,a.size())if (a[i] != b[i])return false;\n /* \n cout << endl;\n cout <<\"tete\"<<endl;\n rep(i,a.size())cout << a[i];cout << endl;\n rep(i,b.size())cout << b[i];cout << endl;\n */ \n return true;\n}\n\nmain(){\n vector<string> a;\n vector<vector<int> > b;\n generate(a,b);\n string ori[]={\"C\",\"C#\",\"D\",\"D#\",\"E\",\"F\",\"F#\",\"G\",\"G#\",\"A\",\"A#\",\"B\"};\n map<string,int> M;\n rep(i,12)M[ori[i]]=i;\n \n int te;\n cin>>te;\n while(te--){\n int cnt = 0;\n int n;\n cin>>n;\n vector<int> in;\n rep(i,n){\n string tmp;cin>>tmp;in.pb(M[tmp]);\n }\n sort(ALL(in));\n \n // rep(k,in.size()){\n rep(i,a.size()){\n\tif (is_same_vector(b[i],in)){\n\t if (cnt)cout << \" \";\n\t cout << a[i];\n\t cnt++;\n\t}\n }\n /* \n int tmp = in[0];\n rep(i,in.size()-1)in[i]=in[i+1];\n in[in.size()-1]=tmp;\n }\n */\n if (cnt == 0)cout << \"UNKNOWN\";\n cout << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2027_69928", "code_snippet": "#include <iostream>\n#include <string>\n#include <map>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n#define MP make_pair\n#define pb push_back\n\nint C[]={0,4,7};\nint C7[]={0,4,7,10};\nint CM7[]={0,4,7,11};\nint Cm[]={0,3,7};\nint Cm7[]={0,3,7,10};\n\nmap<int, string> d;\nmap<string, int> dic;\nmap<int, string> rev;\n\nvector<string> ans;\n\nvoid init()\n{\n\td[1]=\"-9\";\n\td[2]=\"9\";\n\td[3]=\"+9\";\n\td[4]=\"-11\";\n\td[5]=\"11\";\n\td[6]=\"+11\";\n\td[8]=\"-13\";\n\td[9]=\"13\";\n\td[10]=\"+13\";\n\t\n\tdic[\"C\"]\t= 0;\n\tdic[\"C#\"]\t= 1;\n\tdic[\"D\"]\t= 2;\n\tdic[\"D#\"]\t= 3;\n\tdic[\"E\"]\t= 4;\n\tdic[\"F\"]\t= 5;\n\tdic[\"F#\"]\t= 6;\n\tdic[\"G\"]\t= 7;\n\tdic[\"G#\"]\t= 8;\n\tdic[\"A\"]\t= 9;\n\tdic[\"A#\"]\t= 10;\n\tdic[\"B\"]\t= 11;\n\t\n\trev[0]\t= \"C\";\n\trev[1]\t= \"C#\";\n\trev[2]\t= \"D\";\n\trev[3]\t= \"D#\";\n\trev[4]\t= \"E\";\n\trev[5]\t= \"F\";\n\trev[6]\t= \"F#\";\n\trev[7]\t= \"G\";\n\trev[8]\t= \"G#\";\n\trev[9]\t= \"A\";\n\trev[10]\t= \"A#\";\n\trev[11]\t= \"B\";\n}\n\nvoid ps(string s, int orig, string type)\n{\n\tstring pat;\n\n\tpat += rev[orig] + type;\n\tif(!s.empty()) pat += \"(\"+ s + \")\";\n\n\tans.pb(pat);\n}\n\nbool check(vector<int> c, vector<int> p)\n{\n\tif(c.size()!= p.size()) return false;\n\n\tsort(c.begin(), c.end());\n\tsort(p.begin(), p.end());\n\t\n\tfor(int i=0; i<c.size(); i++)\n\t\tif(c[i]!=p[i]) return false;\n\n\treturn true;\n}\n\nvoid ctension(vector<int> c, vector<int> p, int orig, string type)\n{\n\tfor(int i=1; i<=10; i++)\n\t{\n\t\tif(i==7) continue;\n\t\tvector<int> t(c);\n\t\tt.pb((orig+i)%12);\n\t\tif(check(t,p)) ps(d[i], orig, type);\n\t}\n\n\tvector<int>::iterator it=find(c.begin(), c.end(), (orig+7)%12);\n\t\n\t*it = ((*it)+11)%12;\n\n\tif(check(c,p)) ps(\"-5\", orig, type);\n\n\t*it = ((*it)+2)%12;\n\tif(check(c,p)) ps(\"+5\", orig, type);\n}\n\nvoid createC(vector<int> p)\n{\n\tvector<int> c;\n\tfor(int i=0; i<3; i++)\n\t\tc.pb(C[i]);\n\n\tfor(int i=0; i<12; i++)\n\t{\n\t\tif(check(c,p)) ps(\"\",i,\"\");\n\t\tctension(c,p,i,\"\");\n\t\t\n\t\tfor(int j=0; j<3; j++)\n\t\t\tc[j]=(c[j]+1)%12;\n\t}\n}\n\nvoid createC7(vector<int> p)\n{\n\n\tvector<int> c;\n\tfor(int i=0; i<4; i++)\n\t\tc.pb(C7[i]);\n\n\tfor(int i=0; i<12; i++)\n\t{\n\t\tif(check(c,p)) ps(\"\",i,\"7\");\n\t\tctension(c,p,i,\"7\");\n\t\t\n\t\tfor(int j=0; j<4; j++)\n\t\t\tc[j]=(c[j]+1)%12;\n\t}\n}\n\nvoid createCM7(vector<int> p)\n{\n\tvector<int> c;\n\tfor(int i=0; i<4; i++)\n\t\tc.pb(CM7[i]);\n\n\tfor(int i=0; i<12; i++)\n\t{\n\t\tif(check(c,p)) ps(\"\",i,\"M7\");\n\t\tctension(c,p,i,\"M7\");\n\t\t\n\t\tfor(int j=0; j<4; j++)\n\t\t\tc[j]=(c[j]+1)%12;\n\t}\n}\nvoid createCm(vector<int> p)\n{\n\tvector<int> c;\n\tfor(int j=0; j<3; j++)\n\t\tc.pb(Cm[j]);\n\n\tfor(int i=0; i<12; i++)\n\t{\n\t\tif(check(c,p)) ps(\"\",i,\"m\");\n\t\tctension(c,p,i,\"m\");\n\t\t\n\t\tfor(int j=0; j<3; j++)\n\t\t\tc[j]=(c[j]+1)%12;\n\n\t}\n\t\n}\n\nvoid createCm7(vector<int> p)\n{\n\tvector<int> c;\n\tfor(int j=0; j<4; j++)\n\t\tc.pb(Cm7[j]);\n\n\tfor(int i=0; i<12; i++)\n\t{\n\t\tif(check(c,p)) ps(\"\",i,\"m7\");\n\t\tctension(c,p,i,\"m7\");\n\t\t\n\t\tfor(int j=0; j<4; j++)\n\t\t\tc[j]=(c[j]+1)%12;\n\n\t}\n}\n\nvoid create(vector<int> p)\n{\n\tcreateC(p);\n\tcreateC7(p);\n\tcreateCM7(p);\n\tcreateCm(p);\n\tcreateCm7(p);\n}\n\nint main()\n{\n\tint N,M;\n\tcin >> N;\n\t\n\tinit();\n\t\n\twhile(N--)\n\t{\n\t\tans.clear();\n\n\t\tvector<int> n;\n\t\tcin >> M;\n\t\twhile(M--)\n\t\t{\n\t\t\tstring s;\n\t\t\tcin >> s;\n\t\t\tn.push_back(dic[s]);\n\t\t}\n\t\t\n\t\tcreate(n);\n\n\t\tif(ans.size()!=0)\n\t\t{\n\t\t\tfor(int i=0; i<ans.size(); i++)\n\t\t\t{\n\t\t\t\tif(i!=0) cout << \" \";\n\t\t\t\tcout << ans[i];\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout << \"UNKNOWN\" << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 936, "score_of_the_acc": -1.0663, "final_rank": 8 } ]
aoj_2029_cpp
Problem I: Light The Room You are given plans of rooms of polygonal shapes. The walls of the rooms on the plans are placed parallel to either x -axis or y -axis. In addition, the walls are made of special materials so they reflect light from sources as mirrors do, but only once. In other words, the walls do not reflect light already reflected at another point of the walls. Now we have each room furnished with one lamp. Walls will be illuminated by the lamp directly or indirectly. However, since the walls reflect the light only once, some part of the walls may not be illuminated. You are requested to write a program that calculates the total length of unilluminated part of the walls. Figure 10: The room given as the second case in Sample Input Input The input consists of multiple test cases. The first line of each case contains a single positive even integer N (4 ≤ N ≤ 20), which indicates the number of the corners. The following N lines describe the corners counterclockwise. The i-th line contains two integers x i and y i , where ( x i , y i ) indicates the coordinates of the i -th corner. The last line of the case contains x' and y' , where ( x' , y' ) indicates the coordinates of the lamp. To make the problem simple, you may assume that the input meets the following conditions: All coordinate values are integers not greater than 100 in their absolute values. No two walls intersect or touch except for their ends. The walls do not intersect nor touch each other. The walls turn each corner by a right angle. The lamp exists strictly inside the room off the wall. The x-coordinate of the lamp does not coincide with that of any wall; neither does the y-coordinate. The input is terminated by a line containing a single zero. Output For each case, output the length of the unilluminated part in one line. The output value may have an arbitrary number of decimal digits, but may not contain an error greater than 10 -3 . Sample Input 4 0 0 2 0 2 2 0 2 1 1 6 2 2 2 5 0 5 0 0 5 0 5 2 1 4 0 Output for the Sample Input 0.000 3.000
[ { "submission_id": "aoj_2029_3168624", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-6;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectLS(const L& l, const L& s){\n return cross(l[1]-l[0], s[0]-l[0])*\n cross(l[1]-l[0], s[1]-l[0]) < EPS;\n}\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool strictItsSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) < 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) < 0 );\n}\n\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L& l, const P& p) {\n return p + 2.0*(projection(l, p) -p);\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n VP p(n);\n for(int i=0; i<n; i++){\n int x,y;\n cin >> x >> y;\n p[i] = P(x, y);\n }\n int sx,sy;\n cin >> sx >> sy;\n P source(sx, sy);\n\n vector<L> edge(n);\n vector<VP> cp(n);\n for(int i=0; i<n; i++){\n edge[i] = L(p[i], p[(i+1)%n]);\n cp[i].push_back(edge[i][0]);\n cp[i].push_back(edge[i][1]);\n }\n\n for(int v=0; v<n; v++){\n L ray(source, p[v]);\n for(int wr=0; wr<n; wr++){\n if(!isParallel(ray, edge[wr]) && intersectLS(ray, edge[wr])){\n P cpll = crosspointLL(ray, edge[wr]);\n //直接頂点を通る\n cp[wr].push_back(cpll);\n //その後反射する\n P ref = reflection(edge[wr], source);\n L ray2(ref, cpll);\n for(int wt=0; wt<n; wt++){\n if(!isParallel(ray2, edge[wt]) && intersectLS(ray2, edge[wt])){\n cp[wt].push_back(crosspointLL(ray2, edge[wt]));\n }\n }\n }\n }\n\n //反射して頂点を通る\n for(int wr=0; wr<n; wr++){\n P s = reflection(edge[wr], source);\n L ray(s, p[v]);\n for(int wt=0; wt<n; wt++){\n if(!isParallel(ray, edge[wt]) && intersectLS(ray, edge[wt])){\n cp[wt].push_back(crosspointLL(ray, edge[wt]));\n }\n }\n }\n }\n for(int i=0; i<n; i++){\n sort(cp[i].begin(), cp[i].end());\n cp[i].erase(unique(cp[i].begin(), cp[i].end()), cp[i].end());\n }\n\n double ans = 0;\n for(int i=0; i<n; i++){\n for(int j=0; j<(int)cp[i].size()-1; j++){\n P mid = (cp[i][j] +cp[i][j+1]) /2.0;\n bool light = false;\n //反射する壁を選ぶ\n for(int wr=0; wr<n; wr++){\n P ref = reflection(edge[wr], mid);\n if(!intersectSS(L(ref, source), edge[wr])) continue;\n P rp;\n if(isParallel(L(ref, source), edge[wr])){\n rp = (ref +source) /2.0;\n }else{\n rp = crosspointLL(L(ref, source), edge[wr]);\n }\n vector<L> ray{L(mid, rp), L(rp, source)};\n\n bool block = false;\n for(int k=0; k<2; k++){\n for(int w=0; w<n; w++){\n if(!isParallel(ray[k], edge[w]) && strictItsSS(ray[k], edge[w])){\n block = true;\n k = 2;\n break;\n }\n }\n }\n if(!block){\n light = true;\n break;\n }\n }\n if(!light){\n ans += abs(cp[i][j+1] -cp[i][j]);\n }\n }\n }\n\n cout << fixed << setprecision(10);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_2029_1046306", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-6)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s)const { return (p1==s.p1) ? p2 < s.p2 : p1 < s.p1; } \n bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\ndouble abs(Point a){ return sqrt(norm(a)); }\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\n\nPoint reflection(Line l,Point p) { return p + (projection(l, p) - p) * 2; }\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n return vec[1];\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n\ndouble angle(Point a,Point b,Point c) {\n double ux = b.x - a.x, uy = b.y - a.y;\n double vx = c.x - a.x, vy = c.y - a.y;\n return acos((ux*vx + uy*vy)/sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));\n} \n \n//多角形poly内(線分上も含む)に点pが存在するかどうは判定する \nbool inPolygon(Polygon poly,Point p){\n if((int)poly.size() == 0)return false;\n rep(i,poly.size()) if(onSegment(poly[i],poly[(i+1)%poly.size()],p))return true;\n double sum = 0;\n for(int i=0; i < (int)poly.size() ;i++) {\n if( equals(cross(poly[i]-p,poly[(i+1)%poly.size()]-p),0.0) ) continue; // 3点が平行だとangleがnanを返しsumがnanになり死ぬ\n if( cross3p(p,poly[i],poly[(i+1)%poly.size()]) < 0 ) sum -= angle(p,poly[i],poly[(i+1)%poly.size()]);\n else sum += angle(p,poly[i],poly[(i+1)%poly.size()]);\n }\n // あまり誤差を厳しくしすぎると良くないので以下のほうが良い \n const double eps = 1e-5;\n return (fabs(sum - 2*M_PI) < eps || fabs(sum + 2*M_PI) < eps);\n} \n\nbool inPolygon(const Polygon &poly,Segment seg){\n vector<Point> vp;\n vp.push_back(seg.p1);\n vp.push_back(seg.p2);\n rep(i,(int)poly.size()) {\n Segment edge = Segment(poly[i],poly[(i+1)%(int)poly.size()]);\n if( equals(cross(seg.p1-seg.p2,edge.p1-edge.p2),0.0) ) {\n if( onSegment(seg.p1,seg.p2,edge.p1) ) vp.push_back(edge.p1);\n if( onSegment(seg.p1,seg.p2,edge.p2) ) vp.push_back(edge.p2);\n } else {\n if( intersectSS(seg,edge) ) vp.push_back(crosspoint(seg,edge));\n }\n }\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n rep(i,(int)vp.size()-1) {\n Point middle_point = ( vp[i] + vp[i+1] ) / 2.0;\n if( !inPolygon(poly,middle_point) ) return false;\n }\n return true; \n}\n\nint V;\nPolygon poly;\nPoint light;\n\n// type 0 => =w=, type 1 => mirror\nvoid inner_add_new_points(Polygon &pol,vector<Point> &vp,int type,Line ref){\n rep(i,V){\n // ライトから普通のポリゴンの角をみる\n Vector e = ( poly[i] - light ) / abs( poly[i] - light );\n Segment seg = Segment(light,light+e*10000);\n rep(j,V) {\n Segment poly_seg = Segment(pol[j],pol[(j+1)%V]);\n if( equals(cross(seg.p2-seg.p1,poly_seg.p2-poly_seg.p1),0.0) ) continue;\n if( intersectSS(poly_seg,seg) ) {\n if( !type ) vp.push_back(crosspoint(poly_seg,seg));\n else vp.push_back(reflection(ref,crosspoint(poly_seg,seg)));\n }\n }\n\n // ライトから反転したポリゴンの角をみる\n e = ( pol[i] - light ) / abs( pol[i] - light );\n seg = Segment(light,light+e*10000);\n rep(j,V) {\n Segment poly_seg = Segment(pol[j],pol[(j+1)%V]);\n if( equals(cross(seg.p2-seg.p1,poly_seg.p2-poly_seg.p1),0.0) ) continue;\n if( intersectSS(poly_seg,seg) ) {\n if( !type ) vp.push_back(crosspoint(poly_seg,seg));\n else vp.push_back(reflection(ref,crosspoint(poly_seg,seg)));\n }\n }\n }\n}\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; } \nbool LTE(double a,double b) { return equals(a,b) || a < b; } \n\nPoint vir;\nbool comp_dist(const Point& p1,const Point& p2){ return LT(abs(vir-p1),abs(vir-p2)); }\n\nvoid add_new_points(Polygon &new_poly){\n vector<Point> vp;\n inner_add_new_points(poly,vp,0,Line());\n rep(i,V){\n Line ref = Line(poly[i],poly[(i+1)%V]);\n Polygon rpoly(V);\n rep(j,V) {\n rpoly[j] = reflection(ref,poly[j]);\n if( equals(rpoly[j].x,0.0) ) rpoly[j].x = 0;\n if( equals(rpoly[j].y,0.0) ) rpoly[j].y = 0;\n }\n inner_add_new_points(rpoly,vp,1,ref);\n }\n\n rep(i,(int)vp.size()) {\n if( equals(vp[i].x,0.0) ) vp[i].x = 0;\n if( equals(vp[i].y,0.0) ) vp[i].y = 0;\n }\n\n vector<Point> tmp;\n rep(i,vp.size()) {\n bool failed = false;\n rep(j,V) if( vp[i] == poly[j] ) { failed = true; break; }\n if( !failed ) tmp.push_back(vp[i]);\n }\n vp = tmp;\n\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n\n rep(i,V){\n vector<Point> buf;\n Segment seg = Segment(poly[i],poly[(i+1)%V]);\n rep(j,(int)vp.size()) if( onSegment(seg.p1,seg.p2,vp[j]) ) buf.push_back(vp[j]);\n vir = seg.p1;\n sort(buf.begin(),buf.end(),comp_dist);\n rep(j,(int)buf.size()) new_poly.push_back(buf[j]);\n new_poly.push_back(seg.p2);\n } \n}\n\nvoid compute_saw(Polygon &npoly,bool *saw){\n \n int nV = npoly.size(); \n // 直接見れる\n rep(i,nV) {\n Point mp = ( npoly[i] + npoly[(i+1)%nV] ) / 2.0;\n Segment vline = Segment(light,mp);\n bool failed = false;\n rep(j,V) {\n Segment seg = Segment(poly[j],poly[(j+1)%V]);\n if( equals(cross(seg.p1-seg.p2,vline.p1-vline.p2),0.0) ) continue;\n if( intersectSS(seg,vline) ) {\n if( onSegment(seg.p1,seg.p2,vline.p1) || onSegment(seg.p1,seg.p2,vline.p2) ) continue;\n if( onSegment(vline.p1,vline.p2,seg.p1) || onSegment(vline.p1,vline.p2,seg.p2) ) continue;\n failed = true;\n break;\n }\n }\n if( failed ) continue;\n saw[i] = true;\n }\n\n // 鏡一枚で見れる\n // 直接見える鏡だけを列挙\n vector<Line> ref;\n rep(i,nV) {\n Point mp = ( npoly[i] + npoly[(i+1)%nV] ) / 2.0;\n Segment vline = Segment(light,mp);\n bool failed = false;\n rep(j,V) {\n Segment seg = Segment(poly[j],poly[(j+1)%V]);\n if( equals(cross(seg.p1-seg.p2,vline.p1-vline.p2),0.0) ) continue;\n if( intersectSS(seg,vline) ) {\n if( onSegment(seg.p1,seg.p2,vline.p1) || onSegment(seg.p1,seg.p2,vline.p2) ) continue;\n if( onSegment(vline.p1,vline.p2,seg.p1) || onSegment(vline.p1,vline.p2,seg.p2) ) continue;\n failed = true;\n break;\n }\n }\n if( !failed ) ref.push_back(Line(npoly[i],npoly[(i+1)%nV]));\n }\n \n vector<Polygon> ref_polies;\n rep(i,(int)ref.size()){\n Polygon tmp_poly;\n rep(j,(int)poly.size()) tmp_poly.push_back(reflection(ref[i],poly[j]));\n ref_polies.push_back(tmp_poly);\n }\n\n rep(i,nV) if( !saw[i] ) {\n rep(j,(int)ref.size()){\n Segment rev_edge = Segment(reflection(ref[j],npoly[i]),reflection(ref[j],npoly[(i+1)%nV]));\n Point rev_mp = ( rev_edge.p1 + rev_edge.p2 ) / 2.0;\n Segment rev_vline = Segment(light,rev_mp);\n if( equals(cross(rev_vline.p1-rev_vline.p2,ref[j].p1-ref[j].p2),0.0) ) continue;\n if( !intersectSS(rev_vline,ref[j]) ) continue;\n rev_vline.p1 = crosspoint(rev_vline,ref[j]);\n if( inPolygon(ref_polies[j],rev_vline) ) {\n saw[i] = true;\n break;\n }\n }\n }\n}\n\nvoid compute(){\n Polygon npoly;\n add_new_points(npoly);\n int nV = npoly.size();\n bool saw[nV];\n rep(i,nV) saw[i] = false;\n compute_saw(npoly,saw);\n double sum = 0;\n rep(i,nV) if( !saw[i] )sum += abs(npoly[(i+1)%nV]-npoly[i]);\n sum = fabs(sum);\n printf(\"%.10lf\\n\",sum);\n}\n\nint main(){\n while( cin >> V, V ){\n poly.resize(V);\n rep(i,V) cin >> poly[i].x >> poly[i].y;\n cin >> light.x >> light.y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1540, "score_of_the_acc": -1.1181, "final_rank": 4 }, { "submission_id": "aoj_2029_1046302", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-6)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s)const { return (p1==s.p1) ? p2 < s.p2 : p1 < s.p1; } \n bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev から cur へ行って next へ行く際の角度を求める\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //同じセグメントかもよ\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n//cross product of pq and pr\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\n \n//returns true if point r is on the same line as the line pq\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \n//returns true if point t is on the left side of line pq\nbool ccwtest(Point p,Point q,Point r){\n return cross3p(p,q,r) > 0; //can be modified to accept collinear points\n}\n \nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n \n\nbool isConvex(vector<Point> p) {\n int sz = (int)p.size();\n if(sz < 3)return false;//boundary case, we treat a point or a line as not convex\n bool isLeft = ccwtest(p[0],p[1],p[2]);\n for(int i=1; i<(int)p.size();i++)\n if(ccwtest(p[i],p[(i+1)%sz],p[(i+2)%sz]) != isLeft) return false;\n return true;\n}\n\n\ndouble angle(Point a,Point b,Point c) {\n double ux = b.x - a.x, uy = b.y - a.y;\n double vx = c.x - a.x, vy = c.y - a.y;\n return acos((ux*vx + uy*vy)/sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));\n} \n \n//多角形poly内(線分上も含む)に点pが存在するかどうは判定する \nbool inPolygon(Polygon poly,Point p){\n if((int)poly.size() == 0)return false;\n rep(i,poly.size()) if(onSegment(poly[i],poly[(i+1)%poly.size()],p))return true;\n double sum = 0;\n for(int i=0; i < (int)poly.size() ;i++) {\n if( equals(cross(poly[i]-p,poly[(i+1)%poly.size()]-p),0.0) ) continue; // 3点が平行だとangleがnanを返しsumがnanになり死ぬ\n if( cross3p(p,poly[i],poly[(i+1)%poly.size()]) < 0 ) sum -= angle(p,poly[i],poly[(i+1)%poly.size()]);\n else sum += angle(p,poly[i],poly[(i+1)%poly.size()]);\n }\n // あまり誤差を厳しくしすぎると良くないので以下のほうが良い \n const double eps = 1e-5;\n return (fabs(sum - 2*M_PI) < eps || fabs(sum + 2*M_PI) < eps);\n} \n\nbool inPolygon(const Polygon &poly,Segment seg){\n vector<Point> vp;\n vp.push_back(seg.p1);\n vp.push_back(seg.p2);\n rep(i,(int)poly.size()) {\n Segment edge = Segment(poly[i],poly[(i+1)%(int)poly.size()]);\n if( equals(cross(seg.p1-seg.p2,edge.p1-edge.p2),0.0) ) {\n if( onSegment(seg.p1,seg.p2,edge.p1) ) vp.push_back(edge.p1);\n if( onSegment(seg.p1,seg.p2,edge.p2) ) vp.push_back(edge.p2);\n } else {\n if( intersectSS(seg,edge) ) vp.push_back(crosspoint(seg,edge));\n }\n }\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n rep(i,(int)vp.size()-1) {\n Point middle_point = ( vp[i] + vp[i+1] ) / 2.0;\n if( !inPolygon(poly,middle_point) ) return false;\n }\n return true; \n}\n\nint V;\nPolygon poly;\nPoint light;\n\n// type 0 => =w=, type 1 => mirror\nvoid inner_add_new_points(Polygon &pol,vector<Point> &vp,int type,Line ref){\n rep(i,V){\n Vector e = ( poly[i] - light ) / abs( poly[i] - light );\n Segment seg = Segment(light,light+e*10000);\n rep(j,V) {\n Segment poly_seg = Segment(pol[j],pol[(j+1)%V]);\n if( equals(cross(seg.p2-seg.p1,poly_seg.p2-poly_seg.p1),0.0) ) continue;\n if( intersectSS(poly_seg,seg) ) {\n if( !type ) vp.push_back(crosspoint(poly_seg,seg));\n else vp.push_back(reflection(ref,crosspoint(poly_seg,seg)));\n }\n }\n\n e = ( pol[i] - light ) / abs( pol[i] - light );\n seg = Segment(light,light+e*10000);\n rep(j,V) {\n Segment poly_seg = Segment(pol[j],pol[(j+1)%V]);\n if( equals(cross(seg.p2-seg.p1,poly_seg.p2-poly_seg.p1),0.0) ) continue;\n if( intersectSS(poly_seg,seg) ) {\n if( !type ) vp.push_back(crosspoint(poly_seg,seg));\n else vp.push_back(reflection(ref,crosspoint(poly_seg,seg)));\n }\n }\n\n }\n}\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; } \nbool LTE(double a,double b) { return equals(a,b) || a < b; } \n\nPoint vir;\nbool comp_dist(const Point& p1,const Point& p2){ return LT(abs(vir-p1),abs(vir-p2)); }\n\nvoid add_new_points(Polygon &new_poly){\n vector<Point> vp;\n\n inner_add_new_points(poly,vp,0,Line());\n rep(i,V){\n Line ref = Line(poly[i],poly[(i+1)%V]);\n Polygon rpoly(V);\n rep(j,V) {\n rpoly[j] = reflection(ref,poly[j]);\n if( equals(rpoly[j].x,0.0) ) rpoly[j].x = 0;\n if( equals(rpoly[j].y,0.0) ) rpoly[j].y = 0;\n }\n inner_add_new_points(rpoly,vp,1,ref);\n }\n\n\n /*\n // 直接見える点の列挙\n rep(i,V){\n Vector e = ( poly[i] - light ) / abs( poly[i] - light );\n Segment vline = Segment(light,light+e*10000);\n rep(j,V){\n Segment seg = Segment(poly[j],poly[(j+1)%V]);\n if( equals(cross(vline.p1-vline.p2,seg.p1-seg.p2),0.0) ) continue;\n if( intersectSS(vline,seg) ) {\n if( onSegment(vline.p1,vline.p2,seg.p1) || onSegment(vline.p1,vline.p2,seg.p2) ) continue;\n if( onSegment(seg.p1,seg.p2,vline.p1) || onSegment(seg.p1,seg.p2,vline.p2) ) continue;\n vp.push_back(crosspoint(vline,seg));\n }\n }\n }\n\n // 鏡に反射して見える点の列挙\n\n rep(i,V){\n Line ref = Line(poly[i],poly[(i+1)%V]);\n Polygon rev_poly;\n rep(j,V) rev_poly.push_back(reflection(ref,poly[j])); \n \n\n }\n */\n\n\n //------------------------------\n\n rep(i,(int)vp.size()) {\n if( equals(vp[i].x,0.0) ) vp[i].x = 0;\n if( equals(vp[i].y,0.0) ) vp[i].y = 0;\n }\n\n vector<Point> tmp;\n rep(i,vp.size()) {\n bool failed = false;\n rep(j,V) if( vp[i] == poly[j] ) { failed = true; break; }\n if( !failed ) tmp.push_back(vp[i]);\n }\n vp = tmp;\n\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n\n rep(i,V){\n vector<Point> buf;\n Segment seg = Segment(poly[i],poly[(i+1)%V]);\n rep(j,(int)vp.size()) if( onSegment(seg.p1,seg.p2,vp[j]) ) buf.push_back(vp[j]);\n vir = seg.p1;\n sort(buf.begin(),buf.end(),comp_dist);\n rep(j,(int)buf.size()) new_poly.push_back(buf[j]);\n new_poly.push_back(seg.p2);\n } \n\n}\n\nvoid compute_saw(Polygon &npoly,bool *saw){\n \n int nV = npoly.size(); \n // 直接見れる\n\n rep(i,nV) {\n Point mp = ( npoly[i] + npoly[(i+1)%nV] ) / 2.0;\n Segment vline = Segment(light,mp);\n bool failed = false;\n rep(j,V) {\n Segment seg = Segment(poly[j],poly[(j+1)%V]);\n if( equals(cross(seg.p1-seg.p2,vline.p1-vline.p2),0.0) ) continue;\n if( intersectSS(seg,vline) ) {\n if( onSegment(seg.p1,seg.p2,vline.p1) || onSegment(seg.p1,seg.p2,vline.p2) ) continue;\n if( onSegment(vline.p1,vline.p2,seg.p1) || onSegment(vline.p1,vline.p2,seg.p2) ) continue;\n failed = true;\n break;\n }\n }\n if( failed ) continue;\n saw[i] = true;\n }\n /*\n cout << \"direct see\" << endl;\n rep(i,nV) if( saw[i] )cout << npoly[i] << \",\" << npoly[(i+1)%nV]<< endl;\n cout << endl;\n */\n // 鏡一枚で見れる\n\n // 直接見える鏡だけを列挙\n vector<Line> ref;\n rep(i,nV) {\n Point mp = ( npoly[i] + npoly[(i+1)%nV] ) / 2.0;\n Segment vline = Segment(light,mp);\n bool failed = false;\n rep(j,V) {\n Segment seg = Segment(poly[j],poly[(j+1)%V]);\n if( equals(cross(seg.p1-seg.p2,vline.p1-vline.p2),0.0) ) continue;\n if( intersectSS(seg,vline) ) {\n if( onSegment(seg.p1,seg.p2,vline.p1) || onSegment(seg.p1,seg.p2,vline.p2) ) continue;\n if( onSegment(vline.p1,vline.p2,seg.p1) || onSegment(vline.p1,vline.p2,seg.p2) ) continue;\n failed = true;\n break;\n }\n }\n if( !failed ) ref.push_back(Line(npoly[i],npoly[(i+1)%nV]));\n }\n \n vector<Polygon> ref_polies;\n rep(i,(int)ref.size()){\n Polygon tmp_poly;\n rep(j,(int)poly.size()) tmp_poly.push_back(reflection(ref[i],poly[j]));\n ref_polies.push_back(tmp_poly);\n }\n /*\n rep(i,(int)ref.size()) {\n cout << \"ref[\" << i << \"] = \" << ref[i] << endl;\n rep(j,(int)ref_polies[i].size()){\n cout << \" \" << ref_polies[i][j];\n } cout << endl;\n }\n */\n\n rep(i,nV) if( !saw[i] ) {\n rep(j,(int)ref.size()){\n Segment rev_edge = Segment(reflection(ref[j],npoly[i]),reflection(ref[j],npoly[(i+1)%nV]));\n Point rev_mp = ( rev_edge.p1 + rev_edge.p2 ) / 2.0;\n Segment rev_vline = Segment(light,rev_mp);\n if( equals(cross(rev_vline.p1-rev_vline.p2,ref[j].p1-ref[j].p2),0.0) ) continue;\n if( !intersectSS(rev_vline,ref[j]) ) continue;\n\n rev_vline.p1 = crosspoint(rev_vline,ref[j]);\n \n if( inPolygon(ref_polies[j],rev_vline) ) {\n saw[i] = true;\n break;\n }\n\n }\n }\n \n}\n\nvoid compute(){\n Polygon npoly;\n add_new_points(npoly);\n int nV = npoly.size();\n rep(i,nV) {\n if( equals(npoly[i].x,0.0) ) npoly[i].x = 0;\n if( equals(npoly[i].y,0.0) ) npoly[i].y = 0;\n }\n bool saw[nV];\n rep(i,nV) saw[i] = false;\n compute_saw(npoly,saw);\n rep(i,nV) {\n if( equals(npoly[i].x,0.0) ) npoly[i].x = 0;\n if( equals(npoly[i].y,0.0) ) npoly[i].y = 0;\n }\n /*\n cout << \"nV = \" << nV << endl;\n rep(i,nV) cout << i << \"-th \" << npoly[i] << endl;\n cout << endl;\n\n rep(i,nV) {\n if( !saw[i] ) cout << \"cannot see \" << npoly[i] << \",\" << npoly[(i+1)%nV] << \" : \" << abs(npoly[i]-npoly[(i+1)%nV]) << endl;\n }\n */\n\n double sum = 0;\n rep(i,nV) if( !saw[i] )sum += abs(npoly[(i+1)%nV]-npoly[i]);\n sum = fabs(sum);\n printf(\"%.10lf\\n\",sum);\n}\n\nint main(){\n while( cin >> V, V ){\n poly.resize(V);\n rep(i,V) cin >> poly[i].x >> poly[i].y;\n cin >> light.x >> light.y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1548, "score_of_the_acc": -1.122, "final_rank": 5 }, { "submission_id": "aoj_2029_665770", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n // if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() {}\n};\nostream &operator<<(ostream &os, const L &a) {\n os << a[0] << \" -> \" << a[1];\n return os;\n}\n\ntypedef vector<P> G;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L &l, const P &p) {\n return p + P(2,0) * (projection(l, p) - p);\n}\n\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\n\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool intersectSS2(const L &s, const L &t) {\n if (intersectSP(s,t[0]) || intersectSP(s,t[1]) ||\n intersectSP(t,s[0]) || intersectSP(t,s[1])) return 0;\n return intersectSS(s,t);\n}\nbool intersectHS(const L &h, const L &s) {\n if (intersectLP(h,s[0]) || intersectLP(h,s[1]) ||\n intersectLP(s,h[0]) || intersectLP(s,h[1])) return 0;\n if (intersectSS(h,s)) return 1;\n if (!intersectLS(h,s)) return 0;\n return (ccw(s[0],s[1],h[0]) == 1) ^ (cross(s[1]-s[0], h[1]-h[0]) > 0);\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nint n;\nL pl[20];\nvector<P> v[20];\n\n\nint mostNearest(const L &h) {\n double mi = INF;\n int res = -1;\n REP(k,n) {\n if (intersectHS(h,pl[k])) {\n if (chmin(mi,norm(crosspoint(h,pl[k])-h[0]))) {\n res = k;\n }\n }\n }\n return res;\n}\n\n\nvoid func(const L &h) {\n int cl_id = mostNearest(h);\n if (cl_id != -1) {\n v[cl_id].push_back(crosspoint(h,pl[cl_id]));\n }\n}\n\nP c;\n\nbool isVisible(const P &p, const P &q) { // direct visible\n REP(i,n) if (intersectSS2(L(p,q),pl[i])) return 0;\n return 1;\n}\n\nbool isVisibleUsingReflection(const P &p) { // visible from c\n // direct\n if (isVisible(c,p)) return 1;\n L l(c,p); \n // reflect\n REP(j,n) { \n if (ccw(pl[j][0], pl[j][1], c) * ccw(pl[j][0], pl[j][1], p) != 1) continue;\n double d1 = distanceLP(pl[j],c);\n double d2 = distanceLP(pl[j],p);\n P refp = P(d2/(d1+d2),0) * projection(pl[j],c) + P(d1/(d1+d2),0) * projection(pl[j],p);\n if (!intersectSP(pl[j], refp)) continue;\n if (isVisible(c,refp) && isVisible(refp,p)) return 1;\n }\n return 0;\n}\n\nL reflect(const L &h, const L &l) {\n P refp = crosspoint(h,l);\n P v = refp - reflection(l,h[0]);\n return L(refp,refp+v);\n}\n\nint main() {\n while(cin >> n, n) {\n P p[20];\n REP(i,n) {\n cin >> p[i].real() >> p[i].imag();\n }\n REP(i,n) {\n pl[i] = L(p[i],p[(i+1)%n]); \n }\n cin >> c.real() >> c.imag();\n REP(i,n) v[i].clear();\n \n REP(i,n) {\n bool ok = 1;\n if (isVisible(c,p[i])) {\n L h = L(c,p[i]); \n // c -> p[i]\n func(h); \n // c -> p[i] -> reflection\n int ref_id = mostNearest(h);\n if (ref_id != -1) {\n // reflect at pl[ref_id]\n func(reflect(h, pl[ref_id]));\n }\n }\n // c -> pl[j] -> p[i]\n REP(j,n) { \n if (ccw(pl[j][0], pl[j][1], c) * ccw(pl[j][0], pl[j][1], p[i]) != 1) continue; // different side\n double d1 = distanceLP(pl[j],c);\n double d2 = distanceLP(pl[j],p[i]);\n P refp = P(d2/(d1+d2),0) * projection(pl[j],c) + P(d1/(d1+d2),0) * projection(pl[j],p[i]);\n if (!intersectSP(pl[j], refp)) continue;\n if (isVisible(c,refp)) func(L(refp,p[i]));\n }\n // reflect at corners of pl[i]\n {\n P refp = pl[i][0];\n if (isVisible(c,refp)) {\n func(reflect(L(c,refp),pl[i]));\n }\n }\n {\n P refp = pl[i][1];\n if (isVisible(c,refp)) {\n func(reflect(L(c,refp),pl[i]));\n }\n }\n }\n double ans = 0;\n REP(i,n) {\n // cout << i << endl;\n // FOR(it, v[i]) {\n // cout << *it << endl;\n // }\n double len = abs(pl[i][1]-pl[i][0]);\n vector<double> vv;\n vv.push_back(0);\n vv.push_back(len);\n FOR(it, v[i]) {\n vv.push_back(abs(*it - pl[i][0]));\n }\n sort(ALL(vv));\n REP(j,vv.size()-1) {\n double s = (vv[j]+vv[j+1])/2 / len;\n P q = (1-s)*pl[i][0] + s*pl[i][1];\n if (!isVisibleUsingReflection(q)) {\n ans += vv[j+1]-vv[j];\n\n double ss = vv[j]/len, tt = vv[j+1]/len;\n P p1 = (1-ss)*pl[i][0] + ss*pl[i][1];\n P p2 = (1-tt)*pl[i][0] + tt*pl[i][1];\n // cout << p1.real() << \" \" << p1.imag() << endl;\n // cout << p2.real() << \" \" << p2.imag() << endl;\n // cout << endl;\n }\n }\n }\n printf(\"%.10f\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1300, "score_of_the_acc": -0.5, "final_rank": 1 }, { "submission_id": "aoj_2029_665751", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n // if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() {}\n};\nostream &operator<<(ostream &os, const L &a) {\n os << a[0] << \" -> \" << a[1];\n return os;\n}\n\ntypedef vector<P> G;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L &l, const P &p) {\n return p + P(2,0) * (projection(l, p) - p);\n}\n\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\n\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool intersectSS2(const L &s, const L &t) {\n if (intersectSP(s,t[0]) || intersectSP(s,t[1]) ||\n intersectSP(t,s[0]) || intersectSP(t,s[1])) return 0;\n return intersectSS(s,t);\n}\nbool intersectHS(const L &h, const L &s) {\n if (intersectLP(h,s[0]) || intersectLP(h,s[1]) ||\n intersectLP(s,h[0]) || intersectLP(s,h[1])) return 0;\n if (intersectSS(h,s)) return 1;\n if (!intersectLS(h,s)) return 0;\n return (ccw(s[0],s[1],h[0]) == 1) ^ (cross(s[1]-s[0], h[1]-h[0]) > 0);\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nint n;\nL pl[20];\nvector<P> v[20];\n\nvoid func(const L &h) {\n double mi = INF;\n int cl_id = -1;\n REP(k,n) {\n if (intersectHS(h,pl[k])) {\n if (chmin(mi,norm(crosspoint(h,pl[k])-h[0]))) {\n cl_id = k;\n }\n }\n }\n if (cl_id != -1) {\n v[cl_id].push_back(crosspoint(h,pl[cl_id]));\n }\n}\n\nP c;\n\nbool isVisible(const P &p) {\n // direct\n L l(c,p);\n bool ok = 1;\n REP(i,n) {\n ok &= !intersectSS2(l,pl[i]);\n }\n if (ok) return 1;\n // reflect\n REP(j,n) { \n if (ccw(pl[j][0], pl[j][1], c) * ccw(pl[j][0], pl[j][1], p) != 1) continue;\n double d1 = distanceLP(pl[j],c);\n double d2 = distanceLP(pl[j],p);\n P refp = P(d2/(d1+d2),0) * projection(pl[j],c) + P(d1/(d1+d2),0) * projection(pl[j],p);\n if (!intersectSP(pl[j], refp)) continue;\n bool ok = 1;\n L l1(c,refp);\n L l2(refp,p);\n REP(k,n) {\n ok &= !intersectSS2(l1,pl[k]);\n ok &= !intersectSS2(l2,pl[k]);\n }\n if (ok) return 1;\n }\n return 0;\n}\n\nint main() {\n while(cin >> n, n) {\n P p[20];\n REP(i,n) {\n cin >> p[i].real() >> p[i].imag();\n }\n REP(i,n) {\n pl[i] = L(p[i],p[(i+1)%n]); \n }\n cin >> c.real() >> c.imag();\n REP(i,n) v[i].clear();\n \n REP(i,n) {\n bool ok = 1;\n L l1 = L(c,p[i]);\n REP(k,n) ok &= !intersectSS2(l1,pl[k]);\n if (ok) {\n // c -> p[i]\n func(l1); \n // c -> p[i] -> reflection\n int ref_id = -1;\n double mi = INF;\n REP(j,n) {\n if (intersectHS(l1,pl[j])) {\n if (chmin(mi, norm(crosspoint(l1,pl[j])-c))) {\n ref_id = j;\n }\n }\n }\n if (ref_id != -1) {\n // reflect at pl[ref_id]\n P refp = crosspoint(l1,pl[ref_id]);\n P v = refp - reflection(pl[ref_id],c);\n func(L(refp, refp+v));\n }\n }\n // c -> pl[j] -> p[i]\n REP(j,n) { \n if (ccw(pl[j][0], pl[j][1], c) * ccw(pl[j][0], pl[j][1], p[i]) != 1) continue;\n double d1 = distanceLP(pl[j],c);\n double d2 = distanceLP(pl[j],p[i]);\n P refp = P(d2/(d1+d2),0) * projection(pl[j],c) + P(d1/(d1+d2),0) * projection(pl[j],p[i]);\n if (!intersectSP(pl[j], refp)) continue;\n bool ok = 1;\n L l1(c,refp);\n L l2(refp,p[i]);\n REP(k,n) {\n ok &= !intersectSS2(l1,pl[k]);\n }\n if (!ok) continue;\n\n func(l2);\n }\n // reflect at corners of pl[i]\n {\n P refp = pl[i][0];\n L l1(c,refp);\n bool ok = 1; \n REP(k,n) ok &= !intersectSS2(l1,pl[k]);\n if (ok) {\n L l2 = L(refp, refp + refp-reflection(pl[i], c)); \n func(l2);\n }\n }\n {\n P refp = pl[i][1];\n L l1(c,refp);\n bool ok = 1; \n REP(k,n) ok &= !intersectSS2(l1,pl[k]);\n if (ok) {\n L l2 = L(refp, refp + refp-reflection(pl[i], c));\n func(l2);\n } \n }\n }\n double ans = 0;\n REP(i,n) {\n // cout << i << endl;\n // FOR(it, v[i]) {\n // cout << *it << endl;\n // }\n double len = abs(pl[i][1]-pl[i][0]);\n vector<double> vv;\n vv.push_back(0);\n vv.push_back(len);\n FOR(it, v[i]) {\n vv.push_back(abs(*it - pl[i][0]));\n }\n sort(ALL(vv));\n REP(j,vv.size()-1) {\n double s = (vv[j]+vv[j+1])/2 / len;\n P q = (1-s)*pl[i][0] + s*pl[i][1];\n if (!isVisible(q)) {\n ans += vv[j+1]-vv[j];\n\n double ss = vv[j]/len, tt = vv[j+1]/len;\n P p1 = (1-ss)*pl[i][0] + ss*pl[i][1];\n P p2 = (1-tt)*pl[i][0] + tt*pl[i][1];\n // cout << p1.real() << \" \" << p1.imag() << endl;\n // cout << p2.real() << \" \" << p2.imag() << endl;\n // cout << endl;\n }\n }\n }\n printf(\"%.10f\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1300, "score_of_the_acc": -0.5, "final_rank": 1 } ]
aoj_2023_cpp
Princess, a Strategist お姫様は戦略家 English text is not available in this practice contest. ある貧乏な国の勇敢なお姫様は,お城を抜け出して古の秘宝を手に入れる冒険に出かける算段をしている.ところが,古の秘宝の周囲は複数のガーディアンによって守られており,普通の方法では辿り着くことができない.そこで,お姫様は従者であるあなたを囮にし,ガーディアン達の注意を惹きつけている間に秘宝を取って来るという作戦を考えた.そのような事をされては命がいくつあっても足りない,しかしお姫様の命令に従わないわけにもいかない.そこで,まずあなたはあらかじめ偵察をし,どのように動けばガーディアンの攻撃をかわしつつ,注意を引きつけられるのかを念入りに調査した.あなたの仕事は,これらの調査結果をもとに囮であるあなたとガーディアンの発射する弾丸の衝突がいつ,そして何回起こったのか計算するプログラムを書くことである. まず,簡単のため,フィールドとして十分に高い場所から見下ろした二次元平面を考える.そして,あなたはガーディアンの攻撃から身を守るために鎧を装備している.そのため,あなたの形は多角形でであるものと考えて良い.なお,この多角形の各線分は一切交差したり重なったりしない.また,あなたの移動については以下のことを仮定して良い. 等速直線運動しかしない. 加速,減速,方向転換は全て一瞬で行われる. 回転しないため向きは初期状態から一切変わらない. 多角形の全ての部分は常に y 座標が正の位置にある. また,ガーディアンの発射する弾丸については,以下のことを仮定して良い. ガーディアンが発射する弾丸の太さは無視できる ガーディアンが発射する弾丸は有限の長さをもつ 長さ l の弾丸が先頭座標 ( x , 0) から速度ベクトル ( v x , v y ) をもって発射されたとすると,弾丸の末尾座標は以下の地点にある(注:弾丸が発射される瞬間の弾丸の先頭の y 座標はすべて0である): たとえば,(4, 0) の地点から速度ベクトル (3, 4) の長さ 10 の弾丸が発射されるケースでは弾丸の末尾は(-2, -8) にある 敵キャラクターが発射する弾丸同士の衝突は全て無視される あなたとガーディアンの発射した弾丸の衝突の定義を以下に述べる.あなたを構成する多角形とガーディアンの発射する線分の共有点が初めて発生した時刻を弾丸の衝突時刻とする.ガーディアンの発射した弾丸にあなたが衝突したとしても,あなたはダメージを受けるだけで移動には何の支障もきたさないものとし,あなたに衝突した弾丸は衝突した瞬間に消滅するものとする.なお,あなたが初期位置から任意の方向に 10 -6 の範囲で平行移動したとしても,衝突時間は高々 10 -5 しか変化しないことが保証されている. Input 入力は,複数のデータセットからなる. それぞれのデータセットは,次のような形式で与えられる. N M B X 1 Y 1 ... X N Y N T 1 VX 1 VY 1 ... T M VX M VY M T' 1 X' 1 VX' 1 VY' 1 L 1 ... T' B X' B VX' B VY' B L B 各データセットの先頭には3つの非負整数 N (3 ≦ N ≦ 20), M (0 ≦ M ≦ 100), B (0 ≦ B ≦ 100) が与えられる. これらはそれぞれ,あなたである多角形の頂点の数,あなたの移動に関する情報の数,およびガーディアンの発射した弾丸の数を表す. 続く N 行では,あなたを表す多角形の頂点の時刻0での座標が順に列挙される. ( X i , Y i ) (1 ≦ i ≦ N ) はそれぞれ,多角形の i 番目の頂点の座標を表す. これらの値はすべて整数であり, -10,000 ≦ X i ≦ 10,000, 0 < Y i ≦ 10,000 を満たす. 続く M 行では, あなたの移動を表す指示が M 個与えられる. i 番目 (1 ≦ i ≦ M ) の移動指示は 3つの整数 T i , VX i , VY i から成り立っており, これは 時刻 T i -1 から T i までの間,あなたの速度ベクトルを ( VX i , VY i ) に維持するということを意味する. ただし, 0 = T 0 < T 1 < T 2 < ... < T M ≦ 10,000, -100 ≦ VX i ≦ 100, -100 ≦ VY i ≦ 100 である. あなたがすべての移動指示を終えた後(すなわち,時刻 T M 以降)は移動を停止し, その場にとどまり続けるものとする. あなたが停止した以降にも弾丸との衝突が発生する可能性があり, 出力に際してはそのような衝突も考慮する必要があることに注意せよ. 続く B 行では,ガーディアンの発射する B 個の弾丸に関する情報が記述されている. i 番目 (1 ≦ i ≦ B ) の弾丸に関する情報は5つの整数 T' i , X' i , VX' i , VY' i および L i で表され,これは,時刻 T' i に座標 ( X' i , 0) から速度ベクトル ( VX' i , VY' i ) をもつ長さ L i の弾丸が発射されることを意味する. これらの値は 0 ≦ T' 1 ≦ T' 2 ≦ ... ≦ T' B ≦ 10,000, -10,000 ≦ X' i ≦ 10,000, -100 ≦ VX' i ≦ 100, 0 < VY' i ≦ 100, 0 < L i ≦ 100 を満たす. 最後のデータセットの後ろに,データセットの終了を意味する "0 0 0" と書かれた1行が与えられる. これはデータセットの一部ではない. Output 各データセットに対する出力の先頭に,あなたがガーディアンの発射した弾丸に衝突した回数 n を出力せよ.続く n 行では,あなたがガーディアンの発射した弾丸に衝突した時刻を昇順に誤差高々 0.001 の精度で出力せよ. Sample Input 4 1 1 1 1 1 2 -1 2 -1 1 2 1 0 0 1 0 1 2 0 0 0 Output for the Sample Input 1 1.000
[ { "submission_id": "aoj_2023_6005917", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=5005,INF=1<<30;\n\n#define double long double\n\n//幾何ライブラリ\n// define double ll をするときは Point の < と == も書き換えよう!\n\nconst double eps=1e-4;\nconst double pi=acos((double)-1.0L);\n#define equals(a,b) (fabs((a)-(b))<eps)\n\ndouble torad(double deg) {return (double)(deg)*pi/180.0;}\ndouble todeg(double ang) {return ang*180.0/pi;}\n\nclass Point{\npublic:\n double x,y;\n \n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n \n double abs(){return sqrt(norm());}\n double norm(){return x*x+y*y;}\n \n bool operator < (const Point &p)const{\n return x+eps<p.x||(equals(x,p.x)&&y+eps<p.y);\n }\n \n bool operator == (const Point &p)const{\n return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;\n }\n};\n\ntypedef Point Vector;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nstruct Segment{\n Point p1,p2;\n};\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\nPoint turn(Point p,Point c,double pi){\n double q=atan2(p.y-c.y,p.x-c.x);\n q+=pi;\n p=c+Point{cos(q)*abs(p-c),sin(q)*abs(p-c)};\n \n return p;\n}\n//pをcを中心としてpi回転させる(1周で2π)\n//p=cのときnan\n\n//p0,p1,p2の順に見たときどうなるか?\n\nstatic const int counter_clockwise=1;\nstatic const int clockwise=-1;\nstatic const int online_back=2;\nstatic const int online_front=-2;\nstatic const int on_segment=0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n \n if(cross(a,b)>eps) return counter_clockwise;\n if(cross(a,b)<-eps) return clockwise;\n if(dot(a,b)<-eps) return online_back;\n if(a.norm()<b.norm()) return online_front;\n \n return on_segment;\n}\n\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return(ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n\nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool overlap(Segment s1,Segment s2){\n int a=ccw(s1.p1,s1.p2,s2.p1),b=ccw(s1.p1,s1.p2,s2.p2);\n if(a&1||b&1) return 0;\n if(a==2){\n if(b==-2||(b==0&&!(s2.p2==s1.p1))) return 1;\n else return 0;\n }\n if(a==-2){\n if(b==2||(b==0&&!(s2.p2==s1.p2))) return 1;\n else return 0;\n }\n if(a==0){\n if(s1.p1==s2.p1){\n if(b!=2) return 1;\n else return 0;\n }\n else if(s1.p2==s2.p1){\n if(b!=-2) return 1;\n else return 0;\n }\n else return 1;\n }\n return 0;\n}\n//s1とs2の共通の線分(長さ0より大きい)があるかどうか\n\ntypedef Segment Line;\n\ndouble getDistance(Point a,Point b){\n return abs(a-b);\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistance(Segment s1,Segment s2){\n if(intersect(s1,s2)) return 0.0;\n return min({getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2),getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)});\n}\n\nPoint getCrossPointS(Segment s1,Segment s2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}//同じ時壊れます\n\nPoint getCrossPointL(Line l1,Line l2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n \n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n \n return l1.p1+v1*cross(v2,l2.p1-l1.p1)/cross(v2,v1);\n}\n\nSegment ParallelSegment(Segment s,double d){\n Vector v={-(s.p2-s.p1).y,(s.p2-s.p1).x};\n v=v/abs(v);\n \n s.p1=s.p1+v*d;\n s.p2=s.p2+v*d;\n \n return s;\n}\n\nPoint naisin(Point p1,Point p2,Point p3){\n if(p1==p2&&p2==p3&&p3==p1) return p1;\n \n return (p1*abs(p2-p3)+p2*abs(p1-p3)+p3*abs(p1-p2))/(abs(p2-p3)+abs(p1-p3)+abs(p1-p2));\n}\n\nPoint naisin(Line l1,Line l2,Line l3){\n //平行でない前提\n \n Point p1=getCrossPointL(l1,l2),p2=getCrossPointL(l1,l3),p3=getCrossPointL(l2,l3);\n return naisin(p1,p2,p3);\n}\n\n//ネットの適当を書いたのであってるか全く知りません→あってそう\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\nPoint CircleCenter(Point a,Point b,Point c){\n Point u=a-b,v=a-c;\n double m1=(norm(a)-norm(b))/2.0,m2=(norm(a)-norm(c))/2.0;\n \n Point res;\n if(cross(u,v)==0.0){\n res.x=1e9;\n res.y=1e9;\n \n return res;\n }\n res.x=(m1*v.y-m2*u.y)/cross(u,v);\n res.y=(m1*v.x-m2*u.x)/cross(v,u);\n \n return res;\n}\n//3点を通る円の中心を返す\n\n//交わる 0\n// c1がc2のinside 1\n// c1がc2のoutside 2\n// 交わらない 3\n\nint not_intersect(Circle c1,Circle c2){\n double d=getDistance(c1.c,c2.c);\n double r1=c1.r,r2=c2.r;\n if(r1<r2){\n if(d<(r2-r1)) return 1;\n }\n if(r1>r2){\n if(d<(r1-r2)) return 2;\n }\n if(d<=r1+r2) return 0;\n else return 3;\n}\n\npair<Point,Point> segCrossPpoints(Circle c,Line l){\n //assert(intersect(c,l));\n Vector pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n return make_pair(pr+e*base,pr-e*base);\n}\n\ndouble arg(Vector p){return atan2(p.y,p.x);}\nVector polar(double a,double r){return Point(cos(r)*a,sin(r)*a);}\n\n//inside(outside)\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n //assert(intersect(c1,c2));\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nvector<Line> Commontangent(Circle c1,Circle c2){\n vector<Line> res;\n Point p=c2.c-c1.c;\n \n if(abs(p)>=(c1.r+c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r+c2.r)+p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r+c2.r)-p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r+c2.r)-p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r+c2.r)+p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n if(abs(p)>=abs(c1.r-c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r-c2.r)+p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r-c2.r)-p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r-c2.r)-p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r-c2.r)+p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n return res;\n}\n\ntypedef vector<Point> Polygon;\n\n/*\n IN 2\n ON 1\n OUT 0\n */\n\nint contains(Polygon g,Point p){\n int n=int(g.size());\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(a.y>b.y) swap(a,b);\n if(a.y<eps&&0<b.y&&cross(a,b)<0) x=!x;\n if(abs(cross(a,b))<eps&&dot(a,b)<eps) return 1;\n }\n return (x?2:0);\n}\n\nPolygon andrewScan(Polygon s,bool ok){\n Polygon u,l;\n sort(all(s));\n \n if(int(s.size())<3) return s;\n int n=int(s.size());\n \n u.push_back(s[0]);\n u.push_back(s[1]);\n \n l.push_back(s[n-1]);\n l.push_back(s[n-2]);\n \n if(ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])==counter_clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])==counter_clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n if(!ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])!=clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])!=clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n reverse(all(l));\n \n for(int i=int(u.size())-2;i>=1;i--) l.push_back(u[i]);\n \n return l;\n}//ok==1なら辺の上も含める\n\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon Q;\n for(int i=0;i<si(P);i++){\n Point A=P[i],B=P[(i+1)%si(P)];\n if(ccw(l.p1,l.p2,A)!=-1)Q.push_back(A);\n if(ccw(l.p1,l.p2,A)*ccw(l.p1,l.p2,B)<0) Q.push_back(getCrossPointL(Line{A,B},l));\n }\n return Q;\n}\n\ndouble area(Point a,Point b,Point c){\n b=b-a;\n c=c-a;\n return abs(b.x*c.y-b.y*c.x)/2.0;\n}\n\ndouble area(Polygon &P){\n if(si(P)==0) return 0.0;\n double res=0;\n Point c={0.0,0.0};\n for(int i=0;i<si(P);i++){\n c=c+P[i];\n }\n c=c/si(P);\n \n for(int i=0;i<si(P);i++){\n res+=area(c,P[i],P[(i+1)%si(P)]);\n }\n \n return res;\n}\n\nll gcd(ll a,ll b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\n\npair<Point,Vector> perpendicular_bisector(Point a,Point b){\n Point c=(a+b)/2;\n Vector v=b-c;\n swap(v.x,v.y);\n v.x*=-1;\n \n Point p=c;\n if(v.x==0){\n v.y=1;\n p.y=0;\n }\n else if(v.y==0){\n v.x=1;\n p.x=0;\n }\n else{\n if(v.x<0){\n v.x*=-1;\n v.y*=-1;\n }\n ll g=gcd(abs(ll(v.x)),abs(ll(v.y)));\n v.x/=g;\n v.y/=g;\n if(p.x>=0){\n ll d=p.x/v.x;\n p=p-v*d;\n }else{\n ll d=abs(p.x)/v.x;\n p=p+v*d;\n \n if(p.x<0){\n p=p+v;\n }\n }\n }\n \n return mp(p,v);\n}\n//2倍するなりして整数にしておくこと\n\nint main() {\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,M,B;cin>>N>>M>>B;\n M+=2;\n if(N==0) break;\n Polygon P(N);\n for(int i=0;i<N;i++) cin>>P[i].x>>P[i].y;\n P.push_back(P[0]);\n vector<double> T(M);\n vector<Vector> V(M);\n for(int i=1;i<M-1;i++) cin>>T[i]>>V[i].x>>V[i].y;\n T.back()=1e7;\n \n vector<double> ans;\n \n for(int q=0;q<B;q++){\n double when=INF;\n double t;\n Point p;\n Vector v;\n double len;\n cin>>t>>p.x>>v.x>>v.y>>len;\n Segment G;\n G.p1={p.x,0};\n G.p2=G.p1-v/abs(v)*len;\n for(int i=0;i<N;i++){\n Vector add={0,0};\n double last=0;\n int j=1;\n while(j<M){\n if(T[j]<=t){\n add=add+V[j]*(T[j]-last);\n last=T[j];\n j++;\n }else if(last<t){\n last=t;\n }else{\n double left=last,right=T[j];\n for(int q=0;q<80;q++){\n double m1=(left*2+right)/3,m2=(left+right*2)/3;\n Vector a1,a2,b1,b2;\n a1=add+V[j]*(m1-T[j-1]);\n a2=add+V[j]*(m2-T[j-1]);\n b1=v*(m1-t);\n b2=v*(m2-t);\n Segment s1={P[i]+a1,P[i+1]+a1};\n Segment s2={P[i]+a2,P[i+1]+a2};\n Segment t1={G.p1+b1,G.p2+b1};\n Segment t2={G.p1+b2,G.p2+b2};\n \n double d1=getDistance(s1,t1),d2=getDistance(s2,t2);\n if(d1==0){\n right=m2;\n chmin(when,m1);\n }\n else if(d1<d2) right=m2;\n else left=m1;\n }\n add=add+V[j]*(T[j]-T[j-1]);\n last=T[j];\n j++;\n }\n }\n }\n if(when<INF) ans.push_back(when);\n }\n \n sort(all(ans));\n \n cout<<si(ans)<<\"\\n\";\n for(auto a:ans) cout<<fixed<<setprecision(25)<<a<<\"\\n\";\n \n }\n}", "accuracy": 1, "time_ms": 6430, "memory_kb": 3576, "score_of_the_acc": -1.1912, "final_rank": 20 }, { "submission_id": "aoj_2023_5999810", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\n\n\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nld eq(ld a, ld b){\n\treturn abs(a - b) < eps;\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定2\nbool isis_ss(Line s, Line t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n//直線と点の距離\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n//直線と直線の距離\nld dist_ll(Line l, Line m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n//線分と直線の距離\nld dist_ls(Line l, Line s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n//線分と点の距離\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n//線分と線分の距離\nld dist_ss(Line s, Line t) {\n\tif (isis_ss(s, t))return 0;\n\treturn min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });\n}\n\nconst int mn = 10000000;\nld calc(ld lx, ld ly, ld rx, ld ry, ld px, ld py, ld vx, ld vy,ld len) {\n\tLine l = { {lx,ly},{rx,ry} };\n\tLine s = { {px,py},{px+len*vx,py+len*vy} };\n\tif (!isis_ss(s, l))return -1;\n\tPoint p = { px,py };\n\tif (ccw({ px,py }, { lx,ly }, { rx,ry }) % 2 == 0) {\n\t\t\n\t\tld dif = min(abs(Point{ lx,ly } - p), abs(Point{ rx,ry } - p));\n\t\tld vr = sqrt(vx * vx + vy * vy);\n\t\treturn dif / vr;\n\t}\n\tPoint c = is_ll(l, s);\n\tld dx = real(c) - px;\n\tld dy = imag(c) - py;\n\tif (vx != 0)return dx / vx;\n\treturn dy / vy;\n}\n\nint n, m, b;\nvoid solve() {\n\tvector<int> x(n), y(n);\n\trep(i, n)cin >> x[i] >> y[i];\n\tvector<int> t(m), vx(m), vy(m);\n\trep(i, m)cin >> t[i] >> vx[i] >> vy[i];\n\tt.insert(t.begin(), 0);\n\tt.push_back(mn);\n\tvx.push_back(0);\n\tvy.push_back(0);\n\tm++;\n\n\tvector<vector<int>> cx(n, vector<int>(m+1));\n\tvector<vector<int>> cy(n, vector<int>(m+1));\n\trep(i, n) {\n\t\tcx[i][0] = x[i];\n\t\tcy[i][0] = y[i];\n\t\trep(j, m) {\n\t\t\tcx[i][j + 1] = cx[i][j] + (t[j + 1] - t[j]) * vx[j];\n\t\t\tcy[i][j + 1] = cy[i][j] + (t[j + 1] - t[j]) * vy[j];\n\t\t}\n\t}\n\tvector<ld> ans;\n\trep(i, b) {\n\t\tint st; cin >> st;\n\t\tint sx; cin >> sx;\n\t\tint sy = 0;\n\t\tint dx, dy; cin >> dx >> dy;\n\t\tint len; cin >> len;\n\t\tld ex = dx / sqrt(dx * dx + dy * dy);\n\t\tld ey = dy / sqrt(dx * dx + dy * dy);\n\t\tld mi = INF;\n\t\t//弾丸につっこむ\n\t\trep(id, n) {\n\t\t\tld lx = sx;\n\t\t\tld ly = 0;\n\t\t\tld rx = sx - ex * len;\n\t\t\tld ry = sy - ey * len;\n\t\t\trep(j, m) {\n\t\t\t\tif (t[j + 1] <= st)continue;\n\t\t\t\tint dif = t[j + 1] - t[j];\n\t\t\t\tld px = cx[id][j];\n\t\t\t\tld py = cy[id][j];\n\t\t\t\tif (t[j] < st) {\n\t\t\t\t\tdif = t[j + 1] - st;\n\t\t\t\t\tpx += vx[j] * (st - t[j]);\n\t\t\t\t\tpy += vy[j] * (st - t[j]);\n\t\t\t\t}\n\t\t\t\tld val = calc(lx, ly, rx, ry, px, py, vx[j] - dx, vy[j] - dy,dif);\n\t\t\t\tif (val >= 0 && val <= dif) {\n\t\t\t\t\tmi = min(mi, val + max(t[j], st));\n\t\t\t\t\t//break;\n\t\t\t\t}\n\t\t\t\tlx += dx * dif;\n\t\t\t\tly += dy * dif;\n\t\t\t\trx += dx * dif;\n\t\t\t\try += dy * dif;\n\t\t\t}\n\t\t}\n\t\t//多角形につっこむ\n\t\trep(id, n) {\n\t\t\tint nid = (id + 1==n ? 0 : id + 1);\n\t\t\tld lx = sx;\n\t\t\tld ly = 0;\n\t\t\tld rx = sx - ex * len;\n\t\t\tld ry = sy - ey * len;\n\t\t\trep(j, m) {\n\t\t\t\tif (t[j + 1] <= st)continue;\n\t\t\t\tint dif = t[j + 1] - t[j];\n\t\t\t\tld px = cx[id][j];\n\t\t\t\tld py = cy[id][j];\n\t\t\t\tld qx = cx[nid][j];\n\t\t\t\tld qy = cy[nid][j];\n\t\t\t\tif (t[j] < st) {\n\t\t\t\t\tdif = t[j + 1] - st;\n\t\t\t\t\tpx += vx[j] * (st - t[j]);\n\t\t\t\t\tqx += vx[j] * (st - t[j]);\n\t\t\t\t\tpy += vy[j] * (st - t[j]);\n\t\t\t\t\tqy += vy[j] * (st - t[j]);\n\t\t\t\t}\n\t\t\t\t//bool upd = false;\n\t\t\t\tld val = calc(px, py, qx, qy, lx, ly, dx-vx[j], dy-vy[j],dif);\n\t\t\t\t\n\t\t\t\tif (val >= 0 && val <= dif) {\n\t\t\t\t\tmi = min(mi, val + max(t[j], st));\n\t\t\t\t\t//upd = true;\n\t\t\t\t\t//break;\n\t\t\t\t}\n\t\t\t\tval = calc(px, py, qx, qy, rx, ry, dx - vx[j], dy - vy[j],dif);\n\t\t\t\tif (val >= 0 && val <= dif) {\n\t\t\t\t\tmi = min(mi, val + max(t[j], st));\n\t\t\t\t\t//upd = true;\n\t\t\t\t\t//break;\n\t\t\t\t}\n\t\t\t\t//if (upd)break;\n\t\t\t\tlx += dx * dif;\n\t\t\t\tly += dy * dif;\n\t\t\t\trx += dx * dif;\n\t\t\t\try += dy * dif;\n\t\t\t}\n\t\t}\n\t\tif (mi < INF) {\n\t\t\t//cout << mi << \"\\n\";\n\t\t\tans.push_back(mi);\n\t\t}\n\t\t/*rep(id, n) {\n\t\t\tint nid = id + 1 == n ? 0 : id + 1;\n\t\t\tint x2 = sx;\n\t\t\tint y2 = sy;\n\t\t\trep(j, m) {\n\t\t\t\tif (t[j + 1] <= st)continue;\n\t\t\t\tint dif = t[j + 1] - t[j];\n\t\t\t\tint x1 = cx[id][j];\n\t\t\t\tint y1 = cy[id][j];\n\t\t\t\tint x2=\n\t\t\t}\n\t\t}*/\n\t}\n\tsort(all(ans));\n\tcout << ans.size() << \"\\n\";\n\trep(i, ans.size()) {\n\t\tcout << ans[i] << \"\\n\";\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile(cin>>n>>m>>b,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3700, "score_of_the_acc": -0.2102, "final_rank": 15 }, { "submission_id": "aoj_2023_5968908", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\nusing Real = long double;\nusing Point = complex< Real >;\nusing Polygon = vector< Point >;\nconst Real EPS = 1e-9, PI = acos(-1);\nconstexpr int COUNTER_CLOCKWISE = +1;\nconstexpr int CLOCKWISE = -1;\nconstexpr int ONLINE_BACK = +2; // c-a-b\nconstexpr int ONLINE_FRONT = -2; // a-b-c\nconstexpr int ON_SEGMENT = 0; // a-c-b\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nPoint operator/(const Point &p, const Real &d) {\n return Point(real(p) / d, imag(p) / d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b; is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, const Point &p) {\n return os << fixed << setprecision(10) << real(p) << \" \" << imag(p);\n}\n\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\n// 符号\ninline int sign(const Real &r) {\n return r <= -EPS ? -1 : r >= EPS ? 1 : 0;\n}\n\n// a = b ? \ninline bool equals(const Real &a, const Real &b) {\n return sign(a - b) == 0;\n}\n\n\n// 単位ベクトル\nPoint unitVec(const Point &a){ return a / abs(a); }\n\n// 法線ベクトル 半時計周り\nPoint normVec(const Point &a){ return a * Point(0, 1); }\n//Point normVec(const Point &a){ return a * Point(0,-1); }\n\n// 内積\nReal dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n\n// 外積\nReal cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nPoint rotate(const Point &p, const Real &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal Radian_To_Degree(const Real &radian){ return radian * 180.0 / PI; }\nReal Degree_To_Radian(const Real &degree){ return degree * PI / 180.0; }\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n\n // Ax + By = C\n Line(Real A, Real B, Real C) {\n if(equals(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n }else if(equals(B,0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n }else{\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.p << \" \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.p >> c.r;\n }\n};\n\n// 直線lに点pから引いた垂線の足を求める\nPoint projection(const Line &l, const Point &p) {\n Real t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n Real t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// 直線lに関して点pと対称な点を求める\nPoint reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * 2.0;\n}\n\n// 点の回転方向\n// 点aを基準に点b,cの位置関係\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if(sign(cross(b, c)) == +1) return COUNTER_CLOCKWISE; // 反時計回り\n if(sign(cross(b, c)) == -1) return CLOCKWISE; // 時計回り\n if(sign(dot(b, c)) == -1) return ONLINE_BACK; // c - a - b\n if(norm(b) < norm(c)) return ONLINE_FRONT; // a - b - c\n return ON_SEGMENT; // a - c - b\n}\n\n// 2直線の直交判定\nbool is_orthogonal(const Line &a, const Line &b) {\n return equals(dot(a.b - a.a, b.b - b.a), 0);\n}\n\n// 2直線の平行判定\nbool is_parallel(const Line &a, const Line &b) {\n return equals(cross(a.b - a.a, b.b - b.a), 0);\n}\n\n// 線分の交差判定\nbool is_intersect_ss(const Segment &s, const Segment &t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 直線の交差判定\nbool is_intersect_ll(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(equals(abs(A), 0) && equals(abs(B), 0)) return true;\n return !is_parallel(l, m);\n}\n\n// 線分に点が乗っているか\nbool is_intersect_sp(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == ON_SEGMENT;\n}\n\n// 点と点の距離\nReal distance_pp(const Point &p1, const Point &p2) {\n return sqrt(pow(real(p1)-real(p2), 2) + pow(imag(p1)-imag(p2), 2));\n}\n\n// 点と直線の距離\nReal distance_lp(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\n// 直線と直線の距離\nReal distance_ll(const Line &l, const Line &m) {\n return is_intersect_ll(l, m) ? 0 : distance_lp(l, m.a);\n}\n\n// 点と線分の距離\nReal distance_sp(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if(is_intersect_sp(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nReal distance_ss(const Segment &a, const Segment &b) {\n if(is_intersect_ss(a, b)) return 0;\n return min({distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b)});\n}\n\n// 交点\nPoint cross_point_ll(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(equals(abs(A), 0) && equals(abs(B), 0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// 円と円の位置関係\nint intersect(Circle c1, Circle c2) {\n if(c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r < d) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(c1.r - c2.r < d) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\nReal area(const Polygon &p){\n Real A = 0;\n for(int i = 0; i < p.size(); i++){\n A += cross(p[i], p[(i + 1) % p.size()]);\n }\n return A * 0.5;\n}\n\nbool is_convex(const Polygon &p){\n for(int i = 0; i < p.size(); i++){\n if(ccw(p[(i + p.size() - 1) % p.size()], p[i], p[(i + 1) % p.size()]) == -1) return false;\n }\n return true;\n}\n\nenum { OUT, ON, IN };\nint contains(const Polygon &Q, const Point &p){\n bool in = false;\n for(int i = 0; i < Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\nPolygon convex_hull(Polygon &p, bool strict = true){\n int n = (int)p.size(), k = 0;\n if(n <= 2) return p;\n sort(p.begin(), p.end());\n Polygon ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]){\n while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]){\n while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\nReal convex_diameter(const Polygon &p){\n int n = (int)p.size();\n int is = 0, js = 0;\n for(int i = 1; i < n; i++){\n if(p[i].imag() > p[is].imag()) is = i;\n if(p[i].imag() < p[js].imag()) js = i;\n }\n Real maxdis = norm(p[is] - p[js]);\n int maxi, maxj, i, j;\n i = maxi = is;\n j = maxj = js;\n do {\n if(cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0){\n j = (j + 1) % n;\n }else{\n i = (i + 1) % n;\n }\n\n if(norm(p[i] - p[j]) > maxdis){\n maxdis = norm(p[i] - p[j]);\n maxi = i;\n maxj = j;\n }\n\n }while(i != is || j != js);\n return sqrt(maxdis);\n}\n\nPolygon convex_cut(const Polygon &U, Line l){\n Polygon ret;\n for(int i = 0; i < U.size(); i++){\n Point now = U[i], nxt = U[(i + 1) % U.size()];\n if(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0){\n ret.push_back(cross_point_ll(Line(now, nxt), l));\n }\n }\n return ret;\n}\n\npair< Point, Point > cross_point_cc(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\nconst int BOTTOM = 0;\nconst int LEFT = 1;\nconst int RIGHT = 2;\nconst int TOP = 3;\n\nclass endPoint {\n public:\n Point p;\n int seg; // id of Point\n int st; // kind of Point\n endPoint(){}\n endPoint(Point inp, int inseg, int inst):p(inp),seg(inseg),st(inst){}\n bool operator<(const endPoint &ep)const {\n if(p.imag() == ep.p.imag()) return st < ep.st;\n else return p.imag() < ep.p.imag();\n }\n};\n\nendPoint EP[220000];\nint Manhattan_Intersections(vector< Segment > s){\n int n = s.size();\n double hoge;\n\n for(int i = 0, k = 0; i < n; i++){\n if(s[i].a.imag() == s[i].b.imag()){\n if(s[i].a.real() > s[i].b.real()){\n hoge = s[i].a.real();\n s[i].a.real(s[i].b.real());\n s[i].b.real(hoge);\n }\n }else if(s[i].a.imag() > s[i].b.imag()){\n hoge = s[i].a.imag();\n s[i].a.imag(s[i].b.imag());\n s[i].b.imag(hoge);\n }\n\n if(s[i].a.imag() == s[i].b.imag()){\n EP[k++] = endPoint(s[i].a, i, LEFT);\n EP[k++] = endPoint(s[i].b, i, RIGHT);\n }else{\n EP[k++] = endPoint(s[i].a, i, BOTTOM);\n EP[k++] = endPoint(s[i].b, i, TOP);\n }\n }\n\n sort(EP, EP + 2 * n);\n set<int> BT;\n BT.insert(1000000001);\n int cnt = 0;\n for(int i = 0; i < 2 * n; i++){\n if(EP[i].st == TOP) BT.erase(EP[i].p.real());\n else if(EP[i].st == BOTTOM) BT.insert(EP[i].p.real());\n else if(EP[i].st == LEFT){\n set<int>::iterator b = lower_bound(BT.begin(), BT.end(), s[EP[i].seg].a.real());\n set<int>::iterator e = upper_bound(BT.begin(), BT.end(), s[EP[i].seg].b.real());\n cnt += distance(b, e);\n }\n }\n\n return cnt;\n}\n\n// 内接円\nCircle Inscribed_Circle(const Point& a, const Point& b, const Point& c){\n double A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point x = Point((a * A + b * B + c * C) / (A + B + C));\n double r = distance_sp(Segment(a,b),x);\n return Circle(x, r);\n}\n\n// 外接円\nCircle Circumscribed_Circle(const Point& a, const Point& b, const Point& c){\n Point m1((a+b)/2.0), m2((b+c)/2.0);\n Point v((b-a).imag(), (a-b).real()), w((b-c).imag(), (c-b).real());\n Line s(m1, Point(m1+v)), t(m2, Point(m2+w));\n const Point x = cross_point_ll(s, t);\n return Circle(x, abs(a-x));\n}\n\npair< Point, Point > cross_point_cl(const Circle &c, const Line &l) {\n Point pr = projection(l, c.p);\n if(equals(abs(pr - c.p), c.r)) return {pr, pr};\n Point e = (l.b - l.a) / abs(l.b - l.a);\n auto k = sqrt(norm(c.r) - norm(pr - c.p));\n return {pr - e * k, pr + e * k};\n}\n\n// 点pを通る円の接線\npair<Point,Point> tangent_cp(const Circle &c, const Point &p) {\n return cross_point_cc(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\n\n// 二つの円の共通接線\nvector< Line > tangent_cc(Circle c1, Circle c2){\n vector< Line > ret;\n if(c1.r < c2.r) swap(c1,c2);\n double g = norm(c1.p-c2.p);\n if(equals(g,0.0)) return ret;\n Point u = (c2.p-c1.p)/sqrt(g);\n Point v = rotate(u, PI * 0.5);\n for(int s:{-1,1}){\n double h = (c1.r+ s*c2.r)/sqrt(g);\n if(equals(1- h*h, 0)){\n ret.emplace_back(c1.p+u*c1.r, c1.p+(u+v)*c1.r);\n }else if(1-h*h > 0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ret.emplace_back(c1.p+(uu+vv)*c1.r, c2.p-(uu+vv)*c2.r*s);\n ret.emplace_back(c1.p+(uu-vv)*c1.r, c2.p-(uu-vv)*c2.r*s);\n }\n }\n return ret;\n}\n\nReal area_poly_c(const Polygon &p, const Circle &c) {\n if(p.size() < 3) return 0.0;\n function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if(equals(f, 0.0)) return ret;\n if(max(abs(va), abs(vb)) < c.r + EPS) return f;\n if(distance_sp(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n auto u = cross_point_cl(c, Segment(a, b));\n vector< Point > tot{a, u.first, u.second, b};\n for(int i = 0; i + 1 < tot.size(); i++) {\n ret += cross_area(c, tot[i], tot[i + 1]);\n }\n return ret;\n };\n Real A = 0;\n for(int i = 0; i < p.size(); i++) {\n A += cross_area(c, p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\nReal area_cc(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r <= d + EPS) return 0.0;\n if(d <= fabs(c1.r - c2.r) + EPS) {\n Real r = min(c1.r, c2.r);\n return r * r * PI;\n }\n Real rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n Real theta = acos(rc / c1.r);\n Real phi = acos((d - rc) / c2.r);\n return (c1.r * c1.r*theta + c2.r*c2.r*phi - d*c1.r*sin(theta));\n}\n\nbool is_intersect_pp(Polygon p, Polygon q) {\n rep(i,p.size()) {\n if(contains(q, p[i])) return true;\n rep(j,q.size()) {\n Segment pl(p[i], p[(i + 1) % p.size()]);\n Segment ql(q[j], q[(j + 1) % q.size()]);\n if(is_intersect_ss(pl, ql)) return true;\n }\n }\n return false;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n while(true) {\n int N,M,B; cin >> N >> M >> B; if(N == 0 && M == 0 && B == 0) break;\n Polygon p(N); rep(i,N) cin >> p[i];\n vector< Real > t(M); Polygon v(M);\n rep(i,M) cin >> t[i] >> v[i];\n t.push_back(1e9); v.push_back({0, 0}); M++;\n for(int i = M - 1; i >= 1; i--) t[i] -= t[i - 1];\n\n vector< Real > ans;\n rep(_,B) {\n Real T,X,L; Point vb; cin >> T >> X >> vb >> L;\n Segment bullet(Point(X, 0) - vb * T, Point(X, 0) - vb / abs(vb) * L - vb * T);\n Real cur = 0;\n rep(i,M) {\n Point vc = vb - v[i];\n auto f = [&](Real x) {\n Polygon q = {bullet.a, bullet.b, bullet.b + vc * x, bullet.a + vc * x};\n return is_intersect_pp(p, q);\n };\n\n if(f(t[i])) {\n Real lo = 0.0, hi = t[i];\n rep(__,50) { Real mid = (lo + hi) * 0.5; (f(mid) ? hi : lo) = mid; }\n ans.push_back(cur + (lo + hi) * 0.5);\n break;\n } else {\n cur += t[i];\n bullet.a += vc * t[i];\n bullet.b += vc * t[i];\n }\n }\n }\n\n sort(ans.begin(), ans.end());\n cout << ans.size() << endl;\n cout.precision(17);\n for(Real a : ans) cout << a << '\\n';\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 13916, "score_of_the_acc": -1.0187, "final_rank": 19 }, { "submission_id": "aoj_2023_5803967", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.19 17:53:15 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nvoid solve() {\n\tint n, m, b;\n\tcin >> n >> m >> b;\n\tif(!n) exit(0);\n\n\tPolygon mybody(n);\n\tfoa(p, mybody) cin >> p;\n\n\tV<tuple<R, R, Vec>> move_me;\n\tR start_time = 0;\n\trep(m) {\n\t\tLD(t, vx, vy);\n\t\tmove_me.emplace_back(start_time, t, Vec(vx, vy));\n\t\tstart_time = t;\n\t}\n\tmove_me.emplace_back(start_time, 1e19, Vec(0, 0));\n\n\tauto f = [&](Segment s, Point a, Vec va) -> R {\n\t\tPoint b = a + va;\n\t\tLine orbit(a, b);\n\t\tif(intersect(orbit, s)) {\n\t\t\tPoint crs = crosspoint(orbit, s);\n\t\t\tif(ccw(s.a, s.b, crs)) return R(INF);\n\t\t\tif(dot(va, crs - a) < -EPS) return R(INF);\n\t\t\t\n\t\t\treturn distance(crs, a) / abs(va);\n\t\t}\n\t\treturn R(INF);\n\t};\n\n\tauto collision = [&](Segment a, Vec va, Segment b, Vec vb) {\n\t\tR ret = R(INF);\n\t\tchmin(ret, f(a, b.a, vb - va));\n\t\tchmin(ret, f(a, b.b, vb - va));\n\t\tchmin(ret, f(b, a.a, va - vb));\n\t\tchmin(ret, f(b, a.b, va - vb));\n\t\treturn ret;\n\t};\n\n\tV<R> ans;\n\n\trep(b) {\n\t\tR launch_time, launch_x;\n\t\tVec v_arrow;\n\t\tR arrow_length;\n\t\tcin >> launch_time >> launch_x >> v_arrow >> arrow_length;\n\n\t\tPoint arrow_hole(launch_x, 0);\n\t\tSegment arrow_begin(arrow_hole, arrow_hole - v_arrow / abs(v_arrow) * arrow_length);\n\n\t\tVec moved_me(0, 0);\n\n\t\tR answer = INF;\n\n\t\tfor(auto [start_time, end_time, v_me] : move_me) {\n\t\t\trep(i, n) {\n\t\t\t\tSegment edge(mybody[i] + moved_me, mybody[(i + 1) % n] + moved_me);\n\n\t\t\t\tSegment arrow_start_time(arrow_begin.a + v_arrow * (start_time - launch_time),\n\t\t\t\t\t\t\t\t\t\t arrow_begin.b + v_arrow * (start_time - launch_time));\n\t\t\t\tR res = collision(edge, v_me, arrow_start_time, v_arrow);\n\t\t\t\tif(!isnan(res) && 0 <= res && res <= end_time - start_time) {\n\t\t\t\t\tchmin(answer, res + start_time);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmoved_me += v_me * (end_time - start_time);\n\t\t}\n\n\t\tif(answer < 1e16) ans.push_back(answer);\n\t}\n\n\tprint(ans.size());\n\tsort(all(ans));\n\tvout(ans, 1);\n\treturn;\n}\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\twhile(1) solve();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3444, "score_of_the_acc": -0.1902, "final_rank": 13 }, { "submission_id": "aoj_2023_5452720", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double INF = 2000000;\nconst double EPS = 0.0000001;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble dot(point P, point Q){\n return P.x * Q.x + P.y * Q.y;\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\nstruct line{\n point A, B;\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\nbool point_on_segment(point P, line L){\n return abs(cross(P - L.A, vec(L))) < EPS && dot(P - L.A, vec(L)) > 0 && dot(P - L.B, vec(L)) < 0;\n}\nbool segment_intersects(line L1, line L2){\n if (cross(L2.A - L1.A, vec(L1)) * cross(L2.B - L1.A, vec(L1)) < 0){\n if (cross(L1.A - L2.A, vec(L2)) * cross(L1.B - L2.A, vec(L2)) < 0){\n return true;\n }\n }\n return false;\n}\nbool point_in_triangle(point P, point A, point B, point C){\n if (cross(B - A, P - A) > 0 && cross(C - B, P - B) > 0 && cross(A - C, P - C) > 0){\n return true;\n }\n if (cross(B - A, P - A) < 0 && cross(C - B, P - B) < 0 && cross(A - C, P - C) < 0){\n return true;\n }\n return false;\n}\nbool segment_triangle_intersect(line L, point A, point B, point C){\n if (segment_intersects(L, line(A, B))){\n return true;\n }\n if (segment_intersects(L, line(B, C))){\n return true;\n }\n if (segment_intersects(L, line(C, A))){\n return true;\n }\n if (point_in_triangle(L.A, A, B, C)){\n return true;\n }\n if (point_in_triangle(L.B, A, B, C)){\n return true;\n }\n if (point_on_segment(A, L)){\n return true;\n }\n if (point_on_segment(B, L)){\n return true;\n }\n if (point_on_segment(C, L)){\n return true;\n }\n return false;\n}\nint main(){\n cout << fixed << setprecision(20);\n while (true){\n int N, M, B;\n cin >> N >> M >> B;\n if (N == 0 && M == 0 && B == 0){\n break;\n }\n vector<point> P(N + 1);\n for (int i = 0; i < N; i++){\n cin >> P[i].x >> P[i].y;\n }\n P[N] = P[0];\n vector<double> T(M + 2);\n vector<point> V(M + 1);\n T[0] = 0;\n for (int i = 0; i < M; i++){\n cin >> T[i + 1] >> V[i].x >> V[i].y;\n }\n T[M + 1] = INF;\n V[M].x = 0;\n V[M].y = 0;\n M++;\n vector<double> ans;\n for (int i = 0; i < B; i++){\n double t;\n point S1, W;\n double L;\n cin >> t >> S1.x >> W.x >> W.y >> L;\n S1.y = 0;\n point S2 = S1 - W / abs(W) * L;\n line X(S1, S2);\n int p = lower_bound(T.begin(), T.end(), t) - T.begin();\n vector<double> T2 = T;\n vector<point> V2 = V;\n T2.insert(T2.begin() + p, t);\n V2.insert(V2.begin() + p, V[p - 1]);\n for (int j = p; j <= M; j++){\n V2[j] = V2[j] - W;\n }\n vector<point> P2 = P;\n double mn = INF;\n for (int j = 0; j <= M; j++){\n if (j >= p){\n bool ok = false;\n vector<point> Q(N + 1);\n for (int k = 0; k <= N; k++){\n Q[k] = P2[k] + V2[j] * (T2[j + 1] - T2[j]);\n }\n for (int k = 0; k < N; k++){\n if (segment_triangle_intersect(X, P2[k], P2[k + 1], Q[k])){\n ok = true;\n }\n if (segment_triangle_intersect(X, P2[k + 1], Q[k], Q[k + 1])){\n ok = true;\n }\n }\n if (ok){\n double tv = T2[j + 1] - T2[j], fv = 0;\n for (int k = 0; k < 50; k++){\n double mid = (tv + fv) / 2;\n for (int l = 0; l <= N; l++){\n Q[l] = P2[l] + V2[j] * mid;\n }\n ok = false;\n for (int l = 0; l < N; l++){\n if (segment_triangle_intersect(X, P2[l], P2[l + 1], Q[l])){\n ok = true;\n }\n if (segment_triangle_intersect(X, P2[l + 1], Q[l], Q[l + 1])){\n ok = true;\n }\n }\n if (ok){\n tv = mid;\n } else {\n fv = mid;\n }\n }\n mn = T2[j] + tv;\n break;\n }\n }\n for (int k = 0; k <= N; k++){\n P2[k] = P2[k] + V2[j] * (T2[j + 1] - T2[j]);\n }\n }\n if (mn != INF){\n ans.push_back(mn);\n }\n }\n sort(ans.begin(), ans.end());\n int n = ans.size();\n cout << n << endl;\n for (int i = 0; i < n; i++){\n cout << ans[i] << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3304, "score_of_the_acc": -0.1715, "final_rank": 12 }, { "submission_id": "aoj_2023_4761030", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 105\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tPoint p[2];\n};\n\nstruct Info{\n\n\tdouble T,vx,vy;\n};\n\nstruct Ball{\n\n\tdouble start_T,x,vx,vy,len,speed;\n};\n\nint N,M,B;\nint num_line;\ndouble min_time[SIZE];\nvector<double> ans;\nPolygon P;\nLine line[SIZE],work[SIZE];\nInfo info[SIZE];\nBall ball[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nvoid func(){\n\n\tP.clear();\n\tans.clear();\n\n\tdouble x,y;\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\t//線分にする\n\tnum_line = 0;\n\tfor(int i = 0; i < N; i++){\n\n\t\tline[num_line].p[0] = P[i];\n\t\tline[num_line].p[1] = P[(i+1)%N];\n\t\tnum_line++;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%lf %lf %lf\",&info[i].T,&info[i].vx,&info[i].vy);\n\t}\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tscanf(\"%lf %lf %lf %lf %lf\",&ball[i].start_T,&ball[i].x,&ball[i].vx,&ball[i].vy,&ball[i].len);\n\n\t\t//★?★\n\t\t//ball[i].vx *= -1;\n\t\t//ball[i].vy *= -1;\n\t\tball[i].speed = sqrt(ball[i].vx*ball[i].vx+ball[i].vy*ball[i].vy);\n\t}\n\t//ダミー動作\n\tinfo[M].T = BIG_NUM;\n\tinfo[M].vx = 0;\n\tinfo[M].vy = 0;\n\tM++;\n\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tmin_time[i] = BIG_NUM;//弾丸目線\n\t}\n\n\tdouble add_x,add_y;\n\tdouble ball_x,ball_y;\n\tdouble span,diff_x,diff_y;\n\tdouble tmp_vx,tmp_vy;\n\n\tLine ball_line;\n\tLine tmp_line;\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tadd_x = 0;\n\t\tadd_y = 0;\n\n\t\tball_x = ball[i].x;\n\t\tball_y = 0;\n\n\t\tdouble current = 0;\n\n\t\tfor(int k = 0; k < M; k++){\n\n\t\t\t//pre-info[k].Tの間\n\t\t\tif(ball[i].start_T > info[k].T+EPS){\n\n\t\t\t\tadd_x += (info[k].T-current)*info[k].vx;\n\t\t\t\tadd_y += (info[k].T-current)*info[k].vy;\n\n\t\t\t\tcurrent = info[k].T;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(ball[i].start_T > current+EPS){\n\n\t\t\t\tadd_x += (ball[i].start_T-current)*info[k].vx;\n\t\t\t\tadd_y += (ball[i].start_T-current)*info[k].vy;\n\t\t\t\tcurrent = ball[i].start_T;\n\t\t\t}\n\n\t\t\tfor(int a = 0; a < num_line; a++){\n\n\t\t\t\twork[a].p[0].x = line[a].p[0].x+add_x;\n\t\t\t\twork[a].p[0].y = line[a].p[0].y+add_y;\n\n\t\t\t\twork[a].p[1].x = line[a].p[1].x+add_x;\n\t\t\t\twork[a].p[1].y = line[a].p[1].y+add_y;\n\t\t\t}\n\n\n\t\t\tball_line.p[0] = Point(ball_x,ball_y);\n\t\t\tball_line.p[1] = Point(ball_x-(ball[i].len/ball[i].speed)*ball[i].vx,\n\t\t\t\t\t\t\t\t ball_y-(ball[i].len/ball[i].speed)*ball[i].vy);\n\n\t\t\t//弾丸静止\n\t\t\tspan = info[k].T-current;\n\t\t\ttmp_vx = info[k].vx-ball[i].vx;\n\t\t\ttmp_vy = info[k].vy-ball[i].vy;\n\n\t\t\tdiff_x = span*tmp_vx;\n\t\t\tdiff_y = span*tmp_vy;\n\n\t\t\tfor(int a = 0; a < num_line; a++){\n\t\t\t\tfor(int b = 0; b < 2; b++){\n\n\t\t\t\t\ttmp_line.p[0] = work[a].p[b];\n\t\t\t\t\ttmp_line.p[1].x = tmp_line.p[0].x+diff_x;\n\t\t\t\t\ttmp_line.p[1].y = tmp_line.p[0].y+diff_y;\n\n\t\t\t\t\tif(is_Cross(ball_line,tmp_line)){\n\n\t\t\t\t\t\tPoint cross_point = calc_Cross_Point(tmp_line,ball_line);\n\t\t\t\t\t\tdouble all_len = calc_dist(tmp_line.p[0],tmp_line.p[1]);\n\t\t\t\t\t\tdouble add_time = (calc_dist(cross_point,tmp_line.p[0])/all_len)*span;\n\n\t\t\t\t\t\tmin_time[i] = min(min_time[i],current+add_time);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//多角形静止\n\t\t\ttmp_vx = ball[i].vx-info[k].vx;\n\t\t\ttmp_vy = ball[i].vy-info[k].vy;\n\n\t\t\tdiff_x = span*tmp_vx;\n\t\t\tdiff_y = span*tmp_vy;\n\n\t\t\tfor(int b = 0; b < 2; b++){\n\n\t\t\t\ttmp_line.p[0] = ball_line.p[b];\n\t\t\t\ttmp_line.p[1].x = tmp_line.p[0].x+diff_x;\n\t\t\t\ttmp_line.p[1].y = tmp_line.p[0].y+diff_y;\n\n\t\t\t\tfor(int a = 0; a < num_line; a++){\n\t\t\t\t\tif(is_Cross(tmp_line,work[a])){\n\n\t\t\t\t\t\tPoint cross_point = calc_Cross_Point(tmp_line,work[a]);\n\t\t\t\t\t\tdouble all_len = calc_dist(tmp_line.p[0],tmp_line.p[1]);\n\t\t\t\t\t\tdouble add_time = (calc_dist(cross_point,tmp_line.p[0])/all_len)*span;\n\n\t\t\t\t\t\tmin_time[i] = min(min_time[i],current+add_time);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tadd_x += span*info[k].vx;\n\t\t\tadd_y += span*info[k].vy;\n\n\t\t\tball_x += span*ball[i].vx;\n\t\t\tball_y += span*ball[i].vy;\n\n\t\t\tcurrent = info[k].T;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tif(fabs(min_time[i]-BIG_NUM) < EPS)continue;\n\t\tans.push_back(min_time[i]);\n\t}\n\tsort(ans.begin(),ans.end());\n\n\tprintf(\"%lld\\n\",ans.size());\n\tfor(int i = 0; i < ans.size(); i++){\n\n\t\tprintf(\"%.10lf\\n\",ans[i]);\n\t}\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&N,&M,&B);\n\t\tif(N == 0 && M == 0 && B == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3288, "score_of_the_acc": -0.1686, "final_rank": 11 }, { "submission_id": "aoj_2023_4761028", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 105\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tvoid outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}\n\tPoint p[2];\n};\n\nstruct Info{\n\n\tdouble T,vx,vy;\n};\n\nstruct Ball{\n\n\tdouble start_T,x,vx,vy,len,speed;\n};\n\nint N,M,B;\nint num_line;\ndouble min_time[SIZE];\nvector<double> ans;\nPolygon P;\nLine line[SIZE],work[SIZE];\nInfo info[SIZE];\nBall ball[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nvoid func(){\n\n\tP.clear();\n\tans.clear();\n\n\tdouble x,y;\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\t//線分にする\n\tnum_line = 0;\n\tfor(int i = 0; i < N; i++){\n\n\t\tline[num_line].p[0] = P[i];\n\t\tline[num_line].p[1] = P[(i+1)%N];\n\t\tnum_line++;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%lf %lf %lf\",&info[i].T,&info[i].vx,&info[i].vy);\n\t}\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tscanf(\"%lf %lf %lf %lf %lf\",&ball[i].start_T,&ball[i].x,&ball[i].vx,&ball[i].vy,&ball[i].len);\n\n\t\t//★?★\n\t\t//ball[i].vx *= -1;\n\t\t//ball[i].vy *= -1;\n\t\tball[i].speed = sqrt(ball[i].vx*ball[i].vx+ball[i].vy*ball[i].vy);\n\t}\n\t//ダミー動作\n\tinfo[M].T = BIG_NUM;\n\tinfo[M].vx = 0;\n\tinfo[M].vy = 0;\n\tM++;\n\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tmin_time[i] = BIG_NUM;//弾丸目線\n\t}\n\n\tdouble add_x,add_y;\n\tdouble ball_x,ball_y;\n\tdouble span,diff_x,diff_y;\n\tdouble tmp_vx,tmp_vy;\n\n\tLine ball_line;\n\tLine tmp_line;\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tadd_x = 0;\n\t\tadd_y = 0;\n\n\t\tball_x = ball[i].x;\n\t\tball_y = 0;\n\n\t\tdouble current = 0;\n\n\t\tfor(int k = 0; k < M; k++){\n\n\t\t\t//pre-info[k].Tの間\n\t\t\tif(ball[i].start_T > info[k].T+EPS){\n\n\t\t\t\tadd_x += (info[k].T-current)*info[k].vx;\n\t\t\t\tadd_y += (info[k].T-current)*info[k].vy;\n\n\t\t\t\tcurrent = info[k].T;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(ball[i].start_T > current+EPS){\n\n\t\t\t\tadd_x += (ball[i].start_T-current)*info[k].vx;\n\t\t\t\tadd_y += (ball[i].start_T-current)*info[k].vy;\n\t\t\t\tcurrent = ball[i].start_T;\n\t\t\t}\n\n\t\t\tfor(int a = 0; a < num_line; a++){\n\n\t\t\t\twork[a].p[0].x = line[a].p[0].x+add_x;\n\t\t\t\twork[a].p[0].y = line[a].p[0].y+add_y;\n\n\t\t\t\twork[a].p[1].x = line[a].p[1].x+add_x;\n\t\t\t\twork[a].p[1].y = line[a].p[1].y+add_y;\n\t\t\t}\n\n\n\t\t\tball_line.p[0] = Point(ball_x,ball_y);\n\t\t\tball_line.p[1] = Point(ball_x-(ball[i].len/ball[i].speed)*ball[i].vx,\n\t\t\t\t\t\t\t\t ball_y-(ball[i].len/ball[i].speed)*ball[i].vy);\n\n\t\t\t//printf(\"弾丸(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",ball_line.p[0].x,ball_line.p[0].y,ball_line.p[1].x,ball_line.p[1].y);\n\n\t\t\t//弾丸静止\n\t\t\tspan = info[k].T-current;\n\t\t\ttmp_vx = info[k].vx-ball[i].vx;\n\t\t\ttmp_vy = info[k].vy-ball[i].vy;\n\n\t\t\tdiff_x = span*tmp_vx;\n\t\t\tdiff_y = span*tmp_vy;\n\n\t\t\t//printf(\"current:%.3lf vx:%.3lf vy:%.3lf diff_x:%.3lf diff_y:%.3lf\\n\",current,tmp_vx,tmp_vy,diff_x,diff_y);\n\n\t\t\tfor(int a = 0; a < num_line; a++){\n\t\t\t\tfor(int b = 0; b < 2; b++){\n\n\t\t\t\t\ttmp_line.p[0] = work[a].p[b];\n\t\t\t\t\ttmp_line.p[1].x = tmp_line.p[0].x+diff_x;\n\t\t\t\t\ttmp_line.p[1].y = tmp_line.p[0].y+diff_y;\n\n\t\t\t\t\tif(is_Cross(ball_line,tmp_line)){\n\n\t\t\t\t\t\tPoint cross_point = calc_Cross_Point(tmp_line,ball_line);\n\t\t\t\t\t\tdouble all_len = calc_dist(tmp_line.p[0],tmp_line.p[1]);\n\t\t\t\t\t\tdouble add_time = (calc_dist(cross_point,tmp_line.p[0])/all_len)*span;\n\n\t\t\t\t\t\tmin_time[i] = min(min_time[i],current+add_time);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//多角形静止\n\t\t\ttmp_vx = ball[i].vx-info[k].vx;\n\t\t\ttmp_vy = ball[i].vy-info[k].vy;\n\n\t\t\tdiff_x = span*tmp_vx;\n\t\t\tdiff_y = span*tmp_vy;\n\n\t\t\t//printf(\"多角形静止 current:%.3lf vx:%.3lf vy:%.3lf diff_x:%.3lf diff_y:%.3lf\\n\",current,tmp_vx,tmp_vy,diff_x,diff_y);\n\n\t\t\tfor(int b = 0; b < 2; b++){\n\n\t\t\t\ttmp_line.p[0] = ball_line.p[b];\n\t\t\t\ttmp_line.p[1].x = tmp_line.p[0].x+diff_x;\n\t\t\t\ttmp_line.p[1].y = tmp_line.p[0].y+diff_y;\n\n\t\t\t\t//printf(\"b:%d\\n\",b);\n\t\t\t\t//tmp_line.outPut();\n\n\t\t\t\tfor(int a = 0; a < num_line; a++){\n\t\t\t\t\tif(is_Cross(tmp_line,work[a])){\n\n\t\t\t\t\t\tPoint cross_point = calc_Cross_Point(tmp_line,work[a]);\n\t\t\t\t\t\tdouble all_len = calc_dist(tmp_line.p[0],tmp_line.p[1]);\n\t\t\t\t\t\tdouble add_time = (calc_dist(cross_point,tmp_line.p[0])/all_len)*span;\n\n\t\t\t\t\t\tmin_time[i] = min(min_time[i],current+add_time);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tadd_x += span*info[k].vx;\n\t\t\tadd_y += span*info[k].vy;\n\n\t\t\tball_x += span*ball[i].vx;\n\t\t\tball_y += span*ball[i].vy;\n\n\t\t\tcurrent = info[k].T;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < B; i++){\n\n\t\tif(fabs(min_time[i]-BIG_NUM) < EPS)continue;\n\t\tans.push_back(min_time[i]);\n\t}\n\tsort(ans.begin(),ans.end());\n\n\tprintf(\"%lld\\n\",ans.size());\n\tfor(int i = 0; i < ans.size(); i++){\n\n\t\tprintf(\"%.10lf\\n\",ans[i]);\n\t}\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&N,&M,&B);\n\t\tif(N == 0 && M == 0 && B == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3284, "score_of_the_acc": -0.1683, "final_rank": 10 }, { "submission_id": "aoj_2023_3678148", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<map>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<iomanip>\n\nusing namespace std;\n#define rep(i,n) for(int i = 0 ; i < n ; ++i)\nusing ll = long long;\nusing ld = long double;\nusing Point = complex<ld>;\nconst ld eps = 1e-9;\n\nbool eq(ld a, ld b){\n return abs(a-b)<eps;\n}\n\nld dot(Point a, Point b){\n return real(conj(a)*b);\n}\n\nld cross(Point a, Point b){\n return imag(conj(a)*b);\n}\n\nstruct Line{\n Point a, b;\n};\n\nbool isis_ll(Line l, Line m){\n return !eq(cross(l.b-l.a, m.b-m.a),0);\n}\n\nbool isis_ls(Line l, Line s){\n return (cross(l.b-l.a,s.a-l.a)*cross(l.b-l.a,s.b-l.a)<eps);\n}\n\nPoint is_ll(Line s, Line t){\n Point sv= s.b-s.a, tv = t.b-t.a;\n return s.a + sv * cross(tv, t.a-s.a)/cross(tv,sv);\n}\n\n\nvoid solve(int n,int k,int m){\n Point p[n];\n rep(i,n){\n int x,y;\n cin>>x>>y;\n p[i]=Point(x,y);\n }\n vector<ld> t(k+1,1e9);\n vector<Point> v(k+1);\n rep(i,k){\n int x,y;\n cin>>t[i]>>x>>y;\n v[i]=Point(x,y);\n }\n vector<ld> bt(m);\n vector<Point> bp(m),bq(m), bv(m);\n rep(i,m){\n cin>>bt[i];\n ld x,vx,vy,l;\n cin>>x>>vx>>vy>>l;\n bv[i]=Point(vx,vy);\n bp[i]=Point(x,0);\n bq[i]=Point(x-l*vx/abs(bv[i]),-l*vy/abs(bv[i]));\n bp[i]-=bt[i]*bv[i];\n bq[i]-=bt[i]*bv[i];\n }\n vector<ld> ans(m,1e9+7);\n ld st = 0;\n rep(i,k+1){\n rep(j,n){\n rep(bi,m){\n if(t[i]<bt[bi])continue;\n Point nxt = p[j]+v[i]-bv[bi];\n if(abs(nxt-p[j])<eps)continue;\n Line l = {p[j],nxt};\n Line bl = {bp[bi],bq[bi]};\n if(!isis_ls(l,bl))continue;\n if(eq(cross(l.a-l.b, bl.a-bl.b),0))continue;\n Point cp = is_ll(l,bl);\n ld ct = st + real((cp-p[j])/(nxt-p[j]));\n if(st-eps<ct&&ct<t[i]+eps&&ct>bt[bi]-eps){\n ans[bi]=min(ans[bi],ct);\n }\n }\n }\n rep(j,n){\n Line s = {p[j],p[(j+1)%n]};\n rep(bi,m){\n if(t[i]<bt[bi])continue;\n Point nxt = bp[bi]-v[i]+bv[bi];\n if(abs(nxt-bp[bi])<eps)continue;\n Line bl = {bp[bi],nxt};\n if(!isis_ls(bl,s))continue;\n if(eq(cross(s.a-s.b, bl.a-bl.b),0))continue;\n Point cp = is_ll(bl,s);\n ld ct = st + real((cp-bp[bi])/(nxt-bp[bi]));\n if(st-eps<ct&&ct<t[i]+eps&&ct>bt[bi]-eps){\n ans[bi]=min(ans[bi],ct);\n }\n }\n rep(bi,m){\n if(t[i]<bt[bi])continue;\n Point nxt = bq[bi]-v[i]+bv[bi];\n if(abs(nxt-bq[bi])<eps)continue;\n Line bl = {bq[bi],nxt};\n if(!isis_ls(bl,s))continue;\n if(eq(cross(s.a-s.b, bl.a-bl.b),0))continue;\n Point cp = is_ll(bl,s);\n ld ct = st + real((cp-bq[bi])/(nxt-bq[bi]));\n if(st-eps<ct&&ct<t[i]+eps&&ct>bt[bi]-eps){\n ans[bi]=min(ans[bi],ct);\n }\n }\n }\n rep(j,n){\n p[j]+=(ld)(t[i]-st)*v[i];\n }\n rep(j,m){\n bp[j]+=(t[i]-st)*bv[j];\n bq[j]+=(t[i]-st)*bv[j];\n }\n st = t[i];\n }\n vector<ld> at;\n rep(i,m){\n if(ans[i]<1e9){\n at.push_back(ans[i]);\n }\n }\n sort(at.begin(),at.end());\n cout<<at.size()<<endl;\n rep(i,at.size()){\n cout<<at[i]<<endl;\n }\n}\n\nint main(){\n int n,k,m;\n cout<<fixed<<setprecision(10);\n while(cin>>n>>k>>m,n!=0)solve(n,k,m);\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3384, "score_of_the_acc": -0.1933, "final_rank": 14 }, { "submission_id": "aoj_2023_3657209", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing ll = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};\n#define F first\n#define S second\n\n\nnamespace Geometry{\n typedef long double D;\n typedef complex<long double> P;\n typedef pair<P,D> C;\n \n const D EPS=1e-9;\n const D PI=asin(1)*2;\n const D INF=1e18;\n \n const static bool comp(const P &p1,const P &p2){return p1.real()==p2.real()?p1.imag()<p2.imag():p1.real()<p2.real();}\n \n D dot(P p1,P p2){return p1.real()*p2.real()+p1.imag()*p2.imag();}\n \n D cross(P p1,P p2){return p1.real()*p2.imag()-p1.imag()*p2.real();}\n \n P project(P vec,P x){return vec*(x/vec).real();}\n \n P project(P p1,P p2,P x){return p1+project(p2-p1,x-p1);}\n \n P reflect(P vec,P x){return vec*conj(x/vec);}\n \n P reflect(P p1,P p2,P x){return p1+reflect(p2-p1,x-p1);}\n \n bool intersectSL(P p1,P p2,P vec){vec/=abs(vec); p1/=vec; p2/=vec; return (p1.imag()<EPS && p2.imag()>-EPS) || (p1.imag()>-EPS && p2.imag()<EPS);}\n \n bool intersectSL(P p1,P p2,P p3,P p4){return intersectSL(p1-p4,p2-p4,p3-p4);}\n \n bool intersectSS(P p1,P p2,P p3,P p4){return (dot(p2-p1,p3-p1)<-EPS && dot(p2-p1,p4-p1)<-EPS) || (dot(p1-p2,p3-p2)<-EPS && dot(p1-p2,p4-p2)<-EPS)?false:intersectSL(p1,p2,p3,p4) && intersectSL(p3,p4,p1,p2);}\n \n D distLP(P vec,P x){return abs((x/vec).imag())*abs(vec);}\n \n D distLP(P p1,P p2,P x){return distLP(p2-p1,x-p1);}\n \n D distSP(P p1,P p2,P x){return dot(p2-p1,x-p1)<-EPS?abs(x-p1):dot(p1-p2,x-p2)<-EPS?abs(x-p2):distLP(p1,p2,x);}\n \n D distSS(P p1,P p2,P p3,P p4){return intersectSS(p1,p2,p3,p4)?0.0:min(min(distSP(p1,p2,p3),distSP(p1,p2,p4)),min(distSP(p3,p4,p1),distSP(p3,p4,p2)));}\n \n P crosspointLL(P p1,P p2,P vec){return abs(cross(p2-p1,vec))<EPS?vec:vec*cross(p2-p1,p2)/cross(p2-p1,vec);}\n \n P crosspointLL(P p1,P p2,P p3,P p4){return p4+crosspointLL(p1-p4,p2-p4,p3-p4);}\n \n P crosspointSS(P p1,P p2,P p3,P p4){return distSP(p1,p2,p3)<EPS?p3:distSP(p1,p2,p4)<EPS?p4:crosspointLL(p1,p2,p3,p4);}\n \n bool intersectShL(P p1,P p2,P vec){vec/=abs(vec); return intersectSL(p1,p2,vec) && crosspointLL(p1/vec,p2/vec,vec/vec).real()>-EPS;}\n \n bool intersectShL(P p1,P p2,P p3,P p4){return intersectShL(p1-p3,p2-p3,p4-p3);}\n \n //1::in,0::on edge,-1::out\n int contain(const vector<P> &poly,const P &p){\n vector<P> A={{65537,96847},{-24061,6701},{56369,-86509},{-93763,-78049},{56957,10007}};\n vector<bool> cnt(5,false);\n for(int i=1;i<=poly.size();i++){\n if(distSP(poly[i-1],poly[i%poly.size()],p)<EPS){return 0;}\n for(int j=0;j<5;j++){\n if(intersectShL(poly[i-1],poly[i%poly.size()],p,p+A[j])){cnt[j]=!cnt[j];}\n }\n }\n int in=0;\n for(int j=0;j<5;j++){if(cnt[j]){in++;}}\n return in>=3?1:-1;\n }\n \n vector<P> convexcut(const vector<P> &poly,P p1,P p2){\n vector<P> ret;\n for(int i=1;i<=poly.size();i++){\n if(cross(p2-p1,poly[i-1]-p1)>-EPS){ret.push_back(poly[i-1]);}\n if(intersectSL(poly[i-1],poly[i%poly.size()],p1,p2) && distLP(p1,p2,poly[i-1])>EPS && distLP(p1,p2,poly[i%poly.size()])>EPS){ret.push_back(crosspointLL(poly[i-1],poly[i%poly.size()],p1,p2));}\n }\n return ret;\n }\n \n D area(const vector<P> &poly){\n D ans=0;\n for(int i=2;i<poly.size();i++){ans+=cross(poly[i-1]-poly[0],poly[i]-poly[0]);}\n return abs(ans)/2;\n }\n \n vector<P> convexhull(vector<P> pts){\n vector<P> ret;\n sort(pts.begin(),pts.end(),comp);\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n reverse(pts.begin(),pts.end());\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n if(ret[0]==ret.back()){ret.pop_back();}\n return ret;\n }\n \n //4::seperate,3::circumscribe,2::intersect,1::inscribe,0::contain,-1::same\n int intersectCC(C c1,C c2){\n D d=abs(c1.F-c2.F),r=c1.S+c2.S,dif=abs(c2.S-c1.S);\n if(d<EPS && dif<EPS){return -1;}\n if(d-r>EPS){return 4;}\n if(d-r>-EPS){return 3;}\n if(d-dif>EPS){return 2;}\n if(d-dif>-EPS){return 1;}\n return 0;\n }\n \n vector<P> crosspointLC(P p1,P p2,C c){\n vector<P> ret;\n P pr=project(p1,p2,c.F);\n D d=distLP(p1,p2,c.F);\n if(d-c.S>EPS){return ret;}\n if(d-c.S>-EPS){ret.push_back(pr); return ret;}\n P vec=p2-p1; vec*=sqrt(c.S*c.S-d*d)/abs(vec);\n ret.push_back(pr-vec);\n ret.push_back(pr+vec);\n return ret;\n }\n \n vector<P> crosspointSC(P p1,P p2,C c){\n vector<P> ret;\n for(auto &I:crosspointLC(p1,p2,c)){if(distSP(p1,p2,I)<EPS){ret.push_back(I);}}\n return ret;\n }\n \n vector<P> crosspointCC(C c1,C c2){\n vector<P> ret;\n P vec=c2.F-c1.F;\n D base=(c1.S*c1.S+norm(vec)-c2.S*c2.S)/(2*abs(vec));\n D h=sqrt(c1.S*c1.S-base*base);\n vec/=abs(vec);\n ret.push_back(c1.F+vec*P(base,h));\n ret.push_back(c1.F+vec*P(base,-h));\n return ret;\n }\n \n vector<P> tangentCP(C c,P p){return crosspointCC(c,C(p,sqrt(norm(c.F-p)-c.S*c.S)));}\n \n vector<pair<P,P>> tangentCC(C c1,C c2){\n vector<pair<P,P>> ret;\n P d=c2.F-c1.F;\n for(D i:{-1,1}){\n D r=c1.S+c2.S*i;\n if(intersectCC(c1,c2)>i+1){\n for(P s:{-1i,1i}){\n P p=r+s*sqrt(norm(d)-norm(r));\n ret.push_back({c1.F+d*c1.S/norm(d)*p,c2.F-d*i*c2.S/norm(d)*p});\n }\n }\n }\n return ret;\n }\n \n D area(const vector<P> &poly,C c){\n D ret=0;\n for(int i=0;i<poly.size();i++){\n P a=poly[i]-c.F,b=poly[(i+1)%poly.size()]-c.F;\n if(abs(a)<c.S+EPS && abs(b)<c.S+EPS){ret+=cross(a,b);}\n else{\n vector<P> A=crosspointSC(a,b,{0,c.S});\n if(A.empty()){ret+=c.S*c.S*arg(b/a);}\n else{\n ret+=(abs(a)<c.S?cross(a,A[0]):c.S*c.S*arg(A[0]/a));\n ret+=(abs(b)<c.S?cross(A.back(),b):c.S*c.S*arg(b/A.back()));\n ret+=cross(A[0],A.back());\n }\n }\n }\n return abs(ret)/2;\n }\n \n //反時計回り\n D diameter(const vector<P> &poly){\n D ret=0;\n ll l=0,r=0,n=poly.size();\n if(n==2){return abs(poly[0]-poly[1]);}\n for(int i=0;i<n;i++){\n if(comp(poly[l],poly[i])){l=i;}\n if(comp(poly[i],poly[r])){r=i;}\n }\n ll sl=r,sr=l;\n while(sl!=l || sr!=r){\n ret=max(ret,abs(poly[r]-poly[l]));\n if(cross(poly[(l+1)%n]-poly[l],poly[(r+1)%n]-poly[r])<0){(++l)%=n;}\n else{(++r)%=n;}\n }\n return ret;\n }\n \n D closestpair(vector<P> pt){\n sort(pt.begin(),pt.end(),comp);\n D ret=INF;\n for(ll i=1;i<pt.size();i<<=1){\n for(ll j=0;i+j<pt.size();j+=i*2){\n ll m=i+j;\n vector<P> R;\n D l=-INF,r=INF;\n for(ll k=j;k<m;k++){l=max(l,pt[k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){r=min(r,pt[m+k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){if(pt[m+k].real()-l<ret){R.push_back(pt[m+k]);}}\n ll idx=0;\n for(ll k=j;k<m;k++){\n if(r-pt[k].real()>ret){continue;}\n while(idx<R.size() && pt[k].imag()-R[idx].imag()>ret){idx++;}\n for(ll n=idx;n<R.size() && R[n].imag()-pt[k].imag()<ret;n++){ret=min(ret,abs(R[n]-pt[k]));}\n }\n inplace_merge(pt.begin()+j,pt.begin()+m,j+i*2<pt.size()?pt.begin()+j+2*i:pt.end(),[](const P &a,const P &b){return a.imag()==b.imag()?a.real()<b.real():a.imag()<b.imag();});\n }\n }\n return ret;\n }\n \n istream & operator >> (istream &i,P &p){D x,y; i>>x>>y; p={x,y}; return i;}\n istream & operator >> (istream &i,C &p){D x,y; i>>x>>y>>p.S; p.F={x,y}; return i;}\n};\nusing namespace Geometry;\nbool contain(P p,vector<P> &poly){\n D a=0;\n if(area(poly)<EPS){return false;}\n for(int i=1;i<=poly.size();i++){\n a+=area({p,poly[i-1],poly[i%poly.size()]});\n }\n return abs(a-area(poly))<EPS;\n}\n\n\n\nvector<P> sqar(pair<P,P> L,P vec,D t){\n return {L.F,L.S,L.S+vec*t,L.F+vec*t};\n}\n\nbool intersectPP(vector<P> poly,vector<P> me){\n for(int i=1;i<=me.size();i++){\n if(contain(poly,me[i-1])!=-1){return true;}\n for(int j=1;j<=poly.size();j++){\n //if(intersect({poly[j-1],poly[j%poly.size()]},{me[i-1],me[i%me.size()]},true)){return true;}\n if(intersectSS(poly[j-1],poly[j%poly.size()],me[i-1],me[i%me.size()])){return true;}\n }\n }\n return false;\n}\n\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n ll n,m,b;\n while(cin>>n>>m>>b,n){\n vector<P> A(n);\n for(auto &I:A){\n D x,y;\n cin>>x>>y;\n I={x,y};\n }\n vector<pair<P,D>> V(m);\n for(auto &I:V){\n D x,y;\n cin>>I.S>>x>>y;\n I.F={x,y};\n }\n \n V.push_back({{0,0},INF});\n vector<pair<pair<P,P>,P>> L(b);\n for(auto &I:L){\n D t,X,x,y,l;\n cin>>t>>X>>x>>y>>l;\n I.F.F=P(X,0);\n I.S={x,y};\n I.F.S=I.F.F-I.S/abs(I.S)*l;\n I.F.F-=I.S*t;\n I.F.S-=I.S*t;\n }\n for(ll i=m-1;i>0;i--){V[i].S-=V[i-1].S;}\n vector<D> ans(b,INF*2);\n \n for(int i=0;i<b;i++){\n pair<P,P> l=L[i].F;\n D time=0;\n for(auto &I:V){\n P vec=L[i].S-I.F;\n auto judge=[&](D t){return intersectPP(sqar(l,vec,t),A);};\n if(!judge(I.S)){\n\ttime+=I.S;\n\tl.F+=vec*I.S;\n\tl.S+=vec*I.S;\n\tcontinue;\n }\n D lf=0,rg=I.S;\n for(int k=0;k<100;k++){\n\tD m=(lf+rg)/2;\n\tif(judge(m)){rg=m;}\n\telse{lf=m;}\n }\n ans[i]=lf+time;\n // cout<<i<<\" \"<<I.F<<\" \"<<I.S<<\" \"<<lf<<\" \"<<time<<endl;\n break;\n }\n }\n sort(ans.begin(),ans.end());\n ll cnt=0;\n for(auto &I:ans){if(I<INF){cnt++;}}\n cout<<cnt<<endl;\n if(cnt==6&&ans[0]<EPS){\n //cerr<<n<<\" \"<<m<<\" \"<<b<<endl;\n }\n for(int i=0;i<cnt;i++){\n cout<<fixed<<setprecision(12)<<ans[i]<<endl;\n }\n } \n return 0;\n}", "accuracy": 1, "time_ms": 2900, "memory_kb": 3308, "score_of_the_acc": -0.6195, "final_rank": 18 }, { "submission_id": "aoj_2023_3657205", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing ll = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};\n#define F first\n#define S second\n\n\nnamespace Geometry{\n typedef long double D;\n typedef complex<long double> P;\n typedef pair<P,D> C;\n \n const D EPS=1e-9;\n const D PI=asin(1)*2;\n const D INF=1e18;\n \n const static bool comp(const P &p1,const P &p2){return p1.real()==p2.real()?p1.imag()<p2.imag():p1.real()<p2.real();}\n \n D dot(P p1,P p2){return p1.real()*p2.real()+p1.imag()*p2.imag();}\n \n D cross(P p1,P p2){return p1.real()*p2.imag()-p1.imag()*p2.real();}\n \n P project(P vec,P x){return vec*(x/vec).real();}\n \n P project(P p1,P p2,P x){return p1+project(p2-p1,x-p1);}\n \n P reflect(P vec,P x){return vec*conj(x/vec);}\n \n P reflect(P p1,P p2,P x){return p1+reflect(p2-p1,x-p1);}\n \n bool intersectSL(P p1,P p2,P vec){vec/=abs(vec); p1/=vec; p2/=vec; return (p1.imag()<EPS && p2.imag()>-EPS) || (p1.imag()>-EPS && p2.imag()<EPS);}\n \n bool intersectSL(P p1,P p2,P p3,P p4){return intersectSL(p1-p4,p2-p4,p3-p4);}\n \n bool intersectSS(P p1,P p2,P p3,P p4){return (dot(p2-p1,p3-p1)<-EPS && dot(p2-p1,p4-p1)<-EPS) || (dot(p1-p2,p3-p2)<-EPS && dot(p1-p2,p4-p2)<-EPS)?false:intersectSL(p1,p2,p3,p4) && intersectSL(p3,p4,p1,p2);}\n \n D distLP(P vec,P x){return abs((x/vec).imag())*abs(vec);}\n \n D distLP(P p1,P p2,P x){return distLP(p2-p1,x-p1);}\n \n D distSP(P p1,P p2,P x){return dot(p2-p1,x-p1)<-EPS?abs(x-p1):dot(p1-p2,x-p2)<-EPS?abs(x-p2):distLP(p1,p2,x);}\n \n D distSS(P p1,P p2,P p3,P p4){return intersectSS(p1,p2,p3,p4)?0.0:min(min(distSP(p1,p2,p3),distSP(p1,p2,p4)),min(distSP(p3,p4,p1),distSP(p3,p4,p2)));}\n \n P crosspointLL(P p1,P p2,P vec){return abs(cross(p2-p1,vec))<EPS?vec:vec*cross(p2-p1,p2)/cross(p2-p1,vec);}\n \n P crosspointLL(P p1,P p2,P p3,P p4){return p4+crosspointLL(p1-p4,p2-p4,p3-p4);}\n \n P crosspointSS(P p1,P p2,P p3,P p4){return distSP(p1,p2,p3)<EPS?p3:distSP(p1,p2,p4)<EPS?p4:crosspointLL(p1,p2,p3,p4);}\n \n bool intersectShL(P p1,P p2,P vec){vec/=abs(vec); return intersectSL(p1,p2,vec) && crosspointLL(p1/vec,p2/vec,vec/vec).real()>-EPS;}\n \n bool intersectShL(P p1,P p2,P p3,P p4){return intersectShL(p1-p3,p2-p3,p4-p3);}\n \n //1::in,0::on edge,-1::out\n int contain(const vector<P> &poly,const P &p){\n vector<P> A={{65537,96847},{-24061,6701},{56369,-86509},{-93763,-78049},{56957,10007}};\n vector<bool> cnt(5,false);\n for(int i=1;i<=poly.size();i++){\n if(distSP(poly[i-1],poly[i%poly.size()],p)<EPS){return 0;}\n for(int j=0;j<5;j++){\n if(intersectShL(poly[i-1],poly[i%poly.size()],p,p+A[j])){cnt[j]=!cnt[j];}\n }\n }\n int in=0;\n for(int j=0;j<5;j++){if(cnt[j]){in++;}}\n return in>=3?1:-1;\n }\n \n vector<P> convexcut(const vector<P> &poly,P p1,P p2){\n vector<P> ret;\n for(int i=1;i<=poly.size();i++){\n if(cross(p2-p1,poly[i-1]-p1)>-EPS){ret.push_back(poly[i-1]);}\n if(intersectSL(poly[i-1],poly[i%poly.size()],p1,p2) && distLP(p1,p2,poly[i-1])>EPS && distLP(p1,p2,poly[i%poly.size()])>EPS){ret.push_back(crosspointLL(poly[i-1],poly[i%poly.size()],p1,p2));}\n }\n return ret;\n }\n \n D area(const vector<P> &poly){\n D ans=0;\n for(int i=2;i<poly.size();i++){ans+=cross(poly[i-1]-poly[0],poly[i]-poly[0]);}\n return abs(ans)/2;\n }\n \n vector<P> convexhull(vector<P> pts){\n vector<P> ret;\n sort(pts.begin(),pts.end(),comp);\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n reverse(pts.begin(),pts.end());\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n if(ret[0]==ret.back()){ret.pop_back();}\n return ret;\n }\n \n //4::seperate,3::circumscribe,2::intersect,1::inscribe,0::contain,-1::same\n int intersectCC(C c1,C c2){\n D d=abs(c1.F-c2.F),r=c1.S+c2.S,dif=abs(c2.S-c1.S);\n if(d<EPS && dif<EPS){return -1;}\n if(d-r>EPS){return 4;}\n if(d-r>-EPS){return 3;}\n if(d-dif>EPS){return 2;}\n if(d-dif>-EPS){return 1;}\n return 0;\n }\n \n vector<P> crosspointLC(P p1,P p2,C c){\n vector<P> ret;\n P pr=project(p1,p2,c.F);\n D d=distLP(p1,p2,c.F);\n if(d-c.S>EPS){return ret;}\n if(d-c.S>-EPS){ret.push_back(pr); return ret;}\n P vec=p2-p1; vec*=sqrt(c.S*c.S-d*d)/abs(vec);\n ret.push_back(pr-vec);\n ret.push_back(pr+vec);\n return ret;\n }\n \n vector<P> crosspointSC(P p1,P p2,C c){\n vector<P> ret;\n for(auto &I:crosspointLC(p1,p2,c)){if(distSP(p1,p2,I)<EPS){ret.push_back(I);}}\n return ret;\n }\n \n vector<P> crosspointCC(C c1,C c2){\n vector<P> ret;\n P vec=c2.F-c1.F;\n D base=(c1.S*c1.S+norm(vec)-c2.S*c2.S)/(2*abs(vec));\n D h=sqrt(c1.S*c1.S-base*base);\n vec/=abs(vec);\n ret.push_back(c1.F+vec*P(base,h));\n ret.push_back(c1.F+vec*P(base,-h));\n return ret;\n }\n \n vector<P> tangentCP(C c,P p){return crosspointCC(c,C(p,sqrt(norm(c.F-p)-c.S*c.S)));}\n \n vector<pair<P,P>> tangentCC(C c1,C c2){\n vector<pair<P,P>> ret;\n P d=c2.F-c1.F;\n for(D i:{-1,1}){\n D r=c1.S+c2.S*i;\n if(intersectCC(c1,c2)>i+1){\n for(P s:{-1i,1i}){\n P p=r+s*sqrt(norm(d)-norm(r));\n ret.push_back({c1.F+d*c1.S/norm(d)*p,c2.F-d*i*c2.S/norm(d)*p});\n }\n }\n }\n return ret;\n }\n \n D area(const vector<P> &poly,C c){\n D ret=0;\n for(int i=0;i<poly.size();i++){\n P a=poly[i]-c.F,b=poly[(i+1)%poly.size()]-c.F;\n if(abs(a)<c.S+EPS && abs(b)<c.S+EPS){ret+=cross(a,b);}\n else{\n vector<P> A=crosspointSC(a,b,{0,c.S});\n if(A.empty()){ret+=c.S*c.S*arg(b/a);}\n else{\n ret+=(abs(a)<c.S?cross(a,A[0]):c.S*c.S*arg(A[0]/a));\n ret+=(abs(b)<c.S?cross(A.back(),b):c.S*c.S*arg(b/A.back()));\n ret+=cross(A[0],A.back());\n }\n }\n }\n return abs(ret)/2;\n }\n \n //反時計回り\n D diameter(const vector<P> &poly){\n D ret=0;\n ll l=0,r=0,n=poly.size();\n if(n==2){return abs(poly[0]-poly[1]);}\n for(int i=0;i<n;i++){\n if(comp(poly[l],poly[i])){l=i;}\n if(comp(poly[i],poly[r])){r=i;}\n }\n ll sl=r,sr=l;\n while(sl!=l || sr!=r){\n ret=max(ret,abs(poly[r]-poly[l]));\n if(cross(poly[(l+1)%n]-poly[l],poly[(r+1)%n]-poly[r])<0){(++l)%=n;}\n else{(++r)%=n;}\n }\n return ret;\n }\n \n D closestpair(vector<P> pt){\n sort(pt.begin(),pt.end(),comp);\n D ret=INF;\n for(ll i=1;i<pt.size();i<<=1){\n for(ll j=0;i+j<pt.size();j+=i*2){\n ll m=i+j;\n vector<P> R;\n D l=-INF,r=INF;\n for(ll k=j;k<m;k++){l=max(l,pt[k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){r=min(r,pt[m+k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){if(pt[m+k].real()-l<ret){R.push_back(pt[m+k]);}}\n ll idx=0;\n for(ll k=j;k<m;k++){\n if(r-pt[k].real()>ret){continue;}\n while(idx<R.size() && pt[k].imag()-R[idx].imag()>ret){idx++;}\n for(ll n=idx;n<R.size() && R[n].imag()-pt[k].imag()<ret;n++){ret=min(ret,abs(R[n]-pt[k]));}\n }\n inplace_merge(pt.begin()+j,pt.begin()+m,j+i*2<pt.size()?pt.begin()+j+2*i:pt.end(),[](const P &a,const P &b){return a.imag()==b.imag()?a.real()<b.real():a.imag()<b.imag();});\n }\n }\n return ret;\n }\n \n istream & operator >> (istream &i,P &p){D x,y; i>>x>>y; p={x,y}; return i;}\n istream & operator >> (istream &i,C &p){D x,y; i>>x>>y>>p.S; p.F={x,y}; return i;}\n};\nusing namespace Geometry;\nbool contain(P p,vector<P> &poly){\n D a=0;\n if(area(poly)<EPS){return false;}\n for(int i=1;i<=poly.size();i++){\n a+=area({p,poly[i-1],poly[i%poly.size()]});\n }\n return abs(a-area(poly))<EPS;\n}\n\n\n\nvector<P> sqar(pair<P,P> L,P vec,D t){\n return {L.F,L.S,L.S+vec*t,L.F+vec*t};\n}\n\nbool intersectPP(vector<P> poly,vector<P> me){\n for(int i=1;i<=me.size();i++){\n if(contain(me[i-1],poly)){return true;}\n for(int j=1;j<=poly.size();j++){\n //if(intersect({poly[j-1],poly[j%poly.size()]},{me[i-1],me[i%me.size()]},true)){return true;}\n if(intersectSS(poly[j-1],poly[j%poly.size()],me[i-1],me[i%me.size()])){return true;}\n }\n }\n return false;\n}\n\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n ll n,m,b;\n while(cin>>n>>m>>b,n){\n vector<P> A(n);\n for(auto &I:A){\n D x,y;\n cin>>x>>y;\n I={x,y};\n }\n vector<pair<P,D>> V(m);\n for(auto &I:V){\n D x,y;\n cin>>I.S>>x>>y;\n I.F={x,y};\n }\n \n V.push_back({{0,0},INF});\n vector<pair<pair<P,P>,P>> L(b);\n for(auto &I:L){\n D t,X,x,y,l;\n cin>>t>>X>>x>>y>>l;\n I.F.F=P(X,0);\n I.S={x,y};\n I.F.S=I.F.F-I.S/abs(I.S)*l;\n I.F.F-=I.S*t;\n I.F.S-=I.S*t;\n }\n for(ll i=m-1;i>0;i--){V[i].S-=V[i-1].S;}\n vector<D> ans(b,INF*2);\n \n for(int i=0;i<b;i++){\n pair<P,P> l=L[i].F;\n D time=0;\n for(auto &I:V){\n P vec=L[i].S-I.F;\n auto judge=[&](D t){return intersectPP(sqar(l,vec,t),A);};\n if(!judge(I.S)){\n\ttime+=I.S;\n\tl.F+=vec*I.S;\n\tl.S+=vec*I.S;\n\tcontinue;\n }\n D lf=0,rg=I.S;\n for(int k=0;k<100;k++){\n\tD m=(lf+rg)/2;\n\tif(judge(m)){rg=m;}\n\telse{lf=m;}\n }\n ans[i]=lf+time;\n // cout<<i<<\" \"<<I.F<<\" \"<<I.S<<\" \"<<lf<<\" \"<<time<<endl;\n break;\n }\n }\n sort(ans.begin(),ans.end());\n ll cnt=0;\n for(auto &I:ans){if(I<INF){cnt++;}}\n cout<<cnt<<endl;\n if(cnt==6&&ans[0]<EPS){\n //cerr<<n<<\" \"<<m<<\" \"<<b<<endl;\n }\n for(int i=0;i<cnt;i++){\n cout<<fixed<<setprecision(12)<<ans[i]<<endl;\n }\n } \n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3296, "score_of_the_acc": -0.2192, "final_rank": 16 }, { "submission_id": "aoj_2023_3657182", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing ll = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};\n#define F first\n#define S second\n\nnamespace Geometry{\n\n typedef long double D;\n typedef complex<long double> P;\n typedef pair<P, D> C;\n\n const D EPS = 1e-8;\n const D PI = asin(1) * 2;\n const D INF = 1e18;\n \n D dot(P p1, P p2){\n return p1.real() * p2.real() + p1.imag() * p2.imag();\n }\n\n D cross(P p1, P p2){\n return p1.real() * p2.imag() - p1.imag() * p2.real();\n }\n\n D area(const vector<P> poly){\n D ans = 0;\n for(int i=2;i<(int)poly.size();i++)\n ans += cross(poly[i-1] - poly[0], poly[i] - poly[0]);\n return abs(ans)/2;\n }\n\n bool intersectSL(P p1, P p2, P vec){\n vec /= abs(vec); p1 /= vec, p2/= vec;\n return\n (p1.imag() < EPS && p2.imag() >= -EPS) ||\n (p1.imag() > -EPS && p2.imag() < EPS);\n }\n bool intersectSL(P p1, P p2, P p3, P p4){\n return intersectSL(p1 - p4, p2 - p4, p3 - p4);\n }\n\n bool intersectSS(P p1, P p2, P p3, P p4){\n return\n (dot(p2 -p1, p3 - p1) < -EPS && dot(p2 - p1, p4 - p1) < -EPS) ||\n\t\t\t (dot(p1-p2, p3 - p2) < -EPS && dot(p1 - p2, p4 - p2) < -EPS)? false:intersectSL(p1, p2, p3, p4) && intersectSL(p3, p4, p1, p2);\t\t \t\t\t }\n D distLP(P vec, P x){\n return abs((x/vec).imag()) * abs(vec);\n }\n\n D distLP(P p1, P p2, P x){return distLP(p2 - p1, x - p1);}\n \n D distSP(P p1, P p2, P x){\n return dot(p2 - p1, x - p1) < -EPS?\n\t\t\t\t abs(x-p1):dot(p1 - p2, x - p2) < -EPS? abs(x - p2):distLP(p1, p2, x);\n }\n\n P crosspointLL(P p1, P p2, P vec){return abs(cross(p2 - p1, vec)) < EPS? vec: vec * cross(p2 - p1, p2)/ cross(p2 - p1, vec);}\n P crosspointLL(P p1, P p2, P p3, P p4){return p4 + crosspointLL(p1 - p4, p2 - p4, p3 - p4);}\n \n P crosspointSS(P p1, P p2, P p3, P p4){\n return distSP(p1, p2, p3) < EPS? p3:distSP(p1, p2, p4)<EPS? p4:crosspointLL(p1, p2, p3, p4);\n }\n \n};\nusing namespace Geometry;\ntypedef long double GType;\ntypedef std::complex<GType> Point;\ntypedef std::pair<Point, Point> Segment;\nint ccw(const Point& p1, const Point& p2, const Point& p3) {\n Point v1 = p2 - p1, v2 = p3 - p1;\n if (cross(v1, v2) > EPS) return +1;\n if (cross(v1, v2) < -EPS) return -1;\n if (dot(v1, v2) < -EPS) return +2;\n if (std::norm(v1) < std::norm(v2)) return -2;\n return 0;\n}\n \n// ------ Functions Level 2 ------ //\nbool intersect(const Segment& s1, const Point& p1, bool segflag) {\n if (!segflag) return std::abs(cross(s1.second - p1, s1.first - p1)) < EPS;\n return (std::abs(s1.first - p1) + std::abs(s1.second - p1) - std::abs(s1.second - s1.first)) < EPS;\n}\nbool intersect(const Segment& s1, const Segment& s2, bool segflag) {\n if (!segflag) return cross(s1.second - s1.first, s2.first - s1.first) * cross(s1.second - s1.first, s2.second - s1.first) < EPS;\n return (ccw(s1.first, s1.second, s2.first) * ccw(s1.first, s1.second, s2.second)) <= 0 && (ccw(s2.first, s2.second, s1.first) * ccw(s2.first, s2.second, s1.second)) <= 0;\n}\n\n\n\nbool contain(P p,vector<P> &poly){\n D a=0;\n if(area(poly)<EPS){return false;}\n for(int i=1;i<=poly.size();i++){\n a+=area({p,poly[i-1],poly[i%poly.size()]});\n }\n return abs(a-area(poly))<EPS;\n}\nvector<P> sqar(pair<P,P> L,P vec,D t){\n return {L.F,L.S,L.S+vec*t,L.F+vec*t};\n}\n\nbool intersectPP(vector<P> poly,vector<P> me){\n for(int i=1;i<=me.size();i++){\n if(contain(me[i-1],poly)){return true;}\n for(int j=1;j<=poly.size();j++){\n //if(intersect({poly[j-1],poly[j%poly.size()]},{me[i-1],me[i%me.size()]},true)){return true;}\n if(intersectSS(poly[j-1],poly[j%poly.size()],me[i-1],me[i%me.size()])){return true;}\n }\n }\n return false;\n}\n\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n ll n,m,b;\n while(cin>>n>>m>>b,n){\n vector<P> A(n);\n for(auto &I:A){\n D x,y;\n cin>>x>>y;\n I={x,y};\n }\n vector<pair<P,D>> V(m);\n for(auto &I:V){\n D x,y;\n cin>>I.S>>x>>y;\n I.F={x,y};\n }\n \n V.push_back({{0,0},INF});\n vector<pair<pair<P,P>,P>> L(b);\n for(auto &I:L){\n D t,X,x,y,l;\n cin>>t>>X>>x>>y>>l;\n I.F.F=P(X,0);\n I.S={x,y};\n I.F.S=I.F.F-I.S/abs(I.S)*l;\n I.F.F-=I.S*t;\n I.F.S-=I.S*t;\n }\n for(ll i=m-1;i>0;i--){V[i].S-=V[i-1].S;}\n vector<D> ans(b,INF*2);\n \n for(int i=0;i<b;i++){\n pair<P,P> l=L[i].F;\n D time=0;\n for(auto &I:V){\n P vec=L[i].S-I.F;\n auto judge=[&](D t){return intersectPP(sqar(l,vec,t),A);};\n if(!judge(I.S)){\n\ttime+=I.S;\n\tl.F+=vec*I.S;\n\tl.S+=vec*I.S;\n\tcontinue;\n }\n D lf=0,rg=I.S;\n for(int k=0;k<100;k++){\n\tD m=(lf+rg)/2;\n\tif(judge(m)){rg=m;}\n\telse{lf=m;}\n }\n ans[i]=lf+time;\n // cout<<i<<\" \"<<I.F<<\" \"<<I.S<<\" \"<<lf<<\" \"<<time<<endl;\n break;\n }\n }\n sort(ans.begin(),ans.end());\n ll cnt=0;\n for(auto &I:ans){if(I<INF){cnt++;}}\n cout<<cnt<<endl;\n if(cnt==6&&ans[0]<EPS){\n //cerr<<n<<\" \"<<m<<\" \"<<b<<endl;\n }\n for(int i=0;i<cnt;i++){\n cout<<fixed<<setprecision(12)<<ans[i]<<endl;\n }\n } \n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 3308, "score_of_the_acc": -0.2342, "final_rank": 17 }, { "submission_id": "aoj_2023_3127463", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\n\nconst double EPS = 1e-6;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nint main(){\n while(1){\n int n,m,b;\n cin >> n >> m >> b;\n if(n == 0) break;\n\n VP poly(n);\n for(int i=0; i<n; i++){\n double vx,vy;\n cin >> vx >> vy;\n poly[i] = P(vx, vy);\n }\n \n vector<double> ts(m+1);\n VP vs(m+1);\n for(int i=0; i<m; i++){\n double vx,vy;\n cin >> ts[i] >> vx >> vy;\n vs[i] = P(vx, vy);\n }\n //指示終了後は停止し続ける\n ts[m] = INF;\n vs[m] = P(0, 0);\n m++;\n\n vector<double> tg(b), lg(b);\n VP inig(b), vg(b);\n for(int i=0; i<b; i++){\n double xi,vx,vy;\n cin >> tg[i] >> xi >> vx >> vy >> lg[i];\n inig[i] = P(xi, 0);\n vg[i] = P(vx, vy);\n }\n //ここまで入力\n\n vector<double> ans;\n for(int bullet=0; bullet<b; bullet++){\n P poss(0, 0), posb = inig[bullet];\n for(int move=0; move<m; move++){\n double time = 0;\n if(move!=0) time = ts[move-1];\n //まだ始まってない\n if(tg[bullet] +EPS > ts[move]){\n poss += (ts[move] -time) *vs[move];\n continue;\n }\n //区間の途中で弾丸が発射されるとき\n if(tg[bullet] > time +EPS){\n poss += (tg[bullet] -time) *vs[move];\n time = tg[bullet];\n }\n \n L bul(posb, posb +lg[bullet]*unit(-vg[bullet]));\n double mintime = INF;\n //多角形の頂点が弾丸に衝突\n for(int i=0; i<n; i++){\n P p = poss +poly[i];\n L locus;\n locus[0] = p;\n locus[1] = p +(ts[move] -time) *(vs[move] -vg[bullet]);\n if(!isParallel(bul, locus) && intersectSS(bul, locus)){\n P cp = crosspointLL(bul, locus);\n double ratio = abs(cp -locus[0]) /abs(locus[1] -locus[0]);\n mintime = min(mintime, time +ratio *(ts[move] -time));\n }\n }\n //弾丸の端点が多角形の辺に衝突\n for(int d=0; d<2; d++){\n L locus;\n locus[0] = bul[d];\n locus[1] = bul[d] +(ts[move] -time) *(vg[bullet] -vs[move]);\n for(int i=0; i<n; i++){\n L edge(poss +poly[i], poss +poly[(i+1)%n]);\n if(!isParallel(edge, locus) && intersectSS(edge, locus)){\n P cp = crosspointLL(edge, locus);\n double ratio = abs(cp -locus[0]) /abs(locus[1] -locus[0]);\n mintime = min(mintime, time +ratio *(ts[move] -time));\n }\n }\n }\n if(mintime != INF){\n ans.push_back(mintime);\n break;\n }\n\n //位置の更新\n poss += (ts[move] -time) *vs[move];\n posb += (ts[move] -time) *vg[bullet];\n }\n }\n sort(ans.begin(), ans.end());\n cout << fixed << setprecision(5);\n cout << ans.size() << endl;\n for(int i=0; i<(int)ans.size(); i++){\n cout << ans[i] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3280, "score_of_the_acc": -0.168, "final_rank": 9 }, { "submission_id": "aoj_2023_1373907", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-4;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\t// ??§?¨????????????¨EPS???????????§??????????????????????????¨???\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS;\n\t\t\t//return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t\n\ttemplate<class Func> R simpson(R s, R t, Func func){\n\t\treturn (t-s)/6*(func(s+2*EPS) + func(t-2*EPS) + 4*func((s+t)*(R).5));\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst R B = 500;\n\tconst R Z = 2;\n//\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n//\tostream& operator<<(ostream &os, const P &p){return os << \"circle(\"<<B+Z*(p.X)<<\", \"<<1000-B-Z*(p.Y)<<\", 2)\";}\n//\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\n}\n\nstruct G : public vector<P>{\n\tP v;\n\tR l, r;\n\t\n\tG(size_type size=0):vector(size){}\n\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\tS edge(int i, R t)const {return S(at(i)+v*t, at(i+1 == size() ? 0 : i+1)+v*t);}\n};\n\nint T, n, m, b;\n\nR conflict(P p1, P p2, P p3, P v, R l, R r){\n\tif(r + EPS < l) return INF;\n\tR o = outp(p2-p1, v);\n\tR res = INF;\n\tif(!ccw(p1, p2, p3 + v*l, 1)) return l;\n\tdo{\n\t\tif(abs(o) < EPS){\n\t\t\tif(abs(outp(p2 - p1, p3 - p1)) > EPS) break;\n\t\t\tres = min(abs(p1-p3), abs(p2-p3))/abs(v);\n\t\t}else res = -outp(p2 - p1, p3 - p1) / o;\n\t\tif(res > INF - EPS || res < l - EPS || r + EPS < res) break;\n\t\tif(ccw(p1, p2, p3 + v*res, 1)) break;\n\t\treturn res;\n\t}while(0);\n\treturn INF;\n}\n\nR conflict(G a, G b){\n\tR ans = INF;\n\tREP(i, a.size())REP(j, b.size()){\n\t\tR res = conflict(a[i], a[(i+1) % a.size()], b[j], b.v-a.v, max(a.l, b.l), min(a.r, b.r));\n\t\tans = min(ans, res);\n\t}\n\treturn ans;\n}\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> b, n){\n\t\tG g(n);\n\t\tvector<G> polys;\n\t\tREP(i, n) cin >> g[i];\n\t\tR pt = 0;\n\t\tREP(i, m){\n\t\t\tR t;\n\t\t\tP v;\n\t\t\tpolys.pb(g);\n\t\t\tcin >> t >> v;\n\t\t\tfor(auto &p : polys[i]) p -= v*pt;\n\t\t\tpolys[i].v = v; polys[i].l = pt; polys[i].r = t;\n\t\t\tfor(auto &p : g) p += v*(t-pt);\n\t\t\tpt = t;\n\t\t}\n\t\tpolys.pb(g);\n\t\tpolys[m].v = P(0, 0);\n\t\tpolys[m].l = pt;\n\t\tpolys[m].r = INF;\n\t\tvector<R> ans;\n\t\tREP(i, b){\n\t\t\tP p, v;\n\t\t\tR x, t, l;\n\t\t\tcin >> t >> x >> v >> l;\n//\t\t\tcout << \"!\" << t << \" \" << x << \" \" << v << \" \" << l << endl;\n\t\t\tp = P(x, 0) - v*t;\n\t\t\tG bullet(2);\n\t\t\tbullet[0] = p;\n\t\t\tbullet[1] = p - unit(v)*l;\n\t\t\tbullet.v = v;\n\t\t\tbullet.l = t;\n\t\t\tbullet.r = INF;\n\t\t\tR res = INF;\n\t\t\tREP(j, m+1) res = min(res, min(conflict(polys[j], bullet), conflict(bullet, polys[j])));\n\t\t\tif(res < INF-EPS) ans.pb(res);\n\t\t}\n\t\tsort(ALL(ans));\n\t\tprintf(\"%d\\n\", (int)ans.size());\n\t\tREP(i, ans.size()) printf(\"%.15f\\n\", (double)ans[i]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1360, "score_of_the_acc": -0.0241, "final_rank": 4 }, { "submission_id": "aoj_2023_1373905", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\t// ??§?¨????????????¨EPS???????????§??????????????????????????¨???\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS;\n\t\t\t//return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t\n\ttemplate<class Func> R simpson(R s, R t, Func func){\n\t\treturn (t-s)/6*(func(s+2*EPS) + func(t-2*EPS) + 4*func((s+t)*(R).5));\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst R B = 500;\n\tconst R Z = 2;\n//\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n//\tostream& operator<<(ostream &os, const P &p){return os << \"circle(\"<<B+Z*(p.X)<<\", \"<<1000-B-Z*(p.Y)<<\", 2)\";}\n//\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\n}\n\nstruct G : public vector<P>{\n\tP v;\n\tR l, r;\n\t\n\tG(size_type size=0):vector(size){}\n\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\tS edge(int i, R t)const {return S(at(i)+v*t, at(i+1 == size() ? 0 : i+1)+v*t);}\n};\n\nint T, n, m, b;\n\nR conflict(P p1, P p2, P p3, P v, R l, R r){\n\tif(r + EPS < l) return INF;\n\tR o = outp(p2-p1, v);\n\tR res = INF;\n\tif(!ccw(p1, p2, p3 + v*l, 1)) return l;\n\tdo{\n\t\tif(abs(o) < EPS){\n\t\t\tif(abs(outp(p2 - p1, p3 - p1)) > EPS) break;\n\t\t\tres = min(abs(p1-p3), abs(p2-p3))/abs(v);\n\t\t}else res = -outp(p2 - p1, p3 - p1) / o;\n\t\tif(res > INF - EPS || res < l - EPS || r + EPS < res) break;\n\t\tif(ccw(p1, p2, p3 + v*res, 1)) break;\n\t\treturn res;\n\t}while(0);\n\treturn INF;\n}\n\nR conflict(G a, G b){\n\tR ans = INF;\n\tREP(i, a.size())REP(j, b.size()){\n\t\tR res = conflict(a[i], a[(i+1) % a.size()], b[j], b.v-a.v, max(a.l, b.l), min(a.r, b.r));\n\t\tans = min(ans, res);\n\t}\n\treturn ans;\n}\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> b, n){\n\t\tG g(n);\n\t\tvector<G> polys;\n\t\tREP(i, n) cin >> g[i];\n\t\tR pt = 0;\n\t\tREP(i, m){\n\t\t\tR t;\n\t\t\tP v;\n\t\t\tpolys.pb(g);\n\t\t\tcin >> t >> v;\n\t\t\tfor(auto &p : polys[i]) p -= v*pt;\n\t\t\tpolys[i].v = v; polys[i].l = pt; polys[i].r = t;\n\t\t\tfor(auto &p : g) p += v*(t-pt);\n\t\t\tpt = t;\n\t\t}\n\t\tpolys.pb(g);\n\t\tpolys[m].v = P(0, 0);\n\t\tpolys[m].l = pt;\n\t\tpolys[m].r = INF;\n\t\tvector<R> ans;\n\t\tREP(i, b){\n\t\t\tP p, v;\n\t\t\tR x, t, l;\n\t\t\tcin >> t >> x >> v >> l;\n//\t\t\tcout << \"!\" << t << \" \" << x << \" \" << v << \" \" << l << endl;\n\t\t\tp = P(x, 0) - v*t;\n\t\t\tG bullet(2);\n\t\t\tbullet[0] = p;\n\t\t\tbullet[1] = p - unit(v)*l;\n\t\t\tbullet.v = v;\n\t\t\tbullet.l = t;\n\t\t\tbullet.r = INF;\n\t\t\tR res = INF;\n\t\t\tREP(j, m+1) res = min(res, min(conflict(polys[j], bullet), conflict(bullet, polys[j])));\n\t\t\tif(res < INF-EPS) ans.pb(res);\n\t\t}\n\t\tsort(ALL(ans));\n\t\tprintf(\"%d\\n\", (int)ans.size());\n\t\tREP(i, ans.size()) printf(\"%.15f\\n\", (double)ans[i]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 1404, "score_of_the_acc": -0.0509, "final_rank": 6 }, { "submission_id": "aoj_2023_1373902", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\t// ??§?¨????????????¨EPS???????????§??????????????????????????¨???\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS;\n\t\t\t//return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t\n\ttemplate<class Func> R simpson(R s, R t, Func func){\n\t\treturn (t-s)/6*(func(s+2*EPS) + func(t-2*EPS) + 4*func((s+t)*(R).5));\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst R B = 500;\n\tconst R Z = 2;\n//\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n//\tostream& operator<<(ostream &os, const P &p){return os << \"circle(\"<<B+Z*(p.X)<<\", \"<<1000-B-Z*(p.Y)<<\", 2)\";}\n//\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\n}\n\nstruct G : public vector<P>{\n\tP v;\n\tR l, r;\n\t\n\tG(size_type size=0):vector(size){}\n\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\tS edge(int i, R t)const {return S(at(i)+v*t, at(i+1 == size() ? 0 : i+1)+v*t);}\n};\n\nint T, n, m, b;\n\nR conflict(P p1, P p2, P p3, P v, R l, R r){\n\tR o = outp(p2-p1, v);\n\tR res = INF;\n\tif(!ccw(p1, p2, p3 + v*l, 1)) return l;\n\tdo{\n\t\tif(abs(o) < EPS){\n\t\t\tif(abs(outp(p2 - p1, p3 - p1)) > EPS) break;\n\t\t\tres = min(abs(p1-p3), abs(p2-p3))/abs(v);\n\t\t}else res = -outp(p2 - p1, p3 - p1) / o;\n\t\tif(res > INF - EPS || res < l - EPS || r + EPS < res) break;\n\t\tif(ccw(p1, p2, p3 + v*res, 1)) break;\n\t\treturn res;\n\t}while(0);\n\treturn INF;\n}\n\nR conflict(G a, G b){\n\tR ans = INF;\n\tREP(i, a.size())REP(j, b.size()){\n\t\tR res = conflict(a[i], a[(i+1) % a.size()], b[j], b.v-a.v, max(a.l, b.l), min(a.r, b.r));\n\t\tans = min(ans, res);\n\t}\n\treturn ans;\n}\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> b, n){\n\t\tG g(n);\n\t\tvector<G> polys;\n\t\tREP(i, n) cin >> g[i];\n\t\tR pt = 0;\n\t\tREP(i, m){\n\t\t\tR t;\n\t\t\tP v;\n\t\t\tpolys.pb(g);\n\t\t\tcin >> t >> v;\n\t\t\tfor(auto &p : polys[i]) p -= v*pt;\n\t\t\tpolys[i].v = v; polys[i].l = pt; polys[i].r = t;\n\t\t\tfor(auto &p : g) p += v*(t-pt);\n\t\t\tpt = t;\n\t\t}\n\t\tpolys.pb(g);\n\t\tpolys[m].v = P(0, 0);\n\t\tpolys[m].l = pt;\n\t\tpolys[m].r = INF;\n\t\tvector<R> ans;\n\t\tREP(i, b){\n\t\t\tP p, v;\n\t\t\tR x, t, l;\n\t\t\tcin >> t >> x >> v >> l;\n//\t\t\tcout << \"!\" << t << \" \" << x << \" \" << v << \" \" << l << endl;\n\t\t\tp = P(x, 0) - v*t;\n\t\t\tG bullet(2);\n\t\t\tbullet[0] = p;\n\t\t\tbullet[1] = p - unit(v)*l;\n\t\t\tbullet.v = v;\n\t\t\tbullet.l = t;\n\t\t\tbullet.r = INF;\n\t\t\tR res = INF;\n\t\t\tREP(j, m+1) res = min(res, min(conflict(polys[j], bullet), conflict(bullet, polys[j])));\n\t\t\tif(res < INF-EPS) ans.pb(res);\n\t\t}\n\t\tsort(ALL(ans));\n\t\tprintf(\"%d\\n\", (int)ans.size());\n\t\tREP(i, ans.size()) printf(\"%.15f\\n\", (double)ans[i]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 1400, "score_of_the_acc": -0.0662, "final_rank": 7 }, { "submission_id": "aoj_2023_1368752", "code_snippet": "// * template\n\n#include <bits/stdc++.h>\n#ifdef LOCAL\n#include \"dump.hpp\"\n#else\n#define dump(...)\n#endif\n\nusing namespace std;\n\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n\nstruct range {\n\ttypedef int Int;\n\tstruct iter {\n\t\tInt i;\n\t\tconst Int s;\n\t\titer(const Int &i_, const Int &s_):i(i_), s(s_) {}\n\t\tbool operator!=(const iter &r) const { return s > 0 ? i < r.i : i > r.i; }\n\t\tconst Int &operator*() const { return i; }\n\t\titer &operator++() { i += s; return *this; }\n\t};\n\tconst Int f, l, s;\n\trange(const Int &f_, const Int &l_, const Int &s_):f(f_), l(l_), s(s_) {}\n\trange(const Int &f_, const Int &l_):f(f_), l(l_), s(1) {}\n\trange(const Int &num):f(0), l(num), s(1) {}\n\titer begin() const { return iter(f, s); }\n\titer end() const { return iter(l, s); }\n};\n\n// * geoetry library\n\ntypedef double Real;\nconstexpr Real EPS = 1e-8;\ninline int sign(Real d) { return d > EPS ? 1 : d < -EPS ? -1 : 0; }\n\nstruct Point {\n\tReal x, y;\n\tPoint():x(0), y(0) {}\n\tPoint(Real x_, Real y_):x(x_), y(y_) {}\n\tPoint &operator+=(const Point &p) { x += p.x; y += p.y; return *this; }\n\tPoint &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; }\n\tPoint &operator*=(Real s) { x *= s; y *= s; return *this; }\n\tPoint &operator/=(Real s) { x /= s; y /= s; return *this; }\n\tPoint operator-() const { return Point(-x, -y); }\n\tPoint operator+(const Point &p) const { return Point(*this) += p; }\n\tPoint operator-(const Point &p) const { return Point(*this) -= p; }\n\tPoint operator*(Real s) const { return Point(*this) *= s; }\n\tPoint operator/(Real s) const { return Point(*this) /= s; }\n\tbool operator<(const Point &p) const { return sign(x - p.x) == -1 || (sign(x - p.x) == 0 && sign(y - p.y) == -1); }\n\tbool operator>(const Point &p) const { return p < *this; }\n\tbool operator==(const Point &p) const { return sign(x - p.x) == 0 && sign(y - p.y) == 0; }\n\tbool operator!=(const Point &p) const { return !(*this == p); }\n};\n\ninline istream &operator>>(istream &is, Point &p) {\n\treturn is >> p.x >> p.y;\n}\n\ninline ostream &operator<<(ostream &os, const Point &p) {\n\treturn os << '(' << p.x << \", \" << p.y << ')';\n}\n\nstruct Line : public array<Point, 2> {\n\tLine(const Point &a, const Point &b) { at(0) = a; at(1) = b; }\n};\n\nstruct Segment : public array<Point, 2> {\n\tSegment(const Point &a, const Point &b) { at(0) = a; at(1) = b; }\n};\n\ntypedef vector<Point> Polygon;\n\ninline Real norm(const Point &p) {\n\treturn p.x * p.x + p.y * p.y;\n}\n\ninline Real abs(const Point &p) {\n\treturn sqrt(norm(p));\n}\n\ninline Real dot(const Point &a, const Point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\ninline Real cross(const Point &a, const Point &b) {\n\treturn a.x * b.y - a.y * b.x;\n}\n\nenum { CCW = 1, CW = -1, BACK = 2, FRONT = -2, ON = 0};\ninline int ccw(const Point &a, const Point &b, const Point &c) {\n\tconst Point p = b - a;\n\tconst Point q = c - a;\n\tconst int sign_cross = sign(cross(p, q));\n\n\tif(sign_cross == 1) return CCW;\n\tif(sign_cross == -1) return CW;\n\tif(sign(dot(p, q)) == -1) return BACK;\n\tif(sign(norm(p) - norm(q)) == -1) return FRONT;\n\treturn ON;\n}\n\ninline bool intersect(const Segment &s, const Point &p) {\n\treturn ccw(s[0], s[1], p) == ON;\n}\n\ninline bool intersect(const Segment &a, const Segment &b) {\n\treturn ccw(a[0], a[1], b[0]) * ccw(a[0], a[1], b[1]) <= 0\n\t\t&& ccw(b[0], b[1], a[0]) * ccw(b[0], b[1], a[1]) <= 0;\n}\n\ninline Point crosspoint(const Segment &a, const Segment &b) {\n\tconst Real crs = cross(a[1] - a[0], b[1] - b[0]);\n\tif(sign(crs) == 0) { // on same line\n\t\tif(intersect(a, b[0])) return b[0];\n\t\tif(intersect(a, b[1])) return b[1];\n\t\tif(intersect(b, a[0])) return a[0];\n\t\treturn a[1];\n\t}\n\treturn b[0] + (b[1] - b[0]) * (cross(a[1] - a[0], a[1] - b[0]) / crs);\n}\n\n// * solve\n\nReal collision_time(const Segment &s, const Point &p, const Point &v) {\n\tconst Segment seg(p, p + v * 1e7);\n\tif(!intersect(s, seg)) return numeric_limits<Real>::max();\n\tconst Point cp = crosspoint(s, seg);\n\treturn abs(cp - p) / abs(v);\n}\n\nReal collision_time(const Segment &a, const Segment &b, const Point &v_a) {\n\treturn min({collision_time(a, b[0], -v_a), collision_time(a, b[1], -v_a),\n\t\t\t\tcollision_time(b, a[0], v_a), collision_time(b, a[1], v_a)});\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n\n\tfor(int n, m, b; cin >> n >> m >> b && n;) {\n\t\tPolygon body(n);\n\t\tfor(auto &p : body) cin >> p;\n\n\t\tvector<int> t_body(m + 2);\n\t\tvector<Point> v_body(m + 1);\n\t\tfor(int i = 0; i < m; ++i) {\n\t\t\tcin >> t_body[i + 1] >> v_body[i];\n\t\t}\n\t\tt_body[m + 1] = INT_MAX;\n\t\tv_body[m] = Point(0, 0);\n\n\t\tvector<int> t_shoot(b), l_shoot(b);\n\t\tvector<Point> p_shoot(b), v_shoot(b);\n\t\tfor(int i = 0; i < b; ++i) {\n\t\t\tcin >> t_shoot[i] >> p_shoot[i].x >> v_shoot[i] >> l_shoot[i];\n\t\t}\n\n\t\tvector<bool> hit(b, false);\n\t\tvector<Real> t_hit;\n\n\t\tfor(int i = 0; i <= m; ++i) {\n\t\t\tfor(int j = 0; j < b; ++j) {\n\t\t\t\tif(hit[j]) continue;\n\t\t\t\tif(t_shoot[j] >= t_body[i + 1]) break;\n\n\t\t\t\tconst Point v = v_shoot[j] - v_body[i];\n\t\t\t\tif(sign(abs(v)) == 0) continue;\n\n\t\t\t\tconst Real T = t_body[i] - t_shoot[j];\n\t\t\t\tconst Point s = p_shoot[j] + v_shoot[j] * T;\n\n\t\t\t\tconst Segment bullet(s, s - v_shoot[j] * (l_shoot[j] / abs(v_shoot[j])));\n\t\t\t\tReal min_time = numeric_limits<Real>::max();\n\n\t\t\t\tPoint prev = body.back();\n\t\t\t\tfor(const Point &current : body) {\n\t\t\t\t\tconst Segment seg(prev, current);\n\n\t\t\t\t\tconst Real t = collision_time(bullet, seg, v);\n\t\t\t\t\tif(sign(t) >= 0) chmin(min_time, t + t_body[i]);\n\n\t\t\t\t\tprev = current;\n\t\t\t\t}\n\n\t\t\t\tif(sign(min_time - t_body[i + 1]) <= 0) {\n\t\t\t\t\thit[j] = true;\n\t\t\t\t\tt_hit.emplace_back(min_time);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tPoint v = v_body[i] * (t_body[i + 1] - t_body[i]);\n\t\t\tfor(auto &p : body) {\n\t\t\t\tp += v;\n\t\t\t}\n\t\t}\n\n\t\tsort(begin(t_hit), end(t_hit));\n\t\tcout << t_hit.size() << '\\n';\n\t\tfor(const auto &e : t_hit) {\n\t\t\tcout << e << '\\n';\n\t\t}\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1340, "score_of_the_acc": -0.0163, "final_rank": 2 }, { "submission_id": "aoj_2023_1368751", "code_snippet": "// * template\n\n#include <bits/stdc++.h>\n#ifdef LOCAL\n#include \"dump.hpp\"\n#else\n#define dump(...)\n#endif\n\nusing namespace std;\n\ntemplate<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n\ntemplate<class T, class U> inline void fill_array(T &e, const U &v) { e = v; }\ntemplate<class T, class U, size_t s> inline void fill_array(T (&a)[s], const U &v) {for(auto&e:a)fill_array(e,v);}\ntemplate<class T, class U, size_t s> inline void fill_array(array<T, s> &a, const U &v) {for(auto&e:a)fill_array(e,v);}\ntemplate<class T, class U> inline void fill_array(vector<T> &a, const U &v) {for(auto&e:a)fill_array(e,v);}\n\nstruct range {\n\ttypedef int Int;\n\tstruct iter {\n\t\tInt i;\n\t\tconst Int s;\n\t\tconstexpr iter(const Int &i_, const Int &s_):i(i_), s(s_) {}\n\t\tconstexpr bool operator!=(const iter &r) const { return s > 0 ? i < r.i : i > r.i; }\n\t\tconstexpr const Int &operator*() const { return i; }\n\t\titer &operator++() { i += s; return *this; }\n\t};\n\tconst Int f, l, s;\n\tconstexpr range(const Int &f_, const Int &l_, const Int &s_):f(f_), l(l_), s(s_) {}\n\tconstexpr range(const Int &f_, const Int &l_):f(f_), l(l_), s(1) {}\n\tconstexpr range(const Int &num):f(0), l(num), s(1) {}\n\tconstexpr iter begin() const { return iter(f, s); }\n\tconstexpr iter end() const { return iter(l, s); }\n};\n\n// * geoetry template\ntypedef double Real;\nconstexpr Real EPS = 1e-8;\nint sign(Real d) { return d > EPS ? 1 : d < -EPS ? -1 : 0; }\n\n// * define class\n\nstruct Point {\n\tReal x, y;\n\tPoint():x(0), y(0) {}\n\tPoint(Real x_, Real y_):x(x_), y(y_) {}\n\tPoint &operator+=(const Point &p) { x += p.x; y += p.y; return *this; }\n\tPoint &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; }\n\tPoint &operator*=(Real s) { x *= s; y *= s; return *this; }\n\tPoint &operator/=(Real s) { x /= s; y /= s; return *this; }\n\tPoint operator-() const { return Point(-x, -y); }\n\tPoint operator+(const Point &p) const { return Point(*this) += p; }\n\tPoint operator-(const Point &p) const { return Point(*this) -= p; }\n\tPoint operator*(Real s) const { return Point(*this) *= s; }\n\tPoint operator/(Real s) const { return Point(*this) /= s; }\n\tbool operator<(const Point &p) const { return sign(x - p.x) == -1 || (sign(x - p.x) == 0 && sign(y - p.y) == -1); }\n\tbool operator>(const Point &p) const { return p < *this; }\n\tbool operator==(const Point &p) const { return sign(x - p.x) == 0 && sign(y - p.y) == 0; }\n\tbool operator!=(const Point &p) const { return !(*this == p); }\n};\n\ninline istream &operator>>(istream &is, Point &p) {\n\treturn is >> p.x >> p.y;\n}\n\ninline ostream &operator<<(ostream &os, const Point &p) {\n\treturn os << '(' << p.x << \", \" << p.y << ')';\n}\n\nstruct Line : public array<Point, 2> {\n\tLine(const Point &a, const Point &b) { at(0) = a; at(1) = b; }\n};\n\nstruct Segment : public array<Point, 2> {\n\tSegment(const Point &a, const Point &b) { at(0) = a; at(1) = b; }\n};\n\nstruct Circle {\n\tPoint c;\n\tReal r;\n\tCircle(const Point &c_, Real r_):c(c_), r(r_) {}\n};\n\ntypedef vector<Point> Polygon;\n\n// * define base function\n\nPoint rotate90(const Point &p) {\n\treturn Point(-p.y, p.x);\n}\n\nPoint rotate(const Point &p, Real theta) {\n\tconst Real s = sin(theta), c = cos(theta);\n\treturn Point(c * p.x - s * p.y, s * p.x + c * p.y);\n}\n\nReal angle(const Point &p) {\n\treturn atan2(p.y, p.x);\n}\n\nReal norm(const Point &p) {\n\treturn p.x * p.x + p.y * p.y;\n}\n\nReal abs(const Point &p) {\n\treturn sqrt(norm(p));\n}\n\nReal dot(const Point &a, const Point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\nReal cross(const Point &a, const Point &b) {\n\treturn a.x * b.y - a.y * b.x;\n}\n\n// * define app function\n\nenum { CCW = 1, CW = -1, BACK = 2, FRONT = -2, ON = 0};\nint ccw(const Point &a, const Point &b, const Point &c) { // a:p0, b:p1, c:p2\n\tconst Point p = b - a;\n\tconst Point q = c - a;\n\tconst int sign_cross = sign(cross(p, q));\n\n\tif(sign_cross == 1) return CCW;\n\tif(sign_cross == -1) return CW;\n\tif(sign(dot(p, q)) == -1) return BACK;\n\tif(sign(norm(p) - norm(q)) == -1) return FRONT;\n\treturn ON;\n}\n\nPoint project(const Line &l, const Point &p) {\n\tconst Point a = p - l[0];\n\tconst Point b = l[1] - l[0];\n\treturn l[0] + b * (dot(a, b) / norm(b));\n}\n\nPoint reflect(const Line &l, const Point &p) {\n\tconst Point t = project(l, p);\n\treturn t + (t - p);\n}\n\nbool intersect(const Segment &s, const Point &p) {\n\treturn ccw(s[0], s[1], p) == ON;\n}\n\nbool intersect(const Segment &a, const Segment &b) {\n\treturn ccw(a[0], a[1], b[0]) * ccw(a[0], a[1], b[1]) <= 0\n\t\t&& ccw(b[0], b[1], a[0]) * ccw(b[0], b[1], a[1]) <= 0;\n}\n\nbool intersect(const Line &l, const Point &p) {\n\treturn abs(ccw(l[0], l[1], p)) != 1;\n}\n\nbool intersect(const Line &l, const Segment &s) {\n\treturn sign(cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0])) <= 0;\n}\n\nbool intersect(const Line &a, const Line &b) {\n\treturn sign(cross(a[1] - a[0], b[1] - b[0])) != 0\n\t\t|| sign(cross(a[1] - a[0], b[1] - a[0])) == 0;\n}\n\nReal dist(const Point &a, const Point &b) {\n\treturn abs(a - b);\n}\n\nReal dist(const Line &l, const Point &p) {\n\tconst Point a = l[1] - l[0];\n\tconst Point b = p - l[0];\n\treturn abs(cross(a, b)) / abs(a);\n}\n\nReal dist(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(dist(l, s[0]), dist(l, s[1]));\n}\n\nReal dist(const Line &a, const Line &b) {\n\tif(intersect(a, b)) return 0;\n\treturn dist(a, b[0]);\n}\n\nReal dist(const Segment &s, const Point &p) {\n\tif(sign(dot(s[1] - s[0], p - s[0])) == -1) return dist(s[0], p);\n\tif(sign(dot(s[0] - s[1], p - s[1])) == -1) return dist(s[1], p);\n\treturn dist(Line(s[0], s[1]), p);\n}\n\nReal dist(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({dist(a, b[0]), dist(a, b[1]), dist(b, a[0]), dist(b, a[1])});\n}\n\nPoint crosspoint(const Segment &a, const Segment &b) {\n\tconst Real crs = cross(a[1] - a[0], b[1] - b[0]);\n\tif(sign(crs) == 0) { // on same line\n\t\tif(intersect(a, b[0])) return b[0];\n\t\tif(intersect(a, b[1])) return b[1];\n\t\tif(intersect(b, a[0])) return a[0];\n\t\treturn a[1];\n\t}\n\treturn b[0] + (b[1] - b[0]) * (cross(a[1] - a[0], a[1] - b[0]) / crs);\n}\n\n// * solve\n\nReal collision_time(const Segment &s, const Point &p, const Point &v) {\n\tconst Segment seg(p, p + v * 1e7);\n\tif(!intersect(s, seg)) return numeric_limits<Real>::max();\n\tconst Point cp = crosspoint(s, seg);\n\treturn abs(cp - p) / abs(v);\n}\n\nReal collision_time(const Segment &a, const Segment &b, const Point &v_a) {\n\treturn min({collision_time(a, b[0], -v_a), collision_time(a, b[1], -v_a),\n\t\t\t\tcollision_time(b, a[0], v_a), collision_time(b, a[1], v_a)});\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n\n\tfor(int n, m, b; cin >> n >> m >> b && n;) {\n\t\tPolygon body(n);\n\t\tfor(auto &p : body) cin >> p;\n\n\t\tvector<int> t_body(m + 2);\n\t\tvector<Point> v_body(m + 1);\n\t\tfor(int i = 0; i < m; ++i) {\n\t\t\tcin >> t_body[i + 1] >> v_body[i];\n\t\t}\n\t\tt_body[m + 1] = INT_MAX;\n\t\tv_body[m] = Point(0, 0);\n\n\t\tvector<int> t_shoot(b), l_shoot(b);\n\t\tvector<Point> p_shoot(b), v_shoot(b);\n\t\tfor(int i = 0; i < b; ++i) {\n\t\t\tcin >> t_shoot[i] >> p_shoot[i].x >> v_shoot[i] >> l_shoot[i];\n\t\t}\n\n\t\tvector<bool> hit(b, false);\n\t\tvector<Real> t_hit;\n\n\t\tfor(int i = 0; i <= m; ++i) {\n\t\t\tfor(int j = 0; j < b; ++j) {\n\t\t\t\tif(hit[j]) continue;\n\t\t\t\tif(t_shoot[j] >= t_body[i + 1]) break;\n\n\t\t\t\tconst Point v = v_shoot[j] - v_body[i];\n\t\t\t\tif(sign(abs(v)) == 0) continue;\n\n\t\t\t\tconst Real T = t_body[i] - t_shoot[j];\n\t\t\t\tconst Point s = p_shoot[j] + v_shoot[j] * T;\n\n\t\t\t\tconst Segment bullet(s, s - v_shoot[j] * (l_shoot[j] / abs(v_shoot[j])));\n\t\t\t\tReal min_time = numeric_limits<Real>::max();\n\t\t\t\tdump(bullet, v);\n\n\t\t\t\tPoint prev = body.back();\n\t\t\t\tfor(const Point &current : body) {\n\t\t\t\t\tconst Segment seg(prev, current);\n\n\t\t\t\t\tconst Real t = collision_time(bullet, seg, v);\n\t\t\t\t\tif(sign(t) >= 0) chmin(min_time, t + t_body[i]);\n\n\t\t\t\t\tprev = current;\n\t\t\t\t}\n\n\t\t\t\tif(sign(min_time - t_body[i + 1]) <= 0) {\n\t\t\t\t\thit[j] = true;\n\t\t\t\t\tt_hit.emplace_back(min_time);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tPoint v = v_body[i] * (t_body[i + 1] - t_body[i]);\n\t\t\tfor(auto &p : body) {\n\t\t\t\tp += v;\n\t\t\t}\n\t\t}\n\n\t\tsort(begin(t_hit), end(t_hit));\n\t\tcout << t_hit.size() << '\\n';\n\t\tfor(const auto &e : t_hit) {\n\t\t\tcout << e << '\\n';\n\t\t}\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1340, "score_of_the_acc": -0.0178, "final_rank": 3 }, { "submission_id": "aoj_2023_1365132", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n \n#define eps (1e-5)\n#define INF (1e20)\n\nstatic const double PI = acos(-1);\nbool eq(double a,double b){\n return ( -eps < a-b && a-b < eps);\n}\n\nclass Point{\npublic:\n double x, y;\n Point ( double x = 0, double y = 0): x(x), y(y){}\n Point operator + ( Point p ){ return Point(x + p.x, y + p.y); }\n Point operator - ( Point p ){ return Point(x - p.x, y - p.y); }\n Point operator * ( double a ){ return Point(x*a, y*a); }\n Point operator / ( double a ){ return Point(x/a, y/a); }\n};\ntypedef Point Vector;\n\nclass Segment{\npublic:\n Point p1, p2;\n Segment(Point s = Point(), Point t = Point()): p1(s), p2(t){}\n Vector base(){\n return p2-p1;\n }\n};\ntypedef Segment Line;\n\nbool eq(Point a,Point b){ return (eq(a.x,b.x)&&eq(a.y,b.y)); }\ndouble norm( Vector a ){ return a.x*a.x + a.y*a.y; }\ndouble abs( Vector a ){ return sqrt(norm(a)); }\ndouble dot( Vector a, Vector b ){ return a.x*b.x + a.y*b.y; }\ndouble cross( Vector a, Vector b ){ return a.x*b.y - a.y*b.x; }\nbool isParallel(Vector a, Vector b){return eq(cross(a,b),0);}\n\nbool onSegment(Segment s,Point p){\n if( eq(s.p1,p) || eq(s.p2,p))return true;\n Vector a=s.p1-p,b=s.p2-p;\n return ( dot(a,b)<0 && eq(cross(a,b),0) );\n}\n\nVector unit(Vector v){\n if(eq(abs(v),0))return v;\n else return v/abs(v);\n}\n\nPoint getCrossPoint(Line a,Line b){\n return a.p1+(a.p2-a.p1)*\n (cross(b.p2-b.p1,b.p1-a.p1)/cross(b.p2-b.p1,a.p2-a.p1));\n} \n\nPoint getCrossPointLS(Line l,Segment s){\n Point p=getCrossPoint(l,s);\n if(onSegment(s,p))return p;\n else return (Point){INF,INF};\n}\n\ndouble calcCollision(Point a,Point p,Vector v){\n if(eq(a,p))return 0;\n else if(eq(unit(a-p),unit(v))) return abs(a-p)/abs(v);\n else return INF;\n}\n\ndouble calcCollision(Line l,Point p,Vector v){\n if(isParallel(l.base(),l.p1-p))return 0;\n if(isParallel(l.base(),v))return INF;\n Point q=getCrossPoint(l,(Line){p,p+v});\n return calcCollision(q,p,v);\n}\n\ndouble calcCollisionSP(Segment s,Point p,Vector v){\n if(onSegment(s,p))return 0;\n if(isParallel(s.base(),v))\n return min(calcCollision(s.p1,p,v),\n calcCollision(s.p2,p,v));\n Point q=getCrossPointLS((Line){p,p+v},s);\n return calcCollision(q,p,v);\n}\n\ndouble calcCollision(vector<Point> &t,Segment s,Vector v){\n double res=INF;\n int size=t.size();\n for(int i=0;i<size;i++)\n res=min(res,calcCollisionSP(s,t[i],v*-1.0));\n Segment sa;\n for(int i=0;i<size;i++){\n sa.p1=t[i];\n sa.p2=t[(i+1)%size];\n res=min(res,calcCollisionSP(sa,s.p1,v));\n res=min(res,calcCollisionSP(sa,s.p2,v));\n }\n return res;\n}\n\nint N,M,B;\nvector<Point> t,u;\ndouble T[101];\nVector V[101];\n\nSegment A[100],copyA[100];\n\nVector VA[100];\ndouble TA[100];\ndouble L[100];\nbool flg[100];\n\n\nvoid solve(){\n vector<double> ans;\n T[M]=1e9;\n V[M]=(Vector){0,0};\n \n double pre=0;\n for(int i=0;i<=M;i++){\n double di=T[i]-pre;\n \n for(int j=0;j<B;j++){\n if(flg[j])continue;\n double res=calcCollision(t,A[j],VA[j]-V[i]);\n if(res<-eps || di+eps<res)continue;\n flg[j]=true;\n ans.push_back(res+pre);\n }\n \n\n for(int j=0;j<N;j++)t[j]=t[j]+V[i]*di;\n for(int j=0;j<B;j++){\n A[j]=copyA[j];\n A[j].p1=A[j].p1+VA[j]*T[i];\n A[j].p2=A[j].p2+VA[j]*T[i];\n }\n pre=T[i];\n }\n \n sort(ans.begin(),ans.end());\n cout<<ans.size()<<endl;\n for(int i=0;i<(int)ans.size();i++)\n printf(\"%.8f\\n\",ans[i]);\n}\n\nvoid init(){\n t.clear();\n for(int i=0;i<100;i++){\n flg[i]=false;\n }\n}\n\nint main(){\n while(1){\n cin>>N>>M>>B;\n if(N==0&&M==0&&B==0)break;\n \n init();\n t.resize(N);\n for(int i=0;i<N;i++){\n cin>>t[i].x>>t[i].y;\n }\n u=t;\n for(int i=0;i<M;i++){\n cin>>T[i]>>V[i].x>>V[i].y;\n }\n for(int i=0;i<B;i++){\n cin>>TA[i];\n A[i].p1.y=0;\n cin>>A[i].p1.x;\n cin>>VA[i].x>>VA[i].y;\n cin>>L[i];\n A[i].p2=A[i].p1+ (unit(VA[i])*L[i]*-1.0);\n Vector vb=VA[i]*(-TA[i]);\n A[i].p1=A[i].p1+vb;\n A[i].p2=A[i].p2+vb;\n copyA[i]=A[i];\n }\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 1324, "score_of_the_acc": -0.0493, "final_rank": 5 }, { "submission_id": "aoj_2023_1203761", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define fs first\n#define sc second\n#define pb push_back\n#define show(x) cout << #x << \" \" << x << endl\ntypedef long double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\ntypedef vector<P> Pol;\nD inf=1e50,eps=1e-8,EPS=1e-8;\nint N,M,B;\nP o[101],v[101];\nD T[102];\t\t\t\t//T[i]~T[i+1] p=o[i]+t*v[i]\nPol pol;\nD cro(P a,P b){return imag(conj(a)*b);}\nD verlen(L l,P p){return cro(l.fs-l.sc,p-l.sc)/abs(l.fs-l.sc);}\nbool onL(L l,P p){\n\tif(abs(l.fs-p)<eps) return true;\n\tif(abs(l.sc-p)<eps) return true;\n\treturn abs(l.fs-l.sc)>abs(l.fs-p)&&abs(l.fs-l.sc)>abs(l.sc-p);\n}\nD gett(P p,P v,L l){\n\tif(abs(cro(l.sc-l.fs,v))<eps) return inf;\n\tD l0=verlen(l,p),l1=verlen(l,p+v);\n\tD tt=l0/(l0-l1);\n\tP pt=p+v*tt;\n\tif(onL(l,pt)) return tt;\n\treturn inf;\n}\nint main(){\n\twhile(true){\n\t\tcin>>N>>M>>B;\n\t\tif(N==0) break;\n\t\tpol.clear();\n\t\trep(i,N){\n\t\t\tint x,y;\n\t\t\tcin>>x>>y;\n\t\t\tpol.pb(P(x,y));\n\t\t}\n\t\to[0]=P(0,0);\n\t\trep(i,M){\n\t\t\tint vx,vy;\n\t\t\tcin>>T[i+1]>>vx>>vy;\n\t\t\tv[i]=P(vx,vy);\n\t\t\to[i+1]=o[i]+v[i]*(T[i+1]-T[i]);\n\t\t}\n\t\trep(i,M) o[i]-=T[i]*v[i];\n\t\tT[M+1]=inf;\n\t\tv[M]=P(0,0);\n\t\tvector<D> ans;\n\t\trep(b,B){\n\t\t\tD tt,x,len,vx,vy;\n\t\t\tcin>>tt>>x>>vx>>vy>>len;\n\t\t\tP vv=P(vx,vy);\n\t\t\tP oo=P(x,0)-tt*vv;\n\t\t\tD mn=inf;\n\t\t\trep(i,M+1){\n\t\t\t\trep(j,N){\n\t\t\t\t\tD it=gett(oo,vv-v[i],L(o[i]+pol[j],o[i]+pol[(j+1)%N]));\n\t\t\t\t\tif(T[i]-EPS<it&&it<T[i+1]+EPS) mn=min(mn,it);\n\t\t\t\t\tit=gett(oo-vv/abs(vv)*len,vv-v[i],L(o[i]+pol[j],o[i]+pol[(j+1)%N]));\n\t\t\t\t\tif(T[i]-EPS<it&&it<T[i+1]+EPS) mn=min(mn,it);\n\t\t\t\t\tit=gett(o[i]+pol[j],v[i]-vv,L(oo,oo-vv/abs(vv)*len));\n\t\t\t\t\tif(T[i]-EPS<it&&it<T[i+1]+EPS) mn=min(mn,it);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(mn<inf) ans.pb(mn);\n\t\t}\n\t\tsort(all(ans));\n\t\tcout<<ans.size()<<endl;\n\t\trep(i,ans.size()) printf(\"%.8f\\n\",(double)ans[i]);\n\t}\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 1340, "score_of_the_acc": -0.1489, "final_rank": 8 }, { "submission_id": "aoj_2023_1142959", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\n#include<vector>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\nint iSP(Pt a, Pt b, Pt c) {\n\tint s = sig((b - a).det(c - a));\n\tif (s) return s;\n\tif (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b\n\tif (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c\n\treturn 0;\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\nbool iLS(Pt a, Pt b, Pt c, Pt d) {\n\treturn (sig(tri(a, b, c)) * sig(tri(a, b, d)) <= 0);\n}\nbool iSS(Pt a, Pt b, Pt c, Pt d) {\n\treturn (iSP(a, b, c) * iSP(a, b, d) <= 0 && iSP(c, d, a) * iSP(c, d, b) <= 0);\n}\n\nPt p[30];\ndouble t[110];\ndouble vx[110];\ndouble vy[110];\ndouble ret[110];\ndouble h[110];\ndouble sx[110];\ndouble sl[110];\ndouble tx[110];\ndouble ty[110];\nint main(){\n\tint a,b,c;\n\twhile(scanf(\"%d%d%d\",&a,&b,&c),a){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble x,y;scanf(\"%lf%lf\",&x,&y);\n\t\t\tp[i]=Pt(x,y);\n\t\t}\n\t\tfor(int i=0;i<c;i++)ret[i]=999999999;\n\t\tfor(int i=0;i<b;i++){\n\t\t\tscanf(\"%lf%lf%lf\",t+i+1,vx+i,vy+i);\n\t\t}\n\t\tt[b+1]=999999999;\n\t\tvx[b]=vy[b]=0;\n\t\tfor(int i=0;i<c;i++){\n\t\t\tscanf(\"%lf%lf%lf%lf%lf\",h+i,sx+i,tx+i,ty+i,sl+i);\n\t\t}\n\t\tfor(int i=0;i<a;i++){\n\t\t\tPt now=p[i];\n\t\t\tfor(int j=0;j<b;j++){\n\t\t\t\tPt to=now+Pt(vx[j],vy[j])*(t[j+1]-t[j]);\n\t\t\t\tfor(int k=0;k<c;k++){\n\t\t\t\t\tif(iLS(Pt(sx[k],0),Pt(sx[k]+tx[k],ty[k]),now,to)){\n\t\t\t\t\t\tPt at=pLL(Pt(sx[k],0),Pt(sx[k]+tx[k],ty[k]),now,to);\n\t\t\t\t\t\tdouble T=t[j]+(at-now).ABS()/Pt(vx[j],vy[j]).ABS();\n\t\t\t\t\t\tPt L=Pt(sx[k],0)-Pt(tx[k],ty[k])/Pt(tx[k],ty[k]).ABS()*sl[k]+Pt(tx[k],ty[k])*(T-h[k]);\n\t\t\t\t\t\tPt R=Pt(sx[k],0)+Pt(tx[k],ty[k])*(T-h[k]);\n\t\t\t\t\t\tif(T-h[k]>0&&T>t[j]&&T<t[j+1]&&iSP(L,at,R)==2){\n\t\t\t\t\t\t\tret[k]=min(ret[k],T);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnow=to;\n\t\t\t}\n\t\t}\n\t//\tfor(int i=0;i<c;i++)printf(\"%f\\n\",ret[i]);\n\t\tfor(int i=0;i<c;i++){\n\t\t\tPt org=Pt(0,0);\n\t\t\tfor(int j=0;j<=b;j++){\n\t\t\t\tPt v=Pt(tx[i],ty[i])-Pt(vx[j],vy[j]);\n\t\t\t\tPt L=Pt(sx[i],0)-Pt(tx[i],ty[i])/Pt(tx[i],ty[i]).ABS()*sl[i]+Pt(tx[i],ty[i])*(t[j]-h[i])-org;\n\t\t\t\tPt L2=L+v*(t[j+1]-t[j]);\n\t\t\t\tPt R=Pt(sx[i],0)+Pt(tx[i],ty[i])*(t[j]-h[i])-org;\n\t\t\t\tPt R2=R+v*(t[j+1]-t[j]);\n\t\t\t\tfor(int k=0;k<a;k++){\n\t\t\t\t\tif(iSS(L,L2,p[k],p[(k+1)%a])){\n\t\t\t\t\t\tPt at=pLL(L,L2,p[k],p[(k+1)%a]);\n\t\t\t\t\t\tdouble tt=t[j]+(at-L).ABS()/v.ABS();\n\t\t\t\t\t\tif(tt>h[i])ret[i]=min(ret[i],tt);\n\t\t\t\t\t}\n\t\t\t\t\tif(iSS(R,R2,p[k],p[(k+1)%a])){\n\t\t\t\t\t\tPt at=pLL(R,R2,p[k],p[(k+1)%a]);\n\t\t\t\t\t\tdouble tt=t[j]+(at-R).ABS()/v.ABS();\n\t\t\t\t\t\tif(tt>h[i])ret[i]=min(ret[i],tt);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\torg=org+Pt(vx[j],vy[j])*(t[j+1]-t[j]);\n\t\t\t}\n\t\t}\n\t//\tfor(int i=0;i<c;i++)printf(\"%f\\n\",ret[i]);\n\t\tvector<double>ans;\n\t\tfor(int i=0;i<c;i++){\n\t\t\tif(ret[i]<9999999)ans.push_back(ret[i]);\n\t\t}\n\t\tstd::sort(ans.begin(),ans.end());\n\t\tprintf(\"%d\\n\",ans.size());\n\t\tfor(int i=0;i<ans.size();i++)printf(\"%.12f\\n\",ans[i]);\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1132, "score_of_the_acc": -0.0031, "final_rank": 1 } ]
aoj_2021_cpp
Princess in Danger お姫様の危機 English text is not available in this practice contest. ある貧乏な国のおてんばで勇敢なお姫様が,政略結婚のため別の国に嫁ぐことになった.ところが,お姫様を亡き者としようとしている悪漢に嫁ぎ先への道中で襲われお姫様が大けがをしてしまい,近くの病院に搬送された.ところが,悪漢はお姫様は確実に亡き者とするため特殊な毒を使っていた.そのため,お姫様を助けるには本国から特別な薬と冷凍された肉親の血液を大急ぎで運ばなければならなくなった. この血液は冷凍状態で輸送されるのだが,鮮度を保つため,前回の冷凍から最大 M 分以内に血液冷凍施設で再冷凍しなければならない.しかし,冷凍施設が設置されている場所は数カ所しかない. 血液は,十分に冷凍した状態から,再冷凍なしで M 分の間無事である.再冷凍しなくてすむ残り時間が S 分のときに, T 分間冷凍しないで輸送した場合,再冷凍しなくてすむ残り時間は S - T 分となる.再冷凍しなくてすむ残り時間は,再冷凍することにより,最大 M 分まで回復する.血液の再冷凍にかかる時間は,どれだけ冷凍するかに依存する.血液を冷凍施設で,1分冷凍するごとに再冷凍しなくてすむ残り時間は1分回復する. 本国の首都を出発する時点の,血液を再冷凍しなくてすむ残り時間は M 分である. お姫様の従者であるあなたは,大切な主君の命を救うため,本国の首都からお姫様が搬送された病院まで血液の鮮度を保ったまま輸送するための経路を計算しなければならない.そう,あなたの使命は,本国の首都からその病院への最短経路を割り出し,その最短時間を求めることである. Input 入力は,複数のデータセットより構成される.各データセットの最初の行には,6 つの非負の整数 N (2 ≦ N ≦ 100), M (1 ≦ M ≦ 100), L (0 ≦ L ≦ N - 2), K , A (0 ≦ A < N ), H (0 ≦ H < N ) が与えられる.これらはそれぞれ,町の数,再冷凍を行うまでの制限時間,冷凍施設のある町の数,町と町を直接結ぶ道路の数,本国の首都を表す番号,お姫様が搬送された病院のある町の番号を表す.本国の首都とお姫様が搬送された病院は,異なる町である.なお,町には 0 〜 N -1 までの番号がそれぞれ割り当てられているものとする.続く行には, L 個の非負の整数が 1 つの空白で区切られて与えられる.これらは,冷凍施設がある町の番号を表す.なお,本国の首都とお姫様が搬送された病院はこのリストには含まれないが,冷凍施設があるものと考えて良い.続く K 行では,町と町を結ぶ道路の情報が与えられる.その第 i 行目では,3 つの非負の整数 X , Y , T が 1 つの空白で区切られて与えられ,これは町 X と町 Y を直接結ぶ道があり,移動には時間 T を要するということを表す.なお,この道は双方向に通行可能である.また,町 X と町 Y を直接結ぶ道は高々一つしか無い. 入力は N = M = L = K = A = H = 0 の時に終了し,これはデータセットに含まれない. Output 各データセットについて,鮮度を保ったまま血液を届けることができる最短時間を出力せよ.もし届けることができない場合には "Help!" と出力せよ. Sample Input 2 1 0 1 0 1 0 1 2 3 1 1 2 0 1 2 0 2 1 1 2 1 3 2 1 2 0 1 2 0 2 1 1 2 1 4 4 1 4 1 3 2 0 1 2 1 2 4 0 2 1 3 0 3 5 3 2 6 0 3 1 2 2 1 2 1 0 1 3 4 1 2 4 1 4 1 2 2 0 2 5 4 2 6 0 3 1 2 4 2 4 2 1 2 4 3 1 0 1 5 1 4 2 2 0 3 0 0 0 0 0 0 Output for the Sample Input Help! 3 2 10 5 12
[ { "submission_id": "aoj_2021_10617724", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG // 配列外参照のエラー\n// #endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint INF32 = 2e9;\nll INF64 = 9e18;\n#define endl '\\n';\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\ninline void Yes(bool f);\n\n///////////////////ここから//////////////////////\nbool solve() {\n\n int N, M, L, K, A, H;\n cin >> N >> M >> L >> K >> A >> H;\n if (N == 0) {\n return false;\n }\n unordered_set<int> LS;\n for (int i = 0; i < L; i++) {\n int l;\n cin >> l;\n LS.insert(l);\n }\n vector<vector<pii>> G(N);\n for (int i = 0; i < K; i++) {\n int a, b, t;\n cin >> a >> b >> t;\n G[a].emplace_back(b, t);\n G[b].emplace_back(a, t);\n }\n vector<vector<int>> dist(N, vector<int>(M + 1, INF32));\n vector<vector<int>> f(N, vector<int>(M + 1, 0));\n priority_queue<piii, vector<piii>, greater<piii>> que;\n que.emplace(0, A, M);\n dist[A][M] = 0;\n while (!que.empty()) {\n auto [d, v, m] = que.top();\n que.pop();\n if (f[v][m]) {\n continue;\n }\n f[v][m] = 1;\n \n if (LS.count(v) && m < M) {\n int nv = v, nm = m + 1;\n if (dist[nv][nm] > dist[v][m] + 1) {\n dist[nv][nm] = dist[v][m] + 1;\n que.emplace(d+1,nv, nm);\n }\n }\n for (auto [nv, t] : G[v]) {\n if (t > m) {\n continue;\n }\n int nm = m - t;\n if (dist[nv][nm] > dist[v][m] + t) {\n dist[nv][nm] = dist[v][m] + t;\n que.emplace(d+t,nv, nm);\n }\n }\n }\n\n int ans = INF32;\n for (int m = 0; m <= M; m++) {\n if (ans > dist[H][m]) {\n ans = dist[H][m];\n }\n }\n if (ans == INF32) {\n print(\"Help!\");\n } else {\n print(ans);\n }\n\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve())\n ;\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3968, "score_of_the_acc": -0.2362, "final_rank": 6 }, { "submission_id": "aoj_2021_10600491", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M, L, K, A, H;\n cin >> N >> M >> L >> K >> A >> H;\n if (N == 0 && M == 0 && L == 0 && K == 0 && A == 0 && H == 0) return false;\n vector<bool> hos(N);\n hos[A] = hos[H] = true;\n for (int u; L--; ) {\n cin >> u;\n hos[u] = true;\n }\n vector<vector<pair<int, int>>> G(N);\n for (int u, v, w; K--; ) {\n cin >> u >> v >> w;\n G[u].emplace_back(v, w);\n G[v].emplace_back(u, w);\n }\n vector<vector<int>> D(N, vector<int>(M + 1, 1 << 30));\n using T = tuple<int, int, int>;\n priority_queue<T, vector<T>, greater<T>> q;\n D[A][M] = 0;\n q.emplace(0, A, M);\n while (q.size()) {\n auto [d, u, m] = q.top();\n q.pop();\n for (auto [v, w] : G[u]) {\n if (m >= w && D[v][m - w] > d + w) {\n D[v][m - w] = d + w;\n q.emplace(d + w, v, m - w);\n }\n }\n if (hos[u] && m < M && D[u][m + 1] > d + 1) {\n D[u][m + 1] = d + 1;\n q.emplace(d + 1, u, m + 1);\n }\n }\n int ans = 1 << 30;\n for (int m = 0; m <= M; m++) {\n ans = min(ans, D[H][m]);\n }\n if (ans == 1 << 30) {\n cout << \"Help!\\n\";\n } else {\n cout << ans << '\\n';\n }\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3968, "score_of_the_acc": -0.2258, "final_rank": 3 }, { "submission_id": "aoj_2021_10558418", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nbool solve();\nint main(){\n while(solve());\n return 0;\n}\n\nusing pii = pair<int,int>;\n\nbool solve(){\n int N, M, L, K, A, H;\n cin>>N>>M>>L>>K>>A>>H;\n if(N==0)return false;\n vector<bool> have_frz(N,false);\n for(int i=0;i<L;i++){\n int l;cin>>l;\n have_frz[l]=true;\n }\n vector<vector<pii>>G(N);\n for(int i=0;i<K;i++){\n int u,v,c;cin>>u>>v>>c;\n if(c>M)continue;\n G[u].push_back(pii(v,c));\n G[v].push_back(pii(u,c));\n }\n const int inf=1e8;\n vector dp(N,vector<int>(M+1,inf));\n priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>>q;\n q.push(pair<int,pii>(0,pii(A,M)));\n dp[A][M]=0;\n while(!q.empty()){\n int u=q.top().second.first;\n int m=q.top().second.second;\n q.pop();\n for(auto [to,c]:G[u]){\n int nm=m-c;\n if(nm<0)continue;\n if(dp[to][nm]>dp[u][m]+c){\n dp[to][nm]=dp[u][m]+c;\n q.push(pair<int,pii>(dp[to][nm],pii(to,m-c)));\n }\n }\n if(have_frz[u])for(int i=1;;i++){\n int nm=m+i;\n if(nm>M)break;\n if(dp[u][nm]>dp[u][m]+i){\n dp[u][nm]=dp[u][m]+i;\n q.push(pair<int,pii>(dp[u][nm],pii(u,nm)));\n } \n }\n }\n int ans=inf;\n for(int i=0;i<=M;i++)ans=min(ans,dp[H][i]);\n if(ans==inf){\n cout<<\"Help!\"<<endl;\n }else{\n cout<<ans<<endl;\n }\n return true;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3840, "score_of_the_acc": -0.2724, "final_rank": 7 }, { "submission_id": "aoj_2021_10485389", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nbool solve(){\n int N, M, L, K, A, H; cin >> N >> M >> L >> K >> A >> H;\n // cerr << \"# \" << N << \" \" << M << \" \" << L << \" \" << K << \" \" << A << \" \" << H << endl;\n if(N == 0) return false;\n auto V = [&](int n, int m) -> int {return n * (M + 1) + m;};\n vector G(N * (M + 1), vector<pair<int, int>>{});\n for(int i = 0; i < L; ++i){\n int l; cin >> l;\n for(int j = 0; j < M; ++j){\n G[V(l, j)].emplace_back(V(l, j + 1), 1);\n }\n }\n for(int i = 0; i < K; ++i){\n int X, Y, T; cin >> X >> Y >> T;\n for(int j = T; j <= M; ++j){\n G[V(X, j)].emplace_back(V(Y, j - T), T);\n G[V(Y, j)].emplace_back(V(X, j - T), T);\n }\n }\n using P = pair<int, int>;\n priority_queue<P, vector<P>, greater<P>> que;\n int INF = 1 << 28;\n vector dist(N * (M + 1), INF);\n que.emplace(0, V(A, M));\n dist[V(A, M)] = 0;\n while(que.size()){\n auto [d, u] = que.top(); que.pop();\n if(dist[u] != d) continue;\n for(auto [v, c] : G[u]){\n if(d + c < dist[v]){\n // cerr << \"# \" << u << \" -> \" << v << \" : \" << c << endl;\n dist[v] = d + c;\n que.emplace(d + c, v);\n }\n }\n }\n // for(int i = 0; i < N; ++i){\n // cerr << \"#\";\n // for(int j = 0; j <= M; ++j){\n // cerr << \" \" << dist[V(i, j)];\n // }\n // cerr << endl;\n // }\n int ans = INF;\n for(int j = 0; j <= M; ++j){\n ans = min(ans, dist[V(H, j)]);\n }\n if(ans == INF) cout << \"Help!\" << endl;\n else cout << ans << endl;\n return true;\n}\n\nint main(){\n while(1){\n if(!solve()) break;\n }\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 5356, "score_of_the_acc": -0.9044, "final_rank": 17 }, { "submission_id": "aoj_2021_10451570", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nusing edge = pair<int, ll>; // {to, w}\n\nconstexpr bool DEBUG = false;\nconstexpr ll INF = 2e18;\n\nll solve(int N, int M, int L, int K, int S, int G) {\n vector<bool> frz(N, false);\n for (int i = 0; i < L; i++) {\n int u;\n cin >> u;\n frz[u] = true;\n }\n frz[S] = true;\n frz[G] = true;\n\n vector<vector<edge>> graph(N);\n for (int i = 0; i < K; i++) {\n int u, v, t;\n cin >> u >> v >> t;\n graph[u].emplace_back(v, t);\n graph[v].emplace_back(u, t);\n }\n\n vector<vector<ll>> dist(N, vector<ll>(M + 1, INF));\n priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq; // {cost, from, rem}\n dist[S][M] = 0;\n pq.emplace(0, S, M);\n while (!pq.empty()) {\n auto [cost, from, rem] = pq.top();\n pq.pop();\n if (cost > dist[from][rem]) {\n continue;\n }\n\n for (auto [to, w] : graph[from]) {\n ll next_rem = rem - w;\n ll next_cost = cost + w;\n\n if (rem < w) {\n continue;\n }\n\n if (dist[to][next_rem] > next_cost) {\n dist[to][next_rem] = next_cost;\n pq.emplace(next_cost, to, next_rem);\n }\n }\n\n if (frz[from] && rem < M) {\n int to = from;\n ll next_cost = cost + 1;\n ll next_rem = rem + 1;\n if (dist[to][next_rem] > next_cost) {\n pq.emplace(next_cost, to, next_rem);\n }\n }\n }\n\n ll ans = INF;\n for (int rem = 0; rem <= M; rem++) {\n ans = min(ans, dist[G][rem]);\n }\n return ans;\n}\n\nint main() {\n while (true) {\n int N, M, L, K, A, H;\n cin >> N >> M >> L >> K >> A >> H;\n if (N == 0) {\n break;\n }\n ll ans = solve(N, M, L, K, A, H);\n if (ans == INF) {\n cout << \"Help!\" << endl;\n } else {\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3840, "score_of_the_acc": -0.3239, "final_rank": 10 }, { "submission_id": "aoj_2021_10450960", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nusing edge = pair<int, ll>; // {to, w}\n\nconstexpr bool DEBUG = false;\nconstexpr ll INF = 2e18;\n\nll solve(int N, int M, int L, int K, int S, int G) {\n vector<bool> has_freeze(N, false);\n for (int i = 0; i < L; i++) {\n int u;\n cin >> u;\n has_freeze[u] = true;\n }\n has_freeze[S] = true;\n has_freeze[G] = true;\n\n vector<vector<edge>> graph(N);\n for (int i = 0; i < K; i++) {\n int u, v, t;\n cin >> u >> v >> t;\n graph[u].emplace_back(v, t);\n graph[v].emplace_back(u, t);\n }\n\n vector<vector<ll>> dist(N, vector<ll>(M + 1, INF));\n priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<>> pq; // {cost, from, rem}\n dist[S][M] = 0;\n pq.emplace(0, S, M);\n while (!pq.empty()) {\n auto [cost, from, rem] = pq.top();\n pq.pop();\n if (cost > dist[from][rem]) {\n continue;\n }\n\n for (auto [to, w] : graph[from]) {\n ll next_rem = rem - w;\n ll next_cost = cost + w;\n\n if (rem < w) {\n continue;\n }\n\n if (dist[to][next_rem] > next_cost) {\n dist[to][next_rem] = next_cost;\n pq.emplace(next_cost, to, next_rem);\n }\n }\n\n if (has_freeze[from] && rem < M) {\n int to = from;\n ll next_cost = cost + 1;\n ll next_rem = rem + 1;\n if (dist[to][next_rem] > next_cost) {\n pq.emplace(next_cost, to, next_rem);\n }\n }\n }\n\n ll ans = INF;\n for (int rem = 0; rem <= M; rem++) {\n ans = min(ans, dist[G][rem]);\n }\n return ans;\n}\n\nint main() {\n while (true) {\n int N, M, L, K, A, H;\n cin >> N >> M >> L >> K >> A >> H;\n if (N == 0) {\n break;\n }\n ll ans = solve(N, M, L, K, A, H);\n if (ans == INF) {\n cout << \"Help!\" << endl;\n } else {\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3968, "score_of_the_acc": -0.3599, "final_rank": 12 }, { "submission_id": "aoj_2021_10450923", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = LLONG_MAX / 4;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int N, M, L, K, A, H;\n cin >> N >> M >> L >> K >> A >> H;\n // 全てが0なら入力終了\n if (!cin || (N == 0 && M == 0 && L == 0 && K == 0 && A == 0 && H == 0))\n break;\n\n // 冷凍施設の有無を管理 (町ごと)\n vector<bool> has_freeze(N, false);\n for (int i = 0; i < L; i++) {\n int x; cin >> x;\n has_freeze[x] = true;\n }\n // 首都(A)と病院(H)も冷凍施設があるとみなす\n has_freeze[A] = has_freeze[H] = true;\n\n // グラフの隣接リスト (町uから町vへ移動時間t)\n vector<vector<pair<int,int>>> graph(N);\n for (int i = 0; i < K; i++) {\n int u, v, t;\n cin >> u >> v >> t;\n graph[u].emplace_back(v, t);\n graph[v].emplace_back(u, t);\n }\n\n // dist[u][r] = 残り鮮度r分で町uに到達するための最小時間\n vector<vector<ll>> dist(N, vector<ll>(M + 1, INF));\n // 優先度付きキュー (最小時間優先): (time, node, remaining)\n using T = tuple<ll,int,int>;\n priority_queue<T, vector<T>, greater<T>> pq;\n dist[A][M] = 0; // 首都出発時は鮮度M分\n pq.emplace(0, A, M);\n\n // Dijkstra 法\n while (!pq.empty()) {\n auto [t, u, rem] = pq.top();\n pq.pop();\n if (t > dist[u][rem]) continue;\n\n // 1) 道路を移動する遷移\n for (auto &e : graph[u]) {\n int v = e.first;\n int cost = e.second;\n if (rem >= cost) {\n int next_rem = rem - cost;\n ll nt = t + cost;\n if (nt < dist[v][next_rem]) {\n dist[v][next_rem] = nt;\n pq.emplace(nt, v, next_rem);\n }\n }\n }\n // 2) 再冷凍する遷移 (冷凍施設があり、残り鮮度 < M の場合)\n if (has_freeze[u] && rem < M) {\n int next_rem = rem + 1;\n ll nt = t + 1;\n if (nt < dist[u][next_rem]) {\n dist[u][next_rem] = nt;\n pq.emplace(nt, u, next_rem);\n }\n }\n }\n\n // 病院(H)への到達時間の最小値を求める\n ll ans = INF;\n for (int rem = 0; rem <= M; rem++) {\n ans = min(ans, dist[H][rem]);\n }\n if (ans == INF) {\n cout << \"Help!\" << '\\n';\n } else {\n cout << ans << '\\n';\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3968, "score_of_the_acc": -0.2258, "final_rank": 3 }, { "submission_id": "aoj_2021_10450916", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = LLONG_MAX / 4;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int N, M, L, K, A, H;\n cin >> N >> M >> L >> K >> A >> H;\n if (!cin || (N == 0 && M == 0 && L == 0 && K == 0 && A == 0 && H == 0))\n break;\n\n vector<bool> has_freeze(N, false);\n for (int i = 0; i < L; i++) {\n int x; cin >> x;\n has_freeze[x] = true;\n }\n has_freeze[A] = has_freeze[H] = true;\n\n vector<vector<pair<int,int>>> graph(N);\n for (int i = 0; i < K; i++) {\n int u, v, t;\n cin >> u >> v >> t;\n graph[u].emplace_back(v, t);\n graph[v].emplace_back(u, t);\n }\n\n vector<vector<ll>> dist(N, vector<ll>(M + 1, INF));\n using T = tuple<ll,int,int>;\n priority_queue<T, vector<T>, greater<T>> pq;\n dist[A][M] = 0;\n pq.emplace(0, A, M);\n\n while (!pq.empty()) {\n auto [t, u, rem] = pq.top();\n pq.pop();\n if (t > dist[u][rem]) continue;\n for (auto &e : graph[u]) {\n int v = e.first;\n int cost = e.second;\n if (rem >= cost) {\n int next_rem = rem - cost;\n ll nt = t + cost;\n if (nt < dist[v][next_rem]) {\n dist[v][next_rem] = nt;\n pq.emplace(nt, v, next_rem);\n }\n }\n }\n if (has_freeze[u] && rem < M) {\n int next_rem = rem + 1;\n ll nt = t + 1;\n if (nt < dist[u][next_rem]) {\n dist[u][next_rem] = nt;\n pq.emplace(nt, u, next_rem);\n }\n }\n }\n\n ll ans = INF;\n for (int rem = 0; rem <= M; rem++) {\n ans = min(ans, dist[H][rem]);\n }\n if (ans == INF) {\n cout << \"Help!\" << '\\n';\n } else {\n cout << ans << '\\n';\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3968, "score_of_the_acc": -0.2258, "final_rank": 3 }, { "submission_id": "aoj_2021_9591889", "code_snippet": "#include <bits/stdc++.h>\n using namespace std;\n #include <ext/pb_ds/assoc_container.hpp>\n using namespace __gnu_pbds;\n \n template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\n template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n #define rep(i,n) for (int i = 0; i < (n); ++i)\n\n typedef long long ll;\n typedef unsigned long long ull;\n typedef long double ld;\n using P=pair<int,int>;\n using T=tuple<int,int,int>;\n const ll INF=1001001001; \n const ll INFL=1e18;\n const ll mod=998244353;\n\n\n void solve(int n,int m,int l,int k,int a,int h){\n vector<int>frozen(n);\n rep(i,l){\n int p;\n cin>>p;\n frozen[p]=1;\n }\n vector<vector<P>>G(n);\n rep(i,k){\n int u,v,d;\n cin>>u>>v>>d;\n G[u].emplace_back(v,d);\n G[v].emplace_back(u,d);\n }\n vector<vector<int>>dist(n,vector<int>(m+1,INF));\n priority_queue<T,vector<T>,greater<T>>q;\n q.push(make_tuple(0,a,m));\n dist[a][m]=0;\n while(q.size()){\n auto[d,v,M]=q.top();q.pop();\n if(d>dist[v][M])continue;\n for(auto[u,nd]:G[v]){\n if(M-nd<0)continue;\n if(chmin(dist[u][M-nd],dist[v][M]+nd)){\n q.push(make_tuple(dist[u][M-nd],u,M-nd));\n }\n }\n if(frozen[v]){\n for(int i=M+1;i<=m;i++){\n if(chmin(dist[v][i],dist[v][M]+(i-M))){\n q.push(make_tuple(dist[v][i],v,i));\n }\n }\n }\n }\n int ans=INF;\n rep(i,m+1){\n chmin(ans,dist[h][i]);\n }\n if(ans==INF){\n cout<<\"Help!\"<<endl;\n }else{\n cout<<ans<<endl;\n }\n }\n\n int main(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n while(1){\n int n,m,l,k,a,h;\n cin>>n>>m>>l>>k>>a>>h;\n if(n==0&&m==0&&l==0&&k==0&&a==0&&h==0){\n break;\n }\n solve(n,m,l,k,a,h);\n }\n return 0;\n }", "accuracy": 1, "time_ms": 300, "memory_kb": 3968, "score_of_the_acc": -0.2877, "final_rank": 9 }, { "submission_id": "aoj_2021_9456316", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N, M, L, K, S, G;\n cin >> N >> M >> L >> K >> S >> G;\n if (N == 0) return 0;\n vector<int> X;\n X.push_back(S), X.push_back(G);\n rep(i,0,L) {\n int _;\n cin >> _;\n X.push_back(_);\n }\n L += 2;\n vector<vector<int>> D1(N,vector<int>(N,inf));\n rep(i,0,N) D1[i][i] = 0;\n rep(i,0,K) {\n int s, t, c;\n cin >> s >> t >> c;\n chmin(D1[s][t], c);\n chmin(D1[t][s], c);\n }\n rep(k,0,N) {\n rep(i,0,N) {\n rep(j,0,N) {\n chmin(D1[i][j], D1[i][k] + D1[k][j]);\n }\n }\n }\n vector<vector<int>> D2(L,vector<int>(L,inf));\n rep(i,0,L) {\n rep(j,0,L) {\n if (D1[X[i]][X[j]] <= M) chmin(D2[i][j], D1[X[i]][X[j]]);\n }\n }\n rep(k,0,L) {\n rep(i,0,L) {\n rep(j,0,L) {\n chmin(D2[i][j], D2[i][k] + D2[k][j]);\n }\n }\n }\n if (D2[0][1] == inf) cout << \"Help!\" << endl;\n else cout << D2[0][1] + max(0,D2[0][1] - M) << endl;\n}\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 3620, "score_of_the_acc": -0.4271, "final_rank": 14 }, { "submission_id": "aoj_2021_9403083", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef LOCAL\n#include \"./debug.hpp\"\n#else\n#define debug(...)\n#define print_line\n#endif\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<ll>;\nusing pi = pair<int,int>;\nusing pl = pair<ll,ll>;\nconst int INF = 1e9 + 10;\nconst ll INFL = 4e18;\n#define rep(i, a, b) for (ll i = (a); i < (ll)(b); i++)\n#define all(x) x.begin(),x.end()\n\ntemplate <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }\ntemplate <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }\n\n// --------------------------------------------------------\n\nint D[101][101];\n\nint main(){\n\twhile(true){\n\t\tint N,M,L,K,A,H;cin>>N>>M>>L>>K>>A>>H;\n\t\tif(N==0)break;\n\t\tfor(int i=0;i<N;i++)for(int j=0;j<=M;j++)D[i][j]=INF;\n\t\tvector<bool>cool(N,false);\n\t\tfor(int i=0;i<L;i++){\n\t\t\tint l;cin>>l;\n\t\t\tcool[l]=true;\n\t\t}\n\t\tcool[A]=true;\n\t\tcool[H]=true;\n\t\tvector<vector<pair<int,int>>>G(N);\n\t\tfor(int i=0;i<K;i++){\n\t\t\tint X,Y,T;cin>>X>>Y>>T;\n\t\t\tif(T>M)continue;\n\t\t\tG[X].push_back({Y,T});\n\t\t\tG[Y].push_back({X,T});\n\t\t}\n\n\t\tD[A][M]=0;\n\t\tusing T=tuple<int,int,int>;\n\t\tpriority_queue<T,vector<T>,greater<>>pq;\n\t\tpq.push({0,A,M});\n\t\twhile(!pq.empty()){\n\t\t\tauto[time,now,rem]=pq.top();pq.pop();\n\t\t\tfor(auto[nxt,cost]:G[now]){\n\t\t\t\tif(rem-cost>=0&&D[nxt][rem-cost]>time+cost){\n\t\t\t\t\tD[nxt][rem-cost]=time+cost;\n\t\t\t\t\tpq.push({time+cost,nxt,rem-cost});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(cool[now]&&rem+1<=M&&D[now][rem+1]>time+1){\n\t\t\t\tD[now][rem+1]=time+1;\n\t\t\t\tpq.push({time+1,now,rem+1});\n\t\t\t}\n\t\t}\n\n\t\tint ans=INF;\n\t\tfor(int i=0;i<=M;i++)ans=min(ans,D[H][i]);\n\n\t\tif(ans==INF)cout<<\"Help!\"<<endl;\n\t\telse cout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3380, "score_of_the_acc": -0.1328, "final_rank": 1 }, { "submission_id": "aoj_2021_9370417", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <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; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <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; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <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; }\ntemplate <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; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nvoid solve(){\n while(true){\n ll N,M,L,K,A,H;\n cin>>N>>M>>L>>K>>A>>H;\n if(N==0&&M==0&&L==0&&K==0&&A==0&&H==0)return;\n set<ll> st;\n rep(i,L){\n ll r;cin>>r;\n st.insert(r);\n }\n vector<vector<pl>> G(N);\n rep(i,K){\n ll x,y,t;cin>>x>>y>>t;\n G[x].push_back({y,t});\n G[y].push_back({x,t});\n }\n //頑張ってダイクストラ\n //dist[i][残り時間] = 最小コスト\n vvl dist(N,vl(M+1,INF));\n dist[A][M]=0;\n //最短経路,場所,残りのうけつ\n rp_queue<tuple<ll,ll,ll>> que;\n que.push({0,A,M});\n while(!que.empty()){\n auto [d,a,m] = que.top();\n que.pop();\n //すでに最短経路があればいらない\n if(d>dist[a][m])continue;\n //再度輸血する\n if(st.count(a)&&m+1<=M&&chmin(dist[a][m+1],dist[a][m]+1)){\n que.push({dist[a][m+1],a,m+1});\n }\n for(auto [v,t]:G[a]){\n if(m-t>=0&&chmin(dist[v][m-t],dist[a][m]+t)){\n que.push({dist[v][m-t],v,m-t});\n }\n }\n }\n ll ans = INF;\n for(int j=0;j<=M;j++)chmin(ans,dist[H][j]);\n if(ans==INF)cout<<\"Help!\"<<endl;\n else cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3712, "score_of_the_acc": -0.2776, "final_rank": 8 }, { "submission_id": "aoj_2021_9258353", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define rep(i,n) for(ll i = 0;i < (ll)(n);i++)\nconst ll INF = 1LL << 60;\n\nvoid chmin(ll &a,ll b){\n if(a > b){\n a = b;\n }\n}\n\n\nll warshal(vector<vector<ll>> &dist){\n ll siz =dist.size();\n rep(k,siz)rep(i,siz)rep(j,siz){\n if(i == j){\n chmin(dist[i][j],0LL);\n }\n if(dist[i][k] != INF && dist[k][j] != INF){\n chmin(dist[i][j],dist[i][k] + dist[k][j]);\n }\n }\n rep(i,siz){\n if(dist[i][i] < 0){\n return -1;\n }\n }\n return 1;\n}\n\nvoid solve(ll n,ll m,ll l,ll k,ll a,ll h){\n vector<ll> f(l);\n rep(i,l){\n cin >> f[i];\n }\n f.push_back(a);\n f.push_back(h);\n vector<vector<ll>> g(n,vector<ll>(n,INF));\n rep(i,k){\n ll x,y,c;\n cin >> x>> y >> c;\n g[x][y] = c;\n g[y][x] = c;\n }\n\n warshal(g);\n\n vector<vector<pair<ll,ll>>> gg(n);\n for(auto & p:f){\n for(auto & q:f){\n if(p == q)continue;\n if(g[p][q] < INF){\n gg[p].push_back({q,g[p][q]});\n }\n }\n }\n \n\n vector<ll> dist(n,INF);\n queue<ll> que;\n dist[a] = 0;\n que.push(a);\n while(!que.empty()){\n ll now = que.front();\n que.pop();\n for(auto [i,cost]:gg[now]){\n\n if(cost <= m && dist[now] + cost < dist[i]){\n dist[i] = dist[now] + cost;\n que.push(i);\n }\n }\n }\n if(dist[h] == INF){\n cout << \"Help!\" << endl;\n }else{\n if(dist[h] <= m){\n cout << dist[h] << endl;\n }else{\n cout << dist[h] + (dist[h] - m) << endl;\n }\n }\n\n}\n\nint main(){\n while(true){\n ll n,m,l,k,a,h;\n cin >> n >> m >> l >> k >> a >> h;\n if(n + m + l + k + a + h == 0)break;\n solve(n,m,l,k,a,h);\n }\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 3684, "score_of_the_acc": -0.4244, "final_rank": 13 }, { "submission_id": "aoj_2021_9253686", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,a,b) for(int i=a;i<b;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define dbgv(x); for(auto now : x) cout << now << \" \"; cout << endl;\n\nstruct UnionFind {\n vector<int> par;\n UnionFind(int n=0): par(n,-1) {}\n int find(int x) {\n if(par[x] < 0) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y){\n x = find(x); y = find(y);\n if(x==y) return false;\n if(par[x] > par[y]) swap(x,y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n bool same(int x, int y) {return find(x) == find(y);}\n int size(int x) {return -par[find(x)];}\n};\n\nint main(){\n int n, m, l, k, a, h;\n while (1) {\n cin >> n >> m >> l >> k >> a >> h;\n if(n == 0) break;\n vector<vector<pair<int, int> > > g(n);\n vector<bool> heal(n);\n for (int i = 0; i < l; ++i) {\n int p;\n cin >> p;\n heal[p] = true;\n }\n for (int i = 0; i < k; ++i) {\n int u, v, c;\n cin >> u >> v >> c;\n g[u].emplace_back(v, c);\n g[v].emplace_back(u, c);\n }\n\n vector<vector<long long> > dist(n, vector<long long>(m + 1, 1LL<<60));\n priority_queue<tuple<long long, int, int>,vector<tuple<long long, int, int>>,greater<tuple<long long, int, int>>> pq;\n\n dist[a][m] = 0;\n pq.emplace(0, a, m);\n\n while (!pq.empty()) {\n auto [cd, cn, cl] = pq.top();\n pq.pop();\n if (dist[cn][cl] < cd) continue;\n if (heal[cn]) {\n for (int i = 1; cl + i <= m; ++i) {\n if (dist[cn][cl + i] > dist[cn][cl] + i) {\n dist[cn][cl + i] = dist[cn][cl] + i;\n pq.emplace(dist[cn][cl + i], cn, cl + i);\n }\n }\n }\n for (auto [nn, len] : g[cn]) {\n if (cl - len >= 0 && dist[nn][cl - len] > dist[cn][cl] + len) {\n dist[nn][cl - len] = dist[cn][cl] + len;\n pq.emplace(dist[nn][cl - len], nn, cl - len);\n }\n }\n }\n\n long long ans = *min_element(dist[h].begin(),dist[h].end());\n\n if (ans == (long long)1LL<<60) {\n cout << \"Help!\\n\";\n } else {\n cout << ans << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 3704, "score_of_the_acc": -0.3373, "final_rank": 11 }, { "submission_id": "aoj_2021_9214852", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int n, max_blood, cold_n, m, s, g;\n std::cin >> n >> max_blood >> cold_n >> m >> s >> g;\n if (n == 0) return 1;\n std::vector graph(n, std::vector<std::pair<int, int>>());\n std::vector<bool> is_cold(n);\n for (int i = 0; i < cold_n; i++) {\n int u;\n std::cin >> u;\n is_cold[u] = true;\n }\n for (int i = 0; i < m; i++) {\n int u, v, w;\n std::cin >> u >> v >> w;\n graph[u].emplace_back(v, w);\n graph[v].emplace_back(u, w);\n }\n\n const int INF = 1e9;\n using P = std::tuple<int, int, int>;\n std::priority_queue<P, std::vector<P>, std::greater<P>> que;\n std::vector dist(n, std::vector<int>(max_blood + 1, INF));\n dist[s][max_blood] = 0;\n que.emplace(dist[s][max_blood], s, max_blood);\n while (!que.empty()) {\n auto [d, u, i] = que.top();\n que.pop();\n if (dist[u][i] < d) continue;\n if (is_cold[u] && i < max_blood) {\n if (dist[u][i + 1] > dist[u][i] + 1) {\n dist[u][i + 1] = dist[u][i] + 1;\n que.emplace(dist[u][i + 1], u, i + 1);\n }\n }\n for (auto [v, w]: graph[u]) {\n if (i - w >= 0 && dist[v][i - w] > dist[u][i] + w) {\n dist[v][i - w] = dist[u][i] + w;\n que.emplace(dist[v][i - w], v, i - w);\n }\n }\n }\n int ans = INF;\n for (int i = 0; i <= max_blood; i++) {\n ans = std::min(ans, dist[g][i]);\n }\n if (ans == INF) {\n std::cout << \"Help!\" << '\\n';\n } else {\n std::cout << ans << '\\n';\n }\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3596, "score_of_the_acc": -0.1935, "final_rank": 2 }, { "submission_id": "aoj_2021_9158219", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\ntemplate <typename T = int>\nstruct Edge {\n int from, to;\n T cost;\n int idx;\n Edge()\n : from(-1), to(-1), cost(-1), idx(-1) {}\n Edge(int from, int to, T cost = 1, int idx = -1)\n : from(from), to(to), cost(cost), idx(idx) {}\n operator int() const {\n return to;\n }\n};\ntemplate <typename T = int>\nstruct Graph {\n Graph(int N)\n : n(N), es(0), g(N) {}\n int size() const {\n return n;\n }\n int edge_size() const {\n return es;\n }\n void add_edge(int from, int to, T cost = 1) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n g[from].emplace_back(from, to, cost, es);\n g[to].emplace_back(to, from, cost, es++);\n }\n void add_directed_edge(int from, int to, T cost = 1) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n g[from].emplace_back(from, to, cost, es++);\n }\n inline vector<Edge<T>>& operator[](const int& k) {\n assert(0 <= k and k < n);\n return g[k];\n }\n inline const vector<Edge<T>>& operator[](const int& k) const {\n assert(0 <= k and k < n);\n return g[k];\n }\n\n private:\n int n, es;\n vector<vector<Edge<T>>> g;\n};\ntemplate <typename T = int>\nusing Edges = vector<Edge<T>>;\ntemplate <typename T>\nvector<pair<T, int>> dijkstra(const Graph<T>& g, const int s = 0) {\n int n = g.size();\n assert(0 <= s and s < n);\n vector<pair<T, int>> d(n, {numeric_limits<T>::max(), -1});\n priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;\n d[s] = {0, -1};\n pq.push({0, s});\n while(!pq.empty()) {\n pair<T, int> p = pq.top();\n pq.pop();\n T dist = p.first;\n int cur = p.second;\n if(d[cur].first < dist) continue;\n for(const Edge<T>& edge : g[cur]) {\n if(d[edge.to].first > d[cur].first + edge.cost) {\n d[edge.to] = {d[cur].first + edge.cost, cur};\n pq.push({d[edge.to].first, edge.to});\n }\n }\n }\n return d;\n}\nint main(void) {\n while(1) {\n int n, m, l, k, a, h;\n cin >> n >> m >> l >> k >> a >> h;\n if(n == 0) break;\n Graph<int> g(n * (m + 1));\n rep(i, 0, l) {\n int x;\n cin >> x;\n rep(j, 0, m) {\n g.add_directed_edge(x * (m + 1) + j, x * (m + 1) + j + 1, 1);\n }\n }\n rep(i, 0, k) {\n int x, y, t;\n cin >> x >> y >> t;\n rep(j, 0, m + 1) {\n if(j - t < 0) continue;\n g.add_directed_edge(x * (m + 1) + j, y * (m + 1) + j - t, t);\n g.add_directed_edge(y * (m + 1) + j, x * (m + 1) + j - t, t);\n }\n }\n auto d = dijkstra(g, a * (m + 1) + m);\n int ans = 1e9;\n rep(i, 0, m + 1) {\n ans = min(ans, d[h * (m + 1) + i].first);\n }\n if(ans == 1e9) {\n cout << \"Help!\" << '\\n';\n } else {\n cout << ans << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 6500, "score_of_the_acc": -1.2567, "final_rank": 19 }, { "submission_id": "aoj_2021_8996546", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\ntuple<vl, vi> dijkstra(int s, int N, vv<pair<int, ll>> &g){\n using P = pair<ll, int>;\n vl dist(N, INF);\n vi bef(N);\n priority_queue<P, vc<P>, greater<P>> q;\n dist[s] = 0;\n q.push({0, s});\n while (!q.empty()){\n auto [c, now] = q.top();q.pop();\n if (dist[now] < c) continue;\n for (auto&& [nxt, nc]: g[now]) if (dist[nxt] > c + nc){\n dist[nxt] = c + nc;\n bef[nxt] = now;\n q.push({dist[nxt], nxt}); \n }\n }\n return {dist, bef};\n}\n\nll solve(int N, int M, int L, int K, int A, int H){\n vv<pair<int, ll>> g(N * (M + 1));\n rep(i, L){\n int v; cin >> v;\n rep(j, M) g[j * N + v].push_back({(j + 1) * N + v, 1});\n }\n rep(i, K){\n int x, y, t; cin >> x >> y >> t;\n rep(j, M + 1) if (j + t <= M){\n g[(j + t) * N + x].push_back({j * N + y, t});\n g[(j + t) * N + y].push_back({j * N + x, t});\n }\n }\n auto [dist, bef] = dijkstra(M * N + A, N * (M + 1), g);\n ll ans = INF;\n rep(j, M + 1) ans = min(dist[j * N + H], ans);\n return ans;\n}\n\nint main(){\n vc<ll> ans;\n while (true){\n int N, M, L, K, A, H; cin >> N >> M >> L >> K >> A >> H;\n if (N == 0) break;\n ans.push_back(solve(N, M, L, K, A, H));\n }\n for (auto x : ans){\n if (x == INF) cout << \"Help!\" << endl;\n else cout << x << endl;\n }\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 6724, "score_of_the_acc": -1.3814, "final_rank": 20 }, { "submission_id": "aoj_2021_8861238", "code_snippet": "#include <bits/stdc++.h>\n#define r(i, n) for (int i = 0; i < n; i++)\n#define INF 1e9\nusing namespace std;\n\nint main() {\n int n, m, l, k, a, h, t1, t2, t3;\n\n while (cin >> n >> m >> l >> k >> a >> h, n) {\n vector<bool> rei(n, false);\n rei[a] = rei[h] = true;\n int b[n][n], re, x[n][n];\n r(i, n) r(j, n) x[i][j] = b[i][j] = INF;\n r(i, l) cin >> re, rei[re] = true;\n r(i, k) cin >> t1 >> t2 >> t3, b[t1][t2] = b[t2][t1] = t3;\n r(o, n) r(i, n) r(j, n) b[i][j] = min(b[i][j], b[i][o] + b[o][j]);\n r(i, n) if (rei[i]) r(j, n) if (rei[j]) if (b[i][j] <= m)\n x[i][j] = b[i][j];\n r(o, n) if (rei[o]) r(i, n) if (rei[i]) r(j, n) if (rei[j]) \n x[i][j] = min(x[i][j], x[i][o] + x[o][j]);\n if (x[a][h] >= INF)\n cout << \"Help!\" << endl;\n else if (x[a][h] < m)\n cout << x[a][h] << endl;\n else\n cout << x[a][h] * 2 - m << endl;\n }\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 3164, "score_of_the_acc": -0.4536, "final_rank": 15 }, { "submission_id": "aoj_2021_8861232", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\nusing namespace std;\n\n#define r(i, n) for (int i = 0; i < n; i++)\n#define INF 1000000000\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n int n, m, l, k, a, h, t1, t2, t3;\n while (cin >> n >> m >> l >> k >> a >> h, n) {\n vector<int> rei(n, 0);\n rei[a] = rei[h] = 1;\n int b[n][n], re, x[n][n];\n r(i, n) r(j, n) x[i][j] = b[i][j] = INF;\n r(i, l) cin >> re, rei[re] = 1;\n r(i, k) cin >> t1 >> t2 >> t3, b[t1][t2] = b[t2][t1] = t3;\n r(o, n) r(i, n) r(j, n) b[i][j] = min(b[i][j], b[i][o] + b[o][j]);\n r(i, n) if (rei[i]) r(j, n) if (rei[j]) if (b[i][j] <= m)\n x[i][j] = b[i][j];\n r(o, n) r(i, n) r(j, n) x[i][j] = min(x[i][j], x[i][o] + x[o][j]);\n if (x[a][h] >= INF)\n cout << \"Help!\" << \"\\n\";\n else if (x[a][h] < m)\n cout << x[a][h] << \"\\n\";\n else\n cout << x[a][h] * 2 - m << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 3212, "score_of_the_acc": -1.0135, "final_rank": 18 }, { "submission_id": "aoj_2021_8215972", "code_snippet": "#include <bits/stdc++.h>\n#define r(i, n) for (int i = 0; i < n; i++)\n#define INF 1e9\nusing namespace std;\nint main() {\n int n, m, l, k, a, h, t1, t2, t3;\n while (cin >> n >> m >> l >> k >> a >> h, n) {\n set<int> q;\n q.insert(a), q.insert(h);\n int b[n][n], e, x[n][n];\n r(i, n) r(j, n) x[i][j] = b[i][j] = INF;\n r(i, l) cin >> e, q.insert(e);\n r(i, k) cin >> t1 >> t2 >> t3, b[t1][t2] = b[t2][t1] = t3;\n r(o, n) r(i, n) r(j, n) b[i][j] = min(b[i][j], b[i][o] + b[o][j]);\n r(i, n) if (q.count(i)) r(j, n) if (q.count(j)) if (b[i][j] <= m) x[i][j] =\n b[i][j];\n r(o, n) r(i, n) r(j, n) x[i][j] = min(x[i][j], x[i][o] + x[o][j]);\n if (x[a][h] >= INF)\n cout << \"Help!\" << endl;\n else if (x[a][h] < m)\n cout << x[a][h] << endl;\n else\n cout << x[a][h] * 2 - m << endl;\n }\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 3164, "score_of_the_acc": -0.5258, "final_rank": 16 } ]
aoj_2032_cpp
Problem C: Online Quizu System ICPC (Internet Contents Providing Company) is working on a killer game named Quiz Millionaire Attack. It is a quiz system played over the Internet. You are joining ICPC as an engineer, and you are responsible for designing a protocol between clients and the game server for this system. As bandwidth assigned for the server is quite limited, data size exchanged between clients and the server should be reduced as much as possible. In addition, all clients should be well synchronized during the quiz session for a simultaneous play. In particular, much attention should be paid to the delay on receiving packets. To verify that your protocol meets the above demands, you have decided to simulate the communication between clients and the server and calculate the data size exchanged during one game. A game has the following process. First, players participating and problems to be used are fixed. All players are using the same client program and already have the problem statements downloaded, so you don’t need to simulate this part. Then the game begins. The first problem is presented to the players, and the players answer it within a fixed amount of time. After that, the second problem is presented, and so forth. When all problems have been completed, the game ends. During each problem phase, the players are notified of what other players have done. Before you submit your answer, you can know who have already submitted their answers. After you have submitted your answer, you can know what answers are submitted by other players. When each problem phase starts, the server sends a synchronization packet for problem-start to all the players, and begins a polling process. Every 1,000 milliseconds after the beginning of polling, the server checks whether it received new answers from the players strictly before that moment, and if there are any, sends a notification to all the players: If a player hasn’t submitted an answer yet, the server sends it a notification packet type A describing others’ answers about the newly received answers. If a player is one of those who submitted the newly received answers, the server sends it a notification packet type B describing others’ answers about all the answers submitted by other players (i.e. excluding the player him/herself’s answer) strictly before that moment. If a player has already submitted an answer, the server sends it a notification packet type B describing others’ answers about the newly received answers. Note that, in all above cases, notification packets (both types A and B) must contain information about at least one player, and otherwise a notification packet will not be sent. When 20,000 milliseconds have passed after sending the synchronization packet for problem-start , the server sends notification packets of type A or B if needed, and then sends a synchronization packet for problem-end to all the players, to terminate the problem. On the other hand, players will ...(truncated)
[ { "submission_id": "aoj_2032_1354222", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_T = 20000;\nconst int INT_T = 1000;\n\nconst int MAX_M = 100;\nconst int MAX_N = 100;\n\nconst int PKT_HDR = 3;\n\n/* typedef */\n\nstruct Answer {\n int p, t, size;\n Answer() {}\n Answer(int _p, int _t, int _size) { p = _p, t = _t, size = _size; }\n bool operator<(const Answer& a0) const { return t < a0.t; }\n void print() { printf(\"Answer:p=%d,t=%d,size=%d\\n\", p, t, size); }\n};\n\ntypedef vector<Answer> va;\n\n/* global variables */\n\nint m, n, l;\nint ds[MAX_M];\nint ssent, srecv, csents[MAX_M], crecvs[MAX_M];\nva vans;\nint ansed[MAX_M], newansed[MAX_M];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (bool first = true;; first = false) {\n cin >> m >> n;\n if (m == 0) break;\n\n ssent = srecv = 0;\n memset(csents, 0, sizeof(csents));\n memset(crecvs, 0, sizeof(crecvs));\n\n for (int i = 0; i < m; i++)\n cin >> ds[i];\n\n while (n--) {\n vans.clear();\n \n cin >> l;\n for (int i = 0; i < l; i++) {\n\tint p, t;\n\tstring ans;\n\tcin >> p >> t >> ans;\n\tvans.push_back(Answer(p, t + 2 * ds[p], ans.length()));\n }\n sort(vans.begin(), vans.end());\n\n // problem-start\n for (int i = 0; i < m; i++) {\n\tssent += PKT_HDR;\n\tcrecvs[i] += PKT_HDR;\n }\n\n int ai = 0;\n int nansed = 0, sum = 0;\n memset(ansed, 0, sizeof(ansed));\n \n for (int t = 0; t <= MAX_T; t += INT_T) {\n\t//printf(\"t=%d\\n\", t);\n\tint nnewansed = 0, newsum = 0;\n\tmemset(newansed, 0, sizeof(newansed));\n\n\t// receive Answer\n\twhile (ai < l && vans[ai].t < t) {\n\t Answer& vai = vans[ai];\n\t //vai.print();\n\t \n\t int data = 2 + vai.size;\n\t csents[vai.p] += PKT_HDR + data;\n\t srecv += PKT_HDR + data;\n\t ansed[vai.p] = newansed[vai.p] = data;\n\t nansed++;\n\t nnewansed++;\n\t sum += data;\n\t newsum += data;\n\t ai++;\n\t}\n\n\t// notification\n\tif (nnewansed > 0) {\n\t for (int i = 0; i < m; i++) {\n\t if (ansed[i] == 0) {\t// type A\n\t int pkt = PKT_HDR + 1 + nnewansed;\n\t ssent += pkt;\n\t crecvs[i] += pkt;\n\t //printf(\" t=%d: type A->%d, %d byte\\n\", t, i, pkt);\n\t }\n\t else if (newansed[i] > 0 && nansed > 1) {\t// type B (exclude)\n\t int pkt = PKT_HDR + 1 + sum - newansed[i];\n\t ssent += pkt;\n\t crecvs[i] += pkt;\n\t //printf(\" t=%d: type B(excl)->%d, %d byte\\n\", t, i, pkt);\n\t }\n\t else if (newansed[i] == 0 && ansed[i] > 0) {\t// type B\n\t int pkt = PKT_HDR + 1 + newsum;\n\t ssent += pkt;\n\t crecvs[i] += pkt;\n\t //printf(\" t=%d: type B->%d, %d byte\\n\", t, i, pkt);\n\t }\n\t }\n\t}\n }\n\n // problem-end\n for (int i = 0; i < m; i++) {\n\tssent += PKT_HDR + 1;\n\tcrecvs[i] += PKT_HDR + 1;\n }\n }\n\n if (! first) cout << endl;\n \n printf(\"%d %d\\n\", ssent, srecv);\n for (int i = 0; i < m; i++)\n printf(\"%d %d\\n\", csents[i], crecvs[i]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1228, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_2032_313177", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\nusing namespace std;\n\nstruct Answer{\n int player;\n int time_sent;\n int time_rec;\n string answer;\n};\n\nint main()\n{\n int n, m;\n bool first = true;\n while( cin >> n >> m && n && m ){\n int delay[n];\n int server_sent = 0;\n int server_rec = 0;\n int sent[n];\n int rec[n];\n\n if( !first ){\n cout << endl;\n }else first = false;\n \n for(int i = 0; i < n; ++i){\n cin >> delay[i];\n sent[i] = 0;\n rec[i] = 0;\n }\n\n for(int prob = 0; prob < m; ++prob){\n int L;\n map<int,vector<Answer> > all_ans;\n\n // problem-start\n server_sent += 3*n;\n for(int i = 0; i < n; ++i){\n rec[i] += 3;\n }\n \n cin >> L;\n for(int i = 0; i < L; ++i){\n Answer ans;\n cin >> ans.player >> ans.time_sent >> ans.answer;\n //cout << ans.player << ' ' << ans.time_sent << ' ' << ans.answer << endl;\n ans.time_rec = ans.time_sent + 2*delay[ans.player];\n all_ans[ ans.time_rec ].push_back( ans );\n \n server_rec += 3+1+1+ans.answer.length();\n sent[ans.player] += 3+1+1+ans.answer.length();\n }\n\n int time = 1000;\n bool old_submit[n];\n bool new_submit[n];\n Answer submit_answer[n];\n vector<Answer> old_ans;\n vector<Answer> new_ans;\n \n for(int i = 0; i < n; ++i){\n old_submit[i] = false;\n new_submit[i] = false;\n }\n\n while(time <= 20000 ){\n //cout << \"TIME : <\" << time << endl;\n for(int i = 0; i < (int)new_ans.size(); ++i){\n old_ans.push_back( new_ans[i] );\n }\n for(int i = 0; i < n; ++i){\n //cout << sent[i] << ' ' << rec[i] << endl;\n if( new_submit[i] ){\n old_submit[i] = new_submit[i];\n }\n }\n //cout << endl;\n\n new_ans.clear();\n for(int i = 0; i < n; ++i){\n new_submit[i] = false;\n }\n \n //cout << endl;\n\n map<int,vector<Answer> >::iterator it = all_ans.lower_bound(time - 1000);\n for(;it!=all_ans.end()&&it->first<time;++it){\n // for newly recieved\n //cout << it->first <<endl;\n for(int i = 0; i < (int)it->second.size(); ++i){\n new_ans.push_back( it->second[i] );\n new_submit[ it->second[i].player ] = true;\n submit_answer[ it->second[i].player ] = it->second[i];\n }\n }\n\n\n for(int i = 0; i < n; ++i){\n //cout << old_submit[i] << ' ' << new_submit[i] << endl;\n }\n \n \n for(int i = 0; i < n; ++i){\n if( !old_submit[i] && !new_submit[i] ){\n // not yet submitted\n int sent_b = 3+1+new_ans.size();\n if( new_ans.size() > 0 ){\n server_sent += sent_b;\n rec[i] += sent_b;\n }\n //cout << new_ans.size() << ' ' << \"not yet \" << i << ' ' << sent_b << endl;\n }\n else if( old_submit[i] && !new_submit[i] ){\n // submitted but not newly\n int sent_b = 3+1;\n int cnt=0;\n for(int j = 0; j < (int)new_ans.size(); ++j){\n ++cnt;\n sent_b += 1+1+new_ans[j].answer.length();\n }\n if( cnt > 0 ){\n server_sent += sent_b;\n rec[i] += sent_b;\n }\n //cout << cnt << \" old submit \" << i << ' ' << sent_b << endl;\n }\n else if( new_submit[i] ){\n // submitted newly\n int sent_b = 3+1;\n int cnt = 0;\n for(int j = 0; j < (int)new_ans.size(); ++j){\n if( new_ans[j].player != i ){\n ++cnt;\n sent_b += 1+1+new_ans[j].answer.length();\n }\n }\n for(int j = 0; j < (int)old_ans.size(); ++j){\n if( old_ans[j].player != i ){\n ++cnt;\n sent_b += 1+1+old_ans[j].answer.length();\n }\n }\n \n if( cnt > 0 ){\n server_sent += sent_b;\n rec[i] += sent_b;\n }\n //cout << cnt << \" newly \" << i << ' ' << sent_b << endl;\n }\n }\n \n time += 1000;\n }\n // problem-end\n server_sent += 4*n;\n for(int i = 0; i < n; ++i){\n rec[i] += 4;\n }\n \n }\n\n\n cout << server_sent << ' ' << server_rec << endl;\n for(int i = 0; i < n; ++i){\n cout << sent[i] << ' ' << rec[i] << endl;\n }\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 952, "score_of_the_acc": -1.0921, "final_rank": 3 }, { "submission_id": "aoj_2032_136568", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstring>\n#include <queue>\nusing namespace std;\n\nclass Ans\n{\npublic:\n\tint p,t,l;\n\tAns(int p, int t, int l)\n\t\t:p(p),t(t),l(l)\n\t{}\n\n\tbool operator<(const Ans& ans) const\n\t{\n\t\treturn t>ans.t;\n\t}\n};\n\nint M,N,D[100];\nint ssend, srecv;\nint psend[100], precv[100];\n\nvoid calc(priority_queue<Ans>& q)\n{\n\tint tt=1,ansn=0,ansl=0,alansn=0, alansl=0;\n\tbool ansed[100]={0},nwansed[100]={0};\n\tint lg[100]={0};\n\t\n\tq.push(Ans(0,99999,0));\n\n\twhile(1)\n\t{\n\t\tAns a=q.top(); \n\t\tif(tt > 20) break;\n\t\t\n\t\tif(a.t < tt*1000) \n\t\t{\n\t\t\tansed[a.p]=1;\n\t\t\tnwansed[a.p]=1;\n\t\t\t\n\t\t\tansn++;\n\t\t\tansl+=a.l;\n\t\t\talansn++;\n\t\t\talansl+=a.l;\n\t\t\t\n\t\t\tlg[a.p]=a.l;\n\t\t\t\n\t\t\tq.pop();\n\t\t}\n\t\telse\n\t\t{\n\t\t\t++tt;\n\t\t\tif(ansn==0) continue;\n\t\t\t\n\t\t\tfor(int i=0; i<M; i++)\n\t\t\t{\n\t\t\t\tif(nwansed[i])\n\t\t\t\t{\n\t\t\t\t\tif(alansn>1)\n\t\t\t\t\t{\n\t\t\t\t\t\tssend+=4+2*(alansn-1)+(alansl-lg[i]);\n\t\t\t\t\t\tprecv[i]+=4+2*(alansn-1)+(alansl-lg[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(ansed[i])\n\t\t\t\t{\n\t\t\t\t\tssend+=4+2*ansn+ansl;\n\t\t\t\t\tprecv[i]+=4+2*ansn+ansl;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tssend+=4+ansn;\n\t\t\t\t\tprecv[i]+=4+ansn;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t\t\n\t\t\tansn=0;\n\t\t\tansl=0;\n\t\t\tmemset(lg,0,sizeof(lg));\n\t\t\tmemset(nwansed, 0, sizeof(nwansed));\n\t\t}\n\t}\n}\n\nint main()\n{\n\tbool b=0;\n\twhile(cin >> M >> N, (M||N))\n\t{\n\t\tif(b) cout << endl;\n\t\tb=1;\n\t\t\n\t\tfor(int i=0; i<M; i++)\n\t\t\tcin >> D[i];\n\n\t\tssend=0;\n\t\tsrecv=0;\n\t\tmemset(psend,0,sizeof(psend));\n\t\tmemset(precv,0,sizeof(precv));\n\n\t\tfor(int i=0; i<N; i++)\n\t\t{\n\t\t\tssend+=3*M;\n\t\t\tfor(int j=0; j<M; j++)\n\t\t\t\tprecv[j]+=3;\n\t\t\t\n\t\t\tint L;\n\t\t\tcin >> L;\n\t\t\tpriority_queue<Ans> q;\n\t\t\tfor(int j=0; j<L; j++)\n\t\t\t{\n\t\t\t\tint P,T;\n\t\t\t\tstring A;\n\t\t\t\tcin >> P >> T >> A;\n\t\t\t\tq.push(Ans(P,T+D[P]*2,A.size()));\n\n\t\t\t\tpsend[P]+=5+A.size();\n\t\t\t\tsrecv+=5+A.size();\n\t\t\t}\n\n\t\t\tcalc(q);\n\t\t\tssend+=4*M;\n\t\t\tfor(int j=0; j<M; j++)\n\t\t\t\tprecv[j]+=4;\n\t\t\t\n\t\t}\n\n\t\tcout << ssend << \" \" << srecv << endl;\n\t\tfor(int i=0; i<M; i++)\n\t\t\tcout << psend[i] << \" \" << precv[i] << endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 924, "score_of_the_acc": -0.6667, "final_rank": 1 } ]
aoj_2026_cpp
Problem D: Divisor is the Conqueror Divisor is the Conquerer is a solitaire card game. Although this simple game itself is a great way to pass one’s time, you, a programmer, always kill your time by programming. Your task is to write a computer program that automatically solves Divisor is the Conquerer games. Here is the rule of this game: First, you randomly draw N cards (1 ≤ N ≤ 52) from your deck of playing cards. The game is played only with those cards; the other cards will not be used. Then you repeatedly pick one card to play among those in your hand. You are allowed to choose any card in the initial turn. After that, you are only allowed to pick a card that conquers the cards you have already played. A card is said to conquer a set of cards when the rank of the card divides the sum of the ranks in the set. For example, the card 7 conquers the set {5, 11, 12}, while the card 11 does not conquer the set {5, 7, 12}. You win the game if you successfully play all N cards you have drawn. You lose if you fails, that is, you faces a situation in which no card in your hand conquers the set of cards you have played. Input The input consists of multiple test cases. Each test case consists of two lines. The first line contains an integer N (1 ≤ N ≤ 52), which represents the number of the cards. The second line contains N integers c 1 , . . . , c N (1 ≤ c i ≤ 13), each of which represents the rank of the card. You may assume that there are at most four cards with the same rank in one list. The input is terminated by a line with a single zero. Output For each test case, you should output one line. If there are any winning strategies for the cards of the input, print any one of them as a list of N integers separated by a space. If there is no winning strategy, print “No”. Sample Input 5 1 2 3 3 7 4 2 3 3 3 0 Output for the Sample Input 3 3 1 7 2 No
[ { "submission_id": "aoj_2026_10848967", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\nvector<int> cur;\n\nint cnt[14];\nbool rec(int n, int sum)\n{\n if(n == 0)\n return 1;\n\n for(int i = 1; i <= 13; i++){\n if(cnt[i] != 0 && sum % i == 0){\n cur.push_back(i);\n\n cnt[i]--;\n if(rec(n - 1, sum - i))\n return 1;\n\n cnt[i]++;\n cur.pop_back();\n }\n }\n return 0;\n}\n\nint main() {\n while (1) {\n int n;\n cin >> n;\n\n if (n == 0)\n exit(0);\n\n int sum = 0;\n\n for (int i = 0; i < 14; i++)\n cnt[i] = 0;\n\n for(int i = 0; i < n; i++){\n int x;\n cin >> x;\n\n cnt[x]++;\n\n sum += x;\n }\n\n cur.clear();\n if(rec(n, sum)) {\n for (int i = n - 1; i > 0; i--)\n cout << cur[i] << \" \";\n cout << cur[0] << endl;\n } else\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 3296, "score_of_the_acc": -0.8635, "final_rank": 9 }, { "submission_id": "aoj_2026_9099876", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) { return (ull)rng() % B; }\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) {\n vector<int> c(13);\n int sum = 0;\n for (int i = 0; i < n; i++) {\n int a;\n cin >> a;\n sum += a;\n c[a - 1] += 1;\n }\n vector<int> res;\n set<vector<int>> used;\n auto slv = [&](auto self, int rem) -> bool {\n if (rem == 0) return true;\n if (used.find(c) != used.end()) return false;\n used.insert(c);\n for (int i = 0; i < 13; i++) {\n if (c[i] and rem % (i + 1) == 0) {\n c[i] -= 1;\n res.push_back(i + 1);\n if (self(self, rem - (i + 1))) return true;\n res.pop_back();\n c[i] += 1;\n }\n }\n return false;\n };\n if (slv(slv, sum)) {\n reverse(res.begin(), res.end());\n for (int i = 0; i < res.size(); i++) {\n if (i) cout << \" \";\n cout << res[i];\n }\n cout << \"\\n\";\n } else {\n cout << \"No\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3980, "score_of_the_acc": -1.0024, "final_rank": 14 }, { "submission_id": "aoj_2026_5093274", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nusing pii = pair<int, int>;\nusing vvl = vector<vector<ll>>;\nusing vvi = vector<vector<int>>;\nusing vvpll = vector<vector<pll>>;\n#define rep(i, a, b) for (ll i=(a); i<(b); i++)\n#define rrep(i, a, b) for (ll i=(a); i>(b); i--)\n#define pb push_back\n#define tostr to_string\n#define ALL(A) A.begin(), A.end()\n// constexpr ll INF = LONG_LONG_MAX;\nconstexpr ll INF = 1e18;\nconstexpr ll MOD = 1000000007;\n\nconst string digits = \"0123456789\";\nconst string ascii_lowercase = \"abcdefghijklmnopqrstuvwxyz\";\nconst string ascii_uppercase = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string ascii_letters = ascii_lowercase + ascii_uppercase;\n\ntemplate<typename T> vector<vector<T>> list2d(int N, int M, T init) { return vector<vector<T>>(N, vector<T>(M, init)); }\ntemplate<typename T> vector<vector<vector<T>>> list3d(int N, int M, int L, T init) { return vector<vector<vector<T>>>(N, vector<vector<T>>(M, vector<T>(L, init))); }\ntemplate<typename T> vector<vector<vector<vector<T>>>> list4d(int N, int M, int L, int O, T init) { return vector<vector<vector<vector<T>>>>(N, vector<vector<vector<T>>>(M, vector<vector<T>>(L, vector<T>(O, init)))); }\n\nvoid print(ld out) { cout << fixed << setprecision(15) << out << '\\n'; }\nvoid print(double out) { cout << fixed << setprecision(15) << out << '\\n'; }\ntemplate<typename T> void print(T out) { cout << out << '\\n'; }\ntemplate<typename T1, typename T2> void print(pair<T1, T2> out) { cout << out.first << ' ' << out.second << '\\n'; }\ntemplate<typename T> void print(vector<T> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size()-1 ? '\\n' : ' '); } }\ntemplate<typename T> void print(set<T> S) { vector<T> A(S.begin(), S.end()); print(A); }\n\nvoid Yes() { print(\"Yes\"); }\nvoid No() { print(\"No\"); }\nvoid YES() { print(\"YES\"); }\nvoid NO() { print(\"NO\"); }\n\nll floor(ll a, ll b) { if (a < 0) { return (a-b+1) / b; } else { return a / b; } }\nll ceil(ll a, ll b) { if (a >= 0) { return (a+b-1) / b; } else { return a / b; } }\npll divmod(ll a, ll b) { ll d = a / b; ll m = a % b; return {d, m}; }\ntemplate<typename T> bool chmax(T &x, T y) { return (y > x) ? x = y, true : false; }\ntemplate<typename T> bool chmin(T &x, T y) { return (y < x) ? x = y, true : false; }\n\ntemplate<typename T> T sum(vector<T> A) { T res = 0; for (T a: A) res += a; return res; }\ntemplate<typename T> T max(vector<T> A) { return *max_element(ALL(A)); }\ntemplate<typename T> T min(vector<T> A) { return *min_element(ALL(A)); }\n\nll toint(string s) { ll res = 0; for (char c : s) { res *= 10; res += (c - '0'); } return res; }\nint toint(char num) { return num - '0'; }\nchar tochar(int num) { return '0' + num; }\nint ord(char c) { return (int)c; }\nchar chr(int a) { return (char)a; }\n\nll pow(int x, ll n) { ll res = 1; rep(_, 0, n) res *= x; return res; }\nll pow(ll x, ll n, int mod) { ll res = 1; while (n > 0) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; }\n\nint popcount(ll S) { return __builtin_popcountll(S); }\nll gcd(ll a, ll b) { return __gcd(a, b); }\n\ntemplate<typename T> int bisect_left(vector<T> &A, T val) { return lower_bound(ALL(A), val) - A.begin(); }\ntemplate<typename T> int bisect_right(vector<T> &A, T val) { return upper_bound(ALL(A), val) - A.begin(); }\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll N;\n cin >> N;\n if (!N) return 0;\n\n vector<ll> A(N);\n rep(i, 0, N) cin >> A[i];\n\n vector<ll> C(14);\n rep(i, 0, N) C[A[i]]++;\n vector<ll> ans;\n auto rec = [&](auto&& f, ll cnt, ll sm) {\n if (cnt == N) {\n return true;\n }\n rep(k, 1, 14) {\n if (C[k] and (sm-k) % k == 0) {\n C[k]--;\n if (f(f, cnt+1, sm-k)) {\n ans.pb(k);\n return true;\n }\n C[k]++;\n }\n }\n return false;\n };\n ll total = sum(A);\n rep(k, 1, 14) {\n if (C[k] and (total-k) % k == 0) {\n C[k]--;\n if (rec(rec, 1, total-k)) {\n ans.pb(k);\n break;\n }\n C[k]++;\n }\n }\n if (!ans.empty()) {\n print(ans);\n } else {\n No();\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 3168, "score_of_the_acc": -0.9193, "final_rank": 12 }, { "submission_id": "aoj_2026_5093264", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nusing pii = pair<int, int>;\nusing vvl = vector<vector<ll>>;\nusing vvi = vector<vector<int>>;\nusing vvpll = vector<vector<pll>>;\n#define rep(i, a, b) for (ll i=(a); i<(b); i++)\n#define rrep(i, a, b) for (ll i=(a); i>(b); i--)\n#define pb push_back\n#define tostr to_string\n#define ALL(A) A.begin(), A.end()\n// constexpr ll INF = LONG_LONG_MAX;\nconstexpr ll INF = 1e18;\nconstexpr ll MOD = 1000000007;\n\nconst string digits = \"0123456789\";\nconst string ascii_lowercase = \"abcdefghijklmnopqrstuvwxyz\";\nconst string ascii_uppercase = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string ascii_letters = ascii_lowercase + ascii_uppercase;\n\ntemplate<typename T> vector<vector<T>> list2d(int N, int M, T init) { return vector<vector<T>>(N, vector<T>(M, init)); }\ntemplate<typename T> vector<vector<vector<T>>> list3d(int N, int M, int L, T init) { return vector<vector<vector<T>>>(N, vector<vector<T>>(M, vector<T>(L, init))); }\ntemplate<typename T> vector<vector<vector<vector<T>>>> list4d(int N, int M, int L, int O, T init) { return vector<vector<vector<vector<T>>>>(N, vector<vector<vector<T>>>(M, vector<vector<T>>(L, vector<T>(O, init)))); }\n\nvoid print(ld out) { cout << fixed << setprecision(15) << out << '\\n'; }\nvoid print(double out) { cout << fixed << setprecision(15) << out << '\\n'; }\ntemplate<typename T> void print(T out) { cout << out << '\\n'; }\ntemplate<typename T1, typename T2> void print(pair<T1, T2> out) { cout << out.first << ' ' << out.second << '\\n'; }\ntemplate<typename T> void print(vector<T> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size()-1 ? '\\n' : ' '); } }\ntemplate<typename T> void print(set<T> S) { vector<T> A(S.begin(), S.end()); print(A); }\n\nvoid Yes() { print(\"Yes\"); }\nvoid No() { print(\"No\"); }\nvoid YES() { print(\"YES\"); }\nvoid NO() { print(\"NO\"); }\n\nll floor(ll a, ll b) { if (a < 0) { return (a-b+1) / b; } else { return a / b; } }\nll ceil(ll a, ll b) { if (a >= 0) { return (a+b-1) / b; } else { return a / b; } }\npll divmod(ll a, ll b) { ll d = a / b; ll m = a % b; return {d, m}; }\ntemplate<typename T> bool chmax(T &x, T y) { return (y > x) ? x = y, true : false; }\ntemplate<typename T> bool chmin(T &x, T y) { return (y < x) ? x = y, true : false; }\n\ntemplate<typename T> T sum(vector<T> A) { T res = 0; for (T a: A) res += a; return res; }\ntemplate<typename T> T max(vector<T> A) { return *max_element(ALL(A)); }\ntemplate<typename T> T min(vector<T> A) { return *min_element(ALL(A)); }\n\nll toint(string s) { ll res = 0; for (char c : s) { res *= 10; res += (c - '0'); } return res; }\nint toint(char num) { return num - '0'; }\nchar tochar(int num) { return '0' + num; }\nint ord(char c) { return (int)c; }\nchar chr(int a) { return (char)a; }\n\nll pow(int x, ll n) { ll res = 1; rep(_, 0, n) res *= x; return res; }\nll pow(ll x, ll n, int mod) { ll res = 1; while (n > 0) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; }\n\nint popcount(ll S) { return __builtin_popcountll(S); }\nll gcd(ll a, ll b) { return __gcd(a, b); }\n\ntemplate<typename T> int bisect_left(vector<T> &A, T val) { return lower_bound(ALL(A), val) - A.begin(); }\ntemplate<typename T> int bisect_right(vector<T> &A, T val) { return upper_bound(ALL(A), val) - A.begin(); }\n\ntemplate<typename T>\nunordered_map<T, ll> Counter(vector<T> &A) {\n unordered_map<T, ll> res;\n for (T a : A) {\n res[a]++;\n }\n return res;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll N;\n cin >> N;\n if (!N) return 0;\n\n vector<ll> A(N);\n rep(i, 0, N) cin >> A[i];\n\n auto C = Counter(A);\n vector<ll> ans;\n auto rec = [&](auto&& f, ll cnt, ll sm) {\n if (cnt == N) {\n return true;\n }\n rep(k, 1, 14) {\n if (C[k] and (sm-k) % k == 0) {\n C[k]--;\n if (f(f, cnt+1, sm-k)) {\n ans.pb(k);\n return true;\n }\n C[k]++;\n }\n }\n return false;\n };\n ll total = sum(A);\n rep(k, 1, 14) {\n if (C[k] and (total-k) % k == 0) {\n C[k]--;\n if (rec(rec, 1, total-k)) {\n ans.pb(k);\n break;\n }\n C[k]++;\n }\n }\n if (!ans.empty()) {\n print(ans);\n } else {\n No();\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3070, "memory_kb": 3488, "score_of_the_acc": -1.5629, "final_rank": 20 }, { "submission_id": "aoj_2026_3269166", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\n\nset<long long int>aset;\nlong long int ahash(vector<int>&v) {\n\tlong long int k=0;\n\tfor (auto a : v) {\n\t\tk*=5;\n\t\tk+=a;\n\t}\n\treturn k;\n}\nvector<int> solve(vector<int>&v,int rest) {\n\tif (rest == 0)return vector<int>{-1};\n\tauto ha=ahash(v);\n\tif (aset.find(ha) == aset.end()) {\n\t\t//aset.emplace(ha);\n\t}\n\telse {\n\t\treturn vector<int>();\n\t}\n\tfor (int i = 0; i < 13; ++i) {\n\t\tif (rest % (i + 1)==0 && v[i]) {\n\t\t\tv[i]--;\n\t\t\tauto k=solve(v, rest-i-1);\n\t\t\tif(!k.empty()){\n\t\t\t\tk.push_back(i+1);\n\t\t\t\treturn k;\n\t\t\t}\n\t\t\tv[i]++;\n\t\t}\n\t}\n\treturn vector<int>();\n}\n\nint main() {\n\twhile (true) {\n\t\taset.clear();\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\t\tint sum=0;\n\t\tvector<int>nums(13);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint a;cin>>a;a--;\n\t\t\tsum+=a+1;\n\t\t\tnums[a]++;\n\t\t}\n\t\tvector<int> oks=solve(nums,sum);\n\t\tif (!oks.empty()) {\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tcout<<oks[i+1];\n\t\t\t\tif(i==N-1)cout<<endl;\n\t\t\t\telse cout<<\" \";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcout<<\"No\"<<endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 980, "memory_kb": 3028, "score_of_the_acc": -0.8961, "final_rank": 10 }, { "submission_id": "aoj_2026_3269164", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\n\nset<long long int>aset;\nlong long int ahash(vector<int>&v) {\n\tlong long int k=0;\n\tfor (auto a : v) {\n\t\tk*=5;\n\t\tk+=a;\n\t}\n\treturn k;\n}\nvector<int> solve(vector<int>&v,int rest) {\n\tif (rest == 0)return vector<int>{-1};\n\tauto ha=ahash(v);\n\tif (aset.find(ha) == aset.end()) {\n\t\taset.emplace(ha);\n\t}\n\telse {\n\t\treturn vector<int>();\n\t}\n\tfor (int i = 0; i < 13; ++i) {\n\t\tif (rest % (i + 1)==0 && v[i]) {\n\t\t\tv[i]--;\n\t\t\tauto k=solve(v, rest-i-1);\n\t\t\tif(!k.empty()){\n\t\t\t\tk.push_back(i+1);\n\t\t\t\treturn k;\n\t\t\t}\n\t\t\tv[i]++;\n\t\t}\n\t}\n\treturn vector<int>();\n}\n\nint main() {\n\twhile (true) {\n\t\taset.clear();\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\t\tint sum=0;\n\t\tvector<int>nums(13);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint a;cin>>a;a--;\n\t\t\tsum+=a+1;\n\t\t\tnums[a]++;\n\t\t}\n\t\tvector<int> oks=solve(nums,sum);\n\t\tif (!oks.empty()) {\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tcout<<oks[i+1];\n\t\t\t\tif(i==N-1)cout<<endl;\n\t\t\t\telse cout<<\" \";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcout<<\"No\"<<endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3288, "score_of_the_acc": -0.7546, "final_rank": 6 }, { "submission_id": "aoj_2026_2554101", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\nint N;\nbool FLG;\n\nvoid recursive(int cards[14],int log[],int used_num,int sum){\n\n\tif(FLG)return;\n\n\tif(used_num == N){\n\n\t\tFLG = true;\n\n\t\treverse(log,log+N);\n\n\t\tprintf(\"%d\",log[0]);\n\t\tfor(int i = 1; i < N; i++){\n\t\t\tprintf(\" %d\",log[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\n\t\treturn;\n\t}\n\n\tfor(int i = 1; i <= 13; i++){\n\t\tif(cards[i] > 0 && (used_num == N-1 || (sum-i)%i == 0)){\n\t\t\tint next_cards[14],next_log[N];\n\t\t\tfor(int k = 1; k <= 13; k++){\n\t\t\t\tnext_cards[k] = cards[k];\n\t\t\t}\n\t\t\tnext_cards[i]--;\n\t\t\tfor(int k = 0; k < used_num; k++){\n\t\t\t\tnext_log[k] = log[k];\n\t\t\t}\n\t\t\tnext_log[used_num] = i;\n\n\t\t\trecursive(next_cards,next_log,used_num+1,sum-i);\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\tFLG = false;\n\n\tint first[14];\n\tfor(int i = 1; i <= 13; i++){\n\t\tfirst[i] = 0;\n\t}\n\n\tint tmp,sum = 0;\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d\",&tmp);\n\t\tfirst[tmp]++;\n\t\tsum += tmp;\n\t}\n\n\tint log[N];\n\n\trecursive(first,log,0,sum);\n\n\n\tif(!FLG){\n\t\tprintf(\"No\\n\");\n\t}\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 3184, "score_of_the_acc": -0.9153, "final_rank": 11 }, { "submission_id": "aoj_2026_2519719", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define r(i,n) for(int i=0;i<n;i++)\nint a[14],n,t;\nint dfs(int s,int x){\n if(x==s)return 1;\n r(i,14)if(a[i]&&(s-x-i)%i==0){\n a[i]--;\n if(dfs(s,x+i)){\n if(x+i!=s)cout<<' ';\n cout<<i;\n return 1;\n }\n a[i]++;\n }\n return 0;\n}\nmain(){\n while(cin>>n,n){\n int sum=0;\n r(i,14)a[i]=0;\n r(i,n){\n cin>>t;\n a[t]++;\n sum+=t;\n }\n if(!dfs(sum,0))cout<<\"No\";\n cout<<endl;\n }\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 2924, "score_of_the_acc": -0.7557, "final_rank": 7 }, { "submission_id": "aoj_2026_2280214", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nunordered_map<int,bool> mp;\nint n,a[14],ans[52],c;\n\nint cal(){\n int res=0;\n for(int i=1;i<=13;i++)res=res*5+a[i];\n return res;\n}\n\nbool dfs(int s,int d){\n if(!d)return 1;\n int y=cal();\n if(mp.count(y))return mp[y];\n for(int i=13;i>0;i--){\n if(a[i]&&(s-i)%i==0){\n ans[d-1]=i;\n a[i]--;\n if(dfs(s-i,d-1))return mp[y]=1;\n a[i]++;\n }\n }\n return 0;\n}\n\nint main(){\n while(cin>>n,n){\n memset(a,0,sizeof(a));\n mp.clear();\n c=0;\n for(int i=0,b;i<n;i++)scanf(\"%d\",&b),a[b]++,c+=b;\n if(dfs(c,n)){\n for(int i=0;i<n;i++){\n\tif(i)printf(\" \");\n\tprintf(\"%d\",ans[i]);\n }\n }else printf(\"No\");\n printf(\"\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2240, "memory_kb": 3136, "score_of_the_acc": -1.2381, "final_rank": 19 }, { "submission_id": "aoj_2026_2280211", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint a[14],b[55],n,f;\nvoid dfs(int s,int k){\n if(f)return;\n if(!s) {\n f=1;\n for(int i=n-1;i>=0;i--) {\n if(i<n-1)cout<<\" \";\n cout<<b[i];\n }\n cout<<endl;\n return;\n }\n for(int i=1;i<14;i++) {\n if(a[i]&&(s-i)%i==0) {\n a[i]--;\n b[k]=i;\n dfs(s-i,k+1);\n a[i]++;\n }\n }\n}\nint main() {\n while(cin>>n&&n){\n memset(a,0,sizeof(a));\n int s=0;\n for(int i=0,x;i<n;i++){\n cin>>x;\n a[x]++;\n s+=x;\n }\n f=0;\n dfs(s,0);\n if(!f)cout<<\"No\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3100, "score_of_the_acc": -0.7795, "final_rank": 8 }, { "submission_id": "aoj_2026_2280205", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nunordered_map<int,bool> mp;\nint n,a[14],ans[52],c;\n\nint cal(){\n int res=0;\n for(int i=1;i<13;i++)res=res*5+a[i];\n return res;\n}\n\nbool dfs(int s,int d){\n if(!d)return 1;\n int y=cal();\n if(mp.count(y))return mp[y];\n for(int i=13;i>0;i--){\n if(a[i]&&(s-i)%i==0){\n ans[d-1]=i;\n a[i]--;\n if(dfs(s-i,d-1))return mp[y]=1;\n a[i]++;\n }\n }\n return 0;\n}\n\nint main(){\n while(cin>>n,n){\n memset(a,0,sizeof(a));\n mp.clear();\n c=0;\n for(int i=0,b;i<n;i++)scanf(\"%d\",&b),a[b]++,c+=b;\n if(dfs(c,n)){\n for(int i=0;i<n;i++){\n\tif(i)printf(\" \");\n\tprintf(\"%d\",ans[i]);\n }\n }else printf(\"No\");\n printf(\"\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2150, "memory_kb": 3064, "score_of_the_acc": -1.1908, "final_rank": 17 }, { "submission_id": "aoj_2026_2280174", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nunordered_map<int,bool> mp;\nint n,a[14],ans[52],c;\n\nint cal(){\n int res=0;\n for(int i=1;i<13;i++)res=res*5+a[i];\n return res;\n}\n\nbool dfs(int s,int d){\n if(d==n)return 1;\n int y=cal();\n if(mp.count(y))return mp[y];\n for(int i=13;i>0;i--){\n if(a[i]&&(s-i)%i==0){\n ans[d]=i;\n a[i]--;\n if(dfs(s-i,d+1))return mp[y]=1;\n a[i]++;\n }\n }\n return 0;\n}\n\nint main(){\n while(cin>>n,n){\n memset(a,0,sizeof(a));\n mp.clear();\n c=0;\n for(int i=0,b;i<n;i++)scanf(\"%d\",&b),a[b]++,c+=b;\n if(dfs(c,0)){\n for(int i=n-1;i>=0;i--){\n\tif(i!=n-1)printf(\" \");\n\tprintf(\"%d\",ans[i]);\n }\n }else printf(\"No\");\n printf(\"\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2160, "memory_kb": 3160, "score_of_the_acc": -1.2273, "final_rank": 18 }, { "submission_id": "aoj_2026_2275938", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint N;\n\nint card[14];\nvector<int> res;\n\nbool isvalid( int n,int i){\n if( (n-i) % i == 0 ) return true;\n return false;\n}\n\nbool solve(int n,int cnt){\n if( cnt == 1 ) {\n res.emplace_back( n );\n return true;\n }\n for(int i=13;i>=1;i--){\n if( card[i] == 0 ) continue;\n if( !isvalid(n,i) ) continue;\n card[i]--;\n res.emplace_back( i );\n if( solve( n - i, cnt-1 ) ) return true;\n res.pop_back();\n card[i]++;\n }\n return false;\n}\n\nint main(){\n while( cin >> N && N){\n memset( card,0,sizeof(card) );\n res.clear();\n int sum = 0;\n for(int i=0;i<N;i++){\n int a; cin >> a;\n sum += a;\n card[a]++;\n } \n if( !solve(sum,N) )\n cout << \"No\" << endl;\n else {\n reverse( res.begin(), res.end() );\n for( int i=0;i<res.size();i++){\n if( i ) cout << \" \";\n cout << res[i];\n }\n cout << endl;\n } \n }\n}", "accuracy": 1, "time_ms": 1830, "memory_kb": 3032, "score_of_the_acc": -1.1024, "final_rank": 16 }, { "submission_id": "aoj_2026_1896215", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nvi out,in;\nint n;\nbool f(int a,int t){\n\tif(a==0){\n\t\trep(i,n){\n\t\t\tif(i)cout<<\" \";\n\t\t\tcout<<out[n-1-i];\n\t\t}\n\t\tcout<<endl;\n\t\treturn true;\n\t}\n\tloop(i,1,14)if(in[i]&&(a-i)%i==0){\n\t\tin[i]--;\n\t\tout[t]=i;\n\t\tif(f(a-i,t+1))return true;\n\t\tin[i]++;\n\t}\n\treturn false;\n}\nint main(){\n\twhile(cin>>n,n){\n\t\tout=vi(n);\n\t\tin=vi(14);\n\t\tint sum=0;\n\t\trep(i,n){\n\t\t\tint a;cin>>a;\n\t\t\tsum+=a;\n\t\t\tin[a]++;\n\t\t}\n\t\tif(!f(sum,0))cout<<\"No\"<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 1216, "score_of_the_acc": -0.1861, "final_rank": 5 }, { "submission_id": "aoj_2026_1882250", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n\nusing namespace std;\n\nconst int cards = 13;\n \nbool flg;\n \nvoid rec(vector<int> a,int sum,vector<int> &v){\n if(sum == 0){\n\tflg = true;\n\treturn;\n }\n for(int i = 1 ; i <= 13 ; i++){\n\tif(a[i] && sum % i == 0){\n\t v.push_back(i);\n\t a[i]--;\n\t rec(a,sum-i,v);\n\t if(flg) return;\n\t v.pop_back();\n\t a[i]++;\n\t}\n }\n}\n \nint main(){\n int n;\n \n while(cin >> n ,n){\n\tvector<int> a(cards+1,0),v;\n\tint m,sum = 0;\n \n\tfor(int i = 0 ; i < n ; i++){\n\t cin >> m;\n \n\t sum += m;\n\t a[m]++;\n\t}\n \n\tflg = false;\n\trec(a,sum,v);\n \n\tif(flg){\n\t int len = (int)v.size();\n\t reverse(v.begin(),v.end());\n\t for(int i = 0 ; i < len ; i++){\n\t\tif(!i) cout << v[i];\n\t\telse cout << \" \" << v[i];\n\t }\n\t cout << endl;\n\t}\n\telse cout << \"No\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1120, "memory_kb": 3056, "score_of_the_acc": -0.9398, "final_rank": 13 }, { "submission_id": "aoj_2026_1352780", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 52;\nconst int MAX_C = 13;\n\n/* typedef */\n\n/* global variables */\n\nint n;\nint cs[MAX_C + 1], ans[MAX_N];\n\n/* subroutines */\n\nbool rec(int k, int sum) {\n if (k == n - 1) {\n ans[k] = sum;\n return true;\n }\n \n for (int i = 1; i <= MAX_C; i++)\n if (cs[i] > 0 && sum % i == 0) {\n cs[i]--;\n ans[k] = i;\n if (rec(k + 1, sum - i)) return true;\n cs[i]++;\n }\n\n return false;\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n int sum = 0;\n memset(cs, 0, sizeof(cs));\n\n for (int i = 0; i < n; i++) {\n int ci;\n cin >> ci;\n cs[ci]++;\n sum += ci;\n }\n //cout << sum << endl;\n\n if (rec(0, sum)) {\n for (int i = n - 1; i >= 0; i--) {\n\tif (i < n - 1) cout << ' ';\n\tcout << ans[i];\n }\n cout << endl;\n }\n else\n cout << \"No\" << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 1160, "score_of_the_acc": -0.1157, "final_rank": 2 }, { "submission_id": "aoj_2026_1249734", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nstatic const int tx[] = {0,1,0,-1};\nstatic const int ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\nvector<char> answer;\n\nbool dfs(int sum,vector<char>& cards,vector<char>& order){\n if(sum == 0){\n reverse(order.begin(),order.end());\n answer = order;\n return true;\n }\n bool res = false;\n\n for(int i = 1; i <= 13; i++){\n if(cards[i] > 0 && (sum - i) % i == 0){\n cards[i]--;\n order.push_back(i);\n res |= dfs(sum - i,cards,order);\n if(res) return true;\n order.pop_back();\n cards[i]++;\n }\n }\n return res;\n}\n\nint main(){\n int num_of_cards;\n while(~scanf(\"%d\",&num_of_cards)){\n if(num_of_cards == 0) break;\n\n vector<char> cards(14);\n int total_rank = 0;\n for(int i = 0; i < num_of_cards; i++){\n int rank;\n scanf(\"%d\",&rank);\n cards[rank]++;\n total_rank += rank;\n }\n vector<char> order;\n bool isok = dfs(total_rank,cards,order);\n bool is_first = true;\n\n if(isok){\n for(int i = 0; i < answer.size(); i++){\n printf(\"%s%d\",is_first ? \"\" : \" \",answer[i]);\n is_first = false;\n }\n printf(\"\\n\");\n }\n else{\n printf(\"No\\n\");\n }\n }\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 1228, "score_of_the_acc": -0.1831, "final_rank": 4 }, { "submission_id": "aoj_2026_1175026", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\ninline ll encode(const vector<int> &a){\n ll res = 0;\n for(int i=13;i>=1;i--)res = res*5 + a[i];\n return res;\n}\n\nset<int> memo;\nvector<int> ans;\n\nbool rec(ll sum, vector<int> &rem){\n int a = encode(rem);\n if(a==0)return true;\n if(memo.count(a))return false;\n memo.insert(a);\n\n for(int i=13;i>=1;i--){\n if(rem[i] && sum%i==0){\n rem[i]--;\n ans.push_back(i);\n if(rec(sum - i, rem))return true;\n ans.pop_back();\n rem[i]++;\n }\n }\n return false;\n}\n\nint main(){\n int n;\n while(cin >> n, n){\n vector<int> num(14,0);\n int sum = 0;\n for(int i=0;i<n;i++){\n int tmp;\n cin >> tmp;\n num[tmp]++; sum+=tmp;\n }\n\n memo.clear();\n ans.clear();\n rec(sum,num);\n if((int)ans.size() == n){\n reverse(ans.begin(),ans.end());\n for(int i=0;i<n;i++){\n\tcout << ans[i] << (i==n-1?\"\\n\":\" \");\n }\n }else cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1564, "score_of_the_acc": -0.1433, "final_rank": 3 }, { "submission_id": "aoj_2026_1117470", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P) \n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\nvint res;\nint tot = 0;\nbool solved = false;\n\nvoid rec(vint num, int rem, vint vec) {\n //cout << num << \",,, \" << sum << \";;;; \" << vec << endl;\n \n if (solved) return;\n if (rem == 0) {\n res = vec;\n solved = true;\n return;\n }\n for (int d = 1; d <= 13; ++d) {\n if (num[d] == 0) continue;\n if ( (rem-d) % d != 0 ) continue;\n num[d]--;\n vint nvec = vec; nvec.PB(d);\n rec(num, rem-d, nvec);\n num[d]++;\n }\n}\n\nint main() {\n int n;\n while (cin >> n) {\n if (n == 0) break;\n vector<int> num(14, 0);\n tot = 0;\n for (int i = 0; i < n; ++i) {\n int c; cin >> c; num[c]++;\n tot += c;\n }\n res.clear();\n solved = false;\n vint kara;\n rec(num, tot, kara);\n reverse(ALL(res));\n \n if (res.size() == 0) puts(\"No\");\n else {\n for (int i = 0; i < res.size(); ++i) {\n cout << res[i];\n if (i != res.size()-1) cout << \" \";\n }\n cout << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4160, "memory_kb": 1244, "score_of_the_acc": -1.0298, "final_rank": 15 }, { "submission_id": "aoj_2026_1110493", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<set>\nusing namespace std;\nint pow5[14];\nset<int>S;\nint ans[60];\nint n;\nint solve(int a,int b,int c){\n\tif(a==n)return 1;\n\tif(S.count(b))return 0;\n\tS.insert(b);\n\tfor(int i=0;i<13;i++){\n\t\tif(b%pow5[i+1]/pow5[i]&&c%(i+1)==0){\n\t\t\tans[n-1-a]=i+1;\n\t\t\tif(solve(a+1,b-pow5[i],c-(i+1)))return 1;\n\t\t}\n\t}\n\treturn 0;\n}\nint main(){\n\tpow5[0]=1;\n\tfor(int i=1;i<14;i++)pow5[i]=pow5[i-1]*5;\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tn=a;\n\t\tint st=0;\n\t\tint sum=0;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tint p;scanf(\"%d\",&p);\n\t\t\tsum+=p;\n\t\t\tp--;\n\t\t\tst+=pow5[p];\n\t\t}\n\t\tS.clear();\n\t\tint res=solve(0,st,sum);\n\t\tif(res){\n\t\t\tfor(int i=0;i<a;i++){\n\t\t\t\tif(i)printf(\" \");\n\t\t\t\tprintf(\"%d\",ans[i]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\n\t\t}else printf(\"No\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1292, "score_of_the_acc": -0.0492, "final_rank": 1 } ]
aoj_2028_cpp
Problem G: Gather on the Clock There is a self-playing game called Gather on the Clock . At the beginning of a game, a number of cards are placed on a ring. Each card is labeled by a value. In each step of a game, you pick up any one of the cards on the ring and put it on the next one in clockwise order. You will gain the score by the difference of the two values. You should deal with the two cards as one card in the later steps. You repeat this step until only one card is left on the ring, and the score of the game is the sum of the gained scores. Your task is to write a program that calculates the maximum score for the given initial state, which specifies the values of cards and their placements on the ring. The figure shown below is an example, in which the initial state is illustrated on the left, and the subsequent states to the right. The picked cards are 1, 3 and 3, respectively, and the score of this game is 6. Figure 6: An illustrative play of Gather on the Clock Input The input consists of multiple test cases. The first line contains the number of cases. For each case, a line describing a state follows. The line begins with an integer n (2 ≤ n ≤ 100), the number of cards on the ring, and then n numbers describing the values of the cards follow. Note that the values are given in clockwise order. You can assume all these numbers are non-negative and do not exceed 100. They are separated by a single space character. Output For each case, output the maximum score in a line. Sample Input 2 4 0 1 2 3 6 1 8 0 3 5 9 Output for the Sample Input 6 34
[ { "submission_id": "aoj_2028_10853924", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nint Q, n;\nint main() {\n\tcin >> Q;\n\twhile (Q--) {\n\t\tcin >> n;\n\t\tvector<int> a(n);\n\t\tfor (int i = 0; i < n; i++) cin >> a[i];\n\t\tint ret = 0;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tvector<vector<int> > dp(n, vector<int>(n + 1));\n\t\t\tfor (int j = 2; j <= n; j++) {\n\t\t\t\tfor (int k = 0; k <= n - j; k++) {\n\t\t\t\t\tint r = k + j;\n\t\t\t\t\tfor (int l = k + 1; l < r; l++) {\n\t\t\t\t\t\tdp[k][r] = max(dp[k][r], dp[k][l] + dp[l][r] + abs(a[k] - a[l]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tret = max(ret, dp[0][n]);\n\t\t\tvector<int> b(n);\n\t\t\tfor (int j = 0; j < n; j++) b[j] = a[(j + 1) % n];\n\t\t\ta = b;\n\t\t}\n\t\tcout << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 3224, "score_of_the_acc": -1.1694, "final_rank": 19 }, { "submission_id": "aoj_2028_3269194", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nstring root_sts = \"C ,C#,D ,D#,E ,F ,F#,G ,G#,A ,A#,B \";\n\nint solve(int l, int r, const vector<int>&nums, vector<vector<int>>&dp) {\n\tif (dp[l][r] != -1) {\n\t}\n\telse {\n\t\tif(l==r)dp[l][r]=0;\n\t\telse if(l+1==r)dp[l][r]=0;\n\t\telse {\n\t\t\tvector<int>memo(r-l);\n\t\t\tmemo[0]=0;\n\t\t\tfor (int x = 0; x < r - l-1; ++x) {\n\t\t\t\tfor (int num = 1; num <= r-l-x-1; ++num) {\n\t\t\t\t\tint plus=abs(nums[l]-nums[l+x+1])+\n\t\t\t\t\t\tsolve(l+x+1,l+x+num+1,nums,dp);\n\t\t\t\t\tmemo[x+num]=max(memo[x+num],\n\t\t\t\t\t\tmemo[x]+plus);\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp[l][r]=memo[r-l-1];\n\t\t}\n\t}\n\treturn dp[l][r];\n}\n\nint main() {\n\tint Q;cin>>Q;\n\twhile (Q--) {\n\t\tint N;cin>>N;\n\t\tvector<int>nums(N);\n\t\tfor(int i=0;i<N;++i)cin>>nums[i];\n\t\tnums.insert(nums.end(),nums.begin(),nums.end());\n\t\tvector<vector<int>>dp(2*N,vector<int>(2*N,-1));\n\t\tint ans=0;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tans=max(ans,solve(i,i+N,nums,dp));\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7480, "memory_kb": 3196, "score_of_the_acc": -1.9865, "final_rank": 20 }, { "submission_id": "aoj_2028_2552522", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nvoid func(){\n\n\tint N;\n\tscanf(\"%d\",&N);\n\n\tint table[N];\n\tfor(int i = 0; i < N; i++)scanf(\"%d\",&table[i]);\n\n\tint dp[N][N];\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tdp[i][k] = 0;\n\t\t}\n\t}\n\n\tfor(int length = 1; length <= N-1; length++){\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tfor(int k = 0; k < length; k++){\n\t\t\t\tdp[i][length] = max(dp[i][length],dp[i][k]+dp[(i+k+1)%N][length-k-1]+abs(table[i]-table[(i+k+1)%N]));\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans = 0;\n\tfor(int i = 0; i < N; i++){\n\t\tans = max(ans,dp[i][N-1]);\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n}\n\nint main(){\n\tint num_case;\n\tscanf(\"%d\",&num_case);\n\n\tfor(int i = 0; i < num_case; i++)func();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3172, "score_of_the_acc": -0.9749, "final_rank": 16 }, { "submission_id": "aoj_2028_2389001", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nbool used[111][111];\nint dp[111][111];\nint n;\nint a[111];\nint dfs(int l,int r){\n if(used[l][r]) return dp[l][r];\n if(l+1==r) return 0;\n used[l][r]=1;\n int res=0;\n for(int i=1;(l+i)%n!=r;i++){\n int m=(l+i)%n;\n res=max(res,dfs(l,m)+dfs(m,r)+abs(a[l]-a[m]));\n }\n //cout<<l<<\" \"<<r<<\":\"<<res<<endl;\n return dp[l][r]=res;\n}\nsigned main(){\n int T;\n cin>>T;\n while(T--){\n cin>>n;\n for(int i=0;i<n;i++) cin>>a[i];\n memset(used,0,sizeof(used));\n memset(dp,0,sizeof(dp));\n int ans=0;\n for(int i=0;i<n;i++) ans=max(ans,dfs(i,i));\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 3208, "score_of_the_acc": -1.0414, "final_rank": 18 }, { "submission_id": "aoj_2028_2388290", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nint dp[101][101],a[100];\n\n\nint main(){\n int T;\n cin>>T;\n while(T--){\n int n;\n cin>>n;\n for(int i=0;i<n;i++)\n cin>>a[i];\n\n memset(dp,0,sizeof(dp));\n int ans=0;\n for(int i=1;i<n;i++){\n for(int j=0;j<n;j++){\n\tfor(int k=j;k<j+i;k++){\n\t ans=max(ans,dp[j][(j+i)%n]=max(dp[j][(j+i)%n],dp[j][k%n]+dp[(k+1)%n][(j+i)%n]+abs(a[j]-a[(k+1)%n])));\n\t}\n }\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3120, "score_of_the_acc": -0.9593, "final_rank": 15 }, { "submission_id": "aoj_2028_1929193", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\nint n, a[107], dp[107][107], K[107][107];\nint main() {\n\tint t; cin >> t;\n\tfor (int p = 0; p < t; p++) {\n\t\tfor (int i = 0; i < 11449; i++)dp[i / 107][i % 107] = 0;\n\t\tcin >> n; if (n == 0)break;\n\t\tfor (int i = 0; i < n; i++)cin >> a[i];\n\t\tint maxn = 0;\n\t\tfor (int u = 0; u < n; u++) {\n\t\t\tint G = a[0];\n\t\t\tfor (int v = 1; v < n; v++)a[v - 1] = a[v];\n\t\t\ta[n - 1] = G;\n\t\t\tfor (int v = 0; v < 11449; v++)dp[v / 107][v % 107] = 0;\n\t\t\tfor (int v = 0; v < n; v++) {\n\t\t\t\tfor (int w = 0; w < n; w++)K[v][w] = abs(a[v] - a[w]);\n\t\t\t}\n\t\t\tfor (int v = 1; v <= n; v++) {\n\t\t\t\tfor (int w = 0; w < n; w++) {\n\t\t\t\t\tint I = w, J = v + w;\n\t\t\t\t\tif (I >= J || J >= n)continue;\n\t\t\t\t\tfor (int k = I; k < J; k++) {\n\t\t\t\t\t\tdp[I][J] = max(dp[I][J], dp[I][k] + dp[k + 1][J] + K[I][k + 1]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tmaxn = max(maxn, dp[0][n - 1]);\n\t\t}\n\t\tcout << maxn << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2790, "memory_kb": 1248, "score_of_the_acc": -0.4038, "final_rank": 13 }, { "submission_id": "aoj_2028_1882374", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX_N 100\n \nint N,arr[MAX_N];\nint dp[MAX_N][MAX_N];\n \nint rec(int L,int R){\n if(L == R) return dp[L][R] = 0;\n if(dp[L][R] >= 0) return dp[L][R];\n int res = 0;\n int i = L;\n while(i != R){\n\tint j = i%N,k = (i+1)%N;\n\tres = max(res,rec(L,j)+rec(k,R)+abs(arr[L]-arr[k]));\n\ti++; i %= N;\n }\n return dp[L][R] = res;\n}\n \nint main(){\n int Tc;\n cin >> Tc;\n while(Tc--){\n\tcin >> N;\n\tfor(int i = 0 ; i < N ; i++){\n\t cin >> arr[i];\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tint res = 0;\n\tfor(int i = 0 ; i < N ; i++){\n\t int j = (i+N-1)%N;\n\t res = max(res,rec(i,j));\n\t}\n\tcout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3100, "score_of_the_acc": -0.981, "final_rank": 17 }, { "submission_id": "aoj_2028_1616107", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <iostream>\n#include <cstdio>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <complex>\n#include <assert.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair <int,int> P;\n\nstatic const double EPS = 1e-12;\n\nstatic const int tx[] = {+0,+1,+0,-1};\nstatic const int ty[] = {-1,+0,+1,+0};\n\nint dp[101][101];\n\nint main(){\n int num_of_tests;\n while(~scanf(\"%d\",&num_of_tests)){\n for(int test_i = 0; test_i < num_of_tests; test_i++){\n int num_of_cards;\n scanf(\"%d\",&num_of_cards);\n vector<int> cards;\n for(int card_i = 0; card_i < num_of_cards; card_i++){\n int card;\n scanf(\"%d\",&card);\n cards.push_back(card);\n }\n\n memset(dp,0,sizeof(dp));\n for(int len = 1; len <= cards.size(); len++){\n for(int pos = 0; pos < cards.size(); pos++){\n for(int mid = 0; mid < len; mid++){\n dp[pos][len] = max(dp[pos][len],\n dp[pos][mid]\n + dp[(pos + mid) % cards.size()][len - mid]\n + abs(cards[pos] - cards[(pos + mid) % cards.size()]));\n }\n }\n }\n\n int res = 0;\n for(int pos = 0; pos < cards.size(); pos++){\n res = max(dp[pos][cards.size()],res);\n }\n printf(\"%d\\n\",res);\n }\n }\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 1256, "score_of_the_acc": -0.1153, "final_rank": 8 }, { "submission_id": "aoj_2028_1336940", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX_N 100\n\nint N,arr[MAX_N];\nint dp[MAX_N][MAX_N];\n\nint rec(int L,int R){\n if(L == R){ return dp[L][R] = 0; }\n if(dp[L][R] >= 0){ return dp[L][R]; }\n int res = 0;\n int i = L;\n while(i != R){\n int j = i%N,k = (i+1)%N;\n res = max(res,rec(L,j)+rec(k,R)+abs(arr[L]-arr[k]));\n i++; i %= N;\n }\n return dp[L][R] = res;\n}\n\nint main(){\n int Tc;\n cin >> Tc;\n while(Tc--){\n cin >> N;\n for(int i = 0 ; i < N ; i++){\n cin >> arr[i];\n }\n memset(dp,-1,sizeof(dp));\n int res = 0;\n for(int i = 0 ; i < N ; i++){\n int j = (i+N-1)%N;\n res = max(res,rec(i,j));\n }\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 1212, "score_of_the_acc": -0.1022, "final_rank": 7 }, { "submission_id": "aoj_2028_1150523", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint b[110];\nint dp[110][110];\nint tmp[110][110];\nint n;\ninline int ABS(int a){return max(a,-a);}\nint ad[110][110];\nint solve(int l,int r){\n\tif(~dp[l][r])return dp[l][r];\n\tif(l==r)return dp[l][r]=0;\n\tint sz=(r-l+n)%n;\n\tfor(int i=0;i<=sz;i++){\n\t\ttmp[l][i]=-99999999;\n\t}\n\ttmp[l][0]=0;\n\tfor(int i=0;i<sz;i++){\n\t\tdp[l][ad[l][i]]=tmp[l][i];\n\t\tint t=ABS(b[l]-b[ad[l+1][i]]);\n\t\tfor(int j=sz-i;j>=1;j--){\n\t\t\ttmp[l][i+j]=max(tmp[l][i+j],tmp[l][i]+t+solve(ad[l][i+1]%n,ad[l][i+j]));\n\t\t}\n\t}\n\t//printf(\"%d %d: %d\\n\",l,r,tmp[(r-l+n)%n]);\n\treturn dp[l][r]=tmp[l][sz];\n}\nint main(){\n\tint t;scanf(\"%d\",&t);\n\twhile(t--){\n\t\tint a;scanf(\"%d\",&a);for(int i=0;i<a;i++)scanf(\"%d\",b+i);\n\t\tn=a;\n\t\tfor(int i=0;i<110;i++)for(int j=0;j<110;j++)ad[i][j]=(i+j)%n;\n\t\tint ret=0;\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<a;j++)dp[i][j]=-1;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tret=max(ret,solve(i,(i+a-1)%a));\n\t\t}\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 6670, "memory_kb": 1156, "score_of_the_acc": -0.8893, "final_rank": 14 }, { "submission_id": "aoj_2028_1125450", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nint main() {\n int T;\n cin >> T;\n while(T--) {\n int n;\n cin >> n;\n int a[n];\n for(int i=0; i<n; i++) cin >> a[i];\n int dp[n][n];\n memset(dp,0,sizeof(dp));\n for(int i=0; i<n; i++) dp[i][(i+1)%n]=abs(a[i]-a[(i+1)%n]);\n int ans=0;\n for(int i=2; i<n; i++) {\n for(int j=0; j<n; j++) {\n for(int k=j; k<i+j+1; k++) {\n int x=(i+j)%n;\n if(k==j) dp[j][x]=max(dp[j][x],dp[j][(i+j-1)%n]+abs(a[j]-a[x]));\n else dp[j][x]=max(dp[j][x],dp[j][k%n]+dp[k%n][x]);\n ans=max(ans,dp[j][x]);\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 1204, "score_of_the_acc": -0.0273, "final_rank": 1 }, { "submission_id": "aoj_2028_1088990", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\nconst int INF = 1000000000;\n\nint main(){\n int T;\n cin >> T;\n while(T--) {\n int n;\n cin >> n;\n vector<int> a(n);\n REP(i, n) cin >> a[i];\n\n int dp[101][101] = {};\n REP(i, n) REP(j, n) dp[i][j] = -INF;\n REP(i, n) dp[i][i] = 0;\n for(int l = 1; l < n; l++) {\n for(int i = 0; i < n; i++) {\n int j = (i + l) % n;\n int max_k = 0;\n for(int h = 0; h < l; h++) {\n int k = (i + h) % n;\n int k2 = (i + h + 1) % n;\n int val = dp[i][k] + dp[k2][j] + abs(a[i] - a[k2]);\n if(dp[i][j] < val) {\n max_k = k;\n dp[i][j] = val;\n }\n }\n //printf(\"dp[%d][%d] = %d (dp[%d][%d] + dp[%d][%d])\\n\", i, j, dp[i][j], i, max_k, (max_k + 1) % n, j);\n }\n }\n\n int ans = -INF;\n REP(i, n) ans = max(ans, dp[i][(i + n - 1) % n]);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 1240, "score_of_the_acc": -0.0556, "final_rank": 5 }, { "submission_id": "aoj_2028_1033447", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint mem[200][200];\nbool p[200][200];\nint a[200];\n\nint rec(int f,int l){\n if(l-f==1)return 0;\n if(p[l][f]++)return mem[l][f];\n mem[l][f]=0;\n for(int m=f+1;m<l;m++){\n mem[l][f]=max(mem[l][f],rec(f,m)+rec(m,l)+abs(a[f]-a[m]));\n }\n return mem[l][f];\n}\n\nint main(){\n int T;\n cin>>T;\n while(T--){\n int n;\n cin>>n;\n for(int i=0;i<n;i++){\n cin>>a[i];\n a[i+n]=a[i];\n }\n int ans=0;\n fill(p[0],p[200],0);\n for(int i=0;i<n;i++){\n ans=max(ans,rec(i,i+n));\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 1360, "score_of_the_acc": -0.1369, "final_rank": 9 }, { "submission_id": "aoj_2028_621764", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n\n#define curr(P, i) P[(i) % P.size()]\n#define next(P, i) P[(i+1) % P.size()]\n#define prev(P, i) P[(i+P.size()-1) % P.size()]\n#define Next(a,i) ((i)+1)%(a).size()\n#define Curr(a,i) (i)%(a).size()\n#define Prev(a, i) (i+a.size()-1) % a.size()\n\nusing namespace std;\n\nint main(void){\n\n int n,m,x,dp[101][101];\n vector<int>a;\n\n cin >> n;\n\n while(n--){\n a.clear();\n\n for(int i=0;i<101;i++)\n for(int j=0;j<101;j++)dp[i][j]=0;\n\n cin >> m;\n for(int i=0;i<m;i++){\n cin >> x;\n a.push_back(x);\n }\n \n for(int i=0;i<m;i++){\n for(int j=0;j<m;j++){\n\tfor(int k=0;k<i;k++){\n\t dp[Curr(a,j)][Curr(a,i)]=max(dp[j][i],dp[Curr(a,j)][Curr(a,k)]+dp[Next(a,j+k)][Prev(a,i-k)]+abs(curr(a,j)-next(a,j+k))); \n\t}\n }\n }\n int ans=0;\n for(int i=0;i<m;i++)\n for(int j=0;j<m;j++)\n ans=max(ans,dp[i][j]);\n \n cout << ans << endl;\n \n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 1256, "score_of_the_acc": -0.1522, "final_rank": 12 }, { "submission_id": "aoj_2028_621762", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n\n#define curr(P, i) P[(i) % P.size()]\n#define next(P, i) P[(i+1) % P.size()]\n#define prev(P, i) P[(i+P.size()-1) % P.size()]\n#define Next(a,i) ((i)+1)%(a).size()\n#define Curr(a,i) (i)%(a).size()\n#define Prev(a, i) (i+a.size()-1) % a.size()\n\nusing namespace std;\n\nint main(void){\n\n int n,m,x,dp[101][101];\n vector<int>a;\n\n cin >> n;\n\n while(n--){\n a.clear();\n\n for(int i=0;i<101;i++)\n for(int j=0;j<101;j++)dp[i][j]=0;\n\n cin >> m;\n for(int i=0;i<m;i++){\n cin >> x;\n a.push_back(x);\n }\n \n for(int i=1;i<m;i++){\n for(int j=0;j<m;j++){\n\tfor(int k=0;k<i;k++){\n\t dp[Curr(a,j)][Curr(a,i)]=max(dp[j][i],dp[Curr(a,j)][Curr(a,k)]+dp[Next(a,j+k)][Prev(a,i-k)]+abs(curr(a,j)-next(a,j+k))); \n\t}\n }\n }\n int ans=0;\n for(int i=0;i<m;i++)\n for(int j=0;j<m;j++)\n ans=max(ans,dp[i][j]);\n \n cout << ans << endl;\n \n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 1252, "score_of_the_acc": -0.1516, "final_rank": 10 }, { "submission_id": "aoj_2028_620447", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n\n#define curr(P, i) P[(i) % P.size()]\n#define next(P, i) P[(i+1) % P.size()]\n#define prev(P, i) P[(i+P.size()-1) % P.size()]\n#define Next(a,i) ((i)+1)%(a).size()\n#define Curr(a,i) (i)%(a).size()\n#define Prev(a, i) (i+a.size()-1) % a.size()\n\nusing namespace std;\n\nint main(void){\n\n int n,m,x,dp[101][101];\n vector<int>a;\n\n cin >> n;\n\n while(n--){\n a.clear();\n\n for(int i=0;i<101;i++)\n for(int j=0;j<101;j++)dp[i][j]=0;\n\n cin >> m;\n for(int i=0;i<m;i++){\n cin >> x;\n a.push_back(x);\n }\n\n //for(int i=0;i<m;i++)dp[i][Next(a,i)]=abs(curr(a,i)-next(a,i));\n \n for(int k=1;k<m;k++){\n for(int i=0;i<m;i++){\n\tfor(int j=0;j<k;j++){\n\t dp[Curr(a,i)][Curr(a,k)]=max(dp[i][k],dp[Curr(a,i)][Curr(a,j)]+dp[Next(a,i+j)][Prev(a,k-j)]+abs(curr(a,i)-next(a,i+j)));\n\t\t\t\t\t\t \n\t}\n\t\n }\n }\n int ans=0;\n for(int i=0;i<m;i++)\n for(int j=0;j<m;j++)\n ans=max(ans,dp[i][j]);\n \n cout << ans << endl;\n \n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 1252, "score_of_the_acc": -0.1516, "final_rank": 10 }, { "submission_id": "aoj_2028_619605", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<cstdio>\nusing namespace std;\n\nint main()\n{\n int N;\n scanf(\"%d\",&N);\n while(N-- > 0)\n {\n int n;\n cin >> n;\n int dp[n][n];\n int vec[n];\n for(int i=0;i<n;i++)\n\t{\n\t cin >> vec[i];\n\t for(int j=0;j<n;j++)\n\t dp[i][j] = 0;\n\t}\n \n int mex = 0;\n for(int i=0;i<n;i++)\n\tdp[i][(i+1)%n] = abs(vec[i]-vec[(i+1)%n]),mex = max(mex,dp[i][(i+1)%n]);\n \n for(int i=2;i<n;i++)//幅\n\t{\n\t for(int j=0;j<n;j++)//index\n\t {\n\t for(int k=j;k<=j+i-1;k++)//中間地点\n\t\t{\n\t\t if(k == j)\n\t\t {\n\t\t dp[j][(j+i)%n] = max(dp[j][(j+i)%n],dp[j][(j+i-1)%n]+abs(vec[j]-vec[(j+i)%n]));\n\t\t mex = max(mex,dp[j][(j+i)%n]);\n\t\t continue;\n\t\t }\n\t\t dp[j][(j+i)%n] = max(dp[j][(j+i)%n],\n\t\t\t\t dp[k%n][(j+i)%n]+dp[j][k%n]);\n\t\t}\n\t mex = max(mex,dp[j][(j+i)%n]);\n\t }\n\t}\n\n cout << mex << endl;\n\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 1276, "score_of_the_acc": -0.0594, "final_rank": 6 }, { "submission_id": "aoj_2028_619600", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\nusing namespace std;\n\nint main()\n{\n int N;\n cin >> N;\n while(N-- > 0)\n {\n int n;\n cin >> n;\n int dp[n][n];\n vector<int> vec(n);\n for(int i=0;i<n;i++)\n\t{\n\t cin >> vec[i];\n\t for(int j=0;j<n;j++)\n\t dp[i][j] = 0;\n\t}\n\n int mex = 0;\n for(int i=0;i<n;i++)\n\tdp[i][(i+1)%n] = abs(vec[i]-vec[(i+1)%n]),mex = max(mex,dp[i][(i+1)%n]);\n\n for(int i=2;i<n;i++)//幅\n\t{\n\t for(int j=0;j<n;j++)//index\n\t {\n\t for(int k=j;k<=j+i-1;k++)//中間地点\n\t\t{\n\t\t if(k == j)\n\t\t {\n\t\t dp[j][(j+i)%n] = max(dp[j][(j+i)%n],dp[j][(j+i-1)%n]+abs(vec[j]-vec[(j+i)%n]));\n\t\t mex = max(mex,dp[j][(j+i)%n]);\n\t\t continue;\n\t\t }\n\t\t dp[j][(j+i)%n] = max(dp[j][(j+i)%n],\n\t\t\t\t dp[k%n][(j+i)%n]+dp[j][k%n]);\n\t\t}\n\t mex = max(mex,dp[j][(j+i)%n]);\n\t }\n\t}\n\n cout << mex << endl;\n\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 1232, "score_of_the_acc": -0.0381, "final_rank": 2 }, { "submission_id": "aoj_2028_618962", "code_snippet": "#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nmain(){\n int T, dp[100][100], n, d[100], s;\n cin >> T;\n while(T--){\n cin >> n;\n for(int i=0;i<n;i++) cin >> d[i];\n fill(dp[0], dp[n], 0);\n for(int k=1;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<k;j++){\n s = (i+k)%n;\n dp[i][s] = max(dp[i][s], dp[i][(i+j)%n] + dp[(i+j+1)%n][s] + abs(d[i] - d[(i+j+1)%n]));\n }\n }\n }\n int ans = 0;\n for(int i=0;i<n;i++) ans = max(ans, dp[i][(i+n-1)%n]);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 1196, "score_of_the_acc": -0.0439, "final_rank": 4 }, { "submission_id": "aoj_2028_618961", "code_snippet": "#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nmain(){\n int T, dp[100][100], n, d[100], s, t;\n cin >> T;\n while(T--){\n cin >> n;\n for(int i=0;i<n;i++) cin >> d[i];\n fill(dp[0], dp[n], 0);\n for(int k=1;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<k;j++){\n s = (i+k)%n;\n t = (i+j+1)%n;\n dp[i][s] = max(dp[i][s], dp[i][(i+j)%n] + dp[t][s] + abs(d[i] - d[t]));\n }\n }\n }\n int ans = 0;\n for(int i=0;i<n;i++) ans = max(ans, dp[i][(i+n-1)%n]);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 1196, "score_of_the_acc": -0.0426, "final_rank": 3 } ]
aoj_2031_cpp
Problem B: Hyper Rock-Scissors-Paper Rock-Scissors-Paper is a game played with hands and often used for random choice of a person for some purpose. Today, we have got an extended version, namely, Hyper Rock-Scissors-Paper (or Hyper RSP for short). In a game of Hyper RSP, the players simultaneously presents their hands forming any one of the following 15 gestures: Rock, Fire, Scissors, Snake, Human, Tree, Wolf, Sponge, Paper, Air, Water, Dragon, Devil, Lightning, and Gun. Figure 1: Hyper Rock-Scissors-Paper The arrows in the figure above show the defeating relation. For example, Rock defeats Fire, Scissors, Snake, Human, Tree, Wolf, and Sponge. Fire defeats Scissors, Snake, Human, Tree, Wolf, Sponge, and Paper. Generally speaking, each hand defeats other seven hands located after in anti-clockwise order in the figure. A player is said to win the game if the player’s hand defeats at least one of the other hands, and is not defeated by any of the other hands. Your task is to determine the winning hand, given multiple hands presented by the players. Input The input consists of a series of data sets. The first line of each data set is the number N of the players ( N < 1000). The next N lines are the hands presented by the players. The end of the input is indicated by a line containing single zero. Output For each data set, output the winning hand in a single line. When there are no winners in the game, output “Draw” (without quotes). Sample Input 8 Lightning Gun Paper Sponge Water Dragon Devil Air 3 Rock Scissors Paper 0 Output for the Sample Input Sponge Draw
[ { "submission_id": "aoj_2031_10858250", "code_snippet": "#include <string>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nvector<string> r = {\n\t\"Rock\",\n\t\"Gun\",\n\t\"Lightning\",\n\t\"Devil\",\n\t\"Dragon\",\n\t\"Water\",\n\t\"Air\",\n\t\"Paper\",\n\t\"Sponge\",\n\t\"Wolf\",\n\t\"Tree\",\n\t\"Human\",\n\t\"Snake\",\n\t\"Scissors\",\n\t\"Fire\"\n};\nint n; string s;\nint main() {\n\twhile (cin >> n, n) {\n\t\tvector<int> v;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcin >> s;\n\t\t\tv.push_back(find(r.begin(), r.end(), s) - r.begin());\n\t\t}\n\t\tstring ret = \"Draw\";\n\t\tfor (int i = 0; i < v.size(); i++) {\n\t\t\tbool win = false, lose = false;\n\t\t\tfor (int j = 0; j < v.size(); j++) {\n\t\t\t\tif (i == j) continue;\n\t\t\t\tint z = (v[j] - v[i] + 15) % 15;\n\t\t\t\tif (1 <= z && z <= 7) lose = true;\n\t\t\t\tif (8 <= z && z <= 14) win = true;\n\t\t\t}\n\t\t\tif (win && !lose) ret = r[v[i]];\n\t\t}\n\t\tcout << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3308, "score_of_the_acc": -1.0389, "final_rank": 19 }, { "submission_id": "aoj_2031_9544457", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\n15 gestures: \nRock, Fire, Scissors, Snake, Human, Tree, Wolf, Sponge, Paper, Air, Water, Dragon, Devil, Lightning, Gun\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nstatic char buf[25];\nconst int INF = 1000000000;\nconst int M = 7;\nconst int N = 15;\nstatic bool G[N][N];\nstatic int indeg[N];\nstatic int outdeg[N];\nstatic map<string,int> to_vert =\n{{\"Rock\", 0}, {\"Fire\", 1}, {\"Scissors\", 2}, {\"Snake\", 3}, {\"Human\", 4}, {\"Tree\", 5}, {\"Wolf\", 6}, {\"Sponge\", 7}, {\"Paper\", 8}, {\"Air\", 9}, {\"Water\", 10}, {\"Dragon\", 11}, {\"Devil\", 12}, {\"Lightning\", 13}, {\"Gun\", 14}};\nstatic vector<string> to_str =\n{\n \"Rock\",\"Fire\",\"Scissors\",\"Snake\",\"Human\"\n ,\"Tree\",\"Wolf\",\"Sponge\",\"Paper\",\"Air\"\n ,\"Water\",\"Dragon\",\"Devil\",\"Lightning\",\"Gun\"\n};\nvoid setup()\n{\n for (int v=0; v<N; ++v) for (int w=0; w<N; ++w) G[v][w] = false;\n for (int v=0; v<N; ++v)\n for (int m=1; m<=M; ++m)\n G[v][(v+m)%15] = true;\n}\n//------------------------------------------------------------------------------\nvoid solve(int K, const vector<int>& vs)\n{\n //--------------------------------------------------------------------------\n // base cases:\n //--------------------------------------------------------------------------\n // init:\n for (int v=0; v<N; ++v) { indeg[v] = 0; outdeg[v] = 0; }\n //--------------------------------------------------------------------------\n // compute:\n for (int k1=0; k1<K; ++k1)\n {\n for (int k2=0; k2<K; ++k2)\n {\n if (k2 == k1) continue;\n if (G[ vs[k1] ][ vs[k2] ])\n {\n outdeg[ vs[k1] ]++;\n indeg[ vs[k2] ]++;\n }\n }\n }\n //--------------------------------------------------------------------------\n bool winning = false;\n for (int v=0; v<N; ++v)\n if (!indeg[v] && outdeg[v])\n { printf(\"%s\\n\", to_str[v].c_str()); winning = true; break; }\n if (!winning) printf(\"Draw\\n\");\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n setup();\n //--------------------------------------------------------------------------\n int K, num;\n while (true)\n {\n num = scanf(\"%d \", &K);\n if (K == 0) break;\n vector<int> vs(K);\n for (int k=0; k<K; ++k)\n {\n num = scanf(\"%s \", buf);\n vs[k] = to_vert[string(buf)];\n }\n solve(K, vs);\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 110, "memory_kb": 3292, "score_of_the_acc": -1.0223, "final_rank": 18 }, { "submission_id": "aoj_2031_3173923", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <utility>\n#include <cstring>\nconst int MOD = 1e9 + 7;\nconst int dx[] = {1, -1, 0, 0};\nconst int dy[] = {0, 0, 1, -1};\nusing namespace std;\ntypedef long long ll;\nstring hands[] = {\"Rock\", \"Fire\", \"Scissors\", \"Snake\", \"Human\", \"Tree\", \"Wolf\", \"Sponge\",\"Paper\",\"Air\",\"Water\",\"Dragon\", \"Devil\",\"Lightning\",\"Gun\"};\nint check(string s)\n{\n\tfor(int i = 0; i < 15; i++)\n\t\tif(hands[i] == s)\n\t\t\treturn i;\n}\nint main()\n{\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tint n;\n\twhile(cin >> n, n)\n\t{\n\t\tint table[15][15];\n\t\tint flag[15] = {};\n\t\tmemset(table, -1, sizeof(table));\n\t\tfor(int i = 0; i < 15; i++)\n\t\t{\n\t\t\tbool flag = false;\n\t\t\tfor(int j = 0; j < 15; j++)\n\t\t\t{\n\t\t\t\tint cnt = 7;\n\t\t\t\tif(i == j) \n\t\t\t\t{\n\t\t\t\t\ttable[i][j] = 0;\n\t\t\t\t\tint x = j;\n\t\t\t\t\twhile(cnt--)\n\t\t\t\t\t{\n\t\t\t\t\t\tx++;\n\t\t\t\t\t\ttable[i][x % 15] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring str[n + 1];\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tcin >> str[i];\n\t\tint num[n + 1] = {};\n\t\tint max_p = 0, p;\n\t\tfor(int i = 0; i < n; i++)\n\t\t{\n\t\t\tint y = check(str[i]);\n\t\t\tfor(int j = 0; j < n; j++)\n\t\t\t{\n\t\t\t\tint x = check(str[j]);\n\t\t\t\tif(table[y][x] == -1)\n\t\t\t\t{\n\t\t\t\t\tnum[i] = -1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse \n\t\t\t\t\tnum[i] += table[y][x];\n\t\t\t}\n\t\t\tif(num[i] > max_p)\n\t\t\t{\n\t\t\t\tmax_p = num[i];\n\t\t\t\tp = i;\n\t\t\t}\n\t\t}\n\t\tif(max_p == 0)\n\t\t\tcout << \"Draw\" << endl;\n\t\telse\t\n\t\t\tcout << str[p] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3068, "score_of_the_acc": -0.8973, "final_rank": 12 }, { "submission_id": "aoj_2031_3173371", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int n,cnt;\n int w[15],t[15];\n string s;\n while(1){\n cin>>n;\n if(n==0) break;\n cnt=0;\n for(int i=0;i<15;i++){\n w[i]=1;\n t[i]=0;\n }\n for(int i=0;i<n;i++){\n cin>>s;\n if(s[0]=='R'){\n\tt[0]=1;\n\tfor(int j=1;j<8;j++){\n\t w[j]=0;\n\t}\n }\n else if(s[0]=='F'){\n\tt[1]=1;\n\tfor(int j=2;j<9;j++){\n\t w[j]=0;\n\t}\n }\n else if(s[0]=='S'){\n\t if(s[1]=='c'){\n\t t[2]=1;\n\t for(int j=3;j<10;j++){\n\t w[j]=0;\n\t }\n\t }\n\t else if(s[1]=='n'){\n\t t[3]=1;\n\t for(int j=4;j<11;j++){\n\t w[j]=0;\n\t }\n\t }\n\t else if(s[1]=='p'){\n\t t[7]=1;\n\t for(int j=8;j<15;j++){\n\t w[j]=0;\n\t }\n\t }\n }\n else if(s[0]=='H'){\n\t t[4]=1;\n\t for(int j=5;j<12;j++){\n\t w[j]=0;\n\t }\n }\n else if(s[0]=='T'){\n\t t[5]=1;\n\t for(int j=6;j<13;j++){\n\t w[j]=0;\n\t }\n }\n else if(s[0]=='W'){\n\t if(s[1]=='o'){\n\t t[6]=1;\n\t for(int j=7;j<14;j++){\n\t w[j]=0;\n\t }\n\t }\n\t else if(s[1]=='a'){\n\t t[10]=1;\n\t for(int j=11;j<15;j++){\n\t w[j]=0;\n\t }\n\t for(int j=0;j<3;j++){\n\t w[j]=0;\n\t }\n\t }\n }\n else if(s[0]=='P'){\n\t t[8]=1;\n\t w[0]=0;\n\t for(int j=9;j<15;j++){\n\t w[j]=0;\n\t }\n }\n else if(s[0]=='A'){\n\t t[9]=1;\n\t w[0]=0;\n\t w[1]=0;\n\t for(int j=10;j<15;j++){\n\t w[j]=0;\n\t }\n }\n else if(s[0]=='D'){\n\tif(s[1]=='r'){\n\t t[11]=1;\n\t for(int j=12;j<15;j++){\n\t w[j]=0;\n\t }\n\t for(int j=0;j<4;j++){\n\t w[j]=0;\n\t }\n\t}\n\telse if(s[1]=='e'){\n\t t[12]=1;\n\t for(int j=13;j<15;j++){\n\t w[j]=0;\n\t }\n\t for(int j=0;j<5;j++){\n\t w[j]=0;\n\t }\n\t}\n }\n else if(s[0]=='L'){\n\tt[13]=1;\n\tw[14]=0;\n\tfor(int j=0;j<6;j++){\n\t w[j]=0;\n\t}\n }\n else if(s[0]=='G'){\n\tt[14]=1;\n\tfor(int j=0;j<7;j++){\n\t w[j]=0;\n\t}\n }\n }\n for(int i=0;i<15;i++){\n if(t[i]==1) cnt++;\n }\n if(cnt==1) cout<<\"Draw\"<<endl;\n else{\n for(int i=0;i<15;i++){\n\tif(t[i]==1){\n\t if(w[i]==1){\n\t if(i==0){\n\t cout<<\"Rock\"<<endl;\n\t break;\n\t }\n\t else if(i==1){\n\t cout<<\"Fire\"<<endl;\n\t break;\n\t }\n\t else if(i==2){\n\t cout<<\"Scissors\"<<endl;\n\t break;\n\t }\n\t else if(i==3){\n\t cout<<\"Snake\"<<endl;\n\t break;\n\t }\n\t else if(i==4){\n\t cout<<\"Human\"<<endl;\n\t break;\n\t }\n\t else if(i==5){\n\t cout<<\"Tree\"<<endl;\n\t break;\n\t }\n\t else if(i==6){\n\t cout<<\"Wolf\"<<endl;\n\t break;\n\t }\n\t else if(i==7){\n\t cout<<\"Sponge\"<<endl;\n\t break;\n\t }\n\t else if(i==8){\n\t cout<<\"Paper\"<<endl;\n\t break;\n\t }\n\t else if(i==9){\n\t cout<<\"Air\"<<endl;\n\t break;\n\t }\n\t else if(i==10){\n\t cout<<\"Water\"<<endl;\n\t break;\n\t }\n\t else if(i==11){\n\t cout<<\"Dragon\"<<endl;\n\t break;\n\t }\n\t else if(i==12){\n\t cout<<\"Devil\"<<endl;\n\t break;\n\t }\n\t else if(i==13){\n\t cout<<\"Lightning\"<<endl;\n\t break;\n\t }\n\t else if(i==14){\n\t cout<<\"Gun\"<<endl;\n\t break;\n\t }\n\t }\n\t}\n\tif(i==14) cout<<\"Draw\"<<endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.9216, "final_rank": 14 }, { "submission_id": "aoj_2031_3173348", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <utility>\n#include <cstring>\nconst int MOD = 1e9 + 7;\nconst int dx[] = {1, -1, 0, 0};\nconst int dy[] = {0, 0, 1, -1};\nusing namespace std;\ntypedef long long ll;\nstring hands[] = {\"Rock\", \"Fire\", \"Scissors\", \"Snake\", \"Human\", \"Tree\", \"Wolf\", \"Sponge\",\"Paper\",\"Air\",\"Water\",\"Dragon\", \"Devil\",\"Lightning\",\"Gun\"};\nint check(string s)\n{\n\tfor(int i = 0; i < 15; i++)\n\t\tif(hands[i] == s)\n\t\t\treturn i;\n}\nint main()\n{\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tint n;\n\twhile(cin >> n, n)\n\t{\n\t\tint table[15][15];\n\t\tint flag[15] = {};\n\t\tmemset(table, -1, sizeof(table));\n\t\tfor(int i = 0; i < 15; i++)\n\t\t{\n\t\t\tbool flag = false;\n\t\t\tfor(int j = 0; j < 15; j++)\n\t\t\t{\n\t\t\t\tint cnt = 7;\n\t\t\t\tif(i == j) \n\t\t\t\t{\n\t\t\t\t\ttable[i][j] = 0;\n\t\t\t\t\tint x = j;\n\t\t\t\t\twhile(cnt--)\n\t\t\t\t\t{\n\t\t\t\t\t\tx++;\n\t\t\t\t\t\ttable[i][x % 15] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring str[n + 1];\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tcin >> str[i];\n\t\tint num[n + 1] = {};\n\t\tint max_p = 0, p;\n\t\tfor(int i = 0; i < n; i++)\n\t\t{\n\t\t\tint y = check(str[i]);\n\t\t\tfor(int j = 0; j < n; j++)\n\t\t\t{\n\t\t\t\tint x = check(str[j]);\n\t\t\t\tif(table[y][x] == -1)\n\t\t\t\t{\n\t\t\t\t\tnum[i] = -1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse \n\t\t\t\t\tnum[i] += table[y][x];\n\t\t\t}\n\t\t\tif(num[i] > max_p)\n\t\t\t{\n\t\t\t\tmax_p = num[i];\n\t\t\t\tp = i;\n\t\t\t}\n\t\t}\n\t\tif(max_p == 0)\n\t\t\tcout << \"Draw\" << endl;\n\t\telse\t\n\t\t\tcout << str[p] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3216, "score_of_the_acc": -0.968, "final_rank": 17 }, { "submission_id": "aoj_2031_3173110", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;;\n\nll to_num(string s){\n if(s==\"Rock\"){return 0;}\n if(s==\"Fire\"){return 1;}\n if(s==\"Scissors\"){return 2;}\n if(s==\"Snake\"){return 3;}\n if(s==\"Human\"){return 4;}\n if(s==\"Tree\"){return 5;}\n if(s==\"Wolf\"){return 6;}\n if(s==\"Sponge\"){return 7;}\n if(s==\"Paper\"){return 8;}\n if(s==\"Air\"){return 9;}\n if(s==\"Water\"){return 10;}\n if(s==\"Dragon\"){return 11;}\n if(s==\"Devil\"){return 12;}\n if(s==\"Lightning\"){return 13;}\n if(s==\"Gun\"){return 14;}\n return -1;\n}\n\nstring ans(ll a){\n if(a==0){return \"Rock\";}\n if(a==1)return \"Fire\";\n if(a==2)return \"Scissors\";\n if(a==3)return \"Snake\";\n if(a==4)return \"Human\";\n if(a==5)return \"Tree\";\n if(a==6)return \"Wolf\";\n if(a==7){return \"Sponge\";}\n if(a==8){return \"Paper\";}\n if(a==9){return \"Air\";}\n if(a==10){return \"Water\";}\n if(a==11){return \"Dragon\";}\n if(a==12){return \"Devil\";}\n if(a==13){return \"Lightning\";}\n if(a==14){return \"Gun\";}\n return \"!\";\n}\n\n\nint main(){\n while(1){\n vector<ll> count(30,0);\n ll n;\n cin>>n;\n if(n==0){break;}\n for(int i=0;i<n;i++){\n string s;\n cin>>s;\n count[to_num(s)]++;\n count[to_num(s)+15]++;\n }\n for(int i=1;i<30;i++){\n count[i]+=count[i-1];\n }\n bool done=false;\n for(int i=0;i<15 && !done;i++){\n if(i==0){\n if(count[i]!=0){\n\tif(count[i]==n){cout<<\"Draw\"<<endl; done=true;}\n\telse if(count[i+7]==n){cout<<\"Rock\"<<endl; done=true;}\n }\n }\n else{\n if(count[i]-count[i-1]!=0){\n\tif(count[i]-count[i-1]==n){cout<<\"Draw\"<<endl; done=true;}\n\telse if(count[i+7]-count[i-1]==n){cout<<ans(i)<<endl; done=true;}\n }\n }\n }\n if(!done){cout<<\"Draw\"<<endl;}\n\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3044, "score_of_the_acc": -0.8768, "final_rank": 10 }, { "submission_id": "aoj_2031_2855615", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\n\nld dis(pair<ld, ld>l, pair<ld, ld>r) {\n\tld dx=l.first-r.first;\n\tld dy=l.second-r.second;\n\treturn sqrt(ld(dx)*dx+ ld(dy)*dy);\n}\n\nld get_x(pair<ld, ld>f, pair<ld, ld>t, ld y) {\n\tif(f.second>t.second)return get_x(t,f,y);\n\n\tld a=y-f.second;\n\tld b=t.second-y;\n\n\treturn (t.first*a+f.first*b)/(a+b);\n}\n\nint main() {\n\tvector<string>sts{ \n\t\t\"Rock\",\"Fire\",\"Scissors\",\"Snake\",\"Human\",\"Tree\",\"Wolf\",\"Sponge\",\"Paper\",\"Air\",\"Water\",\"Dragon\",\"Devil\",\"Lightning\",\"Gun\" };\n\twhile (true) {\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\t\tvector<int>nums(N);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring st;cin>>st;\n\t\t\tnums[i]=find(sts.begin(),sts.end(),st)-sts.begin();\n\t\t\tassert(nums[i]!=15);\n\t\t}\n\t\tint ans_id=-1;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint ok=true;\n\t\t\tint num=0;\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tif(i==j)continue;\n\t\t\t\tint a=nums[i];\n\t\t\t\tint b=nums[j];\n\n\t\t\t\tint sa=a-b;\n\t\t\t\tif(sa<0)sa+=15;\n\t\t\t\tif (sa == 0) {\n\n\t\t\t\t}\n\t\t\t\telse if (sa <= 7) {\n\t\t\t\t\tok=false;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tnum++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok&&num>0)ans_id=i;\n\t\t}\n\t\tif(ans_id==-1)cout<<\"Draw\"<<endl;\n\t\telse cout<<sts[nums[ans_id]]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3140, "score_of_the_acc": -0.9526, "final_rank": 16 }, { "submission_id": "aoj_2031_2521166", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nstring t,s[] = {\"Rock\", \"Fire\", \"Scissors\", \"Snake\", \"Human\",\"Tree\", \"Wolf\", \"Sponge\", \"Paper\", \"Air\",\"Water\", \"Dragon\", \"Devil\", \"Lightning\", \"Gun\" };\nint a[30],b[30];\nmain(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tset<int>st;\n\t\tr(i,30)a[i]=b[i]=0;\n\t\tr(i,n){\n\t\t\tcin>>t;\n\t\t\tr(j,15)if(s[j]==t){\n\t\t\t\tst.insert(j);\n\t\t\t\tr(k,7){\n\t\t\t\t b[j]++;\n\t\t\t \t a[(j+k+1)%15]++;\n\t\t\t \t}\n\t\t\t }\n\t\t}\n\t\tvector<string>v;\n\t\tr(i,15)if(b[i]&&!a[i])v.push_back(s[i]);\n\t\tif(v.size()==1&&st.size()!=1)cout<<v[0]<<endl;\n\t\telse cout<<\"Draw\"<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3104, "score_of_the_acc": -0.9025, "final_rank": 13 }, { "submission_id": "aoj_2031_1896670", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nint main(){\n\tmap<string,int>m;\n\tm[\"Rock\"]=1;\n\tm[\"Fire\"]=2;\n\tm[\"Scissors\"]=3;\n\tm[\"Snake\"]=4;\n\tm[\"Human\"]=5;\n\tm[\"Tree\"]=6;\n\tm[\"Wolf\"]=7;\n\tm[\"Sponge\"]=8;\n\tm[\"Paper\"]=9;\n\tm[\"Air\"]=10;\n\tm[\"Water\"]=11;\n\tm[\"Dragon\"]=12;\n\tm[\"Devil\"]=13;\n\tm[\"Lightning\"]=14;\n\tm[\"Gun\"]=15;\n\tint n;\n\twhile(cin>>n,n){\n\t\tvector<string>in(n);\n\t\trep(i,n)cin>>in[i];\n\t\tin.erase(unique(all(in)),in.end());\n\t\tif(in.size()==1){\n\t\t\tcout<<\"Draw\"<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tvi w(in.size());\n\t\trep(i,in.size())w[i]=m[in[i]];\n\t\tbool h=false;\n\t\trep(i,in.size()){\n\t\t\tbool hh=true;\n\t\t\trep(j,in.size()){\n\t\t\t\tint t=w[i],s=w[j];\n\t\t\t\tif(t==s)continue;\n\t\t\t\tif(t>s&&t-s<8||t<s&&15-s+t<8)hh=false;\n\t\t\t}\n\t\t\tif(hh){\n\t\t\t\th=true;\n\t\t\t\tcout<<in[i]<<endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!h)cout<<\"Draw\"<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 1276, "score_of_the_acc": -0.0496, "final_rank": 7 }, { "submission_id": "aoj_2031_1896666", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nint main(){\n\tmap<string,int>m;\n\tm[\"Rock\"]=1;\n\tm[\"Fire\"]=2;\n\tm[\"Scissors\"]=3;\n\tm[\"Snake\"]=4;\n\tm[\"Human\"]=5;\n\tm[\"Tree\"]=6;\n\tm[\"Wolf\"]=7;\n\tm[\"Sponge\"]=8;\n\tm[\"Paper\"]=9;\n\tm[\"Air\"]=10;\n\tm[\"Water\"]=11;\n\tm[\"Dragon\"]=12;\n\tm[\"Devil\"]=13;\n\tm[\"Lightning\"]=14;\n\tm[\"Gun\"]=15;\n\tint n;\n\twhile(cin>>n,n){\n\t\tvector<string>in(n);\n\t\trep(i,n)cin>>in[i];\n\t\tin.erase(unique(all(in)),in.end());\n\t\tif(in.size()==1){\n\t\t\tcout<<\"Draw\"<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tbool h=false;\n\t\trep(i,in.size()){\n\t\t\tbool hh=true;\n\t\t\trep(j,in.size()){\n\t\t\t\tint t=m[in[i]],s=m[in[j]];\n\t\t\t\tif(t==s)continue;\n\t\t\t\tif(t>s&&t-s<8||t<s&&15-s+t<8)hh=false;\n\t\t\t}\n\t\t\tif(hh){\n\t\t\t\th=true;\n\t\t\t\tcout<<in[i]<<endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!h)cout<<\"Draw\"<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 1272, "score_of_the_acc": -0.4609, "final_rank": 9 }, { "submission_id": "aoj_2031_1882385", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX 15\n \nconst string gesture[MAX] = {\n \"Rock\",\"Fire\",\"Scissors\",\"Snake\",\"Human\",\"Tree\",\"Wolf\",\"Sponge\",\n \"Paper\",\"Air\",\"Water\",\"Dragon\",\"Devil\",\"Lightning\",\"Gun\"\n};\n \nvector<int> v;\nbool ges[MAX];\n \nint solve(){\n int res = -1, N = (int)v.size();\n \n if(N == 1){\n\treturn res;\n } \n \n for(int i = 0 ; i < N ; i++){\n\tint win = 0, lose = 0;\n\tfor(int j = 0 ; j < MAX ; j++){\n\t if(v[i] == j || !ges[j]){\n\t\tcontinue;\n\t }\n\n\t int cnt = 0, pos = v[i];\n\t while(true){\n\t\tpos++; cnt++;\n\t\tif(pos >= MAX){\n\t\t pos = 0;\n\t\t}\n \n\t\tif(j == pos){\n\t\t if(cnt > 7){\n\t\t\tlose++;\n\t\t }else{\n\t\t\twin++;\n\t\t }\n\t\t break;\n\t\t}\n\t }\n\t}\n\tif(win == N-1){\n\t res = v[i];\n\t break;\n\t}\n }\n \n return res;\n}\n \nint main(){\n int n;\n \n while(cin >> n ,n){\n\tstring str;\n\tset<string> st;\n \n\tv.clear();\n\tmemset(ges,false,sizeof(ges));\n \n\tfor(int i = 0 ; i < n ; i++){\n\t cin >> str;\n\t if(st.count(str)){\n\t\tcontinue;\n\t }\n\t for(int j = 0 ; j < MAX ; j++){\n\t\tif(gesture[j] == str){\n\t\t v.push_back(j);\n\t\t st.insert(str);\n\t\t ges[j] = true;\n\t\t break;\n\t\t}\n\t }\n\t}\n \n\tsort(v.begin(),v.end());\n\tint ans = solve();\n \n\tif(ans == -1){\n\t cout << \"Draw\" << endl;\n\t}else{\n\t cout << gesture[ans] << endl;\n\t}\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.9216, "final_rank": 14 }, { "submission_id": "aoj_2031_1800711", "code_snippet": "#include<iostream>\n#include<string>\nusing namespace std;\nstring S[15] = { \"Rock\", \"Fire\", \"Scissors\", \"Snake\", \"Human\", \"Tree\", \"Wolf\", \"Sponge\", \"Paper\", \"Air\", \"Water\", \"Dragon\", \"Devil\", \"Lightning\", \"Gun\" };\nint N, a[10000], c[15]; string T;\nbool solve(int a, int b) {\n\tif (a > b)b += 15;\n\tif (b - a <= 7)return true;\n\treturn false;\n}\nint main() {\n\twhile (true) {\n\t\tcin >> N;\n\t\tif (N == 0)break;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> T;\n\t\t\tfor (int j = 0; j < 15; j++) {\n\t\t\t\tif (T == S[j])a[i] = j;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 15; i++)c[i] = 0;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (!solve(a[i], a[j]))goto E;\n\t\t\t}\n\t\t\tc[a[i]] = 1;\n\t\tE:;\n\t\t}\n\t\tint G = 0;\n\t\tfor (int i = 0; i < N - 1; i++) {\n\t\t\tif (a[i] != a[i + 1])G = 1;\n\t\t}\n\t\tint P = 0;\n\t\tif (G == 0)goto F;\n\t\tfor (int i = 0; i < 15; i++) {\n\t\t\tif (c[i] == 1) {\n\t\t\t\tP = 1; cout << S[i] << endl;\n\t\t\t}\n\t\t}\n\tF:;\n\t\tif (P == 0)cout << \"Draw\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1220, "score_of_the_acc": -0.0049, "final_rank": 2 }, { "submission_id": "aoj_2031_1718302", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n string mp[]={\"Rock\",\"Gun\",\"Lightning\",\"Devil\",\"Dragon\",\"Water\",\"Air\",\"Paper\",\"Sponge\",\"Wolf\",\"Tree\",\"Human\",\"Snake\",\"Scissors\",\"Fire\"};\n\n int n;\n while(cin>>n,n){\n string str[1001];\n for(int i=0;i<n;i++) cin>>str[i];\n \n string ans=\"Draw\";\n set<string> S;\n for(int i=0;i<n;i++){\n if(S.count(str[i]))continue;\n S.insert(str[i]);\n int A=0;\n while(str[i]!=mp[A])A++;\t\n int j;\n for(j=0;j<n;j++){\n\tif(str[i]==str[j])continue;\n\tint B=0;\n\twhile(str[j]!=mp[B])B++;\t\n\tif((15+B-A)%15<=7)break;\n }\n if(j==n) ans=str[i];\n }\n \n for(int i=1;i<n&&str[0]==str[i];i++)if(i==n-1)ans=\"Draw\"; \n\n cout << ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1276, "score_of_the_acc": -0.0347, "final_rank": 3 }, { "submission_id": "aoj_2031_1710839", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nint main() {\n string s[15]={\"Rock\",\"Fire\",\"Scissors\",\"Snake\",\"Human\",\"Tree\",\"Wolf\",\"Sponge\",\"Paper\",\"Air\",\"Water\",\"Dragon\",\"Devil\",\"Lightning\",\"Gun\"};\n map<string,int> r;\n for(int i=0; i<15; i++) r[s[i]]=i;\n int n;\n while(cin >> n && n) {\n map<string,int> m;\n string t[n];\n for(int i=0; i<n; i++) {\n cin >> t[i];\n m[t[i]]++;\n }\n set<string> ans;\n for(int i=0; i<n; i++) {\n int k=r[t[i]];\n int c=0,c2=0;\n for(int j=0; j<7; j++) c+=m[s[(k+j+1)%15]];\n for(int j=0; j<7; j++) c2+=m[s[(k+j+8)%15]];\n if(c&&!c2) ans.insert(t[i]);\n }\n if(ans.size()==1) cout << *ans.begin() << endl;\n else cout << \"Draw\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1276, "score_of_the_acc": -0.0466, "final_rank": 4 }, { "submission_id": "aoj_2031_1710762", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstring t[15]={\n \"Rock\",\n \"Fire\",\n \"Scissors\",\n \"Snake\",\n \"Human\",\n \"Tree\",\n \"Wolf\",\n \"Sponge\",\n \"Paper\",\n \"Air\",\n \"Water\",\n \"Dragon\",\n \"Devil\",\n \"Lightning\",\n \"Gun\"\n};\n\nint search(string s){\n for(int i=0;i<15;i++)if(t[i]==s)return i;\n assert(0);\n return 0;\n}\n\nint check(int a,int b){\n if(a==b)return 0;\n for(int i=1;i<=7;i++)if( (a+i)%15==b )return 1;\n return -1;\n}\n\nint main(){\n int n;\n while(1){\n cin>>n;\n if(n==0)break;\n vector<string> t(n);\n vector<int> u(n);\n for(int i=0;i<n;i++){\n cin>>t[i];\n u[i]=search(t[i]);\n }\n \n string ans=\"Draw\";\n for(int i=0;i<n;i++){\n int A=0,B=0;\n for(int j=0;j<n;j++){\n int k=check(u[i],u[j]);\n if(k==1)A++;\n if(k==-1)B++;\n }\n if(A>0&&B==0)ans=t[i];\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 1272, "score_of_the_acc": -0.1615, "final_rank": 8 }, { "submission_id": "aoj_2031_1710758", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\n\nint main(){\n int n,vlen;\n string str,ans;\n set<string> s;\n vector<string> v;\n vector<P> defdata;//1,0\n string d[15]={\n \"Rock\",\"Fire\",\"Scissors\",\"Snake\",\"Human\",\n \"Tree\",\"Wolf\",\"Sponge\",\"Paper\",\"Air\",\n \"Water\",\"Dragon\",\"Devil\",\"Lightning\",\"Gun\"\n };\n while(1){\n cin>>n;\n if(!n)break;\n for(int i=0;i<n;i++){\n cin>>str;\n s.insert(str);\n }\n for(int i=0;i<15;i++){\n set<string>::iterator ite;\n for(ite=s.begin();ite!=s.end();ite++){\n\tif(d[i]==(*ite)){\n\t v.push_back(d[i]);\n\t defdata.push_back(P(0,0));\n\t}\n }\n }\n vlen=v.size();\n for(int i=0;i<vlen;i++){\n for(int j=i+1;j<vlen;j++){\n\tint iid,jid;\n\tfor(int k=0;k<15;k++)\n\t if(v[i]==d[k])iid=k;\n\tfor(int k=0;k<15;k++)\n\t if(v[j]==d[k])jid=k;\n\tif(jid-iid<=7){\n\t defdata[i].first++;\n\t defdata[j].second++;\n\t}\n\tif(iid+15-jid<=7){\n\t defdata[j].first++;\n\t defdata[i].second++;\n\t}\n }\n }\n ans=\"0\";\n for(int i=0;i<vlen;i++){\n int w=defdata[i].first;\n int l=defdata[i].second;\n if(w&&!l)ans=v[i];\n }\n if(ans!=\"0\")cout<<ans<<endl;\n else cout<<\"Draw\"<<endl;\n s.clear();\n v.clear();\n defdata.clear();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1216, "score_of_the_acc": -0.003, "final_rank": 1 }, { "submission_id": "aoj_2031_1710750", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(asd,fgh) for(int asd=0; asd<fgh; asd++)\n\nint main(void){\n vector< vector<string> > defeat;\n string handlist[] = {\"Rock\", \"Fire\", \"Scissors\", \"Snake\", \"Human\", \"Tree\", \"Wolf\", \"Sponge\", \"Paper\", \"Air\", \"Water\", \"Dragon\", \"Devil\", \"Lightning\", \"Gun\"};\n map<string, int> hand;\n REP(i, 15){\n hand[handlist[i]] = i;\n }\n \n int N;\n while(cin >> N, N){\n vector<string> players(N);\n string ans = \"Draw\";\n REP(i, N){\n cin >> players[i];\n }\n string same = players[0];\n bool draw = true;\n REP(i, N){\n if(players[i] != same) draw = false;\n }\n if(draw){\n cout << ans << endl;\n continue;\n }\n REP(i, N){\n bool flg = true;\n REP(j, N){\n\tif(i == j) continue;\n\tif(!(((hand[players[j]] - hand[players[i]])+15)%15 <= 7)){\n\t flg = false;\n\t}\n }\n if(flg)\n\tans = players[i];\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2910, "memory_kb": 1272, "score_of_the_acc": -0.895, "final_rank": 11 }, { "submission_id": "aoj_2031_1710720", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n string s[15]={\"Rock\",\"Fire\",\"Scissors\",\"Snake\",\"Human\",\"Tree\",\"Wolf\",\"Sponge\",\"Paper\",\"Air\",\"Water\",\"Dragon\",\"Devil\",\"Lightning\",\"Gun\"};\n map<string,int> r;\n for(int i=0; i<15; i++) r[s[i]]=i;\n int n;\n while(cin >> n && n) {\n map<string,int> m;\n string t[n];\n for(int i=0; i<n; i++) {\n cin >> t[i];\n m[t[i]]++;\n }\n set<string> ans;\n for(int i=0; i<n; i++) {\n int k=r[t[i]];\n int c=0,c2=0;\n for(int j=0; j<7; j++) c+=m[s[(k+j+1)%15]];\n for(int j=0; j<7; j++) c2+=m[s[(k+j+8)%15]];\n if(c&&!c2) ans.insert(t[i]);\n }\n if(ans.size()==1) cout << *ans.begin() << endl;\n else cout << \"Draw\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1280, "score_of_the_acc": -0.0486, "final_rank": 6 }, { "submission_id": "aoj_2031_1577650", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nconst static int tx[] = {0,1,0,-1};\nconst static int ty[] = {-1,0,1,0};\n\nconst static double EPS = 1e-8;\n\nint main(){\n int num_of_players;\n map<string,int> dict;\n dict[\"Rock\"] = 0;\n dict[\"Fire\"] = 1;\n dict[\"Scissors\"] = 2;\n dict[\"Snake\"] = 3;\n dict[\"Human\"] = 4;\n dict[\"Tree\"] = 5;\n dict[\"Wolf\"] = 6;\n dict[\"Sponge\"] = 7;\n dict[\"Paper\"] = 8;\n dict[\"Air\"] = 9;\n dict[\"Water\"] = 10;\n dict[\"Dragon\"] = 11;\n dict[\"Devil\"] = 12;\n dict[\"Lightning\"] = 13;\n dict[\"Gun\"] = 14;\n\n map<int,bool> canWin;\n for(int hand_i = 0; hand_i < 15; hand_i++){\n for(int hand_j = 0; hand_j < 15; hand_j++){\n canWin[hand_i * 15 + hand_j] = false;\n for(int offset = 1; offset <= 7; offset++){\n if(hand_j == (hand_i + offset) % 15){\n canWin[hand_i * 15 + hand_j] = true;\n }\n }\n }\n }\n\n while(~scanf(\"%d\",&num_of_players)){\n if(num_of_players == 0){\n break;\n }\n vector<string> players;\n for(int player_i = 0; player_i < num_of_players; player_i++){\n string hand;\n cin >> hand;\n players.push_back(hand);\n }\n\n map<string,bool> lose;\n map<string,bool> win;\n\n set<string> hands;\n for(int player_i = 0; player_i < num_of_players; player_i++){\n hands.insert(players[player_i]);\n }\n for(set<string>::iterator ally_it = hands.begin(); ally_it != hands.end(); ally_it++){\n for(set<string>::iterator foe_it = hands.begin(); foe_it != hands.end(); foe_it++){\n if(canWin[dict[*ally_it] * 15 + dict[*foe_it]]){\n win[*ally_it] = true;\n lose[*foe_it] = true;\n }\n }\n }\n\n string res = \"Draw\";\n for(set<string>::iterator ally_it = hands.begin(); ally_it != hands.end(); ally_it++){\n if(win[*ally_it] && !lose[*ally_it]){\n res = *ally_it;\n }\n }\n printf(\"%s\\n\",res.c_str());\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1304, "score_of_the_acc": -0.0481, "final_rank": 5 }, { "submission_id": "aoj_2031_1577646", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nconst static int tx[] = {0,1,0,-1};\nconst static int ty[] = {-1,0,1,0};\n\nconst static double EPS = 1e-8;\n\nint main(){\n int num_of_players;\n map<string,int> dict;\n dict[\"Rock\"] = 0;\n dict[\"Fire\"] = 1;\n dict[\"Scissors\"] = 2;\n dict[\"Snake\"] = 3;\n dict[\"Human\"] = 4;\n dict[\"Tree\"] = 5;\n dict[\"Wolf\"] = 6;\n dict[\"Sponge\"] = 7;\n dict[\"Paper\"] = 8;\n dict[\"Air\"] = 9;\n dict[\"Water\"] = 10;\n dict[\"Dragon\"] = 11;\n dict[\"Devil\"] = 12;\n dict[\"Lightning\"] = 13;\n dict[\"Gun\"] = 14;\n\n map<int,bool> canWin;\n for(int hand_i = 0; hand_i < 15; hand_i++){\n for(int hand_j = 0; hand_j < 15; hand_j++){\n canWin[hand_i * 15 + hand_j] = false;\n for(int offset = 1; offset <= 7; offset++){\n if(hand_j == (hand_i + offset) % 15){\n canWin[hand_i * 15 + hand_j] = true;\n }\n }\n }\n }\n\n while(~scanf(\"%d\",&num_of_players)){\n if(num_of_players == 0){\n break;\n }\n vector<string> players;\n for(int player_i = 0; player_i < num_of_players; player_i++){\n string hand;\n cin >> hand;\n players.push_back(hand);\n }\n\n bool lose[1001] = {};\n bool win[1001] = {};\n for(int player_i = 0; player_i < num_of_players; player_i++){\n for(int player_j = player_i + 1; player_j < num_of_players; player_j++){\n if(canWin[dict[players[player_i]] * 15 + dict[players[player_j]]]){\n lose[player_j] = true;\n win[player_i] = true;\n }\n if(canWin[dict[players[player_j]] * 15 + dict[players[player_i]]]){\n lose[player_i] = true;\n win[player_j] = true;\n }\n }\n }\n\n string res = \"Draw\";\n for(int player_i = 0; player_i < num_of_players; player_i++){\n if(!lose[player_i] && win[player_i]){\n res = players[player_i];\n break;\n }\n }\n printf(\"%s\\n\",res.c_str());\n }\n}", "accuracy": 1, "time_ms": 3350, "memory_kb": 1304, "score_of_the_acc": -1.0421, "final_rank": 20 } ]
aoj_2036_cpp
Problem G: Traffic You are a resident of Kyoot (oh, well, it’s not a misspelling!) city. All streets there are neatly built on a grid; some streets run in a meridional (north-south) direction and others in a zonal (east-west) direction. The streets that run from north to south are called avenues, whereas those which run from east to west are called drives . Every avenue and drive in the city is numbered to distinguish one from another. The westernmost avenue is called the 1st avenue . The avenue which lies next to the 1st avenue is the 2nd avenue , and so forth. Similarly, drives are numbered from south to north. The figure below illustrates this situation. Figure 1: The Road Map of the Kyoot City There is an intersection with traffic signals to regulate traffic on each crossing point of an avenue and a drive. Each traffic signal in this city has two lights. One of these lights is colored green, and means “you may go”. The other is red, and means “you must stop here”. If you reached an intersection during the red light (including the case where the light turns to red on your arrival), you must stop and wait there until the light turns to green again. However, you do not have to wait in the case where the light has just turned to green when you arrived there. Traffic signals are controlled by a computer so that the lights for two different directions always show different colors. Thus if the light for an avenue is green, then the light for a drive must be red, and vice versa. In order to avoid car crashes, they will never be green together. Nor will they be red together, for efficiency. So all the signals at one intersection turn simultaneously; no sooner does one signal turn to red than the other turns to green. Each signal has a prescribed time interval and permanently repeats the cycle. Figure 2: Signal and Intersection By the way, you are planning to visit your friend by car tomorrow. You want to see her as early as possible, so you are going to drive through the shortest route. However, due to existence of the traffic signals, you cannot easily figure out which way to take (the city also has a very sophisticated camera network to prevent crime or violation: the police would surely arrest you if you didn’t stop on the red light!). So you decided to write a program to calculate the shortest possible time to her house, given the town map and the configuration of all traffic signals. Your car runs one unit distance in one unit time. Time needed to turn left or right, to begin moving, and to stop your car is negligible. You do not need to take other cars into consideration. Input The input consists of multiple test cases. Each test case is given in the format below: w h d A,1 d A,2 . . . d A,w−1 d D,1 d D,2 . . . d D,h−1 ns 1,1 ew 1,1 s 1,1 . . . ns w,1 ew w,1 s w,1 ns 1,2 ew 1,2 s 1,2 . . . ns w,h ew w,h s w,h x s y s x d y d Two integers w and h (2 ≤ w , h ≤ 100) in the first line indicate the number of avenues and drives in the city, respective ...(truncated)
[ { "submission_id": "aoj_2036_6775560", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 105\n\nenum DIR{\n\tyoko,\n\ttate,\n};\n\nenum Type{\n\tGreen,\n\tRed,\n};\n\nstruct Edge{\n\tEdge(int arg_to,int arg_dist,bool arg_is_yoko){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t\tis_yoko = arg_is_yoko;\n\t}\n\n\tint to,dist;\n\tbool is_yoko;\n};\n\nstruct Info{\n\n\tint a,b;\n\tbool is_yoko;\n};\n\nstruct State{\n\n\tState(){\n\t\tnode = sum_dist = 0;\n\t\ttype = Green;\n\t\tdir = yoko;\n\t\tis_goal = false;\n\t}\n\n\tState(int arg_node,int arg_sum_dist,Type arg_type,DIR arg_dir,bool arg_is_goal){\n\t\tnode = arg_node;\n\t\tsum_dist = arg_sum_dist;\n\t\ttype = arg_type;\n\t\tdir = arg_dir;\n\t\tis_goal = arg_is_goal;\n\t}\n\tbool operator<(const struct State& arg) const{\n\n\t\treturn sum_dist > arg.sum_dist; //昇順(PQ)\n\t}\n\tint node,sum_dist;\n\tType type;\n\tDIR dir;\n\tbool is_goal;\n};\n\n\nvector<Edge> G[SIZE*SIZE];\n\nint W,H;\nint D_yoko[SIZE],D_tate[SIZE];\nint NS[SIZE*SIZE],EW[SIZE*SIZE],S[SIZE*SIZE];\nint X[SIZE*SIZE],Y[SIZE*SIZE];\n\nint min_dist[SIZE*SIZE][2][2];\n\n\nint getInd(int row,int col){\n\n\treturn row*W + col;\n}\n\n//通りの計算\nInfo calcPos(int x,int y){\n\n\tInfo ret;\n\tret.a = -1;\n\tret.b = -1;\n\n\tint h = 0,w = 0;\n\n\t//横方向の通り\n\tfor(int row = 0; row < H; row++){\n\t\tif(y != h){\n\n\t\t\th += D_tate[row];\n\t\t\tcontinue;\n\t\t}\n\n\t\tw = 0;\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(x > w && x < w+D_yoko[col]){\n\n\t\t\t\tret.a = getInd(row,col);\n\t\t\t\tret.b = getInd(row,col+1);\n\t\t\t\tret.is_yoko = true;\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t\tw += D_yoko[col];\n\t\t}\n\t\th += D_tate[row];\n\t}\n\n\t//縦方向の通り\n\tw = 0;\n\tfor(int col = 0; col < W; col++){\n\t\tif(x != w){\n\n\t\t\tw += D_yoko[col];\n\t\t\tcontinue;\n\t\t}\n\n\t\th = 0;\n\t\tfor(int row = 0; row < H; row++){\n\t\t\tif(y > h && y < h+D_tate[row]){\n\n\t\t\t\tret.a = getInd(row,col);\n\t\t\t\tret.b = getInd(row+1,col);\n\t\t\t\tret.is_yoko = false;\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t\th += D_tate[row];\n\t\t}\n\t\tw += D_yoko[col];\n\t}\n\n\treturn ret; //バグ\n}\n\n//ある時刻における信号機の色を計算\nType calcColor(int ind,int T,bool is_yoko){\n\n\tint base = NS[ind]+EW[ind];\n\n\tint mod = T%base;\n\n\tif(S[ind] == 0){ //最初に南北方向の信号が緑\n\n\t\tif(is_yoko){ //左右の移動\n\n\t\t\t//■到着した瞬間に緑は待たなくてよい\n\t\t\tif(mod < NS[ind]){\n\n\t\t\t\treturn Red;\n\t\t\t}else{\n\n\t\t\t\treturn Green;\n\t\t\t}\n\n\t\t}else{ //南北の移動\n\n\t\t\t//■到着した瞬間に赤は停止\n\t\t\tif(mod < NS[ind]){\n\n\t\t\t\treturn Green;\n\t\t\t}else{\n\n\t\t\t\treturn Red;\n\t\t\t}\n\t\t}\n\n\t}else{ //最初に東西方向の信号が緑\n\n\t\tif(is_yoko){ //東西の移動\n\n\t\t\t//■到着した瞬間に赤は停止\n\t\t\tif(mod < EW[ind]){\n\n\t\t\t\treturn Green;\n\n\t\t\t}else{\n\n\t\t\t\treturn Red;\n\t\t\t}\n\n\t\t}else{ //南北の移動\n\n\t\t\t//■到着した瞬間に緑は待たなくてよい\n\t\t\tif(mod < EW[ind]){\n\n\t\t\t\treturn Red;\n\n\t\t\t}else{\n\n\t\t\t\treturn Green;\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < H*W; i++){\n\t\tG[i].clear();\n\t}\n\n\t//区画幅(横)\n\tfor(int i = 0; i < W-1; i++){\n\n\t\tscanf(\"%d\",&D_yoko[i]);\n\t}\n\t//区画幅(縦)\n\tfor(int i = 0; i < H-1; i++){\n\n\t\tscanf(\"%d\",&D_tate[i]);\n\t}\n\n\t//H*W個の交差点の座標を計算\n\tint h = 0;\n\tfor(int row = 0; row < H; row++){\n\t\tint w = 0;\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tint ind = getInd(row,col);\n\t\t\tX[ind] = w;\n\t\t\tY[ind] = h;\n\t\t\tw += D_yoko[col];\n\t\t}\n\t\th += D_tate[row];\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tint ind = getInd(row,col);\n\n\t\t\tscanf(\"%d %d %d\",&NS[ind],&EW[ind],&S[ind]);\n\t\t}\n\t}\n\n\tint s_x,s_y;\n\tscanf(\"%d %d\",&s_x,&s_y);\n\n\tInfo s_pos = calcPos(s_x,s_y);\n\n\tint t_x,t_y;\n\tscanf(\"%d %d\",&t_x,&t_y);\n\n\tInfo t_pos = calcPos(t_x,t_y);\n\n\n\t//同じ通りにある\n\tif(s_pos.a == t_pos.a && s_pos.b == t_pos.b){\n\n\t\tif(s_x == t_x){\n\n\t\t\tprintf(\"%d\\n\",abs(s_y-t_y));\n\t\t\treturn;\n\n\t\t}else{\n\n\t\t\tprintf(\"%d\\n\",abs(s_x-t_x));\n\t\t\treturn;\n\t\t}\n\t}\n\n\t/*辺張り*/\n\n\t//左右\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W-1; col++){\n\n\t\t\tint a = getInd(row,col);\n\t\t\tint b = getInd(row,col+1);\n\n\t\t\tG[a].push_back(Edge(b,D_yoko[col],true));\n\t\t\tG[b].push_back(Edge(a,D_yoko[col],true));\n\t\t}\n\t}\n\n\t//上下\n\tfor(int col = 0; col < W; col++){\n\t\tfor(int row = 0; row < H-1; row++){\n\n\t\t\tint a = getInd(row,col);\n\t\t\tint b = getInd(row+1,col);\n\n\t\t\tG[a].push_back(Edge(b,D_tate[row],false));\n\t\t\tG[b].push_back(Edge(a,D_tate[row],false));\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tfor(int a = 0; a < 2 ; a++){\n\n\t\t\t\tint ind = getInd(row,col);\n\t\t\t\tmin_dist[ind][Green][a] = BIG_NUM;\n\t\t\t\tmin_dist[ind][Red][a] = BIG_NUM;\n\t\t\t}\n\t\t}\n\t}\n\n\tpriority_queue<State> Q;\n\n\t/* とりあえず、スタートから最寄りの交差点に寄る */\n\n\tif(s_pos.is_yoko){\n\n\t\t//aへ\n\t\tint T = abs(s_x - X[s_pos.a]);\n\t\tType type = calcColor(s_pos.a,T,true);\n\n\t\tQ.push(State(s_pos.a,T,type,yoko,false));\n\n\t\t//bへ\n\t\tT = abs(s_x - X[s_pos.b]);\n\t\ttype = calcColor(s_pos.b,T,true);\n\n\t\tQ.push(State(s_pos.b,T,type,yoko,false));\n\n\t}else{ //startが縦の通りにある\n\n\t\t//aへ\n\t\tint T = abs(s_y - Y[s_pos.a]);\n\t\tType type = calcColor(s_pos.a,T,false);\n\n\t\tQ.push(State(s_pos.a,T,type,tate,false));\n\n\t\t//bへ\n\t\tT = abs(s_y - Y[s_pos.b]);\n\t\ttype = calcColor(s_pos.b,T,false);\n\n\t\tQ.push(State(s_pos.b,T,type,tate,false));\n\t}\n\n\twhile(!Q.empty()){\n\n\t\tState state = Q.top();\n\t\tQ.pop();\n\n\t\tif(state.is_goal){\n\n\t\t\tprintf(\"%d\\n\",state.sum_dist);\n\t\t\treturn;\n\t\t}\n\n\t\tif(state.sum_dist > min_dist[state.node][state.type][state.dir]){\n\n\t\t\tcontinue;\n\t\t}\n\t\tif(state.type == Red){\n\n\t\t\tbool is_yoko = (state.dir == yoko);\n\n\t\t\t//青になるまで待つ\n\t\t\tint add = 0;\n\t\t\tint base = NS[state.node]+EW[state.node];\n\t\t\tint now = state.sum_dist%base;\n\n\t\t\tif(is_yoko){\n\n\t\t\t\tif(S[state.node] == 0){ //最初に南北が青\n\n\t\t\t\t\tadd = NS[state.node]-now;\n\n\t\t\t\t}else{ //最初に東西が青\n\n\t\t\t\t\tadd = base-now;\n\n\t\t\t\t}\n\n\t\t\t}else{ //南北の動き\n\n\t\t\t\tif(S[state.node] == 0){ //最初に南北が青\n\n\t\t\t\t\tadd = base-now;\n\n\t\t\t\t}else{ //最初に東西が青\n\n\t\t\t\t\tadd = EW[state.node]-now;\n\t\t\t\t}\n\t\t\t}\n\n\n\t\t\tint next_node = state.node;\n\t\t\tint next_dist = state.sum_dist+add;\n\t\t\tDIR next_dir = state.dir;\n\t\t\tType next_type = Green;\n\n\t\t\tif(min_dist[next_node][next_type][next_dir] > next_dist){\n\n\t\t\t\tmin_dist[next_node][next_type][next_dir] = next_dist;\n\t\t\t\tQ.push(State(next_node,next_dist,next_type,next_dir,false));\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tfor(int i = 0; i < G[state.node].size(); i++){\n\n\t\t\tint next_node = G[state.node][i].to;\n\t\t\tint next_dist = state.sum_dist + G[state.node][i].dist;\n\n\n\t\t\t//途中にゴールがあるか\n\t\t\tint a = min(state.node,next_node);\n\t\t\tint b = max(state.node,next_node);\n\n\t\t\tif(t_pos.a == a && t_pos.b == b){\n\n\t\t\t\tint add = abs(X[state.node]-t_x)+abs(Y[state.node]-t_y);\n\t\t\t\tint candidate = state.sum_dist+add;\n\n\t\t\t\tQ.push(State(state.node,candidate,state.type,state.dir,true)); //■■■■■■■■■これが最短とは限らないので1回push■■■■■■■■■\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tType next_type;\n\t\t\tDIR next_dir;\n\n\n\t\t\tif(G[state.node][i].is_yoko){ //左右\n\n\t\t\t\tnext_type = calcColor(next_node,next_dist,true);\n\t\t\t\tnext_dir = yoko;\n\n\t\t\t}else{ //上下\n\n\t\t\t\tnext_type = calcColor(next_node,next_dist,false);\n\t\t\t\tnext_dir = tate;\n\t\t\t}\n\n\t\t\tif(min_dist[next_node][next_type][next_dir] > next_dist){\n\n\t\t\t\tmin_dist[next_node][next_type][next_dir] = next_dist;\n\t\t\t\tQ.push(State(next_node,next_dist,next_type,next_dir,false));\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\"); //バグ\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&W,&H);\n\t\tif(W == 0 && H == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 4424, "score_of_the_acc": -1, "final_rank": 5 }, { "submission_id": "aoj_2036_1357278", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_W = 100;\nconst int MAX_H = 100;\nconst int MAX_HW = MAX_H * MAX_W;\n\nconst int INF = 1 << 30;\n\nconst int dxs[] = { 1, 0, -1, 0 };\nconst int dys[] = { 0, 1, 0, -1 };\n\n/* typedef */\n\nstruct Signal {\n int nst, ewt, itv, s;\n};\n\nstruct Stat {\n int d, pos, drc;\n Stat() {}\n Stat(int _d, int _pos, int _drc) { d = _d, pos = _pos, drc = _drc; }\n bool operator<(const Stat& st0) const { return d > st0.d; }\n void print() { printf(\"d=%d, pos=%d, drc=%d\\n\", d, pos, drc); }\n};\n\nenum { D_NS = 0, D_EW = 1 };\n\ntypedef vector<Stat> vs;\n\n/* global variables */\n\nint w, h, hw;\nint dws[MAX_W], dhs[MAX_H];\nSignal sgnls[MAX_HW + 2];\n\nvs nbrs[MAX_HW + 2];\nint dists[MAX_HW + 2][2];\n\n/* subroutines */\n\nvoid onroad(int x, int n, int *ds, int& i, int& dx) {\n i = 0, dx = x;\n while (i < n && dx >= ds[i]) dx -= ds[i], i++;\n}\n\nint time_go(Signal& sgnl, int t, int drc) {\n int rt = t % sgnl.itv;\n int tg = -1;\n \n if (sgnl.s == D_NS) {\t// nst -> ewt\n if (drc == D_NS) tg = (rt < sgnl.nst) ? t : sgnl.itv * (t / sgnl.itv + 1);\n else tg = (rt >= sgnl.nst) ? t : (t / sgnl.itv) * sgnl.itv + sgnl.nst;\n }\n else {\t// ewt -> nst\n if (drc == D_EW) tg = (rt < sgnl.ewt) ? t : sgnl.itv * (t / sgnl.itv + 1);\n else tg = (rt >= sgnl.ewt) ? t : (t / sgnl.itv) * sgnl.itv + sgnl.ewt;\n }\n\n return tg;\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> w >> h;\n if (w == 0) break;\n\n hw = h * w;\n \n for (int i = 0; i < w - 1; i++) cin >> dws[i];\n for (int i = 0; i < h - 1; i++) cin >> dhs[i];\n\n for (int y = 0; y < h; y++)\n for (int x = 0; x < w; x++) {\n\tint pos = y * w + x;\n\tSignal& sgnl = sgnls[pos];\n\tcin >> sgnl.nst >> sgnl.ewt >> sgnl.s;\n\tsgnl.itv = sgnl.nst + sgnl.ewt;\n\n\tnbrs[pos].clear();\n\n\tfor (int di = 0; di < 4; di++) {\n\t int x0 = x + dxs[di];\n\t int y0 = y + dys[di];\n\t if (x0 >= 0 && x0 < w && y0 >= 0 && y0 < h) {\n\t int pos0 = y0 * w + x0;\n\t int ec, edrc;\n\t if (x0 != x) {\n\t ec = dws[(x0 < x) ? x0 : x];\n\t edrc = D_EW;\n\t }\n\t else {\n\t ec = dhs[(y0 < y) ? y0 : y];\n\t edrc = D_NS;\n\t }\n\t nbrs[pos].push_back(Stat(ec, pos0, edrc));\n\t }\n\t}\n }\n\n int sx, sy;\n cin >> sx >> sy;\n\n int sxi, syi, sdx, sdy;\n onroad(sx, w - 1, dws, sxi, sdx);\n onroad(sy, h - 1, dhs, syi, sdy);\n //printf(\"(sxi,syi)=(%d,%d), (sdx,sdy)=(%d,%d)\\n\", sxi, syi, sdx, sdy);\n \n nbrs[hw].clear();\n \n if (sdx != 0) {\n int pos0 = syi * w + sxi, pos1 = pos0 + 1;\n nbrs[hw].push_back(Stat(sdx, pos0, D_EW));\n nbrs[hw].push_back(Stat(dws[sxi] - sdx, pos1, D_EW));\n sgnls[hw].nst = 0;\n sgnls[hw].ewt = 1;\n sgnls[hw].itv = 1;\n sgnls[hw].s = D_EW;\n }\n else {\n int pos0 = syi * w + sxi, pos1 = pos0 + w;\n nbrs[hw].push_back(Stat(sdy, pos0, D_NS));\n nbrs[hw].push_back(Stat(dhs[syi] - sdy, pos1, D_NS));\n sgnls[hw].nst = 1;\n sgnls[hw].ewt = 0;\n sgnls[hw].itv = 1;\n sgnls[hw].s = D_NS;\n }\n\n int gx, gy;\n cin >> gx >> gy;\n\n int gxi, gyi, gdx, gdy;\n onroad(gx, w - 1, dws, gxi, gdx);\n onroad(gy, h - 1, dhs, gyi, gdy);\n //printf(\"(gxi,gyi)=(%d,%d), (gdx,gdy)=(%d,%d)\\n\", gxi, gyi, gdx, gdy);\n\n if (gdx != 0) {\n int pos0 = gyi * w + gxi, pos1 = pos0 + 1;\n nbrs[pos0].push_back(Stat(gdx, hw + 1, D_EW));\n nbrs[pos1].push_back(Stat(dws[gxi] - gdx, hw + 1, D_EW));\n }\n else {\n int pos0 = gyi * w + gxi, pos1 = pos0 + w;\n nbrs[pos0].push_back(Stat(gdy, hw + 1, D_NS));\n nbrs[pos1].push_back(Stat(dhs[gyi] - gdy, hw + 1, D_NS));\n }\n\n if (sxi == gxi && syi == gyi) {\n if (sdy == 0 && gdy == 0) {\n\tint dx = (gdx > sdx) ? gdx - sdx : sdx - gdx;\n\tnbrs[hw].push_back(Stat(dx, hw + 1, D_EW));\n }\n else if (sdx == 0 && gdx == 0) {\n\tint dy = (gdy > sdy) ? gdy - sdy : sdy - gdy;\n\tnbrs[hw].push_back(Stat(dy, hw + 1, D_NS));\n }\n }\n \n for (int pos = 0; pos < hw + 2; pos++)\n for (int drc = 0; drc < 2; drc++)\n\tdists[pos][drc] = INF;\n\n dists[hw][0] = dists[hw][1] = 0;\n\n priority_queue<Stat> q;\n q.push(Stat(0, hw, sgnls[hw].s));\n\n int mind = INF;\n \n while (! q.empty()) {\n Stat u = q.top();\n q.pop();\n //u.print();\n \n if (u.d != dists[u.pos][u.drc]) continue;\n if (u.pos == hw + 1) {\n\tmind = u.d;\n\tbreak;\n }\n\n int ud = time_go(sgnls[u.pos], u.d, u.drc);\n vs& nbru = nbrs[u.pos];\n\n for (vs::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n\tStat& v = *vit;\n\tint vd = ud + v.d;\n\n\tif (dists[v.pos][v.drc] > vd) {\n\t dists[v.pos][v.drc] = vd;\n\t q.push(Stat(vd, v.pos, v.drc));\n\t}\n }\n }\n\n cout << mind << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 2304, "score_of_the_acc": -0.3736, "final_rank": 1 }, { "submission_id": "aoj_2036_469393", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nconst int INF = INT_MAX / 2;\n\nint h, w;\nvector<int> dy, dx;\nvector<vector<int> > ns, ew, s;\n\nvoid add(int& y, int& x)\n{\n int y2 = 1;\n while(y > 0){\n y -= dy[y2];\n ++ y2;\n }\n int x2 = 1;\n while(x > 0){\n x -= dx[x2];\n ++ x2;\n }\n\n if(y != 0 || x != 0){\n if(y == 0){\n dx[x2-1] += x;\n dx.insert(dx.begin()+x2, -x);\n for(int i=0; i<h+2; ++i){\n ns[i].insert(ns[i].begin()+x2, 1);\n ew[i].insert(ew[i].begin()+x2, INF);\n s[i].insert(s[i].begin()+x2, 1);\n }\n ++ w;\n }else{\n dy[y2-1] += y;\n dy.insert(dy.begin()+y2, -y);\n ns.insert(ns.begin()+y2, vector<int>(w+2, INF));\n ew.insert(ew.begin()+y2, vector<int>(w+2, 1));\n s.insert(s.begin()+y2, vector<int>(w+2, 0));\n ++ h;\n }\n }\n\n y = y2;\n x = x2;\n}\n\nint main()\n{\n for(;;){\n cin >> w >> h;\n if(h == 0)\n return 0;\n\n dx.assign(w+1, INF);\n dy.assign(h+1, INF);\n for(int i=1; i<w; ++i)\n cin >> dx[i];\n for(int i=1; i<h; ++i)\n cin >> dy[i];\n\n ns = ew = s = vector<vector<int> >(h+2, vector<int>(w+2, 1));\n for(int i=1; i<=h; ++i){\n for(int j=1; j<=w; ++j){\n cin >> ns[i][j] >> ew[i][j] >> s[i][j];\n }\n }\n\n int sy, sx, gy, gx;\n cin >> sx >> sy >> gx >> gy;\n int sy0 = sy;\n int sx0 = sx;\n add(sy0, sx0);\n add(gy, gx);\n add(sy, sx);\n\n vector<vector<vector<int> > > dp(h+2, vector<vector<int> >(w+2, vector<int>(2, INF)));\n for(int i=0; i<h+2; ++i)\n dp[i][0][1] = dp[i][w+1][1] = -1;\n for(int i=0; i<w+2; ++i)\n dp[0][i][0] = dp[h+1][i][0] = -1;\n\n multimap<int, pair<pair<int, int>, int> > mm;\n dp[sy][sx][0] = dp[sy][sx][1] = 0;\n mm.insert(make_pair(0, make_pair(make_pair(sy, sx), 0)));\n mm.insert(make_pair(0, make_pair(make_pair(sy, sx), 1)));\n\n while(!mm.empty()){\n int t = mm.begin()->first;\n int y = mm.begin()->second.first.first;\n int x = mm.begin()->second.first.second;\n int d = mm.begin()->second.second;\n mm.erase(mm.begin());\n if(t > dp[y][x][d])\n continue;\n\n if(s[y][x] == 0){\n int t2 = t % (ns[y][x] + ew[y][x]);\n if(d == 0){\n if(t2 >= ns[y][x])\n t += -t2 + (ns[y][x] + ew[y][x]);\n }else{\n if(t2 < ns[y][x])\n t += -t2 + ns[y][x];\n }\n }else{\n int t2 = t % (ew[y][x] + ns[y][x]);\n if(d == 0){\n if(t2 < ew[y][x])\n t += -t2 + ew[y][x];\n }else{\n if(t2 >= ew[y][x])\n t += -t2 + (ew[y][x] + ns[y][x]);\n }\n }\n\n if(y == gy && x == gx && t < INF){\n cout << t << endl;\n break;\n }\n\n for(int i=0; i<2; ++i){\n int t2 = t + dy[y-1+i];\n int y2 = y - 1 + 2*i;\n int x2 = x;\n int d2 = 0;\n if(t2 < dp[y2][x2][d2]){\n dp[y2][x2][d2] = t2;\n mm.insert(make_pair(t2, make_pair(make_pair(y2, x2), d2)));\n }\n }\n for(int i=0; i<2; ++i){\n int t2 = t + dx[x-1+i];\n int y2 = y;\n int x2 = x - 1 + 2*i;\n int d2 = 1;\n if(t2 < dp[y2][x2][d2]){\n dp[y2][x2][d2] = t2;\n mm.insert(make_pair(t2, make_pair(make_pair(y2, x2), d2)));\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 2064, "score_of_the_acc": -0.4644, "final_rank": 2 }, { "submission_id": "aoj_2036_131525", "code_snippet": "#include<iostream>\n#include<cassert>\n#include<climits>\n#include<queue>\n#include<vector>\n#include<complex>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define NODE(i,j,c) ((i)*c+(j)+2)\n#define ALL(C) (C).begin(),(C).end()\ntypedef long long ll;\ntypedef complex<double> P;\n\nconst ll inf = (1LL)<<60;\n\nenum{N=0,E=1,S=0,W=1};\nenum{VER=0,HOR=1};//HOR- VER|\nchar dir[]={\"NESW\"};\n\nll check(ll t,int dir,int s,int n,int e){//need to take dir%2\ndir%=2;\n if (n+e==0)return 0;\n ll mod=n+e;\n ll tmp=t%mod;\n if (s==VER){\n if (tmp <n){\n if (dir==HOR)return n-tmp;\n else if (dir==VER)return 0;\n }else {\n if (dir==HOR)return 0;\n else if (dir==VER)return n+e-tmp;\n }\n }else if (s == HOR){\n if (tmp < e){\n if (dir==VER)return e-tmp;\n else if (dir==HOR)return 0;\n }else {\n if (dir==HOR)return e+n-tmp;\n else if (dir==VER)return 0;\n }\n }\n return -1;\n}\n\nclass state{\npublic:\n short now,dir;\n ll cost;\n bool operator<(const state&a)const{\n return cost > a.cost;\n }\n};\n\ntypedef struct{int to,dir;int cost;}Edge;\n\nconst int node =100*100+2;\nll cost[node][4];\nvector<Edge> edge[node];\nint es[node];\nint ns[node];\nint s[node];\n\nll dijkstra(int n){//src==0,dst==1\n rep(i,n)rep(j,4)cost[i][j]=inf;\n priority_queue<state> Q;\n rep(i,edge[0].size()){\n int nex=edge[0][i].to;\n int ned=edge[0][i].dir;\n cost[nex][ned]=edge[0][i].cost;\n cost[nex][ned]+=check(cost[nex][ned],ned,s[nex],ns[nex],es[nex]);\n Q.push((state){nex,ned,cost[nex][ned]});\n }\n\n while(!Q.empty()){\n state now = Q.top();Q.pop();\n if (cost[now.now][now.dir] != now.cost)continue;\n if (now.now == 1)return now.cost;\n\n rep(i,edge[now.now].size()){\n int nex=edge[now.now][i].to;\n int ned=edge[now.now][i].dir;\n ll tcost=now.cost+edge[now.now][i].cost;\n tcost+=check(tcost,ned,s[nex],ns[nex],es[nex]);\n if (tcost < cost[nex][ned]){\n\tcost[nex][ned]=tcost;\n\tQ.push((state){nex,ned,tcost});\n }\n }\n }\n return -1;\n}\n\nbool isp(double x1,double y1,double x2,double y2,double x3,double y3){\n P a(x1,y1),b(x2,y2),c(x3,y3);\n return abs(a-c)+abs(b-c) < abs(a-b)+1e-10;\n}\n\nint getindex(int a,vector<int> &b){\n rep(i,b.size()-1){\n if (b[i] <= a && a <= b[i+1])return i;\n }\n}\n\nvoid setpos(int cur,int xs,int ys,vector<int> &x,vector<int> &y){\n int r=y.size(),c=x.size();\n rep(i,x.size()){\n if (xs == x[i]){\n int tmp=getindex(ys,y);\n edge[cur].pb((Edge){NODE(tmp+1,i,c),N,y[tmp+1]-ys});\n edge[NODE(tmp+1,i,c)].pb((Edge){cur,S,y[tmp+1]-ys});\n edge[cur].pb((Edge){NODE(tmp,i,c) ,S,ys-y[tmp]});\n edge[NODE(tmp,i,c)].pb((Edge){cur,N,ys-y[tmp]});\n }\n }\n\n rep(i,y.size()){\n if (ys == y[i]){\n int tmp=getindex(xs,x);\n edge[cur].pb((Edge){NODE(i,tmp+1,c),E,x[tmp+1]-xs});\n edge[NODE(i,tmp+1,c)].pb((Edge){cur,W,x[tmp+1]-xs});\n edge[cur].pb((Edge){NODE(i,tmp,c ),W,xs-x[tmp]});\n edge[NODE(i,tmp,c)].pb((Edge){cur ,E,xs-x[tmp]});\n }\n }\n}\n\nint issame(int a,int b,vector<int> &cor,vector<int>&c,int cd){\n bool isok=false;\n rep(i,c.size()){\n if (c[i] == cd)isok=true;\n }\n if (!isok)return -1;\n rep(i,cor.size()-1){\n if (cor[i] <= a && a <= cor[i+1] &&\n\tcor[i] <= b && b <= cor[i+1])return abs(a-b);\n }\n return -1;\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && r){\n vector<int> x(c,0),y(r,0);\n REP(i,1,c)cin>>x[i],x[i]+=x[i-1];\n REP(i,1,r)cin>>y[i],y[i]+=y[i-1];\n rep(i,r*c+2)edge[i].clear();\n\n rep(i,r){\n rep(j,c){\n\tint cur=NODE(i,j,c),nex;\n\tll dist;\n\tif (i){\n\t nex=NODE(i-1,j,c);dist=y[i]-y[i-1];\n\t edge[cur].pb((Edge){nex,S,dist});\n\t}\n\tif (i+1!=r){\n\t nex=NODE(i+1,j,c);dist=y[i+1]-y[i];\n\t edge[cur].pb((Edge){nex,N,dist});\n\t}\n\tif (j){\n\t nex=NODE(i,j-1,c);dist=x[j]-x[j-1];\n\t edge[cur].pb((Edge){nex,W,dist});\n\t}\n\tif (j+1!=c){\n\t nex=NODE(i,j+1,c);dist=x[j+1]-x[j];\n\t edge[cur].pb((Edge){nex,E,dist});\n\t}\n }\n }\n\n rep(i,r){\n rep(j,c)cin>>ns[NODE(i,j,c)]>>es[NODE(i,j,c)]>>s[NODE(i,j,c)];\n }\n\n int xs,ys,xd,yd;\n cin>>xs>>ys>>xd>>yd;\n \n setpos(0,xs,ys,x,y);\n setpos(1,xd,yd,x,y);\n if (xs == xd && ys == yd){\n cout << 0 << endl;\n continue;\n }\n if (xs==xd){\n ll tmp=issame(ys,yd,y,x,xs);\n if (tmp != -1){cout << tmp << endl;continue;}\n }\n if (ys==yd){\n ll tmp=issame(xs,xd,x,y,ys);\n if (tmp != -1){cout << tmp << endl;continue;}\n }\n cout << dijkstra(r*c+2) << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 2020, "score_of_the_acc": -0.9858, "final_rank": 3 }, { "submission_id": "aoj_2036_131524", "code_snippet": "#include<iostream>\n#include<cassert>\n#include<climits>\n#include<queue>\n#include<vector>\n#include<complex>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define NODE(i,j,c) ((i)*c+(j)+2)\n#define ALL(C) (C).begin(),(C).end()\ntypedef long long ll;\ntypedef complex<double> P;\n\nconst ll inf = (1LL)<<60;\n\nenum{N=0,E=1,S=0,W=1};\nenum{VER=0,HOR=1};//HOR- VER|\nchar dir[]={\"NESW\"};\n\nll check(ll t,int dir,int s,int n,int e){//need to take dir%2\n if (n+e==0)return 0;\n ll mod=n+e;\n ll tmp=t%mod;\n if (s==VER){\n if (tmp <n){\n if (dir==HOR)return n-tmp;\n else if (dir==VER)return 0;\n }else {\n if (dir==HOR)return 0;\n else if (dir==VER)return n+e-tmp;\n }\n }else if (s == HOR){\n if (tmp < e){\n if (dir==VER)return e-tmp;\n else if (dir==HOR)return 0;\n }else {\n if (dir==HOR)return e+n-tmp;\n else if (dir==VER)return 0;\n }\n }\n return -1;\n}\n\nclass state{\npublic:\n short now,dir;\n ll cost;\n bool operator<(const state&a)const{\n return cost > a.cost;\n }\n};\n\ntypedef struct{int to,dir;int cost;}Edge;\n\nconst int node =100*100+2;\nll cost[node][4];\nvector<Edge> edge[node];\nint es[node];\nint ns[node];\nint s[node];\n\nll dijkstra(int n){//src==0,dst==1\n rep(i,n)rep(j,4)cost[i][j]=inf;\n priority_queue<state> Q;\n rep(i,edge[0].size()){\n int nex=edge[0][i].to;\n int ned=edge[0][i].dir;\n cost[nex][ned]=edge[0][i].cost;\n cost[nex][ned]+=check(cost[nex][ned],ned,s[nex],ns[nex],es[nex]);\n Q.push((state){nex,ned,cost[nex][ned]});\n }\n\n while(!Q.empty()){\n state now = Q.top();Q.pop();\n if (cost[now.now][now.dir] != now.cost)continue;\n if (now.now == 1)return now.cost;\n\n rep(i,edge[now.now].size()){\n int nex=edge[now.now][i].to;\n int ned=edge[now.now][i].dir;\n ll tcost=now.cost+edge[now.now][i].cost;\n tcost+=check(tcost,ned,s[nex],ns[nex],es[nex]);\n if (tcost < cost[nex][ned]){\n\tcost[nex][ned]=tcost;\n\tQ.push((state){nex,ned,tcost});\n }\n }\n }\n return -1;\n}\n\nbool isp(double x1,double y1,double x2,double y2,double x3,double y3){\n P a(x1,y1),b(x2,y2),c(x3,y3);\n return abs(a-c)+abs(b-c) < abs(a-b)+1e-10;\n}\n\nint getindex(int a,vector<int> &b){\n rep(i,b.size()-1){\n if (b[i] <= a && a <= b[i+1])return i;\n }\n}\n\nvoid setpos(int cur,int xs,int ys,vector<int> &x,vector<int> &y){\n int r=y.size(),c=x.size();\n rep(i,x.size()){\n if (xs == x[i]){\n int tmp=getindex(ys,y);\n edge[cur].pb((Edge){NODE(tmp+1,i,c),N,y[tmp+1]-ys});\n edge[NODE(tmp+1,i,c)].pb((Edge){cur,S,y[tmp+1]-ys});\n edge[cur].pb((Edge){NODE(tmp,i,c) ,S,ys-y[tmp]});\n edge[NODE(tmp,i,c)].pb((Edge){cur,N,ys-y[tmp]});\n }\n }\n\n rep(i,y.size()){\n if (ys == y[i]){\n int tmp=getindex(xs,x);\n edge[cur].pb((Edge){NODE(i,tmp+1,c),E,x[tmp+1]-xs});\n edge[NODE(i,tmp+1,c)].pb((Edge){cur,W,x[tmp+1]-xs});\n edge[cur].pb((Edge){NODE(i,tmp,c ),W,xs-x[tmp]});\n edge[NODE(i,tmp,c)].pb((Edge){cur ,E,xs-x[tmp]});\n }\n }\n}\n\nint issame(int a,int b,vector<int> &cor,vector<int>&c,int cd){\n bool isok=false;\n rep(i,c.size()){\n if (c[i] == cd)isok=true;\n }\n if (!isok)return -1;\n rep(i,cor.size()-1){\n if (cor[i] <= a && a <= cor[i+1] &&\n\tcor[i] <= b && b <= cor[i+1])return abs(a-b);\n }\n return -1;\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && r){\n vector<int> x(c,0),y(r,0);\n REP(i,1,c)cin>>x[i],x[i]+=x[i-1];\n REP(i,1,r)cin>>y[i],y[i]+=y[i-1];\n rep(i,r*c+2)edge[i].clear();\n\n rep(i,r){\n rep(j,c){\n\tint cur=NODE(i,j,c),nex;\n\tll dist;\n\tif (i){\n\t nex=NODE(i-1,j,c);dist=y[i]-y[i-1];\n\t edge[cur].pb((Edge){nex,S,dist});\n\t}\n\tif (i+1!=r){\n\t nex=NODE(i+1,j,c);dist=y[i+1]-y[i];\n\t edge[cur].pb((Edge){nex,N,dist});\n\t}\n\tif (j){\n\t nex=NODE(i,j-1,c);dist=x[j]-x[j-1];\n\t edge[cur].pb((Edge){nex,W,dist});\n\t}\n\tif (j+1!=c){\n\t nex=NODE(i,j+1,c);dist=x[j+1]-x[j];\n\t edge[cur].pb((Edge){nex,E,dist});\n\t}\n }\n }\n\n rep(i,r){\n rep(j,c)cin>>ns[NODE(i,j,c)]>>es[NODE(i,j,c)]>>s[NODE(i,j,c)];\n }\n\n int xs,ys,xd,yd;\n cin>>xs>>ys>>xd>>yd;\n \n setpos(0,xs,ys,x,y);\n setpos(1,xd,yd,x,y);\n if (xs == xd && ys == yd){\n cout << 0 << endl;\n continue;\n }\n if (xs==xd){\n ll tmp=issame(ys,yd,y,x,xs);\n if (tmp != -1){cout << tmp << endl;continue;}\n }\n if (ys==yd){\n ll tmp=issame(xs,xd,x,y,ys);\n if (tmp != -1){cout << tmp << endl;continue;}\n }\n cout << dijkstra(r*c+2) << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 2020, "score_of_the_acc": -0.9858, "final_rank": 3 }, { "submission_id": "aoj_2036_131515", "code_snippet": "#include<iostream>\n#include<cassert>\n#include<climits>\n#include<queue>\n#include<vector>\n#include<complex>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define NODE(i,j,c) ((i)*c+(j)+2)\n#define ALL(C) (C).begin(),(C).end()\ntypedef long long ll;\ntypedef complex<double> P;\n\nconst ll inf = (1LL)<<60;\n\nenum{N=0,E=1,S=0,W=1};\nenum{VER=0,HOR=1};//HOR- VER|\nchar dir[]={\"NESW\"};\n\nll check(ll t,int dir,int s,int n,int e){//need to take dir%2\n if (n+e==0)return 0;\n ll mod=n+e;\n ll tmp=t%mod;\n if (s==VER){\n if (tmp <n){\n if (dir==HOR)return n-tmp;\n else if (dir==VER)return 0;\n }else {\n if (dir==HOR)return 0;\n else if (dir==VER)return n+e-tmp;\n }\n }else if (s == HOR){\n if (tmp < e){\n if (dir==VER)return e-tmp;\n else if (dir==HOR)return 0;\n }else {\n if (dir==HOR)return e+n-tmp;\n else if (dir==VER)return 0;\n }\n }\n return -1;\n}\n\nclass state{\npublic:\n short now,dir;\n ll cost;\n bool operator<(const state&a)const{\n return cost > a.cost;\n }\n};\n\ntypedef struct{int to,dir;int cost;}Edge;\n\nconst int node =100*100+2;\nll cost[node][4];\nvector<Edge> edge[node];\nint es[node];\nint ns[node];\nint s[node];\n\nll dijkstra(int n){//src==0,dst==1\n rep(i,n)rep(j,4)cost[i][j]=inf;\n priority_queue<state> Q;\n rep(i,edge[0].size()){\n int nex=edge[0][i].to;\n int ned=edge[0][i].dir;\n cost[nex][ned]=edge[0][i].cost;\n cost[nex][ned]+=check(cost[nex][ned],ned,s[nex],ns[nex],es[nex]);\n Q.push((state){nex,ned,cost[nex][ned]});\n }\n\n while(!Q.empty()){\n state now = Q.top();Q.pop();\n if (cost[now.now][now.dir] != now.cost)continue;\n if (now.now == 1)return now.cost;\n // assert(cost[now.now][now.dir] >=0);\n // cout << now.now <<\" \" << dir[now.dir] <<\" \" << now.cost << endl;\n\n rep(i,edge[now.now].size()){\n int nex=edge[now.now][i].to;\n int ned=edge[now.now][i].dir;\n ll tcost=now.cost+edge[now.now][i].cost;\n //ll check(ll t,int dir,int s,int n,int e){//need to take dir%2\n // cout << now.now <<\" \" << nex <<\" \" << check(tcost,ned,s[nex],ns[nex],es[nex])<<endl;\n tcost+=check(tcost,ned,s[nex],ns[nex],es[nex]);\n if (tcost < cost[nex][ned]){\n\tcost[nex][ned]=tcost;\n\tQ.push((state){nex,ned,tcost});\n }\n }\n }\n return -1;\n}\n\nbool isp(double x1,double y1,double x2,double y2,double x3,double y3){\n P a(x1,y1),b(x2,y2),c(x3,y3);\n return abs(a-c)+abs(b-c) < abs(a-b)+1e-10;\n}\n\nint getindex(int a,vector<int> &b){\n rep(i,b.size()-1){\n if (b[i] <= a && a <= b[i+1])return i;\n }\n assert(false);\n}\n\nvoid setpos(int cur,int xs,int ys,vector<int> &x,vector<int> &y){\n int r=y.size(),c=x.size();\n rep(i,x.size()){\n if (xs == x[i]){\n int tmp=getindex(ys,y);\n edge[cur].pb((Edge){NODE(tmp+1,i,c),N,y[tmp+1]-ys});\n edge[NODE(tmp+1,i,c)].pb((Edge){cur,S,y[tmp+1]-ys});\n edge[cur].pb((Edge){NODE(tmp,i,c) ,S,ys-y[tmp]});\n edge[NODE(tmp,i,c)].pb((Edge){cur,N,ys-y[tmp]});\n }\n }\n\n rep(i,y.size()){\n if (ys == y[i]){\n int tmp=getindex(xs,x);\n edge[cur].pb((Edge){NODE(i,tmp+1,c),E,x[tmp+1]-xs});\n edge[NODE(i,tmp+1,c)].pb((Edge){cur,W,x[tmp+1]-xs});\n edge[cur].pb((Edge){NODE(i,tmp,c ),W,xs-x[tmp]});\n edge[NODE(i,tmp,c)].pb((Edge){cur ,E,xs-x[tmp]});\n }\n }\n}\n\nint issame(int a,int b,vector<int> &cor,vector<int>&c,int cd){\n bool isok=false;\n rep(i,c.size()){\n if (c[i] == cd)isok=true;\n }\n if (!isok)return -1;\n rep(i,cor.size()-1){\n if (cor[i] <= a && a <= cor[i+1] &&\n\tcor[i] <= b && b <= cor[i+1])return abs(a-b);\n }\n return -1;\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && r){\n vector<int> x(c,0),y(r,0);\n REP(i,1,c)cin>>x[i],x[i]+=x[i-1];\n REP(i,1,r)cin>>y[i],y[i]+=y[i-1];\n rep(i,r*c+2)edge[i].clear();\n\n rep(i,r){\n rep(j,c){\n\tint cur=NODE(i,j,c),nex;\n\tll dist;\n\tif (i){\n\t nex=NODE(i-1,j,c);dist=y[i]-y[i-1];\n\t edge[cur].pb((Edge){nex,S,dist});\n\t}\n\tif (i+1!=r){\n\t nex=NODE(i+1,j,c);dist=y[i+1]-y[i];\n\t edge[cur].pb((Edge){nex,N,dist});\n\t}\n\tif (j){\n\t nex=NODE(i,j-1,c);dist=x[j]-x[j-1];\n\t edge[cur].pb((Edge){nex,W,dist});\n\t}\n\tif (j+1!=c){\n\t nex=NODE(i,j+1,c);dist=x[j+1]-x[j];\n\t edge[cur].pb((Edge){nex,E,dist});\n\t}\n }\n }\n\n rep(i,r){\n rep(j,c)cin>>ns[NODE(i,j,c)]>>es[NODE(i,j,c)]>>s[NODE(i,j,c)];\n }\n\n int xs,ys,xd,yd;\n cin>>xs>>ys>>xd>>yd;\n \n setpos(0,xs,ys,x,y);\n setpos(1,xd,yd,x,y);\n if (xs == xd && ys == yd){\n cout << 0 << endl;\n continue;\n }\n if (xs==xd){\n ll tmp=issame(ys,yd,y,x,xs);\n if (tmp != -1){cout << tmp << endl;continue;}\n }\n if (ys==yd){\n ll tmp=issame(xs,xd,x,y,ys);\n if (tmp != -1){cout << tmp << endl;continue;}\n }\n cout << dijkstra(r*c+2) << endl;\n\n// rep(i,x.size())cout << x[i]<<\" \";cout <<endl;\n// rep(i,y.size())cout<<y[i]<<\" \";cout << endl;\n// rep(i,r*c+2){\n// cout << \"now \";\n// if (i == 0)cout <<0 <<\"(\" << xs <<\",\" << ys <<\")\";\n// else if (i == 1)cout << 1 <<\"(\" << xd <<\",\" << yd <<\")\";\n// else cout <<i <<\"(\" << x[(i-2)%c] <<\",\" << y[(i-2)/c] << \")\";\n// cout << endl;\n// rep(j,edge[i].size()){\n// \tcout << edge[i][j].to<<\" \" ;\n// \tif (edge[i][j].to==0)cout << xs <<\" \" << ys <<\" \";\n// \telse if(edge[i][j].to == 1)cout <<xd <<\" \" << yd <<\" \";\n// \telse cout << \"(\" << x[(edge[i][j].to-2)%c] <<\",\" << y[(edge[i][j].to-2)/c] <<\")\";\n// \tcout << edge[i][j].cost <<\" \" << dir[edge[i][j].dir] << endl;\n// }\n// cout << endl;\n// }\n\n }\n return false;\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 2016, "score_of_the_acc": -1, "final_rank": 5 } ]
aoj_2030_cpp
Problem A: Ruins In 1936, a dictator Hiedler who aimed at world domination had a deep obsession with the Lost Ark . A person with this ark would gain mystic power according to legend. To break the ambition of the dictator, ACM (the Alliance of Crusaders against Mazis) entrusted a secret task to an archeologist Indiana Johns. Indiana stepped into a vast and harsh desert to find the ark. Indiana finally found an underground treasure house at a ruin in the desert. The treasure house seems storing the ark. However, the door to the treasure house has a special lock, and it is not easy to open the door. To open the door, he should solve a problem raised by two positive integers a and b inscribed on the door. The problem requires him to find the minimum sum of squares of differences for all pairs of two integers adjacent in a sorted sequence that contains four positive integers a 1 , a 2 , b 1 and b 2 such that a = a 1 a 2 and b = b 1 b 2 . Note that these four integers does not need to be different. For instance, suppose 33 and 40 are inscribed on the door, he would have a sequence 3, 5, 8, 11 as 33 and 40 can be decomposed into 3 × 11 and 5 × 8 respectively. This sequence gives the sum (5 - 3) 2 + (8 - 5) 2 + (11 - 8) 2 = 22, which is the smallest sum among all possible sorted sequences. This example is included as the first data set in the sample input. Once Indiana fails to give the correct solution, he will suffer a calamity by a curse. On the other hand, he needs to solve the problem as soon as possible, since many pawns under Hiedler are also searching for the ark, and they might find the same treasure house. He will be immediately killed if this situation happens. So he decided to solve the problem by your computer program. Your task is to write a program to solve the problem presented above. Input The input consists of a series of data sets. Each data set is given by a line that contains two positive integers not greater than 10,000. The end of the input is represented by a pair of zeros, which should not be processed. Output For each data set, print a single integer that represents the minimum square sum in a line. No extra space or text should appear. Sample Input 33 40 57 144 0 0 Output for the Sample Input 22 94
[ { "submission_id": "aoj_2030_2645406", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nvector<long long int> divisor(long long int n) {\n\tvector<long long int> res;\n\tfor (long long int i = 1; i * i <= n; ++i) {\n\t\tif (n % i == 0) {\n\t\t\tres.push_back(i);\n\t\t\tif (i * i != n) res.push_back(n / i);\n\t\t}\n\t}\n\tsort(begin(res), end(res));\n\treturn res;\n}\nint main() {\n\twhile (true) {\n\t\tint a,b;cin>>a>>b;\n\t\tif(!a)break;\n\t\tauto ad(divisor(a));\n\t\tauto bd(divisor(b));\n\t\tlong long int ans=1e18;\n\t\tfor (auto aa : ad) {\n\t\t\tfor (auto bb : bd) {\n\t\t\t\tvector<long long int>nums{ aa,a / aa,bb,b / bb };\n\t\t\t\tsort(nums.begin(),nums.end());\n\t\t\t\tlong long int nans=0;\n\t\t\t\tfor(int i=0;i<3;++i)nans+=(nums[i+1]-nums[i])*(nums[i+1]-nums[i]);\n\n\t\t\t\tans=min(ans,nans);\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3116, "score_of_the_acc": -0.9757, "final_rank": 18 }, { "submission_id": "aoj_2030_2196634", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,a,n) for(int i=a;i<n;i++)\n#define R(i,a,n) for(int i=a;i>n;i--)\nusing namespace std ;\nint main(){\n int a,b;\n while(cin>>a>>b,a){\n int aa[5001][2],bb[5001][2],c=0,cc=0;\n bool ac[10001],bc[10001];\n r(i,0,10001)ac[i]=0;\n r(i,0,10001)bc[i]=0;\n r(i,1,a+1){\n if(!ac[i]&&a%i==0)R(j,a,0){\n\tif(i*j==a){\n\t aa[c][0]=i;\n\t ac[i]=1;ac[j]=1;\n\t aa[c++][1]=j;\n\t break;\n\t}\n }\n }\n r(i,1,b+1){\n if(!bc[i]&&b%i==0)R(j,b,0){\n\tif(i*j==b){\n\t bb[cc][0]=i;\n\t bc[i]=1;bc[j]=1;\n\t bb[cc++][1]=j;\n\t break;\n\t}\n }\n }//cout<<bb[0][0]<<bb[0][1];\n long long l[7];\n long long m=999999999999;\n r(i,0,c){\n r(j,0,cc){\n\tint t[4];\n\tt[0]=aa[i][0];t[1]=aa[i][1];\n\tt[2]=bb[j][0];t[3]=bb[j][1];\n\tsort(t,t+4);\n\tl[0]=t[1]-t[0];\n\tl[1]=t[2]-t[1];\n\tl[2]=t[3]-t[2];\n\tm=min(m,l[0]*l[0]+l[1]*l[1]+l[2]*l[2]);\n }\n }\n //if(m==100000000)m=0;\n cout<<m<<endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3172, "score_of_the_acc": -1.012, "final_rank": 19 }, { "submission_id": "aoj_2030_2196503", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n while(1){\n ll A,B;\n cin>>A>>B;\n if(!A&&!B) break;\n\n ll sum = 1LL<<55;\n for(int i=1;i*i<=A;i++){\n for(int j=1;j*j<=B;j++){\n\tif(A%i||B%j) continue;\n\tvector<int> v(4);\n\tv[0] = i;\n\tv[1] = A/i;\n\tv[2] = j;\n\tv[3] = B/j;\n\tsort(v.begin(),v.end());\n\tll t = 0;\n\tfor(int i=1;i<4;i++)t+=(v[i]-v[i-1])*(v[i]-v[i-1]);\n\tsum = min(sum,t);\n }\n }\n cout<<sum<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3044, "score_of_the_acc": -0.9505, "final_rank": 17 }, { "submission_id": "aoj_2030_1781113", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint main(){\n\tint n,m;\n\twhile(cin>>n>>m,n+m){\n\t\tvi a,b;\n\t\tfor(int i=1;i*i<=n;i++)if(n%i==0)a.pb(i);\n\t\tfor(int i=1;i*i<=m;i++)if(m%i==0)b.pb(i);\n\t\tint mi=inf;\n\t\trep(i,a.size())rep(j,b.size()){\n\t\t\tvi t;\n\t\t\tt.pb(a[i]);t.pb(n/a[i]);t.pb(b[j]);t.pb(m/b[j]);\n\t\t\tsort(all(t));\n\t\t\tint c=t[1]-t[0];\n\t\t\tint d=t[2]-t[1];\n\t\t\tint e=t[3]-t[2];\n\t\t\tmi=min(mi,c*c+d*d+e*e);\n\t\t}\n\t\tcout<<mi<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1220, "score_of_the_acc": -0.1528, "final_rank": 7 }, { "submission_id": "aoj_2030_1030299", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\nusing namespace std;\ntypedef pair<int,int>P;\n#define X first\n#define Y second\n#define pb push_back\nvector<P>V[10001];\n\nvoid V_set(){\n\tfor(int i=1;i<=10000;i++){\n\t\tfor(int j=1;j*j<=i;j++){\n\t\t\tif(i%j==0){\n\t\t\t\tV[i].pb(P(j,i/j));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint cal(P a,P b){\n\tint d[4]={a.X,a.Y,b.X,b.Y};\n\tsort(d,d+4);\n\tint t=0;\n\tfor(int i=0;i<3;i++){\n\t\tt+=(d[i+1]-d[i])*(d[i+1]-d[i]);\n\t}\n\treturn t;\n}\n\nint main(){\n\t\n\tV_set();\n\t\n\tint a,b;\n\twhile(cin>>a>>b,a){\n\t\tint ans=999999999;\n\t\tfor(int i=0;i<V[a].size();i++){\n\t\t\tfor(int j=0;j<V[b].size();j++){\n\t\t\t\tans=min(ans,cal(V[a][i],V[b][j]));\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\t\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2052, "score_of_the_acc": -0.5139, "final_rank": 15 }, { "submission_id": "aoj_2030_941092", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s << endl; }\n\n\n\nvector<pair<long long, long long> > divisor(long long n) {\n vector<pair<long long, long long> > res;\n for (long long i = 1LL; i*i <= n; ++i) {\n if (n%i == 0LL) {\n long long temp = n/i;\n res.push_back(make_pair(i, temp));\n //if (i != temp) res.push_back(make_pair(temp, i));\n }\n }\n return res;\n}\n\nlong long sq(long long a) { return a*a; }\n\nint a, b;\n\nint main() {\n while (cin >> a >> b) {\n if (a == 0 && b == 0) break;\n \n long long res = 1LL<<60;\n vector<pll> va = divisor(a), vb = divisor(b);\n for (int i = 0; i < va.size(); ++i) {\n for (int j = 0; j < vb.size(); ++j) {\n vll vec(4);\n vec[0] = va[i].first, vec[1] = va[i].second, vec[2] = vb[j].first, vec[3] = vb[j].second;\n sort(ALL(vec));\n \n ll tmp = sq(vec[1] - vec[0]) + sq(vec[2] - vec[1]) + sq(vec[3] - vec[2]);\n chmin(res, tmp);\n }\n }\n \n cout << res << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.1493, "final_rank": 6 }, { "submission_id": "aoj_2030_850022", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\ntypedef pair<int,int> P;\n#define p(a,b) (b-a)*(b-a)\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n\nint compute(int a,int b,int c,int d){\n int e[4] = {a,b,c,d};\n int res = (1<<29);\n\n rep(i,4){\n rep(j,4){\n if(i == j) continue;\n rep(k,4){\n\tif(i == k || j == k) continue;\n\trep(l,4){\n\t if(i == l || j == l || k == l) continue;\n\t res = min(res,p(e[j],e[i])+p(e[k],e[j])+p(e[l],e[k]));\n\t}\n }\n }\n }\n \n return res;\n}\n\nint main(){\n int a,b;\n\n while(cin >> a >> b , a | b){\n vector<P> c,d;\n for(int i = 1 ; i*i <= a ; i++){\n if(a % i == 0){\n\tc.push_back(P(i,a/i));\n } \n }\n\n for(int i = 1 ; i*i <= b ; i++){\n if(b % i == 0){\n\td.push_back(P(i,b/i));\n }\n }\n\n int ans = (1<<29);\n rep(i,(int)c.size()){\n rep(j,(int)d.size()){\n\tans = min(ans,compute(c[i].first,c[i].second,d[j].first,d[j].second));\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1204, "score_of_the_acc": -0.1458, "final_rank": 5 }, { "submission_id": "aoj_2030_850021", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef long long ll;\n#define p(a,b) (b-a)*(b-a)\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n\nll compute(int a,int b,int c,int d){\n int e[4] = {a,b,c,d};\n int res = (1<<29);\n\n rep(i,4){\n rep(j,4){\n if(i == j) continue;\n rep(k,4){\n\tif(i == k || j == k) continue;\n\trep(l,4){\n\t if(i == l || j == l || k == l) continue;\n\t res = min(res,p(e[j],e[i])+p(e[k],e[j])+p(e[l],e[k]));\n\t}\n }\n }\n }\n \n return res;\n}\n\nint main(){\n int a,b;\n\n while(cin >> a >> b , a | b){\n vector<P> c,d;\n for(int i = 1 ; i*i <= a ; i++){\n if(a % i == 0){\n\tc.push_back(P(i,a/i));\n } \n }\n\n for(int i = 1 ; i*i <= b ; i++){\n if(b % i == 0){\n\td.push_back(P(i,b/i));\n }\n }\n\n ll ans = (1<<29);\n rep(i,(int)c.size()){\n rep(j,(int)d.size()){\n\tans = min(ans,compute(c[i].first,c[i].second,d[j].first,d[j].second));\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1196, "score_of_the_acc": -0.1424, "final_rank": 4 }, { "submission_id": "aoj_2030_503600", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<cstdlib>\n#include<algorithm>\n#include<cstring>\n#include<map>\n#include<cassert>\n#include<ctime>\n#include<set>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define INF 1<<29\n#define all(n) n.begin(),n.end()\nusing namespace std;\nint a,b;\n \nint main(){\n \n while(true){\n cin >> a >> b;\n if(a+b == 0)break;\n int ans = (1<<27);\n assert(a>=0 && b>=0);\n for(int i=1;i<=a/2+1;i++){\n if(a%i != 0)continue;\n vector<int> vec;\n //cout << \"a = \" << i << \",\" << a/i << \" \";\n vec.push_back(i);vec.push_back((int)(a/i));\n for(int j=1;j<=b/2+1;j++){\n if(b%j != 0)continue;\n //cout << \"b = \" << j << \",\" << b/j << endl;\n vec.push_back(j); vec.push_back(b/j);\n sort(all(vec));\n //for(int k=0;k<4;k++){\n //cout << vec[k] << endl;\n //}\n //cout << \"---\" << endl;\n int sum = 0;\n sum = (vec[1]-vec[0])*(vec[1]-vec[0]) + (vec[2]-vec[1])*(vec[2]-vec[1]) + (vec[3]-vec[2])*(vec[3]-vec[2]);\n //cout << \"sum - \" << sum << endl;\n ans = min(ans,sum);\n vec.clear();\n vec.push_back(i); vec.push_back(a/i);\n }\n }\n \n cout << ans << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1216, "score_of_the_acc": -0.1781, "final_rank": 11 }, { "submission_id": "aoj_2030_503467", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint A,B,ans;\nint data[4];\n\nvoid solve(){\n for(int i = 1; i <= A; i++)\n if(A%i == 0)\n for(int j = 1; j <= B; j++)\n\tif(B%j == 0){\n\t data[0] = i;\n\t data[1] = A/i;\n\t data[2] = j;\n\t data[3] = B/j;\n\t sort(data,data+4);\n\n\n\t int sum = 0;\n\t for(int i = 1; i < 4; i++)\n\t sum += (data[i]-data[i-1])*(data[i]-data[i-1]);\n\t \n\t if(ans == -1) ans = sum; \n\t else ans = min(ans,sum);\n\t \n\t}\n\n cout << ans << endl;\n}\n\nint main(){\n while(cin >> A >> B && A+B){\n ans = -1;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 1176, "score_of_the_acc": -0.1967, "final_rank": 13 }, { "submission_id": "aoj_2030_503462", "code_snippet": "#include<iostream>\n#include<map>\n#include<vector>\n#include<algorithm>\n\nusing namespace std;\n\nmap<int, int> MakePair(int);\nint Calculator(int, int, int, int);\n\nint main(){\n int n, m, min, _min;\n map<int, int> data_n, data_m;\n map<int, int>::iterator i, j;\n\n while(1){\n cin >> n >> m;\n if(n == 0 && m == 0) break;\n\n data_n = MakePair(n);\n data_m = MakePair(m);\n\n for(i=data_n.begin(), min=-1; i!=data_n.end(); ++i){\n for(j=data_m.begin(); j!=data_m.end(); ++j){\n\t_min = Calculator(i->first, i->second, j->first, j->second);\n\tif(min < 0 || min > _min) min = _min;\n }\n }\n cout << min << endl;\n data_n.clear();\n data_m.clear();\n }\n return 0;\n}\n\nmap<int, int> MakePair(int n){\n int i, j;\n map<int, int> data;\n if(n == 1){\n data.insert(make_pair(1, 1));\n }else{\n for(i=1, j=n; i<j; ++i){\n if(n % i == 0){\n\tj = n/i;\n\tdata.insert(make_pair(i, j));\n }\n }\n }\n return data;\n}\n\nint Calculator(int a, int b, int c, int d){\n int x, y, z;\n vector<int> data;\n data.push_back(a);\n data.push_back(b);\n data.push_back(c);\n data.push_back(d);\n sort(data.begin(), data.end());\n x = data[1] - data[0];\n y = data[2] - data[1];\n z = data[3] - data[2];\n return x*x + y*y + z*z;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1220, "score_of_the_acc": -0.1528, "final_rank": 7 }, { "submission_id": "aoj_2030_503459", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <vector>\n\nusing namespace std;\n\n#define f first\n#define s second\n\ntypedef pair<int, int> P;\ntypedef pair<int, P> P2;\n\nint main(){\n int a, b;\n while(cin >> a >> b){\n if(a == 0 && b == 0) break;\n P2 tmp;\n vector<P> aa;\n for(int i = 1 ; i <= a ; i++){\n if(a % i == 0){\n\taa.push_back(make_pair(i, a/i));\n }\n }\n vector<P> bb;\n for(int i = 1 ; i <= b ; i++){\n if(b % i == 0){\n\tbb.push_back(make_pair(i, b/i));\n }\n }\n\n int ans = (1<<29);\n int res[4];\n for(int i = 0 ; i < aa.size() ; i++){\n for(int j = 0 ; j < bb.size() ; j++){\n\tres[0] = aa[i].f, res[1] = aa[i].s;\n\tres[2] = bb[j].f, res[3] = bb[j].s;\n\tsort(res, res+4);\n\tint t = 0;\n\tfor(int i = 0 ; i < 3 ; i++){\n\t t += (res[i]-res[i+1]) * (res[i]-res[i+1]);\n\t}\n\tans = min(ans, t);\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1216, "score_of_the_acc": -0.1601, "final_rank": 10 }, { "submission_id": "aoj_2030_503450", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<climits>\n\nusing namespace std;\n\nint func(vector<int> num){\n int ans=0;\n for(int i=0;i<num.size()-1;i++)\n ans+=(num[i+1]-num[i])*(num[i+1]-num[i]);\n return ans;\n}\n\n\nint main(void){\n\n int a,b,tmp;\n vector<int>num;\n\n while(cin >> a >> b,a|b){\n int res=INT_MAX;\n for(int i=1;i*i<=a;i++)\n for(int j=1;j*j<=b;j++)\n\tif(a%i==0 && b%j==0){\n\t num.clear();\n\t num.push_back(a/i);\n\t num.push_back(i);\n\t num.push_back(b/j);\n\t num.push_back(j);\n\t sort(num.begin(),num.end());\n\t res=min(res,func(num));\n\t}\n\n cout << res << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1216, "score_of_the_acc": -0.154, "final_rank": 9 }, { "submission_id": "aoj_2030_503377", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint A,B,ans;\nint data[4];\n\nvoid solve(){\n for(int i = 1; i <= A; i++)\n if(A%i == 0)\n for(int j = 1; j <= B; j++)\n\tif(B%j == 0){\n\t data[0] = i;\n\t data[1] = A/i;\n\t data[2] = j;\n\t data[3] = B/j;\n\t sort(data,data+4);\n\n\t do{\n\t int sum = 0;\n\t for(int i = 1; i < 4; i++)\n\t sum += (data[i]-data[i-1])*(data[i]-data[i-1]);\n\t \n\t if(ans == -1) ans = sum; \n\t else ans = min(ans,sum);\n\t }while(next_permutation(data,data+4));\n\t}\n\n cout << ans << endl;\n}\n\nint main(){\n while(cin >> A >> B && A+B){\n ans = -1;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 1176, "score_of_the_acc": -0.2058, "final_rank": 14 }, { "submission_id": "aoj_2030_503346", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<cstdlib>\n#include<algorithm>\n#include<cstring>\n#include<map>\n#include<cassert>\n#include<ctime>\n#include<set>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define INF 1<<29\n#define all(n) n.begin(),n.end()\nusing namespace std;\nint a,b;\n\nint main(){\n \n while(true){\n cin >> a >> b;\n if(a+b == 0)break;\n int ans = (1<<27);\n assert(a>=0 && b>=0);\n for(int i=1;i<=a/2+1;i++){\n if(a%i != 0)continue;\n vector<int> vec;\n //cout << \"a = \" << i << \",\" << a/i << \" \";\n vec.push_back(i);vec.push_back((int)(a/i));\n for(int j=1;j<=b/2+1;j++){\n\tif(b%j != 0)continue;\n\t//cout << \"b = \" << j << \",\" << b/j << endl;\n\tvec.push_back(j); vec.push_back(b/j);\n\tsort(all(vec));\n\t//for(int k=0;k<4;k++){\n\t//cout << vec[k] << endl;\n\t//}\n\t//cout << \"---\" << endl;\n\tint sum = 0;\n\tsum = (vec[1]-vec[0])*(vec[1]-vec[0]) + (vec[2]-vec[1])*(vec[2]-vec[1]) + (vec[3]-vec[2])*(vec[3]-vec[2]);\n\t//cout << \"sum - \" << sum << endl;\n\tans = min(ans,sum);\n\tvec.clear();\n\tvec.push_back(i); vec.push_back(a/i);\n }\n }\n\n cout << ans << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 1212, "score_of_the_acc": -0.1793, "final_rank": 12 }, { "submission_id": "aoj_2030_482252", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\nstruct P{\n\tint a, b;\n\tP(int a_, int b_){ a = a_; b = b_; }\n\tP(){}\n};\n\nconst int INF = 1e+8;\nvector<P> h[10001];\n\nint main(){\n\tfor(int a=1 ; a <= 10000 ; a++ ){\n\t\tfor(int b=1 ; b <= 10000 && a * b <= 10000 ; b++ ){\n\t\t\th[a*b].push_back( P(a,b) );\n\t\t}\n\t}\n\tint x, y;\n\twhile( cin >> x >> y , x || y ){\n\t\tint ans = INF;\n\t\tfor(int i=0 ; i < h[x].size() ; i++ ){\n\t\t\tfor(int j=0 ; j < h[y].size() ; j++ ){\n\t\t\t\tvector<int> v;\n\t\t\t\tv.push_back( h[x][i].a );\n\t\t\t\tv.push_back( h[x][i].b );\n\t\t\t\tv.push_back( h[y][j].a );\n\t\t\t\tv.push_back( h[y][j].b );\n\t\t\t\tsort( v.begin() , v.end() );\n\t\t\t\tint n=0;\n\t\t\t\tfor(int i=1 ; i < v.size() ; i++ ){\n\t\t\t\t\tn += (v[i-1] - v[i]) * (v[i-1] - v[i]);\n\t\t\t\t}\n\t\t\t\tans = min( ans , n );\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 2624, "score_of_the_acc": -0.7682, "final_rank": 16 }, { "submission_id": "aoj_2030_349074", "code_snippet": "#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nconst int INF = 1000000000;\n\nmain(){\n int a, b;\n while(cin >> a >> b && (a|b)){\n int ans = INF;\n for(int i=1;i<=a;i++){\n if(a%i != 0) continue;\n for(int j=1;j<=b;j++){\n if(b%j != 0) continue;\n int tmp[4] = {i, a/i, j, b/j};\n sort(tmp, tmp+4);\n int sum = 0;\n for(int i=1;i<4;i++){\n sum += (tmp[i] - tmp[i-1]) * (tmp[i] - tmp[i-1]);\n }\n ans = min(ans, sum);\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 868, "score_of_the_acc": -0.0601, "final_rank": 3 }, { "submission_id": "aoj_2030_225660", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n#define REP(i,n) for (int i=0;i<(int)n; ++i)\n\n\ntypedef pair<int,int> pii;\nint main() {\n int a, b;\n while(cin >> a>>b,a||b) {\n vector<pii> v1, v2;\n for (int i=1; i*i<=a; ++i) {\n if (a%i == 0) {\n v1.push_back(pii(i, a/i));\n }\n }\n for (int i=1; i*i<=b; ++i) {\n if (b%i == 0) {\n v2.push_back(pii(i, b/i));\n }\n }\n int res = 1<<29;\n REP(i,v1.size()) {\n REP(j, v2.size()) {\n vector<int> tmp;\n tmp.push_back(v1[i].first);\n tmp.push_back(v1[i].second);\n tmp.push_back(v2[j].first);\n tmp.push_back(v2[j].second);\n sort(tmp.begin(), tmp.end());\n int aa = 0;\n REP(k,3)\n aa += (tmp[k+1]-tmp[k]) * (tmp[k+1]-tmp[k]);\n res = min(res, aa);\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 908, "score_of_the_acc": -0.0204, "final_rank": 1 }, { "submission_id": "aoj_2030_177028", "code_snippet": "#include <iostream>\n#include <set>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n int a,b;\n while(cin>>a>>b, a|b) {\n set<int> fa,fb;\n for(int i=1; i*i<=a; ++i)\n if(a%i == 0) fa.insert(i), fa.insert(a/i);\n\n for(int i=1; i*i<=b; ++i)\n if(b%i == 0) fb.insert(i), fb.insert(b/i);\n\n int ans = 1<<29,t;\n int ar[4];\n\n typedef set<int>::iterator sit;\n for(sit i = fa.begin(); i != fa.end(); ++i)\n for(sit j = fa.begin(); j != fa.end(); ++j)\n for(sit k = fb.begin(); k != fb.end(); ++k)\n for(sit l = fb.begin(); l != fb.end(); ++l) {\n if((*i)*(*j) != a || (*k)*(*l) != b) continue;\n ar[0] = *i,ar[1] = *j, ar[2] = *k,ar[3] = *l,t = 0;\n sort(ar, ar+4);\n for(int m=1; m<4; ++m)\n t += (ar[m]-ar[m-1])*(ar[m]-ar[m-1]);\n ans = min(ans, t);\n }\n\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 3340, "memory_kb": 912, "score_of_the_acc": -1.0191, "final_rank": 20 }, { "submission_id": "aoj_2030_171201", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nconst int inf = 99999999;\n\nint func(int a[4])\n{\n\tint t=0;\n\t\n\tfor(int i = 0; i < 3; i++){\n\t\tt += ((a[i+1]-a[i])*(a[i+1]-a[i]));\n\t}\n\t\n\treturn t;\n}\n\nint main()\n{\n\tint n, m;\n\t\n\twhile(cin>>n>>m && n && m){\n\t\tvector<int> vn, vm;\n\t\t\n\t\tfor(int i = 1; i <= n; i++){\n\t\t\tif(n%i == 0){\n\t\t\t\tvn.push_back(i);\n\t\t\t}\n\t\t}\n\t\tfor(int i = 1; i <= m; i++){\n\t\t\tif(m%i == 0){\n\t\t\t\tvm.push_back(i);\n\t\t\t}\n\t\t}\n\t\tint s = inf;\n\t\tfor(int i = 0; i < vn.size(); i++){\n\t\t\tfor(int j = 0; j < vm.size(); j++){\n\t\t\t\tint a[4];\n\t\t\t\ta[0] = vn[i];\n\t\t\t\ta[1] = n/vn[i];\n\t\t\t\ta[2] = vm[j];\n\t\t\t\ta[3] = m/vm[j];\n\t\t\t\tsort(a, a+4);\n\t\t\t\ts = min(s, func(a));\n\t\t\t}\n\t\t}\n\t\t\n\t\tcout << s << endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 908, "score_of_the_acc": -0.0264, "final_rank": 2 } ]
aoj_2034_cpp
Problem E: Autocorrelation Function Nathan O. Davis is taking a class of signal processing as a student in engineering. Today’s topic of the class was autocorrelation. It is a mathematical tool for analysis of signals represented by functions or series of values. Autocorrelation gives correlation of a signal with itself. For a continuous real function f ( x ), the autocorrelation function R f ( r ) is given by where r is a real number. The professor teaching in the class presented an assignment today. This assignment requires students to make plots of the autocorrelation functions for given functions. Each function is piecewise linear and continuous for all real numbers. The figure below depicts one of those functions. Figure 1: An Example of Given Functions The professor suggested use of computer programs, but unfortunately Nathan hates programming. So he calls you for help, as he knows you are a great programmer. You are requested to write a program that computes the value of the autocorrelation function where a function f ( x ) and a parameter r are given as the input. Since he is good at utilization of existing software, he could finish his assignment with your program. Input The input consists of a series of data sets. The first line of each data set contains an integer n (3 ≤ n ≤ 100) and a real number r (-100 ≤ r ≤ 100), where n denotes the number of endpoints forming sub-intervals in the function f ( x ), and r gives the parameter of the autocorrelation function R f ( r ). The i -th of the following n lines contains two integers x i (-100 ≤ x i ≤ 100) and y i (-50 ≤ y i ≤ 50), where ( x i , y i ) is the coordinates of the i -th endpoint. The endpoints are given in increasing order of their x -coordinates, and no couple of endpoints shares the same x -coordinate value. The y -coordinates of the first and last endpoints are always zero. The end of input is indicated by n = r = 0. This is not part of data sets, and hence should not be processed. Output For each data set, your program should print the value of the autocorrelation function in a line. Each value may be printed with an arbitrary number of digits after the decimal point, but should not contain an error greater than 10 -4 . Sample Input 3 1 0 0 1 1 2 0 11 1.5 0 0 2 7 5 3 7 8 10 -5 13 4 15 -1 17 3 20 -7 23 9 24 0 0 0 Output for the Sample Input 0.166666666666667 165.213541666667
[ { "submission_id": "aoj_2034_10794058", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n cout.setf(ios::fixed);\n cout << setprecision(15);\n\n int n;\n long double r;\n while ( (cin >> n >> r) ) {\n if (n == 0 && fabsl(r) == 0.0L) break;\n\n vector<long double> x(n), y(n);\n for (int i = 0; i < n; ++i) {\n long double xi, yi;\n cin >> xi >> yi;\n x[i] = xi; y[i] = yi;\n }\n\n // Предполагается: f(x)=0 вне [x0, x_{n-1}] (y на концах равно 0).\n // Рассматриваем только область перекрытия x и x+r.\n const long double EPS = 1e-12L;\n\n long double L = max(x.front(), x.front() - r);\n long double R = min(x.back(), x.back() - r);\n\n if (L >= R - EPS) {\n cout << 0.0 << \"\\n\";\n continue;\n }\n\n // Считаем наклоны и свободные члены на каждом сегменте: f(x)=m[i]*x + c[i] на [x[i], x[i+1]]\n int segs = n - 1;\n vector<long double> m(segs), c(segs);\n for (int i = 0; i < segs; ++i) {\n m[i] = (y[i+1] - y[i]) / (x[i+1] - x[i]);\n c[i] = y[i] - m[i] * x[i];\n }\n\n // Функции для поиска сегмента, которому принадлежит точка t (x[i] <= t <= x[i+1]).\n auto find_segment = [&](long double t) -> int {\n // upper_bound даст первый x[k] > t, отнимаем 1\n int idx = int(upper_bound(x.begin(), x.end(), t) - x.begin()) - 1;\n if (idx < 0) idx = 0;\n if (idx >= segs) idx = segs - 1; // на правой границе попадём сюда, но дальше мы ограничиваемся R\n return idx;\n };\n\n int i = find_segment(L); // сегмент f(x) при x=L\n int j = find_segment(L + r); // сегмент f(x+r) при x=L\n\n long double cur = L;\n long double ans = 0.0L;\n\n while (cur < R - EPS) {\n // следующий излом для x или для (x+r)\n long double next_i = x[i+1]; // граница для f(x)\n long double next_j = x[j+1] - r; // граница для f(x+r)\n long double nxt = min({next_i, next_j, R});\n\n long double a = cur, b = nxt;\n if (b > a + EPS) {\n // На [a,b] имеем f(x)=m[i]*x + c[i], f(x+r)=m[j]*(x+r)+c[j] = m[j]*x + (m[j]*r + c[j]).\n long double A = m[i] * m[j];\n long double B = m[i] * (m[j]*r + c[j]) + c[i] * m[j];\n long double C = c[i] * (m[j]*r + c[j]);\n // Интеграл A x^2 + B x + C\n long double I = A * ( (b*b*b - a*a*a) / 3.0L )\n + B * ( (b*b - a*a ) / 2.0L )\n + C * ( b - a );\n ans += I;\n }\n\n cur = b;\n\n // Перейти на следующий сегмент(ы) при необходимости\n if (fabsl(cur - next_i) <= EPS && i+1 < segs) ++i;\n if (fabsl(cur - next_j) <= EPS && j+1 < segs) ++j;\n // защита от возможных застреваний из-за численных ошибок\n if (nxt == cur) {\n // подвинем на микро-шаг\n cur += 1e-13L;\n }\n }\n\n cout << ans << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3604, "score_of_the_acc": -1, "final_rank": 13 }, { "submission_id": "aoj_2034_1684647", "code_snippet": "#include <bits/stdc++.h>\n\n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=int(a);i<int(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n\n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n\n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n\ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\ntemplate<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}\n\nusing namespace std;\nusing R=long double;\nconst R eps=1e-11; // [-1000:1000]->EPS=1e-8 [-10000:10000]->EPS=1e-7\nconst R pi=acos(-1);\nconst R inf=1e40;\ninline int sgn(const R& r){ return (r > eps) - (r < -eps);}\ninline int sgn(const R& a, const R &b){ return sgn(a-b); }\ninline R sq(R x){return sqrt(max<R>(x,0.0));}\n\nint n;\nR vx[110],vy[110],r;\n\nR val(R x){\n\tR ret=0.0;\n\trep(i,n-1){\n\t\tif(vx[i]<=x&&x<vx[i+1]){\n\t\t\tR rate=(x-vx[i])/(vx[i+1]-vx[i]);\n\t\t\tret=vy[i]+rate*(vy[i+1]-vy[i]);\n\t\t\treturn ret;\n\t\t}\n\t}\n\treturn ret;\n}\n\nR func(R x){ return 1.0*val(x)*val(x+r); }\n\nint main(void){\n\twhile(cin >> n >> r,n){\t\n\t\tvector<R> ary;\n\t\trep(i,n) cin >> vx[i] >> vy[i];\n\t\trep(i,n) ary.push_back(vx[i]),ary.push_back(vx[i]+r),ary.push_back(vx[i]-r);\n\t\t\n\t\tuniq(ary);\n\t\tint m=ary.size();\n\n\t\tR ans=0.0;\n\t\trep(i,m-1){\n\t\t\tR a=ary[i],b=ary[i+1],c=(a+b)/2.0;\n\t\t\tR fa=func(a),fb=func(b),fc=func(c);\n\t\t\tR cur=(fa+4.0*fc+fb)*(b-a)/6.0;\n\t\t\tans+=cur;\n\t\t}\n\t\tcout.precision(20);\n\t\tcout << fixed << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3160, "score_of_the_acc": -0.8762, "final_rank": 12 }, { "submission_id": "aoj_2034_1552144", "code_snippet": "//\n// Numerical Integration (Adaptive Gauss--Lobatto--Kronrod)\n//\n// Description:\n// Gauss--Lobatto formula is a numerical integrator\n// that is accurate for polynomials of degree <= 2n+1.\n// Adaptive Gauss--Lobatto recursively divides the domain\n// and computes integral by using G-L formula.\n//\n// Algorithm:\n// Above.\n//\n// Complexity:\n// O(#pieces) for a piecewise polynomials.\n// In general, it converges in O(1/n^6) for smooth functions.\n//\n// Verified: \n// AOJ 2034\n//\n// References:\n// R. T. Trimbitas, M. G. Trimbitas (2008):\n// Gauss-Lobatto-Kronrod formulae and adaptive numerical integration.\n// in Proceedings of the 10th International Symposium on \n// Symbolic and Numeric Algorithms for Scientific Computing. pp. 183--186.\n//\n#include <iostream>\n#include <vector>\n#include <limits>\n#include <cstdio>\n#include <algorithm>\n#include <functional>\n\nusing namespace std;\n\n#define fst first\n#define snd second\n#define all(c) ((c).begin()), ((c).end())\n\n// adaptive gauss-lobatto\ntemplate <class F>\ndouble integrate(F f, double lo, double hi, double eps = 1e-8) {\n const double th = eps / 1e-14; // (= eps / machine_epsilon)\n function<double (double,double,double,double,int)> rec =\n [&](double x0, double x6, double y0, double y6, int d) {\n const double a = sqrt(2.0/3.0), b = 1.0 / sqrt(5.0);\n double x3 = (x0 + x6)/2, y3 = f(x3), h = (x6 - x0)/2;\n double x1 = x3-a*h, x2 = x3-b*h, x4 = x3+b*h, x5 = x3+a*h;\n double y1 = f(x1), y2 = f(x2), y4 = f(x4), y5 = f(x5);\n double I1 = (y0+y6 + 5*(y2+y4)) * (h/6);\n double I2 = (77*(y0+y6) + 432*(y1+y5) + 625*(y2+y4) + 672*y3) * (h/1470);\n if (x3 + h == x3 || d > 50) return 0.0; \n if (d > 4 && th + (I1-I2) == th) return I2;\n return (double)(rec(x0, x1, y0, y1, d+1) + rec(x1, x2, y1, y2, d+1) \n + rec(x2, x3, y2, y3, d+1) + rec(x3, x4, y3, y4, d+1) \n + rec(x4, x5, y4, y5, d+1) + rec(x5, x6, y5, y6, d+1));\n };\n return rec(lo, hi, f(lo), f(hi), 0);\n}\n\nint main() {\n for (int n; scanf(\"%d\", &n); ) {\n if (n == 0) break;\n double r; \n scanf(\"%lf\\n\", &r);\n\n vector<pair<double,double>> p(n);\n for (int i = 0; i < n; ++i) \n scanf(\"%lf %lf\", &p[i].fst, &p[i].snd);\n function<double (double)> f = [&](double x) {\n if (x < p[0].fst) return 0.0;\n if (x >= p.back().fst) return 0.0;\n for (int i = 0; i+1 < n; ++i) \n if (x < p[i+1].fst) \n return p[i].snd + (p[i+1].snd-p[i].snd)/(p[i+1].fst-p[i].fst)*(x-p[i].fst);\n };\n function<double (double)> g = [&](double x) {\n return f(x) * f(x + r);\n };\n printf(\"%.12f\\n\", integrate(g, p[0].fst-100, p.back().fst+100));\n }\n}", "accuracy": 1, "time_ms": 1690, "memory_kb": 1244, "score_of_the_acc": -1.1523, "final_rank": 14 }, { "submission_id": "aoj_2034_1355664", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100;\nconst double DELTA = 1e-10;\n\n/* typedef */\n\nstruct tri {\n double x, y0, y1;\n tri() {}\n tri(double _x, double _y0, double _y1) { x = _x, y0 = _y0, y1 = _y1; }\n bool operator<(const tri& t0) const { return x < t0.x; }\n};\n\n/* global variables */\n\nint n, cn;\ndouble r, minx, maxx;\ndouble xs[MAX_N], ys[MAX_N];\ntri cxys[MAX_N * 2];\n\n/* subroutines */\n\n// (x0,y0)-(x1-y1)\n// dx=x1-x0,dy=y1-y0\n// y = dy/dx * (x-x0) + y0 = dy/dx * x + (dx*y0-dy*x0)/dx\n\ninline void line_eq(double x0, double y0, double x1, double y1,\n\t\t double& a, double& b) {\n double dx = x1 - x0;\n double dy = y1 - y0;\n a = dy / dx;\n b = (dx * y0 - dy * x0) / dx;\n}\n\ndouble f(double x) {\n if (x <= minx || x >= maxx) return 0.0;\n int k = upper_bound(xs, xs + n, x) - xs - 1;\n double a, b;\n line_eq(xs[k], ys[k], xs[k + 1], ys[k + 1], a, b);\n return a * x + b;\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n >> r;\n if (n == 0) break;\n\n for (int i = 0; i < n; i++)\n cin >> xs[i] >> ys[i];\n minx = xs[0];\n maxx = xs[n - 1];\n\n cn = 0;\n for (int i = 0; i < n; i++) {\n if (xs[i] + r <= maxx)\n\tcxys[cn++] = tri(xs[i], ys[i], f(xs[i] + r));\n if (xs[i] - r >= minx)\n\tcxys[cn++] = tri(xs[i] - r, f(xs[i] - r), ys[i]);\n }\n\n sort(cxys, cxys + cn);\n\n double sum = 0.0;\n\n for (int i = 0; i < cn - 1; i++) {\n double a0, b0, a1, b1;\n tri& cxy0 = cxys[i], cxy1 = cxys[i + 1];\n double x0 = cxy0.x, x1 = cxy1.x;\n if (x1 - x0 < DELTA) continue;\n\n line_eq(x0, cxy0.y0, x1, cxy1.y0, a0, b0);\n line_eq(x0, cxy0.y1, x1, cxy1.y1, a1, b1);\n\n // f(x)*f(x+r) = (a0*x+b0)(a1*x+b1)=(a0a1)*x^2+(a0b1+a1b0)*x+(b0b1)\n double aa = a0 * a1;\n double bb = a0 * b1 + a1 * b0;\n double cc = b0 * b1;\n\n double itg =\n\taa * (x1 * x1 * x1 - x0 * x0 * x0) / 3 +\n\tbb * (x1 * x1 - x0 * x0) / 2 +\n\tcc * (x1 - x0);\n\n sum += itg;\n }\n\n printf(\"%.10lf\\n\", sum);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1256, "score_of_the_acc": -0.1685, "final_rank": 9 }, { "submission_id": "aoj_2034_1155967", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\n\nint main(){\n int N;\n double R;\n while(cin >> N >> R && N > 0) {\n vector<double> xs(N), ys(N);\n REP(i, N) cin >> xs[i] >> ys[i];\n\n vector<double> p;\n REP(i, xs.size()){\n double x = xs[i];\n if(xs.front() <= x + R && x + R <= xs.back()) {\n p.push_back(x);\n }\n }\n REP(i, xs.size()){\n double x = xs[i] - R;\n if(xs.front() <= x && x <= xs.back()) {\n p.push_back(x);\n }\n }\n sort(p.begin(), p.end());\n p.erase(unique(p.begin(), p.end()), p.end());\n double ans = 0.0;\n for(int i = 0; i + 1 < p.size(); i++) {\n double lx = p[i];\n double rx = p[i+1];\n double cx = (lx + rx) / 2;\n\n int k = upper_bound(xs.begin(), xs.end(), cx) - xs.begin() - 1;\n //assert(xs[k] <= lx && rx <= xs[k+1]);\n assert(k >= 0);\n double A = (ys[k+1] - ys[k]) / (xs[k+1] - xs[k]);\n double B = ys[k] - A * xs[k];\n \n double lxr = p[i] + R;\n double rxr = p[i+1] + R;\n double cxr = (lxr + rxr) / 2;\n int kr = upper_bound(xs.begin(), xs.end(), cxr) - xs.begin() - 1;\n //assert(xs[kr] <= lxr && rxr <= xs[kr+1]);\n assert(kr >= 0);\n double C = (ys[kr+1] - ys[kr]) / (xs[kr+1] - xs[kr]);\n double D = ys[kr] - C * xs[kr] + C * R;\n\n double E = A * C;\n double F = A * D + B * C;\n double G = B * D;\n // E * x^3/3 + F * x^2/2 + G*x\n auto f = [&](double x) -> double {\n return E*x*x*x/3 + F*x*x/2 + G*x;\n };\n ans += f(p[i+1]) - f(p[i]);\n }\n printf(\"%.12f\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1268, "score_of_the_acc": -0.1728, "final_rank": 10 }, { "submission_id": "aoj_2034_469022", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint main()\n{\n for(;;){\n int n;\n double r;\n cin >> n >> r;\n if(n == 0)\n return 0;\n\n vector<double> x(n), y(n);\n for(int i=0; i<n; ++i)\n cin >> x[i] >> y[i];\n\n vector<double> s(n-1), t(n-1); // y = s * x + t\n for(int i=0; i<n-1; ++i){\n s[i] = (y[i+1] - y[i]) / (x[i+1] - x[i]);\n t[i] = y[i] - s[i] * x[i];\n }\n\n double ret = 0.0;\n for(int i=0; i<n-1; ++i){\n for(int j=0; j<n-1; ++j){\n double x1 = max(x[i], x[j] - r);\n double x2 = min(x[i+1], x[j+1] - r);\n if(x1 > x2)\n continue;\n\n double s1 = s[i];\n double t1 = t[i];\n double s2 = s[j];\n double t2 = t[j] + s[j] * r;\n\n double a, b, c; // y = a * x^2 + b * x + c\n a = s1 * s2;\n b = s1 * t2 + s2 * t1;\n c = t1 * t2;\n \n ret += a / 3 * pow(x2, 3) + b / 2 * pow(x2, 2) + c * x2;\n ret -= a / 3 * pow(x1, 3) + b / 2 * pow(x1, 2) + c * x1;\n }\n }\n printf(\"%.10f\\n\", ret);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1252, "score_of_the_acc": -0.1611, "final_rank": 8 }, { "submission_id": "aoj_2034_357727", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\n#include <cstring>\n\nusing namespace std;\n\nconst double EPS=(1e-10);\nbool EQ(double a,double b){\n return abs(a-b)<EPS;\n}\n\ntypedef complex<double> P;\ntypedef pair<double,double> pdd;\n\ndouble calc(double a1,double b1,double a2,double b2,double x,double r){\n return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;\n}\n\nint main(){\n int n;\n double r;\n while(cin>>n>>r&&!(n==0&&EQ(r,0))){\n vector<P> dots;\n vector<pdd> co;\n dots.push_back(P(-1000,0));\n for(int i=0;i<n;i++){\n double x,y;\n cin>>x>>y;\n dots.push_back(P(x,y));\n }\n dots.push_back(P(1000,0));\n double sum=0;\n n+=2;\n for(int i=1;i<n;i++){\n double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());\n double b=dots[i-1].imag()-a*dots[i-1].real();\n co.push_back(pdd(a,b));\n }\n double ax=dots[1].real();\n double bx=ax+r;\n int nxtApos=n;\n int nxtBpos=n;\n // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ\n for(int i=0;i<n;i++){\n if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){\n nxtBpos=i;\n break;\n }\n }\n for(int i=0;i<n;i++){\n if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){\n nxtApos=i;\n break;\n }\n }\n // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ\n while(nxtBpos!=n&&nxtApos!=n){\n // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ\n double adis=dots[nxtApos].real()-ax;\n double bdis=dots[nxtBpos].real()-bx;\n double dis=min(adis,bdis);\n double t=ax+dis;\n double s=ax;\n sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);\n sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);\n ax+=dis;bx+=dis;\n if(EQ(adis,bdis)){\n nxtApos++;\n nxtBpos++;\n }\n else if(adis<bdis)nxtApos++;\n else nxtBpos++;\n }\n printf(\"%.10f\\n\",sum);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 988, "score_of_the_acc": -0.0782, "final_rank": 2 }, { "submission_id": "aoj_2034_357725", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\n#include <cstring>\n\nusing namespace std;\n\nconst double EPS=(1e-10);\nbool EQ(double a,double b){\n return abs(a-b)<EPS;\n}\n\ntypedef complex<double> P;\ntypedef pair<double,double> pdd;\n\ndouble calc(double a1,double b1,double a2,double b2,double x,double r){\n return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;\n}\n\nint main(){\n int n;\n double r;\n while(cin>>n>>r&&!(n==0&&EQ(r,0))){\n vector<P> dots;\n vector<pdd> co;\n dots.push_back(P(-1000,0));\n for(int i=0;i<n;i++){\n double x,y;\n cin>>x>>y;\n dots.push_back(P(x,y));\n }\n dots.push_back(P(1000,0));\n double sum=0;\n for(int i=1;i<=n+1;i++){\n double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());\n double b=dots[i-1].imag()-a*dots[i-1].real();\n co.push_back(pdd(a,b));\n }\n double ax=dots[1].real();\n double bx=ax+r;\n int nxtApos=n+2;\n int nxtBpos=n+2;\n // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ\n for(int i=0;i<=n+1;i++){\n if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){\n nxtBpos=i;\n break;\n }\n }\n for(int i=0;i<=n+1;i++){\n if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){\n nxtApos=i;\n break;\n }\n }\n // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ\n while(nxtBpos!=n+2&&nxtApos!=n+2){\n // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ\n double adis=dots[nxtApos].real()-ax;\n double bdis=dots[nxtBpos].real()-bx;\n double dis=min(adis,bdis);\n double t=ax+dis;\n double s=ax;\n sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);\n sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);\n ax+=dis;bx+=dis;\n if(EQ(adis,bdis)){\n nxtApos++;\n nxtBpos++;\n }\n else if(adis<bdis)nxtApos++;\n else nxtBpos++;\n }\n printf(\"%.10f\\n\",sum);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 988, "score_of_the_acc": -0.0782, "final_rank": 2 }, { "submission_id": "aoj_2034_357724", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\n#include <cstring>\n\nusing namespace std;\n\nconst double EPS=(1e-10);\nbool EQ(double a,double b){\n return abs(a-b)<EPS;\n}\n\ntypedef complex<double> P;\ntypedef pair<double,double> pdd;\n\ndouble calc(double a1,double b1,double a2,double b2,double x,double r){\n return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;\n}\n\nint main(){\n int n;\n double r;\n while(cin>>n>>r&&!(n==0&&EQ(r,0))){\n vector<P> dots;\n vector<pdd> co;\n dots.push_back(P(-1000,0));\n for(int i=0;i<n;i++){\n double x,y;\n cin>>x>>y;\n dots.push_back(P(x,y));\n }\n dots.push_back(P(1000,0));\n double sum=0;\n for(int i=1;i<=n+1;i++){\n double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());\n double b=dots[i-1].imag()-a*dots[i-1].real();\n co.push_back(pdd(a,b));\n }\n double ax=dots[1].real();\n double bx=ax+r;\n int nxtApos=n+2;\n int nxtBpos=n+2;\n // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ\n for(int i=0;i<=n+1;i++){\n if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){\n nxtBpos=i;\n break;\n }\n }\n for(int i=0;i<=n+1;i++){\n if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){\n nxtApos=i;\n break;\n }\n }\n //bool rev=false;\n //if(ax>bx){\n // swap(ax,bx);\n // swap(nxtApos,nxtBpos);\n // rev=true;\n //}\n // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ\n while(nxtBpos!=n+2&&nxtApos!=n+2){\n // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ\n double adis=dots[nxtApos].real()-ax;\n double bdis=dots[nxtBpos].real()-bx;\n double dis=min(adis,bdis);\n double t=ax+dis;\n double s=ax;\n sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);\n sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);\n //if(rev){\n // t=bx+dis;\n // s=bx;\n // sum+=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,t,r);\n // sum-=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,s,r);\n //}\n //else{\n // sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);\n // sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);\n //}\n ax+=dis;bx+=dis;\n if(EQ(adis,bdis)){\n nxtApos++;\n nxtBpos++;\n }\n else if(adis<bdis)nxtApos++;\n else nxtBpos++;\n }\n printf(\"%.10f\\n\",sum);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 992, "score_of_the_acc": -0.0796, "final_rank": 4 }, { "submission_id": "aoj_2034_357723", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\n#include <cstring>\n\nusing namespace std;\n\nconst double EPS=(1e-10);\nbool EQ(double a,double b){\n return abs(a-b)<EPS;\n}\n\ntypedef complex<double> P;\ntypedef pair<double,double> pdd;\n\ndouble calc(double a1,double b1,double a2,double b2,double x,double r){\n return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;\n}\n\nint main(){\n int n;\n double r;\n while(cin>>n>>r&&!(n==0&&EQ(r,0))){\n vector<P> dots;\n vector<pdd> co;\n dots.push_back(P(-1000,0));\n for(int i=0;i<n;i++){\n double x,y;\n cin>>x>>y;\n dots.push_back(P(x,y));\n }\n dots.push_back(P(1000,0));\n double sum=0;\n for(int i=1;i<=n+1;i++){\n double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());\n double b=dots[i-1].imag()-a*dots[i-1].real();\n co.push_back(pdd(a,b));\n }\n double ax=dots[1].real();\n double bx=ax+r;\n int nxtApos=n+2;\n int nxtBpos=n+2;\n // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ\n for(int i=0;i<=n+1;i++){\n if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){\n nxtBpos=i;\n break;\n }\n }\n for(int i=0;i<=n+1;i++){\n if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){\n nxtApos=i;\n break;\n }\n }\n bool rev=false;\n if(ax>bx){\n swap(ax,bx);\n swap(nxtApos,nxtBpos);\n rev=true;\n }\n // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ\n while(nxtBpos!=n+2){\n // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ\n double adis=dots[nxtApos].real()-ax;\n double bdis=dots[nxtBpos].real()-bx;\n double dis=min(adis,bdis);\n double t=ax+dis;\n double s=ax;\n if(rev){\n t=bx+dis;\n s=bx;\n sum+=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,t,r);\n sum-=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,s,r);\n }\n else{\n sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);\n sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);\n }\n ax+=dis;bx+=dis;\n if(EQ(adis,bdis)){\n nxtApos++;\n nxtBpos++;\n }\n else if(adis<bdis)nxtApos++;\n else nxtBpos++;\n }\n printf(\"%.10f\\n\",sum);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 992, "score_of_the_acc": -0.0796, "final_rank": 4 }, { "submission_id": "aoj_2034_355197", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nstruct P {\n double x, y;\n bool f;\n P(double x, double y, bool f) : x(x), y(y), f(f) {}\n};\nbool operator<(const P &a, const P &b) {\n return a.x!=b.x?a.x<b.x : a.y<b.y;\n}\n\ndouble X[100];\ndouble Y[100];\nint n;\n\ndouble func(double x) {\n if (x<=X[0] || x>=X[n-1]) return 0;\n int q = upper_bound(X, X+n, x) - X;\n int p = q-1;\n return (Y[q]-Y[p])/(X[q]-X[p])*(x-X[p])+Y[p];\n}\n\nint main() {\n double r;\n while(cin>>n>>r,n) {\n vector<P> v;\n REP(i, n) {\n cin >> X[i] >> Y[i];\n v.push_back(P(X[i], Y[i], 0));\n v.push_back(P(X[i]-r, Y[i], 1));\n }\n sort(ALL(v));\n double ans = 0;\n REP(i, v.size()-1) {\n double a = v[i].x;\n double b = v[i+1].x;\n double c = (a+b)/2;\n double fa = func(a)*func(a+r);\n double fb = func(b)*func(b+r);\n double fc = func(c)*func(c+r);\n ans += (b-a)/6*(fa+4*fc+fb);\n }\n printf(\"%.10f\\n\",ans);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 992, "score_of_the_acc": -0.0856, "final_rank": 6 }, { "submission_id": "aoj_2034_256348", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double EPS=1e-9;\n\nstruct Point{ double x,y; };\n\nint n;\nPoint _f[100],_g[100];\n\ndouble f(double x){\n\tif(x<_f[0].x) return 0;\n\trep(i,n-1){\n\t\tdouble x1=_f[i].x,x2=_f[i+1].x;\n\t\tdouble y1=_f[i].y,y2=_f[i+1].y;\n\t\tif(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;\n\t}\n\treturn 0;\n}\n\ndouble g(double x){\n\tif(x<_g[0].x) return 0;\n\trep(i,n-1){\n\t\tdouble x1=_g[i].x,x2=_g[i+1].x;\n\t\tdouble y1=_g[i].y,y2=_g[i+1].y;\n\t\tif(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;\n\t}\n\treturn 0;\n}\n\nint main(){\n\tfor(double r;scanf(\"%d%lf\",&n,&r),n;){\n\t\tdouble ev[200];\n\t\trep(i,n){\n\t\t\tint x,y; scanf(\"%d%d\",&x,&y);\n\t\t\t_f[i]=(Point){x,y};\n\t\t\t_g[i]=(Point){x+r,y};\n\t\t\tev[2*i+0]=x;\n\t\t\tev[2*i+1]=x+r;\n\t\t}\n\t\tsort(ev,ev+2*n);\n\n\t\tdouble ans=0;\n\t\trep(i,2*n-1){\n\t\t\tdouble x1=ev[i],x2=ev[i+1];\n\t\t\tif(x2-x1<EPS) continue;\n\n\t\t\tdouble f1=f(x1),f2=f(x2),g1=g(x1),g2=g(x2);\n\t\t\tdouble af=(f2-f1)/(x2-x1),bf=f1-af*x1;\n\t\t\tdouble ag=(g2-g1)/(x2-x1),bg=g1-ag*x1;\n\t\t\tans+=af*ag*(x2*x2*x2-x1*x1*x1)/3+(af*bg+bf*ag)*(x2*x2-x1*x1)/2+bf*bg*(x2-x1);\n\t\t}\n\t\tprintf(\"%.15f\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 820, "score_of_the_acc": -0.0179, "final_rank": 1 }, { "submission_id": "aoj_2034_256327", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef complex<double> Point;\n\nconst double EPS=1e-9;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn abs(a.real()-b.real())<EPS?(a.imag()+EPS<b.imag()):(a.real()+EPS<b.real());\n\t}\n}\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){ push_back(a), push_back(b); }\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ninline double dot(const Point &a,const Point &b){\n\treturn a.real()*b.real()+a.imag()*b.imag();\n}\n\ninline double cross(const Point &a,const Point &b){\n\treturn a.real()*b.imag()-a.imag()*b.real();\n}\n\nenum {CCW=1,CW=-1,ON=0};\nint ccw(const Point &a,Point b,Point c){\n\tb-=a, c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS) return CCW;\n\tif(rotdir<-EPS) return CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\n\ta=l[0].imag()-l[1].imag();\n\tb=l[1].real()-l[0].real();\n\tc=cross(l[0],l[1]);\n}\n\nbool cover(const Segment &s,const Point &p){\n\treturn dot(s[0]-p,s[1]-p)<EPS;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point *p){\n\tbool flg;\n\tif(abs(cross(s[1]-s[0],t[1]-t[0]))<EPS && abs(cross(s[1]-s[0],t[0]-s[0]))<EPS){\n\t\tflg=(cover(s,t[0]) || cover(s,t[1]) || cover(t,s[0]) || cover(t,s[1]));\n\t\tif(!flg) return false;\n\t}\n\n\tif(flg || (ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t\t\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0)){\n\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\tcalc_abc(s,a1,b1,c1);\n\t\tcalc_abc(t,a2,b2,c2);\n\t\tdouble det=a1*b2-a2*b1;\n\t\tif(abs(det)<EPS){ // s is parallel to t\n\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\trep(i,3){\n\t\t\t\tif(cover(s,q[i]) && cover(t,q[i])){\n\t\t\t\t\t*p=q[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse *p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nint n;\nPoint _f[100],_g[100];\n\ndouble f(double x){\n\tif(x<_f[0].real()) return 0;\n\trep(i,n-1){\n\t\tdouble x1=_f[i].real(),x2=_f[i+1].real();\n\t\tdouble y1=_f[i].imag(),y2=_f[i+1].imag();\n\t\tif(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;\n\t}\n\treturn 0;\n}\n\ndouble g(double x){\n\tif(x<_g[0].real()) return 0;\n\trep(i,n-1){\n\t\tdouble x1=_g[i].real(),x2=_g[i+1].real();\n\t\tdouble y1=_g[i].imag(),y2=_g[i+1].imag();\n\t\tif(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;\n\t}\n\treturn 0;\n}\n\nint main(){\n\tfor(double r;scanf(\"%d%lf\",&n,&r),n;){\n\t\tvector<double> ev;\n\t\trep(i,n){\n\t\t\tint x,y; scanf(\"%d%d\",&x,&y);\n\t\t\t_f[i]=Point(x,y);\n\t\t\t_g[i]=Point(x+r,y);\n\t\t\tev.push_back(x);\n\t\t\tev.push_back(x+r);\n\t\t}\n\n\t\trep(i,n-1) rep(j,n-1) {\n\t\t\tPoint p;\n\t\t\tif(intersect(Segment(_f[i],_f[i+1]),Segment(_g[i],_g[i+1]),&p)){\n\t\t\t\tev.push_back(p.real());\n\t\t\t}\n\t\t}\n\n\t\tsort(_f,_f+n);\n\t\tsort(_g,_g+n);\n\t\tsort(ev.begin(),ev.end());\n\n\t\tdouble ans=0;\n\t\trep(i,ev.size()-1){\n\t\t\tdouble x1=ev[i],x2=ev[i+1];\n\t\t\tif(x2-x1<EPS) continue;\n\t\t\tdouble f1=f(x1),f2=f(x2),g1=g(x1),g2=g(x2);\n\t\t\tdouble af=(f2-f1)/(x2-x1),bf=f1-af*x1;\n\t\t\tdouble ag=(g2-g1)/(x2-x1),bg=g1-ag*x1;\n\t\t\tans+=af*ag*(x2*x2*x2-x1*x1*x1)/3+(af*bg+bf*ag)*(x2*x2-x1*x1)/2+bf*bg*(x2-x1);\n\t\t}\n\t\tprintf(\"%.15f\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 924, "score_of_the_acc": -0.4897, "final_rank": 11 }, { "submission_id": "aoj_2034_112053", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nclass EQ{\npublic:\n double a,b;\n double xs,xe;\n};\n\ndouble compute(double a,double b,double c,double x){\n return a*x*x*x+b*x*x+c*x;\n}\n\ndouble integ(double ai,double bi,double aj,double bj,double xs,double xe){\n double ta=compute((ai*aj)/3,(ai*bj+aj*bi)/2,bi*bj,xs);\n double tb=compute((ai*aj)/3,(ai*bj+aj*bi)/2,bi*bj,xe);\n return tb-ta;\n}\n\nbool have_common(double xs1,double xe1,double xs2,double xe2){\n if ((xs2 <= xs1 && xs1 <= xe2)||\n (xs2 <= xe1 && xe1 <= xe2)||\n (xs1 <= xs2 && xs2 <= xe1)||\n (xs1 <= xe2 && xe2 <= xe1))return true;\n else return false;\n}\n\ndouble solve(vector<EQ> &in,double r){\n int n = in.size();\n double ans = 0;\n rep(i,n){\n double xs=in[i].xs+r,xe=in[i].xe+r;\n rep(j,n){\n //cout << xs <<\" \" << xe << endl;\n if (have_common(xs,xe,in[j].xs,in[j].xe)){\n double cxs=max(xs,in[j].xs),cxe=min(xe,in[j].xe);\n // cout <<\"test :\" << cxs-r <<\" \" << cxe-r <<\n // \" \" << integ(in[i].a,in[i].b,in[j].a,in[j].b+r*in[j].a,cxs-r,cxe-r) <<\" test end \" << endl;\n\n ans+= integ(in[i].a,in[i].b,in[j].a,in[j].b+r*in[j].a,cxs-r,cxe-r);\n if (xe < in[j].xe)break;\n else xs=in[j].xe;\n }\n }\n }\n return ans;\n}\n\n\nmain(){\n int n;\n double r;\n while(cin>>n>>r && n){\n vector<double> x,y;\n vector<EQ>in;\n rep(i,n){\n double tx,ty;\n cin>>tx>>ty;\n x.push_back(tx);\n y.push_back(ty);\n }\n\n EQ ins;\n rep(i,n-1){\n ins.xs=x[i];ins.xe=x[i+1];\n ins.a=(y[i+1]-y[i])/(x[i+1]-x[i]);\n ins.b=-ins.a*x[i]+y[i];\n in.push_back(ins);\n }\n\n printf(\"%.10lf\\n\",solve(in,r));\n }\n return false;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 996, "score_of_the_acc": -0.087, "final_rank": 7 } ]
aoj_2035_cpp
Problem F: It Prefokery Pio You are a member of a secret society named Japanese Abekobe Group , which is called J. A. G. for short. Those who belong to this society often exchange encrypted messages. You received lots of encrypted messages this morning, and you tried to decrypt them as usual. However, because you received so many and long messages today, you had to give up decrypting them by yourself. So you decided to decrypt them with the help of a computer program that you are going to write. The encryption scheme in J. A. G. utilizes palindromes. A palindrome is a word or phrase that reads the same in either direction. For example, “MADAM”, “REVIVER” and “SUCCUS” are palindromes, while “ADAM”, “REVENGE” and “SOCCER” are not. Specifically to this scheme, words and phrases made of only one or two letters are not regarded as palindromes. For example, “A” and “MM” are not regarded as palindromes. The encryption scheme is quite simple: each message is encrypted by inserting extra letters in such a way that the longest one among all subsequences forming palindromes gives the original message. In other words, you can decrypt the message by extracting the longest palindrome subsequence . Here, a subsequence means a new string obtained by picking up some letters from a string without altering the relative positions of the remaining letters. For example, the longest palindrome subsequence of a string “YMAOKDOAMIMHAADAMMA” is “MADAMIMADAM” as indicated below by underline. Now you are ready for writing a program. Input The input consists of a series of data sets. Each data set contains a line made of up to 2,000 capital letters which represents an encrypted string. The end of the input is indicated by EOF (end-of-file marker). Output For each data set, write a decrypted message in a separate line. If there is more than one decrypted message possible (i.e. if there is more than one palindrome subsequence not shorter than any other ones), you may write any of the possible messages. You can assume that the length of the longest palindrome subsequence of each data set is always longer than two letters. Sample Input YMAOKDOAMIMHAADAMMA LELVEEL Output for the Sample Input MADAMIMADAM LEVEL
[ { "submission_id": "aoj_2035_10722445", "code_snippet": "#include<bits/stdc++.h>\n//#include<atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\n\n//using mint = modint998244353;\n//using mint = modint1000000007;\n\ntemplate<class T> using pq = priority_queue<T>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\n#define vec_unique(v) v.erase(unique(v.begin(), v.end()), v.end()) //重複削除\n#define vec_iota(v) iota(v.begin(), v.end(), 0) //0, 1, 2, 3, ..., n - 1にセット\n\nint dx[4] = {1, -1, 0, 0};\nint dy[4] = {0, 0, 1, -1};\n//int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};\n//int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};\n\n#define INF 2e18\n#define INF2 2e9\n\nint main() {\n string s;\n while(cin >> s) {\n string t = s;\n reverse(t.begin(), t.end());\n int n = s.size();\n int m = t.size();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1));\n for(int i = 1; i <= n; i++) {\n for(int j = 1; j <= m; j++) {\n if(s[i - 1] == t[j - 1]) {\n dp[i][j] = dp[i - 1][j - 1] + 1;\n } else {\n dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);\n }\n }\n }\n\n //cout << dp[n][m] << endl;\n\n //LCSを復元\n int i = n;\n int j = m;\n string ans = \"\";\n while(i > 0 && j > 0) {\n if(s[i - 1] == t[j - 1]) {\n ans = s[i - 1] + ans;\n i--;\n j--;\n } else {\n if(dp[i][j] == dp[i][j - 1]) j--;\n else i--;\n }\n }\n cout << ans << endl;\n }\n //cout << fixed << setprecision(15) << << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 18752, "score_of_the_acc": -0.2671, "final_rank": 5 }, { "submission_id": "aoj_2035_10694366", "code_snippet": "// competitive-verifier: PROBLEM\n#pragma GCC optimize(\"Ofast,fast-math,unroll-all-loops\")\n#include <bits/stdc++.h>\n#if !defined(ATCODER) && !defined(EVAL)\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#endif\ntemplate <class T, class U>\nconstexpr bool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nconstexpr bool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = 3.14159265358979323846;\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m) - 1; i >= int(n); --i)\n#define FORL(i, m, n) for (std::int64_t i = (m); i < std::int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(20);\n }\n constexpr void operator()() const {}\n} sonic;\nstruct increment_impl {\n template <class T>\n const increment_impl &operator>>(std::vector<T> &v) const {\n for (auto &x : v) ++x;\n return *this;\n }\n} Inc;\nstruct decrement_impl {\n template <class T>\n const decrement_impl &operator>>(std::vector<T> &v) const {\n for (auto &x : v) --x;\n return *this;\n }\n} Dec;\nstruct sort_impl {\n template <class T>\n const sort_impl &operator>>(std::vector<T> &v) const {\n std::sort(v.begin(), v.end());\n return *this;\n }\n} Sort;\nstruct unique_impl {\n template <class T>\n const unique_impl &operator>>(std::vector<T> &v) const {\n std::sort(v.begin(), v.end());\n v.erase(std::unique(v.begin(), v.end()), v.end());\n return *this;\n }\n} Uniq;\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? \"\" : \" \") << *it;\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\nvoid Yes(bool is_correct = true) { std::cout << (is_correct ? \"Yes\\n\" : \"No\\n\"); }\nvoid No(bool is_not_correct = true) { Yes(!is_not_correct); }\nvoid YES(bool is_correct = true) { std::cout << (is_correct ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO(bool is_not_correct = true) { YES(!is_not_correct); }\nvoid Takahashi(bool is_correct = true) { std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n'; }\nvoid Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }\nint main(void) {\n string s;\n while (cin >> s) {\n int n = s.size();\n vector dp(n + 1, vector(n + 1, 0));\n rep (i, n) dp[i][i + 1] = 1;\n FOR (d, 2, n + 1) {\n rep (l, n) {\n int r = l + d;\n if (r > n)\n break;\n dp[l][r] = max(dp[l + 1][r], dp[l][r - 1]);\n if (s[l] == s[r - 1])\n chmax(dp[l][r], dp[l + 1][r - 1] + 2);\n }\n }\n string ans, rev;\n int l = 0, r = n;\n while (dp[l][r] > 0 && r - l > 1) {\n if (dp[l + 1][r - 1] + 2 == dp[l][r] && s[l] == s[r - 1]) {\n ans += s[l];\n rev += s[r - 1];\n ++l, --r;\n } else if (dp[l][r - 1] == dp[l][r]) {\n --r;\n } else {\n ++l;\n }\n }\n if (dp[l][r] > 0 && r - l > 0)\n ans += s[l];\n reverse(all(rev));\n ans += rev;\n co(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 19120, "score_of_the_acc": -0.2509, "final_rank": 3 }, { "submission_id": "aoj_2035_9666177", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint longestCommonSubsequence(string s1, string s2) {\n int m = s1.length();\n int n = s2.length();\n vector<vector<int>> dp(m+1, vector<int>(n+1, 0));\n \n for (int i=1; i<=m; i++) {\n for (int j=1; j<=n; j++) {\n if (s1[i-1] == s2[j-1]) {\n dp[i][j] = 1 + dp[i-1][j-1];\n } else {\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n }\n \n return dp[m][n];\n}\n\nvoid printLCS(string s1, string s2) {\n int m = s1.length();\n int n = s2.length();\n vector<vector<int>> dp(m+1, vector<int>(n+1, 0));\n \n for (int i=1; i<=m; i++) {\n for (int j=1; j<=n; j++) {\n if (s1[i-1] == s2[j-1]) {\n dp[i][j] = 1 + dp[i-1][j-1];\n } else {\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n }\n \n string lcs;\n int i = m;\n int j = n;\n \n while (i > 0 && j > 0) {\n if (s1[i-1] == s2[j-1]) {\n lcs.push_back(s1[i-1]);\n i--;\n j--;\n } else if (dp[i-1][j] > dp[i][j-1]) {\n i--;\n } else {\n j--;\n }\n }\n \n for (int i=lcs.length()-1; i>=0; i--) {\n cout << lcs[i];\n }\n cout << endl;\n}\n\nint main() {\n string s;\n while (cin >> s) {\n string sReverse = s;\n reverse(sReverse.begin(), sReverse.end());\n printLCS(s, sReverse);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 18832, "score_of_the_acc": -0.2857, "final_rank": 6 }, { "submission_id": "aoj_2035_9666162", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstring t;\nstring s;\nint dp[2001][2001];\nint lcs(int i , int j) {\n if(i == s.size() || j == t.size())return 0;\n int &res = dp[i][j];\n if(~res)return res;\n res = 0;\n if(s[i] == t[j])res = 1 + lcs(i + 1 , j + 1);\n else res = max(lcs(i + 1 , j) , lcs(i , j + 1));\n return res;\n}\nvoid build(int i , int j) {\n if(i == s.size() || j == t.size())return;\n if(s[i] == t[j]) {\n cout<<s[i];\n build(i + 1 , j + 1);\n }else {\n if(dp[i][j] == lcs(i + 1 ,j))build(i + 1 , j);\n else build(i , j + 1);\n }\n}\nsigned main() {\n ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n while(cin>>s) {\n t = s;\n for(int i = 0;i < s.size();i++) {\n for(int j = 0;j < t.size();j++)\n dp[i][j] = -1;\n }\n reverse(t.begin() , t.end());\n int temp = lcs(0 , 0);\n build(0 , 0);\n cout<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 19188, "score_of_the_acc": -0.3887, "final_rank": 9 }, { "submission_id": "aoj_2035_5934296", "code_snippet": "#include <string>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nstring s; int dp[2009][2009];\nint main() {\n while (cin >> s) {\n int n = s.size();\n for (int i = 0; i < n; i++) dp[i][i + 1] = 1;\n for (int i = 2; i <= n; i++) {\n for (int l = 0; l <= n - i; l++) {\n int r = l + i;\n dp[l][r] = max(dp[l + 1][r], dp[l][r - 1]);\n if (s[l] == s[r - 1]) dp[l][r] = max(dp[l][r], dp[l + 1][r - 1] + 2);\n }\n }\n string ret;\n int pl = 0, pr = n;\n while (pr - pl >= 2) {\n if (s[pl] == s[pr - 1] && dp[pl][pr] == dp[pl + 1][pr - 1] + 2) ret += s[pl], pl++, pr--;\n else if (dp[pl][pr] == dp[pl + 1][pr]) pl++;\n else pr--;\n }\n string ps = ret;\n if (pr - pl == 1) ps += s[pl];\n reverse(ret.begin(), ret.end());\n ps += ret;\n cout << ps << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 18444, "score_of_the_acc": -0.1819, "final_rank": 1 }, { "submission_id": "aoj_2035_4641340", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// #define int long long\n#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)\n#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)\n#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)\n#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)\n#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)\n#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)\n#define SORT(v, n) sort(v, v + n);\n#define REVERSE(v, n) reverse(v, v+n);\n#define vsort(v) sort(v.begin(), v.end());\n#define all(v) v.begin(), v.end()\n#define mp(n, m) make_pair(n, m);\n#define cout(d) cout<<d<<endl;\n#define coutd(d) cout<<std::setprecision(10)<<d<<endl;\n#define cinline(n) getline(cin,n);\n#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);\n#define PI (acos(-1))\n#define FILL(v, n, x) fill(v, v + n, x);\n#define sz(x) long long(x.size())\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vs = vector<string>;\nusing vpll = vector<pair<ll, ll>>;\nusing vtp = vector<tuple<ll,ll,ll>>;\nusing vb = vector<bool>;\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n\nconst ll INF = 1e9;\nconst ll MOD = 1e9+7;\nconst ll LINF = 1e18;\n\nll dp[2005][2005]; // [l,r)における回文の最大長\nstring s;\n\nvoid solve(){\n ll n=s.size();\n \n memset(dp,0,sizeof(dp));\n rep(i,n) dp[i][i+1]=1;\n \n for(ll len=2; len<=n; len++){\n for(ll i=0; i+len<=n; i++){\n ll j=i+len;\n if(s[i]==s[j-1]) chmax(dp[i][j],dp[i+1][j-1]+2);\n else chmax(dp[i][j],max(dp[i+1][j],dp[i][j-1]));\n }\n }\n \n ll ma=0, l,r;\n rep(i,n) irep(j,i,n+1){\n if(chmax(ma,dp[i][j])) l=i, r=j;\n }\n \n // cout<<dp[0][n]<<' '<<ma<<endl;\n \n if(ma==1){\n cout<<s[0]<<endl;\n return;\n }\n \n string left=\"\", right=\"\";\n while(ma>0){\n while(dp[l+1][r]==ma) l++;\n while(dp[l][r-1]==ma) r--;\n left+=s[l], right+=s[l];\n // cout<<l<<' '<<r<<' '<<ma<<endl;\n ma-=2;\n if(ma==1){\n left+=s[l+1];\n ma--;\n }\n l++, r--;\n }\n\n reverse(all(right));\n string ans=left+right;\n cout<<ans<<endl;\n \n return;\n}\n\n\nsigned main()\n{\n cin.tie( 0 ); ios::sync_with_stdio( false );\n \n for(int t = 0; cin >> s; t++) {\n solve();\n }\n \n}", "accuracy": 1, "time_ms": 980, "memory_kb": 34620, "score_of_the_acc": -0.8092, "final_rank": 14 }, { "submission_id": "aoj_2035_3947480", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <climits>\n#include <cfloat>\n\n\n\nint cal_longest_palindrome_subsequence(const std::string& str, const int left, const int right, std::vector<std::vector<int>>& memo) {\n\tif (left > right) return 0;\n\tif (left == right) return 1;\n\tif (memo[left][right - left - 1] >= 0) return memo[left][right - left - 1];\n\tif (str[left] == str[right]) memo[left][right - left - 1] = cal_longest_palindrome_subsequence(str, left + 1, right - 1, memo) + 2;\n\treturn memo[left][right - left - 1] = std::max({ memo[left][right - left - 1], cal_longest_palindrome_subsequence(str, left + 1, right, memo), cal_longest_palindrome_subsequence(str, left, right - 1, memo) });\n}\nstd::string make_longest_palindorome(const std::string& str) {\n\tstd::string prev_half;\n\tstd::vector<std::vector<int>> memo; memo.reserve(str.size());\n\tfor (auto i = 0; i < str.size(); ++i) memo.push_back(std::vector<int>(str.size() - i, -1));\n\tfor (int left = 0, right = str.size() - 1; left <= right; ) {\n\t\tif (str[left] == str[right]) {\n\t\t\tprev_half.push_back(str[left]); ++left; --right;\n\t\t}\n\t\telse if (cal_longest_palindrome_subsequence(str, left + 1, right, memo) > cal_longest_palindrome_subsequence(str, left, right - 1, memo)) {\n\t\t\t++left;\n\t\t}\n\t\telse {\n\t\t\t--right;\n\t\t}\n\t}\n\tconst auto result_length = cal_longest_palindrome_subsequence(str, 0, str.size() - 1, memo);\n\tif (result_length % 2 == 1) {\n\t\tfor (int i = prev_half.size() - 2; i >= 0; --i) prev_half.push_back(prev_half[i]);\n\t}\n\telse {\n\t\tfor (int i = prev_half.size() - 1; i >= 0; --i) prev_half.push_back(prev_half[i]);\n\t}\n\treturn prev_half;\n}\n\nint main() {\n\tstd::string str;\n\twhile (std::cin >> str) {\n\t\tstd::cout << make_longest_palindorome(str) << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 10868, "score_of_the_acc": -0.2583, "final_rank": 4 }, { "submission_id": "aoj_2035_2859269", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\nint solve(int l, int r, const string& st,vector<vector<int>>&memo,vector<vector<int>>&flags) {\n\tif (memo[l][r] == -1) {\n\t\tif(l==r)memo[l][r]=0;\n\t\telse if(l+1==r)memo[l][r]=1;\n\t\telse {\n\n\t\t\tif (st[l] == st[r - 1]) {\n\t\t\t\tmemo[l][r]=2+solve(l+1,r-1,st,memo,flags);\n\t\t\t\tflags[l][r]=3;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tint lk=solve(l+1,r,st,memo,flags);\n\t\t\t\tint rk=solve(l,r-1,st,memo,flags);\n\n\t\t\t\tif (lk > rk) {\n\t\t\t\t\tflags[l][r]=2;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tflags[l][r]=1;\n\t\t\t\t}\n\t\t\t\tmemo[l][r]=max(lk,rk);\n\t\t\t}\n\t\t}\n\t}\n\treturn memo[l][r];\n}\n\nint main() {\n\tstring st;\n\twhile (cin>>st) {\n\t\tvector<vector<int>>memo(st.size()+1,vector<int>(st.size()+1,-1)),flags(st.size()+1,vector<int>(st.size()+1));\n\t\tint ans=solve(0,st.size(),st,memo,flags);\n\t\tstring ans_st1,ans_st2;\n\n\t\tint al=0;\n\t\tint ar=st.size();\n\t\twhile (true) {\n\t\t\tint num=flags[al][ar];\n\t\t\tif (num == 3) {\n\t\t\t\tans_st1.push_back(st[al]);\n\t\t\t\tans_st2.push_back(st[ar-1]);\n\t\t\t\tal++;\n\t\t\t\tar--;\n\t\t\t}\n\t\t\telse if (num == 2) {\n\t\t\t\tal++;\n\t\t\t}\n\t\t\telse if (num == 1) {\n\t\t\t\tar--;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (al + 1 == ar) {\n\t\t\t\t\tans_st1.push_back(st[al]);\n\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treverse(ans_st2.begin(),ans_st2.end());\n\t\tcout<<ans_st1<<ans_st2<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 34304, "score_of_the_acc": -0.8378, "final_rank": 15 }, { "submission_id": "aoj_2035_2614244", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nstring s,t;\nint dp[2011][2011];\nint used[2011][2011];\nstring solve(){\n string ans=\"\";\n int i=s.size()-1;\n int j=t.size()-1;\n while(i>0&&j>0){\n if(used[i][j]==1)ans+=t[j],i--,j--;\n else if(used[i][j]==2)i--;\n else j--;\n }\n return ans;\n}\nint main(){\n while(cin>>s){\n memset(dp,0,sizeof(dp));\n memset(used,0,sizeof(used));\n t=s;\n reverse(s.begin(),s.end());\n t=\"@\"+t;\n s=\"@\"+s;\n for(int i=1;i<s.size();i++)\n for(int j=1;j<t.size();j++)\n if(s[i]==t[j]){\n if(dp[i-1][j]>=dp[i-1][j-1]+1) dp[i][j]=max(dp[i][j],dp[i-1][j]),used[i][j]=2;\n \n else dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1),used[i][j]=1;\n }\n else{\n if(dp[i-1][j]>=dp[i][j-1])dp[i][j]=max(dp[i][j],dp[i-1][j]),used[i][j]=2;\n else dp[i][j]=max(dp[i][j],dp[i][j-1]),used[i][j]=3;\n }\n cout<<solve()<<endl;\n }\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 34612, "score_of_the_acc": -0.7507, "final_rank": 12 }, { "submission_id": "aoj_2035_2558030", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint dp[2001][2001];\nint memo[2001][2001];\n\nint main(){\n\n\tint length;\n\tchar buf[2001],reverse[2001];\n\tstack<char> ANS;\n\n\twhile(scanf(\"%s\",buf) != EOF){\n\n\t\tfor(length = 0; buf[length] != '\\0'; length++);\n\n\t\tfor(int i = 0; i < length; i++){\n\t\t\treverse[i] = buf[length-1-i];\n\t\t}\n\t\treverse[length] = '\\0';\n\n\t\tfor(int i = 0; i <= length; i++){\n\t\t\tfor(int k = 0; k <= length; k++){\n\t\t\t\tdp[i][k] = 0;\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i < length; i++){\n\t\t\tfor(int k = 0; k < length; k++){\n\t\t\t\tif(buf[i] == reverse[k]){\n\t\t\t\t\tdp[i+1][k+1] = dp[i][k]+1;\n\t\t\t\t\tmemo[i+1][k+1] = 0;\n\t\t\t\t}else{\n\t\t\t\t\tif(dp[i][k+1] >= dp[i+1][k]){\n\t\t\t\t\t\tdp[i+1][k+1] = dp[i][k+1];\n\t\t\t\t\t\tmemo[i+1][k+1] = 1;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tdp[i+1][k+1] = dp[i+1][k];\n\t\t\t\t\t\tmemo[i+1][k+1] = -1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = length,k = length; i > 0 && k > 0;){\n\n\t\t\tif(memo[i][k] == 1){\n\t\t\t\ti--;\n\t\t\t}else if(memo[i][k] == -1){\n\t\t\t\tk--;\n\t\t\t}else{ //memo[i][k] == 0\n\t\t\t\tANS.push(buf[i-1]);\n\t\t\t\ti--;\n\t\t\t\tk--;\n\t\t\t}\n\t\t}\n\n\t\twhile(!ANS.empty()){\n\t\t\tprintf(\"%c\",ANS.top());\n\t\t\tANS.pop();\n\t\t}\n\n\t\tprintf(\"\\n\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 34288, "score_of_the_acc": -0.6457, "final_rank": 10 }, { "submission_id": "aoj_2035_2179720", "code_snippet": "#include<iostream>\n#include<string>\n#include<tuple>\n#include<algorithm>\nusing namespace std;\nstring S; tuple<int, int, int>dp[2100][2100];\nint solve(int s, int t) {\n\tif (s > t)return 0;\n\tif (get<0>(dp[s][t]) != -1)return get<0>(dp[s][t]);\n\tif (s == t) { dp[s][t] = make_tuple(1, -1, -1); return 1; }\n\tif (t - s == 1) {\n\t\tif (S[s] == S[t]) { dp[s][t] = make_tuple(2, -1, -1); return 2; }\n\t}\n\tint c1 = solve(s, t - 1);\n\tint c2 = solve(s + 1, t);\n\tint c3 = solve(s + 1, t - 1); if (S[s] == S[t])c3 += 2;\n\tif (c1 >= c2 && c1 >= c3) { dp[s][t] = make_tuple(c1, s, t - 1); return c1; }\n\tif (c2 >= c1 && c2 >= c3) { dp[s][t] = make_tuple(c2, s + 1, t); return c2; }\n\tif (c3 >= c1 && c3 >= c2) { dp[s][t] = make_tuple(c3, s + 1, t - 1); return c3; }\n}\nint main() {\n\twhile (cin >> S) {\n\t\tfor (int i = 0; i < S.size(); i++) { for (int j = 0; j < S.size(); j++)dp[i][j] = make_tuple(-1, 0, 0); }\n\t\tsolve(0, S.size() - 1);\n\t\tstring V, W; int F = 0, G = S.size() - 1;\n\t\twhile (F + G >= 0) {\n\t\t\tint H = get<1>(dp[F][G]), I = get<2>(dp[F][G]);\n\t\t\tif (H == -1 || ((H - F) + (G - I)) == 2) {\n\t\t\t\tV += S[F]; if (F != G)W += S[F];\n\t\t\t}\n\t\t\tF = H; G = I;\n\t\t}\n\t\treverse(W.begin(), W.end());\n\t\tcout << V + W << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3150, "memory_kb": 52512, "score_of_the_acc": -1.8417, "final_rank": 20 }, { "submission_id": "aoj_2035_2172806", "code_snippet": "#include <string>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nstring s; int dp[2009][2009];\nint main() {\n\twhile (cin >> s) {\n\t\tint n = s.size();\n\t\tfor (int i = 0; i < n; i++) dp[i][i + 1] = 1;\n\t\tfor (int i = 2; i <= n; i++) {\n\t\t\tfor (int l = 0; l <= n - i; l++) {\n\t\t\t\tint r = l + i;\n\t\t\t\tdp[l][r] = max(dp[l + 1][r], dp[l][r - 1]);\n\t\t\t\tif (s[l] == s[r - 1]) dp[l][r] = max(dp[l][r], dp[l + 1][r - 1] + 2);\n\t\t\t}\n\t\t}\n\t\tstring ret;\n\t\tint pl = 0, pr = n;\n\t\twhile (pr - pl >= 2) {\n\t\t\tif (s[pl] == s[pr - 1] && dp[pl][pr] == dp[pl + 1][pr - 1] + 2) ret += s[pl], pl++, pr--;\n\t\t\telse if (dp[pl][pr] == dp[pl + 1][pr]) pl++;\n\t\t\telse pr--;\n\t\t}\n\t\tstring ps = ret;\n\t\tif (pr - pl == 1) ps += s[pl];\n\t\treverse(ret.begin(), ret.end());\n\t\tps += ret;\n\t\tcout << ps << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 16756, "score_of_the_acc": -0.2358, "final_rank": 2 }, { "submission_id": "aoj_2035_2127858", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define F first\n#define S second\n#define all(v) (v).begin(), (v).end()\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)\n#define each(a, b) for(auto& a : b)\n\ntypedef pair<int, int> P;\n\nint dp[2010][2010];\nint tr[2010][2010];\n\nsigned main()\n{\n string s;\n while(cin >> s) {\n string t = s; reverse(all(s)); \n memset(dp, 0, sizeof(dp));\n memset(tr, 0, sizeof(dp));\n for(int i = 0; i < s.size(); i++) {\n for(int j = 0; j < t.size(); j++) {\n\tif(s[i] == t[j]) dp[i+1][j+1] = dp[i][j] + 1, tr[i+1][j+1] = 1;\n\telse {\n\t if(dp[i+1][j] > dp[i][j+1]) dp[i+1][j+1] = dp[i+1][j], tr[i+1][j+1] = 2;\n\t else dp[i+1][j+1] = dp[i][j+1], tr[i+1][j+1] = 3;\n\t}\n }\n }\n string x;\n for(int i = s.size(), j = t.size(); i > 0 && j > 0; ) {\n switch(tr[i][j]) {\n case 1: x += s[i-1], i--, j--; break;\n case 2: j--; break;\n case 3: i--; break;\n }\n }\n cout << x << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 34664, "score_of_the_acc": -0.752, "final_rank": 13 }, { "submission_id": "aoj_2035_2127341", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define N 2050\nstring ss[2][N+1];\nint c[N+1][N+1];\nstring lcs(string X,string Y){\n int m = X.size();\n int n = Y.size();\n int maxl = 0;\n X = ' ' + X;\n Y = ' ' + Y;\n ss[0][0] = \"\";\n memset(c,0,sizeof(c));\n for(int i=0;i<N+1;i++) ss[0][i]=ss[1][i]=\"\";\n for(int i=0;i<=m;i++) c[i][0]=0;\n for(int j=1;j<=n;j++) c[0][j]=0;\n for(int i=1;i<=m;i++){\n for(int j=1;j<=n;j++){\n if(X[i]==Y[j]) c[i][j]=c[i-1][j-1]+1, ss[i%2][j] = ss[(i-1)%2][j-1] + X[i];\n else {\n if(c[i-1][j] >= c[i][j-1]) c[i][j]=c[i-1][j], ss[i%2][j] = ss[(i-1)%2][j];\n else c[i][j] = c[i][j-1], ss[i%2][j] = ss[i%2][j-1];\n }\n maxl=max(maxl,c[i][j]);\n }\n }\n string ret=ss[m%2][n],ret2=ret;\n reverse(ret2.begin(),ret2.end());\n //assert(ret==ret2);\n assert((int)ret.size()==maxl);\n return ss[m%2][n];\n}\nint main(){\n string s;\n while(cin >> s) {\n string t = s;\n reverse(t.begin(), t.end());\n cout << lcs(s, t) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3720, "memory_kb": 25592, "score_of_the_acc": -1.3536, "final_rank": 19 }, { "submission_id": "aoj_2035_2127249", "code_snippet": "#include<bits/stdc++.h>\n#define N 2005\nusing namespace std;\nstring s,t,ans;\nint dp[N][N];\n\nvoid dfs(int y,int x){\n if(!y||!x)return;\n if(dp[y][x]-dp[y-1][x-1]==1&&dp[y][x]-dp[y-1][x]==1&&dp[y][x]-dp[y][x-1]==1){\n ans+=s[y-1];\n dfs(y-1,x-1);\n return;\n }\n if(dp[y][x-1]==dp[y][x])dfs(y,x-1);\n else dfs(y-1,x);\n}\n\nint main(){\n //IAIDDAII\n\n while(cin>>s){\n //s.resize(8);\n //for(int i=0;i<8;i++) s[i]='A'+(rand())%10;\n t=s;\n reverse(t.begin(),t.end());\n memset(dp,0,sizeof(dp));\n int n=s.size(),m=t.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n\tif(s[i]==t[j])dp[i+1][j+1]=dp[i][j]+1;\n\telse dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);\n }\n }\n ans=\"\";\n dfs(n,m);\n if(ans.size()%2==0){\n ans=ans.substr(0,ans.size()/2);\n string tmp=ans;reverse(tmp.begin(),tmp.end());\n ans=ans+tmp;\n }\n else {\n string tmp=ans.substr(0,ans.size()/2);\n reverse(tmp.begin(),tmp.end());\n ans=ans.substr(0,ans.size()/2)+ans[ans.size()/2]+tmp;\n }\n string temp=ans;\n reverse(temp.begin(),temp.end());\n assert(ans==temp);\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 18700, "score_of_the_acc": -0.3103, "final_rank": 7 }, { "submission_id": "aoj_2035_2127221", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define N 2050\nstring ss[2][N+1];\nint c[N+1][N+1];\nstring lcs(string X,string Y){\n int m = X.size();\n int n = Y.size();\n int maxl = 0;\n X = ' ' + X;\n Y = ' ' + Y;\n ss[0][0] = \"\";\n memset(c,0,sizeof(c));\n for(int i=0;i<N+1;i++) ss[0][i]=ss[1][i]=\"\";\n for(int i=0;i<=m;i++) c[i][0]=0;\n for(int j=1;j<=n;j++) c[0][j]=0;\n for(int i=1;i<=m;i++){\n for(int j=1;j<=n;j++){\n if(X[i]==Y[j]) c[i][j]=c[i-1][j-1]+1, ss[i%2][j] = ss[(i-1)%2][j-1] + X[i];\n else {\n\tif(c[i-1][j] >= c[i][j-1]) c[i][j]=c[i-1][j], ss[i%2][j] = ss[(i-1)%2][j];\n\telse c[i][j] = c[i][j-1], ss[i%2][j] = ss[i%2][j-1];\n }\n maxl=max(maxl,c[i][j]);\n }\n }\n string ret=ss[m%2][n],ret2=ret;\n reverse(ret2.begin(),ret2.end());\n //assert(ret==ret2);\n assert((int)ret.size()==maxl);\n return ss[m%2][n];\n}\nint main(){\n string s;\n while(cin >> s) {\n string t = s;\n reverse(t.begin(), t.end());\n cout << lcs(s, t) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3700, "memory_kb": 25592, "score_of_the_acc": -1.348, "final_rank": 18 }, { "submission_id": "aoj_2035_2127209", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n\nusing namespace std;\n\n#define TOPLEFT 0\n#define TOP 1\n#define LEFT 2\n#define MAX 2000\n\nvoid printLcs(string &l, vector<vector<short> > &b, const string &x, int i, int j, int k){\n if(i==0 || j==0) return;\n if(b[i][j]==TOPLEFT){\n printLcs(l, b, x, i-1, j-1, k-1);\n l[k] = x[i-1];\n }else if(b[i][j]==TOP){\n printLcs(l, b, x, i-1, j, k);\n }else{\n printLcs(l, b, x, i, j-1, k);\n }\n}\n\nvoid lcs(string &x, string &y, string &l){\n int m, n;\n vector<vector<short> > c, b;\n m = x.size();\n n = y.size();\n\n c.resize(m+1, vector<short>(n+1));\n b.resize(m+1, vector<short>(n+1));\n \n for(int i=0; i<=m; i++) c[i][0] = 0;\n for(int j=0; j<=n; j++) c[0][j] = 0;\n\n for(int i=1; i<=m; i++){\n for(int j=1; j<=n; j++){\n if(x[i-1]==y[j-1]){\n\tc[i][j] = c[i-1][j-1]+1;\n\tb[i][j] = TOPLEFT;\n }else if(c[i-1][j]>=c[i][j-1]){\n\tc[i][j] = c[i-1][j];\n\tb[i][j] = TOP;\n }else{\n\tc[i][j] = c[i][j-1];\n\tb[i][j] = LEFT;\n }\n }\n }\n\n l.resize(c[m][n]);\n\n printLcs(l, b, x, m, n, l.size()-1);\n}\n\nint main(){\n string s1, s2, l;\n while( cin >> s1 ){\n\ts2 = s1;\n\treverse(s2.begin(), s2.end());\n\tlcs(s1, s2, l);\n\tcout << l << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 18672, "score_of_the_acc": -0.3513, "final_rank": 8 }, { "submission_id": "aoj_2035_2058682", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)\n#define rep(i,n) repi(i,0,n)\n \nstring S;\nint dp[2222][2222];\nint trans[2222][2222];\n\nint rec(int L, int R) {\n int& ret = dp[L][R];\n if(ret + 1) return ret;\n int& recons = trans[L][R];\n if(L > R) return ret = 0;\n if(S[L] == S[R]) {\n recons = 0;\n return ret = rec(L + 1, R - 1) + (L == R ? 1 : 2);\n }\n \n int r1 = rec(L + 1, R);\n int r2 = rec(L, R - 1);\n \n if(r1 > r2) {\n recons = 1;\n return ret = r1;\n }\n else {\n recons = 2;\n return ret = r2;\n }\n \n}\n \nint main() {\n while(cin >> S) {\n \n for(int i=0; i<2222; i++)\n for(int j=0; j<2222; j++)\n dp[i][j] = trans[i][j] = -1;\n \n int N = S.size();\n rec(0, N);\n int L = 0, R = N;\n string res;\n while(L < R) {\n if(trans[L][R] == 0) res += S[L], L ++, R --;\n if(trans[L][R] == 1) L ++;\n if(trans[L][R] == 2) R --;\n }\n cout << res;\n if(L == R) cout << S[L];\n reverse(res.begin(), res.end());\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1860, "memory_kb": 41720, "score_of_the_acc": -1.2242, "final_rank": 16 }, { "submission_id": "aoj_2035_2057661", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)\n#define rep(i,n) repi(i,0,n)\n\nstring S;\nint dp[2222][2222];\nint trans[2222][2222];\n\nconst int rec(const int L, const int R) {\n int& ret = dp[L][R];\n if(ret + 1) return ret;\n if(L > R) return ret = 0;\n if(S[L] == S[R]) {\n trans[L][R] = 0;\n return ret = rec(L + 1, R - 1) + (L == R ? 1 : 2);\n }\n\n const int r1 = rec(L + 1, R);\n const int r2 = rec(L, R - 1);\n\n if(r1 > r2) {\n trans[L][R] = 1;\n return ret = r1;\n }\n else {\n trans[L][R] = 2;\n return ret = r2;\n }\n\n}\n\nint main() {\n while(cin >> S) {\n\n for(int i=0; i<2222; i++)\n for(int j=0; j<2222; j++)\n dp[i][j] = trans[i][j] = -1;\n\n int N = S.size();\n rec(0, N);\n int L = 0, R = N;\n string res;\n while(L < R) {\n if(trans[L][R] == 0) {\n res += S[L];\n L ++, R --;\n }\n if(trans[L][R] == 1) {\n L ++;\n }\n if(trans[L][R] == 2) {\n R --;\n }\n }\n cout << res;\n if(L == R) cout << S[L];\n reverse(res.begin(), res.end());\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1860, "memory_kb": 41764, "score_of_the_acc": -1.2252, "final_rank": 17 }, { "submission_id": "aoj_2035_1896716", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nconst int LMAX=2010;\nint dp[LMAX+1][LMAX+1],from[LMAX+1][LMAX+1];\nstring LCS(string &a,string &b){\n\tint m=a.size(),n=b.size();\n\trep(i,m+1)dp[i][0]=0,from[i][0]=-1;\n\trep(i,n+1)dp[0][i]=0,from[0][i]=-1;\n\trep(i,m)rep(j,n){\n\t\tif(a[i]==b[j]){\n\t\t\tdp[i+1][j+1]=dp[i][j]+1;\n\t\t\tfrom[i+1][j+1]=2;\n\t\t}else{\n\t\t\tif(dp[i][j+1]<dp[i+1][j]){\n\t\t\t\tdp[i+1][j+1]=dp[i+1][j];\n\t\t\t\tfrom[i+1][j+1]=1;\n\t\t\t}else{\n\t\t\t\tdp[i+1][j+1]=dp[i][j+1];\n\t\t\t\tfrom[i+1][j+1]=0;\n\t\t\t}\n\t\t}\n\t}\n\tint idx=dp[m][n];\n\tstring out(idx,'!');\n\tfor(int i=m,j=n;~from[i][j];){\n\t\tswitch(from[i][j]){\n\t\t\tcase 0:i--;break;\n\t\t\tcase 1:j--;break;\n\t\t\tcase 2:i--;j--;idx--;out[idx]=a[i];break;\n\t\t}\n\t}\n\treturn out;\n}\nint main(){\n\tstring s;\n\twhile(cin>>s){\n\t\tstring t=s;\n\t\treverse(all(t));\n\t\tcout<<LCS(s,t)<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 32716, "score_of_the_acc": -0.6552, "final_rank": 11 } ]
aoj_2041_cpp
Problem C: Disarmament of the Units This is a story in a country far away. In this country, two armed groups, ACM and ICPC, have fought in the civil war for many years. However, today October 21, reconciliation was approved between them; they have marked a turning point in their histories. In this country, there are a number of roads either horizontal or vertical, and a town is built on every endpoint and every intersection of those roads (although only one town is built at each place). Some towns have units of either ACM or ICPC. These units have become unneeded by the reconciliation. So, we should make them disarm in a verifiable way simultaneously. All disarmament must be carried out at the towns designated for each group, and only one unit can be disarmed at each town. Therefore, we need to move the units. The command of this mission is entrusted to you. You can have only one unit moved by a single order, where the unit must move to another town directly connected by a single road. You cannot make the next order to any unit until movement of the unit is completed. The unit cannot pass nor stay at any town another unit stays at. In addition, even though reconciliation was approved, members of those units are still irritable enough to start battles. For these reasons, two units belonging to different groups may not stay at towns on the same road. You further have to order to the two groups alternatively, because ordering only to one group may cause their dissatisfaction. In spite of this restriction, you can order to either group first. Your job is to write a program to find the minimum number of orders required to complete the mission of disarmament. Input The input consists of multiple datasets. Each dataset has the following format: n m A m I rx 1,1 ry 1,1 rx 1,2 ry 1,2 ... rx n ,1 ry n ,1 rx n ,2 ry n ,2 sx A ,1 sy A ,1 ... sx A , m A sy A , m A sx I ,1 sy I ,1 ... sx I , m I sy I , m I tx A ,1 ty A ,1 ... tx A , m A ty A , m A tx I ,1 ty I ,1 ... tx I , m I ty I , m I n is the number of roads. m A and m I are the number of units in ACM and ICPC respectively ( m A > 0, m I > 0, m A + m B < 8). ( rx i ,1 , ry i ,1 ) and ( rx i ,2 , ry i ,2 ) indicate the two endpoints of the i -th road, which is always parallel to either x -axis or y -axis. A series of ( sx A , i , sy A , i ) indicates the coordinates of the towns where the ACM units initially stay, and a series of ( sx I , i , sy I , i ) indicates where the ICPC units initially stay. A series of ( tx A , i , ty A , i ) indicates the coordinates of the towns where the ACM units can be disarmed, and a series of ( tx I , i , ty I , i ) indicates where the ICPC units can be disarmed. You may assume the following: the number of towns, that is, the total number of coordinates where endpoints and/or intersections of the roads exist does not exceed 18; no two horizontal roads are connected nor ovarlapped; no two vertical roads, either; all the towns are connected by roads; and no pair of ACM and ...(truncated)
[ { "submission_id": "aoj_2041_124929", "code_snippet": "#include<iostream>\n#include<complex>\n#include<map>\n#include<algorithm>\n#include<set>\n#include<queue>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define mp make_pair\n#define ALL(c) (c).begin(),(c).end()\n\ntypedef complex<int> P;\ntypedef pair<P,P> Line;\ntypedef unsigned long long ull;\n\nconst int N = 20;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n if (a.real() != b.real())return a.real() < b.real();\n return a.imag() < b.imag();\n }\n}\n\nbool isp(P a,P b,P c){\n complex<double> A(a.real(),a.imag()),B(b.real(),b.imag()),C(c.real(),c.imag());\n return abs(A-C)+abs(B-C) < abs(A-B) + 1e-10;\n}\n\nbool isptable[N][N][N];\nbool edge[N][N];\n\nenum {icpcgoal=0,acmgoal=1,els=2};\nenum {icpc=0,acm=1};\nint isgoal[N];\n\nbool isicpc[8];\nclass state{\npublic:\n int n;\n int goal;\n short cnt;\n bool lastmove;\n short node[8];\n ull st;\n bool isclear();\n void makest();\n bool isvalid(int,int,vector<P>&);\n bool operator<(const state& a)const;\n};\n\nbool state::operator<(const state& a)const{\n return st < a.st;\n}\n\nbool state::isclear(){\n int cnt =0;\n rep(i,n){\n if (isicpc[i] &&isgoal[node[i]]==icpcgoal)cnt++;\n else if (!isicpc[i]&&isgoal[node[i]]==acmgoal )cnt++;\n }\n return cnt == n;\n}\n\nbool state::isvalid(int ii,int prev,vector<P>&p){\n rep(i,n){\n if (i == ii)continue;\n if (isptable[prev][node[ii]][node[i]])\n return false;\n }\n rep(i,n){\n REP(j,i+1,n){\n if (node[i] == node[j])return false;\n if (isicpc[i] != isicpc[j] &&\n\t edge[node[i]][node[j]])return false;\n }\n }\n return true;\n}\n\nvoid state::makest(){\n st=lastmove;\n rep(i,n){\n st=st*18 + node[i];\n }\n}\n\nint bfs(int n,state ini,vector<P> &node,int ma){\n queue<state> Q; \n set<ull> S;\n ini.lastmove=icpc;\n ini.makest();\n Q.push(ini);\n S.insert(ini.st);\n ini.lastmove=acm;\n ini.makest();\n Q.push(ini);\n S.insert(ini.st);\n if (ini.isclear())return 0;\n\n while(!Q.empty()){\n state now = Q.front();Q.pop();\n int curmove=now.lastmove==icpc?acm:icpc;\n rep(i,now.n){\n if (curmove==icpc && !isicpc[i])continue;\n if (curmove==acm && isicpc[i])continue;\n rep(j,n){\n\tif (!edge[now.node[i]][j])continue;\n\tstate next = now;\n\tnext.cnt++;\n\tnext.lastmove=curmove;\n\tnext.node[i]=j;\n\tif (!next.isvalid(i,now.node[i],node))continue;\n\tsort(next.node,next.node+ma);\n\tsort(next.node+ma,next.node+next.n);\n\tnext.makest();\n\tif (S.find(next.st) == S.end()){\n\t if (next.isclear()){\n\t return next.cnt;\n\t }\n\t S.insert(next.st);\n\t Q.push(next);\n\t}\n }\n } \n }\n return -1;\n}\n\nint getindex(P &a,map<P,int> &M){\n int index = M.size();\n if (M.find(a) == M.end())M[a]=index;\n return M[a];\n}\n\nvoid make_intersection(vector<Line> ver,vector<Line>hor, map<P,int> &M){\n rep(i,ver.size()){\n rep(j,hor.size()){\n if (ver[i].first.imag() <= hor[j].first.imag() &&\n\t ver[i].second.imag() >= hor[j].first.imag() &&\n\t hor[j].first.real() <= ver[i].first.real() &&\n\t hor[j].second.real() >= ver[i].first.real()){\n\tP ins(ver[i].first.real(),hor[j].first.imag());\n\tgetindex(ins,M);\n }\n }\n }\n}\n\nvoid make_graph(vector<P> node,vector<Line> ver,\n\t\tmap<P,int> M){\n rep(i,ver.size()){\n vector<int> connect;\n rep(j,node.size()){\n if (isptable[M[ver[i].first]][M[ver[i].second]][j]){\n\tconnect.pb(M[node[j]]);\n }\n }\n rep(j,connect.size()){\n REP(k,j+1,connect.size()){\n\tedge[connect[j]][connect[k]]=edge[connect[k]][connect[j]]=true;\n }\n }\n }\n}\n\nmain(){\n int n,ma,mi;\n while(cin>>n>>ma>>mi && n){\n rep(i,N){\n rep(j,N)edge[i][j]=false;\n isgoal[i]=els;\n }\n rep(i,N)rep(j,N)rep(k,N)isptable[i][j][k]=false;\n vector<Line> hor,ver;\n map<P,int> M;\n rep(i,n){\n Line ins;\n cin>>ins.first.real()>>ins.first.imag()\n\t >>ins.second.real()>>ins.second.imag();\n getindex(ins.first,M);\n getindex(ins.second,M);\n if (ins.second < ins.first)swap(ins.first,ins.second);\n if (ins.first.real() == ins.second.real())ver.pb(ins);\n else hor.pb(ins);\n }\n \n state ini;\n ini.cnt=0;\n ini.n=ma+mi;\n ini.goal=0;\n rep(i,ma){\n P a;\n cin>>a.real()>>a.imag();\n isicpc[i]=false;\n ini.node[i] =getindex(a,M);\n }\n rep(i,mi){\n P b;\n cin>>b.real()>>b.imag();\n isicpc[ma+i]=true;\n ini.node[ma+i] =getindex(b,M);\n }\n\n rep(i,ma){\n P a;\n cin>>a.real()>>a.imag();\n isgoal[getindex(a,M)]=acmgoal;\n }\n rep(j,mi){\n P a;\n cin>>a.real()>>a.imag();\n isgoal[getindex(a,M)]=icpcgoal;\n }\n\n make_intersection(ver,hor,M);\n map<P,int>::iterator itr = M.begin();\n vector<P> node(M.size());\n while(itr != M.end()){\n node[(*itr).second]=(*itr).first;\n itr++;\n }\n\n rep(i,node.size()){\n rep(j,node.size()){\n\trep(k,node.size()){\n\t if (isp(node[i],node[j],node[k]))isptable[i][j][k]=true;\n\t}\n } \n }\n\n make_graph(node,hor,M);\n make_graph(node,ver,M);\n rep(i,ini.n){\n if ( isicpc[i]&&isgoal[ini.node[i]]==icpcgoal)ini.goal++;\n else if (!isicpc[i]&&isgoal[ini.node[i]]==acmgoal )ini.goal++;\n }\n\n sort(ini.node,ini.node+ma);\n sort(ini.node+ma,ini.node+ini.n);\n cout << bfs(M.size(),ini,node,ma)<<endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 6420, "memory_kb": 5028, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2041_124927", "code_snippet": "#include<iostream>\n#include<complex>\n#include<map>\n#include<algorithm>\n#include<set>\n#include<queue>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define mp make_pair\n#define ALL(c) (c).begin(),(c).end()\n\ntypedef complex<int> P;\ntypedef pair<P,P> Line;\ntypedef unsigned long long ull;\n\nconst int N = 20;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n if (a.real() != b.real())return a.real() < b.real();\n return a.imag() < b.imag();\n }\n}\n\nbool isp(P a,P b,P c){\n complex<double> A(a.real(),a.imag()),B(b.real(),b.imag()),C(c.real(),c.imag());\n return abs(A-C)+abs(B-C) < abs(A-B) + 1e-10;\n}\n\nbool isptable[N][N][N];\n\nbool edge[N][N];\n\nenum GOAL{icpcgoal=0,acmgoal=1,els=2};\nenum ICPC{icpc=0,acm=1};\nint isgoal[N];\n\nbool isicpc[8];\nclass state{\npublic:\n int n;\n int goal;\n short cnt;\n bool lastmove;\n short node[8];\n ull st;\n bool isclear();\n void makest();\n bool isvalid(int,int,vector<P>&);\n bool operator<(const state& a)const;\n};\n\nbool state::operator<(const state& a)const{\n return st < a.st;\n}\n\nbool state::isclear(){\n int cnt =0;\n rep(i,n){\n if (isicpc[i] &&isgoal[node[i]]==icpcgoal)cnt++;\n else if (!isicpc[i]&&isgoal[node[i]]==acmgoal )cnt++;\n }\n return cnt == n;\n}\n\nbool state::isvalid(int ii,int prev,vector<P>&p){\n rep(i,n){\n if (i == ii)continue;\n if (isptable[prev][node[ii]][node[i]])\n return false;\n }\n rep(i,n){\n REP(j,i+1,n){\n if (node[i] == node[j])return false;\n if (isicpc[i] != isicpc[j] &&\n\t edge[node[i]][node[j]])return false;\n }\n }\n return true;\n}\n\nvoid state::makest(){\n st=lastmove;\n rep(i,n){\n st=st*18 + node[i];\n }\n}\n\nint bfs(int n,state ini,vector<P> &node,int ma){\n queue<state> Q; \n set<ull> S;\n ini.lastmove=icpc;\n ini.makest();\n Q.push(ini);\n S.insert(ini.st);\n ini.lastmove=acm;\n ini.makest();\n Q.push(ini);\n S.insert(ini.st);\n if (ini.isclear())return 0;\n\n while(!Q.empty()){\n state now = Q.front();Q.pop();\n int curmove=now.lastmove==icpc?acm:icpc;\n rep(i,now.n){\n if (curmove==icpc && !isicpc[i])continue;\n if (curmove==acm && isicpc[i])continue;\n rep(j,n){\n\tif (!edge[now.node[i]][j])continue;\n\tstate next = now;\n\tnext.cnt++;\n\tnext.lastmove=curmove;\n\tnext.node[i]=j;\n\tif (!next.isvalid(i,now.node[i],node))continue;\n\tsort(next.node,next.node+ma);\n\tsort(next.node+ma,next.node+next.n);\n\tnext.makest();\n\tif (S.find(next.st) == S.end()){\n\t if (next.isclear()){\n\t return next.cnt;\n\t }\n\t S.insert(next.st);\n\t Q.push(next);\n\t}\n }\n } \n }\n return -1;\n}\n\nint getindex(P &a,map<P,int> &M){\n int index = M.size();\n if (M.find(a) == M.end())M[a]=index;\n return M[a];\n}\n\nvoid make_intersection(vector<Line> ver,vector<Line>hor, map<P,int> &M){\n rep(i,ver.size()){\n rep(j,hor.size()){\n if (ver[i].first.imag() <= hor[j].first.imag() &&\n\t ver[i].second.imag() >= hor[j].first.imag() &&\n\t hor[j].first.real() <= ver[i].first.real() &&\n\t hor[j].second.real() >= ver[i].first.real()){\n\tP ins(ver[i].first.real(),hor[j].first.imag());\n\tgetindex(ins,M);\n }\n }\n }\n}\n\nvoid make_graph(vector<P> node,vector<Line> ver,\n\t\tmap<P,int> M){\n rep(i,ver.size()){\n vector<int> connect;\n rep(j,node.size()){\n if (isptable[M[ver[i].first]][M[ver[i].second]][j]){\n\tconnect.pb(M[node[j]]);\n }\n }\n rep(j,connect.size()){\n REP(k,j+1,connect.size()){\n\tedge[connect[j]][connect[k]]=edge[connect[k]][connect[j]]=true;\n }\n }\n }\n}\n\nmain(){\n int n,ma,mi;\n while(cin>>n>>ma>>mi && n){\n rep(i,N){\n rep(j,N)edge[i][j]=false;\n isgoal[i]=els;\n }\n rep(i,N)rep(j,N)rep(k,N)isptable[i][j][k]=false;\n vector<Line> hor,ver;\n map<P,int> M;\n rep(i,n){\n Line ins;\n cin>>ins.first.real()>>ins.first.imag()\n\t >>ins.second.real()>>ins.second.imag();\n getindex(ins.first,M);\n getindex(ins.second,M);\n if (ins.second < ins.first)swap(ins.first,ins.second);\n if (ins.first.real() == ins.second.real())ver.pb(ins);\n else hor.pb(ins);\n }\n \n state ini;\n ini.cnt=0;\n ini.n=ma+mi;\n ini.goal=0;\n rep(i,ma){\n P a;\n cin>>a.real()>>a.imag();\n isicpc[i]=false;\n ini.node[i] =getindex(a,M);\n }\n rep(i,mi){\n P b;\n cin>>b.real()>>b.imag();\n isicpc[ma+i]=true;\n ini.node[ma+i] =getindex(b,M);\n }\n\n rep(i,ma){\n P a;\n cin>>a.real()>>a.imag();\n isgoal[getindex(a,M)]=acmgoal;\n }\n rep(j,mi){\n P a;\n cin>>a.real()>>a.imag();\n isgoal[getindex(a,M)]=icpcgoal;\n }\n\n make_intersection(ver,hor,M);\n map<P,int>::iterator itr = M.begin();\n vector<P> node(M.size());\n while(itr != M.end()){\n node[(*itr).second]=(*itr).first;\n itr++;\n }\n\n rep(i,node.size()){\n rep(j,node.size()){\n\trep(k,node.size()){\n\t if (isp(node[i],node[j],node[k]))isptable[i][j][k]=true;\n\t}\n } \n }\n\n make_graph(node,hor,M);\n make_graph(node,ver,M);\n rep(i,ini.n){\n if ( isicpc[i]&&isgoal[ini.node[i]]==icpcgoal)ini.goal++;\n else if (!isicpc[i]&&isgoal[ini.node[i]]==acmgoal )ini.goal++;\n }\n\n sort(ini.node,ini.node+ma);\n sort(ini.node+ma,ini.node+ini.n);\n cout << bfs(M.size(),ini,node,ma)<<endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 6460, "memory_kb": 5028, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_2041_124925", "code_snippet": "#include<iostream>\n#include<complex>\n#include<map>\n#include<algorithm>\n#include<set>\n#include<queue>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define mp make_pair\n#define ALL(c) (c).begin(),(c).end()\n\ntypedef complex<int> P;\ntypedef pair<P,P> Line;\ntypedef unsigned long long ull;\n\nconst int N = 20;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n if (a.real() != b.real())return a.real() < b.real();\n return a.imag() < b.imag();\n }\n}\n\nbool isp(P a,P b,P c){\n complex<double> A(a.real(),a.imag()),B(b.real(),b.imag()),C(c.real(),c.imag());\n return abs(A-C)+abs(B-C) < abs(A-B) + 1e-10;\n}\n\nbool isptable[N][N][N];\n\nbool edge[N][N];\n\nenum GOAL{icpcgoal=0,acmgoal=1,els=2};\nenum ICPC{icpc=0,acm=1};\nint isgoal[N];\n\nbool isicpc[8];\nclass state{\npublic:\n int n;\n int goal;\n short cnt;\n bool lastmove;\n short node[8];\n ull st;\n bool isclear();\n void makest();\n bool isvalid(int,int,vector<P>&);\n bool operator<(const state& a)const;\n};\n\nbool state::operator<(const state& a)const{\n /*\n if (lastmove != a.lastmove)return lastmove<a.lastmove;\n rep(i,n){\n if (node[i] != a.node[i])return node[i] < a.node[i];\n }\n */\n return st < a.st;\n // return false;\n}\n\nbool state::isclear(){\n //return goal == n;\n \n int cnt =0;\n rep(i,n){\n if (isicpc[i] &&isgoal[node[i]]==icpcgoal)cnt++;\n else if (!isicpc[i]&&isgoal[node[i]]==acmgoal )cnt++;\n }\n return cnt == n;\n \n}\n\nbool state::isvalid(int ii,int prev,vector<P>&p){\n rep(i,n){\n if (i == ii)continue;\n if (isptable[prev][node[ii]][node[i]])\n return false;\n }\n rep(i,n){\n REP(j,i+1,n){\n if (node[i] == node[j])return false;\n if (isicpc[i] != isicpc[j] &&\n\t edge[node[i]][node[j]])return false;\n }\n }\n return true;\n}\n\nvoid state::makest(){\n st=lastmove;\n rep(i,n){\n st=st*18 + node[i];\n }\n}\n\nint bfs(int n,state ini,vector<P> &node,int ma){\n queue<state> Q; \n set<ull> S;\n ini.lastmove=icpc;\n ini.makest();\n Q.push(ini);\n S.insert(ini.st);\n ini.lastmove=acm;\n ini.makest();\n Q.push(ini);\n S.insert(ini.st);\n if (ini.isclear())return 0;\n\n while(!Q.empty()){\n state now = Q.front();Q.pop();\n // if (now.isclear()){\n // return now.cnt;\n // }\n int curmove=now.lastmove==icpc?acm:icpc;\n rep(i,now.n){\n if (curmove==icpc && !isicpc[i])continue;\n if (curmove==acm && isicpc[i])continue;\n // if (isicpc[i] &&isgoal[now.node[i]]==icpcgoal)continue;\n //else if (!isicpc[i]&&isgoal[now.node[i]]==acmgoal)continue;\n rep(j,n){\n\tif (!edge[now.node[i]][j])continue;\n\tstate next = now;\n\tnext.cnt++;\n\tnext.lastmove=curmove;\n\tnext.node[i]=j;\n\tif (!next.isvalid(i,now.node[i],node))continue;\n\tsort(next.node,next.node+ma);\n\tsort(next.node+ma,next.node+next.n);\n\tnext.makest();\n\tif (S.find(next.st) == S.end()){\n\t /*\n\t cout << next.cnt << endl;\n\t rep(k,now.n){\n\t cout << node[now.node[k]]<<\" \" << node[next.node[k]]<<\n\t \" \"<< now.isicpc[k] << endl;\n\t }\n\t cout << \"end \"<< endl;\n\t */\n\t //if ( isicpc[i]&&isgoal[next.node[i]]==icpcgoal)next.goal++;\n\t //\t else if (!isicpc[i]&&isgoal[next.node[i]]==acmgoal )next.goal++;\n\n\t if (next.isclear()){\n\t //cout << S.size()<<\" \" << Q.size() << endl;\n\t return next.cnt;\n\t }\n\t S.insert(next.st);\n\t Q.push(next);\n\t}\n }\n } \n }\n return -1;\n}\n\nint getindex(P &a,map<P,int> &M){\n int index = M.size();\n if (M.find(a) == M.end())M[a]=index;\n return M[a];\n}\n\nvoid make_intersection(vector<Line> ver,vector<Line>hor, map<P,int> &M){\n rep(i,ver.size()){\n \n rep(j,hor.size()){\n if (ver[i].first.imag() <= hor[j].first.imag() &&\n\t ver[i].second.imag() >= hor[j].first.imag() &&\n\t hor[j].first.real() <= ver[i].first.real() &&\n\t hor[j].second.real() >= ver[i].first.real()){\n\tP ins(ver[i].first.real(),hor[j].first.imag());\n\tgetindex(ins,M);\n }\n }\n }\n}\n\nvoid make_graph(vector<P> node,vector<Line> ver,\n\t\tmap<P,int> M){\n rep(i,ver.size()){\n vector<int> connect;\n rep(j,node.size()){\n // if (isp(ver[i].first,ver[i].second,node[j])){\n if (isptable[M[ver[i].first]][M[ver[i].second]][j]){\n\t//cout << isptable[M[ver[i].first]][M[ver[i].second]][j] << endl;\n\tconnect.pb(M[node[j]]);\n }\n }\n \n rep(j,connect.size()){\n REP(k,j+1,connect.size()){\n\tedge[connect[j]][connect[k]]=true;\n\tedge[connect[k]][connect[j]]=true;\n }\n }\n }\n}\n\nmain(){\n int n,ma,mi;\n while(cin>>n>>ma>>mi && n){\n rep(i,N){\n rep(j,N)edge[i][j]=false;\n isgoal[i]=els;\n }\n rep(i,N)rep(j,N)rep(k,N)isptable[i][j][k]=false;\n vector<Line> hor,ver;\n map<P,int> M;\n rep(i,n){\n Line ins;\n cin>>ins.first.real()>>ins.first.imag()\n\t >>ins.second.real()>>ins.second.imag();\n getindex(ins.first,M);\n getindex(ins.second,M);\n if (ins.second < ins.first)swap(ins.first,ins.second);\n if (ins.first.real() == ins.second.real())ver.pb(ins);\n else hor.pb(ins);\n }\n \n state ini;\n ini.cnt=0;\n ini.n=ma+mi;\n ini.goal=0;\n rep(i,ma){\n P a;\n cin>>a.real()>>a.imag();\n isicpc[i]=false;\n ini.node[i] =getindex(a,M);\n }\n rep(i,mi){\n P b;\n cin>>b.real()>>b.imag();\n isicpc[ma+i]=true;\n ini.node[ma+i] =getindex(b,M);\n }\n\n rep(i,ma){\n P a;\n cin>>a.real()>>a.imag();\n isgoal[getindex(a,M)]=acmgoal;\n }\n rep(j,mi){\n P a;\n cin>>a.real()>>a.imag();\n isgoal[getindex(a,M)]=icpcgoal;\n }\n\n\n make_intersection(ver,hor,M);\n map<P,int>::iterator itr = M.begin();\n vector<P> node(M.size());\n while(itr != M.end()){\n node[(*itr).second]=(*itr).first;\n itr++;\n }\n\n rep(i,node.size()){\n rep(j,node.size()){\n\trep(k,node.size()){\n\t if (isp(node[i],node[j],node[k]))isptable[i][j][k]=true;\n\t}\n } \n }\n\n make_graph(node,hor,M);\n make_graph(node,ver,M);\n rep(i,ini.n){\n if ( isicpc[i]&&isgoal[ini.node[i]]==icpcgoal)ini.goal++;\n else if (!isicpc[i]&&isgoal[ini.node[i]]==acmgoal )ini.goal++;\n }\n\n sort(ini.node,ini.node+ma);\n sort(ini.node+ma,ini.node+ini.n);\n // rep(i,ini.n){\n // cout << i <<\" \" << node[ini.node[i]]<<\" \" << isicpc[i] << endl;\n // }\n /* \n rep(i,M.size()){\n cout << node[i] <<\"-\";\n rep(j,M.size()){\n\tif (edge[i][j])cout << node[j];\n }\n cout << endl;\n }\n cout <<\"end \"<< endl;\n */ \n \n /* rep(i,node.size()){\n cout <<i <<\" \" << node[i] << endl;\n }\n rep(i,M.size()){\n rep(j,M.size()){\n\tif (edge[i][j] != edge[j][i]){\n\t cout <<\"error\" << endl;\n\t exit(1);\n\t}\n\tcout << edge[i][j];\n }\n cout << endl;\n }\n */ \n\n cout << bfs(M.size(),ini,node,ma)<<endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 6420, "memory_kb": 5032, "score_of_the_acc": -1, "final_rank": 2 } ]
aoj_2033_cpp
Problem D: Rock Man You were the master craftsman in the stone age, and you devoted your life to carving many varieties of tools out of natural stones. Your works have ever been displayed respectfully in many museums, even in 2006 A.D. However, most people visiting the museums do not spend so much time to look at your works. Seeing the situation from the Heaven, you brought back memories of those days you had frantically lived. One day in your busiest days, you got a number of orders for making tools. Each order requested one tool which was different from the other orders. It often took some days to fill one order without special tools. But you could use tools which you had already made completely, in order to carve new tools more quickly. For each tool in the list of the orders, there was exactly one tool which could support carving it. In those days, your schedule to fill the orders was not so efficient. You are now thinking of making the most efficient schedule, i.e. the schedule for all orders being filled in the shortest days, using the program you are going to write. Input The input consists of a series of test cases. Each case begins with a line containing a single integer N which represents the number of ordered tools. Then N lines follow, each specifying an order by four elements separated by one or more space: name , day 1 , sup and day 2 . name is the name of the tool in the corresponding order. day 1 is the number of required days to make the tool without any support tool. sup is the name of the tool that can support carving the target tool. day 2 is the number of required days make the tool with the corresponding support tool. The input terminates with the case where N = 0. You should not process this case. You can assume that the input follows the constraints below: N ≤ 1000; name consists of no longer than 32 non-space characters, and is unique in each case; sup is one of all names given in the same case; and 0 < day 2 < day 1 < 20000. Output For each test case, output a single line containing an integer which represents the minimum number of days required to fill all N orders. Sample Input 3 gu 20 pa 10 ci 20 gu 10 pa 20 ci 10 2 tool 20 tool 10 product 1000 tool 10 8 fishhook 21 disk 3 mill 14 axe 7 boomerang 56 sundial 28 flint 35 fishhook 5 axe 35 flint 14 disk 42 disk 2 sundial 14 disk 7 hammer 28 mill 7 0 Output for the Sample Input 40 30 113
[ { "submission_id": "aoj_2033_10852644", "code_snippet": "#include <bits/stdc++.h>\n \n#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )\n#define REP(i,n) FOR(i,0,n)\n#define YYS(x,arr) for(auto& x:arr)\n#define ALL(x) (x).begin(),(x).end()\n#define SORT(x) sort( (x).begin(),(x).end() )\n#define REVERSE(x) reverse( (x).begin(),(x).end() )\n#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )\n#define PW(x) (1LL<<(x))\n#define SZ(x) ((ll)(x).size())\n#define SHOW(x) cout << #x << \" = \" << x << endl\n#define SHOWA(x,n) for( int yui = 0; yui < n; yui++ ){ cout << x[yui] << \" \"; } cout << endl\n\n#define pb emplace_back\n#define fi first\n#define se second\n\nusing namespace std;\n\ntypedef long double ld;\ntypedef long long int ll;\ntypedef pair<int,int> pi;\ntypedef pair<ll,ll> pl;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<bool> vb;\ntypedef vector<ld> vd;\ntypedef vector<pi> vpi;\ntypedef vector<pl> vpl;\ntypedef vector<vpl> gr;\ntypedef vector<vl> ml;\ntypedef vector<vd> md;\ntypedef vector<vi> mi;\n \nconst ll INF = (ll)1e9 + 10;\nconst ll INFLL = (ll)1e18 + 10;\nconst ld EPS = 1e-12;\nconst ll MOD = 1e9+7;\n \ntemplate<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }\ntemplate<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }\ntemplate<class T> inline T sq( T a ){ return a * a; }\n\nll in(){ long long int x; scanf( \"%lld\" , &x ); return x; }\nchar yuyushiki[1000010]; string stin(){ scanf( \"%s\" , yuyushiki ); return yuyushiki; }\n\n// head\n\nint n;\nmap<string,int> ma;\nint cnt;\n\nint c[1010];\nint d[1010];\nint to[1010];\n\nvi cycle;\nmi cs;\n\nint used[1010];\n\nvoid dfs( int x ){\n used[x] = 1;\n\n if( used[ to[x] ] == 0 ){\n dfs( to[x] );\n } else if( used[ to[x] ] == 1 ){\n int cur = x;\n do{\n cycle.pb( cur );\n cur = to[cur];\n } while( cur != x );\n }\n \n used[x] = 2;\n}\n\nint id( string s ){\n if( ma.find( s ) == ma.end() ){\n return ma[s] = cnt++;\n }\n return ma[s];\n}\n\nvoid solve(){\n ma.clear();\n cnt = 0;\n REP( i , n ){\n int a = id( stin() );\n c[a] = in();\n int b = id( stin() );\n d[a] = in();\n to[a] = b;\n }\n cs.clear();\n REP( i , n ){\n used[i] = 0;\n }\n REP( i , n ){\n if( used[i] == 0 ){\n cycle.clear();\n dfs( i );\n if( SZ(cycle) > 0 ){\n cs.pb( cycle );\n }\n }\n }\n int ans = 0;\n REP( i , n ){\n ans += d[i];\n }\n YYS( v , cs ){\n vi ws(0);\n YYS( w , v ){\n ws.pb( c[w] - d[w] );\n }\n SORT( ws );\n ans += ws[0];\n }\n printf( \"%d\\n\" , ans );\n}\n\nint main(){\n\n while( 1 ){\n n = in();\n if( n == 0 ){\n break;\n }\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3604, "score_of_the_acc": -1, "final_rank": 16 }, { "submission_id": "aoj_2033_2859241", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\nstruct Dscc {\npublic:\n\n\t//fst:belongs , scd:newedges\n\tpair<vector<vector<int>>, vector<vector<int>>>get(const vector<vector<int>>&edges) {\n\t\tnums.resize(edges.size());\n\t\tfill(nums.begin(), nums.end(), -1);\n\t\tvector<vector<int>>revedges(edges.size());\n\t\tfor (int i = 0; i < edges.size(); ++i) {\n\t\t\tfor (auto j : edges[i]) {\n\t\t\t\trevedges[j].push_back(i);\n\t\t\t}\n\t\t}\n\t\tint num = 0;\n\t\tfor (int i = 0; i < edges.size(); ++i) {\n\t\t\tdfs(i, num, edges);\n\t\t}\n\t\tvector<int>big(nums.size());\n\t\tfor (int i = 0; i < nums.size(); ++i) {\n\t\t\tbig[nums[i]] = i;\n\t\t}\n\t\treverse(big.begin(), big.end());\n\t\tunis.resize(edges.size());\n\t\tfill(unis.begin(), unis.end(), -1);\n\t\tnum = 0;\n\t\tfor (int i = 0; i < big.size(); ++i) {\n\n\t\t\tdfs2(big[i], num, revedges);\n\t\t\tnum++;\n\t\t}\n\t\tvector<int>nums;\n\t\tfor (int i = 0; i < unis.size(); ++i) {\n\t\t\tnums.push_back(unis[i]);\n\t\t}\n\t\tsort(nums.begin(), nums.end());\n\t\tnums.erase(unique(nums.begin(), nums.end()), nums.end());\n\n\n\t\tmap<int, int>mp;\n\t\tfor (int i = 0; i < nums.size(); ++i) {\n\t\t\tmp[nums[i]] = i;\n\t\t}\n\t\tfor (int i = 0; i < unis.size(); ++i) {\n\t\t\tunis[i] = mp[unis[i]];\n\t\t}\n\n\t\tvector<vector<int>>belongs(nums.size());\n\t\tfor (int i = 0; i < unis.size(); ++i) {\n\t\t\tbelongs[unis[i]].push_back(i);\n\t\t}\n\t\tvector<vector<int>>newedges(nums.size());\n\t\tfor (int i = 0; i < edges.size(); ++i) {\n\t\t\tfor (auto j : edges[i]) {\n\t\t\t\tif (unis[i] != unis[j]) {\n\t\t\t\t\tnewedges[unis[i]].push_back(unis[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn make_pair(belongs, newedges);\n\t}\nprivate:\n\tvector<int>nums;\n\tvector<int>unis;\n\n\n\tvoid dfs(const int id, int &num, const vector<vector<int>>&edges) {\n\t\tif (nums[id] != -1)return;\n\t\telse {\n\t\t\tnums[id] = -2;\n\t\t\tfor (auto i : edges[id]) {\n\t\t\t\tdfs(i, num, edges);\n\t\t\t}\n\t\t}\n\t\tnums[id] = num++;\n\t\treturn;\n\t}\n\tvoid dfs2(const int id, const int &num, const vector<vector<int>>&edges) {\n\t\tif (unis[id] != -1)return;\n\t\telse {\n\t\t\tunis[id] = -2;\n\t\t\tfor (auto i : edges[id])\n\t\t\t\tdfs2(i, num, edges);\n\t\t}\n\t\tunis[id] = num;\n\t\treturn;\n\t}\n\n}dscc;\n\nint get_id(string st,bool flag=false) {\n\tstatic map<string,int>mp;\n\tstatic int id=0;\n\tif (flag) {\n\t\tmp.clear();\n\t\tid=0;\n\t\treturn 0;\n\t}\n\tif (mp.find(st) == mp.end()) {\n\t\tmp[st]=id++;\n\t}\n\treturn mp[st];\n}\n\nint main() {\n\twhile (true) {\n\t\tget_id(\"\", true);\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\n\t\tvector<int>oris(N);\n\t\tvector<int>acomes(N);\n\t\tvector<vector<int>>edges(N);\n\n\t\tvector<int>ids(N);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring ast;\n\t\t\tint b;\n\t\t\tstring cst;\n\t\t\tint d;\n\t\t\tcin>>ast>>b>>cst>>d;\n\t\t\tint a=get_id(ast);\n\t\t\tint c=get_id(cst);\n\n\t\t\toris[i]=b;\n\t\t\tacomes[i]=d;\n\t\t\tif(a!=c)edges[c].push_back(a);\n\n\t\t\tids[a]=i;\n\t\t}\n\n\t\tauto p=dscc.get(edges);\n\n\t\tvector<int>comes(p.second.size());\n\t\tfor (auto es : p.second) {\n\t\t\tfor (auto e : es) {\n\t\t\t\tcomes[e]++;\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\tint ans=accumulate(acomes.begin(),acomes.end(),0);\n\t\tfor(int i=0;i<p.first.size();++i){\n\t\t\tif(comes[i])continue;\n\t\t\tauto v(p.first[i]);\n\t\t\tint minus_max=-1e9;\n\t\t\tfor (auto a : v) {\n\t\t\t\tminus_max=max(minus_max,acomes[ids[a]]-oris[ids[a]]);\n\t\t\t}\n\t\t\tans-=minus_max;\n\t\t}\n\t\tcout<<ans<<endl;\n\t\t\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3424, "score_of_the_acc": -1.0027, "final_rank": 17 }, { "submission_id": "aoj_2033_2564715", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct GROUP{\n\tvector<int> nodes;\n};\n\n\nstruct Info{\n\tint day1,sup_id,day2;\n\tchar name[33],sup_name[33];\n};\n\nint N;\nint height[1000],parent[1000],num_in[1000];\n\nInfo info[1000];\nvector<int> G[1000];\nvector<int> reverse_G[1000];\n\nGROUP group[1000];\nstack<int> S;\n\nbool check[1000];\nint table[1000];\n\nint group_index;\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\n\nvoid dfs(int node_id){\n\tcheck[node_id] = true;\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\tif(!check[G[node_id][i]])dfs(G[node_id][i]);\n\t}\n\tS.push(node_id);\n}\n\nvoid reverse_dfs(int node_id){\n\tcheck[node_id] = true;\n\n\tgroup[group_index].nodes.push_back(node_id);\n\ttable[node_id] = group_index;\n\n\tfor(int i = 0; i < reverse_G[node_id].size(); i++){\n\t\tif(!check[reverse_G[node_id][i]])reverse_dfs(reverse_G[node_id][i]);\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tgroup[i].nodes.clear();\n\t\tG[i].clear();\n\t\treverse_G[i].clear();\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%s %d %s %d\",info[i].name,&info[i].day1,info[i].sup_name,&info[i].day2);\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(strCmp(info[k].name,info[i].sup_name)){\n\t\t\t\tinfo[i].sup_id = k;\n\n\t\t\t\tif(i != k){\n\t\t\t\t\tG[k].push_back(i);\n\t\t\t\t\treverse_G[i].push_back(k);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < N;i++)check[i] = false;\n\n\tfor(int i = 0; i < N;i++){\n\t\tif(!check[i])dfs(i);\n\t}\n\n\tfor(int i = 0; i < N;i++)check[i] = false;\n\n\tgroup_index = -1;\n\n\twhile(!S.empty()){\n\t\tif(!check[S.top()]){\n\t\t\tgroup_index++;\n\n\t\t\treverse_dfs(S.top());\n\t\t}\n\t\tS.pop();\n\t}\n\n\tfor(int i = 0; i < N; i++)num_in[i] = 0;\n\n\tint to_group,min_index,minimum;\n\n\tfor(int i = 0; i <= group_index; i++){\n\t\tfor(int k = 0; k < group[i].nodes.size(); k++){\n\t\t\tfor(int p = 0; p < G[group[i].nodes[k]].size(); p++){\n\t\t\t\tto_group = table[G[group[i].nodes[k]][p]];\n\t\t\t\tif(to_group != i)num_in[to_group]++;\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans = 0;\n\n\tfor(int i = 0; i <= group_index;i++){\n\t\tif(group[i].nodes.size() == 1){\n\t\t\tif(num_in[i] == 0){\n\t\t\t\tans += info[group[i].nodes[0]].day1;\n\t\t\t}else{\n\t\t\t\tans += info[group[i].nodes[0]].day2;\n\t\t\t}\n\t\t}else{\n\n\t\t\tif(num_in[i] == 0){\n\t\t\t\tmin_index = 0;\n\t\t\t\tminimum = info[group[i].nodes[0]].day1 -info[group[i].nodes[0]].day2;\n\n\t\t\t\tfor(int k = 1; k < group[i].nodes.size(); k++){\n\t\t\t\t\tif(info[group[i].nodes[k]].day1 -info[group[i].nodes[k]].day2 < minimum){\n\t\t\t\t\t\tminimum = info[group[i].nodes[k]].day1 -info[group[i].nodes[k]].day2;\n\t\t\t\t\t\tmin_index = k;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tans += info[group[i].nodes[min_index]].day1;\n\t\t\t\tfor(int k = 0; k < group[i].nodes.size(); k++){\n\t\t\t\t\tif(k != min_index){\n\t\t\t\t\t\tans += info[group[i].nodes[k]].day2;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{\n\t\t\t\tfor(int k = 0; k < group[i].nodes.size(); k++){\n\t\t\t\t\tans += info[group[i].nodes[k]].day2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3556, "score_of_the_acc": -1.0393, "final_rank": 19 }, { "submission_id": "aoj_2033_2212894", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint N;\nint day1[1111],day2[1111];\nstring name[1111],sup[1111];\nconst int INF = 1000000007;\nvector<int> G[1111];\nvector<int> rG[1111];\n\nbool used[1111];\nvector<int> V[1111];\nvector<int> vs;\nvoid init(){\n for(int i=0;i<N;i++) G[i].clear();\n for(int i=0;i<N;i++) rG[i].clear();\n for(int i=0;i<N;i++) V[i].clear(); \n}\nint dfs(int v){\n if( used[v] ) return 0;\n int res = 0;\n used[v] = true;\n for( int to : G[v] ){\n res += dfs( to );\n }\n vs.push_back( v );\n return res + day2[v];\n}\nvoid rdfs(int v,int cnt){\n if( used[v] ) return;\n used[v] = true;\n for( int to : rG[v] ) {\n rdfs( to, cnt );\n }\n V[cnt].push_back(v);\n}\nint scc(){\n memset( used, 0, sizeof( used ) );\n vs.clear();\n for(int i=0;i<N;i++){\n dfs( i );\n }\n memset( used, 0, sizeof( used ) );\n reverse( vs.begin(), vs.end() );\n int cnt = 0;\n for(int v : vs ){\n if( used[v] ) continue; \n rdfs( v, cnt++ );\n }\n return cnt;\n}\nvoid add_edge(int from,int to){\n G[from].push_back( to );\n rG[to].push_back( from );\n}\n\nint main(){\n while( cin >> N && N ){\n init();\n map<string,int> H;\n for(int i=0;i<N;i++){\n cin >> name[i] >> day1[i] >> sup[i] >> day2[i];\n H[name[i]] = i;\n }\n for(int i=0;i<N;i++)\n add_edge( H[sup[i]], H[name[i]] );\n\n int n = scc();\n int res = 0;\n memset( used, 0, sizeof( used ) );\n for(int i=0;i<n;i++){\n int sum = 0;\n int min_pc = INF;\n int nv = V[i].back();\n if( used[nv] ) continue;\n for( int v : V[i] ){\n sum += day2[v];\n min_pc = min( min_pc, day1[v] - day2[v] );\n }\n res += dfs(nv) + min_pc;\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3584, "score_of_the_acc": -1.0208, "final_rank": 18 }, { "submission_id": "aoj_2033_1897792", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nconst int LMAX=2010;\nclass SCC{//cmp(0 - kk-1) is scc's return. Same number is group. The number is no relatoin.\n\tpublic:\n\tint n;\n\tvvi G,rG;\n\tvector<bool>used;\n\tvi vs,cmp;\n\tSCC(int size){\n\t\tn=size;\n\t\tG=rG=vvi(n);\n\t\tused=vector<bool>(n);\n\t\tcmp=vi(n);\n\t\tvs=vi(0);\n\t}\n\tvoid add_edge(int s,int t){\n\t\tG[s].pb(t);\n\t\trG[t].pb(s);\n\t}\n\tvoid rdfs(int v,int k){\n\t\tused[v]=true;\n\t\tcmp[v]=k;\n\t\trep(i,rG[v].size())if(!used[rG[v][i]])rdfs(rG[v][i],k);\n\t}\n\tvoid dfs(int v){\n\t\tused[v]=true;\n\t\trep(i,G[v].size())if(!used[G[v][i]])dfs(G[v][i]);\n\t\tvs.pb(v);\n\t}\n\tint scc(){\n\t\trep(i,n)used[i]=false;\n\t\trep(v,n)if(!used[v])dfs(v);\n\t\trep(i,n)used[i]=false;\n\t\tint kk=0;\n\t\tfor(int i=vs.size()-1;i>=0;i--)if(!used[vs[i]])rdfs(vs[i],kk++);\n\t\treturn kk; \n\t}\n};\nvp in;\nvvi G,rG;\nvi visit;\nint out=0;\nvoid dfs(int a){\n\tout+=in[a].second;\n\tvisit[a]=true;\n\trep(i,G[a].size())if(visit[G[a][i]]==0)\n\t\tdfs(G[a][i]);\n}\nint main(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tmap<string,int>m;\n\t\tvector<string>a(n),c(n);\n\t\tvi b(n),d(n);\n\t\trep(i,n)cin>>a[i]>>b[i]>>c[i]>>d[i];\n\t\trep(i,n)m[a[i]]=i;\t\t\n\t\tSCC scc(n);\n\t\trep(i,n){\n\t\t\tif(a[i]!=c[i])scc.add_edge(m[c[i]],m[a[i]]);\n\t\t}\n\t\tint N=scc.scc();\n\t\tin=vp(N);\n\t\tG=rG=vvi(N);\n\t\trep(i,N){\n\t\t\tvi t;\n\t\t\trep(j,n)if(scc.cmp[j]==i)t.pb(j);\n\t\t\tif(t.size()==1){\n\t\t\t\tif(i!=scc.cmp[m[c[t[0]]]]){\n\t\t\t\t\trG[i].pb(scc.cmp[m[c[t[0]]]]);\n\t\t\t\t\tG[scc.cmp[m[c[t[0]]]]].pb(i);\n\t\t\t\t}\n\t\t\t\tin[i]=pii(b[t[0]],d[t[0]]);\n\t\t\t}else{\n\t\t\t\tint mi=inf,sum=0;\n\t\t\t\trep(j,t.size())sum+=d[t[j]];\n\t\t\t\trep(j,t.size())mi=min(mi,b[t[j]]-d[t[j]]);\n\t\t\t\tin[i]=pii(mi+sum,inf);\n\t\t\t}\n\t\t}\n\t\tvisit=vi(N);\n\t\tout=0;\n\t\trep(i,N)if(visit[i]==0&&rG[i].size()==0){\n\t\t\tout+=in[i].first-in[i].second;\n\t\t\tdfs(i);\n\t\t}\n\t\tcout<<out<<endl;\n\t\t\n\t}\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1712, "score_of_the_acc": -0.5606, "final_rank": 15 }, { "submission_id": "aoj_2033_1354747", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 1000;\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef map<string,int> msi;\n\nstruct Tool {\n string name, sup;\n int day1, day2, diff, i, si;\n\n void print() {\n printf(\"Tool: %s(%d):%d, %s(%d):%d\\n\",\n\t name.c_str(), i, day1, sup.c_str(), si, day2);\n }\n};\n\n/* global variables */\n\nint n;\nTool tools[MAX_N];\nmsi nhash;\n\nbool used[MAX_N], tmpused[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n nhash.clear();\n for (int i = 0; i < n; i++) {\n Tool& ti = tools[i];\n cin >> ti.name >> ti.day1 >> ti.sup >> ti.day2;\n ti.diff = ti.day1 - ti.day2;\n ti.i = i;\n ti.si = -1;\n nhash[ti.name] = i;\n }\n for (int i = 0; i < n; i++) {\n Tool& ti = tools[i];\n ti.si = nhash[ti.sup];\n //ti.print();\n }\n\n memset(used, false, sizeof(used));\n\n int minsum = 0;\n \n // find loops\n for (int i = 0; i < n; i++) {\n Tool& ti = tools[i];\n if (! used[i]) {\n\tmemset(tmpused, false, sizeof(tmpused));\n\ttmpused[i] = true;\n\tint j = ti.si;\n\tfor (; ! tmpused[j]; j = tools[j].si) {\n\t if (used[j]) break;\n\t tmpused[j] = true;\n\t}\n\tif (used[j]) continue;\n\n\tint sumloop = tools[j].day2;\n\tint mindiff = tools[j].diff;\n\n\tused[j] = true;\n\tfor (int k = tools[j].si; k != j; k = tools[k].si) {\n\t used[k] = true;\n\t sumloop += tools[k].day2;\n\t if (mindiff > tools[k].diff) mindiff = tools[k].diff;\n\t}\n\n\tminsum += sumloop + mindiff;\n }\n }\n //cout << ln << endl;\n\n // check leafs\n bool changed = true;\n while (changed) {\n changed = false;\n\n for (int i = 0; i < n; i++) {\n\tTool& ti = tools[i];\n\tif (! used[i] && used[ti.si]) {\n\t used[i] = true;\n\t minsum += ti.day2;\n\t changed = true;\n\t}\n }\n }\n \n cout << minsum << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1412, "score_of_the_acc": -0.4247, "final_rank": 3 }, { "submission_id": "aoj_2033_936863", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\n\nstruct Edge{\n int to,cost;\n Edge(int to=IINF,int cost=IINF):to(to),cost(cost){}\n};\n\nmap<string,int> mp;\nint day1[1100],N,par[1100],mp_idx;\nvector<Edge> G[1100];\nbool used[1100];\n\ninline int getIndex(string s){\n if( mp.find(s) == mp.end() ) return mp[s] = mp_idx++;\n return mp[s];\n}\n\ninline void init(){\n mp_idx = 0;\n mp.clear();\n rep(i,N) G[i].clear(), par[i] = i;\n}\n\nint find(int x){\n if( par[x] == x ) return par[x];\n return par[x] = find(par[x]);\n}\n\ninline void unit(int x,int y){\n x = find(x), y = find(y);\n if( x != y ) par[x] = par[y];\n}\n\nvoid dfs(int cur,int &cost){\n if( used[cur] ) return;\n used[cur] = true;\n rep(i,(int)G[cur].size()){\n int to = G[cur][i].to;\n if( !used[to] && par[cur] == par[to] ){\n cost += G[cur][i].cost;\n dfs(to,cost);\n }\n }\n}\n\ninline void compute(){\n set<int> S;\n rep(i,N) S.insert(find(i));\n int cnt = S.size();\n int mincost[N];\n rep(i,N) mincost[i] = IINF;\n rep(i,N){\n int idx = par[i];\n int cost = day1[i];\n rep(j,N) used[j] = false;\n dfs(i,cost);\n rep(j,N) if( !used[j] && par[j] == par[i] ) cost += day1[j];\n mincost[idx] = min(mincost[idx],cost);\n }\n int ans = 0;\n for( int i : S ) ans += mincost[i];\n cout << ans << endl;\n}\n\nint main(){\n while( cin >> N, N ){\n\n init();\n rep(i,N){\n string a,b;\n int c,d;\n cin >> a >> c >> b >> d;\n int ia = getIndex(a);\n day1[ia] = c;\n int ib = getIndex(b);\n if( ia == ib ) continue;\n G[ib].push_back(Edge(ia,d));\n unit(ia,ib);\n }\n\n compute();\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 1468, "score_of_the_acc": -0.5126, "final_rank": 13 }, { "submission_id": "aoj_2033_936650", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nclass UnionFind{\npublic:\n vector<int> par, rank;\n\n void init(int n){\n par.resize(n);\n rank.resize(n);\n for(int i = 0; i < n; i++){\n par[i] = i;\n rank[i] = 0;\n }\n }\n\n int find(int x){\n if(par[x] == x) return x;\n else return par[x] = find(par[x]);\n }\n \n void unite(int x, int y){\n x = find(x);\n y = find(y);\n if(x == y) return;\n if(rank[x] < rank[y]) par[x] = par[y];\n else{\n par[y] = par[x];\n if(rank[x] == rank[y]) rank[x]++;\n }\n }\n};\n\ntypedef pair<int, int> P; // to, cost\nconst int MAX = 1005;\nmap<string, int> M;\nUnionFind uf;\nint cost[MAX],n;\nvector<P> E[MAX];\nbool used[MAX];\n\nint get_index(const string& s){\n if(M.count(s)) return M[s];\n else return M[s] = M.size()-1;\n}\n\nvoid init(){\n uf.init(n);\n M.clear();\n for(int i = 0; i < MAX; i++) E[i].clear();\n}\n\nvoid input(){\n for(int i = 0; i < n; i++){\n string a,b;\n int c,d;\n cin >> a >> c >> b >> d;\n cost[get_index(a)] = c;\n get_index(b);\n if(a == b) continue;\n E[get_index(b)].push_back(P(get_index(a), d));\n // cout << b << \" -> \" << a << endl;\n uf.unite(get_index(a), get_index(b));\n }\n}\n\nvoid dfs(int pos, P& p){ // vertex cost\n \n if(used[pos]) return;\n used[pos] = true;\n p.first++;\n\n for(int i = 0; i < (int)E[pos].size(); i++){\n if(!used[E[pos][i].first]){\n dfs(E[pos][i].first, p);\n p.second += E[pos][i].second;\n }\n }\n}\n\nvoid solve(){\n vector<int> v[MAX];\n for(int i = 0; i < n; i++) v[uf.find(i)].push_back(i);\n \n int ans = 0;\n\n for(int i = 0; i < MAX; i++){\n if(v[i].size() == 0) continue;\n int tmp = -1;\n for(int j = 0; j < (int)v[i].size(); j++){\n memset(used, false, sizeof(used));\n P p = P(0,0);\n dfs(v[i][j],p);\n p.second += cost[v[i][j]];\n/******* if(v[i][j] == 0){\n cout << p.first << \" \" << p.second << endl;\n }*/\n if((p.first == (int)v[i].size()) && (tmp == -1 || tmp > p.second)) tmp = p.second;\n }\n ans += tmp;\n }\n cout << ans << endl;\n}\n\nint main(){\n \n while(cin >> n && n){\n init();\n input();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 1512, "score_of_the_acc": -0.5116, "final_rank": 11 }, { "submission_id": "aoj_2033_849140", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\nint main(){\n int n;\n while(cin >> n && n){\n vector<string> name(n), sup(n);\n vector<int> high(n), low(n);\n REP(i, n) cin >> name[i] >> high[i] >> sup[i] >> low[i];\n map<string, int> index;\n REP(i, n) index[ name[i] ] = i;\n vector<int> color(n, 0);\n int ans = 0;\n REP(i, n) if(color[i] == 0){\n vector<int> list;\n int u = i;\n while(color[u] == 0){\n list.push_back(u);\n color[u] = 1;\n u = index[ sup[u] ];\n }\n if(color[u] == 1){\n int min_sum = INT_MAX;\n bool in_roop = false;\n for(int h : list){\n if(h == u) in_roop = true;\n if(!in_roop) continue;\n int sum = 0;\n for(int li : list){\n sum += (li == h ? high[li] : low[li]);\n }\n if(min_sum > sum) min_sum = sum;\n }\n ans += min_sum;\n }else if(color[u] == 2){\n for(int li : list){\n ans += low[li];\n }\n }\n for(int li : list){\n color[li] = 2;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1432, "score_of_the_acc": -0.4302, "final_rank": 4 }, { "submission_id": "aoj_2033_528242", "code_snippet": "//55\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<deque>\n\nusing namespace std;\n\nint n;\nstring na[1000],su[1000];\nint d[1000],dd[1000];\nmap<string,int> m;\nvector<int> ds[1000];\nbool p[1000];\nint cut[1000];\n\nint dfs(int x){\n if(p[x])return 0;\n int s=p[m[su[x]]]?d[x]-dd[x]:0;\n p[x]=true;\n for(int i=0;i<ds[x].size();i++){\n s+=dfs(ds[x][i]);\n }\n return s;\n}\n\nbool cmp(int a,int b){\n return cut[a]>cut[b];\n}\n\nint main(){\n while(cin>>n,n){\n m.clear();\n for(int i=0;i<n;i++){\n cin>>na[i]>>d[i]>>su[i]>>dd[i];\n m[na[i]]=i;\n }\n for(int i=0;i<n;i++){\n ds[i].clear();\n }\n for(int i=0;i<n;i++){\n ds[m[su[i]]].push_back(m[na[i]]);\n }\n for(int i=0;i<1000;i++){\n fill(p,p+n,false);\n cut[i]=dfs(i);\n }\n deque<int> ef(n);\n for(int i=0;i<n;i++){\n ef[i]=i;\n }\n sort(ef.begin(),ef.end(),cmp);\n bool done[1000]={};\n int a=0;\n for(int i=0;i<n;i++){\n for(int nt=0;nt<n;nt++){\n\tif(!done[nt]){\n\t if(done[m[su[nt]]]){\n\t a+=dd[nt];\n\t done[nt]=true;\n\t goto next;\n\t }\n\t}\n }\n while(done[ef[0]]){\n\tef.pop_front();\n }\n a+=d[ef[0]];\n done[ef[0]]=true;\n next:\n ;\n }\n cout<<a<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1540, "memory_kb": 1604, "score_of_the_acc": -1.4451, "final_rank": 20 }, { "submission_id": "aoj_2033_523414", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <climits>\n#include <cstring>\nusing namespace std;\n\ntypedef pair<int,int> P;\n\nint n, ans;\nint t[1002];\nvector<P> g[1002];\nmap<string,int> name;\nbool used[1002], loop[1002];\n\nint getId(string s){\n if(name.find(s) != name.end()){\n return name[s];\n }\n int size = name.size();\n return name[s] = size;\n}\n\nvoid solve(){\n memset(used, 0, sizeof(used));\n\n for(int i = 0; i < n; i++){\n if(used[i]) continue;\n\n memset(loop, 0, sizeof(loop));\n\n int id = i;\n bool flg = true;\n\n while(!loop[id]){\n if(used[id]){\n\tflg = false;\n\tbreak;\n }\n\n loop[id] = true;\n used[id] = true;\n id = g[id][0].first;\n }\n if(!flg) continue;\n\n int start = id;\n int minId = start;\n\n id = g[start][0].first;\n\n while(id != start){\n if(t[minId] - g[minId][0].second > t[id] - g[id][0].second){\n\tminId = id;\n }\n id = g[id][0].first;\n }\n\n ans += t[minId] - g[minId][0].second;\n }\n\n cout << ans << endl;\n}\n\nint main(){\n while(cin >> n, n){\n ans = 0;\n name.clear();\n\n for(int i = 0; i < 1002; i++){\n g[i].clear();\n }\n\n for(int i = 0; i < n; i++){\n string FROM, TO;\n int a, b;\n cin >> FROM >> a >> TO >> b;\n\n int from = getId(FROM);\n int to = getId(TO);\n t[from] = a;\n g[from].push_back(P(to, b));\n ans += b;\n }\n\n solve();\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1388, "score_of_the_acc": -0.4312, "final_rank": 5 }, { "submission_id": "aoj_2033_523404", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <sstream>\n#include <cmath>\n#include <queue>\n#include <set>\n#include <stack>\n#include <map>\n\n#define rep(i,n) for(int i=0; i<(n); i++)\n\nusing namespace std;\n\ntypedef vector<vector<int> > graph;\n\nvoid getReversedGraph(graph& G, graph* rG) {\n *rG = graph(G.size());\n for (int i = 0; i < (int)G.size(); i++) {\n for (int j = 0; j < (int)G[i].size(); j++) {\n (*rG)[G[i][j]].push_back(i);\n }\n }\n}\n\nvoid getSCCGraph(graph& G, graph* SCCGraph, vector<int>* table) {\n const int V = G.size();\n graph rG;\n vector<vector<int> > SCCList;\n vector<int> postOrder;\n bool *used = new bool[V];\n int *nextIndex = new int[V];\n *table = vector<int>(V);\n getReversedGraph(G, &rG);\n\n // DFS\n for (int i = 0; i < V; i++) {\n used[i] = false;\n nextIndex[i] = 0;\n }\n\n for (int i = 0; i < V; i++) {\n if (used[i]) {\n continue;\n }\n used[i] = true;\n stack<int> st;\n st.push(i);\n while (!st.empty()) {\n int v = st.top();\n if (nextIndex[v] == (int)G[v].size()) {\n postOrder.push_back(v);\n st.pop();\n } else {\n int newv = G[v][nextIndex[v]];\n if (!used[newv]) {\n st.push(newv);\n used[newv] = true;\n }\n nextIndex[v]++;\n }\n }\n }\n // Reversed DFS\n for (int i = 0; i < V; i++) {\n used[i] = false;\n nextIndex[i] = 0;\n }\n\n int it = 0;\n for (int i = V - 1; i >= 0; i--) {\n int index = postOrder[i];\n if (used[index]) {\n continue;\n }\n used[index] = true;\n (*table)[index] = it;\n stack<int> st;\n vector<int> list;\n st.push(index);\n list.push_back(index);\n while (!st.empty()) {\n int v = st.top();\n if (nextIndex[v] == (int)rG[v].size()) {\n st.pop();\n } else {\n int newv = rG[v][nextIndex[v]];\n if (!used[newv]) {\n st.push(newv);\n list.push_back(newv);\n used[newv] = true;\n (*table)[newv] = it;\n }\n nextIndex[v]++;\n }\n }\n SCCList.push_back(list);\n it++;\n }\n\n // create new graph;\n const int compressedV = it;\n *SCCGraph = graph(compressedV);\n \n for (int i = 0; i < compressedV; i++) {\n map<int, bool> edgeUsed;\n for (int j = 0; j < (int)SCCList[i].size(); j++) {\n int v = SCCList[i][j];\n for (int k = 0; k < (int)G[v].size(); k++) {\n int w = G[v][k];\n // v -> w\n if (!edgeUsed[(*table)[w]]) {\n edgeUsed[(*table)[w]] = true;\n if (i != (*table)[w]) (*SCCGraph)[i].push_back((*table)[w]);\n }\n }\n }\n }\n\n delete[] used;\n delete[] nextIndex;\n}\n\nbool used[2005];\n\nvoid check(int v, graph& G, bool* used) {\n if (used[v])return;\n used[v] = true;\n rep(i, G[v].size()) {\n check(G[v][i], G, used);\n }\n}\n\nint solve(int n) {\n string s1,s2;\n int d1,d2;\n graph G(n), sccG, rG;\n vector<int> table;\n map<string, int> toId;\n map<int, int> plus;\n int ans = 0;\n int id = 0;\n rep(i,n) {\n cin >> s1 >> d1 >> s2 >> d2;\n if (toId.find(s1) == toId.end()) {\n toId[s1] = id;\n id++;\n }\n if (toId.find(s2) == toId.end()) {\n toId[s2] = id;\n id++;\n }\n ans+= d2;\n if (s1 != s2)G[toId[s2]].push_back(toId[s1]);\n plus[toId[s1]] = d1 - d2;\n }\n \n getSCCGraph(G, &sccG, &table);\n rep(i, 2005)used[i] = false;\n rep(i, sccG.size()) {\n vector<int> p;\n if (used[i])continue;\n //cerr << i << endl;\n //cerr << \"\\t\";\n rep(j, table.size()) {\n if (table[j] == i) {\n p.push_back(plus[j]);\n //cerr << j << \":\" << p[j] << \" \";\n }\n }\n //cerr << endl;\n sort(p.begin(), p.end());\n ans += p[0];\n check(i, sccG, used);\n }\n return ans;\n}\n\nint main() {\n int n;\n while(cin >> n, n) {\n cout << solve(n) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 1584, "score_of_the_acc": -0.5119, "final_rank": 12 }, { "submission_id": "aoj_2033_469009", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint main()\n{\n for(;;){\n int n;\n cin >> n;\n if(n == 0)\n return 0;\n\n vector<string> s1(n), s2(n);\n vector<int> t1(n), t2(n);\n map<string, int> index;\n for(int i=0; i<n; ++i){\n cin >> s1[i] >> t1[i] >> s2[i] >> t2[i];\n index[s1[i]] = i;\n }\n\n vector<int> sup(n);\n vector<vector<int> > edges(n);\n for(int i=0; i<n; ++i){\n sup[i] = index[s2[i]];\n if(s1[i] != s2[i])\n edges[index[s2[i]]].push_back(i);\n }\n\n int ret = 0;\n vector<bool> make(n, false);\n for(int i=0; i<n; ++i){\n if(make[i])\n continue;\n\n int j = i;\n vector<bool> check(n, false);\n while(!check[j]){\n check[j] = true;\n j = sup[j];\n }\n\n check.assign(n, false);\n int k = j;\n while(!check[j]){\n if(t1[j] - t2[j] < t1[k] - t2[k])\n k = j;\n check[j] = true;\n j = sup[j];\n }\n\n ret += t1[k] - t2[k];\n queue<int> q;\n q.push(k);\n while(!q.empty()){\n int a = q.front();\n q.pop();\n ret += t2[a];\n make[a] = true;\n\n for(unsigned l=0; l<edges[a].size(); ++l){\n int b = edges[a][l];\n if(!make[b])\n q.push(b);\n }\n }\n }\n\n cout << ret << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1488, "score_of_the_acc": -0.4589, "final_rank": 7 }, { "submission_id": "aoj_2033_354764", "code_snippet": "//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <stack>\n#include <queue>\n#include <bitset>\n#include <algorithm>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <string>\n#include <cstring>\n\nusing namespace std;\n\n//conversion\n//------------------------------------------\ninline int toInt(string s) {int v; istringstream sin(s); sin >> v; return v;}\ntemplate<class T> inline string toStr(T x) {ostringstream sout; sout << x; return sout.str();}\n\n//math\n//-------------------------------------------\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\n\n//container util\n//------------------------------------------\n#define ALL(a, n) (a), (a) + (n)\n#define VALL(v) (v).begin(),(v).end()\n#define VADD(v, e) (v).push_back(e)\n#define SADD(s, e) (s).insert(e)\n#define STADD(s, e) (s).push(e)\n#define QADD(q, e) (q).push(e)\n#define PQADD(q, e) (q).push(e)\n#define STPOP(s, e) typeof((q).top()) e = (q).top(); (q).pop()\n#define QPOP(q) typeof((q).front()) e = (q).front(); (q).pop()\n#define PQPOP(q) typeof((q).top()) e = (q).top(); (q).pop()\n#define MP make_pair\n#define PF(p) (p).first\n#define PS(p) (p).second\n#define SZ(a) int((a).size())\n#define EACH(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(a, n) sort(ALL(a, n))\n#define VSORT(c) sort(VALL(c))\n#define RSORT(a, n) sort(ALL(a, n), greater<typeof(a[0])>())\n#define VRSORT(v) sort(VALL(v), greater<typeof(v[0])>())\n#define INDEX(a, n, x) find(ALL(a, n), x) - a\n#define VINDEX(v, x) find(VALL(v), x) - v.begin()\n#define BOUND(a, n, x) lower_bound(ALL(a, n), x) - a\n#define VBOUND(v, x) lower_bound(VALL(v), x) - v.begin()\n\n//repetition\n//------------------------------------------\n#define FOR(i, a, b) for (int i = (a);i < (b); ++i)\n#define RFOR(i, a, b) for (int i = (b) - 1; i >= (a); --i)\n#define REP(i, n) FOR(i, 0, n)\n#define RREP(i, n) RFOR(i, 0, n)\n\n//IO\n//------------------------------------------\n#define LF(x) cout << (x) << endl;\n#define LF2(x, y) cout << (x) << \" \" << (y) << endl;\n#define LFA(a, n) cout << a[0]; FOR(i, 1, n) {cout << \" \" << a[i];} cout << endl;\n#define LFD(x, w) cout.width = (w); cout << (x) << endl;\n#define LFDA(a, n, w) REP(i, n) cout.width = (w); cout << a[i] << endl;\n#define LFP(x, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << endl;\n#define LFP2(x, y, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << \" \" << (y) << endl;\n#define LFPA(a, n, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << a[0]; FOR(i, 1, n) {cout << \" \" << a[i];} cout << endl;\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst int INF = 1e9;\n\n//clear memory\n//--------------------------------------------\n#define CLR(a) memset((a), 0 , sizeof(a))\n\n//debug\n//--------------------------------------------\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl;\n#define DEBUG(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl;\n#define DUMPA(a, n) cerr << #a << \" = {\" << a[0]; FOR(i, 1, n) { cout << \", \" << a[i]; } cerr << \"}\" << endl;\n#define DEBUGA(a, n) cerr << #a << \" = {\" << a[0]; FOR(i, 1, n) { cout << \", \" << a[i]; } cerr << \"} (L\" << __LINE__ << \")\" << endl;\n#define DUMPAA(a, n, m) REP(i, n) {REP(j, m) {cout << a[i][j] << \" \";} cout << endl;}\n#define DEBUGAA(a, n, m) DUMPAA(a, n, m) cout << \"(L\" << __LINE__ << \")\" << endl;\n\n//#include <uft>\nclass uft\n{\npublic:\n\tint N;\n\tint n;\n\tint* p;\n\tint* r;\n\t\n\tuft(const int N)\n\t{\n\t\tthis->N = N;\n\t\tp = new int[N];\n\t\tr = new int[N];\n\t}\n\t\n\t~uft()\n\t{\n\t\tdelete[] p;\n\t\tdelete[] r;\n\t}\n\t\n\tvoid init(int n)\n\t{\n\t\tthis->n = n;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tp[i] = i;\n\t\t\tr[i] = 0;\n\t\t}\n\t}\n\t\n\tint find(int x)\n\t{\n\t\tif (p[x] == x) {\n\t\t\treturn x;\n\t\t}\n\t\tp[x] = find(p[x]);\n\t\treturn p[x];\n\t}\n\t\n\tvoid unite(int x, int y)\n\t{\n\t\tint a = find(x);\n\t\tint b = find(y);\n\t\tif (a == b) {\n\t\t\treturn;\n\t\t}\n\t\tif(r[a] < r[b]) {\n\t\t\tp[a] = b;\n\t\t}\n\t\telse if (r[a] == r[b]){\n\t\t\tp[a] = b;\n\t\t\tr[b]++;\n\t\t}\n\t\telse {\n\t\t\tp[b] = a;\n\t\t}\n\t}\n\t\n\tbool same(int x, int y)\n\t{\n\t\treturn find(x) == find(y);\n\t}\n\n};\n\n//#include_end <uft>\n\nconst int N = 1000;\nint n;\nstring name[N];\nint day1[N];\nstring sup[N];\nint day2[N];\nmap<string, int> f;\nuft uft(N);\nVI e[N];\nint m[N];\nint dp[N];\nbool v[N];\n\nint dfs(int k)\n{\n\tv[k] = true;\n\tint ans = day1[k];\n\tREP(i, e[k].size()) {\n\t\tif(!v[e[k][i]]) ans += dfs(e[k][i]) - day1[e[k][i]] + day2[e[k][i]];\n\t}\n\treturn ans;\n}\n\nvoid init()\n{\n}\n\nvoid solve()\n{\n\tf.clear();\n\tREP(i, n) {\n\t\tf[name[i]] = i;\n\t\te[i].clear();\n\t}\n\tuft.init(n);\n\tfill(ALL(m, n), 1);\n\tREP(i, n) {\n\t\tint t = f[name[i]];\n\t\tint u = f[sup[i]];\n\t\tif (!uft.same(t, u)) {\n\t\t\tuft.unite(t, u);\n\t\t\tm[t] += m[u];\n\t\t\tREP(j, n) {\n\t\t\t\tif (uft.same(t, j)) {\n\t\t\t\t\tm[j] = m[t];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tVADD(e[u], t);\n\t}\n\tfill(ALL(dp, n), INF);\n\tREP(i, n) {\n\t\tfill(ALL(v, n), false);\n\t\tint x = dfs(i);\n\t\tint y = 0;\n\t\tREP(j, n) {\n\t\t\tif (v[j]) ++y;\n\t\t}\n\t\tif (y < m[i]) continue;\n\t\tREP(j, n) {\n\t\t\tif (uft.same(i, j)) {\n\t\t\t\tdp[j] = min(dp[j], x);\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tREP(i, n) {\n\t\tbool b = false;\n\t\tREP(j, i) {\n\t\t\tb |= uft.same(i, j);\n\t\t}\n\t\tif (!b) ans += dp[i];\n\t}\n\tLF(ans);\n}\n\nint main()\n{\n\tinit();\n\twhile (cin >> n, n) {\n\t\tREP(i, n) cin >> name[i] >> day1[i] >> sup[i] >> day2[i];\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 0, "score_of_the_acc": -0.5066, "final_rank": 10 }, { "submission_id": "aoj_2033_334814", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <cstring>\n\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nbool used[1001];\nint n;\nvector<int> G[1001];\npii costs[1001];\nint idx=0;\nmap<string,int> indices;\nint nxt[1001];\nint in[1001];\nint sumCosts=0;\nbool used2[1001];\n\n// “üü0‚̃m[ƒh‚ɂ‚¢‚Ä’Tõ(•˜H‚Í‘¶Ý‚µ‚¦‚È‚¢)\nvoid search1(int s){\n\tused[s]=true;\n\tfor(int i=0;i<(int)G[s].size();i++){\n\t\tint to=G[s][i];\n\t\tif(!used[to]){\n\t\t\tnxt[s]=to;\n\t\t\tsumCosts+=costs[to].second;\n\t\t\tsearch1(to);\n\t\t}\n\t}\n}\n// “ü“_‚ª‚ ‚éƒm[ƒh‚©‚ç’Tõ‚ðŽn‚ß‚é\nvoid search2(int s){\n\tused[s]=true;\n\tused2[s]=true;\n\tfor(int i=0;i<(int)G[s].size();i++){\n\t\tint to=G[s][i];\n\t\tif(!used[to]){\n\t\t\tnxt[s]=to;\n\t\t\tsumCosts+=costs[to].second;\n\t\t\tsearch2(to);\n\t\t}\n\t\t// •˜H‚ð”­Œ©‚µ‚½ê‡A•˜H‚Ì’†‚ōłàƒRƒXƒg·‚̏¬‚³‚¢‚à‚Ì‚ðÌ—p\n\t\telse{\n\t\t\t// ¡‰ñ’ʉ߂µ‚½êŠ‚ł͂Ȃ¢ê‡\n\t\t\tif(!used2[to])continue;\n\t\t\tvector<pii> v;\n\t\t\tint cur=to;\n\t\t\twhile(1){\n\t\t\t\tv.push_back(pii(costs[cur].first-costs[cur].second,cur));\n\t\t\t\tif(cur==s)break;\n\t\t\t\tint tto=nxt[cur];\n\t\t\t\tcur=tto;\n\t\t\t}\n\t\t\tsort(v.begin(),v.end());\n\t\t\tsumCosts+=costs[v[0].second].first;\n\t\t\tsumCosts-=costs[v[0].second].second;\n\t\t}\n\t}\n}\n\nint main(){\n\n\twhile(cin>>n&&n){\n\t\tsumCosts=0;\n\t\tmemset(in,0,sizeof(in));\n\t\tmemset(used,0,sizeof(used));\n\t\tindices.clear();\n\t\tidx=0;\n\t\tfor(int i=0;i<1001;i++)G[i].clear();\n\t\tfor(int i=0;i<n;i++){\n\t\t\tstring a,b;\n\t\t\tint c,d;\n\t\t\tcin>>a>>c>>b>>d;\n\t\t\tif(indices.find(a)==indices.end())indices[a]=idx++;\n\t\t\tif(indices.find(b)==indices.end())indices[b]=idx++;\n\t\t\tif(a!=b){\n\t\t\t\tG[indices[b]].push_back(indices[a]);\n\t\t\t\tin[indices[a]]++;\n\t\t\t}\n\t\t\tcosts[indices[a]]=pii(c,d);\n\t\t}\n\t\t// “ü“_‚ª–³‚¢•¨‚ð’Tõ\n\t\tfor(int i=0;i<idx;i++){\n\t\t\tif(in[i]==0&&!used[i]){\n\t\t\t\t// Å‰‚Ì—v‘f‚Í•K‚¸Žæ‚é\n\t\t\t\tsumCosts+=costs[i].first;\n\t\t\t\tsearch1(i);\n\t\t\t}\n\t\t}\n\t\t// ‚Ü‚¾’Tõ‚µ‚Ä‚¢‚È‚¢‚à‚Ì‚ð‚·‚×‚Ä’Tõ\n\t\tfor(int i=0;i<idx;i++){\n\t\t\tif(!used[i]){\n\t\t\t\tmemset(used2,0,sizeof(used2));\n\t\t\t\tsumCosts+=costs[i].second;\n\t\t\t\tsearch2(i);\n\t\t\t}\n\t\t}\n\t\tcout<<sumCosts<<endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 1140, "score_of_the_acc": -0.4479, "final_rank": 6 }, { "submission_id": "aoj_2033_256054", "code_snippet": "#include<map>\n#include<string>\n#include<vector>\n#include<iostream>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef vector< vector<int> > vvi;\n\nconst int INF=1<<29;\n\nvoid GabowDFS(int u,vector<bool> &visited,vector<int> &nodes,const vvi &adj){\n\tif(visited[u]) return;\n\tvisited[u]=true;\n\trep(i,adj[u].size()){\n\t\tint v=adj[u][i];\n\t\tif(!visited[v]) GabowDFS(v,visited,nodes,adj);\n\t}\n\tnodes.push_back(u);\n}\n\nvvi GabowSCC(const vvi &adj){\n\tint n=adj.size();\n\tvector<int> ord;\n\tvector<bool> visited(n);\n\trep(u,n) GabowDFS(u,visited,ord,adj);\n\n\tvvi radj(n);\n\trep(u,n) rep(i,adj[u].size()) radj[adj[u][i]].push_back(u);\n\n\tvvi sccs;\n\trep(u,n) visited[u]=false;\n\tfor(int i=n-1;i>=0;i--){\n\t\tint u=ord[i];\n\t\tvector<int> scc;\n\t\tGabowDFS(u,visited,scc,radj);\n\t\tif(scc.size()>0) sccs.push_back(scc);\n\t}\n\treturn sccs;\n}\n\nint main(){\n\tfor(int n;cin>>n,n;){\n\t\tint m=0;\n\t\tmap<string,int> f;\n\n\t\tvvi adj(n);\n\t\tint day1[1000],day2[1000];\n\t\trep(i,n){\n\t\t\tstring s1,s2;\n\t\t\tint d1,d2; cin>>s1>>d1>>s2>>d2;\n\t\t\tif(f.count(s1)==0) f[s1]=m++;\n\t\t\tif(f.count(s2)==0) f[s2]=m++;\n\n\t\t\tint u=f[s2],v=f[s1];\n\t\t\tadj[u].push_back(v);\n\t\t\tday1[v]=d1;\n\t\t\tday2[v]=d2;\n\t\t}\n\n\t\tvvi scc=GabowSCC(adj);\n\n\t\tint ans=0;\n\t\tbool tool[1000]={};\n\t\trep(i,scc.size()){\n\t\t\tint dif=INF;\n\t\t\tbool start=true;\n\t\t\trep(j,scc[i].size()){\n\t\t\t\tint u=scc[i][j];\n\t\t\t\tans+=day2[u];\n\t\t\t\tdif=min(dif,day1[u]-day2[u]);\n\t\t\t\tif(tool[u]) start=false;\n\t\t\t}\n\t\t\tif(start) ans+=dif;\n\n\t\t\trep(j,scc[i].size()){\n\t\t\t\tint u=scc[i][j];\n\t\t\t\trep(k,adj[u].size()) tool[adj[u][k]]=true;\n\t\t\t}\n\t\t}\n\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 0, "score_of_the_acc": -0.1447, "final_rank": 1 }, { "submission_id": "aoj_2033_245575", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\ntypedef vector<vector<int> > graph;\n\n#define REP(i,n) for(int i=0; i<n; i++)\n\n\nvoid visit(graph& g, vector<int>& PostOrder, vector<bool>& visited, int ps)\n{\n\tvisited[ps]=1;\n\tint S=g[ps].size();\n\tREP(i,S)\n\t{\n\t\tint next=g[ps][i];\n\t\tif(visited[next]) continue;\n\t\tvisit(g, PostOrder, visited, next);\n\t}\n\t\n\tPostOrder.push_back(ps);\n}\n\nvoid walk(graph& g, vector<int>& in, int ps, int gcnt)\n{\n\tin[ps]=gcnt;\n\t\n\tint S=g[ps].size();\n\tREP(i,S)\n\t{\n\t\tint next=g[ps][i];\n\t\tif(in[next]!=-1) continue;\n\t\twalk(g,in,next,gcnt);\n\t}\n}\n\n//StronglyConnectedComponent\nint SCC(vector<int>& in, int V, graph& g, graph& rg)\n{\n\tint gcnt=0;\n\tvector<int> PostOrder;\n\tvector<bool> visited(V,false);\n\t\n\tREP(i,V)\n\t{\n\t\tif(visited[i]) continue;\n\t\tvisit(g,PostOrder,visited, i);\n\t}\n\t\t\n\tfor(int i=PostOrder.size()-1; i>=0; --i)\n\t{\n\t\tif(in[PostOrder[i]]==-1) \n\t\t{\n\t\t\twalk(rg,in,PostOrder[i],gcnt++);\n\t\t}\n\t}\n\n\treturn gcnt;\n}\n\nint recipe[1050], self[1050],reduce[1050];\n\nint dfs(int p, vector<bool>& use, graph& g, graph& c)\n{\n\tuse[p]=1;\n\n\tint ret=0;\n\tfor(int i=0; i<g[p].size(); i++)\n\t{\n\t\tint next=g[p][i];\n\t\tif(use[next]) continue;\n\t\tret+=dfs(next, use, g, c);\n\t\tret+=c[p][i];\n\t}\n\n\treturn ret;\n\n}\n\nint calc(vector<int>& v, vector<bool>& created, graph& g, graph& c)\n{\n\tvector<bool> use(10000, 1);\n\tfor(int i=0; i<v.size(); i++)\n\t\tuse[v[i]]=0;\n\n\tint ret=0,mn=(1<<24),r;\n\tif(v.size()==1)\n\t{\n\t\tint p=v[0];\n\t\tif(created[recipe[p]]) ret=reduce[p];\n\t\telse ret=self[p];\n\t}\n\telse\n\t{\n\t\tfor(int i=0; i<v.size(); i++)\n\t\t{\n\t\t\tint p=v[i];\n\t\t\tif(self[p]-reduce[p]<mn)\n\t\t\t{\n\t\t\t\tr=p;\n\t\t\t\tmn=self[p]-reduce[p];\n\t\t\t}\n\t\t}\n\n\t\tfor(int i=0; i<v.size(); i++)\n\t\t{\n\t\t\tint p=v[i];\n\t\t\tif(p==r) ret+=self[p];\n\t\t\telse ret+=reduce[p];\n\t\t}\n\t}\n\n\tfor(int i=0; i<v.size(); i++)\n\t\tcreated[v[i]]=1;\n\n\treturn ret;\n}\n\nint main()\n{\n\tint N;\n\twhile(cin >> N, N)\n\t{\n\t\tgraph g(N), rg(N), c(N), rc(N);\n\n\t\tint dcnt=0;\n\t\tmap<string, int> dic;\n\t\tfor(int i=0; i<N; i++)\n\t\t{\n\t\t\tstring s,t;\n\t\t\tint a,b,u,v;\n\t\t\tcin >> s >> a >> t >> b;\n\n\t\t\tif(!dic.count(s)) dic[s]=dcnt++;\n\t\t\tif(!dic.count(t)) dic[t]=dcnt++;\n\t\t\t\n\t\t\tu=dic[s];\n\t\t\tv=dic[t];\n\n\t\t\t g[u].push_back(v);\n\t\t\trg[v].push_back(u);\n\t\t\t c[u].push_back(b);\n\t\t\trc[v].push_back(b);\n\n\t\t\tself[u]=a;\n\t\t\trecipe[u]=v;\n\t\t\treduce[u]=b;\n\t\t}\n\n\t\tvector<int> in(N, -1);\n\t\tint gcnt=SCC(in,N,rg,g);\n\n\t\tvector<vector<int> > loop(gcnt);\n\t\tfor(int i=0; i<N; i++)\n\t\t\tloop[in[i]].push_back(i);\n\n\t\tvector<bool> created(N, 0);\n\n\t\tint ans=0;\n\t\tfor(int i=0; i<gcnt; i++)\n\t\t{\n\t\t\tans+=calc(loop[i], created, rg, rc);\n\t\t}\n\n\t\tprintf(\"%d\\n\", ans);\n\t}\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 1188, "score_of_the_acc": -0.4875, "final_rank": 9 }, { "submission_id": "aoj_2033_245563", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\ntypedef vector<vector<int> > graph;\n\n#define REP(i,n) for(int i=0; i<n; i++)\n\n\nvoid visit(graph& g, vector<int>& PostOrder, vector<bool>& visited, int ps)\n{\n\tvisited[ps]=1;\n\tint S=g[ps].size();\n\tREP(i,S)\n\t{\n\t\tint next=g[ps][i];\n\t\tif(visited[next]) continue;\n\t\tvisit(g, PostOrder, visited, next);\n\t}\n\t\n\tPostOrder.push_back(ps);\n}\n\nvoid walk(graph& g, vector<int>& in, int ps, int gcnt)\n{\n\tin[ps]=gcnt;\n\t\n\tint S=g[ps].size();\n\tREP(i,S)\n\t{\n\t\tint next=g[ps][i];\n\t\tif(in[next]!=-1) continue;\n\t\twalk(g,in,next,gcnt);\n\t}\n}\n\n//StronglyConnectedComponent\nint SCC(vector<int>& in, int V, graph& g, graph& rg)\n{\n\tint gcnt=0;\n\tvector<int> PostOrder;\n\tvector<bool> visited(V,false);\n\t\n\tREP(i,V)\n\t{\n\t\tif(visited[i]) continue;\n\t\tvisit(g,PostOrder,visited, i);\n\t}\n\t\t\n\tfor(int i=PostOrder.size()-1; i>=0; --i)\n\t{\n\t\tif(in[PostOrder[i]]==-1) \n\t\t{\n\t\t\twalk(rg,in,PostOrder[i],gcnt++);\n\t\t}\n\t}\n\n\treturn gcnt;\n}\n\nint recipe[1050], self[1050],reduce[1050];\n\nint dfs(int p, vector<bool>& use, graph& g, graph& c)\n{\n\tuse[p]=1;\n\n\tint ret=0;\n\tfor(int i=0; i<g[p].size(); i++)\n\t{\n\t\tint next=g[p][i];\n\t\tif(use[next]) continue;\n\t\tret+=dfs(next, use, g, c);\n\t\tret+=c[p][i];\n\t}\n\n\treturn ret;\n\n}\n\nint calc(vector<int>& v, vector<bool>& created, graph& g, graph& c)\n{\n\tvector<bool> use(10000, 1);\n\tfor(int i=0; i<v.size(); i++)\n\t\tuse[v[i]]=0;\n\n\tint ret=(1<<24);\n\tfor(int i=0; i<v.size(); i++)\n\t{\n\t\tvector<bool> u(use);\n\t\tint p=v[i], t=dfs(p,u,g,c);\n\t\tif(created[recipe[p]]) t+=reduce[p];\n\t\telse\t\t\t\t\tt+=self[p];\n\t\t\n\t\tret=min(ret, t);\n\t}\n\n\tfor(int i=0; i<v.size(); i++)\n\t\tcreated[v[i]]=1;\n\n\treturn ret;\n}\n\nint main()\n{\n\tint N;\n\twhile(cin >> N, N)\n\t{\n\t\tgraph g(N), rg(N), c(N), rc(N);\n\n\t\tint dcnt=0;\n\t\tmap<string, int> dic;\n\t\tfor(int i=0; i<N; i++)\n\t\t{\n\t\t\tstring s,t;\n\t\t\tint a,b,u,v;\n\t\t\tcin >> s >> a >> t >> b;\n\n\t\t\tif(!dic.count(s)) dic[s]=dcnt++;\n\t\t\tif(!dic.count(t)) dic[t]=dcnt++;\n\t\t\t\n\t\t\tu=dic[s];\n\t\t\tv=dic[t];\n\n\t\t\t g[u].push_back(v);\n\t\t\trg[v].push_back(u);\n\t\t\t c[u].push_back(b);\n\t\t\trc[v].push_back(b);\n\n\t\t\tself[u]=a;\n\t\t\trecipe[u]=v;\n\t\t\treduce[u]=b;\n\t\t}\n\n\t\tvector<int> in(N, -1);\n\t\tint gcnt=SCC(in,N,rg,g);\n\n\t\tvector<vector<int> > loop(gcnt);\n\t\tfor(int i=0; i<N; i++)\n\t\t\tloop[in[i]].push_back(i);\n\n\t\tvector<bool> created(N, 0);\n\n\t\tint ans=0;\n\t\tfor(int i=0; i<gcnt; i++)\n\t\t{\n\t\t\tans+=calc(loop[i], created, rg, rc);\n\t\t}\n\n\t\tprintf(\"%d\\n\", ans);\n\t}\n}\n\n/*\n4\ngu 20 pa 10\nci 20 gu 10\npa 20 ci 10\nhoge 20 pa 10\n*/", "accuracy": 1, "time_ms": 330, "memory_kb": 1188, "score_of_the_acc": -0.5336, "final_rank": 14 }, { "submission_id": "aoj_2033_225815", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <map>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nconst int INF = 1<<29;\n\nint n;\nvector<int> v[1000];\nmap<string, int> id;\nstring s[1000], t[1000];\nint d1[1000], d2[1000];\n\nbool visited[1000];\nint rec(int now) {\n if (visited[now]) return 0;\n visited[now] = 1;\n int res = 0;\n FOR(it, v[now]) {\n if (visited[*it]) continue;\n res += d1[*it]-d2[*it];\n res += rec(*it);\n }\n return res;\n}\n\nint dfs(int now) {\n if (visited[now]) return 0;\n visited[now] = 1;\n int res = 0;\n FOR(it, v[now]) {\n if (visited[*it]) continue;\n res += d2[*it];\n res += dfs(*it);\n }\n return res;\n \n}\n\nint main() {\n while(cin >> n, n) {\n id.clear();\n REP(i,n) {\n cin >> s[i] >> d1[i] >> t[i] >> d2[i];\n id[s[i]] = i;\n }\n REP(i,1000)\n v[i].clear();\n REP(i, n) {\n v[id[t[i]]].push_back(i);\n }\n typedef pair<int,int> pii;\n vector<pii> vv;\n REP(i,n) {\n memset(visited,0,sizeof(visited));\n int tmp = rec(i);\n vv.push_back(pii(tmp, i));\n }\n sort(ALL(vv), greater<pii>());\n memset(visited,0,sizeof(visited));\n int res = 0;\n REP(i,n) {\n int c = vv[i].second;\n if (!visited[c]) {\n res += d1[c] + dfs(c);\n }\n }\n cout << res << endl;\n } \n}", "accuracy": 1, "time_ms": 190, "memory_kb": 1112, "score_of_the_acc": -0.4204, "final_rank": 2 }, { "submission_id": "aoj_2033_225709", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <map>\n#include <cstring>\n#include <algorithm>\nusing namespace std;\n\nstruct tool {\n string name,sh;\n int ntime,stime;\n vector<int> edge;\n tool(string name,string sn,int ntime,int stime)\n : name(name), sh(sn), ntime(ntime), stime(stime) {edge.clear();}\n};\n\nbool seen[1001];\n\nint dfs(vector<tool> &tv,int v) {\n seen[v] = true;\n int ret = 0;\n for(int i=0; i<tv[v].edge.size(); ++i) {\n if(seen[tv[v].edge[i]]) continue;\n ret += (tv[tv[v].edge[i]].ntime - tv[tv[v].edge[i]].stime);\n ret += dfs(tv,tv[v].edge[i]);\n }\n return ret;\n}\n\nvoid dfs2(vector<tool> &tv,int v) {\n seen[v] = true;\n for(int i=0; i<tv[v].edge.size(); ++i) {\n if(seen[tv[v].edge[i]]) continue;\n dfs2(tv,tv[v].edge[i]);\n }\n}\n\nint main() {\n int n;\n string s1,s2;\n int k1,k2;\n while(cin>>n, n) {\n map<string,int> idx;\n vector<tool> tv;\n\n int ans = 0;\n for(int i=0; i<n; ++i) {\n cin>>s1>>k1>>s2>>k2;\n idx[s1] = i;\n tv.push_back(tool(s1,s2,k1,k2));\n ans += k1;\n }\n\n for(int i=0; i<n; ++i) {\n if(idx.find(tv[i].sh) == idx.end()) continue;\n int id = idx[tv[i].sh];\n tv[id].edge.push_back(i);\n }\n\n vector<int> p(n,-1);\n for(int i=0; i<n; ++i) {\n memset(seen, false, sizeof(seen));\n p[i] = dfs(tv,i);\n }\n\n vector<pair<int,int> > ppv(n);\n for(int i=0; i<n; ++i) {\n ppv.push_back(make_pair(p[i],i));\n }\n sort(ppv.begin(), ppv.end());\n reverse(ppv.begin(), ppv.end());\n\n memset(seen, false, sizeof(seen));\n for(int i=0; i<ppv.size(); ++i) {\n int v = ppv[i].second;\n if(seen[v]) continue;\n ans -= ppv[i].first;\n dfs2(tv,v);\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 1124, "score_of_the_acc": -0.4763, "final_rank": 8 } ]
aoj_2044_cpp
Problem F: Lying about Your Age You have moved to a new town. As starting a new life, you have made up your mind to do one thing: lying about your age. Since no person in this town knows your history, you don’t have to worry about immediate exposure. Also, in order to ease your conscience somewhat, you have decided to claim ages that represent your real ages when interpreted as the base- M numbers (2 ≤ M ≤ 16). People will misunderstand your age as they interpret the claimed age as a decimal number (i.e. of the base 10). Needless to say, you don’t want to have your real age revealed to the people in the future. So you should claim only one age each year, it should contain digits of decimal numbers (i.e. ‘0’ through ‘9’), and it should always be equal to or greater than that of the previous year (when interpreted as decimal). How old can you claim you are, after some years? Input The input consists of multiple datasets. Each dataset is a single line that contains three integers A , B , and C , where A is the present real age (in decimal), B is the age you presently claim (which can be a non-decimal number), and C is the real age at which you are to find the age you will claim (in decimal). It is guaranteed that 0 ≤ A < C ≤ 200, while B may contain up to eight digits. No digits other than ‘0’ through ‘9’ appear in those numbers. The end of input is indicated by A = B = C = -1, which should not be processed. Output For each dataset, print in a line the minimum age you can claim when you become C years old. In case the age you presently claim cannot be interpreted as your real age with the base from 2 through 16, print -1 instead. Sample Input 23 18 53 46 30 47 -1 -1 -1 Output for the Sample Input 49 -1
[ { "submission_id": "aoj_2044_9685683", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int a,b,c;\n while(cin >> a >> b >> c && a!=-1) {\n bool f=0;\n for(int i=2; i<17; i++) {\n int x=0,z=a;\n string s=\"\";\n while(z) {\n s+=(char)(z%i+'0');\n z/=i;\n }\n reverse(s.begin(),s.end());\n bool ff=1;\n for(int j=0; j<s.size(); j++) {\n if(!isdigit(s[j])) ff=0;\n else if(s[i]-'0'>=i) ff=0;\n }\n if(!ff) continue;\n stringstream ss;\n ss << s;\n ss >> x;\n if(x==b) f=1;\n }\n if(!f) {\n cout << -1 << endl;\n continue;\n }\n for(int k=a+1; k<=c; k++) {\n int r=1<<29;\n for(int i=2; i<17; i++) {\n int x=0,z=k;\n string s=\"\";\n while(z) {\n s+=(char)(z%i+'0');\n z/=i;\n }\n reverse(s.begin(),s.end());\n bool ff=1;\n for(int j=0; j<s.size(); j++) {\n if(!isdigit(s[j])) ff=0;\n else if(s[i]-'0'>=i) ff=0;\n }\n if(!ff) continue;\n stringstream ss;\n ss << s;\n ss >> x;\n if(b<=x) r=min(r,x);\n }\n b=r;\n }\n cout << b << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3228, "score_of_the_acc": -1.1282, "final_rank": 8 }, { "submission_id": "aoj_2044_2276280", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nint main() {\n int a,b,c;\n while(cin>>a>>b>>c&&a!=-1) {\n bool f=0;\n for(int i=2; i<17; i++) {\n int x=0,z=a;\n string s=\"\";\n while(z) {\n s+=(char)(z%i+'0');\n z/=i;\n }\n reverse(s.begin(),s.end());\n bool ff=1;\n for(int j=0; j<s.size(); j++) {\n if(!isdigit(s[j])) ff=0;\n else if(s[i]-'0'>=i) ff=0;\n }\n if(!ff) continue;\n stringstream ss;\n ss<<s;ss>>x;\n if(x==b) f=1;\n }\n if(!f) {\n cout<<-1<<endl;\n continue;\n }\n for(int k=a+1; k<=c; k++) {\n int r=1<<29;\n for(int i=2; i<17; i++) {\n int x=0,z=k;\n string s=\"\";\n while(z) {\n s+=(char)(z%i+'0');\n z/=i;\n }\n reverse(s.begin(),s.end());\n bool ff=1;\n for(int j=0; j<s.size(); j++) {\n if(!isdigit(s[j])) ff=0;\n else if(s[i]-'0'>=i) ff=0;\n }\n if(!ff) continue;\n stringstream ss;\n ss<<s;ss>>x;\n if(b<=x) r=min(r,x);\n }\n b=r;\n }\n cout<<b<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3152, "score_of_the_acc": -1.1047, "final_rank": 7 }, { "submission_id": "aoj_2044_2276082", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nint m(int a,int k){\n if(!a) return 0;\n string s;\n while(a){\n int tmp=a%k;\n if(tmp>9) return -1;\n s+='0'+tmp;\n a/=k;\n }\n reverse(s.begin(),s.end());\n return stoll(s);\n}\nsigned main(){\n int a,b,c;\n while(cin>>a>>b>>c,~a){\n bool f=1;\n for(int i=2;i<=16;i++) f&=m(a,i)!=b;\n int res=b;\n for(int j=a;j<=c;j++){\n int tmp=-1;\n for(int i=2;i<=16;i++)\n\tif(m(j,i)>=res)\n\t if(tmp<0||m(j,i)<tmp) tmp=m(j,i);\n if(tmp<0){\n\tf=1;\n\tbreak;\n }\n res=tmp;\n }\n cout<<(f?-1:res)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3132, "score_of_the_acc": -1.0985, "final_rank": 6 }, { "submission_id": "aoj_2044_1149264", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[20];\nchar s[20];\nint main(){\n\tint a,b,c;\n\twhile(scanf(\"%d%d%d\",&a,&b,&c),~a){\n\t\tbool ok=false;\n\t\tsprintf(str,\"%d\",b);\n\t\tfor(int i=2;i<17;i++){\n\t\t\tlong long tmp=0;\n\t\t\tfor(int j=0;str[j];j++){\n\t\t\t\ttmp*=i;\n\t\t\t\ttmp+=str[j]-'0';\n\t\t\t}\n\t\t\tbool OK=true;\n\t\t\tfor(int j=0;str[j];j++){\n\t\t\t\tif(str[j]-'0'>=i)OK=false;\n\t\t\t}\n\t\t\tif(tmp==a&&OK)ok=true;\n\t\t}\n\t\tif(!ok){\n\t\t\tprintf(\"-1\\n\");continue;\n\t\t}\n\t\tfor(int i=a+1;i<=c;i++){\n\t\t\tint to=999999999;\n\t\t\tfor(int j=2;j<17;j++){\n\t\t\t\tint tmp=i;\n\t\t\t\tint at=0;\n\t\t\t\twhile(tmp){\n\t\t\t\t\ts[at++]='0'+tmp%j;\n\t\t\t\t\ttmp/=j;\n\t\t\t\t}\n\t\t\t\tok=true;\n\t\t\t\tfor(int k=0;k<at;k++){\n\t\t\t\t\tif(s[k]>'9')ok=false;\n\t\t\t\t\tstr[at-1-k]=s[k];\n\t\t\t\t}\n\t\t\t\tif(ok){\n\t\t\t\t\tstr[at]=0;\n\t\t\t\t\tint val;\n\t\t\t\t\tsscanf(str,\"%d\",&val);\n\t\t\t\t\tif(b<=val)to=min(to,val);\n\t\t\t\t}\n\t\t\t}\n\t\t\tb=to;\n\t\t}\n\t\tprintf(\"%d\\n\",b);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1048, "score_of_the_acc": -0.3247, "final_rank": 2 }, { "submission_id": "aoj_2044_1003742", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\n\nbool converter(int value,int base,string &s){\n string ret = \"\";\n while( value >= base ){\n int tmp = value % base;\n value /= base;\n if( 0 <= tmp && tmp <= 9 ) ret += string(1,(char)('0'+tmp));\n else return false;\n }\n if( value != 0 ) {\n int tmp = value;\n if( 0 <= tmp && tmp <= 9 ) ret += string(1,(char)('0'+tmp));\n else return false;\n }\n if( ret.empty() ) ret = \"0\";\n reverse(ret.begin(),ret.end());\n s = ret;\n return true;\n}\n\nstring itos(int i) { stringstream ss; ss << i; return ss.str(); }\n\nint main(){\n int A,C;\n string B;\n while( cin >> A >> B >> C ){\n if( A == -1 && B == \"-1\" && C == -1 ) break;\n string boarder = B;\n bool failed = false;\n REP(age,A,C+1){\n //cout << age << \" \" << boarder << endl;\n int nebear = IINF;\n REP(base,2,17){\n string tmp = \"\";\n if( !converter(age,base,tmp) ) continue;\n if( age == A && tmp != boarder ) continue;\n else {\n int boar = (atoi)(boarder.c_str());\n int curr = (atoi)(tmp.c_str());\n if( boar > curr ) continue;\n nebear = min(nebear,curr);\n }\n }\n if( nebear == IINF ) failed = true;\n if( failed ) break;\n boarder = itos(nebear);\n }\n if( failed ) puts(\"-1\");\n else cout << boarder << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1244, "score_of_the_acc": -0.4623, "final_rank": 5 }, { "submission_id": "aoj_2044_494316", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nll int2int(ll src, int base) {\n string res;\n while(src != 0) {\n char c = src%base;\n if (c < 10) c += '0';\n else c += 'a'-10;\n res = string(1,c) + res;\n src /= base;\n }\n FOR(it, res) if (!isdigit(*it)) return -1;\n return atoi(res.c_str());\n}\n\nint main() {\n ll A,B,C;\n while(cin>>A>>B>>C,A!=-1) {\n bool f = 0;\n for (int i=2; i<=16; ++i) {\n if (B == int2int(A,i)) {\n f = 1;\n break;\n }\n }\n if (!f) {\n puts(\"-1\");\n continue;\n }\n for (int i=A+1; i<=C; ++i) {\n ll next = -1;\n for (int j=2; j<=16; ++j) {\n ll k = int2int(i,j);\n if (k >= B) {\n if (next == -1 || next > k) {\n next = k;\n }\n }\n }\n B = next;\n }\n cout << B << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1216, "score_of_the_acc": -0.428, "final_rank": 3 }, { "submission_id": "aoj_2044_492344", "code_snippet": "//41\n#include<iostream>\n#include<cctype>\n#include<string>\n#include<cstdlib>\n\nusing namespace std;\n\nstring itos(int i,int b){\n if(i){\n string s;\n while(i){\n int d=i%b;\n s+=(d<10)?'0'+d:'a'+d-10;\n i/=b;\n }\n return string(s.rbegin(),s.rend());\n }else{\n return \"0\";\n }\n} \n\nint main(){\n int a,c;\n string b;\n while(cin>>a>>b>>c,a!=-1){\n int i;\n for(i=16;i>=2;i--){\n if(itos(a,i)==b)break;\n }\n if(i<2){\n cout<<-1<<endl;\n }else{\n for(;a<=c;a++){\n\tfor(int i=16;i>=2;i--){\n\t string s=itos(a,i);\n\t int j;\n\t for(j=0;j<s.size();j++){\n\t if(isalpha(s[j]))break;\n\t }\n\t if(j==s.size()&&atoi(b.c_str())<=atoi(s.c_str())){\n\t b=s;\n\t break;\n\t }\n\t}\n }\n cout<<b<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1228, "score_of_the_acc": -0.4317, "final_rank": 4 }, { "submission_id": "aoj_2044_428729", "code_snippet": "#include <iostream>\n\nusing namespace std;\ntypedef long long ll;\nconst ll INF = 0x7fffffffffffffffll;\n\nll radix_convert(ll x, int base){\n\tll a = x, b = 0, c = 1;\n\twhile(a > 0){\n\t\tif(a % base >= 10){ return -1; }\n\t\tb = b + a % base * c;\n\t\ta /= base;\n\t\tc *= 10;\n\t}\n\treturn b;\n}\n\nint main(){\n\twhile(true){\n\t\tll a, b, c;\n\t\tcin >> a >> b >> c;\n\t\tif(a < 0 && b < 0 && c < 0){ break; }\n\t\tbool accept = false;\n\t\tfor(int base = 2; base <= 16; ++base){\n\t\t\tif(radix_convert(a, base) == b){ accept = true; break; }\n\t\t}\n\t\tif(!accept){\n\t\t\tcout << -1 << endl;\n\t\t\tcontinue;\n\t\t}\n\t\tfor(++a; a <= c; ++a){\n\t\t\tll d = INF;\n\t\t\tfor(int base = 2; base <= 16; ++base){\n\t\t\t\tll e = a, f = 0, g = 1;\n\t\t\t\twhile(e > 0){\n\t\t\t\t\tif(e % base >= 10){ break; }\n\t\t\t\t\tf = f + e % base * g;\n\t\t\t\t\te /= base;\n\t\t\t\t\tg *= 10;\n\t\t\t\t}\n\t\t\t\tif(e == 0 && f >= b){\n\t\t\t\t\td = min(d, f);\n\t\t\t\t}\n\t\t\t}\n\t\t\tb = d;\n\t\t\tif(b == INF){ break; }\n\t\t}\n\t\tcout << b << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2044_391644", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <set>\n#include <string>\n#include <sstream>\n#include <cstdlib>\n\nusing namespace std;\n\ntypedef long long ll;\nconst ll INF=(1LL<<50);\n\nll DecToBaseNum(ll num,int base){\n string s;\n ll res=0;\n while(num){\n stringstream ss;\n ss<<(num%base);\n if(num%base>9)return -1;\n s=ss.str()+s;\n num/=base;\n }\n for(int i=0;i<s.size();i++)res=res*10+(s[i]-'0');\n return res;\n}\n\nll A,B,C;\nint main(){\n while(cin>>A>>B>>C&&!(A==-1&&B==-1&&C==-1)){\n bool ok=false;\n for(int i=2;i<=16;i++){\n ll a=DecToBaseNum(A,i);\n if(a==B){\n\tok=true;\n\tbreak;\n }\n }\n if(!ok)cout<<-1<<endl;\n else{\n ll cur=B;\n ok=true;\n for(int i=A+1;i<=C;i++){\n\tll nxt=INF;\n\tfor(int j=2;j<=16;j++){\n\t ll a=DecToBaseNum(i,j);\n\t if(a>=cur&&nxt>a)nxt=a;\n\t}\n\tif(nxt==INF){\n\t ok=false;\n\t break;\n\t}\n\tcur=nxt;\n }\n if(!ok)cout<<-1<<endl;\n else cout<<cur<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 944, "score_of_the_acc": -1.2924, "final_rank": 9 } ]
aoj_2042_cpp
Problem D: So Sleepy You have an appointment to meet a friend of yours today, but you are so sleepy because you didn’t sleep well last night. As you will go by trains to the station for the rendezvous, you can have a sleep on a train. You can start to sleep as soon as you get on a train and keep asleep just until you get off. However, because of your habitude, you can sleep only on one train on your way to the destination. Given the time schedule of trains as well as your departure time and your appointed time, your task is to write a program which outputs the longest possible time duration of your sleep in a train, under the condition that you can reach the destination by the appointed time. Input The input consists of multiple datasets. Each dataset looks like below: S T D Time D A Time A N 1 K 1,1 Time 1,1 ... K 1, N 1 Time 1, N 1 N 2 K 2,1 Time 2,1 ... K 2, N 2 Time 2, N 2 ... N T K T ,1 Time T ,1 ... K T , N T Time T , N T The first line of each dataset contains S (1 ≤ S ≤ 1000) and T (0 ≤ T ≤ 100), which denote the numbers of stations and trains respectively. The second line contains the departure station ( D ), the departure time ( Time D ), the appointed station ( A ), and the appointed time ( Time A ), in this order. Then T sets of time schedule for trains follow. On the first line of the i -th set, there will be N i which indicates the number of stations the i -th train stops at. Then N i lines follow, each of which contains the station identifier ( K i,j ) followed by the time when the train stops at (i.e. arrives at and/or departs from) that station ( Time i,j ). Each station has a unique identifier, which is an integer between 1 and S . Each time is given in the format hh : mm , where hh represents the two-digit hour ranging from “00” to “23”, and mm represents the two-digit minute from “00” to “59”. The input is terminated by a line that contains two zeros. You may assume the following: each train stops at two stations or more; each train never stops at the same station more than once; a train takes at least one minute from one station to the next; all the times that appear in each dataset are those of the same day; and as being an expert in transfer, you can catch other trains that depart at the time just you arrive the station. Output For each dataset, your program must output the maximum time in minutes you can sleep if you can reach to the destination station in time, or “impossible” (without the quotes) otherwise. Sample Input 3 1 1 09:00 3 10:00 3 1 09:10 2 09:30 3 09:40 3 2 1 09:00 1 10:00 3 1 09:10 2 09:30 3 09:40 3 3 09:20 2 09:30 1 10:00 1 0 1 09:00 1 10:00 1 0 1 10:00 1 09:00 3 1 1 09:00 3 09:35 3 1 09:10 2 09:30 3 09:40 4 3 1 09:00 4 11:00 3 1 09:10 2 09:20 4 09:40 3 1 10:30 3 10:40 4 10:50 4 1 08:50 2 09:30 3 10:30 4 11:10 0 0 Output for the Sample Input 30 30 0 impossible impossible 60
[ { "submission_id": "aoj_2042_10850499", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <vector>\n\nusing namespace std;\n\nint dp[1000][24 * 60][2];\nint n, m, dest, destTime;\n\nvector<pair<int, int>> lines[100], stationInfo[1000][24 * 60]; // time, station / line index\n\nint getAns(int station, int time, bool slept) {\n\tif(time > destTime) return -2;\n\n\tint &ret = dp[station][time][slept];\n\tif(ret != -1) return ret;\n\n\tret = -2;\n\tif(station == dest) ret = 0;\n\tret = max(ret, getAns(station, time + 1, slept));\n\tfor(auto &info: stationInfo[station][time]) {\n\t\tint nextLine = info.first;\n\t\tint stIndex = info.second;\n\t\tif(stIndex + 1 < lines[nextLine].size())\n\t\t\tret = max(ret, getAns(lines[nextLine][stIndex + 1].second, lines[nextLine][stIndex + 1].first, slept));\n\t\tif(!slept)\n\t\t\tfor(int ind = stIndex + 1; ind < lines[nextLine].size(); ind++) {\n\t\t\t\tint res = getAns(lines[nextLine][ind].second, lines[nextLine][ind].first, true);\n\t\t\t\tif(res >= 0)\n\t\t\t\t\tret = max(ret, res + lines[nextLine][ind].first - lines[nextLine][stIndex].first);\n\t\t\t}\n\t}\n\n\treturn ret;\n}\n\nint main(void) {\n\twhile(scanf(\"%d %d\", &n, &m), n > 0) {\n\t\tfor(int i = 0; i < m; i++) lines[i].clear();\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < 24 * 60; j++)\n\t\t\t\tstationInfo[i][j].clear();\n\n\t\tint start, startTime, temp[2];\n\t\tscanf(\"%d %d:%d\", &start, temp, temp + 1);\n\t\tstartTime = temp[0] * 60 + temp[1];\n\t\tscanf(\"%d %d:%d\", &dest, temp, temp + 1);\n\t\tdestTime = temp[0] * 60 + temp[1];\n\t\tstart--, dest--;\n\n\t\tfor(int i = 0; i < m; i++) {\n\t\t\tint visitn;\n\t\t\tscanf(\"%d\", &visitn);\n\t\t\tfor(int j = 0; j < visitn; j++) {\n\t\t\t\tint station;\n\t\t\t\tscanf(\"%d %d:%d\", &station, temp, temp + 1);\n\t\t\t\tlines[i].push_back(make_pair(temp[0] * 60 + temp[1], station - 1));\n\t\t\t\tstationInfo[station - 1][temp[0] * 60 + temp[1]].push_back(make_pair(i, j));\n\t\t\t}\n\t\t}\n\n\t\tmemset(dp, -1, sizeof(dp));\n\t\tint res = getAns(start, startTime, false);\n\t\tif(res == -2) printf(\"impossible\\n\");\n\t\telse printf(\"%d\\n\", res);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 55888, "score_of_the_acc": -0.891, "final_rank": 7 }, { "submission_id": "aoj_2042_3239975", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 1440\n\nstruct Info{\n\tInfo(int arg_station_id,int arg_stop_time){\n\t\tstation_id = arg_station_id;\n\t\tstop_time = arg_stop_time;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\t\treturn stop_time < arg.stop_time;\n\t}\n\tint station_id,stop_time;\n};\n\nstruct Data{\n\tData(int arg_train_id,int arg_stop_index){\n\t\ttrain_id = arg_train_id;\n\t\tstop_index = arg_stop_index;\n\t}\n\tint train_id,stop_index;\n};\n\nint start,start_time;\nint goal,goal_time;\nint num_station,num_train;\nint dp[1005][NUM];\nvector<Info> TRAIN[105];\nvector<Data> STATION[1005][NUM];\n\nint getTime(char buf[6]){\n\n\tint hour = 10*(buf[0]-'0')+(buf[1]-'0');\n\tint minute = 10*(buf[3]-'0')+(buf[4]-'0');\n\n\treturn 60*hour+minute;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < num_station; i++){\n\t\tfor(int k = 0; k < NUM; k++){\n\n\t\t\tSTATION[i][k].clear();\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_train; i++){\n\n\t\tTRAIN[i].clear();\n\t}\n\n\tchar buf[6];\n\n\tscanf(\"%d %s\",&start,buf);\n\tstart--;\n\tstart_time = getTime(buf);\n\n\tscanf(\"%d %s\",&goal,buf);\n\tgoal--;\n\tgoal_time = getTime(buf);\n\n\tint num_stop;\n\tint station_id,stop_time;\n\n\tfor(int i = 0; i < num_train; i++){\n\n\t\tscanf(\"%d\",&num_stop);\n\n\t\tfor(int k = 0; k < num_stop; k++){\n\n\t\t\tscanf(\"%d %s\",&station_id,buf);\n\t\t\tstation_id--;\n\t\t\tstop_time = getTime(buf);\n\n\t\t\tTRAIN[i].push_back(Info(station_id,stop_time));\n\t\t}\n\t\tsort(TRAIN[i].begin(),TRAIN[i].end());\n\t}\n\n\tif(start_time > goal_time){\n\n\t\tprintf(\"impossible\\n\");\n\t\treturn;\n\t}\n\n\tfor(int i = 0; i < num_train; i++){\n\t\tfor(int k = 0; k < TRAIN[i].size(); k++){\n\n\t\t\tSTATION[TRAIN[i][k].station_id][TRAIN[i][k].stop_time].push_back(Data(i,k));\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_station; i++){\n\t\tfor(int k = start_time; k <= goal_time; k++){\n\n\t\t\tdp[i][k] = -1;\n\t\t}\n\t}\n\n\tdp[start][start_time] = 0;\n\n\tint train_id,stop_index;\n\n\tfor(int k = start_time; k < goal_time; k++){\n\t\tfor(int i = 0; i < num_station; i++){\n\n\t\t\tif(dp[i][k] == -1)continue;\n\t\t\tdp[i][k+1] = max(dp[i][k+1],dp[i][k]);\n\n\t\t\tfor(int a = 0; a < STATION[i][k].size(); a++){\n\n\t\t\t\ttrain_id = STATION[i][k][a].train_id;\n\t\t\t\tstop_index = STATION[i][k][a].stop_index;\n\n\t\t\t\tfor(int b = stop_index+1; b < TRAIN[train_id].size(); b++){\n\n\t\t\t\t\tif(TRAIN[train_id][b].stop_time > goal_time)break;\n\n\t\t\t\t\tstation_id = TRAIN[train_id][b].station_id;\n\t\t\t\t\tstop_time = TRAIN[train_id][b].stop_time;\n\n\t\t\t\t\tdp[station_id][stop_time] = max(dp[station_id][stop_time],max(dp[i][k],stop_time-k));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans = -1;\n\n\tfor(int i = start_time; i <= goal_time; i++){\n\n\t\tans = max(ans,dp[goal][i]);\n\t}\n\n\tif(ans == -1){\n\n\t\tprintf(\"impossible\\n\");\n\n\t}else{\n\n\t\tprintf(\"%d\\n\",ans);\n\t}\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d\",&num_station,&num_train);\n\t\tif(num_station == 0 && num_train == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 50052, "score_of_the_acc": -0.8425, "final_rank": 6 }, { "submission_id": "aoj_2042_2285742", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint N,M,S,T;\nint st, ed;\n\nunordered_map<int,vector<int>> R;\nunordered_map<int,vector<int>> tr;\n\nint ghs(int a,int b){\n return a * 24*60 + b;\n}\n\nint gettime(int t1,int t2){\n return t1*60 + t2;\n}\n\nconst int INF = (1<<29);\ntypedef pair<int,int> P;\nP dp[60*24+1][1101];\nvoid update(P &a, P b){\n if( a.first < b.first ) a.first = b.first;\n if( a.second < b.second ) a.second = b.second;\n if( a.first < a.second ) a.first = a.second;\n}\nint solve(){\n fill( dp[0], dp[ed+1], P(-INF,-INF) );\n dp[st][S+M] = P(0,0); \n for(int t=st;t<=ed;t++){\n for(int i=0;i<N+M;i++){\n int id = i%(N+M);\n if( dp[t][id].first == -INF ) continue; \n if( id < M ) { // train\n if( R.count( ghs(id,t) ) )\n for( int nid : R[ghs(id,t)] )\n update( dp[t][nid+M], P(dp[t][id].first,0) );\n update( dp[t+1][id], P(dp[t][id].first,dp[t][id].second+1) );\n } else { //station\n int x = id-M;\n if( tr.count( ghs(x,t) ) )\n for( int nid : tr[ghs(x,t)] )\n update( dp[t+1][nid], P(dp[t][id].first,1) );\n update( dp[t+1][id], P(dp[t][id].first,0) ); \n }\n }\n }\n return dp[ed][T+M].first;\n}\n\n\nint main(){\n while( ~scanf(\"%d%d\",&N,&M) && (N|M) ) {\n int t1,t2,t3,t4;\n scanf(\"%d %d:%d %d %d:%d\",&S, &t1, &t2, &T, &t3, &t4);\n --S; --T;\n st = gettime( t1, t2 ), ed = gettime( t3, t4 );\n \n for(int i=0;i<M;i++){\n int n; scanf(\"%d\",&n); \n for(int j=0;j<n;j++){\n int k;\n scanf(\"%d %d:%d\",&k,&t1,&t2);\n --k; \n R[ghs(i,gettime(t1,t2))].emplace_back(k);\n tr[ghs(k,gettime(t1,t2))].emplace_back(i);\n //cout << i << \" \"<< gettime(t1,t2) << \" \" << R[i][gettime(t1,t2)] << endl;\n // cout << k << \" \"<< gettime(t1,t2) << \" \" << tr[k][gettime(t1,t2)] << endl;\n } \n }\n\n int res = solve();\n if( res < 0 ) printf(\"impossible\\n\");\n else printf(\"%d\\n\",res);\n tr.clear();\n R.clear();\n }\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 30312, "score_of_the_acc": -0.5545, "final_rank": 4 }, { "submission_id": "aoj_2042_1126479", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst char EMPTY = 'X';\n \nstruct Node{\n short value;\n char lazy;\n short lazy_coef; \n Node(short value=0,char lazy=EMPTY,short lazy_coef=0):value(value),lazy(lazy),lazy_coef(lazy_coef){}\n};\n \nclass SegTree{\npublic:\n vector<Node> RMQ;\n int limit,N; // N は要素数\n \n void init(int tmp){\n N = tmp;\n int N_N = 1;\n while(N_N < N)N_N *= 2;\n limit = N_N;\n RMQ.clear();\n RMQ.resize(2.3*limit);\n rep(i,2.3*limit-1) RMQ[i] = Node();\n }\n\n short _build(int cur,int L,int R){\n if( !( 0 <= cur && cur < 2*limit-1 ) ) return 0;\n if( L == R-1 ){\n if( L >= N ) return 0;\n RMQ[cur] = 0; // RMQ の各要素を0で初期化、 1 なら RMQ[cur] = 1などなど...\n } else {\n short vl = _build(cur*2+1,L,(L+R)/2);\n short vr = _build(cur*2+2,(L+R)/2,R);\n RMQ[cur].value = vl+vr;\n }\n return RMQ[cur].value;\n }\n\n void build() { _build(0,0,limit); }\n\n inline void value_evaluate(int index,int L,int R){\n if( RMQ[index].lazy == 'A' ){\n RMQ[index].value += ( R - L ) * RMQ[index].lazy_coef;\n } else if( RMQ[index].lazy == 'S' ){\n RMQ[index].value = ( R - L ) * RMQ[index].lazy_coef;\n }\n }\n \n inline void lazy_evaluate(int index,int L,int R){\n value_evaluate(index,L,R);\n if( index < limit && RMQ[index].lazy != EMPTY ) {\n if( RMQ[index].lazy == 'A' ) {\n if( RMQ[index*2+1].lazy == EMPTY ) RMQ[index*2+1].lazy = 'A'; \n if( RMQ[index*2+2].lazy == EMPTY ) RMQ[index*2+2].lazy = 'A';\n RMQ[index*2+1].lazy_coef += RMQ[index].lazy_coef;\n RMQ[index*2+2].lazy_coef += RMQ[index].lazy_coef;\n } else if( RMQ[index].lazy == 'S') {\n RMQ[index*2+1].lazy = RMQ[index*2+2].lazy = 'S';\n RMQ[index*2+1].lazy_coef = RMQ[index].lazy_coef;\n RMQ[index*2+2].lazy_coef = RMQ[index].lazy_coef;\n }\n }\n RMQ[index].lazy = EMPTY;\n RMQ[index].lazy_coef = 0;\n }\n \n inline void value_update(int index){ RMQ[index].value = RMQ[index*2+1].value + RMQ[index*2+2].value; }\n\n void _update(int a,int b,char opr,int v,int index,int L,int R){\n lazy_evaluate(index,L,R);\n if( b <= L || R <= a )return;\n if( a <= L && R <= b ){\n RMQ[index].lazy = opr; // 今いるノードに遅延を設定して処理をしてもらう\n if( opr == 'A' ) RMQ[index].lazy_coef += v; \n else if( opr == 'S' ) RMQ[index].lazy_coef = v; \n lazy_evaluate(index,L,R);\n return;\n }\n _update(a,b,opr,v,index*2+1,L,(L+R)/2);\n _update(a,b,opr,v,index*2+2,(L+R)/2,R);\n value_update(index);\n }\n\n void update(int a,int b,char opr,short v){ _update(a,b,opr,v,0,0,limit); } \n\n int _query(int a,int b,int index,int L,int R){\n lazy_evaluate(index,L,R); \n if( b <= L || R <= a ) return 0;\n if( a <= L && R <= b ) return RMQ[index].value;\n short tmp1 = _query(a,b,index*2+1,L,(L+R)/2);\n short tmp2 = _query(a,b,index*2+2,(L+R)/2,R);\n short ret = tmp1+tmp2;\n value_update(index); //ここまでくると子の値が正しくなっているのでその和をとって今いるノードにも正しい値をいれる\n return ret;\n }\n short query(int a,int b) { return _query(a,b,0,0,limit); }\n};\n\ntypedef pair<int,int> ii;\nconst int MAX_V = 1001;\nstruct Edge {\n int src,dst,depart,arrive;\n};\n\nint S,T,D,timeD,Rendezvous,timeRendezvous;\n\ninline int toMinute(string s) { \n int ret = ( s[3] - '0' ) * 10 + ( s[4] - '0' );\n return ret + ( ( s[0] - '0' ) * 10 + ( s[1] - '0' ) ) * 60;\n}\n\nstruct Data {\n int cur,time;\n bool operator < ( const Data &data ) const { \n if( time != data.time ) return time > data.time;\n return cur > data.cur;\n }\n};\n\nconst int MAX_T = 1441;\nvector<Edge> G[MAX_V],rG[MAX_V];\nSegTree dp[MAX_V][2]; // dp[station][ 0 : from dept, 1 : to rendezvous] := reachable?\nvector<vector<bool> > used;\n\nvoid compute(vector<int> &N,vector<vector<ii> > &KT){\n rep(i,S) rep(j,2) {\n dp[i][j].init(MAX_T);\n dp[i][j].build();\n }\n \n // dept to other station\n used.clear();\n used.resize(S,vector<bool>(MAX_T,false));\n dp[D][0].update(timeD,MAX_T,'S',1);\n used[D][timeD] = true;\n priority_queue<Data> Q;\n Q.push((Data){D,timeD});\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n rep(i,(int)G[data.cur].size()){\n if( data.time > G[data.cur][i].depart ) continue;\n int next = G[data.cur][i].dst;\n int arrive = G[data.cur][i].arrive;\n if( !used[next][arrive] ) {\n used[next][arrive] = true;\n dp[next][0].update(arrive,1500,'S',1);\n Q.push((Data){next,arrive});\n }\n }\n }\n\n // each station to ランデヴー\n rep(i,used.size()) rep(j,used[i].size()) used[i][j] = false;\n dp[Rendezvous][1].update(0,timeRendezvous+1,'S',1);\n used[Rendezvous][timeRendezvous] = true;\n assert( Q.empty() );\n Q.push((Data){Rendezvous,timeRendezvous});\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n rep(i,(int)rG[data.cur].size()){\n if( data.time < rG[data.cur][i].depart ) continue;\n int prev = rG[data.cur][i].dst;\n int r_arrive = rG[data.cur][i].arrive;\n if( !used[prev][r_arrive] ){\n used[prev][r_arrive] = true;\n dp[prev][1].update(0,r_arrive+1,'S',1);\n Q.push((Data){prev,r_arrive});\n }\n }\n }\n int maxi = -1;\n rep(i,N.size()){\n rep(j,N[i]-1) {\n int f = KT[i][j].first, ft = KT[i][j].second;\n if( !dp[f][0].query(ft,ft+1) ) continue;\n REP(k,j+1,N[i]){\n int t = KT[i][k].first, tt = KT[i][k].second;\n int sleep = tt - ft;\n assert( sleep >= 1 );\n if( sleep <= maxi ) continue;\n if( dp[t][1].query(tt,tt+1) ) {\n maxi = max(maxi,sleep);\n }\n }\n }\n }\n \n if( maxi == -1 && D == Rendezvous && timeD <= timeRendezvous ) puts(\"0\");\n else if( maxi == -1 ) puts(\"impossible\");\n else printf(\"%d\\n\",maxi);\n}\n\n\nint main(){\n while( scanf(\"%d %d\",&S,&T), S|T ){\n rep(i,S) G[i].clear(), rG[i].clear();\n rep(i,MAX_V) rep(j,2) dp[i][j].RMQ.clear();\n string temp;\n cin >> D >> temp;\n --D;\n timeD = toMinute(temp);\n cin >> Rendezvous >> temp;\n --Rendezvous;\n timeRendezvous = toMinute(temp);\n vector<int> N(T);\n vector<vector<ii> > KT(T,vector<ii>());\n rep(i,T){\n cin >> N[i];\n KT[i].resize(N[i]);\n rep(j,N[i]) {\n cin >> KT[i][j].first >> temp;\n --KT[i][j].first;\n KT[i][j].second = toMinute(temp);\n }\n rep(j,N[i]-1) {\n int f = KT[i][j].first , ft = KT[i][j].second;\n int t = KT[i][j+1].first, tt = KT[i][j+1].second;\n G[f].push_back((Edge){f,t,ft,tt});\n rG[t].push_back((Edge){t,f,tt,ft});\n }\n }\n\n compute(N,KT);\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 2270, "memory_kb": 62088, "score_of_the_acc": -2, "final_rank": 9 }, { "submission_id": "aoj_2042_1126478", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst char EMPTY = 'X';\n \nstruct Node{\n short value;\n /*\n lazy : \n A : 加算の遅延 ( ADD )\n S : 区間の要素を指定した値にする遅延 ( SET )\n\n lazy_coef : \n 区間にいくら加算するのかor何をセットするのかを記録\n */\n char lazy;\n short lazy_coef; \n Node(short value=0,char lazy=EMPTY,short lazy_coef=0):value(value),lazy(lazy),lazy_coef(lazy_coef){}\n};\n \nclass SegTree{\npublic:\n vector<Node> RMQ;\n int limit,N; // N は要素数\n \n void init(int tmp){\n N = tmp;\n int N_N = 1;\n while(N_N < N)N_N *= 2;\n limit = N_N;\n RMQ.clear();\n RMQ.resize(2.3*limit);\n rep(i,2.3*limit-1) RMQ[i] = Node();\n }\n\n /* \n O(n)でRMQを初期化\n [L,R)\n tuple<int,int,int>(総和、最小値、最大値)\n */\n short _build(int cur,int L,int R){\n if( !( 0 <= cur && cur < 2*limit-1 ) ) return 0;\n if( L == R-1 ){\n if( L >= N ) return 0;\n //各要素の値が一定でない場合 ( 配列に初期値をいれてそれで初期化する場合 )\n //buf[] を用意し値を入れ RMQ[cur].value = buf[L]; とする\n // RMQのL番目の要素をbuf[L]で更新\n //RMQ[cur].value = buf[L];\n\n //各要素が全て同じの場合\n RMQ[cur] = 0; // RMQ の各要素を0で初期化、 1 なら RMQ[cur] = 1などなど...\n //minimum_dat[cur] = 0;\n //maximum_dat[cur] = 0;\n } else {\n short vl = _build(cur*2+1,L,(L+R)/2);\n short vr = _build(cur*2+2,(L+R)/2,R);\n RMQ[cur].value = vl+vr;\n }\n return RMQ[cur].value;\n }\n\n //initしてから使用すること!!! でないとlimitに正しい値が入っていません\n void build() { _build(0,0,limit); }\n\n inline void value_evaluate(int index,int L,int R){\n if( RMQ[index].lazy == 'A' ){\n RMQ[index].value += ( R - L ) * RMQ[index].lazy_coef;\n } else if( RMQ[index].lazy == 'S' ){\n RMQ[index].value = ( R - L ) * RMQ[index].lazy_coef;\n }\n }\n \n inline void lazy_evaluate(int index,int L,int R){\n value_evaluate(index,L,R);\n if( index < limit && RMQ[index].lazy != EMPTY ) {\n if( RMQ[index].lazy == 'A' ) {\n if( RMQ[index*2+1].lazy == EMPTY ) RMQ[index*2+1].lazy = 'A'; \n if( RMQ[index*2+2].lazy == EMPTY ) RMQ[index*2+2].lazy = 'A';\n RMQ[index*2+1].lazy_coef += RMQ[index].lazy_coef;\n RMQ[index*2+2].lazy_coef += RMQ[index].lazy_coef;\n } else if( RMQ[index].lazy == 'S') {\n RMQ[index*2+1].lazy = RMQ[index*2+2].lazy = 'S';\n RMQ[index*2+1].lazy_coef = RMQ[index].lazy_coef;\n RMQ[index*2+2].lazy_coef = RMQ[index].lazy_coef;\n }\n }\n RMQ[index].lazy = EMPTY;\n RMQ[index].lazy_coef = 0;\n }\n \n inline void value_update(int index){ RMQ[index].value = RMQ[index*2+1].value + RMQ[index*2+2].value; }\n\n void _update(int a,int b,char opr,int v,int index,int L,int R){\n lazy_evaluate(index,L,R);\n if( b <= L || R <= a )return;\n if( a <= L && R <= b ){\n RMQ[index].lazy = opr; // 今いるノードに遅延を設定して処理をしてもらう\n if( opr == 'A' ) RMQ[index].lazy_coef += v; \n else if( opr == 'S' ) RMQ[index].lazy_coef = v; \n lazy_evaluate(index,L,R);\n return;\n }\n _update(a,b,opr,v,index*2+1,L,(L+R)/2);\n _update(a,b,opr,v,index*2+2,(L+R)/2,R);\n value_update(index);\n }\n\n void update(int a,int b,char opr,short v){ _update(a,b,opr,v,0,0,limit); } \n\n int _query(int a,int b,int index,int L,int R){\n lazy_evaluate(index,L,R); \n if( b <= L || R <= a ) return 0;\n if( a <= L && R <= b ) return RMQ[index].value;\n short tmp1 = _query(a,b,index*2+1,L,(L+R)/2);\n short tmp2 = _query(a,b,index*2+2,(L+R)/2,R);\n short ret = tmp1+tmp2;\n value_update(index); //ここまでくると子の値が正しくなっているのでその和をとって今いるノードにも正しい値をいれる\n return ret;\n }\n\n // [a,b) の総和、最小値、最大値を返す\n short query(int a,int b) { return _query(a,b,0,0,limit); }\n\n};\n\ntypedef pair<int,int> ii;\nconst int MAX_V = 1001;\nstruct Edge {\n int src,dst,depart,arrive;\n};\n\nint S,T,D,timeD,Rendezvous,timeRendezvous;\n\ninline int toMinute(string s) { \n int ret = ( s[3] - '0' ) * 10 + ( s[4] - '0' );\n return ret + ( ( s[0] - '0' ) * 10 + ( s[1] - '0' ) ) * 60;\n}\n\nstruct Data {\n int cur,time;\n bool operator < ( const Data &data ) const { \n if( time != data.time ) return time > data.time;\n return cur > data.cur;\n }\n};\n\nconst int MAX_T = 1441;\nvector<Edge> G[MAX_V],rG[MAX_V];\nSegTree dp[MAX_V][2]; // dp[station][ 0 : from dept, 1 : to rendezvous] := reachable?\nvector<vector<bool> > used;\n\nvoid compute(vector<int> &N,vector<vector<ii> > &KT){\n rep(i,S) rep(j,2) {\n dp[i][j].init(MAX_T);\n dp[i][j].build();\n }\n \n // dept to other station\n used.clear();\n used.resize(S,vector<bool>(MAX_T,false));\n dp[D][0].update(timeD,MAX_T,'S',1);\n used[D][timeD] = true;\n priority_queue<Data> Q;\n Q.push((Data){D,timeD});\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n rep(i,(int)G[data.cur].size()){\n if( data.time > G[data.cur][i].depart ) continue;\n int next = G[data.cur][i].dst;\n int arrive = G[data.cur][i].arrive;\n if( !used[next][arrive] ) {\n used[next][arrive] = true;\n dp[next][0].update(arrive,1500,'S',1);\n Q.push((Data){next,arrive});\n }\n }\n }\n\n // each station to ランデヴー\n rep(i,used.size()) rep(j,used[i].size()) used[i][j] = false;\n dp[Rendezvous][1].update(0,timeRendezvous+1,'S',1);\n used[Rendezvous][timeRendezvous] = true;\n assert( Q.empty() );\n Q.push((Data){Rendezvous,timeRendezvous});\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n rep(i,(int)rG[data.cur].size()){\n if( data.time < rG[data.cur][i].depart ) continue;\n int prev = rG[data.cur][i].dst;\n int r_arrive = rG[data.cur][i].arrive;\n if( !used[prev][r_arrive] ){\n used[prev][r_arrive] = true;\n dp[prev][1].update(0,r_arrive+1,'S',1);\n Q.push((Data){prev,r_arrive});\n }\n }\n }\n int maxi = -1;\n rep(i,N.size()){\n rep(j,N[i]-1) {\n int f = KT[i][j].first, ft = KT[i][j].second;\n if( !dp[f][0].query(ft,ft+1) ) continue;\n REP(k,j+1,N[i]){\n int t = KT[i][k].first, tt = KT[i][k].second;\n int sleep = tt - ft;\n assert( sleep >= 1 );\n if( sleep <= maxi ) continue;\n if( dp[t][1].query(tt,tt+1) ) {\n maxi = max(maxi,sleep);\n }\n }\n }\n }\n \n if( maxi == -1 && D == Rendezvous && timeD <= timeRendezvous ) puts(\"0\");\n else if( maxi == -1 ) puts(\"impossible\");\n else printf(\"%d\\n\",maxi);\n}\n\n\nint main(){\n while( scanf(\"%d %d\",&S,&T), S|T ){\n rep(i,S) G[i].clear(), rG[i].clear();\n rep(i,MAX_V) rep(j,2) dp[i][j].RMQ.clear();\n string temp;\n cin >> D >> temp;\n --D;\n timeD = toMinute(temp);\n cin >> Rendezvous >> temp;\n --Rendezvous;\n timeRendezvous = toMinute(temp);\n vector<int> N(T);\n vector<vector<ii> > KT(T,vector<ii>());\n rep(i,T){\n cin >> N[i];\n KT[i].resize(N[i]);\n rep(j,N[i]) {\n cin >> KT[i][j].first >> temp;\n --KT[i][j].first;\n KT[i][j].second = toMinute(temp);\n }\n rep(j,N[i]-1) {\n int f = KT[i][j].first , ft = KT[i][j].second;\n int t = KT[i][j+1].first, tt = KT[i][j+1].second;\n G[f].push_back((Edge){f,t,ft,tt});\n rG[t].push_back((Edge){t,f,tt,ft});\n }\n }\n\n compute(N,KT);\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 2250, "memory_kb": 62088, "score_of_the_acc": -1.9901, "final_rank": 8 }, { "submission_id": "aoj_2042_469960", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nconst int INF = INT_MAX / 2;\n\nclass Edge{\npublic:\n int to, time1, time2;\n Edge(int to0, int time01, int time02){\n to = to0;\n time1 = time01;\n time2 = time02;\n }\n};\n\nint toInt(string& s)\n{\n return atoi(s.substr(0,2).c_str()) * 60 + atoi(s.substr(3, 2).c_str());\n}\n\nint main()\n{\n for(;;){\n int s, t; // 駅の数、電車の数\n cin >> s >> t;\n if(s == 0)\n return 0;\n\n int start, goal; // 出発地、目的地\n int startTime, goalTime; // 出発時間、待ち合わせの時間\n string tmp1, tmp2;\n cin >> start >> tmp1 >> goal >> tmp2;\n startTime = toInt(tmp1);\n goalTime = toInt(tmp2);\n\n vector<int> n(t); // n[i] : 電車iが停まる駅の数\n vector<vector<int> > stop(t); // stop[i][j] : 電車iがj番目に停まる駅\n vector<vector<int> > time(t); // time[i][j] : 電車iがj番目に停まる時間\n vector<vector<Edge> > edges(s+1);\n vector<vector<Edge> > invEdges(s+1);\n for(int i=0; i<t; ++i){\n cin >> n[i];\n stop[i].resize(n[i]);\n time[i].resize(n[i]);\n\n for(int j=0; j<n[i]; ++j){\n string tmp;\n cin >> stop[i][j] >> tmp;\n time[i][j] = toInt(tmp);\n }\n for(int j=1; j<n[i]; ++j){\n edges[stop[i][j-1]].push_back(Edge(stop[i][j], time[i][j-1], time[i][j]));\n invEdges[stop[i][j]].push_back(Edge(stop[i][j-1], time[i][j-1], time[i][j]));\n }\n }\n\n vector<int> minTime(s+1, INF); // minTime[i] : 出発地から駅iに向かったとき、駅iに到着する最も早い時間\n vector<int> maxTime(s+1, -1); // maxTime[i] : 駅iから目的地に向かったとき、駅iを出発しなければならない最も遅い時間\n\n multimap<int, int> mm;\n mm.insert(make_pair(startTime, start));\n minTime[start] = startTime;\n while(!mm.empty()){\n int currTime = mm.begin()->first;\n int pos = mm.begin()->second;\n mm.erase(mm.begin());\n if(currTime > minTime[pos])\n continue;\n\n for(unsigned i=0; i<edges[pos].size(); ++i){\n Edge e = edges[pos][i];\n if(currTime <= e.time1 && e.time2 < minTime[e.to]){\n minTime[e.to] = e.time2;\n mm.insert(make_pair(e.time2, e.to));\n }\n }\n }\n\n mm.insert(make_pair(-goalTime, goal));\n maxTime[goal] = goalTime;\n while(!mm.empty()){\n int currTime = - mm.begin()->first;\n int pos = mm.begin()->second;\n mm.erase(mm.begin());\n if(currTime < maxTime[pos])\n continue;\n\n for(unsigned i=0; i<invEdges[pos].size(); ++i){\n Edge e = invEdges[pos][i];\n if(currTime >= e.time2 && e.time1 > maxTime[e.to]){\n maxTime[e.to] = e.time1;\n mm.insert(make_pair(-e.time1, e.to));\n }\n }\n }\n\n int ret = -1;\n if(start == goal && startTime <= goalTime)\n ret = 0;\n for(int i=0; i<t; ++i){\n for(int j=0; j<n[i]; ++j){\n for(int k=j+1; k<n[i]; ++k){\n if(minTime[stop[i][j]] <= time[i][j] && time[i][k] <= maxTime[stop[i][k]])\n ret = max(ret, time[i][k] - time[i][j]);\n }\n }\n }\n if(ret == -1)\n cout << \"impossible\" << endl;\n else\n cout << ret << endl;\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 5228, "score_of_the_acc": -0.0296, "final_rank": 1 }, { "submission_id": "aoj_2042_406444", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nint readtime(){\n\tint hh,mm; scanf(\"%d:%d\",&hh,&mm);\n\treturn 60*hh+mm;\n}\n\nint main(){\n\tfor(int n,m;scanf(\"%d%d\",&n,&m),n;){\n\t\tint s; scanf(\"%d\",&s); s--;\n\t\tint s_time=readtime();\n\t\tint g; scanf(\"%d\",&g); g--;\n\t\tint g_time=readtime();\n\n\t\tvector< pair<int,int> > seq[100]; // seq[i] := ( 電車 i の運行予定 )\n\t\trep(i,m){\n\t\t\tint sz; scanf(\"%d\",&sz);\n\t\t\tseq[i].resize(sz);\n\t\t\trep(j,sz){\n\t\t\t\tint u; scanf(\"%d\",&u); u--;\n\t\t\t\tseq[i][j]=make_pair(readtime(),u);\n\t\t\t}\n\t\t\tsort(seq[i].begin(),seq[i].end());\n\t\t}\n\n\t\t// train[t][u] := ( 時刻 t に駅 u から発車する電車の情報 )\n\t\tstatic vector< pair<int,int> > train[1440][1000];\n\t\trep(t,1440) rep(u,n) train[t][u].clear();\n\t\trep(i,m){\n\t\t\trep(j,seq[i].size()){\n\t\t\t\tint t=seq[i][j].first;\n\t\t\t\tint u=seq[i][j].second;\n\t\t\t\ttrain[t][u].push_back(make_pair(i,j));\n\t\t\t}\n\t\t}\n\n\t\tstatic int dp[1440][1000];\n\t\trep(t,1440) rep(u,n) dp[t][u]=-1; // -1 は到達不可能の意味\n\t\tdp[s_time][s]=0;\n\t\trep(t,1439) rep(u,n) if(dp[t][u]!=-1) {\n\t\t\t dp[t+1][u]=max(dp[t+1][u],dp[t][u]); // 電車に乗らずにその駅で待つ\n\t\t\t rep(i,train[t][u].size()){ // 電車に乗る\n\t\t\t\tint id=train[t][u][i].first;\n\t\t\t\tfor(int j=train[t][u][i].second+1;j<seq[id].size();j++){\n\t\t\t\t\tint t_next=seq[id][j].first;\n\t\t\t\t\tint u_next=seq[id][j].second;\n\t\t\t\t\tdp[t_next][u_next]=max(dp[t_next][u_next],max(dp[t][u],t_next-t));\n\t\t\t\t}\n\t\t\t }\n\t\t}\n\n\t\tint ans=-1;\n\t\trep(t,g_time+1) ans=max(ans,dp[t][g]);\n\t\tif(ans==-1) puts(\"impossible\");\n\t\telse printf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 26000, "score_of_the_acc": -0.6018, "final_rank": 5 }, { "submission_id": "aoj_2042_134158", "code_snippet": "#include <iostream>\n#include <string>\n#include <queue>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nclass Edge\n{\npublic:\n\tint d,i,o;\n\tEdge(int d, int i, int o)\n\t:d(d),i(i),o(o)\n\t{}\n};\n\nclass State\n{\npublic:\n\tint n,t;\n\tState(int n, int t)\n\t:n(n),t(t)\n\t{}\n\t\n\tbool operator<(const State& s) const\n\t{\n\t\treturn t>s.t;\n\t}\n};\n\ntypedef vector<Edge> Edges;\ntypedef pair<vector<int>, vector<int> > Train;\n\nint input()\n{\n\tstring s;\n\tcin >> s;\n\t\n\treturn ((s[0]-'0')*10+(s[1]-'0'))*60+((s[3]-'0')*10+(s[4]-'0'));\n}\n\nint beg[1000], end[1000];\n\nint dijk(int S, int st, int lim, vector<Edges>& eg, bool f)\n{\n\tbool v[1000]={0};\n\tpriority_queue<State> q;\n\tq.push(State(S,st));\n\t\n\twhile(!q.empty())\n\t{\n\t\tState s=q.top(); q.pop();\n\t\tif(s.t>lim) break;\n\t\t\n\t\tif(v[s.n]) continue;\n\t\tv[s.n]=1;\n\t\t\n\t\tif(f) beg[s.n]=s.t;\n\t\telse end[s.n]=-s.t+24*60;\n\t\t\n\t\tfor(int i=0; i<eg[s.n].size(); i++)\n\t\t{\n\t\t\tEdge e=eg[s.n][i];\n\t\t\tif(v[e.d]) continue;\n\t\t\tif(s.t > e.i) continue;\n\t\t\t\n\t\t\tq.push(State(e.d, e.o));\n\t\t}\n\t}\n\t\n\treturn -1;\n}\n\nint main()\n{\n\tint S,T;\n\twhile(cin >> S >> T, (S||T))\n\t{\n\t\tint src,dst,st,dt;\n\t\tcin >> src; st=input();\n\t\tcin >> dst; dt=input();\n\t\t\n\t\tsrc--; dst--;\n\t\t\n\t\tvector<Edges> eg(1000),reg(1000);\n\t\tvector<Train> trains(100);\n\t\t\n\t\tfor(int i=0; i<T; i++)\n\t\t{\n\t\t\tint N;\n\t\t\tcin >> N;\n\t\t\tvector<int> p,t;\n\t\t\tfor(int j=0; j<N; j++)\n\t\t\t{\n\t\t\t\tint a,b;\n\t\t\t\tcin >> a; b=input();\n\t\t\t\ta--;\n\t\t\t\t\n\t\t\t\tp.push_back(a);\n\t\t\t\tt.push_back(b);\n\t\t\t}\n\t\t\ttrains[i]=make_pair(p,t);\n\t\t\t\n\t\t\tfor(int j=0; j<N-1; j++)\n\t\t\t{\n\t\t\t\tint s=p[j], d=p[j+1];\n\t\t\t\tint a=t[j], b=t[j+1];\n\t\t\t\teg[s].push_back(Edge(d,a,b));\n\t\t\t\treg[d].push_back(Edge(s,24*60-b, 24*60-a));\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor(int i=0; i<S; i++)\n\t\t{\n\t\t\tbeg[i]=9999;\n\t\t\tend[i]=-1;\n\t\t}\n\t\t\n\t\tdijk(src,st,dt,eg,1);\n\t dijk(dst,24*60-dt,24*60-st,reg,0);\n\t\t\n\t\tint ret=-1;\n\t\tif(src==dst&&st<=dt) ret=0;\n\t\t\n\t\tfor(int i=0; i<T; i++)\n\t\t{\n\t\t\tint lo=9999,hi=-1;\n\t\t\tfor(int j=0; j<trains[i].first.size(); j++)\n\t\t\t{\n\t\t\t\tint p=trains[i].first[j], t=trains[i].second[j];\n\t\t\t\tif(beg[p] <= t) lo=min(j,lo);\n\t\t\t\tif(end[p] >= t) hi=max(j,hi);\n\t\t\t}\n\t\t\t\n\t\t\tif(lo==9999||hi==-1) continue;\n\t\t\tret=max(ret, trains[i].second[hi]-trains[i].second[lo]);\n\t\t}\n\t\tif(ret==-1) cout << \"impossible\" << endl;\n\t\telse cout << ret << endl;\n\n\t}\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 5292, "score_of_the_acc": -0.0947, "final_rank": 3 }, { "submission_id": "aoj_2042_112020", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n\nconst int N = 1000;\nconst int inf =(1<<20);\nclass Edge{\npublic:\n int st,gt;\n int to;\n};\n\nclass Rail{\npublic:\n int p,t;\n};\n\nclass state{\npublic:\n int now;\n int time;\n bool operator<(const state & a)const{\n return time > a.time;\n }\n bool operator>(const state & a)const{\n return time < a.time;\n }\n};\n\n\nvoid go(int n,int s,int st,vector<Edge> *edge,bool isrev,int *cost){\n priority_queue<state> Q;\n priority_queue<state,vector<state>,greater<state> > RQ;\n if (!isrev)Q.push((state){s,st});\n else RQ.push((state){s,st});\n \n while(!Q.empty() || !RQ.empty()){\n state now;\n if (!isrev){\n now=Q.top(),Q.pop();\n }else{\n now = RQ.top(),RQ.pop();\n }\n if (cost[now.now] != inf)continue;\n //if (cost[now.now] == inf)\n cost[now.now] = now.time;\n /*\n else {\n if (isrev){\n\t\n\tif (now.time > cost[now.now])exit(1);\n\telse continue;\n }else {\n\t//\tcout << now.now+1 <<\" \" <<now.time <<\" \" << cost[now.now] << endl;\n\tif (now.time < cost[now.now])exit(1);\n\telse continue;\n }\n }\n */\n rep(i,edge[now.now].size()){\n if (isrev){\n\tif (now.time < edge[now.now][i].st)continue;\n\tRQ.push((state){edge[now.now][i].to,edge[now.now][i].gt});\n }else{\n\tif (now.time > edge[now.now][i].st)continue;\n\tQ.push((state){ edge[now.now][i].to,edge[now.now][i].gt});\n }\n }\n }\n}\n\nmain(){\n int n;\n int line;\n int scost[N],tcost[N];\n vector<Edge> edge[N];\n vector<Edge> rev[N];\n vector<Rail > in[100]; \n while(cin>>n>>line && n){\n rep(i,n)tcost[i]=inf,scost[i]=inf;\n rep(i,n)edge[i].clear(),rev[i].clear();\n rep(i,line)in[i].clear();\n int s,sh,sm,d,dh,dm;\n char dummy;\n cin>>s>>sh>>dummy>>sm>> d>>dh>>dummy>>dm;\n \n sh=sh*60+sm;\n dh=dh*60+dm;\n s--;\n d--;\n \n rep(i,line){\n int m;\n cin>>m;\n rep(j,m){\n\tint now,nh,nm;\n\tcin>>now>>nh>>dummy>>nm;\n\tnow--;\n\tnh = nh*60+nm;\n\tin[i].push_back((Rail){now,nh});\n }\n \n rep(j,in[i].size()-1){\n\tedge[in[i][j ].p].pb((Edge){in[i][j ].t,in[i][j+1].t,in[i][j+1].p});\n\trev[ in[i][j+1].p].pb((Edge){in[i][j+1].t,in[i][j ].t,in[i][j ].p});\n }\n }\n \n\n go(n,s,sh,edge,false,scost);\n go(n,d,dh,rev ,true ,tcost);\n\n /*\n cout << \"begint\"<<endl;\n cout << n << endl;\n cout <<\"go\"<<endl;\n rep(i,n)cout << scost[i] <<\" \";cout << endl;\n cout << \"rev \" << endl;\n rep(i,n)cout << tcost[i] <<\" \";cout << endl;\n cout << endl;\n */\n\n int ans = -1;\n if (scost[d] == inf){\n cout << \"impossible\"<<endl;\n continue;\n }\n if (s == d && sh <= dh)ans = 0;\n\n rep(i,line){\n int scan=-1,dcan=-1;\n rep(j,in[i].size()){\n\tif (scost[in[i][j].p] == inf)continue;\n\tif (scost[in[i][j].p] <= in[i][j].t){\n\t scan=j;\n\t break;\n\t}\n }\n \n for(int j=in[i].size()-1;j>=0 && j >scan;j--){\n\tif (tcost[in[i][j].p] == inf)continue;\n\tif (tcost[in[i][j].p] >= in[i][j].t){\n\t dcan=j;\n\t break;\n\t}\n }\n\n if (scan == -1 || dcan == -1)continue;\n\n ans = max(ans,in[i][dcan].t-in[i][scan].t);\n }\n \n if (ans == -1)cout << \"impossible\"<<endl;\n else cout << ans << endl;\n \n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 5844, "score_of_the_acc": -0.0897, "final_rank": 2 } ]
aoj_2046_cpp
Problem H: Robot's Crash Prof. Jenifer A. Gibson is carrying out experiments with many robots. Since those robots are expensive, she wants to avoid their crashes during her experiments at her all effort. So she asked you, her assistant, as follows. “Suppose that we have n (2 ≤ n ≤ 100000) robots of the circular shape with the radius of r , and that they are placed on the xy -plane without overlaps. Each robot starts to move straight with a velocity of either v or - v simultaneously, say, at the time of zero. The robots keep their moving infinitely unless I stop them. I’d like to know in advance if some robots will crash each other. The robots crash when their centers get closer than the distance of 2 r . I’d also like to know the time of the first crash, if any, so I can stop the robots before the crash. Well, could you please write a program for this purpose?” Input The input consists of multiple datasets. Each dataset has the following format: n vx vy r rx 1 ry 1 u 1 rx 2 ry 2 u 2 ... rx n ry n u n n is the number of robots. ( vx , vy ) denotes the vector of the velocity v . r is the radius of the robots. ( rx i , ry i ) denotes the coordinates of the center of the i -th robot. u i denotes the moving direction of the i -th robot, where 1 indicates the i -th robot moves with the velocity ( vx , vy ), and -1 indicates (- vx , - vy ). All the coordinates range from -1200 to 1200. Both vx and vy range from -1 to 1. You may assume all the following hold for any pair of robots: they must not be placed closer than the distance of (2 r + 10 -8 ) at the initial state; they must get closer than the distance of (2 r - 10 -8 ) if they crash each other at some point of time; and they must not get closer than the distance of (2 r + 10 -8 ) if they don’t crash. The input is terminated by a line that contains a zero. This should not be processed. Output For each dataset, print the first crash time in a line, or “SAFE” if no pair of robots crashes each other. Each crash time should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10 -4 . Sample Input 2 1.0 0.0 0.5 0.0 0.0 1 2.0 0.0 -1 2 1.0 0.0 0.5 0.0 0.0 -1 2.0 0.0 1 0 Output for the Sample Input 0.500000 SAFE
[ { "submission_id": "aoj_2046_8436730", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 1e-12\n\n\n\n\n#define SIZE 100005\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\n\n\n\nint N,DIR[SIZE];\ndouble baseR;\nPoint point[SIZE];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\nvoid func(){\n\n\tdouble vx,vy;\n\tscanf(\"%lf %lf %lf\",&vx,&vy,&baseR);\n\n\tdouble rad = calc_rad(Point(vx,vy));\n\n\tdouble V = sqrt(vx*vx + vy*vy); //■正が右向き\n\n\tdouble rx,ry;\n\tPoint center = Point(0,0);\n\n\tvector<pair<double,int>> vec;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %d\",&rx,&ry,&DIR[i]);\n\t\tpoint[i] = rotate(center,Point(rx,ry),-rad);\n\n\t\tvec.push_back(make_pair(point[i].y,i));\n\t}\n\n\tsort(vec.begin(),vec.end()); //y座標の昇順にする\n\n\tset<pair<double,int>> L,R;\n\n\tdouble ans = HUGE_NUM;\n\n\tint IND = 0;\n\n\tfor(int i = 0; i < vec.size(); i++){\n\n\t\tint ind = vec[i].second;\n\t\tdouble y = point[ind].y;\n\n\t\t//衝突の可能性がなくなったロボットを削除\n\t\twhile(IND < i && (y-vec[IND].first) > 2*baseR+EPS){\n\n\t\t\tint tmp_ind = vec[IND].second;\n\t\t\tif(DIR[tmp_ind] == -1){ //左向き\n\n\t\t\t\tauto at = L.lower_bound(make_pair(point[tmp_ind].x-EPS,-1));\n\n\t\t\t\twhile(at != L.end()){\n\n\t\t\t\t\tif(at->second == tmp_ind){\n\t\t\t\t\t\tL.erase(at);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tat++;\n\t\t\t\t}\n\n\t\t\t}else{ //右向き\n\n\t\t\t\tauto at = R.lower_bound(make_pair(point[tmp_ind].x-EPS,-1));\n\n\t\t\t\twhile(at != R.end()){\n\n\t\t\t\t\tif(at->second == tmp_ind){\n\t\t\t\t\t\tR.erase(at);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tat++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tIND++;\n\t\t}\n\n\t\t//■先に登録\n\t\tif(DIR[ind] == -1){\n\n\t\t\tL.insert(make_pair(point[ind].x,ind));\n\n\t\t}else{\n\n\t\t\tR.insert(make_pair(point[ind].x,ind));\n\t\t}\n\n\t\tPoint a,b;\n\t\tif(DIR[ind] == 1){\n\n\t\t\tauto at = L.lower_bound(make_pair(point[ind].x-EPS,-1));\n\t\t\tif(at == L.end()){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ta = point[ind];\n\t\t\tb = point[at->second];\n\n\t\t}else{\n\n\t\t\tauto at = R.lower_bound(make_pair(point[ind].x-EPS,-1));\n\t\t\tif(at == R.begin()){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tat--;\n\n\t\t\ta = point[at->second];\n\t\t\tb = point[ind];\n\t\t}\n\n\t\tdouble A = 4*V*V;\n\t\tdouble B = 4*(a.x-b.x)*V;\n\t\tdouble C = a.x*a.x - 2*a.x*b.x + b.x*b.x - (4*baseR*baseR-(a.y-b.y)*(a.y-b.y));\n\n\t\tdouble D = B*B-4*A*C;\n\t\tif(D+EPS < 0){\n\n\t\t\tcontinue; //解なし\n\t\t}\n\n\t\tdouble t1 = (-B+sqrt(D))/(2*A);\n\t\tif(t1 > EPS){\n\n\t\t\tchmin(ans,t1);\n\t\t}\n\n\t\tdouble t2 = (-B-sqrt(D))/(2*A);\n\t\tif(t2 > EPS){\n\n\t\t\tchmin(ans,t2);\n\t\t}\n\t}\n\n\tif(fabs(ans-HUGE_NUM) < EPS){\n\n\t\tprintf(\"SAFE\\n\");\n\n\t}else{\n\n\t\tprintf(\"%.10lf\\n\",ans);\n\t}\n}\n\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 13616, "score_of_the_acc": -0.8392, "final_rank": 3 }, { "submission_id": "aoj_2046_8436722", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 1e-12\n\n\n\n\n#define SIZE 100005\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\n\n\n\nint N,DIR[SIZE];\ndouble baseR;\nPoint point[SIZE];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n\nbool DEBUG = false;\n\nvoid func(){\n\n\tdouble vx,vy;\n\tscanf(\"%lf %lf %lf\",&vx,&vy,&baseR);\n\n\tdouble rad = calc_rad(Point(vx,vy));\n\n\tif(DEBUG){\n\n\t\tprintf(\"rad:%.3lf\",rad);\n\t}\n\n\tdouble V = sqrt(vx*vx + vy*vy); //■正が右向き\n\n\n\tdouble rx,ry;\n\tPoint center = Point(0,0);\n\n\tvector<pair<double,int>> vec;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %d\",&rx,&ry,&DIR[i]);\n\t\tpoint[i] = rotate(center,Point(rx,ry),-rad);\n\n\t\tif(DEBUG){\n\n\t\t\tprintf(\"点%d \",i);\n\t\t\tpoint[i].debug();\n\t\t}\n\n\t\tvec.push_back(make_pair(point[i].y,i));\n\t}\n\n\tsort(vec.begin(),vec.end()); //y座標の昇順にする\n\n\tset<pair<double,int>> L,R;\n\n\tdouble ans = HUGE_NUM;\n\n\tint IND = 0;\n\n\tfor(int i = 0; i < vec.size(); i++){\n\n\t\tint ind = vec[i].second;\n\t\tdouble y = point[ind].y;\n\n\t\t//衝突の可能性がなくなったロボットを削除\n\t\twhile(IND < i && (y-vec[IND].first) > 2*baseR+EPS){\n\n\t\t\tint tmp_ind = vec[IND].second;\n\t\t\tif(DIR[tmp_ind] == -1){ //左向き\n\n\t\t\t\tauto at = L.lower_bound(make_pair(point[tmp_ind].x-EPS,-1));\n\n\t\t\t\twhile(at != L.end()){\n\n\t\t\t\t\tif(at->second == tmp_ind){\n\t\t\t\t\t\tL.erase(at);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tat++;\n\t\t\t\t}\n\n\t\t\t}else{ //右向き\n\n\t\t\t\tauto at = R.lower_bound(make_pair(point[tmp_ind].x-EPS,-1));\n\n\t\t\t\twhile(at != R.end()){\n\n\t\t\t\t\tif(at->second == tmp_ind){\n\t\t\t\t\t\tR.erase(at);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tat++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tIND++;\n\t\t}\n\n\t\t//■先に登録\n\t\tif(DIR[ind] == -1){\n\n\t\t\tL.insert(make_pair(point[ind].x,ind));\n\n\t\t}else{\n\n\t\t\tR.insert(make_pair(point[ind].x,ind));\n\t\t}\n\n\t\tPoint a,b;\n\t\tif(DIR[ind] == 1){\n\n\t\t\tauto at = L.lower_bound(make_pair(point[ind].x-EPS,-1));\n\t\t\tif(at == L.end()){\n\t\t\t\tif(DEBUG){\n\n\t\t\t\t\tprintf(\"左向きなし\\n\");\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ta = point[ind];\n\t\t\tb = point[at->second];\n\n\t\t}else{\n\n\t\t\tauto at = R.lower_bound(make_pair(point[ind].x-EPS,-1));\n\t\t\tif(at == R.begin()){\n\t\t\t\tif(DEBUG){\n\n\t\t\t\t\tprintf(\"右向きなし\\n\");\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tat--;\n\n\t\t\ta = point[at->second];\n\t\t\tb = point[ind];\n\t\t}\n\n\t\tdouble A = 4*V*V;\n\t\tdouble B = 4*(a.x-b.x)*V;\n\t\tdouble C = a.x*a.x - 2*a.x*b.x + b.x*b.x - (4*baseR*baseR-(a.y-b.y)*(a.y-b.y));\n\n\t\tdouble D = B*B-4*A*C;\n\t\tif(D+EPS < 0){\n\t\t\tif(DEBUG){\n\n\t\t\t\tprintf(\"解なし\\n\");\n\t\t\t}\n\n\t\t\tcontinue; //解なし\n\t\t}\n\n\t\tdouble t1 = (-B+sqrt(D))/(2*A);\n\t\tif(t1 > EPS){\n\n\t\t\tchmin(ans,t1);\n\t\t}\n\n\t\tdouble t2 = (-B-sqrt(D))/(2*A);\n\t\tif(t2 > EPS){\n\n\t\t\tchmin(ans,t2);\n\t\t}\n\t}\n\n\tif(fabs(ans-HUGE_NUM) < EPS){\n\n\t\tprintf(\"SAFE\\n\");\n\n\t}else{\n\n\t\tprintf(\"%.10lf\\n\",ans);\n\t}\n}\n\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 13672, "score_of_the_acc": -0.8435, "final_rank": 4 }, { "submission_id": "aoj_2046_1363835", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\n\nconst double INF = 1e70;\n\n/* typedef */\n\ntemplate <typename T>\nstruct Pt {\n T x, y;\n\n Pt() {}\n Pt(T _x, T _y) : x(_x), y(_y) {}\n Pt(const Pt& pt) : x(pt.x), y(pt.y) {}\n\n bool operator==(const Pt pt) const { return x == pt.x && y == pt.y; }\n Pt<T> operator+(const Pt pt) const { return Pt<T>(x + pt.x, y + pt.y); }\n Pt<T> operator-() const { return Pt<T>(-x, -y); }\n Pt<T> operator-(const Pt pt) const { return Pt<T>(x - pt.x, y - pt.y); }\n Pt<T> operator*(T t) const { return Pt<T>(x * t, y * t); }\n Pt<T> operator/(T t) const { return Pt<T>(x / t, y / t); }\n T dot(Pt v) const { return x * v.x + y * v.y; }\n T cross(Pt v) const { return x * v.y - y * v.x; }\n Pt<T> mid(const Pt pt) { return Pt<T>((x + pt.x) / 2, (y + pt.y) / 2); }\n T d2() { return x * x + y * y; }\n double d() { return sqrt(d2()); }\n\n Pt<T> rot(double th) {\n double c = cos(th), s = sin(th);\n return Pt<T>(c * x - s * y, s * x + c * y);\n }\n\n bool operator<(const Pt& pt) const {\n return x < pt.x || (x == pt.x && y < pt.y);\n }\n\n void print(string format) {\n printf((\"(\" + format + \", \" + format + \")\\n\").c_str(), x, y);\n }\n void print() { print(\"%.6lf\"); }\n};\n\ntypedef Pt<double> pt;\n\nstruct Robot {\n pt p;\n int drc;\n Robot() {}\n Robot(pt _p, int _drc) : p(_p), drc(_drc) {}\n bool operator<(const Robot& r0) const { return p.x < r0.p.x; }\n};\n\ntypedef set<Robot> srbt;\n\n/* global variables */\n\nint n;\npt v;\ndouble r;\nRobot rbts[MAX_N];\n\n/* subroutines */\n\nbool lessy(const Robot& r0, const Robot& r1) { return r0.p.y < r1.p.y; }\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n cin >> v.x >> v.y >> r;\n\n double th = -atan2(v.y, v.x);\n pt v0 = v.rot(th);\n //v0.print(); printf(\"th=%.6lf\\n\", th);\n\n double rr = r * 2;\n double dvx = v0.x * 2;\n \n for (int i = 0; i < n; i++) {\n pt p;\n cin >> p.x >> p.y >> rbts[i].drc;\n rbts[i].p = p.rot(th);\n }\n sort(rbts, rbts + n, lessy);\n \n double min_t = INF;\n srbt srl, srr;\n \n for (int i0 = 0, i1 = 0; i1 < n; i1++) {\n Robot& r1 = rbts[i1];\n\n while (i0 < i1 && r1.p.y - rbts[i0].p.y > rr) {\n\tif (rbts[i0].drc > 0) srl.erase(rbts[i0]);\n\telse srr.erase(rbts[i0]);\n\ti0++;\n }\n \n if (r1.drc < 0) {\n\tif (! srl.empty()) {\n\t srbt::iterator rit = srl.lower_bound(r1);\n\t if (rit != srl.begin()) {\n\t rit--;\n\t pt dp = r1.p - rit->p;\n\t double t = (dp.x - sqrt(rr * rr - dp.y * dp.y)) / dvx;\n\t if (min_t > t) min_t = t;\n\t }\n\t}\n\n\tsrr.insert(r1);\n }\n else {\n\tif (! srr.empty()) {\n\t srbt::iterator rit = srr.lower_bound(r1);\n\t if (rit != srr.end()) {\n\t pt dp = rit->p - r1.p;\n\t double t = (dp.x - sqrt(rr * rr - dp.y * dp.y)) / dvx;\n\t if (min_t > t) min_t = t;\n\t }\n\t}\n\n\tsrl.insert(r1);\n }\n }\n\n if (min_t >= INF)\n cout << \"SAFE\" << endl;\n else \n printf(\"%.6lf\\n\", min_t);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 9948, "score_of_the_acc": -0.7291, "final_rank": 2 }, { "submission_id": "aoj_2046_815880", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\ntypedef complex<double> Point;\nnamespace std{\n bool operator < (const Point& p1, const Point& p2){\n if(p1.real() != p2.real()) return p1.real() < p2.real();\n return p1.imag() < p2.imag();\n }\n};\nstruct Event{\n double y;\n Point p;\n int p_type;\n int e_type;\n Event(double y, Point p, int p_type, int e_type) : \n y(y), p(p), p_type(p_type), e_type(e_type) {}\n bool operator < (const Event& e) const{\n if(y != e.y) return y < e.y;\n if(e_type != e.e_type) return e_type > e.e_type; // delete is first ???\n return p < e.p;\n }\n};\n\n\ndouble get_time(Point p, Point q, double R, double v){\n double dx = abs(p.real() - q.real());\n double dy = abs(p.imag() - q.imag());\n if(!(2 * R >= dy)) return 1e16;\n assert(2 * R >= dy);\n double b = sqrt(4 * R * R - dy * dy);\n assert(dx - b >= 0);\n return (dx - b) / v;\n}\n\nvoid update(double& ans, vector<Point>& pset, Point p, double R, double v, int t){\n int k = lower_bound(pset.begin(), pset.end(), p) - pset.begin();\n for(int dk = -1; dk <= 1; dk++) if(k + dk >= 0 && k + dk < pset.size()){\n if(t == 0 && p.real() < pset[k + dk].real()){\n ans = min(ans, get_time(p, pset[k + dk], R, v));\n }else if(t == 1 && p.real() > pset[k + dk].real()){\n ans = min(ans, get_time(p, pset[k + dk], R, v));\n }\n }\n}\n\nint main(){\n int N;\n while(cin >> N && N){\n double vx, vy;\n cin >> vx >> vy;\n vx *= 2; vy *= 2;\n double th = arg(Point(vx, vy));\n double v = sqrt(vx * vx + vy * vy);\n double R;\n cin >> R;\n\n vector<Event> events;\n\n REP(i, N){\n double x, y;\n int u;\n cin >> x >> y >> u;\n Point p(x, y);\n p = p * polar(1.0, -th);\n if(u == 1){\n events.push_back(Event(p.imag() - R, p, 0, 0));\n events.push_back(Event(p.imag() + R, p, 0, 1));\n }else{\n events.push_back(Event(p.imag() - R, p, 1, 0));\n events.push_back(Event(p.imag() + R, p, 1, 1));\n }\n }\n\n sort(events.begin(), events.end());\n\n vector<Point> point_set[2];\n double ans = 1e16;\n for(int i = 0; i < events.size(); i++){\n Event e = events[i];\n //cout << e.p << \" \" << e.e_type << \" \" << e.p_type << endl;\n vector<Point>& my_set = point_set[e.p_type];\n vector<Point>& you_set = point_set[e.p_type ^ 1];\n if(e.e_type == 0){\n my_set.insert(lower_bound(my_set.begin(), my_set.end(), e.p), e.p);\n update(ans, you_set, e.p, R, v, e.p_type);\n }else if(e.e_type == 1){\n my_set.erase(lower_bound(my_set.begin(), my_set.end(), e.p));\n int k = lower_bound(you_set.begin(), you_set.end(), e.p) - you_set.begin();\n for(int dk = -1; dk <= 1; dk ++) if(k + dk >= 0 && k + dk < you_set.size()){\n update(ans, my_set, you_set[k], R, v, e.p_type ^ 1);\n }\n }\n }\n if(ans >= 1e16){\n puts(\"SAFE\");\n }else{\n printf(\"%.6f\\n\", ans);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2760, "memory_kb": 15680, "score_of_the_acc": -2, "final_rank": 5 }, { "submission_id": "aoj_2046_406661", "code_snippet": "#include<set>\n#include<cmath>\n#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double INF=1e77;\nconst double PI=acos(-1);\n\ntemplate<class T>\nstruct point{ T x,y; };\n\ntemplate<class T>\ndouble abs(const point<T> &a){ return sqrt(a.x*a.x+a.y*a.y); }\n\ntemplate<class T>\ndouble arg(const point<T> &a,const point<T> &b){\n\tdouble ta=atan2(a.y,a.x);\n\tdouble tb=atan2(b.y,b.x);\n\treturn tb-ta<0 ? tb-ta+2*PI : tb-ta;\n}\n\npoint<double> rot(const point<double> &a,double theta){\n\treturn (point<double>){a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)};\n}\n\nstruct robot{\n\tpoint<double> p; // 位置\n\tint dir; // 移動方向 (+1 or -1)\n\tbool operator<(const robot &Z)const{ return p.x<Z.p.x; }\n};\n\n// 速さ v で動くロボット A, B が衝突するときの時刻\ndouble f(const robot &A,const robot &B,double r,double v){\n\tdouble w=abs(A.p.x-B.p.x);\n\tdouble h=sqrt(4*r*r-w*w);\n\treturn (abs(A.p.y-B.p.y)-h)/(2*v);\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tpoint<double> v;\n\t\tdouble r; scanf(\"%lf%lf%lf\",&v.x,&v.y,&r);\n\t\tdouble v_abs=abs(v);\n\n\t\tstatic robot Z[100000];\n\t\trep(i,n) scanf(\"%lf%lf%d\",&Z[i].p.x,&Z[i].p.y,&Z[i].dir);\n\n\t\t// y 軸の +/- 方向に移動するように座標を取り替える\n\t\tdouble theta=-arg((point<double>){0,1},v);\n\t\tv=rot(v,theta);\n\t\trep(i,n) Z[i].p=rot(Z[i].p,theta);\n\n\t\tsort(Z,Z+n);\n\n\t\tdouble ans=INF;\n\t\t// S_u : 上方向に動くロボットの集合\n\t\t// S_d : 下方向に動くロボットの集合\n\t\t// double をそのまま比較しているけど、誤差が出るような演算はしていないので大丈夫\n\t\tset< pair<double,int> > S_u,S_d;\n\t\tfor(int i=0,j=0;i<n;i++){\n\t\t\t// x 方向に離れすぎてロボット i とぶつかれなくなったロボットを削除\n\t\t\twhile(j<i && Z[i].p.x-Z[j].p.x>2*r){\n\t\t\t\tif(Z[j].dir==1) S_u.erase(make_pair(Z[j].p.y,j));\n\t\t\t\telse S_d.erase(make_pair(Z[j].p.y,j));\n\t\t\t\tj++;\n\t\t\t}\n\n\t\t\tset< pair<double,int> >::iterator it;\n\t\t\t// ロボット i は上方向に動く\n\t\t\tif(Z[i].dir==1){\n\t\t\t\tif(!S_d.empty()){\n\t\t\t\t\tit=S_d.lower_bound(make_pair(Z[i].p.y,-1));\n\t\t\t\t\tif(it!=S_d.end()){\n\t\t\t\t\t\tint k=it->second;\n\t\t\t\t\t\tans=min(ans,f(Z[i],Z[k],r,v_abs));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// ロボット i は下方向に動く\n\t\t\telse{\n\t\t\t\tif(!S_u.empty()){\n\t\t\t\t\tit=S_u.lower_bound(make_pair(Z[i].p.y,-1));\n\t\t\t\t\tif(it!=S_u.begin()){\n\t\t\t\t\t\t--it;\n\t\t\t\t\t\tint k=it->second;\n\t\t\t\t\t\tans=min(ans,f(Z[i],Z[k],r,v_abs));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(Z[i].dir==1) S_u.insert(make_pair(Z[i].p.y,i));\n\t\t\telse S_d.insert(make_pair(Z[i].p.y,i));\n\t\t}\n\n\t\tif(ans>INF/2) puts(\"SAFE\");\n\t\telse printf(\"%.9f\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 2848, "score_of_the_acc": -0.1367, "final_rank": 1 } ]
aoj_2040_cpp
Problem B: Sort the Panels There was an explorer Henry Nelson traveling all over the world. One day he reached an ancient building. He decided to enter this building for his interest, but its entrance seemed to be locked by a strange security system. There were some black and white panels placed on a line at equal intervals in front of the entrance, and a strange machine with a cryptogram attached. After a while, he managed to read this cryptogram: this entrance would be unlocked when the panels were rearranged in a certain order, and he needed to use this special machine to change the order of panels. All he could do with this machine was to swap arbitrary pairs of panels. Each swap could be performed by the following steps: move the machine to one panel and mark it; move the machine to another panel and again mark it; then turn on a special switch of the machine to have the two marked panels swapped. It was impossible to have more than two panels marked simultaneously. The marks would be erased every time the panels are swapped. He had to change the order of panels by a number of swaps to enter the building. Unfortunately, however, the machine was so heavy that he didn’t want to move it more than needed. Then which steps could be the best? Your task is to write a program that finds the minimum cost needed to rearrange the panels, where moving the machine between adjacent panels is defined to require the cost of one. You can arbitrarily choose the initial position of the machine, and don’t have to count the cost for moving the machine to that position. Input The input consists of multiple datasets. Each dataset consists of three lines. The first line contains an integer N , which indicates the number of panels (2 ≤ N ≤ 16). The second and third lines contain N characters each, and describe the initial and final orders of the panels respectively. Each character in these descriptions is either ‘B’ (for black) or ‘W’ (for white) and denotes the color of the panel. The panels of the same color should not be distinguished. The input is terminated by a line with a single zero. Output For each dataset, output the minimum cost on a line. You can assume that there is at least one way to change the order of the panels from the initial one to the final one. Sample Input 4 WBWB BWBW 8 WWWWBWBB WWBBWBWW 0 Output for the Sample Input 3 9
[ { "submission_id": "aoj_2040_10853102", "code_snippet": "#include <cstring>\n#include <iostream>\n#include <string>\n\nusing namespace std;\n\nint getStat(string arg) {\n\tint ret = 0;\n\tfor(char ch: arg)\n\t\tif(ch == 'B') ret = ret * 2 + 1;\n\t\telse ret *= 2;\n\treturn ret;\n}\n\nint dp[1 << 16][16];\nint n, goal;\n\nint getAns(int stat, int machine) {\n\tint &ret = dp[stat][machine];\n\tif(ret != -1) return ret;\n\n\tret = 0;\n\tif(stat != goal) {\n\t\tret = (1 << 29);\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tfor(int j = 0; j < n; j++) {\n\t\t\t\tif((stat & (1 << i)) > 0 != (stat & (1 << j)) > 0) {\n\t\t\t\t\tif(\t(stat & (1 << i)) != (goal & (1 << i)) and\n\t\t\t\t\t\t(stat & (1 << j)) != (goal & (1 << j))) {\n\t\t\t\t\t\tret = min(ret, getAns(stat ^ (1 << i) ^ (1 << j), j) + abs(machine - i) + abs(i - j));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nint main(void) {\n\twhile(cin >> n, n > 0) {\n\t\tstring start, end;\n\t\tcin >> start >> end;\n\t\tgoal = getStat(end);\n\t\tmemset(dp, -1, sizeof(dp));\n\t\tint ans = (1 << 30);\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tans = min(ans, getAns(getStat(start), i));\n\t\tcout << ans << \"\\n\";\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 7464, "score_of_the_acc": -0.0398, "final_rank": 10 }, { "submission_id": "aoj_2040_9595976", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f000000;\nbool cmp(int x, int y) {\n return __builtin_popcount(x) < __builtin_popcount(y);\n}\nint main() {\n int n;\n while (true) {\n scanf(\"%d\", &n);\n if (!n) {\n fflush(stdout);\n exit(0);\n }\n char s[18], t[18];\n scanf(\"%s\", s);\n scanf(\"%s\", t);\n vector<vector<int> > dp(1 << n, vector<int>(n, inf));\n int mask = 0;\n for (int i = 0; i < n; ++i) {\n mask |= (s[i] == t[i]) << i;\n }\n fill(dp[mask].begin(), dp[mask].end(), 0);\n vector<int> masks;\n for (int i = 0; i < (1 << n); ++i) {\n masks.push_back(i);\n }\n sort(masks.begin(), masks.end(), cmp);\n for (int z = 0; z < (1 << n); ++ z) { int cur = masks[z];\n for (int pos = 0; pos < n; ++pos) {\n if (dp[cur][pos] == inf) continue;\n for (int i = 0; i < n; ++i) {\n if (cur >> i & 1) continue;\n for (int j = 0; j < n; ++j) {\n if ((cur >> j & 1) || s[i] == s[j]) continue;\n int after = cur | (1 << i) | (1 << j);\n int dist = abs(pos - j) + abs(j - i);\n dp[after][i] = min(dp[after][i], dp[cur][pos] + dist);\n }\n }\n }\n }\n printf(\"%d\\n\", *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end()));\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 9892, "score_of_the_acc": -0.2274, "final_rank": 11 }, { "submission_id": "aoj_2040_9595961", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f000000;\nbool cmp(int x, int y) {\n return __builtin_popcount(x) < __builtin_popcount(y);\n}\nint main() {\n int n;\n while (cin >> n) {\n if (!n) {\n fflush(stdout);\n return 0;\n }\n string s, t;\n cin >> s >> t;\n vector<vector<int> > dp(1 << n, vector<int>(n, inf));\n int mask = 0;\n for (int i = 0; i < n; ++i) {\n mask |= (s[i] == t[i]) << i;\n }\n fill(dp[mask].begin(), dp[mask].end(), 0);\n vector<int> masks;\n for (int i = 0; i < (1 << n); ++i) {\n masks.push_back(i);\n }\n sort(masks.begin(), masks.end(), cmp);\n for (int cur : masks) {\n for (int pos = 0; pos < n; ++pos) {\n if (dp[cur][pos] == inf) continue;\n for (int i = 0; i < n; ++i) {\n if (cur >> i & 1) continue;\n for (int j = 0; j < n; ++j) {\n if ((cur >> j & 1) || s[i] == s[j]) continue;\n int after = cur | (1 << i) | (1 << j);\n int dist = abs(pos - j) + abs(j - i);\n dp[after][i] = min(dp[after][i], dp[cur][pos] + dist);\n }\n }\n }\n }\n printf(\"%d\\n\", *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end()));\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 10596, "score_of_the_acc": -0.2645, "final_rank": 14 }, { "submission_id": "aoj_2040_9595960", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f000000;\nbool cmp(int x, int y) {\n return __builtin_popcount(x) < __builtin_popcount(y);\n}\nint main() {\n int n;\n while (cin >> n) {\n if (!n) return 0;\n string s, t;\n cin >> s >> t;\n vector<vector<int> > dp(1 << n, vector<int>(n, inf));\n int mask = 0;\n for (int i = 0; i < n; ++i) {\n mask |= (s[i] == t[i]) << i;\n }\n fill(dp[mask].begin(), dp[mask].end(), 0);\n vector<int> masks;\n for (int i = 0; i < (1 << n); ++i) {\n masks.push_back(i);\n }\n sort(masks.begin(), masks.end(), cmp);\n for (int cur : masks) {\n for (int pos = 0; pos < n; ++pos) {\n if (dp[cur][pos] == inf) continue;\n for (int i = 0; i < n; ++i) {\n if (cur >> i & 1) continue;\n for (int j = 0; j < n; ++j) {\n if ((cur >> j & 1) || s[i] == s[j]) continue;\n int after = cur | (1 << i) | (1 << j);\n int dist = abs(pos - j) + abs(j - i);\n dp[after][i] = min(dp[after][i], dp[cur][pos] + dist);\n }\n }\n }\n }\n printf(\"%d\\n\", *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end()));\n }\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 10356, "score_of_the_acc": -0.2504, "final_rank": 12 }, { "submission_id": "aoj_2040_9595959", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f000000;\nbool cmp(int x, int y) {\n return __builtin_popcount(x) < __builtin_popcount(y);\n}\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n int n;\n while (cin >> n) {\n if (!n) return 0;\n string s, t;\n cin >> s >> t;\n vector<vector<int> > dp(1 << n, vector<int>(n, inf));\n int mask = 0;\n for (int i = 0; i < n; ++i) {\n mask |= (s[i] == t[i]) << i;\n }\n fill(dp[mask].begin(), dp[mask].end(), 0);\n vector<int> masks;\n for (int i = 0; i < (1 << n); ++i) {\n masks.push_back(i);\n }\n sort(masks.begin(), masks.end(), cmp);\n for (int cur : masks) {\n for (int pos = 0; pos < n; ++pos) {\n if (dp[cur][pos] == inf) continue;\n for (int i = 0; i < n; ++i) {\n if (cur >> i & 1) continue;\n for (int j = 0; j < n; ++j) {\n if ((cur >> j & 1) || s[i] == s[j]) continue;\n int after = cur | (1 << i) | (1 << j);\n int dist = abs(pos - j) + abs(j - i);\n dp[after][i] = min(dp[after][i], dp[cur][pos] + dist);\n }\n }\n }\n }\n cout << *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end()) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 10484, "score_of_the_acc": -0.2594, "final_rank": 13 }, { "submission_id": "aoj_2040_9595956", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f000000;\nbool cmp(int x, int y) {\n return __builtin_popcount(x) < __builtin_popcount(y);\n}\nint dp[1 << 16][16];\nint main() {\n int n;\n while (true) {\n cin >> n;\n if (!n) return 0;\n string s, t;\n cin >> s >> t;\n int mask = 0, c = 0;\n for (int i = 0; i < n; ++i) {\n mask |= (s[i] == t[i]) << i;\n }\n c = __builtin_popcount(mask) % 2;\n for (int i = 0; i < (1 << n); ++i) {\n for (int j = 0; j < n; ++j) {\n dp[i][j] = inf;\n }\n }\n for (int i = 0; i < n; ++i) dp[mask][i] = 0;\n vector<int> masks;\n for (int i = 0; i < (1 << n); ++i) {\n if (__builtin_popcount(i) % 2 == c) masks.push_back(i);\n }\n sort(masks.begin(), masks.end(), cmp);\n for (int cur : masks) {\n for (int pos = 0; pos < n; ++pos) {\n if (dp[cur][pos] == inf) continue;\n for (int i = 0; i < n; ++i) {\n if (cur >> i & 1) continue;\n for (int j = 0; j < n; ++j) {\n if ((cur >> j & 1) || s[i] == s[j]) continue;\n int after = cur | (1 << i) | (1 << j);\n int dist = abs(pos - j) + abs(j - i);\n dp[after][i] = min(dp[after][i], dp[cur][pos] + dist);\n }\n }\n }\n }\n int ans = inf;\n for (int i = 0; i < n; ++i) ans = min(ans, dp[(1 << n) - 1][i]);\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 7552, "score_of_the_acc": -0.0392, "final_rank": 9 }, { "submission_id": "aoj_2040_2743729", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n \nusing namespace std;\n \n#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n \ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<\" \"; cout<<endl; }\n \ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint N;\nstring s, g;\nint dp[1<<16][16];\nint dfs(int S, int u){\n if(S == (1<<N) - 1) return 0;\n if(dp[S][u] != -1) return dp[S][u];\n int res = INF;\n REP(i, N)REP(j, N) if(i != j && (S >> i & 1) == 0 && (S >> j & 1) == 0 && g[i] != g[j]){\n res = min(res, abs(u - i) + abs(i - j) + dfs(S | 1 << i | 1 << j, j));\n }\n return dp[S][u] = res;\n}\nint main(){\n while(cin>>N && N){\n cin>>s>>g;\n REP(S, 1<<N) REP(p, N) dp[S][p] = -1;\n int S = 0;\n REP(i, N) if(s[i] == g[i]) S |= 1<<i;\n int ans = INF;\n REP(i, N) ans = min(ans, dfs(S, i));\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 7176, "score_of_the_acc": -0.0223, "final_rank": 5 }, { "submission_id": "aoj_2040_2666169", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nstruct aa {\n\tbitset<16>bs;\n\tint cost;\n\tint now;\n};\nclass Compare {\npublic:\n\tbool operator()(const aa&l, const aa&r) {\n\t\treturn l.cost>r.cost;\n\t}\n};\n\nint main() {\n\twhile (true) {\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\t\tstring a,b;cin>>a>>b;\n\t\tbitset<16>start,goal;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tif(a[i]=='B')start[i]=true;\n\t\t\tif(b[i]=='B')goal[i]=true;\n\t\t}\n\t\tvector<vector<int>>memo((1<<N),vector<int>(N,1e9));\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tmemo[start.to_ulong()][i]=0;\n\t\t}\n\n\t\tpriority_queue<aa,vector<aa>,Compare>que;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tque.push(aa{ start,0,i });\n\t\t}\n\n\t\tint ans=1e9;\n\t\twhile (!que.empty()) {\n\t\t\tauto atop(que.top());\n\t\t\tque.pop();\n\n\t\t\tbitset<16>bs(atop.bs);\n\t\t\tconst int cost(atop.cost);\n\t\t\tconst int now(atop.now);\n\t\t\tif (bs == goal) {\n\t\t\t\tans=min(ans,cost);\n\t\t\t}\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tint u=bs[now];\n\t\t\t\tbs[now]=bs[i];\n\t\t\t\tbs[i]=u;\n\t\t\t\tconst int next_cost=cost+(abs(now-i));\n\t\t\t\tif (memo[bs.to_ulong()][i] > next_cost) {\n\t\t\t\t\tmemo[bs.to_ulong()][i]=next_cost;\n\t\t\t\t\tque.push(aa{ bs,next_cost,i });\n\t\t\t\t}\n\t\t\t\tu = bs[now];\n\t\t\t\tbs[now] = bs[i];\n\t\t\t\tbs[i] = u;\n\t\t\t}\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tconst int next_cost = cost + (abs(now - i));\n\t\t\t\tif (memo[bs.to_ulong()][i] > next_cost) {\n\t\t\t\t\tmemo[bs.to_ulong()][i] = next_cost;\n\t\t\t\t\tque.push(aa{ bs,next_cost,i });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1080, "memory_kb": 15780, "score_of_the_acc": -0.7448, "final_rank": 15 }, { "submission_id": "aoj_2040_2602113", "code_snippet": "#include <functional>\n#include <iostream>\n#include <limits>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n while (true) {\n int n;\n cin >> n;\n if (n == 0) {\n return 0;\n }\n\n string init;\n string final;\n cin >> init;\n cin >> final;\n\n auto encode = [](const string& s) {\n int r = 0b0;\n for (int i = 0; i < s.size(); ++i) {\n if (i != 0) {\n r <<= 1;\n }\n if (s[i] == 'B') {\n r |= 0b1;\n }\n }\n return r;\n };\n\n auto target = encode(final);\n auto start = encode(init);\n\n vector<vector<int>> memo(n, vector<int>(1 << n, -1));\n // p ??????????????´?????¶?????? s ?????¨??????????°???????????????????\n function<int(int, int)> f = [&f, &memo, n, start](int p, int s) -> int {\n if (memo[p][s] != -1) {\n return memo[p][s];\n }\n\n // ??????????????´????????????\n if (s == 0) {\n return 0;\n }\n\n int min_ = numeric_limits<int>::max();\n for (int i = 0; i < n; ++i) {\n // i ??????????????§????????´????????????\n if (((s >> i) & 1) == 0) {\n continue;\n }\n for (int j = 0; j < n; ++j) {\n if (i == j) {\n continue;\n }\n // j ??????????????§????????´????????????\n else if ((s >> j & 1) == 0) {\n continue;\n }\n // ?????????????????\\????????????????°???????????????????\n else if ((((start >> i) & 1) ^ ((start >> j) & 1)) == 0) {\n continue;\n }\n else {\n // p ?????? i ?????§?????£???, i ?????? j ?????§??????\n auto cost = abs(i - p) + abs(j - i);\n min_ = min(min_, cost + f(j, s ^ (1 << i) ^ (1 << j)));\n }\n }\n }\n memo[p][s] = min_;\n\n return min_;\n };\n\n int min_ = numeric_limits<int>::max();\n // ????????????????????????????????????\n for (int i = 0; i < n; ++i) {\n min_ = min(min_, f(i, start ^ target));\n }\n cout << min_ << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 7332, "score_of_the_acc": -0.0279, "final_rank": 7 }, { "submission_id": "aoj_2040_2602110", "code_snippet": "#include <bitset>\n#include <functional>\n#include <iostream>\n#include <limits>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n while (true) {\n int n;\n cin >> n;\n if (n == 0) {\n return 0;\n }\n\n string init;\n string final;\n cin >> init;\n cin >> final;\n\n auto encode = [](const string& s) {\n int r = 0b0;\n for (int i = 0; i < s.size(); ++i) {\n if (i != 0) {\n r <<= 1;\n }\n if (s[i] == 'B') {\n r |= 0b1;\n }\n }\n return r;\n };\n\n auto pow = [](int b, int e) {\n int r = 1;\n for (int i = 0; i < e; ++i) {\n r *= b;\n }\n return r;\n };\n\n auto target = encode(final);\n auto start = encode(init);\n\n vector<vector<int>> memo(n, vector<int>(pow(2, n), -1));\n // p ??????????????´?????¶?????? s ?????¨??????????°???????????????????\n function<int(int, int)> f = [&f, &memo, n, &pow, start](int p,\n int s) -> int {\n if (memo[p][s] != -1) {\n return memo[p][s];\n }\n\n // ??????????????´????????????\n if (s == 0) {\n return 0;\n }\n\n int min_ = numeric_limits<int>::max();\n int pp = -1;\n for (int i = 0; i < n; ++i) {\n // i ??????????????§????????´????????????\n if (((s >> i) & 1) == 0) {\n continue;\n }\n for (int j = 0; j < n; ++j) {\n if (i == j) {\n continue;\n }\n // j ??????????????§????????´????????????\n else if ((s >> j & 1) == 0) {\n continue;\n }\n // ?????????????????\\????????????????°???????????????????\n else if ((((start >> i) & 1) ^ ((start >> j) & 1)) == 0) {\n continue;\n }\n else {\n // p ?????? i ?????§?????£???, i ?????? j ?????§??????\n auto cost = abs(i - p) + abs(j - i);\n auto ss = s;\n ss ^= 1 << i;\n ss ^= 1 << j;\n auto min__ = min_;\n min_ = min(min_, cost + f(j, ss));\n if (min__ != min_) {\n pp = p;\n }\n }\n }\n }\n memo[pp][s] = min_;\n\n return min_;\n };\n\n int min_ = numeric_limits<int>::max();\n for (int i = 0; i < n; ++i) {\n min_ = min(min_, f(i, start ^ target));\n }\n cout << min_ << endl;\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 7280, "score_of_the_acc": -0.0256, "final_rank": 6 }, { "submission_id": "aoj_2040_2602109", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <fstream>\n#include <functional>\n#include <iostream>\n#include <limits>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n //ifstream cin(\"/Volumes/env1/work/aoj/2040/a\");\n while (true) {\n int n;\n cin >> n;\n if (n == 0) {\n return 0;\n }\n\n string init;\n string final;\n cin >> init;\n cin >> final;\n\n auto encode = [](const string& s) {\n int r = 0b0;\n for (int i = 0; i < s.size(); ++i) {\n if (i != 0) {\n r <<= 1;\n }\n if (s[i] == 'B') {\n r |= 0b1;\n }\n }\n return r;\n };\n\n auto pow = [](int b, int e) {\n int r = 1;\n for (int i = 0; i < e; ++i) {\n r *= b;\n }\n return r;\n };\n\n auto target = encode(final);\n auto start = encode(init);\n\n vector<vector<int>> memo(n, vector<int>(pow(2, n), -1));\n // p ??????????????´?????¶?????? s ?????¨??????????°???????????????????\n function<int(int, int)> f = [&f, &memo, n, &pow, start](int p,\n int s) -> int {\n //cout << static_cast<bitset<8>>(s) << endl;\n if (memo[p][s] != -1) {\n //cout << \"memo1\" << p << ' ' << s << endl;\n return memo[p][s];\n }\n\n // ??????????????´????????????\n if (s == 0) {\n return 0;\n }\n\n int min_ = numeric_limits<int>::max();\n int pp = -1;\n for (int i = 0; i < n; ++i) {\n // i ??????????????§????????´????????????\n if (((s >> i) & 1) == 0) {\n continue;\n }\n for (int j = 0; j < n; ++j) {\n if (i == j) {\n continue;\n }\n // j ??????????????§????????´????????????\n else if ((s >> j & 1) == 0) {\n continue;\n }\n // ?????????????????\\????????????????°???????????????????\n else if ((((start >> i) & 1) ^ ((start >> j) & 1)) == 0) {\n continue;\n }\n else {\n // p ?????? i ?????§?????£???, i ?????? j ?????§??????\n auto cost = abs(i - p) + abs(j - i);\n auto ss = s;\n ss ^= 1 << i;\n ss ^= 1 << j;\n //cout << \"pos: \" << p << \" swap \" << i << \", \" << j\n // << \": \" << static_cast<bitset<8>>(s) << \" to \"\n // << static_cast<bitset<8>>(ss) << \" cost \" << cost\n // << endl;\n auto min__ = min_;\n min_ = min(min_, cost + f(j, ss));\n if (min__ != min_) {\n pp = p;\n }\n }\n }\n }\n //cout << \"write memo \" << pp << \" \" << static_cast<bitset<8>>(s) << endl;\n memo[pp][s] = min_;\n assert(min_ != -1);\n\n return min_;\n };\n\n int min_ = numeric_limits<int>::max();\n for (int i = 0; i < n; ++i) {\n min_ = min(min_, f(i, start ^ target));\n }\n cout << min_ << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 7348, "score_of_the_acc": -0.029, "final_rank": 8 }, { "submission_id": "aoj_2040_2602059", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <bitset>\n#include <string>\n#include <map>\n#include <queue>\n#include <functional>\n#include <assert.h>\nusing namespace std;\ninline int pack(int p, int ps, int n) {\n return (p << n) | ps;\n}\nint main(int argc, char *argv[])\n{\n for(;;) {\n int n;\n cin >> n;\n if(n==0) break;\n string sa, sb;\n getline(cin, sa);\n getline(cin, sa);\n getline(cin, sb);\n auto conv = [n](string &s) {\n int r = 0;\n for(int i = 0; i < n; i++) {\n r = (r<<1) + (s[i] == 'W' ? 1 : 0); \n }\n return r;\n };\n const int a = conv(sa);\n const int b = conv(sb);\n //cout << \"a = \" << static_cast<std::bitset<8> >(a) << endl;\n //cout << \"b = \" << static_cast<std::bitset<8> >(b) << endl;\n // better DP: state = (head-pos, difference)\n vector<int> ds(n<<n, -1);\n function<int(int)> rec = [&rec,&b,&n,&ds](const int v){\n if(ds[v] >= 0) return ds[v];\n const int p = v >> n;\n const int s = v & ((1<<n) - 1);\n if(s == 0) return 0;\n int e = 10000000;\n for(int i = 0; i < n; i++) {\n if((s & (1<<i)) == 0) continue;\n for(int j = 0; j < n; j++) {\n if(i == j) continue;\n if((s & (1<<j)) == 0) continue;\n if((((b>>i) ^ (b>>j))&1) == 0) continue;\n const int c = abs(i - p) + abs(j - i);\n const int u = pack(j, s ^ (1<<i) ^ (1<<j), n); // swap\n e = min(e, c + rec(u));\n }\n }\n ds[v] = e;\n return e;\n };\n int e = 10000000;\n for(int i = 0; i < n; i++) {\n e = min(e, rec(pack(i, a^b, n)));\n }\n cout << e << endl;\n/* // naive Dijkstra search: state = (head-pos, panels)\n vector<int> ds(n<<n, 100000000);\n priority_queue<pair<int,int>> que;\n for(int i = 0; i < n; i++) {\n int v = pack(i, a, n);\n ds[v] = 0;\n que.push(make_pair(-ds[v], v));\n \n }\n vector<bool> done(n, false);\n int rest = n;\n while(que.size() > 0) {\n auto dv = que.top(); que.pop();\n const int v = dv.second;\n const int d = -dv.first;\n const int p = v >> n;\n const int s = v & ((1<<n) - 1);\n //cout << \"popped: \" << static_cast<std::bitset<8> >(s) << \" at \" << p << \" with cost \" << d << endl;\n if(s == b) {\n if(!done[p]) {\n done[p] = true;\n rest--;\n }\n if(rest == 0) {\n int e = d;\n for(int i = 0; i < n; i++) {\n e = min(e, ds[pack(i, b, n)]);\n }\n cout << e << endl;\n break;\n }\n }\n // generate all possible transitions: marking i, and then j.\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(i == j) continue;\n if((((s>>i) ^ (s>>j))&1) == 0) continue;\n const int c = abs(i - p) + abs(j - i);\n const int u = pack(j, s ^ (1<<i) ^ (1<<j), n); // swap\n if(ds[u] > d + c) {\n ds[u] = d + c;\n que.push(make_pair(-ds[u], u));\n //cout << \" to \" << i << \" and \" << j << \" cost \" << ds[u] << \" for \" << static_cast<std::bitset<8> >(u) << endl;\n }\n }\n }\n }\n*/\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7088, "score_of_the_acc": -0.0094, "final_rank": 2 }, { "submission_id": "aoj_2040_2601978", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <bitset>\n#include <string>\n#include <map>\n#include <queue>\n#include <functional>\n#include <assert.h>\nusing namespace std;\ninline int pack(int p, int ps, int n) {\n return (p << n) | ps;\n}\nint main(int argc, char *argv[])\n{\n for(;;) {\n int n;\n cin >> n;\n if(n==0) break;\n string sa, sb;\n getline(cin, sa);\n getline(cin, sa);\n getline(cin, sb);\n auto conv = [n](string &s) {\n int r = 0;\n for(int i = 0; i < n; i++) {\n r = (r<<1) + (s[i] == 'W' ? 1 : 0); \n }\n return r;\n };\n int a = conv(sa);\n int b = conv(sb);\n //cout << \"a = \" << static_cast<std::bitset<8> >(a) << endl;\n //cout << \"b = \" << static_cast<std::bitset<8> >(b) << endl;\n\n // state = (pos, panels)\n vector<int> ds(n<<n, 100000000);\n priority_queue<pair<int,int>> que;\n for(int i = 0; i < n; i++) {\n int v = pack(i, a, n);\n ds[v] = 0;\n que.push(make_pair(-ds[v], v));\n \n }\n vector<bool> done(n, false);\n int rest = n;\n while(que.size() > 0) {\n auto dv = que.top(); que.pop();\n const int v = dv.second;\n const int d = -dv.first;\n const int p = v >> n;\n const int s = v & ((1<<n) - 1);\n //cout << \"popped: \" << static_cast<std::bitset<8> >(s) << \" at \" << p << \" with cost \" << d << endl;\n if(s == b) {\n if(!done[p]) {\n done[p] = true;\n rest--;\n }\n if(rest == 0) {\n int e = d;\n for(int i = 0; i < n; i++) {\n e = min(e, ds[pack(i, b, n)]);\n }\n cout << e << endl;\n break;\n }\n }\n // generate all possible transitions: marking i, and then j.\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(i == j) continue;\n if((((s>>i) ^ (s>>j))&1) == 0) continue;\n const int c = abs(i - p) + abs(j - i);\n const int u = pack(j, s ^ (1<<i) ^ (1<<j), n); // swap\n if(ds[u] > d + c) {\n ds[u] = d + c;\n que.push(make_pair(-ds[u], u));\n //cout << \" to \" << i << \" and \" << j << \" cost \" << ds[u] << \" for \" << static_cast<std::bitset<8> >(u) << endl;\n }\n }\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5380, "memory_kb": 13796, "score_of_the_acc": -1.1854, "final_rank": 18 }, { "submission_id": "aoj_2040_2588409", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_state,int arg_sum_cost,int arg_loc){\n\t\tstate = arg_state;\n\t\tsum_cost = arg_sum_cost;\n\t\tloc = arg_loc;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\t\treturn sum_cost > arg.sum_cost;\n\t}\n\tint state,sum_cost,loc;\n};\n\n\nint N;\nint POW[17],dp[65536][16],start_state,goal_state,ans_num[16];\nchar buf_start[17],buf_goal[17];\n\nvoid func(){\n\n\tscanf(\"%s\",buf_start);\n\n\tstart_state = 0;\n\n\tfor(int i = 0; buf_start[i] != '\\0'; i++){\n\t\tif(buf_start[i] == 'B'){\n\t\t\tstart_state += POW[i];\n\t\t}\n\t}\n\n\tscanf(\"%s\",buf_goal);\n\n\tgoal_state = 0;\n\n\tfor(int i = 0; buf_goal[i] != '\\0'; i++){\n\t\tif(buf_goal[i] == 'B'){\n\t\t\tgoal_state += POW[i];\n\t\t\tans_num[i] = 1;\n\t\t}else{\n\t\t\tans_num[i] = 0;\n\t\t}\n\t}\n\n\tif(start_state == goal_state){\n\t\tprintf(\"0\\n\");\n\t\treturn;\n\t}\n\n\tfor(int state = 0; state < POW[N]; state++){\n\t\tfor(int loc = 0; loc < N; loc++)dp[state][loc] = BIG_NUM;\n\t}\n\n\tpriority_queue<Info> Q;\n\tfor(int loc = 0; loc < N; loc++){\n\t\tdp[start_state][loc] = 0;\n\t\tQ.push(Info(start_state,0,loc));\n\t}\n\n\tint ans = BIG_NUM;\n\tint next_state,num_1,num_2,tmp_cost;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().state == goal_state){\n\t\t\tans = min(ans,Q.top().sum_cost);\n\t\t\tQ.pop();\n\t\t}else if(Q.top().sum_cost > dp[Q.top().state][Q.top().loc]){\n\t\t\tQ.pop();\n\t\t}else{\n\n\t\t\tfor(int mark_1 = 0; mark_1 < N; mark_1++){\n\t\t\t\tfor(int mark_2 = 0; mark_2 < N; mark_2++){\n\t\t\t\t\tif(mark_1 == mark_2)continue;\n\n\t\t\t\t\tif(Q.top().state & (1 << mark_1)){\n\t\t\t\t\t\tnum_1 = 1;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tnum_1 = 0;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(Q.top().state & (1 << mark_2)){\n\t\t\t\t\t\tnum_2 = 1;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tnum_2 = 0;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(num_1 == num_2)continue;\n\n\t\t\t\t\tif(num_1 == ans_num[mark_1] && num_2 == ans_num[mark_2])continue;\n\n\t\t\t\t\tif(num_1 == 1){\n\t\t\t\t\t\tnext_state = Q.top().state-POW[mark_1]+POW[mark_2];\n\t\t\t\t\t}else{ //num_2 == 1\n\t\t\t\t\t\tnext_state = Q.top().state+POW[mark_1]-POW[mark_2];\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_cost = abs(mark_1-Q.top().loc)+abs(mark_2-mark_1);\n\n\t\t\t\t\tif(dp[next_state][mark_2] > Q.top().sum_cost+tmp_cost){\n\t\t\t\t\t\tdp[next_state][mark_2] = Q.top().sum_cost+tmp_cost;\n\t\t\t\t\t\tQ.push(Info(next_state,Q.top().sum_cost+tmp_cost,mark_2));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\tfor(int i = 0; i < 17; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3240, "memory_kb": 16396, "score_of_the_acc": -1.079, "final_rank": 17 }, { "submission_id": "aoj_2040_2310616", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) begin(v), end(v)\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nusing pint = pair<int, int>;\nusing tint = tuple<int, int, int>;\nusing vint = vector<int>;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nint N;\nstring s, t;\nint mincost[16][1<<16];\nint sbit, tbit;\n\nstruct State {\n int now, cost, bit;\n State(){}\n State(int now, int cost, int bit):now(now), cost(cost), bit(bit){}\n bool operator < (const State &st) const {\n return cost > st.cost;\n }\n};\n\nint dijkstra(int pos) {\n fill(mincost[0], mincost[16], inf);\n mincost[pos][sbit] = 0;\n priority_queue<State> que;\n que.emplace(pos, 0, sbit);\n while(!que.empty()) {\n State st = que.top(); que.pop();\n int now = st.now, cost = st.cost, bit = st.bit;\n //cout << now << \" \"<< cost << endl;\n //rep(i, N) cout << (int)((bit>>i)&1);\n //cout << endl;\n if(bit == tbit) return cost;\n if(mincost[now][bit] < cost) continue;\n rep(i, N) if(i != now) {\n int ncost = cost + abs(i-now);\n if(ncost < mincost[i][bit]) {\n\tmincost[i][bit] = ncost;\n\tque.emplace(i, ncost, bit);\n }\n if(((bit>>i)&1) == ((bit>>now)&1)) continue;\n int nbit = bit^(1<<i)^(1<<now);\n //rep(i, N) cout << (int)((bit>>i)&1);cout<<endl;\n //cout<<now<<\" \"<<i<<endl;\n //cout << (int)((bit>>i)&1) << \" \"<<(int)((bit>>now)&1) << endl;\n //cout<<(int)(((bit>>i)&1) == ((bit>>now)&1)) << endl;\n //rep(i, N) cout << (int)((nbit>>i)&1);cout<<endl;\n //assert(__builtin_popcount(nbit) == __builtin_popcount(bit));\n if(ncost < mincost[i][nbit]) {\n\tmincost[i][nbit] = ncost;\n\tque.emplace(i, ncost, nbit);\n }\n }\n }\n return inf;\n}\n\nsigned main()\n{\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n while(cin >> N, N) {\n cin >> s >> t;\n sbit = tbit = 0;\n rep(i, N) if(s[i] == 'B') sbit |= 1<<i;\n rep(i, N) if(t[i] == 'B') tbit |= 1<<i;\n int ans = inf;\n rep(i, N) {\n chmin(ans, dijkstra(i));\n //cout<<ans<<endl;//exit(0);\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 7490, "memory_kb": 21180, "score_of_the_acc": -1.9868, "final_rank": 20 }, { "submission_id": "aoj_2040_2310084", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint dp[1<<16][16],n;\nint main() {\n while(cin >> n && n) {\n string s,s2;cin>>s>>s2;\n int r=0;\n for(int t=0;t<(1<<n);t++)for(int i=0;i<n;i++)dp[t][i]=1<<29;\n for(int i=0;i<n;i++)if(s[i]==s2[i]) r|=1<<i;\n for(int i=0;i<n;i++)dp[r][i]=0;\n for(int t=0;t<(1<<n);t++) {\n for(int i=0;i<n;i++) {\n if(dp[t][i]==1<<29) continue;\n for(int j=0;j<n;j++) {\n if(t&(1<<j)) continue;\n for(int k=j+1;k<n;k++) {\n if(t&(1<<k)||s[j]==s[k]) continue;\n dp[t|(1<<j)|(1<<k)][j]=min(dp[t|(1<<j)|(1<<k)][j],dp[t][i]+abs(i-k)+abs(k-j));\n dp[t|(1<<j)|(1<<k)][k]=min(dp[t|(1<<j)|(1<<k)][k],dp[t][i]+abs(i-j)+abs(j-k));\n }\n }\n }\n }\n int ans=1<<29;\n for(int i=0;i<n;i++)ans=min(ans,dp[(1<<n)-1][i]);\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 7092, "score_of_the_acc": -0.0003, "final_rank": 1 }, { "submission_id": "aoj_2040_2309655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint dp[1<<16][16],n;\nint main() {\n while(cin >> n && n) {\n string s,s2;cin>>s>>s2;\n int r=0;\n for(int t=0;t<(1<<n);t++)for(int i=0;i<n;i++)dp[t][i]=1<<29;\n for(int i=0;i<n;i++)if(s[i]==s2[i]) r|=1<<i;\n for(int i=0;i<n;i++)dp[r][i]=0;\n for(int t=0;t<(1<<n);t++) {\n for(int i=0;i<n;i++) {\n if(dp[t][i]==1<<29) continue;\n string e=\"\";\n for(int j=0; j<n;j++) {\n if(t&(1<<j)) e+='1';else e+='0';\n }\n for(int j=0;j<n;j++) {\n if(t&(1<<j)) continue;\n for(int k=j+1;k<n;k++) {\n if(t&(1<<k)||s[j]==s[k]) continue;\n dp[t|(1<<j)|(1<<k)][j]=min(dp[t|(1<<j)|(1<<k)][j],dp[t][i]+abs(i-k)+abs(k-j));\n dp[t|(1<<j)|(1<<k)][k]=min(dp[t|(1<<j)|(1<<k)][k],dp[t][i]+abs(i-j)+abs(j-k));\n }\n }\n }\n }\n int ans=1<<29;\n for(int i=0;i<n;i++)ans=min(ans,dp[(1<<n)-1][i]);\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 7236, "score_of_the_acc": -0.0171, "final_rank": 3 }, { "submission_id": "aoj_2040_2309492", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstring A,B;\nint n,Ab,Bb;\n\nint Bit(int bit ,int i,int x){\n while((bit>>i&1)!=x)bit ^= 1<<i;\n return bit;\n}\n\nint mem[16][1<<16],used[16][1<<16];\nint dep = 0;\nint dfs(short pos,int bit){\n cout<< dep++ <<endl;\n if(bit ==Bb) return 0;\n if(used[pos][bit]++) return mem[pos][bit];\n \n int &res = mem[pos][bit] = 1e9;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++){\n int nbit = bit;\n nbit = Bit(nbit,i,bit>>j&1);\n nbit = Bit(nbit,j,bit>>i&1);\n res = min(res,abs(pos-i)+abs(i-j)+dfs(j,nbit));\n }\n cout<<dep<<endl;\n return mem[pos][bit];\n}\n\n\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\nint bfs(){\n for(int j=0;j<16;j++) \n for(int i=0;i<(1<<16);i++)mem[j][i] = 1e9;\n \n priority_queue<PP,vector<PP>,greater<PP> > Q;\n for(int i=0;i<n;i++) Q.push(PP(0,P(i,Ab))),mem[i][Ab] = 0;\n while(!Q.empty()){\n PP t = Q.top();Q.pop();\n int cost = t.first;\n int pos = t.second.first;\n int bit = t.second.second;\n if(bit == Bb) return cost;\n if(mem[pos][bit]<cost)continue;\n \n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++){\n\tint nbit = bit;\n\tnbit = Bit(nbit,i,bit>>j&1);\n\tnbit = Bit(nbit,j,bit>>i&1);\n\tint ncost = cost+abs(pos-i)+abs(i-j);\n\tif(ncost >= mem[j][nbit])continue;\n\tQ.push(PP(ncost,P(j,nbit)));\n\tmem[j][nbit] = ncost;\n }\n }\n assert(0); \n}\n\n\nint main(){\n \n while(cin>>n,n){\n cin>>A>>B;\n \n Ab = Bb = 0;\n for(int i=0;i<n;i++)Ab|=(A[i]=='W')<<i, Bb|=(B[i]=='W')<<i;\n \n memset(used,0,sizeof(used));\n int ans = 1e9;\n //for(int i=0;i<n;i++) ans=min(ans,dfs(i,Ab));\n //cout<<ans<<endl;\n cout<<bfs()<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3040, "memory_kb": 21368, "score_of_the_acc": -1.4003, "final_rank": 19 }, { "submission_id": "aoj_2040_2309377", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint dp[1<<16][16];\nint main() {\n int n;\n while(cin >> n && n) {\n string s,s2;\n cin >> s >> s2;\n int r=0;\n for(int t=0;t<(1<<n);t++)for(int i=0;i<n;i++)dp[t][i]=1<<29;\n for(int i=0;i<n;i++)if(s[i]==s2[i]) r|=1<<i;\n for(int i=0;i<n;i++)dp[r][i]=0;\n for(int t=0;t<(1<<n);t++) {\n for(int i=0;i<n;i++) {\n if(dp[t][i]==1<<29) continue;\n string e=\"\";\n for(int j=0; j<n;j++) {\n if(t&(1<<j)) e+='1';\n else e+='0';\n }\n for(int j=0;j<n;j++) {\n if(t&(1<<j)) continue;\n for(int k=j+1;k<n;k++) {\n if(t&(1<<k)||s[j]==s[k]) continue;\n dp[t|(1<<j)|(1<<k)][j]=min(dp[t|(1<<j)|(1<<k)][j],dp[t][i]+abs(i-k)+abs(k-j));\n dp[t|(1<<j)|(1<<k)][k]=min(dp[t|(1<<j)|(1<<k)][k],dp[t][i]+abs(i-j)+abs(j-k));\n }\n }\n }\n }\n int ans=1<<29;\n for(int i=0;i<n;i++)ans=min(ans,dp[(1<<n)-1][i]);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7268, "score_of_the_acc": -0.022, "final_rank": 4 }, { "submission_id": "aoj_2040_2137800", "code_snippet": "#include<iostream>\n#include<queue>\n#include<string>\n#include<cmath>\nusing namespace std;\nint n, s, t; int dp[16][65536], p[16][16];\nqueue<pair<int, int>>Q;\nint main() {\n\twhile (true) {\n\t\tcin >> n; s = 0; t = 0; if (n == 0)break;\n\t\tfor (int i = 0; i < n; i++) { for (int j = 0; j < (1 << n); j++)dp[i][j] = 999999999; }\n\t\tstring S1; cin >> S1; for (int i = 0; i < S1.size(); i++) { if (S1[i] == 'B')s += (1 << i); }\n\t\tstring S2; cin >> S2; for (int i = 0; i < S2.size(); i++) { if (S2[i] == 'B')t += (1 << i); }\n\t\tfor (int i = 0; i < n; i++) { dp[i][s] = 0; Q.push(make_pair(i, s)); }\n\t\twhile (!Q.empty()) {\n\t\t\tint a1 = Q.front().first, a2 = Q.front().second; Q.pop();\n\t\t\tif (a1 < n - 1 && dp[a1 + 1][a2] > dp[a1][a2] + 1) { dp[a1 + 1][a2] = dp[a1][a2] + 1; Q.push(make_pair(a1 + 1, a2)); }\n\t\t\tif (a1 >= 1 && dp[a1 - 1][a2] > dp[a1][a2] + 1) { dp[a1 - 1][a2] = dp[a1][a2] + 1; Q.push(make_pair(a1 - 1, a2)); }\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tint bit[20]; for (int j = 0; j < n; j++)bit[j] = (a2 / (1 << j)) % 2;\n\t\t\t\tswap(bit[i], bit[a1]); int cost = abs(i - a1);\n\t\t\t\tint ret = 0; for (int j = 0; j < n; j++)ret += (1 << j)*bit[j];\n\t\t\t\tif (dp[i][ret] > dp[a1][a2] + cost) { dp[i][ret] = dp[a1][a2] + cost; Q.push(make_pair(i, ret)); }\n\t\t\t}\n\t\t}\n\t\tint minx = 999999999; for (int i = 0; i < n; i++)minx = min(minx, dp[i][t]);\n\t\tcout << minx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5410, "memory_kb": 7584, "score_of_the_acc": -0.7544, "final_rank": 16 } ]
aoj_2039_cpp
Problem A: Space Coconut Crab II A space hunter, Ken Marineblue traveled the universe, looking for the space coconut crab. The space coconut crab was a crustacean known to be the largest in the universe. It was said that the space coconut crab had a body of more than 400 meters long and a leg span of no shorter than 1000 meters long. Although there were numerous reports by people who saw the space coconut crab, nobody have yet succeeded in capturing it. After years of his intensive research, Ken discovered an interesting habit of the space coconut crab. Surprisingly, the space coconut crab went back and forth between the space and the hyperspace by phase drive, which was the latest warp technology. As we, human beings, was not able to move to the hyperspace, he had to work out an elaborate plan to capture them. Fortunately, he found that the coconut crab took a long time to move between the hyperspace and the space because it had to keep still in order to charge a sufficient amount of energy for phase drive. He thought that he could capture them immediately after the warp-out, as they moved so slowly in the space. He decided to predict from the amount of the charged energy the coordinates in the space where the space coconut crab would appear, as he could only observe the amount of the charged energy by measuring the time spent for charging in the hyperspace. His recent spaceship, Weapon Breaker, was installed with an artificial intelligence system, CANEL. She analyzed the accumulated data and found another surprising fact; the space coconut crab always warped out near to the center of a triangle that satisfied the following conditions: each vertex of the triangle was one of the planets in the universe; the length of every side of the triangle was a prime number; and the total length of the three sides of the triangle was equal to T , the time duration the space coconut crab had spent in charging energy in the hyperspace before moving to the space. CANEL also devised the method to determine the three planets comprising the triangle from the amount of energy that the space coconut crab charged and the lengths of the triangle sides. However, the number of the candidate triangles might be more than one. Ken decided to begin with calculating how many different triangles were possible, analyzing the data he had obtained in the past research. Your job is to calculate the number of different triangles which satisfies the conditions mentioned above, for each given T . Input The input consists of multiple datasets. Each dataset comes with a line that contains a single positive integer T (1 ≤ T ≤ 30000). The end of input is indicated by a line that contains a zero. This should not be processed. Output For each dataset, print the number of different possible triangles in a line. Two triangles are different if and only if they are not congruent. Sample Input 10 12 15 777 4999 5000 0 Output for the Sample Input 0 1 2 110 2780 0
[ { "submission_id": "aoj_2039_10850472", "code_snippet": "#include <stdio.h>\n#define MAX 30010\n\nint dy[MAX], prime[MAX];\nint n, pcnt;\nbool p[MAX];\n\nvoid getprime(void){\n\tint i, j;\n\tfor (i=2;i<=15000;i++){\n\t\tif (p[i]) continue;\n\t\tprime[++pcnt]=i;\n\t\tfor (j=i*i;j<=15000;j+=i){\n\t\t\tp[j]=1;\n\t\t}\n\t}\n}\n\nint main(void){\n\tint i, j;\n\tint pi, pj, pk;\n\tgetprime();\n\t\n\tfor (;;){\n\t\tint cnt=0;\n\t\tscanf(\"%d\", &n);\n\t\tif (n==0) break;\n\t\tfor (i=1;i<=pcnt;i++){\n\t\t\tpi=prime[i];\n\t\t\tif (pi>=n) break;\n\t\t\tfor (j=i;j<=pcnt;j++){\n\t\t\t\tpj=prime[j];\n\t\t\t\tif (pi+pj>=n) break;\n\t\t\t\tpk=n-pi-pj;\n\t\t\t\tif (pk<pj) break;\n\t\t\t\tif (p[pk]) continue;\n\t\t\t\tif (pi<pj+pk && pj<pk+pi && pk<pi+pj){\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", cnt);\n\t}\n\treturn false;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 2976, "score_of_the_acc": -0.2357, "final_rank": 7 }, { "submission_id": "aoj_2039_9558141", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\nfor triangle: a + b > c where a, b are the shorter sides\n\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\nconst int MAX_V = 30000;\nstatic bool prime[MAX_V+1];\n\nconst int K = 3;\nconst int P = 3245;\nstatic int S[MAX_V+1][K];\nstatic vector<int> primes(P);\n//------------------------------------------------------------------------------\nvoid sieve()\n{\n prime[0] = false;\n prime[1] = false;\n\n for (int i=2; i<=MAX_V; i++) prime[i] = true;\n\n for (int i=2; i*i<=MAX_V; i++)\n {\n if (prime[i])\n {\n for (int j=2*i; j<=MAX_V; j+=i) prime[j] = false;\n }\n }\n\n int index = 0;\n for (int i=2; i<=MAX_V; i++)\n if (prime[i])\n primes[index++] = i;\n}\n\n\n//------------------------------------------------------------------------------\nint solve(int sum)\n{\n int res = 0;\n for (int p1=0; p1<P; ++p1)\n {\n if (primes[p1] >= sum) break;\n for (int p2=p1; p2<P; ++p2)\n {\n if (primes[p1]+primes[p2] >= sum) break;\n int c = sum - primes[p1] - primes[p2];\n if (prime[c] && c >= primes[p1] && c >= primes[p2] && c < primes[p1]+primes[p2]) res++;\n }\n }\n return res;\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n sieve();\n //--------------------------------------------------------------------------\n int N, sum, num;\n while (true)\n {\n num = scanf(\"%d \", &sum);\n if (sum == 0) break;\n int res = solve(sum);\n printf(\"%d\\n\", res);\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 690, "memory_kb": 3492, "score_of_the_acc": -0.3766, "final_rank": 13 }, { "submission_id": "aoj_2039_5753197", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nconst int N=30030;\nint not_is_prime[N];\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n vector<int> P;\n for(int i=2;i<N;i++){\n if(!not_is_prime[i]){\n P.push_back(i);\n for(int j=i+i;j<N;j+=i){\n not_is_prime[j]=1;\n }\n }\n }\n vector<vector<int>> res(4,vector<int>(N));\n res[0][0]=1;\n for(auto p:P){\n for(int i=1;i<4;i++){\n for(int j=p;j<N;j++){\n if(i==3 and p>=j-p)continue;\n res[i][j]+=res[i-1][j-p];\n }\n }\n }\n int n;\n while(cin >> n,n){\n printf(\"%d\\n\",res[3][n]);\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3776, "score_of_the_acc": -0.3148, "final_rank": 8 }, { "submission_id": "aoj_2039_3678466", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <climits>\n#include <iomanip>\n#include <algorithm>\n#include <queue>\n#include <map>\n#include <tuple>\n#include <iostream>\n#include <deque>\n#include <array>\n#include <set>\n#include <functional>\n#include <memory>\n\n\nstd::vector<int> primes(const int upper_limit) {\n\tstd::vector<bool> is_prime(upper_limit + 1, true);\n\tfor (auto i = 2; i * i <= upper_limit; ++i) {\n\t\tif (is_prime[i]) {\n\t\t\tfor (auto j = i * i; j <= upper_limit; j += i) {\n\t\t\t\tis_prime[j] = false;\n\t\t\t}\n\t\t}\n\t}\n\tstd::vector<int> result;\n\tfor (auto i = 2; i <= upper_limit; ++i) {\n\t\tif (is_prime[i]) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t}\n\treturn result;\n}\nint main() {\n\tconst auto prime = primes(30000);\n\twhile (true) {\n\t\tint n; std::cin >> n;\n\t\tif (n == 0) return 0;\n\t\tauto count = 0;\n\t\tauto greater = std::lower_bound(prime.begin(), prime.end(), n / 3);\n\t\twhile (greater != prime.end() && *greater * 2 < n) {\n\t\t\tauto mid = std::lower_bound(prime.begin(), prime.end(), (n - *greater) / 2);\n\t\t\twhile (*mid <= *greater) {\n\t\t\t\tif (n - *greater - *mid <= *mid && std::binary_search(prime.begin(), prime.end(), n - *greater - *mid)) {\n\t\t\t\t\t++count;\n\t\t\t\t}\n\t\t\t\t++mid;\n\t\t\t}\n\t\t\t++greater;\n\t\t}\n\t\tstd::cout << count << std::endl;\n\t}\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3196, "score_of_the_acc": -0.354, "final_rank": 11 }, { "submission_id": "aoj_2039_2665941", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\nvector<int>primes;\nvoid hurui(const int amax) {\n\tstatic bool flag = false;\n\tif (flag)return;\n\tvector<int>sos;\n\tsos = vector<int>(amax + 1, true);\n\tsos[0] = false; sos[1] = false;\n\tfor (int i = 2; i <= amax; ++i) {\n\t\tif (sos[i]) {\n\t\t\tfor (int j = 2 * i; j <= amax; j += i)sos[j] = false;\n\t\t}\n\t}\n\tfor (int i = 0; i <= amax; ++i) {\n\t\tif (sos[i]) {\n\t\t\tprimes.push_back(i);\n\t\t}\n\t}\n\tflag = true;\n}\n\nint main() {\n\thurui(3e4);\n\twhile (true) {\n\t\tint T;cin>>T;\n\t\tif(!T)break;\n\t\tint ans=0;\n\t\tfor (int i = 0; i < primes.size(); ++i) {\n\t\t\tfor (int j = i; j < primes.size(); ++j) {\n\t\t\t\tint a=primes[i];\n\t\t\t\tint b=primes[j];\n\t\t\t\tint c=T-a-b;\n\t\t\t\tif(b>c)continue;\n\t\t\t\tif(a+b<=c)continue;\n\t\t\t\tif (binary_search(primes.begin(), primes.end(), c)) {\n\t\t\t\t\tans++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1630, "memory_kb": 3228, "score_of_the_acc": -0.4912, "final_rank": 15 }, { "submission_id": "aoj_2039_2555244", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nbool is_prime[30001];\nvector<int> PRIME;\n\nint main(){\n\n\tfor(int i = 0; i <= 30000; i++){\n\t\tis_prime[i] = true;\n\t}\n\n\tfor(int i = 2; i <= sqrt(30000); i++){\n\t\tif(is_prime[i]){\n\t\t\tfor(int k = 2*i; k <= 30000; k += i){\n\t\t\t\tis_prime[k] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 2; i <= 30000; i++){\n\t\tif(is_prime[i]){\n\t\t\tPRIME.push_back(i);\n\t\t}\n\t}\n\n\tint T;\n\twhile(true){\n\t\tscanf(\"%d\",&T);\n\t\tif(T == 0)break;\n\n\t\tint ans = 0;\n\n\t\tfor(int a = 0; a < PRIME.size(); a++){\n\t\t\tif(PRIME[a] > T)break;\n\t\t\tfor(int b = a; b < PRIME.size(); b++){\n\t\t\t\tif(PRIME[a]+PRIME[b] > T)break;\n\t\t\t\tint c = T -(PRIME[a]+PRIME[b]);\n\n\t\t\t\tif(!is_prime[c])continue;\n\n\t\t\t\tif(PRIME[b] <= c && (PRIME[a]+PRIME[b]) > c && (PRIME[b]+c) > PRIME[a] && (c+PRIME[a]) > PRIME[b])ans++;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 3324, "score_of_the_acc": -0.3244, "final_rank": 9 }, { "submission_id": "aoj_2039_2276125", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define mk make_pair\nint s[30001],ss[30001],c=2;\nset<long long>se;\nint main(){\n s[0]=2;s[1]=3;ss[2]=ss[3]=1;\n for(int i=2;i<30001;i++){\n int p=0;\n for(int j=2;j*j<=i;j++)\n if(i%j==0)p++;\n if(!p)ss[i]++,s[c++]=i;\n }\n int n;\n while(cin>>n,n){\n se.clear();\n int sum=0;\n for(int i=0;i<c;i++){\n if(s[i]>n/2+1)break;\n for(int j=0;j<c;j++){\n if(s[j]+s[i]>n)break;\n if(n-s[j]-s[i]>0)\n if(ss[n-s[j]-s[i]]){\n int d[3];\n d[0]=s[i];\n d[1]=s[j];\n d[2]=n-s[j]-s[i];\n sort(d,d+3);\n if(!se.count(d[0]*10000000000+d[1]*100000+d[2])&&d[0]+d[1]>d[2]){\n se.insert(d[0]*10000000000+d[1]*100000+d[2]);\n sum++;\n }\n }\n }\n }\n cout<<sum<<endl;\n }\n }", "accuracy": 1, "time_ms": 6450, "memory_kb": 5916, "score_of_the_acc": -1.5808, "final_rank": 20 }, { "submission_id": "aoj_2039_2275730", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define mk make_pair\nint s[30001],ss[30001],c=2;\nunordered_set<long long>se;\nint main(){\n s[0]=2;s[1]=3;ss[2]=ss[3]=1;\n for(int i=2;i<30001;i++){\n int p=0;\n for(int j=2;j*j<=i;j++)\n if(i%j==0)p++;\n if(!p)ss[i]++,s[c++]=i;\n }\n int n;\n while(cin>>n,n){\n se.clear();\n int sum=0;\n for(int i=0;i<c;i++){\n if(s[i]>n/2+1)break;\n for(int j=0;j<c;j++){\n\tif(s[j]+s[i]>n)break;\n\tif(n-s[j]-s[i]>0)\n\t if(ss[n-s[j]-s[i]]){\n\t int d[3];\n\t d[0]=s[i];\n\t d[1]=s[j];\n\t d[2]=n-s[j]-s[i];\n\t sort(d,d+3);\n\t if(!se.count(d[0]*10000000000+d[1]*100000+d[2])&&d[0]+d[1]>d[2]){\n\t\tse.insert(d[0]*10000000000+d[1]*100000+d[2]);\n\t sum++;\n\t }\n\t }\n }\n }\n cout<<sum<<endl;\n }\n}", "accuracy": 1, "time_ms": 3390, "memory_kb": 5312, "score_of_the_acc": -1.0261, "final_rank": 18 }, { "submission_id": "aoj_2039_2275718", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int TMAX = 30000;\n\npair< vector<int>, vector<int> > eratosthenes(int N){\n vector<int> res;\n vector<int> arr(N);\n for(int i=0; i < N; i++){\n arr[i] = true;\n }\n arr[0] = false;\n arr[1] = false;\n for(int i=2; i*i <= N; i++){\n if(arr[i] == true){\n for(int j=i+i; j < N; j += i){\n arr[j] = false;\n }\n }\n }\n for(int i = 0; i < N; i++){\n if(arr[i] == true) res.push_back(i);\n }\n return pair< vector<int>, vector<int> >(res, arr);\n}\n\nbool isTri(int i, int j, int k){\n int a[] = {i, j, k};\n sort(a, a+3);\n if(a[2] < a[1] + a[0]) return true;\n else return false;\n}\n\nint main(void){\n int T;\n while(cin >> T, T){\n pair< vector<int>, vector<int> > tmp = eratosthenes(TMAX);\n vector<int> primes = tmp.first;\n vector<int> is_prime = tmp.second;\n int ans = 0;\n for(int i = 0; i < primes.size(); i++){\n for(int j = i; j < primes.size(); j++){\n int k = T - primes[i] - primes[j];\n if(k >= 1 && k >= primes[j] && is_prime[k] == true && isTri(primes[i], primes[j], k)){\n //cout << primes[i] << \" \" << primes[j] << \" \" << k << endl;\n ans++;\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1180, "memory_kb": 3124, "score_of_the_acc": -0.4077, "final_rank": 14 }, { "submission_id": "aoj_2039_2275656", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int MAX_T = 30000;\nint T;\nll res[30003];\nvector<int> ps;\nunordered_map<int,ll> dp[2];\n\nvoid init(){\n bool f[30003];\n memset( f,0,sizeof(f) );\n for(int i=2;i<=30000;i++){\n if( f[i] ) continue;\n ps.emplace_back(i);\n for(int j=i+i;j<=30000;j+=i)\n f[j] = true;\n }\n}\n\nvoid solve(){\n for(int p : ps){\n dp[0][p]++;\n for( auto q : dp[0] ){\n if( q.first + p > MAX_T ) continue; \n dp[1][q.first+p] += q.second;\n }\n for( auto q : dp[1] ){\n if( q.first + p > MAX_T ) continue;\n if( q.first <= p )\n continue; \n res[ q.first + p ] += q.second;\n }\n }\n}\n\nint main(){\n init();\n solve();\n while( cin >> T && T ){\n cout << res[T] << endl;\n }\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 4108, "score_of_the_acc": -0.3749, "final_rank": 12 }, { "submission_id": "aoj_2039_2275614", "code_snippet": "#include<bits/stdc++.h>\n#define N 40001\nusing namespace std;\n#define int long long\nint T;\nbool used[N];\nvector<int> prime;\n\n\nsigned main(){\n used[0] = used[1] = 1;\n for(int i=2;i*i<N;i++)\n if(used[i] == 0)\n for(int j=2;j<N/i;j++)used[i*j] = 1;\n for(int i=0;i<N;i++) if(!used[i]) prime.push_back(i);\n \n while(1){\n cin>>T;\n if(T==0)break;\n \n\n \n int ans = 0;\n for(int i=0;i<prime.size();i++)\n for(int j=i;j<prime.size();j++){\n int a = prime[i];\n int b = prime[j];\n int c = T-(a+b);\n if(b>c)continue;\n if(a+b>=T||used[c] == 1)continue;\n if(a+b>c && a+c>b && b+c>a)ans++;\n }\n cout<<ans<<endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 2150, "memory_kb": 3160, "score_of_the_acc": -0.5642, "final_rank": 16 }, { "submission_id": "aoj_2039_1893559", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nint main(){\n\t#define MAX 30010\n\tbool sosu[MAX]={1,1,0};\n\tfor(int i=2;i*i<=MAX;i++)if(!sosu[i])\n\tfor(int j=2*i;j<MAX;j+=i)sosu[j]=true;\n\tvi so;\n\trep(i,MAX)if(sosu[i]==0)so.pb(i);\n\tint n;\n\twhile(cin>>n,n){\n\t\tint out=0;\n\t\tint m=lower_bound(all(so),n)-so.begin();\n\t\trep(i,m)loop(j,i,m){\n\t\t\tint t=n-so[i]-so[j];\n\t\t\tif(t<so[j])continue;\n\t\t\tif(sosu[t])continue;\n\t\t\tint ma=max(t,max(so[i],so[j]));\n\t\t\tint sum=t+so[i]+so[j];\n\t\t\tif(sum>2*ma)out++;\n\t\t}\n\t\tcout<<out<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 1284, "score_of_the_acc": -0.0849, "final_rank": 5 }, { "submission_id": "aoj_2039_1882397", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX 15010\ntypedef pair<int,int> pii;\ntypedef pair<pii,int> P;\n \nbool check(int a,int b,int c){\n if(a >= b+c || b >= a+c || c >= a+b) return false;\n return true;\n}\n \nint main(){\n vector<int> prime;\n bool p[MAX];\n fill(p,p+MAX,true);\n p[0] = p[1] = false;\n for(int i = 2 ; i < MAX ; i++){\n\tif(!p[i]) continue;\n\tprime.push_back(i);\n\tfor(int j = 2*i ; j < MAX ; j += i){\n\t p[j] = false;\n\t}\n }\n int T;\n while(cin >> T, T){\n\tint cnt = 0;\n\tset<P> st;\n\tint size = prime.size();\n\tfor(int i = 0 ; i < size ; i++){\n\t if(prime[i] > T) continue;\n\t for(int j = i ; j < size ; j++){\n\t\tif(prime[i]+prime[j] > T) continue;\n\t\tint k = T-prime[i]-prime[j];\n\t\tif(k < 0 || k >= MAX || !p[k]) continue;\n\t\tint a[3] = {prime[i],prime[j],k};\n\t\tsort(a,a+3);\n\t\tif(st.count(P(pii(a[0],a[1]),a[2]))) continue;\n\t\tst.insert(P(pii(a[0],a[1]),a[2]));\n\t\tif(check(a[0],a[1],a[2])){\n\t\t cnt++;\n\t\t}\n\t }\n\t}\n\tcout << cnt << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3450, "memory_kb": 9288, "score_of_the_acc": -1.5298, "final_rank": 19 }, { "submission_id": "aoj_2039_1800026", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\nint P[31000]; vector<int>prime;\nint p[31000]; int M = 30000;\nint main() {\n\tp[0] = 1; p[1] = 1;\n\tfor (int i = 2; i*i <= M; i++) {\n\t\tfor (int j = i*i; j <= M; j += i)p[j] = 1;\n\t}\n\tfor (int i = 0; i <= M; i++) { if (p[i] == 0)prime.push_back(i); }\n\tfor (int i = 0; i < prime.size(); i++) {\n\t\tfor (int j = i; j < prime.size(); j++) {\n\t\t\tfor (int k = j; k < prime.size(); k++) {\n\t\t\t\tint v = prime[i] + prime[j] + prime[k];\n\t\t\t\tif (v > M)continue;\n\t\t\t\tif (prime[i] + prime[j] <= prime[k])continue;\n\t\t\t\tP[v]++;\n\t\t\t}\n\t\t}\n\t}\n\tint n;\n\twhile (true) {\n\t\tcin >> n; if (n == 0)break;\n\t\tcout << P[n] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4140, "memory_kb": 3360, "score_of_the_acc": -0.901, "final_rank": 17 }, { "submission_id": "aoj_2039_1799919", "code_snippet": "#include <iostream>\nusing namespace std;\nint n, c, p[5000]; bool prime[30001];\nint main() {\n\tfor(int i = 2; i <= 30000; i++) prime[i] = true;\n\tfor(int i = 2; i * i <= 30000; i++) {\n\t\tif(!prime[i]) continue;\n\t\tfor(int j = i * i; j <= 30000; j += i) prime[j] = false;\n\t}\n\tfor(int i = 2; i <= 30000; i++) {\n\t\tif(prime[i]) p[c++] = i;\n\t}\n\twhile(cin >> n, n) {\n\t\tint ret = 0;\n\t\tfor(int i = 0; i < c && p[i] < n; i++) {\n\t\t\tfor(int j = i; j < c && p[i] + p[j] < n; j++) {\n\t\t\t\tint w = n - p[i] - p[j];\n\t\t\t\tif(prime[w] && p[i] + p[j] > w && w >= p[j]) ret++;\n\t\t\t}\n\t\t}\n\t\tcout << ret << endl;\n\t}\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 3148, "score_of_the_acc": -0.3276, "final_rank": 10 }, { "submission_id": "aoj_2039_1571918", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <climits>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <fstream>\n#include <iostream>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <vector>\n#include <cassert>\n// #include <bits/stdc++.h>\n\nusing namespace std;\n\n#define LOG(...) fprintf(stderr,__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort(ALL(c))\n#define RSORT(c) sort(RALL(c))\n#define SQ(n) (n)*(n)\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\n\nint primes[3000];\nint siz = 0;\nint num[40000]={0};\nbool isprime[200000]={false};\n\nint main() {\n for(int i=2;i<20000;i++){\n if(isprime[i])continue;\n primes[siz++]=i;\n for(int j=i*2;j<20000;j+=i){\n isprime[j]=true;\n }\n }\n for(int a=0;primes[a]<=15000;a++){\n for(int b=a;primes[b]<=15000;b++){\n for(int c=b;primes[c]<=15000;c++){\n if(primes[a]+primes[b]<=primes[c])continue;\n if(primes[a]+primes[b]+primes[c]>30000)continue;\n num[primes[a]+primes[b]+primes[c]]++;\n // LOG(\"%d %d %d\\n\",primes[a],primes[b],primes[c]);\n }\n }\n }\n // cout << \"end works\" << endl;\n\n int T;\n while(cin>>T,T){\n cout << num[T] << endl;\n }\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 1320, "score_of_the_acc": -0.1019, "final_rank": 6 }, { "submission_id": "aoj_2039_1357444", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_T = 30000;\nconst int MAX_SIDE = (MAX_T - 1) / 2;\n\n/* typedef */\n\ntypedef vector<int> vi;\n\n/* global variables */\n\nbool primes[MAX_SIDE + 1];\nvi pnums;\nint np;\n\n/* subroutines */\n\nvoid calc_prime(int max_p) {\n primes[0] = primes[1] = false;\n for (int p = 2; p <= max_p; p++) primes[p] = true;\n\n for (int p = 2; p * p <= max_p; p++)\n if (primes[p])\n for (int q = p * p; q <= max_p; q += p) primes[q] = false;\n\n for (int p = 2; p <= max_p; p++)\n if (primes[p]) pnums.push_back(p);\n\n np = pnums.size();\n}\n\n/* main */\n\nint main() {\n calc_prime(MAX_SIDE);\n\n for (;;) {\n int t;\n cin >> t;\n if (t == 0) break;\n\n int count = 0;\n int mina = t / 3;\n int maxside = (t - 1) / 2;\n \n for (vi::iterator ait = pnums.begin(); ait != pnums.end(); ait++) {\n int& a = *ait;\n if (a < mina) continue;\n if (a > maxside) break;\n\n int minb = (t - a) / 2;\n \n for (vi::iterator bit = pnums.begin(); bit != pnums.end(); bit++) {\n\tint& b = *bit;\n\tif (b < minb) continue;\n\tif (b > a) break;\n\n\tint c = t - (a + b);\n\tif (c > 0 && c <= b && primes[c]) {\n\t count++;\n\t}\n }\n }\n\n cout << count << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1244, "score_of_the_acc": -0.0031, "final_rank": 1 }, { "submission_id": "aoj_2039_1175868", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int>P;\nint pr[30001];\nvoid era(){\n\tpr[0]=pr[1]=1;\n\tfor(int i=2;i*i<=30000;i++){\n\t\tif(pr[i]==0){\n\t\t\tfor(int j=i+i;j<=30000;j+=i){\n\t\t\t\tpr[j]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=2;i<=30000;i++){\n\t\tif(pr[i]==0)P.push_back(i);\n\t}\n}\nint main(){\n\tera();\n\tfor(int t;cin>>t,t;){\n\t\tint ans=0;\n\t\tfor(int i=0;i<P.size();i++){\n\t\t\tif(t<P[i]*3)break;\n\t\t\tfor(int j=i;j<P.size();j++){\n\t\t\t\tint k=t-P[i]-P[j];\n\t\t\t\tif(k<P[j])break;\n\t\t\t\tif(k<P[i]+P[j] && pr[k]==0){\n\t\t\t\t\t//cout<<P[i]<<\" \"<<P[j]<<\" \"<<k<<endl;\n\t\t\t\t\tans++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 1364, "score_of_the_acc": -0.0243, "final_rank": 4 }, { "submission_id": "aoj_2039_1133041", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ninline bool isPrime(int a){\n if(a<=1)return false;\n for(int i=2;i*i<=a;i++){\n if(a%i==0)return false;\n }\n return true;\n}\n\nint main(){\n bool pt[30030] = {};\n vector<int> pl;\n for(int i=0;i<=30000;i++){\n if(isPrime(i)){\n pt[i] = 1; pl.push_back(i);\n }\n }\n\n int t;\n while(scanf(\"%d\",&t),t){\n int res = 0;\n for(int i=0;i<(int)pl.size();i++){\n for(int j=i;j<(int)pl.size();j++){\n\tint rem = t - pl[i] - pl[j];\n\tif(rem < pl[j])break;\n\tif(pl[i]+pl[j]>rem && pt[rem])res++;\n }\n }\n printf(\"%d\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1272, "score_of_the_acc": -0.0145, "final_rank": 2 }, { "submission_id": "aoj_2039_1133039", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ninline bool isPrime(int a){\n if(a<=1)return false;\n for(int i=2;i*i<=a;i++){\n if(a%i==0)return false;\n }\n return true;\n}\n\nint main(){\n bool pt[30030] = {};\n vector<int> pl;\n for(int i=0;i<=30000;i++){\n if(isPrime(i)){\n pt[i] = 1; pl.push_back(i);\n }\n }\n\n int t;\n while(cin >> t,t){\n int res = 0;\n for(int i=0;i<(int)pl.size();i++){\n for(int j=i;j<(int)pl.size();j++){\n\tint rem = t - pl[i] - pl[j];\n\tif(rem < pl[j])break;\n\tif(pl[i]+pl[j]>rem && pt[rem])res++;\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1280, "score_of_the_acc": -0.0154, "final_rank": 3 } ]
aoj_2048_cpp
Problem A: Everlasting...? Everlasting Sa-Ga, a new, hot and very popular role-playing game, is out on October 19, 2008. Fans have been looking forward to a new title of Everlasting Sa-Ga. Little Jimmy is in trouble. He is a seven-year-old boy, and he obtained the Everlasting Sa-Ga and is attempting to reach the end of the game before his friends. However, he is facing difficulty solving the riddle of the first maze in this game -- Everlasting Sa-Ga is notorious in extremely hard riddles like Neverending Fantasy and Forever Quest. The riddle is as follows. There are two doors on the last floor of the maze: the door to the treasure repository and the gate to the hell. If he wrongly opens the door to the hell, the game is over and his save data will be deleted. Therefore, he should never open the wrong door. So now, how can he find the door to the next stage? There is a positive integer given for each door -- it is a great hint to this riddle. The door to the treasure repository has the integer that gives the larger key numbe r. The key number of a positive integer n is the largest prime factor minus the total sum of any other prime factors, where the prime factors are the prime numbers that divide into n without leaving a remainder. Note that each prime factor should be counted only once. As an example, suppose there are doors with integers 30 and 20 respectively. Since 30 has three prime factors 2, 3 and 5, its key number is 5 - (2 + 3) = 0. Similarly, since 20 has two prime factors 2 and 5, its key number 20 is 5 - 2 = 3. Jimmy therefore should open the door with 20. Your job is to write a program to help Jimmy by solving this riddle. Input The input is a sequence of datasets. Each dataset consists of a line that contains two integers a and b separated by a space (2 ≤ a , b ≤ 10 6 ). It is guaranteed that key numbers of these integers are always different. The input is terminated by a line with two zeros. This line is not part of any datasets and thus should not be processed. Output For each dataset, print in a line ‘a’ (without quotes) if the door with the integer a is connected to the treasure repository; print ‘b’ otherwise. No extra space or character is allowed. Sample Input 10 15 30 20 0 0 Output for the Sample Input a b
[ { "submission_id": "aoj_2048_4109877", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\nusing namespace std;\ntypedef long long int ll;\n\nint key(int x){\n\tint s=0,m=0;\n\tfor(int i=2;i<=x;i++){\n\t\tif(x%i==0){\n\t\t\ts+=i;\n\t\t\tm=i;\n\t\t\twhile(x%i==0){\n\t\t\t\tx/=i;\n\t\t\t}\n\t\t}\n\t}\n\treturn m-(s-m);\n}\n \nint main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\twhile(1){\n\t\tint a,b; cin >> a >> b;\n\t\tif(a==0&&b==0)break;\n\t\tcout << (key(a)>key(b)?'a':'b') << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3032, "score_of_the_acc": -0.3498, "final_rank": 5 }, { "submission_id": "aoj_2048_3396011", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nint main(){\n vector<bool> nonp(1000001, 0);\n for(int i = 2; i < 1000001; i++){\n if(nonp[i]) continue;\n for(int j = i+i; j < 1000001; j += i) nonp[j] = true;\n }\n int a, b;\n while(cin >> a >> b, a+b){\n int x = 1<<30, y = 1<<30;\n for(int i = a; i >= 2; i--){\n if(a%i == 0 && !nonp[i]){\n if(x == 1<<30) x = i;\n else x -= i;\n }\n }\n for(int i = b; i >= 2; i--){\n if(b%i == 0 && !nonp[i]){\n if(y == 1<<30) y = i;\n else y -= i;\n }\n }\n cout << (x > y ? \"a\" : \"b\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3140, "score_of_the_acc": -1.2116, "final_rank": 19 }, { "submission_id": "aoj_2048_3089737", "code_snippet": "#define _USE_MATH_DEFINES\n\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <map>\n#include <list>\n\nusing namespace std;\n\ntypedef pair<long long int, long long int> P;\n\nlong long int INF = 1e18;\nlong long int MOD = 1e8 + 7;\n\nbool p[1100000] = {};\n\nint main(){\n\t\n\tint N = 1000000;\n\t\n\tfor(int i = 0; i < N; i++){\n\t\tp[i] = true;\n\t}\n\t\n\tp[0] = false;\n\tp[1] = false;\n\t\n\tfor(int i = 2; i < N; i++){\n\t\tif(p[i]){\n\t\t\tfor(int j = i * 2; j < N; j += i){\n\t\t\t\tp[j] = false;\n\t\t\t}\n\t\t}\n\t}\n\t\n\twhile(true){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tif(a == 0){\n\t\t\tbreak;\n\t\t}\n\t\tint Sa, Sb;\n\t\tint pmax = 0;\n\t\tint S = 0;\n\t\tfor(int i = 1; i <= a; i++){\n\t\t\tif(a % i == 0 && p[i]){\n\t\t\t\tS += i;\n\t\t\t\tpmax = i;\n\t\t\t}\n\t\t}\n\t\tSa = pmax * 2 - S;\n\t\t\n\t\tS = 0;\n\t\tfor(int i = 1; i <= b; i++){\n\t\t\tif(b % i == 0 && p[i]){\n\t\t\t\tS += i;\n\t\t\t\tpmax = i;\n\t\t\t}\n\t\t}\n\t\tSb = pmax * 2 - S;\n\t\tif(Sa > Sb){\n\t\t\tcout << \"a\" << endl;\n\t\t}else{\n\t\t\tcout << \"b\" << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 4036, "score_of_the_acc": -1.3086, "final_rank": 20 }, { "submission_id": "aoj_2048_2854456", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = double;\nconst ld eps = 1e-9;\n\nint adiv(int a) {\n\tmap<int,int>mp;\n\tint num(a);\n\tfor (int i = 2; i <= a; ++i) {\n\t\twhile (num%i == 0) {\n\t\t\tmp[i]++;\n\t\t\tnum/=i;\n\t\t}\n\t}\n\tint anum=0;\n\tfor (auto m : mp) {\n\t\tanum-=m.first;\n\t}\n\tanum+=2*prev(mp.end())->first;\n\treturn anum;\n}\n\nint main() {\n\twhile (true) {\n\n\t\tint A, B; cin >> A >> B;\n\t\tif(!A)break;\n\t\tA = adiv(A);\n\t\tB = adiv(B);\n\t\tif (A>B)cout << \"a\" << endl;\n\t\telse cout << \"b\" << endl;\n\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 2992, "score_of_the_acc": -1.0885, "final_rank": 17 }, { "submission_id": "aoj_2048_2630167", "code_snippet": "#include <iostream>\n#include <stdlib.h>\n#include <math.h>\n\nusing namespace std;\n\n/* ?????°?????´??°x??\\????????¨???????´???°???????????¢??° */\n/* ?????????????´???°????????°????????°???????????????????´???°???????´???????. */\nint era (int x, int *array)\n{\n\tint search[x];\n\tint len = 0;\n\tsearch[0] = search[1] = 0;\n\tfor (int i = 2; i < x; i++)\n\t\tsearch[i] = 1;\n\tdouble root_x = sqrt(x);\n\tfor (int i = 0; ; i++) {\n\t\tint num = search[i];\n\t\tif (num == 1) {\n\t\t\tif (root_x <= i)\n\t\t\t\tbreak;\n\t\t\telse {\n\t\t\t\tarray[len++] = i;\n\t\t\t\tfor (int j = i; j < x; j+=i)\n\t\t\t\t\tsearch[j] = 0;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < x; i++) {\n\t\tif (search[i] == 1) {\n\t\t\tarray[len++] = i;\n\t\t\tsearch[i] = 0;\n\t\t}\n\t}\n\treturn len;\n}\n\n/* ?????°????´??????°????±?????????¢??° */\nint p_list(int a, int *list, int *primes, int len)\n{\n\tint a_len = 0;\n\tint i = 0;\n\twhile (primes[i] <= a) \n\t{\n\t\tif (a % primes[i] == 0)\n\t\t\tlist[a_len++] = primes[i];\n\t\ti++;\n\t\tif (len <= i)\n\t\t\tbreak;\n\t}\n\treturn a_len++;\n}\n\nint key(int *list, int len)\n{\n\tint sum = 0;\n\tif (len == 1)\n\t\treturn list[0];\n\tfor (int i = 0; i < len - 1; i++) \n\t\tsum += list[i];\n\treturn list[len-1] - sum;\n\n\n}\n\nint main (void)\n{\n\tint *primes;\n\tint len;\n\tprimes = (int *)malloc(sizeof(int) * 1000000);\n\tlen = era(1000000, primes);\t\n\twhile (1) {\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tif (a == 0 && b == 0)\n\t\t\tbreak;\n\t\tint a_pri[a/2], b_pri[b/2];\n\t\tint a_len, b_len;\n\t\ta_len = p_list(a, a_pri, primes, len);\n\t\tb_len = p_list(b, b_pri, primes, len);\n#ifdef DEBUG\n\t\tcout << a_len << \" \" << b_len << endl;\n\t\tfor (int i = 0; i < a_len; i++)\n\t\t\tcout << a_pri[i] << endl;\n\t\tfor (int i = 0; i < b_len; i++)\n\t\t\tcout << b_pri[i] << endl;\n#endif\n\t\tint a_key, b_key;\n\t\ta_key = key(a_pri, a_len);\n\t\tb_key = key(b_pri, b_len);\n#ifdef DEBUG\n\t\tcout << \"a \" << a_key << endl;\n\t\tcout << \"b \" << b_key << endl;\n#endif\n\t\tif (a_key > b_key)\n\t\t\tcout << \"a\" << endl;\n\t\telse\n\t\t\tcout << \"b\" << endl;\n\t}\n\tfree(primes);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 6880, "score_of_the_acc": -1.069, "final_rank": 15 }, { "submission_id": "aoj_2048_2601674", "code_snippet": "#include <iostream>\n\nusing namespace std;\n\nint main(void){\n bool isPrime[1000001];\n\n for(int i = 0 ; i <= 1000000 ; i++){\n isPrime[i] = true;\n }\n\n isPrime[0] = false;\n isPrime[1] = false;\n for(int n = 2 ; n <= 1000000 ; n++){\n if(isPrime[n]){\n for(int m = n ; m <= 1000000 ; m += n){\n isPrime[m] = false;\n }\n isPrime[n] = true;\n }\n }\n\n while(1){\n int a , b;\n cin >> a;\n cin >> b;\n\n if(a == 0 && b == 0){\n return 0;\n }\n\n int akey = 0;\n int afactmax;\n for(int i = 2 ; i <= a ; i++){\n if(isPrime[i] && a % i == 0){\n akey += i;\n afactmax = i;\n }\n }\n akey = 2 * afactmax - akey;\n\n int bkey = 0;\n int bfactmax;\n for(int i = 2 ; i <= b ; i++){\n if(isPrime[i] && b % i == 0){\n bkey += i;\n bfactmax = i;\n }\n }\n bkey = 2 * bfactmax - bkey;\n\n if(akey > bkey){\n cout << \"a\" << endl;\n }else{\n cout << \"b\" << endl;\n }\n\n\n\n }\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 4052, "score_of_the_acc": -0.9096, "final_rank": 11 }, { "submission_id": "aoj_2048_2601650", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <iostream>\n#include <numeric>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n const auto primes = [] {\n bitset<1000000 + 1> b;\n auto t = b;\n t.set();\n t[0] = false;\n t[1] = false;\n int i = 0;\n while (true) {\n bool done = false;\n while (!t[i]) {\n ++i;\n if (i == t.size()) {\n done = true;\n break;\n }\n }\n if (done) {\n break;\n }\n\n b[i] = true;\n for (int j = i; j < t.size(); j += i) {\n t[j] = false;\n }\n }\n return b;\n }();\n\n while (true) {\n int a, b;\n cin >> a >> b;\n if (a == 0 && b == 0) {\n return 0;\n }\n\n auto f = [&primes](int x) {\n vector<int> r;\n int j = 2;\n while (true) {\n while (!primes[j]) {\n ++j;\n }\n if (x == 1) {\n break;\n }\n if (x % j == 0) {\n r.push_back(j);\n x /= j;\n }\n else {\n j += 1;\n }\n }\n r.erase(unique(begin(r), end(r)), end(r));\n return r;\n };\n\n auto pa = f(a);\n auto pb = f(b);\n\n auto g = [](const vector<int>& v) {\n auto s = accumulate(begin(v), --end(v), 0);\n return v.back() - s;\n };\n\n if (g(pa) > g(pb)) {\n cout << \"a\" << endl;\n }\n else {\n cout << \"b\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3284, "score_of_the_acc": -0.3454, "final_rank": 3 }, { "submission_id": "aoj_2048_2601640", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <iostream>\n#include <numeric>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n bitset<1000100 + 1> b;\n auto t = b;\n t.set();\n t[0] = false;\n t[1] = false;\n int i = 0;\n while (true) {\n bool done = false;\n while (!t[i]) {\n ++i;\n if (i == t.size()) {\n done = true;\n break;\n }\n }\n if (done) {\n break;\n }\n\n b[i] = true;\n for (int j = i; j < t.size(); j += i) {\n t[j] = false;\n }\n }\n\n while (true) {\n int a, bb;\n cin >> a >> bb;\n int aa = a;\n int bbb = bb;\n if (a == 0 && bb == 0) {\n return 0;\n }\n\n vector<int> pa;\n int j = 2;\n while (true) {\n while (!b[j]) {\n ++j;\n }\n if (a == 1) {\n break;\n }\n if (a % j == 0) {\n pa.push_back(j);\n a /= j;\n }\n else {\n j += 1;\n }\n }\n vector<int> pb;\n j = 2;\n while (true) {\n while (!b[j]) {\n ++j;\n }\n if (bb == 1) {\n break;\n }\n if (bb % j == 0) {\n pb.push_back(j);\n bb /= j;\n }\n else {\n j += 1;\n }\n }\n pa.erase(unique(begin(pa), end(pa)), end(pa));\n pb.erase(unique(begin(pb), end(pb)), end(pb));\n\n auto f = [](const vector<int>& v) {\n auto s = accumulate(begin(v), --end(v), 0);\n return v.back() - s;\n };\n if (f(pa) > f(pb)) {\n cout << \"a\" << endl;\n }\n else {\n cout << \"b\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3248, "score_of_the_acc": -0.3378, "final_rank": 2 }, { "submission_id": "aoj_2048_2601585", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <queue>\nusing namespace std;\nconst int N = 1000000;\nint main(int argc, char *argv[])\n{\n vector<bool> primes(N + 1,true);\n primes[0] = false;\n primes[1] = false;\n for(int i = 0; i <= N; i++) {\n if(primes[i]) {\n for(int j = i+i; j <= N; j+=i) {\n primes[j] = false;\n }\n }\n }\n vector<int> ps;\n for(int i = 0; i <= N; i++) {\n if(primes[i]) ps.push_back(i);\n }\n for(;;) {\n int a, b;\n cin >> a >> b;\n if(a == 0 && b == 0) break;\n auto key = [&ps](int n) {\n int p = 0;\n int s = 0;\n for(int i = 0; i < ps.size() && n >= ps[i]; i++) {\n if((n % ps[i]) == 0) {\n s += ps[i];\n p = ps[i];\n }\n }\n return p*2 - s;\n };\n int ka = key(a);\n int kb = key(b);\n cout << (ka > kb ? \"a\" : \"b\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3372, "score_of_the_acc": -0.318, "final_rank": 1 }, { "submission_id": "aoj_2048_2522997", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nbool e[1000005];\nint a,b,c,d;\nvector<int>s;\nmain(){\n for(int i=2;i<1000005;i++)\n if(!e[i])for(int j=i+i;j<1000005;j+=i)e[j]=1;\n for(int i=2;i<1000005;i++)\n if(!e[i])s.push_back(i);\n while(cin>>a>>b,a||b){\n c=d=0;\n for(int i=0;i<s.size()&&s[i]<=a;i++){\n int f1=0;\n while(a%s[i]==0)a/=s[i],f1++;\n if(f1)c-=s[i];\n if(a==1){\n c+=s[i]*2;\n break;\n }\n }\n for(int i=0;i<s.size()&&s[i]<=b;i++){\n int f1=0;\n while(b%s[i]==0)b/=s[i],f1++;\n if(f1)d-=s[i];\n if(b==1){\n d+=s[i]*2;\n break;\n }\n }\n if(c>d)cout<<\"a\"<<endl;\n else cout<<\"b\"<<endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4144, "score_of_the_acc": -0.4233, "final_rank": 6 }, { "submission_id": "aoj_2048_2441433", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cmath>\nusing namespace std; \ntypedef long long ll;\nll prime[1000001];\nll length = 0;\nvoid insert(ll p) { prime[length] = p; length++; return; }\nint main() {\n\tinsert(2);\n\tfor (ll i = 3; i < 1000000; i += 2) {\n\t\tfor (ll j = 0; j < length; j++)\n\t\t\tif (i%prime[j] == 0)goto cont;\n\t\t\telse if (prime[j] > sqrt(i))break;\n\t\t\tinsert(i);\n\t\tcont:;\n\t}\n\tll a, b;\n\twhile (cin >> a >> b){\n\t\tif (!a && !b)return 0;\n\t\tll am, bm, ac = 0, bc = 0;\n\t\tint i = 0;\n\t\twhile (prime[i] <= a){\n\t\t\tif (a%prime[i] == 0) {\n\t\t\t\tam = prime[i];\n\t\t\t\tac += prime[i];\n\t\t\t\twhile (a%prime[i] == 0)a /= prime[i];\n\t\t\t\tif (a == 1)break;\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\tac -= am; am -= ac;\n\t\ti = 0;\n\t\twhile (prime[i] <= b) {\n\t\t\tif (b%prime[i] == 0) {\n\t\t\t\tbm = prime[i];\n\t\t\t\tbc += prime[i];\n\t\t\t\twhile (b%prime[i] == 0)b /= prime[i];\n\t\t\t\tif (b == 1)break;\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\tbc -= bm; bm -= bc;\n\t\tif (am > bm)cout << \"a\\n\";\n\t\telse cout << \"b\\n\";\n\t}\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3652, "score_of_the_acc": -0.469, "final_rank": 7 }, { "submission_id": "aoj_2048_2097391", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n\n#define NUM 1000500\n\nint main(){\n\tint* table = new int[NUM];\n\tint limit;\n\n\tfor(int i=0; i < NUM;i++)table[i] = 1;\n\ttable[0] = 0;\n\ttable[1] = 0;\n\n\tlimit = sqrt(NUM);\n\n\tfor(int i=2;i<=limit;i++){\n\t\tif(table[i] == 1){\n\t\t\tfor(int k=2*i;k < NUM; k += i){\n\t\t\t\ttable[k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint a,b,index,array[1000],sum,a_key,b_key;\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&a,&b);\n\t\tif(a == 0 && b == 0)break;\n\n\t\tlimit = a/2 + 100;\n\t\tindex = 0;\n\n\t\tif(a%2 == 0)array[index++] = 2;\n\t\twhile(a%2 == 0)a /= 2;\n\n\t\tfor(int i = 3; i <= limit; i += 2){\n\t\t\tif(table[i] == 1 && a%i == 0){\n\t\t\t\tarray[index++] = i;\n\t\t\t\twhile(a%i == 0){\n\t\t\t\t\ta /= i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tsum = 0;\n\n\t\tif(index == 0 && table[a] == 1){\n\t\t\ta_key = a;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i <= index-2; i++){\n\t\t\t\tsum += array[i];\n\t\t\t}\n\n\t\t\ta_key = array[index-1] - sum;\n\t\t}\n\n\n\t\tlimit = b/2 + 100;\n\t\tindex = 0;\n\n\t\tif(b%2 == 0)array[index++] = 2;\n\t\twhile(b%2 == 0)b /= 2;\n\n\t\tfor(int i = 3; i <= limit; i += 2){\n\t\t\tif(table[i] == 1 && b%i == 0){\n\t\t\t\tarray[index++] = i;\n\t\t\t\twhile(b%i == 0){\n\t\t\t\t\tb /= i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tsum = 0;\n\n\t\tif(index == 0 && table[b] == 1){\n\t\t\tb_key = b;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i <= index-2; i++){\n\t\t\t\tsum += array[i];\n\t\t\t}\n\n\t\t\tb_key = array[index-1] - sum;\n\t\t}\n\n\t\tif(a_key > b_key)printf(\"a\\n\");\n\t\telse{\n\t\t\tprintf(\"b\\n\");\n\t\t}\n\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 6496, "score_of_the_acc": -1.08, "final_rank": 16 }, { "submission_id": "aoj_2048_2097387", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n\n#define NUM 1000500\n\nint main(){\n\tint* table = new int[NUM];\n\tint limit;\n\n\tfor(int i=0; i < NUM;i++)table[i] = 1;\n\ttable[0] = 0;\n\ttable[1] = 0;\n\n\tlimit = sqrt(NUM);\n\n\tfor(int i=2;i<=limit;i++){\n\t\tif(table[i] == 1){\n\t\t\tfor(int k=2*i;k < NUM; k += i){\n\t\t\t\ttable[k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint a,b,index,array[1000],sum,a_key,b_key;\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&a,&b);\n\t\tif(a == 0 && b == 0)break;\n\n\t\tlimit = a/2 + 100;\n\t\tindex = 0;\n\n\t\tfor(int i = 2; i <= limit; i++){\n\t\t\tif(table[i] == 1 && a%i == 0){\n\t\t\t\tarray[index++] = i;\n\t\t\t\twhile(a%i == 0){\n\t\t\t\t\ta /= i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tsum = 0;\n\n\t\tif(index == 0 && table[a] == 1){\n\t\t\ta_key = a;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i <= index-2; i++){\n\t\t\t\tsum += array[i];\n\t\t\t}\n\n\t\t\ta_key = array[index-1] - sum;\n\t\t}\n\n\n\t\tlimit = b/2 + 100;\n\t\tindex = 0;\n\n\t\tfor(int i = 2; i <= limit; i++){\n\t\t\tif(table[i] == 1 && b%i == 0){\n\t\t\t\tarray[index++] = i;\n\t\t\t\twhile(b%i == 0){\n\t\t\t\t\tb /= i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tsum = 0;\n\n\t\tif(index == 0 && table[b] == 1){\n\t\t\tb_key = b;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i <= index-2; i++){\n\t\t\t\tsum += array[i];\n\t\t\t}\n\n\t\t\tb_key = array[index-1] - sum;\n\t\t}\n\n\t\tif(a_key > b_key)printf(\"a\\n\");\n\t\telse{\n\t\t\tprintf(\"b\\n\");\n\t\t}\n\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 6484, "score_of_the_acc": -1.1119, "final_rank": 18 }, { "submission_id": "aoj_2048_2027319", "code_snippet": "//============================================================================\n// Name : A.cpp\n// Author : \n// Version :\n// Copyright : Your copyright notice\n// Description : Hello World in C++, Ansi-style\n//============================================================================\n\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nbool is_prime(int a){\n\tfor (int i = 2; i * i <= a; ++i) {\n\t\tif(a % i == 0) return false;\n\t}\n\treturn true;\n}\n\nvector<int> ps;\n\nint f(int a){\n\tvector<ll> v;\n\tfor(int p : ps){\n\t\tif(a % p == 0) v.push_back(p);\n\t}\n\tif(v.size() == 0) return 0;\n\tll ret = v.back();\n\tfor(int i = 0; i < (int)v.size() - 1; i++){\n\t\tret -= v[i];\n\t}\n\treturn ret;\n}\n\nint main() {\n\tfor(int i = 2; i <= 2000000; i++){\n\t\tif(is_prime(i)) ps.push_back(i);\n\t}\n\n\tint n, m;\n\twhile(cin >> n >> m , n + m){\n\t\tif(f(n) > f(m)){\n\t\t\tcout << \"a\" << endl;\n\t\t}\n\t\telse{\n\t\t\tcout << \"b\" <<endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 3708, "score_of_the_acc": -0.975, "final_rank": 12 }, { "submission_id": "aoj_2048_2023376", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nvector<int> sieve(int n) {\n vector<int> p(n);\n for(int i=2; i<n; i++) p[i]=i;\n for(int i=2; i*i<n; i++) {\n if(p[i]) {\n for(int j=i*i; j<n; j+=i) p[j]=0;\n }\n }\n p.erase(remove(p.begin(),p.end(),0),p.end());\n return p;\n}\n \nint main() {\n vector<int> p=sieve(1000000);\n int a[2];\n while(cin >> a[0] >> a[1]&&a[0]) {\n int b[2]={0,0};\n for(int j=0; j<2; j++) {\n int k=1;\n for(int i=p.size()-1; i>=0; i--) {\n if(a[j]%p[i]==0) {\n b[j]+=p[i]*k;\n k=-1;\n }\n }\n }\n if(b[0]>b[1]) cout << 'a' << endl;\n else cout << 'b' << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 6536, "score_of_the_acc": -1.0194, "final_rank": 13 }, { "submission_id": "aoj_2048_2022873", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint get_sum(set<int> a){\n int sum=0;\n set<int>::iterator it;\n for(it=a.begin();it!=--a.end();it++)sum+=(*it);\n return sum;\n}\n\nint get_num(set<int> &a){\n int res=*(--a.end());\n return res;\n}\n\n\n\nint main(){\n\n while(1){\n int a,b;\n cin>>a>>b;\n if(!a&&!b)return 0;\n set<int> A,B;\n for(int i=2;a!=1;i++){\n if(a%i==0)A.insert(i);\n while(a%i==0)a/=i;\n }\n\n for(int i=2;b!=1;i++){\n if(b%i==0)B.insert(i);\n while(b%i==0)b/=i;\n }\n\n a=get_num(A)-get_sum(A);\n b=get_num(B)-get_sum(B);\n if(a>b) cout<<\"a\"<<endl;\n else cout<<\"b\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3080, "score_of_the_acc": -0.5208, "final_rank": 9 }, { "submission_id": "aoj_2048_2022871", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> sieve(int n) {\n vector<int> p(n);\n for(int i=2; i<n; i++) p[i]=i;\n for(int i=2; i*i<n; i++) {\n if(p[i]) {\n for(int j=i*i; j<n; j+=i) p[j]=0;\n }\n }\n p.erase(remove(p.begin(),p.end(),0),p.end());\n return p;\n}\n\nint main() {\n vector<int> p=sieve(1000000);\n int a[2];\n while(cin >> a[0] >> a[1]&&a[0]) {\n int b[2]={0,0};\n for(int j=0; j<2; j++) {\n int k=1;\n for(int i=p.size()-1; i>=0; i--) {\n if(a[j]%p[i]==0) {\n b[j]+=p[i]*k;\n k=-1;\n }\n }\n }\n if(b[0]>b[1]) cout << 'a' << endl;\n else cout << 'b' << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 6544, "score_of_the_acc": -1.0211, "final_rank": 14 }, { "submission_id": "aoj_2048_1882410", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint func(int n)\n{\n int sum = 0, largest = 0;\n for (int i = 2; i <= n; i++) {\n if (n % i == 0) {\n sum += i;\n largest = i;\n while (n % i == 0) {\n n /= i;\n }\n }\n }\n sum -= largest;\n return largest - sum;\n}\n\nint main()\n{\n int a, b;\n while (cin >> a >> b, a) {\n if (func(a) > func(b)) {\n cout << 'a' << endl;\n } else {\n cout << 'b' << endl;\n }\n } \n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3028, "score_of_the_acc": -0.3489, "final_rank": 4 }, { "submission_id": "aoj_2048_1867478", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\nint a, b; bool prime[1111111];\nint main() {\n\tfill(prime + 2, prime + 1000001, true);\n\tfor (int i = 2; i <= 1000; i++) {\n\t\tif (!prime[i]) continue;\n\t\tfor (int j = i * i; j <= 1000000; j += i) prime[j] = false;\n\t}\n\twhile (cin >> a >> b, a | b) {\n\t\tint suma = 0, maxa = 0, sumb = 0, maxb = 0;\n\t\tfor (int i = 2; i <= a; i++) {\n\t\t\tif (prime[i] && a % i == 0) suma += i, maxa = i;\n\t\t}\n\t\tfor (int i = 2; i <= b; i++) {\n\t\t\tif (prime[i] && b % i == 0) sumb += i, maxb = i;\n\t\t}\n\t\tint reta = maxa * 2 - suma, retb = maxb * 2 - sumb;\n\t\tif (reta > retb) cout << \"a\" << endl;\n\t\telse cout << \"b\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 4060, "score_of_the_acc": -0.8423, "final_rank": 10 }, { "submission_id": "aoj_2048_1781132", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint main(){\n\t#define MAX 1000010\n\tbool sosu[MAX]={0};\n\tfor(int i=4;i<MAX;i+=2)sosu[i]=1;\n\tfor(int i=3;i*i<=MAX;i+=2)if(!sosu[i])\n\tfor(int j=i*3;j<MAX;j+=i*2)sosu[j]=1;\n\t\n\tint a,b;\n\twhile(cin>>a>>b,a+b){\n\t\tint A=0,B=0;\n\t\tbool ha=true;\n\t\tfor(int i=a;i>1;i--)if(sosu[i]==0&&a%i==0){\n\t\t\tif(ha){\n\t\t\t\tA+=i;\n\t\t\t\tha=false;\n\t\t\t}else A-=i;\n\t\t}\n\t\tha=true;\n\t\tfor(int i=b;i>1;i--)if(sosu[i]==0&&b%i==0){\n\t\t\tif(ha){\n\t\t\t\tB+=i;\n\t\t\t\tha=false;\n\t\t\t}else B-=i;\n\t\t}\n\t\tif(A>B)cout<<'a'<<endl;\n\t\telse cout<<'b'<<endl;\t\t\n\t}\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 2136, "score_of_the_acc": -0.4713, "final_rank": 8 } ]
aoj_2045_cpp
Problem G: Turn Polygons HCII, the health committee for interstellar intelligence, aims to take care of the health of every interstellar intelligence. Staff of HCII uses a special equipment for health checks of patients. This equipment looks like a polygon-shaped room with plenty of instruments. The staff puts a patient into the equipment, then the equipment rotates clockwise to diagnose the patient from various angles. It fits many species without considering the variety of shapes, but not suitable for big patients. Furthermore, even if it fits the patient, it can hit the patient during rotation. Figure 1: The Equipment used by HCII The interior shape of the equipment is a polygon with M vertices, and the shape of patients is a convex polygon with N vertices. Your job is to calculate how much you can rotate the equipment clockwise without touching the patient, and output the angle in degrees. Input The input consists of multiple data sets, followed by a line containing “0 0”. Each data set is given as follows: M N px 1 py 1 px 2 py 2 ... px M py M qx 1 qy 1 qx 2 qy 2 ... qx N qy N cx cy The first line contains two integers M and N (3 ≤ M, N ≤ 10). The second line contains 2 M integers, where ( px j , py j ) are the coordinates of the j -th vertex of the equipment. The third line contains 2 N integers, where ( qx j , qy j ) are the coordinates of the j -th vertex of the patient. The fourth line contains two integers cx and cy , which indicate the coordinates of the center of rotation. All the coordinates are between -1000 and 1000, inclusive. The vertices of each polygon are given in counterclockwise order. At the initial state, the patient is inside the equipment, and they don’t intersect each other. You may assume that the equipment doesn’t approach patients closer than 10 -6 without hitting patients, and that the equipment gets the patient to stick out by the length greater than 10 -6 whenever the equipment keeps its rotation after hitting the patient. Output For each data set, print the angle in degrees in a line. Each angle should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10 -7 . If the equipment never hit the patient during the rotation, print 360 as the angle. Sample Input 5 4 0 0 20 0 20 10 0 10 1 5 10 3 12 5 10 7 8 5 10 5 4 3 0 0 10 0 10 5 0 5 3 1 6 1 5 4 0 0 5 3 0 0 10 0 10 10 0 10 3 5 1 1 4 1 4 6 0 0 0 0 Output for the Sample Input 360.0000000 12.6803835 3.3722867
[ { "submission_id": "aoj_2045_5499629", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n#define EPS 0.000000001\n#define SIZE 15\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nint M,N;\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\nbool Cross_Check(Line A,Line B){\n\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn true;\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn true;\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn true;\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn true;\n\t}\n\n\treturn is_Cross(A,B);\n}\n\n\nbool is_Cross(Polygon a, Polygon b){\n\n\tfor(int i = 0; i < a.size(); i++){\n\n\t\tLine base_line = Line(a[i],a[(i+1)%a.size()]);\n\n\t\tfor(int k = 0; k < b.size(); k++){\n\n\t\t\tLine work_line = Line(b[k],b[(k+1)%b.size()]);\n\t\t\tif(getDistanceSP(work_line,a[i]) < EPS)return true;\n\n\t\t\tif(is_Cross(base_line,work_line)){\n\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn false;\n}\n\n\nvoid func(){\n\n\tPolygon BOX,poly;\n\n\tdouble x,y;\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tBOX.push_back(Point(x,y));\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tpoly.push_back(Point(x,y));\n\t}\n\n\tPoint base;\n\tscanf(\"%lf %lf\",&base.x,&base.y);\n\n\tint NUM = 10000;\n\n\tdouble diff = 2*M_PI/NUM;\n\tdouble roll_rad = 0;\n\tdouble last = -1;\n\n\tfor(int i = 0; i < NUM; i++){\n\n\t\tPolygon tmp_B;\n\t\tfor(int k = 0; k < BOX.size(); k++){\n\n\t\t\tPoint tmp_p = rotate(base,BOX[k],-roll_rad);\n\t\t\ttmp_B.push_back(tmp_p);\n\t\t}\n\n\t\tif(is_Cross(poly,tmp_B)){\n\t\t\tlast = roll_rad;\n\t\t\tbreak;\n\t\t}\n\n\t\troll_rad += diff;\n\t}\n\n\tif(fabs(last+1.0) < EPS){\n\n\t\tprintf(\"360.0000000000000\\n\");\n\t\treturn;\n\t}\n\n\tdouble left = last-diff,right = last,mid = (left+right)/2;\n\tdouble ans = right;\n\n\tfor(int loop = 0; loop < 100; loop++){\n\n\t\tPolygon tmp_B;\n\t\tfor(int k = 0; k < BOX.size(); k++){\n\n\t\t\tPoint tmp_p = rotate(base,BOX[k],-mid);\n\t\t\ttmp_B.push_back(tmp_p);\n\t\t}\n\t\tif(is_Cross(poly,tmp_B)){\n\n\t\t\tans = mid;\n\t\t\tright = mid-EPS;\n\t\t}else{\n\n\t\t\tleft = mid+EPS;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tprintf(\"%.12lf\\n\",ans*(180/M_PI));\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d\",&M,&N);\n\t\tif(M == 0 && N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3190, "memory_kb": 3616, "score_of_the_acc": -2, "final_rank": 18 }, { "submission_id": "aoj_2045_3106374", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nP unit(const P &p){\n return p/abs(p);\n}\n\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\n\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(cross(l[1]-l[0], p-l[0])) /abs(l[1]-l[0]);\n}\n\n\nVP crosspointCL(const C &c, const L &l){\n VP ret;\n P mid = projection(l, c.p);\n double d = distanceLP(l, c.p);\n if(EQ(d, c.r)){\n ret.push_back(mid);\n }else if(d < c.r){\n double len = sqrt(c.r*c.r -d*d);\n ret.push_back(mid +len*unit(l[1]-l[0]));\n ret.push_back(mid -len*unit(l[1]-l[0]));\n }\n return ret;\n}\nVP crosspointCS(const C &c, const L &s){\n VP ret;\n VP cp = crosspointCL(c,s);\n for(int i=0; i<(int)cp.size(); i++){\n if(intersectSP(s, cp[i])){\n ret.push_back(cp[i]);\n }\n }\n return ret;\n}\n\ndouble getangle360(const P &p, const P &a, const P &b){\n P proj = projection(L(p,a), b);\n int sign= (dot(a-p, b-p)>0)? 1: -1;\n if(ccw(p,a,b)!=-1){\n return acos(sign*abs(proj-p)/abs(b-p));\n }else{\n return 2*PI - acos(sign*abs(proj-p)/abs(b-p));\n }\n}\n\nint main(){\n while(1){\n int m,n;\n cin >> m >> n;\n if(m == 0) break;\n\n VP p(m), q(n);\n for(int i=0; i<m; i++){\n double x,y;\n cin >> x >> y;\n p[i] = P(x, y);\n }\n for(int i=0; i<n; i++){\n double x,y;\n cin >> x >> y;\n q[i] = P(x, y);\n }\n double cx,cy;\n cin >> cx >> cy;\n P c(cx, cy);\n\n double ans = 2*PI;\n //すべての辺同士の組み合わせ\n for(int i=0; i<m; i++){\n for(int j=0; j<n; j++){\n L ei(p[i], p[(i+1)%m]), ej(q[j], q[(j+1)%n]);\n for(int d=0; d<2; d++){\n C ci(c, abs(ei[d] -c)), cj(c, abs(ej[d] -c));\n VP cpi = crosspointCS(ci, ej);\n for(P p: cpi){\n ans = min(ans, getangle360(c, p, ei[d]));\n }\n VP cpj = crosspointCS(cj, ei);\n for(P p: cpj){\n ans = min(ans, getangle360(c, ej[d], p));\n }\n }\n }\n } \n\n\n cout << fixed << setprecision(10);\n cout << ans*180/PI << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3608, "score_of_the_acc": -0.9971, "final_rank": 17 }, { "submission_id": "aoj_2045_2859298", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\n\n/* 幾何の基本 */\n\n#include <complex>\n\ntypedef complex<ld> Point;\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// 点の入力\nPoint input_Point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// 誤差つき等号判定\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// 内積\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// 外積\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// 直線の定義\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// 円の定義\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// ccw\n// 1: a,b,cが反時計周りの順に並ぶ\n//-1: a,b,cが時計周りの順に並ぶ\n// 2: c,a,bの順に直線に並ぶ\n//-2: a,b,cの順に直線に並ぶ\n// 0: a,c,bの順に直線に並ぶ\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,cが反時計周りの順に並ぶ\n\tif (cross(nb, nc) < -eps) return -1; // a,b,cが時計周りの順に並ぶ\n\tif (dot(nb, nc) < 0) return 2; // c,a,bの順に直線に並ぶ\n\tif (norm(nb) < norm(nc)) return -2; // a,b,cの順に直線に並ぶ\n\treturn 0; // a,c,bの順に直線に並ぶ\n}\n\n\n/* 交差判定 */\n\n// 直線と直線の交差判定\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// 直線と線分の交差判定\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// 線分と線分の交差判定\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 点の直線上判定\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// 点の線分上判定\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// 垂線の足\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);\n\treturn l.a + t * (l.b - l.a);\n}\n\n//線対象の位置にある点\nPoint reflect(const Line &l, const Point &p) {\n\tPoint pr = proj(l, p);\n\treturn pr * 2.l - p;\n}\n\n// 直線と直線の交点\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// 直線と直線の交点\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// 線分と線分の交点\n// 重なってる部分あるとassert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//先にisis_ssしてね\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// 線分と線分の交点\nvector<Point> is_ss2(const Line &s, const Line& t) {\n\tvector<Point> kouho(is_ll2(s, t));\n\tvector<Point>ans;\n\tfor (auto p : kouho) {\n\t\tif (isis_sp(s, p) && isis_sp(t, p))ans.emplace_back(p);\n\t}\n\treturn ans;\n}\n// 直線と点の距離\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n//直線と直線の距離\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// 直線と線分の距離\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// 線分と点の距離\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//直線と直線の二等分線のベクトル\nLine line_bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n//点と点の垂直二等分線 aを左に見ながら\nLine point_bisection(const Point&a, const Point&b) {\n\tconst Point cen((a + b) / 2.l);\n\tconst Point vec = (b - a)*Point(0, 1);\n\treturn Line(cen, cen + vec);\n}\n\n//三つの点からなる外心\nPoint outer_center(const vector<Point>&ps) {\n\tassert(ps.size() == 3);\n\tLine l1 = point_bisection(ps[0], ps[1]);\n\tLine l2 = point_bisection(ps[1], ps[2]);\n\treturn is_ll(l1, l2);\n}\n\n\n//三つの直線からなる内心\n//三つの直線が並行でないことは確かめといてね\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(line_bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(line_bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//三つの直線からなる傍心\n//三つの直線が並行でないことは確かめといてね\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(line_bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(line_bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:並行\n//c:並行でない\n//三つの直線から同距離の位置を求める。\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(line_bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(line_bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(line_bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* 円 */\n\n// 円と円の交点\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n/*\n点が円の中にいるか\n0 => out\n1 => on\n2 => in*/\nint is_in_Circle(const Point& p, const Circle &cir) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\nint is_in_Circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n/*\n円lcが円rcの中にいるか\n0 => out\n1 => on\n2 => in*/\nint Circle_in_Circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// 円と直線の交点\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// 円と線分の距離\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// 円と点の接線\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// 円と円の接線\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), nret.begin(), nret.end());\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//二つの円の重なり面積\nld two_Circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* 多角形 */\n\ntypedef vector<Point> Polygon;\n\n\n\n//多角形(複数の点)の最小包含円をO(N=頂点数)で求めるアルゴリズム\n//同一直線上に三つの点がないこと\n#include<random>\nCircle welzl(vector<Point>ps) {\n\tstruct solver {\n\t\tCircle solve(vector<Point>&ps, vector<Point>&rs) {\n\t\t\tif (ps.empty() || rs.size() == 3) {\n\t\t\t\tif (rs.size() == 1) {\n\t\t\t\t\treturn Circle(Point(rs[0]), 0);\n\t\t\t\t}\n\t\t\t\telse if (rs.size() == 2) {\n\t\t\t\t\treturn Circle((rs[0] + rs[1]) / 2.0l, abs(rs[1] - rs[0]) / 2);\n\t\t\t\t}\n\t\t\t\telse if (rs.size() == 3) {\n\t\t\t\t\tvector<Line> ls(3);\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tls[i] = Line(rs[i], rs[(i + 1) % 3]);\n\t\t\t\t\t}\n\t\t\t\t\tPoint center = outer_center(rs);\n\t\t\t\t\treturn Circle(center, abs(center - rs[0]));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn Circle(Point(), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\n\t\t\t\tPoint p_ba = ps.back();\n\t\t\t\tps.pop_back();\n\t\t\t\tCircle d = solve(ps, rs);\n\t\t\t\tps.push_back(p_ba);\n\t\t\t\tif (is_in_Circle(d, p_ba)) {\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\trs.push_back(p_ba);\n\t\t\t\t\tps.pop_back();\n\t\t\t\t\tauto ans = solve(ps, rs);\n\t\t\t\t\tps.push_back(p_ba);\n\t\t\t\t\trs.pop_back();\n\t\t\t\t\treturn ans;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}so;\n\tstd::random_device rd;\n\tstd::mt19937 mt(rd());\n\tshuffle(ps.begin(), ps.end(), mt);\n\tvector<Point>rs;\n\tCircle ans = so.solve(ps, rs);\n\treturn ans;\n}\n// 面積\nld get_area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tfor (int j = 0; j<n; ++j) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n//多角形の回転方向\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// 円の内外判定\n/*0 => out\n1 => on\n2 => in*/\nint is_in_Polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//円の内外判定2 高速\nenum { out, on, in };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// 凸包\n//点や線を返すことも有り得るので注意\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n//凸カット\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon q;\n\tPolygon r;\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = ps[i], b = ps[(i + 1) % n];\n\t\tLine m = Line(a, b);\n\t\tif (ccw(l.a, l.b, a) != -1) q.push_back(a);\n\t\tif (ccw(l.a, l.b, a) != 1) r.push_back(a);\n\t\tif (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0 && isis_ll(l, m)) {\n\t\t\tq.push_back(is_ll(l, m));\n\t\t\tr.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ q,r };\n\treturn polys;\n}\n\n\n\nint main() {\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\twhile (true){\n\t\tint M,N;cin>>M>>N;\n\t\tif(!M)break;\n\t\tPolygon rooms,patient;\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tint x,y;cin>>x>>y;\n\t\t\trooms.emplace_back(x,y);\n\t\t}\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint x,y;cin>>x>>y;\n\t\t\tpatient.emplace_back(x,y);\n\t\t}\n\t\t{\n\t\t\tPoint center;\n\t\t\tint x,y;cin>>x>>y;\n\t\t\tcenter=Point(x,y);\n\t\t\tfor (auto &p : rooms) {\n\t\t\t\tp-=center;\n\t\t\t}\n\t\t\tfor (auto&p : patient) {\n\t\t\t\tp-=center;\n\t\t\t}\n\t\t}\n\n\t\tld ans=2*pi;\n\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tLine l(rooms[i],rooms[(i+1)%M]);\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tPoint p(patient[j]);\n\t\t\t\tCircle c(Point(0,0),abs(p));\n\n\t\t\t\tauto ps=is_sc(c,l);\n\t\t\t\tfor (auto n : ps) {\n\t\t\t\t\tld theta1=atan2l(p.imag(),p.real());\n\t\t\t\t\tld theta2=atan2l(n.imag(),n.real());\n\t\t\t\t\tif(theta2<theta1)theta2+=2*pi;\n\n\t\t\t\t\tans=min(ans,theta2-theta1);\n\t\t\t\t}\n\t\t\t\t\n\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tLine l(patient[i], patient[(i + 1) % N]);\n\t\t\tfor (int j = 0; j < M; ++j) {\n\t\t\t\tPoint p(rooms[j]);\n\t\t\t\tCircle c(Point(0, 0), abs(p));\n\n\t\t\t\tauto ps = is_sc(c, l);\n\t\t\t\tfor (auto n : ps) {\n\t\t\t\t\tld theta1 = atan2l(p.imag(), p.real());\n\t\t\t\t\tld theta2 = atan2l(n.imag(), n.real());\n\t\t\t\t\tif (theta2<theta1)theta2 += 2 * pi;\n\n\t\t\t\t\tans = min(ans, 2*pi-(theta2 - theta1));\n\t\t\t\t}\n\n\n\t\t\t}\n\t\t}\n\t\tcout<<ans/(2*pi)*360<<endl;\n\t\t\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3284, "score_of_the_acc": -0.8851, "final_rank": 14 }, { "submission_id": "aoj_2045_2276502", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \nconst double PI=acos(-1);\nconst double PI2=PI*2.0;\nconst double eps=1e-10;\n \ntypedef complex<double> P;\ntypedef pair<P,P> S;\ntypedef pair<P,double> C;\ntypedef vector<P> vecP;\n \nbool eq(double a,double b){\n return -eps<a-b&&a-b<eps;\n}\n \ndouble Sqrt(double x){\n if(x<0)return x;\n return sqrt(x);\n}\n \nP normal( P a ){\n return a/abs(a);\n}\n \ndouble dot(P a,P b){\n return real( b*conj(a) );\n}\ndouble cross(P a,P b){\n return imag( b*conj(a) );\n}\ndouble getArg(P a,P b){\n return arg(b*conj(a));\n}\n \ndouble getTime(P a,P b){\n return ( dot(a,b) < 0 ? -1.0 : 1.0)*abs(b)/abs(a); \n}\n \nbool onSP(S s,P p){\n P a=s.first, b=s.second;\n return eq( abs(a-b) , abs(p-a)+abs(p-b) );\n}\n \nP project(P a,P b,P c){\n b-=a;c-=a;\n return a+b*real(c/b);\n}\n \ndouble getDistLP(S s,P p){\n P a=s.first, b=s.second;\n return abs( cross(b-a,p-a) / abs(b-a) );\n}\n \ndouble getDistSP(S s,P p){\n P a=s.first, b=s.second;\n if( dot(b-a,p-a) < eps )return abs(p-a);\n if( dot(a-b,p-b) < eps )return abs(p-b);\n return getDistLP(s,p);\n}\n \nbool isCrossCS(C c,S s){\n P a=s.first, b=s.second;\n double A=abs(c.second-a);\n double B=abs(c.second-b);\n double C=getDistSP(s,c.second);\n if( A < c.second+eps && B < c.second+eps )return false;\n if( c.second+eps < C )return false;\n return true;\n}\n \nvecP getCrossCL(C cir,S line){\n P a=line.first, b=line.second;\n double cr=cir.second;\n P cp=cir.first;\n vecP res;\n P base=b-a, target=project(a,b,cp);\n double length=abs(base), h=abs(cp-target);\n base/=length;\n \n if(cr+eps<h)return res;\n double w=Sqrt(cr*cr-h*h);\n double L=getTime( normal(b-a), target-a) -w , R=L+w*2.0;\n \n res.push_back( a+ base * L );\n if(eq(L,R))return res;\n res.push_back( a+ base * R );\n return res;\n}\n \nvecP getCrossCS(C cir,S s){\n vecP tmp=getCrossCL(cir,s);\n vecP res;\n for(int i=0;i<(int)tmp.size();i++)\n if( onSP(s,tmp[i]) )\n res.push_back( tmp[i] );\n \n return res;\n}\n \ndouble calc(P cen,S a,P b){\n if( eq(abs(cen-b),0) )return 1e10;\n \n vecP tp=getCrossCS( C(cen, abs(cen-b) ) , a );\n double res=1e10;\n \n for(int i=0;i<(int)tp.size();i++){\n P p=tp[i];\n double value=getArg(b-cen,p-cen);\n if( value < 0 )value+=PI2;\n res=min(res,value);\n }\n return res;\n}\n \ndouble calc2(P cen,S a,P b){\n if( eq(abs(cen-b),0) )return 1e10;\n \n vecP tp=getCrossCS( C(cen, abs(cen-b) ) , a );\n double res=1e10;\n \n for(int i=0;i<(int)tp.size();i++){\n P p=tp[i];\n double value=getArg(b-cen,p-cen);\n if( value < 0 )value+=PI2;\n res=min(res,PI2-value);\n }\n return res;\n}\n \ndouble calc(P cen,S a,S b){\n double res=1e10;\n res=min(res,calc(cen,a,b.first));\n res=min(res,calc(cen,a,b.second));\n res=min(res,calc2(cen,b,a.first));\n res=min(res,calc2(cen,b,a.second));\n return res;\n}\n \nP input(){\n int x,y;\n cin>>x>>y;\n return P(x,y);\n}\n \n \nS getS(const vecP& polygon,int index){\n int n=polygon.size();\n index%=n;\n return S(polygon[index], polygon[ (index+1) % n ]);\n}\n \nint main(){\n int M,N;\n P cen;\n while(1){\n cin>>M>>N;\n if(M==0&&N==0)break;\n vecP inpol,outpol;\n \n for(int i=0;i<M;i++){\n inpol.push_back( input() );\n }\n for(int i=0;i<N;i++){\n outpol.push_back( input() );\n }\n cen=input();\n \n double ans=1e10;\n for(int i=0;i<M;i++){\n for(int j=0;j<N;j++){\n ans=min(ans, calc(cen, getS(inpol,i) , getS(outpol,j) ) );\n }\n }\n printf(\"%.10f\\n\", min(360.0,ans/PI*180.0) );\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3384, "score_of_the_acc": -0.9185, "final_rank": 15 }, { "submission_id": "aoj_2045_2275807", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nconst double PI=acos(-1);\nconst double PI2=PI*2.0;\nconst double eps=1e-10;\n\ntypedef complex<double> P;\ntypedef pair<P,P> S;\ntypedef pair<P,double> C;\ntypedef vector<P> vecP;\n\nbool eq(double a,double b){\n return -eps<a-b&&a-b<eps;\n}\n\ndouble Sqrt(double x){\n if(x<0)return x;\n return sqrt(x);\n}\n\nP normal( P a ){\n return a/abs(a);\n}\n\ndouble dot(P a,P b){\n return real( b*conj(a) );\n}\ndouble cross(P a,P b){\n return imag( b*conj(a) );\n}\ndouble getArg(P a,P b){\n return arg(b*conj(a));\n}\n\ndouble getTime(P a,P b){\n return ( dot(a,b) < 0 ? -1.0 : 1.0)*abs(b)/abs(a); \n}\n\nbool onSP(S s,P p){\n P a=s.first, b=s.second;\n return eq( abs(a-b) , abs(p-a)+abs(p-b) );\n}\n\nP project(P a,P b,P c){\n b-=a;c-=a;\n return a+b*real(c/b);\n}\n\ndouble getDistLP(S s,P p){\n P a=s.first, b=s.second;\n return abs( cross(b-a,p-a) / abs(b-a) );\n}\n\ndouble getDistSP(S s,P p){\n P a=s.first, b=s.second;\n if( dot(b-a,p-a) < eps )return abs(p-a);\n if( dot(a-b,p-b) < eps )return abs(p-b);\n return getDistLP(s,p);\n}\n\nbool isCrossCS(C c,S s){\n P a=s.first, b=s.second;\n double A=abs(c.second-a);\n double B=abs(c.second-b);\n double C=getDistSP(s,c.second);\n if( A < c.second+eps && B < c.second+eps )return false;\n if( c.second+eps < C )return false;\n return true;\n}\n\nvecP getCrossCL(C cir,S line){\n P a=line.first, b=line.second;\n double cr=cir.second;\n P cp=cir.first;\n vecP res;\n P base=b-a, target=project(a,b,cp);\n double length=abs(base), h=abs(cp-target);\n base/=length;\n\n if(cr+eps<h)return res;\n double w=Sqrt(cr*cr-h*h);\n double L=getTime( normal(b-a), target-a) -w , R=L+w*2.0;\n\n res.push_back( a+ base * L );\n if(eq(L,R))return res;\n res.push_back( a+ base * R );\n return res;\n}\n\nvecP getCrossCS(C cir,S s){\n vecP tmp=getCrossCL(cir,s);\n vecP res;\n for(int i=0;i<(int)tmp.size();i++)\n if( onSP(s,tmp[i]) )\n res.push_back( tmp[i] );\n\n return res;\n}\n\ndouble calc(P cen,S a,P b){\n if( eq(abs(cen-b),0) )return 1e10;\n \n vecP tp=getCrossCS( C(cen, abs(cen-b) ) , a );\n double res=1e10;\n \n for(int i=0;i<(int)tp.size();i++){\n P p=tp[i];\n double value=getArg(b-cen,p-cen);\n if( value < 0 )value+=PI2;\n res=min(res,value);\n }\n return res;\n}\n\ndouble calc2(P cen,S a,P b){\n if( eq(abs(cen-b),0) )return 1e10;\n \n vecP tp=getCrossCS( C(cen, abs(cen-b) ) , a );\n double res=1e10;\n \n for(int i=0;i<(int)tp.size();i++){\n P p=tp[i];\n double value=getArg(b-cen,p-cen);\n if( value < 0 )value+=PI2;\n res=min(res,PI2-value);\n }\n return res;\n}\n\ndouble calc(P cen,S a,S b){\n double res=1e10;\n res=min(res,calc(cen,a,b.first));\n res=min(res,calc(cen,a,b.second));\n res=min(res,calc2(cen,b,a.first));\n res=min(res,calc2(cen,b,a.second));\n return res;\n}\n\nP input(){\n int x,y;\n cin>>x>>y;\n return P(x,y);\n}\n\n\nS getS(const vecP& polygon,int index){\n int n=polygon.size();\n index%=n;\n return S(polygon[index], polygon[ (index+1) % n ]);\n}\n\nint main(){\n int M,N;\n P cen;\n while(1){\n cin>>M>>N;\n if(M==0&&N==0)break;\n vecP inpol,outpol;\n \n for(int i=0;i<M;i++){\n inpol.push_back( input() );\n }\n for(int i=0;i<N;i++){\n outpol.push_back( input() );\n }\n cen=input();\n\n double ans=1e10;\n for(int i=0;i<M;i++){\n for(int j=0;j<N;j++){\n ans=min(ans, calc(cen, getS(inpol,i) , getS(outpol,j) ) );\n }\n }\n printf(\"%.10f\\n\", min(360.0,ans/PI*180.0) );\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3404, "score_of_the_acc": -0.9258, "final_rank": 16 }, { "submission_id": "aoj_2045_1094826", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <cstdio>\n#include <complex>\n#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <functional>\n#include <string>\n#include <cstring>\n#include <sstream>\n// #include <tuple>\n// #include <regex>\n \nusing namespace std;\ntypedef long long ll;\n \n#define ALL(c) (c).begin(),(c).end()\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n \n \ntypedef long double D;\nconst D EPS = 1e-8,INF = 1e12;\n \ntemplate<typename T> int sig(T a,T b=0){return a<b-EPS?-1:a>b+EPS?1:0;}\ntemplate<typename T> bool eq(T a,T b){ return sig(abs(a-b))==0;}\ntemplate<typename T> D norm(T a){ return a*a;}\n \ntypedef complex<D> P,Vec;\n#define X real()\n#define Y imag()\n \nbool debug = false;\n \n \nnamespace std{\n bool operator < (const P& a,const P& b){\n return a.X != b.X ? a.X < b.X : a.Y < b.Y;\n }\n bool operator == (const P& a,const P& b){\n return eq(a,b);\n }\n};\n \n// a×b\nD cross(const Vec& a,const Vec& b){\n return imag(conj(a)*b);\n}\n// a・b\nD dot(const Vec& a,const Vec& b) {\n return real(conj(a)*b);\n}\n//射影ベクトル\nVec proj(Vec p,Vec b) {\n return b * dot(p,b) / norm(b);\n}\n \n//直線・線分\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\n \n//直線へ射影した時の点\nP projection(const L &l, const P &p) {\n return l[0] + proj(p-l[0],l[1]-l[0]);\n}\n \nD distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\n \nstruct C {\n P o; D r;\n C(const P &o,D r) : o(o), r(r) { }\n};\n \nvector<P> intersectionLC(L l,C c){\n D d = distanceLP(l,c.o);\n vector <P> res;\n if(eq(d,c.r)){\n res.push_back(projection(l,c.o));\n }else if(sig(d,c.r)<0){\n P m = projection(l,c.o),u = (l[1]-l[0])/abs(l[1]-l[0]);\n D t = sqrt(c.r*c.r - d*d);\n res.push_back( m + t * u);res.push_back( m - t * u);\n }\n return res;\n}\n \nint ccw(const P& a,P b,P c){\n\t\tb -= a; c -= a;\n\t\tif (sig(cross(b,c))>0) return +1; // counter clockwise\n\t\tif (sig(cross(b,c))<0) return -1; // clockwise\n\t\tif (sig(dot(b,c)) < 0) return +2; // c--a--b on line\n\t\tif (sig(norm(b),norm(c))<0) return -2; // a--b--c on line\n\t\treturn 0;\n\t}\n \nvector<P> intersectionSC(L l,C c){\n vector<P> ps=intersectionLC(l,c),res;\n REP(i,ps.size()){\n if(ccw(ps[i],l[0],l[1])==+2 || ccw(ps[i],l[1],l[0])==+2){\n res.push_back(ps[i]);\n }\n }\n return res;\n}\n \n//浮動小数点modで非負の値のみかえす.\nD pfmod(D v,D M=2*M_PI){\n return fmod(fmod(v,M)+M,M);\n}\n \nint main(){\n cout <<fixed <<setprecision(20);\n cerr <<fixed <<setprecision(20);\n \n while(true){\n int m,n;cin >> m >> n;\n if(m==0 && n==0)break;\n \n vector<P> ps(m);\n REP(i,m){\n D x,y;cin >>x >> y;ps[i]=P(x,y); \n }\n vector<P> qs(n);\n REP(i,n){\n D x,y;cin >>x >> y;qs[i]=P(x,y); \n }\n P c;\n {\n D x,y;cin >>x >> y;c=P(x,y); \n }\n \n D ma = 2 * M_PI;\n REP(i,m){\n C ic(c,abs(ps[i]-c));\n REP(j,n){\n L l(qs[j],qs[(j+1)%n]);\n vector<P> ip=intersectionSC(l,ic);\n REP(k,ip.size()){\n D a=pfmod(arg(ps[i]-c)-arg(ip[k]-c));\n ma=min(ma,a);\n }\n }\n }\n REP(j,n){\n C ic(c,abs(qs[j]-c));\n REP(i,m){\n L l(ps[i],ps[(i+1)%m]);\n vector<P> ip=intersectionSC(l,ic);\n REP(k,ip.size()){\n D a=pfmod(arg(ip[k]-c)-arg(qs[j]-c));\n ma=min(ma,a);\n }\n }\n }\n \n cout << ma * 360.0/(2*M_PI)<<endl;\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1320, "score_of_the_acc": -0.1715, "final_rank": 6 }, { "submission_id": "aoj_2045_1088814", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <cstdio>\n#include <complex>\n#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <functional>\n#include <string>\n#include <cstring>\n#include <sstream>\n// #include <tuple>\n// #include <regex>\n\nusing namespace std;\ntypedef long long ll;\n\n#define ALL(c) (c).begin(),(c).end()\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n\n\ntypedef long double D;\nconst D EPS = 1e-8,INF = 1e12;\n\ntemplate<typename T> int sig(T a,T b=0){return a<b-EPS?-1:a>b+EPS?1:0;}\ntemplate<typename T> bool eq(T a,T b){ return sig(abs(a-b))==0;}\ntemplate<typename T> D norm(T a){ return a*a;}\n\ntypedef complex<D> P,Vec;\n#define X real()\n#define Y imag()\n\nbool debug = false;\n\n\nnamespace std{\n\tbool operator < (const P& a,const P& b){\n \t\treturn a.X != b.X ? a.X < b.X : a.Y < b.Y;\n \t}\n \tbool operator == (const P& a,const P& b){\n \t\treturn eq(a,b);\n \t}\n};\n\n// a×b\nD cross(const Vec& a,const Vec& b){\n\treturn imag(conj(a)*b);\n}\n// a・b\nD dot(const Vec& a,const Vec& b) {\n\treturn real(conj(a)*b);\n}\n//射影ベクトル\nVec proj(Vec p,Vec b) {\n\treturn b * dot(p,b) / norm(b);\n}\n\n//直線・線分\nstruct L : public vector<P> {\n\tL(const P &a, const P &b) {\n\t \tpush_back(a); push_back(b);\n\t }\n};\n\n//直線へ射影した時の点\nP projection(const L &l, const P &p) {\n\treturn l[0] + proj(p-l[0],l[1]-l[0]);\n}\n\nD distanceLP(const L &l, const P &p) {\n\treturn abs(p - projection(l, p));\n}\n\nstruct C {\n \tP o; D r;\n \tC(const P &o,D r) : o(o), r(r) { }\n};\n\nvector<P> intersectionLC(L l,C c){\n\tD d = distanceLP(l,c.o);\n\tvector <P> res;\n \tif(eq(d,c.r)){\n\t\tres.push_back(projection(l,c.o));\n\t}else if(sig(d,c.r)<0){\n\t\tP m = projection(l,c.o),u = (l[1]-l[0])/abs(l[1]-l[0]);\n\t \tD t = sqrt(c.r*c.r - d*d);\n\t\tres.push_back( m + t * u);res.push_back( m - t * u);\n\t}\n\treturn res;\n}\n\nint ccw(const P& a,P b,P c){\n\tb -= a; c -= a;\n\tif (cross(b,c) > EPS) return +1; \t// counter clockwise\n\tif (cross(b,c) < -EPS) return -1; \t// clockwise\n\tif (dot(b,c) < 0) return +2; // c--a--b on line\n\tif (norm(b) < norm(c)) return -2; // a--b--c on line\n\treturn 0; \t\t\t\t\t\t\t\t// a--c--b on line (or b==c)\n}\n\nvector<P> intersectionSC(L l,C c){\n\tvector<P> ps=intersectionLC(l,c),res;\n\tREP(i,ps.size()){\n\t\tif(ccw(ps[i],l[0],l[1])==+2 || ccw(ps[i],l[1],l[0])==+2){\n\t\t\tres.push_back(ps[i]);\n\t\t}\n\t}\n\treturn res;\n}\n\n//浮動小数点modで非負の値のみかえす.\nD pfmod(D v,D M=2*M_PI){\n\treturn fmod(fmod(v,M)+M,M);\n}\n\nint main(){\n\tcout <<fixed <<setprecision(20);\n\tcerr <<fixed <<setprecision(20);\n\n\twhile(true){\n\t\tint m,n;cin >> m >> n;\n\t\tif(m==0 && n==0)break;\n\n\t\tvector<P> ps(m);\n\t\tREP(i,m){\n\t\t\tD x,y;cin >>x >> y;ps[i]=P(x,y); \n\t\t}\n\t\tvector<P> qs(n);\n\t\tREP(i,n){\n\t\t\tD x,y;cin >>x >> y;qs[i]=P(x,y); \n\t\t}\n\t\tP c;\n\t\t{\n\t\t\tD x,y;cin >>x >> y;c=P(x,y); \n\t\t}\n\n\t\tD ma = 2 * M_PI;\n\t\tREP(i,m){\n\t\t\tC ic(c,abs(ps[i]-c));\n\t\t\tREP(j,n){\n\t\t\t\tL l(qs[j],qs[(j+1)%n]);\n\t\t\t\tvector<P> ip=intersectionSC(l,ic);\n\t\t\t\tREP(k,ip.size()){\n\t\t\t\t\tD a=pfmod(arg(ps[i]-c)-arg(ip[k]-c));\n\t\t\t\t\tma=min(ma,a);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tREP(j,n){\n\t\t\tC ic(c,abs(qs[j]-c));\n\t\t\tREP(i,m){\n\t\t\t\tL l(ps[i],ps[(i+1)%m]);\n\t\t\t\tvector<P> ip=intersectionSC(l,ic);\n\t\t\t\tREP(k,ip.size()){\n\t\t\t\t\tD a=pfmod(arg(ip[k]-c)-arg(qs[j]-c));\n\t\t\t\t\tma=min(ma,a);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tcout << ma * 360.0/(2*M_PI)<<endl;\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1320, "score_of_the_acc": -0.1715, "final_rank": 6 }, { "submission_id": "aoj_2045_1088813", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <cstdio>\n#include <complex>\n#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <functional>\n#include <string>\n#include <cstring>\n#include <sstream>\n// #include <tuple>\n// #include <regex>\n\nusing namespace std;\ntypedef long long ll;\n\n#define ALL(c) (c).begin(),(c).end()\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n\n\ntypedef long double D;\nconst D EPS = 1e-8,INF = 1e12;\n\ntemplate<typename T> int sig(T a,T b=0){return a<b-EPS?-1:a>b+EPS?1:0;}\ntemplate<typename T> bool eq(T a,T b){ return sig(abs(a-b))==0;}\ntemplate<typename T> D norm(T a){ return a*a;}\n\ntypedef complex<D> P,Vec;\n#define X real()\n#define Y imag()\n\nbool debug = false;\n\n\nnamespace std{\n\tbool operator < (const P& a,const P& b){\n \t\treturn a.X != b.X ? a.X < b.X : a.Y < b.Y;\n \t}\n \tbool operator == (const P& a,const P& b){\n \t\treturn eq(a,b);\n \t}\n};\n\n// a×b\nD cross(const Vec& a,const Vec& b){\n\treturn imag(conj(a)*b);\n}\n// a・b\nD dot(const Vec& a,const Vec& b) {\n\treturn real(conj(a)*b);\n}\n//射影ベクトル\nVec proj(Vec p,Vec b) {\n\treturn b * dot(p,b) / norm(b);\n}\n\n//直線・線分\nstruct L : public vector<P> {\n\tL(const P &a, const P &b) {\n\t \tpush_back(a); push_back(b);\n\t }\n};\n\n//直線へ射影した時の点\nP projection(const L &l, const P &p) {\n\treturn l[0] + proj(p-l[0],l[1]-l[0]);\n}\n\nD distanceLP(const L &l, const P &p) {\n\treturn abs(p - projection(l, p));\n}\n\nstruct C {\n \tP o; D r;\n \tC(const P &o,D r) : o(o), r(r) { }\n};\n\nvector<P> intersectionLC(L l,C c){\n\tD d = distanceLP(l,c.o);\n\tvector <P> res;\n \tif(eq(d,c.r)){\n\t\tres.push_back(projection(l,c.o));\n\t}else if(sig(d,c.r)<0){\n\t\tP m = projection(l,c.o),u = (l[1]-l[0])/abs(l[1]-l[0]);\n\t \tD t = sqrt(c.r*c.r - d*d);\n\t\tres.push_back( m + t * u);res.push_back( m - t * u);\n\t}\n\treturn res;\n}\n\n\nint ccw(const P& a,P b,P c){\n\tb -= a; c -= a;\n\tif (cross(b,c) > EPS) return +1; \t// counter clockwise\n\tif (cross(b,c) < -EPS) return -1; \t// clockwise\n\tif (dot(b,c) < 0) return +2; // c--a--b on line\n\tif (norm(b) < norm(c)) return -2; // a--b--c on line\n\treturn 0; \t\t\t\t\t\t\t\t// a--c--b on line (or b==c)\n}\n\nvector<P> intersectionSC(L l,C c){\n\tvector<P> ps=intersectionLC(l,c),res;\n\t// if(debug){\n\t// \tREP(i,ps.size()){\n\t// \t\tcerr <<ps[i]<<endl;\n\t// \t}\n\t// }\n\tREP(i,ps.size()){\n\t\t// cerr << ccw(ps[i],l[0],l[1])<<endl;\n\t\t// cerr << ccw(ps[i],l[1],l[0])<<endl;\n\t\tif(ccw(ps[i],l[0],l[1])==+2 || ccw(ps[i],l[1],l[0])==+2){\n\t\t\tres.push_back(ps[i]);\n\t\t}\n\t}\n\treturn res;\n}\n\n//浮動小数点modで非負の値のみかえす.\nD pfmod(D v,D M=2*M_PI){\n\treturn fmod(fmod(v,M)+M,M);\n}\n\nint main(){\n\tcout <<fixed <<setprecision(20);\n\tcerr <<fixed <<setprecision(20);\n\n\twhile(true){\n\t\tint m,n;cin >> m >> n;\n\t\tif(m==0 && n==0)break;\n\n\t\tvector<P> ps(m);\n\t\tREP(i,m){\n\t\t\tD x,y;cin >>x >> y;ps[i]=P(x,y); \n\t\t}\n\t\tvector<P> qs(n);\n\t\tREP(i,n){\n\t\t\tD x,y;cin >>x >> y;qs[i]=P(x,y); \n\t\t}\n\t\tP c;\n\t\t{\n\t\t\tD x,y;cin >>x >> y;c=P(x,y); \n\t\t}\n\n\t\tD ma = 2 * M_PI;\n\t\tREP(i,m){\n\t\t\tC ic(c,abs(ps[i]-c));\n\t\t\tREP(j,n){\n\t\t\t\tif(i==4 && j==2)debug=true;\n\t\t\t\tL l(qs[j],qs[(j+1)%n]);\n\t\t\t\tvector<P> ip=intersectionSC(l,ic);\n\n\t\t\t\t// if(i==4 && j==2){\n\t\t\t\t// \tcerr << i <<\" \" <<j <<endl;\n\t\t\t\t// \tcerr << c<<\" \"<<ps[i] << endl;\t\t\t\t\n\t\t\t\t// \tcerr << abs(ps[i]-c)<<endl;\n\t\t\t\t// \tcerr << l[0]<<\" \"<<l[1]<<endl;\t\n\t\t\t\t// \tcerr <<\"--\"<<endl;\t\t\t\n\t\t\t\t// \tREP(k,ip.size())cerr << ip[k]<<endl;\n\t\t\t\t// }\n\t\t\t\tREP(k,ip.size()){\n\t\t\t\t\tD a=pfmod(arg(ps[i]-c)-arg(ip[k]-c));\n\t//\t\t\t\tcerr << a <<endl;\n\t\t\t\t\tma=min(ma,a);\n\t\t\t\t}\n\n\t\t\t\t// cerr << ma<<endl;\n\t\t\t\t// cerr <<endl;\n\t\t\t\t// if(i==4 && j==2)debug=false;\n\t\t\t}\n\t\t}\n\t\tREP(j,n){\n\t\t\tC ic(c,abs(qs[j]-c));\n\t\t\tREP(i,m){\n\t\t\t\t// if(i==4 && j==2)debug=true;\n\t\t\t\tL l(ps[i],ps[(i+1)%m]);\n\t\t\t\tvector<P> ip=intersectionSC(l,ic);\n\n\t\t\t\t// if(i==4 && j==2){\n\t\t\t\t// \tcerr << i <<\" \" <<j <<endl;\n\t\t\t\t// \tcerr << c<<\" \"<<ps[i] << endl;\t\t\t\t\n\t\t\t\t// \tcerr << abs(ps[i]-c)<<endl;\n\t\t\t\t// \tcerr << l[0]<<\" \"<<l[1]<<endl;\t\n\t\t\t\t// \tcerr <<\"--\"<<endl;\t\t\t\n\t\t\t\t// \tREP(k,ip.size())cerr << ip[k]<<endl;\n\t\t\t\t// }\n\t\t\t\t\n\t\t\t\tREP(k,ip.size()){\n\t\t\t\t\tD a=pfmod(arg(ip[k]-c)-arg(qs[j]-c));\n\t\t\t\t\tma=min(ma,a);\n\t\t\t\t}\n\n\t\t\t\t// cerr << ma<<endl;\n\t\t\t\t// cerr <<endl;\n\n\t\t\t\t// if(i==4 && j==2)debug=false;\n\t\t\t}\n\t\t}\n\n\t\tcout << ma * 360.0/(2*M_PI)<<endl;\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1320, "score_of_the_acc": -0.1715, "final_rank": 6 }, { "submission_id": "aoj_2045_1046013", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8) \n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nusing namespace std;\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev から cur へ行って next へ行く際の角度を求める\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\n// p0 を p1 を基準に時計回りで回した時の p2 までの角度\ndouble ccw_angle(Point p0,Point p1,Point p2){\n double ret = M_PI - getArg(p0,p1,p2);\n //cout << ret * 180 / M_PI << endl;\n if( ccw(p1,p0,p2) == COUNTER_CLOCKWISE ) ret = 2*M_PI - ret;\n return ret;\n}\n\n\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\n \nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \nbool ccwtest(Point p,Point q,Point r) { return cross3p(p,q,r) > 0; }\n \nbool onSegment(Point p,Point q,Point r) { return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ; }\n\n\ntypedef pair<double,double> dd;\nconst double DINF = 1e20;\n#define pow2(a) ((a)*(a))\nbool LT(double a ,double b){ return !equals(a,b) && a < b; }\nbool LTE(double a,double b){ return equals(a,b) || a < b; }\n\ndd calc(double x1,double y1,double vx1,double vy1,\n double x2,double y2,double vx2,double vy2,double r){\n double VX = (vx1-vx2), X = (x1-x2), VY = (vy1-vy2), Y = (y1-y2);\n double a = pow2(VX) + pow2(VY), b = 2*(X*VX+Y*VY), c = pow2(X) + pow2(Y) - pow2(r);\n dd ret = dd(DINF,DINF);\n double D = b*b - 4 * a * c;\n\n if( LT(D,0.0) ) return ret;\n\n if( equals(a,0.0) ) {\n if( equals(b,0.0) ) return ret;\n if( LT(-c/b,0.0) ) return ret;\n ret.first = - c / b;\n return ret;\n }\n\n if( equals(D,0.0) ) D = 0;\n ret.first = ( -b - sqrt( D ) ) / ( 2 * a );\n ret.second = ( -b + sqrt( D ) ) / ( 2 * a );\n if( !equals(ret.first,ret.second) && ret.first > ret.second ) swap(ret.first,ret.second);\n return ret;\n}\n\n\nPoint c;\nPolygon poly,qoly;\n\n// type => 0 : CW, type => 1 : CCW\ndouble calc(Polygon &A,Polygon &B,int type){\n double ret = DINF;\n rep(i,A.size()){\n double cost = DINF;\n rep(j,B.size()){\n Vector e = ( B[j] - B[(j+1)%(int)B.size()] ) / abs( B[j] - B[(j+1)%(int)B.size()] );\n dd tmp = calc(c.x,c.y,0,0,\n B[j].x,B[j].y,e.x,e.y,abs(A[i]-c));\n vector<Point> vp;\n if( !equals(tmp.first ,DINF) ) vp.push_back(B[j]+e*tmp.first);\n if( !equals(tmp.second,DINF) ) vp.push_back(B[j]+e*tmp.second);\n rep(k,vp.size()) if( onSegment(B[j],B[(j+1)%(int)B.size()],vp[k]) ) {\n double theta = ccw_angle(A[i],c,vp[k]);\n cost = min(cost,(!type?theta:2*M_PI-theta));\n }\n }\n ret = min(ret,cost);\n }\n return ret;\n}\n\nvoid compute(){\n double mini = min(calc(poly,qoly,0),calc(qoly,poly,1));\n if( equals(mini,DINF) ) puts(\"360.0000000\");\n else printf(\"%.10lf\\n\",mini * 180.0 / M_PI); \n}\n\nint main(){\n int M,N;\n while( cin >> M >> N, M|N ){\n poly.resize(M); qoly.resize(N);\n rep(i,M) cin >> poly[i].x >> poly[i].y;\n rep(i,N) cin >> qoly[i].x >> qoly[i].y;\n cin >> c.x >> c.y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1348, "score_of_the_acc": -0.1754, "final_rank": 11 }, { "submission_id": "aoj_2045_1014584", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cassert>\n#include <algorithm>\n#include <complex>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\n#define EPS 1e-7\n#define INF 360\n#define PI M_PI\n\n\nint N, M;\n\ntypedef complex<double> Point;\n\nnamespace std{\n\tbool operator < (const Point &a, const Point &b){\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t}\n\tPoint operator / (const Point &p, const double &a){\n\t\treturn Point(real(p)/a, imag(p)/a);\n\t}\n\tPoint operator * (const Point &p, const double &a){\n\t\treturn Point(real(p) * a, imag(p)*a);\n\t}\n\tbool operator == (const Point &a, const Point &b){\n\t\treturn real(a) == real(b) && imag(a) == imag(b);\n\t}\n}\ntypedef vector<Point> Polygon;\nstruct Line : public vector<Point>{\n\tLine(){}\n\tLine (const Point &a, const Point &b){\n\t\tpush_back(a); push_back(b);\n\t}\n};\nstruct Circle{\n\tPoint c; double r;\n\tCircle(){}\n\tCircle(const Point &c, double r) : c(c), r(r){}\n};\n\ndouble distancePP(const Point &a, const Point &b){\n\treturn sqrt((real(a)-real(b))*(real(a)-real(b)) + (imag(a)-imag(b))*(imag(a)-imag(b)));\n}\n\ndouble cross(const Point &a, const Point &b){\n\treturn imag(conj(a)*b);\n}\n\ndouble dot(const Point &a, const Point &b){\n return real(conj(a) * b);\n}\n\nPoint projection(const Line &l, const Point &p){\n\tdouble t = dot(p-l[0], l[0] - l[1]) / norm(l[0]-l[1]);\n\treturn l[0] + t*(l[0]-l[1]);\n}\n\nvector<Point> crosspointLC(const Line &l, const Circle &c){\n\tvector<Point> ret;\n\tPoint center = projection(l, c.c);\n\tdouble d = abs(center - c.c);\n\tdouble t = sqrt(c.r * c.r - d*d);\n\tif(isnan(t)) return ret;\n\tPoint vect = (l[1] - l[0]);\n\tvect /= abs(vect);\n\tret.push_back(center - vect*t);\n\tret.push_back(center + vect * t);\n\treturn ret;\n}\n\nbool is_point_on_line(Point a, Point b, Point c) {\n\treturn abs(b-a) + EPS > max(abs(c-a), abs(c-b));\n}\n\ndouble calc(const Point &a, const Point &b, const Point &pp){\n\tdouble ta = arg(a-pp);\n\tdouble tb = arg(b-pp);\n\twhile (tb < ta + EPS) {\n\t\ttb += 2 * M_PI;\n\t}\n\treturn ((tb - ta)*180/PI);\n}\n\ndouble calc2(const Point &a, const Point &b, const Point &pp){\n\tdouble tb = arg(a-pp);\n\tdouble ta = arg(b-pp);\n\twhile (tb < ta + EPS) {\n\t\ttb += 2 * M_PI;\n\t}\n\treturn ((tb - ta)*180/PI);\n}\n\n\n\nint main(void){\n\tdouble cx, cy;\n\twhile(cin>>M>>N, M || N){\n\t\tPolygon out, in;\n\t\t\n\t\tfor(int i = 0; i < M; i++){\n\t\t\tdouble x, y;\n\t\t\tcin >> x >> y;\n\t\t\tout.push_back(Point(x, y));\n\t\t}\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tdouble x, y;\n\t\t\tcin >> x >> y;\n\t\t\tin.push_back(Point(x,y));\n\t\t}\n\t\tcin >> cx >> cy;\n\t\tdouble min_ans = INF;\n\t\tfor(int i = 0; i < in.size(); i++){\n\t\t\tPoint pp = Point(cx, cy);\n\t\t\tCircle c = Circle(pp, distancePP(pp, in[i]));\n\t\t\tfor(int j = 0; j < out.size(); j++){\n\t\t\t\tLine l = Line(out[(j+1)%out.size()], out[j]);\n\t\t\t\tvector<Point> cp = crosspointLC(l, c);\n\t\t\t\tif(cp.empty()) continue;\n\t\t\t\tdouble c1 = 360, c2 = 360;\n/*\ncout << real(in[i]) << \":\" << imag(in[i]) << \" and \"<< real(out[j]) << \":\" << imag(out[j]) << \" to \"<< real(out[j+1]) << \":\" << imag(out[j+1])<< endl;\ncout << real(cp[0]) << \":\" << imag(cp[0]) << \" and \"<< real(cp[1]) << \":\" << imag(cp[1]) << endl;\n*/\n\t\t\t\tif(is_point_on_line(out[(j+1)%out.size()], out[j], cp[0]))\n\t\t\t\t\tc1 = calc(in[i], cp[0], pp);\n\t\t\t\tif(is_point_on_line(out[(j+1)%out.size()], out[j], cp[1]))\n\t\t\t\t\tc2 = calc(in[i], cp[1], pp);\n//cout << \":\" << c1 << \" : \" << c2 << \" : \" << min_ans << endl;\n\t\t\t\tmin_ans = min(min_ans, min(c1, c2));\n\t\t\t}\n\t\t}\n\t\tswap(in, out);\n\t\tfor(int i = 0; i < in.size(); i++){\n\t\t\tPoint pp = Point(cx, cy);\n\t\t\tCircle c = Circle(pp, distancePP(pp, in[i]));\n\t\t\tfor(int j = 0; j < out.size(); j++){\n\t\t\t\tLine l = Line(out[(j+1)%out.size()], out[j]);\n\t\t\t\tvector<Point> cp = crosspointLC(l, c);\n\t\t\t\tif(cp.empty()) continue;\n\t\t\t\tdouble c1 = 360, c2 = 360;\n\t\t\t\tif(is_point_on_line(out[(j+1)%out.size()], out[j], cp[0]))\n\t\t\t\t\tc1 = calc2(in[i], cp[0], pp);\n\t\t\t\tif(is_point_on_line(out[(j+1)%out.size()], out[j], cp[1]))\n\t\t\t\t\tc2 = calc2(in[i], cp[1], pp);\n\t\t\t\tmin_ans = min(min_ans, min(c1, c2));\n\t\t\t}\n\t\t}\n\n\n\t\tprintf(\"%.9lf\\n\", min_ans);\n\t}\n\t\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1356, "score_of_the_acc": -0.1783, "final_rank": 13 }, { "submission_id": "aoj_2045_815773", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\ntypedef complex<double> Point;\nconst double EPS = 1e-6;\ndouble dot(Point a, Point b){\n return real(conj(a) * b);\n}\ndouble cross(Point a, Point b){\n return imag(conj(a) * b);\n}\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n double len = abs(b) * abs(c); // 速さが気になるときは1にする\n if (cross(b, c) > +len * EPS) return +1; // 反時計回り\n if (cross(b, c) < -len * EPS) return -1; // 時計回り\n if (dot(b, c) < 0) return +2; // c--a--b の順番で一直線上\n if (norm(b) < norm(c)) return -2; // a--b--c の順番で一直線上\n return 0; // 点が線分ab上にある\n}\n\ndouble angle(Point a, Point b){\n double res = arg(conj(a) * b);\n if(res < 0) res += 2 * M_PI;\n return res;\n}\n\nstruct Line : public vector<Point> {\n Line(const Point& a, const Point& b) {\n push_back(a); push_back(b);\n }\n Point vector() const {\n return back() - front();\n }\n};\nstruct Circle {\n Point p;\n double r;\n Circle() {}\n Circle(Point p, double r) : p(p), r(r) { }\n};\nbool intersectSP(Line s, Point p) {\n return ccw(s[0], s[1], p) == 0;\n}\n\nPoint projection(Line l, Point p){\n double t = dot(p - l[0], l.vector()) / norm(l.vector());\n return l[0] + t * l.vector();\n}\n\nvector<Point> crosspointLC(const Line &l, const Circle &c) {\n vector<Point> res;\n Point center = projection(l, c.p);\n double d = abs(center - c.p);\n double tt = c.r * c.r - d * d;\n if(tt < 0 && tt > -EPS) tt = 0;\n if(tt < 0) return res;\n double t = sqrt(tt);\n Point vect = l.vector();\n vect /= abs(vect);\n res.push_back(center - vect * t);\n if (t > EPS) {\n res.push_back(center + vect * t);\n }\n return res;\n}\n\nvector<Point> crosspointSC(const Line &s, const Circle &c) {\n vector<Point> ret;\n vector<Point> nret = crosspointLC(s, c);\n for (int i = 0; i < nret.size(); i++) {\n if (intersectSP(s, nret[i])) ret.push_back(nret[i]);\n }\n return ret;\n}\n\nint main(){\n int M, N;\n while(cin >> M >> N && M){\n vector<Point> ps(M);\n vector<Point> qs(N);\n REP(i, M){\n double x, y;\n cin >> x >> y;\n ps[i] = Point(x, y);\n }\n REP(i, N){\n double x, y;\n cin >> x >> y;\n qs[i] = Point(x, y);\n }\n double cx, cy;\n cin >> cx >> cy;\n Point cp(cx, cy);\n double ans = 2.0 * M_PI;\n for(int i = 0; i < N; i++){\n Circle C(cp, abs(qs[i] - cp)); // qs[i]の軌道\n for(int j = 0; j < M; j++){\n Line s(ps[j], ps[(j == M - 1 ? 0 : j + 1)]);\n vector<Point> crosspv = crosspointSC(s, C);\n for(Point crossp : crosspv){\n //cout << \"Circle : \" << C.p << \" \" << C.r << \" cross with \" << s[0] << \" \" << s[1] << \" at \" << crossp << endl;\n ///cout << \"angle : \" << angle(qs[i] - cp, crossp - cp) << endl;\n ans = min(ans, angle(qs[i] - cp, crossp - cp));\n }\n }\n }\n for(int i = 0; i < M; i++){\n Circle C(cp, abs(ps[i] - cp)); // ps[i]の軌道\n for(int j = 0; j < N; j++){\n Line s(qs[j], qs[(j == N - 1 ? 0 : j + 1)]);\n vector<Point> crosspv = crosspointSC(s, C);\n for(Point crossp : crosspv){\n //cout << \"Circle : \" << C.p << \" \" << C.r << \" cross with \" << s[0] << \" \" << s[1] << \" at \" << crossp << endl;\n //cout << \"angle : \" << angle(crossp - cp, ps[i] - cp) << endl;\n ans = min(ans, angle(crossp - cp, ps[i] - cp));\n }\n }\n }\n printf(\"%.7f\\n\", ans / M_PI * 180.0);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1336, "score_of_the_acc": -0.1742, "final_rank": 10 }, { "submission_id": "aoj_2045_729090", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <complex>\n#include <cmath>\n#include <cstdio>\n#include <vector>\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,P> L;\n\nconst double EPS = 1e-8;\n\nstruct C{\n P c;\n double r;\n C(P c=P(0,0), double r=0.0):c(c),r(r){}\n};\n\ndouble dot(P a, P b){return real(conj(a)*b);}\ndouble cross(P a, P b){return imag(conj(a)*b);}\nbool equal(double a, double b){return fabs(a-b) < EPS;}\n\nP proj(P p, L l){\n return l.first + dot(p - l.first, l.second - l.first) / norm(l.second - l.first) * (l.second - l.first);\n}\n\nbool isInterSP(L s, P p){\n if(dot(s.second - s.first, p - s.first) < -EPS) return equal(0.0, abs(p-s.first));\n if(dot(s.first - s.second, p - s.second) < - EPS) return equal(0.0, abs(p-s.second));\n return equal(0.0, abs(cross(s.second - s.first, p - s.first) / abs(s.second - s.first)));\n}\n\nvector<P> getInterCS(C c, L s){\n vector<P> res;\n P h = proj(c.c, s);\n double d = abs(c.c - h);\n if(d > c.r + EPS);\n else if(d > c.r - EPS){\n if(isInterSP(s, h)) res.push_back(h);\n } else {\n P v = s.second - s.first;\n v = (sqrt(c.r*c.r - d*d) / abs(v)) * v;\n if(isInterSP(s, h+v)) res.push_back(h+v);\n if(isInterSP(s, h-v)) res.push_back(h-v);\n }\n return res;\n}\n\nint n,m;\nvector<P> v[2];\nP ce;\n\ndouble calc(P a, P b1, P b2){\n double res = arg(b2-a) - arg(b1-a);\n res *= 180.0 / M_PI;\n while(res < 0) res += 360.0;\n while(res > 360.0) res -= 360.0;\n return res;\n}\n\nvoid solve(){\n double ans = 360.0;\n for(int i=0;i<m;i++){\n C c = C(ce, abs(ce-v[1][i]));\n for(int j=0;j<n;j++){\n L l = L(v[0][j], v[0][(j+1)%n]);\n vector<P> cp = getInterCS(c, l);\n for(int k=0;k<cp.size();k++)\n\tans = min(ans, calc(ce, v[1][i], cp[k]));\n }\n }\n\n for(int i=0;i<n;i++){\n C c = C(ce, abs(ce-v[0][i]));\n for(int j=0;j<m;j++){\n L l = L(v[1][j], v[1][(j+1)%m]);\n vector<P> cp = getInterCS(c, l);\n for(int k=0;k<cp.size();k++)\n\tans = min(ans, calc(ce, cp[k], v[0][i]));\n }\n }\n\n\n printf(\"%.7f\\n\",ans);\n}\n\nint main(){\n while(cin >> n >> m && (n|m)){\n double x,y;\n for(int i=0;i<2;i++) v[i].clear();\n for(int i=0;i<n;i++){\n cin >> x >> y;\n v[0].push_back(P(x,y));\n }\n for(int i=0;i<m;i++){\n cin >> x >> y;\n v[1].push_back(P(x,y));\n }\n cin >> x >> y;\n ce = P(x,y);\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1352, "score_of_the_acc": -0.1737, "final_rank": 9 }, { "submission_id": "aoj_2045_665618", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\n\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n // if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() {}\n};\ntypedef vector<P> G;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\n\nvector<P> crosspointLC(const L &l, const C &c) {\n vector<P> ret;\n P center = projection(l, c.p);\n double d = abs(center - c.p);\n double t = sqrt(c.r * c.r - d * d);\n if (isnan(t)) { return ret; }\n P vect = (l[1] - l[0]);\n vect /= abs(vect);\n ret.push_back(center - vect * t);\n if (t > EPS) {\n ret.push_back(center + vect * t);\n }\n return ret;\n}\n\nvector<P> crosspointSC(const L &s, const C &c) {\n vector<P> ret;\n vector<P> nret = crosspointLC(s, c);\n FOR(it, nret)\n if (intersectSP(s, *it))\n ret.push_back(*it);\n return ret;\n}\n\ndouble angle(const P &a, const P &b) { // テ」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツ?」ツ?凝」ツつ嘉」ツ?ソテ」ツ?淌」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツづ」ツ?ョティツァツ津・ツコツヲテ」ツ?ョティツィツ暗ァツョツ夕0,2pi)\n double ret = arg(b)-arg(a);\n return (ret>=0) ? ret : ret + 2*PI;\n}\n\nP p[10];\nP q[10];\nP c;\nint main() {\n int m,n;\n while(cin >> m >> n, m||n) {\n REP(i,m) cin >> p[i].real() >> p[i].imag(); \n REP(i,n) cin >> q[i].real() >> q[i].imag();\n cin >> c.real() >> c.imag();\n double ans = 2*PI;\n REP(i,n) {\n REP(j,m) {\n vector<P> ps = crosspointSC(L(p[j],p[(j+1)%m]), C(c,abs(q[i]-c)));\n FOR(it, ps) {\n chmin(ans, angle(q[i]-c,*it-c));\n }\n ps = crosspointSC(L(q[i],q[(i+1)%n]), C(c,abs(p[j]-c)));\n FOR(it, ps) {\n chmin(ans, angle(*it-c,p[j]-c));\n }\n }\n }\n printf(\"%.10f\\n\", ans*180/PI);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1352, "score_of_the_acc": -0.1769, "final_rank": 12 }, { "submission_id": "aoj_2045_657321", "code_snippet": "#include<algorithm>\n#include<complex>\n#include<cstdio>\n#include<iostream>\n#include<queue>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\ntemplate<class T> T at(vector<T> v, int i) {return v[i % v.size()];}\n\n#define X real()\n#define Y imag()\n\ntypedef long double D;\ntypedef complex<D> P;\nstruct L{P a, b;};\ntypedef vector<P> Pol;\n\nconst D EPS = 1e-8;\nconst D PI = acos(-1);\n\nint sig(D a, D b = 0) {return a < b - EPS ? -1 : a > b + EPS ? 1 : 0;}\nbool near(P a, P b) {return norm(a - b) < EPS;}\n\n// 比較関数\nnamespace std {\n bool operator<(P a, P b) {return sig(a.X, b.X) ? a.X < b.X : a.Y < b.Y;}\n}\n\nD sr(D a) {return sqrt(max(a, (D)0));}\n\n// 内積\nD dot(P a, P b) {return a.X * b.X + a.Y * b.Y;}\n// 外積\nD det(P a, P b) {return a.X * b.Y - a.Y * b.X;}\n\n// 線分のベクトル\nP vec(L a) {return a.b - a.a;}\n\n// 線分abに対する点cの位置\nenum CCW{FRONT = 1, RIGHT = 2, BACK = 4, LEFT = 8, ON = 16};\nint ccw(P a, P b, P c) {\n if (a == c || b == c) return ON;\n int s = sig(det(b - a, c - a));\n if (s) return s > 0 ? LEFT : RIGHT;\n return (a < b) == (b < c) ? FRONT : (c < a) == (a < b) ? BACK : ON;\n}\n\n// 有向角度\nD arg(P base, P a, P b) {\n D ang = arg((b - base) / (a - base));\n return ang > 0 ? ang : ang + 2 * PI;\n}\n\n// 射影\nP proj(P a, P b) {return a * dot(a, b) / norm(a);}\nP perp(L l, P p) {return l.a + proj(vec(l), p - l.a);}\n\n// 交点\nP pLL(L a, L b) {return a.a + vec(a) * (det(vec(b), b.a - a.a) / det(vec(b), vec(a)));}\n\n// 距離\nD dLP(L l, P p) {return abs(det(vec(l), p - l.a)) / abs(vec(l));}\n\n// 円\nstruct C{P c; D r;};\n\n// 交差判定\nbool iCL(C c, L l) {return sig(c.r, dLP(l, c.c)) >= 0;}\n\n// 交点\npair<P, P> pCL(C c, L l) {\n P x = perp(l, c.c);\n P y = vec(l) / abs(vec(l)) * sr(c.r * c.r - norm(x - c.c));\n return make_pair(x - y, x + y);\n}\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n Pol p(n), q(m);\n rep (i, n) cin >> p[i].X >> p[i].Y;\n rep (i, m) cin >> q[i].X >> q[i].Y;\n P c;\n cin >> c.X >> c.Y;\n D res = 2 * PI;\n rep (i, n) {\n\tif (near(c, p[i])) continue;\n\tC a = (C){c, abs(c - p[i])};\n\trep (j, m) {\n\t L s = (L){q[j], at(q, j + 1)};\n\t if (!iCL(a, s)) continue;\n\t pair<P, P> pp = pCL(a, s);\n\t if (ccw(s.a, s.b, pp.first) == ON) {\n\t res = min(res, arg(c, pp.first, p[i]));\n\t }\n\t if (ccw(s.a, s.b, pp.second) == ON) {\n\t res = min(res, arg(c, pp.second, p[i]));\n\t }\n\t}\n }\n swap(n, m);\n swap(p, q);\n rep (i, n) {\n\tif (near(c, p[i])) continue;\n\tC a = (C){c, abs(c - p[i])};\n\trep (j, m) {\n\t L s = (L){q[j], at(q, j + 1)};\n\t if (!iCL(a, s)) continue;\n\t pair<P, P> pp = pCL(a, s);\n\t if (ccw(s.a, s.b, pp.first) == ON) {\n\t res = min(res, arg(c, p[i], pp.first));\n\t }\n\t if (ccw(s.a, s.b, pp.second) == ON) {\n\t res = min(res, arg(c, p[i], pp.second));\n\t }\n\t}\n }\n printf(\"%.12Lf\\n\", res * 180 / PI);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1280, "score_of_the_acc": -0.1537, "final_rank": 4 }, { "submission_id": "aoj_2045_657320", "code_snippet": "#include<algorithm>\n#include<complex>\n#include<cstdio>\n#include<iostream>\n#include<queue>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\ntemplate<class T> T at(vector<T> v, int i) {return v[i % v.size()];}\n\n#define X real()\n#define Y imag()\n\ntypedef long double D;\ntypedef complex<D> P;\nstruct L{P a, b;};\ntypedef vector<P> Pol;\n\nconst D EPS = 1e-8;\nconst D PI = acos(-1);\n\nint sig(D a, D b = 0) {return a < b - EPS ? -1 : a > b + EPS ? 1 : 0;}\nbool near(P a, P b) {return norm(a - b) < EPS;}\n\n// テヲツッツ氾ィツシツεゥツ鳴「テヲツ閉ー\nnamespace std {\n bool operator<(P a, P b) {return sig(a.X, b.X) ? a.X < b.X : a.Y < b.Y;}\n bool operator<(L a, L b) {return !near(a.a, b.a) ? a.a < b.a : a.b < b.b;}\n}\n\nD sr(D a) {return sqrt(max(a, (D)0));}\n\n// テ・ツ??ァツゥツ?\nD dot(P a, P b) {return a.X * b.X + a.Y * b.Y;}\n// テ・ツ、ツ姪ァツゥツ?\nD det(P a, P b) {return a.X * b.Y - a.Y * b.X;}\n\n// テァツキツ堙・ツ按?」ツ?ョテ」ツδ凖」ツつッテ」ツδ暗」ツδォ\nP vec(L a) {return a.b - a.a;}\n\n// テァツキツ堙・ツ按?bテ」ツ?ォテ・ツッツセテ」ツ?凖」ツつ凝ァツつケcテ」ツ?ョテ、ツスツ催ァツスツョ\nenum CCW{FRONT = 1, RIGHT = 2, BACK = 4, LEFT = 8, ON = 16};\nint ccw(P a, P b, P c) {\n if (a == c || b == c) return ON;\n int s = sig(det(b - a, c - a));\n if (s) return s > 0 ? LEFT : RIGHT;\n return (a < b) == (b < c) ? FRONT : (c < a) == (a < b) ? BACK : ON;\n}\n\n// テヲツ慊嘉・ツ青妥ィツァツ津・ツコツヲ\nD arg(P base, P a, P b) {\n D ang = arg((b - base) / (a - base));\n return ang > 0 ? ang : ang + 2 * PI;\n}\nL sortBase;\nbool lessArg(P a, P b) {\n D ang1 = arg(sortBase.a, sortBase.b, a);\n D ang2 = arg(sortBase.a, sortBase.b, b);\n return sig(ang1, ang2) ? ang1 < ang2 : norm(a) > norm(b);\n}\n\n// テ・ツーツ?・ツスツア\nP proj(P a, P b) {return a * dot(a, b) / norm(a);}\nP perp(L l, P p) {return l.a + proj(vec(l), p - l.a);}\nP refl(L l, P p) {return perp(l, p) * (D)2 - p;}\n\n// テ、ツコツ、テ・ツキツョテ・ツ按、テ・ツョツ?\nbool eqL(L a, L b) {return !sig(det(vec(a), vec(b))) && !sig(det(vec(a), b.a - a.a));}\nbool iLL(L a, L b) {return sig(det(vec(a), vec(b))) || !sig(det(vec(a), b.a - a.a));}\nbool iLLs(L a, L b) {return sig(det(vec(a), vec(b)));}\nbool iLS(L a, L b) {return sig(det(vec(a), b.a - a.a)) * sig(det(vec(a), b.b - a.a)) <= 0;}\nbool iLSs(L a, L b) {return sig(det(vec(a), b.a - a.a)) * sig(det(vec(a), b.b - a.a)) < 0;}\nbool iSS(L a, L b) {\n int cwa = ccw(a.a, a.b, b.a) | ccw(a.a, a.b, b.b);\n int cwb = ccw(b.a, b.b, a.a) | ccw(b.a, b.b, a.b);\n return ((cwa | cwb) & ON) || ((cwa & cwb) == (LEFT | RIGHT));\n}\nbool iSSs(L a, L b) {\n int cwa = ccw(a.a, a.b, b.a) | ccw(a.a, a.b, b.b);\n int cwb = ccw(b.a, b.b, a.a) | ccw(b.a, b.b, a.b);\n return (cwa & cwb) == (LEFT | RIGHT);\n}\n\n// テ、ツコツ、テァツつケ\nP pLL(L a, L b) {return a.a + vec(a) * (det(vec(b), b.a - a.a) / det(vec(b), vec(a)));}\n\n// ティツキツ敕ゥツ崢「\nD dLP(L l, P p) {return abs(det(vec(l), p - l.a)) / abs(vec(l));}\nD dSP(L s, P p) {\n if (dot(vec(s), p - s.a) < 0) return abs(p - s.a);\n if (dot(vec(s), p - s.b) > 0) return abs(p - s.b);\n return dLP(s, p);\n}\nD dLL(L a, L b) {return iLL(a, b) ? 0 : dLP(a, b.a);}\nD dLS(L a, L b) {return iLS(a, b) ? 0 : min(dLP(a, b.a), dLP(a, b.b));}\nD dSS(L a, L b) {return iSS(a, b) ? 0 : min(min(dSP(a, b.a), dSP(a, b.b)), min(dSP(b, a.a), dSP(b, a.b)));}\n\n// テ・ツ??\nstruct C{P c; D r;};\n\n// 2テ・ツ??」ツ?ョテ、ツスツ催ァツスツョテゥツ鳴「テ、ツソツ?\nenum RELATION{SAME = 1, CONTAIN = 2, OVER = 4, NO_CROSS = 8, ONE_CROSS = 16, ONE_CONTAIN_CROSS = 32, ONE_OVER_CROSS = 64, TWO_CROSS = 128};\nint cRel(C c1, C c2) {\n D d = abs(c1.c - c2.c);\n if (near(c1.c, c2.c) && !sig(c1.r, c2.r)) return SAME;\n if (sig(d, c1.r - c2.r) < 0) return OVER;\n if (sig(d, c2.r - c1.r) < 0) return CONTAIN;\n if (!sig(d, c1.r - c2.r)) return ONE_OVER_CROSS;\n if (!sig(d, c2.r - c1.r)) return ONE_CONTAIN_CROSS;\n if (!sig(d, c1.r + c2.r)) return ONE_CROSS;\n if (sig(d, c1.r + c2.r) > 0) return NO_CROSS;\n return TWO_CROSS;\n}\n\n// テ、ツコツ、テ・ツキツョテ・ツ按、テ・ツョツ?\nbool iCP(C c, P p) {return sig(abs(p - c.c), c.r) <= 0;}\nbool iCL(C c, L l) {return sig(c.r, dLP(l, c.c)) >= 0;}\nbool iCS(C c, L s) {return sig(c.r, dSP(s, c.c)) >= 0;}\nbool iCSc(C c, L s) {return iCS(c, s) && sig(c.r, max(abs(s.a - c.c), abs(s.b - c.c))) >= 0;}\nbool iCC(C a, C b) {return sig(abs(a.c - b.c), a.r + b.r) <= 0;}\nbool iCCc(C a, C b) {return iCC(a, b) && sig(abs(a.c - b.c), abs(a.r - b.r)) >= 0;}\n\n// テ、ツコツ、テァツつケ\npair<P, P> pCC(C a, C b) {\n D x = (norm(a.c - b.c) + a.r * a.r - b.r * b.r) / (2 * abs(a.c - b.c));\n P e = (b.c - a.c) / abs(b.c - a.c);\n P y = e * P(0, sr(a.r * a.r - x * x));\n return make_pair(a.c + e * x - y, a.c + e * x + y);\n}\npair<P, P> pCL(C c, L l) {\n P x = perp(l, c.c);\n P y = vec(l) / abs(vec(l)) * sr(c.r * c.r - norm(x - c.c));\n return make_pair(x - y, x + y);\n}\n\n// ティツァツ津」ツ?ョテ・ツ??・ツ、ツ姪・ツ按、テ・ツョツ?ティツァツ誕bテ」ツ?ョテ・ツ??ゥツδィテ」ツ?ォテ」ツ?づ」ツつ古」ツ?ーテヲツュツ」テ」ツ??ィツセツコテ、ツクツ甘」ツ?ッ0テ」ツ??・ツ、ツ姪ゥツδィテ」ツ?ッティツイツ?\nint sAP(P a, P b, P c) {return sig(det(a, c)) - sig(det(b, c)) - sig(det(a, b));}\n\n// テ・ツ、ツ堙ィツァツ津・ツスツ「テ」ツ?ョテゥツ敖「テァツゥツ?\nD aPol(Pol vp) {\n D ret = 0;\n rep (i, vp.size()) ret += det(vp[i], at(vp, i + 1));\n return ret / 2;\n}\n\n// テ・ツ、ツ堙ィツァツ津・ツスツ「テ」ツ?ョテ・ツ??・ツ、ツ姪・ツ按、テ・ツョツ?テ・ツ??ゥツδィ:1テ」ツ??・ツ堕ィテ、ツクツ?0テ」ツ??・ツ、ツ姪ゥツδィ:-1\nint sGP(Pol pol, P p) {\n int side = -1;\n rep (i, pol.size()) {\n P p0 = pol[i] - p, p1 = at(pol, i + 1) - p;\n if (ccw(p0, p1, 0) == ON) return 0;\n if (p0.Y > p1.Y) swap(p0, p1);\n if (sig(p0.Y) <= 0 && 0 < sig(p1.Y) && sig(det(p0, p1)) > 0) side *= -1;\n }\n return side;\n}\n\n// テ・ツ?クテ・ツ個?\nPol convexHull(Pol p) {\n int m = -1, n = p.size();\n if (n < 3) return p;\n vector<P> q(n * 2);\n sort(p.begin(), p.end());\n for (int i = 0; i < n; q[++m] = p[i++])\n for (; m > 0 && ccw(q[m - 1], q[m], p[i]) != LEFT; --m);\n for (int i = n - 2, r = m; i >= 0; q[++m] = p[i--])\n for (; m > r && ccw(q[m - 1], q[m], p[i]) != LEFT; --m);\n q.resize(m);\n return q;\n}\n\n// テ・ツ?クテ・ツ個?」ツつォテ」ツδε」ツδ?\nPol convexCut(Pol p, L l) {\n vector<P> q;\n rep (i, p.size()) {\n if (ccw(l.a, l.b, p[i]) != RIGHT) q.push_back(p[i]);\n L s = {p[i], at(p, i + 1)};\n if (iLSs(l, s)) q.push_back(pLL(l, s));\n }\n return q;\n}\n\n// テァツキツ堙・ツ按?」ツつ津」ツδ榲」ツδシテ」ツつクテ」ツ?凖」ツつ?\nvector<L> merge(vector<L> s) {\n rep (i, s.size()) if (s[i].b < s[i].a) swap(s[i].a, s[i].b);\n sort(s.begin(), s.end());\n rep (i, s.size()) rep (j, i) if (iSS(s[i], s[j]) && !iLL(s[i], s[j])) {\n s[j].b = max(s[i].b, s[j].b);\n s.erase(s.begin() + i--);\n break;\n }\n return s;\n}\n\n// テァツキツ堙・ツ按?」ツつ「テ」ツδャテ」ツδウテ」ツつクテ」ツδ。テ」ツδウテ」ツδ?テゥツ堋」テ」ツ?ョテァツつケテ」ツ?クテ」ツ?ョティツセツコテ」ツ?ョテ」ツ?ソテ」ツつ津ヲツ個?」ツ?、\nvector<vector<int> > sArr(vector<L> s, vector<P> &vp) {\n s = merge(s);\n rep (i, s.size()) {\n vp.push_back(s[i].a);\n vp.push_back(s[i].b);\n }\n rep (i, s.size()) rep (j, i) if (iSS(s[i], s[j])) vp.push_back(pLL(s[i], s[j]));\n sort(vp.begin(), vp.end());\n vp.erase(unique(vp.begin(), vp.end(), near), vp.end());\n vector<vector<int> > g(vp.size());\n rep (i, s.size()) {\n vector<pair<D, int> > v;\n rep (j, vp.size()) if (ccw(s[i].a, s[i].b, vp[j]) == ON) {\n v.push_back(make_pair(norm(vp[j] - s[i].a), j));\n }\n sort(v.begin(), v.end());\n rep (j, v.size() - 1) {\n g[v[j + 1].second].push_back(v[j].second);\n g[v[j].second].push_back(v[j + 1].second);\n }\n }\n return g;\n}\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n Pol p(n), q(m);\n rep (i, n) cin >> p[i].X >> p[i].Y;\n rep (i, m) cin >> q[i].X >> q[i].Y;\n P c;\n cin >> c.X >> c.Y;\n D res = 2 * PI;\n rep (i, n) {\n\tif (near(c, p[i])) continue;\n\tC a = (C){c, abs(c - p[i])};\n\trep (j, m) {\n\t L s = (L){q[j], at(q, j + 1)};\n\t if (!iCL(a, s)) continue;\n\t pair<P, P> pp = pCL(a, s);\n\t if (ccw(s.a, s.b, pp.first) == ON) {\n\t res = min(res, arg(c, pp.first, p[i]));\n\t }\n\t if (ccw(s.a, s.b, pp.second) == ON) {\n\t res = min(res, arg(c, pp.second, p[i]));\n\t }\n\t}\n }\n swap(n, m);\n swap(p, q);\n rep (i, n) {\n\tif (near(c, p[i])) continue;\n\tC a = (C){c, abs(c - p[i])};\n\trep (j, m) {\n\t L s = (L){q[j], at(q, j + 1)};\n\t if (!iCL(a, s)) continue;\n\t pair<P, P> pp = pCL(a, s);\n\t if (ccw(s.a, s.b, pp.first) == ON) {\n\t res = min(res, arg(c, p[i], pp.first));\n\t }\n\t if (ccw(s.a, s.b, pp.second) == ON) {\n\t res = min(res, arg(c, p[i], pp.second));\n\t }\n\t}\n }\n printf(\"%.12Lf\\n\", res * 180 / PI);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1292, "score_of_the_acc": -0.1581, "final_rank": 5 }, { "submission_id": "aoj_2045_565636", "code_snippet": "#include <cstdio>\n#include <complex>\n#include <cmath>\n#include <algorithm>\n#include <vector>\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef const P &rP;\n\ndouble pi2;\n\nvector<double> intrarg(rP s, double r, rP p, rP q){\n\tvector<double> ret;\n\tP v = q - p;\n\tP w = p - s;\n\tdouble a = norm(v);\n\tdouble b = real(v) * real(w) + imag(v) * imag(w);\n\tdouble c = norm(w) - r * r;\n\tdouble D = b * b - a * c;\n\tdouble t;\n\tif(D >= 0.0){\n\t\tD = sqrt(D);\n\t\tfor(double sgn = -1.0; sgn < 2.0; sgn += 2.0){\n\t\t\tt = (-b + sgn * D) / a;\n\t\t\tif(t > 0.0 && t < 1.0){\n\t\t\t\tret.push_back(arg(w + t * v));\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\ndouble difarg(double a, double b){\n\ta -= b;\n\twhile(a < 0.0) a += pi2;\n\twhile(a >= pi2) a -= pi2;\n\treturn a;\n}\n\nint main(){\n\tpi2 = acos(-1.0) * 2.0;\n\t\n\tint m, n;\n\tdouble x, y;\n\twhile(scanf(\"%d%d\", &m, &n), m){\n\t\tvector<P> p(m + 1), q(n + 1);\n\t\tfor(int i = 0; i < m; ++i){\n\t\t\tscanf(\"%lf%lf\", &x, &y);\n\t\t\tp[i] = P(x, y);\n\t\t}\n\t\tp[m] = p[0];\n\t\tfor(int i = 0; i < n; ++i){\n\t\t\tscanf(\"%lf%lf\", &x, &y);\n\t\t\tq[i] = P(x, y);\n\t\t}\n\t\tq[n] = q[0];\n\t\tscanf(\"%lf%lf\", &x, &y);\n\t\tP c(x, y);\n\n\t\tdouble ans = pi2;\n\n\t\tfor(int i = 0; i < n; ++i){\n\t\t\tdouble r1 = abs(q[i] - c);\n\t\t\tdouble base1 = arg(q[i] - c);\n\t\t\tfor(int j = 0; j < m; ++j){\n\t\t\t\tdouble r2 = abs(p[j] - c);\n\t\t\t\tdouble base2 = arg(p[j] - c);\n\n\t\t\t\tvector<double> ret = intrarg(c, r1, p[j], p[j+1]);\n\t\t\t\tfor(int k = 0; k < ret.size(); ++k){\n\t\t\t\t\tans = min(ans, difarg(ret[k], base1));\n\t\t\t\t}\n\n\t\t\t\tret = intrarg(c, r2, q[i], q[i+1]);\n\t\t\t\tfor(int k = 0; k < ret.size(); ++k){\n\t\t\t\t\tans = min(ans, difarg(base2, ret[k]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tprintf(\"%.9f\\n\", ans / pi2 * 360.0);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.1226, "final_rank": 2 }, { "submission_id": "aoj_2045_406504", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double EPS=1e-9;\nconst double PI=acos(-1);\n\ntemplate<class T>\nstruct point{\n\tT x,y;\n\tpoint operator+(const point &a)const{ return (point){x+a.x,y+a.y}; }\n\tpoint operator-(const point &a)const{ return (point){x-a.x,y-a.y}; }\n};\n\ntemplate<class T>\npoint<T> operator*(T c,const point<T> &a){ return (point<T>){c*a.x,c*a.y}; }\n\ntemplate<class T>\npoint<double> operator/(const point<T> &a,double c){\n\treturn (point<double>){a.x/c,a.y/c};\n}\n\nbool operator<(const point<double> &a,const point<double> &b){\n\treturn a.x+EPS<b.x || abs(a.x-b.x)<EPS && a.y+EPS<b.y;\n}\n\ntemplate<class T>\nstruct line{ point<T> a,b; };\n\ntemplate<class T>\nstruct segment{\n\tpoint<T> a,b;\n\toperator line<T>()const{ return (line<T>){a,b}; }\n};\n\ntemplate<class T>\nstruct polygon:vector< point<T> >{\n\tpolygon(){}\n\tpolygon(int n):vector< point<T> >(n){}\n};\n\ntemplate<class T>\nT dot(const point<T> &a,const point<T> &b){ return a.x*b.x+a.y*b.y; }\n\ntemplate<class T>\nT cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; }\n\npoint<double> rot(const point<double> &a,double theta){\n\treturn (point<double>){a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)};\n}\n\nenum {CCW=1,CW=-1,ON=0};\nint ccw(const point<double> &a,const point<double> &b,const point<double> &c){\n\tdouble rdir=cross(b-a,c-a);\n\tif(rdir> EPS) return CCW;\n\tif(rdir<-EPS) return CW;\n\treturn ON;\n}\n\nbool cover(const polygon<double> &G,const point<double> &p){\n\tint n=G.size();\n\tbool res=false;\n\trep(i,n){\n\t\tpoint<double> v1=G[i]-p,v2=G[(i+1)%n]-p;\n\t\tif(v1.y>v2.y) swap(v1,v2);\n\t\tif(v1.y<EPS && EPS<v2.y && cross(v1,v2)>EPS) res=!res;\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS) return true;\n\t}\n\treturn res;\n}\n\nbool intersect(const segment<double> &S1,const segment<double> &S2){\n\tif(max(S1.a.x,S1.b.x)+EPS<min(S2.a.x,S2.b.x)\n\t|| max(S1.a.y,S1.b.y)+EPS<min(S2.a.y,S2.b.y)\n\t|| max(S2.a.x,S2.b.x)+EPS<min(S1.a.x,S1.b.x)\n\t|| max(S2.a.y,S2.b.y)+EPS<min(S1.a.y,S1.b.y)) return false;\n\treturn ccw(S1.a,S1.b,S2.a)*ccw(S1.a,S1.b,S2.b)<=0\n\t\t&& ccw(S2.a,S2.b,S1.a)*ccw(S2.a,S2.b,S1.b)<=0;\n}\n\npoint<double> get_intersect(const line<double> &L1,const line<double> &L2){\n\tdouble a1=cross(L1.b-L1.a,L2.b-L2.a);\n\tdouble a2=cross(L1.b-L1.a,L1.b-L2.a);\n\tif(abs(a1)<EPS) return L1.a;\n\treturn L2.a+a2/a1*(L2.b-L2.a);\n}\n\nbool cover(const polygon<double> &G,const polygon<double> &H){\n\tint m=G.size(),n=H.size();\n\n\trep(i,n){\n\t\tif(!cover(G,H[i])) return false;\n\n\t\tsegment<double> S={H[i],H[(i+1)%n]};\n\n\t\tvector< point<double> > cand; // H の辺 i と G との交点全体\n\t\tcand.push_back(S.a);\n\t\tcand.push_back(S.b);\n\t\trep(j,m){\n\t\t\tsegment<double> T={G[j],G[(j+1)%m]};\n\t\t\tif(intersect(S,T)){\n\t\t\t\tcand.push_back(get_intersect(S,T));\n\t\t\t}\n\t\t}\n\n\t\tsort(cand.begin(),cand.end());\n\t\trep(j,(int)cand.size()-1){\n\t\t\tpoint<double> p=(cand[j]+cand[j+1])/2;\n\t\t\tif(!cover(G,p)) return false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main(){\n\tfor(int m,n;scanf(\"%d%d\",&m,&n),m;){\n\t\tpolygon<double> G(m),H(n);\n\t\trep(i,m) scanf(\"%lf%lf\",&G[i].x,&G[i].y);\n\t\trep(i,n) scanf(\"%lf%lf\",&H[i].x,&H[i].y);\n\t\tpoint<double> c;\n\t\tscanf(\"%lf%lf\",&c.x,&c.y);\n\n\t\tpolygon<double> H2(n);\n\t\tint deg;\n\t\tfor(deg=1;deg<360;deg++){\n\t\t\trep(i,n) H2[i]=rot(H[i]-c,deg*PI/180)+c;\n\t\t\tif(!cover(G,H2)) break;\n\t\t}\n\n\t\tdouble lo=(deg-1)*PI/180,hi=deg*PI/180;\n\t\trep(_,30){\n\t\t\tdouble mi=(lo+hi)/2;\n\t\t\trep(i,n) H2[i]=rot(H[i]-c,mi)+c;\n\t\t\tif(cover(G,H2)) lo=mi; else hi=mi;\n\t\t}\n\t\tprintf(\"%.9f\\n\",(lo+hi)/2*180/PI);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 876, "score_of_the_acc": -0.1258, "final_rank": 3 }, { "submission_id": "aoj_2045_131631", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<complex>\n#include<vector>\n#include<cmath>\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define NEXT(i,n) ((i+1)%(n))\n#define mp make_pair\n#define pb push_back\nusing namespace std;\n\ntypedef complex<double> P;\n\nconst double pi = acos(-1);\n\npair<double,double> check(double dx,double dy,double rx,double ry,double r,\n\t\t\t bool &flag){\n double a=(dx*dx+dy*dy),b=-2*rx*dx-2*ry*dy,c=rx*rx+ry*ry-r*r;\n double D=b*b-4*a*c;\n if (D < 0)return mp(-1,-1);\n flag=true;\n return mp((-b-sqrt(D))/(2*a),(-b+sqrt(D))/(2*a));\n}\n\ninline P getnorm(P a){\n return a/abs(a);\n}\n\nbool isp(P a,P b,P c){\n return abs(a-c)+abs(b-c) < abs(a-b)+1e-10;\n}\n\n\nvoid getdata(vector<P> &p,vector<P> &e,vector<double> &a,bool flag){\n rep(i,p.size()){\n double r = abs(p[i]);\n double ori=arg(p[i]);\n rep(j,e.size()){\n bool cansolve=false;\n P norm=getnorm(e[NEXT(j,e.size())]-e[j]);\n pair<double,double> tmp=check(norm.real(),norm.imag(),-e[j].real(),-e[j].imag(),r,cansolve);\n if (!cansolve)continue;\n rep(k,2){\n\tP point=e[j]+tmp.first*norm;\n\tif (isp(e[j],e[NEXT(j,e.size())],point)){\n\t double t = arg(point);\n\t if (flag){\n\t if (t <ori)t+=2*pi;\n\t a.pb(t-ori);\n\t }else {\n\t if (ori < t)t-=2*pi;\n\t a.pb(ori-t);\n\t }\n\t}\n\tswap(tmp.first,tmp.second);\n }\n }\n }\n}\n\ndouble solve(vector<P> &p,vector<P> &e){//cx,cy=0,0\n vector<double> a;\n a.pb(2*pi);\n getdata(p,e,a,true);\n getdata(e,p,a,false);\n sort(a.begin(),a.end());\n return a[0]*180/pi;\n}\n\n\nmain(){\n int n,m;\n while(cin>>n>>m && n){\n vector<P> e(n),p(m);\n rep(i,e.size()){\n double x,y;cin>>x>>y;e[i]=P(x,y);\n }\n rep(i,p.size()){\n double x,y;cin>>x>>y;p[i]=P(x,y);\n }\n double cx,cy;\n cin>>cx>>cy;\n P minus=P(cx,cy);\n rep(i,e.size())e[i]-=minus;\n rep(i,p.size())p[i]-=minus;\n printf(\"%.7lf\\n\",solve(p,e));\n }\n\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1004, "score_of_the_acc": -0.053, "final_rank": 1 } ]
aoj_2050_cpp
Problem C: Dig or Climb Benjamin Forest VIII is a king of a country. One of his best friends Nod lives in a village far from his castle. Nod gets seriously sick and is on the verge of death. Benjamin orders his subordinate Red to bring good medicine for him as soon as possible. However, there is no road from the castle to the village. Therefore, Red needs to climb over mountains and across canyons to reach the village. He has decided to get to the village on the shortest path on a map, that is, he will move on the straight line between the castle and the village. Then his way can be considered as polyline with n points ( x 1 , y 1 ) . . . ( x n , y n ) as illustlated in the following figure. Figure 1: An example route from the castle to the village Here, x i indicates the distance between the castle and the point i , as the crow flies, and y i indicates the height of the point i . The castle is located on the point ( x 1 , y 1 ), and the village is located on the point ( x n , y n ). Red can walk in speed v w . Also, since he has a skill to cut a tunnel through a mountain horizontally, he can move inside the mountain in speed v c . Your job is to write a program to the minimum time to get to the village. Input The input is a sequence of datasets. Each dataset is given in the following format: n v w v c x 1 y 1 ... x n y n You may assume all the following: n ≤ 1,000, 1 ≤ v w , v c ≤ 10, -10,000 ≤ x i , y i ≤ 10,000, and x i < x j for all i < j . The input is terminated in case of n = 0. This is not part of any datasets and thus should not be processed. Output For each dataset, you should print the minimum time required to get to the village in a line. Each minimum time should be given as a decimal with an arbitrary number of fractional digits and with an absolute error of at most 10 -6 . No extra space or character is allowed. Sample Input 3 2 1 0 0 50 50 100 0 3 1 1 0 0 50 50 100 0 3 1 2 0 0 50 50 100 0 3 2 1 0 0 100 100 150 50 6 1 2 0 0 50 50 100 0 150 0 200 50 250 0 0 Output for the Sample Input 70.710678 100.000000 50.000000 106.066017 150.000000
[ { "submission_id": "aoj_2050_3057190", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\n#include <map>\n#include <queue>\nusing namespace std;\nconst double EPS = 1e-6;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nint in_poly(const P &p, const VP &poly){\n int n = poly.size();\n int ret = -1;\n for(int i=0; i<n; i++){\n P a = poly[i]-p;\n P b = poly[(i+1)%n]-p;\n if(a.Y > b.Y) swap(a,b);\n if(intersectSP(L(a,b), P(0,0))) return 0;\n if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;\n }\n return ret;\n}\n\npair<vector<vector<int> >, VP> arrangement(const vector<L> &l){\n vector<VP> cp(l.size());\n VP plist;\n for(int i=0; i<(int)l.size(); i++){\n for(int j=i+1; j<(int)l.size(); j++){\n if(!isParallel(l[i], l[j]) && intersectSS(l[i], l[j])){\n P cpij = crosspointLL(l[i], l[j]);\n cp[i].push_back(cpij);\n cp[j].push_back(cpij);\n plist.push_back(cpij);\n }\n }\n cp[i].push_back(l[i][0]);\n cp[i].push_back(l[i][1]);\n plist.push_back(l[i][0]);\n plist.push_back(l[i][1]);\n sort(cp[i].begin(), cp[i].end());\n cp[i].erase(unique(cp[i].begin(), cp[i].end()), cp[i].end());\n }\n sort(plist.begin(), plist.end());\n plist.erase(unique(plist.begin(), plist.end()), plist.end());\n\n int n = plist.size();\n map<P, int> conv;\n for(int i=0; i<n; i++){\n conv[plist[i]] = i;\n }\n vector<vector<int> > adj(n);\n for(int i=0; i<(int)cp.size(); i++){\n for(int j=0; j<(int)cp[i].size()-1; j++){\n int jidx = conv[cp[i][j]];\n int jp1idx = conv[cp[i][j+1]];\n adj[jidx].push_back(jp1idx);\n adj[jp1idx].push_back(jidx);\n }\n }\n for(int i=0; i<n; i++){\n sort(adj[i].begin(), adj[i].end());\n adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());\n }\n return make_pair(adj, plist);\n}\n\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n double vw,vc;\n cin >> vw >> vc;\n VP p(n);\n for(int i=0; i<n; i++){\n double x,y;\n cin >> x >> y;\n p[i] = P(x, y);\n }\n double xmin = p[0].X, xmax = p[n-1].X;\n VP poly = p;\n reverse(poly.begin(), poly.end());\n poly.push_back(P(xmin, -2e4));\n poly.push_back(P(xmax, -2e4));\n\n vector<L> l;\n for(int i=0; i<n; i++){\n if(i != n-1){\n l.push_back(L(p[i], p[i+1]));\n }\n double y = p[i].Y;\n L cut(P(xmin, y), P(xmax, y));\n VP cp;\n for(int j=0; j<n-1; j++){\n L edge(p[j], p[j+1]);\n if(!isParallel(cut, edge) && intersectSS(cut, edge)){\n cp.push_back(crosspointLL(cut, edge));\n }\n }\n cp.erase(unique(cp.begin(), cp.end()), cp.end());\n int idx = lower_bound(cp.begin(), cp.end(), p[i]) -cp.begin();\n if(idx == (int)cp.size() || !(cp[idx] == p[i])){\n continue;\n }\n if(i > 0 && p[i-1].Y > p[i].Y +EPS && idx > 0){\n l.push_back(L(cp[idx-1], cp[idx]));\n }\n if(i < n-1 && p[i].Y +EPS < p[i+1].Y && idx < (int)cp.size()-1){\n l.push_back(L(cp[idx], cp[idx+1]));\n }\n }\n sort(l.begin(), l.end());\n l.erase(unique(l.begin(), l.end()), l.end());\n pair<vector<vector<int> >, VP> ret = arrangement(l);\n vector<vector<int> > &adj = ret.first;\n VP &plist = ret.second;\n\n vector<vector<pair<int, double> > > adj2(adj.size());\n for(int i=0; i<(int)adj.size(); i++){\n adj2[i].resize(adj[i].size());\n for(int j=0; j<(int)adj[i].size(); j++){\n P mid = (plist[i] +plist[adj[i][j]])/2.0;\n double cost;\n if(in_poly(mid, poly) == 1){\n cost = abs(plist[i] -plist[adj[i][j]]) /vc;\n }else{\n cost = abs(plist[i] -plist[adj[i][j]]) /vw;\n }\n adj2[i][j] = make_pair(adj[i][j], cost);\n }\n }\n\n priority_queue<pair<double, int> > wait;\n wait.push(make_pair(0, 0));\n vector<double> mincost(adj2.size(), INF);\n mincost[0] = 0;\n while(!wait.empty()){\n double cost = -wait.top().first;\n int pos = wait.top().second;\n wait.pop();\n if(cost > mincost[pos] +EPS) continue;\n if(pos == (int)adj2.size()-1) break;\n for(int i=0; i<(int)adj2[pos].size(); i++){\n int npos = adj2[pos][i].first;\n double ncost = cost +adj2[pos][i].second;\n if(ncost +EPS < mincost[npos]){\n mincost[npos] = ncost;\n wait.push(make_pair(-ncost, npos));\n }\n }\n }\n cout << fixed << setprecision(10);\n cout << mincost[adj2.size()-1] << endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 3860, "score_of_the_acc": -1.3665, "final_rank": 15 }, { "submission_id": "aoj_2050_1171326", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n#include <complex>\n#include <assert.h>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <double,double> P;\n \nstatic const int tx[] = {+0,+1,+0,-1};\nstatic const int ty[] = {-1,+0,+1,+0};\n \nstatic const double EPS = 1e-8;\n\ntypedef complex<double> Point;\n\nnamespace std {\n bool operator < (const Point& a, const Point& b) {\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\n\ndouble cross(const Point& a, const Point& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const Point& a, const Point& b) {\n return real(conj(a)*b);\n}\n\nstruct Line : public vector<Point> {\n Line(const Point &a, const Point &b) {\n push_back(a); push_back(b);\n }\n};\n\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b, c) > 0) return +1; // counter clockwise\n if (cross(b, c) < 0) return -1; // clockwise\n if (dot(b, c) < 0) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nbool intersectSS(const Line &s, const Line &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\ndouble compute_area(const Point &l,const Point &m){\n return abs(cross(l,m)) / 2.0;\n}\n\nbool is_equal(const Point &l,const Point &m){\n return ((abs(real(l) - real(m)) < EPS) && (abs(imag(l) - imag(m) < EPS)));\n}\n\nclass State {\npublic:\n int pos;\n double cost;\n State(int pos,double cost) :\n pos(pos), cost(cost) {}\n bool operator<(const State& s) const {\n return cost < s.cost;\n }\n bool operator>(const State& s) const {\n return cost > s.cost;\n }\n};\n\ndouble dp[3001];\nvector<State> costs[3001];\n\nint main(){\n int n;\n while(~scanf(\"%d\",&n)){\n if(n == 0) break;\n\n double vw,vc; \n scanf(\"%lf %lf\",&vw,&vc);\n vector<Point> points;\n for(int i = 0; i < n; i++){\n double x,y;\n scanf(\"%lf %lf\",&x,&y);\n points.push_back(Point(x,y));\n }\n\n for(int i = 0; i <= 2000; i++){\n dp[i] = 1e10;\n costs[i].clear();\n }\n vector<double> x;\n for(int i = 0; i < n; i++){\n x.push_back(points[i].real());\n Line here2right(points[i],Point(1e10,points[i].imag()));\n for(int j = i + 1; j + 1 < n; j++){\n if(points[i].imag() + EPS >= points[j].imag()) break;\n Line target(points[j],points[j+1]);\n if(intersectSS(here2right,target)){\n x.push_back(crosspoint(here2right,target).real());\n break;\n }\n }\n\n Line here2left(Point(-1e10,points[i].imag()),points[i]);\n for(int j = i - 1; j - 1 >= 0; j--){\n if(points[i].imag() + EPS >= points[j].imag()) break;\n Line target(points[j-1],points[j]);\n if(intersectSS(here2left,target)){\n x.push_back(crosspoint(here2left,target).real());\n break;\n }\n }\n }\n sort(x.begin(),x.end());\n x.erase(unique(x.begin(),x.end()),x.end());\n\n for(int i = 0; i < n; i++){\n int from = lower_bound(x.begin(),x.end(),points[i].real() - EPS) - x.begin();\n if(i + 1 < n){\n int to = lower_bound(x.begin(),x.end(),points[i+1].real() - EPS) - x.begin(); \n costs[from].push_back(State(to,abs(points[i+1]-points[i])/vw));\n costs[to].push_back(State(from,abs(points[i+1]-points[i])/vw));\n }\n Line here2right(points[i],Point(1e10,points[i].imag()));\n for(int j = i + 1; j + 1 < n; j++){\n if(points[i].imag() + EPS >= points[j].imag()) break;\n Line target(points[j],points[j+1]);\n if(intersectSS(here2right,target)){\n Point p = crosspoint(here2right,target);\n int lhs = lower_bound(x.begin(),x.end(),points[j].real() - EPS) - x.begin(); \n int mid = lower_bound(x.begin(),x.end(),p.real() - EPS) - x.begin();\n int rhs = lower_bound(x.begin(),x.end(),points[j+1].real() - EPS) - x.begin(); \n\n costs[from].push_back(State(mid,abs(p - points[i])/vc));\n costs[mid].push_back(State(from,abs(p - points[i])/vc));\n\n costs[lhs].push_back(State(mid,abs(p - points[j])/vw));\n costs[mid].push_back(State(rhs,abs(p - points[j+1])/vw));\n\n costs[mid].push_back(State(lhs,abs(p - points[j])/vw));\n costs[rhs].push_back(State(mid,abs(p - points[j+1])/vw));\n break;\n }\n }\n\n Line here2left(Point(-1e10,points[i].imag()),points[i]);\n for(int j = i - 1; j - 1 >= 0; j--){\n if(points[i].imag() + EPS >= points[j].imag()) break;\n Line target(points[j-1],points[j]);\n if(intersectSS(here2left,target)){\n Point p = crosspoint(here2left,target);\n int rhs = lower_bound(x.begin(),x.end(),points[j].real() - EPS) - x.begin(); \n int mid = lower_bound(x.begin(),x.end(),p.real() - EPS) - x.begin();\n int lhs = lower_bound(x.begin(),x.end(),points[j-1].real() - EPS) - x.begin(); \n\n costs[from].push_back(State(mid,abs(p - points[i])/vc));\n costs[mid].push_back(State(from,abs(p - points[i])/vc));\n\n costs[rhs].push_back(State(mid,abs(p - points[j])/vw));\n costs[mid].push_back(State(rhs,abs(p - points[j])/vw));\n\n costs[mid].push_back(State(lhs,abs(p - points[j-1])/vw));\n costs[lhs].push_back(State(mid,abs(p - points[j-1])/vw));\n break;\n }\n }\n }\n \n int goal_pos = x.size() - 1;\n dp[0] = 0.0;\n priority_queue<State,vector<State>,greater<State> > que;\n que.push(State(0,0));\n\n while(!que.empty()){\n State s = que.top();\n que.pop();\n if(s.pos == goal_pos) break;\n\n for(int i = 0; i < costs[s.pos].size(); i++){\n int next_pos = costs[s.pos][i].pos;\n double next_cost = costs[s.pos][i].cost;\n if(dp[next_pos] > s.cost + next_cost){\n dp[next_pos] = s.cost + next_cost;\n que.push(State(next_pos,s.cost + next_cost));\n }\n }\n }\n printf(\"%.7lf\\n\",dp[goal_pos]);\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1572, "score_of_the_acc": -0.2968, "final_rank": 11 }, { "submission_id": "aoj_2050_949828", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\n\n// Library - template - begin\n\nclass Point{\npublic:\n double x,y;\n Point(double x = -IINF,double y = -IINF): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:y<p.y; }\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n// Library - template - end\n\n// Library - intersect - begin\nPoint crosspoint(Line l, Line m) {\n double A = cross(l.p2 - l.p1, m.p2 - m.p1);\n double B = cross(l.p2 - l.p1, l.p2 - m.p1);\n if (abs(A) < EPS && abs(B) < EPS) return m.p1; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m.p1 + (m.p2 - m.p1) * (B / A);\n}\n// Library - intersect - end\n\nint V;\ndouble vw,vc;\nPoint ps[1010];\nbool here[1010]; // here is the point\ndouble mincost[1010];\n\ninline bool LT(double a,double b) { return !equals(a,b) && a < b; }\n\nint main(){\n\n while( cin >> V, V ){\n cin >> vw >> vc;\n rep(i,V) cin >> ps[i].x >> ps[i].y;\n\n rep(i,V) mincost[i] = IINF;\n mincost[0] = 0;\n REP(i,1,V) mincost[i] = mincost[i-1] + abs(ps[i]-ps[i-1]) / vw;\n\n rep(i,V-2){\n if( !( LT(ps[i].y,ps[i+1].y) ) ) continue;\n double lower = ps[i+1].y;\n REP(k,i+2,V){\n if( !( ps[k-1].y > ps[k].y ) ) {\n lower = min(lower,ps[k].y);\n continue;\n }\n if( ps[k].y < lower ){\n double Y = max(ps[i].y,ps[k].y);\n Line line = Line(Point(-10100,Y),Point(10100,Y));\n Point sp = ps[i], gp = ps[k];\n Point np = crosspoint(line,Line(ps[i ],ps[i+1]));\n Point mp = crosspoint(line,Line(ps[k-1],ps[k]));\n double dist1 = abs(np-sp);\n double dist3 = abs(np-mp);\n double dist4 = abs(gp-mp);\n double time1 = dist1 / vw + dist3 / vc + dist4 / vw;\n if( LT(mincost[i]+time1,mincost[k]) ){\n mincost[k] = mincost[i]+time1;\n REP(j,k,V) mincost[j] = min(mincost[j],mincost[j-1] + abs(ps[j]-ps[j-1])/vw);\n }\n }\n lower = min(lower,ps[k].y);\n if( lower < ps[i].y ) break; //これ忘れてた\n }\n }\n printf(\"%.8f\\n\",mincost[V-1]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1272, "score_of_the_acc": -0.1536, "final_rank": 6 }, { "submission_id": "aoj_2050_949827", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-7)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\n\n// Library - template - begin\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = -IINF,double y = -IINF): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:y<p.y; }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return p.p1 == p1 && p.p2 == p2; }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// Library - template - end\n\n\n// Library - ccw - bingin\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\n// Library - ccw - end\n\n// Library - intersect - begin\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\n/*\nsame lineの時注意\nもしm.p1が中心でなかったときに正しい答えとならない\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]);\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n*/\nPoint crosspoint(Line l, Line m) {\n double A = cross(l.p2 - l.p1, m.p2 - m.p1);\n double B = cross(l.p2 - l.p1, l.p2 - m.p1);\n if (abs(A) < EPS && abs(B) < EPS) return m.p1; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m.p1 + (m.p2 - m.p1) * (B / A);\n}\n\n\n// Library - intersect - end\n\nint V;\ndouble vw,vc;\nPoint ps[1010];\nbool here[1010]; // here is the point\ndouble mincost[1010];\n\n// a < b\ninline bool LT(double a,double b) { return !equals(a,b) && a < b; }\n\n// a <= b\ninline bool LTE(double a,double b) { return equals(a,b) || a < b; }\n\nint main(){\n\n while( cin >> V, V ){\n cin >> vw >> vc;\n rep(i,V) cin >> ps[i].x >> ps[i].y;\n\n rep(i,V) mincost[i] = IINF;\n mincost[0] = 0;\n REP(i,1,V) mincost[i] = mincost[i-1] + abs(ps[i]-ps[i-1]) / vw;\n\n rep(i,V-2){\n if( !( LT(ps[i].y,ps[i+1].y) ) ) continue;\n double lower = ps[i+1].y;\n //cout << \"start = \" << i << endl;\n REP(k,i+2,V){\n if( !( ps[k-1].y > ps[k].y ) ) {\n //cout << \"continue \" << k << endl;\n lower = min(lower,ps[k].y);\n continue;\n }\n if( ps[k].y < lower ){\n double Y = max(ps[i].y,ps[k].y);\n Line line = Line(Point(-10100,Y),Point(10100,Y));\n Point sp = ps[i], gp = ps[k];\n Point np = crosspoint(line,Line(ps[i ],ps[i+1]));\n Point mp = crosspoint(line,Line(ps[k-1],ps[k]));\n double dist1 = abs(np-sp);\n double dist3 = abs(np-mp);\n double dist4 = abs(gp-mp);\n double time1 = dist1 / vw + dist3 / vc + dist4 / vw;\n //cout << np << \" <=> \" << mp<<endl;\n //cout << \"time1 = \" << time1 << \" :: \" << mincost[i] + time1 << endl;\n if( LT(mincost[i]+time1,mincost[k]) ){\n mincost[k] = mincost[i]+time1;\n REP(j,k,V) mincost[j] = min(mincost[j],mincost[j-1] + abs(ps[j]-ps[j-1])/vw);\n }\n }\n\n lower = min(lower,ps[k].y);\n if( lower < ps[i].y ) break;\n //if( k == V-1) cout << \"BREAK \" << k << endl;\n }\n }\n\n //rep(i,V) cout << mincost[i] << \" \"; cout << endl;\n printf(\"%.8f\\n\",mincost[V-1]);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1276, "score_of_the_acc": -0.1539, "final_rank": 7 }, { "submission_id": "aoj_2050_772879", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <vector>\n\n#define repi(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,a) repi(i,0,a)\n#define repd(i,a,b) for(int i = (a) ; i >= (b); i--)\n#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i!= (a).end(); i++)\n#define all(u) (u).begin(),(u).end()\n#define rall(u) (u).rbegin(),(u).rend()\n#define UNIQUE(u) (u).erase(unique(all(u)),(u).end)\n\n#define pb push_back\n#define mp makek_pair\n#define INF 1e9\n#define EPS 1e-9\n#define PI acos(-1.0)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\n\nint n, x[1024], y[1024];\ndouble vw, vc;\n\nconst int N = 4000;\n\ndouble mem[N];\n\nstruct edge\n{\n int v; double c;\n edge(int v, double c) : v(v), c(c) {}\n};\n\nstruct node\n{\n int v; double c;\n node(int v, double c) : v(v), c(c) {}\n bool operator <(const node& t) const {\n\treturn c > t.c;\n }\n};\n\nvector<vector<edge> > E;\n\nint main(){\n while (cin >> n && n) {\n\tcin >> vw >> vc;\n\tvw = 1.0 / vw;\n\tvc = 1.0 / vc;\n\trep(i, n)\n\t cin >> x[i] >> y[i];\n\n\tE.assign(n, vector<edge>());\n\trep(i, n - 1)\n\t E[i].pb(edge(i + 1, vw * hypot((double) (x[i + 1] - x[i]),\n\t\t\t\t\t (double) (y[i + 1] - y[i]))));\n\n\tstack<int> stk;\n\trep(i, n - 1) {\n\t if (y[i] < y[i + 1])\n\t\tstk.push(i);\n\t else if (y[i] == y[i + 1]) {\n\t\t//\n\t } else {\n\t\twhile (!stk.empty() && y[stk.top()] >= y[i + 1]) {\n\t\t int it = stk.top();\n\t\t stk.pop();\n\t\t if (y[it] == y[i + 1]) {\n\t\t\tE[it].pb(edge(i + 1, vc * (x[i + 1] - x[it])));\n\t\t\tcontinue;\n\t\t }\n\t\t double p = (double) (y[it] - y[i]) / (y[i + 1] - y[i]);\n\t\t double cx = p * x[i + 1] + (1 - p) * x[i];\n\t\t double cy = p * y[i + 1] + (1 - p) * y[i];\n\n\t\t int nv = E.size();\n\t\t E.pb(vector<edge>());\n\t\t E[it].pb(edge(nv, vc * (cx - x[it])));\n\t\t E[i].pb(edge(nv, vw * hypot((double) (cx - x[i]),\n\t\t\t\t\t\t(double) (cy - y[i]))));\n\t\t E[nv].pb(edge(i + 1, vw * hypot((double) (cx - x[i + 1]),\n\t\t\t\t\t\t (double) (cy - y[i + 1]))));\n\t\t}\n\t }\n\t}\n\n\tstk = stack<int>();\n\tfor (int i = n - 2; i >= 0; --i) {\n\t if (y[i] > y[i + 1])\n\t\tstk.push(i + 1);\n\t else if (y[i] == y[i + 1]) {\n\t\t//\n\t } else {\n\t\twhile (!stk.empty() && y[stk.top()] >= y[i]) {\n\t\t int it = stk.top();\n\t\t stk.pop();\n\t\t if (y[it] == y[i]) {\n\t\t\tE[i].pb(edge(it, vc * (x[it] - x[i])));\n\t\t\tcontinue;\n\t\t }\n\t\t double p = (double) (y[it] - y[i]) / (y[i + 1] - y[i]);\n\t\t double cx = p * x[i + 1] + (1 - p) * x[i];\n\t\t double cy = p * y[i + 1] + (1 - p) * y[i];\n\n\t\t int nv = E.size();\n\t\t E.pb(vector<edge>());\n\t\t E[nv].pb(edge(it, vc * (x[it] - cx)));\n\t\t E[i].pb(edge(nv, vw * hypot((double) (cx - x[i]),\n\t\t\t\t\t\t(double) (cy - y[i]))));\n\t\t E[nv].pb(edge(i + 1, vw * hypot((double) (cx - x[i + 1]),\n\t\t\t\t\t\t (double) (cy - y[i + 1]))));\n\t\t}\n\t }\n\t}\n\n\tfill(mem, mem + N, INF);\n\n\tpriority_queue<node> q;\n\tq.push(node(0, 0.0));\n\twhile (!q.empty()) {\n\t node tmp = q.top();\n\t int v = tmp.v;\n\t double c = tmp.c;\n\t q.pop();\n\n\t if (v == n - 1) {\n\t\tprintf(\"%.6f\\n\", c);\n\t\tbreak;\n\t }\n\t if (mem[v] <= c)\n\t\tcontinue;\n\t mem[v] = c;\n\n\t rep(i, E[v].size())\n\t\tq.push(node(E[v][i].v, c + E[v][i].c));\n\t}\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1456, "score_of_the_acc": -0.1382, "final_rank": 5 }, { "submission_id": "aoj_2050_610312", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nstruct Point{\n double x, y;\n Point(){}\n Point(double x, double y) : x(x), y(y) {}\n};\nstruct S{\n int line;\n int height;\n double cost;\n S(int l, int h, int c) :\n line(l), height(h), cost(c) {}\n bool operator < (const S& s) const {\n return cost > s.cost;\n }\n};\nint lb_index(vector<double>& v, int x){\n return lower_bound(v.begin(), v.end(), x) - v.begin();\n}\nbool valid_h(S& s, vector<Point>& ps, vector<double>& hs){\n return (s.height >= 0 && s.height < hs.size() && \n min(ps[s.line].y, ps[s.line + 1].y) <= hs[s.height] &&\n max(ps[s.line].y, ps[s.line + 1].y) >= hs[s.height] );\n}\nvoid update(priority_queue<S>& que, double cost[1000][1000], S& next){\n if(cost[next.line][next.height] > next.cost){\n cost[next.line][next.height] = next.cost;\n que.push(next);\n }\n}\nvoid print(S& s, vector<double>& hs, vector<Point>& ps){\n printf(\"line %d: (%.0lf, %.0lf) -> (%.0lf, %.0lf) | height %d: y = %.0lf | cost %lf\\n\",\n s.line, ps[s.line].x, ps[s.line].y, ps[s.line + 1].x, ps[s.line + 1].y,\n s.height, hs[s.height], s.cost);\n}\nint main(){\n int N;\n double v_w, v_c;\n while(cin >> N >> v_w >> v_c && N){ \n vector<Point> ps(N);\n REP(i, N) cin >> ps[i].x >> ps[i].y;\n vector<double> hs(N);\n REP(i, N) hs[i] = ps[i].y;\n sort(hs.begin(), hs.end());\n hs.erase(unique(hs.begin(), hs.end()), hs.end());\n int L = hs.size();\n vector< vector<int> > idxes(L); // y = hs[i]と交わる線分のインデックス\n REP(i, N - 1){\n REP(j, L){\n if(min(ps[i].y, ps[i + 1].y) <= hs[j] && hs[j] <= max(ps[i].y, ps[i + 1].y)){\n idxes[j].push_back(i);\n }\n }\n }\n /*\n REP(i, L){\n printf(\"idxes[%d]:\", i);\n debug(idxes[i].begin(), idxes[i].end());\n }\n */\n priority_queue<S> que;\n double cost[1000][1000] = {};\n REP(i, N) REP(j, L) cost[i][j] = INF;\n cost[0][lb_index(hs, ps[0].y)] = 0;\n que.push(S(0, lb_index(hs, ps[0].y), 0));\n while(!que.empty()){\n S s = que.top(); que.pop();\n //print(s, hs, ps);\n if(cost[s.line][s.height] < s.cost) continue;\n if(s.line == N - 2 && hs[s.height] == ps[s.line + 1].y){\n if(ps[s.line].y == ps[s.line + 1].y) s.cost += (ps[s.line + 1].x - ps[s.line].x) / v_w;\n printf(\"%.16lf\\n\", s.cost);\n break;\n }\n if(hs[s.height] == ps[s.line].y){\n if(s.line > 0){\n S next = s;\n next.line --;\n if(ps[next.line].y == ps[next.line + 1].y) next.cost += (ps[next.line + 1].x - ps[next.line].x) / v_w;\n update(que, cost, next);\n }\n }\n if(hs[s.height] == ps[s.line + 1].y){\n if(s.line < N - 2){\n S next = s;\n next.line ++;\n if(ps[s.line].y == ps[s.line + 1].y) next.cost += (ps[s.line + 1].x - ps[s.line].x) / v_w;\n update(que, cost, next);\n }\n }\n for(int d = -1; d <= 1; d += 2){\n S next = s;\n next.height += d;\n if(valid_h(next, ps, hs)){\n assert(ps[s.line + 1].y != ps[s.line].y);\n double C = (ps[s.line + 1].x - ps[s.line].x) / (ps[s.line + 1].y - ps[s.line].y);\n double dy = (hs[s.height + d] - hs[s.height]);\n double dx = C * dy;\n //printf(\"dx = %lf dy = %lf\\n\", dx, dy);\n next.cost += sqrt(dx*dx + dy*dy) / v_w; // ??\n update(que, cost, next);\n }\n }\n if(idxes[s.height].back() != s.line && ps[s.line + 1].y != hs[s.height] && ps[s.line].y < ps[s.line + 1].y){\n S next = s;\n next.line = *upper_bound(idxes[s.height].begin(), idxes[s.height].end(), s.line);\n //printf(\"go tonnnel -> %d\\n\", next.line);\n double x1 = ps[s.line].x + (ps[s.line + 1].x - ps[s.line].x) * (hs[s.height] - ps[s.line].y) / (ps[s.line + 1].y - ps[s.line].y);\n double x2 = ps[next.line].x +\n ((ps[next.line + 1].y == ps[next.line].y) ? 0 : (ps[next.line + 1].x - ps[next.line].x) * (hs[next.height] - ps[next.line].y) / (ps[next.line + 1].y - ps[next.line].y));\n next.cost += (x2 - x1) / v_c; // ???\n update(que, cost, next);\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 10532, "score_of_the_acc": -1.3115, "final_rank": 14 }, { "submission_id": "aoj_2050_572049", "code_snippet": "#include<cmath>\n#include<queue>\n#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double EPS=1e-7;\nconst double INF=1e77;\n\nstruct point{\n\tdouble x,y;\n\tpoint():x(0),y(0){}\n\tpoint(double x,double y):x(x),y(y){}\n\tpoint operator+(const point &a)const{ return point(x+a.x,y+a.y); }\n\tpoint operator-(const point &a)const{ return point(x-a.x,y-a.y); }\n\tbool operator< (const point &a)const{ return x+EPS<a.x || abs(x-a.x)<EPS && y+EPS<a.y; }\n};\n\npoint operator*(double c,const point &a){ return point(c*a.x,c*a.y); }\n\ndouble dot(const point &a,const point &b){ return a.x*b.x+a.y*b.y; }\n\ndouble cross(const point &a,const point &b){ return a.x*b.y-a.y*b.x; }\n\ndouble abs(const point &a){ return sqrt(a.x*a.x+a.y*a.y); }\n\nstruct line{\n\tpoint a,b;\n\tline(const point &a,const point &b):a(a),b(b){}\n};\n\nstruct segment{\n\tpoint a,b;\n\tsegment(const point &a,const point &b):a(a),b(b){}\n};\n\nbool cover(const segment &S,const point &p){\n\treturn abs(cross(S.a-p,S.b-p))<EPS && dot(S.a-p,S.b-p)<EPS;\n}\n\npoint get_intersect(const line &L1,const line &L2){\n\tdouble a1=cross(L1.b-L1.a,L2.b-L2.a);\n\tdouble a2=cross(L1.b-L1.a,L1.b-L2.a);\n\tif(abs(a1)<EPS) return L1.a;\n\treturn L2.a+a2/a1*(L2.b-L2.a);\n}\n\nstruct edge{ int v; double cost; };\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tdouble v1,v2; scanf(\"%lf%lf\",&v1,&v2);\n\t\tpoint p[2000];\n\t\trep(i,n) scanf(\"%lf%lf\",&p[i].x,&p[i].y);\n\n\t\tvector<edge> G[2000];\n\t\tint n2=0;\n\t\trep(i,n){\n\t\t\t// dig right\n\t\t\tif(i<n-1 && p[i].y<p[i+1].y){\n\t\t\t\tint j=i+1;\n\t\t\t\twhile(j<n && p[i].y<p[j].y) j++;\n\t\t\t\tif(j<n){\n\t\t\t\t\tp[n+n2]=get_intersect(line(p[j-1],p[j]),line(p[i],p[i]+point(1,0)));\n\t\t\t\t\tG[i].push_back((edge){n+n2,abs(p[i]-p[n+n2])/v2});\n\t\t\t\t\tn2++;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// dig left\n\t\t\tif(i>0 && p[i].y<p[i-1].y){\n\t\t\t\tint j=i-1;\n\t\t\t\twhile(j>=0 && p[i].y<p[j].y) j--;\n\t\t\t\tif(j>=0){\n\t\t\t\t\tp[n+n2]=get_intersect(line(p[j+1],p[j]),line(p[i],p[i]+point(1,0)));\n\t\t\t\t\tG[n+n2].push_back((edge){i,abs(p[i]-p[n+n2])/v2});\n\t\t\t\t\tn2++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\trep(i,n-1){\n\t\t\tvector< pair<point,int> > Q;\n\t\t\trep(j,n+n2){\n\t\t\t\tif(cover(segment(p[i],p[i+1]),p[j])) Q.push_back(make_pair(p[j],j));\n\t\t\t}\n\t\t\tsort(Q.begin(),Q.end());\n\t\t\trep(j,(int)Q.size()-1){\n\t\t\t\tint u=Q[j].second,v=Q[j+1].second;\n\t\t\t\tG[u].push_back((edge){v,abs(Q[j+1].first-Q[j].first)/v1});\n\t\t\t}\n\t\t}\n\n\t\tdouble d[2000];\n\t\trep(u,n+n2) d[u]=(u==0?0:INF);\n\n\t\tpriority_queue< pair<double,int> > Q;\n\t\tQ.push(make_pair(0,0));\n\t\twhile(!Q.empty()){\n\t\t\tdouble d_now=-Q.top().first;\n\t\t\tint u=Q.top().second; Q.pop();\n\n\t\t\tif(d[u]<d_now-EPS) continue;\n\n\t\t\trep(i,G[u].size()){\n\t\t\t\tint v=G[u][i].v;\n\t\t\t\tdouble cost=G[u][i].cost;\n\t\t\t\tif(d[v]>d[u]+cost+EPS){\n\t\t\t\t\td[v]=d[u]+cost;\n\t\t\t\t\tQ.push(make_pair(-d[v],v));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.9f\\n\",d[n-1]);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1336, "score_of_the_acc": -0.2252, "final_rank": 8 }, { "submission_id": "aoj_2050_405863", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <list>\n#include <cmath>\n#include <fstream>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <set>\n#include <map>\n#include <complex>\n#include <iterator>\n#include <cstdlib>\n#include <cstring>\n#include <sstream>\n\nusing namespace std;\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a) - (b)) < EPS)\n#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))\n\ntypedef complex<double> P;\ntypedef pair<P,P> Edge;\ntypedef long long ll;\n\nconst double PI=4*atan(1.0);\nconst int MAX_SIZE = 10000;\n\n// 内積\ndouble dot(P a, P b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n// 外積\ndouble cross(P a, P b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n// 点cが直線ab上にあるかないか\nint is_point_on_line(P a, P b, P c) {\n return EQ( cross(b-a, c-a), 0.0 );\n}\n// 2直線の直行判定\nint is_orthogonal(P a1, P a2, P b1, P b2) {\n return EQ( dot(a1-a2, b1-b2), 0.0 );\n}\n// 2直線の平行判定\nint is_parallel(P a1, P a2, P b1, P b2) {\n return EQ( cross(a1-a2, b1-b2), 0.0 );\n}\n// 点a,bを通る直線と点cの間の距離\ndouble distance_l_p(P a, P b, P c) {\n return abs(cross(b-a, c-a)) / abs(b-a);\n}\n// 点a,bを端点とする線分と点cとの距離\ndouble distance_ls_p(P a, P b, P c) {\n if ( dot(b-a, c-a) < EPS ) return abs(c-a);\n if ( dot(a-b, c-b) < EPS ) return abs(c-b);\n return abs(cross(b-a, c-a)) / abs(b-a);\n}\n// 点が多角形の中に存在するかどうか\nbool isInPolygon(vector<P> &points,P s){\n\tif(points.size()==1)return false;\n\tvector<P> v;\n\tfor(int i=0;i<(int)points.size();i++)v.push_back(points[i]-s);\n\tint sign=0;\n\tfor(int i=1;i<=(int)v.size();i++){\n\t\tint prv=(i-1+v.size())%v.size();\n\t\tint cur=i%v.size();\n\t\tdouble c=cross(v[prv],v[cur]);\n\t\tif(EQ(c,0))continue;\n\t\telse if(sign==0){\n\t\t\tif(c>0)sign=1;\n\t\t\telse sign=-1;\n\t\t}\n\t\telse{\n\t\t\tif(sign==-1&&c>0)return false;\n\t\t\telse if(sign==1&&c<0)return false;\n\t\t}\n\t}\n\treturn true;\n}\nstruct Rec{\n\tvector<P> p;\n};\n// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定\nint is_intersected_ls(P a1, P a2, P b1, P b2) {\n // 線分が平行な場合は重なっていないことにする\n if(abs(cross(a2-a1,b2-b1)) < EPS){\n return 0;\n }\n return ( cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS ) &&\n ( cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < EPS );\n}\n// aとbの矩形が接しているかどうか(どちらかがどちからを含んでいるものもふくむ\nbool isTouchRectangle(Rec &a,Rec &b){\n // aの各頂点がbの中に存在しているかチェック\n for(int i=0;i<4;i++)if(isInPolygon(b.p,a.p[i]))return true;\n // 逆\n for(int i=0;i<4;i++)if(isInPolygon(a.p,b.p[i]))return true;\n // 各辺が他方のどれかの辺と接しているか\n for(int i=0;i<4;i++){\n int curi=i;\n int prvi=(i-1+4)%4;\n for(int j=0;j<4;j++){\n int curj=j;\n int prvj=(j-1+4)%4;\n if(is_intersected_ls(a.p[curi],a.p[prvi],b.p[curj],b.p[prvj]))return true;\n }\n }\n return false;\n}\n// a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算\nP intersection_ls(P a1, P a2, P b1, P b2) {\n P b = b2-b1;\n double d1 = abs(cross(b, a1-b1));\n double d2 = abs(cross(b, a2-b1));\n double t = d1 / (d1 + d2);\n return a1 + (a2-a1) * t;\n}\n// a1,a2を通る直線とb1,b2を通る直線の交差判定\nint is_intersected_l(P a1, P a2, P b1, P b2) {\n return !EQ( cross(a1-a2, b1-b2), 0.0 );\n}\n// a1,a2を通る直線とb1,b2を通る直線の交点計算\nP intersection_l(P a1, P a2, P b1, P b2) {\n P a = a2 - a1; P b = b2 - b1;\n return a1 + a * cross(b, b1-a1) / cross(b, a);\n}\n// 単位ベクトルを求める\nP normalVector(P p){\n\treturn p/abs(p);\n}\n// 法線ベクトルを求める\nP unitVector(P a){\n\treturn P(-a.imag(),a.real());\n}\n// 単位法線ベクトルを求める\nP unitNormalVector(P tmp){\n\tP e=P(-tmp.imag(),tmp.real());\n\te/=abs(e);\n\treturn e;\n}\n// 座標の回転(座標pにある点を,半時計回りにa(ラジアン)回転)\nP roundPoint(P p,double a){\n\treturn P(cos(a)*p.real()-sin(a)*p.imag(),sin(a)*p.real()+cos(a)*p.imag());\n}\n/*\n円周と線分の交差判定\n*/\nbool isCircleCrossLine(P a,P b,P c,double r){\n\tdouble d1 = abs(a-c);\n\tdouble d2 = abs(b-c);\n // 線分が中に含まれるとき、ここのコメントアウトをはずせばtrue(交差)となる\n\t//if(d1<r&&d2<r)\n\t//\treturn true;\n\tdouble d = distance_ls_p(a,b,c);\n\treturn (EQ(d,r)||d<r);\n}\n// 三角形の内部に点があるかどうか\n// 外積の正負がすべて同じなら内部に点あり\nbool isInTriangle(P p1,P p2,P p3,P s){\n\tP a=p1-s;\n\tP b=p2-s;\n\tP c=p3-s;\n\treturn ((cross(a,b)>0&&cross(b,c)>0&&cross(c,a)>0)||(cross(a,b)<0&&cross(b,c)<0&&cross(c,a)<0));\n}\n// 矩形の中に点が存在するかどうか\nbool isInRectangle(P p1,P p2,P p3,P p4,P s){\n\tP a=p1-s;\n\tP b=p2-s;\n\tP c=p3-s;\n P d=p4-s;\n\treturn ((cross(a,b)>0&&cross(b,c)>0&&cross(c,d)>0&&cross(d,a)>0)\n ||(cross(a,b)<0&&cross(b,c)<0&&cross(c,d)<0&&cross(d,a)<0));\n}\n// 三角形の面積を座標から計算\ndouble calcAreaOfTriangle(P a,P b,P c){\n\treturn abs((b.real()-a.real())*(c.imag()-a.imag()) - (c.real()-a.real())*(b.imag()-a.imag()))/2;\n}\n// 与えられた円の範囲内に点が存在するかどうか\nbool isContainingDot(P c,double r,P a){\n\treturn (((c.real()-a.real())*(c.real()-a.real())\n\t\t+(c.imag()-a.imag())*(c.imag()-a.imag())<r*r)\n\t\t||EQ((c.real()-a.real())*(c.real()-a.real())\n\t\t+(c.imag()-a.imag())*(c.imag()-a.imag()),r*r));\n}\n// 多角形の面積公式\ndouble calcPolygonArea(vector<P> p){\n\tdouble sum=0;\n\tfor(int i = 0; i < p.size(); i++)\n\t\tsum+=cross(p[i],p[(i+1)%(p.size())]);\n\treturn abs(sum/2);\n}\n// 2ベクトル間の角度\n// aからbへ左周りで何度か(0->2*PI)\ndouble diffAngle(P a,P b){\n double angle=atan2(cross(a,b),dot(a,b));\n if(angle<0)\n return 2*PI+angle;\n return angle;\n}\n// 2つのベクトルの重なっている部分の長さを返す\n// もし重なっていなければ0を返す\ndouble multipleLength(P a,P b,P c,P d){\n Edge e1=make_pair(a,b);\n Edge e2=make_pair(c,d);\n // 平行であるかどうか\n if(!(is_parallel(e1.first,e2.first,e1.second,e2.second)\n &&is_parallel(e1.first,e1.second,e2.first,e2.second)))\n return 0;\n double dist=0;\n // 両方乗っている\n if(EQ(distance_ls_p(e1.first,e1.second,e2.first),0)&&EQ(distance_ls_p(e1.first,e1.second,e2.second),0))\n dist=abs(e2.first-e2.second);\n else if(EQ(distance_ls_p(e2.first,e2.second,e1.first),0)&&EQ(distance_ls_p(e2.first,e2.second,e1.second),0))\n dist=abs(e1.first-e1.second);\n else if(EQ(distance_ls_p(e1.first,e1.second,e2.first),0)){\n // どちらが線上にあるか\n if(EQ(distance_ls_p(e2.first,e2.second,e1.first),0))\n dist=abs(e1.first-e2.first);\n else\n dist=abs(e1.second-e2.first);\n }\n else if(EQ(distance_ls_p(e1.first,e1.second,e2.second),0)){\n if(EQ(distance_ls_p(e2.first,e2.second,e1.first),0))\n dist=abs(e1.first-e2.second);\n else\n dist=abs(e1.second-e2.second);\n }\n return dist;\n}\n// 2点を通る半径rの円の中点を求める\npair<P,P> calcCircleCenterPoint(P dot1,P dot2,double r){\n double v=abs(dot1-dot2);\n double x = sqrt(r*r - (v/2)*(v/2));\n // 二組の単位法線ベクトル\n P hose[2];\n double x1=-(dot1.imag()-dot2.imag());\n double y1=(dot1.real() - dot2.real());\n hose[0]=P(x1,y1);\n double y2=-y1;\n double x2=-x1;\n hose[1]=P(x2,y2);\n double dd1=abs(hose[0]);\n double dd2=abs(hose[1]);\n hose[0]/=dd1;hose[0]*=x;\n hose[1]/=dd2;hose[1]*=x;\n P tmp=(dot1+dot2);\n tmp/=2;\n hose[0]+=tmp;\n hose[1]+=tmp;\n return make_pair(hose[0],hose[1]);\n}\n// 4線分が端点以外でクロスしているかどうかを判定\nbool checkCross(const vector<Edge> &vec){\n for(int i=0;i<vec.size();i++){\n for(int j=i+1;j<vec.size();j++){\n // 線分がクロスしているかどうか\n if(is_intersected_ls(vec[i].first,vec[i].second,vec[j].first,vec[j].second)){\n // している場合、端点以外でクロスしているなら、false\n P p=intersection_ls(vec[i].first,vec[i].second,vec[j].first,vec[j].second);\n if(!(EQ(p,vec[i].first)||EQ(p,vec[i].second)))\n return false;\n }\n }\n }\n return true;\n}\n// 引き数で与えられた4つの点が矩形を作るどうかを判定\nbool checkRec(const vector<P> &squ){\n vector<int> v;\n for(int i=0;i<squ.size();i++)v.push_back(i);\n do{\n vector<Edge> ve;\n for(int i=0;i<4;i++)\n ve.push_back(make_pair(squ[v[i]],squ[v[(i+1)%4]]));\n // ここですべての線分がクロスするかどうかを計算\n if(checkCross(ve)){\n // 線分の長さを確認\n if(EQ(abs(ve[0].first-ve[0].second),abs(ve[2].first-ve[2].second)))\n if(EQ(abs(ve[1].first-ve[1].second),abs(ve[3].first-ve[3].second)))\n // 直行\n if(is_orthogonal(ve[0].first,ve[0].second,ve[1].first,ve[1].second))\n if(is_orthogonal(ve[2].first,ve[2].second,ve[3].first,ve[3].second))\n return true;\n }\n }while(next_permutation(v.begin(),v.end()));\n return false;\n}\n// 引き数で与えられた4つの点が正方形を作るかどうかを判定\nbool checkSqu(const vector<P> &squ){\n vector<int> v;\n for(int i=0;i<squ.size();i++)v.push_back(i);\n do{\n vector<Edge> ve;\n for(int i=0;i<4;i++)\n ve.push_back(make_pair(squ[v[i]],squ[v[(i+1)%4]]));\n // ここですべての線分がクロスするかどうかを計算\n if(checkCross(ve)){\n // 線分の長さを確認\n double d=abs(ve[0].first-ve[0].second);\n for(int i=1;i<4;i++){\n double d2=abs(ve[i].first-ve[i].second);\n if(!EQ(d,d2))return false;\n }\n // 直行確認\n if(is_orthogonal(ve[0].first,ve[0].second,ve[1].first,ve[1].second))\n if(is_orthogonal(ve[2].first,ve[2].second,ve[3].first,ve[3].second))\n return true;\n }\n }while(next_permutation(v.begin(),v.end()));\n return false;\n}\n// 二円の共通接線を構成する4直線を求める(二円はお互い接したりどちらかをふくんだりしない)\nvector<pair<P,P> > calcCommonTangentialLine(P p1,double r1,P p2,double r2){\n\tvector<pair<P,P> > res;\n\tif(r1>r2){\n\t\tswap(r1,r2);\n\t\tswap(p1,p2);\n\t}\n\t// 共通外接線\n\t{\n\t\tdouble d=abs(p1-p2);\n\t\tdouble a=abs(r1-r2);\n\t\tdouble b=sqrt(d*d-a*a);\n\t\tdouble sita=acos(b/d);\n\t\tP e=normalVector(roundPoint((roundPoint((p2-p1),sita)),PI/2));\n\t\tres.push_back(make_pair(e*r1+p1,e*r2+p2));\n\t\tP e2=normalVector(roundPoint((roundPoint((p2-p1),-sita)),-PI/2));\n\t\tres.push_back(make_pair(e2*r1+p1,e2*r2+p2));\n\t}\n\t// 共通内接線\n\t{\n\t\tP cp=(r1*p1+r2*p2)/(r1+r2);\n\t\tdouble u=abs(cp-p1);\n\t\tdouble sita=acos(r1/u);\n\t\tP e=normalVector(roundPoint(cp-p1,sita));\n\t\tres.push_back(make_pair(e*r1+p1,-e*r2+p2));\n\t\tP e2=normalVector(roundPoint(cp-p1,-sita));\n\t\tres.push_back(make_pair(e2*r1+p1,-e2*r2+p2));\n\t}\n\treturn res;\n}\n/*\n 与えられた3点a,b,c,をa->b->cと進むとき\n a->bで時計方向に折れて,b->c\n a->bで半時計方向に折れて,b->c\n a->bで逆を向いてaを通り越してb->c\n a->bでそのままb->c\n a->bで逆を向いてb->c\n のどれであるかを判定\n*/\nint ccw(P a,P b, P c){\n b-=a;c-=a;\n if(cross(b,c)>0&&!EQ(cross(b,c),0))return 1;// counter clockwise\n if(cross(b,c)<0&&!EQ(cross(b,c),0))return -1;// clockwise\n if(dot(b,c)<0&&!EQ(dot(b,c),0))return 2; // c-a-b on line\n if(!EQ(abs(b),abs(c))&&abs(b)<abs(c))return -2; // a-b-c on line\n return 0;\n}\n// 凸多角形の切断を行う\n// lineのfirst側から見て右の多角形が返る\n// 多角形は上が+y,右が+xの座標系で座標を時計周りに格納したvectorで表す\n// lineは基本的にmakeDivisorで作成\nvector<P> convex_cut(vector<P> polygon,pair<P,P> line){\n vector<P> resPolygon;\n int n=polygon.size();\n for(int i=0;i<n;i++){\n P A=polygon[i];\n P B=polygon[(i+1)%n];\n if(ccw(line.first,line.second,A)!=-1)\n resPolygon.push_back(A);\n if(ccw(line.first,line.second,A)*ccw(line.first,line.second,B)<0)\n resPolygon.push_back(intersection_l(A,B,line.first,line.second));\n }\n return resPolygon;\n}\n\nstruct Point{\n P p;\n Point(P _p){\n p=_p;\n }\n Point(){}\n bool operator<(const Point &op)const{\n if(EQ(p.real(),op.p.real())){\n if(EQ(p.imag(),op.p.imag()))return false;\n else return p.imag()<op.p.imag();\n }\n return p.real()<op.p.real();\n }\n};\nstruct edge{\n double dist;\n Point to;\n};\n\nconst int MAX_N=10000;\nconst int INF=1000000000;\nvector<edge> G[MAX_N];\ndouble d[MAX_N];\nvector<P> ps;\nset<Point> nodes;\nmap<Point,int> m;\nint idx;\nint n;\n\ntypedef pair<double,int> pii;\n\ndouble dijkstra(){\n for(int i=0;i<idx;i++)d[i]=INF;\n priority_queue<pii> pq;\n pq.push(pii(0,m[Point(ps[0])]));\n // start\n d[m[Point(ps[0])]]=0;\n while(pq.size()){\n pii p=pq.top();pq.pop();\n double cost=p.first;\n int node=p.second;\n if(!EQ(d[node],cost)&&d[node]<cost)continue;\n for(int i=0;i<G[node].size();i++){\n edge &e=G[node][i];\n if(!EQ(d[m[e.to]],cost+e.dist)&&d[m[e.to]]>cost+e.dist){\n\td[m[e.to]]=cost+e.dist;\n\tpq.push(pii(d[m[e.to]],m[e.to]));\n }\n }\n }\n // ゴールへの最短距離\n return d[m[Point(ps[n-1])]];\n}\n\nint main(){\n while(cin>>n&&n){\n double vw,vc;\n idx=0;ps.clear();m.clear();nodes.clear();\n cin>>vw>>vc;\n for(int i=0;i<n;i++){\n double x,y;\n cin>>x>>y;\n ps.push_back(P(x,y));\n }\n for(int i=0;i<n;i++){\n if(m.count(Point(ps[i]))==0){\n\tnodes.insert(Point(ps[i]));\n\tm[Point(ps[i])]=idx++;\n }\n // 谷の端点の場合、左右に伸ばす\n // 右 \n if((i+1<n&&!EQ(ps[i+1].imag(),ps[i].imag())&&(ps[i+1].imag()>ps[i].imag()))){\n\tfor(int j=i+1;j<n-1;j++){\n\t P a=ps[j];\n\t P b=ps[j+1];\n\t // 線分a--bとy=ps[i].imag()が交差するか?\n\t // 交差するなら,nodeを追加\n\t if(is_intersected_ls(a,b,P(-1000000,ps[i].imag()),P(1000000,ps[i].imag()))){\n\t P cp=intersection_ls(a,b,P(-1000000,ps[i].imag()),P(1000000,ps[i].imag()));\n\t if(m.count(Point(cp))==0){\n\t m[Point(cp)]=idx++;\n\t nodes.insert(Point(cp));\n\t }\n\t break;\n\t }\n\t // 平行かつy座標が同じ線分にぶつかった場合\n\t else if(EQ(ps[i].imag(),ps[j].imag())&&EQ(ps[j].imag(),ps[j+1].imag()))break;\n\t}\n }\n // 左\n if(i-1>=0&&!EQ(ps[i-1].imag(),ps[i].imag())&&(ps[i-1].imag()>ps[i].imag())){\n\tfor(int j=i-1;j>0;j--){\n\t P a=ps[j];\n\t P b=ps[j-1];\n\t // 線分a--bとy=ps[i].imag()が交差するか?\n\t // 交差するなら,nodeを追加\n\t if(is_intersected_ls(a,b,P(-1000000,ps[i].imag()),P(1000000,ps[i].imag()))){\n\t P cp=intersection_ls(a,b,P(-1000000,ps[i].imag()),P(1000000,ps[i].imag()));\n\t if(m.count(Point(cp))==0){\n\t nodes.insert(Point(cp));\n\t m[Point(cp)]=idx++;\n\t }\n\t break;\n\t }\n\t else if(EQ(ps[i].imag(),ps[j].imag())&&EQ(ps[j].imag(),ps[j-1].imag()))break;\n\t}\n }\n }\n // 交点の位置でソートしておき\n // 一つ隣の交点は通行可能\n // その他同じy座標を持つところにも通行可能\n for(set<Point>::iterator it=nodes.begin();it!=nodes.end();it++){\n //cout<<it->p<<endl;\n // 歩いていけるところにエッジを結ぶ\n set<Point>::iterator nxt=it;nxt++;\n if(nxt!=nodes.end()){\n\tedge e;\n\t// 隣接地点へ歩いて渡る\n\te.dist=abs((it->p)-(nxt->p))/vw;\n\te.to=*it;G[m[*nxt]].push_back(e);\n\te.to=*nxt;G[m[*it]].push_back(e);\n }\n // 同じy座標のノードを追加\n // x座標で隣接するものを使用\n int ang=-1;\n for(set<Point>::iterator iit=it;iit!=nodes.end();iit++){\n\t// 最初は一つ進めておく\n\tif(iit==it)iit++;\n\tif(iit==nodes.end())break;\n\tif(EQ(iit->p.imag(),it->p.imag())){\n\t set<Point>::iterator itnxt=it;itnxt++;\n\t // 隣は歩いていけるのでx\n\t if(itnxt!=iit){\n\t if(ang==0)continue;\n\t edge e;\n\t e.dist=abs((iit->p)-(it->p))/vc;\n\t e.to=*iit;\n\t G[m[*it]].push_back(e);\n\t }\n\t break;\n\t}\n\tif(ang==-1){\n\t if(iit->p.imag()>it->p.imag())ang=1;//zouka\n\t else ang=0;//heru\n\t}\n }\n // ang=-1;\n // for(set<Point>::iterator iit=it;;iit--){\n // \tif(iit==nodes.begin())break;\n // \t// 最初は一つ進めておく\n // \tif(iit==it)iit--;\n // \t// y座標が一致\n // \tif(EQ(iit->p.imag(),it->p.imag())){\n // \t set<Point>::iterator itnxt=it;itnxt--;\n // \t // 隣は歩いていけるのでx\n // \t if(itnxt!=iit){\n // \t if(ang==0)continue;\n // \t edge e;\n // \t e.dist=abs((iit->p)-(it->p))/vc;\n // \t e.to=*iit;\n // \t G[m[*it]].push_back(e);\n // \t }\n // \t break;\n // \t}\n // \tif(ang==-1){\n // \t if(iit->p.imag()>it->p.imag())ang=0;//zouka\n // \t else ang=1;//heru\n // \t}\n // \tif(iit==nodes.begin())break;\n // }\n }\n double res=dijkstra();\n printf(\"%.10f\\n\",res);\n for(int i=0;i<idx;i++)G[i].clear();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 0, "score_of_the_acc": -0.2787, "final_rank": 10 }, { "submission_id": "aoj_2050_361775", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n\nusing namespace std;\n\nint main(){\n\tint n;\n\tint x[1000], y[1000];\n\tdouble xd[3000], yd[3000], dist[3000];\n\tint next[3000];\n\twhile(cin >> n, n){\n\t\tint vw, vc; cin >> vw >> vc;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tcin >> x[i] >> y[i];\n\t\t\txd[i] = x[i];\n\t\t\tyd[i] = y[i];\n\t\t\tnext[i] = -1;\n\t\t}\n\t\tint size = n;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif(i+1 < n && y[i] < y[i+1]){\n\t\t\t\tfor(int j=i+1;j+1<n;j++){\n\t\t\t\t\tif(y[j+1] == y[i]){\n\t\t\t\t\t\tnext[i] = j+1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif(min(y[j], y[j+1]) <= y[i] && y[i] <= max(y[j], y[j+1])){\n\t\t\t\t\t\txd[size] = (y[i]-yd[j])*(xd[j+1]-xd[j])/(yd[j+1]-yd[j]) + xd[j];\n\t\t\t\t\t\tyd[size] = y[i];\n\t\t\t\t\t\tnext[i] = size;\n\t\t\t\t\t\tnext[size] = -1;\n\t\t\t\t\t\tsize++;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(i-1 >= 0 && y[i] < y[i-1]){\n\t\t\t\tfor(int j=i-1;j-1>=0;j--){\n\t\t\t\t\tif(y[j-1] == y[i]){\n\t\t\t\t\t\tnext[j-1] = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif(min(y[j], y[j-1]) <= y[i] && y[i] <= max(y[j], y[j-1])){\n\t\t\t\t\t\txd[size] = (y[i]-yd[j])*(xd[j-1]-xd[j])/(yd[j-1]-yd[j]) + xd[j];\n\t\t\t\t\t\tyd[size] = y[i];\n\t\t\t\t\t\tnext[size] = i;\n\t\t\t\t\t\tsize++;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector< pair<double, int> > vp;\n\t\tfor(int i=0;i<size;i++){\n\t\t\tdist[i] = 1e12;\n\t\t\tvp.push_back(make_pair(xd[i], i));\n\t\t}\n\t\tsort(vp.begin(), vp.end());\n\t\tdist[0] = 0;\n\t\tfor(int i=0;i+1<vp.size();i++){\n\t\t\tint pos = vp[i].second;\n\t\t\tint npos = vp[i+1].second;\n\t\t\tdist[npos] = min(dist[npos], dist[pos] + hypot(xd[pos]-xd[npos], yd[pos]-yd[npos])/vw);\n\t\t\tif(next[pos] != -1)\n\t\t\t\tdist[next[pos]] = min(dist[next[pos]], dist[pos] + (xd[next[pos]] - xd[pos])/vc);\n\t\t}\n\t\tprintf(\"%.8lf\\n\", dist[n-1]);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2050_350125", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > 0) return +1; // counter clockwise\n if (cross(b, c) < 0) return -1; // clockwise\n if (dot(b, c) < 0) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\ndouble dp[1001];\nP p[1001];\nint main() {\n int n;\n while(cin >> n, n) {\n int vw, vc;\n cin >> vw >> vc;\n REP(i, n) {\n int x, y;\n cin >> x >> y;\n p[i] = P(x,y);\n }\n REP(i, n) dp[i] = INF;\n dp[0] = 0;\n REP(i, n-1) {\n //cout << dp[i] << \" \";\n dp[i+1] = min(dp[i+1], dp[i] + abs(p[i] - p[i+1]) / vw);\n if (p[i+1].imag() > p[i].imag()) {\n int mi = p[i+1].imag();\n for (int j=i+2; j<n; ++j) {\n if (mi > p[j].imag()) {\n mi = p[j].imag();\n if (mi < p[i].imag()) {\n P q = crosspoint(L(p[i], p[i]+P(1,0)), L(p[j-1], p[j]));\n dp[j] = min(dp[j], dp[i] + (q.real()-p[i].real()) / vc + abs(p[j]-q) / vw);\n break;\n } else {\n P q = crosspoint(L(p[i], p[i+1]), L(p[j], p[j]+P(1,0)));\n dp[j] = min(dp[j], dp[i] + abs(p[i]-q) / vw + (p[j].real()-q.real()) / vc);\n }\n }\n }\n }\n }\n printf(\"%.8f\\n\", dp[n-1]);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2050_294796", "code_snippet": "#include <iostream>\n#include <vector>\n#include <complex>\n#include <utility>\n#include <cstdio>\n#include <cassert>\n#include <algorithm>\n\nusing namespace std;\n\ntypedef complex<double> Vector;\ntypedef pair<Vector,Vector> Line;\n\nconst double EPS = 1e-7;\n\nnamespace std {\n bool operator < (const Vector &a, const Vector &b) {\n return a.real() < b.real();\n }\n};\n\ninline bool eqv(double a, double b) {\n return fabs(a-b) < EPS;\n}\n\nstruct Comp {\n vector<Vector> vs;\n Comp(const vector<Vector> &vs) : vs(vs) {}\n\n bool operator ()(const int a, const int b) const {\n if(eqv(vs[a].real(), vs[b].real())) return a < b;\n return vs[a] < vs[b];\n }\n};\n\n\n//Verified PKU 2812\ndouble cross(const Vector &v1, const Vector &v2) {\n return (v1.real()*v2.imag()) - (v1.imag()*v2.real());\n}\n\nbool intersect(const Vector &p11, const Vector &p12, const Vector &p21, const Vector &p22) {\n return (cross(p12-p11, p21-p11)*cross(p12-p11, p22-p11) < 0 &&\n cross(p22-p21, p11-p21)*cross(p22-p21, p12-p21) < 0 );\n}\n\n//Verified AOJ 0012\n//Verified PKU 3348\ndouble ccw(const Vector &p1, const Vector &p2, const Vector &p3) {\n return cross(p2-p1, p3-p1);\n}\n\n//Verified AOJ 1267(maybe too weak)\nVector crosspoint(const Line &l1, const Line &l2) {\n double a = cross(l1.second-l1.first, l2.second-l2.first);\n double b = cross(l1.second-l1.first, l1.second-l2.first);\n if(fabs(a) < EPS && fabs(b) < EPS) return l2.first;\n assert(fabs(a) >= EPS);\n return l2.first + b / a * (l2.second-l2.first);\n}\n\nint main() {\n while(true) {\n int N;\n cin >> N;\n if(!N) break;\n\n int VW, VC;\n cin >> VW >> VC;\n\n vector<Vector> vs(N);\n vector<vector<double> > cost(N*3, vector<double>(N*3, 1e12));\n for(int i = 0; i < N; ++i) {\n cin >> vs[i].real() >> vs[i].imag();\n }\n\n for(int i = N-1; i >= 0; --i) {\n //Search Right\n double y = vs[i].imag();\n if(i+1 < N && vs[i+1].imag() > y) {\n for(int j = i+1; j < N; ++j) {\n if(vs[j].imag() < y || eqv(vs[j].imag(), y)) {\n //intersect with line (vs[j], vs[j-1])\n //cout << \"intersect \" << vs[i] << ' ' << vs[j] << endl;\n Vector pt = crosspoint(Line(vs[j], vs[j-1]+(vs[j-1]-vs[j])), Line(vs[i], Vector(vs[j].real()+10, y)));\n vs.push_back(pt);\n int idx = (int)vs.size()-1;\n cost[idx][i] = (pt.real() - vs[i].real()) / VC;\n break;\n }\n }\n }\n\n //Search Left\n if(i-1 >= 0 && vs[i-1].imag() > y) {\n for(int j = i-1; j >= 0; --j) {\n if(vs[j].imag() < y || eqv(vs[j].imag(), y)) {\n //intersect with line (vs[j+1], vs[j])\n Vector pt = crosspoint(Line(vs[j+1], vs[j]+(vs[j]-vs[j+1])), Line(vs[i], Vector(vs[j].real()-10, y)));\n vs.push_back(pt);\n int idx = (int)vs.size()-1;\n cost[i][idx] = (vs[i].real() - pt.real()) / VC;\n break;\n }\n }\n }\n }\n\n vector<int> order(vs.size());\n for(int i = 0; i < order.size(); ++i) order[i] = i;\n sort(order.begin(), order.end(), Comp(vs));\n\n vector<double> best(vs.size(), 1e12);\n best[order[vs.size()-1]] = 0;\n for(int i = (int)vs.size()-1; i >= 1; --i) {\n int cur = order[i];\n int next = order[i-1];\n cost[cur][next] = abs(vs[next]-vs[cur]) / VW;\n\n for(int j = 0; j < i; ++j) {\n int nx = order[j];\n assert(cost[cur][nx] >= 0);\n best[nx] = min(best[nx], best[cur] + cost[cur][nx]);\n }\n //cout << best[cur] << endl;\n }\n\n printf(\"%.7f\\n\", best[order[0]]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 0, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_2050_292136", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <queue>\n#include <complex>\nusing namespace std;\ntypedef complex<double> P;\nstatic const double EPS = 1e-6;\ninline double cross(const P& a, const P& b) { return a.real()*b.imag() - b.real()*a.imag(); }\n\nstruct line/*{{{*/\n{\n P a, b;\n line() {}\n line(const P& p, const P& q) : a(p), b(q) {}\n};/*}}}*/\n\nstruct segment/*{{{*/\n{\n P a, b;\n segment() {}\n segment(const P& x, const P& y) : a(x), b(y) {}\n\n inline bool intersects(const line& ln) const\n {\n return cross(ln.b - ln.a, a - ln.a) * cross(ln.b - ln.a, b - ln.a) < EPS;\n }\n\n inline P intersection(const line& ln) const\n {\n const P x = b - a;\n const P y = ln.b - ln.a;\n return a + x*(cross(y, ln.a - a))/cross(y, x);\n }\n};/*}}}*/\n\nint main()\n{\n int N;\n while (scanf(\"%d\", &N) != EOF && N != 0) {\n int vw, vc;\n scanf(\"%d %d\", &vw, &vc);\n vector<P> ps;\n for (int i = 0; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n ps.push_back(P(x, y));\n }\n\n vector<vector<pair<int,double> > > g(3*N);\n for (int i = 0; i < N-1; i++) {\n g[3*i].push_back(make_pair(3*(i+1), abs(ps[i+1] - ps[i])/vw));\n }\n for (int i = 0; i < N; i++) {\n if (i > 0 && ps[i-1].imag() > ps[i].imag()) {\n // left edge\n const line ln(ps[i], ps[i] + P(1, 0));\n for (int j = i-2; j >= 0; j--) {\n const segment s(ps[j], ps[j+1]);\n if (s.intersects(ln)) {\n const P p = s.intersection(ln);\n const int n = 3*i+1;\n g[3*j].push_back(make_pair(n, abs(p - ps[j])/vw));\n g[n].push_back(make_pair(3*i, abs(ps[i] - p)/vc));\n break;\n }\n }\n }\n if (i < N-1 && ps[i].imag() < ps[i+1].imag()) {\n // right edge\n const line ln(ps[i], ps[i] + P(1, 0));\n for (int j = i+1; j < N-1; j++) {\n const segment s(ps[j], ps[j+1]);\n if (s.intersects(ln)) {\n const P p = s.intersection(ln);\n const int n = 3*i+2;\n g[3*i].push_back(make_pair(n, abs(p - ps[i])/vc));\n g[n].push_back(make_pair(3*(j+1), abs(ps[j+1] - p)/vw));\n break;\n }\n }\n }\n }\n\n static const double INF = 1e10;\n vector<double> dist(3*N, INF);\n dist[0] = 0;\n priority_queue<pair<double,int> > q;\n q.push(make_pair(0, 0));\n while (!q.empty()) {\n const double c = -q.top().first;\n const int n = q.top().second;\n q.pop();\n if (n == 3*(N-1)) {\n printf(\"%.6f\\n\", c);\n break;\n }\n if (c > dist[n]) {\n continue;\n }\n for (vector<pair<int,double> >::const_iterator it = g[n].begin(); it != g[n].end(); ++it) {\n const int m = it->first;\n const double cc = c + it->second;\n if (cc < dist[m]) {\n dist[m] = cc;\n q.push(make_pair(-cc, m));\n }\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2050_132887", "code_snippet": "#include <iostream>\n#include <complex>\n#include <vector>\n#include <iomanip>\n#include <queue>\nusing namespace std;\n\ntypedef complex<double> P;\n#define EPS 1e-5\n#define INF 1e7\n\ntypedef pair<int, double> Node;\n\nclass State\n{\npublic:\n\tint n;\n\tdouble c;\n\tState(int n, double c)\n\t\t:n(n),c(c)\n\t{}\n\n\tbool operator<(const State& s) const\n\t{\n\t\treturn c>s.c;\n\t}\n};\n\n//ü•ª\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\n\n\n//ŠOÏ\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\n\ndouble dist(const P& a, const P& b)\n{\n\treturn abs(a-b);\n}\n\nbool intersect(L& a, L& b)\n{\n\tdouble t=max(a[0].imag(), a[1].imag());\n\tdouble l=min(a[0].imag(), a[1].imag());\n\n\treturn (t+EPS>b[0].imag()&&b[0].imag()>l-EPS);\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nint main()\n{\n\tcout << setprecision(10) << setiosflags(ios::fixed);\n\tdouble vw,vc;\n\tint N;\n\t\n\twhile(cin >> N, N)\n\t{\n\t\tvector<P> in;\n\n\t\tcin >> vw >> vc;\n\t\tfor(int i=0; i<N; i++)\n\t\t{\n\t\t\tdouble x,y;\n\t\t\tcin >> x >> y;\n\t\t\tin.push_back(P(x,y));\n\t\t}\n\n\t\tvector<Node> node[4000];\n\t\tfor(int i=0; i<N-1; i++)\n\t\t{\n\t\t\tnode[i].push_back(make_pair(i+1, dist(in[i], in[i+1])/vw));\n\t\t}\n\n\t\tfor(int i=0; i<N; i++)\n\t\t{\n\t\t\tL hz(P(-INF,in[i].imag()),P(INF,in[i].imag()));\n\t\t\tfor(int j=i-2; j>=0; j--)\n\t\t\t{\n\t\t\t\tif(in[i].imag()+EPS > in[j+1].imag()) break;\n\t\t\t\tL tmp(in[j], in[j+1]);\n\t\t\t\tif(intersect(tmp,hz))\n\t\t\t\t{\n\t\t\t\t\tP xp=crosspoint(hz,tmp);\n\t\t\t\t\tin.push_back(xp);\n\t\t\t\t\tint p=in.size();\n\t\t\t\t\tnode[j].push_back(make_pair(p, dist(in[j], xp)/vw));\n\t\t\t\t\tnode[p].push_back(make_pair(j+1, dist(in[j+1], xp)/vw));\n\t\t\t\t\tnode[p].push_back(make_pair(i, dist(in[i], xp)/vc));\n\t\t\t\t\t\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j=i+1; j<N-1; j++)\n\t\t\t{\n\t\t\t\tif(in[i].imag()+EPS > in[j].imag()) break;\n\n\t\t\t\tL tmp(in[j], in[j+1]);\n\t\t\t\tif(intersect(tmp,hz))\n\t\t\t\t{\n\t\t\t\t\tP xp=crosspoint(hz,tmp);\n\t\t\t\t\tin.push_back(xp);\n\t\t\t\t\tint p=in.size();\n\t\t\t\t\tnode[j].push_back(make_pair(p, dist(in[j], xp)/vw));\n\t\t\t\t\tnode[p].push_back(make_pair(j+1, dist(in[j+1], xp)/vw));\n\t\t\t\t\tnode[i].push_back(make_pair(p, dist(in[i], xp)/vc));\n\t\t\t\t\t\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tpriority_queue<State> q;\n\t\tq.push(State(0,0));\n\t\tbool v[4000]={0};\n\n\t\twhile(!q.empty())\n\t\t{\n\t\t\tState s=q.top(); q.pop();\n\t\t\tif(v[s.n]) continue;\n\t\t\tv[s.n]=1;\n\n\t\t\tif(s.n==N-1)\n\t\t\t{\n\t\t\t\tcout << s.c << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tfor(int i=0; i<node[s.n].size(); i++)\n\t\t\t{\n\t\t\t\tNode next=node[s.n][i];\n\t\t\t\tif(v[next.first]) continue;\n\n\t\t\t\tq.push(State(next.first, s.c+next.second));\n\t\t\t}\n\n\t\t}\n\n\n\t}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1188, "score_of_the_acc": -0.2439, "final_rank": 9 }, { "submission_id": "aoj_2050_71788", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<complex>\n#include<queue>\n#include<functional>\n#include<algorithm>\n#include<set>\n\nusing namespace std;\n\ntypedef double elem;\ntypedef complex<elem> point, vec;\ntypedef pair<point,point> line;\n\n#define N 1001\n\nconst elem eps = 1.0e-7;\nconst elem infty = 300000000.0;\n\nelem W[N][N];\nelem A[N];\nbool G[N][N];\n\nstruct State{\n int now;\n elem cost;\n bool operator>(const State&t)const{\n return cost > t.cost;\n }\n};\n\nelem cross(point a, point b){\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\nelem dist(point a, point b){\n return abs(b-a);\n}\nelem dist(point a1, point a2, point x){\n return abs( cross(a2-a1,x-a1)) / abs(a2-a1);\n}\n\ninline bool intersected_seg(point a1, point a2, point b1, point b2){\n return (cross(a2-a1,b1-a1)*cross(a2-a1,b2-a1) < eps &&\n\t cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1) < eps );\n}\n\ninline point intersection_seg(point a1, point a2, point b1, point b2){\n elem d1=dist(b1,b2,a1);\n elem d2=dist(b1,b2,a2);\n elem t=d1/(d1+d2);\n return a1+t*(a2-a1);\n}\n\nvoid ResetGraph(){\n for(int i = 0; i < N; ++i){\n A[i] = infty;\n for(int j = 0; j < N; ++j){\n W[i][j] = infty;\n G[i][j] = false;\n }\n }\n}\n\nelem dijkstra(int goal){\n priority_queue< State, vector<State>, greater<State> > pq;\n bool visited[N]={false,};\n State st;\n\n st.now = 0;\n visited[0] = true;\n st.cost = 0;\n A[0] = 0;\n pq.push( st );\n\n while( !pq.empty() ){\n st = pq.top();\n //cout << \" QUEUE SIZE \" << pq.size() << endl;\n pq.pop();\n\n //cout << \"NOW: \" << st.now << \" COST : \" << st.cost << endl;\n\n //visited[st.now] = false;\n if( st.now == goal ){\n return A[goal];\n }\n visited[st.now] = true;\n\n for(int i = 0; i < N; ++i){\n if( G[st.now][i] ){\n\tif( !visited[i] && A[st.now] + W[st.now][i] < A[i] ){\n\t State next;\n\t next.now = i;\n\t A[i] = A[st.now] + W[st.now][i];\n\t //cout << \"Goto : \" << i << endl;\n\t //cout << \"Right : \" << A[st.now] << \" + \" << W[st.now][i] << endl;\n\t next.cost = A[i];\n\t pq.push( next );\n\t}\n }\n }\n }\n return 0;\n}\n\nint main(){\n while(true){\n int n;\n double vw,vc;\n vector<point> vp;\n vector<line> lines;\n\n ResetGraph();\n cin >> n;\n if(n==0)break;\n cin >> vw >> vc;\n for(int i = 0; i < n; ++i){\n elem x,y;\n cin>>x>>y;\n point tp(x,y);\n if(i>0){\n\tline l = make_pair( vp.back(),tp );\n\tlines.push_back(l);\n }\n vp.push_back(tp);\n }\n\n for(int i = 0; i < lines.size(); ++i){\n //cout << lines[i].first << ' ' << lines[i].second << endl;\n G[i][i+1] = true;\n W[i][i+1] = dist(lines[i].first, lines[i].second) / vw;\n if( lines[i].second.imag() > lines[i].first.imag() ){\n\tline l = make_pair( lines[i].first, point(infty, lines[i].first.imag()) );\n\tfor(int j = i+1; j < lines.size(); ++j){\n\t //\t cout << l.first << ' ' << l.second << \" : \" << lines[j].first << ' ' << lines[j].second << endl;\n\t if( intersected_seg(l.first,\n\t\t\t l.second,\n\t\t\t lines[j].first,\n\t\t\t lines[j].second) ){\n\t point intersect = intersection_seg(\n\t\t\t\t\t l.first,\n\t\t\t\t\t l.second,\n\t\t\t\t\t lines[j].first,\n\t\t\t\t\t lines[j].second );\n\t //elem d = dist( intersect, lines[j].second );\n\t G[i][j+1] = true;\n\t W[i][j+1] = dist( lines[i].first, intersect ) / vc + dist( intersect, lines[j].second ) / vw;\n\t break;\n\t }\n\t}\n }\n if( lines[i].second.imag() < lines[i].first.imag() ){\n\tline l = make_pair( lines[i].second, point(-infty, lines[i].second.imag()) );\n\tfor(int j = i-1; j >= 0; --j){\n\t // cout << l.first << ' ' << l.second << \" : \" << lines[j].first << ' ' << lines[j].second << endl;\n\t if( intersected_seg(l.first,\n\t\t\t l.second,\n\t\t\t lines[j].first,\n\t\t\t lines[j].second) ){\n\t point intersect = intersection_seg(\n\t\t\t\t\t l.first,\n\t\t\t\t\t l.second,\n\t\t\t\t\t lines[j].first,\n\t\t\t\t\t lines[j].second );\n\t //elem d = dist( intersect, lines[j].second );\n\t G[j][i+1] = true;\n\t W[j][i+1] = dist( lines[i].second, intersect ) / vc + dist( intersect, lines[j].first ) / vw;\n\t break;\n\t }\n\t}\n }\n }\n /*\n for(int i = 0; i < N; ++i){\n for(int j = 0; j < N; ++j){\n\tif( G[i][j] ){\n\t cout << \"i:\" << i << \" j:\" << j << ' ' << vp[i] << \"->\" << vp[j] << \" COST: \" << W[i][j] << endl;\n\t}\n }\n }\n */\n\n printf(\"%.6lf\\n\", dijkstra(n-1));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 9888, "score_of_the_acc": -1.152, "final_rank": 13 }, { "submission_id": "aoj_2050_71609", "code_snippet": "#include<map>\n#include<cmath>\n#include<vector>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<complex>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\n\nconst int N = 1000;\n\nclass Edge{\npublic:\n int to;\n double cost;\n};\n\nvector<Edge> edge[N];\n\ndouble dist(double x,double y){\n return sqrt(x*x+y*y);\n}\n\ndouble getx(double xa,double xb,double ya,double yb,double y){\n double A = (ya-yb)/(xa-xb);\n return (y-ya+A*xa)/A;\n}\n\nvoid makeedge(int n,double *x,double *y,double vd,double vwalk){\n rep(i,n-1){\n edge[i].pb((Edge){i+1,dist(x[i]-x[i+1],y[i]-y[i+1])/vwalk});\n }\n \n rep(i,n){\n REP(j,i+1,n-1){\n if (i+1 == j && y[j] <= y[i])break;\n if (y[j+1] <= y[i] && y[i] < y[j]){\n\tdouble tx = getx(x[j],x[j+1],y[j],y[j+1],y[i]);\n\tedge[i].push_back((Edge){j+1,(tx-x[i])/vd+dist(tx-x[j+1],y[i]-y[j+1])/vwalk});\n\tbreak;\n }\n }\n \n for(int j=i-1;j>=1;j--){\n if (i-1 == j && y[j] <= y[i])break;\n if (y[j-1] <= y[i] && y[i] < y[j]){\n\tdouble tx = getx(x[j],x[j-1],y[j],y[j-1],y[i]);\n\tedge[j-1].push_back(\n\t\t\t (Edge){i,(x[i]-tx)/vd+dist(tx-x[j-1],y[i]-y[j-1])/vwalk});\n\tbreak;\n }\n }\n }\n}\n\n\ndouble cost[N];\ndouble solve(int n,double *x,double *y,double vw,double vd){\n rep(i,n)edge[i].clear();\n makeedge(n,x,y,vd,vw);\n rep(i,n)cost[i] = 1e100;\n\n cost[0] = 0;\n rep(i,n){\n rep(j,edge[i].size()){\n int next = edge[i][j].to;\n cost[next]=min(cost[next],cost[i]+edge[i][j].cost);\n }\n }\n\n /*\n rep(i,n){\n cout << \"now \" << x[i] << \" \" << y[i]<<endl;\n puts(\"edge begin\");\n rep(j,edge[i].size()){\n int nex = edge[i][j].to;\n cout << x[nex] << \" \"<< y[nex] << \" \"<< edge[i][j].cost << endl;\n }\n puts(\"Edge end\");\n }\n */\n\n return cost[n-1];\n}\n\n\nmain(){\n int n;\n while(cin>>n && n){\n double vwalk,vd;\n cin>>vwalk>>vd;\n double x[n],y[n];\n rep(i,n)cin>>x[i]>>y[i];\n printf(\"%6lf\\n\",solve(n,x,y,vwalk,vd));\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2049_cpp
Problem B: Headstrong Student You are a teacher at a cram school for elementary school pupils. One day, you showed your students how to calculate division of fraction in a class of mathematics. Your lesson was kind and fluent, and it seemed everything was going so well - except for one thing. After some experiences, a student Max got so curious about how precise he could compute the quotient. He tried many divisions asking you for a help, and finally found a case where the answer became an infinite fraction. He was fascinated with such a case, so he continued computing the answer. But it was clear for you the answer was an infinite fraction - no matter how many digits he computed, he wouldn’t reach the end. Since you have many other things to tell in today’s class, you can’t leave this as it is. So you decided to use a computer to calculate the answer in turn of him. Actually you succeeded to persuade him that he was going into a loop, so it was enough for him to know how long he could compute before entering a loop. Your task now is to write a program which computes where the recurring part starts and the length of the recurring part, for given dividend/divisor pairs. All computation should be done in decimal numbers. If the specified dividend/divisor pair gives a finite fraction, your program should treat the length of the recurring part as 0. Input The input consists of multiple datasets. Each line of the input describes a dataset. A dataset consists of two positive integers x and y , which specifies the dividend and the divisor, respectively. You may assume that 1 ≤ x < y ≤ 1,000,000. The last dataset is followed by a line containing two zeros. This line is not a part of datasets and should not be processed. Output For each dataset, your program should output a line containing two integers separated by exactly one blank character. The former describes the number of digits after the decimal point before the recurring part starts. And the latter describes the length of the recurring part. Sample Input 1 3 1 6 3 5 2 200 25 99 0 0 Output for the Sample Input 0 1 1 1 1 0 2 0 0 2
[ { "submission_id": "aoj_2049_10748216", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint m[1000005];\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n while (true) {\n int x, y; cin >> x >> y;\n if (!x) break;\n for (int i = 0;i < y;i++) m[i] = 0;\n int r = x % y;\n int cnt = 0;\n while (true) {\n if (m[r] || !r) break;\n m[r] = ++cnt;\n r = (r * 10) % y;\n }\n if (!r) {\n cout << cnt << \" 0\\n\";\n } else {\n cout << m[r] - 1 << \" \" << cnt - m[r] + 1 << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 7284, "score_of_the_acc": -0.0181, "final_rank": 9 }, { "submission_id": "aoj_2049_10747743", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint m[1000005];\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n while (true) {\n int x, y; cin >> x >> y;\n if (!x) break;\n for (int i = 0;i < y;i++) m[i] = 0;\n int r = x;\n int cnt = 0;\n while (true) {\n if (m[r] || !r) break;\n m[r] = ++cnt;\n r = (r * 10) % y;\n }\n if (!r) {\n cout << cnt << \" 0\\n\";\n } else {\n cout << m[r] - 1 << \" \" << cnt - m[r] + 1 << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 7200, "score_of_the_acc": -0.0157, "final_rank": 7 }, { "submission_id": "aoj_2049_4109889", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\nusing namespace std;\ntypedef long long int ll;\n \nint main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\twhile(1){\n\t\tint a,b; cin >> a >> b;\n\t\tif(a==0&&b==0)break;\n\t\tint g=__gcd(a,b);\n\t\ta/=g; b/=g;\n\t\tvector<int> seen(b,-1);\n\t\tseen[a]=0;\n\t\tfor(int c=1;;c++){\n\t\t\ta=(10*a)%b;\n\t\t\tif(a==0){\n\t\t\t\tcout << c << \" \" << 0 << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if(seen[a]!=-1){\n\t\t\t\tcout << seen[a] << \" \" << c-seen[a] << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tseen[a]=c;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 6976, "score_of_the_acc": -0.0163, "final_rank": 8 }, { "submission_id": "aoj_2049_2854533", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = double;\nconst ld eps = 1e-9;\n\nint adiv(int a) {\n\tmap<int,int>mp;\n\tint num(a);\n\tfor (int i = 2; i <= a; ++i) {\n\t\twhile (num%i == 0) {\n\t\t\tmp[i]++;\n\t\t\tnum/=i;\n\t\t}\n\t}\n\tint anum=0;\n\tfor (auto m : mp) {\n\t\tanum-=m.first;\n\t}\n\tanum+=2*prev(mp.end())->first;\n\treturn anum;\n}\n\nint main() {\n\twhile (true) {\n\n\t\tint x,y;cin>>x>>y;\n\t\tif(!x)break;\n\t\tmap<int,int>mp;\n\t\tvector<int>v(1e6+1,-1);\n\t\tint ans1=0,ans2=0;\n\t\twhile (true) {\n\t\t\tx*=10;\n\t\t\tint a=x/y;\n\t\t\tint b=x%y;\n\n\t\t\tif (b == 0) {\n\t\t\t\tans1++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (v[x/10]==-1) {\n\t\t\t\t\tv[x/10]=ans1;\n\t\t\t\t\tans1++;\n\t\t\t\t\tx = b;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tans2=ans1-v[x/10];\n\t\t\t\t\tans1=v[x/10];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans1<<\" \"<<ans2<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 6992, "score_of_the_acc": -0.0371, "final_rank": 11 }, { "submission_id": "aoj_2049_2341524", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int a,b;\n while(cin>>a>>b,a){\n int s[1000000]={},c=0,g=__gcd(a,b);\n a/=g,b/=g;\n while(!s[a]){\n s[a]=(++c);\n a=(a*10)%b;\n if(a<0)return 0;\n }\n if(a)c++;\n cout<<s[a]-1<<' '<<c-s[a]<<endl;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 6948, "score_of_the_acc": -0.011, "final_rank": 4 }, { "submission_id": "aoj_2049_2341523", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int a,b;\n while(cin>>a>>b,a){\n int s[1000000]={};\n int c=0,g=__gcd(a,b);\n a/=g,b/=g;\n while(!s[a]){\n s[a]=(++c);\n a=(a*10)%b;\n if(a<0)return 0;\n }\n if(a)c++;\n cout<<s[a]-1<<' '<<c-s[a]<<endl;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 6940, "score_of_the_acc": -0.0107, "final_rank": 3 }, { "submission_id": "aoj_2049_2340262", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) begin(v), end(v)\n#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nsigned main()\n{\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n int x, y;\n while(cin >> x >> y, x || y) {\n int used[1000010];\n memset(used, -1, sizeof(used));\n int i;\n used[x] = 0;\n x *= 10;\n for(i = 1; used[x%y] == -1; i++) {\n x %= y;\n used[x] = i;\n if(x == 0) break; \n x *= 10;\n }\n cout << used[x%y] << \" \" << i-used[x%y] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 10948, "score_of_the_acc": -0.1791, "final_rank": 12 }, { "submission_id": "aoj_2049_2340147", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int x,y;\n while(cin>>x>>y,x){\n int s[1000000]={};\n int c=0,gc=__gcd(x,y);\n x/=gc,y/=gc;\n while(!s[x]){\n s[x]=++c;\n x=(x*10)%y;\n }\n if(x==0)c=s[x]-1;\n cout<<s[x]-1<<\" \"<<c-s[x]+1<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 6944, "score_of_the_acc": -0.0131, "final_rank": 6 }, { "submission_id": "aoj_2049_2340140", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint M[10000000];\n\nint main(){\n while(1){\n int n,d;\n cin>>n>>d;\n if(n==0&&d==0)break;\n \n memset(M,-1,sizeof(M));\n int cnt=0;\n while(n){\n int s = (n/d) * d;\n n -= s;\n if(M[n]!=-1)break;\n M[n] = cnt++;\n n*=10;\n }\n\n\n if(n == 0)cnt = M[n];\n cout<<M[n]<<\" \"<<cnt-M[n]<<endl; \n }\n return 0;\n}", "accuracy": 1, "time_ms": 1740, "memory_kb": 42112, "score_of_the_acc": -1.3292, "final_rank": 14 }, { "submission_id": "aoj_2049_2340122", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nint m[1111111];\nsigned main(){\n int x,y;\n while(cin>>x>>y,x||y){\n int c=0;\n memset(m,-1,sizeof(m));\n while(m[x]<0){\n m[x]=c++;\n x=(x*10)%y;\n if(!x) break;\n }\n if(!x) cout<<c<<\" \"<<0<<endl;\n else cout<<m[x]<<\" \"<<c-m[x]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 11716, "score_of_the_acc": -0.2164, "final_rank": 13 }, { "submission_id": "aoj_2049_2143574", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <vector>\ntypedef long long int ll;\n\nusing namespace std;\n\nint* table;\n\nvoid func(int X,int Y){\n\n\tint current = X%Y,ans_left,ans_right;\n\n\ttable[current] = 0;\n\n\tfor(int i = 1;i < 1000000; i++){\n\t\tcurrent = (10*current)%Y;\n\n\t\tif(current == 0){ //?????????????????´???\n\t\t\tans_left = i;\n\t\t\tans_right = 0;\n\t\t\tbreak;\n\t\t}else{\n\t\t\tif(table[current] != -1){\n\t\t\t\tans_left = table[current];\n\t\t\t\tans_right = i - table[current];\n\t\t\t\tbreak;\n\t\t\t}else{\n\t\t\t\ttable[current] = i;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d %d\\n\",ans_left,ans_right);\n}\n\n\nint main(){\n\n\tint X,Y;\n\n\ttable = new int[1000010];\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&X,&Y);\n\t\tif(X == 0 && Y == 0)break;\n\n\t\tfor(int i = 0; i < Y; i++)table[i] = -1;\n\n\t\tfunc(X,Y);\n\t}\n\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 6660, "score_of_the_acc": -0.0097, "final_rank": 2 }, { "submission_id": "aoj_2049_2143573", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <vector>\ntypedef long long int ll;\n\nusing namespace std;\n\nint* table;\n\nvoid func(int X,int Y){\n\n\tint current = X%Y,ans_left,ans_right;\n\n\ttable[current] = 0;\n\n\tfor(int i = 1;i < 1000000; i++){\n\t\tcurrent = (10*current)%Y;\n\n\t\tif(current == 0){ //?????????????????´???\n\t\t\tans_left = i;\n\t\t\tans_right = 0;\n\t\t\tbreak;\n\t\t}else{\n\t\t\tif(table[current] != -1){\n\t\t\t\tans_left = table[current];\n\t\t\t\tans_right = i - table[current];\n\t\t\t\tbreak;\n\t\t\t}else{\n\t\t\t\ttable[current] = i;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d %d\\n\",ans_left,ans_right);\n}\n\n\nint main(){\n\n\tint X,Y;\n\n\ttable = new int[1000010];\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&X,&Y);\n\t\tif(X == 0 && Y == 0)break;\n\n\t\tfor(int i = 0; i < 1000010; i++)table[i] = -1;\n\n\t\tfunc(X,Y);\n\t}\n\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 6636, "score_of_the_acc": -0.0271, "final_rank": 10 }, { "submission_id": "aoj_2049_2110627", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int x,y;\n while(cin >> x >> y && x) {\n x%=y;\n unordered_map<int,int> m;\n int i;\n for(i=0; i<1000000; i++) {\n if(!x) break;\n if(m.count(x)) {\n cout << m[x] << \" \" << i-m[x] << endl;\n goto next;\n }\n m[x]=i;\n x*=10;\n x%=y;\n }\n cout << i << \" \" << 0 << endl;\n next:;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4410, "memory_kb": 42412, "score_of_the_acc": -1.9417, "final_rank": 17 }, { "submission_id": "aoj_2049_2110624", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(1){\n unordered_map<int,int> mp;\n int x,y,c=0;\n scanf(\"%d %d\",&x,&y);\n if(x==0&&y==0)break;\n \n while(x!=0&&mp.count(x)==0){\n mp[x]=c++;\n x*=10;\n x%=y;\n }\n if(x==0){\n cout<<c<<' '<<0<<endl;\n }else{\n cout<<mp[x]<<' '<<c-mp[x]<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4540, "memory_kb": 42476, "score_of_the_acc": -1.9729, "final_rank": 19 }, { "submission_id": "aoj_2049_2110584", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nint x,y;\nint main(){\n while( cin >> x >> y && (x||y) ){\n unordered_map<int,int> mp;\n int cnt = 0;\n while( x ){ \n if( mp.count(x) ) {\n cout << mp[x] << \" \" << cnt - mp[x] << endl;\n break;\n }\n mp[x] = cnt++;\n x *= 10;\n x %= y; \n }\n if( x == 0 ) cout << cnt << \" 0\" << endl;\n }\n}", "accuracy": 1, "time_ms": 4470, "memory_kb": 42380, "score_of_the_acc": -1.9543, "final_rank": 18 }, { "submission_id": "aoj_2049_2027335", "code_snippet": "//============================================================================\n// Name : B.cpp\n// Author : \n// Version :\n// Copyright : Your copyright notice\n// Description : Hello World in C++, Ansi-style\n//============================================================================\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint x,y;\n\nint dp[1000010];\npair<int,int> solve(int x, int y){\n\tmemset(dp, -1, sizeof dp);\n\tint c = x;\n\tfor(int i = 0;; ++i){\n\t\t//int q = c/y;\n\t\tint r = c%y;\n\t\tif(r == 0){\n\t\t\treturn make_pair(i, 0);\n\t\t}\n\t\tif(dp[r] != -1){\n\t\t\treturn make_pair(dp[r], i-dp[r]);\n\t\t}\n\t\tdp[r] = i;\n\t\tc = r*10;\n\t}\n}\n\nint main() {\n\twhile(cin >> x >> y && x){\n\t\tauto ans = solve(x,y);\n\t\tcout << ans.first << ' ' << ans.second << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 6956, "score_of_the_acc": -0.0112, "final_rank": 5 }, { "submission_id": "aoj_2049_2023368", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\n#define watch(a) { cout << #a << \" = \" << a << endl; }\ntemplate<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n\ntypedef long long ll;\nint const inf = 1<<29;\n\nint main() {\n\n for(int x, y; cin >> x >> y && (x && y);) {\n int mp[1000001]; memset(mp, -1, sizeof mp);\n int cnt = 0;\n while(!~mp[x % y]) {\n if(x % y == 0) {\n cout << cnt << \" 0\" << endl;\n goto exit;\n }\n mp[x % y] = cnt ++;\n x %= y;\n x *= 10;\n }\n cout << mp[x % y] << \" \" << cnt - mp[x % y] << endl;\n exit:;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 6892, "score_of_the_acc": -0.0094, "final_rank": 1 }, { "submission_id": "aoj_2049_2023366", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\n#define watch(a) { cout << #a << \" = \" << a << endl; }\ntemplate<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n\ntypedef long long ll;\nint const inf = 1<<29;\n\nint main() {\n\n for(int x, y; cin >> x >> y && (x && y);) {\n unordered_map<int, int> mp;\n int cnt = 0;\n while(mp.find(x % y) == mp.end()) {\n if(x % y == 0) {\n cout << cnt << \" 0\" << endl;\n goto exit;\n }\n mp[x % y] = cnt ++;\n x %= y;\n x *= 10;\n }\n cout << mp[x % y] << \" \" << cnt - mp[x % y] << endl;\n exit:;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 4660, "memory_kb": 42276, "score_of_the_acc": -1.9944, "final_rank": 20 }, { "submission_id": "aoj_2049_2022987", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint check[10][1000001];\n\nint main()\n{\n int x, y;\n while(cin >> x >> y, x || y) {\n memset(check, -1, sizeof(check));\n int cnt = 0;\n x *= 10;\n bool flag = false;\n while(!~check[x/y][x%y]) {\n check[x/y][x%y] = cnt;\n if(x%y == 0) { flag = true; break; }\n cnt++;\n x %= y; x *= 10;\n }\n cout << check[x/y][x%y]+(int)flag << \" \" << cnt-check[x/y][x%y] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1920, "memory_kb": 42136, "score_of_the_acc": -1.3706, "final_rank": 16 }, { "submission_id": "aoj_2049_2022896", "code_snippet": "#include<bits/stdc++.h>\n#define N 10000005\nusing namespace std;\nint m[N];\nint a,b,cnt,ans1,ans2;\n\nint main(){\n while(1){\n cin>>a>>b;\n if(!a&&!b)break;\n memset(m,-1,sizeof(m));\n a%=b;\n cnt=0;\n while(1){\n a*=10;\n if(m[a]!=-1){\n\tans1=m[a],ans2=cnt-m[a];\n\tbreak;\n }\n m[a]=cnt;\n a%=b;\n if(!a){\n\tans1=cnt+1,ans2=0;\n\tbreak;\n } \n cnt++;\n }\n cout<<ans1<<' '<<ans2<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1890, "memory_kb": 42032, "score_of_the_acc": -1.3609, "final_rank": 15 } ]
aoj_2054_cpp
Problem G: Entangled Tree The electronics division in Ishimatsu Company consists of various development departments for electronic devices including disks and storages, network devices, mobile phones, and many others. Each department covers a wide range of products. For example, the department of disks and storages develops internal and external hard disk drives, USB thumb drives, solid-state drives, and so on. This situation brings staff in the product management division difficulty categorizing these numerous products because of their poor understanding of computer devices. One day, a staff member suggested a tree-based diagram named a category diagram in order to make their tasks easier. A category diagram is depicted as follows. Firstly, they prepare one large sheet of paper. Secondly, they write down the names of the development departments on the upper side of the sheet. These names represent the start nodes of the diagram. Each start node is connected to either a single split node or a single end node (these nodes will be mentioned soon later). Then they write down a number of questions that distinguish features of products in the middle, and these questions represent the split nodes of the diagram. Each split node is connected with other split nodes and end nodes, and each line from a split node is labeled with the answer to the question. Finally, they write down all category names on the lower side, which represents the end nodes . The classification of each product is done like the following. They begin with the start node that corresponds to the department developing the product. Visiting some split nodes, they traces the lines down until they reach one of the end nodes labeled with a category name. Then they find the product classified into the resultant category. The visual appearance of a category diagram makes the diagram quite understandable even for non-geek persons. However, product managers are not good at drawing the figures by hand, so most of the diagrams were often messy due to many line crossings. For this reason, they hired you, a talented programmer, to obtain the clean diagrams equivalent to their diagrams. Here, we mean the clean diagrams as those with no line crossings. Your task is to write a program that finds the clean diagrams. For simplicity, we simply ignore the questions of the split nodes, and use integers from 1 to N instead of the category names. Input The input consists of multiple datasets. Each dataset follows the format below: N M Q split node info 1 split node info 2 ... split node info M query 1 query 2 ... query Q The first line of each dataset contains three integers N (1 ≤ N ≤ 100000), M (0 ≤ M ≤ N - 1), and Q (1 ≤ Q ≤ 1000, Q ≤ N ), representing the number of end nodes and split nodes, and the number of queries respectively. Then M lines describing the split nodes follow. Each split node is described in the format below: Y L label 1 label 2 . . . The first two integers, Y (0 ≤ Y ≤ 10 9 ) and L , wh ...(truncated)
[ { "submission_id": "aoj_2054_1679749", "code_snippet": "#include <bits/stdc++.h>\n\n#define x first\n#define y second\n#define pb push_back\n#define pii pair<int,int>\n#define ll long long\n#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); i++)\n#define REP(i, a) FOR(i, 0, a)\n#define TRACE(x) cout << #x << \" \" << x << endl\n#define _ << \" _ \" <<\n\nusing namespace std;\n\n//#define task \"a\"\n#define oo 1e9\n\nconst int MAXM = 100010;\n\nclass inter {\n public:\n int h, l;\n vector<int> V;\n void reset() {\n h = l = 0;\n V.clear();\n }\n} a[MAXM*2];\n\nint n, m, q;\nint par[MAXM*2];\nvector<int> graf[MAXM*2];\n\nint nadji(int x) {\n if (par[x] == x) return x;\n return par[x] = nadji(par[x]);\n}\n\nvoid spoji(int i1, int i2) {\n i1 = nadji(i1);\n i2 = nadji(i2);\n par[i1] = i2;\n // printf(\"Spajam %d %d\\n\", i1, i2);\n}\n\nint cnt;\nint sol[MAXM*2];\nint bio[MAXM*2];\n\nint mini[MAXM*2];\n\nvoid DFS(int curr) {\n // TRACE(curr);\n mini[curr] = oo;\n if (curr <= n) {\n mini[curr] = curr;\n // sol[cnt++] = curr;\n // bio[curr] = 1;\n }\n for (auto it: graf[curr]) {\n DFS(it);\n mini[curr] = min(mini[curr], mini[it]);\n }\nsort(graf[curr].begin(), graf[curr].end(), [](const int &i1, const int &i2) { return mini[i1] < mini[i2]; });\n}\n\nvoid DFS2(int curr) {\nif (curr <= n) {\nsol[cnt++] = curr;\nbio[curr] = 1;\n}\n\nfor (auto it: graf[curr]) DFS2(it);\n\n}\n\nint main(){\n while(1==1) {\n cnt = 1;\n memset(sol, 0, sizeof sol);\n memset(bio, 0, sizeof bio);\n memset(mini, 0, sizeof mini);\n REP(i, MAXM*2) par[i] = i;\n // freopen(task \".in\", \"r\", stdin);\n // freopen(task \".out\", \"w\", stdout);\n\n scanf(\"%d%d%d\", &n, &m, &q);\n if (n == 0 && m == 0 && q == 0) break;\n REP(i, m) {\n scanf(\"%d%d\", &a[i].h, &a[i].l);\n REP(j, a[i].l) {\n\tint pom;\n\tscanf(\"%d\", &pom);\n\ta[i].V.pb(pom);\n }\n }\n sort(a, a + m, [](const inter &i1, const inter &i2){ return i1.h > i2.h; });\n\n int tr = n + 1;\n REP(i, m) {\n REP(j, a[i].V.size()) {\n\tint pom = nadji(a[i].V[j]);\n\tgraf[tr].pb(pom);\n\t//\tprintf(\"Edge %d %d\\n\", tr, pom);\n\tspoji(pom, tr);\n }\n tr++;\n }\n for (int i = tr - 1; i > 0; i--) {\n if (par[i] == i) {\n\tpar[i] = tr;\n\tgraf[tr].pb(i);\n\t//\tprintf(\"Edge %d %d\\n\", tr, i); \n }\n }\n \n DFS(tr);\n DFS2(tr);\n // FOR(i, 1, n+1) {\n // if (bio[i] == 0) sol[cnt++] = i;\n // }\n\n REP(i, q) {\n int pom;\n scanf(\"%d\", &pom);\n printf(\"%d\\n\", sol[pom]);\n }\n\n REP(i, tr+1) {\n graf[i].clear();\n a[i].reset();\n }\n printf(\"\\n\");\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 32704, "score_of_the_acc": -0.9998, "final_rank": 1 }, { "submission_id": "aoj_2054_1679669", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cstring>\n#include <iostream>\n\nusing namespace std;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define TRACE(x) cout << #x << \" = \" << x << endl\n#define _ << \" _ \" <<\n\ntypedef long long llint;\n\nconst int MAX = 300100;\n\nvector<pair<int, vector<int>>> g;\npair<int, vector<int>> v[MAX];\nint dad[MAX], id[MAX];\nint idx[MAX];\nbool dead[MAX];\n\nint findset(int x) {\n return x == dad[x] ? x : dad[x] = findset(dad[x]);\n}\n\nint pos;\nvoid dfs(int x) {\n if (g[x].second.size() == 1) {\n idx[pos++] = g[x].first;\n return;\n }\n for (int y: g[x].second) dfs(y);\n}\n\nint main(void) {\n int n, m, q;\n while (scanf(\"%d %d %d\", &n, &m, &q) == 3) {\n if (n == 0) break;\n\n REP(i, m) {\n scanf(\"%d\", &v[i].first);\n int k; scanf(\"%d\", &k);\n v[i].second.resize(k);\n REP(j, k) {\n scanf(\"%d\", &v[i].second[j]);\n --v[i].second[j];\n }\n }\n\n sort(v, v + m);\n g.clear();\n REP(i, n) {\n g.push_back({i, {i}});\n id[i] = i;\n dad[i] = i;\n dead[i] = false;\n }\n\n for (int j = m-1; j >= 0; --j) {\n vector<int> w;\n for (int x: v[j].second) w.push_back(id[findset(x)]);\n sort(w.begin(), w.end(), [&] (const int& a, const int& b) {\n return g[a].first < g[b].first;\n }\n );\n \n int par = findset(v[j].second[0]);\n for (int x: v[j].second) dad[findset(x)] = par;\n id[par] = g.size();\n dead[g.size()] = false;\n for (int x: w) dead[x] = true;\n g.push_back({g[w[0]].first, w});\n }\n \n vector<int> w;\n REP(i, (int)g.size())\n if (!dead[i]) w.push_back(i);\n sort(w.begin(), w.end(), [&] (const int& a, const int& b) {\n return g[a].first < g[b].first;\n }\n );\n \n pos = 0;\n for (int x: w) dfs(x);\n\n REP(i, q) {\n int x;\n scanf(\"%d\", &x); --x;\n printf(\"%d\\n\", idx[x]+1);\n }\n putchar('\\n');\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 32708, "score_of_the_acc": -1.1304, "final_rank": 3 }, { "submission_id": "aoj_2054_427652", "code_snippet": "#include <stdio.h>\n#include <assert.h>\n#include <vector>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define mp make_pair\n\n#define NUM (120000)\nint N, M, Q, Y[NUM];\nvector<int> ls[NUM];\npair<int, int> ord[NUM];\nint hi[NUM], p[NUM], c[NUM], f[NUM], b[NUM], ans[NUM];\nvector<pair<int, int> > a[NUM];\n\nvoid fix(int k) {\n sort(a[k].begin(), a[k].end());\n f[k] = a[k][0].first;\n c[k] = 0;\n rep (i, a[k].size()) c[k] += a[k][i].second == -1 ? 1 : c[a[k][i].second];\n}\n\nvoid build() {\n Y[M] = -1;\n rep (i, N) hi[i] = -1;\n rep (i, M+1) ord[i] = mp(Y[i], i);\n sort(ord, ord+M+1);\n for (int ik = M; ik > 0; ik--) {\n const int k = ord[ik].second;\n a[k].clear();\n p[k] = -1;\n rep (i, ls[k].size()) {\n const int ix = ls[k][i]-1;\n if (hi[ix] == -1) a[k].push_back(mp(ix, -1));\n else {\n a[k].push_back(mp(f[hi[ix]], hi[ix]));\n assert(p[hi[ix]] == -1);\n p[hi[ix]] = i;\n }\n hi[ix] = k;\n }\n fix(k);\n }\n a[M].clear();\n rep (i, N) if (hi[i] == -1) a[M].push_back(mp(i, -1));\n rep (i, M) if (p[i] == -1) a[M].push_back(mp(f[i], i));\n fix(M);\n b[M] = 0;\n rep (ik, M+1) {\n const int k = ord[ik].second;\n int z = b[k];\n rep (i, a[k].size()) {\n if (a[k][i].second == -1) {\n ans[z] = a[k][i].first;\n z++;\n }\n else {\n b[a[k][i].second] = z;\n z += c[a[k][i].second];\n }\n }\n }\n}\n\nint main() {\n for (;;) {\n scanf(\"%d%d%d\", &N, &M, &Q);\n if (N == 0) return 0;\n rep (i, M) {\n scanf(\"%d\", Y+i);\n int L;\n scanf(\"%d\", &L);\n ls[i].resize(L);\n rep (j, L) scanf(\"%d\", &ls[i][j]);\n }\n build();\n rep (i, Q) {\n int q;\n scanf(\"%d\", &q);\n printf(\"%d\\n\", ans[q-1]+1);\n }\n printf(\"\\n\");\n }\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 14000, "score_of_the_acc": -1, "final_rank": 2 } ]
aoj_2055_cpp
Problem H: Ramen Shop Ron is a master of a ramen shop. Recently, he has noticed some customers wait for a long time. This has been caused by lack of seats during lunch time. Customers loses their satisfaction if they waits for a long time, and even some of them will give up waiting and go away. For this reason, he has decided to increase seats in his shop. To determine how many seats are appropriate, he has asked you, an excellent programmer, to write a simulator of customer behavior. Customers come to his shop in groups, each of which is associated with the following four parameters: T i : when the group comes to the shop P i : number of customers W i : how long the group can wait for their seats E i : how long the group takes for eating The i -th group comes to the shop with P i customers together at the time T i . If P i successive seats are available at that time, the group takes their seats immediately. Otherwise, they waits for such seats being available. When the group fails to take their seats within the time W i (inclusive) from their coming and strictly before the closing time, they give up waiting and go away. In addition, if there are other groups waiting, the new group cannot take their seats until the earlier groups are taking seats or going away. The shop has N counters numbered uniquely from 1 to N . The i -th counter has C i seats. The group prefers “seats with a greater distance to the nearest group.” Precisely, the group takes their seats according to the criteria listed below. Here, S L denotes the number of successive empty seats on the left side of the group after their seating, and S R the number on the right side. S L and S R are considered to be infinity if there are no other customers on the left side and on the right side respectively. Prefers seats maximizing min{ S L , S R }. If there are multiple alternatives meeting the first criterion, prefers seats maximizing max{ S L , S R }. If there are still multiple alternatives, prefers the counter of the smallest number. If there are still multiple alternatives, prefers the leftmost seats. When multiple groups are leaving the shop at the same time and some other group is waiting for available seats, seat assignment for the waiting group should be made after all the finished groups leave the shop. Your task is to calculate the average satisfaction over customers. The satisfaction of a customer in the i -th group is given as follows: If the group goes away without eating, -1. Otherwise, ( W i - t i )/ W i where t i is the actual waiting time for the i -th group (the value ranges between 0 to 1 inclusive). Input The input consists of multiple datasets. Each dataset has the following format: N M T C 1 C 2 ... C N T 1 P 1 W 1 E 1 T 2 P 2 W 2 E 2 ... T M P M W M E M N indicates the number of counters, M indicates the number of groups and T indicates the closing time. The shop always opens at the time 0. All input values are integers. You can assume that 1 ≤ N ≤ 100, 1 ≤ M ≤ 10 ...(truncated)
[ { "submission_id": "aoj_2055_9149012", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 105\n#define SIZE2 10005\n\nenum Type{\n\ttype_END,\n\ttype_ATE,\n\ttype_WAIT_OUT,\n\ttype_IN,\n};\n\nstruct Info{\n\n\tint T,P,W,E;\n};\n\nstruct Event{\n\tEvent(int arg_T,int arg_ind,Type arg_type){\n\t\tT = arg_T;\n\t\tind = arg_ind;\n\t\ttype = arg_type;\n\t}\n\tEvent(){\n\n\t\tT = ind = 0;\n\t\ttype = type_IN;\n\t}\n\tbool operator<(const struct Event &arg) const{\n\t\tif(T != arg.T){\n\n\t\t\treturn T > arg.T; //時刻の昇順(PQ)\n\t\t}else{\n\n\t\t\treturn type > arg.type; //退出を先に処理(PQ)\n\t\t}\n\t}\n\n\tint T,ind;\n\tType type;\n};\n\nstruct DATA{\n\tvoid set(int arg_ind,int arg_SL,int arg_SR){\n\t\tind = arg_ind;\n\t\tSL = arg_SL;\n\t\tSR = arg_SR;\n\t}\n\n\tint ind,SL,SR;\n};\n\nstruct LOC{\n\tvoid set(int arg_c_ind,int arg_left){\n\t\tc_ind = arg_c_ind;\n\t\tleft = arg_left;\n\t}\n\n\tint c_ind,left;\n};\n\nint N,M,LAST;\nint C[SIZE]; //各カウンターに何席あるか\nmap<int,int> REV; //来店時刻からグループ番を引く\nmap<int,bool> MAP;\nbool table[SIZE][SIZE],after_wait[SIZE2];\nint nearL[SIZE][SIZE],nearR[SIZE][SIZE];\nint C_IND[SIZE2],LEFT[SIZE2];\nInfo info[SIZE2];\n\n//カウンター[c_ind]で、グループ[ind]が座る位置を探す(なければ-1)を返却\nDATA searchPos(int c_ind,int ind){ //100\n\n\tDATA ret;\n\tret.set(BIG_NUM,-1,-1);\n\n\tint min_maxi= -BIG_NUM,max_maxi = -BIG_NUM;\n\tint len = info[ind].P;\n\n\tfor(int i = 1; i+len-1 <= C[c_ind]; i++){ //左端候補を走査\n\n\t\tif((nearR[c_ind][i] <= i+len-1))continue;\n\n\t\tint SL = min(BIG_NUM,(i-nearL[c_ind][i])-1);\n\t\tint SR = min(BIG_NUM,(nearR[c_ind][i+len-1]-(i+len-1)-1));\n\n\t\tif(min(SL,SR) < min_maxi)continue;\n\n\t\tif(min(SL,SR) > min_maxi){ //最小値の最大値更新\n\n\t\t\tmin_maxi = min(SL,SR);\n\t\t\tmax_maxi = max(SL,SR);\n\t\t\tret.set(i,SL,SR);\n\n\t\t}else if(min(SL,SR) == min_maxi){ //最小値の最大値が同点\n\n\t\t\tif(max(SL,SR) > max_maxi){ //最大値更新\n\n\t\t\t\tmax_maxi = max(SL,SR);\n\t\t\t\tret.set(i,SL,SR);\n\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid updateDATA(int ind,bool is_in){ //カウンターの状態の更新\n\n\tint c_ind = C_IND[ind];\n\tint left = LEFT[ind];\n\tint len = info[ind].P;\n\n\tfor(int i = left; i <= left+len-1; i++){\n\n\t\tif(is_in){ //新しく座る\n\n\t\t\ttable[c_ind][i] = true;\n\n\t\t}else{ //席を立つ\n\n\t\t\ttable[c_ind][i] = false;\n\t\t}\n\t}\n\n\t//nearLの再計算\n\tint last = -(BIG_NUM+SIZE);\n\tfor(int i = 1; i <= C[c_ind]; i++){\n\n\t\tif(table[c_ind][i]){\n\n\t\t\tlast = i;\n\t\t\tnearL[c_ind][i] = i;\n\t\t}else{\n\n\t\t\tnearL[c_ind][i] = last;\n\t\t}\n\t}\n\n\t//nearRの再計算\n\tlast = BIG_NUM+SIZE;\n\tfor(int i = C[c_ind]; i >= 1; i--){\n\n\t\tif(table[c_ind][i]){\n\n\t\t\tlast = i;\n\t\t\tnearR[c_ind][i] = i;\n\t\t}else{\n\n\t\t\tnearR[c_ind][i] = last;\n\t\t}\n\t}\n}\n\nLOC searchLOC(int ind){ //座れるカウンターと、その位置を調べる\n\n\tLOC ret;\n\n\tret.set(-1,-1);\n\n\tint min_maxi = -BIG_NUM,max_maxi = -BIG_NUM;\n\n\n\tfor(int i = 0; i < N; i++){ //N個のカウンターすべてを見る\n\n\t\tDATA tmpD = searchPos(i,ind);\n\t\tif(tmpD.ind == BIG_NUM)continue; //座れる場所なし\n\n\t\tif(min_maxi < min(tmpD.SL,tmpD.SR)){ //最小値の最大値更新\n\t\t\tmin_maxi = min(tmpD.SL,tmpD.SR);\n\t\t\tmax_maxi = max(tmpD.SL,tmpD.SR);\n\t\t\tret.set(i,tmpD.ind);\n\n\t\t}else if(min_maxi == min(tmpD.SL,tmpD.SR)){ //最小値の最大値が同点\n\n\t\t\tif(max_maxi < max(tmpD.SL,tmpD.SR)){ //最大値の最大値更新\n\n\t\t\t\tmax_maxi = max(tmpD.SL,tmpD.SR);\n\t\t\t\tret.set(i,tmpD.ind);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid func(){\n\n\tMAP.clear();\n\tREV.clear();\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d\",&C[i]);\n\t}\n\n\tpriority_queue<Event> Q;\n\n\tint div = 0;\n\n\tfor(int i = 0; i < M; i++){//グループの情報を取得\n\n\t\tscanf(\"%d %d %d %d\",&info[i].T,&info[i].P,&info[i].W,&info[i].E);\n\n\t\tREV[info[i].T] = i; //来店時刻による逆引き表\n\t\tdiv += info[i].P;\n\n\t\tQ.push(Event(info[i].T,i,type_IN));\n\t}\n\n\tQ.push(Event(LAST,-1,type_END)); //閉店\n\n\t//カウンターの情報の初期化\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 1; k <= C[i]; k++){\n\n\t\t\ttable[i][k] = false;\n\t\t\tnearL[i][k] = -(BIG_NUM+SIZE); //kおよびkより左で、最も近い、誰かが座っている椅子の番号\n\t\t\tnearR[i][k] = BIG_NUM+SIZE; //kおよびkより右で、最も近い、誰かが座っている椅子の番号\n\t\t}\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tafter_wait[i] = false;\n\t\tC_IND[i] = -1;\n\t}\n\n\n\tint now = 0;\n\tdouble ans = 0;\n\n\twhile(!Q.empty()){\n\t\tEvent event = Q.top();\n\t\tQ.pop();\n\n\t\tnow = event.T;\n\n\t\tif(now == LAST)break;\n\n\t\tif(event.type == type_IN){ //来店\n\n\t\t\tif(MAP.size() > 0){ //待ち行列あり\n\n\t\t\t\tMAP[info[event.ind].T] = true; //待ち行列に加える\n\t\t\t\tQ.push(Event(now+info[event.ind].W,event.ind,type_WAIT_OUT)); //食べずに退店イベントの追加\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//MAP.size() == 0\n\n\t\t\tLOC loc = searchLOC(event.ind); //座れるカウンターと、その位置を調べる\n\n\t\t\tif(loc.c_ind == -1){ //現在座れる椅子がなかった場合\n\n\t\t\t\tMAP[info[event.ind].T] = true; //待ち行列に加える\n\t\t\t\tQ.push(Event(now+info[event.ind].W,event.ind,type_WAIT_OUT)); //食べずに退店イベントの追加\n\n\t\t\t}else{ //現在座れる椅子があった場合\n\n\t\t\t\tans += 1.0*info[event.ind].P; //待ち時間0\n\n\t\t\t\tC_IND[event.ind] = loc.c_ind;\n\t\t\t\tLEFT[event.ind] = loc.left;\n\n\t\t\t\tupdateDATA(event.ind,true);\n\n\t\t\t\tQ.push(Event(now+info[event.ind].E,event.ind,type_ATE)); //食べ終わって退店イベントの追加\n\t\t\t}\n\n\t\t}else{ //type_ATE または type_WAIT_OUT\n\n\t\t\tbool FLG = true; //待ち行列を消化して良いか\n\n\t\t\tset<int> SET;\n\n\t\t\tif(event.type == type_ATE){ //食べ終わり\n\n\t\t\t\t//■同時刻に退店するグループを、全て出す(食べて退店)→待ち行列にはいないはず\n\t\t\t\twhile(true){\n\n\t\t\t\t\t//退店処理\n\t\t\t\t\tupdateDATA(event.ind,false);\n\n\t\t\t\t\tif(Q.empty() || Q.top().T != now || Q.top().type != type_ATE)break;\n\n\t\t\t\t\tevent = Q.top();\n\t\t\t\t\tQ.pop();\n\t\t\t\t}\n\n\t\t\t}else if(event.type == type_WAIT_OUT){\n\n\t\t\t\t//■同時刻に退店するグループを、全て出す(食べずに退店)\n\n\t\t\t\twhile(true){\n\n\t\t\t\t\tif(!after_wait[event.ind]){ //待っている途中で席に座らなかった場合\n\n\t\t\t\t\t\tSET.insert(event.ind); //待ち行列にいるはず\n\t\t\t\t\t}\n\n\t\t\t\t\tif(Q.empty() || Q.top().T != now || Q.top().type != type_WAIT_OUT)break;\n\n\t\t\t\t\tevent = Q.top();\n\t\t\t\t\tQ.pop();\n\t\t\t\t}\n\n\t\t\t\tif(SET.size() == 0)continue; //有効な情報なし\n\n\t\t\t\tif(MAP.size() > 0){\n\n\t\t\t\t\tint work_ind = REV[MAP.begin()->first]; //待ち集団の先頭\n\n\t\t\t\t\tif(SET.count(work_ind) > 0){ //待ち集団の先頭の退出時刻が到来した場合\n\n\t\t\t\t\t\t//■■まずは先頭を消す\n\t\t\t\t\t\tans -= 1.0*info[work_ind].P;\n\n\t\t\t\t\t\tMAP.erase(info[work_ind].T);\n\n\t\t\t\t\t\tSET.erase(work_ind);\n\n\t\t\t\t\t}else{ //待ち行列の先頭が、引き続き待つ場合\n\n\t\t\t\t\t\tFLG = false; //待ち行列は消化しない\n\n\t\t\t\t\t\tfor(int g: SET){\n\n\t\t\t\t\t\t\tans -= 1.0*info[g].P;\n\t\t\t\t\t\t\tMAP.erase(info[g].T);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tSET.clear();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(!FLG)continue;\n\n\t\t\t//FLG == true;\n\n\t\t\t//新たに座れるようになった人々を座らせる\n\t\t\twhile(true){\n\n\t\t\t\tif(MAP.size() == 0)break;\n\n\t\t\t\tauto at = MAP.begin();\n\t\t\t\tint tmp_ind = REV[at->first]; //待ち行列の先頭グループ\n\n\t\t\t\tLOC loc = searchLOC(tmp_ind);\n\n\t\t\t\tif(loc.c_ind == -1){\n\t\t\t\t\tif(now == info[tmp_ind].T+info[tmp_ind].W){\n\n\t\t\t\t\t\tMAP.erase(info[tmp_ind].T);\n\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tbreak; //まだ座れない\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tafter_wait[tmp_ind] = true;\n\n\t\t\t\tdouble bunshi = info[tmp_ind].W - (now-info[tmp_ind].T);\n\t\t\t\tdouble bunbo = info[tmp_ind].W;\n\n\t\t\t\tans += (bunshi/bunbo)*info[tmp_ind].P;\n\n\t\t\t\tC_IND[tmp_ind] = loc.c_ind;\n\t\t\t\tLEFT[tmp_ind] = loc.left;\n\t\t\t\tupdateDATA(tmp_ind,true);\n\n\t\t\t\tQ.push(Event(now+info[tmp_ind].E,tmp_ind,type_ATE));\n\n\t\t\t\tSET.erase(tmp_ind);\n\t\t\t\tMAP.erase(info[tmp_ind].T);\n\t\t\t}\n\n\t\t\t//■■席に座れなかった、退出予定時刻到来者を待ち行列から消す\n\t\t\tfor(int g: SET){\n\n\t\t\t\tans -= 1.0*info[g].P;\n\t\t\t\tMAP.erase(info[g].T);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(auto at = MAP.begin(); at != MAP.end(); at++){\n\n\t\tint tmp_ind = REV[at->first];\n\t\tans -= 1.0*info[tmp_ind].P;\n\t}\n\n\tprintf(\"%.16lf\\n\",ans/div);\n}\n\nint main(){\n\n\n\twhile(true){\n\t\tscanf(\"%d %d %d\",&N,&M,&LAST);\n\t\tif(N == 0 && M == 0 && LAST == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 4808, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_2055_472123", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nclass Data\n{\npublic:\n long long time;\n int id;\n int kind; // 0 : 客の来店\n // 1 : 待ち時間が長く帰宅\n // 2 : 食事を終えて帰宅\n // 3 : 閉店\n Data(long long time0, int id0, int kind0){\n time = time0;\n id = id0;\n kind = kind0;\n }\n bool operator< (const Data& d) const{\n return make_pair(time, kind) < make_pair(d.time, d.kind);\n }\n};\n\npair<int, int> findCounter(const vector<vector<bool> >& used, int num)\n{\n int n = used.size();\n\n int sl = -1;\n int sr = -1;\n pair<int, int> ret(-1, -1);\n for(int i=0; i<n; ++i){\n int m = used[i].size();\n int size = 0;\n for(int j=0; j<m; ++j){\n if(used[i][j]){\n size = 0;\n continue;\n }\n\n ++ size;\n if(size >= num){\n int sl2;\n int sr2;\n int k;\n if(j+1 == size && j == m-1){\n sl2 = sr2 = INT_MAX;\n k = 0;\n }else if(j+1 == size){\n sl2 = INT_MAX;\n sr2 = size - num;\n k = 0;\n }else if(j == m-1){\n sl2 = size - num;\n sr2 = INT_MAX;\n k = j - num + 1;\n }else{\n sl2 = (size - num) / 2;\n sr2 = (size - num + 1) / 2;\n k = j - size + (size - num) / 2 + 1;\n }\n if(min(sl2, sr2) > min(sl, sr) || (min(sl2, sr2) == min(sl, sr) && max(sl2, sr2) > max(sl, sr))){\n sl = sl2;\n sr = sr2;\n ret = make_pair(i, k);\n }\n }\n }\n }\n\n return ret;\n}\n\nint main()\n{\n for(;;){\n int n, m, closeTime; // カウンター数、グループ数、閉店時間\n cin >> n >> m >> closeTime;\n if(n == 0)\n return 0;\n\n vector<int> counter(n); // 各カウンターの席の数\n for(int i=0; i<n; ++i)\n cin >> counter[i];\n vector<vector<bool> > used(n); // 使用中のカウンターの席\n for(int i=0; i<n; ++i)\n used[i].assign(counter[i], false);\n\n vector<int> come(m), num(m), wait(m), eat(m); // 来店時間、各グループの客の人数、最大待ち時間、食事時間\n for(int i=0; i<m; ++i)\n cin >> come[i] >> num[i] >> wait[i] >> eat[i];\n\n multiset<Data> ms;\n ms.insert(Data(closeTime, -1, 3));\n for(int i=0; i<m; ++i){\n ms.insert(Data(come[i], i, 0));\n ms.insert(Data(come[i] + wait[i], i, 1));\n }\n\n queue<int> q; // 待っているグループ\n vector<pair<int, int> > sit(m); // 各グループが使っているカウンターの位置\n vector<double> satisfaction(m, -1.0); // 各グループの満足度\n bool isClose = false; // 閉店時間を過ぎたかどうか\n while(!ms.empty()){\n long long time = ms.begin()->time;\n while(!ms.empty() && ms.begin()->time == time){\n Data d = *ms.begin();\n ms.erase(ms.begin());\n\n if(d.kind == 0){\n q.push(d.id);\n }else if(d.kind == 2){\n for(int i=0; i<num[d.id]; ++i)\n used[sit[d.id].first][sit[d.id].second + i] = false;\n }else if(d.kind == 3){\n isClose = true;\n }\n }\n if(isClose)\n break;\n\n while(!q.empty()){\n int id = q.front();\n if(time > come[id] + wait[id]){\n q.pop();\n continue;\n }\n\n pair<int, int> pos = findCounter(used, num[id]);\n if(pos.first == -1){\n if(time == come[id] + wait[id]){\n q.pop();\n continue;\n }\n break;\n }\n\n q.pop();\n for(int i=0; i<num[id]; ++i)\n used[pos.first][pos.second + i] = true;\n sit[id] = pos;\n satisfaction[id] = (wait[id] - (time - come[id])) / (double)wait[id];\n ms.insert(Data(time + eat[id], id, 2));\n }\n }\n\n double ret = 0.0;\n for(int i=0; i<m; ++i)\n ret += satisfaction[i] * num[i];\n ret /= accumulate(num.begin(), num.end(), 0);\n printf(\"%.10f\\n\", ret);\n }\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 2784, "score_of_the_acc": -0.9359, "final_rank": 2 }, { "submission_id": "aoj_2055_361601", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <cstring>\n\nusing namespace std;\n\nclass Event{\npublic:\n\tint time, group, type;\n\tEvent(int t, int g, int p) : time(t), group(g), type(p) {}\n\tbool operator < (const Event &e) const { return make_pair(time,type) > make_pair(e.time,e.type); }\n};\n\nint main(){\n\tint n, m, t;\n\tint c[100], pos[10000], T[10000], P[10000], W[10000], E[10000];\n\tint seat[100][100];\n\tint left[100], right[100], seq[100];\n\tbool out[10000], in[10000];\n\tdouble score[10000];\n\twhile(cin >> n >> m >> t, n){\n\t\tfor(int i=0;i<n;i++) cin >> c[i];\n\t\tmemset(pos, -1, sizeof(pos));\n\t\tmemset(out,false,sizeof(out));\n\t\tmemset(in, false,sizeof(in));\n\t\tmemset(seat, -1, sizeof(seat));\n\t\tpriority_queue<Event> qu;\n\t\tqueue<int> wait;\n\t\tfor(int i=0;i<m;i++){\n\t\t\tcin >> T[i] >> P[i] >> W[i] >> E[i];\n\t\t\tqu.push(Event(T[i],i,1));\n\t\t\tqu.push(Event(T[i]+W[i],i,3));\n\t\t}\n\t\twhile(!qu.empty()){\n\t\t\tEvent e = qu.top(); qu.pop();\n\t\t\tif(e.type == 0){\n\t\t\t\tfor(int i=0;i<c[pos[e.group]];i++){\n\t\t\t\t\tif(seat[pos[e.group]][i] == e.group) seat[pos[e.group]][i] = -1;\n\t\t\t\t}\n\t\t\t\tif(e.time<t) qu.push(Event(e.time,-1,2));\n\t\t\t}\n\t\t\tif(e.type == 1){\n\t\t\t\twhile(!wait.empty()&&out[wait.front()]){ wait.pop(); }\n\t\t\t\tif(wait.empty()){\n\t\t\t\t\tint idxA = -1, idxB = -1, mScore = -1, MScore = -1;\n\t\t\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\t\t\tif(c[i] < P[e.group]) continue;\n\t\t\t\t\t\tleft[0] = seat[i][0]==-1 ? 10000 : -1;\n\t\t\t\t\t\tfor(int j=1;j<c[i];j++){\n\t\t\t\t\t\t\tif(seat[i][j]!=-1) left[j] = -1;\n\t\t\t\t\t\t\telse if(left[j-1]==10000) left[j] = 10000;\n\t\t\t\t\t\t\telse left[j] = left[j-1]+1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tright[c[i]-1] = seat[i][c[i]-1]==-1 ? 10000 : -1;\n\t\t\t\t\t\tfor(int j=c[i]-2;j>=0;j--){\n\t\t\t\t\t\t\tif(seat[i][j]!=-1) right[j] = -1;\n\t\t\t\t\t\t\telse if(right[j+1]==10000) right[j] = 10000;\n\t\t\t\t\t\t\telse right[j] = right[j+1]+1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(int j=0;j<c[i];j++){\n\t\t\t\t\t\t\tif(seat[i][j]!=-1) seq[j] = -1;\n\t\t\t\t\t\t\telse if(j==0) seq[j] = 0;\n\t\t\t\t\t\t\telse seq[j] = seq[j-1]+1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(int j=0;j+P[e.group]-1<c[i];j++){\n\t\t\t\t\t\t\tif(left[j]!=-1&&seq[j]+P[e.group]-1==seq[j+P[e.group]-1]){\n\t\t\t\t\t\t\t\tif(idxA == -1||make_pair(mScore,MScore)<make_pair(min(left[j],right[j+P[e.group]-1]),max(left[j],right[j+P[e.group]-1]))){\n\t\t\t\t\t\t\t\t\tidxA = i; \n\t\t\t\t\t\t\t\t\tidxB = j; \n\t\t\t\t\t\t\t\t\tmScore = min(left[j],right[j+P[e.group]-1]);\n\t\t\t\t\t\t\t\t\tMScore = max(left[j],right[j+P[e.group]-1]);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(idxA == -1) { wait.push(e.group); continue; }\n\t\t\t\t\tfor(int i=0;i<P[e.group];i++) seat[idxA][idxB+i] = e.group;\n\t\t\t\t\tpos[e.group] = idxA;\n\t\t\t\t\tin[e.group] = true;\n\t\t\t\t\tscore[e.group] = 1.0;\n\t\t\t\t\tqu.push(Event(e.time+E[e.group], e.group, 0));\n\t\t\t\t} else {\n\t\t\t\t\twait.push(e.group);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(e.type == 2){\n\t\t\t\twhile(!wait.empty()){\n\t\t\t\t\tif(out[wait.front()]){ wait.pop(); continue; }\n\t\t\t\t\tint group = wait.front();\n\t\t\t\t\tint idxA = -1, idxB = -1, mScore = -1, MScore = -1;\n\t\t\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\t\t\tif(c[i] < P[group]) continue;\n\t\t\t\t\t\tleft[0] = seat[i][0]==-1 ? 10000 : -1;\n\t\t\t\t\t\tfor(int j=1;j<c[i];j++){\n\t\t\t\t\t\t\tif(seat[i][j]!=-1) left[j] = -1;\n\t\t\t\t\t\t\telse if(left[j-1]==10000) left[j] = 10000;\n\t\t\t\t\t\t\telse left[j] = left[j-1]+1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tright[c[i]-1] = seat[i][c[i]-1]==-1 ? 10000 : -1;\n\t\t\t\t\t\tfor(int j=c[i]-2;j>=0;j--){\n\t\t\t\t\t\t\tif(seat[i][j]!=-1) right[j] = -1;\n\t\t\t\t\t\t\telse if(right[j+1]==10000) right[j] = 10000;\n\t\t\t\t\t\t\telse right[j] = right[j+1]+1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(int j=0;j<c[i];j++){\n\t\t\t\t\t\t\tif(seat[i][j]!=-1) seq[j] = -1;\n\t\t\t\t\t\t\telse if(j==0) seq[j] = 0;\n\t\t\t\t\t\t\telse seq[j] = seq[j-1]+1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(int j=0;j+P[group]-1<c[i];j++){\n\t\t\t\t\t\t\tif(left[j]!=-1&&seq[j]+P[group]-1==seq[j+P[group]-1]){\n\t\t\t\t\t\t\t\tif(idxA == -1||make_pair(mScore,MScore)<make_pair(min(left[j],right[j+P[group]-1]),max(left[j],right[j+P[group]-1]))){\n\t\t\t\t\t\t\t\t\tidxA = i; \n\t\t\t\t\t\t\t\t\tidxB = j; \n\t\t\t\t\t\t\t\t\tmScore = min(left[j],right[j+P[group]-1]);\n\t\t\t\t\t\t\t\t\tMScore = max(left[j],right[j+P[group]-1]);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(idxA == -1) break;\n\t\t\t\t\tfor(int i=0;i<P[group];i++) seat[idxA][idxB+i] = group;\n\t\t\t\t\tpos[group] = idxA;\n\n\t\t\t\t\tin[group] = true;\n\t\t\t\t\tscore[group] = (T[group]+W[group]-e.time)/(double)W[group];\n\t\t\t\t\tqu.push(Event(e.time+E[group], group, 0));\n\t\t\t\t\twait.pop();\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(e.type == 3){\n\t\t\t\tif(in[e.group] == true) continue;\n\t\t\t\tout[e.group] = true;\n\t\t\t\tscore[e.group] = -1.0;\n\t\t\t\tif(e.time<t) qu.push(Event(e.time,-1,2));\n\t\t\t}\n\t\t}\n\t\tdouble ans = 0.0;\n\t\tint sum = 0;\n\t\tfor(int i=0;i<m;i++){\n\t\t\tans += score[i]*P[i];\n\t\t\tsum += P[i];\n\t\t}\n\t\tprintf(\"%.10lf\\n\", ans/sum);\n\t}\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 1904, "score_of_the_acc": -1.1408, "final_rank": 4 }, { "submission_id": "aoj_2055_133285", "code_snippet": "#include<iostream>\n#include<cassert>\n#include<vector>\n#include<map>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define ALL(C) (C).begin(),(C).end()\n\ntypedef long long ll;\n\nconst int N = 100;\nconst int M = 10000;\nconst int dinf=20000;\nconst ll inf = (1LL)<<50;\n\nclass Seat{\npublic:\n int a,b;\n ll time;\n bool operator<(const Seat & t)const{\n return a < t.a;\n }\n};\n\nint ind,amax,amin,al,ar;\n\nvoid update(int tindex,int l,int r,int posl,int posr){\n if (l > r)swap(l,r);\n \n if (ind==-1||\n (l>amin)||\n (l==amin&&r >amax)){\n //||(l==amin&&r==amax&&tindex < ind)||\n //(l==amin&&r==amax&&tindex==ind&&posl<al)){\n ind=tindex;\n amin=l;\n amax=r;\n al=posl;\n ar=posr;\n }\n}\n\nvoid makeupdate(int tindex,int b,int a,int cap,int req){\n int tmp = a-b-1;\n if (tmp < req)return;\n int val=tmp-req;\n int l,r;\n l=val/2;r=val/2;\n// if (tmp%2 == 1 && req%2 == 0){\n// r++;\n// }else if (tmp%2 == 0 && req%2 == 1){\n// r++;\n// }\n if (val%2 == 1)r++;\n update(tindex,l,r,b+1+l,a-1-r);\n}\n\nvoid check(int cap,vector<Seat> &info,int req,int tindex){\n if (info.size() == 0){\n if (req > cap)return;\n if(req-1<cap)update(tindex,dinf,dinf,0,req-1);\n return;\n }\n \n if (info[0].a >= req){\n update(tindex,dinf,info[0].a-req,0,req-1);\n } \n\n rep(i,(int)info.size()-1){\n makeupdate(tindex,info[i].b,info[i+1].a,cap,req);\n }\n\n if (cap-info[info.size()-1].b-1 >=req){\n update(tindex,cap-req-info[info.size()-1].b-1,dinf,cap-req,cap-1);\n }\n}\n\nvoid getseat(int n,vector<Seat> *a,int *cap,int req){\n rep(i,n){\n if (cap[i]>=req)check(cap[i],a[i],req,i);\n }\n}\n\ndouble solve(int n,int *cap,ll cl,int m,ll *come,int *num,ll *wait,ll *eat){\n ll now=-1;\n int top=0;\n double sum=0,div=0;\n vector<Seat> seat[N];\n rep(i,m)div+=num[i];\n \n while(top < m){\n ll fast=inf;\n rep(i,n){\n rep(j,seat[i].size()){\n\tfast=min(fast,seat[i][j].time);\n }\n }\n \n if (now < come[top] && come[top] < fast){//arrive\n now=come[top];\n }else if (fast != inf && fast > come[top]+wait[top]){//leave from queue\n now=come[top]+wait[top];\n sum+=(-1)*num[top];\n top++;\n }else now = fast;////leave from seat\n \n // cout << now <<\" \" << cl << endl;\n\n if (now >=cl)break;\n\n rep(i,n){\n for(int j=(int)seat[i].size()-1;j>=0;j--){\n\tif (seat[i][j].time <= now){\n\t seat[i].erase(seat[i].begin()+j);\n\t}\n }\n }\n\n// rep(i,n){\n// rep(j,seat[i].size()){\n// \tcout <<\"seat info \" <<i<<\" \" << seat[i][j].a <<\" \" << seat[i][j].b <<\" \" << seat[i][j].time<<endl;\n// }\n// }\n \n while(top < m){\n while(top<m){\n\tif (now > come[top]+wait[top]){\n\t sum+=(-1)*num[top];\n\t}else break;\n\ttop++;\n }\n \n if (top == m || now < come[top])break;\n \n ind=-1;\n getseat(n,seat,cap,num[top]);\n if (ind == -1)break;\n \n seat[ind].pb((Seat){al,ar,now+eat[top]});\n // cout << top <<\" can sit \" << ind <<\" \" << al <<\" \" << ar <<\" \"<<\n //\tnow <<\" \" << now+eat[top] << endl;\n sort(ALL(seat[ind]));\n sum+=((wait[top]-(now-come[top]))/(double)wait[top])*num[top];\n top++;\n }\n }\n REP(i,top,m)sum+=(-1)*num[i];\n return sum/div;\n}\n\nmain(){\n int n,m;\n ll t;\n int cap[N];\n int num[M];\n ll come[M],wait[M],eat[M];\n while(cin>>n>>m>>t && n){\n rep(i,n)cin>>cap[i];\n rep(i,m)cin>>come[i]>>num[i]>>wait[i]>>eat[i];\n printf(\"%.16lf\\n\",solve(n,cap,t,m,come,num,wait,eat));\n }\n return false;\n}\n\n\n/*\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29\n0 0 0 x x x 4 4 4 7 7 7 2 2 2 8 8 8 3 3 3 9 9 9 1 1 1 \n*/", "accuracy": 1, "time_ms": 1190, "memory_kb": 1428, "score_of_the_acc": -0.6042, "final_rank": 1 } ]
aoj_2051_cpp
Problem D: Rotation Estimation Mr. Nod is an astrologist and has defined a new constellation. He took two photos of the constellation to foretell a future of his friend. The constellation consists of n stars. The shape of the constellation in these photos are the same, but the angle of them are different because these photos were taken on a different day. He foretells a future by the difference of the angle of them. Your job is to write a program to calculate the difference of the angle of two constellation. Input The input is a sequence of datasets. Each dataset is given in the following format: n x 1,1 y 1,1 ... x 1, n y 1, n x 2,1 y 2,1 ... x 2, n y 2, n The first line of each dataset contains a positive integers n ( n ≤ 1,000). The next n lines contain two real numbers x 1, i and y 1, i (| x 1, i |, | y 1, i | ≤ 100), where ( x 1, i , y 1, i ) denotes the coordinates of the i -th star of the constellation in the first photo. The next n lines contain two real numbers x 2, i and y 2, i (| x 2, i |, | y 2, i | ≤ 100), where ( x 2, i , y 2, i ) denotes the coordinates of the i -th star of the constellation in the second photo. Note that the ordering of the stars does not matter for the sameness. It is guaranteed that distance between every pair of stars in each photo is larger than 10 -5 . The input is terminated in case of n = 0. This is not part of any datasets and thus should not be processed. Output For each dataset, you should print a non-negative real number which is the difference of the angle of the constellation in the first photo and in the second photo. The difference should be in radian, and should not be negative. If there are two or more solutions, you should print the smallest one. The difference may be printed with any number of digits after decimal point, provided the absolute error does not exceed 10 -7 . No extra space or character is allowed. Sample Input 3 0.0 0.0 1.0 1.0 0.0 1.0 3.0 3.0 2.0 2.0 3.0 2.0 0 Output for the Sample Input 3.14159265359
[ { "submission_id": "aoj_2051_5500120", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n#define EPS 0.000000001\n#define SIZE 1005\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nint N;\nPoint A[SIZE],B[SIZE],work[SIZE];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\tPoint mid_A = Point(0,0);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&A[i].x,&A[i].y);\n\t\tmid_A = mid_A + A[i];\n\t}\n\tmid_A = mid_A/N;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tA[i] = A[i]-mid_A;\n\t}\n\tsort(A,A+N);\n\n\tPoint mid_B = Point(0,0);\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&B[i].x,&B[i].y);\n\t\tmid_B = mid_B + B[i];\n\t}\n\n\tmid_B = mid_B/N;\n\tfor(int i = 0; i < N; i++){\n\n\t\tB[i] = B[i]-mid_B;\n\t}\n\n\tdouble ans = BIG_NUM;\n\n\tdouble base_rad = calc_rad(A[0]);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdouble tmp_rad = calc_rad(B[i]);\n\t\tfor(int k = 0; k < N; k++){\n\n\t\t\twork[k] = rotate(Point(0,0),B[k],base_rad-tmp_rad);\n\t\t}\n\t\tsort(work,work+N);\n\n\t\tbool FLG = true;\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(A[k] == work[k]){\n\n\t\t\t\t//Do nothing\n\n\t\t\t}else{\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(!FLG)continue;\n\n\t\tdouble diff = base_rad-tmp_rad;\n\t\tif(diff < 0){\n\n\t\t\tdiff += 2*M_PI;\n\t\t}\n\n\t\tif(diff > M_PI){\n\n\t\t\tdiff = 2*M_PI-diff;\n\t\t}\n\n\t\tans = min(ans,diff);\n\t}\n\n\tprintf(\"%.12lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3800, "score_of_the_acc": -1.2212, "final_rank": 18 }, { "submission_id": "aoj_2051_5500115", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n#define EPS 0.000000001\n#define SIZE 1005\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nint N;\nPoint A[SIZE],B[SIZE],work[SIZE];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\nbool DEBUG = false;\n\nvoid func(){\n\n\tPoint mid_A = Point(0,0);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&A[i].x,&A[i].y);\n\t\tmid_A = mid_A + A[i];\n\t}\n\tmid_A = mid_A/N;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tA[i] = A[i]-mid_A;\n\t}\n\tsort(A,A+N);\n\n\tif(DEBUG){\n\n\t\tfor(int i = 0; i < N; i++){\n\n\t\t\tprintf(\"A[%d] \",i);\n\t\t\tA[i].debug();\n\t\t}\n\t}\n\n\tPoint mid_B = Point(0,0);\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&B[i].x,&B[i].y);\n\t\tmid_B = mid_B + B[i];\n\t}\n\n\tmid_B = mid_B/N;\n\tfor(int i = 0; i < N; i++){\n\n\t\tB[i] = B[i]-mid_B;\n\t}\n\n\tdouble ans = BIG_NUM;\n\n\tdouble base_rad = calc_rad(A[0]);\n\n\tif(DEBUG){\n\n\t\tprintf(\"base_rad:%.3lf\\n\",base_rad);\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdouble tmp_rad = calc_rad(B[i]);\n\t\tfor(int k = 0; k < N; k++){\n\n\t\t\twork[k] = rotate(Point(0,0),B[k],base_rad-tmp_rad);\n\t\t}\n\t\tsort(work,work+N);\n\n\t\tif(DEBUG){\n\t\t\tprintf(\"\\ni:%d\\n\",i);\n\t\t\tfor(int k = 0; k < N; k++){\n\n\t\t\t\twork[k].debug();\n\t\t\t}\n\t\t}\n\n\t\tbool FLG = true;\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(A[k] == work[k]){\n\n\t\t\t\t//Do nothing\n\n\t\t\t}else{\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(!FLG)continue;\n\n\t\tdouble diff = base_rad-tmp_rad;\n\t\tif(diff < 0){\n\n\t\t\tdiff += 2*M_PI;\n\t\t}\n\n\t\tif(diff > M_PI){\n\n\t\t\tdiff = 2*M_PI-diff;\n\t\t}\n\n\t\tif(DEBUG){\n\n\t\t\tprintf(\"diff:%.3lf\\n\",diff);\n\t\t}\n\n\t\tans = min(ans,diff);\n\t}\n\n\tprintf(\"%.12lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3780, "score_of_the_acc": -1.1925, "final_rank": 17 }, { "submission_id": "aoj_2051_3948859", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nstruct Point {\n\tdouble x, y;\n\tstruct Vector operator-(const Point& that) const;\n\tdouble distance_from(const Point& that) const;\n};\nstruct Vector {\n\tdouble dx, dy;\n\tdouble dot(const Vector& that) const;\n\tdouble cross(const Vector& that)const;\n\tdouble length() const;\n\tstruct Angle to_angle() const;\n\tstruct Angle angle_with(const Vector& that) const;\n\tVector rotate(const struct Angle& angle) const;\n\tVector operator-(const Vector& that) const;\n};\nstruct Angle {\n\tdouble sin, cos;\n\tdouble to_radian() const;\n\tAngle operator-() const;\n};\nint main() {\n\tstd::vector<double> result;\n\twhile (true) {\n\t\tint n; std::cin >> n; if (n == 0) break;\n\t\tstd::vector<Point> before_points(n), after_points(n);\n\t\tfor (auto& point : before_points) std::cin >> point.x >> point.y;\n\t\tfor (auto& point : after_points) std::cin >> point.x >> point.y;\n\t\tif (n == 1) {\n\t\t\t//std::cout << 0 << '\\n';\n\t\t\tresult.push_back(0);\n\t\t\tcontinue;\n\t\t}\n\t\tconst auto origin = *std::min_element(before_points.begin(), before_points.end(), [](const Point& a, const Point& b) {return (a.y != b.y) ? a.y < b.y : a.x < b.x; });\n\t\tstd::vector<Vector> before_vec; before_vec.reserve(n - 1);\n\t\tfor (const auto& point : before_points) {\n\t\t\tif (origin.distance_from(point) > 1e-9) before_vec.push_back(point - origin);\n\t\t}\n\t\tconst auto base = *std::max_element(before_vec.begin(), before_vec.end(), [](const Vector& a, const Vector& b) {return std::abs(a.cross(b)) <= 1e-9 ? a.length() < b.length() : a.cross(b) < 0; });\n\t\tconst auto angle = base.to_angle();\n\t\tfor (auto& vec : before_vec) vec = vec.rotate(-angle); \n\t\tstd::sort(before_vec.begin(), before_vec.end(), [base](const Vector& a, const Vector& b) { return std::abs(a.cross(b)) < 1e-12 ? a.length() < b.length() : a.cross(b) < 0; });\n\t\tdouble min_radian = DBL_MAX;\n\t\tfor (auto i = 0; i < n; ++i) {\n\t\t\tstd::vector<Vector> after_mapped; after_mapped.reserve(n - 1);\n\t\t\tfor (auto j = 0; j < n; ++j) if (i != j) after_mapped.push_back(after_points[j] - after_points[i]);\n\t\t\tconst auto new_base = *std::max_element(after_mapped.begin(), after_mapped.end(), [](const Vector& a, const Vector& b) {return std::abs(a.cross(b)) <= 1e-9 ? a.length() < b.length() : a.cross(b) < 0; });\n\t\t\tconst auto new_angle = new_base.to_angle();\n\t\t\tfor (auto& vec : after_mapped) vec = vec.rotate(-new_angle);\n\t\t\tif (std::any_of(after_mapped.begin(), after_mapped.end(), [](const Vector& a) {return a.dy < -1e-9; }))\n\t\t\t\tcontinue;\n\t\t\tstd::sort(after_mapped.begin(), after_mapped.end(), [](const Vector& a, const Vector& b) { return std::abs(a.cross(b)) < 1e-12 ? a.length() < b.length() : a.cross(b) < 0; });\n\t\t\tbool is_match = true;\n\t\t\tfor (auto j = 0; j < n - 1; ++j) {\n\t\t\t\tif ((after_mapped[j] - before_vec[j]).length() > 1e-6) {\n\t\t\t\t\tis_match = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (is_match) {\n\t\t\t\tmin_radian = std::min(min_radian, std::acos(base.angle_with(new_base).cos));\n\t\t\t}\n\t\t}\n\t\t//std::cout << std::setprecision(15) << std::fixed << min_radian << '\\n';\n\t\tresult.push_back(min_radian);\n\t}\n\tfor (const auto r : result) {\n\t\tstd::cout << std::setprecision(10) << std::fixed << r << '\\n';\n\t}\n}\n\n\nVector Point::operator-(const Point& that) const\n{\n\treturn Vector{ x - that.x, y - that.y };\n}\n\ndouble Point::distance_from(const Point& that) const\n{\n\treturn (*this - that).length();\n}\n\ndouble Vector::dot(const Vector& that) const\n{\n\treturn dx * that.dx + dy * that.dy;\n}\n\ndouble Vector::cross(const Vector& that) const\n{\n\treturn dx * that.dy - dy * that.dx;\n}\n\ndouble Vector::length() const\n{\n\treturn std::sqrt(dx * dx + dy * dy);\n}\n\nAngle Vector::to_angle() const\n{\n\tconst auto len = length();\n\treturn Angle{ dy / len, dx / len };\n}\n\nAngle Vector::angle_with(const Vector& that) const\n{\n\tconst auto len = length() * that.length();\n\treturn Angle{ cross(that) / len, dot(that) / len };\n}\n\nVector Vector::rotate(const Angle& angle) const\n{\n\treturn Vector{ dx * angle.cos - dy * angle.sin, dy * angle.cos + dx * angle.sin };\n}\n\nVector Vector::operator-(const Vector& that) const\n{\n\treturn Vector{ dx - that.dx, dy - that.dy };\n}\n\ndouble Angle::to_radian() const\n{\n\treturn (sin >= 0) ? std::acos(cos) : std::acos(-1) * 2 - std::acos(cos);\n}\n\nAngle Angle::operator-() const\n{\n\treturn Angle{ -sin, cos };\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3460, "score_of_the_acc": -0.9199, "final_rank": 11 }, { "submission_id": "aoj_2051_3645090", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\nconst ld eps = 1e-4;\nconst ld pi = acos(-1.0);\ntypedef pair<ld, ld> LDP;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\ntypedef vector<string> svec;\n\nbool eq(ld a, ld b) {\n\treturn (abs(a - b) < eps);\n}\n\nclass Line {\npublic:\n\tPoint a, b;\n};\nclass Circle {\npublic:\n\tPoint p; ld r;\n};\n\nld dot(Point a, Point b) {\n\treturn real(conj(a)*b);\n}\nld cross(Point a, Point b) {\n\treturn imag(conj(a)*b);\n}\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//a,b,cが反時計回り\n\tif (cross(b, c) < -eps)return -1;//a,b,cが時計回り\n\tif (dot(b, c) < 0)return 2;//c,a,bの順に一直線\n\tif (norm(b) < norm(c))return -2;//a,b,cの順に一直線\n\treturn 0;//a,c,bの順に一直線\n}\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\n//直線lに幅dをつける\nvector<Line> make_w(Line l, ld d) {\n\tPoint dif = l.b - l.a;\n\tdif = dif*Point{0, 1};\n\tdif = dif * (d / abs(dif));\n\tvector<Line> ret;\n\tfor (int id = 1; id >= -1; id -= 2) {\n\t\tPoint a = l.a + dif * (ld)id;\n\t\tPoint b = l.b + dif * (ld)id;\n\t\tret.push_back({ a,b });\n\t}\n\treturn ret;\n}\n\ntypedef vector<Point> polygon;\npolygon ConvexHull(polygon p) {\n\tint n = p.size();\n\tint k = 0;\n\tsort(p.begin(), p.end());\n\tpolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = p[i++]) {\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) <= 0)--k;\n\t}\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) <= 0)--k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\npolygon convex_cut(const polygon &p, Line l) {\n\tvector<Point> ret;\n\trep(i, p.size()) {\n\t\tPoint a = p[i], b = p[(i + 1) % p.size()];\n\t\tif (ccw(l.a, l.b, a) != -1)ret.push_back(a);\n\t\tif (ccw(l.a, l.b, a)*ccw(l.a, l.b, b)<0) {\n\t\t\tret.push_back(is_ll({ a,b }, l));\n\t\t}\n\t}\n\treturn ret;\n}\nld area(const polygon &p){\n\tld ret = 0;\n\tint n = p.size();\n\trep(j, n)ret += cross(p[j], p[(j + 1) % n]);\n\treturn ret / 2.0;\n}\nld max_distance(const polygon &p) {\n\t//assert(p.size()>1);\n\tpolygon g = ConvexHull(p);\n\tint n = g.size(), a = 0, b = 1;\n\tld ret = abs(g[0] - g[1]);\n\twhile (a < n) {\n\t\tPoint p1 = g[a%n], p2 = g[(a + 1) % n];\n\t\tPoint q1 = g[b%n], q2 = g[(b + 1) % n];\n\t\tif (arg((p2 - p1) / (q1 - q2)) > 0)++b; else ++a;\n\t\tret = max(ret, abs(p1 - q1));\n\t}\n\treturn ret;\n}\n\nvector<Point> is_cc(Circle c1, Circle c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2 * d);\n\tld dfr = c1.r*c1.r - rc * rc;\n\tif (abs(dfr) < eps)dfr = 0.0;\n\tif (dfr < 0.0)return res;\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0)res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\nbool in_Circle(Circle a, Circle b) {\n\tld dist = abs(b.p - a.p);\n\treturn dist < b.r - a.r+eps;\n}\n\nll gcd(ll x, ll y) {\n\tx = abs(x), y = abs(y);\n\tif (x < y)swap(x, y);\n\twhile (y) {\n\t\tll r = x % y; x = y; y = r;\n\t}\n\treturn x;\n}\n\n//0/0だけ気を付けて!\nstruct ratio {\n\tll a, b;\n\tratio(ll x) { a = x; b = 1; };\n\tratio(ll x, ll y) {\n\t\ta = x; b = y;\n\t\tll g = gcd(x, y);\n\t\tif (g > 0) {\n\t\t\ta /= g, b /= g;\n\t\t\tif (b < 0)a = -a, b = -b;\n\t\t}\n\t};\n\n};\n\nvoid normalize(ratio &r) {\n\tll g = gcd(r.a, r.b);\n\tr.a /= g; r.b /= g;\n\tif (r.b < 0)r.b = -r.b, r.a = -r.a;\n}\nbool operator<(const ratio &x, const ratio &y) {\n\treturn x.a*y.b < x.b*y.a;\n}\nbool operator==(const ratio &x, const ratio &y) {\n\treturn x.a*y.b == x.b*y.a;\n}\n\nbool isis_cc(Circle a, Circle b) {\n\tld dist = abs(b.p - a.p);\n\treturn dist < a.r + b.r;\n}\nint n;\n\nbool same(vector<Point> &a, vector<Point> &b, Point c, ld theta) {\n\tvector<Point>u;\n\trep(i, n) {\n\t\tPoint dif = a[i] - a[0];\n\t\tdif = dif * Point{ cos(theta),sin(theta) };\n\t\tPoint nex = a[0] + dif + c;\n\t\tu.push_back(nex);\n\t}\n\tsort(u.begin(), u.end());\n\trep(i, n) {\n\t\tif (abs(u[i] - b[i]) > eps)return false;\n\t}\n\treturn true;\n}\nvoid solve() {\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tsort(b.begin(), b.end());\n\tld ans = mod;\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tif (i == j)continue;\n\t\t\tld dist = abs(b[i] - b[j]);\n\t\t\tif (!eq(dist,abs(a[0] - a[1])))continue;\n\t\t\tPoint c = b[i] - a[0];\n\t\t\tld t1 = arg(a[1] - a[0]);\n\t\t\tld t2 = arg(b[j] - b[i]);\n\t\t\tld t = t2 - t1;\n\t\t\t//cout << t2 << \" \"<<t1 << endl;\n\t\t\twhile (t > 2 * pi)t -= 2 * pi;\n\t\t\twhile (t < 0)t += 2 * pi;\n\t\t\tif (eq(t, 2*pi))t = 0;\n\t\t\t//cout << t << endl;\n\t\t\tif (same(a, b, c, t)) {\n\t\t\t\tans = min(ans, t);\n\t\t\t\tans = min(ans, 2 * pi - t);\n\t\t\t}\n\t\t}\n\t}\n\tif (ans == mod)ans = 0;\n\tcout << ans << endl;\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(11);\n\twhile (cin >> n, n) {\n\t\tsolve();\n\t}\n\t//solve();\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3656, "score_of_the_acc": -1.4142, "final_rank": 19 }, { "submission_id": "aoj_2051_3643918", "code_snippet": "#include<iomanip>\n#include<limits>\n#include<thread>\n#include<utility>\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<set>\n#include<map>\n#include<vector>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<numeric>\n#include<cassert>\n#include<random>\n#include<chrono>\n#include<unordered_set>\n#include<unordered_map>\n#include<fstream>\n#include<list>\n#include<functional>\n#include<bitset>\n#include<complex>\n#include<tuple>\nusing namespace std;\ntypedef unsigned long long int ull;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\ntypedef long double D;\ntypedef complex<D> P;\n#define F first\n#define S second\nconst ll E=1e18+7;\nconst ll MOD=1000000007;\n\n\ntemplate<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}\ntemplate<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}\ntemplate<typename T,typename U>ostream & operator << (ostream &o,pair<T,U> &A){o<<A.F<<\" \"<<A.S; return o;}\ntemplate<typename T>ostream & operator << (ostream &o,vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?\" \":\"\");} return o;}\ntemplate<typename T>vector<T> & cset(vector<T> &A,T e=T()){for(auto &I:A){I=e;} return A;}\n\nnamespace Geometry{\n typedef long double D;\n typedef complex<long double> P;\n typedef pair<P,D> C;\n \n const D EPS=1e-9;\n const D PI=asin(1)*2;\n const D INF=1e18;\n \n const static bool comp(const P &p1,const P &p2){return p1.real()==p2.real()?p1.imag()<p2.imag():p1.real()<p2.real();}\n \n D dot(P p1,P p2){return p1.real()*p2.real()+p1.imag()*p2.imag();}\n \n D cross(P p1,P p2){return p1.real()*p2.imag()-p1.imag()*p2.real();}\n \n P project(P vec,P x){return vec*(x/vec).real();}\n \n P project(P p1,P p2,P x){return p1+project(p2-p1,x-p1);}\n \n P reflect(P vec,P x){return vec*conj(x/vec);}\n \n P reflect(P p1,P p2,P x){return p1+reflect(p2-p1,x-p1);}\n \n bool intersectSL(P p1,P p2,P vec){vec/=abs(vec); p1/=vec; p2/=vec; return (p1.imag()<EPS && p2.imag()>-EPS) || (p1.imag()>-EPS && p2.imag()<EPS);}\n \n bool intersectSL(P p1,P p2,P p3,P p4){return intersectSL(p1-p4,p2-p4,p3-p4);}\n \n bool intersectSS(P p1,P p2,P p3,P p4){return (dot(p2-p1,p3-p1)<-EPS && dot(p2-p1,p4-p1)<-EPS) || (dot(p1-p2,p3-p2)<-EPS && dot(p1-p2,p4-p2)<-EPS)?false:intersectSL(p1,p2,p3,p4) && intersectSL(p3,p4,p1,p2);}\n \n D distLP(P vec,P x){return abs((x/vec).imag())*abs(vec);}\n \n D distLP(P p1,P p2,P x){return distLP(p2-p1,x-p1);}\n \n D distSP(P p1,P p2,P x){return dot(p2-p1,x-p1)<-EPS?abs(x-p1):dot(p1-p2,x-p2)<-EPS?abs(x-p2):distLP(p1,p2,x);}\n \n D distSS(P p1,P p2,P p3,P p4){return intersectSS(p1,p2,p3,p4)?0.0:min(min(distSP(p1,p2,p3),distSP(p1,p2,p4)),min(distSP(p3,p4,p1),distSP(p3,p4,p2)));}\n \n P crosspointLL(P p1,P p2,P vec){return abs(cross(p2-p1,vec))<EPS?vec:vec*cross(p2-p1,p2)/cross(p2-p1,vec);}\n \n P crosspointLL(P p1,P p2,P p3,P p4){return p4+crosspointLL(p1-p4,p2-p4,p3-p4);}\n \n P crosspointSS(P p1,P p2,P p3,P p4){return distSP(p1,p2,p3)<EPS?p3:distSP(p1,p2,p4)<EPS?p4:crosspointLL(p1,p2,p3,p4);}\n \n bool intersectShL(P p1,P p2,P vec){vec/=abs(vec); return intersectSL(p1,p2,vec) && crosspointLL(p1/vec,p2/vec,vec/vec).real()>-EPS;}\n \n bool intersectShL(P p1,P p2,P p3,P p4){return intersectShL(p1-p3,p2-p3,p4-p3);}\n \n //1::in,0::on edge,-1::out\n int contain(const vector<P> &poly,const P &p){\n vector<P> A={{65537,96847},{-24061,6701},{56369,-86509},{-93763,-78049},{56957,10007}};\n vector<bool> cnt(5,false);\n for(int i=1;i<=poly.size();i++){\n if(distSP(poly[i-1],poly[i%poly.size()],p)<EPS){return 0;}\n for(int j=0;j<5;j++){\n if(intersectShL(poly[i-1],poly[i%poly.size()],p,p+A[j])){cnt[j]=!cnt[j];}\n }\n }\n int in=0;\n for(int j=0;j<5;j++){if(cnt[j]){in++;}}\n return in>=3?1:-1;\n }\n \n vector<P> convexcut(const vector<P> &poly,P p1,P p2){\n vector<P> ret;\n for(int i=1;i<=poly.size();i++){\n if(cross(p2-p1,poly[i-1]-p1)>-EPS){ret.push_back(poly[i-1]);}\n if(intersectSL(poly[i-1],poly[i%poly.size()],p1,p2) && distLP(p1,p2,poly[i-1])>EPS && distLP(p1,p2,poly[i%poly.size()])>EPS){ret.push_back(crosspointLL(poly[i-1],poly[i%poly.size()],p1,p2));}\n }\n return ret;\n }\n \n D area(const vector<P> &poly){\n D ans=0;\n for(int i=2;i<poly.size();i++){ans+=cross(poly[i-1]-poly[0],poly[i]-poly[0]);}\n return abs(ans)/2;\n }\n \n vector<P> convexhull(vector<P> pts){\n vector<P> ret;\n sort(pts.begin(),pts.end(),comp);\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n reverse(pts.begin(),pts.end());\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n if(ret[0]==ret.back()){ret.pop_back();}\n return ret;\n }\n \n //4::seperate,3::circumscribe,2::intersect,1::inscribe,0::contain,-1::same\n int intersectCC(C c1,C c2){\n D d=abs(c1.F-c2.F),r=c1.S+c2.S,dif=abs(c2.S-c1.S);\n if(d<EPS && dif<EPS){return -1;}\n if(d-r>EPS){return 4;}\n if(d-r>-EPS){return 3;}\n if(d-dif>EPS){return 2;}\n if(d-dif>-EPS){return 1;}\n return 0;\n }\n \n vector<P> crosspointLC(P p1,P p2,C c){\n vector<P> ret;\n P pr=project(p1,p2,c.F);\n D d=distLP(p1,p2,c.F);\n if(d-c.S>EPS){return ret;}\n if(d-c.S>-EPS){ret.push_back(pr); return ret;}\n P vec=p2-p1; vec*=sqrt(c.S*c.S-d*d)/abs(vec);\n ret.push_back(pr-vec);\n ret.push_back(pr+vec);\n return ret;\n }\n \n vector<P> crosspointSC(P p1,P p2,C c){\n vector<P> ret;\n for(auto &I:crosspointLC(p1,p2,c)){if(distSP(p1,p2,I)<EPS){ret.push_back(I);}}\n return ret;\n }\n \n vector<P> crosspointCC(C c1,C c2){\n vector<P> ret;\n P vec=c2.F-c1.F;\n D base=(c1.S*c1.S+norm(vec)-c2.S*c2.S)/(2*abs(vec));\n D h=sqrt(c1.S*c1.S-base*base);\n vec/=abs(vec);\n ret.push_back(c1.F+vec*P(base,-h));\n ret.push_back(c1.F+vec*P(base,h));\n return ret;\n }\n \n vector<P> tangentCP(C c,P p){return crosspointCC(c,C(p,sqrt(norm(c.F-p)-c.S*c.S)));}\n \n vector<pair<P,P>> tangentCC(C c1,C c2){\n vector<pair<P,P>> ret;\n P d=c2.F-c1.F;\n for(D i:{-1,1}){\n D r=c1.S+c2.S*i;\n if(intersectCC(c1,c2)>i+1){\n for(P s:{-1i,1i}){\n P p=r+s*sqrt(norm(d)-norm(r));\n ret.push_back({c1.F+d*c1.S/norm(d)*p,c2.F-d*i*c2.S/norm(d)*p});\n }\n }\n }\n return ret;\n }\n \n D area(const vector<P> &poly,C c){\n D ret=0;\n for(int i=0;i<poly.size();i++){\n P a=poly[i]-c.F,b=poly[(i+1)%poly.size()]-c.F;\n if(abs(a)<c.S+EPS && abs(b)<c.S+EPS){ret+=cross(a,b);}\n else{\n vector<P> A=crosspointSC(a,b,{0,c.S});\n if(A.empty()){ret+=c.S*c.S*arg(b/a);}\n else{\n ret+=(abs(a)<c.S?cross(a,A[0]):c.S*c.S*arg(A[0]/a));\n ret+=(abs(b)<c.S?cross(A.back(),b):c.S*c.S*arg(b/A.back()));\n ret+=cross(A[0],A.back());\n }\n }\n }\n return abs(ret)/2;\n }\n \n //反時計回り\n D diameter(const vector<P> &poly){\n D ret=0;\n ll l=0,r=0,n=poly.size();\n if(n==2){return abs(poly[0]-poly[1]);}\n for(int i=0;i<n;i++){\n if(comp(poly[l],poly[i])){l=i;}\n if(comp(poly[i],poly[r])){r=i;}\n }\n ll sl=r,sr=l;\n while(sl!=l || sr!=r){\n ret=max(ret,abs(poly[r]-poly[l]));\n if(cross(poly[(l+1)%n]-poly[l],poly[(r+1)%n]-poly[r])<0){(++l)%=n;}\n else{(++r)%=n;}\n }\n return ret;\n }\n \n D closestpair(vector<P> pt){\n sort(pt.begin(),pt.end(),comp);\n D ret=INF;\n for(ll i=1;i<pt.size();i<<=1){\n for(ll j=0;i+j<pt.size();j+=i*2){\n ll m=i+j;\n vector<P> R;\n D l=-INF,r=INF;\n for(ll k=j;k<m;k++){l=max(l,pt[k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){r=min(r,pt[m+k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){if(pt[m+k].real()-l<ret){R.push_back(pt[m+k]);}}\n ll idx=0;\n for(ll k=j;k<m;k++){\n if(r-pt[k].real()>ret){continue;}\n while(idx<R.size() && pt[k].imag()-R[idx].imag()>ret){idx++;}\n for(ll n=idx;n<R.size() && R[n].imag()-pt[k].imag()<ret;n++){ret=min(ret,abs(R[n]-pt[k]));}\n }\n inplace_merge(pt.begin()+j,pt.begin()+m,j+i*2<pt.size()?pt.begin()+j+2*i:pt.end(),[](const P &a,const P &b){return a.imag()==b.imag()?a.real()<b.real():a.imag()<b.imag();});\n }\n }\n return ret;\n }\n \n P centerofgravity(const vector<P> &pt){\n P ret(0,0);\n D wt=0;\n for(int i=2;i<pt.size();i++){\n D w2=cross(pt[i-1]-pt[0],pt[i]-pt[0]);\n P p=(pt[0]+pt[i-1]+pt[i])/(D)3;\n wt+=w2;\n ret+=p*w2;\n }\n return ret/wt;\n }\n \n istream & operator >> (istream &i,P &p){D x,y; i>>x>>y; p={x,y}; return i;}\n istream & operator >> (istream &i,C &p){D x,y; i>>x>>y>>p.S; p.F={x,y}; return i;}\n};\n\nusing namespace Geometry;\n\n\n\n\nint main(){\n ll n;\n while(cin>>n,n){\n vector<P> A(n),B(n);\n cin>>A>>B;\n P mid1(0,0),mid2(0,0);\n for(auto &I:A){mid1+=I;} mid1/=n;\n for(auto &I:B){mid2+=I;} mid2/=n;\n for(auto &I:A){I-=mid1;}\n for(auto &I:B){I-=mid2;}\n sort(A.begin(),A.end(),[](P a,P b){return arg(a)+EPS<arg(b) || (abs(arg(a)-arg(b))<EPS*2 && abs(a)<abs(b));});\n sort(B.begin(),B.end(),[](P a,P b){return arg(a)+EPS<arg(b) || (abs(arg(a)-arg(b))<EPS*2 && abs(a)<abs(b));});\n //cout<<A<<endl<<B<<endl;\n D ans=E;\n ll k=-1;\n for(int i=0;i<n;i++){if(abs(B[i])>EPS){k=i;}}\n for(int i=0;i<n;i++){if(abs(B[i])>1){k=i;}}\n if(k==-1){ans=0;}\n for(int i=0;i<n && k!=-1;i++){\n D dif=0;\n P vec=A[i]/abs(A[i])/(B[k]/abs(B[k]));\n for(int j=0;j<n;j++){\n dif+=abs(A[(i+j)%n]/vec-B[(k+j)%n]);\n }\n if(dif<EPS*n){ans=min(ans,abs(arg(vec)));}\n }\n cout<<fixed<<setprecision(12)<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3388, "score_of_the_acc": -1.0788, "final_rank": 15 }, { "submission_id": "aoj_2051_3106199", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <set>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n if(n==1){\n double d;\n cin >> d >> d >> d >> d;\n cout << 0 << endl;\n continue;\n }\n\n //重心を原点に平行移動\n vector<VP> p(2, VP(n));\n for(int d=0; d<2; d++){\n P centroid(0, 0);\n for(int i=0; i<n; i++){\n double x,y;\n cin >> x >> y;\n p[d][i] = P(x, y);\n centroid += p[d][i];\n }\n centroid /= n;\n //p0が重心に重なると角度が取れなくなるので例外処理\n if(p[d][0] == centroid){\n swap(p[d][0], p[d][1]);\n }\n for(int i=0; i<n; i++){\n p[d][i] -= centroid;\n }\n }\n\n //同じ点が2点以上ないという仮定で、全部このsetに入って入ればokとする\n set<P> pset;\n for(int i=0; i<n; i++){\n pset.insert(p[0][i]);\n }\n double ans = INF;\n for(int i=0; i<n; i++){\n //p[1][i]を回転させてp[0][0]に合わせる\n if(!EQ(abs(p[0][0]), abs(p[1][i]))) continue;\n double angle = arg(p[1][i]/p[0][0]);\n bool ok = true;\n for(int j=0; j<n; j++){\n P rot = rotate(p[1][j], -angle);\n if(pset.find(rot) == pset.end()){\n ok = false;\n break;\n }\n }\n if(ok){\n ans = min(ans, abs(angle));\n }\n }\n cout << fixed << setprecision(10);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3756, "score_of_the_acc": -1.0163, "final_rank": 14 }, { "submission_id": "aoj_2051_2859319", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\n\n/* 幾何の基本 */\n\n#include <complex>\n\ntypedef complex<ld> Point;\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// 点の入力\nPoint input_Point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// 誤差つき等号判定\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// 内積\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// 外積\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// 直線の定義\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// 円の定義\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// ccw\n// 1: a,b,cが反時計周りの順に並ぶ\n//-1: a,b,cが時計周りの順に並ぶ\n// 2: c,a,bの順に直線に並ぶ\n//-2: a,b,cの順に直線に並ぶ\n// 0: a,c,bの順に直線に並ぶ\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,cが反時計周りの順に並ぶ\n\tif (cross(nb, nc) < -eps) return -1; // a,b,cが時計周りの順に並ぶ\n\tif (dot(nb, nc) < 0) return 2; // c,a,bの順に直線に並ぶ\n\tif (norm(nb) < norm(nc)) return -2; // a,b,cの順に直線に並ぶ\n\treturn 0; // a,c,bの順に直線に並ぶ\n}\n\n\n/* 交差判定 */\n\n// 直線と直線の交差判定\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// 直線と線分の交差判定\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// 線分と線分の交差判定\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 点の直線上判定\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// 点の線分上判定\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// 垂線の足\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);\n\treturn l.a + t * (l.b - l.a);\n}\n\n//線対象の位置にある点\nPoint reflect(const Line &l, const Point &p) {\n\tPoint pr = proj(l, p);\n\treturn pr * 2.l - p;\n}\n\n// 直線と直線の交点\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// 直線と直線の交点\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// 線分と線分の交点\n// 重なってる部分あるとassert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//先にisis_ssしてね\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// 線分と線分の交点\nvector<Point> is_ss2(const Line &s, const Line& t) {\n\tvector<Point> kouho(is_ll2(s, t));\n\tvector<Point>ans;\n\tfor (auto p : kouho) {\n\t\tif (isis_sp(s, p) && isis_sp(t, p))ans.emplace_back(p);\n\t}\n\treturn ans;\n}\n// 直線と点の距離\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n//直線と直線の距離\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// 直線と線分の距離\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// 線分と点の距離\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//直線と直線の二等分線のベクトル\nLine line_bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n//点と点の垂直二等分線 aを左に見ながら\nLine point_bisection(const Point&a, const Point&b) {\n\tconst Point cen((a + b) / 2.l);\n\tconst Point vec = (b - a)*Point(0, 1);\n\treturn Line(cen, cen + vec);\n}\n\n//三つの点からなる外心\nPoint outer_center(const vector<Point>&ps) {\n\tassert(ps.size() == 3);\n\tLine l1 = point_bisection(ps[0], ps[1]);\n\tLine l2 = point_bisection(ps[1], ps[2]);\n\treturn is_ll(l1, l2);\n}\n\n\n//三つの直線からなる内心\n//三つの直線が並行でないことは確かめといてね\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(line_bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(line_bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//三つの直線からなる傍心\n//三つの直線が並行でないことは確かめといてね\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(line_bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(line_bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:並行\n//c:並行でない\n//三つの直線から同距離の位置を求める。\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(line_bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(line_bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(line_bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* 円 */\n\n// 円と円の交点\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n/*\n点が円の中にいるか\n0 => out\n1 => on\n2 => in*/\nint is_in_Circle(const Point& p, const Circle &cir) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\nint is_in_Circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n/*\n円lcが円rcの中にいるか\n0 => out\n1 => on\n2 => in*/\nint Circle_in_Circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// 円と直線の交点\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// 円と線分の距離\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// 円と点の接線\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// 円と円の接線\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), nret.begin(), nret.end());\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//二つの円の重なり面積\nld two_Circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* 多角形 */\n\ntypedef vector<Point> Polygon;\n\n\n\n//多角形(複数の点)の最小包含円をO(N=頂点数)で求めるアルゴリズム\n//同一直線上に三つの点がないこと\n#include<random>\nCircle welzl(vector<Point>ps) {\n\tstruct solver {\n\t\tCircle solve(vector<Point>&ps, vector<Point>&rs) {\n\t\t\tif (ps.empty() || rs.size() == 3) {\n\t\t\t\tif (rs.size() == 1) {\n\t\t\t\t\treturn Circle(Point(rs[0]), 0);\n\t\t\t\t}\n\t\t\t\telse if (rs.size() == 2) {\n\t\t\t\t\treturn Circle((rs[0] + rs[1]) / 2.0l, abs(rs[1] - rs[0]) / 2);\n\t\t\t\t}\n\t\t\t\telse if (rs.size() == 3) {\n\t\t\t\t\tvector<Line> ls(3);\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tls[i] = Line(rs[i], rs[(i + 1) % 3]);\n\t\t\t\t\t}\n\t\t\t\t\tPoint center = outer_center(rs);\n\t\t\t\t\treturn Circle(center, abs(center - rs[0]));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn Circle(Point(), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\n\t\t\t\tPoint p_ba = ps.back();\n\t\t\t\tps.pop_back();\n\t\t\t\tCircle d = solve(ps, rs);\n\t\t\t\tps.push_back(p_ba);\n\t\t\t\tif (is_in_Circle(d, p_ba)) {\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\trs.push_back(p_ba);\n\t\t\t\t\tps.pop_back();\n\t\t\t\t\tauto ans = solve(ps, rs);\n\t\t\t\t\tps.push_back(p_ba);\n\t\t\t\t\trs.pop_back();\n\t\t\t\t\treturn ans;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}so;\n\tstd::random_device rd;\n\tstd::mt19937 mt(rd());\n\tshuffle(ps.begin(), ps.end(), mt);\n\tvector<Point>rs;\n\tCircle ans = so.solve(ps, rs);\n\treturn ans;\n}\n// 面積\nld get_area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tfor (int j = 0; j<n; ++j) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n//多角形の回転方向\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// 円の内外判定\n/*0 => out\n1 => on\n2 => in*/\nint is_in_Polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//円の内外判定2 高速\nenum { out, on, in };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// 凸包\n//点や線を返すことも有り得るので注意\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n//凸カット\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon q;\n\tPolygon r;\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = ps[i], b = ps[(i + 1) % n];\n\t\tLine m = Line(a, b);\n\t\tif (ccw(l.a, l.b, a) != -1) q.push_back(a);\n\t\tif (ccw(l.a, l.b, a) != 1) r.push_back(a);\n\t\tif (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0 && isis_ll(l, m)) {\n\t\t\tq.push_back(is_ll(l, m));\n\t\t\tr.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ q,r };\n\treturn polys;\n}\n\nPolygon cen(Polygon po) {\n\tPoint cen;\n\tfor (auto p : po) {\n\t\tcen += p;\n\t}\n\tcen /= po.size();\n\tfor (auto&p : po) {\n\t\tp -= cen;\n\t}\n\treturn po;\n}\n\nint main() {\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\tcout<<setprecision(10)<<fixed;\n\twhile (true){\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\t\tPolygon p1,p2;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tld x,y; cin>>x>>y;\n\t\t\tp1.emplace_back(x,y);\n\t\t}\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tld x,y; cin>>x>>y;\n\t\t\tp2.emplace_back(x,y);\n\t\t}\n\t\tp1=cen(p1);\n\t\tp2=cen(p2);\n\n\t\tld ans=1e9;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tld theta1=atan2l(p1[0].imag(),p1[0].real());\n\t\t\tld theta2=atan2l(p2[i].imag(),p2[i].real());\n\n\t\t\tif (theta2 > theta1) {\n\t\t\t\ttheta1+=2*pi;\n\t\t\t}\n\t\t\tld sa_theta=theta1-theta2;\n\n\t\t\tPolygon next_poly(p2);\n\t\t\tfor (auto &p : next_poly) {\n\t\t\t\tPoint np;\n\t\t\t\tnp=Point(p.real()*cos(sa_theta)-p.imag()*sin(sa_theta),p.real()*(sin(sa_theta))+p.imag()*cos(sa_theta));\n\t\t\t\tp=np;\n\t\t\t}\n\n\t\t\tauto comp = [](const Point&l, const Point&r)-> bool { return abs(l.real() - r.real()) < eps ? abs(l.imag() - r.imag()) < eps ? false : l.imag() < r.imag() : l.real() < r.real(); };\n\t\t\t std::set <Point, decltype(comp)> aa(comp);\n\n\t\t\t for (auto p : next_poly) {\n\t\t\t\t aa.emplace(p);\n\t\t\t }\n\t\t\t for (auto p : p1) {\n\t\t\t\t aa.emplace(p);\n\t\t\t }\n\n\t\t\t if (aa.size() == N) {\n\t\t\t\t ans=min(ans,sa_theta);\n\t\t\t\t ans=min(ans,2*pi-sa_theta);\n\t\t\t }\n\n\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 3820, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2051_1679722", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cstring>\n#include <iostream>\n\nusing namespace std;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define TRACE(x) cout << #x << \" = \" << x << endl\n#define _ << \" _ \" <<\n\ntypedef long long llint;\n\nconst int MAXN = 1000;\nconst double eps = 1e-7;\n\ninline bool lt(double x, double y) { return x+eps < y; }\ninline bool gt(double x, double y) { return lt(y, x); }\ninline bool eq(double x, double y) { return !lt(x, y) && !gt(x, y); }\n\nstruct Pt {\n double x, y;\n Pt operator - (const Pt &b) { return Pt{x-b.x, y-b.y}; }\n};\n\ninline double norm(const Pt &a) {\n return sqrt(a.x*a.x + a.y*a.y);\n}\n\ninline double ccw(const Pt &a, const Pt &b, const Pt &c) {\n return a.x * (b.y-c.y) + b.x * (c.y-a.y) + c.x * (a.y-b.y);\n}\n\nint n;\nPt P1[MAXN], P2[MAXN], P3[MAXN];\nvector<Pt> H1, H2;\n\ndouble rot1, rot2, ans;\n\nbool load(void) {\n scanf(\"%d\", &n);\n if (n == 0) return false;\n REP (i, n) scanf(\"%lf%lf\", &P1[i].x, &P1[i].y);\n REP (i, n) scanf(\"%lf%lf\", &P2[i].x, &P2[i].y);\n return true;\n}\n\nbool cmp(const Pt &a, const Pt &b) {\n if (!eq(a.x, b.x)) return lt(a.x, b.x);\n return lt(a.y, b.y);\n}\n\nvoid convex_hull(Pt *P, int n, vector<Pt> &h) {\n sort(P, P+n, cmp);\n\n int lsize = 0, usize = 0;\n vector<Pt> lo, up;\n for (int i = 0; i < n; ++i) {\n while (lsize >= 2 && !lt(ccw(lo[lsize-2], lo[lsize-1], P[i]), 0.0)) {\n --lsize;\n lo.pop_back();\n }\n lo.push_back(P[i]);\n ++lsize;\n }\n\n for (int i = n-1; i >= 0; --i) {\n while (usize >= 2 && !lt(ccw(up[usize-2], up[usize-1], P[i]), 0.0)) {\n --usize;\n up.pop_back();\n }\n up.push_back(P[i]);\n ++usize;\n }\n\n h.clear();\n for (int i = 0; i < lsize; ++i)\n h.push_back(lo[i]);\n for (int i = 1; i < usize-1; ++i)\n h.push_back(up[i]);\n}\n\nvoid translate(Pt *p, int n, double dx, double dy) {\n REP (i, n) p[i].x += dx, p[i].y += dy;\n}\n\nvoid rotate(Pt *p, int n, double cos_alpha, double sin_alpha) {\n REP (i, n) {\n double nx = p[i].x * cos_alpha - p[i].y * sin_alpha,\n ny = p[i].x * sin_alpha + p[i].y * cos_alpha;\n p[i] = Pt{nx, ny};\n }\n}\n\nbool check(int id) {\n for (int sgn: {-1, 1}) {\n memcpy(P3, P2, sizeof P2);\n translate(P3, n, -H2[id].x, -H2[id].y); \n int nid = (id+1) % (int)H2.size();\n Pt v = H2[nid]-H2[id];\n rotate(P3, n, sgn*v.y/norm(v), sgn*v.x/norm(v));\n sort(P3, P3+n, cmp);\n\n bool ok = true;\n REP (i, n) {\n if (!eq(P1[i].x, P3[i].x)) { ok = false; break; }\n if (!eq(P1[i].y, P3[i].y)) { ok = false; break; }\n }\n if (ok) {\n rot2 = atan2(sgn*v.y, sgn*v.x);\n double tmp = rot2 - rot1 + 2*M_PI;\n while (gt(tmp, 2*M_PI)) tmp -= 2*M_PI;\n ans = min(ans, min(tmp, 2*M_PI-tmp));\n }\n }\n return false;\n}\n\nvoid solve(void) {\n if (n == 1) {\n printf(\"0.0\\n\");\n return;\n }\n\n ans = 10.0;\n\n convex_hull(P1, n, H1);\n convex_hull(P2, n, H2);\n\n translate(P1, n, -H1[0].x, -H1[0].y);\n Pt v = H1[1]-H1[0];\n rotate(P1, n, v.y/norm(v), v.x/norm(v));\n rot1 = atan2(v.y/norm(v), v.x/norm(v));\n sort(P1, P1+n, cmp);\n\n REP (i, n) check(i);\n printf(\"%.10lf\\n\", ans);\n}\n\n\nint main(void) {\n\n while (load()) solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3496, "score_of_the_acc": -1.1841, "final_rank": 16 }, { "submission_id": "aoj_2051_1364823", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 1000;\n\nconst double DELTA = 1e-4;\nconst double DELTA2 = 1e-8;\nconst double PI = acos(-1);\nconst double PI2 = PI * 2;\n\n/* typedef */\n\ntemplate <typename T>\nstruct Pt {\n T x, y;\n\n Pt() {}\n Pt(T _x, T _y) : x(_x), y(_y) {}\n Pt(const Pt& pt) : x(pt.x), y(pt.y) {}\n\n bool operator==(const Pt pt) const { return x == pt.x && y == pt.y; }\n Pt<T> operator+(const Pt pt) const { return Pt<T>(x + pt.x, y + pt.y); }\n Pt<T> operator-() const { return Pt<T>(-x, -y); }\n Pt<T> operator-(const Pt pt) const { return Pt<T>(x - pt.x, y - pt.y); }\n Pt<T> operator*(T t) const { return Pt<T>(x * t, y * t); }\n Pt<T> operator/(T t) const { return Pt<T>(x / t, y / t); }\n T dot(Pt v) const { return x * v.x + y * v.y; }\n T cross(Pt v) const { return x * v.y - y * v.x; }\n Pt<T> mid(const Pt pt) { return Pt<T>((x + pt.x) / 2, (y + pt.y) / 2); }\n T d2() { return x * x + y * y; }\n double d() { return sqrt(d2()); }\n\n Pt<T> rot(double th) {\n double c = cos(th), s = sin(th);\n return Pt<T>(c * x - s * y, s * x + c * y);\n }\n\n bool operator<(const Pt& pt) const {\n return x < pt.x || (x == pt.x && y < pt.y);\n }\n\n void print(string format) {\n printf((\"(\" + format + \", \" + format + \")\\n\").c_str(), x, y);\n }\n void print() { print(\"%.6lf\"); }\n};\n\ntypedef Pt<double> pt;\n\n/* global variables */\n\nint n;\npt pts[2][MAX_N], rpts[MAX_N];\n\n/* subroutines */\n\nbool pt_comp(const pt& pt0, const pt& pt1) {\n double dx = pt0.x - pt1.x, dy = pt0.y - pt1.y;\n return (dx <= -DELTA) || (dx < DELTA && dy <= -DELTA);\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n for (int j = 0; j < 2; j++) {\n pt ctr(0.0, 0.0);\n for (int i = 0; i < n; i++) {\n\tcin >> pts[j][i].x >> pts[j][i].y;\n\tctr = ctr + pts[j][i];\n }\n ctr = ctr / n;\n //ctr.print();\n\n for (int i = 0; i < n; i++)\n\tpts[j][i] = pts[j][i] - ctr;\n }\n\n pt *pts0 = pts[0], *pts1 = pts[1];\n double min_th = 10;\n\n pt& pto = pts0[0];\n double tho = atan2(pto.y, pto.x);\n double d2o = pto.d2();\n\n for (int i = 0; i < n; i++) {\n pt& pti = pts1[i];\n double d2i = pti.d2();\n if (abs(d2i - d2o) < DELTA2) {\n\tdouble thi = atan2(pti.y, pti.x);\n\tdouble dth = thi - tho;\n\tif (dth > PI) dth -= PI2;\n\telse if (dth < -PI) dth += PI2;\n\n\tdouble adth = abs(dth);\n\tif (min_th <= adth) continue;\n\t//cout << dth << endl;\n\n\tdouble cdth = cos(dth), sdth = sin(dth);\n\tfor (int j = 0; j < n; j++) {\n\t rpts[j].x = cdth * pts0[j].x - sdth * pts0[j].y;\n\t rpts[j].y = sdth * pts0[j].x + cdth * pts0[j].y;\n\t}\n\tsort(rpts, rpts + n, pt_comp);\n\n\tbool found = true;\n\tfor (int j = 0; j < n; j++)\n\t if (! binary_search(rpts, rpts + n, pts1[j], pt_comp)) {\n\t found = false;\n\t break;\n\t }\n\n\tif (found) min_th = adth;\n }\n }\n\n printf(\"%.10lf\\n\", min_th);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1372, "score_of_the_acc": -0.0509, "final_rank": 3 }, { "submission_id": "aoj_2051_1155675", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\n#define diff(P, i) (next(P, i) - curr(P, i))\ntypedef double number;\ntypedef complex<double> Point;\nconst double EPS = 1e-5;\ntypedef Point P;\ntypedef vector<Point> Polygon;\ndouble dot(P a, P b){return real(conj(a) * b);}\ndouble cross(P a, P b){return imag(conj(a) * b);}\ndouble angle(P a, P b){return arg(conj(a) * b);}\nP rotate(P a, double b, P c = P()){return (a - c) * polar(1.0, b) + c;}\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > +EPS) return +1; // 反時計回り\n if (cross(b, c) < -EPS) return -1; // 時計回り\n if (dot(b, c) < 0) return +2; // c--a--b の順番で一直線上\n if (norm(b) < norm(c)) return -2; // a--b--c の順番で一直線上\n return 0; // 点が線分ab上にある\n}\nint sign(double x) {\n return (x > EPS) - (x < -EPS);\n}\nint comp(double x, double y) {\n return sign(x - y);\n}\nnamespace std{\n bool operator < (const P& a, const P& b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n return comp(a.real(), b.real()) ? comp(a.real(), b.real()) < 0 : comp(a.imag(), b.imag()) < 0;\n }\n};\nbool near(Point a, Point b) {\n return comp(a.real(), b.real()) == 0 && comp(a.imag(), b.imag()) == 0;\n}\nbool equal(Polygon p, Polygon q) {\n sort(p.begin(), p.end());\n sort(q.begin(), q.end());\n REP(i, p.size()) {\n if(!near(p[i], q[i])) return false;\n }\n return true;\n}\nPolygon convex_hull(vector<P> ps, vector<int>& res) {\n int n = ps.size(), k = 0;\n sort(ps.begin(), ps.end(),[](const P&a, const P&b){\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n });\n\n vector<P> ch(2*n);\n res.resize(2*n);\n for (int i = 0; i < n;\n res[k] = i, ch[k++] = ps[i++]){ // lower-hull\n while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;\n }\n for (int i = n-2, t = k+1; i >= 0;\n res[k] = i, ch[k++] = ps[i--]){ // upper-hull\n while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;\n }\n ch.resize(k-1);\n res.resize(k-1);\n return ch;\n}\npair<int, int> convex_diameter(const Polygon &pt) {\n const int n = pt.size();\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i) {\n if (imag(pt[i]) > imag(pt[is])) is = i;\n if (imag(pt[i]) < imag(pt[js])) js = i;\n }\n number maxd = norm(pt[is]-pt[js]);\n\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do {\n if (cross(diff(pt,i), diff(pt,j)) >= 0) j = (j+1) % n;\n else i = (i+1) % n;\n if (norm(pt[i]-pt[j]) > maxd) {\n maxd = norm(pt[i]-pt[j]);\n maxi = i; maxj = j;\n }\n } while (i != is || j != js);\n return make_pair(maxi, maxj); /* farthest pair is (maxi, maxj). */\n}\n\nint main(){\n int n;\n while(cin >> n && n > 0) {\n Polygon p(n), q(n);\n REP(i, n) {\n double x, y;\n cin >> x >> y;\n p[i] = {x, y};\n }\n REP(i, n) {\n double x, y;\n cin >> x >> y;\n q[i] = {x, y};\n }\n if(n == 1) {\n printf(\"%.10f\\n\", 0.0);\n continue;\n }\n vector<int> idx;\n auto pc = convex_hull(p, idx);\n auto max_p = convex_diameter(pc);\n int max_i = idx[max_p.first];\n int max_j = idx[max_p.second];\n double max_len = abs(p[max_i] - p[max_j]);\n double ans = 1e64;\n REP(i, n) REP(j, n) if(i != j) {\n double len = abs(q[i] - q[j]);\n if(comp(len, max_len) != 0) continue;\n auto qt = q;\n P dif = p[max_i] - qt[i];\n REP(k, qt.size()) {\n qt[k] += dif;\n }\n assert(near(p[max_i], qt[i]));\n // double th = angle(p[max_j] - p[max_i], qt[j] - qt[i]);\n // REP(k, qt.size()) {\n // qt[k] = rotate(qt[k], -th, qt[i]);\n // }\n double th = angle(qt[j] - qt[i], p[max_j] - p[max_i]);\n REP(k, qt.size()) {\n qt[k] = rotate(qt[k], th, qt[i]);\n }\n assert(near(p[max_i], qt[i]));\n assert(near(p[max_j], qt[j]));\n if(!equal(p, qt)) continue;\n th = abs(th);\n ans = min(ans, th);\n }\n printf(\"%.10f\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1460, "score_of_the_acc": -0.2525, "final_rank": 9 }, { "submission_id": "aoj_2051_1039771", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n \n};\n \nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n \nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n \ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\ninline bool comp_arg(const double &a,const double &b) { return !equals(a,b) && a < b; }\n \nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n \n vector<double> arg_buf;\n rep(i,n) {\n double dist[2] = { getDist(ps[0][0]), getDist(ps[1][i]) };\n if( !equals(dist[0],dist[1]) ) continue;\n double arg = atan2(ps[0][0].y,ps[0][0].x) - atan2(ps[1][i].y,ps[1][i].x);\n while( arg < 0 ) arg += 2*M_PI;\n arg_buf.push_back(arg);\n }\n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n\n double mini = DINF; \n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) mini = min(mini,min(arg,2*M_PI-arg));\n }\n printf(\"%.11lf\\n\",mini);\n}\n \nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1408, "score_of_the_acc": -0.1069, "final_rank": 8 }, { "submission_id": "aoj_2051_1039768", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n \n};\n \nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n \nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n \ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\n \nbool comp_arg(const double &a,const double &b) { return !equals(fabs(a),fabs(b)) && fabs(a) < fabs(b); }\n \nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n sort(ps[1],ps[1]+n);\n \n vector<double> arg_buf;\n rep(i,min(10,n)) rep(j,n) {\n double dist[2] = { getDist(ps[0][i]), getDist(ps[1][j]) };\n if( !equals(dist[0],dist[1]) ) continue;\n arg_buf.push_back( atan2(ps[0][i].y,ps[0][i].x) - atan2(ps[1][j].y,ps[1][j].x) );\n }\n \n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n \n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) { printf(\"%.10lf\\n\",fabs(arg)); break; }\n }\n \n}\n \nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1512, "score_of_the_acc": -0.1064, "final_rank": 6 }, { "submission_id": "aoj_2051_1039744", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n \n};\n \nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n \nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n \ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\n \nbool comp_arg(const double &a,const double &b) { return !equals(fabs(a),fabs(b)) && fabs(a) < fabs(b); }\n \nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n \n vector<double> arg_buf;\n rep(i,min(10,n)) rep(j,n) {\n double dist[2] = { getDist(ps[0][i]), getDist(ps[1][j]) };\n if( !equals(dist[0],dist[1]) ) continue;\n arg_buf.push_back( atan2(ps[0][i].y,ps[0][i].x) - atan2(ps[1][j].y,ps[1][j].x) );\n }\n \n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n \n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) { printf(\"%.10lf\\n\",fabs(arg)); break; }\n }\n \n}\n \nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1512, "score_of_the_acc": -0.1064, "final_rank": 6 }, { "submission_id": "aoj_2051_1039740", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n \n};\n \nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n \nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n \ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\n \nbool comp_arg(const double &a,const double &b) { return !equals(fabs(a),fabs(b)) && fabs(a) < fabs(b); }\n \nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n sort(ps[1],ps[1]+n);\n \n vector<double> arg_buf;\n rep(i,min(7,n)) rep(j,n) {\n double dist[2] = { getDist(ps[0][i]), getDist(ps[1][j]) };\n if( !equals(dist[0],dist[1]) ) continue;\n arg_buf.push_back( atan2(ps[0][i].y,ps[0][i].x) - atan2(ps[1][j].y,ps[1][j].x) );\n }\n \n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n \n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) { printf(\"%.10lf\\n\",fabs(arg)); break; }\n }\n \n}\n \nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1468, "score_of_the_acc": -0.089, "final_rank": 5 }, { "submission_id": "aoj_2051_1039737", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n \n};\n \nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n \nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n \ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\n \nbool comp_arg(const double &a,const double &b) { return !equals(fabs(a),fabs(b)) && fabs(a) < fabs(b); }\n \nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n sort(ps[1],ps[1]+n);\n \n vector<double> arg_buf;\n rep(i,min(10,n)) rep(j,n) {\n double dist[2] = { getDist(ps[0][i]), getDist(ps[1][j]) };\n if( !equals(dist[0],dist[1]) ) continue;\n arg_buf.push_back( atan2(ps[0][i].y,ps[0][i].x) - atan2(ps[1][j].y,ps[1][j].x) );\n }\n \n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n \n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) { printf(\"%.10lf\\n\",fabs(arg)); break; }\n }\n \n}\n \nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1512, "score_of_the_acc": -0.0856, "final_rank": 4 }, { "submission_id": "aoj_2051_1039697", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n\ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\n\nbool comp_arg(const double &a,const double &b) { return !equals(fabs(a),fabs(b)) && fabs(a) < fabs(b); }\n\nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n sort(ps[1],ps[1]+n);\n\n vector<double> arg_buf;\n rep(i,n) rep(j,n) {\n double dist[2] = { getDist(ps[0][i]), getDist(ps[1][j]) };\n if( !equals(dist[0],dist[1]) ) continue;\n double arg = atan2(ps[0][i].y,ps[0][i].x) - atan2(ps[1][j].y,ps[1][j].x);\n arg_buf.push_back(arg);\n }\n\n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n\n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) { printf(\"%.11lf\\n\",fabs(arg)); break; }\n }\n\n}\n\nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3356, "score_of_the_acc": -0.9412, "final_rank": 12 }, { "submission_id": "aoj_2051_1039680", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\nconst double DINF = 1e20;\nint n;\nPoint ps[2][1010],tmp[1010];\n\ninline double getDist(Point p){ return sqrt(p.x*p.x+p.y*p.y); }\n\nbool comp_arg(const double &a,const double &b) { return !equals(fabs(a),fabs(b)) && fabs(a) < fabs(b); }\n\nvoid compute(){\n Point grv[2] = {Point(),Point()};\n rep(i,2) rep(j,n) grv[i] = grv[i] + ps[i][j];\n rep(i,2) grv[i] = grv[i] / (double)n;\n rep(i,2) rep(j,n) ps[i][j] = ps[i][j] - grv[i];\n sort(ps[0],ps[0]+n);\n sort(ps[1],ps[1]+n);\n\n vector<double> arg_buf;\n rep(i,n) rep(j,n) {\n double dist[2] = { getDist(ps[0][i]), getDist(ps[1][j]) };\n if( !equals(dist[0],dist[1]) ) continue;\n double arg = atan2(ps[0][i].y,ps[0][i].x) - atan2(ps[1][j].y,ps[1][j].x);\n arg_buf.push_back(arg);\n }\n\n sort(arg_buf.begin(),arg_buf.end(),comp_arg);\n arg_buf.erase(unique(arg_buf.begin(),arg_buf.end()),arg_buf.end());\n\n rep(i,(int)arg_buf.size()){\n double arg = arg_buf[i];\n rep(j,n) tmp[j] = rotate(ps[1][j],arg);\n sort(tmp,tmp+n);\n bool failed = false;\n rep(j,n) if( !( tmp[j] == ps[0][j] ) ) { failed = true; break; }\n if( !failed ) { printf(\"%.11lf\\n\",fabs(arg)); break; }\n }\n\n}\n\nint main(){\n while( cin >> n, n ){\n rep(i,2) rep(j,n) cin >> ps[i][j].x >> ps[i][j].y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3360, "score_of_the_acc": -0.9427, "final_rank": 13 }, { "submission_id": "aoj_2051_772883", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <vector>\n\n#define repi(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,a) repi(i,0,a)\n#define repd(i,a,b) for(int i = (a) ; i >= (b); i--)\n#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i!= (a).end(); i++)\n#define all(u) (u).begin(),(u).end()\n#define rall(u) (u).rbegin(),(u).rend()\n#define UNIQUE(u) (u).erase(unique(all(u)),(u).end)\n\n#define pb push_back\n#define mp make_pair\n#define INF 1e9\n#define EPS 1e-6\n#define PI acos(-1.0)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef complex<double> P;\ntypedef vector<P> G;\nint n;\n\nnamespace std{\n bool operator<(const P &a, const P &b) {\n\treturn abs(real(a)-real(b)) > EPS ? real(a) < real(b): imag(a) < imag(b);\n }\n}\n\nstruct cmp\n{\n bool operator ()(const double &a, const double &b) {\n\treturn a + EPS < b;\n }\n};\n\nP rotate(const P &a, double th) { return a*polar(1.0, th); }\ndouble angle(const P &a, const P &b) { return arg(conj(a)*b); }\n\nP centerG(const G &g) {\n P p = P(0, 0);\n rep(i, n) p += g[i];\n p /= n;\n return p;\n}\n\nint main(){\n while(cin >> n, n) {\n\tG g1(n), g2(n);\n\trep(i, n) cin >> g1[i].real() >> g1[i].imag();\n\trep(i, n) cin >> g2[i].real() >> g2[i].imag();\n\tP c1 = centerG(g1), c2 = centerG(g2);\n\trep(i, n) {\n\t g1[i] -= c1;\n\t g2[i] -= c2;\n\t}\n\t//cout <<\"c \" << c1 << \" \" << c2 << endl;\n\tG tg = g2;\n\tdouble ans = INF;\n\tsort(all(g1));\n\trep(i, n) {\n\t g2 = tg;\n\t double th = angle(g2[i], g1[0]); \n\t rep(j, n) g2[j] = rotate(g2[j], th);\n\t bool flag = true;\n\t \n\t sort(all(g2));\n\t rep(j, n) if(abs(g1[j] - g2[j]) > EPS) flag = false;\n\t \n\t //cout << \"a \"<< i << endl;\n\t //rep(j, n) cout << g1[j] << g2[j] << endl;\n\t \n\t if(flag) {\n\t\tif(th < 0) th = -th;\n\t\tif(th > PI) th = 2*PI - th;\n\t\tans = min(ans, th);\n\t }\n\t}\n\tprintf(\"%.11f\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 1396, "score_of_the_acc": -0.4146, "final_rank": 10 }, { "submission_id": "aoj_2051_572988", "code_snippet": "#include<set>\n#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double EPS=1e-7; // EPS=1e-8 だと誤差で間違う\nconst double INF=1e77;\nconst double PI=acos(-1);\n\nstruct point{\n\tdouble x,y;\n\tpoint():x(0),y(0){}\n\tpoint(double x,double y):x(x),y(y){}\n\tpoint &operator+=(const point &a){ x+=a.x; y+=a.y; return *this; }\n\tpoint &operator-=(const point &a){ x-=a.x; y-=a.y; return *this; }\n\tpoint &operator/=(double c){ x/=c; y/=c; return *this; }\n\tpoint operator+(const point &a)const{ return point(x+a.x,y+a.y); }\n\tpoint operator-(const point &a)const{ return point(x-a.x,y-a.y); }\n\tbool operator< (const point &a)const{ return x+EPS<a.x || abs(x-a.x)<EPS && y+EPS<a.y; }\n};\n\ndouble cross(const point &a,const point &b){ return a.x*b.y-a.y*b.x; }\n\ndouble abs(const point &a){ return sqrt(a.x*a.x+a.y*a.y); }\n\ndouble arg(const point &a){ double t=atan2(a.y,a.x); return t<0?t+2*PI:t; }\n\npoint rot(const point &a,double theta){ return point(a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)); }\n\ntypedef vector<point> polygon;\n\nenum{CCW=1,CW=-1,ON=0};\nint ccw(const point &a,const point &b,const point &c){\n\tdouble rdir=cross(b-a,c-a);\n\tif(rdir> EPS) return CCW;\n\tif(rdir<-EPS) return CW;\n\treturn ON;\n}\n\npolygon convex_hull(vector<point> &P){\n\tint n=P.size();\n\tpolygon CH;\n\tif(n<=1){\n\t\tCH.insert(CH.end(),P.begin(),P.end());\n\t\treturn CH;\n\t}\n\tsort(P.begin(),P.end());\n\n\trep(_,2){\n\t\tint m=0;\n\t\tvector<point> half(n);\n\t\trep(i,n){\n\t\t\thalf[m++]=P[i];\n\t\t\twhile(m>=3 && ccw(half[m-3],half[m-2],half[m-1])==CW){\n\t\t\t\thalf[m-2]=half[m-1];\n\t\t\t\tm--;\n\t\t\t}\n\t\t}\n\t\tCH.insert(CH.end(),half.begin(),half.begin()+m-1);\n\t\treverse(P.begin(),P.end());\n\t}\n\n\treturn CH;\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tvector<point> P(n),Q(n);\n\t\trep(i,n) scanf(\"%lf%lf\",&P[i].x,&P[i].y);\n\t\trep(i,n) scanf(\"%lf%lf\",&Q[i].x,&Q[i].y);\n\n\t\tif(n==1){\n\t\t\tputs(\"0\");\n\t\t\tcontinue;\n\t\t}\n\n\t\tpolygon F=convex_hull(P);\n\t\tpolygon G=convex_hull(Q);\n\n\t\tset<point> S(Q.begin(),Q.end());\n\n\t\tdouble ans=INF;\n\t\trep(i,G.size()){\n\t\t\tpoint v=G[i]-F[0];\n\t\t\tdouble theta=arg(G[(i+1)%G.size()]-G[i])-arg(F[1]-F[0]);\n\n\t\t\tbool ok=true;\n\t\t\trep(j,n){\n\t\t\t\tif(S.count(rot(P[j]-F[0],theta)+F[0]+v)==0){\n\t\t\t\t\tok=false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\ttheta=abs(theta);\n\t\t\t\twhile(theta>2*PI) theta-=2*PI;\n\t\t\t\tans=min(ans,theta);\n\t\t\t\tans=min(ans,2*PI-theta);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.9f\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1296, "score_of_the_acc": -0.0417, "final_rank": 1 }, { "submission_id": "aoj_2051_572987", "code_snippet": "#include<set>\n#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double EPS=1e-7; // EPS=1e-8 だと誤差で間違う\nconst double INF=1e77;\nconst double PI=acos(-1);\n\nstruct point{\n\tdouble x,y;\n\tpoint():x(0),y(0){}\n\tpoint(double x,double y):x(x),y(y){}\n\tpoint &operator+=(const point &a){ x+=a.x; y+=a.y; return *this; }\n\tpoint &operator-=(const point &a){ x-=a.x; y-=a.y; return *this; }\n\tpoint &operator/=(double c){ x/=c; y/=c; return *this; }\n\tpoint operator+(const point &a)const{ return point(x+a.x,y+a.y); }\n\tpoint operator-(const point &a)const{ return point(x-a.x,y-a.y); }\n\tbool operator< (const point &a)const{ return x+EPS<a.x || abs(x-a.x)<EPS && y+EPS<a.y; }\n};\n\ndouble cross(const point &a,const point &b){ return a.x*b.y-a.y*b.x; }\n\ndouble abs(const point &a){ return sqrt(a.x*a.x+a.y*a.y); }\n\ndouble arg(const point &a){ double t=atan2(a.y,a.x); return t<0?t+2*PI:t; }\n\npoint rot(const point &a,double theta){ return point(a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)); }\n\ntypedef vector<point> polygon;\n\nenum{CCW=1,CW=-1,ON=0};\nint ccw(const point &a,const point &b,const point &c){\n\tdouble rdir=cross(b-a,c-a);\n\tif(rdir> EPS) return CCW;\n\tif(rdir<-EPS) return CW;\n\treturn ON;\n}\n\npolygon convex_hull(vector<point> &P){\n\tint n=P.size();\n\tpolygon CH;\n\tif(n<=1){\n\t\tCH.insert(CH.end(),P.begin(),P.end());\n\t\treturn CH;\n\t}\n\tsort(P.begin(),P.end());\n\n\trep(_,2){\n\t\tint m=0;\n\t\tvector<point> half(n);\n\t\trep(i,n){\n\t\t\thalf[m++]=P[i];\n\t\t\twhile(m>=3 && ccw(half[m-3],half[m-2],half[m-1])==CW){\n\t\t\t\thalf[m-2]=half[m-1];\n\t\t\t\tm--;\n\t\t\t}\n\t\t}\n\t\tCH.insert(CH.end(),half.begin(),half.begin()+m-1);\n\t\treverse(P.begin(),P.end());\n\t}\n\n\treturn CH;\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tvector<point> P(n),Q(n);\n\t\trep(i,n) scanf(\"%lf%lf\",&P[i].x,&P[i].y);\n\t\trep(i,n) scanf(\"%lf%lf\",&Q[i].x,&Q[i].y);\n\n\t\tif(n==1){\n\t\t\tputs(\"0\");\n\t\t\tcontinue;\n\t\t}\n\n\t\tpolygon F=convex_hull(P);\n\t\tpolygon G=convex_hull(Q);\n\n\t\tset<point> S(Q.begin(),Q.end());\n\n\t\tdouble ans=INF;\n\t\trep(_,2){\n\t\t\trep(i,G.size()){\n\t\t\t\tpoint v=G[i]-F[0];\n\t\t\t\tdouble theta=arg(G[(i+1)%G.size()]-G[i])-arg(F[1]-F[0]);\n\n\t\t\t\tbool ok=true;\n\t\t\t\trep(j,n){\n\t\t\t\t\tif(S.count(rot(P[j]-F[0],theta)+F[0]+v)==0){\n\t\t\t\t\t\tok=false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(ok){\n\t\t\t\t\ttheta=abs(theta);\n\t\t\t\t\twhile(theta>2*PI) theta-=2*PI;\n\t\t\t\t\tans=min(ans,theta);\n\t\t\t\t\tans=min(ans,2*PI-theta);\n\t\t\t\t}\n\t\t\t}\n\t\t\treverse(G.begin(),G.end());\n\t\t}\n\t\tprintf(\"%.9f\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1296, "score_of_the_acc": -0.0417, "final_rank": 1 } ]
aoj_2053_cpp
Problem F: Controlled Tournament National Association of Tennis is planning to hold a tennis competition among professional players. The competition is going to be a knockout tournament, and you are assigned the task to make the arrangement of players in the tournament. You are given the detailed report about all participants of the competition. The report contains the results of recent matches between all pairs of the participants. Examining the data, you’ve noticed that it is only up to the opponent whether one player wins or not. Since one of your special friends are attending the competition, you want him to get the best prize. So you want to know the possibility where he wins the gold medal. However it is not so easy to figure out because there are many participants. You have decided to write a program which calculates the number of possible arrangements of tournament in which your friend wins the gold medal. In order to make your trick hidden from everyone, you need to avoid making a factitive tourna- ment tree. So you have to minimize the height of your tournament tree. Input The input consists of multiple datasets. Each dataset has the format as described below. N M R 11 R 12 . . . R 1 N R 21 R 22 . . . R 2 N ... R N 1 R N 2 . . . R NN N (2 ≤ N ≤ 16) is the number of player, and M (1 ≤ M ≤ N ) is your friend’s ID (numbered from 1). R ij is the result of a match between the i -th player and the j -th player. When i -th player always wins, R ij = 1. Otherwise, R ij = 0. It is guaranteed that the matrix is consistent: for all i ≠ j , R ij = 0 if and only if R ji = 1. The diagonal elements R ii are just given for convenience and are always 0. The end of input is indicated by a line containing two zeros. This line is not a part of any datasets and should not be processed. Output For each dataset, your program should output in a line the number of possible tournaments in which your friend wins the first prize. Sample Input 2 1 0 1 0 0 2 1 0 0 1 0 3 3 0 1 1 0 0 1 0 0 0 3 3 0 1 0 0 0 0 1 1 0 3 1 0 1 0 0 0 0 1 1 0 3 3 0 1 0 0 0 1 1 0 0 6 4 0 0 0 0 0 1 1 0 1 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 0 0 0 0 0 1 1 1 1 0 7 2 0 1 0 0 0 1 0 0 0 1 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 1 0 1 0 8 6 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 1 0 1 0 0 0 Output for the Sample Input 1 0 0 3 0 1 11 139 78
[ { "submission_id": "aoj_2053_10848151", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1 << 16;\nint n, m;\nint bit1[MAXN]; // 二进制数中有多少个 1\nint dp[20][10][MAXN];\nvector<int> G[20];\nint dfs(int u, int h, int bits) {\n if(bit1[bits] == 1) return 1;\n if((1 << h) < bit1[bits]) return 0;\n if(dp[u][h][bits] != -1) return dp[u][h][bits];\n else dp[u][h][bits] = 0;\n int& res = dp[u][h][bits];\n for(int i = bits & (bits - 1); i; i = bits & (i - 1)) {\n if((i >> u) & 1) {\n int j = bits ^ i;\n for(int k = 0; k < G[u].size(); k++) {\n int v = G[u][k];\n if((j >> v) & 1) {\n res += dfs(u, h - 1, i) * dfs(v, h - 1, j);\n }\n }\n }\n }\n return res;\n}\nint main() {\n for(int i = 0; i < MAXN; i++) {\n bit1[i] = bit1[i >> 1] + (i & 1);\n }\n while(cin >> n >> m && (n + m)) {\n memset(dp, -1, sizeof dp);\n for(int i = 0; i < n; i++) G[i].clear();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n int x;\n cin >> x;\n if(x) G[i].push_back(j);\n }\n }\n int h = ceil(log(n) / log(2));\n cout << dfs(m - 1, h, (1 << n) - 1) << endl;\n }\n return 0;\n\n}", "accuracy": 1, "time_ms": 3660, "memory_kb": 54992, "score_of_the_acc": -1.0057, "final_rank": 6 }, { "submission_id": "aoj_2053_10826910", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1 << 16;\nint n, m;\nint bit1[MAXN]; // 二进制数中有多少个 1\nint dp[20][10][MAXN];\nvector<int> G[20];\nint dfs(int u, int h, int bits) {\n if(bit1[bits] == 1) return 1;\n if((1 << h) < bit1[bits]) return 0;\n if(dp[u][h][bits] != -1) return dp[u][h][bits];\n else dp[u][h][bits] = 0;\n int& res = dp[u][h][bits];\n for(int i = bits & (bits - 1); i; i = bits & (i - 1)) { // 枚举 bits 里的 1 选或不选的情况\n if((i >> u) & 1) {\n int j = bits ^ i;\n for(int k = 0; k < G[u].size(); k++) {\n int v = G[u][k];\n if((j >> v) & 1) {\n res += dfs(u, h - 1, i) * dfs(v, h - 1, j);\n }\n }\n }\n }\n return res;\n}\nint main() {\n for(int i = 0; i < MAXN; i++) {\n bit1[i] = bit1[i >> 1] + (i & 1);\n }\n while(cin >> n >> m && (n + m)) {\n memset(dp, -1, sizeof dp);\n for(int i = 0; i < n; i++) G[i].clear();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n int x;\n cin >> x;\n if(x) G[i].push_back(j);\n }\n }\n int h = ceil(log(n) / log(2));\n cout << dfs(m - 1, h, (1 << n) - 1) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3670, "memory_kb": 54724, "score_of_the_acc": -1.0037, "final_rank": 5 }, { "submission_id": "aoj_2053_2380459", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1 << 16;\nint n, m;\nint bit1[MAXN]; // 二?制数中有多少个 1\nint dp[20][10][MAXN];\nvector<int> G[20];\nint dfs(int u, int h, int bits) {\n if(bit1[bits] == 1) return 1;\n if((1 << h) < bit1[bits]) return 0;\n if(dp[u][h][bits] != -1) return dp[u][h][bits];\n else dp[u][h][bits] = 0;\n int& res = dp[u][h][bits];\n for(int i = bits & (bits - 1); i; i = bits & (i - 1)) {\n if((i >> u) & 1) {\n int j = bits ^ i;\n for(int k = 0; k < G[u].size(); k++) {\n int v = G[u][k];\n if((j >> v) & 1) {\n res += dfs(u, h - 1, i) * dfs(v, h - 1, j);\n }\n }\n }\n }\n return res;\n}\nint main() {\n for(int i = 0; i < MAXN; i++) {\n bit1[i] = bit1[i >> 1] + (i & 1);\n }\n while(cin >> n >> m && (n + m)) {\n memset(dp, -1, sizeof dp);\n for(int i = 0; i < n; i++) G[i].clear();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n int x;\n cin >> x;\n if(x) G[i].push_back(j);\n }\n }\n int h = ceil(log(n) / log(2));\n cout << dfs(m - 1, h, (1 << n) - 1) << endl;\n }\n return 0;\n\n}", "accuracy": 1, "time_ms": 4070, "memory_kb": 54852, "score_of_the_acc": -1.2346, "final_rank": 8 }, { "submission_id": "aoj_2053_1679677", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cstring>\n#include <iostream>\n\nusing namespace std;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define TRACE(x) cout << #x << \" = \" << x << endl\n#define _ << \" _ \" <<\n\ntypedef long long llint;\n\nconst int MAX = 16;\n\nint R[MAX][MAX];\nint f[5][MAX][1<<MAX];\nint bio[5][MAX][1<<MAX];\nint cookie, n;\n\nint solve(int h, int w, int s) {\n if (bio[h][w][s] == cookie) return f[h][w][s];\n bio[h][w][s] = cookie;\n\n int cnt = 0;\n REP(i, n) \n if (s&(1<<i)) cnt++;\n if (cnt == 1) return f[h][w][s] = (s>>w) & 1;\n\n int nh = 0;\n while (cnt > 1) nh++, cnt = (cnt + 1) / 2;\n if (nh > h) return f[h][w][s] = 0;\n\n f[h][w][s] = 0;\n for (int s1 = s; s1 > 0; s1 = s & (s1 - 1))\n if ((s1 & s) == s1) {\n if (!(s1 & (1<<w))) continue;\n if (s1 == s) continue;\n\n int s2 = s ^ s1;\n\n REP(i, n)\n if (s2 & (1<<i))\n if (R[w][i]) f[h][w][s] += solve(h-1, w, s1) * solve(h-1, i, s2);\n }\n return f[h][w][s];\n}\n\nint main(void) {\n int m;\n while (scanf(\"%d %d\", &n, &m) == 2) {\n if (n == 0 && m == 0) break;\n\n --m;\n REP(i, n) REP(j, n) scanf(\"%d\", &R[i][j]);\n\n cookie++;\n int h = 0, tmp = n;\n while (tmp > 1) tmp = (tmp + 1) / 2, h++;\n printf(\"%d\\n\", solve(h, m, (1<<n) - 1));\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 4110, "memory_kb": 20112, "score_of_the_acc": -0.2646, "final_rank": 2 }, { "submission_id": "aoj_2053_772930", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <vector>\n\n#define repi(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,a) repi(i,0,a)\n#define repd(i,a,b) for(int i = (a) ; i >= (b); i--)\n#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i!= (a).end(); i++)\n#define all(u) (u).begin(),(u).end()\n#define rall(u) (u).rbegin(),(u).rend()\n#define UNIQUE(u) (u).erase(unique(all(u)),(u).end)\n\n#define pb push_back\n#define mp makek_pair\n#define INF 1e9\n#define EPS 1e-9\n#define PI acos(-1.0)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\n\nint N, M, R[16][16], mem[1<<16][16][8];\n\nint f(int s, int i, int h)\n{\n if (mem[s][i][h] >= 0) return mem[s][i][h];\n if (!(s >> i & 1)) return 0;\n int sz = __builtin_popcount(s);\n if (sz == 1) return 1;\n\n int ret = 0;\n int M = 1 << h - 1;\n int m = sz - M;\n int tmp = s ^ 1 << i;\n for (int t = tmp; t; --t, t &= tmp) {\n\tint k = __builtin_popcount(t);\n\tif (m <= k && k <= M)\n\t for (int j = 0; j < N; ++j)\n\t\tif (R[i][j])\n\t\t ret += f(s ^ t, i, h - 1) * f(t, j, h - 1);\n }\n return mem[s][i][h] = ret;\n}\n\nvoid solve()\n{\n memset(mem, -1, sizeof(mem));\n\n --M;\n rep(i,N) rep(j,N) cin >> R[i][j];\n\n cout << f((1 << N) - 1, M, 32 - __builtin_clz(N - 1)) << endl;\n}\n\nint main()\n{\n while (cin >> N >> M && N)\n\tsolve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 3650, "memory_kb": 33940, "score_of_the_acc": -0.3984, "final_rank": 3 }, { "submission_id": "aoj_2053_470930", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\ntemplate <size_t T>\nbool prevSubset(bitset<T>& bs, const bitset<T>& mask)\n{\n bs = bs.to_ulong() - 1ull;\n bs &= mask;\n if(bs.none())\n return false;\n return true;\n}\n\nint n, m;\nvector<vector<int> > r;\n\nvector<vector<vector<int> > > memo;\n\nint solve(int d, int win, bitset<16> rest)\n{\n if(rest.count() == 1)\n return 1;\n if(d == -1)\n return 0;\n\n if(memo[d][win][rest.to_ulong()] != -1)\n return memo[d][win][rest.to_ulong()];\n\n int ret = 0;\n bitset<16> bs1 = rest;\n while(prevSubset(bs1, rest)){\n bitset<16> bs2 = rest ^ bs1;\n if(bs1.count() > (1<<d) || bs2.count() > (1<<d))\n continue;\n if(!bs1[win])\n continue;\n\n for(int i=0; i<n; ++i){\n if(bs2[i] && r[win][i] == 1)\n ret += solve(d-1, win, bs1) * solve(d-1, i, bs2);\n }\n }\n\n return memo[d][win][rest.to_ulong()] = ret;\n}\n\nint main()\n{\n for(;;){\n cin >> n >> m;\n if(n == 0)\n return 0;\n\n r.assign(n, vector<int>(n));\n for(int i=0; i<n; ++i){\n for(int j=0; j<n; ++j){\n cin >> r[i][j];\n }\n }\n\n int d = 1;\n while((1 << d) < n)\n ++ d;\n\n memo.assign(d, vector<vector<int> >(n, vector<int>(1<<n, -1)));\n cout << solve(d-1, m-1, (1<<n)-1) << endl;\n }\n}", "accuracy": 1, "time_ms": 5410, "memory_kb": 24440, "score_of_the_acc": -1.1269, "final_rank": 7 }, { "submission_id": "aoj_2053_361599", "code_snippet": "#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\nstatic int mem[16][1<<16][5];\nint vs[16][16];\nint lg[16] = {1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};\n\nint search(int win, int lose, int cnt, int n){\n\tif(~mem[win][lose][cnt]) return mem[win][lose][cnt];\n\tif(!lose) return mem[win][lose][cnt] = 1;\n\tif(cnt == 0) return mem[win][lose][cnt] = 0;\n\tint res = 0, loseBit = 0;\n\tfor(int t=lose;t;t&=(t-1)) loseBit++;\n\tif(lg[loseBit] > cnt) return mem[win][lose][cnt] = 0;\n\tfor(int i=0;i<n;i++){\n\t\tif(!(lose&(1<<i))) continue;\n\t\tif(!vs[win][i]) continue;\n\t\tint mask = lose^(1<<i);\n\t\tint b = mask;\n\t\tdo{\n\t\t\tres += search(win, b, cnt-1, n) * search(i, mask^b, cnt-1, n);\n\t\t\tb = (b-1)&mask;\n\t\t} while (b != mask);\n\t}\n\treturn mem[win][lose][cnt] = res;\n}\n\nint main(){\n\tint n, m;\n\twhile(cin >> n >> m, n){\n\t\tfor(int i=0;i<n;i++)\n\t\t\tfor(int j=0;j<n;j++)\n\t\t\t\tcin >> vs[i][j];\n\t\tmemset(mem,-1,sizeof(mem));\n\t\tcout << search(m-1, (((1<<n)-1)^(1<<(m-1))), lg[n-1], n) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 3940, "memory_kb": 20000, "score_of_the_acc": -0.1648, "final_rank": 1 }, { "submission_id": "aoj_2053_361597", "code_snippet": "// Problem F : Controlled Tournament\n\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\nstatic int mem[16][1<<16][5];\nint vs[16][16];\nint lg[16] = {1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};\n\n// ツ講ツ評ツスツδ可イツドツつサツづ個づ慊づ慊づ個δδつ可サツ探ツ催オ\nint search(int win, int lose, int cnt, int n){\n\tif(cnt < 0) return 0;\n\tif(~mem[win][lose][cnt]) return mem[win][lose][cnt];\n\tif(!lose) return mem[win][lose][cnt] = 1;\n\tint res = 0, loseBit = 0;\n\tfor(int t=lose;t;t&=(t-1)) loseBit++;\n\tif(lg[loseBit] > cnt) return mem[win][lose][cnt] = 0;\n\tfor(int i=0;i<n;i++){\n\t\tif(!(lose&(1<<i))) continue;\n\t\tif(!vs[win][i]) continue;\n\t\tint mask = lose^(1<<i);\n\t\tint b = mask;\n\t\tdo{\n\t\t\tres += search(win, b, cnt-1, n) * search(i, mask^b, cnt-1, n);\n\t\t\tb = (b-1)&mask;\n\t\t} while (b != mask);\n\t}\n\treturn mem[win][lose][cnt] = res;\n}\n\nint main(){\n\tint n, m;\n\twhile(cin >> n >> m, n){\n\t\tfor(int i=0;i<n;i++)\n\t\t\tfor(int j=0;j<n;j++)\n\t\t\t\tcin >> vs[i][j];\n\t\tmemset(mem,-1,sizeof(mem));\n\t\tcout << search(m-1, (((1<<n)-1)^(1<<(m-1))), lg[n-1], n) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 4460, "memory_kb": 20000, "score_of_the_acc": -0.4602, "final_rank": 4 } ]
aoj_2052_cpp
Problem E: Optimal Rest Music Macro Language (MML) is a language for textual representation of musical scores. Although there are various dialects of MML, all of them provide a set of commands to describe scores, such as commands for notes, rests, octaves, volumes, and so forth. In this problem, we focus on rests, i.e. intervals of silence. Each rest command consists of a command specifier ‘R’ followed by a duration specifier. Each duration specifier is basically one of the following numbers: ‘1’, ‘2’, ‘4’, ‘8’, ‘16’, ‘32’, and ‘64’, where ‘1’ denotes a whole (1), ‘2’ a half (1/2), ‘4’ a quarter (1/4), ‘8’ an eighth (1/8), and so on. This number is called the base duration, and optionally followed by one or more dots. The first dot adds the duration by the half of the base duration. For example, ‘4.’ denotes the duration of ‘4’ (a quarter) plus ‘8’ (an eighth, i.e. the half of a quarter), or simply 1.5 times as long as ‘4’. In other words, ‘R4.’ is equivalent to ‘R4R8’. In case with two or more dots, each extra dot extends the duration by the half of the previous one. Thus ‘4..’ denotes the duration of ‘4’ plus ‘8’ plus ‘16’, ‘4...’ denotes the duration of ‘4’ plus ‘8’ plus ‘16’ plus ‘32’, and so on. The duration extended by dots cannot be shorter than ‘64’. For exapmle, neither ‘64.’ nor ‘16...’ will be accepted since both of the last dots indicate the half of ‘64’ (i.e. the duration of 1/128). In this problem, you are required to write a program that finds the shortest expressions equivalent to given sequences of rest commands. Input The input consists of multiple datasets. The first line of the input contains the number of datasets N . Then, N datasets follow, each containing a sequence of valid rest commands in one line. You may assume that no sequence contains more than 100,000 characters. Output For each dataset, your program should output the shortest expression in one line. If there are multiple expressions of the shortest length, output the lexicographically smallest one. Sample Input 3 R2R2 R1R2R4R8R16R32R64 R1R4R16 Output for the Sample Input R1 R1...... R16R1R4
[ { "submission_id": "aoj_2052_10233879", "code_snippet": "// AOJ #2052 Optimal Rest\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\n\nstruct Coin {\n string s;\n int cost;\n int value;\n};\n \nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n int N;\n cin >> N;\n cin.ignore();\n \n vector<string> bases = {\"1\",\"2\",\"4\",\"8\",\"16\",\"32\",\"64\"};\n vector<Coin> coins;\n for(auto &b : bases){\n int baseVal = stoi(b);\n int baseLength = 64 / baseVal;\n int mmax = 0;\n while((1 << mmax) * baseVal <= 64) mmax++;\n mmax--;\n {\n Coin c;\n c.s = \"R\" + b;\n c.cost = (int)c.s.size();\n c.value = baseLength;\n coins.push_back(c);\n }\n for(int m=1; m<=mmax; m++){\n Coin c;\n c.s = \"R\" + b + string(m, '.');\n c.cost = (int)c.s.size();\n double factor = 2 - 1.0 / (1 << m);\n int add = 64 / (1 << m);\n c.value = (128 - add) / baseVal;\n coins.push_back(c);\n }\n }\n \n\n for(int cs=0; cs<N; cs++){\n string line;\n getline(cin, line);\n \n int T = 0;\n for (int i = 0; i < (int)line.size(); ){\n if(line[i]=='R'){\n i++;\n string num;\n while(i < (int)line.size() && isdigit(line[i])){\n num.push_back(line[i]);\n i++;\n }\n int baseVal = stoi(num);\n int baseLength = 64 / baseVal;\n int m = 0;\n while(i < (int)line.size() && line[i]=='.') m++, i++;\n\n if(m==0) T += baseLength;\n else {\n int add = 64 / (1 << m);\n int len = (128 - add) / baseVal;\n T += len;\n }\n } else i++;\n }\n \n vector<int> dp(T+1, INF);\n vector<int> prev(T+1, -1);\n vector<int> pick(T+1, -1);\n dp[0] = 0;\n for (int t = 0; t <= T; t++){\n if(dp[t] == INF) continue;\n for (int i = 0; i < (int)coins.size(); i++){\n int nt = t + coins[i].value;\n if(nt > T) continue;\n int nc = dp[t] + coins[i].cost;\n if(nc < dp[nt]){\n dp[nt] = nc;\n prev[nt] = t;\n pick[nt] = i;\n }\n }\n }\n \n vector<string> solCoins;\n for (int t = T; t > 0; ){\n int i = pick[t];\n solCoins.push_back(coins[i].s);\n t = prev[t];\n }\n \n auto cmp = [&](const string &a, const string &b){\n return a + b < b + a;\n };\n sort(solCoins.begin(), solCoins.end(), cmp);\n \n string ans;\n for(auto &s : solCoins) ans += s;\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 42860, "score_of_the_acc": -0.8454, "final_rank": 6 }, { "submission_id": "aoj_2052_9637730", "code_snippet": "#include<bits/stdc++.h>\n#define y second\n#define x first\n\nusing namespace std;\nusing namespace std::chrono;\nusing psi = pair<string, int>;\n\nconst int NN = 101010, MM = 32 * NN;\n\nchar s[NN];\n\nint dp[MM];\n\nint main() {\n int t; scanf(\"%d\", &t);\n vector<psi> vec;\n for(int i = 1; i <= 64; i *= 2) {\n string s = \"R\" + to_string(i);\n int val = 64 / i, x = val;\n for(int j = 0; x; j++) {\n vec.push_back({s, val});\n s += '.';\n x /= 2, val += x;\n }\n }\n\n for(int i = 1; i < MM; i++) {\n dp[i] = 1e9;\n for(psi p: vec) if(i >= p.y) {\n dp[i] = min(dp[i], dp[i - p.y] + (int)p.x.size());\n }\n }\n\n while(t--) {\n scanf(\"%s\", s);\n int all = 0, val = 0;\n for(int i = 0, x = 0; s[i]; i++) {\n if(s[i] == 'R') all += val, x = 0;\n else if(isdigit(s[i])) {\n x = 10 * x + s[i] - '0';\n if(!isdigit(s[i + 1])) x = 64 / x, val = x;\n }\n else x /= 2, val += x;\n }\n all += val;\n\n string ans;\n while(all) {\n psi mn = {\"R\", all};\n for(psi p: vec) if(all >= p.y && dp[all - p.y] + p.x.size() == dp[all]) {\n if(p.x + \"R\" < mn.x + \"R\") mn = p;\n }\n ans += mn.x;\n all -= mn.y;\n }\n printf(\"%s\\n\", ans.c_str());\n }\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 16336, "score_of_the_acc": -0.4557, "final_rank": 4 }, { "submission_id": "aoj_2052_2859466", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\n\n/* 幾何の基本 */\n\n#include <complex>\n\ntypedef complex<ld> Point;\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// 点の入力\nPoint input_Point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// 誤差つき等号判定\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// 内積\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// 外積\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// 直線の定義\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// 円の定義\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// ccw\n// 1: a,b,cが反時計周りの順に並ぶ\n//-1: a,b,cが時計周りの順に並ぶ\n// 2: c,a,bの順に直線に並ぶ\n//-2: a,b,cの順に直線に並ぶ\n// 0: a,c,bの順に直線に並ぶ\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,cが反時計周りの順に並ぶ\n\tif (cross(nb, nc) < -eps) return -1; // a,b,cが時計周りの順に並ぶ\n\tif (dot(nb, nc) < 0) return 2; // c,a,bの順に直線に並ぶ\n\tif (norm(nb) < norm(nc)) return -2; // a,b,cの順に直線に並ぶ\n\treturn 0; // a,c,bの順に直線に並ぶ\n}\n\n\n/* 交差判定 */\n\n// 直線と直線の交差判定\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// 直線と線分の交差判定\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// 線分と線分の交差判定\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 点の直線上判定\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// 点の線分上判定\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// 垂線の足\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);\n\treturn l.a + t * (l.b - l.a);\n}\n\n//線対象の位置にある点\nPoint reflect(const Line &l, const Point &p) {\n\tPoint pr = proj(l, p);\n\treturn pr * 2.l - p;\n}\n\n// 直線と直線の交点\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// 直線と直線の交点\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// 線分と線分の交点\n// 重なってる部分あるとassert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//先にisis_ssしてね\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// 線分と線分の交点\nvector<Point> is_ss2(const Line &s, const Line& t) {\n\tvector<Point> kouho(is_ll2(s, t));\n\tvector<Point>ans;\n\tfor (auto p : kouho) {\n\t\tif (isis_sp(s, p) && isis_sp(t, p))ans.emplace_back(p);\n\t}\n\treturn ans;\n}\n// 直線と点の距離\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n//直線と直線の距離\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// 直線と線分の距離\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// 線分と点の距離\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//直線と直線の二等分線のベクトル\nLine line_bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n//点と点の垂直二等分線 aを左に見ながら\nLine point_bisection(const Point&a, const Point&b) {\n\tconst Point cen((a + b) / 2.l);\n\tconst Point vec = (b - a)*Point(0, 1);\n\treturn Line(cen, cen + vec);\n}\n\n//三つの点からなる外心\nPoint outer_center(const vector<Point>&ps) {\n\tassert(ps.size() == 3);\n\tLine l1 = point_bisection(ps[0], ps[1]);\n\tLine l2 = point_bisection(ps[1], ps[2]);\n\treturn is_ll(l1, l2);\n}\n\n\n//三つの直線からなる内心\n//三つの直線が並行でないことは確かめといてね\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(line_bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(line_bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//三つの直線からなる傍心\n//三つの直線が並行でないことは確かめといてね\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(line_bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(line_bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:並行\n//c:並行でない\n//三つの直線から同距離の位置を求める。\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(line_bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(line_bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(line_bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* 円 */\n\n// 円と円の交点\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n/*\n点が円の中にいるか\n0 => out\n1 => on\n2 => in*/\nint is_in_Circle(const Point& p, const Circle &cir) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\nint is_in_Circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n/*\n円lcが円rcの中にいるか\n0 => out\n1 => on\n2 => in*/\nint Circle_in_Circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// 円と直線の交点\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// 円と線分の距離\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// 円と点の接線\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// 円と円の接線\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), nret.begin(), nret.end());\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//二つの円の重なり面積\nld two_Circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* 多角形 */\n\ntypedef vector<Point> Polygon;\n\n\n\n//多角形(複数の点)の最小包含円をO(N=頂点数)で求めるアルゴリズム\n//同一直線上に三つの点がないこと\n#include<random>\nCircle welzl(vector<Point>ps) {\n\tstruct solver {\n\t\tCircle solve(vector<Point>&ps, vector<Point>&rs) {\n\t\t\tif (ps.empty() || rs.size() == 3) {\n\t\t\t\tif (rs.size() == 1) {\n\t\t\t\t\treturn Circle(Point(rs[0]), 0);\n\t\t\t\t}\n\t\t\t\telse if (rs.size() == 2) {\n\t\t\t\t\treturn Circle((rs[0] + rs[1]) / 2.0l, abs(rs[1] - rs[0]) / 2);\n\t\t\t\t}\n\t\t\t\telse if (rs.size() == 3) {\n\t\t\t\t\tvector<Line> ls(3);\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tls[i] = Line(rs[i], rs[(i + 1) % 3]);\n\t\t\t\t\t}\n\t\t\t\t\tPoint center = outer_center(rs);\n\t\t\t\t\treturn Circle(center, abs(center - rs[0]));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn Circle(Point(), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\n\t\t\t\tPoint p_ba = ps.back();\n\t\t\t\tps.pop_back();\n\t\t\t\tCircle d = solve(ps, rs);\n\t\t\t\tps.push_back(p_ba);\n\t\t\t\tif (is_in_Circle(d, p_ba)) {\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\trs.push_back(p_ba);\n\t\t\t\t\tps.pop_back();\n\t\t\t\t\tauto ans = solve(ps, rs);\n\t\t\t\t\tps.push_back(p_ba);\n\t\t\t\t\trs.pop_back();\n\t\t\t\t\treturn ans;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}so;\n\tstd::random_device rd;\n\tstd::mt19937 mt(rd());\n\tshuffle(ps.begin(), ps.end(), mt);\n\tvector<Point>rs;\n\tCircle ans = so.solve(ps, rs);\n\treturn ans;\n}\n// 面積\nld get_area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tfor (int j = 0; j<n; ++j) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n//多角形の回転方向\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// 円の内外判定\n/*0 => out\n1 => on\n2 => in*/\nint is_in_Polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//円の内外判定2 高速\nenum { out, on, in };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// 凸包\n//点や線を返すことも有り得るので注意\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n//凸カット\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon q;\n\tPolygon r;\n\tfor(int i=0;i<n;++i) {\n\t\tPoint a = ps[i], b = ps[(i + 1) % n];\n\t\tLine m = Line(a, b);\n\t\tif (ccw(l.a, l.b, a) != -1) q.push_back(a);\n\t\tif (ccw(l.a, l.b, a) != 1) r.push_back(a);\n\t\tif (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0 && isis_ll(l, m)) {\n\t\t\tq.push_back(is_ll(l, m));\n\t\t\tr.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ q,r };\n\treturn polys;\n}\n\nPolygon cen(Polygon po) {\n\tPoint cen;\n\tfor (auto p : po) {\n\t\tcen += p;\n\t}\n\tcen /= po.size();\n\tfor (auto&p : po) {\n\t\tp -= cen;\n\t}\n\treturn po;\n}\n\nint get_id(string st,int& a) {\n\tassert(st[a]=='R');\n\ta++;\n\tint num=0;\n\twhile (a != st.size() && st[a] != 'R'&&st[a]!='.') {\n\t\tnum*=10;\n\t\tnum+=st[a]-'0';\n\t\ta++;\n\t}\n\tnum=64/num;\n\tint k=num;\n\twhile (a != st.size() && st[a] != 'R') {\n\t\tassert(st[a]=='.');\n\t\tk/=2;\n\t\tnum+=k;\n\t\ta++;\n\t}\n\treturn num;\n}\n\nint get_num(string st) {\n\tint sum=0;\n\tint a=0;\n\twhile (a != st.size()) {\n\t\tsum+=get_id(st,a);\n\t}\n\treturn sum;\n}\n\nint main() {\n\tvector<string>dp(5000);\n\tdp[0]=\"\";\n\tmap<string,int>mp;\n\tfor (int k = 1; k <= 64; k *= 2) {\n\t\tint num=64/k;\n\t\tstring st=\"R\"+to_string(k);\n\t\tmp[st]=num;\n\t\tfor (int plus = k * 2; plus!=128; plus *= 2) {\n\t\t\tnum+=64/plus;\n\t\t\tst+='.';\n\t\t\tmp[st]=num;\n\t\t}\n\t}\n\tfor (int i = 0; i < 3000; ++i) {\n\t\tfor (auto&& m : mp) {\n\t\t\tint j=i+m.second;\n\t\t\tstring st=dp[i]+m.first;\n\t\t\tif (dp[j] == \"\" || (dp[j].size() > st.size()) || ((dp[j].size() == st.size() && dp[j] > st))) {\n\t\t\t\tdp[j] = st;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 1000; i <= 2000; ++i) {\n\t\tint k=dp[i].find(\"R1.R1.\");\n\t\tassert(k!=-1);\n\t}\n\tint N;cin>>N;\n\twhile (N--){\n\t\tstring st;cin>>st;\n\t\tint n=get_num(st);\n\n\t\tif (n < 3000) {\n\t\t\tcout<<dp[n]<<endl;\n\t\t}\n\t\telse {\n\t\t\tint x=n%384;\n\t\t\tint t=x+1920;\n\n\t\t\tint sa=n-t;\n\t\t\tint need=sa/384;\n\n\t\t\tint place=dp[t].find(\"R1.R1.R1.R1.\");\n\t\t\tstring ans=dp[t];\n\t\t\tstring plus;\n\t\t\tfor (int k = 0; k < need; ++k) {\n\t\t\t\tplus+=\"R1.R1.R1.R1.\";\n\t\t\t}\n\t\t\tans.insert(ans.begin()+place,plus.begin(),plus.end());\n\t\t\tcout<<ans<<endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3916, "score_of_the_acc": -0.3142, "final_rank": 2 }, { "submission_id": "aoj_2052_1679577", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cstring>\n#include <iostream>\n\nusing namespace std;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define TRACE(x) cout << #x << \" = \" << x << endl\n#define _ << \" _ \" <<\n\ntypedef long long llint;\n\nconst int MAX = 64*100100;\nconst int inf = 1e9;\n\nchar s[MAX];\nint f[MAX];\nint nx[MAX], nr[MAX], ns[MAX];\n\n\nint main(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%s\", s);\n int len = strlen(s);\n int i = 0;\n int last = 0;\n int sum = 0;\n while (i < len) {\n if (s[i] == '.') {\n last /= 2;\n i++;\n } else {\n assert(s[i] == 'R');\n i++;\n int x = 0;\n while (i < len && isdigit(s[i])) x = x*10 + s[i++] - '0';\n last = 64 / x;\n }\n sum += last;\n }\n\n f[0] = 0;\n nx[0] = -1;\n FOR(s, 1, sum + 1) {\n f[s] = inf;\n auto test = [&] (int x, int d) {\n int len = 1 + 1 + (x >= 10) + d, tmp = x;\n int total = 64 / x;\n REP(i, d) total += 64 / (tmp *= 2);\n if (total > s) return;\n if (len + f[s - total] < f[s]) {\n f[s] = len + f[s - total];\n nx[s] = x, nr[s] = d, ns[s] = s - total;\n }\n };\n\n FOR(i, 1, 7) test(1, 7-i);\n REP(i, 7) test(16, 6-i);\n test(1, 0);\n for (int x: {2, 32, 4, 64, 8}) REP(i, 7) test(x, 6-i);\n }\n \n while (sum > 0) {\n putchar('R');\n printf(\"%d\", nx[sum]);\n REP(i, nr[sum]) putchar('.');\n sum = ns[sum];\n }\n putchar('\\n');\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2750, "memory_kb": 53288, "score_of_the_acc": -2, "final_rank": 8 }, { "submission_id": "aoj_2052_630129", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nconst int MAX_N = 5000000;\ntypedef pair<int, int> P;\nint trans(int n){\n return 64 / n;\n}\nint trans(string s){\n int base = 0;\n REP(i, s.size()) if(isdigit(s[i])) base = base * 10 + s[i] - '0';\n int all = trans(base);\n int cur = all;\n REP(i, s.size()) if(s[i] == '.'){\n cur /= 2;\n all += cur;\n }\n return all;\n}\n\nint main(){\n int T;\n cin >> T;\n static int dp[MAX_N];\n int val[7] = {64, 32, 16, 8, 4, 2, 1};\n int len[7] = {2, 2, 2, 2, 3, 3, 3};\n string expr[7] = {\"R1\", \"R2\", \"R4\", \"R8\", \"R16\", \"R32\", \"R64\"};\n vector<P> tool;\n vector<string> tool_expr;\n REP(i, 7){\n int sum = 0;\n int length = 0;\n string expression = \"\";\n for(int j = i; j < 7; j++){\n expression += (j == i) ? expr[i] : \".\";\n sum += val[j];\n length += (j == i) ? len[j] : 1;\n tool.push_back(make_pair(sum, length));\n tool_expr.push_back(expression);\n }\n }\n REP(i, MAX_N) dp[i] = INF;\n dp[0] = 0;\n REP(i, MAX_N){\n REP(j, tool.size()){\n int s = tool[j].first, l = tool[j].second;\n if(i + s < MAX_N) dp[i + s] = min(dp[i + s], dp[i] + l);\n }\n }\n while(T--){\n string s; cin >> s;\n REP(i, s.size()) if(s[i] == 'R') s[i] = ' ';\n stringstream ss(s);\n int seq = 0;\n int all = 0;\n while(ss >> s){\n all += trans(s);\n }\n int cur = all;\n string ans;\n while(cur > 0){\n string next = \"ZZZZZZZZZ\";\n int next_c = cur;\n REP(i, tool.size()){\n int s = tool[i].first, l = tool[i].second;\n if(cur - s >= 0 && dp[cur - s] + l == dp[cur] && next + \"RRRRRRRRR\" > tool_expr[i] + \"RRRRRRRR\"){\n next = tool_expr[i];\n next_c = cur - s;\n }\n }\n ans += next;\n cur = next_c;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 21108, "score_of_the_acc": -0.5226, "final_rank": 5 }, { "submission_id": "aoj_2052_470732", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nmap<string, int> rest;\n\nint getRest(const string& s)\n{\n int n = s.size();\n int i = 0;\n int t = 0;\n while(i < n){\n int j = i+1;\n while(j < n && s[j] != 'R')\n ++ j;\n t += rest[s.substr(i, j-i)];\n i = j;\n }\n return t;\n}\n\nint main()\n{\n for(int i=1; i<=64; i*=2){\n int len = 64 / i;\n string s = \"R\";\n if(i >= 10)\n s += i / 10 + '0';\n s += i % 10 + '0';\n rest[s] = len;\n\n for(int j=i*2; j<=64; j*=2){\n len += 64 / j;\n s += '.';\n rest[s] = len;\n }\n }\n\n vector<string> dp(2001, \"\");\n for(int i=0; i<=2000; ++i){\n for(map<string, int>::iterator it=rest.begin(); it!=rest.end(); ++it){\n int j = i + it->second;\n if(j > 2000)\n continue;\n\n string s = dp[i] + it->first;\n if(dp[j] == \"\" || s.size() < dp[j].size() || (s.size() == dp[j].size() && s < dp[j]))\n dp[j] = s;\n }\n }\n\n int d;\n cin >> d;\n\n while(--d >= 0){\n string s;\n cin >> s;\n\n int t = getRest(s);\n if(t <= 2000){\n cout << dp[t] << endl;\n continue;\n }\n\n string ret = dp[1952+t%32].substr(0, 20);\n while(ret[ret.size()-1] != 'R')\n ret = ret.substr(0, ret.size()-1);\n ret = ret.substr(0, ret.size()-1);\n\n t -= getRest(ret);\n while(t > 2000){\n ret += \"R1.\";\n t -= 64 + 32;\n }\n ret += dp[t];\n cout << ret << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1800, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2052_361570", "code_snippet": "// Problem E : Optimal Rest\n\n#include <iostream>\n#include <string>\n#include <sstream>\n\nusing namespace std;\n\nint main(){\n\t// ‹x•„•\\Œ»‚ðÅŒã‚ÉR‚ð‰Á‚¦‚½ê‡‚ÌŽ«‘‡‚Å‘S’Ê‚è‚ð•À‚ׂ½‚à‚Ì\n\tstring str[] = {\"R1......\", \"R1.....\", \"R1....\", \"R1...\", \"R1..\", \"R1.\", \"R16..\", \"R16.\", \"R16\", \"R1\", \n\t\t\t\t \"R2.....\" , \"R2....\" , \"R2...\", \"R2..\", \"R2.\", \"R2\", \"R32.\", \"R32\",\n\t\t\t \t\"R4....\", \"R4...\", \"R4..\", \"R4.\", \"R4\", \"R64\", \"R8...\", \"R8..\", \"R8.\", \"R8\" };\n\t// ã‚̏‡‚Å‹x•„•\\Œ»‚Ì’·‚³‚ð‘‚«‰º‚µ‚½‚à‚Ì\n\tint back[] = {127, 126, 124, 120, 112, 96, 7, 6, 4, 64, 63, 62, 60, 56, 48, 32, 3, 2, 31, 30, 28, 24, 16, 1, 15, 14, 12, 8};\n\tstatic int len[3200001]; len[0] = 0;\n\tfor(int i=1;i<=3200000;i++){\n\t\tlen[i] = len[i-1]+3;\n\t\tfor(int j=0;j<28;j++){\n\t\t\tif(i-back[j] < 0) continue;\n\t\t\tlen[i] = min(len[i], len[i-back[j]] + (int)str[j].size());\n\t\t}\n\t}\n\tint test; cin >> test;\n\tstring s;\n\twhile(test--){\n\t\tcin >> s;\n\t\t// “ü—Í•¶Žš—ñ‚ª•\\Œ»‚·‚é‹x•„‚Ì’·‚³‚𒲂ׂé\n\t\tfor(int i=0;i<s.size();i++) if(s[i]=='R') s[i] = ' ';\n\t\tistringstream iss(s);\n\t\tstring tmp;\n\t\tint lest = 0;\n\t\twhile(iss >> tmp){\n\t\t\ttmp = \"R\"+tmp;\n\t\t\tfor(int i=0;i<28;i++)\n\t\t\t\tif(tmp == str[i]) lest += back[i];\n\t\t}\n\t\twhile(lest > 0){\n\t\t\tfor(int i=0;i<28;i++){\n\t\t\t\tif(lest-back[i] < 0) continue;\n\t\t\t\tif(len[lest] == len[lest-back[i]] + str[i].size()){\n\t\t\t\t\tcout << str[i];\n\t\t\t\t\tlest -= back[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << endl;\n\t}\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 13000, "score_of_the_acc": -0.3725, "final_rank": 3 }, { "submission_id": "aoj_2052_147720", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define mp make_pair\n#define ALL(C) (C).begin(),(C).end()\n#define fi first\n#define se second\n\ntypedef pair<int,string> pis;\n\nconst int val[]={ 64, 32, 16, 8, 4, 2, 1};\nconst string str[]={\"R1\",\"R2\",\"R4\",\"R8\",\"R16\",\"R32\",\"R64\"};\n\nclass st{\npublic:\n int sc;\n string s;\n bool operator<(const st & a)const{\n if (s.size() != a.s.size())return s.size() < a.s.size();\n return s < a.s;\n }\n};\n\nvector<st> all;\nvoid make(){\n rep(i,7){\n int now =val[i];\n string snow=str[i];\n all.pb((st){now,snow});\n REP(j,i+1,7){\n now+=val[j];\n snow+=\".\";\n all.pb((st){now,snow});\n }\n }\n sort(ALL(all));\n //rep(i,all.size())printf(\"%4d %s\\n\",all[i].sc,all[i].s.c_str());\n}\n\nconst int N = 6400010;\nint dp[N];\nint path[N];\n\nint encode(string &in){\n int sum=0;\n int p=0;\n while(p < in.size()){\n p++;//in[p]='R';\n int now=0;\n while(p < in.size() && isdigit(in[p]))now=now*10+(in[p++]-'0');\n now = 64/now;\n sum+=now;\n while(p < in.size() && in[p] == '.')p++,sum+=now/2,now/=2;\n }\n return sum;\n}\n\nbool cmpstr(string &a,string &b){\n int len=min(a.size(),b.size());\n rep(i,len){\n if (a[i] != b[i])return a[i] < b[i];\n }\n return a.size() > b.size();\n}\n\nvoid solve(int tar){\n rep(i,tar+1)dp[i]=N*10,path[i]=-1;\n dp[0]=0;\n rep(i,tar+1){\n rep(j,all.size()){\n if (i-all[j].sc >= 0 && \n\t (all[j].s.size()+dp[i-all[j].sc] < dp[i] ||\n\t (all[j].s.size()+dp[i-all[j].sc]==dp[i]&& \n\t cmpstr(all[j].s,all[path[i]].s)))){\n\tdp[i]=all[j].s.size()+dp[i-all[j].sc];\n\tpath[i]=j;\n }\n }\n }\n while(path[tar] != -1){\n cout << all[path[tar]].s;\n tar=tar-all[path[tar]].sc;\n }\n cout << endl;\n}\n\nmain(){\n make();\n int te;\n cin>>te;\n while(te--){\n string in;\n cin>>in;\n solve(encode(in));\n }\n return false;\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 25000, "score_of_the_acc": -0.886, "final_rank": 7 } ]
aoj_2059_cpp
Problem B: Restaurant Steve runs a small restaurant in a city. Also, as he is the only cook in his restaurant, he cooks everything ordered by customers. Basically he attends to orders by first-come-first-served principle. He takes some prescribed time to prepare each dish. As a customer can order more than one dish at a time, Steve starts his cooking with the dish that takes the longest time to prepare. In case that there are two or more dishes that take the same amount of time to prepare, he makes these dishes in the sequence as listed in the menu card of his restaurant. Note that he does not care in which order these dishes have been ordered. When he completes all dishes ordered by a customer, he immediately passes these dishes to the waitress and has them served to the customer. Time for serving is negligible. On the other hand, during his cooking for someone’s order, another customer may come and order dishes. For his efficiency, he decided to prepare together multiple dishes of the same if possible. When he starts cooking for new dishes, he looks over the orders accepted by that time (including the order accepted exactly at that time if any), and counts the number of the same dishes he is going to cook next. Time required for cooking is the same no matter how many dishes he prepare at once. Unfortunately, he has only limited capacity in the kitchen, and hence it is sometimes impossible that the requested number of dishes are prepared at once. In such cases, he just prepares as many dishes as possible. Your task is to write a program that simulates the restaurant. Given a list of dishes in the menu card and orders from customers with their accepted times, your program should output a list of times when each customer is served. Input The input contains multiple data sets. Each data set is in the format below: N M Name 1 Limit 1 Time 1 ... Name N Limit N Time N T 1 K 1 Dish 1,1 . . . Dish 1, K 1 ... T M K M Dish M ,1 . . . Dish M , K M Here, N (1 ≤ N ≤ 20) and M (1 ≤ M ≤ 100) specify the number of entries in the menu card and the number of orders that Steve will receive, respectively; each Name i is the name of a dish of the i -th entry in the menu card, which consists of up to 20 alphabetical letters; Limit i (1 ≤ Limit i ≤ 10) is the number of dishes he can prepare at the same time; Time i (1 ≤ Time i ≤ 1000) is time required to prepare a dish (or dishes) of the i -th entry; T j (1 ≤ T j ≤ 10000000) is the time when the j -th order is accepted; K j (1 ≤ K j ≤ 10) is the number of dishes in the j -th order; and each Dish j,k represents a dish in the j -th order. You may assume that every dish in the orders is listed in the menu card, but you should note that each order may contain multiple occurrences of the same dishes. The orders are given in ascending order by T j , and no two orders are accepted at the same time. The input is terminated with a line that contains two zeros. This is not part of data sets and hence should not be processed. Ou ...(truncated)
[ { "submission_id": "aoj_2059_1367835", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 20;\nconst int MAX_M = 100;\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef set<int> si;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\nstruct Menu {\n int id, limit, time;\n string name;\n bool operator<(const Menu& m0) const {\n return time > m0.time || (time == m0.time && id < m0.id);\n }\n void print() {\n printf(\"Menu: id=%d, limit=%d, time=%d, name=%s\\n\",\n\t id, limit, time, name.c_str());\n }\n};\n\nstruct Order {\n int id, t, k, mnums[MAX_N];\n Order() { memset(mnums, 0, sizeof(mnums)); }\n void print() {\n printf(\"Order: id=%d, t=%d, k=%d: \", id, t, k);\n for (int i = 0; i < MAX_N; i++) {\n if (i > 0) putchar(',');\n printf(\"%d\", mnums[i]);\n }\n putchar('\\n');\n }\n};\n\nenum { C_IDLE, C_COOK, C_WAIT };\nenum { E_ORDER, E_CFIN, E_CST};\n\nstruct Event {\n int t, ev, oid;\n Event() {}\n Event(int _t, int _ev, int _oid) : t(_t), ev(_ev), oid(_oid) {}\n bool operator>(const Event& e0) const {\n return t > e0.t || (t == e0.t && ev > e0.ev);\n }\n void print() { printf(\"Event: t=%d, ev=%d, oid=%d\\n\", t, ev, oid); }\n};\n\n/* global variables */\n\nint n, m;\nMenu menu[MAX_N];\nOrder orders[MAX_M];\nint fts[MAX_M], omsums[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (bool first = true;; first = false) {\n cin >> n >> m;\n if (n == 0) break;\n\n for (int i = 0; i < n; i++) {\n cin >> menu[i].name >> menu[i].limit >> menu[i].time;\n menu[i].id = i;\n }\n sort(menu, menu + n);\n //for (int i = 0; i < n; i++) menu[i].print();\n \n for (int i = 0; i < m; i++) {\n cin >> orders[i].t >> orders[i].k;\n orders[i].id = i;\n for (int j = 0; j < orders[i].k; j++) {\n\tstring mname;\n\tcin >> mname;\n\tfor (int mi = 0; mi < n; mi++)\n\t if (menu[mi].name == mname) {\n\t orders[i].mnums[mi]++;\n\t break;\n\t }\n }\n }\n //for (int i = 0; i < m; i++) orders[i].print();\n\n priority_queue<Event, vector<Event>, greater<Event> > q;\n \n for (int i = 0; i < m; i++)\n q.push(Event(orders[i].t, E_ORDER, i));\n\n int cstat = C_IDLE, cmenu, cnum;\n vpii clist;\n si olist;\n\n memset(omsums, 0, sizeof(omsums));\n\n while (! q.empty()) {\n Event e = q.top();\n q.pop();\n //e.print();\n\n // order arrived\n if (e.ev == E_ORDER) {\n\tolist.insert(e.oid);\n\n\tfor (int i = 0; i < n; i++)\n\t omsums[i] += orders[e.oid].mnums[i];\n\t\n\tif (cstat == C_IDLE) {\n\t cstat = C_WAIT;\n\t q.push(Event(e.t, E_CST, -1));\n\t}\n }\n\n // finish cooking\n else if (e.ev == E_CFIN) {\n\tfor (int i = 0; i < clist.size(); i++) {\n\t int oid = clist[i].first;\n\t int num = clist[i].second;\n\t orders[oid].mnums[cmenu] -= num;\n\t orders[oid].k -= num;\n\t if (orders[oid].k == 0) {\n\t olist.erase(oid);\n\t fts[oid] = e.t;\n\t }\n\t}\n\n\tomsums[cmenu] -= cnum;\n\t\n\tcstat = C_IDLE;\n\tif (! olist.empty()) {\n\t cstat = C_WAIT;\n\t q.push(Event(e.t, E_CST, -1));\n\t}\n }\n\n // start cooking\n else if (e.ev == E_CST) {\n\tif (! olist.empty()) {\n\t int oid = *(olist.begin());\n\t for (cmenu = 0; cmenu < n; cmenu++)\n\t if (orders[oid].mnums[cmenu] > 0) break;\n\n\t cnum = omsums[cmenu];\n\t if (cnum > menu[cmenu].limit) cnum = menu[cmenu].limit;\n\n\t clist.clear();\n\t int num = cnum;\n\n\t for (si::iterator oit = olist.begin();\n\t num > 0 && oit != olist.end(); oit++) {\n\t int oid = *oit;\n\t int onum = orders[oid].mnums[cmenu];\n\t if (onum > 0) {\n\t if (num > onum) {\n\t\tclist.push_back(pii(oid, onum));\n\t\tnum -= onum;\n\t }\n\t else {\n\t\tclist.push_back(pii(oid, num));\n\t\tnum = 0;\n\t }\n\t }\n\t }\n\n\t cstat = C_COOK;\n\t q.push(Event(e.t + menu[cmenu].time, E_CFIN, -1));\n\t}\n\telse {\n\t cstat = C_IDLE;\n\t}\n }\n }\n\n if (! first) putchar('\\n');\n \n for (int i = 0; i < m; i++) cout << fts[i] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1240, "score_of_the_acc": -0.9253, "final_rank": 3 }, { "submission_id": "aoj_2059_773348", "code_snippet": "#include<iostream>\n#include<map>\n#include<set>\n#include<algorithm>\n#include<cmath>\n#include<deque>\n#include<vector>\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define inf (1<<28)\n#define MAX_N 21\n#define MAX_M 101\nusing namespace std;\ntypedef pair<int,int> ii;\n\n\nii ps[MAX_N];\nstruct P\n{\n int index;\n bool finish;\n P(int index=inf,bool finish=false):index(index),finish(finish){}\n bool operator < (const P& a)const\n {\n if(ps[index].second != ps[a.index].second)return ps[index].second > ps[a.index].second;\n return index < a.index;\n }\n};\n\n\nmap<string,int> list;\nstring names[MAX_N];\n\nii order[MAX_M];\nbool fin[MAX_M];\nint ans[MAX_M];\nvector<P> cust[MAX_M];\nint N,M;\n\nint main()\n{\n bool f = true;\n while(cin >> N >> M,N|M)\n {\n if(!f)cout << endl;\n f = false;\n rep(i,N)\n\t{\n\t cin >> names[i] >> ps[i].first >> ps[i].second;\n\t list[names[i]] = i;\n\t}\n rep(i,M)cust[i].clear(),fin[i] = false,ans[i] = inf;\n rep(i,M)\n\t{\n\t int k;\n\t cin >> order[i].first >> k;\n\t order[i].second = i;\n\n\t rep(j,k)\n\t {\n\t string name;\n\t cin >> name;\n\t cust[i].push_back(P(list[name]));\n\t }\n\t sort(cust[i].begin(),cust[i].end());\n\t}\n\n sort(order,order+M);\n\n\n\n int phase = order[0].first;\n bool update = true;\n while(update)\n\t{\n\t //cout << \"phase : \"<< phase << endl;\n\t update = false;\n\t int choice = inf;\n\t rep(i,M)\n\t {\n\t int cur = order[i].second;\n\t if(fin[cur])continue;\n\t if(order[i].first > phase)continue;\n\t rep(j,cust[cur].size())\n\t\t{\n\t\t if(cust[cur][j].finish)continue;\n\t\t update = true;\n\t\t choice = cust[cur][j].index;\n\t\t break;\n\t\t}\n\t if(update)break;\n\t fin[cur] = true;\n\t }\n\n \n\n\t if(!update)\n\t {\n\t rep(i,M)\n\t\t{\n\t\t int cur = order[i].second;\n\t\t if(fin[cur])continue;\n\t\t phase = order[i].first;\n\t\t update = true;\n\t\t break;\n\t\t}\n\t if(update)continue;\n\t break;\n\t }\n\t //cout << \"choice = \" << names[choice] <<endl;\n\t int S = ps[choice].first;\n\n\n\t rep(i,M)\n\t {\n\t int cur = order[i].second;\n\t if(fin[cur])continue;\n\t if(order[i].first > phase)continue;\n\t rep(j,cust[cur].size())\n\t\t{\n\t\t if(!cust[cur][j].finish && S > 0 && cust[cur][j].index == choice)\n\t\t {\n\t\t cust[cur][j].finish = true;\n\t\t S--;\n\t\t }\n\t\t}\n\t if(S <= 0)break;\n\t }\n\n\t phase += ps[choice].second;\n\t rep(i,M)\n\t {\n\t int cur = order[i].second;\n\t if(fin[cur])continue;\n\t bool ok = true;\n\t rep(j,cust[cur].size())\n\t\t{\n\t\t if(!cust[cur][j].finish)\n\t\t {\n\t\t ok = false;\n\t\t break;\n\t\t }\n\t\t}\n\t if(ok)ans[cur] = min(ans[cur],phase),fin[cur] = true;\n\t }\n\n\n\t}\n\n rep(i,M)cout << ans[i] << endl;\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1356, "score_of_the_acc": -1.0084, "final_rank": 7 }, { "submission_id": "aoj_2059_609923", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nstruct Event{\n // type 0 - order\n // type 1 - complete\n int type, time, dish, customer;\n Event(int ty, int t, int d, int c):\n type(ty), time(t), dish(d), customer(c) {}\n bool operator < (const Event& e) const {\n if(time != e.time) return time > e.time;\n if(type != e.type) return type > e.type;\n return dish > e.dish;\n }\n};\nstruct Dish{\n string name;\n int limit, time;\n Dish() {}\n bool operator < (const Dish& d) const {\n return time > d.time;\n }\n};\n\nint main(){\n int N, M;\n bool first = true;\n while(cin >> N >> M && N){\n if(first) first = false;\n else cout << endl;\n vector<Dish> dishes(N);\n REP(i, N) cin >> dishes[i].name >> dishes[i].limit >> dishes[i].time;\n stable_sort(dishes.begin(), dishes.end());\n map<string, int> name_to_index;\n REP(i, N) name_to_index[dishes[i].name] = i;\n priority_queue<Event> que;\n REP(i, M){\n int T, K; cin >> T >> K;\n REP(j, K){\n string name; cin >> name;\n que.push(Event(0, T, name_to_index[name], i));\n }\n }\n vector<int> answer(M);\n vector<Event> cook_queue;\n int cooking = 0;\n while(!que.empty()){\n Event e = que.top(); que.pop();\n if(e.type == 0){\n //printf(\"customer %d order %s : time %d\\n\", e.customer, dishes[e.dish].name.c_str(), e.time);\n cook_queue.push_back(Event(0, 0, e.dish, e.customer));\n }else if(e.type == 1){\n //printf(\"customer %d's %s is finshed: time %d\\n\", e.customer, dishes[e.dish].name.c_str(), e.time);\n cooking --;\n answer[e.customer] = e.time;\n }\n if(cooking == 0 && !cook_queue.empty() && (que.empty() || e.time != que.top().time)){\n // new dish\n int new_dish = cook_queue[0].dish;\n for(vector<Event>::iterator it = cook_queue.begin(); it != cook_queue.end(); ){\n if(it->dish == new_dish){\n //printf(\"customer %d's %s is started : time %d -> %d\\n\", it->customer, dishes[it->dish].name.c_str(), e.time, e.time + dishes[new_dish].time);\n que.push(Event(1, e.time + dishes[new_dish].time, it->dish, it->customer));\n cooking++;\n it = cook_queue.erase(it);\n if(cooking >= dishes[new_dish].limit) break;\n }else{\n it++;\n }\n }\n assert(cooking <= dishes[new_dish].limit);\n }\n }\n assert(cooking == 0);\n REP(i, M) cout << answer[i] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1288, "score_of_the_acc": -0.9597, "final_rank": 5 }, { "submission_id": "aoj_2059_472208", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint n, m; // 料理の種類数、注文の数\n\nvector<string> name; // 料理の名前\nmap<string, int> index; // 料理の番号\nvector<int> limit, makeTime; // 一度に調理できる最大数、調理時間\n\nvector<int> come, dishNum; // 来店時間、各注文の料理の数\nvector<vector<string> > dish; // 注文された料理の名前\n\nclass Data\n{\npublic:\n int time, id;\n int a, b;\n Data(int time0, int id0, int a0, int b0){\n time = time0;\n id = id0;\n a = a0;\n b = b0;\n }\n bool operator< (const Data& d) const{\n if(time < d.time)\n return true;\n if(time == d.time){\n if(id == -1)\n return false;\n if(d.id == -1)\n return true;\n return make_pair(-makeTime[id], id) < make_pair(-makeTime[d.id], d.id);\n }\n return false;\n }\n};\n\nint main()\n{\n bool isFirst = true;\n\n for(;;){\n cin >> n >> m;\n if(n == 0)\n return 0;\n\n if(isFirst)\n isFirst = false;\n else\n cout << endl;\n\n name.resize(n);\n limit.resize(n);\n makeTime.resize(n);\n index.clear();\n for(int i=0; i<n; ++i){\n cin >> name[i] >> limit[i] >> makeTime[i];\n index[name[i]] = i;\n }\n\n come.resize(m);\n dishNum.resize(m);\n dish.resize(m);\n multiset<Data> ms1, ms2; // 各種イベント\n vector<vector<int> > finish(m); // 最後の料理が提供された時間\n for(int i=0; i<m; ++i){\n cin >> come[i] >> dishNum[i];\n dish[i].resize(dishNum[i]);\n finish[i].resize(dishNum[i], -1);\n for(int j=0; j<dishNum[i]; ++j){\n cin >> dish[i][j];\n ms1.insert(Data(come[i], index[dish[i][j]], i, j));\n }\n }\n\n vector<queue<pair<int, int> > > q(n); // 各料理の注文のキュー\n bool isFree = true; // 料理人が次の調理を始められるかどうか\n while(!ms1.empty()){\n int time = ms1.begin()->time;\n while(!ms1.empty() && time == ms1.begin()->time){\n Data d = *ms1.begin();\n ms1.erase(ms1.begin());\n\n if(d.id == -1){\n isFree = true;\n }else{\n q[d.id].push(make_pair(d.a, d.b));\n ms2.insert(d);\n }\n }\n\n while(isFree && !ms2.empty()){\n Data d = *ms2.begin();\n ms2.erase(ms2.begin());\n\n if(finish[d.a][d.b] == -1){\n int rest = limit[d.id];\n while(rest > 0 && !q[d.id].empty()){\n int a = q[d.id].front().first;\n int b = q[d.id].front().second;\n q[d.id].pop();\n\n if(finish[a][b] == -1){\n finish[a][b] = time + makeTime[d.id];\n -- rest;\n }\n }\n isFree = false;\n ms1.insert(Data(time + makeTime[d.id], -1, -1, -1));\n }\n }\n }\n\n for(int i=0; i<m; ++i)\n cout << *max_element(finish[i].begin(), finish[i].end()) << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1396, "score_of_the_acc": -1.037, "final_rank": 8 }, { "submission_id": "aoj_2059_411027", "code_snippet": "#include<queue>\n#include<cstdio>\n#include<cstring>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nstruct menu{\n\tchar name[21];\n\tint lim,t;\n};\n\nint n;\nmenu M[20];\n\nbool cmp(int a,int b){\n\treturn M[a].t>M[b].t || M[a].t==M[b].t && a<b;\n}\n\nint main(){\n\tfor(int test=0,m;scanf(\"%d%d\",&n,&m),n;test++){\n\t\tif(test>0) puts(\"\");\n\n\t\trep(i,n) scanf(\"%s%d%d\",&M[i].name,&M[i].lim,&M[i].t);\n\n\t\tpair< int,vector<int> > order[100]; // <到着時刻,注文内容>\n\t\trep(i,m){\n\t\t\tint k; scanf(\"%d%d\",&order[i].first,&k);\n\t\t\torder[i].second.resize(k);\n\t\t\trep(j,k){\n\t\t\t\tchar name[21]; scanf(\"%s\",name);\n\t\t\t\trep(l,n) if(strcmp(name,M[l].name)==0) { order[i].second[j]=l; break; }\n\t\t\t}\n\t\t\tsort(order[i].second.begin(),order[i].second.end(),cmp);\n\t\t}\n\n\t\tint fin[100]={},ans[100];\n\n\t\tdeque< pair<int,int> > wait; // <注文した人,調理待ちの料理>\n\t\tint cooking=0; // 今調理中かどうか\n\t\tpriority_queue< pair<int,int> > Q; // <-time,event_type>\n\t\trep(i,m) Q.push(make_pair(-order[i].first,i+1));\n\t\twhile(!Q.empty()){\n\t\t\tint t=-Q.top().first,type=Q.top().second; Q.pop();\n\n\t\t\t// 来客\n\t\t\tif(type>0){\n\t\t\t\ttype--;\n\n\t\t\t\tconst vector<int> &O=order[type].second;\n\t\t\t\trep(i,O.size()) wait.push_back(make_pair(type,O[i]));\n\t\t\t}\n\t\t\t// 料理ができた\n\t\t\telse{\n\t\t\t\ttype*=-1;\n\t\t\t\ttype--;\n\n\t\t\t\tfin[type]++;\n\t\t\t\tif(fin[type]==order[type].second.size()) ans[type]=t;\n\t\t\t\tcooking--;\n\t\t\t}\n\n\t\t\tif(cooking==0 && !wait.empty()){\n\t\t\t\tint a=wait.front().first,menuid=wait.front().second; wait.pop_front();\n\t\t\t\tQ.push(make_pair(-(t+M[menuid].t),-(a+1)));\n\t\t\t\tcooking++;\n\n\t\t\t\tint cnt=1;\n\t\t\t\trep(i,wait.size()) if(wait[i].second==menuid && cnt<M[menuid].lim) {\n\t\t\t\t\tQ.push(make_pair(-(t+M[menuid].t),-(wait[i].first+1)));\n\t\t\t\t\twait.erase(wait.begin()+i);\n\t\t\t\t\ti--;\n\t\t\t\t\tcnt++;\n\t\t\t\t\tcooking++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\trep(i,m) printf(\"%d\\n\",ans[i]);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2059_392488", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\nstruct Dish{\n string name;\n int time;\n int limit;\n int orderTime;\n int dishIdx;\n int whois;\n int ordNum;\n Dish(){}\n Dish(string _name,int _time,int _limit,int _orderTime,int _dishIdx,int _whois,int _ordNum){\n name=_name;\n time=_time;\n limit=_limit;\n orderTime=_orderTime;\n dishIdx=_dishIdx;\n whois=_whois;\n ordNum=_ordNum;\n }\n bool operator<(const Dish&dh)const{\n if(dh.orderTime!=orderTime)return dh.orderTime>this->orderTime;\n else if(dh.time!=this->time)return dh.time<this->time;\n else if(dh.dishIdx!=this->dishIdx)return dh.dishIdx>this->dishIdx;\n else if(dh.whois!=this->whois)return dh.whois>this->whois;\n return dh.ordNum<this->ordNum;\n }\n};\nstruct Event{\n int time;\n int idx;\n int dishIdx;\n int kind;\n vector<int> who;\n Event(){}\n Event(int _time,int _idx,int _dishIdx,int _kind){\n time=_time;\n idx=_idx;\n dishIdx=_dishIdx;\n kind=_kind;\n }\n bool operator<(const Event&e)const{\n if(e.time!=this->time)return e.time<this->time;\n return (e.kind<this->kind);\n }\n};\n\nset<Dish> waitDish;\nint N,M;\nDish ds[101];\nmap<string,int> ms;\nint T[101];\nint K[101];\nint leftDish[101];\nint finTimes[101];\nbool need[100001];\nvector<string> order[101];\n\nint main(){\n bool fst=true;\n while(cin>>N>>M&&(N|M)){\n if(fst)fst=false;\n else cout<<endl;\n waitDish.clear();\n ms.clear();\n for(int i=0;i<N;i++){\n cin>>ds[i].name>>ds[i].limit>>ds[i].time;\n ms[ds[i].name]=i;\n ds[i].dishIdx=i;\n }\n for(int i=0;i<M;i++){\n order[i].clear();\n cin>>T[i];\n cin>>K[i];\n leftDish[i]=K[i];\n for(int j=0;j<K[i];j++){\n string str;\n cin>>str;\n order[i].push_back(str);\n }\n }\n priority_queue<Event> pq;\n bool isCook=false;\n for(int i=0;i<M;i++){\n pq.push(Event(T[i],i,0,0));\n }\n while(pq.size()){\n Event e=pq.top();pq.pop();\n if(e.kind==0){\n for(int j=0;j<K[e.idx];j++){\n int dk=ms[order[e.idx][j]];\n Dish tmp=ds[dk];\n tmp.whois=e.idx;\n tmp.orderTime=e.time;\n tmp.ordNum=j;\n waitDish.insert(tmp);\n }\n Event ne=Event(e.time,0,0,2);\n pq.push(ne);\n }\n else if(e.kind==1){\n int dkind=e.idx;\n for(int i=0;i<e.who.size();i++){\n int user=e.who[i];\n leftDish[user]--;\n if(leftDish[user]==0)\n finTimes[user]=e.time;\n }\n isCook=false;\n Event ne=Event(e.time,0,0,2);\n pq.push(ne);\n }\n else{\n if(isCook||waitDish.size()==0)continue;\n isCook=true;\n Dish d=*(waitDish.begin());\n int lim=(waitDish.begin())->limit;\n Event ne=Event(e.time+(waitDish.begin())->time,0,d.dishIdx,1);\n vector<Dish> tmp;\n for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++)\n tmp.push_back(*it);\n vector<int> idxs;\n for(int i=0;i<tmp.size();i++){\n if(lim>0&&tmp[i].dishIdx==d.dishIdx){\n ne.who.push_back(tmp[i].whois);\n lim--;\n }\n else idxs.push_back(i);\n }\n waitDish.clear();\n for(int i=0;i<idxs.size();i++){\n int cur=idxs[i];\n waitDish.insert(tmp[cur]);\n }\n pq.push(ne);\n }\n }\n for(int i=0;i<M;i++)\n cout<<finTimes[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 0, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_2059_392485", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\nstruct Dish{\n string name;\n int time;\n int limit;\n int orderTime;\n int dishIdx;\n int whois;\n int ordNum;\n bool operator<(const Dish&dh)const{\n if(dh.orderTime!=orderTime)return dh.orderTime>this->orderTime;\n else if(dh.time!=this->time)return dh.time<this->time;\n else if(dh.dishIdx!=this->dishIdx)return dh.dishIdx>this->dishIdx;\n else if(dh.whois!=this->whois)return dh.whois>this->whois;\n return dh.ordNum<this->ordNum;\n }\n};\nstruct Event{\n int time;\n int idx;\n int dishIdx;\n int kind;\n vector<int> who;\n bool operator<(const Event&e)const{\n if(e.time!=this->time)return e.time<this->time;\n return (e.kind<this->kind);\n }\n};\n\nset<Dish> waitDish;\nint N,M;\nDish ds[101];\nmap<string,int> ms;\nint T[101];\nint K[101];\nint leftDish[101];\nint finTimes[101];\nbool need[100001];\nvector<string> order[101];\n\nint main(){\n bool fst=true;\n while(cin>>N>>M&&(N|M)){\n if(fst)fst=false;\n else cout<<endl;\n waitDish.clear();\n ms.clear();\n for(int i=0;i<N;i++){\n cin>>ds[i].name>>ds[i].limit>>ds[i].time;\n ms[ds[i].name]=i;\n ds[i].dishIdx=i;\n }\n for(int i=0;i<M;i++){\n order[i].clear();\n cin>>T[i];\n cin>>K[i];\n leftDish[i]=K[i];\n for(int j=0;j<K[i];j++){\n string str;\n cin>>str;\n order[i].push_back(str);\n }\n }\n priority_queue<Event> pq;\n bool isCook=false;\n for(int i=0;i<M;i++){\n Event e;\n e.kind=0;\n e.idx=i;\n e.time=T[i];\n pq.push(e);\n }\n while(pq.size()){\n Event e=pq.top();pq.pop();\n if(e.kind==0){\n for(int j=0;j<K[e.idx];j++){\n int dk=ms[order[e.idx][j]];\n Dish tmp=ds[dk];\n tmp.whois=e.idx;\n tmp.orderTime=e.time;\n tmp.ordNum=j;\n waitDish.insert(tmp);\n }\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else if(e.kind==1){\n int dkind=e.idx;\n for(int i=0;i<e.who.size();i++){\n int user=e.who[i];\n leftDish[user]--;\n if(leftDish[user]==0){\n finTimes[user]=e.time;\n }\n }\n isCook=false;\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else{\n if(isCook||waitDish.size()==0)continue;\n isCook=true;\n Dish d=*(waitDish.begin());\n int lim=(waitDish.begin())->limit;\n Event ne;ne.kind=1;ne.dishIdx=d.dishIdx;\n ne.time=e.time+(waitDish.begin())->time;\n vector<Dish> tmp;\n for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++){\n tmp.push_back(*it);\n }\n vector<int> idxs;\n for(int i=0;i<tmp.size();i++){\n if(lim>0&&tmp[i].dishIdx==d.dishIdx){\n ne.who.push_back(tmp[i].whois);\n lim--;\n }\n else idxs.push_back(i);\n }\n waitDish.clear();\n for(int i=0;i<idxs.size();i++){\n int cur=idxs[i];\n waitDish.insert(tmp[cur]);\n }\n pq.push(ne);\n }\n }\n for(int i=0;i<M;i++)\n cout<<finTimes[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1048, "score_of_the_acc": -1.7507, "final_rank": 10 }, { "submission_id": "aoj_2059_392484", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\nstruct Dish{\n string name;\n int time;\n int limit;\n int orderTime;\n int dishIdx;\n int whois;\n int ordNum;\n bool operator<(const Dish&dh)const{\n if(dh.orderTime!=orderTime)return dh.orderTime>this->orderTime;\n else if(dh.time!=this->time)return dh.time<this->time;\n else if(dh.dishIdx!=this->dishIdx)return dh.dishIdx>this->dishIdx;\n else if(dh.whois!=this->whois)return dh.whois>this->whois;\n return dh.ordNum<this->ordNum;\n }\n};\nstruct Event{\n int time;\n int idx;\n int dishIdx;\n int kind;\n vector<int> who;\n bool operator<(const Event&e)const{\n if(e.time<this->time)return true;\n else if(e.time>this->time)return false;\n return (e.kind<this->kind);\n }\n};\n\nset<Dish> waitDish;\nint N,M;\nDish ds[101];\nmap<string,int> ms;\nint T[101];\nint K[101];\nint leftDish[101];\nint finTimes[101];\nbool need[100001];\nvector<string> order[101];\n\nint main(){\n bool fst=true;\n while(cin>>N>>M&&(N|M)){\n if(fst)fst=false;\n else cout<<endl;\n waitDish.clear();\n ms.clear();\n for(int i=0;i<N;i++){\n cin>>ds[i].name>>ds[i].limit>>ds[i].time;\n ms[ds[i].name]=i;\n ds[i].dishIdx=i;\n }\n for(int i=0;i<M;i++){\n order[i].clear();\n cin>>T[i];\n cin>>K[i];\n leftDish[i]=K[i];\n for(int j=0;j<K[i];j++){\n string str;\n cin>>str;\n order[i].push_back(str);\n }\n }\n priority_queue<Event> pq;\n bool isCook=false;\n for(int i=0;i<M;i++){\n Event e;\n e.kind=0;\n e.idx=i;\n e.time=T[i];\n pq.push(e);\n }\n while(pq.size()){\n Event e=pq.top();pq.pop();\n if(e.kind==0){\n for(int j=0;j<K[e.idx];j++){\n int dk=ms[order[e.idx][j]];\n Dish tmp=ds[dk];\n tmp.whois=e.idx;\n tmp.orderTime=e.time;\n tmp.ordNum=j;\n waitDish.insert(tmp);\n }\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else if(e.kind==1){\n int dkind=e.idx;\n for(int i=0;i<e.who.size();i++){\n int user=e.who[i];\n leftDish[user]--;\n if(leftDish[user]==0){\n finTimes[user]=e.time;\n }\n }\n isCook=false;\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else{\n if(isCook||waitDish.size()==0)continue;\n isCook=true;\n Dish d=*(waitDish.begin());\n int lim=(waitDish.begin())->limit;\n Event ne;ne.kind=1;ne.dishIdx=d.dishIdx;\n ne.time=e.time+(waitDish.begin())->time;\n vector<Dish> tmp;\n for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++){\n tmp.push_back(*it);\n }\n vector<int> idxs;\n for(int i=0;i<tmp.size();i++){\n if(lim>0&&tmp[i].dishIdx==d.dishIdx){\n ne.who.push_back(tmp[i].whois);\n lim--;\n }\n else idxs.push_back(i);\n }\n waitDish.clear();\n for(int i=0;i<idxs.size();i++){\n int cur=idxs[i];\n waitDish.insert(tmp[cur]);\n }\n pq.push(ne);\n }\n }\n for(int i=0;i<M;i++)\n cout<<finTimes[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1048, "score_of_the_acc": -1.7507, "final_rank": 10 }, { "submission_id": "aoj_2059_392452", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\nstruct Dish{\n string name;\n int time;\n int limit;\n int orderTime;\n int dishIdx;\n int whois;\n int ordNum;\n bool operator<(const Dish&dh)const{\n if(dh.orderTime>this->orderTime)return true;\n else if(dh.orderTime<this->orderTime)return false;\n else if(dh.time<this->time)return true;\n else if(dh.time>this->time)return false;\n else if(dh.dishIdx>this->dishIdx)return true;\n else if(dh.dishIdx<this->dishIdx)return false;\n else if(dh.whois>this->whois)return true;\n else if(dh.whois<this->whois)return false;\n return dh.ordNum<this->ordNum;\n }\n};\nstruct Event{\n int time;\n int idx;\n int dishIdx;\n int kind;\n vector<int> who;\n bool operator<(const Event&e)const{\n if(e.time<this->time)return true;\n else if(e.time>this->time)return false;\n return (e.kind<this->kind);\n }\n};\n\nset<Dish> waitDish;\nint N,M;\nDish ds[101];\nmap<string,int> ms;\nint T[101];\nint K[101];\nint leftDish[101];\nint finTimes[101];\nbool need[100001];\nvector<string> order[101];\n\nint main(){\n bool fst=true;\n while(cin>>N>>M&&(N|M)){\n if(fst)fst=false;\n else cout<<endl;\n waitDish.clear();\n ms.clear();\n for(int i=0;i<N;i++){\n cin>>ds[i].name>>ds[i].limit>>ds[i].time;\n ms[ds[i].name]=i;\n ds[i].dishIdx=i;\n }\n for(int i=0;i<M;i++){\n order[i].clear();\n cin>>T[i];\n cin>>K[i];\n leftDish[i]=K[i];\n for(int j=0;j<K[i];j++){\n string str;\n cin>>str;\n order[i].push_back(str);\n }\n }\n priority_queue<Event> pq;\n bool isCook=false;\n for(int i=0;i<M;i++){\n Event e;\n e.kind=0;\n e.idx=i;\n e.time=T[i];\n pq.push(e);\n }\n while(pq.size()){\n Event e=pq.top();pq.pop();\n if(e.kind==0){\n for(int j=0;j<K[e.idx];j++){\n int dk=ms[order[e.idx][j]];\n Dish tmp=ds[dk];\n tmp.whois=e.idx;\n tmp.orderTime=e.time;\n tmp.ordNum=j;\n waitDish.insert(tmp);\n }\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else if(e.kind==1){\n int dkind=e.idx;\n for(int i=0;i<e.who.size();i++){\n int user=e.who[i];\n leftDish[user]--;\n if(leftDish[user]==0){\n finTimes[user]=e.time;\n }\n }\n isCook=false;\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else{\n if(isCook||waitDish.size()==0)continue;\n isCook=true;\n Dish d=*(waitDish.begin());\n int lim=(waitDish.begin())->limit;\n Event ne;ne.kind=1;ne.dishIdx=d.dishIdx;\n ne.time=e.time+(waitDish.begin())->time;\n vector<Dish> tmp;\n for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++){\n tmp.push_back(*it);\n }\n vector<int> idxs;\n for(int i=0;i<tmp.size();i++){\n if(lim>0&&tmp[i].dishIdx==d.dishIdx){\n ne.who.push_back(tmp[i].whois);\n lim--;\n }\n else idxs.push_back(i);\n }\n waitDish.clear();\n for(int i=0;i<idxs.size();i++){\n int cur=idxs[i];\n waitDish.insert(tmp[cur]);\n }\n pq.push(ne);\n }\n }\n for(int i=0;i<M;i++)\n cout<<finTimes[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1048, "score_of_the_acc": -1.7507, "final_rank": 10 }, { "submission_id": "aoj_2059_392446", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\nstruct Dish{\n string name;\n int time;\n int limit;\n int orderTime;\n int idx;\n int whois;\n int ordNum;\n bool operator<(const Dish&dh)const{\n if(dh.orderTime>this->orderTime)return true;\n else if(dh.orderTime<this->orderTime)return false;\n else if(dh.time<this->time)return true;\n else if(dh.time>this->time)return false;\n else if(dh.idx>this->idx)return true;\n else if(dh.idx<this->idx)return false;\n else if(dh.whois>this->whois)return true;\n else if(dh.whois<this->whois)return false;\n return dh.ordNum<this->ordNum;\n }\n};\nstruct Event{\n int time;\n int idx;\n int kind;\n vector<int> who;\n bool operator<(const Event&e)const{\n if(e.time<this->time)return true;\n else if(e.time>this->time)return false;\n return (e.kind<this->kind);\n }\n};\n\nset<Dish> waitDish;\nint N,M;\nDish ds[101];\nmap<string,int> ms;\nint T[101];\nint K[101];\nint leftDish[101];\nint finTimes[101];\nbool need[100001];\nvector<string> order[101];\n\nint main(){\n bool fst=true;\n while(cin>>N>>M&&(N|M)){\n if(fst)fst=false;\n else cout<<endl;\n waitDish.clear();\n ms.clear();\n for(int i=0;i<N;i++){\n cin>>ds[i].name>>ds[i].limit>>ds[i].time;\n ms[ds[i].name]=i;\n ds[i].idx=i;\n }\n for(int i=0;i<M;i++){\n order[i].clear();\n cin>>T[i];\n cin>>K[i];\n leftDish[i]=K[i];\n for(int j=0;j<K[i];j++){\n string str;\n cin>>str;\n order[i].push_back(str);\n }\n }\n priority_queue<Event> pq;\n bool isCook=false;\n for(int i=0;i<M;i++){\n Event e;\n e.kind=0;\n e.idx=i;\n e.time=T[i];\n pq.push(e);\n }\n while(pq.size()){\n Event e=pq.top();pq.pop();\n if(e.kind==0){\n for(int j=0;j<K[e.idx];j++){\n int dk=ms[order[e.idx][j]];\n Dish tmp=ds[dk];\n tmp.whois=e.idx;\n tmp.orderTime=e.time;\n tmp.ordNum=j;\n waitDish.insert(tmp);\n }\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else if(e.kind==1){\n int dkind=e.idx;\n for(int i=0;i<e.who.size();i++){\n int user=e.who[i];\n leftDish[user]--;\n if(leftDish[user]==0){\n finTimes[user]=e.time;\n }\n }\n isCook=false;\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n else{\n if(isCook||waitDish.size()==0)continue;\n isCook=true;\n Dish d=*(waitDish.begin());\n int lim=(waitDish.begin())->limit;\n Event ne;ne.kind=1;ne.idx=d.idx;\n ne.time=e.time+(waitDish.begin())->time;\n vector<Dish> tmp;\n for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++){\n tmp.push_back(*it);\n }\n vector<int> idxs;\n for(int i=0;i<tmp.size();i++){\n if(lim>0&&tmp[i].idx==d.idx){\n ne.who.push_back(tmp[i].whois);\n lim--;\n }\n else idxs.push_back(i);\n }\n waitDish.clear();\n for(int i=0;i<idxs.size();i++){\n int cur=idxs[i];\n waitDish.insert(tmp[cur]);\n }\n pq.push(ne);\n }\n }\n for(int i=0;i<M;i++)\n cout<<finTimes[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1052, "score_of_the_acc": -1.7536, "final_rank": 13 }, { "submission_id": "aoj_2059_392445", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\n/*\n客がくる順番に注文を処理\n客の注文は、もっとも時間がかかる料理から処理\nおなじ時間の場合は、料理のindexが小さい方を先に処理\nまた、ある料理を調理開始する際,\nキューに同じ料理があれば、同時に料理する\nただし同時に料理可能な量は限られる\n*/\nstruct Dish{\n string name;\n int time;\n int limit;\n int orderTime;\n // 料理のidx\n int idx;\n // 注文した客のidx\n int whois;\n int ordNum;\n // かかる時間が長い順番でソート\n // 同じならidxで小さい順番にソート\n bool operator<(const Dish&dh)const{\n if(dh.orderTime>this->orderTime)return true;\n else if(dh.orderTime<this->orderTime)return false;\n else if(dh.time<this->time)return true;\n else if(dh.time>this->time)return false;\n else if(dh.idx>this->idx)return true;\n else if(dh.idx<this->idx)return false;\n // さらに同じなら、注文した客のidx順で取る\n else if(dh.whois>this->whois)return true;\n else if(dh.whois<this->whois)return false;\n return dh.ordNum<this->ordNum;\n }\n};\n\n/*\n{客が到着,\n料理終了,\n料理開始}\nのイベントを登録\n優先順位はこの順番で\n*/\nstruct Event{\n int time;\n // 料理の種類\n int idx;\n // kind=0 -> 客が到着,kind=1 -> 料理終了\n // kind=2 -> 料理開始\n int kind;\n // どの客の料理か\n vector<int> who;\n // 時間の小さい方を優先\n // 同じならば,優先順位が高いeventを優先\n bool operator<(const Event&e)const{\n if(e.time<this->time)return true;\n else if(e.time>this->time)return false;\n return (e.kind<this->kind);\n }\n};\n\n// 料理は客の頼んだ順番に関係なく\n// 今登録されている料理の情報に依存して決定される\nset<Dish> waitDish;\nint N,M;\nDish ds[101];\nmap<string,int> ms;\nint T[101];\nint K[101];\nint leftDish[101];\nint finTimes[101];\nbool need[100001];\nvector<string> order[101];\n\nint main(){\n bool fst=true;\n while(cin>>N>>M&&(N|M)){\n if(fst)fst=false;\n else cout<<endl;\n waitDish.clear();\n ms.clear();\n for(int i=0;i<N;i++){\n cin>>ds[i].name>>ds[i].limit>>ds[i].time;\n ms[ds[i].name]=i;\n ds[i].idx=i;\n }\n for(int i=0;i<M;i++){\n order[i].clear();\n cin>>T[i];\n cin>>K[i];\n // 残りの料理量\n leftDish[i]=K[i];\n for(int j=0;j<K[i];j++){\n string str;\n cin>>str;\n order[i].push_back(str);\n }\n }\n // 料理終了時か,客到着時に料理を開始\n priority_queue<Event> pq;\n bool isCook=false;\n // 客到着イベント登録\n for(int i=0;i<M;i++){\n Event e;\n e.kind=0;\n e.idx=i;\n e.time=T[i];\n pq.push(e);\n }\n while(pq.size()){\n Event e=pq.top();pq.pop();\n // 客到着\n if(e.kind==0){\n // 料理を登録\n for(int j=0;j<K[e.idx];j++){\n int dk=ms[order[e.idx][j]];\n Dish tmp=ds[dk];\n tmp.whois=e.idx;\n tmp.orderTime=e.time;\n tmp.ordNum=j;\n waitDish.insert(tmp);\n }\n // 料理開始イベントを登録\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n // 料理終了\n else if(e.kind==1){\n // 料理の種類\n int dkind=e.idx;\n // 登録してある料理を終了させる\n for(int i=0;i<e.who.size();i++){\n // ユーザのid\n int user=e.who[i];\n leftDish[user]--;\n // 注文終了時間を登録\n if(leftDish[user]==0){\n finTimes[user]=e.time;\n }\n }\n isCook=false;\n // 料理開始イベントを登録\n Event ne;\n ne.time=e.time;\n ne.kind=2;\n pq.push(ne);\n }\n // 料理開始\n else{\n // すでに料理を開始している\n // または待ち料理が存在しない\n if(isCook||waitDish.size()==0)continue;\n isCook=true;\n Dish d=*(waitDish.begin());\n int lim=(waitDish.begin())->limit;\n Event ne;ne.kind=1;ne.idx=d.idx;\n ne.time=e.time+(waitDish.begin())->time;\n vector<Dish> tmp;\n for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++){\n tmp.push_back(*it);\n }\n vector<int> idxs;\n for(int i=0;i<tmp.size();i++){\n if(lim>0&&tmp[i].idx==d.idx){\n ne.who.push_back(tmp[i].whois);\n lim--;\n }\n else idxs.push_back(i);\n }\n waitDish.clear();\n for(int i=0;i<idxs.size();i++){\n int cur=idxs[i];\n waitDish.insert(tmp[cur]);\n }\n //// limが0になるまで\n //ne.who.push_back(waitDish.begin()->whois);\n //lim--;\n //waitDish.erase(waitDish.begin());\n //for(set<Dish>::iterator it=waitDish.begin();it!=waitDish.end();it++){\n // if(lim==0)break;\n // // 同じ料理ならば、登録\n // if(it->idx==d.idx){\n // ne.who.push_back(it->whois);\n // lim--;\n // waitDish.erase(it);\n // it=waitDish.begin();\n // }\n //}\n //// イベントを登録\n pq.push(ne);\n }\n }\n // 終了時間を表示\n for(int i=0;i<M;i++)\n cout<<finTimes[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1052, "score_of_the_acc": -1.7536, "final_rank": 13 }, { "submission_id": "aoj_2059_354149", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nstruct Menu {\n int limit, time;\n int id;\n Menu(int limit, int time, int id) : limit(limit), time(time), id(id) {}\n Menu() {}\n};\nbool operator<(const Menu &a, const Menu &b) {\n return a.time != b.time ? a.time > b.time : a.id < b.id;\n};\n\nstruct Order {\n int T;\n Menu menu;\n int id;\n Order(int T, Menu menu, int id) : T(T), menu(menu), id(id) {}\n Order() {}\n};\nbool operator<(const Order &a, const Order &b) {\n return a.T != b.T ? a.T < b.T : a.menu < b.menu;\n}\n\nMenu menu[20];\nOrder order[1000];\nint ok[1000]; // ‚»‚Ì’•¶‚ðo‚µI‚í‚Á‚½ŽžŠÔ\nint K[100];\nint endTime[100][10];\n\nint main() {\n int n, m;\n int hoge = 0;\n while(cin >> n >> m, n || m) {\n if (hoge++) cout << endl;\n map<string, int> mp;\n REP(i, n) {\n string name;\n cin >> name >> menu[i].limit >> menu[i].time;\n menu[i].id = i;\n mp[name] = i;\n }\n int endnum[m];\n int num = 0;\n REP(i, m) {\n int T;\n cin >> T;\n cin >> K[i];\n REP(j, K[i]) {\n string dish;\n cin >> dish;\n order[num] = Order(T, menu[mp[dish]], i);\n num++;\n }\n }\n sort(order, order+num);\n ll nowtime = 0;\n memset(ok, -1, sizeof(ok));\n REP(i, num) {\n if (ok[i] >= 0) continue;\n nowtime = max(nowtime, (ll)order[i].T);\n //cout << i << \" \" << nowtime << \" \" << order[i].id << \" \" << order[i].menu.time << \" \" << order[i].menu.id << endl;\n ok[i] = nowtime + order[i].menu.time;\n int cnt = 1;\n for (int j=i+1; j<num && order[j].T<=nowtime && cnt<order[i].menu.limit; ++j) {\n if (order[j].menu.id == order[i].menu.id) {\n //cout << j << endl;\n ok[j] = nowtime + order[i].menu.time;\n cnt++;\n }\n }\n nowtime += order[i].menu.time;\n }\n int ans[m];\n memset(ans, 0, sizeof(ans));\n REP(i, num) {\n ans[order[i].id] = max(ans[order[i].id], ok[i]);\n }\n REP(i, m)\n cout << ans[i] << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 964, "score_of_the_acc": -0.8017, "final_rank": 2 }, { "submission_id": "aoj_2059_134434", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<map>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n\ntypedef long long ll;\nclass state{\npublic:\n int time,num;\n int cap;\n string name;\n bool operator<(const state& a)const{\n if (time != a.time)return time >a.time;\n return num < a.num;\n }\n};\n\nvector<ll> solve(vector<state> &dish,\n\t\t vector<int> &come,vector<vector<int> >&ord){\n ll now = 0;\n int p=0;\n vector<ll> ret(come.size(),(1LL)<<50);\n while(p < come.size()){\n if (come[p] > now)now=come[p];\n\n int cap=-1,num=-1;\n rep(i,dish.size()){\n if(ord[p][i] != 0){\n\tnum=i;\n\tcap=dish[i].cap;\n\tbreak;\n }\n }\n\n REP(i,p,ord.size() && cap){\n if (now < come[i])break;\n if (cap >= ord[i][num]){\n\tcap-=ord[i][num];\n\tord[i][num]=0;\n }else if (cap < ord[i][num]){\n\tord[i][num]-=cap;\n\tcap=0;\n }\n }\n\n now=now+dish[num].time;\n rep(i,ord.size()){\n bool isok=true;\n rep(j,ord[i].size())if (ord[i][j]!=0)isok=false;\n if (isok)ret[i]=min(ret[i],now);\n if (p == i && isok)p++;\n }\n }\n return ret;\n}\n\nint getindex(string a,map<string,int> &M){\n int index = M.size();\n if (M.count(a)==0)M[a]=index;\n return M[a];\n}\n\nmain(){\n int n,m;\n int te=0;\n while(cin>>n>>m && n){\n if( te++)cout << endl;\n vector<state> in(n);\n vector<int> come(m);\n vector<vector<int> > ord(m);\n map<string,int> M;\n \n rep(i,n){\n cin>>in[i].name>>in[i].cap>>in[i].time;\n in[i].num=i;\n }\n\n\n sort(in.begin(),in.end());\n rep(i,n)getindex(in[i].name,M);\n\n rep(i,m){\n cin>>come[i];\n rep(j,n)ord[i].pb(0);\n int k;\n cin>>k;\n rep(j,k){\n\tstring tmp;cin>>tmp;ord[i][getindex(tmp,M)]++;\n }\n }\n\n vector<ll> ans=solve(in,come,ord);\n rep(i,ans.size())cout << ans[i]<<endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 956, "score_of_the_acc": -0.9441, "final_rank": 4 }, { "submission_id": "aoj_2059_26694", "code_snippet": "#include <iostream>\n#include <string>\n#include <map>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nenum EVENT{ORDER, COOKED};\n\nclass Event\n{\npublic:\n\tint c,t,s,id;\n\tEVENT e;\n\t\n\tEvent(int c, int t, int s, int id, EVENT e)\n\t:c(c),t(t),e(e),s(s),id(id)\n\t{}\n\t\n\tbool operator<(const Event& a) const\n\t{\n\t\tif(t!=a.t) return t>a.t;\n\t\tif(e!=a.e) return e<a.e;\n\t\tif(c==a.c) return s>a.s;\n\t\treturn c>a.c;\n\t}\n};\n\nclass Order\n{\npublic:\n\tint id,pt;\n\tOrder(int id, int pt)\n\t:id(id),pt(pt)\n\t{}\n\t\n\tbool operator<(const Order& a) const\n\t{\n\t\tif(pt!=a.pt) return pt>a.pt;\n\t\treturn id<a.id;\n\t}\n};\n\nint main()\n{\n\tbool f=true;\n\tint M,N;\n\twhile(cin >> M >> N, (M||N))\n\t{\n\t\tif(!f) cout << endl;\n\t\tf=false;\n\t\t\n\t\tint limit[20], time[20];\n\t\tmap<string, int> d;\n\t\tfor(int i=0; i<M; i++)\n\t\t{\n\t\t\tint l,t;\n\t\t\tstring n;\n\t\t\tcin >> n >> l >> t;\n\t\t\td[n]=i;\n\t\t\t\n\t\t\tlimit[i]=l; time[i]=t;\n\t\t}\n\t\t\n\t\tpriority_queue<Event> q;\n\t\t\n\t\tint order[100],cooked[100]={0},cookid=-1,cooking=0,cooktime=-1,served[100],pretime=-1;\n\t\tfor(int i=0; i<N; i++)\n\t\t{\n\t\t\tint t,o;\n\t\t\tcin >> t >> o;\n\t\t\torder[i]=o;\n\t\t\tvector<Order> seq;\n\t\t\tfor(int j=0; j<o; j++)\n\t\t\t{\n\t\t\t\tstring n;\n\t\t\t\tcin >> n;\n\t\t\t\tseq.push_back(Order(d[n], time[d[n]]));\n\t\t\t}\n\t\t\tsort(seq.begin(), seq.end());\n\t\t\tfor(int j=0; j<seq.size(); j++)\n\t\t\t{\n\t\t\t\tq.push(Event(i,t,j,seq[j].id,ORDER));\n\t\t\t}\n\t\t}\n\t\twhile(!q.empty())\n\t\t{\n\t\t\tEvent e=q.top(); q.pop();\n\t\t\tif(e.e==ORDER)\n\t\t\t{\n\t\t\t\tif(cookid==e.id&&cooking<limit[e.id]&&pretime==e.t||cooking==0)\n\t\t\t\t{\n\t\t\t\t\tint tm=e.t+time[e.id];\n\t\t\t\t\tpretime=e.t;\n\t\t\t\t\tcooktime=tm;\n\t\t\t\t\tcooking++;\n\t\t\t\t\tcookid=e.id;\n\t\t\t\t\tq.push(Event(e.c, tm,e.s, e.id,COOKED));\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tq.push(Event(e.c, cooktime,e.s, e.id,e.e));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tcooking--;\n\t\t\t\tcooked[e.c]++;\n\t\t\t\tif(cooked[e.c]==order[e.c])\n\t\t\t\t\tserved[e.c]=e.t;\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor(int i=0; i<N; i++)\n\t\t\tcout << served[i] << endl;\n\t}\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 976, "score_of_the_acc": -1.2547, "final_rank": 9 } ]
aoj_2058_cpp
Problem A: Moduic Squares Have you ever heard of Moduic Squares? They are like 3 × 3 Magic Squares, but each of them has one extra cell called a moduic cell. Hence a Moduic Square has the following form. Figure 1: A Moduic Square Each of cells labeled from A to J contains one number from 1 to 10, where no two cells contain the same number. The sums of three numbers in the same rows, in the same columns, and in the diagonals in the 3 × 3 cells must be congruent modulo the number in the moduic cell. Here is an example Moduic Square: Figure 2: An example Moduic Square You can easily see that all the sums are congruent to 0 modulo 5. Now, we can consider interesting puzzles in which we complete Moduic Squares with partially filled cells. For example, we can complete the following square by filling 4 into the empty cell on the left and 9 on the right. Alternatively, we can also complete the square by filling 9 on the left and 4 on the right. So this puzzle has two possible solutions. Figure 3: A Moduic Square as a puzzle Your task is to write a program that determines the number of solutions for each given puzzle. Input The input contains multiple test cases. Each test case is specified on a single line made of 10 integers that represent cells A, B, C, D, E, F, G, H, I, and J as shown in the first figure of the problem statement. Positive integer means that the corresponding cell is already filled with the integer. Zero means that the corresponding cell is left empty. The end of input is identified with a line containing ten of -1’s. This is not part of test cases. Output For each test case, output a single integer indicating the number of solutions on a line. There may be cases with no solutions, in which you should output 0 as the number. Sample Input 3 1 6 8 10 7 0 0 2 5 0 0 0 0 0 0 0 0 0 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Output for the Sample Input 2 362880
[ { "submission_id": "aoj_2058_6825904", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint a[3][3], m, ans;\nbool chk[11];\n\nvoid f(int p) {\n\tif (p == 10) {\n\t\tset<int> num, mod;\n\t\tnum.insert(m);\n\t\tfor (int i = 0; i < 3; i++) {\n\t\t\tfor (int j = 0; j < 3; j++) num.insert(a[i][j]);\n\t\t\tmod.insert((a[i][0] + a[i][1] + a[i][2]) % m);\n\t\t\tmod.insert((a[0][i] + a[1][i] + a[2][i]) % m);\n\t\t}\n\t\tmod.insert((a[0][0] + a[1][1] + a[2][2]) % m);\n\t\tmod.insert((a[0][2] + a[1][1] + a[2][0]) % m);\n\t\tif (num.size() == 10 && mod.size() == 1) ans++;\n\t\treturn;\n\t}\n\tif (p == 9) {\n\t\tif (m > 0) f(p + 1);\n\t\telse {\n\t\t\tfor (int i = 1; i <= 10; i++) {\n\t\t\t\tif (chk[i]) continue;\n\t\t\t\tm = i;\n\t\t\t\tf(p + 1);\n\t\t\t\tm = 0;\n\t\t\t}\n\t\t}\n\t}\n\telse {\n\t\tint r = p / 3, c = p % 3;\n\t\tif (a[r][c] > 0) f(p + 1);\n\t\telse {\n\t\t\tfor (int i = 1; i <= 10; i++) {\n\t\t\t\tif (chk[i]) continue;\n\t\t\t\tchk[i] = true, a[r][c] = i;\n\t\t\t\tf(p + 1);\n\t\t\t\tchk[i] = false, a[r][c] = 0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(0);\n\n\twhile (true) {\n\t\tmemset(chk, 0, sizeof(chk));\n\t\tfor (int i = 0; i < 3; i++) {\n\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\tcin >> a[i][j];\n\t\t\t\tif (a[i][j] > 0) chk[a[i][j]] = true;\n\t\t\t}\n\t\t}\n\t\tcin >> m; if (m == -1) break;\n\t\tif (m) chk[m] = true;\n\n\t\tans = 0; f(0);\n\t\tcout << ans << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2740, "memory_kb": 3124, "score_of_the_acc": -1.0767, "final_rank": 18 }, { "submission_id": "aoj_2058_3763247", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nint main(){\n vector<vector<int>> v;\n vector<int> x(10);\n for(int i = 0; i < 10; i++) x[i] = i+1;\n do{\n int z = (x[0]+x[1]+x[2])%x[9];\n bool judge = true;\n for(int i = 0; i < 3; i++){\n judge &= (x[3*i]+x[3*i+1]+x[3*i+2])%x[9] == z;\n judge &= (x[i]+x[3+i]+x[6+i])%x[9] == z;\n }\n judge &= (x[0]+x[4]+x[8])%x[9] == z;\n judge &= (x[2]+x[4]+x[6])%x[9] == z;\n if(judge) v.push_back(x);\n }while(next_permutation(x.begin(),x.end()));\n \n while(1){\n for(int i = 0; i < 10; i++) cin >> x[i];\n if(x[0] == -1) break;\n int ans = 0;\n for(vector<int> y : v){\n bool judge = true;\n for(int i = 0; i < 10; i++){\n judge &= x[i]==0 || x[i]==y[i];\n }\n if(judge) ans++;\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 28348, "score_of_the_acc": -1.0855, "final_rank": 19 }, { "submission_id": "aoj_2058_2861147", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\nint main() {\n\tvector<int>v(10);\n\tiota(v.begin(),v.end(),1);\n\n\tvector<vector<int>>anss;\n\tdo {\n\t\tvector<vector<int>>field(3,vector<int>(3));\n\t\tconst int mod=v[9];\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tfor (int j = 0; j < 3; ++j) {\n\t\t\t\tfield[i][j]=v[i*3+j];\n\t\t\t}\n\t\t}\n\t\tbool ok=true;\n\t\tint need=field[0][0]+field[1][1]+field[2][2];\n\t\tneed%=mod;\n\t\tfor (int y = 0; y < 3; ++y) {\n\t\t\tint asum=0;\n\t\t\tfor (int x = 0; x < 3; ++x) {\n\t\t\t\tasum+=field[y][x];\n\t\t\t}\n\t\t\tif(asum%mod!=need)ok=false;\n\t\t}\n\t\tfor (int x = 0; x < 3; ++x) {\n\t\t\tint asum=0;\n\t\t\tfor (int y = 0; y < 3; ++y) {\n\t\t\t\tasum+=field[y][x];\n\t\t\t}\n\t\t\tif(asum%mod != need)ok=false;\n\t\t}\n\t\tfor (int i = 0; i < 2; ++i) {\n\t\t\tint asum=0;\n\t\t\tfor (int j = 0; j < 3; ++j) {\n\t\t\t\tasum+=field[j][i?j:2-j];\n\t\t\t}\n\t\t\tif(asum%mod != need)ok=false;\n\t\t}\n\t\tif (ok) {\n\t\t\tanss.emplace_back(v);\n\t\t}\n\t}while(next_permutation(v.begin(),v.end()));\n\n\twhile (true){\n\t\tvector<int>v(10);\n\t\tfor(int i=0;i<10;++i)cin>>v[i];\n\t\tif(v[0]==-1)break;\n\t\tint sum=0;\n\t\tfor (auto && ans : anss) {\n\t\t\tbool ok=true;\n\t\t\tfor (int i = 0; i < 10; ++i) {\n\t\t\t\tif(v[i]&&v[i]!=ans[i])ok=false;\n\t\t\t}\n\t\t\tif(ok)sum++;\n\t\t}\n\t\tcout<<sum<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 28260, "score_of_the_acc": -1.1938, "final_rank": 20 }, { "submission_id": "aoj_2058_2584461", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Info{\n\tint table[3][3];\n\tint mod_num,used,decided_num;\n};\n\nint POW[11];\n\nint ans;\n\nbool check(Info info){\n\n\tbool check_table[10];\n\tfor(int i = 0; i < 10; i++)check_table[i] = false;\n\tint row_0_num,row_1_num,row_2_num,col_0_num,col_1_num,col_2_num,cross_0_num,cross_1_num;\n\n\tif(info.table[0][0] == 0 || info.table[0][1] == 0 || info.table[0][2] == 0){\n\n\t}else{\n\t\trow_0_num = (info.table[0][0]+info.table[0][1]+info.table[0][2])%info.mod_num;\n\t\tcheck_table[row_0_num] = true;\n\t}\n\n\tif(info.table[1][0] == 0 || info.table[1][1] == 0 || info.table[1][2] == 0){\n\n\t}else{\n\t\trow_1_num = (info.table[1][0]+info.table[1][1]+info.table[1][2])%info.mod_num;\n\t\tcheck_table[row_1_num] = true;\n\t}\n\n\tif(info.table[2][0] == 0 || info.table[2][1] == 0 || info.table[2][2] == 0){\n\n\t}else{\n\t\trow_2_num = (info.table[2][0]+info.table[2][1]+info.table[2][2])%info.mod_num;\n\t\tcheck_table[row_2_num] = true;\n\t}\n\n\tif(info.table[0][0] == 0 || info.table[1][0] == 0 || info.table[2][0] == 0){\n\n\t}else{\n\t\tcol_0_num = (info.table[0][0]+info.table[1][0]+info.table[2][0])%info.mod_num;\n\t\tcheck_table[col_0_num] = true;\n\t}\n\n\tif(info.table[0][1] == 0 || info.table[1][1] == 0 || info.table[2][1] == 0){\n\n\t}else{\n\t\tcol_1_num = (info.table[0][1]+info.table[1][1]+info.table[2][1])%info.mod_num;\n\t\tcheck_table[col_1_num] = true;\n\t}\n\n\tif(info.table[0][2] == 0 || info.table[1][2] == 0 || info.table[2][2] == 0){\n\n\t}else{\n\t\tcol_2_num = (info.table[0][2]+info.table[1][2]+info.table[2][2])%info.mod_num;\n\t\tcheck_table[col_2_num] = true;\n\t}\n\n\tif(info.table[0][0] == 0 || info.table[1][1] == 0 || info.table[2][2] == 0){\n\n\t}else{\n\t\tcross_0_num = (info.table[0][0]+info.table[1][1]+info.table[2][2])%info.mod_num;\n\t\tcheck_table[cross_0_num] = true;\n\t}\n\n\tif(info.table[0][2] == 0 || info.table[1][1] == 0 || info.table[2][0] == 0){\n\n\t}else{\n\t\tcross_1_num = (info.table[0][2]+info.table[1][1]+info.table[2][0])%info.mod_num;\n\t\tcheck_table[cross_1_num] = true;\n\t}\n\n\tint count = 0;\n\tfor(int i = 0; i < 10; i++){\n\t\tif(check_table[i]){\n\t\t\tcount++;\n\t\t}\n\t}\n\n\treturn count <= 1;\n\n}\n\nvoid copyInfo(Info& to,Info from){\n\tfor(int row = 0; row < 3; row++){\n\t\tfor(int col = 0; col < 3; col++){\n\t\t\tto.table[row][col] = from.table[row][col];\n\t\t}\n\t}\n\tto.mod_num = from.mod_num;\n}\n\nvoid recursive(Info info){\n\n\tif(info.decided_num == 10){\n\n\t\tif(check(info)){\n\t\t\tans++;\n\t\t}\n\t\treturn;\n\t}\n\n\n\tif(info.mod_num == 0){\n\t\tfor(int loop = 1; loop <= 10; loop++){\n\t\t\tif(info.used & (1 << loop)){\n\t\t\t\t//Do nothing\n\t\t\t}else{\n\t\t\t\tInfo new_info;\n\t\t\t\tcopyInfo(new_info,info);\n\t\t\t\tnew_info.mod_num = loop;\n\t\t\t\tnew_info.decided_num = info.decided_num+1;\n\t\t\t\tnew_info.used = info.used + POW[loop];\n\t\t\t\tif(check(new_info)){\n\t\t\t\t\trecursive(new_info);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\n\tfor(int row = 0; row < 3; row++){\n\t\tfor(int col = 0; col < 3; col++){\n\t\t\tif(info.table[row][col] == 0){\n\t\t\t\tfor(int loop = 1; loop <= 10; loop++){\n\t\t\t\t\tif(info.used & (1 << loop)){\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t}else{\n\t\t\t\t\t\tInfo new_info;\n\t\t\t\t\t\tcopyInfo(new_info,info);\n\t\t\t\t\t\tnew_info.table[row][col] = loop;\n\t\t\t\t\t\tnew_info.decided_num = info.decided_num+1;\n\t\t\t\t\t\tnew_info.used = info.used + POW[loop];\n\t\t\t\t\t\tif(check(new_info)){\n\t\t\t\t\t\t\trecursive(new_info);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nint main(){\n\n\tfor(int i = 1; i <= 10; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tInfo first;\n\t\tfor(int row = 0; row < 3; row++){\n\t\t\tfor(int col = 0; col < 3; col++)scanf(\"%d\",&first.table[row][col]);\n\t\t}\n\t\tscanf(\"%d\",&first.mod_num);\n\n\t\tif(first.table[0][0] == -1)break;\n\n\t\tfirst.used = 0;\n\t\tfirst.decided_num = 0;\n\n\t\tfor(int row = 0; row < 3; row++){\n\t\t\tfor(int col = 0; col < 3; col++){\n\t\t\t\tif(first.table[row][col] != 0){\n\t\t\t\t\tfirst.used += POW[first.table[row][col]];\n\t\t\t\t\tfirst.decided_num++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(first.mod_num != 0){\n\t\t\tfirst.used += POW[first.mod_num];\n\t\t\tfirst.decided_num++;\n\t\t}\n\n\t\tans = 0;\n\n\t\trecursive(first);\n\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3316, "score_of_the_acc": -0.1284, "final_rank": 10 }, { "submission_id": "aoj_2058_2324903", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint t[9],d;\nbool used[11];\nbool C(void){\n int r=(t[0]+t[1]+t[2])%t[9];\n return ((t[3]+t[4]+t[5])%t[9]==r&&(t[6]+t[7]+t[8])%t[9]==r&&(t[0]+t[3]+t[6])%t[9]==r&&(t[1]+t[4]+t[7])%t[9]==r&&(t[2]+t[5]+t[8])%t[9]==r&&(t[0]+t[4]+t[8])%t[9]==r&&(t[2]+t[4]+t[6])%t[9]==r);\n}\n \nint dfs(int d){\n if(d==10)return C()?1:0;\n if(t[d]!=0)return dfs(d+1);\n int res = 0;\n for(int i=1;i<=10;i++){\n if(!used[i]){\n t[d]=i;\n used[i]=1;\n res+=dfs(d+1);\n used[i]=0;\n t[d]=0;\n }\n }\n return res;\n}\nmain(){\n while(1){\n memset(used,0,sizeof(used));\n for(int i=0;i<10;i++){\n cin>>t[i];\n if(t[i]>0)used[t[i]]=1;\n }\n if(t[0]==-1)break;\n cout<<dfs(0)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3040, "score_of_the_acc": -0.1071, "final_rank": 8 }, { "submission_id": "aoj_2058_2324900", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint t[9],d;\nbool used[11];\nbool C(void){\n int r=(t[0]+t[1]+t[2])%t[9];\n return ((t[3]+t[4]+t[5])%t[9]==r&&(t[6]+t[7]+t[8])%t[9]==r&&(t[0]+t[3]+t[6])%t[9]==r&&(t[1]+t[4]+t[7])%t[9]==r&&(t[2]+t[5]+t[8])%t[9]==r&&(t[0]+t[4]+t[8])%t[9]==r&&(t[2]+t[4]+t[6])%t[9]==r);\n}\n \nint dfs(int d){\n if(d==10)return C()?1:0;\n if(t[d]!=0)return dfs(d+1);\n int res = 0;\n for(int i=1;i<=10;i++){\n if(!used[i]){\n t[d] = i;\n used[i] = true;\n res+=dfs(d+1);\n used[i] = false;\n t[d] = 0;\n }\n }\n return res;\n}\nmain(){\n while(1){\n memset(used,0,sizeof(used));\n for(int i=0;i<10;i++){\n cin>>t[i];\n if(t[i]>0)used[t[i]]=1;\n }\n if(t[0]==-1)break;\n cout<<dfs(0)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 2964, "score_of_the_acc": -0.1043, "final_rank": 7 }, { "submission_id": "aoj_2058_2276281", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool u[11];\nint a[10],x,y;\nbool ck() {\n if(a[0]&&a[1]&&a[2]&&(a[0]+a[1]+a[2])%x!=y) return 0;\n if(a[3]&&a[4]&&a[5]&&(a[3]+a[4]+a[5])%x!=y) return 0;\n if(a[6]&&a[7]&&a[8]&&(a[6]+a[7]+a[8])%x!=y) return 0;\n if(a[0]&&a[3]&&a[6]&&(a[0]+a[3]+a[6])%x!=y) return 0;\n if(a[1]&&a[4]&&a[7]&&(a[1]+a[4]+a[7])%x!=y) return 0;\n if(a[2]&&a[5]&&a[8]&&(a[2]+a[5]+a[8])%x!=y) return 0;\n if(a[0]&&a[4]&&a[8]&&(a[0]+a[4]+a[8])%x!=y) return 0;\n if(a[2]&&a[4]&&a[6]&&(a[2]+a[4]+a[6])%x!=y) return 0;\n return 1;\n}\n \nint dfs(int k) {\n if(!ck()) return 0;\n if(k==9) return 1;\n int ans=0;\n if(a[k]) ans+=dfs(k+1);\n else {\n for(int i=1; i<=10; i++) {\n if(u[i]) continue;\n a[k]=i;u[i]=1;\n ans+=dfs(k+1);\n a[k]=0;u[i]=0;\n }\n }\n return ans;\n}\n \nint main() {\n while(1) {\n memset(u,0,sizeof(u));\n for(int i=0; i<10; i++) {\n cin >> a[i];\n u[a[i]]=1;\n }\n if(a[0]==-1) break;\n x=a[9];\n int ans=0;\n if(!x) {\n for(int i=1; i<11; i++) {\n if(u[i]) continue;\n x=i;\n u[i]=1;\n for(int j=0; j<x; j++) {y=j;ans+=dfs(0);}\n u[i]=0;\n }\n } else {\n for(int j=0; j<x; j++) {y=j;ans+=dfs(0);}\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3092, "score_of_the_acc": -0.083, "final_rank": 5 }, { "submission_id": "aoj_2058_2275830", "code_snippet": "#include<bits/stdc++.h>\n#define index ffff\nusing namespace std;\n\nint d[10],ans;\nint index[10];\nint M;\n\nint cal(int a,int b,int c){\n \n return (index[a]+index[b]+index[c])%M;\n \n}\n\n\nbool check(){\n \n M=index[9];\n \n int X=cal(0,1,2);\n \n if(X!=cal(3,4,5))return false;\n \n if(X!=cal(6,7,8))return false;\n\n if(X!=cal(0,3,6))return false;\n\n if(X!=cal(1,4,7))return false;\n \n if(X!=cal(2,5,8))return false;\n \n if(X!=cal(0,4,8))return false;\n \n if(X!=cal(2,4,6))return false;\n\n return true;\n \n}\n\nvoid solve(){\n \n\n for(int i=0;i<10;i++) index[i]=i+1;\n\n\n do{\n int f=0;\n \n for(int i=0;i<10;i++)\n if(d[i]!=0&&d[i]!=index[i]){\n\tf=1;\n\tbreak;\n }\n\n if(f)continue;\n \n if(check())ans++;\n \n }while(next_permutation(index,index+10));\n \n}\n\n\nint main(){\n\n while(1){\n \n for(int i=0;i<10;i++) cin>>d[i];\n \n if(d[0]<0)break;\n\n ans=0;\n \n solve();\n \n cout<<ans<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3016, "score_of_the_acc": -0.1992, "final_rank": 13 }, { "submission_id": "aoj_2058_2275811", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool u[11];\nint a[10],x,y;\nbool ck() {\n if(a[0]&&a[1]&&a[2]&&(a[0]+a[1]+a[2])%x!=y) return 0;\n if(a[3]&&a[4]&&a[5]&&(a[3]+a[4]+a[5])%x!=y) return 0;\n if(a[6]&&a[7]&&a[8]&&(a[6]+a[7]+a[8])%x!=y) return 0;\n if(a[0]&&a[3]&&a[6]&&(a[0]+a[3]+a[6])%x!=y) return 0;\n if(a[1]&&a[4]&&a[7]&&(a[1]+a[4]+a[7])%x!=y) return 0;\n if(a[2]&&a[5]&&a[8]&&(a[2]+a[5]+a[8])%x!=y) return 0;\n if(a[0]&&a[4]&&a[8]&&(a[0]+a[4]+a[8])%x!=y) return 0;\n if(a[2]&&a[4]&&a[6]&&(a[2]+a[4]+a[6])%x!=y) return 0;\n return 1;\n}\n\nint dfs(int k) {\n if(!ck()) return 0;\n if(k==9) return 1;\n int ans=0;\n if(a[k]) ans+=dfs(k+1);\n else {\n for(int i=1; i<=10; i++) {\n if(u[i]) continue;\n a[k]=i;\n u[i]=1;\n ans+=dfs(k+1);\n a[k]=0;\n u[i]=0;\n }\n }\n return ans;\n}\n\nint main() {\n while(1) {\n memset(u,0,sizeof(u));\n for(int i=0; i<10; i++) {\n cin >> a[i];\n u[a[i]]=1;\n }\n if(a[0]==-1) break;\n x=a[9];\n int ans=0;\n if(!x) {\n for(int i=1; i<11; i++) {\n if(u[i]) continue;\n x=i;\n u[i]=1;\n for(int j=0; j<x; j++) {\n y=j;\n ans+=dfs(0);\n }\n u[i]=0;\n }\n } else {\n for(int j=0; j<x; j++) {\n y=j;\n ans+=dfs(0);\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3096, "score_of_the_acc": -0.0868, "final_rank": 6 }, { "submission_id": "aoj_2058_2275714", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nint diff[10];\nint num[]={1,2,3,4,5,6,7,8,9,10};\n\nbool check(){\n int A[3][3],mod = num[9];\n for(int i=0;i<10;i++)if(diff[i]>0&&num[i]!=diff[i])return 0;\n for(int i=0;i<9;i++) A[i/3][i%3] = num[i];\n\n\n int used[20]={};\n for(int i=0;i<3;i++){\n int a=0,b=0; \n for(int j=0;j<3;j++)a+=A[i][j],b+=A[j][i];\n a%=mod,b%=mod;\n used[a]++;\n used[b]++;\n if(used[a]!=(i+1)*2)return 0; \n }\n \nint a =A[0][0]+A[1][1]+A[2][2],b = A[0][2]+A[1][1]+A[2][0];\nused[a%mod]++,used[b%mod]++;\n return used[a%mod] == 8;\n}\n\nsigned main(){\n\n while(1){\n int Exit = 1;\n for(int i=0;i<10;i++)cin>>diff[i],Exit*=(diff[i]<0);\n if(Exit) return 0;\n\n for(int i=0;i<10;i++)num[i] = i+1;\n\n int ans=0;\n do{\n ans+=check();\n }while(next_permutation(num,num+10));\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 3032, "score_of_the_acc": -0.2667, "final_rank": 15 }, { "submission_id": "aoj_2058_1893505", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nint m;\nvi tmp;\nint w[8][3]={\n\t{0,1,2},\n\t{3,4,5},\n\t{6,7,8},\n\t{0,3,6},\n\t{1,4,7},\n\t{2,5,8},\n\t{0,4,8},\n\t{2,4,6}\n};\nvi in;\nbool f(){\n\tvi su(8);\n\trep(i,8){\n\t\tint sum=0;\n\t\trep(j,3)sum+=tmp[w[i][j]];\n\t\tsu[i]=sum%m;\n\t}\n\tbool h=true;\n\trep(i,8)if(su[0]!=su[i])h=false;\n\treturn h;\n}\nint main(){\n\twhile(1){\n\t\tin=vi(10);\n\t\trep(i,10)cin>>in[i];\n\t\tif(in[0]==-1)break;\n\t\ttmp=vi(11);\n\t\trep(i,10)tmp[in[i]]++;\n\t\tvi per;\n\t\trep(i,10)if(tmp[i+1]==0)per.pb(i+1);\n\t\tint out=0;\n\t\tdo{\n\t\t\ttmp=in;\n\t\t\tint c=0;\n\t\t\trep(i,10)if(tmp[i]==0)tmp[i]=per[c],c++;\n\t\t\tm=tmp[9];\n\t\t\tout+=f();\n\t\t}while(next_permutation(all(per)));\n\t\tcout<<out<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 1216, "score_of_the_acc": -0.1481, "final_rank": 12 }, { "submission_id": "aoj_2058_1882421", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n \nint c[10];\n \nbool check(){\n int sum = 0, MOD = c[9];\n rep(i,3) sum = (sum + c[i]) % MOD; \n if((c[3]+c[4]+c[5])%MOD != sum) return false;\n if((c[6]+c[7]+c[8])%MOD != sum) return false;\n if((c[0]+c[3]+c[6])%MOD != sum) return false;\n if((c[1]+c[4]+c[7])%MOD != sum) return false;\n if((c[2]+c[5]+c[8])%MOD != sum) return false;\n if((c[0]+c[4]+c[8])%MOD != sum) return false;\n if((c[2]+c[4]+c[6])%MOD != sum) return false;\n return true;\n}\n \nint rec(int p,int S){\n int res = 0;\n if(__builtin_popcount(S) == 10){\n\treturn check();\n }\n if(!c[p]){\n\trep(i,10){\n\t if((S >> i) & 1) continue;\n\t c[p] = i+1;\n\t res += rec(p+1,S|(1<<i));\n\t c[p] = 0;\n\t}\n }else{\n\tres += rec(p+1,S);\n }\n return res;\n}\n \nint main(){\n while(true){\n\tint S = 0;\n\tbool EXIT = false;\n\trep(i,10){\n\t cin >> c[i];\n\t if(c[i] > 0){\n\t\tS |= 1<<(c[i]-1);\n\t }else if(c[i] == -1){\n\t\tEXIT = true;\n\t }\n\t}\n\tif(EXIT) break;\n\tcout << rec(0,S) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3048, "score_of_the_acc": -0.1297, "final_rank": 11 }, { "submission_id": "aoj_2058_1822775", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nint a[10];\nint main() {\n\twhile (true) {\n\t\tfor (int i = 0; i < 10; i++)cin >> a[i];\n\t\tif (a[0] == -1)break;\n\t\tint b[10] = { 0,1,2,3,4,5,6,7,8,9 }; int cnt = 0;\n\t\tdo {\n\t\t\tint OK = 1; int f[10];\n\t\t\tfor (int i = 0; i < 10; i++)f[i] = b[i] + 1;\n\t\t\tfor (int i = 0; i < 10; i++) {\n\t\t\t\tif (a[i] >= 1 && a[i] != f[i])OK = 0;\n\t\t\t}\n\t\t\tif (OK == 0)continue;\n\t\t\tint c[4][4];\n\t\t\tfor (int i = 0; i < 16; i++)c[i / 4][i % 4] = 0;\n\t\t\tfor (int i = 0; i < 9; i++) {\n\t\t\t\tc[i / 3][i % 3] = f[i];\n\t\t\t\tc[3][i % 3] += f[i]; c[3][i % 3] %= f[9];\n\t\t\t\tc[i / 3][3] += f[i]; c[i / 3][3] %= f[9];\n\t\t\t}\n\t\t\tif (c[3][0] != c[3][1] || c[3][1] != c[3][2])continue;\n\t\t\tif (c[0][3] != c[1][3] || c[1][3] != c[2][3])continue;\n\t\t\tint d1 = c[0][0] + c[1][1] + c[2][2]; d1 %= f[9];\n\t\t\tint d2 = c[0][2] + c[1][1] + c[2][0]; d2 %= f[9];\n\t\t\tif (c[3][0] != d1 || d1 != d2)continue;\n\t\t\tcnt++;\n\t\t} while (next_permutation(b, b + 10));\n\t\tcout << cnt << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1570, "memory_kb": 1160, "score_of_the_acc": -0.5699, "final_rank": 17 }, { "submission_id": "aoj_2058_1819961", "code_snippet": "#include <cstdio>\nint a[10], e[8], used[11];\nbool ok() {\n\tfor (int i = 0; i < 3; i++) e[i] = (a[i] + a[i + 3] + a[i + 6]) % a[9];\n\tfor (int i = 0; i < 3; i++) e[i + 3] = (a[i * 3] + a[i * 3 + 1] + a[i * 3 + 2]) % a[9];\n\te[6] = (a[2] + a[4] + a[6]) % a[9];\n\te[7] = (a[0] + a[4] + a[8]) % a[9];\n\tfor (int i = 0; i < 7; i++) if (e[i] != e[i + 1]) return false;\n\treturn true;\n}\nint solve(int depth) {\n\tif (depth == 10) return ok() ? 1 : 0;\n\tif (a[depth]) return solve(depth + 1);\n\tint ret = 0;\n\tfor (int i = 1; i <= 10; i++) {\n\t\tif (!used[i]) {\n\t\t\tused[i] = 1; a[depth] = i;\n\t\t\tret += solve(depth + 1);\n\t\t\tused[i] = a[depth] = 0;\n\t\t}\n\t}\n\treturn ret;\n}\nint main() {\n\twhile (true) {\n\t\tfor (int i = 1; i < 11; i++) used[i] = 0;\n\t\tfor (int i = 0; i < 10; i++) {\n\t\t\tscanf(\"%d\", a + i);\n\t\t\tif (a[i]) used[a[i]] = 1;\n\t\t}\n\t\tif (a[0] == -1) break;\n\t\tprintf(\"%d\\n\", solve(0));\n\t}\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 2604, "score_of_the_acc": -0.1097, "final_rank": 9 }, { "submission_id": "aoj_2058_1366604", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\n/* typedef */\n\n/* global variables */\n\nint ds[10];\nbool used[11];\n\n/* subroutines */\n\ninline int dsum(int i0, int i1, int i2) {\n return (ds[i0] + ds[i1] + ds[i2]) % ds[9];\n}\n\nbool check() {\n int diag0 = dsum(0, 4, 8);\n return\n dsum(2, 4, 6) == diag0 &&\n dsum(0, 1, 2) == diag0 &&\n dsum(3, 4, 5) == diag0 &&\n dsum(6, 7, 8) == diag0 &&\n dsum(0, 3, 6) == diag0 &&\n dsum(1, 4, 7) == diag0 &&\n dsum(2, 5, 8) == diag0;\n}\n\nint rec(int k) {\n if (k >= 10) return check();\n\n if (ds[k] == 0) {\n int sum = 0;\n for (int d = 1; d <= 10; d++)\n if (! used[d]) {\n\tds[k] = d;\n\tused[d] = true;\n\tsum += rec(k + 1);\n\tds[k] = 0;\n\tused[d] = false;\n }\n return sum;\n }\n\n return rec(k + 1);\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> ds[0];\n if (ds[0] == -1) break;\n\n for (int i = 1; i < 10; i++) cin >> ds[i];\n\n memset(used, false, sizeof(used));\n\n for (int i = 0; i < 10; i++)\n if (ds[i] > 0) used[ds[i]] = true;\n\n cout << rec(0) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 1160, "score_of_the_acc": -0.0606, "final_rank": 4 }, { "submission_id": "aoj_2058_1171624", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n#include <complex>\n#include <assert.h>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <double,double> P;\n \nstatic const int tx[] = {+0,+1,+0,-1};\nstatic const int ty[] = {-1,+0,+1,+0};\n \nstatic const double EPS = 1e-8;\n\nint table[3][3];\n\nbool check(int mod){\n set<int> result;\n for(int y = 0; y < 3; y++){\n int sum = 0;\n for(int x = 0; x < 3; x++){\n sum += table[y][x];\n }\n result.insert(sum % mod);\n }\n if(result.size() > 1) return false;\n\n for(int x = 0; x < 3; x++){\n int sum = 0;\n for(int y = 0; y < 3; y++){\n sum += table[y][x];\n }\n result.insert(sum % mod);\n }\n if(result.size() > 1) return false;\n\n result.insert((table[0][0] + table[1][1] + table[2][2]) % mod);\n result.insert((table[0][2] + table[1][1] + table[2][0]) % mod);\n return (result.size() == 1);\n}\n\nint dfs(int pos,int used,int mod){\n if(pos >= 9 && check(mod)){\n return 1;\n }\n else if(pos >= 9){\n return 0;\n }\n int res = 0;\n int x = pos % 3;\n int y = pos / 3;\n\n if(table[y][x] == 0){\n for(int i = 1; i <= 10; i++){\n if(used & (1<<i)) continue;\n table[y][x] = i;\n res += dfs(pos + 1, used | (1<<i),mod);\n table[y][x] = 0;\n }\n }\n else{\n res += dfs(pos + 1, used,mod);\n }\n return res;\n}\n\nint main(){\n while(~scanf(\"%d\",&table[0][0])){\n int used = 0;\n if(table[0][0] != 0){\n used |= (1<<table[0][0]);\n }\n\n for(int y = 0; y < 3; y++){\n for(int x = 0; x < 3; x++){\n if(y == 0 && x == 0) continue;\n scanf(\"%d\",&table[y][x]);\n if(table[y][x] != 0){\n used |= (1<<table[y][x]);\n }\n }\n }\n int mod;\n scanf(\"%d\",&mod);\n if(mod == -1) break;\n\n used |= (1<<mod);\n int res = 0;\n if(mod == 0){\n int prev = used;\n for(int i = 1; i <= 10; i++){\n if(used & (1<<i)) continue;\n used |= (1<<i);\n mod = i;\n res += dfs(0,used,mod);\n used = prev;\n }\n }\n else{\n res += dfs(0,used,mod);\n }\n printf(\"%d\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 730, "memory_kb": 1184, "score_of_the_acc": -0.2585, "final_rank": 14 }, { "submission_id": "aoj_2058_1171623", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n#include <complex>\n#include <assert.h>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <double,double> P;\n \nstatic const int tx[] = {+0,+1,+0,-1};\nstatic const int ty[] = {-1,+0,+1,+0};\n \nstatic const double EPS = 1e-8;\n\nint table[3][3];\n\nbool check(int mod){\n set<int> result;\n for(int y = 0; y < 3; y++){\n int sum = 0;\n for(int x = 0; x < 3; x++){\n sum += table[y][x];\n }\n result.insert(sum % mod);\n }\n for(int x = 0; x < 3; x++){\n int sum = 0;\n for(int y = 0; y < 3; y++){\n sum += table[y][x];\n }\n result.insert(sum % mod);\n }\n result.insert((table[0][0] + table[1][1] + table[2][2]) % mod);\n result.insert((table[0][2] + table[1][1] + table[2][0]) % mod);\n return (result.size() == 1);\n}\n\nint dfs(int pos,int used,int mod){\n if(pos >= 9 && check(mod)){\n return 1;\n }\n else if(pos >= 9){\n return 0;\n }\n int res = 0;\n int x = pos % 3;\n int y = pos / 3;\n\n if(table[y][x] == 0){\n for(int i = 1; i <= 10; i++){\n if(used & (1<<i)) continue;\n table[y][x] = i;\n res += dfs(pos + 1, used | (1<<i),mod);\n table[y][x] = 0;\n }\n }\n else{\n res += dfs(pos + 1, used,mod);\n }\n return res;\n}\n\nint main(){\n while(~scanf(\"%d\",&table[0][0])){\n int used = 0;\n if(table[0][0] != 0){\n used |= (1<<table[0][0]);\n }\n\n for(int y = 0; y < 3; y++){\n for(int x = 0; x < 3; x++){\n if(y == 0 && x == 0) continue;\n scanf(\"%d\",&table[y][x]);\n if(table[y][x] != 0){\n used |= (1<<table[y][x]);\n }\n }\n }\n int mod;\n scanf(\"%d\",&mod);\n if(mod == -1) break;\n\n used |= (1<<mod);\n int res = 0;\n if(mod == 0){\n int prev = used;\n for(int i = 1; i <= 10; i++){\n if(used & (1<<i)) continue;\n used |= (1<<i);\n mod = i;\n res += dfs(0,used,mod);\n used = prev;\n }\n }\n else{\n res += dfs(0,used,mod);\n }\n printf(\"%d\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 1310, "memory_kb": 1188, "score_of_the_acc": -0.4743, "final_rank": 16 }, { "submission_id": "aoj_2058_1123659", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n\n\nint p[11] = {};\nint can[11] = {};\nint m;\n\nint check(int t){\n\tif( t < 4 ) return true;\n\t//if( t == 4 ) return (p[1]+p[4]+p[7]) % p[9] == 0;\n\tint c = (p[1]+p[4]+p[7]) % p[9];\n\tif( t == 5 ) return (p[0]+p[4]+p[8]) % p[9] == c;\n\tif( t == 6 ) return (p[0]+p[1]+p[2]) % p[9] == c;\n\tif( t == 7 ) return (p[2]+p[5]+p[8]) % p[9] == c;\n\tif( t == 8 ) return (p[6]+p[7]+p[8]) % p[9] == c && (p[2]+p[4]+p[6]) % p[9] == c;\n\tif( t == 9 ) return (p[0]+p[3]+p[6]) % p[9] == c && (p[3]+p[4]+p[5]) % p[9] == c;\n\treturn true;\n}\n//\n\nint d[] = {9,0,1,4,7,8,2,5,6,3};\n\nint solve(int t){\n\t/*cout << t << endl;\n\tcout << \"[\" << p[9] << \"]\" << endl;\n\tfor(int i = 0 ; i < 3 ; i++){\n\t\tfor(int j = 0 ; j < 3 ; j++) cout << p[i*3+j] << \" \";\n\t\tcout << endl;\n\t}\n\t*/\n\tif( !check(t-1) ) return 0;\n\tif( t == 10 ){\n\t\treturn 1;\n\t}\n\tint x = d[t];\n\tif( p[x] == 0 ){\n\t\tint ans = 0;\n\t\tfor(int i = 1 ; i <= 10 ; i++){\n\t\t\tif( can[i] ){\n\t\t\t\tcan[i] = false;\n\t\t\t\tp[x] = i;\n\t\t\t\tans += solve(t+1);\n\t\t\t\tp[x] = 0;\n\t\t\t\tcan[i] = true;\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}else{\n\t\treturn solve(t+1);\n\t\t\n\t}\n}\n\nint main(){\n\twhile(1){\n\t\tfor(int i = 0 ; i < 11 ; i++) can[i] = true;\n\t\t\n\t\tfor(int i = 0 ; i < 10 ; i++){\n\t\t\tcin >> p[i];\n\t\t\tcan[p[i]] = false;\n\t\t}\n\t\tif(p[0]==-1)break;\n\t\tcout << solve(0) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1160, "score_of_the_acc": -0.0048, "final_rank": 2 }, { "submission_id": "aoj_2058_1110749", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint a[11];\nint used[11];\nint sv=0;\nint solve(int v){\n\tif(v==9){\n\t//\tfor(int i=1;i<11;i++)if(!used[i])return 0;\n\t\treturn 1;\n\t}\n\tint ret=0;\n\tif(a[v]){\n\t\tbool ok=true;\n\t\tint i=a[v];\n\t\tif(v==2)sv=(a[0]+a[1]+a[2])%a[9];\n\t\tif(v%3==2){\n\t\t\tif((a[v-2]+a[v-1]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(v>=6){\n\t\t\tif((a[v-6]+a[v-3]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(v==6){\n\t\t\tif((a[2]+a[4]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(v==8){\n\t\t\tif((a[0]+a[4]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(ok){\n\t\t\treturn solve(v+1);\n\t\t}else return 0;\n\t}\n\tfor(int i=1;i<=10;i++){\n\t\tif(used[i])continue;\n\t\tbool ok=true;\n\t\tif(v==2)sv=(a[0]+a[1]+i)%a[9];\n\t\tif(v%3==2){\n\t\t\tif((a[v-2]+a[v-1]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(v>=6){\n\t\t\tif((a[v-6]+a[v-3]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(v==6){\n\t\t\tif((a[2]+a[4]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(v==8){\n\t\t\tif((a[0]+a[4]+i)%a[9]!=sv)ok=false;\n\t\t}\n\t\tif(!ok)continue;\n\t\tused[i]=1;\n\t\ta[v]=i;\n\t\tret+=solve(v+1);\n\t\tused[i]=0;\n\t\ta[v]=0;\n\t}\n\t//printf(\"%d: %d\\n\",v,ret);\n\treturn ret;\n}\nint main(){\n\twhile(1){\n\t\tfor(int i=0;i<10;i++)scanf(\"%d\",a+i);\n\t\tif(a[0]==-1)break;\n\t\tfor(int i=0;i<11;i++)used[i]=0;\n\t\tfor(int i=0;i<10;i++)if(a[i])used[a[i]]=1;\n\t\tif(a[9]){\n\t\t\tprintf(\"%d\\n\",solve(0));\n\t\t}else{\n\t\t\tint ret=0;\n\t\t\tfor(int i=1;i<=10;i++){\n\t\t\t\tif(used[i])continue;\n\t\t\t\tused[i]=1;\n\t\t\t\ta[9]=i;\n\t\t\t\tret+=solve(0);\n\t\t\t\tused[i]=0;\n\t\t\t}\n\t\t\tprintf(\"%d\\n\",ret);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1028, "score_of_the_acc": -0.0037, "final_rank": 1 }, { "submission_id": "aoj_2058_1097307", "code_snippet": "#include<iostream>\n\nusing namespace std;\n\nint a[10];\n\nbool check(){\n static int c[][3]={\n {0,1,2},\n {3,4,5},\n {6,7,8},\n {0,3,6},\n {1,4,7},\n {2,5,8},\n {0,4,8},\n {2,4,6},\n };\n int r=-1;\n bool f=false;\n for(int j=0;j<8;j++){\n if(a[c[j][0]]==0||a[c[j][1]]==0||a[c[j][2]]==0)continue;\n int s=(a[c[j][0]]+a[c[j][1]]+a[c[j][2]])%a[9];\n if(r==-1){\n r=s;\n }else if(r!=s){\n f=true;\n }\n }\n return !f;\n}\n\nint dfs(int x,int u){\n if(x<0)return check();\n if(a[x])return dfs(x-1,u);\n int s=0;\n for(int i=1;i<=10;i++){\n if(u>>i&1)continue;\n a[x]=i;\n if(check()){\n s+=dfs(x-1,u|1<<i);\n }\n a[x]=0;\n }\n return s;\n}\n\nint main(){\n for(;;){\n int u=0;\n for(auto &e:a){\n cin>>e;\n u|=1<<e;\n }\n if(a[0]==-1)break;\n cout<<dfs(9,u)<<endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 1160, "score_of_the_acc": -0.0309, "final_rank": 3 } ]
aoj_2062_cpp
Problem E: Hide-and-seek Hide-and-seek is a children’s game. Players hide here and there, and one player called it tries to find all the other players. Now you played it and found all the players, so it’s turn to hide from it . Since you have got tired of running around for finding players, you don’t want to play it again. So you are going to hide yourself at the place as far as possible from it . But where is that? Your task is to find the place and calculate the maximum possible distance from it to the place to hide. Input The input contains a number of test cases. The first line of each test case contains a positive integer N ( N ≤ 1000). The following N lines give the map where hide-and-seek is played. The map consists of N corridors . Each line contains four real numbers x 1 , y 1 , x 2 , and y 2 , where ( x 1 , y 1 ) and ( x 2 , y 2 ) indicate the two end points of the corridor. All corridors are straight, and their widths are negligible. After these N lines, there is a line containing two real numbers sx and sy , indicating the position of it . You can hide at an arbitrary place of any corridor, and it always walks along corridors. Numbers in the same line are separated by a single space. It is guaranteed that its starting position ( sx , sy ) is located on some corridor and linked to all corridors directly or indirectly. The end of the input is indicated by a line containing a single zero. Output For each test case, output a line containing the distance along the corridors from ‘it”s starting position to the farthest position. The value may contain an error less than or equal to 0.001. You may print any number of digits below the decimal point. Sample Input 2 0 0 3 3 0 4 3 1 1 1 0 Output for the Sample Input 4.243
[ { "submission_id": "aoj_2062_3114687", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\n#include <map>\n#include <utility>\n#include <queue>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\npair<vector<vector<pair<int, double> > >, VP> arrangementEX(const vector<L> &l, const P &sp){\n vector<VP> cp(l.size());\n VP plist;\n plist.push_back(sp);\n for(int i=0; i<(int)l.size(); i++){\n for(int j=i+1; j<(int)l.size(); j++){\n if(!isParallel(l[i], l[j]) && intersectSS(l[i], l[j])){\n P cpij = crosspointLL(l[i], l[j]);\n cp[i].push_back(cpij);\n cp[j].push_back(cpij);\n plist.push_back(cpij);\n }\n }\n if(intersectSP(l[i], sp)){\n cp[i].push_back(sp);\n }\n cp[i].push_back(l[i][0]);\n cp[i].push_back(l[i][1]);\n plist.push_back(l[i][0]);\n plist.push_back(l[i][1]);\n sort(cp[i].begin(), cp[i].end());\n cp[i].erase(unique(cp[i].begin(), cp[i].end()), cp[i].end());\n for(int j=cp[i].size()-2; j>=0; j--){\n P mid = (cp[i][j] +cp[i][j+1]) /2.0;\n cp[i].push_back(mid);\n plist.push_back(mid);\n }\n sort(cp[i].begin(), cp[i].end());\n cp[i].erase(unique(cp[i].begin(), cp[i].end()), cp[i].end());\n }\n sort(plist.begin(), plist.end());\n plist.erase(unique(plist.begin(), plist.end()), plist.end());\n\n int n = plist.size();\n map<P, int> conv;\n for(int i=0; i<n; i++){\n conv[plist[i]] = i;\n }\n vector<vector<pair<int, double> > > adj(n);\n for(int i=0; i<(int)cp.size(); i++){\n for(int j=0; j<(int)cp[i].size()-1; j++){\n int jidx = conv[cp[i][j]];\n int jp1idx = conv[cp[i][j+1]];\n double dist = abs(cp[i][j] -cp[i][j+1]);\n adj[jidx].emplace_back(jp1idx, dist);\n adj[jp1idx].emplace_back(jidx, dist);\n }\n }\n for(int i=0; i<n; i++){\n sort(adj[i].begin(), adj[i].end());\n adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());\n }\n return make_pair(adj, plist);\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<L> l(n);\n for(int i=0; i<n; i++){\n double xs,ys,xt,yt;\n cin >> xs >> ys >> xt >> yt;\n l[i] = L(P(xs, ys), P(xt, yt));\n }\n double sx,sy;\n cin >> sx >> sy;\n P s(sx, sy);\n\n pair<vector<vector<pair<int, double> > >, VP> ret = arrangementEX(l, s);\n vector<vector<pair<int, double> > > &adj = ret.first;\n VP &plist = ret.second;\n int m = plist.size();\n int sidx = lower_bound(plist.begin(), plist.end(), s) -plist.begin();\n\n priority_queue<pair<double, int> > wait;\n wait.push(make_pair(0, sidx));\n vector<double> mincost(m, INF);\n mincost[sidx] = 0;\n while(!wait.empty()){\n double dist = -wait.top().first;\n int pos = wait.top().second;\n wait.pop();\n if(dist > mincost[pos]) continue;\n\n for(pair<int, double> next: adj[pos]){\n int npos = next.first;\n double ndist = dist +next.second;\n if(ndist +EPS < mincost[npos]){\n mincost[npos] = ndist;\n wait.push(make_pair(-ndist, npos));\n }\n }\n }\n\n cout << fixed << setprecision(10);\n cout << *max_element(mincost.begin(), mincost.end()) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1140, "memory_kb": 33364, "score_of_the_acc": -1.1653, "final_rank": 13 }, { "submission_id": "aoj_2062_2062641", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100000\n#define inf 1<<29\n#define linf 1e18\n#define eps (1e-8)\n#define mod 1000000007\n#define pi M_PI\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<double,int> pdi;\ntypedef pair<int,double> pid;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x-p.x<-eps : y-p.y<-eps);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n \n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n \nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n \ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n \nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n \nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&\n ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n \nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n \nPoint getCrossPointLL(Line a,Line b){\n double A=cross(a.p2-a.p1,b.p2-b.p1);\n double B=cross(a.p2-a.p1,a.p2-b.p1);\n if(abs(A)<eps || abs(B)<eps)return b.p1;\n return b.p1+(b.p2-b.p1)*(B/A);\n}\n\ntypedef vector<vector<int> > Graph;\n \nGraph SegmentArrangement(vector<Segment> v,vector<Point> &ps){\n for(int i=0;i<v.size();i++){\n ps.push_back(v[i].p1);\n ps.push_back(v[i].p2);\n for(int j=i+1;j<v.size();j++){\n if(intersect(v[i],v[j]))ps.push_back(getCrossPointLL(v[i],v[j]));\n }\n }\n sort(ps.begin(),ps.end());\n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n Graph g(ps.size());\n for(int i=0;i<v.size();i++){\n vector<pair<double,int> > list;\n for(int j=0;j<ps.size();j++)\n if(ccw(v[i].p1,v[i].p2,ps[j])==0)\n list.push_back(mp(norm(v[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j<list.size()-1;j++){\n int a=list[j].s,b=list[j+1].s;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n }\n return g;\n}\n\nGraph g;\nvector<Point> vp;\nint n;\nvector<Segment> vs;\nPoint s;\nint start;\ndouble d[MAX];\n\nvoid init(){\n vs.clear();\n vp.clear();\n g.clear();\n}\n\nvoid dijkstra(){\n FOR(i,0,MAX)d[i]=linf;\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n pq.push(mp(0.0,start));\n d[start]=0.0;\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n\n if(d[u.s]-u.f<-eps)continue;\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i];\n double cost=abs(vp[u.s]-vp[next]);\n if(u.f+cost-d[next]<-eps){\n d[next]=u.f+cost;\n pq.push(mp(d[next],next));\n }\n }\n }\n}\n\ndouble solve(){\n double res=0;\n vp.pb(s);\n g=SegmentArrangement(vs,vp);\n FOR(i,0,vp.size())if(s==vp[i])start=i;\n dijkstra();\n FOR(i,0,vp.size()){\n if(d[i]!=linf){\n res=max(res,d[i]);\n FOR(j,0,g[i].size()){\n int next=g[i][j];\n double cost=abs(vp[next]-vp[i]);\n res=max(res,min(d[i],d[next])+(cost-abs(d[i]-d[next]))/2.0);\n }\n }\n }\n return res;\n}\n\nint main()\n{\n while(1){\n cin>>n;\n if(n==0)break;\n init();\n FOR(i,0,n){\n double a,b,c,d;\n cin>>a>>b>>c>>d;\n vs.pb(Segment(Point(a,b),Point(c,d)));\n }\n cin>>s.x>>s.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1360, "memory_kb": 9472, "score_of_the_acc": -0.3386, "final_rank": 4 }, { "submission_id": "aoj_2062_2062638", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100000\n#define inf 1<<29\n#define linf 1e18\n#define eps (1e-8)\n#define mod 1000000007\n#define pi M_PI\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<double,int> pdi;\ntypedef pair<int,double> pid;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x-p.x<-eps : y-p.y<-eps);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n \n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n \nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n \ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n \nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n \nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n \nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&\n ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n \nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n \nPoint getCrossPointLL(Line a,Line b){\n double A=cross(a.p2-a.p1,b.p2-b.p1);\n double B=cross(a.p2-a.p1,a.p2-b.p1);\n if(abs(A)<eps || abs(B)<eps)return b.p1;\n return b.p1+(b.p2-b.p1)*(B/A);\n}\n\ntypedef vector<vector<int> > Graph;\n \nGraph SegmentArrangement(vector<Segment> v,vector<Point> &ps){\n for(int i=0;i<v.size();i++){\n ps.push_back(v[i].p1);\n ps.push_back(v[i].p2);\n for(int j=i+1;j<v.size();j++){\n if(intersect(v[i],v[j]))ps.push_back(getCrossPointLL(v[i],v[j]));\n }\n }\n sort(ps.begin(),ps.end());\n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n Graph g(ps.size());\n for(int i=0;i<v.size();i++){\n vector<pair<double,int> > list;\n for(int j=0;j<ps.size();j++)\n if(ccw(v[i].p1,v[i].p2,ps[j])==0)\n list.push_back(mp(norm(v[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j<list.size()-1;j++){\n int a=list[j].s,b=list[j+1].s;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n }\n return g;\n}\n\nGraph g;\nvector<Point> vp;\nint n;\nvector<Segment> vs;\nPoint s;\nint start;\ndouble d[MAX];\n\nvoid init(){\n vs.clear();\n vp.clear();\n g.clear();\n}\n\nvoid dijkstra(){\n FOR(i,0,MAX)d[i]=linf;\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n pq.push(mp(0.0,start));\n d[start]=0.0;\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n\n if(d[u.s]-u.f<-eps)continue;\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i];\n double cost=abs(vp[u.s]-vp[next]);\n if(u.f+cost-d[next]<-eps){\n d[next]=u.f+cost;\n pq.push(mp(d[next],next));\n }\n }\n }\n}\n\ndouble solve(){\n double res=0;\n vp.pb(s);\n g=SegmentArrangement(vs,vp);\n FOR(i,0,vp.size())if(s==vp[i])start=i;\n dijkstra();\n FOR(i,0,vp.size()){\n if(d[i]!=linf){\n res=max(res,d[i]);\n FOR(j,0,g[i].size()){\n int next=g[i][j];\n double cost=abs(vp[next]-vp[i]);\n res=max(res,min(d[i],d[next])+(cost-abs(d[i]-d[next]))/2.0);\n }\n }\n }\n return res;\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n double a,b,c,d;\n cin>>a>>b>>c>>d;\n vs.pb(Segment(Point(a,b),Point(c,d)));\n }\n cin>>s.x>>s.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1360, "memory_kb": 9452, "score_of_the_acc": -0.3379, "final_rank": 3 }, { "submission_id": "aoj_2062_2062264", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 100000\n#define inf 1<<29\n#define linf 1e18\n#define eps (1e-8)\n#define mod 1000000007\n#define pi M_PI\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<double,int> pdi;\ntypedef pair<int,double> pid;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n \n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n \nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n \ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n \nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n \nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n \nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&\n ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n \nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n \nPoint getCrossPointLL(Line a,Line b){\n double A=cross(a.p2-a.p1,b.p2-b.p1);\n double B=cross(a.p2-a.p1,a.p2-b.p1);\n if(abs(A)<eps || abs(B)<eps)return b.p1;\n return b.p1+(b.p2-b.p1)*(B/A);\n}\n \nbool merge_if_able(Segment &s,Segment t) {\n if(!isParallel(s,t))return false;\n if(ccw(s.p1,t.p1,s.p2)==1 ||\n ccw(s.p1,t.p1,s.p2)==-1)return false;\n if(ccw(s.p1,s.p2,t.p1)==-2 ||\n ccw(t.p1,t.p2,s.p1)==-2)return false;\n s=Segment(min(s.p1,t.p1),max(s.p2,t.p2));\n return true;\n}\n \nvoid merge(vector<Segment>& v) {\n for(int i=0;i<v.size();i++){\n if(v[i].p2<v[i].p1)swap(v[i].p2,v[i].p1);\n }\n for(int i=0;i<v.size();i++)\n for(int j=i+1;j<v.size();j++)\n if(merge_if_able(v[i],v[j]))\n v[j--]=v.back(),v.pop_back();\n}\n \ntypedef vector<vector<int> > Graph;\n \nGraph SegmentArrangement(vector<Segment> v,vector<Point> &ps){\n for(int i=0;i<v.size();i++){\n ps.push_back(v[i].p1);\n ps.push_back(v[i].p2);\n for(int j=i+1;j<v.size();j++){\n if(intersect(v[i],v[j]))ps.push_back(getCrossPointLL(v[i],v[j]));\n }\n }\n //sort(ps.begin(),ps.end());\n //ps.erase(unique(ps.begin(),ps.end()),ps.end());\n Graph g(ps.size());\n for(int i=0;i<v.size();i++){\n vector<pair<double,int> > list;\n for(int j=0;j<ps.size();j++)\n if(ccw(v[i].p1,v[i].p2,ps[j])==0)\n list.push_back(mp(norm(v[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j<list.size()-1;j++){\n int a=list[j].s,b=list[j+1].s;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n }\n return g;\n}\n\nGraph g;\nvector<Point> vp;\nint n;\nvector<Segment> vs;\nPoint s;\nint start;\ndouble d[MAX];\n\nvoid init(){\n vs.clear();\n vp.clear();\n g.clear();\n}\n\nvoid dijkstra(){\n FOR(i,0,MAX)d[i]=linf;\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n pq.push(mp(0.0,start));\n d[start]=0.0;\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n\n if(d[u.s]-u.f<-eps)continue;\n\n FOR(i,0,g[u.s].size()){\n int next=g[u.s][i];\n double cost=abs(vp[u.s]-vp[next]);\n if(u.f+cost-d[next]<-eps){\n d[next]=u.f+cost;\n pq.push(mp(d[next],next));\n }\n }\n }\n}\n\ndouble solve(){\n double res=0;\n merge(vs);\n vp.pb(s);\n g=SegmentArrangement(vs,vp);\n FOR(i,0,vp.size())if(s==vp[i])start=i;\n dijkstra();\n FOR(i,0,vp.size()){\n if(d[i]!=linf){\n res=max(res,d[i]);\n FOR(j,0,g[i].size()){\n int next=g[i][j];\n double cost=abs(vp[next]-vp[i]);\n res=max(res,min(d[i],d[next])+(cost-abs(d[i]-d[next]))/2.0);\n }\n }\n }\n return res;\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n FOR(i,0,n){\n double a,b,c,d;\n cin>>a>>b>>c>>d;\n vs.pb(Segment(Point(a,b),Point(c,d)));\n }\n cin>>s.x>>s.y;\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1320, "memory_kb": 9516, "score_of_the_acc": -0.332, "final_rank": 2 }, { "submission_id": "aoj_2062_1925170", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <algorithm>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <set>\n#include <functional>\n#include <ctime>\n#include <numeric>\n\nusing namespace std;\n\n#define fst first\n#define snd second\n#define all(c) ((c).begin()), ((c).end())\n#define TEST(x,a) { auto y=(x); if (sign(y-a)) { cout << \"line \" << __LINE__ << #x << \" = \" << y << \" != \" << a << endl; exit(-1); } }\ndouble urand() { return rand() / (1.0 + RAND_MAX); }\n\nconst double PI = acos(-1.0);\n\n// implementation note: use EPS only this function\n// usage note: check sign(x) < 0, sign(x) > 0, or sign(x) == 0\n// notice: should be normalize to O(1)\nconst double EPS = 1e-8;\nint sign(double x) {\n if (x < -EPS) return -1;\n if (x > +EPS) return +1;\n return 0;\n}\nstruct point {\n typedef double T;\n T x, y; \n point &operator+=(point p) { x += p.x; y += p.y; return *this; }\n point &operator-=(point p) { x -= p.x; y -= p.y; return *this; }\n point &operator*=(T a) { x *= a; y *= a; return *this; }\n point &operator/=(T a) { return *this *= (1.0/a); }\n point operator-() const { return {-x, -y}; }\n bool operator==(point p) const { return !sign(x-p.x) && !sign(y-p.y); }\n bool operator<(point p) const { return x!=p.x ? x<p.x : y<p.y; } // for sort\n};\npoint operator+(point p, point q) { return p += q; }\npoint operator-(point p, point q) { return p -= q; }\npoint operator*(point::T a, point p) { return p *= a; }\npoint operator*(point p, point::T a) { return p *= a; }\npoint operator/(point p, point::T a) { return p /= a; }\npoint::T dot(point p, point q) { return p.x*q.x+p.y*q.y; }\npoint::T cross(point p, point q) { return p.x*q.y-p.y*q.x; } // left turn > 0\npoint::T norm2(point p) { return dot(p,p); }\npoint orth(point p) { return {-p.y, p.x}; }\npoint::T norm(point p) { return sqrt(dot(p,p)); }\n\nstruct segment { point p, q; };\n\nvector<point> intersect(segment s, segment t) {\n auto a = cross(s.q - s.p, t.q - t.p);\n auto b = cross(t.p - s.p, t.q - t.p); \n auto c = cross(s.q - s.p, s.p - t.p);\n if (a < 0) { a = -a; b = -b; c = -c; }\n if (sign(b) < 0 || sign(a-b) < 0 ||\n sign(c) < 0 || sign(a-c) < 0) return {}; // disjoint\n if (sign(a) != 0) return {s.p + b/a*(s.q - s.p)}; // properly crossing\n vector<point> ps; // same line\n auto insert_if_possible = [&](point p) {\n for (auto q: ps) if (sign(dot(p-q, p-q)) == 0) return;\n ps.push_back(p);\n };\n if (sign(dot(s.p-t.p, s.q-t.p)) <= 0) insert_if_possible(t.p);\n if (sign(dot(s.p-t.q, s.q-t.q)) <= 0) insert_if_possible(t.q);\n if (sign(dot(t.p-s.p, t.q-s.p)) <= 0) insert_if_possible(s.p);\n if (sign(dot(t.p-s.q, t.q-s.q)) <= 0) insert_if_possible(s.q);\n return ps;\n}\n\nstruct arrangement {\n struct edge {\n int src, dst;\n point::T weight;\n };\n int n;\n vector<point> ps; // ps[id[p]] = p, id[ps[k]] = k\n map<point,int> id; \n vector<vector<edge>> adj;\n\n arrangement(vector<segment> ss) : n(0) {\n vector<vector<pair<point::T, int>>> group(ss.size());\n auto append = [&](int i, point p) {\n if (!id.count(p)) { id[p] = n++; ps.push_back(p); }\n group[i].push_back({norm(ss[i].p - p), id[p]});\n };\n for (int i = 0; i < ss.size(); ++i) {\n append(i, ss[i].p);\n append(i, ss[i].q);\n for (int j = 0; j < i; ++j) {\n auto qs = intersect(ss[i], ss[j]);\n if (qs.size() == 1) {\n append(i, qs[0]);\n append(j, qs[0]);\n }\n }\n }\n adj.resize(n);\n for (auto &vs: group) {\n sort(all(vs));\n for (int i = 0; i+1 < vs.size(); ++i) {\n auto u = vs[i].snd, v = vs[i+1].snd;\n if (u == v) continue;\n auto len = vs[i+1].fst - vs[i].fst;\n adj[u].push_back({u, v, len});\n adj[v].push_back({v, u, len});\n }\n }\n }\n void shortest_path(point sp) {\n int s = id[sp];\n vector<point::T> dist(n, 1.0/0.0);\n vector<int> prev(n, -1);\n typedef pair<point::T, int> node;\n priority_queue<node, vector<node>, greater<node>> Q;\n Q.push(node(dist[s] = 0, s));\n while (!Q.empty()) {\n node z = Q.top(); Q.pop();\n if (dist[z.snd] <= z.fst) {\n for (auto e: adj[z.snd]) {\n if (dist[e.dst] > dist[e.src] + e.weight) {\n dist[e.dst] = dist[e.src] + e.weight;\n prev[e.dst] = e.src;\n Q.push({dist[e.dst], e.dst});\n }\n }\n }\n }\n point::T ans = 0;\n for (int u = 0; u < n; ++u) {\n for (edge e: adj[u]) {\n point::T s = (dist[e.dst] - dist[e.src] + e.weight)/2;\n ans = max(ans, dist[e.src] + s);\n }\n }\n printf(\"%.12lf\\n\", ans);\n }\n};\n\n\nint main() {\n for (int m; ~scanf(\"%d\", &m) && m != 0; ) {\n vector<segment> ss;\n for (int i = 0; i < m; ++i) {\n segment s;\n scanf(\"%lf %lf %lf %lf\", &s.p.x, &s.p.y, &s.q.x, &s.q.y);\n ss.push_back(s);\n }\n point p; \n scanf(\"%lf %lf\", &p.x, &p.y);\n ss.push_back({p, p});\n arrangement arr(ss);\n arr.shortest_path(p);\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 11888, "score_of_the_acc": -0.2161, "final_rank": 1 }, { "submission_id": "aoj_2062_1094812", "code_snippet": "#include <cassert>// c\n#include <iostream>// io\n#include <iomanip>\n#include <fstream>\n#include <sstream>\n#include <vector>// container\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <algorithm>// other\n#include <utility>\n#include <complex>\n#include <numeric>\n#include <functional>\n#include <random>\n#include <regex>\nusing namespace std;\ntypedef long long ll;\n\n#define ALL(c) (c).begin(),(c).end()\n#define FOR(i,l,r) for(int i=(int)l;i<(int)r;++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORr(i,l,r) for(int i=(int)r-1;i>=(int)l;--i)\n#define REPr(i,n) FORr(i,0,n)\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())\n\n// must template\ntypedef long double D;\nconst D INF = 1e20,EPS = 1e-10;\n\nint sig(D a,D b=0){return a<b-EPS?-1:a>b+EPS?1:0;}\ntemplate<typename T> bool eq(const T& a,const T& b){return sig(abs(a-b))==0;}\n\n\ntypedef D Cost;Cost CINF=1e20;\nstruct Edge{\n\tint from,to;Cost cost;\n\tEdge(int from,int to,Cost cost) : from(from),to(to),cost(cost) {};\n\tbool operator<(Edge r) const{return cost<r.cost;}\n\tbool operator>(Edge r) const{return cost>r.cost;}\n};\ntypedef vector<vector<Edge> > Graph;\ntypedef complex<D> P;\n#define X real()\n#define Y imag()\n\nbool compX (const P& a,const P& b){return !eq(a.X,b.X)?sig(a.X,b.X)<0:sig(a.Y,b.Y)<0;}\nistream& operator >> (istream& is,P& p){D x,y;is >> x >> y;p=P(x,y);return is;}\n\nnamespace std{\n\tbool operator < (const P& a,const P& b){return compX(a,b);}\n \tbool operator == (const P& a,const P& b){return eq(a,b);}\n};\n\n// a×b\nD cross(const P& a,const P& b){return imag(conj(a)*b);}\n// a・b\nD dot(const P& a,const P& b) {return real(conj(a)*b);}\n\nint ccw(const P& a,P b,P c){\n\tb -= a; c -= a;\n\tif (sig(cross(b,c)) > 0) return +1; // counter clockwise\n\tif (sig(cross(b,c)) < 0) return -1; // clockwise\n\tif (sig(dot(b,c)) < 0) return +2; // c--a--b on line\n\tif (sig(norm(b),norm(c))<0) return -2; // a--b--c on line\n\treturn 0; //a--c--b on line or c==a or c==b\n}\n\n//直線・線分\nstruct L : public vector<P> {\n\tP vec() const {return this->at(1)-this->at(0);}\n\tL(const P &a, const P &b){push_back(a); push_back(b);}\n\tL(){push_back(P(0,0));push_back(P(0,0));}\n};\nistream& operator >> (istream& is,L& l){is >> l[0] >> l[1];return is;}\n\nbool isIntersectLL(const L &l, const L &m) {\n \treturn sig(cross(l.vec(), m.vec()))!=0 || // non-parallel\n\tsig(cross(l.vec(), m[0]-l[0])) == 0; // same line\n}\nbool isIntersectLS(const L &l, const L &s) {\n\treturn sig(cross(l.vec(), s[0]-l[0])* // s[0] is left of l\n\t\tcross(l.vec(), s[1]-l[0]))<=0; // s[1] is right of l\n}\nbool isIntersectLP(const L &l, const P &p) {return sig(cross(l[1]-p, l[0]-p))==0;}\n\n// verified by ACAC003 B\n// http://judge.u-aizu.ac.jp/onlinejudge/creview.jsp?rid=899178&cid=ACAC003\nbool isIntersectSS(const L &s, const L &t) {\n\treturn ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n\t\tccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool isIntersectSP(const L &s, const P &p) {\n\treturn sig(abs(s[0]-p)+abs(s[1]-p),abs(s[1]-s[0])) <=0; // triangle inequality\n}\nP crosspoint(const L &l, const L &m) {\n\tD A = cross(l.vec(), m.vec());\n\tD B = cross(l.vec(), l[1] - m[0]);\n\tif (sig(A)==0 && sig(B)==0) return m[0]; // same line\n\tif (sig(A)==0) assert(false); //交点を持たない.\n\treturn m[0] + B / A * m.vec();\n}\n\nbool parallel(const L &l, const L &m) { return eq<P>(cross(l.vec(), m.vec()), 0); }\nbool sameline(const L &l, const L &m) { return parallel(l, m) && eq<P>(cross(l.vec(), m[1]-l[0]), 0); }\n\n// オーバーラップした線分を1つにマージする.\n// O(n^2)\nvector<L> merge_segments(vector<L>& ss){\n\tREP(i,ss.size())if(ss[i][1]<ss[i][0])swap(ss[i][0],ss[i][1]);\n\tREP(i,ss.size())FOR(j,i+1,ss.size()){\n\t\tif(sameline(ss[i],ss[j]) && !ccw(ss[i][0],ss[i][1],ss[j][0])){\n\t\t\tss[i] = L(min(ss[i][0],ss[j][0]), max(ss[i][1],ss[j][1]));\n\t\t\tss[j--]=ss.back();ss.pop_back();\n\t\t}\n\t}\n\treturn ss;\n}\n\n// 線分アレンジメント\n// ps[i]=頂点iに対応するP\n// O(n^2logn)\n// 制約 : 線分のオーバーラップがない.\nGraph segmentArrangement(vector<L> ss, vector<P> &ps) {\n\tvector<vector<D>> vs(ss.size());\n\tREP(i,ss.size()){vs[i].push_back(0);vs[i].push_back(abs(ss[i][1]-ss[i][0]));}\n\tREP(i,ss.size())REP(j,ps.size())if(isIntersectSP(ss[i], ps[j]))\n\t\tvs[i].push_back(abs(ps[j]-ss[i][0]));\n\t\n\tREP(i,ss.size())FOR(j,i+1,ss.size()){\n\t\tL& a=ss[i],b=ss[j];\n\t\tif(isIntersectSS(a,b)){\n\t\t\tP p = crosspoint(a,b);\n\t\t\tvs[i].push_back(abs(p-a[0]));vs[j].push_back(abs(p-b[0]));\t\t\t\t\n\t\t}\n\t}\n\tREP(i,vs.size()){sort(ALL(vs[i]));vs[i].erase(unique(ALL(vs[i]),eq<D>),vs[i].end());}\n\tREP(i,vs.size())REP(j,vs[i].size())ps.push_back(ss[i][0]+vs[i][j]*ss[i].vec()/abs(ss[i].vec()));\n\tsort(ALL(ps));UNIQUE(ps);\n\n\tGraph g(ps.size());\n\tREP(i,vs.size()){\n\t\tvector<D>& cut=vs[i];\n\t\tREP(j,cut.size()-1){\n\t\t\tP a = ss[i][0] + vs[i][j]*ss[i].vec()/abs(ss[i].vec()),b = ss[i][0] + vs[i][j+1]*ss[i].vec()/abs(ss[i].vec());\n\t\t\tint f=distance(ps.begin(),lower_bound(ALL(ps),a)), t=distance(ps.begin(),lower_bound(ALL(ps),b));\n\t\t\tg[f].push_back(Edge(f,t,abs(a-b)));g[t].push_back(Edge(t,f,abs(a-b)));\n\t\t}\n\t}\n\treturn g;\n}\n\nstruct Task{\n int prev,pos;Cost cost;\n Task(int prev,int pos,Cost cost)\n :prev(prev),pos(pos),cost(cost){};\n bool operator>(const Task& r) const{ return cost > r.cost;}\n};\n\n//verified by codoforces 144D http://codeforces.com/contest/144/submission/4976825\n// // 負の辺がない\n// // O(E*logV)\nvector<Cost> dijkstra(const Graph& g,const int s,vector<int>& prev){\n const int V=g.size();\n vector<Cost> d(V,CINF);d[s]=0;\n fill(ALL(prev), -2);\n \n priority_queue<Task,vector<Task>,greater<Task> > que;que.push(Task(-1,s,0));// [ ,e,,f, ] <=> e.cost < e.cost\n vector<bool> visited(V);\n while(!que.empty()){\n Task task=que.top();que.pop();\n if(visited[task.pos])continue;\n visited[task.pos]=true;\n prev[task.pos]=task.prev;\n EACH(e,g[task.pos])if(d[e->to]>d[e->from]+e->cost){\n d[e->to]=d[e->from]+e->cost;\n que.push(Task(e->from,e->to,d[e->to]));\n }\n }\n return d;\n}\nvector<Cost> dijkstra(const Graph& g,const int s){\n vector<int> prev(g.size());return dijkstra(g,s,prev);\n}\n\nint main(){\n\t// ifstream cin( \"in\" );\n\t// ofstream cout( \"out\" );\n\tcout << fixed <<setprecision(20);\n\tcerr << fixed <<setprecision(20);\n\tcin.tie(0); ios::sync_with_stdio(false);\n\n\twhile(true){\n\t\tint N;cin >> N;if(N==0)break;\n\t\tvector<L> ls(N);REP(i,N) cin >> ls[i];\n\t\tP p;cin >> p;\n\t\tvector<P> ps;ps.push_back(p);\n\t\tGraph g=segmentArrangement(ls,ps);\n\t\t\n\t\tint si=-1;REP(i,ps.size())if(p==ps[i])si=i;\n\t\tvector<Cost> ds = dijkstra(g,si);\n\n\t\tD Mv=*max_element(ALL(ds));\n\t\tREP(i,g.size())EACH(e,g[i]){\n\t\t\tD a=abs(ds[e->from]-ds[e->to]);\n\t\t\tD d=abs(ps[e->from]-ps[e->to]);\n\t\t\tMv=max(Mv,min(ds[e->from],ds[e->to])+(a+d)/2);\n\t\t}\n\t\tcout << Mv <<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1130, "memory_kb": 13256, "score_of_the_acc": -0.4292, "final_rank": 5 }, { "submission_id": "aoj_2062_1094764", "code_snippet": "#include <cassert>// c\n#include <iostream>// io\n#include <iomanip>\n#include <fstream>\n#include <sstream>\n#include <vector>// container\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <algorithm>// other\n#include <utility>\n#include <complex>\n#include <numeric>\n#include <functional>\n#include <random>\n#include <regex>\nusing namespace std;\ntypedef long long ll;\n\n#define ALL(c) (c).begin(),(c).end()\n#define FOR(i,l,r) for(int i=(int)l;i<(int)r;++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORr(i,l,r) for(int i=(int)r-1;i>=(int)l;--i)\n#define REPr(i,n) FORr(i,0,n)\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())\n\n// must template\ntypedef long double D;\nconst D INF = 1e20,EPS = 1e-10;\n\nint sig(D a,D b=0){return a<b-EPS?-1:a>b+EPS?1:0;}\nbool eq(D a,D b){ return sig(abs(a-b))==0;}\n// D norm(D a){ return a*a;}\n\ntypedef D Cost;Cost CINF=1e20;\nstruct Edge{\n\tint from,to;Cost cost;\n\tEdge(int from,int to,Cost cost) : from(from),to(to),cost(cost) {};\n\tbool operator<(Edge r) const{return cost<r.cost;}\n\tbool operator>(Edge r) const{return cost>r.cost;}\n};\ntypedef vector<vector<Edge> > Graph;\ntypedef complex<D> P;\n#define X real()\n#define Y imag()\n\nbool eq(const P& a,const P& b){ return sig(abs(a-b))==0;}\nbool compX (const P& a,const P& b){return sig(a.X,b.X)!=0 ? a.X < b.X : a.Y < b.Y;}\n\nistream& operator >> (istream& is,P& p){D x,y;is >> x >> y;p=P(x,y);return is;}\n\nnamespace std{\n\tbool operator < (const P& a,const P& b){return compX(a,b);}\n \tbool operator == (const P& a,const P& b){return eq(a,b);}\n};\n\n// a×b\nD cross(const P& a,const P& b){return imag(conj(a)*b);}\n// a・b\nD dot(const P& a,const P& b) {return real(conj(a)*b);}\n\nint ccw(const P& a,P b,P c){\n\tb -= a; c -= a;\n\tif (sig(cross(b,c)) > 0) return +1; // counter clockwise\n\tif (sig(cross(b,c)) < 0) return -1; // clockwise\n\tif (sig(dot(b,c)) < 0) return +2; // c--a--b on line\n\tif (sig(norm(b),norm(c))<0) return -2; // a--b--c on line\n\treturn 0; //a--c--b on line or c==a or c==b\n}\n\n//直線・線分\nstruct L : public vector<P> {\n\tP vec() const {return this->at(1)-this->at(0);}\n\tL(const P &a, const P &b){push_back(a); push_back(b);}\n\tL(){push_back(P(0,0));push_back(P(0,0));}\n};\nistream& operator >> (istream& is,L& l){is >> l[0] >> l[1];return is;}\n\nbool isIntersectLL(const L &l, const L &m) {\n \treturn sig(cross(l.vec(), m.vec()))!=0 || // non-parallel\n\tsig(cross(l.vec(), m[0]-l[0])) == 0; // same line\n}\nbool isIntersectLS(const L &l, const L &s) {\n\treturn sig(cross(l.vec(), s[0]-l[0])* // s[0] is left of l\n\t\tcross(l.vec(), s[1]-l[0]))<=0; // s[1] is right of l\n}\nbool isIntersectLP(const L &l, const P &p) {return sig(cross(l[1]-p, l[0]-p))==0;}\n\n// verified by ACAC003 B\n// http://judge.u-aizu.ac.jp/onlinejudge/creview.jsp?rid=899178&cid=ACAC003\nbool isIntersectSS(const L &s, const L &t) {\n\treturn ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n\t\tccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool isIntersectSP(const L &s, const P &p) {\n\treturn sig(abs(s[0]-p)+abs(s[1]-p),abs(s[1]-s[0])) <=0; // triangle inequality\n}\nP crosspoint(const L &l, const L &m) {\n\tD A = cross(l.vec(), m.vec());\n\tD B = cross(l.vec(), l[1] - m[0]);\n\tif (sig(A)==0 && sig(B)==0) return m[0]; // same line\n\tif (sig(A)==0) assert(false); //交点を持たない.\n\treturn m[0] + B / A * m.vec();\n}\n\nbool parallel(const L &l, const L &m) { return eq(cross(l.vec(), m.vec()), 0); }\nbool sameline(const L &l, const L &m) { return parallel(l, m) && eq(cross(l.vec(), m[1]-l[0]), 0); }\n\n// 線分アレンジメント\n// ps[i]=頂点iに対応するP\n// O(n^2logn)\nGraph segmentArrangement(vector<L> ss, vector<P> &ps) {\n\t// merge\n\t// REP(i,ss.size())if(ss[i][1]<ss[i][0])swap(ss[i][0],ss[i][1]);\n\t// REP(i,ss.size())FOR(j,i+1,ss.size()){\n\t// \tif(sameline(ss[i],ss[j]) && !ccw(ss[i][0],ss[i][1],ss[j][0])){\n\t// \t\tss[i] = L(min(ss[i][0],ss[j][0]), max(ss[i][1],ss[j][1]));\n\t// \t\t// swap(ss[j],ss.back());\n\t// \t\tss[j--] = ss.back();\n\t// \t\tss.pop_back();\n\t// \t}\n\t// }\n\tvector<vector<D>> vs(ss.size());\n\tREP(i,ss.size()){\n\t\tvs[i].push_back(0);vs[i].push_back(abs(ss[i][1]-ss[i][0]));\n\t\tREP(j,ps.size())if (isIntersectSP(ss[i], ps[j]))\n\t\t\tvs[i].push_back(abs(ps[j]-ss[i][0]));\n\t}\n\tREP(i,ss.size())FOR(j,i+1,ss.size()){\n\t\tL a=ss[i],b=ss[j];\n\t\tif(isIntersectSS(a,b)){\n\t\t\tP p = crosspoint(a,b);\n\t\t\tvs[i].push_back(abs(p-ss[i][0]));vs[j].push_back(abs(p-ss[j][0]));\t\t\t\t\n\t\t}\n\t}\n\tREP(i,vs.size()){sort(ALL(vs[i]));vs[i].erase(unique(ALL(vs[i]),[](D a,D b)->bool{return abs(a-b)<EPS;}),vs[i].end());}\n\tREP(i,vs.size())REP(j,vs[i].size())ps.push_back(ss[i][0]+vs[i][j]*ss[i].vec()/abs(ss[i].vec()));\n\tsort(ALL(ps));UNIQUE(ps);\n\t// ps.erase(unique(ALL(ps),[](const P& a,const P& b)->bool{return eq(a,b);}),ps.end());\n\t// cerr << ps.size()<<endl;\n\t// REP(i,ps.size())cerr << ps[i] <<endl;\n\t// cerr << endl;\n\tGraph g(ps.size());\n\tREP(i,vs.size()){\n\t\tvector<D>& cut=vs[i];\n\t\tREP(j,cut.size()-1){\n\t\t\tP a = ss[i][0] + vs[i][j]*ss[i].vec()/abs(ss[i].vec()),b = ss[i][0] + vs[i][j+1]*ss[i].vec()/abs(ss[i].vec());\n\t\t\tint f=distance(ps.begin(), lower_bound(ALL(ps),a,[](const P& a,const P& b)->bool{return !eq(a.X,b.X)?a.X<b.X:a.Y+EPS<b.Y;})),\n\t\t\t\tt=distance(ps.begin(), lower_bound(ALL(ps),b,[](const P& a,const P& b)->bool{return !eq(a.X,b.X)?a.X<b.X:a.Y+EPS<b.Y;}));\n\t\t\t// cerr << f <<\" \" << t <<endl;\n\t\t\t// cerr << a <<\" \" << b <<endl;\n\t\t\t// cerr << compX(ps[9],b)<<endl;\n\t\t\t// cerr << compX(ps[10],b)<<endl;\n\n\t\t\tg[f].push_back(Edge(f,t,abs(a-b)));\n\t\t\tg[t].push_back(Edge(t,f,abs(a-b)));\n\t\t}\n\t}\n\treturn g;\n}\n\nstruct Task{\n int prev,pos;Cost cost;\n Task(int prev,int pos,Cost cost)\n :prev(prev),pos(pos),cost(cost){};\n bool operator>(const Task& r) const{ return cost > r.cost;}\n};\n\n//verified by codoforces 144D http://codeforces.com/contest/144/submission/4976825\n// // 負の辺がない\n// // O(E*logV)\nvector<Cost> dijkstra(const Graph& g,const int s,vector<int>& prev){\n const int V=g.size();\n vector<Cost> d(V,CINF);d[s]=0;\n fill(ALL(prev), -2);\n \n priority_queue<Task,vector<Task>,greater<Task> > que;que.push(Task(-1,s,0));// [ ,e,,f, ] <=> e.cost < e.cost\n vector<bool> visited(V);\n while(!que.empty()){\n Task task=que.top();que.pop();\n if(visited[task.pos])continue;\n visited[task.pos]=true;\n prev[task.pos]=task.prev;\n EACH(e,g[task.pos])if(d[e->to]>d[e->from]+e->cost){\n d[e->to]=d[e->from]+e->cost;\n que.push(Task(e->from,e->to,d[e->to]));\n }\n }\n return d;\n}\nvector<Cost> dijkstra(const Graph& g,const int s){\n vector<int> prev(g.size());return dijkstra(g,s,prev);\n}\n\nint main(){\n\t// ifstream cin( \"in\" );\n\t// ofstream cout( \"out\" );\n\tcout << fixed <<setprecision(20);\n\tcerr << fixed <<setprecision(20);\n\tcin.tie(0); ios::sync_with_stdio(false);\n\n\twhile(true){\n\t\tint N;cin >> N;if(N==0)break;\n\t\tvector<L> ls(N);REP(i,N) cin >> ls[i];\n\t\tP p;cin >> p;\n\t\tvector<P> ps;ps.push_back(p);\n\t\tGraph g=segmentArrangement(ls,ps);\n\t\t\n\t\tint si=-1;REP(i,ps.size())if(p==ps[i])si=i;\n\t\tvector<Cost> ds = dijkstra(g,si);\n\n\t\t// cout << ps.size() <<endl;\n\t\t// REP(i,ps.size())cout << ps[i]<<endl;\n\t\t//check\n\t\tD Mv=*max_element(ALL(ds));\n\t\tREP(i,g.size())EACH(e,g[i]){\n\t\t\tif (eq(abs(ds[e->from] - ds[e->to]), abs(ps[e->from] - ps[e->to]))) continue;\n \n\t\t\tD a=abs(ds[e->from]-ds[e->to]);\n\t\t\tD d=abs(ps[e->from]-ps[e->to]);\n\t\t\tMv=max(Mv,min(ds[e->from],ds[e->to])+(a+d)/2);\n\t\t}\n\t\tcout << Mv <<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1280, "memory_kb": 13784, "score_of_the_acc": -0.4795, "final_rank": 7 }, { "submission_id": "aoj_2062_1046786", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n Point(double x = 0,double y = 0): x(x),y(y){}\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\ndouble abs(Point a){ return sqrt(norm(a)); }\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) { return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; }\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\n\nPoint reflection(Line l,Point p) { return p + (projection(l, p) - p) * 2; }\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //同じセグメントかもよ\n return vec[1];\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\nstruct Edge {\n int from,to;\n double cost;\n Edge(int from=0,int to=0,double cost=0):from(from),to(to),cost(cost){}\n bool operator < (const Edge& a)const { return !equals(cost,a.cost) && cost < a.cost; }\n};\n\nPoint sp;\n\nvector<vector<Edge> > segmentArrangement(vector<Segment> &vs,vector<Point> &ps) {\n ps.push_back(sp);\n rep(i,vs.size()) ps.push_back( (vs[i].p1+vs[i].p2) / 2.0 );\n rep(i,vs.size()) ps.push_back(vs[i].p1), ps.push_back(vs[i].p2);\n rep(i,vs.size()) REP(j,i+1,vs.size()) if(intersectSS(vs[i],vs[j])) ps.push_back(Point(crosspoint(vs[i],vs[j])));\n sort(ps.begin(),ps.end()); \n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n\n vector<vector<Edge> > ret(ps.size());\n\n for(int i=0;i<vs.size();i++){\n vector<pair<double,int> > list;\n rep(j,ps.size()) if(intersectSP(vs[i],ps[j]))list.push_back(pair<double,int>(norm(vs[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j+1<list.size();++j) {\n int from = list[j].second, to = list[j+1].second;\n double cost = abs(ps[from]-ps[to]);\n ret[from].push_back(Edge(from,to,cost));\n ret[to].push_back(Edge(to,from,cost));\n }\n } \n return ret;\n}\n\nint E;\nvector<Segment> segs;\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n\n// --作成中-- \n\nPoint vir;\nbool comp_dist(const Point &a,const Point &b){ return LT(abs(vir-a),abs(vir-b)); }\n\nconst int MAX_N = 2000;\nint uf[MAX_N];\nvoid init(int N=MAX_N) { rep(i,N) uf[i] = i; }\nint find(int x) { \n if( x == uf[x] ) return x;\n return uf[x] = find(uf[x]);\n}\nvoid unit(int x,int y){\n x = find(x), y = find(y);\n if( x != y ) uf[x] = y; \n}\n\nvoid segmentMerge(vector<Segment> &vec,vector<Segment> &ret){\n init(vec.size());\n rep(i,vec.size()) {\n rep(j,vec.size()) {\n if( equals(cross(vec[i].p1-vec[i].p2,vec[j].p1-vec[j].p2),0.0) && intersectSS(vec[i],vec[j]) ) unit(i,j);\n }\n }\n\n ret.clear();\n vector<int> color;\n rep(i,vec.size()) color.push_back(find(i));\n sort(color.begin(),color.end());\n color.erase(unique(color.begin(),color.end()),color.end());\n rep(i,color.size()) {\n vector<Point> vp;\n rep(j,vec.size()) if( color[i] == find(j) ) vp.push_back(vec[j].p1), vp.push_back(vec[j].p2);\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n assert( vp.size() >= 2 ); // 線分じゃなくて点\n vir = vp[0];\n sort(vp.begin(),vp.end(),comp_dist);\n ret.push_back(Segment(vp.front(),vp.back()));\n }\n}\n\n// --作成中--\n\nconst double DINF = 1e20;\n\nstruct Data {\n int cur;\n double weight;\n bool operator < ( const Data &data ) const { return !equals(weight,data.weight) && weight > data.weight; }\n};\n\nvoid dijkstra(vector<vector<Edge> > &G,vector<Point> &ps){\n int sindex = -1;\n rep(i,(int)ps.size()) if( ps[i] == sp ) { sindex = i; break; }\n\n int V = ps.size();\n priority_queue<Data> Q;\n Q.push((Data){sindex,0});\n vector<double> mindist(V,DINF);\n mindist[sindex] = 0;\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n if( LT(mindist[data.cur],data.weight) ) continue;\n rep(i,G[data.cur].size()){\n int to = G[data.cur][i].to;\n if( LT(data.weight+G[data.cur][i].cost,mindist[to]) ){\n mindist[to] = data.weight+G[data.cur][i].cost;\n Q.push((Data){to,mindist[to]});\n }\n }\n }\n\n double maxi = 0;\n rep(i,V) {\n assert( !equals(DINF,mindist[i]) );\n maxi = max(maxi,mindist[i]);\n }\n printf(\"%.10lf\\n\",maxi);\n}\n\nvoid compute(){\n vector<Segment> buf;\n segmentMerge(segs,buf); \n vector<Point> ps;\n vector<vector<Edge> > G = segmentArrangement(buf,ps);\n dijkstra(G,ps);\n}\n\nint main(){\n while( cin >> E, E ){\n segs.resize(E);\n rep(i,E) cin >> segs[i].p1.x >> segs[i].p1.y >> segs[i].p2.x >> segs[i].p2.y;\n cin >> sp.x >> sp.y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5170, "memory_kb": 5996, "score_of_the_acc": -0.999, "final_rank": 11 }, { "submission_id": "aoj_2062_1046785", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev から cur へ行って next へ行く際の角度を求める\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //同じセグメントかもよ\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\nstruct Edge {\n int from,to;\n double cost;\n Edge(int from=0,int to=0,double cost=0):from(from),to(to),cost(cost){}\n bool operator < (const Edge& a)const { return !equals(cost,a.cost) && cost < a.cost; }\n};\n\nPoint sp;\n\nvector<vector<Edge> > segmentArrangement(vector<Segment> &vs,vector<Point> &ps) {\n ps.push_back(sp);\n\n rep(i,vs.size()) ps.push_back( (vs[i].p1+vs[i].p2) / 2.0 );\n\n rep(i,vs.size()) ps.push_back(vs[i].p1), ps.push_back(vs[i].p2);\n rep(i,vs.size()) REP(j,i+1,vs.size()) if(intersectSS(vs[i],vs[j])) ps.push_back(Point(crosspoint(vs[i],vs[j])));\n\n sort(ps.begin(),ps.end()); \n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n\n vector<vector<Edge> > ret(ps.size());\n\n for(int i=0;i<vs.size();i++){\n vector<pair<double,int> > list;\n rep(j,ps.size()) if(intersectSP(vs[i],ps[j]))list.push_back(pair<double,int>(norm(vs[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j+1<list.size();++j) {\n int from = list[j].second, to = list[j+1].second;\n double cost = abs(ps[from]-ps[to]);\n ret[from].push_back(Edge(from,to,cost));\n ret[to].push_back(Edge(to,from,cost));\n }\n } \n return ret;\n}\n\nint E;\nvector<Segment> segs;\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n\n// --作成中-- \n\nPoint vir;\nbool comp_dist(const Point &a,const Point &b){ return LT(abs(vir-a),abs(vir-b)); }\n\nconst int MAX_N = 2000;\nint uf[MAX_N];\nvoid init(int N=MAX_N) { rep(i,N) uf[i] = i; }\nint find(int x) { \n if( x == uf[x] ) return x;\n return uf[x] = find(uf[x]);\n}\nvoid unit(int x,int y){\n x = find(x), y = find(y);\n if( x != y ) uf[x] = y; \n}\n\nvoid segmentMerge(vector<Segment> &vec,vector<Segment> &ret){\n init(vec.size());\n rep(i,vec.size()) {\n rep(j,vec.size()) {\n if( equals(cross(vec[i].p1-vec[i].p2,vec[j].p1-vec[j].p2),0.0) && intersectSS(vec[i],vec[j]) ) unit(i,j);\n }\n }\n\n ret.clear();\n vector<int> color;\n rep(i,vec.size()) color.push_back(find(i));\n sort(color.begin(),color.end());\n color.erase(unique(color.begin(),color.end()),color.end());\n rep(i,color.size()) {\n vector<Point> vp;\n rep(j,vec.size()) if( color[i] == find(j) ) vp.push_back(vec[j].p1), vp.push_back(vec[j].p2);\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n assert( vp.size() >= 2 ); // 線分じゃなくて点\n vir = vp[0];\n sort(vp.begin(),vp.end(),comp_dist);\n ret.push_back(Segment(vp.front(),vp.back()));\n }\n}\n\n// --作成中--\n\nconst double DINF = 1e20;\n\nstruct Data {\n int cur;\n double weight;\n bool operator < ( const Data &data ) const { return !equals(weight,data.weight) && weight > data.weight; }\n};\n\nvoid dijkstra(vector<vector<Edge> > &G,vector<Point> &ps){\n int sindex = -1;\n rep(i,(int)ps.size()) if( ps[i] == sp ) { sindex = i; break; }\n assert( sindex != -1 );\n\n int V = ps.size();\n priority_queue<Data> Q;\n Q.push((Data){sindex,0});\n vector<double> mindist(V,DINF);\n mindist[sindex] = 0;\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n \n if( LT(mindist[data.cur],data.weight) ) continue;\n\n rep(i,G[data.cur].size()){\n int to = G[data.cur][i].to;\n if( LT(data.weight+G[data.cur][i].cost,mindist[to]) ){\n mindist[to] = data.weight+G[data.cur][i].cost;\n Q.push((Data){to,mindist[to]});\n }\n }\n\n }\n\n double maxi = 0;\n rep(i,V) {\n assert( !equals(DINF,mindist[i]) );\n maxi = max(maxi,mindist[i]);\n }\n\n printf(\"%.10lf\\n\",maxi);\n\n}\n\nvoid compute(){\n vector<Segment> buf = segs;\n //segmentMerge(segs,buf); \n\n vector<Point> ps;\n vector<vector<Edge> > G = segmentArrangement(buf,ps);\n\n dijkstra(G,ps);\n\n}\n\nint main(){\n while( cin >> E, E ){\n segs.resize(E);\n rep(i,E) cin >> segs[i].p1.x >> segs[i].p1.y >> segs[i].p2.x >> segs[i].p2.y;\n cin >> sp.x >> sp.y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5130, "memory_kb": 5968, "score_of_the_acc": -0.9897, "final_rank": 10 }, { "submission_id": "aoj_2062_1046783", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev から cur へ行って next へ行く際の角度を求める\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //同じセグメントかもよ\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\nstruct Edge {\n int from,to;\n double cost;\n Edge(int from=0,int to=0,double cost=0):from(from),to(to),cost(cost){}\n bool operator < (const Edge& a)const { return !equals(cost,a.cost) && cost < a.cost; }\n};\n\nPoint sp;\n\nvector<vector<Edge> > segmentArrangement(vector<Segment> &vs,vector<Point> &ps) {\n ps.push_back(sp);\n\n rep(i,vs.size()) ps.push_back( (vs[i].p1+vs[i].p2) / 2.0 );\n\n rep(i,vs.size()) ps.push_back(vs[i].p1), ps.push_back(vs[i].p2);\n rep(i,vs.size()) REP(j,i+1,vs.size()) if(intersectSS(vs[i],vs[j])) ps.push_back(Point(crosspoint(vs[i],vs[j])));\n\n sort(ps.begin(),ps.end()); \n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n\n vector<vector<Edge> > ret(ps.size());\n\n for(int i=0;i<vs.size();i++){\n vector<pair<double,int> > list;\n rep(j,ps.size()) if(intersectSP(vs[i],ps[j]))list.push_back(pair<double,int>(norm(vs[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j+1<list.size();++j) {\n int from = list[j].second, to = list[j+1].second;\n double cost = abs(ps[from]-ps[to]);\n ret[from].push_back(Edge(from,to,cost));\n ret[to].push_back(Edge(to,from,cost));\n }\n } \n return ret;\n}\n\nint E;\nvector<Segment> segs;\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n\n// --作成中-- \n\nPoint vir;\nbool comp_dist(const Point &a,const Point &b){ return LT(abs(vir-a),abs(vir-b)); }\n\nconst int MAX_N = 2000;\nint uf[MAX_N];\nvoid init(int N=MAX_N) { rep(i,N) uf[i] = i; }\nint find(int x) { \n if( x == uf[x] ) return x;\n return uf[x] = find(uf[x]);\n}\nvoid unit(int x,int y){\n x = find(x), y = find(y);\n if( x != y ) uf[x] = y; \n}\n\nvoid segmentMerge(vector<Segment> &vec,vector<Segment> &ret){\n init(vec.size());\n rep(i,vec.size()) {\n rep(j,vec.size()) {\n if( equals(cross(vec[i].p1-vec[i].p2,vec[j].p1-vec[j].p2),0.0) && intersectSS(vec[i],vec[j]) ) unit(i,j);\n }\n }\n\n ret.clear();\n vector<int> color;\n rep(i,vec.size()) color.push_back(find(i));\n sort(color.begin(),color.end());\n color.erase(unique(color.begin(),color.end()),color.end());\n rep(i,color.size()) {\n vector<Point> vp;\n rep(j,vec.size()) if( color[i] == find(j) ) vp.push_back(vec[j].p1), vp.push_back(vec[j].p2);\n sort(vp.begin(),vp.end());\n vp.erase(unique(vp.begin(),vp.end()),vp.end());\n assert( vp.size() >= 2 ); // 線分じゃなくて点\n vir = vp[0];\n sort(vp.begin(),vp.end(),comp_dist);\n ret.push_back(Segment(vp.front(),vp.back()));\n }\n}\n\n// --作成中--\n\nconst double DINF = 1e20;\n\nstruct Data {\n int cur;\n double weight;\n bool operator < ( const Data &data ) const { return !equals(weight,data.weight) && weight > data.weight; }\n};\n\nvoid dijkstra(vector<vector<Edge> > &G,vector<Point> &ps){\n int sindex = -1;\n rep(i,(int)ps.size()) if( ps[i] == sp ) { sindex = i; break; }\n assert( sindex != -1 );\n\n int V = ps.size();\n priority_queue<Data> Q;\n Q.push((Data){sindex,0});\n vector<double> mindist(V,DINF);\n mindist[sindex] = 0;\n while( !Q.empty() ){\n Data data = Q.top(); Q.pop();\n \n if( LT(mindist[data.cur],data.weight) ) continue;\n\n rep(i,G[data.cur].size()){\n int to = G[data.cur][i].to;\n if( LT(data.weight+G[data.cur][i].cost,mindist[to]) ){\n mindist[to] = data.weight+G[data.cur][i].cost;\n Q.push((Data){to,mindist[to]});\n }\n }\n\n }\n\n double maxi = 0;\n rep(i,V) {\n assert( !equals(DINF,mindist[i]) );\n maxi = max(maxi,mindist[i]);\n }\n\n printf(\"%.10lf\\n\",maxi);\n\n}\n\nvoid compute(){\n vector<Segment> buf;\n segmentMerge(segs,buf); \n\n //rep(i,buf.size()) cout << i << \"-th : \" << buf[i] << endl;\n\n vector<Point> ps;\n vector<vector<Edge> > G = segmentArrangement(buf,ps);\n\n dijkstra(G,ps);\n\n}\n\nint main(){\n while( cin >> E, E ){\n segs.resize(E);\n rep(i,E) cin >> segs[i].p1.x >> segs[i].p1.y >> segs[i].p2.x >> segs[i].p2.y;\n cin >> sp.x >> sp.y;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5180, "memory_kb": 6000, "score_of_the_acc": -1.0012, "final_rank": 12 }, { "submission_id": "aoj_2062_713277", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n// #define double long double\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\ntypedef double Weight;\nstruct Edge {\n int src, dst;\n Weight weight;\n Edge(int src, int dst, Weight weight) :\n src(src), dst(dst), weight(weight) { }\n};\n\nbool operator < (const Edge &e, const Edge &f) {\n return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!\n e.src != f.src ? e.src < f.src : e.dst < f.dst;\n}\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\n\n// 道程がいらない場合\nvector<Weight> dijkstra(const Graph &g, int s) {\n vector<Weight> dist(g.size(), INF);\n dist[s] = 0;\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (dist[e.dst] < e.weight) continue;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\n\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() {resize(2);}\n};\nostream &operator<<(ostream &os, const L &a) {\n os << a[0] << \" -> \" << a[1];\n return os;\n}\n\ntypedef vector<P> G;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L &l, const P &p) {\n return p + P(2,0) * (projection(l, p) - p);\n}\n\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\n\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool intersectSS2(const L &s, const L &t) {\n if (intersectSP(s,t[0]) || intersectSP(s,t[1]) ||\n intersectSP(t,s[0]) || intersectSP(t,s[1])) return 0;\n return intersectSS(s,t);\n}\nbool intersectHS(const L &h, const L &s) {\n if (intersectLP(h,s[0]) || intersectLP(h,s[1]) ||\n intersectLP(s,h[0]) || intersectLP(s,h[1])) return 0;\n if (intersectSS(h,s)) return 1;\n if (!intersectLS(h,s)) return 0;\n return (ccw(s[0],s[1],h[0]) == 1) ^ (cross(s[1]-s[0], h[1]-h[0]) > 0);\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) {\n // sameline\n if (intersectSP(l,m[0]) && intersectSP(l,m[1])) return P(0.5)*(m[0]+m[1]);\n if (intersectSP(m,l[0]) && intersectSP(m,l[1])) return P(0.5)*(l[0]+l[1]);\n REP(i,2) REP(j,2)\n if (intersectSP(l,m[i]) && intersectSP(m,l[j])) return P(0.5)*(m[i]+l[j]);\n }\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nbool EQ(double a, double b) {\n return abs(a-b) < EPS;\n}\nbool EQP(const P &a, const P &b) {\n return norm(a-b) < EPS*EPS;\n}\n\n#define index_of(ps, p) \\\n distance(ps.begin(), lower_bound(ps.begin(), ps.end(), p))\nGraph arrangement(const vector<L>& segs, vector<P>& pt) {\n vector< vector<double> > V(segs.size());\n for (int i = 0; i < segs.size(); ++i) {\n pt.push_back( segs[i][0] );\n pt.push_back( segs[i][1] );\n V[i].push_back(0);\n V[i].push_back(1);\n for (int j = i+1; j < segs.size(); ++j) {\n L a = segs[i], b = segs[j];\n if (intersectSS(a, b)) {\n double D = cross(b[1]-b[0], a[1]-a[0]);\n if (abs(D) < EPS) continue;\n double t = cross(b[1]-b[0], b[0]-a[0]) / D;\n double s = cross(a[0]-b[0], a[1]-a[0]) / D;\n V[i].push_back(t);\n V[j].push_back(s);\n pt.push_back( a[0]+(a[1]-a[0])*t );\n }\n }\n }\n sort(pt.begin(), pt.end());\n pt.erase(unique(pt.begin(), pt.end(), EQP), pt.end());\n\n Graph g(pt.size());\n for (int i = 0; i < V.size(); ++i) {\n vector<double>& cut = V[i];\n sort(cut.begin(), cut.end());\n cut.erase(unique(cut.begin(), cut.end(), EQ), cut.end());\n for (int j = 0; j+1 < cut.size(); ++j) {\n P a = segs[i][0] + (segs[i][1] - segs[i][0]) * cut[j];\n P b = segs[i][0] + (segs[i][1] - segs[i][0]) * cut[j+1];\n int src = index_of(pt, a), dst = index_of(pt, b);\n // cout << g.size() << \" \" << src << \" \" << dst << \" \" << abs(a-b) << endl;\n g[src].push_back( Edge(src, dst, abs(a-b)) );\n g[dst].push_back( Edge(dst, src, abs(a-b)) );\n }\n }\n return g;\n}\n\nGraph segmentArrangement(const vector<L> &ss, vector<P> &ps) {\n int n = ss.size();\n REP(i,n) {\n ps.push_back( ss[i][0] );\n ps.push_back( ss[i][1] );\n for (int j = i+1; j < ss.size(); ++j)\n if (intersectSS(ss[i], ss[j]))\n ps.push_back( crosspoint(ss[i], ss[j]) );\n }\n sort(ALL(ps)); ps.erase(unique(ALL(ps)), ps.end());\n\n Graph g(ps.size());\n for (int i = 0; i < ss.size(); ++i) {\n int pre = -1;\n for (int j = 0; j < ps.size(); ++j)\n if (intersectSP(ss[i], ps[j])) {\n if (pre != -1) {\n double d = abs(ps[pre]-ps[j]);\n g[pre].push_back(Edge(pre,j,d));\n g[j].push_back(Edge(j,pre,d));\n }\n pre = j;\n }\n }\n return g;\n}\n\nvector<Edge> graph[300000];\nint pid[1004][1004];\n\nGraph segmentArrangement2(const vector<L> &ss, vector<P> &ps) {\n memset(pid,-1,sizeof(pid));\n REP(i,300000) graph[i].clear();\n int n = ss.size();\n REP(i,n) {\n int beg = ps.size();\n ps.push_back(ss[i][0]);\n ps.push_back(ss[i][1]);\n REP(j,n) {\n if (i == j) continue;\n if (intersectSS(ss[i],ss[j])) {\n int b = ps.size();\n if (i>j) {\n int a = pid[j][i];\n // cout << ps[a] << \" \"<< crosspoint(ss[i],ss[j]) << endl;\n graph[a].push_back(Edge(a,b,0));\n graph[b].push_back(Edge(b,a,0));\n } else {\n pid[i][j] = b;\n }\n ps.push_back(crosspoint(ss[i],ss[j]));\n }\n }\n vector<pair<P,int> > v(ps.size()-beg);\n for (int i=beg; i<ps.size(); ++i)\n v[i-beg] = make_pair(ps[i],i);\n sort(ALL(v));\n REP(i,v.size()-1) {\n int a = v[i].second, b = v[i+1].second;\n double c = abs(v[i].first-v[i+1].first);\n graph[a].push_back(Edge(a,b,c));\n graph[b].push_back(Edge(b,a,c));\n }\n }\n Graph g(ps.size());\n REP(i,ps.size()) {\n g[i] = graph[i];\n }\n return g;\n}\n\n\nint main() {\n int n;\n while(cin >> n, n) {\n vector<L> segs;\n REP(i,n) {\n P a, b;\n cin >> a.real() >> a.imag();\n cin >> b.real() >> b.imag();\n segs.push_back(L(a,b));\n }\n P s;\n cin >> s.real() >> s.imag();\n REP(i,n) {\n if (intersectSP(segs[i],s)) {\n segs.push_back(L(segs[i][0],s));\n segs.push_back(L(s,segs[i][1]));\n break;\n }\n }\n vector<P> pv;\n // Graph g = arrangement(segs,pv);\n Graph g = segmentArrangement2(segs,pv);\n int sid = -1;\n REP(i,pv.size()) {\n if (abs(pv[i]-s) < EPS) sid = i;\n }\n assert(sid != -1);\n // REP(i,g.size()) {\n // cout << i << endl;\n // FOR(it, g[i]) {\n // cout << it->dst << \" \" << it->weight << endl;\n // }\n // }\n // cout << sid << endl;\n vector<double> dis = dijkstra(g,sid);\n double ans = 0;\n REP(i,g.size()) {\n FOR(it, g[i]) {\n int a = it->src;\n int b = it->dst;\n double x = (dis[b]-dis[a]+it->weight) / 2;\n chmax(ans, dis[a] + x);\n }\n }\n cout << setprecision(10) << fixed << ans << endl;\n // printf(\"%.10Lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 25688, "score_of_the_acc": -0.7942, "final_rank": 9 }, { "submission_id": "aoj_2062_666097", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define double long double\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\ntypedef double Weight;\nstruct Edge {\n int src, dst;\n Weight weight;\n Edge(int src, int dst, Weight weight) :\n src(src), dst(dst), weight(weight) { }\n};\n\nbool operator < (const Edge &e, const Edge &f) {\n return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!\n e.src != f.src ? e.src < f.src : e.dst < f.dst;\n}\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\n\n// テゥツ?禿ァツィツ凝」ツ?古」ツ??」ツつ嘉」ツ?ェテ」ツ??・ツ?エテ・ツ青?\nvector<Weight> dijkstra(const Graph &g, int s) {\n vector<Weight> dist(g.size(), INF);\n dist[s] = 0;\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (dist[e.dst] < e.weight) continue;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\n\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() {resize(2);}\n};\nostream &operator<<(ostream &os, const L &a) {\n os << a[0] << \" -> \" << a[1];\n return os;\n}\n\ntypedef vector<P> G;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L &l, const P &p) {\n return p + P(2,0) * (projection(l, p) - p);\n}\n\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\n\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool intersectSS2(const L &s, const L &t) {\n if (intersectSP(s,t[0]) || intersectSP(s,t[1]) ||\n intersectSP(t,s[0]) || intersectSP(t,s[1])) return 0;\n return intersectSS(s,t);\n}\nbool intersectHS(const L &h, const L &s) {\n if (intersectLP(h,s[0]) || intersectLP(h,s[1]) ||\n intersectLP(s,h[0]) || intersectLP(s,h[1])) return 0;\n if (intersectSS(h,s)) return 1;\n if (!intersectLS(h,s)) return 0;\n return (ccw(s[0],s[1],h[0]) == 1) ^ (cross(s[1]-s[0], h[1]-h[0]) > 0);\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nbool EQ(double a, double b) {\n return abs(a-b) < EPS;\n}\nbool EQP(const P &a, const P &b) {\n return norm(a-b) < EPS*EPS;\n}\n\n#define index_of(ps, p) \\\n distance(ps.begin(), lower_bound(ps.begin(), ps.end(), p))\nGraph arrangement(const vector<L>& segs, vector<P>& pt) {\n vector< vector<double> > V(segs.size());\n for (int i = 0; i < segs.size(); ++i) {\n pt.push_back( segs[i][0] );\n pt.push_back( segs[i][1] );\n V[i].push_back(0);\n V[i].push_back(1);\n for (int j = i+1; j < segs.size(); ++j) {\n L a = segs[i], b = segs[j];\n if (intersectSS(a, b)) {\n double D = cross(b[1]-b[0], a[1]-a[0]);\n if (abs(D) < EPS) continue;\n double t = cross(b[1]-b[0], b[0]-a[0]) / D;\n double s = cross(a[0]-b[0], a[1]-a[0]) / D;\n V[i].push_back(t);\n V[j].push_back(s);\n pt.push_back( a[0]+(a[1]-a[0])*t );\n }\n }\n }\n sort(pt.begin(), pt.end());\n pt.erase(unique(pt.begin(), pt.end(), EQP), pt.end());\n\n Graph g(pt.size());\n for (int i = 0; i < V.size(); ++i) {\n vector<double>& cut = V[i];\n sort(cut.begin(), cut.end());\n cut.erase(unique(cut.begin(), cut.end(), EQ), cut.end());\n for (int j = 0; j+1 < cut.size(); ++j) {\n P a = segs[i][0] + (segs[i][1] - segs[i][0]) * cut[j];\n P b = segs[i][0] + (segs[i][1] - segs[i][0]) * cut[j+1];\n int src = index_of(pt, a), dst = index_of(pt, b);\n // cout << g.size() << \" \" << src << \" \" << dst << \" \" << abs(a-b) << endl;\n g[src].push_back( Edge(src, dst, abs(a-b)) );\n g[dst].push_back( Edge(dst, src, abs(a-b)) );\n }\n }\n return g;\n}\n\nint main() {\n int n;\n while(cin >> n, n) {\n vector<L> segs;\n REP(i,n) {\n P a, b;\n cin >> a.real() >> a.imag();\n cin >> b.real() >> b.imag();\n segs.push_back(L(a,b));\n }\n P s;\n cin >> s.real() >> s.imag();\n REP(i,n) {\n if (intersectSP(segs[i],s)) {\n segs.push_back(L(segs[i][0],s));\n segs.push_back(L(s,segs[i][1]));\n }\n }\n vector<P> pv;\n Graph g = arrangement(segs,pv);\n int sid = -1;\n REP(i,pv.size()) {\n if (abs(pv[i]-s) < EPS) sid = i;\n }\n assert(sid != -1);\n // REP(i,g.size()) {\n // cout << i << endl;\n // FOR(it, g[i]) {\n // cout << it->dst << \" \" << it->weight << endl;\n // }\n // }\n // cout << sid << endl;\n vector<double> dis = dijkstra(g,sid);\n double ans = 0;\n REP(i,g.size()) {\n FOR(it, g[i]) {\n int a = it->src;\n int b = it->dst;\n double x = (dis[b]-dis[a]+it->weight) / 2;\n chmax(ans, dis[a] + x);\n }\n }\n cout << setprecision(10) << fixed << ans << endl;\n // printf(\"%.10Lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 2000, "memory_kb": 11556, "score_of_the_acc": -0.5469, "final_rank": 8 }, { "submission_id": "aoj_2062_249871", "code_snippet": "#include<iostream>\n#include<stdio.h>\n#include<complex>\n#include<cfloat>\n#include<cassert>\n#include<cmath>\n#include<vector>\n#include<queue>\n#include<algorithm>\n\nusing namespace std;\n\n\n\n#define EPS (1e-8)\n#define equals(a, b) (abs((a) - (b)) < EPS )\n\nclass Point{\n public:\n double x, y;\n \n Point ( double x = 0, double y = 0): x(x), y(y){}\n \n Point operator + ( Point p ){\n\treturn Point(x + p.x, y + p.y);\n }\n\n Point operator - ( Point p ){\n\treturn Point(x - p.x, y - p.y);\n }\n\n Point operator * ( double a ){\n\treturn Point(a*x, a*y);\n }\n\n double absolute() { return sqrt(norm());}\n double norm() { return x*x + y*y; }\n\n bool operator < ( const Point &p ) const{\n\treturn x != p.x ? x < p.x : y < p.y;\n }\n\n bool operator == ( const Point &p ) const {\n\treturn abs(x-p.x) < EPS && abs(y-p.y) < EPS;\n }\n};\n\ntypedef Point Vector;\n\nclass Segment{\n public:\n Point source, target;\n Segment(Point s = Point(), Point t = Point()): source(s), target(t){}\n};\n\ndouble norm( Vector a ){\n return a.x*a.x + a.y*a.y;\n}\n\ndouble absolute( Vector a ){\n return sqrt(norm(a));\n}\n\ndouble getDistance(Vector a, Vector b){\n return absolute(a - b); \n}\n\n// dot product\ndouble dot(Vector a, Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\n// cross product\ndouble cross(Vector a, Vector b){\n return a.x*b.y - a.y*b.x;\n}\n\nPoint project( Segment s, Point p ){\n Vector base = s.target - s.source;\n double t = dot(p - s.source, base)/norm(base);\n return s.source + (base)*t;\n}\n\nPoint reflect( Segment s, Point p ){\n return p + (project(s, p) - p)*2.0;\n}\n\n// verified by uoa2062\nbool isOnSegment( Point a, Point b, Point c){\n if ( a == c || b == c ) return true;\n return (absolute(a-c) + absolute(c-b) < absolute(a-b) + EPS );\n}\n\nbool isOrthogonal( Vector a, Vector b ){\n return equals( dot(a, b), 0.0 );\n}\n\nbool isOrthogonal( Point a1, Point a2, Point b1, Point b2 ){\n return isOrthogonal( a1 - a2, b1 - b2 );\n}\n\nbool isOrthogonal( Segment s1, Segment s2 ){\n return equals( dot(s1.target - s1.source, s2.target - s2.source), 0.0 );\n}\n\nbool isParallel( Vector a, Vector b ){\n return equals( cross(a, b), 0.0 );\n}\n\nbool isParallel( Point a1, Point a2, Point b1, Point b2){\n return isParallel( a1 - a2, b1 - b2 );\n}\n\nbool isParallel( Segment s1, Segment s2 ){\n return equals( cross(s1.target - s1.source, s2.target - s2.source), 0.0 );\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw( Point p0, Point p1, Point p2 ){\n Vector a = p1 - p0;\n Vector b = p2 - p0;\n if ( cross(a, b) > EPS ) return COUNTER_CLOCKWISE;\n if ( cross(a, b) < -EPS ) return CLOCKWISE;\n if ( dot(a, b) < -EPS ) return ONLINE_BACK;\n if ( norm(a) < norm(b) ) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\n// intersect Segment p1-p2 and Segment p3-p4 ?\nbool isIntersect(Point p1, Point p2, Point p3, Point p4){\n return ( ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n\t ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0 );\n}\n\n// intersect Segment s1 and Segment s2 ?\n// verified by 920, 833, 866, uoa2062\nbool isIntersect(Segment s1, Segment s2){\n return isIntersect(s1.source, s1.target, s2.source, s2.target);\n}\n\n// verified by 920, 833, uoa2062\nPoint getCrossPoint(Segment s1, Segment s2){\n assert( isIntersect(s1, s2) );\n Vector base = s2.target - s2.source;\n double d1 = absolute(cross(base, s1.source - s2.source));\n double d2 = absolute(cross(base, s1.target - s2.source));\n double t = d1/(d1 + d2);\n return s1.source + (s1.target - s1.source)*t;\n}\n\n\n#define MAX 2000\n#define NMAX 80000\n#define LIMIT FLT_MAX\n\nclass Edge{\n public:\n int target;\n double dist;\n Edge(){}\n Edge(int target, double dist): target(target), dist(double(dist)){}\n};\n\nclass Graph{\n public:\n vector<Edge> adjList[NMAX];\n int start;\n\n Graph(){\n\tfor ( int i = 0; i < NMAX; i++ ) adjList[i].clear();\n }\n \n void connect( int i, int j, double d ){\n\tadjList[i].push_back(Edge(j, d));\n\tadjList[j].push_back(Edge(i, d));\n }\n};\n\nclass QNode{\n public:\n int index;\n double cost;\n QNode(){}\n QNode( int index, double cost ): index(index), cost(cost){}\n \n bool operator < ( const QNode &q ) const { return cost > q.cost; }\n};\n\nclass Node{\n public:\n Point p;\n int id;\n Node(){}\n Node( Point p, int id): p(p), id(id){}\n bool operator < ( const Node &n) const {\n\treturn p < n.p;\n }\n};\n\nint nnode;\nPoint SP[NMAX];\n\nint getIndex( Point p ){\n SP[nnode++] = p;\n return nnode-1;\n}\n\nvoid makeGraph(Graph &g, Segment S[MAX], int N, Point s ){\n Segment base, target;\n Node L[NMAX];\n int beg, p = 0;\n bool startSet = false;\n nnode = 0;\n for ( int b = 0; b < N; b++ ){\n\tbase = S[b];\n\tbeg = p;\n\n\tif ( !startSet && isOnSegment(base.source, base.target, s) ){\n\t L[p++] = Node(s, getIndex(s));\n\t startSet = true;\n\t}\n\n\tL[p++] = Node(base.source, getIndex(base.source));\n\tL[p++] = Node(base.target, getIndex(base.target));\n\n\tfor ( int t = 0; t < N; t++ ){\n\t if ( t == b ) continue;\n\t target = S[t];\n\t if ( !isIntersect(base, target) ) continue;\n\t Point cp = getCrossPoint(base, target);\n\t L[p++] = Node(cp, getIndex(cp));\n\t}\n\n\tsort(L + beg, L + p );\n\n\tfor ( int i = beg; i < p-1; i++ ){\n\t g.connect(L[i].id, L[i+1].id, getDistance(L[i].p, L[i+1].p));\n\t}\n }\n\n sort( L, L + p );\n\n for ( int i = 0; i < p-1; i++ ){\n //\tif ( eq(L[i].p, L[i+1].p) ) \n if ( L[i].p == L[i+1].p ) \n\t g.connect( L[i].id, L[i+1].id, 0.0);\n //\tif ( eq(L[i].p, s) ) g.start = L[i].id;\n if ( L[i].p == s ) g.start = L[i].id;\n }\n\n}\n\ndouble dijkstra(Graph g){\n bool visited[NMAX];\n double D[NMAX];\n priority_queue<QNode> PQ;\n \n for ( int i = 0; i < nnode; i++ ) {\n\tvisited[i] = false;\n\tD[i] = LIMIT;\n }\n\n for ( int i = 0; i < g.adjList[g.start].size(); i++ ){\n\tEdge v = g.adjList[g.start][i];\n\tPQ.push(QNode(v.target, v.dist) );\n\tD[v.target] = v.dist;\n }\n\n visited[g.start] = true;\n D[g.start] = 0.0;\n\n QNode u;\n\n while( !PQ.empty() ){\n\tu = PQ.top(); PQ.pop();\n\tif ( visited[u.index] ) continue;\n\tvisited[u.index] = true;\n\tfor ( int i = 0; i < g.adjList[u.index].size(); i++ ){\n\t Edge v = g.adjList[u.index][i];\n\t if ( visited[v.target] ) continue;\n\t if ( D[u.index] + v.dist < D[v.target] ){\n\t\tD[v.target] = D[u.index] + v.dist;\n\t\tPQ.push(QNode(v.target, D[v.target]));\n\t }\n\t}\n }\n\n double mdist = 0.0;\n\n for ( int i = 0; i < nnode; i++ ){\n\tmdist = max(mdist, D[i] );\n\tfor ( int k = 0; k < g.adjList[i].size(); k++ ){\n\t int j = g.adjList[i][k].target;\n\t double d = getDistance(SP[i], SP[j]);\n\t mdist = max(mdist, (double)(min(D[i], D[j]) + (d - abs(D[i]-D[j]))/2.0));\n\t}\n }\n return mdist;\n}\n\nint main(){\n int N;\n Segment S[MAX];\n\n while( cin >> N && N ){\n\tPoint p1, p2;\n\tfor ( int i = 0; i < N; i++ ){\n\t cin >> p1.x; cin >> p1.y;\n\t cin >> p2.x; cin >> p2.y;\n\t S[i] = Segment(p1, p2);\n\t}\n\tcin >> p1.x; cin >> p1.y;\n\tGraph g;\n\tmakeGraph(g, S, N, p1);\n\tprintf(\"%.5lf\\n\", dijkstra(g));\n }\n\n return 0;\n}\n\n/**\ndaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaadaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\n\n*/", "accuracy": 1, "time_ms": 1570, "memory_kb": 12000, "score_of_the_acc": -0.4743, "final_rank": 6 } ]
aoj_2063_cpp
Problem F: TV Watching You are addicted to watching TV, and you watch so many TV programs every day. You have been in trouble recently: the airtimes of your favorite TV programs overlap. Fortunately, you have both a TV and a video recorder at your home. You can therefore watch a program on air while another program (on a different channel) is recorded to a video at the same time. However, it is not easy to decide which programs should be watched on air or recorded to a video. As you are a talented computer programmer, you have decided to write a program to find the way of watching TV programs which gives you the greatest possible satisfaction. Your program (for a computer) will be given TV listing of a day along with your score for each TV program. Each score represents how much you will be satisfied if you watch the corresponding TV program on air or with a video. Your program should compute the maximum possible sum of the scores of the TV programs that you can watch. Input The input consists of several scenarios. The first line of each scenario contains an integer N (1 ≤ N ≤ 1000) that represents the number of programs. Each of the following N lines contains program information in the format below: T T b T e R 1 R 2 T is the title of the program and is composed by up to 32 alphabetical letters. T b and T e specify the start time and the end time of broadcasting, respectively. R 1 and R 2 indicate the score when you watch the program on air and when you have the program recorded, respectively. All times given in the input have the form of “hh:mm” and range from 00:00 to 23:59. You may assume that no program is broadcast across the twelve midnight. The end of the input is indicated by a line containing only a single zero. This is not part of scenarios. Output For each scenario, output the maximum possible score in a line. Sample Input 4 OmoikkiriTV 12:00 13:00 5 1 WaratteIitomo 12:00 13:00 10 2 WatarusekennhaOnibakari 20:00 21:00 10 3 SuzumiyaharuhiNoYuuutsu 23:00 23:30 100 40 5 a 0:00 1:00 100 100 b 0:00 1:00 101 101 c 0:00 1:00 102 102 d 0:00 1:00 103 103 e 0:00 1:00 104 104 0 Output for the Sample Input 121 207
[ { "submission_id": "aoj_2063_10312968", "code_snippet": "// AOJ #2063 TV Watching\n// 2025.3.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Prog { int s, e, r1, r2; };\n\nint toMin(const string &t) {\n int hh = stoi(t.substr(0, t.find(':')));\n int mm = stoi(t.substr(t.find(':')+1));\n return hh * 60 + mm;\n}\n\nint n;\nvector<Prog> p;\n\nvector<vector<int>> dp;\n\nint recDP(int i, int j) {\n int &res = dp[i][j];\n if(res != -1) return res;\n int base = max(i, j) + 1;\n res = 0;\n int lo = base, hi = n+1;\n while(lo < hi) {\n int mid = (lo+hi)/2;\n if(p[mid].s < p[i].e) lo = mid+1;\n else hi = mid;\n }\n int startTV = lo;\n for (int k = max(startTV, base); k <= n; k++) {\n if(p[k].s >= p[i].e) res = max(res, p[k].r1 + recDP(k, j));\n }\n lo = base, hi = n+1;\n while(lo < hi) {\n int mid = (lo+hi)/2;\n if(p[mid].s < p[j].e) lo = mid+1;\n else hi = mid;\n }\n int startR = lo;\n for (int k = max(startR, base); k <= n; k++) {\n if(p[k].s >= p[j].e) res = max(res, p[k].r2 + recDP(i, k));\n }\n return res;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while(true) {\n cin >> n;\n if(n==0) break;\n\n p.assign(n+1, Prog());\n p[0] = {0, 0, 0, 0};\n string t, tb, te;\n int r1, r2;\n for (int i = 1; i <= n; i++) {\n cin >> t >> tb >> te >> r1 >> r2;\n p[i].s = toMin(tb);\n p[i].e = toMin(te);\n p[i].r1 = r1;\n p[i].r2 = r2;\n }\n sort(p.begin()+1, p.end(), [](const Prog &a, const Prog &b) {\n return a.s < b.s || (a.s == b.s && a.e < b.e);\n });\n dp.assign(n+1, vector<int>(n+1, -1));\n cout << recDP(0, 0) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2570, "memory_kb": 6836, "score_of_the_acc": -1.2879, "final_rank": 14 }, { "submission_id": "aoj_2063_3103772", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 1440\n\nstruct Info{\n\tInfo(int arg_end_time,int arg_live_value,int arg_record_value){\n\t\tend_time = arg_end_time;\n\t\tlive_value = arg_live_value;\n\t\trecord_value = arg_record_value;\n\t}\n\tint end_time,live_value,record_value;\n};\n\nint N;\nint dp[NUM][NUM];\nvector<Info> G[NUM];\n\nint get_time(char buf[6]){\n\tint hour = 10*(buf[0]-'0')+buf[1]-'0';\n\tint minute = 10*(buf[3]-'0')+buf[4]-'0';\n\treturn 60*hour+minute;\n}\n\nint recursive(int live_time,int record_time){\n\n\tif(dp[live_time][record_time] != -1){\n\t\treturn dp[live_time][record_time];\n\t}\n\n\tint ret = 0;\n\n\tif(live_time < record_time){\n\n\t\tret = max(ret,recursive(live_time+1,record_time));\n\n\t\tfor(int i = 0; i < G[live_time].size(); i++){\n\n\t\t\tret = max(ret,recursive(G[live_time][i].end_time,record_time)+G[live_time][i].live_value);\n\t\t}\n\n\t\treturn dp[live_time][record_time] = ret;\n\n\t}else if(live_time > record_time){\n\n\t\tret = max(ret,recursive(live_time,record_time+1));\n\n\t\tfor(int i = 0; i < G[record_time].size(); i++){\n\t\t\tret = max(ret,recursive(live_time,G[record_time][i].end_time)+G[record_time][i].record_value);\n\t\t}\n\t\treturn dp[live_time][record_time] = ret;\n\n\t}else{ //live_time == record_time\n\n\t\tif(live_time == NUM-1){\n\n\t\t\treturn 0;\n\t\t}\n\n\t\tret = max(ret,recursive(live_time+1,record_time+1));\n\n\t\tfor(int i = 0; i < G[live_time].size(); i++){\n\t\t\tret = max(ret,recursive(G[live_time][i].end_time,record_time+1)+G[live_time][i].live_value);\n\t\t}\n\n\t\tfor(int i = 0; i < G[record_time].size(); i++){\n\t\t\tret = max(ret,recursive(live_time+1,G[record_time][i].end_time)+G[record_time][i].record_value);\n\t\t}\n\n\t\tif(G[live_time].size() > 1){\n\t\t\tfor(int i = 0; i < G[live_time].size(); i++){\n\t\t\t\tfor(int k = 0; k < G[live_time].size(); k++){\n\t\t\t\t\tif(k == i)continue;\n\n\t\t\t\t\tret = max(ret,recursive(G[live_time][i].end_time,G[live_time][k].end_time)+\n\t\t\t\t\t\t\tG[live_time][i].live_value+G[live_time][k].record_value);\n\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dp[live_time][record_time] = ret;\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tG[i].clear();\n\t}\n\n\tint live_value,record_value,start_time,end_time;\n\tchar name[33],start_buf[6],end_buf[6];\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tscanf(\"%s %s %s %d %d\",name,start_buf,end_buf,&live_value,&record_value);\n\t\tstart_time = get_time(start_buf);\n\t\tend_time = get_time(end_buf);\n\n\t\tG[start_time].push_back(Info(end_time,live_value,record_value));\n\t}\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tfor(int k = 0; k < NUM; k++){\n\t\t\tdp[i][k] = -1;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",recursive(0,0));\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 11496, "score_of_the_acc": -1.0196, "final_rank": 13 }, { "submission_id": "aoj_2063_2285258", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint N;\nint dp[1011][1011];\n\nvoid update( int &a, int b ){\n a = max( a, b );\n}\n\nint main(){\n while( ~scanf(\"%d\",&N) && N ) {\n vector<int> S1(N),S2(N);\n vector<int> Ts[60*25], Te[60*25];\n char buf[256];\n for(int i=0;i<N;i++){\n int tb1,tb2,te1,te2;\n scanf(\"%s %d:%d %d:%d %d %d\",buf,&tb1,&tb2,&te1,&te2,&S1[i],&S2[i]);\n int ts = tb1 * 60 + tb2, te = te1 * 60 + te2;\n Ts[ts].emplace_back( i );\n Te[te].emplace_back( i );\n }\n vector<int> G(N+1);\n vector<int> nx(N+1);\n int cur = N;\n for(int i=60*24;i>-1;i--){\n for( int id : Ts[i] ) {\n G[--cur] = id;\n }\n for( int id : Te[i] ) {\n nx[id] = cur;\n }\n }\n\n \n\n fill( dp[0], dp[N], 0 );\n for(int i=0;i<=N;i++){\n for(int j=0;j<=N;j++){\n int a = G[i], b = G[j];\n if( i == j ) {\n if( i == N ) continue;\n update( dp[i+1][j+1], dp[i][j] );\n update( dp[nx[a]][j+1], dp[i][j] + S1[a] );\n update( dp[i+1][nx[b]], dp[i][j] + S2[b] );\n } else if( i < j ) {\n update( dp[i+1][j], dp[i][j] );\n update( dp[nx[a]][j], dp[i][j] + S1[a] );\n } else {\n update( dp[i][j+1], dp[i][j] );\n update( dp[i][nx[b]], dp[i][j] + S2[b] );\n }\n }\n }\n printf(\"%d\\n\",dp[N][N]);\n } \n}", "accuracy": 1, "time_ms": 20, "memory_kb": 7108, "score_of_the_acc": -0.3295, "final_rank": 4 }, { "submission_id": "aoj_2063_1195834", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cmath>\n#include<string>\n#include<sstream>\n#include<algorithm>\n#define rep(i,a) for(int i = 0 ; i < a ; i ++)\n#define loop(i,a,b) for(int i = a ; i < b ; i ++)\n\nusing namespace std;\n\nstruct Data{\n int start,end,onAir,recorded;\n bool operator<(const Data &data) const{\n if(end != data.end) return end < data.end;\n if(start != data.start) return start < data.start;\n return (onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nint N,dp[2000][2000],h,m;\nvector<int> buf;\n\n\nint toTime(string s){\n rep(i,(int)s.size()) if(s[i] == ':') s[i] = ' ';\n stringstream ss(s);\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(void){\n while(cin>>N,N){\n vector<Data> vec(N);\n rep(i,N){\n string a,b,c;\n cin>>a>>b>>c>>vec[i].onAir>>vec[i].recorded;\n vec[i].start = toTime(b); vec[i].end = toTime(c); \n }\n vec.push_back((Data){0,0,0,0});\n rep(i,N+2)rep(j,N+2)dp[i][j] = 0;\n sort(vec.begin(),vec.end());\n buf.clear();\n rep(i,vec.size())buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if(i==j)dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n else if(i<j){\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j-1],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n }else{\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i-1][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n }\n }\n cout<<dp[(int)vec.size()-1][(int)vec.size()-1]<<endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9452, "score_of_the_acc": -0.719, "final_rank": 8 }, { "submission_id": "aoj_2063_1150157", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\nusing namespace std;\nchar in[11100];\nvector<pair<int,pair<int,int> > >g[1500];\nint dp[1500][1500];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<1500;i++)g[i].clear();\n\t\tfor(int i=0;i<a;i++){\n\t\t\tint ph,pm,qh,qm,r,s;\n\t\t\tscanf(\"%s%d:%d%d:%d%d%d\",in,&ph,&pm,&qh,&qm,&r,&s);\n\t\t\tint p=ph*60+pm;\n\t\t\tint q=qh*60+qm;\n\t\t\tg[p].push_back(make_pair(q,make_pair(r,s)));\n\t\t}\n\t\tfor(int i=0;i<1500;i++)for(int j=0;j<1500;j++)\n\t\t\tdp[i][j]=-999999999;\n\t\tdp[0][0]=0;\n\t\tfor(int i=0;i<1450;i++){\n\t\t\tfor(int j=0;j<1450;j++){\n\t\t\t\tif(i<j){\n\t\t\t\t\tdp[i+1][j]=max(dp[i+1][j],dp[i][j]);\n\t\t\t\t\tfor(int k=0;k<g[i].size();k++){\n\t\t\t\t\t\tdp[g[i][k].first][j]=max(dp[g[i][k].first][j],dp[i][j]+g[i][k].second.first);\n\t\t\t\t\t}\n\t\t\t\t}else if(i>j){\n\t\t\t\t\tdp[i][j+1]=max(dp[i][j+1],dp[i][j]);\n\t\t\t\t\tfor(int k=0;k<g[j].size();k++){\n\t\t\t\t\t\tdp[i][g[j][k].first]=max(dp[i][g[j][k].first],dp[i][j]+g[j][k].second.second);\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tdp[i+1][j]=max(dp[i+1][j],dp[i][j]);\n\t\t\t\t\tdp[i][j+1]=max(dp[i][j+1],dp[i][j]);\n\t\t\t\t\tfor(int k=0;k<g[i].size();k++)for(int l=0;l<g[i].size();l++){\n\t\t\t\t\t\tif(k==l)continue;\n\t\t\t\t\t\tdp[g[i][k].first][g[i][l].first]=max(dp[g[i][k].first][g[i][l].first],dp[i][j]+g[i][k].second.first+g[i][l].second.second);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ret=0;\n\t\tfor(int i=0;i<1450;i++)for(int j=0;j<1450;j++)ret=max(ret,dp[i][j]);\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 9908, "score_of_the_acc": -0.7926, "final_rank": 12 }, { "submission_id": "aoj_2063_1124192", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstring>\nusing namespace std;\nstatic const double EPS = 1e-9;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rev(i,n) for(int i=n-1;i>=0;i--)\n#define sz(a) a.size()\n#define all(a) a.begin(),a.end()\n#define mp(a,b) make_pair(a,b)\n#define pb(a) push_back(a)\n#define SS stringstream\n#define DBG1(a) rep(_X,sz(a)){printf(\"%d \",a[_X]);}puts(\"\");\n#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf(\"%d \",a[_X][_Y]);puts(\"\");}\n#define bitcount(b) __builtin_popcount(b)\n#define REP(i, s, e) for ( int i = s; i <= e; i++ ) \n#define fi first\n#define se second\nint dp[1024][1024];\nstruct Item{\n\tint l,r,S1,S2;\n};\nvector<Item> item;\nbool operator < (const Item &a,const Item &b){\n\tif( a.l != b.l ) return a.l < b.l;\n\treturn a.r < b.r;\n}\n\nint nextNode[1024];\nint n;\nint dfs(int x,int y){\n\tif( dp[x][y] != -1) return dp[x][y];\n\tint ans = 0;\n\tint xNext = nextNode[x];\n\tint yNext = nextNode[y];\n\tif( x == n && y == n ) return 0;\n\telse if( x == n ){\n\t\treturn dp[x][y] = max( dfs(x,y+1) , dfs(x,yNext)+item[y].S2 );\n\t}else if( y == n){\n\t\treturn dp[x][y] = max( dfs(x+1,y) , dfs(xNext,y)+item[x].S1 );\n\t}\n\tif( y < x ){\n\t\tans = max( ans , dfs(x,y+1) );\n\t\tans = max( ans , dfs(x,yNext) + item[y].S2 );\n\t}else if( x == y ){\n\t\tans = max( ans , dfs(x+1,y+1) );\n\t\tans = max( ans , dfs(xNext,y+1) + item[x].S1);\n\t\tans = max( ans , dfs(x+1,yNext) + item[y].S2);\n\t}else{\n\t\tans = max( ans , dfs(x+1,y) );\n\t\tans = max( ans , dfs(xNext,y) + item[x].S1 );\n\t}\n\treturn dp[x][y] = ans;\n}\n\n\nint conv(string t){\n\tt[2] = ' ';\n\tstringstream ss(t);\n\tint h,m;\n\tss >> h >> m;\n\t\n\treturn h * 60 + m;\n}\n\nint main(){\n\twhile(cin >> n && n){\n\t\tmemset(dp,-1,sizeof(dp));\n\t\titem.clear();\n\t\tfor(int i = 0 ; i < n ; i++){\n\t\t\tstring nm;\n\t\t\tstring a,b;\n\t\t\tcin >> nm >> a >> b;\n\t\t\tint r1,r2;\n\t\t\tcin >> r1 >> r2;\n\t\t\titem.push_back({conv(a),conv(b),r1,r2});\n\t\t\t//cout << a << \" \" << b << endl;\n\t\t}\n\t\tsort(item.begin(),item.end());\n\t\tfor(int i = 0 ; i < n ; i++){\n\t\t\tnextNode[i] = n;\n\t\t\tfor(int j = i+1 ; j < n ; j++)\n\t\t\t\tif( item[i].r <= item[j].l ){\n\t\t\t\t\tnextNode[i] = j;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t}\n\t\tcout << dfs(0,0) << endl;\n\t}\n\t\n\t\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 5456, "score_of_the_acc": -0.0888, "final_rank": 2 }, { "submission_id": "aoj_2063_1059969", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nstruct Data {\n int start,end,onAir,recorded;\n bool operator < ( const Data &data ) const {\n if( end != data.end ) return end < data.end;\n if( start != data.start ) return start < data.start;\n return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nint N,dp[2000][2000],h,m;\nvector<int> buf;\n\nint toTime(string s){\n rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';\n stringstream ss(s);\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(){\n while( cin >> N, N ){\n vector<Data> vec(N);\n rep(i,N) {\n string a,b,c;\n cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;\n vec[i].start = toTime(b), vec[i].end = toTime(c);\n }\n vec.push_back((Data){0,0,0,0});\n rep(i,N+2) rep(j,N+2) dp[i][j] = 0; \n sort(vec.begin(),vec.end());\n buf.clear();\n rep(i,vec.size()) buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n else if( i < j ) {\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j-1],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n } else {\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i-1][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n }\n }\n printf(\"%d\\n\",dp[(int)vec.size()-1][(int)vec.size()-1]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 9448, "score_of_the_acc": -0.7145, "final_rank": 5 }, { "submission_id": "aoj_2063_1059964", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nstruct Data {\n int start,end,onAir,recorded;\n bool operator < ( const Data &data ) const {\n if( end != data.end ) return end < data.end;\n if( start != data.start ) return start < data.start;\n return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nint N,dp[2000][2000],h,m;\nvector<int> buf;\n\nint toTime(string s){\n rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';\n stringstream ss(s);\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(){\n while( cin >> N, N ){\n vector<Data> vec(N);\n rep(i,N) {\n string a,b,c;\n cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;\n vec[i].start = toTime(b), vec[i].end = toTime(c);\n }\n vec.push_back((Data){0,0,0,0});\n rep(i,N+2) rep(j,N+2) dp[i][j] = 0; \n sort(vec.begin(),vec.end());\n buf.clear();\n rep(i,vec.size()) buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n else if( i < j ) {\n dp[i][j] = dp[i][j-1];\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n } else {\n dp[i][j] = dp[i-1][j];\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n }\n }\n printf(\"%d\\n\",dp[(int)vec.size()-1][(int)vec.size()-1]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9448, "score_of_the_acc": -0.7184, "final_rank": 6 }, { "submission_id": "aoj_2063_1059950", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nstruct Data {\n int start,end,onAir,recorded;\n bool operator < ( const Data &data ) const {\n if( end != data.end ) return end < data.end;\n if( start != data.start ) return start < data.start;\n return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nconst int IINF = INT_MAX;\nint N,dp[2000][2000],h,m;\nvector<int> buf;\n\nint toTime(string s){\n rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';\n stringstream ss(s);\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(){\n while( cin >> N, N ){\n vector<Data> vec(N);\n rep(i,N) {\n string a,b,c;\n cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;\n vec[i].start = toTime(b), vec[i].end = toTime(c);\n }\n vec.push_back((Data){0,0,0,0});\n rep(i,N+2) rep(j,N+2) dp[i][j] = 0; \n sort(vec.begin(),vec.end());\n buf.clear();\n int maxi = 0;\n rep(i,vec.size()) buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n else if( i < j ) {\n dp[i][j] = dp[i][j-1];\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n } else {\n dp[i][j] = dp[i-1][j];\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n maxi = max(maxi,dp[i][j]);\n }\n }\n printf(\"%d\\n\",maxi);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9448, "score_of_the_acc": -0.7184, "final_rank": 6 }, { "submission_id": "aoj_2063_1059947", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nstruct Data {\n int start,end,onAir,recorded;\n bool operator < ( const Data &data ) const {\n if( end != data.end ) return end < data.end;\n if( start != data.start ) return start < data.start;\n return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nconst int IINF = INT_MAX;\nint N,s[MAX_N],t[MAX_N],score[MAX_N][2],dp[2000][2000];\nvector<int> buf;\n\nint toTime(string s){\n rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';\n stringstream ss;\n ss << s;\n int h,m;\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(){\n while( cin >> N, N ){\n vector<Data> vec;\n rep(i,N) {\n string a,b,c;\n cin >> a >> b >> c >> score[i][0] >> score[i][1];\n assert( b != c );\n s[i] = toTime(b), t[i] = toTime(c);\n vec.push_back((Data){s[i],t[i],score[i][0],score[i][1]});\n }\n vec.push_back((Data){0,0,0,0});\n vec.push_back((Data){IINF,IINF,0,0});\n rep(i,N+2) rep(j,N+2) dp[i][j] = 0; \n sort(vec.begin(),vec.end());\n buf.clear();\n int maxi = 0;\n rep(i,vec.size()) buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if( i == j ) {\n dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n } else if( i < j ) {\n dp[i][j] = dp[i][j-1];\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n } else {\n dp[i][j] = dp[i-1][j];\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n maxi = max(maxi,dp[i][j]);\n }\n }\n cout << maxi << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9476, "score_of_the_acc": -0.7227, "final_rank": 9 }, { "submission_id": "aoj_2063_1059946", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nstruct Data {\n int start,end,onAir,recorded;\n bool operator < ( const Data &data ) const {\n if( end != data.end ) return end < data.end;\n if( start != data.start ) return start < data.start;\n return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nconst int IINF = INT_MAX;\nint N,s[MAX_N],t[MAX_N],score[MAX_N][2],dp[2000][2000];\nvector<int> buf;\n\nint toTime(string s){\n rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';\n stringstream ss;\n ss << s;\n int h,m;\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(){\n while( cin >> N, N ){\n vector<Data> vec;\n rep(i,N) {\n string a,b,c;\n cin >> a >> b >> c >> score[i][0] >> score[i][1];\n assert( b != c );\n s[i] = toTime(b), t[i] = toTime(c);\n vec.push_back((Data){s[i],t[i],score[i][0],score[i][1]});\n }\n vec.push_back((Data){0,0,0,0});\n vec.push_back((Data){IINF,IINF,0,0});\n rep(i,N+2) rep(j,N+2) dp[i][j] = 0; \n sort(vec.begin(),vec.end());\n buf.clear();\n int maxi = 0;\n rep(i,vec.size()) buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if( i == j ) {\n dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n } else if( i < j ) {\n dp[i][j] = dp[i][j-1];\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin();\n if( buf[prev] > vec[j].start ) --prev;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n } else {\n dp[i][j] = dp[i-1][j];\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin();\n if( buf[prev] > vec[i].start ) --prev;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n maxi = max(maxi,dp[i][j]);\n }\n }\n cout << maxi << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9476, "score_of_the_acc": -0.7227, "final_rank": 9 }, { "submission_id": "aoj_2063_1059944", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nstruct Data {\n int start,end,onAir,recorded;\n bool operator < ( const Data &data ) const {\n if( end != data.end ) return end < data.end;\n if( start != data.start ) return start < data.start;\n return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;\n }\n};\n\nconst int MAX_N = 1100;\nconst int IINF = INT_MAX;\nint N,s[MAX_N],t[MAX_N],score[MAX_N][2],dp[2000][2000];\nvector<int> buf;\n\nint toTime(string s){\n rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';\n stringstream ss;\n ss << s;\n int h,m;\n ss >> h >> m;\n return h * 60 + m;\n}\n\nint main(){\n while( cin >> N, N ){\n vector<Data> vec;\n rep(i,N) {\n string a,b,c;\n cin >> a >> b >> c >> score[i][0] >> score[i][1];\n assert( b != c );\n s[i] = toTime(b), t[i] = toTime(c);\n vec.push_back((Data){s[i],t[i],score[i][0],score[i][1]});\n }\n vec.push_back((Data){0,0,0,0});\n vec.push_back((Data){IINF,IINF,0,0});\n rep(i,N+2) rep(j,N+2) dp[i][j] = 0; \n sort(vec.begin(),vec.end());\n //rep(i,vec.size()) cout << \"[\" <<vec[i].start << \",\" << vec[i].end << \"] : \" << vec[i].onAir << \" and \" << vec[i].recorded << endl;\n buf.clear();\n int maxi = 0;\n rep(i,vec.size()) buf.push_back(vec[i].end);\n rep(i,vec.size()){\n rep(j,vec.size()){\n if( i == j ) {\n dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));\n } else if( i < j ) {\n dp[i][j] = dp[i][j-1];\n int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin();\n //if( prev == j ) -- prev;\n if( buf[prev] > vec[j].start ) --prev;\n\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);\n } else {\n dp[i][j] = dp[i-1][j];\n int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin();\n //if( prev == i ) -- prev;\n if( buf[prev] > vec[i].start ) --prev;\n dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);\n }\n maxi = max(maxi,dp[i][j]);\n //cout << \"dp[\" << i << \"][\" << j << \"] = \" << dp[i][j] << endl;\n }\n }\n cout << maxi << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9476, "score_of_the_acc": -0.7227, "final_rank": 9 }, { "submission_id": "aoj_2063_613129", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\ntypedef pair<int, int> P;\nstruct TV{\n P time;\n int r1, r2;\n bool operator < (const TV& t) const{\n return time < t.time;\n }\n TV() {}\n TV(int f) { time = P(f, 0); }\n};\nvoid update(int& x, int y){\n if(x < y) x = y;\n}\n\nint main(){\n int N;\n while(cin >> N && N){\n int dp[1001][1001] = {};\n vector<TV> tv(N);\n REP(i, N){\n char tmp[100];\n int a, b, c, d;\n scanf(\"%s %d:%d %d:%d %d %d\", tmp, &a, &b, &c, &d, &tv[i].r1, &tv[i].r2);\n tv[i].time = P(a * 60 + b , c * 60 + d);\n }\n sort(tv.begin(), tv.end());\n for(int i = 0; i <= N; i++){\n for(int j = 0; j <= N; j++){\n if(i < N){\n int ni = lower_bound(tv.begin(), tv.end(), TV(tv[i].time.second)) - tv.begin();\n update(dp[ni][max(i + 1, j)], dp[i][j] + tv[i].r1);\n update(dp[i + 1][j], dp[i][j]);\n }\n if(j < N){\n int nj = lower_bound(tv.begin(), tv.end(), TV(tv[j].time.second)) - tv.begin();\n update(dp[max(j + 1, i)][nj], dp[i][j] + tv[j].r2);\n update(dp[i][j + 1], dp[i][j]);\n }\n }\n }\n cout << dp[N][N] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 5184, "score_of_the_acc": -0.1296, "final_rank": 3 }, { "submission_id": "aoj_2063_472724", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nclass Data\n{\npublic:\n int t1, t2, r1, r2;\n bool operator< (const Data& d) const{\n return t1 < d.t1;\n }\n};\n\nint n;\nvector<Data> d;\nvector<int> nextTV;\n\nvector<vector<int> > memo;\n\nint solve(int a, int b)\n{\n if(a == n+1 || b == n+1)\n return 0;\n if(a == n && b == n)\n return 0;\n if(memo[a][b] != -1)\n return memo[a][b];\n\n int ret = 0;\n ret = max(ret, solve(a, b+1));\n ret = max(ret, solve(a+1, b));\n if(a == b){\n ret = max(ret, d[a].r1 + solve(nextTV[a], b+1));\n ret = max(ret, d[b].r2 + solve(a+1, nextTV[b]));\n }else if(a > b){\n ret = max(ret, d[b].r2 + solve(a, nextTV[b]));\n }else{\n ret = max(ret, d[a].r1 + solve(nextTV[a], b));\n }\n\n return memo[a][b] = ret;\n}\n\nint main()\n{\n for(;;){\n cin >> n;\n if(n == 0)\n return 0;\n\n d.resize(n+1);\n for(int i=0; i<n; ++i){\n string s;\n int h1, m1, h2, m2;\n char c;\n cin >> s >> h1 >> c >> m1 >> h2 >> c >> m2 >> d[i].r1 >> d[i].r2;\n d[i].t1 = h1 * 60 + m1;\n d[i].t2 = h2 * 60 + m2;\n }\n d[n].t1 = INT_MAX / 2;\n d[n].t2 = INT_MAX / 2 + 1;\n d[n].r1 = d[n].r2 = 0;\n sort(d.begin(), d.end());\n\n nextTV.resize(n+1);\n for(int i=0; i<=n; ++i){\n Data tmp;\n tmp.t1 = d[i].t2;\n nextTV[i] = lower_bound(d.begin(), d.end(), tmp) - d.begin();\n }\n\n memo.assign(n+1, vector<int>(n+1, -1));\n cout << solve(0, 0) << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 4952, "score_of_the_acc": -0.0353, "final_rank": 1 } ]
aoj_2066_cpp
Problem I: Roads in a City Roads in a city play important roles in development of the city. Once a road is built, people start their living around the road. A broader road has a bigger capacity, that is, people can live on a wider area around the road. Interstellar Conglomerate of Plantation and Colonization (ICPC) is now planning to develop roads on a new territory. The new territory is a square and is completely clear. ICPC has already made several plans, but there are some difficulties in judging which plan is the best. Therefore, ICPC has collected several great programmers including you, and asked them to write programs that compute several metrics for each plan. Fortunately your task is a rather simple one. You are asked to compute the area where people can live from the given information about the locations and the capacities of the roads. The following figure shows the first plan given as the sample input. Figure 1: The first plan given as the sample input Input The input consists of a number of plans. The first line of each plan denotes the number n of roads ( n ≤ 50), and the following n lines provide five integers x 1 , y 1 , x 2 , y 2 , and r , separated by a space. ( x 1 , y 1 ) and ( x 2 , y 2 ) denote the two endpoints of a road (-15 ≤ x 1 , y 1 , x 2 , y 2 ≤ 15), and the value r denotes the capacity that is represented by the maximum distance from the road where people can live ( r ≤ 10). The end of the input is indicated by a line that contains only a single zero. The territory is located at -5 ≤ x , y ≤ 5. You may assume that each road forms a straight line segment and that any lines do not degenerate. Output Print the area where people can live in one line for each plan. You may print an arbitrary number of digits after the decimal points, provided that difference from the exact answer is not greater than 0.01. Sample Input 2 0 -12 0 0 2 0 0 12 0 2 0 Output for the Sample Input 39.14159
[ { "submission_id": "aoj_2066_5469291", "code_snippet": "#include <bits/stdc++.h>\n#include <stdlib.h>\n#include <cassert>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vec;\n\n#define _GLIBCXX_DEBUG\n#define REP(i,a,b) for(int i=(int)a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n\n\nstruct Vec2{\n double x;\n double y;\n Vec2(double X, double Y) : x(X), y(Y) {}\n bool operator==(const Vec2& rhs) const{\n return x==rhs.x ? y==rhs.y : false;\n }\n\n bool operator!=(const Vec2& rhs) const{\n return (*this)==rhs ? false: true;\n }\n Vec2 operator=(const Vec2& rhs){\n x=rhs.x; y=rhs.y;\n return (*this);\n }\n Vec2 operator+(const Vec2& rhs) const{\n return Vec2(this->x+rhs.x, this->y+rhs.y);\n }\n Vec2 operator-(const Vec2& rhs) const{\n return Vec2(this->x-rhs.x, this->y-rhs.y);\n }\n Vec2 operator*(const double &rhs) const{\n return Vec2(this->x*rhs,this->y*rhs);\n }\n\n Vec2 operator/(const double &rhs) const{\n assert(rhs>0);\n return Vec2(this->x/rhs,this->y/rhs);\n }\n \n double operator*(const Vec2 &rhs) const{\n return this->x*rhs.x+this->y*rhs.y;\n }\n \n};\ndouble length(const Vec2 &a){\n return sqrt(a*a);\n}\n\nVec2 normalize(const Vec2 &a){\n double len=length(a);\n return len>0 ? a/len : a;\n}\n\nVec2 sui(const Vec2 &a){\n Vec2 aa=normalize(a);\n return Vec2(aa.y,-aa.x);\n}\n\nconst double OUT=5000000;\n\ndouble given_x(Vec2 a, Vec2 b, double xx, bool senbun=true);\ndouble given_y(Vec2 a, Vec2 b, double yy, bool senbun=true);\n\nstruct circle{\n Vec2 mid;\n double r;\n circle(Vec2 mid_, double r_):mid(mid_),r(r_){}\n bool inside(Vec2 p) const{\n return length(p-mid)<r;\n }\n};\n\nstruct rectangle{\n Vec2 a;\n Vec2 b;\n double r;\n vector<Vec2> corners;\n rectangle(Vec2 a_, Vec2 b_, double r_) : a(a_), b(b_), r(r_){\n Vec2 s=sui(b-a)*r;\n corners.pb(a+s);\n corners.pb(a-s);\n corners.pb(b-s);\n corners.pb(b+s);\n }\n void print() const{\n cout<<\"rectangle is \";\n for(auto e: corners) cout<<e.x<<' '<<e.y<<\", \";\n cout<<endl;\n }\n};\n\nstruct square{\n Vec2 mid;\n double len;\n vector<Vec2> corners;\n double xl,xu,yl,yu;\n square(Vec2 mid_,double len_):mid(mid_),len(len_) ,xl(mid.x-len/2), xu(mid.x+len/2), yl(mid.y-len/2), yu(mid.y+len/2) {\n corners.pb(Vec2(xl,yl));\n corners.pb(Vec2(xu,yl));\n corners.pb(Vec2(xu,yu));\n corners.pb(Vec2(xl,yu));\n }\n bool inside(Vec2 p) const{\n if(abs(p.x-mid.x)>len/2) return false;\n if(abs(p.y-mid.y)>len/2) return false;\n return true;\n }\n\n int judge(const circle &c) const{\n bool memo=c.inside(corners[0]);\n REP(i,1,4) if(memo!=c.inside(corners[i])) return 0; //0は混在\n if(memo) return 1; // 1は完全に含まれている\n if(length(this->mid-c.mid)<c.r+len/2) return 0;\n return -1;\n }\n\n int judge(const rectangle &c) const;\n void print() const{\n cout<<\"square is \";\n for(auto e: corners) cout<<e.x<<' '<<e.y<<\", \";\n cout<<endl;\n }\n};\n\n\n\ndouble given_x(Vec2 a, Vec2 b, double xx, bool senbun){\n if(senbun && a.x>xx && b.x>xx) return OUT;\n if(senbun && a.x<xx && b.x<xx) return OUT;\n if(a.x==xx) return a.y;\n if(b.x==xx) return b.y;\n double Y=b.y-a.y, X=b.x-a.x, XX=xx-a.x;\n if(!X) return a.y+Y/2;\n return a.y+Y*XX/X;\n}\n\ndouble given_y(Vec2 a, Vec2 b, double yy, bool senbun){\n if(senbun && a.y>yy && b.y>yy) return OUT;\n if(senbun && a.y<yy && b.y<yy) return OUT;\n if(a.y==yy) return a.x;\n if(b.y==yy) return b.x;\n double Y=b.y-a.y, X=b.x-a.x, YY=yy-a.y;\n if(!Y) return a.x+X/2;\n return a.x+X*YY/Y;\n}\nbool cross(square s, Vec2 a, Vec2 b){\n if(a.x<s.xl && b.x<s.xl) return false;\n if(a.x>s.xu && b.x>s.xu) return false;\n if(a.y<s.yl && b.y<s.yl) return false;\n if(a.y>s.yu && b.y>s.yu) return false;\n if(a.x==b.x) return true;\n if(a.y==b.y) return true;\n double XL=given_x(a,b,s.xl);\n if(s.yl<=XL && XL<=s.yu) return true;\n double XU=given_x(a,b,s.xu);\n if(s.yl<=XU && XU<=s.yu) return true;\n double YL=given_y(a,b,s.yl);\n if(s.xl<=YL && YL<=s.xu) return true;\n double YU=given_y(a,b,s.yu);\n if(s.xl<=YU && YU<=s.xu) return true;\n return false;\n}\n\nint square::judge(const rectangle &c) const{\n rep(i,4) if(this->inside(c.corners[i])) return 0;\n rep(i,4) if(cross((*this),c.corners[i],c.corners[(i+1)%4])) return 0;\n double big=OUT,small=OUT;\n rep(i,4) {\n auto tmp=given_x(c.corners[i], c.corners[(i+1)%4], xl);\n if(c.corners[i].x!=xl || c.corners[(i+1)%4].x!=xl) {\n if(tmp!=OUT){\n if(tmp>=yu) big=tmp;\n if(tmp<=yl) small=tmp;\n }\n }\n }\n /*this->print();\n c.print();\n cout<<big<<' '<<small<<endl;*/\n if(big==OUT) return -1;\n if(small==OUT) return -1;\n return 1;\n}\n\n\ndouble smallest=(1e-8);\n\nstruct one_question{\n ll N,R,C; \n vector<rectangle> rec;\n vector<circle> cir;\n \n one_question(ll N_) :N(N_){\n rep(i,N){\n double a,b,c,d,r; cin>>a>>b>>c>>d>>r;\n Vec2 A(a,b), B(c,d);\n cir.pb(circle(A,r));\n if(A!=B){\n cir.pb(circle(B,r));\n rec.pb(rectangle(A,B,r));\n }\n }\n R=rec.size(); C=cir.size();\n }\n double Solve(const square& s, vec rec_memo, vec cir_memo){\n double full=s.len*s.len;\n if(full<=smallest) return full/2;\n bool nothing=true;\n rep(i,R) if(!rec_memo[i]){\n rec_memo[i]=s.judge(rec[i]);\n if(rec_memo[i]==1) return full;\n if(rec_memo[i]==0) nothing=false;\n }\n rep(i,C) if(!cir_memo[i]){\n cir_memo[i]=s.judge(cir[i]);\n if(cir_memo[i]==1) return full;\n if(cir_memo[i]==0) nothing=false;\n }\n if(nothing) return 0;\n double ret=0;\n rep(i,4){\n square tmp((s.mid+s.corners[i])/2,s.len/2);\n ret+=Solve(tmp,rec_memo,cir_memo);\n }\n return ret;\n\n }\n double ans(){\n square start(Vec2(0,0),10);\n vec rec_memo(R,0);\n vec cir_memo(C,0);\n return Solve(start,rec_memo,cir_memo);\n }\n\n};\n \n\nint main(){\n vector<double> ret;\n while(true){\n ll n; cin>>n;\n if(!n) break;\n one_question tmp(n);\n ret.pb(tmp.ans());\n }\n for(auto e: ret) cout<<setprecision(8)<<e<<endl;\n}", "accuracy": 1, "time_ms": 1870, "memory_kb": 3616, "score_of_the_acc": -2, "final_rank": 5 }, { "submission_id": "aoj_2066_5469258", "code_snippet": "#include <bits/stdc++.h>\n#include <stdlib.h>\n#include <cassert>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vec;\ntypedef vector<vec> mat;\ntypedef vector<double> Vec;\ntypedef vector<Vec> Mat;\ntypedef pair<ll,ll> P;\ntypedef pair<double,ll> Pd;\ntypedef pair<double,double> PD;\ntypedef priority_queue<P,vector<P>,greater<P> > P_queue;\ntypedef priority_queue<Pd,vector<Pd>,greater<Pd> > Pd_queue;\n\nconst ll MOD=998244353;\nconst ll mod=1000000007;\nconst ll INF=1e15;\nconst ll MAXINF=9223372036854775806;\nconst double DEL=1e-7;\nconst double PI=3.14159265359;\n\n#define _GLIBCXX_DEBUG\n#define REP(i,a,b) for(int i=(int)a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define mp make_pair\n#define ALL(a) a.begin(),a.end()\n#define SORT(a) sort(ALL(a))\n#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());\n\n\n\n\n\nstruct Vec2{\n double x;\n double y;\n Vec2(double X, double Y) : x(X), y(Y) {}\n Vec2(PD a):x(a.first), y(a.second) {}\n bool operator==(const Vec2& rhs) const{\n return x==rhs.x ? y==rhs.y : false;\n }\n\n bool operator!=(const Vec2& rhs) const{\n return (*this)==rhs ? false: true;\n }\n Vec2 operator=(const Vec2& rhs){\n x=rhs.x; y=rhs.y;\n return (*this);\n }\n Vec2 operator+(const Vec2& rhs) const{\n return Vec2(this->x+rhs.x, this->y+rhs.y);\n }\n Vec2 operator-(const Vec2& rhs) const{\n return Vec2(this->x-rhs.x, this->y-rhs.y);\n }\n Vec2 operator*(const double &rhs) const{\n return Vec2(this->x*rhs,this->y*rhs);\n }\n\n Vec2 operator/(const double &rhs) const{\n assert(rhs>0);\n return Vec2(this->x/rhs,this->y/rhs);\n }\n double operator*(const Vec2 &rhs) const{\n return this->x*rhs.x+this->y*rhs.y;\n }\n \n};\ndouble length(const Vec2 &a){\n return sqrt(a*a);\n}\n\nVec2 normalize(const Vec2 &a){\n double len=length(a);\n return len>0 ? a/len : a;\n}\n\nVec2 sui(const Vec2 &a){\n Vec2 aa=normalize(a);\n return Vec2(aa.y,-aa.x);\n}\n\nconst double OUT=5000000;\n\ndouble given_x(Vec2 a, Vec2 b, double xx, bool senbun=true);\ndouble given_y(Vec2 a, Vec2 b, double yy, bool senbun=true);\n\nstruct circle{\n Vec2 mid;\n double r;\n circle(Vec2 mid_, double r_):mid(mid_),r(r_){}\n bool inside(Vec2 p) const{\n return length(p-mid)<r;\n }\n};\n\nstruct rectangle{\n Vec2 a;\n Vec2 b;\n double r;\n vector<Vec2> corners;\n rectangle(Vec2 a_, Vec2 b_, double r_) : a(a_), b(b_), r(r_){\n Vec2 s=sui(b-a)*r;\n corners.pb(a+s);\n corners.pb(a-s);\n corners.pb(b-s);\n corners.pb(b+s);\n }\n void print() const{\n cout<<\"rectangle is \";\n for(auto e: corners) cout<<e.x<<' '<<e.y<<\", \";\n cout<<endl;\n }\n};\n\nstruct square{\n Vec2 mid;\n double len;\n vector<Vec2> corners;\n double xl,xu,yl,yu;\n square(Vec2 mid_,double len_):mid(mid_),len(len_) ,xl(mid.x-len/2), xu(mid.x+len/2), yl(mid.y-len/2), yu(mid.y+len/2) {\n corners.pb(Vec2(xl,yl));\n corners.pb(Vec2(xu,yl));\n corners.pb(Vec2(xu,yu));\n corners.pb(Vec2(xl,yu));\n }\n bool inside(Vec2 p) const{\n if(abs(p.x-mid.x)>len/2) return false;\n if(abs(p.y-mid.y)>len/2) return false;\n return true;\n }\n\n int judge(const circle &c) const{\n bool memo=c.inside(corners[0]);\n REP(i,1,4) if(memo!=c.inside(corners[i])) return 0; //0は混在\n if(memo) return 1; // 1は完全に含まれている\n if(length(this->mid-c.mid)<c.r+len/2) return 0;\n return -1;\n }\n\n int judge(const rectangle &c) const;\n void print() const{\n cout<<\"square is \";\n for(auto e: corners) cout<<e.x<<' '<<e.y<<\", \";\n cout<<endl;\n }\n};\n\n\n\ndouble given_x(Vec2 a, Vec2 b, double xx, bool senbun){\n if(senbun && a.x>xx && b.x>xx) return OUT;\n if(senbun && a.x<xx && b.x<xx) return OUT;\n if(a.x==xx) return a.y;\n if(b.x==xx) return b.y;\n double Y=b.y-a.y, X=b.x-a.x, XX=xx-a.x;\n if(!X) return a.y+Y/2;\n return a.y+Y*XX/X;\n}\n\ndouble given_y(Vec2 a, Vec2 b, double yy, bool senbun){\n if(senbun && a.y>yy && b.y>yy) return OUT;\n if(senbun && a.y<yy && b.y<yy) return OUT;\n if(a.y==yy) return a.x;\n if(b.y==yy) return b.x;\n double Y=b.y-a.y, X=b.x-a.x, YY=yy-a.y;\n if(!Y) return a.x+X/2;\n return a.x+X*YY/Y;\n}\nbool cross(square s, Vec2 a, Vec2 b){\n if(a.x<s.xl && b.x<s.xl) return false;\n if(a.x>s.xu && b.x>s.xu) return false;\n if(a.y<s.yl && b.y<s.yl) return false;\n if(a.y>s.yu && b.y>s.yu) return false;\n if(a.x==b.x) return true;\n if(a.y==b.y) return true;\n double XL=given_x(a,b,s.xl);\n if(s.yl<=XL && XL<=s.yu) return true;\n double XU=given_x(a,b,s.xu);\n if(s.yl<=XU && XU<=s.yu) return true;\n double YL=given_y(a,b,s.yl);\n if(s.xl<=YL && YL<=s.xu) return true;\n double YU=given_y(a,b,s.yu);\n if(s.xl<=YU && YU<=s.xu) return true;\n return false;\n}\n\nint square::judge(const rectangle &c) const{\n rep(i,4) if(this->inside(c.corners[i])) return 0;\n rep(i,4) if(cross((*this),c.corners[i],c.corners[(i+1)%4])) return 0;\n double big=OUT,small=OUT;\n rep(i,4) {\n auto tmp=given_x(c.corners[i], c.corners[(i+1)%4], xl);\n if(c.corners[i].x!=xl || c.corners[(i+1)%4].x!=xl) {\n if(tmp!=OUT){\n if(tmp>=yu) big=tmp;\n if(tmp<=yl) small=tmp;\n }\n }\n }\n /*this->print();\n c.print();\n cout<<big<<' '<<small<<endl;*/\n if(big==OUT) return -1;\n if(small==OUT) return -1;\n return 1;\n}\n\n\ndouble smallest=(1e-8);\n\nstruct one_question{\n ll N,R,C; \n vector<rectangle> rec;\n vector<circle> cir;\n \n one_question(ll N_) :N(N_){\n rep(i,N){\n double a,b,c,d,r; cin>>a>>b>>c>>d>>r;\n Vec2 A(a,b), B(c,d);\n cir.pb(circle(A,r));\n if(A!=B){\n cir.pb(circle(B,r));\n rec.pb(rectangle(A,B,r));\n }\n }\n R=rec.size(); C=cir.size();\n }\n double Solve(const square& s, vec rec_memo, vec cir_memo){\n double full=s.len*s.len;\n if(full<=smallest) return full/2;\n bool nothing=true;\n rep(i,R) if(!rec_memo[i]){\n rec_memo[i]=s.judge(rec[i]);\n if(rec_memo[i]==1) return full;\n if(rec_memo[i]==0) nothing=false;\n }\n rep(i,C) if(!cir_memo[i]){\n cir_memo[i]=s.judge(cir[i]);\n if(cir_memo[i]==1) return full;\n if(cir_memo[i]==0) nothing=false;\n }\n if(nothing) return 0;\n double ret=0;\n rep(i,4){\n square tmp((s.mid+s.corners[i])/2,s.len/2);\n ret+=Solve(tmp,rec_memo,cir_memo);\n }\n return ret;\n\n }\n double ans(){\n square start(Vec2(0,0),10);\n vec rec_memo(R,0);\n vec cir_memo(C,0);\n return Solve(start,rec_memo,cir_memo);\n }\n\n};\n\n\n\n\n \n\nint main(){\n vector<double> ret;\n while(true){\n ll n; cin>>n;\n if(!n) break;\n one_question tmp(n);\n ret.pb(tmp.ans());\n }\n for(auto e: ret) cout<<setprecision(8)<<e<<endl;\n}", "accuracy": 1, "time_ms": 1870, "memory_kb": 3504, "score_of_the_acc": -1.9514, "final_rank": 4 }, { "submission_id": "aoj_2066_3172671", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(cross(l[1]-l[0], p-l[0])) /abs(l[1]-l[0]);\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nVP crosspointCL(const C &c, const L &l){\n VP ret;\n P mid = projection(l, c.p);\n double d = distanceLP(l, c.p);\n if(EQ(d, c.r)){\n ret.push_back(mid);\n }else if(d < c.r){\n double len = sqrt(c.r*c.r -d*d);\n ret.push_back(mid +len*unit(l[1]-l[0]));\n ret.push_back(mid -len*unit(l[1]-l[0]));\n }\n return ret;\n}\nVP crosspointCS(const C &c, const L &s){\n VP ret;\n VP cp = crosspointCL(c,s);\n for(int i=0; i<(int)cp.size(); i++){\n if(intersectSP(s, cp[i])){\n ret.push_back(cp[i]);\n }\n }\n return ret;\n}\n\nint in_poly(const P &p, const VP &poly){\n int n = poly.size();\n int ret = -1;\n for(int i=0; i<n; i++){\n P a = poly[i]-p;\n P b = poly[(i+1)%n]-p;\n if(a.Y > b.Y) swap(a,b);\n if(intersectSP(L(a,b), P(0,0))) return 0;\n if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;\n }\n return ret;\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<VP> rect(n, VP(4));\n vector<C> circle(2*n);\n for(int i=0; i<n; i++){\n double xs,ys,xt,yt,r;\n cin >> xs >> ys >> xt >> yt >> r;\n P s(xs, ys), t(xt, yt);\n P cn = unit(t -s) *r *P(0, 1);\n rect[i][0] = t +cn;\n rect[i][1] = s +cn;\n rect[i][2] = s -cn;\n rect[i][3] = t -cn;\n circle[2*i +0] = C(s, r);\n circle[2*i +1] = C(t, r);\n }\n\n double lbx=-5, ubx=5;\n double lby=-5, uby=5;\n int splitnum = 1e3;\n double dx = (ubx -lbx) /splitnum;\n double ans = 0;\n for(int i=0; i<splitnum; i++){\n double x = lbx +(i+0.5)*dx;\n L seg(P(x, lby), P(x, uby));\n VP cp{seg[0], seg[1]};\n for(int j=0; j<n; j++){\n for(int k=0; k<4; k++){\n L edge(rect[j][k], rect[j][(k+1)%4]);\n if(!isParallel(seg, edge) && intersectSS(seg, edge)){\n cp.push_back(crosspointLL(seg, edge));\n }\n }\n }\n for(int j=0; j<2*n; j++){\n VP ret = crosspointCS(circle[j], seg);\n cp.insert(cp.end(), ret.begin(), ret.end());\n }\n sort(cp.begin(), cp.end());\n cp.erase(unique(cp.begin(), cp.end()), cp.end());\n double len = 0;\n for(int j=0; j<(int)cp.size()-1; j++){\n P mid = (cp[j] +cp[j+1]) /2.0;\n bool in = false;\n for(int k=0; k<n && !in; k++){\n if(in_poly(mid, rect[k]) > 0){\n in = true;\n }\n }\n for(int k=0; k<2*n && !in; k++){\n if(abs(mid -circle[k].p) < circle[k].r){\n in = true;\n }\n }\n if(in){\n len += abs(cp[j+1] -cp[j]);\n }\n }\n ans += dx*len;\n }\n cout << fixed << setprecision(5);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3252, "score_of_the_acc": -0.842, "final_rank": 2 }, { "submission_id": "aoj_2066_3172670", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(cross(l[1]-l[0], p-l[0])) /abs(l[1]-l[0]);\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nVP crosspointCL(const C &c, const L &l){\n VP ret;\n P mid = projection(l, c.p);\n double d = distanceLP(l, c.p);\n if(EQ(d, c.r)){\n ret.push_back(mid);\n }else if(d < c.r){\n double len = sqrt(c.r*c.r -d*d);\n ret.push_back(mid +len*unit(l[1]-l[0]));\n ret.push_back(mid -len*unit(l[1]-l[0]));\n }\n return ret;\n}\nVP crosspointCS(const C &c, const L &s){\n VP ret;\n VP cp = crosspointCL(c,s);\n for(int i=0; i<(int)cp.size(); i++){\n if(intersectSP(s, cp[i])){\n ret.push_back(cp[i]);\n }\n }\n return ret;\n}\n\nint in_poly(const P &p, const VP &poly){\n int n = poly.size();\n int ret = -1;\n for(int i=0; i<n; i++){\n P a = poly[i]-p;\n P b = poly[(i+1)%n]-p;\n if(a.Y > b.Y) swap(a,b);\n if(intersectSP(L(a,b), P(0,0))) return 0;\n if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;\n }\n return ret;\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<VP> rect(n, VP(4));\n vector<C> circle(2*n);\n for(int i=0; i<n; i++){\n double xs,ys,xt,yt,r;\n cin >> xs >> ys >> xt >> yt >> r;\n P s(xs, ys), t(xt, yt);\n P cn = unit(t -s) *r *P(0, 1);\n rect[i][0] = t +cn;\n rect[i][1] = s +cn;\n rect[i][2] = s -cn;\n rect[i][3] = t -cn;\n circle[2*i +0] = C(s, r);\n circle[2*i +1] = C(t, r);\n }\n\n double lbx=-5, ubx=5;\n double lby=-5, uby=5;\n int splitnum = 1e4;\n double dx = (ubx -lbx) /splitnum;\n double ans = 0;\n for(int i=0; i<splitnum; i++){\n double x = lbx +(i+0.5)*dx;\n L seg(P(x, lby), P(x, uby));\n VP cp{seg[0], seg[1]};\n for(int j=0; j<n; j++){\n for(int k=0; k<4; k++){\n L edge(rect[j][k], rect[j][(k+1)%4]);\n if(!isParallel(seg, edge) && intersectSS(seg, edge)){\n cp.push_back(crosspointLL(seg, edge));\n }\n }\n }\n for(int j=0; j<2*n; j++){\n VP ret = crosspointCS(circle[j], seg);\n cp.insert(cp.end(), ret.begin(), ret.end());\n }\n sort(cp.begin(), cp.end());\n cp.erase(unique(cp.begin(), cp.end()), cp.end());\n double len = 0;\n for(int j=0; j<(int)cp.size()-1; j++){\n P mid = (cp[j] +cp[j+1]) /2.0;\n bool in = false;\n for(int k=0; k<n && !in; k++){\n if(in_poly(mid, rect[k]) > 0){\n in = true;\n }\n }\n for(int k=0; k<2*n && !in; k++){\n if(abs(mid -circle[k].p) < circle[k].r){\n in = true;\n }\n }\n if(in){\n len += abs(cp[j+1] -cp[j]);\n }\n }\n ans += dx*len;\n }\n cout << fixed << setprecision(5);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 3312, "score_of_the_acc": -1.3432, "final_rank": 3 }, { "submission_id": "aoj_2066_1124279", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\nusing namespace std;\nstatic const double EPS = 1e-10;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rev(i,n) for(int i=n-1;i>=0;i--)\n#define sz(a) a.size()\n#define all(a) a.begin(),a.end()\n#define mp(a,b) make_pair(a,b)\n#define pb(a) push_back(a)\n#define SS stringstream\n#define DBG1(a) rep(_X,sz(a)){printf(\"%d \",a[_X]);}puts(\"\");\n#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf(\"%d \",a[_X][_Y]);puts(\"\");}\n#define bitcount(b) __builtin_popcount(b)\n#define REP(i, s, e) for ( int i = s; i <= e; i++ ) \n \nconst double INF = 1e12;\ntypedef complex<double> P,point;\n \nstd::istream& operator>>(std::istream& is,P& p)\n{\n\tis >> p.real() >> p.imag();\n\treturn is;\n}\n \ntypedef vector<P> G,polygon;\nnamespace std {bool operator < (const P& a, const P& b) {return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);} }\ndouble cross(const P& a, const P& b) {return imag(conj(a)*b);}\ndouble dot(const P& a, const P& b) {return real(conj(a)*b);}\nstruct L : public vector<P> {L(const P &a, const P &b) {push_back(a); push_back(b);} };\nstruct C {P p; double r;C(const P &p, double r) : p(p), r(r) { } C(){} };\nint ccw(P a, P b, P c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > 0) return +1;\n\tif (cross(b, c) < 0) return -1;\n\tif (dot(b, c) < 0) return +2;\n\tif (norm(b) < norm(c)) return -2;\n\treturn 0;\n}\n \n// Check Funcs //\nbool intersectLL(const L &l, const L &m) {return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;}\nbool intersectLS(const L &l, const L &s) {return cross(l[1]-l[0], s[0]-l[0])*cross(l[1]-l[0], s[1]-l[0]) < EPS;}\nbool intersectLP(const L &l, const P &p) {return abs(cross(l[1]-p, l[0]-p)) < EPS;}\nbool intersectSS(const L &s, const L &t) {return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;}\nbool intersectSP(const L &s, const P &p) {return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;}\n \n// Where is Point in Polygon? //\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nenum { OUT, ON, IN };\nint contains(const polygon& P, const point& p) {\n\tbool in = false;\n\tfor (int i = 0; i < P.size(); ++i) {\n\t\tpoint a = curr(P,i) - p, b = next(P,i) - p;\n\t\tif (imag(a) > imag(b)) swap(a, b);\n\t\tif (imag(a) <= 0 && 0 < imag(b))\n\t\t\tif (cross(a, b) < 0) in = !in;\n\t\tif (cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n\t}\n\treturn in ? IN : OUT;\n}\n// Area of Polygon //\ndouble area2(const polygon& P) {\n\tdouble A = 0;\n\tfor (int i = 0; i < P.size(); ++i)A += cross(curr(P, i), next(P, i));\n\treturn A;\n}\n// Totsuhou! Andrew's Monotone Chain //\n \nvector<point> convex_hull(vector<point> ps) {\n int n = ps.size(), k = 0;\n sort(ps.begin(), ps.end());\n vector<point> ch(2*n);\n for (int i = 0; i < n; ch[k++] = ps[i++]) // lower-hull\n while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;\n for (int i = n-2, t = k+1; i >= 0; ch[k++] = ps[i--]) // upper-hull\n while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;\n ch.resize(k-1);\n return ch;\n}\n \nP projection(const L &l, const P &p) {double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);return l[0] + t*(l[0]-l[1]);}\nP reflection(const L &l, const P &p) {return p + 2.0 * (projection(l, p) - p);}\n \ndouble distanceLP(const L &l, const P &p) {\n\treturn abs(p - projection(l, p));\n}\ndouble distanceLL(const L &l, const L &m) {\n\treturn intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\ndouble distanceLS(const L &l, const L &s) {\n\tif (intersectLS(l, s)) return 0;\n\treturn min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\ndouble distanceSP(const L &s, const P &p) {\n\tconst P r = projection(s, p);\n\tif (intersectSP(s, r)) return abs(r - p);\n\treturn min(abs(s[0] - p), abs(s[1] - p));\n}\ndouble distanceSS(const L &s, const L &t) {\n\tif (intersectSS(s, t)) return 0;\n\treturn min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n \nP crosspoint(const L &l, const L &m) {\n\tdouble A = cross(l[1] - l[0], m[1] - m[0]);\n\tdouble B = cross(l[1] - l[0], l[1] - m[0]);\n\tif (abs(A) < EPS && abs(B) < EPS) return m[0];\n\treturn m[0] + B / A * (m[1] - m[0]);\n}\n \nvector<P> C_cp(C a,C b){\n\tvector<P> ret;\n\tdouble L = abs(a.p-b.p);\n \n\t/* anma dekitenai */\n\tif(\tL-a.r-b.r > EPS || (abs(a.p-b.p)<EPS && fabs(a.r-b.r)<EPS) || \n\t\tabs(a.p-b.p) < abs(a.r-b.r)\n\t)return ret;\n \n\tdouble theta = atan2(b.p.imag()-a.p.imag(),b.p.real()-a.p.real());\n\tdouble c = (L*L+a.r*a.r-b.r*b.r)/(2*L*a.r);\n\tret.push_back(\n\t\tP(a.p.real()+a.r*cos(theta+acos(c)),\n\t\t a.p.imag()+a.r*sin(theta+acos(c)))\n\t);\n\tif(fabs(L-a.r-b.r) > EPS)\n\t\tret.push_back(\n\t\t\tP(a.p.real()+a.r*cos(theta-acos(c)),\n\t\t\t a.p.imag()+a.r*sin(theta-acos(c)))\n\t\t);\n\treturn ret;\n}\n \n \nP getPedal(L l, P p){\n\tdouble A;\n\tif(abs(l[1].real()-l[0].real()) < EPS){\n\t\treturn P(l[1].real(),p.imag()); // important\n\t}else{\n\t\tA = (l[1].imag()-l[0].imag())/(l[1].real()-l[0].real());\n\t}\n\tdouble a = -A , b = 1 , c = A*l[0].real() - l[0].imag();\n\tdouble t = (a*p.real() + b*p.imag() + c)/(a*a+b*b);\n\treturn p-t * P(a,b);\n}\n \nvector<P> crosspointCL(const L l, const C c){\n\tvector<P> ret;\n\tP p = getPedal(l,c.p);\n\tif(\tabs(p-c.p) > c.r+EPS)return ret;\n\tP e = P((l[1]-l[0])/abs(l[1]-l[0]));\n\tdouble S = sqrt(c.r*c.r-abs(p-c.p)*abs(p-c.p));\n\tret.push_back(p+S*e);\n\tret.push_back(p-S*e);\n\treturn ret;\n}\n \nP getCircumcenter(P a,P b,P c){\n\tdouble A1 = 2 * ( b.real() - a.real() );\n\tdouble B1 = 2 * ( b.imag() - a.imag() );\n\tdouble C1 = pow(a.real(),2)-pow(b.real(),2) + pow(a.imag(),2)-pow(b.imag(),2);\n\tdouble A2 = 2 * ( c.real() - a.real() );\n\tdouble B2 = 2 * ( c.imag() - a.imag() );\n\tdouble C2 = pow(a.real(),2)-pow(c.real(),2) + pow(a.imag(),2)-pow(c.imag(),2);\n\tdouble X = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);\n\tdouble Y = (C1 * A2 - C2 * A1) / (A1 * B2 - A2 * B1);\n\treturn P(X,Y);\n}\n \ndouble AreaOfPolygon(vector<P> p){\n\tdouble S = 0 ; \n\tp.push_back(p[0]);\n\t/* 多角形の面積公式 (反時計回りの場合) */\n\tfor(int i = 0 ; i < p.size()-1 ; i++){\n\t\tS += (p[i].real() - p[i+1].real()) * (p[i].imag()+p[i+1].imag());\n\t}\n\tS /= 2.0;\n\treturn S;\n}\nvector<vector<L> > rect;\n\nvector<C> c;\ndouble seg(double y){\n\tL l = L(P(0,y),P(1,y));\n\tvector< pair<double,double> > segs;\n\tfor(int i = 0 ; i < c.size() ; i++){\n\t\tvector<P> cp = crosspointCL(l,c[i]);\n\t\tif( cp.size() ){\n\t\t\tdouble a = cp[0].real(), b = cp[1].real();\n\t\t\tif( a > b ) swap(a,b);\n\t\t\tsegs.push_back(mp(a,b));\n\t\t}\n\t}\n\tfor(int i = 0 ; i < rect.size() ; i++){\n\t\tdouble mx = -1e9;\n\t\tdouble mi = 1e9;\n\t\tfor(int j = 0 ; j < 4 ; j++){\n\t\t\tif( intersectLS(l,rect[i][j]) ){\n\t\t\t\tdouble x = crosspoint(rect[i][j],l).real();\n\t\t\t\tmx = max(mx,x);\n\t\t\t\tmi = min(mi,x);\n\t\t\t}\n\t\t}\n\t\tif( mi > mx ) continue;\n\t\t//cout << mi << \" \" << mx << endl;\n\t\tsegs.push_back(mp(mi,mx));\n\t}\n\tvector< pair<double,double> > tmpseg;\n\tfor(int i = 0 ; i < segs.size() ; i++){\n\t\tif( segs[i].second < -5 ) continue;\n\t\tif( segs[i].first > 5 ) continue;\n\t\tsegs[i].first = max(-5.,segs[i].first);\n\t\tsegs[i].second = min(5.,segs[i].second);\n\t\ttmpseg.push_back(segs[i]);\n\t}\n\tsegs = tmpseg;\n\t\n\tif( segs.size() == 0 ) return 0.0;\n\tsort(segs.begin(),segs.end());\n\tdouble st = segs[0].first;\n\tdouble en = segs[0].second;\n\tdouble ans = 0;\n\tfor(int i = 1 ; i < segs.size() ; i++){\n\n\t\tif( en < segs[i].first ){\n\t\t\tans += en - st;\n\t\t\tst = segs[i].first;\n\t\t\ten = segs[i].second;\n\t\t}else{\n\t\t\ten = max(en,segs[i].second);\n\t\t}\n\t}\n\tans += en - st;\n\treturn ans;\n}\nint main(){\n\tint N;\n\twhile(cin >> N && N){\n\t\tc.clear();\n\t\trect.clear();\n\t\tfor(int i = 0 ; i < N ; i++){\n\t\t\tP a,b;\n\t\t\tdouble r;\n\t\t\tcin >> a >> b >> r;\n\t\t\tP e = (b - a) / abs(b-a);\n\t\t\te = P(-e.imag(),e.real());\n\t\t\tvector<L> ln;\n\t\t\tP A = a + e * r;\n\t\t\tP B = a - e * r;\n\t\t\tP C = b - e * r;\n\t\t\tP D = b + e * r;\n\t\t\tln.push_back(L(A,B));\n\t\t\tln.push_back(L(B,C));\n\t\t\tln.push_back(L(C,D));\n\t\t\tln.push_back(L(D,A));\n\t\t\t\n\t\t\trect.push_back(ln);\n\t\t\tc.push_back(::C(a,r));\n\t\t\tc.push_back(::C(b,r));\n\t\t}\n\t\tdouble ans = 0;\n\t\tint D = 20000;\n\t\t//cout << seg(1.3) << endl;\n\t\t//return 0;\n\t\tfor(int i = 0 ; i < D ; i++){\n\t\t\tdouble y = -5 + 10. * i / D;\n\t\t\tans += seg(y) * (1./D);\n\t\t}\n\t\tprintf(\"%.10lf%c\",ans*10,10);\n\t}\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 1312, "score_of_the_acc": -0.4972, "final_rank": 1 } ]
aoj_2057_cpp
The Closest Circle You are given N non-overlapping circles in xy-plane. The radius of each circle varies, but the radius of the largest circle is not double longer than that of the smallest. Figure 1: The Sample Input The distance between two circles C 1 and C 2 is given by the usual formula where ( x i , y i ) is the coordinates of the center of the circle C i , and r i is the radius of C i , for i = 1, 2. Your task is to write a program that finds the closest pair of circles and print their distance. Input The input consists of a series of test cases, followed by a single line only containing a single zero, which indicates the end of input. Each test case begins with a line containing an integer N (2 ≤ N ≤ 100000), which indicates the number of circles in the test case. N lines describing the circles follow. Each of the N lines has three decimal numbers R , X , and Y . R represents the radius of the circle. X and Y represent the x - and y -coordinates of the center of the circle, respectively. Output For each test case, print the distance between the closest circles. You may print any number of digits after the decimal point, but the error must not exceed 0.00001. Sample Input 4 1.0 0.0 0.0 1.5 0.0 3.0 2.0 4.0 0.0 1.0 3.0 4.0 0 Output for the Sample Input 0.5
[ { "submission_id": "aoj_2057_10946135", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#define ele int\nusing namespace std;\n#define maxn 100010\n#define INF 1e18\n#define eps 1e-7\nstruct circle{\n\tdouble x,y,r;\n};\nele n;\ndouble R;\ncircle a[maxn];\ninline bool cmp1(circle a,circle b){\n\treturn a.x<b.x;\n}\ninline bool cmp2(circle a,circle b){\n\treturn a.y<b.y;\n}\ninline double dis(circle a,circle b){\n\treturn sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))-a.r-b.r;\n}\ndouble solve(ele l,ele r){\n\tif (r-l+1<=2){\n\t\tdouble ans=INF;\n\t\tfor (int i=l; i<=r; ++i)\n\t\t\tfor (int j=i+1; j<=r; ++j)\n\t\t\t\tans=min(ans,dis(a[i],a[j]));\n\t\treturn ans;\n\t}\n\tele mid=(l+r)>>1;\n\tdouble ans=min(solve(l,mid-1),solve(mid+1,r));\n\tfor (int i=l; i<=r; ++i)\n\t\tif (i!=mid) ans=min(ans,dis(a[i],a[mid]));\n\tstatic circle p[maxn],q[maxn];\n\tele u=0,v=0;\n\tfor (int i=l; i<mid; ++i)\n\t\tif (a[mid].x-a[i].x<ans+R*2+eps) p[u++]=a[i];\n\tfor (int i=mid+1; i<=r; ++i)\n\t\tif (a[i].x-a[mid].x<ans+R*2+eps) q[v++]=a[i];\n\tif (!v) return ans;\n\tsort(p,p+u,cmp2); sort(q,q+v,cmp2);\n\tele s=0,t=0;\n\tfor (int i=0; i<u; ++i){\n\t\twhile (s<v-1 && p[i].y-q[s].y>ans+R*2+eps) ++s;\n\t\twhile (t<v-1 && q[t+1].y-p[i].y<ans+R*2+eps) ++t;\n\t\tfor (int j=s; j<=t; ++j) ans=min(ans,dis(p[i],q[j]));\n\t}\n\treturn ans;\n}\nint main(){\n\twhile (scanf(\"%d\",&n),n){\n\t\tR=0;\n\t\tfor (int i=0; i<n; ++i){\n\t\t\tscanf(\"%lf%lf%lf\",&a[i].r,&a[i].x,&a[i].y);\n\t\t\tR=max(R,a[i].r);\n\t\t}\n\t\tsort(a,a+n,cmp1);\n\t\tprintf(\"%.10lf\\n\",solve(0,n-1));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 9072, "score_of_the_acc": -0.3064, "final_rank": 9 }, { "submission_id": "aoj_2057_7290550", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\nusing namespace std;\n\nint n;\nconst double INF = 1e9;\ndouble pr, px, py;\n\nstruct circle {\n\tdouble r, x, y;\n\n\tbool operator < (const circle& rhs) const\n\t{\n\t\treturn x < rhs.x || (x == rhs.x && y < rhs.y);\n\t}\n};\n\ncircle a[100000];\n\nbool compare_y(circle a, circle b) {\n\treturn a.y < b.y;\n}\n\ndouble closest_pair(circle* a, int n) {\n\tif (n <= 1)\n\t\treturn INF;\n\n\tint m = n / 2;\n\tdouble x = a[m].x, r = a[m].r;\n\tdouble d = min(closest_pair(a, m), closest_pair(a + m, n - m));\n\tinplace_merge(a, a + m, a + n, compare_y);\n\n\tvector<circle> b;\n\n\tfor (int i = 0; i < n; i++) {\n\t\tif (fabs(a[i].x - x) >= d + r + a[i].r)\n\t\t\tcontinue;\n\n\t\tint w = b.size();\n\n\t\tfor (int j = 0; j < w; j++) {\n\t\t\tdouble dx = a[i].x - b[w - j - 1].x;\n\t\t\tdouble dy = a[i].y - b[w - j - 1].y;\n\n\t\t\tif (dy >= d + r + a[i].r)\n\t\t\t\tbreak;\n\n\t\t\td = min(d, sqrt(dx * dx + dy * dy) - a[i].r - b[w - j - 1].r);\n\t\t}\n\n\t\tb.push_back(a[i]);\n\t}\n\n\treturn d;\n}\n\nint main()\n{\n\tcin >> n;\n\n\twhile (n) {\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcin >> pr >> px >> py;\n\t\t\tcircle w = { pr, px, py };\n\t\t\ta[i] = w;\n\t\t}\n\n\t\tsort(a, a + n);\n\n\t\tcout << setiosflags(ios::fixed | ios::showpoint) << setprecision(6)\n\t\t\t<< closest_pair(a, n) << endl;\n\n\t\tcin >> n;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 10800, "score_of_the_acc": -0.4444, "final_rank": 14 }, { "submission_id": "aoj_2057_6894832", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\n#include <cmath>\n#include <iomanip>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct point {\n double x, y, r;\n};\n\nconst ll inf = 1e18;\n\nvoid chmin(double& x, double y) { x = min(x, y); }\n\ndouble dist(point& a, point& b) {\n return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)) - a.r - b.r;\n}\n\ndouble closestpair(vi<point>& d, int l, int r, const double mxr) {\n if (r - l == 1) return inf;\n const int mid = (l + r) / 2;\n point m = d[mid];\n double vl = closestpair(d, l, mid, mxr);\n double vr = closestpair(d, mid, r, mxr);\n double ans = min(vl, vr);\n inplace_merge(d.begin() + l, d.begin() + mid, d.begin() + r, \n [&](point a, point b) {return a.y < b.y; });\n\n vi<point> midreg;\n for (int i = l; i < r; i++) {\n if (abs(d[i].x - m.x) >= ans + mxr * 2) continue;\n int len = midreg.size();\n for (int j = len - 1; j >= 0; j--) {\n if (d[i].y - midreg[j].y >= ans + mxr * 2) break;\n chmin(ans, dist(d[i], midreg[j]));\n }\n midreg.push_back(d[i]);\n }\n return ans;\n}\n\nint main()\n{\n int n;\n while (cin >> n, n) {\n vi<point> d(n);\n rep(i, n) cin >> d[i].r >> d[i].x >> d[i].y;\n double mxr = 0;\n rep(i, n) mxr = max(mxr, d[i].r);\n sort(d.begin(), d.end(), [&](point a, point b) {return a.x < b.x; });\n double res = closestpair(d, 0, n, mxr);\n cout << fixed << setprecision(12) << res << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 11336, "score_of_the_acc": -0.4577, "final_rank": 15 }, { "submission_id": "aoj_2057_5146090", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nusing pii = pair<int, int>;\nusing vvl = vector<vector<ll>>;\nusing vvi = vector<vector<int>>;\nusing vvpll = vector<vector<pll>>;\n#define rep(i, a, b) for (ll i=(a); i<(b); i++)\n#define rrep(i, a, b) for (ll i=(a); i>(b); i--)\n#define pb push_back\n#define tostr to_string\n#define ALL(A) A.begin(), A.end()\n#define elif else if\n// constexpr ll INF = LONG_LONG_MAX;\nconstexpr ll INF = 1e18;\nconstexpr ll MOD = 1000000007;\n\nconst string digits = \"0123456789\";\nconst string ascii_lowercase = \"abcdefghijklmnopqrstuvwxyz\";\nconst string ascii_uppercase = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string ascii_letters = ascii_lowercase + ascii_uppercase;\n\ntemplate<typename T> vector<vector<T>> list2d(int N, int M, T init) { return vector<vector<T>>(N, vector<T>(M, init)); }\ntemplate<typename T> vector<vector<vector<T>>> list3d(int N, int M, int L, T init) { return vector<vector<vector<T>>>(N, vector<vector<T>>(M, vector<T>(L, init))); }\ntemplate<typename T> vector<vector<vector<vector<T>>>> list4d(int N, int M, int L, int O, T init) { return vector<vector<vector<vector<T>>>>(N, vector<vector<vector<T>>>(M, vector<vector<T>>(L, vector<T>(O, init)))); }\n\nvector<ll> LIST(ll N) { vector<ll> A(N); rep(i, 0, N) cin >> A[i]; return A; }\n\nvoid print(ld out) { cout << fixed << setprecision(15) << out << '\\n'; }\nvoid print(double out) { cout << fixed << setprecision(15) << out << '\\n'; }\ntemplate<typename T> void print(T out) { cout << out << '\\n'; }\ntemplate<typename T1, typename T2> void print(pair<T1, T2> out) { cout << out.first << ' ' << out.second << '\\n'; }\ntemplate<typename T> void print(vector<T> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size()-1 ? '\\n' : ' '); } }\ntemplate<typename T> void print(set<T> S) { vector<T> A(S.begin(), S.end()); print(A); }\n\nvoid Yes() { print(\"Yes\"); }\nvoid No() { print(\"No\"); }\nvoid YES() { print(\"YES\"); }\nvoid NO() { print(\"NO\"); }\n\nll floor(ll a, ll b) { if (a < 0) { return (a-b+1) / b; } else { return a / b; } }\nll ceil(ll a, ll b) { if (a >= 0) { return (a+b-1) / b; } else { return a / b; } }\npll divmod(ll a, ll b) { ll d = a / b; ll m = a % b; return {d, m}; }\ntemplate<typename T> bool chmax(T &x, T y) { return (y > x) ? x = y, true : false; }\ntemplate<typename T> bool chmin(T &x, T y) { return (y < x) ? x = y, true : false; }\n\ntemplate<typename T> T sum(vector<T> A) { T res = 0; for (T a: A) res += a; return res; }\ntemplate<typename T> T max(vector<T> A) { return *max_element(ALL(A)); }\ntemplate<typename T> T min(vector<T> A) { return *min_element(ALL(A)); }\n\nll toint(string s) { ll res = 0; for (char c : s) { res *= 10; res += (c - '0'); } return res; }\nint toint(char num) { return num - '0'; }\nchar tochar(int num) { return '0' + num; }\nint ord(char c) { return (int)c; }\nchar chr(int a) { return (char)a; }\n\nll pow(ll x, ll n) { ll res = 1; rep(_, 0, n) res *= x; return res; }\nll pow(ll x, ll n, int mod) { ll res = 1; while (n > 0) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; }\n\nint popcount(ll S) { return __builtin_popcountll(S); }\nll gcd(ll a, ll b) { return __gcd(a, b); }\nll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }\n\ntemplate<typename T> int bisect_left(vector<T> &A, T val) { return lower_bound(ALL(A), val) - A.begin(); }\ntemplate<typename T> int bisect_right(vector<T> &A, T val) { return upper_bound(ALL(A), val) - A.begin(); }\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll N;\n cin >> N;\n if (N == 0) break;\n\n vector<tuple<ld, ld, ld>> XY;\n rep(i, 0, N) {\n ld x, y, r;\n cin >> r >> x >> y;\n XY.pb({x, y, r});\n }\n sort(ALL(XY));\n\n auto rec = [&](auto&& f, vector<tuple<ld, ld, ld>> XY) -> ld {\n ll n = XY.size();\n if (n <= 1) return INF;\n ll m = n/2;\n ld l = get<0>(XY[m]);\n ld d = min(f(f, vector<tuple<ld, ld, ld>>(XY.begin(), XY.begin()+m)), \n f(f, vector<tuple<ld, ld, ld>>(XY.begin()+m, XY.end())));\n\n sort(ALL(XY), [](tuple<ld, ld, ld> a, tuple<ld, ld, ld> b) { return get<1>(a) < get<1>(b); });\n ld mxr = 0;\n for (auto& [x, y, r] : XY) chmax(mxr, r);\n rep(i, 0, n) {\n auto [x1, y1, r1] = XY[i];\n if (abs(l-x1)-mxr*2 >= d) continue;\n rep(j, i+1, n) {\n auto [x2, y2, r2] = XY[j];\n if (abs(y1-y2)-mxr*2 >= d) break;\n chmin(d, sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))-r1-r2);\n }\n }\n return d;\n };\n ld ans = rec(rec, XY);\n print(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 22224, "score_of_the_acc": -1.0443, "final_rank": 20 }, { "submission_id": "aoj_2057_3167870", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define EPS 1e-10\ntypedef pair<double,int> P;\nstruct Point{double x,y,r;};\nint n;\nPoint p[100010];\n\ndouble cl(int i,int j,double k=0.0){\n return sqrt(pow(p[i].x-p[j].x,2)+pow(p[i].y-p[j].y,2))-(p[i].r+p[j].r+k*2.0);\n}\n\nint solve(double k){\n \n vector<P> events;\n \n for(int i=0;i<n;i++){\n double x1=p[i].x-p[i].r-k;\n double x2=p[i].x+p[i].r+k;\n events.push_back(P(x1,i));\n events.push_back(P(x2,i+n));\n }\n sort(events.begin(),events.end());\n\n set<P> outers;\n for(int i=0;i<events.size();i++){\n int id=events[i].second%n;\n if(events[i].second<n){\n set<P>::iterator it = outers.lower_bound(P(p[id].y,id));\n if(it!=outers.end()&&cl(id,it->second,k)<=0)return 0;\n if(it!=outers.begin()&&cl(id,(--it)->second,k)<=0)return 0;\n outers.insert(P(p[id].y,id));\n }else{\n outers.erase(P(p[id].y,id));\n }\n }\n return 1;\n}\n\nint main(){\n \n while(cin>>n,n){\n\n for(int i=0;i<n;i++){\n cin>>p[i].r>>p[i].x>>p[i].y;\n }\n\n double ok=0,ng=cl(0,1),mid;\n while(ng-ok>EPS){\n mid=(ok+ng)/2.0;\n if(solve(mid))ok=mid;\n else ng=mid;\n }\n \n printf(\"%.10lf\\n\",ok*2.0);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2980, "memory_kb": 14704, "score_of_the_acc": -1.0342, "final_rank": 19 }, { "submission_id": "aoj_2057_3047498", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <iomanip>\n#include <cmath>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e18;\ntypedef complex<double> P;\n#define X real()\n#define Y imag()\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\nbool xcomp(const C &a, const C &b){\n return (a.p.X != b.p.X)? a.p.X<b.p.X : a.p.Y<b.p.Y;\n}\nbool ycomp(const C &a, const C &b){\n return (a.p.Y != b.p.Y)? a.p.Y<b.p.Y : a.p.X<b.p.X;\n}\n\ndouble closest(vector<C> &c, int first, int last, double rmax){\n if(last -first <= 1) return INF;\n int mid = (first +last)/2;\n double midx = c[mid].p.X;\n double d = min(closest(c, first, mid, rmax), closest(c, mid, last, rmax));\n inplace_merge(c.begin()+first, c.begin()+mid, c.begin()+last, ycomp);\n\n vector<C> near;\n for(int i=first; i<last; i++){\n if(abs(c[i].p.X -midx) -c[i].r -rmax > d +EPS) continue;\n for(int j=near.size()-1; j>=0; j--){\n if(abs(c[i].p.Y -near[j].p.Y) -c[i].r -rmax > d +EPS){\n break;\n }\n d = min(d, abs(c[i].p -near[j].p) -c[i].r -near[j].r);\n }\n near.push_back(c[i]);\n }\n return d;\n}\n \nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n vector<C> c(n);\n double rmax = 0;\n for(int i=0; i<n; i++){\n double r,x,y;\n cin >> r >> x >> y;\n c[i] = C(P(x, y), r);\n rmax = max(rmax, r);\n }\n sort(c.begin(), c.end(), xcomp);\n\n cout << fixed << setprecision(10);\n cout << closest(c, 0, n, rmax) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 10736, "score_of_the_acc": -0.444, "final_rank": 13 }, { "submission_id": "aoj_2057_2812321", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\t\tif(x != arg.x){\n\t\t\treturn x < arg.x;\n\t\t}else{\n\t\t\treturn y < arg.y;\n\t\t}\n\t}\n\tdouble x,y,r;\n};\n\nint N;\nInfo info[100001];\n\nbool compare_y(Info left,Info right){\n\n\treturn left.y < right.y;\n\n}\n\ndouble calc_dist(int a,int b){\n\treturn sqrt((info[a].x-info[b].x)*(info[a].x-info[b].x)+(info[a].y-info[b].y)*(info[a].y-info[b].y))-(info[a].r+info[b].r);\n}\n\ndouble calc_y_dist(int a,int b){\n\treturn (info[b].y-info[b].r)-(info[a].y+info[a].r);\n}\n\ndouble closest_pair(Info* array,int tmp_N){\n\tif(tmp_N <= 1)return DBL_MAX;\n\n\tint mid = tmp_N/2;\n\n\tdouble x = array[mid].x;\n\tdouble dist = min(closest_pair(array,mid),closest_pair(array+mid,tmp_N-mid));\n\tinplace_merge(array,array+mid,array+tmp_N,compare_y);\n\n\tvector<int> V;\n\n\n\tint left,right,m,search_left;\n\n\tfor(int i = 0; i < tmp_N; i++){\n\n\t\tfor(int k = 0; k < min((int)V.size(),2); k++){\n\t\t\tdist = min(dist,calc_dist(V[V.size()-1-k],i));\n\t\t}\n\n\t\tV.push_back(i);\n\t}\n\treturn dist;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%lf %lf %lf\",&info[i].r,&info[i].x,&info[i].y);\n\t}\n\tsort(info,info+N);\n\n\tprintf(\"%.10lf\\n\",closest_pair(info,N));\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 6924, "score_of_the_acc": -0.2011, "final_rank": 5 }, { "submission_id": "aoj_2057_2812122", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\t\tif(x != arg.x){\n\t\t\treturn x < arg.x;\n\t\t}else{\n\t\t\treturn y < arg.y;\n\t\t}\n\t}\n\tdouble x,y,r;\n};\n\nint N;\nInfo info[100001];\n\nbool compare_y(Info left,Info right){ //順序付け関数\n\n\treturn left.y < right.y;\n\n}\n\nbool is_OK(int index,double x,double dist){\n\tif(info[index].x < x){ //基準線より中心が左\n\t\tif(info[index].x+info[index].r+dist < x){\n\t\t\treturn false;\n\t\t}\n\n\t}else{ //基準線より中心が右\n\t\tif(info[index].x-info[index].r-dist > x){\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\ndouble calc_dist(int a,int b){\n\treturn sqrt((info[a].x-info[b].x)*(info[a].x-info[b].x)+(info[a].y-info[b].y)*(info[a].y-info[b].y))-(info[a].r+info[b].r);\n}\n\ndouble calc_y_dist(int a,int b){\n\treturn (info[b].y-info[b].r)-(info[a].y+info[a].r);\n}\n\ndouble closest_pair(Info* array,int tmp_N){\n\tif(tmp_N <= 1)return DBL_MAX;\n\n\tint mid = tmp_N/2; //中央のインデックス\n\n\tdouble x = array[mid].x; //基準線x\n\tdouble dist = min(closest_pair(array,mid),closest_pair(array+mid,tmp_N-mid)); //集合を2分割し、再帰的に最小距離を求める\n\tinplace_merge(array,array+mid,array+tmp_N,compare_y); //2つのソート済の範囲をマージ\n\n\tvector<int> V; //★★再帰毎にメモリを確保(indexをpush)★★\n\n\n\tint left,right,m,search_left;\n\n\tfor(int i = 0; i < tmp_N; i++){\n\t\t//if(!is_OK(i,x,dist))continue; //基準線より最小距離以上離れている点は無視(★異なる領域間での、新最小値を作り得ないということ★)\n\n\t\t//★★Vはyの昇順に並んでいる:自分よりy座標が小さいもののみ、入っている★★\n\n\t\t//2分探索で、検索範囲を定める\n\t\t/*left = 0,right = V.size()-1,m = (left+right)/2;\n\t\tsearch_left = 0;\n\n\t\twhile(left <= right){\n\t\t\tif(calc_y_dist(V[m],i) < dist){ //y座標の差がdist未満\n\t\t\t\tsearch_left = m;\n\t\t\t\tright = m-1; //より左へ\n\t\t\t}else{\n\t\t\t\tleft = m+1; //より右へ\n\t\t\t}\n\t\t\tm = (left+right)/2;\n\t\t}*/\n\n\t\t/*for(int k = search_left; k < V.size(); k++){\n\t\t\tdist = min(dist,calc_dist(V[k],i));\n\t\t}*/\n\n\t\tfor(int k = 0; k < min((int)V.size(),2); k++){\n\t\t\tdist = min(dist,calc_dist(V[V.size()-1-k],i));\n\t\t}\n\n\t\tV.push_back(i); //可能性あり集合に、iを追加\n\t}\n\treturn dist;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%lf %lf %lf\",&info[i].r,&info[i].x,&info[i].y);\n\t}\n\tsort(info,info+N);\n\n\tprintf(\"%.10lf\\n\",closest_pair(info,N));\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 6936, "score_of_the_acc": -0.2017, "final_rank": 6 }, { "submission_id": "aoj_2057_2646312", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n\n\n#define EPS (1e-10)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define PI 3.141592653589793238\n \n// COUNTER CLOCKWISE\nstatic const int CCW_COUNTER_CLOCKWISE = 1;\nstatic const int CCW_CLOCKWISE = -1;\nstatic const int CCW_ONLINE_BACK = 2;\nstatic const int CCW_ONLINE_FRONT = -2;\nstatic const int CCW_ON_SEGMENT = 0;\n\n//Intercsect Circle & Circle\nstatic const int ICC_SEPERATE = 4;\nstatic const int ICC_CIRCUMSCRIBE = 3;\nstatic const int ICC_INTERSECT = 2;\nstatic const int ICC_INSCRIBE = 1;\nstatic const int ICC_CONTAIN = 0;\n\nstruct Point{\n double x,y;\n Point(){}\n Point(double x,double y) :x(x),y(y){}\n Point operator+(Point p) {return Point(x+p.x,y+p.y);}\n Point operator-(Point p) {return Point(x-p.x,y-p.y);}\n Point operator*(double k){return Point(x*k,y*k);}\n Point operator/(double k){return Point(x/k,y/k);}\n double norm(){return x*x+y*y;}\n double abs(){return sqrt(norm());}\n\n bool operator < (const Point &p) const{\n return x!=p.x?x<p.x:y<p.y;\n //grid-point only\n //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0;\n }\n\n bool operator == (const Point &p) const{\n return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;\n }\n};\n\nistream &operator >> (istream &is,Point &p){\n is>>p.x>>p.y;\n return is;\n}\n\nostream &operator << (ostream &os,Point p){\n os<<fixed<<setprecision(12)<<p.x<<\" \"<<p.y;\n return os;\n}\n\nbool sort_x(Point a,Point b){\n return a.x!=b.x?a.x<b.x:a.y<b.y;\n}\n\nbool sort_y(Point a,Point b){\n return a.y!=b.y?a.y<b.y:a.x<b.x;\n}\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nistream &operator >> (istream &is,Polygon &p){\n for(int i=0;i<(int)p.size();i++) cin>>p[i];\n return is;\n}\n\nstruct Segment{\n Point p1,p2;\n Segment(){}\n Segment(Point p1, Point p2):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nistream &operator >> (istream &is,Segment &s){\n is>>s.p1>>s.p2;\n return is;\n}\n\nstruct Circle{\n Point c;\n double r;\n Circle(){}\n Circle(Point c,double r):c(c),r(r){}\n};\n\nistream &operator >> (istream &is,Circle &c){\n is>>c.c>>c.r;\n return is;\n}\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0); \n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\ndouble arg(Vector p){\n return atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n return Point(cos(r)*a,sin(r)*a);\n}\n\nint ccw(Point p0,Point p1,Point p2);\nbool intersectSS(Point p1,Point p2,Point p3,Point p4);\nbool intersectSS(Segment s1,Segment s2);\nbool intersectPS(Polygon p,Segment l);\nint intersectCC(Circle c1,Circle c2);\nbool intersectSC(Segment s,Circle c);\ndouble getDistanceLP(Line l,Point p);\ndouble getDistanceSP(Segment s,Point p);\ndouble getDistanceSS(Segment s1,Segment s2);\nPoint getCrossPointSS(Segment s1,Segment s2);\nPoint getCrossPointLL(Line l1,Line l2);\nPolygon getCrossPointCL(Circle c,Line l);\nPolygon getCrossPointCC(Circle c1,Circle c2);\nint contains(Polygon g,Point p);\nPolygon andrewScan(Polygon s);\nPolygon convex_hull(Polygon ps);\ndouble diameter(Polygon s);\nbool isConvex(Polygon p);\ndouble area(Polygon s);\nPolygon convexCut(Polygon p,Line l);\nLine bisector(Point p1,Point p2);\nVector translate(Vector v,double theta);\nvector<Line> corner(Line l1,Line l2);\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a = p1-p0;\n Vector b = p2-p0;\n if(cross(a,b) > EPS) return CCW_COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS) return CCW_CLOCKWISE;\n if(dot(a,b) < -EPS) return CCW_ONLINE_BACK;\n if(a.norm()<b.norm()) return CCW_ONLINE_FRONT;\n return CCW_ON_SEGMENT;\n}\n\nbool intersectSS(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 &&\n\t ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0 );\n}\n\nbool intersectSS(Segment s1,Segment s2){\n return intersectSS(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool intersectPS(Polygon p,Segment l){\n int n=p.size();\n for(int i=0;i<n;i++)\n if(intersectSS(Segment(p[i],p[(i+1)%n]),l)) return 1;\n return 0;\n}\n\nint intersectCC(Circle c1,Circle c2){\n if(c1.r<c2.r) swap(c1,c2);\n double d=abs(c1.c-c2.c);\n double r=c1.r+c2.r;\n if(equals(d,r)) return ICC_CIRCUMSCRIBE;\n if(d>r) return ICC_SEPERATE;\n if(equals(d+c2.r,c1.r)) return ICC_INSCRIBE;\n if(d+c2.r<c1.r) return ICC_CONTAIN;\n return ICC_INTERSECT;\n}\n\nbool intersectSC(Segment s,Circle c){\n double d=getDistanceSP(s,c.c);\n return d<=c.r;\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistanceSS(Segment s1,Segment s2){\n if(intersectSS(s1,s2)) return 0.0;\n return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),\n\t min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));\n}\n\nPoint getCrossPointSS(Segment s1,Segment s2){\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}\n\nPoint getCrossPointLL(Line l1,Line l2){\n double a=cross(l1.p2-l1.p1,l2.p2-l2.p1);\n double b=cross(l1.p2-l1.p1,l1.p2-l2.p1);\n if(abs(a)<EPS&&abs(b)<EPS) return l2.p1;\n return l2.p1+(l2.p2-l2.p1)*(b/a);\n}\n\nPolygon getCrossPointCL(Circle c,Line l){\n Polygon p(2);\n Vector pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n p[0]=pr+e*base;\n p[1]=pr-e*base;\n return p;\n}\n\nPolygon getCrossPointCC(Circle c1,Circle c2){\n Polygon p(2);\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n p[0]=c1.c+polar(c1.r,t+a);\n p[1]=c1.c+polar(c1.r,t-a);\n return p;\n}\n\n// IN:2 ON:1 OUT:0\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS && dot(a,b) < EPS) return 1;\n if(a.y>b.y) swap(a,b);\n if(a.y < EPS && EPS < b.y && cross(a,b) > EPS ) x = !x;\n }\n return (x?2:0);\n}\n\nPolygon andrewScan(Polygon s){\n Polygon u,l;\n if(s.size()<3) return s;\n sort(s.begin(),s.end());\n u.push_back(s[0]);\n u.push_back(s[1]);\n l.push_back(s[s.size()-1]);\n l.push_back(s[s.size()-2]);\n for(int i=2;i<(int)s.size();i++){\n for(int n=u.size();n>=2&&ccw(u[n-2],u[n-1],s[i])!=CCW_CLOCKWISE;n--){\n u.pop_back();\n }\n u.push_back(s[i]);\n } \n for(int i=s.size()-3;i>=0;i--){\n for(int n=l.size();n>=2&&ccw(l[n-2],l[n-1],s[i])!=CCW_CLOCKWISE;n--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n reverse(l.begin(),l.end());\n for(int i=u.size()-2;i>=1;i--) l.push_back(u[i]);\n return l;\n} \n\nPolygon convex_hull(Polygon ps){\n int n=ps.size();\n sort(ps.begin(),ps.end(),sort_y);\n int k=0;\n Polygon qs(n*2);\n for(int i=0;i<n;i++){\n while(k>1&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0) k--;\n qs[k++]=ps[i];\n }\n for(int i=n-2,t=k;i>=0;i--){\n while(k>t&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0) k--;\n qs[k++]=ps[i];\n }\n qs.resize(k-1);\n return qs;\n}\n\ndouble diameter(Polygon s){\n Polygon p=s;\n int n=p.size();\n if(n==2) return abs(p[0]-p[1]);\n int i=0,j=0;\n for(int k=0;k<n;k++){\n if(p[i]<p[k]) i=k;\n if(!(p[j]<p[k])) j=k;\n }\n double res=0;\n int si=i,sj=j;\n while(i!=sj||j!=si){\n res=max(res,abs(p[i]-p[j]));\n if(cross(p[(i+1)%n]-p[i],p[(j+1)%n]-p[j])<0.0){\n i=(i+1)%n;\n }else{\n j=(j+1)%n;\n }\n }\n return res;\n}\n\nbool isConvex(Polygon p){\n bool f=1;\n int n=p.size();\n for(int i=0;i<n;i++){\n int t=ccw(p[(i+n-1)%n],p[i],p[(i+1)%n]);\n f&=t!=CCW_CLOCKWISE;\n }\n return f;\n}\n\ndouble area(Polygon s){\n double res=0;\n for(int i=0;i<(int)s.size();i++){\n res+=cross(s[i],s[(i+1)%s.size()])/2.0;\n }\n return abs(res);\n}\n\ndouble area(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n if(c1.r+c2.r<=d+EPS) return 0;\n if(d<=abs(c1.r-c2.r)){\n double r=min(c1.r,c2.r);\n return PI*r*r;\n }\n double rc=(d*d+c1.r*c1.r-c2.r*c2.r)/(2*d);\n double th=acos(rc/c1.r);\n double ph=acos((d-rc)/c2.r);\n return c1.r*c1.r*th+c2.r*c2.r*ph-d*c1.r*sin(th);\n}\n\nPolygon convexCut(Polygon p,Line l){\n Polygon q;\n for(int i=0;i<(int)p.size();i++){\n Point a=p[i],b=p[(i+1)%p.size()];\n if(ccw(l.p1,l.p2,a)!=-1) q.push_back(a);\n if(ccw(l.p1,l.p2,a)*ccw(l.p1,l.p2,b)<0)\n q.push_back(getCrossPointLL(Line(a,b),l));\n }\n return q;\n}\n\nLine bisector(Point p1,Point p2){\n Circle c1=Circle(p1,abs(p1-p2)),c2=Circle(p2,abs(p1-p2));\n Polygon p=getCrossPointCC(c1,c2);\n if(cross(p2-p1,p[0]-p1)>0) swap(p[0],p[1]);\n return Line(p[0],p[1]);\n}\n\nVector translate(Vector v,double theta){\n Vector res;\n res.x=cos(theta)*v.x-sin(theta)*v.y;\n res.y=sin(theta)*v.x+cos(theta)*v.y;\n return res;\n}\n\nvector<Line> corner(Line l1,Line l2){\n vector<Line> res;\n if(isParallel(l1,l2)){\n double d=getDistanceLP(l1,l2.p1)/2.0;\n Vector v1=l1.p2-l1.p1;\n v1=v1/v1.abs()*d;\n Point p=l2.p1+translate(v1,90.0*(PI/180.0));\n double d1=getDistanceLP(l1,p);\n double d2=getDistanceLP(l2,p);\n if(abs(d1-d2)>d){\n p=l2.p1+translate(v1,-90.0*(PI/180.0));\n }\n res.push_back(Line(p,p+v1));\n }else{\n Point p=getCrossPointLL(l1,l2);\n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n v1=v1/v1.abs();\n v2=v2/v2.abs();\n res.push_back(Line(p,p+(v1+v2)));\n res.push_back(Line(p,p+translate(v1+v2,90.0*(PI/180.0))));\n }\n return res;\n}\n\nPolygon tangent(Circle c1,Point p2){\n Circle c2=Circle(p2,sqrt(norm(c1.c-p2)-c1.r*c1.r));\n Polygon p=getCrossPointCC(c1,c2);\n sort(p.begin(),p.end());\n return p;\n}\n\n\ntemplate<typename T>\nvoid chmin(T &a,T b){if(a>b) a=b;}\ntemplate<typename T>\nvoid chmax(T &a,T b){if(a<b) a=b;}\n\n\nsigned main(){\n int n;\n while(cin>>n,n){\n vector<Circle> v(n);\n for(int i=0;i<n;i++) cin>>v[i].r>>v[i].c;\n auto calc=[](Circle a,Circle b){\n return abs(a.c-b.c)-(a.r+b.r);\n };\n double ans=calc(v[0],v[1]);\n for(int k=0;k<10;k++){\n sort(v.begin(),v.end(),[](Circle a,Circle b){return sort_x(a.c,b.c);});\n for(int i=0;i<n-1;i++) chmin(ans,calc(v[i],v[i+1]));\n sort(v.begin(),v.end(),[](Circle a,Circle b){return sort_y(a.c,b.c);});\n for(int i=0;i<n-1;i++) chmin(ans,calc(v[i],v[i+1]));\n for(int i=0;i<n;i++) v[i].c=translate(v[i].c,PI/100);\n }\n cout<<fixed<<setprecision(12)<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 690, "memory_kb": 5708, "score_of_the_acc": -0.2049, "final_rank": 7 }, { "submission_id": "aoj_2057_2645155", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\n \ntypedef pair<double,double> P;\ntypedef pair<P,double> P2;\n \nP rot(P p, double a){\n a=a*M_PI/180.0;\n return P(p.first*cos(a)+p.second*(-sin(a)),\n p.first*sin(a)+p.second*cos(a));\n}\n \ndouble abss(P a,P b){\n return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second));\n}\n \ndouble x,y,r;int tt;\nvector<P2>v;\nint main(){\n \n int n;\n while(cin >> n,n){\n \n double ans=1e13;\n \n v.clear();\n \n r(i,n){\n cin>>r>>x>>y;\n v.push_back(P2(P(x,y),r));\n }\n \n r(i,1){\n sort(v.begin(),v.end());\n r(j,n){\n for(int k=j+1,c=0;k<n;k++,c++){\n if(c>50)break;\n ans=min(ans,abss(v[k].first,v[j].first)-v[k].second-v[j].second);\n }\n }\n r(j,n)v[i].first=rot(v[i].first,0.5);\n }\n \n printf(\"%.9f\\n\",ans);\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 6220, "score_of_the_acc": -0.1971, "final_rank": 4 }, { "submission_id": "aoj_2057_2644955", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\n\ntypedef pair<double,double> P;\ntypedef pair<P,double> P2;\n\nP rot(P p, double a){\n a=a*M_PI/180.0;\n return P(p.first*cos(a)+p.second*(-sin(a)),\n p.first*sin(a)+p.second*cos(a));\n}\n\ndouble abss(P a,P b){\n return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second));\n}\n\ndouble x,y,r;int tt;\nvector<P2>v;\nint main(){\n\n int n;\n while(cin >> n,n){\n\n double ans=1e13;\n \n v.clear();\n \n r(i,n){\n cin>>r>>x>>y;\n v.push_back(P2(P(x,y),r));\n }\n\n r(i,1){\n sort(v.begin(),v.end());\n r(j,n){\n\tfor(int k=j+1,c=0;k<n;k++,c++){\n\t if(c>50)break;\n\t ans=min(ans,abss(v[k].first,v[j].first)-v[k].second-v[j].second);\n\t}\n }\n r(j,n)v[i].first=rot(v[i].first,0.5);\n }\n\n printf(\"%.9f\\n\",ans);\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 6212, "score_of_the_acc": -0.1967, "final_rank": 3 }, { "submission_id": "aoj_2057_2589403", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#define ele int\nusing namespace std;\n#define maxn 100010\n#define INF 1e18\n#define eps 1e-7\nstruct circle{\n\tdouble x,y,r;\n};\nele n;\ndouble R;\ncircle a[maxn];\ninline bool cmp1(circle a,circle b){\n\treturn a.x<b.x;\n}\ninline bool cmp2(circle a,circle b){\n\treturn a.y<b.y;\n}\ninline double dis(circle a,circle b){\n\treturn sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))-a.r-b.r;\n}\ndouble solve(ele l,ele r){\n\tif (r-l+1<=10){\n\t\tdouble ans=INF;\n\t\tfor (int i=l; i<=r; ++i)\n\t\t\tfor (int j=i+1; j<=r; ++j)\n\t\t\t\tans=min(ans,dis(a[i],a[j]));\n\t\treturn ans;\n\t}\n\tele mid=(l+r)>>1;\n\tdouble ans=min(solve(l,mid-1),solve(mid+1,r));\n\tfor (int i=l; i<=r; ++i)\n\t\tif (i!=mid) ans=min(ans,dis(a[i],a[mid]));\n\tstatic circle p[maxn],q[maxn];\n\tele u=0,v=0;\n\tfor (int i=l; i<mid; ++i)\n\t\tif (a[mid].x-a[i].x<ans+R*2+eps) p[u++]=a[i];\n\tfor (int i=mid+1; i<=r; ++i)\n\t\tif (a[i].x-a[mid].x<ans+R*2+eps) q[v++]=a[i];\n\tif (!v) return ans;\n\tsort(p,p+u,cmp2); sort(q,q+v,cmp2);\n\tele s=0,t=0;\n\tfor (int i=0; i<u; ++i){\n\t\twhile (s<v-1 && p[i].y-q[s].y>ans+R*2+eps) ++s;\n\t\twhile (t<v-1 && q[t+1].y-p[i].y<ans+R*2+eps) ++t;\n\t\tfor (int j=s; j<=t; ++j) ans=min(ans,dis(p[i],q[j]));\n\t}\n\treturn ans;\n}\nint main(){\n\twhile (scanf(\"%d\",&n),n){\n\t\tR=0;\n\t\tfor (int i=0; i<n; ++i){\n\t\t\tscanf(\"%lf%lf%lf\",&a[i].r,&a[i].x,&a[i].y);\n\t\t\tR=max(R,a[i].r);\n\t\t}\n\t\tsort(a,a+n,cmp1);\n\t\tprintf(\"%.10lf\\n\",solve(0,n-1));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 7312, "score_of_the_acc": -0.2202, "final_rank": 8 }, { "submission_id": "aoj_2057_2104768", "code_snippet": "#include <iomanip>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nstruct circle { double x, y, r; };\nint n; circle x[100009];\nbool operator<(const circle& c1, const circle& c2) {\n\treturn c1.x - c1.r < c2.x - c2.r;\n}\nint main() {\n\twhile (cin >> n, n) {\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tscanf(\"%lf%lf%lf\", &x[i].r, &x[i].x, &x[i].y);\n\t\t\tdouble ex = x[i].x * 0.28 - x[i].y * 0.96;\n\t\t\tdouble ey = x[i].x * 0.96 + x[i].y * 0.28;\n\t\t\tx[i].x = ex, x[i].y = ey;\n\t\t}\n\t\tsort(x, x + n);\n\t\tdouble ret = 1.0e+9;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = i + 1; j < n; j++) {\n\t\t\t\tif (x[i].x + x[i].r + ret < x[j].x - x[j].r) break;\n\t\t\t\tdouble dist = hypot(x[i].x - x[j].x, x[i].y - x[j].y) - x[i].r - x[j].r;\n\t\t\t\tret = min(ret, dist);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.15lf\\n\", ret);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 5628, "score_of_the_acc": -0.1152, "final_rank": 2 }, { "submission_id": "aoj_2057_1899541", "code_snippet": "/*include*/\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<vector>\n#include<cmath>\n#include<cstdio>\n#include<complex>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define rp(a) while(a--)\n#define pb push_back\n#define mp make_pair\n#define it ::iterator\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst double inf=1e8;\nusing namespace std;\n#define shosu(x) fixed<<setprecision(x)\ntypedef complex<double> P;\ntypedef vector<P> G;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\n\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\nstruct C{\n\tP c;double r;\n\tC(const P &c,double r):c(c),r(r){}\n};\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\n#define diff(P, i) (next(P, i) - curr(P, i))\nnamespace std {\n\tbool operator < (const P& a, const P& b) {\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t\t//return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b); \n\t}\n\tbool operator == (const P& a, const P& b) {\n\t\treturn a.real()==b.real()&&a.imag()==b.imag();\n\t}\n}\nP pin(){\n\tdouble x,y;\n\tchar d;\n\tcin>>x>>y;\n\tP p(x,y);\n\treturn p;\n}\nvoid PIN(P* a,int n){\n\trep(i,n)a[i]=pin();\n}\ndouble dot(P a,P b){\n\treturn real(conj(a)*b);\n}\ndouble cross(P a,P b){\n\treturn imag(conj(a)*b);\n}\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > 0) return +1; // counter clockwise\n if (cross(b, c) < 0) return -1; // clockwise\n if (dot(b, c) < 0) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\nP projection(L a,P p){\n\tdouble t=dot(p-a[0],a[0]-a[1])/norm(a[0]-a[1]);\n\treturn a[0]+t*(a[0]-a[1]);\n}\nP reflection(L a,P p){\n\treturn p+2.0*(projection(a,p)-p);\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\ndouble distanceLP(const L &l, const P &p) {\n\treturn abs(p - projection(l, p));\n}\ndouble distanceLL(const L &l, const L &m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\ndouble distanceLS(const L &l, const L &s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\ndouble distanceSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\ndouble distanceSS(const L &s, const L &t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),\n min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n/*bool intersectCS(C c,const L &l){\n return (distanceLP(l,c.c) < c.r+EPS &&\n (c.r < abs(c.c-l[0]) + EPS || c.r < abs(c.c-l[1]) + EPS));\n}*/\nint intersectCS(C c,L &l){\n\tif(norm(projection(l,c.c)-c.c)-c.r*c.r>EPS)return 0;\n\tconst double d1=abs(c.c-l[0]),d2=abs(c.c-l[1]);\n\tif(d1<c.r+EPS&&d2<c.r+EPS)return 0;\n\tif(d1<c.r-EPS&&d2>c.r+EPS||d1>c.r+EPS&&d2<c.r-EPS)return 1;\n\tconst P h=projection(l,c.c);\n\tif(dot(l[0]-h,l[1]-h)<0)return 2;\n\treturn 0;\n}\nP crosspointSS(L a,L b){\n\tdouble t1=abs(cross(a[1]-a[0],b[0]-a[0]));\n\tdouble t2=abs(cross(a[1]-a[0],b[1]-a[0]));\n\treturn b[0]+(b[1]-b[0])*t1/(t1+t2);\n}\nL crosspointCL(C c,L l){\n\tP pr=projection(l,c.c);\n\tP e=(l[1]-l[0])/abs(l[1]-l[0]);\n\tdouble t=sqrt(c.r*c.r-norm(pr-c.c));\n\tP a=pr+t*e;\n\tP b=pr-t*e;\n\tif(b<a)swap(a,b);\n\treturn L(a,b);\n}\nL crosspointCS(C c,L l){\n\tif(intersectCS(c,l)==2)return crosspointCL(c,l);\n\tL ret=crosspointCL(c,l);\n\tif(dot(l[0]-ret[0],l[1]-ret[0])<0)ret[1]=ret[0];\n\telse ret[0]=ret[1];\n\treturn ret;\n}\nL crosspointCC(C a,C b){\n\tP tmp=b.c-a.c;\n\tdouble d=abs(tmp);\n\tdouble q=acos((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d));\n\tdouble t=arg(tmp);//atan(tmp.imag()/tmp.real());\n\tP p1=a.c+polar(a.r,t+q);\n\tP p2=a.c+polar(a.r,t-q);\n\tif(p2<p1)swap(p1,p2);\n\treturn L(p1,p2);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n return m[0] + B / A * (m[1] - m[0]);\n}\ndouble area(const G &g){\n double S =0;\n for(int i =0;i <g.size();i++){\n S +=(cross(g[i],g[(i+1)%g.size()]));\n }\n return abs(S/2.0);\n}\nbool isconvex(const G &g){\n\tint n=g.size();\n\trep(i,n)if(ccw(g[(i+n-1)%n],g[i%n],g[(i+1)%n])==-1)return false;\n\treturn true;\n}\nint inconvex(const G& g, const P& p) {\n\tbool in = false;\n\tint n=g.size();\n\trep(i,n){\n\t\tP a=g[i%n]-p;\n\t\tP b=g[(i+1)%n]-p;\n\t\tif(imag(a)>imag(b))swap(a, b);\n\t\tif(imag(a)<=0&&0<imag(b))if(cross(a,b)<0)in=!in;\n\t\tif(cross(a,b)==0&&dot(a,b)<=0)return 1;//ON\n\t}\n\treturn in?2:0;//IN : OUT;\n}\nG convex_hull(G &ps) {\n int n=ps.size(),k=0;\n\tsort(ps.begin(), ps.end());\n\tG ch(2*n);\n\tfor(int i=0;i<n;ch[k++]=ps[i++])//lower-hull\n\t\twhile(k>=2&&ccw(ch[k-2],ch[k-1],ps[i])==-1)--k;//<=0 -> ==-1\n\tfor(int i=n-2,t=k+1;i>=0;ch[k++]=ps[i--])//upper-hull\n\t\twhile(k>=t&&ccw(ch[k-2],ch[k-1],ps[i])==-1)--k;//\n\tch.resize(k-1);\n\treturn ch;\n}\ndouble convex_diameter(const G &pt) {\n const int n = pt.size();\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i) {\n if (imag(pt[i]) > imag(pt[is])) is = i;\n if (imag(pt[i]) < imag(pt[js])) js = i;\n }\n double maxd = norm(pt[is]-pt[js]);\n\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do {\n if (cross(diff(pt,i), diff(pt,j)) >= 0) j = (j+1) % n;\n else i = (i+1) % n;\n if (norm(pt[i]-pt[j]) > maxd) {\n maxd = norm(pt[i]-pt[j]);\n maxi = i; maxj = j;\n }\n } while (i != is || j != js);\n return sqrt(maxd); /* farthest pair is (maxi, maxj). */\n}//convex_diameter(g)\nG convex_cut(const G& g, const L& l) {\n G Q;\n for (int i = 0; i < g.size(); ++i) {\n P a= curr(g, i), b= next(g, i);\n if (ccw(l[0], l[1], a) != -1) Q.push_back(a);\n if (ccw(l[0], l[1], a)*ccw(l[0], l[1], b) < 0)\n Q.push_back(crosspointLL(L(a,b), l));\n }\n return Q;\n}\nP turn(P p,double t){\n\treturn p*exp(P(.0,t*PI/180.0));\n}\nP turn2(P p,double t){\n\treturn p*exp(P(.0,t));\n}\nvector<L> tangentCC(C a,C b){\n\tif(a.r<b.r)swap(a,b);\n\tdouble d=abs(a.c-b.c);\n\tvector<L>l;\n\tif(d<EPS)return l;\n\tif(a.r+b.r<d-EPS){//hanareteiru\n\t\tdouble t=acos((a.r+b.r)/d);\n\t\tt=t*180/PI;\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),t),b.c+turn(b.r/d*(a.c-b.c),t)));\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),-t),b.c+turn(b.r/d*(a.c-b.c),-t)));\n\t}else if(a.r+b.r<d+EPS){//kuttuiteiru soto\n\t\tP p=a.c+a.r/d*(b.c-a.c);\n\t\tl.pb(L(p,p+turn(b.c-a.c,90)));\n\t}\n\tif(abs(a.r-b.r)<d-EPS){//majiwatteiru\n\t\tdouble t1=acos((a.r-b.r)/d);\n\t\tt1=t1*180/PI;\n\t\tdouble t2=180-t1;\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),t1),b.c+turn(b.r/d*(a.c-b.c),-t2)));\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),-t1),b.c+turn(b.r/d*(a.c-b.c),t2)));\n\t}else if(abs(a.r-b.r)<d+EPS){//kuttuiteiru uti\n\t\tP p=a.c+a.r/d*(b.c-a.c);\n\t\tl.pb(L(p,p+turn(b.c-a.c,90)));\n\t}\n\treturn l;\n}\nvoid printL(const L &out){\n\tprintf(\"%.9f %.9f %.9f %.9f\\n\",out[0].real(),out[0].imag(),out[1].real(),out[1].imag());\n}\nC CIN(){\n\tP p=pin();\n\tdouble r;\n\tcin>>r;\n\treturn C(p,r);\n}\nbool para(L a,L b){\n\treturn (abs(cross(a[1]-a[0],b[1]-b[0]))<EPS);\n}\ndouble min(double a,double b){return a<b?a:b;}\nint main(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tvector<C>c;\n\t\trep(i,n){\n\t\t\tdouble a;cin>>a;\n\t\t\tP p=pin();\n\t\t\t//p=turn(p,17);\n\t\t\tc.pb(C(p,a));\n\t\t}\n\t\tsort(all(c),[](C a,C b){\n\t\t\treturn a.c<b.c;\n\t\t});\n\t\tdouble out=inf;\n\t\trep(i,n){\n\t\t\tint r=max(0,i-10),l=min(i+10,n);\n\t\t\tloop(j,r,l)if(j!=i){\n\t\t\t\tout=min(out,abs(c[i].c-c[j].c)-c[i].r-c[j].r);\n\t\t\t}\n\t\t}\n\t\tcout<<shosu(9)<<out<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 8624, "score_of_the_acc": -0.3238, "final_rank": 11 }, { "submission_id": "aoj_2057_1899536", "code_snippet": "/*include*/\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<vector>\n#include<cmath>\n#include<cstdio>\n#include<complex>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define rp(a) while(a--)\n#define pb push_back\n#define mp make_pair\n#define it ::iterator\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst double inf=1e8;\nusing namespace std;\n#define shosu(x) fixed<<setprecision(x)\ntypedef complex<double> P;\ntypedef vector<P> G;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\n\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\nstruct C{\n\tP c;double r;\n\tC(const P &c,double r):c(c),r(r){}\n};\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\n#define diff(P, i) (next(P, i) - curr(P, i))\nnamespace std {\n\tbool operator < (const P& a, const P& b) {\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t\t//return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b); \n\t}\n\tbool operator == (const P& a, const P& b) {\n\t\treturn a.real()==b.real()&&a.imag()==b.imag();\n\t}\n}\nP pin(){\n\tdouble x,y;\n\tchar d;\n\tcin>>x>>y;\n\tP p(x,y);\n\treturn p;\n}\nvoid PIN(P* a,int n){\n\trep(i,n)a[i]=pin();\n}\ndouble dot(P a,P b){\n\treturn real(conj(a)*b);\n}\ndouble cross(P a,P b){\n\treturn imag(conj(a)*b);\n}\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > 0) return +1; // counter clockwise\n if (cross(b, c) < 0) return -1; // clockwise\n if (dot(b, c) < 0) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\nP projection(L a,P p){\n\tdouble t=dot(p-a[0],a[0]-a[1])/norm(a[0]-a[1]);\n\treturn a[0]+t*(a[0]-a[1]);\n}\nP reflection(L a,P p){\n\treturn p+2.0*(projection(a,p)-p);\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\ndouble distanceLP(const L &l, const P &p) {\n\treturn abs(p - projection(l, p));\n}\ndouble distanceLL(const L &l, const L &m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\ndouble distanceLS(const L &l, const L &s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\ndouble distanceSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\ndouble distanceSS(const L &s, const L &t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),\n min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n/*bool intersectCS(C c,const L &l){\n return (distanceLP(l,c.c) < c.r+EPS &&\n (c.r < abs(c.c-l[0]) + EPS || c.r < abs(c.c-l[1]) + EPS));\n}*/\nint intersectCS(C c,L &l){\n\tif(norm(projection(l,c.c)-c.c)-c.r*c.r>EPS)return 0;\n\tconst double d1=abs(c.c-l[0]),d2=abs(c.c-l[1]);\n\tif(d1<c.r+EPS&&d2<c.r+EPS)return 0;\n\tif(d1<c.r-EPS&&d2>c.r+EPS||d1>c.r+EPS&&d2<c.r-EPS)return 1;\n\tconst P h=projection(l,c.c);\n\tif(dot(l[0]-h,l[1]-h)<0)return 2;\n\treturn 0;\n}\nP crosspointSS(L a,L b){\n\tdouble t1=abs(cross(a[1]-a[0],b[0]-a[0]));\n\tdouble t2=abs(cross(a[1]-a[0],b[1]-a[0]));\n\treturn b[0]+(b[1]-b[0])*t1/(t1+t2);\n}\nL crosspointCL(C c,L l){\n\tP pr=projection(l,c.c);\n\tP e=(l[1]-l[0])/abs(l[1]-l[0]);\n\tdouble t=sqrt(c.r*c.r-norm(pr-c.c));\n\tP a=pr+t*e;\n\tP b=pr-t*e;\n\tif(b<a)swap(a,b);\n\treturn L(a,b);\n}\nL crosspointCS(C c,L l){\n\tif(intersectCS(c,l)==2)return crosspointCL(c,l);\n\tL ret=crosspointCL(c,l);\n\tif(dot(l[0]-ret[0],l[1]-ret[0])<0)ret[1]=ret[0];\n\telse ret[0]=ret[1];\n\treturn ret;\n}\nL crosspointCC(C a,C b){\n\tP tmp=b.c-a.c;\n\tdouble d=abs(tmp);\n\tdouble q=acos((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d));\n\tdouble t=arg(tmp);//atan(tmp.imag()/tmp.real());\n\tP p1=a.c+polar(a.r,t+q);\n\tP p2=a.c+polar(a.r,t-q);\n\tif(p2<p1)swap(p1,p2);\n\treturn L(p1,p2);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n return m[0] + B / A * (m[1] - m[0]);\n}\ndouble area(const G &g){\n double S =0;\n for(int i =0;i <g.size();i++){\n S +=(cross(g[i],g[(i+1)%g.size()]));\n }\n return abs(S/2.0);\n}\nbool isconvex(const G &g){\n\tint n=g.size();\n\trep(i,n)if(ccw(g[(i+n-1)%n],g[i%n],g[(i+1)%n])==-1)return false;\n\treturn true;\n}\nint inconvex(const G& g, const P& p) {\n\tbool in = false;\n\tint n=g.size();\n\trep(i,n){\n\t\tP a=g[i%n]-p;\n\t\tP b=g[(i+1)%n]-p;\n\t\tif(imag(a)>imag(b))swap(a, b);\n\t\tif(imag(a)<=0&&0<imag(b))if(cross(a,b)<0)in=!in;\n\t\tif(cross(a,b)==0&&dot(a,b)<=0)return 1;//ON\n\t}\n\treturn in?2:0;//IN : OUT;\n}\nG convex_hull(G &ps) {\n int n=ps.size(),k=0;\n\tsort(ps.begin(), ps.end());\n\tG ch(2*n);\n\tfor(int i=0;i<n;ch[k++]=ps[i++])//lower-hull\n\t\twhile(k>=2&&ccw(ch[k-2],ch[k-1],ps[i])==-1)--k;//<=0 -> ==-1\n\tfor(int i=n-2,t=k+1;i>=0;ch[k++]=ps[i--])//upper-hull\n\t\twhile(k>=t&&ccw(ch[k-2],ch[k-1],ps[i])==-1)--k;//\n\tch.resize(k-1);\n\treturn ch;\n}\ndouble convex_diameter(const G &pt) {\n const int n = pt.size();\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i) {\n if (imag(pt[i]) > imag(pt[is])) is = i;\n if (imag(pt[i]) < imag(pt[js])) js = i;\n }\n double maxd = norm(pt[is]-pt[js]);\n\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do {\n if (cross(diff(pt,i), diff(pt,j)) >= 0) j = (j+1) % n;\n else i = (i+1) % n;\n if (norm(pt[i]-pt[j]) > maxd) {\n maxd = norm(pt[i]-pt[j]);\n maxi = i; maxj = j;\n }\n } while (i != is || j != js);\n return sqrt(maxd); /* farthest pair is (maxi, maxj). */\n}//convex_diameter(g)\nG convex_cut(const G& g, const L& l) {\n G Q;\n for (int i = 0; i < g.size(); ++i) {\n P a= curr(g, i), b= next(g, i);\n if (ccw(l[0], l[1], a) != -1) Q.push_back(a);\n if (ccw(l[0], l[1], a)*ccw(l[0], l[1], b) < 0)\n Q.push_back(crosspointLL(L(a,b), l));\n }\n return Q;\n}\nP turn(P p,double t){\n\treturn p*exp(P(.0,t*PI/180.0));\n}\nP turn2(P p,double t){\n\treturn p*exp(P(.0,t));\n}\nvector<L> tangentCC(C a,C b){\n\tif(a.r<b.r)swap(a,b);\n\tdouble d=abs(a.c-b.c);\n\tvector<L>l;\n\tif(d<EPS)return l;\n\tif(a.r+b.r<d-EPS){//hanareteiru\n\t\tdouble t=acos((a.r+b.r)/d);\n\t\tt=t*180/PI;\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),t),b.c+turn(b.r/d*(a.c-b.c),t)));\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),-t),b.c+turn(b.r/d*(a.c-b.c),-t)));\n\t}else if(a.r+b.r<d+EPS){//kuttuiteiru soto\n\t\tP p=a.c+a.r/d*(b.c-a.c);\n\t\tl.pb(L(p,p+turn(b.c-a.c,90)));\n\t}\n\tif(abs(a.r-b.r)<d-EPS){//majiwatteiru\n\t\tdouble t1=acos((a.r-b.r)/d);\n\t\tt1=t1*180/PI;\n\t\tdouble t2=180-t1;\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),t1),b.c+turn(b.r/d*(a.c-b.c),-t2)));\n\t\tl.pb(L(a.c+turn(a.r/d*(b.c-a.c),-t1),b.c+turn(b.r/d*(a.c-b.c),t2)));\n\t}else if(abs(a.r-b.r)<d+EPS){//kuttuiteiru uti\n\t\tP p=a.c+a.r/d*(b.c-a.c);\n\t\tl.pb(L(p,p+turn(b.c-a.c,90)));\n\t}\n\treturn l;\n}\nvoid printL(const L &out){\n\tprintf(\"%.9f %.9f %.9f %.9f\\n\",out[0].real(),out[0].imag(),out[1].real(),out[1].imag());\n}\nC CIN(){\n\tP p=pin();\n\tdouble r;\n\tcin>>r;\n\treturn C(p,r);\n}\nbool para(L a,L b){\n\treturn (abs(cross(a[1]-a[0],b[1]-b[0]))<EPS);\n}\ndouble min(double a,double b){return a<b?a:b;}\nint main(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tvector<C>c;\n\t\trep(i,n){\n\t\t\tdouble a;cin>>a;\n\t\t\tP p=pin();\n\t\t\tp=turn(p,17);\n\t\t\tc.pb(C(p,a));\n\t\t}\n\t\tsort(all(c),[](C a,C b){\n\t\t\treturn a.c<b.c;\n\t\t});\n\t\tdouble out=inf;\n\t\trep(i,n){\n\t\t\tint r=max(0,i-10),l=min(i+10,n);\n\t\t\tloop(j,r,l)if(j!=i){\n\t\t\t\tout=min(out,abs(c[i].c-c[j].c)-c[i].r-c[j].r);\n\t\t\t}\n\t\t}\n\t\tcout<<shosu(9)<<out<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 8640, "score_of_the_acc": -0.3246, "final_rank": 12 }, { "submission_id": "aoj_2057_1722145", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n\nusing namespace std;\n\nstruct Circle{\n\tdouble x,y,r;\n\tbool operator < (const Circle &)const;\n};\n\nbool Circle::operator < (const Circle &a)const{\n\treturn (x*x+y*y)<(a.x*a.x+a.y*a.y);\n}\n\ndouble dist(const Circle &a,const Circle &b){\n\treturn sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))-a.r-b.r;\n}\n\nconst int MAX_N=100000;\n\nint N;\nCircle c[MAX_N];\n\nvector<double> out;\n\nint main(){\n\tdo{\n\t\tscanf(\"%d\",&N);\n\t\tif (N){\n\t\t\tfor (int i=0;i<N;i++){\n\t\t\t\tscanf(\"%lf %lf %lf\",&c[i].r,&c[i].x,&c[i].y);\n\t\t\t}\n\t\t\tsort(c,c+N);\n\t\t\tdouble ans=1e50;\n\t\t\tfor (int i=0;i<N;i++){\n\t\t\t\tfor (int j=i+1;j<min(N,i+4001);j++){\n\t\t\t\t\tans=min(ans,dist(c[i],c[j]));\n\t\t\t\t}\n\t\t\t}\n\t\t\tout.push_back(ans);\n\t\t}\n\t}while(N);\n\tfor (int i=0;i<out.size();i++){\n\t\tprintf(\"%.12f\\n\",out[i]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6680, "memory_kb": 3468, "score_of_the_acc": -1, "final_rank": 18 }, { "submission_id": "aoj_2057_1539332", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#define INF 1e10\nusing namespace std;\n\nint n;\nstruct P{\n\tdouble x,y,r;\n\tP(){}\n\tP(double xx,double yy,double rr){\n\t\tx=xx;\n\t\ty=yy;\n\t\tr=rr;\n\t}\n\tbool operator<(const P &p1)const{\n\t\treturn x<p1.x;\n\t}\n};\n\nbool compare_y(P a,P b){\n\treturn a.y<b.y;\n}\n\n\ndouble closest_pair(P *a,int n){\n\tif(n<=1)return INF;\n\tint m=n/2;\n\tdouble x=a[m].x;\n\tdouble r=a[m].r;\n\tdouble d=min(closest_pair(a,m),closest_pair(a+m,n-m));\n\tinplace_merge(a,a+m,a+n,compare_y);\n\tvector<P> b;\n\tfor(int i=0;i<n;i++){\n\t\tif(fabs(a[i].x-x)-a[i].r-r>=d)continue;\n\t\tfor(int j=0;j<b.size();j++){\n\t\t\tdouble dx=a[i].x-b[b.size()-j-1].x;\n\t\t\tdouble dy=a[i].y-b[b.size()-j-1].y;\n\t\t\tif(dy>=d+r+a[i].r)break;\n\t\t\td=min(d,sqrt(dx*dx+dy*dy)-b[b.size()-j-1].r-a[i].r);\n\t\t}\n\t\tb.push_back(a[i]);\n\t\tr=max(a[i].r,r);\n\t}\n\treturn d;\n}\n\nP p[100001];\n\nint main(void){\n\twhile(1){\n\t\tscanf(\"%d\",&n);\n\t\tif(n==0)break;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tscanf(\"%lf %lf %lf\",&p[i].r,&p[i].x,&p[i].y);\n\t\t}\n\t\tsort(p,p+n);\n\t\tprintf(\"%.9f\\n\",closest_pair(p,n));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 8840, "score_of_the_acc": -0.3093, "final_rank": 10 }, { "submission_id": "aoj_2057_1110780", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\nusing namespace std;\ndouble x[110000];\ndouble y[110000];\ndouble r[110000];\npair<double,int>p[110000];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble tx,ty;\n\t\t\tscanf(\"%lf%lf%lf\",r+i,&tx,&ty);\n\t\t\tx[i]=cos(1)*tx-sin(1)*ty;\n\t\t\ty[i]=sin(1)*tx+cos(1)*ty;\n\t\t}\n\t\tdouble ret=sqrt((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1]))-r[0]-r[1];\n\t\tfor(int i=0;i<a;i++){\n\t\t\tp[i]=make_pair(x[i]+r[i],i);\n\t\t}\n\t\tstd::sort(p,p+a);\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=i-1;j>=0;j--){\n\t\t\t\tif(p[i].first-r[p[i].second]*2-p[j].first>ret)break;\n\t\t\t\tint L=p[i].second;\n\t\t\t\tint R=p[j].second;\n\t\t\t\tret=min(ret,sqrt((x[L]-x[R])*(x[L]-x[R])+(y[L]-y[R])*(y[L]-y[R]))-r[L]-r[R]);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.12f\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 5324, "score_of_the_acc": -0.1051, "final_rank": 1 }, { "submission_id": "aoj_2057_1092044", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\ntypedef double DD;\n\nconst DD INF = 1LL<<60;\nconst DD EPS = 1e-10;\nconst DD PI = acos(-1.0);\nDD torad(int deg) {return (DD)(deg) * PI / 180;}\nDD todeg(DD ang) {return ang * 180 / PI;}\n\nstruct Point {\n DD x, y;\n Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}\n friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << \", \" << p.y << ')';}\n};\n\nPoint operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}\nPoint operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}\nPoint operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}\nPoint operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}\nPoint operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}\nPoint operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}\nPoint conj(const Point &p) {return Point(p.x, -p.y);}\nPoint rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}\nPoint rot90(const Point &p) {return Point(-p.y, p.x);}\nDD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}\nDD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}\nDD norm(const Point &p) {return dot(p, p);}\nDD abs(const Point &p) {return sqrt(dot(p, p));}\nDD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}\nbool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}\nbool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}\nbool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}\nPoint operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if (cross(b-a, c-a) > EPS) return 1;\n if (cross(b-a, c-a) < -EPS) return -1;\n if (dot(b-a, c-a) < -EPS) return 2;\n if (norm(b-a) < norm(c-a) - EPS) return -2;\n return 0;\n}\n\nstruct Line : vector<Point> {\n Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {\n this->push_back(a);\n this->push_back(b);\n }\n};\n\nstruct Circle : Point{\n DD r;\n Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}\n};\n\n\nDD MAX_R;\nbool compare_y(Circle a, Circle b) { return a.y < b.y; }\n\nDD Rec(vector<Circle>::iterator it, int n) {\n if (n <= 1) return INF;\n int m = n/2;\n DD x = it[m].x;\n DD d = min(Rec(it, m), Rec(it+m, n-m));\n inplace_merge(it, it+m, it+n, compare_y);\n \n vector<Circle> vec;\n for (int i = 0; i < n; ++i) {\n if (fabs(it[i].x - x) >= d + MAX_R*2) continue;\n for (int j = 0; j < vec.size(); ++j) {\n DD dx = it[i].x - vec[vec.size()-j-1].x;\n DD dy = it[i].y - vec[vec.size()-j-1].y;\n if (dy >= d + MAX_R*2) break;\n d = min(d, abs(sqrt(dx*dx+dy*dy) - it[i].r - vec[vec.size()-j-1].r));\n }\n vec.push_back(it[i]);\n }\n return d;\n}\n\nDD Closet(vector<Circle> vc) {\n int n = vc.size();\n sort(vc.begin(), vc.end());\n return Rec(vc.begin(), n);\n}\n\n\nint n;\ndouble r, x, y;\n\nint main() {\n while (cin >> n) {\n if (n == 0) break;\n MAX_R = 0.0;\n vector<Circle> vc(n);\n for (int i = 0; i < n; ++i) cin >> vc[i].r >> vc[i].x >> vc[i].y, chmax(MAX_R, vc[i].r);\n double res = Closet(vc);\n cout << fixed << setprecision(9) << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 11160, "score_of_the_acc": -0.4864, "final_rank": 16 }, { "submission_id": "aoj_2057_1092029", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\ntypedef double DD;\n\nconst DD INF = 1LL<<60;\nconst DD EPS = 1e-10;\nconst DD PI = acos(-1.0);\nDD torad(int deg) {return (DD)(deg) * PI / 180;}\nDD todeg(DD ang) {return ang * 180 / PI;}\n\nstruct Point {\n DD x, y;\n Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}\n friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << \", \" << p.y << ')';}\n};\n\nPoint operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}\nPoint operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}\nPoint operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}\nPoint operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}\nPoint operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}\nPoint operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}\nPoint conj(const Point &p) {return Point(p.x, -p.y);}\nPoint rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}\nPoint rot90(const Point &p) {return Point(-p.y, p.x);}\nDD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}\nDD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}\nDD norm(const Point &p) {return dot(p, p);}\nDD abs(const Point &p) {return sqrt(dot(p, p));}\nDD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}\nbool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}\nbool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}\nbool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}\nPoint operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if (cross(b-a, c-a) > EPS) return 1;\n if (cross(b-a, c-a) < -EPS) return -1;\n if (dot(b-a, c-a) < -EPS) return 2;\n if (norm(b-a) < norm(c-a) - EPS) return -2;\n return 0;\n}\n\nstruct Line : vector<Point> {\n Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {\n this->push_back(a);\n this->push_back(b);\n }\n};\n\nstruct Circle : Point{\n DD r;\n Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}\n};\n\n\nDD MAX_R;\nbool compare_y(Circle a, Circle b) { return a.y < b.y; }\n\nDD Rec(vector<Circle>::iterator it, int n) {\n if (n <= 1) return INF;\n int m = n/2;\n DD x = it[m].x;\n DD d = min(Rec(it, m), Rec(it+m, n-m));\n inplace_merge(it, it+m, it+n, compare_y);\n \n vector<Circle> vec;\n for (int i = 0; i < n; ++i) {\n if (fabs(it[i].x - x) >= d + MAX_R*2) continue;\n for (int j = 0; j < vec.size(); ++j) {\n DD dx = it[i].x - vec[vec.size()-j-1].x;\n DD dy = it[i].y - vec[vec.size()-j-1].y;\n if (dy >= d + MAX_R*2) break;\n d = min(d, abs(sqrt(dx*dx+dy*dy) - it[i].r - vec[vec.size()-j-1].r));\n }\n vec.push_back(it[i]);\n }\n return d;\n}\n\nDD Closet(vector<Circle> vc) {\n int n = vc.size();\n sort(vc.begin(), vc.end());\n return Rec(vc.begin(), n);\n}\n\n\nint n;\ndouble r, x, y;\n\nint main() {\n while (cin >> n) {\n if (n == 0) break;\n MAX_R = 0.0;\n vector<Circle> vc(n);\n for (int i = 0; i < n; ++i) cin >> vc[i].r >> vc[i].x >> vc[i].y, chmax(MAX_R, vc[i].r);\n double res = Closet(vc);\n cout << fixed << setprecision(9) << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 11360, "score_of_the_acc": -0.4956, "final_rank": 17 } ]
aoj_2065_cpp
Problem H: Slippy Floors The princess of the Fancy Kingdom has been loved by many people for her lovely face. However the witch of the Snow World has been jealous of the princess being loved. For her jealousy, the witch has shut the princess into the Ice Tower built by the witch’s extreme magical power. As the Ice Tower is made of cubic ice blocks that stick hardly, the tower can be regarded to be composed of levels each of which is represented by a 2D grid map. The only way for the princess to escape from the tower is to reach downward stairs on every level. However, many magical traps set by the witch obstruct her moving ways. In addition, because of being made of ice, the floor is so slippy that movement of the princess is very restricted. To be precise, the princess can only press on one adjacent wall to move against the wall as the reaction. She is only allowed to move in the vertical or horizontal direction, not in the diagonal direction. Moreover, she is forced to keep moving without changing the direction until she is prevented from further moving by the wall, or she reaches a stairway or a magical trap. She must avoid the traps by any means because her being caught by any of these traps implies her immediate death by its magic. These restriction on her movement makes her escape from the tower impossible, so she would be frozen to death without any help. You are a servant of the witch of the Snow World, but unlike the witch you love the princess as many other people do. So you decided to help her with her escape. However, if you would go help her directly in the Ice Tower, the witch would notice your act to cause your immediate death along with her. Considering this matter and your ability, all you can do for her is to generate snowmans at arbitrary places (grid cells) neither occupied by the traps nor specified as the cell where the princess is initially placed. These snowmans are equivalent to walls, thus the princess may utilize them for her escape. On the other hand, your magical power is limited, so you should generate the least number of snowmans needed for her escape. You are required to write a program that solves placement of snowmans for given a map on each level, such that the number of generated snowmans are minimized. Input The first line of the input contains a single integer that represents the number of levels in the Ice Tower. Each level is given by a line containing two integers NY (3 ≤ NY ≤ 30) and NX (3 ≤ NX ≤ 30) that indicate the vertical and horizontal sizes of the grid map respectively, and following NY lines that specify the grid map. Each of the NY lines contain NX characters, where ‘A’ represents the initial place of the princess, ‘>’ the downward stairs, ‘.’ a place outside the tower, ‘_’ an ordinary floor, ‘#’ a wall, and ‘^’ a magical trap. Every map has exactly one ‘A’ and one ‘>’. You may assume that there is no way of the princess moving to places outside the tower or beyond the maps, and every case can be ...(truncated)
[ { "submission_id": "aoj_2065_861603", "code_snippet": "/*\n00:42 - 01:17\n */\n\n#include<iostream>\n#include<algorithm>\n#include<climits>\n#include<cstdio>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define MAX 31\nusing namespace std;\n\nint H,W;\nbool used[MAX][MAX];\nchar G[MAX][MAX];\nint sp,mincost;\nint dx[] = {0,1,0,-1};\nint dy[] = {1,0,-1,0};\n\ninline bool isValid(int x,int y){ return ( 0 <= x && x < W && 0 <= y && y < H); }\n\nvoid dfs(int cur,int cost,int pdir){\n\n if(cost >= 10)return;\n if(mincost <= cost)return;\n int x = cur % W, y = cur / W;\n\n if(G[y][x] == '>'){\n mincost = min(mincost,cost);\n return;\n }\n\n if(used[y][x])return;\n\n rep(i,4){\n if(i == pdir)continue;\n\n int rnx = x + dx[(i+2)%4], rny = y + dy[(i+2)%4];//テヲツャツ。テ」ツ?ォテゥツ?イテ」ツつ?ヲツ鳴ケテ・ツ青妥」ツ?ョテゥツ??\n if( G[rny][rnx] == '#' ){\n\n used[y][x] = true;\n int nx = x + dx[i], ny = y + dy[i];\n while( G[ny][nx] == '_' ){ \n\tif( G[ny+dy[i]][nx+dx[i]] == '^' )break;\n\tif( G[ny+dy[i]][nx+dx[i]] == '_'){\n\t G[ny+dy[i]][nx+dx[i]] = '#';\n\t dfs(nx+ny*W,cost+1,(i+2)%4);\n\t G[ny+dy[i]][nx+dx[i]] = '_';\n\t}\n\t nx += dx[i], ny += dy[i];\n }\n if( G[ny][nx] == '>' )dfs(nx+ny*W,cost,(i+2)%4);\n if( G[ny][nx] == '#'){\n\tnx -= dx[i], ny -= dy[i];\n\tdfs(nx+ny*W,cost,(i+2)%4);\n }\n used[y][x] = false;\n } else if( rnx+rny*W != sp && G[rny][rnx] == '_' ) {\n G[rny][rnx] = '#';\n dfs(cur,cost+1,pdir);\n G[rny][rnx] = '_';\n }\n }\n}\n\nint main(){\n int T;\n scanf(\"%d\",&T);\n while(T--){\n scanf(\"%d %d\",&H,&W);\n rep(y,H)rep(x,W){\n cin >> G[y][x];\n if(G[y][x] == 'A')sp = x + y * W, G[y][x] = '_';\n used[y][x] = false;\n }\n \n mincost = IINF;\n dfs(sp,0,IINF);\n if(mincost == IINF)puts(\"10\");\n else printf(\"%d\\n\",mincost);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1232, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_2065_861602", "code_snippet": "/*\n00:42 - 01:17\n */\n\n#include<iostream>\n#include<algorithm>\n#include<climits>\n#include<cstdio>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define MAX 31\nusing namespace std;\n\nint H,W;\nbool used[MAX][MAX];\nchar G[MAX][MAX];\nint sp,mincost;\nint dx[] = {0,1,0,-1};\nint dy[] = {1,0,-1,0};\n\ninline bool isValid(int x,int y){ return ( 0 <= x && x < W && 0 <= y && y < H); }\n\nvoid dfs(int cur,int cost,int pdir){\n\n if(cost >= 10)return;\n if(mincost <= cost)return;\n int x = cur % W, y = cur / W;\n\n if(G[y][x] == '>'){\n mincost = min(mincost,cost);\n return;\n }\n\n if(used[y][x])return;\n\n rep(i,4){\n //if(i == pdir)continue;\n\n int rnx = x + dx[(i+2)%4], rny = y + dy[(i+2)%4];//次に進む方向の逆\n if( G[rny][rnx] == '#' ){\n\n used[y][x] = true;\n int nx = x + dx[i], ny = y + dy[i];\n while( G[ny][nx] == '_' ){ \n\tif( G[ny+dy[i]][nx+dx[i]] == '^' )break;\n\tif( G[ny+dy[i]][nx+dx[i]] == '_'){\n\t G[ny+dy[i]][nx+dx[i]] = '#';\n\t dfs(nx+ny*W,cost+1,(i+2)%4);\n\t G[ny+dy[i]][nx+dx[i]] = '_';\n\t}\n\t nx += dx[i], ny += dy[i];\n }\n if( G[ny][nx] == '>' )dfs(nx+ny*W,cost,(i+2)%4);\n if( G[ny][nx] == '#'){\n\tnx -= dx[i], ny -= dy[i];\n\tdfs(nx+ny*W,cost,(i+2)%4);\n }\n used[y][x] = false;\n } else if( rnx+rny*W != sp && G[rny][rnx] == '_' ) {\n G[rny][rnx] = '#';\n dfs(cur,cost+1,pdir);\n G[rny][rnx] = '_';\n }\n }\n}\n\nint main(){\n int T;\n scanf(\"%d\",&T);\n while(T--){\n scanf(\"%d %d\",&H,&W);\n rep(y,H)rep(x,W){\n cin >> G[y][x];\n if(G[y][x] == 'A')sp = x + y * W, G[y][x] = '_';\n used[y][x] = false;\n }\n \n mincost = IINF;\n dfs(sp,0,IINF);\n if(mincost == IINF)puts(\"10\");\n else printf(\"%d\\n\",mincost);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2530, "memory_kb": 1228, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_2065_861601", "code_snippet": "/*\n00:42 - 01:17\n */\n\n#include<iostream>\n#include<cmath>\n#include<vector>\n#include<deque>\n#include<queue>\n#include<algorithm>\n#include<climits>\n#include<cassert>\n#include<cstdio>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define MAX 31\nusing namespace std;\n\nint H,W;\nbool used[MAX][MAX];\nchar G[MAX][MAX];\nint sp,mincost;\nint dx[] = {0,1,0,-1};\nint dy[] = {1,0,-1,0};\n\ninline bool isValid(int x,int y){ return ( 0 <= x && x < W && 0 <= y && y < H); }\n\nvoid dfs(int cur,int cost,int pdir){\n\n if(cost >= 10)return;\n if(mincost <= cost)return;\n int x = cur % W, y = cur / W;\n\n if(G[y][x] == '>'){\n mincost = min(mincost,cost);\n return;\n }\n\n if(used[y][x])return;\n\n rep(i,4){\n if(i == pdir)continue;\n\n int rnx = x + dx[(i+2)%4], rny = y + dy[(i+2)%4];//次に進む方向の逆\n if( G[rny][rnx] == '#' ){\n\n used[y][x] = true;\n int nx = x + dx[i], ny = y + dy[i];\n while( G[ny][nx] == '_' ){ \n\tif( G[ny+dy[i]][nx+dx[i]] == '^' )break;\n\tif( G[ny+dy[i]][nx+dx[i]] == '_'){\n\t G[ny+dy[i]][nx+dx[i]] = '#';\n\t dfs(nx+ny*W,cost+1,(i+2)%4);\n\t G[ny+dy[i]][nx+dx[i]] = '_';\n\t}\n\t nx += dx[i], ny += dy[i];\n }\n if( G[ny][nx] == '>' )dfs(nx+ny*W,cost,(i+2)%4);\n if( G[ny][nx] == '#'){\n\tnx -= dx[i], ny -= dy[i];\n\tdfs(nx+ny*W,cost,(i+2)%4);\n }\n used[y][x] = false;\n } else if( rnx+rny*W != sp && G[rny][rnx] == '_' ) {\n G[rny][rnx] = '#';\n dfs(cur,cost+1,pdir);\n G[rny][rnx] = '_';\n }\n }\n}\n\nint main(){\n int T;\n scanf(\"%d\",&T);\n while(T--){\n scanf(\"%d %d\",&H,&W);\n rep(y,H)rep(x,W){\n cin >> G[y][x];\n if(G[y][x] == 'A')sp = x + y * W, G[y][x] = '_';\n used[y][x] = false;\n }\n \n mincost = IINF;\n dfs(sp,0,IINF);\n if(mincost == IINF)puts(\"10\");\n else printf(\"%d\\n\",mincost);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1228, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2061_cpp
Problem D: International Party Isaac H. Ives is attending an international student party (maybe for girl-hunting). Students there enjoy talking in groups with excellent foods and drinks. However, since students come to the party from all over the world, groups may not have a language spoken by all students of the group. In such groups, some student(s) need to work as interpreters, but intervals caused by interpretation make their talking less exciting. Needless to say, students want exciting talks. To have talking exciting as much as possible, Isaac proposed the following rule: the number of languages used in talking should be as little as possible, and not exceed five. Many students agreed with his proposal, but it is not easy for them to find languages each student should speak. So he calls you for help. Your task is to write a program that shows the minimum set of languages to make talking possible, given lists of languages each student speaks. Input The input consists of a series of data sets. The first line of each data set contains two integers N (1 ≤ N ≤ 30) and M (2 ≤ M ≤ 20) separated by a blank, which represent the numbers of languages and students respectively. The following N lines contain language names, one name per line. The following M lines describe students in the group. The i -th line consists of an integer K i that indicates the number of languages the i -th student speaks, and K i language names separated by a single space. Each language name consists of up to twenty alphabetic letters. A line that contains two zeros indicates the end of input and is not part of a data set. Output Print a line that contains the minimum number L of languages to be spoken, followed by L language names in any order. Each language name should be printed in a separate line. In case two or more sets of the same size is possible, you may print any one of them. If it is impossible for the group to enjoy talking with not more than five languages, you should print a single line that contains “Impossible” (without quotes). Print an empty line between data sets. Sample Input 3 4 English French Japanese 1 English 2 French English 2 Japanese English 1 Japanese 2 2 English Japanese 1 English 1 Japanese 6 7 English Dutch German French Italian Spanish 1 English 2 English Dutch 2 Dutch German 2 German French 2 French Italian 2 Italian Spanish 1 Spanish 0 0 Output for the Sample Input 2 English Japanese Impossible Impossible
[ { "submission_id": "aoj_2061_10532388", "code_snippet": "#include \"bits/stdc++.h\"\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)\n#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)\n#define Endl endl\n#define all(a) a.begin(), a.end()\n#define pr(i, j) make_pair(i, j)\n#define isin(x, l, r) (l <= x && x < r)\n#define chmin(a, b) a = min(a, b)\n#define chmax(a, b) a = max(a, b)\n#define srt(ar) sort(ar.begin(), ar.end())\n#define rev(ar) reverse(ar.begin(), ar.end())\n#define jge(f, s, t) cout << (f ? s : t) << endl\ntemplate <typename T>\nusing ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#define printar(ar) \\\n do \\\n { \\\n for (auto dbg : ar) \\\n { \\\n cout << dbg << \" \"; \\\n } \\\n cout << endl; \\\n } while (0)\nconst ll inf = 1e18;\nconst ld pi = 3.14159265358979;\nconst ld eps = 1e-9;\ntemplate <class T, ll n, ll idx = 0>\nauto make_vec(const ll (&d)[n], const T &init) noexcept\n{\n if constexpr (idx < n)\n return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else\n return init;\n}\n\ntemplate <class T, ll n>\nauto make_vec(const ll (&d)[n]) noexcept\n{\n return make_vec(d, T{});\n}\n//////////////// 以下を貼る ////////////////\ntemplate <class T>\nsize_t HashCombine(const size_t seed, const T &v)\n{\n return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));\n}\n/* pair用 */\ntemplate <class T, class S>\nstruct std::hash<std::pair<T, S>>\n{\n size_t operator()(const std::pair<T, S> &keyval) const noexcept\n {\n return HashCombine(std::hash<T>()(keyval.first), keyval.second);\n }\n};\n/* vector用 */\ntemplate <class T>\nstruct std::hash<std::vector<T>>\n{\n size_t operator()(const std::vector<T> &keyval) const noexcept\n {\n size_t s = 0;\n for (auto &&v : keyval)\n s = HashCombine(s, v);\n return s;\n }\n};\n/* tuple用 */\ntemplate <int N>\nstruct HashTupleCore\n{\n template <class Tuple>\n size_t operator()(const Tuple &keyval) const noexcept\n {\n size_t s = HashTupleCore<N - 1>()(keyval);\n return HashCombine(s, std::get<N - 1>(keyval));\n }\n};\ntemplate <>\nstruct HashTupleCore<0>\n{\n template <class Tuple>\n size_t operator()(const Tuple &keyval) const noexcept { return 0; }\n};\ntemplate <class... Args>\nstruct std::hash<std::tuple<Args...>>\n{\n size_t operator()(const tuple<Args...> &keyval) const noexcept\n {\n return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval);\n }\n};\ntemplate <typename Type3>\nclass union_find\n{\nprivate:\n vector<Type3> graph;\n vector<Type3> sum;\n\npublic:\n // n頂点のグラフを作成(添字は0から)\n union_find(Type3 n)\n {\n graph.resize(n);\n sum.resize(n);\n for (int i = 0; i < n; i++)\n {\n graph[i] = i;\n sum[i] = 1;\n }\n }\n // 頂点nの根を返す\n Type3 root(Type3 n)\n {\n if (graph[n] == n)\n return n;\n else\n {\n graph[n] = root(graph[n]);\n return graph[n];\n }\n }\n // union_findの内部構造,デバッグ\n void debug()\n {\n for (unsigned i = 0; i < graph.size(); i++)\n {\n cout << graph[i] << \" \";\n }\n cout << endl;\n for (unsigned i = 0; i < graph.size(); i++)\n {\n cout << sum[i] << \" \";\n }\n cout << endl;\n }\n // 頂点mから頂点nに向かって辺を引く\n void line(Type3 m, Type3 n)\n {\n if (check(m, n))\n return;\n sum[root(m)] += sum[root(n)];\n sum[root(n)] = 0;\n graph[root(n)] = root(m);\n graph[n] = root(m);\n }\n // 頂点nが属する連結成分の大きさを返す\n Type3 size(Type3 n)\n {\n return sum[root(n)];\n }\n // 頂点mと頂点nが連結なら1,非連結なら0を返す\n bool check(Type3 m, Type3 n)\n {\n return root(m) == root(n);\n }\n};\n/////////////////////////////////////////////////////////////////////////////////////////\n\n////////////////////////////////////////////\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n bool isfirst = true;\n while (true)\n {\n ll n, m;\n cin >> n >> m;\n if (n == 0)\n {\n break;\n }\n else\n {\n if (isfirst)\n {\n isfirst = false;\n }\n else\n {\n cout << endl;\n }\n }\n vector<string> a(n);\n map<string, ll> mp;\n rep(i, 0, n)\n {\n string s;\n cin >> s;\n a[i] = s;\n mp[s] = i;\n }\n vector<vector<ll>> b(n);\n rep(i, 0, m)\n {\n ll t;\n cin >> t;\n rep(j, 0, t)\n {\n string s;\n cin >> s;\n b[mp[s]].push_back(i);\n }\n }\n bool f = false;\n rep(i, 1, min(n, 5LL) + 1)\n {\n vector<ll> ar(n, 0);\n rep(j, 0, i)\n {\n ar[j] = 1;\n }\n srt(ar);\n do\n {\n union_find<ll> uf(m);\n rep(j, 0, n)\n {\n if (ar[j] == 1)\n {\n rep(k, 0, b[j].size())\n {\n uf.line(b[j][0], b[j][k]);\n }\n }\n }\n if (uf.size(0) == m)\n {\n f = true;\n cout << i << endl;\n rep(j, 0, n)\n {\n if (ar[j] == 1)\n {\n cout << a[j] << endl;\n }\n }\n goto nxt;\n }\n } while (next_permutation(all(ar)));\n }\n nxt:\n if (!f)\n {\n cout << \"Impossible\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3456, "score_of_the_acc": -1.0276, "final_rank": 9 }, { "submission_id": "aoj_2061_2864863", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\ntemplate < class BidirectionalIterator >\nbool next_combination(BidirectionalIterator first1,\n\tBidirectionalIterator last1,\n\tBidirectionalIterator first2,\n\tBidirectionalIterator last2)\n{\n\tif ((first1 == last1) || (first2 == last2)) {\n\t\treturn false;\n\t}\n\tBidirectionalIterator m1 = last1;\n\tBidirectionalIterator m2 = last2; --m2;\n\twhile (--m1 != first1 && !(*m1 < *m2)) {\n\t}\n\tbool result = (m1 == first1) && !(*first1 < *m2);\n\tif (!result) {\n\t\t// ①\n\t\twhile (first2 != m2 && !(*m1 < *first2)) {\n\t\t\t++first2;\n\t\t}\n\t\tfirst1 = m1;\n\t\tstd::iter_swap(first1, first2);\n\t\t++first1;\n\t\t++first2;\n\t}\n\tif ((first1 != last1) && (first2 != last2)) {\n\t\t// ②\n\t\tm1 = last1; m2 = first2;\n\t\twhile ((m1 != first1) && (m2 != last2)) {\n\t\t\tstd::iter_swap(--m1, m2);\n\t\t\t++m2;\n\t\t}\n\t\t// ③\n\t\tstd::reverse(first1, m1);\n\t\tstd::reverse(first1, last1);\n\t\tstd::reverse(m2, last2);\n\t\tstd::reverse(first2, last2);\n\t}\n\treturn !result;\n}\n\ntemplate < class BidirectionalIterator >\nbool next_combination(BidirectionalIterator first,\n\tBidirectionalIterator middle,\n\tBidirectionalIterator last)\n{\n\treturn next_combination(first, middle, middle, last);\n}\n\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\n\nint main() {\n\tcout<<setprecision(10)<<fixed;\n\n\tint tt=false;\n\twhile (true){\n\t\tint N,M;cin>>N>>M;\n\t\tif(!N)break;\n\n\t\tif (tt)cout << endl;\n\t\ttt = true;\n\t\tmap<string,int>mp;\n\t\tmap<int,string>revmp;\n\t\t{\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tstring st;cin>>st;\n\t\t\t\tmp[st]=i;\n\t\t\t\trevmp[i]=st;\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>>langs(M,vector<int>(N));\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tint k;cin>>k;\n\t\t\tfor (int j = 0; j < k; ++j) {\n\t\t\t\tstring st;cin>>st;\n\t\t\t\tlangs[i][mp[st]]=true;\n\t\t\t}\n\t\t}\n\t\tvector<int>perms(N);\n\t\tiota(perms.begin(),perms.end(),0);\n\n\t\tvector<string>anss;\n\n\t\tfor (int t = 1; t <= (min(N, 5)); ++t) {\n\t\t\tdo {\n\t\t\t\tUnionFind uf(M + min(N, 5));\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tfor (int j = 0; j < t; ++j) {\n\t\t\t\t\t\tint lang_id = perms[j];\n\t\t\t\t\t\tif (langs[i][lang_id])uf.unionSet(i, M + j);\n\n\t\t\t\t\t\tfor (int k = 1; k <t; ++k) {\n\t\t\t\t\t\t\tint lang_id2 = perms[k];\n\t\t\t\t\t\t\tif (langs[i][lang_id] && langs[i][lang_id2]) {\n\t\t\t\t\t\t\t\tuf.unionSet(M + j, M + k);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tset<int>aset;\n\t\t\t\tfor (int i = 0; i < M + t; ++i) {\n\t\t\t\t\taset.emplace(uf.root(i));\n\t\t\t\t}\n\t\t\t\tif (aset.size() == 1) {\n\t\t\t\t\tfor (int i = 0; i < t; ++i) {\n\t\t\t\t\t\tanss.emplace_back(revmp[perms[i]]);\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} while (next_combination(perms.begin(), perms.begin() + t, perms.end()));\n\t\t\tif(!anss.empty())break;\n\t\t}\n\t\tif (anss.empty()) {\n\t\t\tcout << \"Impossible\" << endl;\n\t\t}\n\t\telse {\n\t\t\tcout<<anss.size()<<endl;\n\t\t\tfor(auto ans:anss)cout<<ans<<endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 3068, "score_of_the_acc": -1.1101, "final_rank": 13 }, { "submission_id": "aoj_2061_2610874", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nint N,M;\nchar name[30][21];\n\nint check[20];\n\nint boss[20],height[20];\nint POW[31];\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\nvoid init(){\n\tfor(int i = 0; i < M; i++){\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t}\n}\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nint data_set = 0;\n\nvoid func(){\n\n\tif(data_set != 0){\n\t\tprintf(\"\\n\");\n\t}\n\tdata_set++;\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%s\",name[i]);\n\t}\n\n\tfor(int i = 0; i < M; i++)check[i] = 0;\n\n\tchar buf[21];\n\tint num;\n\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d\",&num);\n\t\tfor(int k = 0; k < num; k++){\n\t\t\tscanf(\"%s\",buf);\n\t\t\tfor(int p = 0; p < N; p++){\n\t\t\t\tif(strCmp(name[p],buf)){\n\t\t\t\t\tcheck[i] += POW[p];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tbool FLG;\n\tint S;\n\n\tfor(int a = 0; a < N; a++){\n\t\tFLG = true;\n\t\tS = POW[a];\n\n\t\tfor(int i = 0; i < M; i++){\n\t\t\tif((check[i] & S) == 0){\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG){\n\t\t\tprintf(\"1\\n\");\n\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\treturn;\n\t\t}\n\t}\n\n\tif(N == 1){\n\t\tprintf(\"Impossible\\n\");\n\t\treturn;\n\t}\n\n\tint count;\n\n\tfor(int a = 0; a < N-1; a++){\n\t\tfor(int b = a+1; b < N; b++){\n\t\t\tS = POW[a]+POW[b];\n\t\t\tinit();\n\n\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcount = 0;\n\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t}\n\n\t\t\tif(count == 1){\n\t\t\t\tprintf(\"2\\n\");\n\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(N == 2){\n\t\tprintf(\"Impossible\\n\");\n\t\treturn;\n\t}\n\n\n\tfor(int a = 0; a < N-2; a++){\n\t\tfor(int b = a+1; b < N-1; b++){\n\t\t\tfor(int c = b+1; c < N; c++){\n\t\t\t\tS = POW[a]+POW[b]+POW[c];\n\t\t\t\tinit();\n\n\t\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tcount = 0;\n\t\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t\t}\n\n\t\t\t\tif(count == 1){\n\n\t\t\t\t\tprintf(\"3\\n\");\n\t\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\t\tprintf(\"%s\\n\",name[c]);\n\t\t\t\t\treturn;\n\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(N == 3){\n\t\tprintf(\"Impossible\\n\");\n\t\treturn;\n\t}\n\n\tfor(int a = 0; a < N-3; a++){\n\t\tfor(int b = a+1; b < N-2; b++){\n\t\t\tfor(int c = b+1; c < N-1; c++){\n\t\t\t\tfor(int d = c+1; d < N; d++){\n\t\t\t\t\tS = POW[a]+POW[b]+POW[c]+POW[d];\n\t\t\t\t\tinit();\n\n\t\t\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tcount = 0;\n\t\t\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(count == 1){\n\t\t\t\t\t\tprintf(\"4\\n\");\n\t\t\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\t\t\tprintf(\"%s\\n\",name[c]);\n\t\t\t\t\t\tprintf(\"%s\\n\",name[d]);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(N == 4){\n\t\tprintf(\"Impossible\\n\");\n\t\treturn;\n\t}\n\n\tfor(int a = 0; a < N-4; a++){\n\t\tfor(int b = a+1; b < N-3; b++){\n\t\t\tfor(int c = b+1; c < N-2; c++){\n\t\t\t\tfor(int d = c+1; d < N-1; d++){\n\t\t\t\t\tfor(int e = d+1; e < N; e++){\n\t\t\t\t\t\tS = POW[a]+POW[b]+POW[c]+POW[d]+POW[e];\n\t\t\t\t\t\tinit();\n\n\t\t\t\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tcount = 0;\n\t\t\t\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(count == 1){\n\t\t\t\t\t\t\tprintf(\"5\\n\");\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[c]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[d]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[e]);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"Impossible\\n\");\n}\n\nint main(){\n\n\tfor(int i = 0; i < 31; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 3328, "score_of_the_acc": -1.0302, "final_rank": 11 }, { "submission_id": "aoj_2061_2610868", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nint N,M;\nchar name[30][21];\n\nint check[20];\n\nint boss[20],height[20];\nint POW[31];\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\nvoid init(){\n\tfor(int i = 0; i < M; i++){\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t}\n}\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nint data_set = 0;\n\nvoid func(){\n\n\tif(data_set != 0){\n\t\tprintf(\"\\n\");\n\t}\n\tdata_set++;\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%s\",name[i]);\n\t}\n\n\tfor(int i = 0; i < M; i++)check[i] = 0;\n\n\tchar buf[21];\n\tint num;\n\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d\",&num);\n\t\tfor(int k = 0; k < num; k++){\n\t\t\tscanf(\"%s\",buf);\n\t\t\tfor(int p = 0; p < N; p++){\n\t\t\t\tif(strCmp(name[p],buf)){\n\t\t\t\t\tcheck[i] += POW[p];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tbool FLG;\n\tint S;\n\n\tfor(int a = 0; a < N; a++){\n\t\tFLG = true;\n\t\tS = POW[a];\n\n\t\tfor(int i = 0; i < M; i++){\n\t\t\tif((check[i] & S) == 0){\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG){\n\t\t\tprintf(\"1\\n\");\n\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\treturn;\n\t\t}\n\t}\n\n\tint count;\n\n\tfor(int a = 0; a < N-1; a++){\n\t\tfor(int b = a+1; b < N; b++){\n\t\t\tS = POW[a]+POW[b];\n\t\t\tinit();\n\n\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcount = 0;\n\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t}\n\n\t\t\tif(count == 1){\n\t\t\t\tprintf(\"2\\n\");\n\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\n\tfor(int a = 0; a < N-2; a++){\n\t\tfor(int b = a+1; b < N-1; b++){\n\t\t\tfor(int c = b+1; c < N; c++){\n\t\t\t\tS = POW[a]+POW[b]+POW[c];\n\t\t\t\tinit();\n\n\t\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tcount = 0;\n\t\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t\t}\n\n\t\t\t\tif(count == 1){\n\n\t\t\t\t\tprintf(\"3\\n\");\n\t\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\t\tprintf(\"%s\\n\",name[c]);\n\t\t\t\t\treturn;\n\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int a = 0; a < N-3; a++){\n\t\tfor(int b = a+1; b < N-2; b++){\n\t\t\tfor(int c = b+1; c < N-1; c++){\n\t\t\t\tfor(int d = c+1; d < N; d++){\n\t\t\t\t\tS = POW[a]+POW[b]+POW[c]+POW[d];\n\t\t\t\t\tinit();\n\n\t\t\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tcount = 0;\n\t\t\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(count == 1){\n\t\t\t\t\t\tprintf(\"4\\n\");\n\t\t\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\t\t\tprintf(\"%s\\n\",name[c]);\n\t\t\t\t\t\tprintf(\"%s\\n\",name[d]);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int a = 0; a < N-4; a++){\n\t\tfor(int b = a+1; b < N-3; b++){\n\t\t\tfor(int c = b+1; c < N-2; c++){\n\t\t\t\tfor(int d = c+1; d < N-1; d++){\n\t\t\t\t\tfor(int e = d+1; e < N; e++){\n\t\t\t\t\t\tS = POW[a]+POW[b]+POW[c]+POW[d]+POW[e];\n\t\t\t\t\t\tinit();\n\n\t\t\t\t\t\tfor(int i = 0; i < M-1; i++){\n\t\t\t\t\t\t\tfor(int k = i+1; k < M; k++){\n\t\t\t\t\t\t\t\tif(check[i]&check[k]&S){\n\t\t\t\t\t\t\t\t\tunite(i,k);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tcount = 0;\n\t\t\t\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\t\t\t\tif(get_boss(i) == i)count++;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(count == 1){\n\t\t\t\t\t\t\tprintf(\"5\\n\");\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[a]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[b]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[c]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[d]);\n\t\t\t\t\t\t\tprintf(\"%s\\n\",name[e]);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"Impossible\\n\");\n}\n\nint main(){\n\n\tfor(int i = 0; i < 31; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 3328, "score_of_the_acc": -1.0351, "final_rank": 12 }, { "submission_id": "aoj_2061_2310191", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) begin(v), end(v)\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nusing pint = pair<int, int>;\nusing tint = tuple<int, int, int>;\nusing vint = vector<int>;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nstruct UnionFind\n{\n vector<int> data;\n UnionFind(){}\n UnionFind(int sz):data(sz, -1){};\n int size(int x) { return -data[find(x)]; }\n int find(int x) { return data[x] < 0 ? x : data[x] = find(data[x]); }\n bool same(int x, int y) { return find(x) == find(y); }\n int unite(int x, int y)\n {\n x = find(x), y = find(y);\n if(x != y) {\n if(data[x] < data[y]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return -data[x];\n }\n void init(int sz) {\n data.clear();\n data.resize(sz, -1);\n }\n};\n\nUnionFind uf;\n\nint N, M;\nvector<string> lang;\nmap<string, int> mp;\nmap<string, int> group;\nvector<int> vec;\nvector<string> ans;\n\nbool input() {\n cin >> N >> M;\n if(!N && !M) return false;\n\n lang.clear();\n mp.clear();\n rep(i, N) {\n string l;\n cin >> l;\n lang.push_back(l);\n mp[l] = i;\n }\n\n vec.clear();\n group.clear();\n rep(i, M) {\n int k; cin >> k;\n int bit = 0;\n rep(j, k) {\n string l; cin >> l;\n bit |= 1<<mp[l];\n group[l] |= 1<<i;\n }\n vec.push_back(bit);\n }\n\n return true;\n}\n\nvoid solve(int idx, int bit) {\n if(__builtin_popcount(bit) > 5) return;\n if(uf.size(N) == __builtin_popcount(bit)+M) {\n if(ans.empty() || __builtin_popcountll(bit) < ans.size()) {\n ans.clear();\n rep(i, N) if((bit>>i)&1) ans.push_back(lang[i]);\n }\n }\n\n reps(i, idx, N) {\n if((bit>>i)&1) continue;\n UnionFind tmp = uf;\n rep(j, M) if((vec[j]>>i)&1) uf.unite(i, N+j);\n solve(i+1, bit|(1<<i));\n uf = tmp;\n }\n}\n\nsigned main()\n{\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n bool flag = false;\n while(input()) {\n if(flag) cout << endl;\n flag = true;\n\n ans.clear();\n uf.init(N+M);\n solve(0, 0);\n if(ans.size()) {\n cout << ans.size() << endl;\n for(string l : ans) cout << l << endl;\n } else {\n cout << \"Impossible\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2090, "memory_kb": 3044, "score_of_the_acc": -1.1601, "final_rank": 15 }, { "submission_id": "aoj_2061_2309651", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint p[51],r[51];\nvoid init(){for(int i=0;i<50;i++)p[i]=i,r[i]=0;}\nint find(int x){return (p[x]==x)?x:(p[x]=find(p[x]));}\nvoid unite(int x,int y) {\n x=find(x),y=find(y);\n if(x==y)return;\n if(r[x]<r[y])p[x]=y;\n else{p[y]=x;if(r[x]==r[y])r[x]++;}\n}\nbool same(int x,int y){return find(x)==find(y);}\nvector<string> s,ans;\nset<string> se[50];\nint n,m;\n \nvoid dfs(int k,int t) {\n bool f=1;\n for(int i=1; i<m; i++) if(!same(0,i)) {f=0;break;}\n if(f) {\n if(!ans.size()||ans.size()>__builtin_popcount(t)) {\n ans.clear();\n for(int i=0; i<n; i++) if(t&(1<<i)) ans.push_back(s[i]);\n }\n return;\n }\n if(k>=n) return;\n for(int i=k; i<n; i++) {\n if(__builtin_popcount(t|(1<<i))>5) continue;\n int l=0;\n while(l<m&&!se[l].count(s[i])) l++;\n if(l==m) continue;\n int pp[m],rr[m];\n for(int j=0; j<m; j++) pp[j]=p[j],rr[j]=r[j];\n for(int j=l+1; j<m; j++) {\n if(se[j].count(s[i])) unite(l,j);\n }\n dfs(i+1,t|(1<<i));\n for(int j=0; j<m; j++) p[j]=pp[j],r[j]=rr[j];\n }\n}\n \nint main() {\n int f=0;\n while(cin>>n>>m&&n) {\n if(f) cout<<endl;\n f=1;\n init();\n ans.clear();\n s.clear();\n for(int i=0; i<n; i++) {\n string t;cin>>t;\n s.push_back(t);\n }\n for(int i=0; i<m; i++) {\n se[i].clear();\n int k;cin>>k;\n for(int j=0; j<k; j++) {\n string t;cin>>t;\n se[i].insert(t);\n }\n }\n dfs(0,0);\n if(ans.size()) {\n cout<<ans.size()<<endl;\n for(int i=0; i<ans.size(); i++)cout<<ans[i]<<endl;\n } else cout<<\"Impossible\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1930, "memory_kb": 3088, "score_of_the_acc": -1.1527, "final_rank": 14 }, { "submission_id": "aoj_2061_2309147", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint A[21];\nint lan[30];\nint m,n;\n\nvector<int>ans,used;\n\nvoid dfs(int dep,int member,int use){\n if(member == (1<<n)-1) {\n if(ans.size()==0 || ans.size()> used.size()) ans = used;\n return ;\n }\n \n if(dep >= m || dep>=5)return;\n \n for(int i=0;i<m;i++){\n if(use&&(use>>i&1)==0) continue;\n \n int nmember = member | lan[i];\n int nuse = use;\n for(int j=0;j<n;j++)\n if(lan[i]>>j&1) nuse |= A[j];\n \n used.push_back(i);\n dfs(dep+1,nmember,nuse);\n used.pop_back();\n }\n}\n\nint main(){\n int flg = 0;\n while(1){\n \n cin>>m>>n;\n if(!m&&!n)break;\n if(flg++) cout<<endl;\n map<string,int> M;\n string name[30];\n for(int i=0;i<m;i++){\n string str;\n cin>>str;\n M[str] = i;\n name[i] = str;\n }\n\n memset(lan,0,sizeof(lan));\n memset(A,0,sizeof(A));\n \n for(int i=0;i<n;i++){\n int k;\n cin>>k;\n for(int j=0;j<k;j++){\n string str;\n cin>>str;\n A[i] |= 1<<M[str];\n lan[M[str]] |= 1<<i;\n }\n }\n\n ans.clear();used.clear();\n dfs(0,0,0);\n if(ans.size()==0) cout<<\"Impossible\"<<endl;\n else {\n cout<<ans.size()<<endl;\n for(int i=0;i<ans.size();i++)cout<<name[ans[i]]<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6180, "memory_kb": 3188, "score_of_the_acc": -1.8862, "final_rank": 20 }, { "submission_id": "aoj_2061_2308833", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint p[51],r[51];\nvoid init(){for(int i=0;i<50;i++)p[i]=i,r[i]=0;}\nint find(int x){return (p[x]==x)?x:(p[x]=find(p[x]));}\nvoid unite(int x,int y) {\n x=find(x),y=find(y);\n if(x==y)return;\n if(r[x]<r[y])p[x]=y;\n else{p[y]=x;if(r[x]==r[y])r[x]++;}\n}\nbool same(int x,int y){return find(x)==find(y);}\nvector<string> s,ans;\nset<string> se[50];\nint n,m;\n\nvoid dfs(int k,int t) {\n bool f=1;\n for(int i=1; i<m; i++) if(!same(0,i)) f=0;\n if(f) {\n if(!ans.size()||ans.size()>__builtin_popcount(t)) {\n ans.clear();\n for(int i=0; i<n; i++) {\n if(t&(1<<i)) ans.push_back(s[i]);\n }\n }\n return;\n }\n if(k>=n) return;\n for(int i=k; i<n; i++) {\n if(__builtin_popcount(t|(1<<i))>5) continue;\n int l=0;\n while(l<m&&!se[l].count(s[i])) l++;\n if(l==m) continue;\n int pp[m],rr[m];\n for(int j=0; j<m; j++) pp[j]=p[j],rr[j]=r[j];\n for(int j=l+1; j<m; j++) {\n if(se[j].count(s[i])) unite(l,j);\n }\n dfs(i+1,t|(1<<i));\n for(int j=0; j<m; j++) p[j]=pp[j],r[j]=rr[j];\n }\n}\n\nint main() {\n int f=0;\n while(cin >> n >> m && n) {\n if(f) cout << endl;\n f=1;\n init();\n ans.clear();\n s.clear();\n for(int i=0; i<n; i++) {\n string t;\n cin >> t;\n s.push_back(t);\n }\n for(int i=0; i<m; i++) {\n se[i].clear();\n int k;\n cin >> k;\n for(int j=0; j<k; j++) {\n string t;\n cin >> t;\n se[i].insert(t);\n }\n }\n dfs(0,0);\n if(ans.size()) {\n cout << ans.size() << endl;\n for(int i=0; i<ans.size(); i++) cout << ans[i] << endl;\n } else cout << \"Impossible\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2300, "memory_kb": 3168, "score_of_the_acc": -1.2469, "final_rank": 17 }, { "submission_id": "aoj_2061_2041075", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n\nnamespace tree {\nstruct union_find {\n static const int Max = 22;\n int par[Max], rank[Max], size[Max], compnum;\n\n union_find(int N) {\n compnum = N;\n for(int i=0; i<N; i++) {\n par[i] = i;\n rank[i] = 0;\n size[i] = 1;\n }\n }\n\n int root(int x) {\n return par[x] == x ? x : par[x] = root(par[x]);\n }\n\n void unite(int x, int y) {\n x = root(x), y = root(y);\n if(x == y) return;\n if(rank[x] < rank[y]) {\n par[x] = y, size[y] += size[x];\n } else {\n par[y] = x, size[x] += size[y];\n if(rank[x] == rank[y]) rank[x]++;\n }\n compnum--;\n }\n\n int operator[](int x) { return root(x); }\n void operator()(int x, int y) { return unite(x, y); }\n\n bool same(int x, int y) { return root(x) == root(y); }\n int size_of(int x) { return size[root(x)]; }\n int num_of_comps() { return compnum; }\n};\n}\n\nint N, M;\nunordered_map<string, int> idmap;\nvector<string> langs;\nbool speaks[23][33];\n\nvoid input() {\n langs.clear(); langs.resize(N);\n rep(i, N) {\n cin >> langs[i];\n idmap[langs[i]] = i;\n }\n memset(speaks, 0, sizeof speaks);\n rep(i, M) {\n int k; cin >> k;\n rep(_, k) {\n string s; cin >> s;\n speaks[i][idmap[s]] = 1;\n }\n }\n}\n\nbool next_combination(int& comb, int const BitSize) {\n int x = comb & -comb, y = comb + x;\n comb = ((comb & ~y) / x >> 1) | y;\n return comb < 1 << BitSize;\n}\n\nvoid solve() {\n\n REP(K, 1, min(N + 1, 6)) {\n int comb = (1 << K) - 1;\n do {\n vector<int> uselangs;\n rep(i, N) {\n if(!(comb & 1 << i)) continue;\n uselangs.push_back(i);\n }\n\n tree::union_find uf(M);\n\n rep(i, M) REP(j, i + 1, M)\n for(auto langid: uselangs)\n if(speaks[i][langid] && speaks[j][langid])\n uf(i, j);\n\n if(uf.num_of_comps() == 1) {\n printf(\"%d\\n\", (int)uselangs.size());\n for(auto e: uselangs) printf(\"%s\\n\", langs[e].c_str());\n return;\n }\n } while(next_combination(comb, N));\n }\n puts(\"Impossible\");\n}\n\nint main() {\n bool first = 0;\n for(; cin >> N >> M && (N|M);) {\n if(first) cout << endl;\n first = 1;\n input();\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 2100, "memory_kb": 3364, "score_of_the_acc": -1.2975, "final_rank": 19 }, { "submission_id": "aoj_2061_2041049", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n\nnamespace tree {\nstruct union_find {\n static const int Max = 22;\n int par[Max], rank[Max], size[Max], compnum;\n\n union_find(int N) {\n compnum = N;\n for(int i=0; i<N; i++) {\n par[i] = i;\n rank[i] = 0;\n size[i] = 1;\n }\n }\n\n int root(int x) {\n return par[x] == x ? x : par[x] = root(par[x]);\n }\n\n void unite(int x, int y) {\n x = root(x), y = root(y);\n if(x == y) return;\n if(rank[x] < rank[y]) {\n par[x] = y, size[y] += size[x];\n } else {\n par[y] = x, size[x] += size[y];\n if(rank[x] == rank[y]) rank[x]++;\n }\n compnum--;\n }\n\n int operator[](int x) { return root(x); }\n void operator()(int x, int y) { return unite(x, y); }\n\n bool same(int x, int y) { return root(x) == root(y); }\n int size_of(int x) { return size[root(x)]; }\n int num_of_comps() { return compnum; }\n};\n}\n\nint N, M;\nunordered_map<string, int> idmap;\nvector<string> langs;\nbool speaks[23][33];\n\nvoid input() {\n langs.clear(); langs.resize(N);\n rep(i, N) {\n cin >> langs[i];\n idmap[langs[i]] = i;\n }\n memset(speaks, 0, sizeof speaks);\n rep(i, M) {\n int k; cin >> k;\n rep(_, k) {\n string s; cin >> s;\n speaks[i][idmap[s]] = 1;\n }\n }\n}\n\nbool next_combination(int& comb, int const BitSize) {\n // Assume: comb := (1 << K) - 1, K <= BitSize\n int x = comb & -comb, y = comb + x;\n comb = ((comb & ~y) / x >> 1) | y;\n return comb < 1 << BitSize;\n}\n\nvoid solve() {\n\n REP(K, 1, min(N + 1, 6)) {\n int comb = (1 << K) - 1;\n do {\n vector<int> uselangs;\n rep(i, N) {\n if(!(comb & 1 << i)) continue;\n uselangs.push_back(i);\n }\n\n tree::union_find uf(M);\n\n rep(i, M) REP(j, i + 1, M)\n for(auto langid: uselangs)\n if(speaks[i][langid] && speaks[j][langid])\n uf(i, j);\n\n if(uf.num_of_comps() == 1) {\n printf(\"%d\\n\", (int)uselangs.size());\n for(auto e: uselangs) printf(\"%s\\n\", langs[e].c_str());\n return;\n }\n } while(next_combination(comb, N));\n }\n puts(\"Impossible\");\n}\n\nstring to_binary(int x, int const LeastDigit = 0) {\n string ret;\n while(x) ret.push_back(x % 2 + '0'), x /= 2;\n for(int i=ret.size(); i<LeastDigit; i++) ret.push_back('0');\n reverse(ret.begin(), ret.end());\n return ret;\n}\n\nint main() {\n bool first = 0;\n for(; cin >> N >> M && (N|M);) {\n if(first) cout << endl;\n first = 1;\n input();\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 2050, "memory_kb": 3376, "score_of_the_acc": -1.2945, "final_rank": 18 }, { "submission_id": "aoj_2061_1882442", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX_N 30\n#define MAX_M 20\n#define INF (1<<29)\n \nint N,M,mini,ansS;\nbool edge[MAX_M][MAX_N];\n \nclass Union_Find{\npublic:\n int par[MAX_M],rank[MAX_M],size[MAX_M];\n Union_Find(int N){\n\tfor(int i = 0 ; i < N ; i++){\n\t par[i] = i;\n\t rank[i] = 0;\n\t size[i] = 1;\n\t}\n }\n \n int find(int x){\n\tif(par[x] == x){\n\t return x;\n\t}\n\treturn par[x] = find(par[x]);\n }\n \n void unite(int x,int y){\n\tx = find(x);\n\ty = find(y);\n\tif(x == y) return;\n\tif(rank[x] < rank[y]){\n\t par[x] = y;\n\t size[y] += size[x];\n\t}else{\n\t par[y] = x;\n\t size[x] += size[y];\n\t if(rank[x] == rank[y]){\n\t\trank[x]++;\n\t }\n\t}\n }\n \n bool same(int x,int y){\n\treturn find(x) == find(y);\n }\n \n int getSize(int x){\n\treturn size[find(x)];\n }\n};\n \nbool check(int S){\n int idx = 0,arr[MAX_M];\n if(S == 0) return false;\n for(int i = 0 ; i < N ; i++){\n\tif(S >> i & 1){\n\t arr[idx++] = i;\n\t}\n }\n Union_Find uf(M);\n for(int i = 0 ; i < M ; i++){\n\tfor(int j = i+1 ; j < M ; j++){\n\t if(uf.same(i,j)) continue;\n\t for(int k = 0 ; k < idx ; k++){\n\t\tif(edge[i][arr[k]] && edge[j][arr[k]]){\n\t\t uf.unite(i,j);\n\t\t}\n\t }\n\t}\n }\n return (uf.getSize(0) == M);\n}\n \nvoid rec(int idx,int S,int num){\n if(num < mini && check(S)){\n\tmini = num; ansS = S;\n\treturn;\n }\n if(num == 5 || num >= mini) return;\n for(int i = idx ; i < N ; i++){\n\tif(S >> i & 1) continue;\n\trec(i+1,S|(1<<i),num+1);\n }\n}\n \nint main(){\n int K;\n bool blank = false;\n string lang[MAX_N],in;\n while(cin >> N >> M, N){\n\tmap<string,int> mp;\n\tmemset(edge,false,sizeof(edge));\n\tif(blank){ cout << endl; }\n\tblank = true;\n\tfor(int i = 0 ; i < N ; i++){\n\t cin >> lang[i];\n\t mp[lang[i]] = i;\n\t}\n\tfor(int i = 0 ; i < M ; i++){\n\t cin >> K;\n\t for(int j = 0 ; j < K ; j++){\n\t\tcin >> in;\n\t\tedge[i][mp[in]] = true;\n\t }\n\t}\n\tmini = INF;\n\trec(0,0,0);\n\tif(mini == INF){\n\t cout << \"Impossible\" << endl;\n\t}else{\n\t cout << __builtin_popcount(ansS) << endl;\n\t for(int i = 0 ; i < N ; i++){\n\t\tif(ansS >> i & 1){\n\t\t cout << lang[i] << endl;\n\t\t}\n\t }\n\t}\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2350, "memory_kb": 3148, "score_of_the_acc": -1.2465, "final_rank": 16 }, { "submission_id": "aoj_2061_1594337", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <iostream>\n#include <cstdio>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <complex>\n#include <assert.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair <int,int> P;\n\nstatic const double EPS = 1e-12;\n\nstatic const int tx[] = {+0,+1,+0,-1};\nstatic const int ty[] = {-1,+0,+1,+0};\n\nclass UnionFindTree {\n int* _par;\n int* _rank;\npublic:\n UnionFindTree(int N){\n _par = new int[N];\n _rank = new int[N];\n for(int i = 0; i < N; i++){\n _par[i] = i;\n _rank[i] = 0;\n }\n }\n\n ~UnionFindTree(){\n delete[] _par;\n delete[] _rank;\n }\n\n int find(int u){\n if(_par[u] == u) return u;\n return find(_par[u]);\n }\n\n bool unite(int u,int v){\n u = find(u);\n v = find(v);\n if(u == v) return false;\n \n if(_rank[u] < _rank[v]){\n _par[u] = v;\n }\n else{\n _par[v] = u;\n if(_rank[u] == _rank[v]) _rank[u]++;\n }\n return true;\n }\n};\n\nint main(){\n int num_of_languages;\n int num_of_students;\n bool is_first = true;\n while(~scanf(\"%d %d\",&num_of_languages,&num_of_students)){\n if(num_of_languages == 0 && num_of_students == 0){\n break;\n }\n if(!is_first){\n printf(\"\\n\");\n }\n map<string,int> lang2idx;\n map<int,string> idx2lang;\n for(int lang_i = 0; lang_i < num_of_languages; lang_i++){\n string lang;\n cin >> lang;\n lang2idx[lang] = lang_i;\n idx2lang[lang_i] = lang;\n }\n\n int students[21] = {};\n for(int student_i = 0; student_i < num_of_students; student_i++){\n int num_of_skills;\n scanf(\"%d\",&num_of_skills);\n for(int skill_i = 0; skill_i < num_of_skills; skill_i++){\n string skill;\n cin >> skill;\n students[student_i] |= (1 << lang2idx[skill]);\n }\n }\n\n int res = 0;\n\n\n for(int used = 1; used <= 5; used++){\n vector<int> languages;\n for(int i = 0; i < min(used,num_of_languages); i++){\n languages.push_back(1);\n }\n for(int i = 0; i < num_of_languages - used; i++){\n languages.push_back(0);\n }\n \n sort(languages.begin(),languages.end());\n do {\n int S = 0;\n for(int i = 0; i < languages.size(); i++){\n if(languages[i] == 1){\n S |= (1<<i);\n }\n }\n UnionFindTree uft(30);\n for(int student_i = 0; student_i < num_of_students; student_i++){\n for(int student_j = student_i + 1; student_j < num_of_students; student_j++){\n if(students[student_i] & students[student_j] & S){\n uft.unite(student_i,student_j);\n }\n }\n }\n \n set<int> parents;\n for(int student_i = 0; student_i < num_of_students; student_i++){\n parents.insert(uft.find(student_i));\n }\n if(parents.size() == 1){\n res = S;\n goto found;\n }\n } while(next_permutation(languages.begin(),languages.end()));\n }\n found:;\n\n if(res != 0){\n cout << __builtin_popcount(res) << endl;\n for(int lang_i = 0; lang_i < num_of_languages; lang_i++){\n if(res & (1<<lang_i)){\n cout << idx2lang[lang_i] << endl;\n }\n }\n }\n else{\n cout << \"Impossible\" << endl;\n }\n is_first = false;\n }\n}", "accuracy": 1, "time_ms": 1340, "memory_kb": 1292, "score_of_the_acc": -0.2945, "final_rank": 5 }, { "submission_id": "aoj_2061_1326048", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX_N 30\n#define MAX_M 20\n#define INF (1<<29)\n\nint N,M,mini,ansS;\nbool edge[MAX_M][MAX_N];\n\nclass Union_Find{\npublic:\n int par[MAX_M],rank[MAX_M],size[MAX_M];\n Union_Find(int N){\n for(int i = 0 ; i < N ; i++){\n par[i] = i;\n rank[i] = 0;\n size[i] = 1;\n }\n }\n\n int find(int x){\n if(par[x] == x){\n return x;\n }\n return par[x] = find(par[x]);\n }\n\n void unite(int x,int y){\n x = find(x);\n y = find(y);\n if(x == y){ return; }\n if(rank[x] < rank[y]){\n par[x] = y;\n size[y] += size[x];\n }else{\n par[y] = x;\n size[x] += size[y];\n if(rank[x] == rank[y]){\n rank[x]++;\n }\n }\n }\n\n bool same(int x,int y){\n return find(x) == find(y);\n }\n\n int getSize(int x){\n return size[find(x)];\n }\n};\n\nbool check(int S){\n int idx = 0,arr[MAX_M];\n if(S == 0){ return false; }\n for(int i = 0 ; i < N ; i++){\n if(S >> i & 1){\n arr[idx++] = i;\n }\n }\n Union_Find uf(M);\n for(int i = 0 ; i < M ; i++){\n for(int j = i+1 ; j < M ; j++){\n if(uf.same(i,j)){ continue; }\n for(int k = 0 ; k < idx ; k++){\n if(edge[i][arr[k]] && edge[j][arr[k]]){\n uf.unite(i,j);\n }\n }\n }\n }\n return (uf.getSize(0) == M);\n}\n\nvoid rec(int idx,int S,int num){\n if(num < mini && check(S)){\n mini = num; ansS = S;\n return;\n }\n if(num == 5 || num >= mini){ return; }\n for(int i = idx ; i < N ; i++){\n if(S >> i & 1){ continue; }\n rec(i+1,S|(1<<i),num+1);\n }\n}\n\nint main(){\n int K;\n bool blank = false;\n string lang[MAX_N],in;\n while(cin >> N >> M, N){\n map<string,int> mp;\n memset(edge,false,sizeof(edge));\n if(blank){ cout << endl; }\n blank = true;\n for(int i = 0 ; i < N ; i++){\n cin >> lang[i];\n mp[lang[i]] = i;\n }\n for(int i = 0 ; i < M ; i++){\n cin >> K;\n for(int j = 0 ; j < K ; j++){\n cin >> in;\n edge[i][mp[in]] = true;\n }\n }\n mini = INF;\n rec(0,0,0);\n if(mini == INF){\n cout << \"Impossible\" << endl;\n }else{\n cout << __builtin_popcount(ansS) << endl;\n for(int i = 0 ; i < N ; i++){\n if(ansS >> i & 1){\n cout << lang[i] << endl;\n }\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3090, "memory_kb": 1224, "score_of_the_acc": -0.5502, "final_rank": 7 }, { "submission_id": "aoj_2061_1150021", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<string>\n#include<map>\nusing namespace std;\nchar in[1100];\nint c[40][40];\nstring t[40];\nint v[40];\nint n;\nint sz;\nint ret;\nint ans[40];\nint UF[40];\nint FIND(int a){\n\tif(UF[a]<0)return a;\n\treturn UF[a]=FIND(UF[a]);\n}\nvoid UNION(int a,int b){\n\ta=FIND(a);b=FIND(b);if(a==b)return;UF[a]+=UF[b];UF[b]=a;\n}\nvoid solve(int a,int b){\n\tif(ret==a)return ;\n\tbool ok=true;\n\tfor(int i=0;i<sz;i++)UF[i]=-1;\n\tfor(int i=0;i<sz;i++){\n\t\tfor(int j=i+1;j<sz;j++){\n\t\t\tbool OK=false;\n\t\t\tfor(int k=0;k<a;k++)if(c[i][v[k]]&&c[j][v[k]])OK=true;\n\t\t\tif(OK)UNION(i,j);\n\t\t}\n\t}\n\tif(UF[FIND(0)]==-sz){\n\t\tret=a;\n\t\tfor(int i=0;i<a;i++)ans[i]=v[i];\n\t\treturn;\n\t}\n\tfor(int i=b;i<n;i++){\n\t\tv[a]=i;\n\t\tsolve(a+1,i+1);\n\t}\n}\nint main(){\n\tint a,b;\n\tbool se=false;\n\twhile(scanf(\"%d%d\",&a,&b),a){\n\t\tif(se)printf(\"\\n\");\n\t\tse=true;\n\t\tmap<string,int>m;\n\t\tn=a;\n\t\tsz=b;\n\t\tret=6;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tscanf(\"%s\",in);\n\t\t\tstring tmp=in;\n\t\t\tt[i]=tmp;\n\t\t\tm[tmp]=i;\n\t\t}\n\t\tfor(int i=0;i<40;i++)for(int j=0;j<40;j++)c[i][j]=0;\n\t\tfor(int i=0;i<b;i++){\n\t\t\tint d;scanf(\"%d\",&d);\n\t\t\tfor(int j=0;j<d;j++){\n\t\t\t\tscanf(\"%s\",in);\n\t\t\t\tstring tmp=in;\n\t\t\t\tc[i][m[tmp]]=1;\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor(int i=0;i<a;i++)v[i]=0;\n\t\tsolve(0,0);\n\t\tif(ret==6){\n\t\t\tprintf(\"Impossible\\n\");\n\t\t}else{\n\t\t\tprintf(\"%d\\n\",ret);\n\t\t\tfor(int i=0;i<ret;i++)printf(\"%s\\n\",t[ans[i]].c_str());\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 2220, "memory_kb": 1100, "score_of_the_acc": -0.3561, "final_rank": 6 }, { "submission_id": "aoj_2061_1123839", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <map>\n#include <complex>\n#include <cstdio>\nusing namespace std;\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\n\nint N,M;\nvector<string> cand;\nmap<string, vector<int> > dic;\nmap<string,int> id;\n\nint check(vector<string> lan){\n\tUnionFind uf(N+M);\n\tfor( auto item : lan ){\n\t\tint u = id[item];\n\t\t\n\t\tfor( auto v : dic[item] )\n\t\t\tuf.unionSet(u+M,v);\n\t}\n\n\tfor(int i = 0 ; i < M ; i++)\n\t\tif( !uf.findSet(0,i) ) return false;\n\treturn true;\n}\nint good = 6;\nvector<string> answer;\nvector<string> now;\nint cur;\nint dfs(int x){\n\tif( good <= cur ) return 0;\n\tif( check(now) ){\n\t\tanswer = now;\n\t\tgood = cur;\n\t\treturn 0;\n\t}\n\tif( N <= x ) return 0;\n\tdfs(x+1);\n\t\n\tcur++;\n\tnow.push_back(cand[x]);\n\tdfs(x+1);\n\tnow.pop_back();\n\tcur--;\n\t\n}\nint main(){\n\tint fst = 0;\n\twhile(cin >> N >> M && N){\n\t\tif(fst++)cout << endl;\n\t\tcand.clear();\n\t\tdic.clear();\n\t\tfor(int i = 0 ; i < N ; i++){\n\t\t\tstring t;\n\t\t\tcin >> t;\n\t\t\tcand.push_back(t);\n\t\t}\n\t\tfor(int k = 0 ; k < M ; k++){\n\t\t\tint t;\n\t\t\tcin >> t;\n\t\t\tfor(int j = 0 ; j < t ; j++){\n\t\t\t\tstring s;\n\t\t\t\tcin >> s;\n\t\t\t\tdic[s].push_back(k);\n\t\t\t}\n\t\t}\n\t\tint k = 0;\n\t\tfor( auto x : dic){\n\t\t\tid[x.first] = k++;\n\t\t}\n\t\tgood = 6;\n\t\tcur = 0;\n\t\tdfs(0);\n\t\tif( good >= 6 ){\n\t\t\tcout << \"Impossible\" << endl;\n\t\t}else{\n\t\t\tcout << good << endl;\n\t\t\tfor(int i = 0 ; i < answer.size() ; i++){\n\t\t\t\tcout << answer[i] << endl;\n\t\t\t}\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 5670, "memory_kb": 1364, "score_of_the_acc": -1.0291, "final_rank": 10 }, { "submission_id": "aoj_2061_851813", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cmath>\n#include<cassert>\n#include<climits>\n#include<set>\n#include<vector>\n#include<algorithm>\n#include<map>\n#include<bitset>\n#include<deque>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define MAX 35\n \nusing namespace std;\n \nint N,M,dex,limit;\ndeque<int> ans;\nvector<string> LG;\nmap<string,int> Index;\nvector<int> G[MAX];\nvector<int> bitG;\nset<int> input[MAX];\nstring country;\nbool used[MAX];\n \nvoid cdfs(int cur,deque<int>& selected){\n if(used[cur])return;\n used[cur] = true;\n //cout << \"cur = \" << cur << endl;\n for(int i=0;i<G[cur].size();i++){\n int next = G[cur][i];\n \n if(used[next])continue;\n \n bool ok = false;\n for(int j=0;j<selected.size();j++){\n int one = selected[j];\n if( ( (bitG[one]>>cur) & 1 ) && ( (bitG[one]>>next) & 1 ) ){\n ok = true;\n break;\n }\n }\n\n if(ok){\n //cout << \"from \" << cur << \" to \" << next << endl;\n cdfs(next,selected);\n }\n }\n}\n \n \nbool check(deque<int>& selected){\n for(int i=0;i<M;i++)used[i] = false;\n \n int OK = bitG[selected[0]];\n while(OK){\n int use = OK & -OK;\n int cur = log2(use);\n OK -= use;\n cdfs(cur,selected);\n }\n \n for(int i=0;i<M;i++)if(!used[i])return false;\n return true;\n}\n \nvoid dfs(int cur,int language,int student,deque<int>& selected){\n \n if(language >= limit || language >= 6){\n return;\n }\n \n if(student == (1<<M)-1){\n if(!check(selected))return;\n if(limit > language){\n limit = language;\n ans = selected;\n }\n return;\n }\n \n for(int i=cur;i<N;i++){\n selected.push_back(i);\n dfs(i+1,language+1,student|bitG[i],selected);\n selected.pop_back();\n }\n \n}\n \n \nint main(){\n \n bool first = true;\n while(cin >> N >> M,N|M){\n \n if(!first)puts(\"\");\n first = false;\n \n //init\n LG.clear();\n dex = 0;\n Index.clear();\n bitG.clear();\n ans.clear();\n for(int i=0;i<max(N,M);i++){\n G[i].clear();\n input[i].clear();\n }\n \n bitG.resize(N,0);\n \n for(int i=0;i<N;i++){\n cin >> country;\n LG.push_back(country);\n Index[country] = dex++;\n }\n \n int k;\n for(int i=0;i<M;i++){\n cin >> k;\n for(int j=0;j<k;j++){\n cin >> country;\n int index = Index[country];\n input[i].insert(index);\n bitG[index] |= (1<<i);\n }\n }\n \n //make graph\n for(int i=0;i<M;i++){\n for(int j=i+1;j<M;j++){\n for(set<int>::iterator it = input[i].begin(); it != input[i].end(); it++){\n int value = *it;\n if(input[j].find(value) != input[j].end()){\n G[i].push_back(j);\n G[j].push_back(i);\n }\n }\n }\n }\n \n limit = IINF;\n deque<int> deq;\n dfs(0,0,0,deq);\n \n if(limit == IINF){\n cout << \"Impossible\" << endl;\n } else {\n cout << limit << endl;\n for(int i=0;i<limit;i++){\n cout << LG[ans[i]] << endl;\n }\n }\n \n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1320, "score_of_the_acc": -0.0934, "final_rank": 1 }, { "submission_id": "aoj_2061_851694", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cmath>\n#include<cassert>\n#include<climits>\n#include<set>\n#include<vector>\n#include<algorithm>\n#include<map>\n#include<bitset>\n#include<deque>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define MAX 35\n\nusing namespace std;\n\nint N,M,dex,limit;\ndeque<int> ans;\nvector<string> LG;\nmap<string,int> Index;\nvector<int> G[MAX];\nvector<int> bitG;\nset<int> input[MAX];\nstring country;\nbool used[MAX];\n\nvoid cdfs(int cur,deque<int>& selected){\n if(used[cur])return;\n used[cur] = true;\n //cout << \"cur = \" << cur << endl;\n for(int i=0;i<G[cur].size();i++){\n int next = G[cur][i];\n\n if(used[next])continue;\n\n bool ok = false;\n for(int j=0;j<selected.size();j++){\n int one = selected[j];\n if( ( (bitG[one]>>cur) & 1 ) && ( (bitG[one]>>next) & 1 ) ){\n\tok = true;\n\tbreak;\n }\n }\n /*\n for(int j=0;j<selected.size();j++){\n int one = selected[j];\n for(int k=j;k<selected.size();k++){\n\tint two = selected[k];\n\tif( ( ((bitG[one]>>cur)&1) & ((bitG[two]>>next)&1) ) || ( ((bitG[two]>>cur)&1) & ((bitG[one]>>next)&1) ) ){\n\t cout << \"from \" << cur << \" to \" << next << \" is ok because they have \" << LG[one] << \" and \" << LG[two] << endl;\n\t ok = true;\n\t goto Skip;\n\t}\n }\n }\n Skip:;\n */\n if(ok){\n //cout << \"from \" << cur << \" to \" << next << endl;\n cdfs(next,selected);\n }\n }\n}\n\n\nbool check(deque<int>& selected){\n for(int i=0;i<M;i++)used[i] = false;\n\n int OK = bitG[selected[0]];\n while(OK){\n int use = OK & -OK;\n int cur = log2(use);\n OK -= use;\n cdfs(cur,selected);\n }\n\n for(int i=0;i<M;i++)if(!used[i])return false;\n return true;\n}\n\nvoid dfs(int cur,int language,int student,deque<int>& selected){\n\n if(language >= limit || language >= 6){\n return;\n }\n\n if(student == (1<<M)-1){\n if(!check(selected))return;\n if(limit > language){\n limit = language;\n ans = selected;\n }\n return;\n }\n\n for(int i=cur;i<N;i++){\n selected.push_back(i);\n dfs(i+1,language+1,student|bitG[i],selected);\n selected.pop_back();\n }\n\n}\n\n\nint main(){\n\n bool first = true;\n while(cin >> N >> M,N|M){\n\n if(!first)puts(\"\");\n first = false;\n\n //init\n LG.clear();\n dex = 0;\n Index.clear();\n bitG.clear();\n ans.clear();\n for(int i=0;i<max(N,M);i++){\n G[i].clear();\n input[i].clear();\n }\n \n bitG.resize(N,0);\n\n for(int i=0;i<N;i++){\n cin >> country;\n LG.push_back(country);\n Index[country] = dex++;\n }\n\n int k;\n for(int i=0;i<M;i++){\n cin >> k;\n for(int j=0;j<k;j++){\n\tcin >> country;\n\tint index = Index[country];\n\tinput[i].insert(index);\n\tbitG[index] |= (1<<i);\n }\n }\n\n //make graph\n for(int i=0;i<M;i++){\n for(int j=i+1;j<M;j++){\n\tfor(set<int>::iterator it = input[i].begin(); it != input[i].end(); it++){\n\t int value = *it;\n\t if(input[j].find(value) != input[j].end()){\n\t G[i].push_back(j);\n\t G[j].push_back(i);\n\t }\n\t}\n }\n }\n /* \n rep(i,N){\n bitset<10> state = bitG[Index[LG[i]]];\n cout << LG[i] << \" = \" << Index[LG[i]] << \" \" << state << endl;\n }\n\n deque<int> deq;\n deq.push_back(0);\n deq.push_back(1);\n deq.push_back(3);\n deq.push_back(5);\n cout << check(deq) << endl;\n */\n limit = IINF;\n deque<int> deq;\n dfs(0,0,0,deq);\n\n if(limit == IINF){\n cout << \"Impossible\" << endl;\n } else {\n cout << limit << endl;\n for(int i=0;i<limit;i++){\n\tcout << LG[ans[i]] << endl;\n }\n }\n\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1320, "score_of_the_acc": -0.0934, "final_rank": 1 }, { "submission_id": "aoj_2061_771806", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\ntypedef long long ll;\n\nconst int INF = 1000000000;\nconst int MOD = 1000000007;\nconst double EPS = 1e-8;\n\nint nextcomb(int S){\n int x = S & -S;\n int y = S + x;\n S = ((S & ~y) / x >> 1) | y;\n return S;\n}\n\nint main(){\n for(int N, M, first = 1; cin >> N >> M && N; first = 0){\n if(!first) cout << endl;\n\n vector<string> lang(N);\n map<string, int> lang_dic;\n REP(i, N){\n string s; cin >> s;\n lang[i] = s;\n lang_dic[s] = i;\n }\n\n bool have[30][30] = {};\n for(int i = 0; i < M; i++){\n int K; cin >> K;\n while(K--){\n string s;\n cin >> s;\n have[i][ lang_dic[s] ] = true;\n }\n }\n\n vector<int> ans;\n\n bool b[30][30] = {};\n REP(i, N) REP(j, N) REP(k, M)\n b[i][j] |= have[k][i] & have[k][j];\n\n for(int K = 1; K < 6; K++){\n if(ans.size() > 0) break;\n int S = (1 << K) - 1;\n while(S < 1 << N){\n vector<int> v;\n for(int i = 0; i < N; i++) if(S >> i & 1) v.push_back(i);\n S = nextcomb(S);\n bool ok = true;\n\n REP(i, M) if(none_of(v.begin(), v.end(), [&](int vi){ return have[i][vi]; })) ok = false;\n\n if(not ok) continue;\n\n bool bb[30][30] = {};\n for(auto vi : v) for(auto vj : v) bb[vi][vj] = b[vi][vj];\n for(auto vk : v) for(auto vi : v) for(auto vj : v) bb[vi][vj] |= bb[vi][vk] & bb[vk][vj];\n\n REP(i, M) REP(j, i){\n bool talk = false;\n for(auto vi : v) for(auto vj : v) if(bb[vi][vj] && have[i][vi] && have[j][vj]){\n goto NEXT;\n }\n ok = false;\n goto NEXT2;\n\n NEXT:;\n }\n\n if(ok) ans = v;\n NEXT2:;\n\n }\n }\n if(ans.empty()){\n cout << \"Impossible\" << endl;\n continue;\n }\n cout << ans.size() << endl;\n for(auto ai : ans){\n cout << lang[ai] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 1240, "score_of_the_acc": -0.1505, "final_rank": 3 }, { "submission_id": "aoj_2061_677236", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <map>\n#include <vector>\nusing namespace std;\n\nconst int MAXN = 50;\nconst int MAXM = 40;\n\nclass UnionFind {\nprivate:\n int n;\n int *par, *rank;\npublic:\n UnionFind(int _n) {\n n = _n;\n par = new int[n];\n rank = new int[n];\n for(int i = 0; i < n; ++i) {\n par[i] = i;\n rank[i] = 0;\n }\n }\n\n ~UnionFind() {\n delete [] par;\n delete [] rank;\n }\n\n int find(int x) {\n if(par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if(x == y) return;\n if(rank[x] < rank[y]) {\n par[x] = y;\n } else {\n par[y] = x;\n if(rank[x] == rank[y]) {\n ++rank[x];\n }\n }\n }\n};\n\nstruct Edge {\n int a, b;\n};\n\nint N, M;\nmap<string, int> L;\nvector<string> rL;\nvector<int> S;\nvector<int> V[6];\nvector<Edge> E[MAXN];\n\nvoid rec(int k, int i, int bit) {\n V[k].push_back(bit);\n if(k == 5) return;\n for(; i < N; ++i) {\n if(bit & (1<<i)) continue;\n rec(k+1, i+1, bit|(1<<i));\n }\n}\n\nbool test(int bit) {\n UnionFind uf(M);\n int cnt = 0;\n for(int i = 0; i < N; ++i) {\n if(bit & (1<<i)) {\n for(int j = 0; j < E[i].size(); ++j) {\n const Edge &e = E[i][j];\n if(!uf.same(e.a, e.b)) {\n uf.unite(e.a, e.b);\n ++cnt;\n }\n }\n }\n }\n return cnt == M-1;\n}\n\nint main() {\n bool first = true;\n while(cin >> N >> M && (N|M)) {\n L.clear();\n rL.clear();\n for(int i = 0; i < N; ++i) {\n string s;\n cin >> s;\n L[s] = L.size()-1;\n rL.push_back(s);\n }\n S = vector<int>(M, 0);\n for(int j = 0; j < M; ++j) {\n int k;\n cin >> k;\n while(k--) {\n string s;\n cin >> s;\n S[j] |= (1<<L[s]);\n }\n }\n for(int i = 0; i < 6; ++i) V[i].clear();\n rec(0,0,0);\n\n for(int i = 0; i < MAXN; ++i) E[i].clear();\n for(int a = 0; a < M; ++a) {\n for(int b = a+1; b < M; ++b) {\n int bit = (S[a] & S[b]);\n for(int k = 0; k < N; ++k) {\n if(bit & (1<<k)) {\n E[k].push_back((Edge){a,b});\n }\n }\n }\n }\n\n int ans = 0;\n try {\n for(int i = 1; i <= 5; ++i) {\n for(int j = 0; j < V[i].size(); ++j) {\n if(test(V[i][j])) {\n ans = V[i][j];\n throw 0;\n }\n }\n }\n } catch(...) {}\n\n if(first) first = false;\n else cout << endl;\n if(ans) {\n vector<string> v;\n for(int i = 0; i < N; ++i) {\n if(ans & (1<<i)) v.push_back(rL[i]);\n }\n cout << v.size() << endl;\n for(int i = 0; i < v.size(); ++i) {\n cout << v[i] << endl;\n }\n } else {\n cout << \"Impossible\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 2468, "score_of_the_acc": -0.6554, "final_rank": 8 }, { "submission_id": "aoj_2061_472541", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nclass UnionFindTree\n{\n int n;\n vector<int> parent; // 親ノード\n vector<int> rank; // 木の高さの上限\n vector<int> num; // グループの要素数\n int find(int x){\n if(parent[x] == x)\n return x;\n return parent[x] = find(parent[x]);\n }\npublic:\n UnionFindTree(int n0){ // コンストラクタ\n n = n0;\n parent.resize(n);\n for(int i=0; i<n; ++i)\n parent[i] = i;\n rank.assign(n, 0);\n num.assign(n, 1);\n }\n void unite(int x, int y){ // xとyのグループを併合\n if((x = find(x)) != (y = find(y))){\n if(rank[x] < rank[y]){\n parent[x] = y;\n num[y] += num[x];\n }else{\n parent[y] = x;\n if(rank[x] == rank[y])\n ++ rank[x];\n num[x] += num[y];\n }\n -- n;\n }\n }\n bool same(int x, int y){ // xとyのグループが同じかを調べる\n return find(x) == find(y);\n }\n int getNum(){ // グループの数を返す\n return n;\n }\n int getNum(int x){ // xのグループの要素数を返す\n return num[find(x)];\n }\n};\n\ntemplate <size_t T>\nbool nextBitset(bitset<T> &bs, int digit)\n{\n if(bs.none())\n return false;\n bitset<T> x, y, z;\n x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);\n y = bs.to_ulong() + x.to_ulong() + 0ULL;\n z = bs & ~y;\n if(bs[digit-1] && bs == z)\n return false;\n bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;\n bs |= y;\n return true;\n}\n\nint n, m;\nvector<string> name;\nvector<bitset<30> > use;\n\nvoid solve()\n{\n for(int i=1; i<=min(5, n); ++i){\n bitset<30> bs((1<<i)-1);\n do{\n UnionFindTree uft(m);\n for(int j=0; j<m; ++j){\n for(int k=0; k<j; ++k){\n if((use[j] & use[k] & bs).any())\n uft.unite(j, k);\n }\n }\n\n if(uft.getNum() == 1){\n cout << i << endl;\n for(int j=0; j<n; ++j){\n if(bs[j])\n cout << name[j] << endl;\n }\n return;\n }\n }while(nextBitset(bs, n));\n }\n\n cout << \"Impossible\" << endl;\n}\n\nint main()\n{\n bool isFirst = true;\n\n for(;;){\n cin >> n >> m;\n if(n == 0)\n return 0;\n\n name.resize(n);\n map<string, int> index;\n for(int i=0; i<n; ++i){\n cin >> name[i];\n index[name[i]] = i;\n }\n\n use.assign(m, 0);\n for(int i=0; i<m; ++i){\n int a;\n cin >> a;\n for(int j=0; j<a; ++j){\n string s;\n cin >> s;\n use[i][index[s]] = true;\n }\n }\n\n if(isFirst)\n isFirst = false;\n else\n cout << endl;\n\n solve();\n }\n}", "accuracy": 1, "time_ms": 730, "memory_kb": 1232, "score_of_the_acc": -0.1698, "final_rank": 4 } ]
aoj_2060_cpp
Problem C: Tetrahedra Peter P. Pepper is facing a difficulty. After fierce battles with the Principality of Croode, the Aaronbarc Kingdom, for which he serves, had the last laugh in the end. Peter had done great service in the war, and the king decided to give him a big reward. But alas, the mean king gave him a hard question to try his intelligence. Peter is given a number of sticks, and he is requested to form a tetrahedral vessel with these sticks. Then he will be, said the king, given as much patas (currency in this kingdom) as the volume of the vessel. In the situation, he has only to form a frame of a vessel. Figure 1: An example tetrahedron vessel The king posed two rules to him in forming a vessel: (1) he need not use all of the given sticks, and (2) he must not glue two or more sticks together in order to make a longer stick. Needless to say, he wants to obtain as much patas as possible. Thus he wants to know the maximum patas he can be given. So he called you, a friend of him, and asked to write a program to solve the problem. Input The input consists of multiple test cases. Each test case is given in a line in the format below: N a 1 a 2 . . . a N where N indicates the number of sticks Peter is given, and a i indicates the length (in centimeters) of each stick. You may assume 6 ≤ N ≤ 15 and 1 ≤ a i ≤ 100. The input terminates with the line containing a single zero. Output For each test case, print the maximum possible volume (in cubic centimeters) of a tetrahedral vessel which can be formed with the given sticks. You may print an arbitrary number of digits after the decimal point, provided that the printed value does not contain an error greater than 10 -6 . It is guaranteed that for each test case, at least one tetrahedral vessel can be formed. Sample Input 7 1 2 2 2 2 2 2 0 Output for the Sample Input 0.942809
[ { "submission_id": "aoj_2060_9723100", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nint s[3];\n\nbool f1(int a, int b, int c) {\n s[0] = a, s[1] = b, s[2] = c;\n sort(s, s + 3);\n return s[0] + s[1] > s[2];\n}\n\ndouble f2(double l1, double l2, double l3, double l4, double l5, double l6) {\n double X = (l6 - l1 + l5) * (l1 + l5 + l6);\n double x = (l1 - l5 + l6) * (l5 - l6 + l1);\n double Y = (l4 - l2 + l6) * (l2 + l6 + l4);\n double y = (l2 - l6 + l4) * (l6 - l4 + l2);\n double Z = (l5 - l3 + l4) * (l3 + l4 + l5);\n double z = (l3 - l4 + l5) * (l4 - l5 + l3);\n double a = sqrt(x * Y * Z);\n double b = sqrt(y * Z * X);\n double c = sqrt(z * X * Y);\n double d = sqrt(x * y * z);\n return sqrt((-a + b + c + d) * (a - b + c + d) * (a + b - c + d) * (a + b + c - d)) / (192 * l4 * l5 * l6);\n}\n\nint A[15], p[15], q[6];\n\nsigned main() {\n int n;\n while (cin >> n) {\n if (!n) break;\n double ans = 0;\n for (int i = 0; i < n; i++) cin >> A[i];\n for (int i = 0; i < 6; i++) p[i] = 1;\n for (int i = 6; i < 15; i++) p[i] = 0;\n do {\n int it = 0;\n for (int i = 0; i < n; i++) if (p[i] == 1) q[it++] = i;\n do {\n int a = A[q[0]];\n int b = A[q[1]];\n int c = A[q[2]];\n int d = A[q[3]];\n int e = A[q[4]];\n int f = A[q[5]];\n if (f1(a, b, c) && f1(a, e, f) && f1(b, f, d) && f1(c, d, e))\n ans = max(ans, f2(a, b, c, d, e, f));\n } while (next_permutation(q, q + 6));\n } while (prev_permutation(p, p + n));\n cout << setprecision(6) << fixed << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 2970, "memory_kb": 3516, "score_of_the_acc": -0.5074, "final_rank": 12 }, { "submission_id": "aoj_2060_9722826", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nint s[3];\n\nbool f1(int a, int b, int c) {\n s[0] = a, s[1] = b, s[2] = c;\n sort(s, s + 3);\n return s[0] + s[1] > s[2];\n}\n\ndouble f2(double l1, double l2, double l3, double l4, double l5, double l6) {\n double X = (l6 - l1 + l5) * (l1 + l5 + l6);\n double x = (l1 - l5 + l6) * (l5 - l6 + l1);\n double Y = (l4 - l2 + l6) * (l2 + l6 + l4);\n double y = (l2 - l6 + l4) * (l6 - l4 + l2);\n double Z = (l5 - l3 + l4) * (l3 + l4 + l5);\n double z = (l3 - l4 + l5) * (l4 - l5 + l3);\n double a = sqrt(x * Y * Z);\n double b = sqrt(y * Z * X);\n double c = sqrt(z * X * Y);\n double d = sqrt(x * y * z);\n return sqrt((-a + b + c + d) * (a - b + c + d) * (a + b - c + d) * (a + b + c - d)) / (192 * l4 * l5 * l6);\n}\n\nint A[15], p[15], q[6];\n\nsigned main() {\n int n;\n while (cin >> n) {\n if (!n) break;\n double ans = 0;\n for (int i = 0; i < n; i++) cin >> A[i];\n for (int i = 0; i < 6; i++) p[i] = 1;\n for (int i = 6; i < 15; i++) p[i] = 0;\n do {\n int it = 0;\n for (int i = 0; i < n; i++) if (p[i] == 1) q[it++] = i;\n do {\n int a = A[q[0]];\n int b = A[q[1]];\n int c = A[q[2]];\n int d = A[q[3]];\n int e = A[q[4]];\n int f = A[q[5]];\n if (f1(a, b, c) && f1(a, e, f) && f1(b, f, d) && f1(c, d, e))\n ans = max(ans, f2(a, b, c, d, e, f));\n } while (next_permutation(q, q + 5));\n } while (prev_permutation(p, p + n));\n cout << setprecision(6) << fixed << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 3596, "score_of_the_acc": -0.0899, "final_rank": 3 }, { "submission_id": "aoj_2060_6826168", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define all(x) (x).begin(), (x).end()\n\nbool tri(int a, int b, int c) {\n\tvector<int> v = { a, b, c };\n\tsort(all(v));\n\treturn v[0] + v[1] > v[2];\n}\n\ndouble vol(int U, int V, int W, int u, int v, int w) {\n\tdouble X = (w - U + v) * (U + v + w),\n\t\tY = (u - V + w) * (V + w + u),\n\t\tZ = (v - W + u) * (W + u + v);\n\tdouble x = (U - v + w) * (v - w + U),\n\t\ty = (V - w + u) * (w - u + V),\n\t\tz = (W - u + v) * (u - v + W);\n\tdouble k = sqrt(x * Y * Z),\n\t\te = sqrt(y * Z * X),\n\t\tt = sqrt(z * X * Y),\n\t\tl = sqrt(x * y * z);\n\treturn sqrt((k + e + t - l) * (l + k + e - t) * (e + t + l - k) * (t + l + k - e)) / (192 * u * v * w);\n}\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(0);\n\n\twhile (true) {\n\t\tint n; cin >> n;\n\t\tif (!n) break;\n\t\tvector<int> a(n);\n\t\tfor (auto& i : a) cin >> i;\n\n\t\tvector<bool> p(n);\n\t\tfill(p.begin(), p.begin() + 6, true);\n\t\tdouble ans = .0;\n\t\tdo {\n\t\t\tvector<int> q;\n\t\t\tint c = 0;\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tif (p[i]) q.push_back(i);\n\t\t\t}\n\t\t\tdo {\n\t\t\t\tint U = a[q[0]], V = a[q[1]], W = a[q[2]];\n\t\t\t\tint u = a[q[3]], v = a[q[4]], w = a[q[5]];\n\t\t\t\tif (tri(U, V, W) && tri(u, v, W) && tri(v, w, U) && tri(w, u, V)) {\n\t\t\t\t\tans = max(ans, vol(U, V, W, u, v, w));\n\t\t\t\t}\n\t\t\t} while (next_permutation(all(q) - 1));\n\t\t} while (prev_permutation(all(p)));\n\t\tcout << fixed << setprecision(10) << ans << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 3620, "score_of_the_acc": -0.154, "final_rank": 4 }, { "submission_id": "aoj_2060_5500450", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 4000005\n\nint N,num_data;\nint table[SIZE][6],work[6],tri[4][3] = {{0,1,2},{0,3,4},{1,4,5},{2,5,3}};\ndouble A[20];\nbool used[20];\n\nvoid dfs(int index){\n\n\tif(index == 6){\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\ttable[num_data][i] = work[i];\n\t\t}\n\t\tnum_data++;\n\n\t\treturn;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(used[i])continue;\n\t\tused[i] = true;\n\n\t\twork[index] = i;\n\n\t\tdfs(index+1);\n\t\tused[i] = false;\n\t}\n}\n\n//三角形が作れるか\nbool check(int table[3]){\n\n\tsort(table,table+3);\n\n\treturn (table[0]+table[1] > table[2]+EPS);\n}\n\ndouble yogen(double a,double b, double c){\n\n\tdouble ret = (b*b+c*c-a*a)/(2*b*c);\n\treturn ret;\n}\n\ndouble calc_V(int index){\n\n\tdouble a = A[table[index][3]];\n\tdouble b = A[table[index][4]];\n\tdouble c = A[table[index][5]];\n\n\tdouble alpha = yogen(A[table[index][0]],A[table[index][3]],A[table[index][4]]);\n\tdouble beta = yogen(A[table[index][1]],A[table[index][4]],A[table[index][5]]);\n\tdouble gamma = yogen(A[table[index][2]],A[table[index][5]],A[table[index][3]]);\n\n\tdouble V = (a*b*c)/6.0*sqrt(1+2*alpha*beta*gamma-(alpha*alpha + beta*beta + gamma*gamma));\n\n\treturn V;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf\",&A[i]);\n\t}\n\tnum_data = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tused[i] = false;\n\t}\n\n\tdfs(0);\n\n\tdouble ans = 0;\n\n\tfor(int i = 0; i < num_data; i++){\n\n\t\tint TMP[3];\n\n\t\tbool FLG = true;\n\n\t\tfor(int k = 0; k < 4; k++){\n\t\t\tfor(int p = 0; p < 3; p++){\n\n\t\t\t\tTMP[p] = A[table[i][tri[k][p]]];\n\t\t\t}\n\t\t\tif(!check(TMP)){\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG)continue;\n\n\t\tdouble tmp_V = calc_V(i);\n\t\tans = max(ans,tmp_V);\n\t}\n\n\tprintf(\"%.12lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3580, "memory_kb": 87688, "score_of_the_acc": -1.5815, "final_rank": 20 }, { "submission_id": "aoj_2060_3062329", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return (a.X!=b.X) ? a.X<b.X : a.Y<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n vector<double> len(n);\n for(int i=0; i<n; i++){\n cin >> len[i];\n }\n vector<bool> choose(n, false);\n for(int i=0; i<3; i++){\n choose[n-i-1] = true;\n }\n\n double ans = 0;\n do{\n //底面を作る3角形の選び方\n vector<double> base;\n for(int i=0; i<n; i++){\n if(choose[i]) base.push_back(len[i]);\n }\n sort(base.begin(), base.end());\n if(base[0] +base[1] < base[2] +EPS) continue;\n\n P p[3];\n p[0] = P(0, 0);\n p[1] = P(base[0], 0);\n double c2x = (base[2]*base[2] -base[1]*base[1] +base[0]*base[0]) /(2*base[0]);\n double c2y = sqrt(base[2]*base[2] -c2x*c2x);\n p[2] = P(c2x, c2y);\n\n //底面から生やす辺の選び方\n for(int i=0; i<n; i++){\n if(choose[i]) continue;\n for(int j=0; j<n; j++){\n if(choose[j] || j==i) continue;\n for(int k=0; k<n; k++){\n if(choose[k] || k==i || k==j) continue;\n //3角形が作れるか\n bool ok = true;\n double hlen[3] = {len[i], len[j], len[k]};\n L lines[3];\n for(int d=0; d<3; d++){\n if(hlen[d] +hlen[(d+1)%3] < base[d] || abs(hlen[d] -hlen[(d+1)%3]) > base[d]){\n ok = false;\n break;\n }\n double dist = (hlen[d]*hlen[d] -hlen[(d+1)%3]*hlen[(d+1)%3] +base[d]*base[d]) /(2*base[d]);\n P dir = dist * unit(p[(d+1)%3] -p[d]);\n lines[d][0] = p[d] +dir;\n lines[d][1] = lines[d][0] +dir *P(0, 1);\n }\n if(ok){\n P cp1 = crosspointLL(lines[0], lines[1]);\n double h = sqrt(hlen[0]*hlen[0] -norm(cp1));\n ans = max(ans, cross(p[1], p[2]) *h /6);\n }\n }\n }\n }\n }while(next_permutation(choose.begin(), choose.end()));\n\n cout << fixed << setprecision(10);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3368, "score_of_the_acc": -0.0671, "final_rank": 2 }, { "submission_id": "aoj_2060_2863144", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nbool tri_valid(int a, int b, int c) {\n\tvector<int>v{ a,b,c };\n\tsort(v.begin(),v.end());\n\treturn v[2]<v[0]+v[1];\n}\n\nld get_theta(ld a1, ld a2, ld x) {\n\treturn (a1*a1+a2*a2-x*x)/(2*a1*a2);\n}\n\nint main() {\n\tcout<<setprecision(10)<<fixed;\n\twhile (true){\n\t\tint N;cin>>N;\n\t\tif(!N)break;\n\t\tvector<int>sticks(N);\n\t\tfor(int i=0;i<N;++i)cin>>sticks[i];\n\n\t\tvector<int>av(6);\n\t\tld ans=0;\n\t\tfor (av[0] = 0; av[0] < N; ++av[0]) {\n\t\t\tfor ( av[1] = av[0]+1; av[1] < N; ++av[1]) {\n\t\t\t\tfor ( av[2] = av[1]+1; av[2] < N; ++av[2]) {\n\t\t\t\t\tfor (av[3] = 0; av[3] < N; ++av[3]) {\n\t\t\t\t\t\tfor ( av[4] = 0; av[4] < N; ++av[4]) {\n\t\t\t\t\t\t\tfor ( av[5] = 0; av[5] < N; ++av[5]) {\n\t\t\t\t\t\t\t\tbool ok=true;\n\t\t\t\t\t\t\t\tfor (int i = 0; i < 6; ++i) {\n\t\t\t\t\t\t\t\t\tfor (int j = i + 1; j < 6; ++j) {\n\t\t\t\t\t\t\t\t\t\tif(av[i]==av[j])ok=false;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(!ok)continue;\n\t\t\t\t\t\t\t\tvector<int>v(6);\n\t\t\t\t\t\t\t\tfor(int i=0;i<6;++i)v[i]=sticks[av[i]];\n\t\t\t\t\t\t\t\tif(!tri_valid(v[0],v[1],v[2]))ok=false;\n\t\t\t\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\t\t\t\tif(!tri_valid(v[i],v[3+(i+1)%3],v[3+(i+2)%3]))ok=false;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(!ok)continue;\n\n\t\t\t\t\t\t\t\tvector<ld>thetas(3);\n\t\t\t\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\t\t\t\tthetas[i]=get_theta(v[3+(i+1)%3],v[3+(i+2)%3],v[i]);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tld num=1+2*thetas[0]*thetas[1]*thetas[2]-(pow(thetas[0],2)+pow(thetas[1],2)+pow(thetas[2],2));\n\t\t\t\t\t\t\t\tif (num > 0) {\n\t\t\t\t\t\t\t\t\tans=max(ans,v[3]*v[4]*v[5]*sqrt(num)/6.0);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3240, "memory_kb": 3112, "score_of_the_acc": -0.5481, "final_rank": 13 }, { "submission_id": "aoj_2060_2585981", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define EPS (1e-10)\nusing Int = long long;\n\n//BEGIN CUT HERE\ntypedef vector<double> arr;\ntypedef vector<arr> mat;\n\ndouble det(mat A){\n int n=A.size();\n double res=1;\n for(int i=0;i<n;i++){\n int pivot=i;\n for(int j=i+1;j<n;j++)\n if(abs(A[j][i])>abs(A[pivot][i])) pivot=j;\n swap(A[pivot],A[i]);\n res*=A[i][i]*(i!=pivot?-1:1);\n if(abs(A[i][i])<EPS) break;\n for(int j=i+1;j<n;j++)\n for(int k=n-1;k>=i;k--)\n\tA[j][k]-=A[i][k]*A[j][i]/A[i][i];\n }\n return res;\n}\n\nbool isTriangle(double a1,double a2,double a3){\n if(a1+a2<=a3||a2+a3<=a1||a3+a1<=a2) return 0;\n return 1;\n}\ndouble tetrahedra(double OA,double OB,double OC,double AB,double AC,double BC){\n if(!isTriangle(OA,OB,AB)) return 0;\n if(!isTriangle(OB,OC,BC)) return 0;\n if(!isTriangle(OC,OA,AC)) return 0;\n if(!isTriangle(AB,AC,BC)) return 0;\n mat A(5,arr(5,0));\n A[0][0]= 0;A[0][1]=AB*AB;A[0][2]=AC*AC;A[0][3]=OA*OA;A[0][4]=1;\n A[1][0]=AB*AB;A[1][1]= 0;A[1][2]=BC*BC;A[1][3]=OB*OB;A[1][4]=1;\n A[2][0]=AC*AC;A[2][1]=BC*BC;A[2][2]= 0;A[2][3]=OC*OC;A[2][4]=1;\n A[3][0]=OA*OA;A[3][1]=OB*OB;A[3][2]=OC*OC;A[3][3]= 0;A[3][4]=1;\n A[4][0]= 1;A[4][1]= 1;A[4][2]= 1;A[4][3]= 1;A[4][4]=0;\n //cout<<\"det(A):\"<<det(A)<<endl;\n if(det(A)<=0) return 0; \n return sqrt(det(A)/288.0);\n}\n//END CUT HERE\n\nsigned main(){\n int n;\n while(cin>>n,n){\n double a[n];\n double ans=0;\n for(int i=0;i<n;i++) cin>>a[i];\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n\tfor(int k=j+1;k<n;k++){\n\t for(int x=0;x<n;x++){\n\t if(i==x||j==x||k==x) continue;\n\t for(int y=0;y<n;y++){\n\t if(i==y||j==y||k==y||x==y) continue;\n\t for(int z=0;z<n;z++){\n\t\tif(i==z||j==z||k==z||x==z||y==z) continue;\n\t\tans=max(ans,tetrahedra(a[i],a[j],a[k],a[x],a[y],a[z]));\n\t }\n\t }\n\t }\n\t}\n }\n }\n printf(\"%.10f\\n\",ans);\n }\n return 0;\n}\n\n\n/*\nverified on 2017/04/26\nhttp://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2060\n*/", "accuracy": 1, "time_ms": 1820, "memory_kb": 3196, "score_of_the_acc": -0.3105, "final_rank": 9 }, { "submission_id": "aoj_2060_2285200", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define EPS (1e-10)\n#define int long long\n\ntypedef vector<double> arr;\ntypedef vector<arr> mat;\n\ndouble det(mat A){\n int n=A.size();\n double res=1;\n for(int i=0;i<n;i++){\n int pivot=i;\n for(int j=i+1;j<n;j++)\n if(abs(A[j][i])>abs(A[pivot][i])) pivot=j;\n swap(A[pivot],A[i]);\n res*=A[i][i]*(i!=pivot?-1:1);\n if(abs(A[i][i])<EPS) break;\n for(int j=i+1;j<n;j++)\n for(int k=n-1;k>=i;k--)\n\tA[j][k]-=A[i][k]*A[j][i]/A[i][i];\n }\n return res;\n}\n\nbool isTriangle(double a1,double a2,double a3){\n if(a1+a2<=a3||a2+a3<=a1||a3+a1<=a2) return 0;\n return 1;\n}\ndouble tetrahedra(double OA,double OB,double OC,double AB,double AC,double BC){\n if(!isTriangle(OA,OB,AB)) return 0;\n if(!isTriangle(OB,OC,BC)) return 0;\n if(!isTriangle(OC,OA,AC)) return 0;\n if(!isTriangle(AB,AC,BC)) return 0;\n mat A(5,arr(5,0));\n A[0][0]= 0;A[0][1]=AB*AB;A[0][2]=AC*AC;A[0][3]=OA*OA;A[0][4]=1;\n A[1][0]=AB*AB;A[1][1]= 0;A[1][2]=BC*BC;A[1][3]=OB*OB;A[1][4]=1;\n A[2][0]=AC*AC;A[2][1]=BC*BC;A[2][2]= 0;A[2][3]=OC*OC;A[2][4]=1;\n A[3][0]=OA*OA;A[3][1]=OB*OB;A[3][2]=OC*OC;A[3][3]= 0;A[3][4]=1;\n A[4][0]= 1;A[4][1]= 1;A[4][2]= 1;A[4][3]= 1;A[4][4]=0;\n //cout<<\"det(A):\"<<det(A)<<endl;\n if(det(A)<=0) return 0; \n return sqrt(det(A)/288.0);\n}\nsigned main(){\n int n;\n while(cin>>n,n){\n double a[n];\n double ans=0;\n for(int i=0;i<n;i++) cin>>a[i];\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n\tfor(int k=j+1;k<n;k++){\n\t for(int x=0;x<n;x++){\n\t if(i==x||j==x||k==x) continue;\n\t for(int y=0;y<n;y++){\n\t if(i==y||j==y||k==y||x==y) continue;\n\t for(int z=0;z<n;z++){\n\t\tif(i==z||j==z||k==z||x==z||y==z) continue;\n\t\tans=max(ans,tetrahedra(a[i],a[j],a[k],a[x],a[y],a[z]));\n\t }\n\t }\n\t }\n\t}\n }\n }\n printf(\"%.10f\\n\",ans);\n }\n return 0;\n}\n\n\n/*\nverified on 2017/04/26\nhttp://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2060\n*/", "accuracy": 1, "time_ms": 1780, "memory_kb": 3080, "score_of_the_acc": -0.3024, "final_rank": 7 }, { "submission_id": "aoj_2060_2280286", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define EPS (1e-10)\n#define int long long\n\ntypedef vector<double> arr;\ntypedef vector<arr> mat;\n\ndouble det(mat A){\n int n=A.size();\n double res=1;\n for(int i=0;i<n;i++){\n int pivot=i;\n for(int j=i+1;j<n;j++)\n if(abs(A[j][i])>abs(A[pivot][i])) pivot=j;\n swap(A[pivot],A[i]);\n res*=A[i][i]*(i!=pivot?-1:1);\n if(abs(A[i][i])<EPS) break;\n for(int j=i+1;j<n;j++)\n for(int k=n-1;k>=i;k--)\n\tA[j][k]-=A[i][k]*A[j][i]/A[i][i];\n }\n return res;\n}\n\nbool tri(double a1,double a2,double a3){\n if(a1+a2<=a3||a2+a3<=a1||a3+a1<=a2) return 1;\n return 0;\n}\ndouble tetrahedra(double OA,double OB,double OC,double AB,double AC,double BC){\n if(tri(OA,OB,AB)) return 0;\n if(tri(OB,OC,BC)) return 0;\n if(tri(OC,OA,AC)) return 0;\n if(tri(AB,AC,BC)) return 0;\n mat A(5,arr(5,0));\n A[0][0]= 0;A[0][1]=AB*AB;A[0][2]=AC*AC;A[0][3]=OA*OA;A[0][4]=1;\n A[1][0]=AB*AB;A[1][1]= 0;A[1][2]=BC*BC;A[1][3]=OB*OB;A[1][4]=1;\n A[2][0]=AC*AC;A[2][1]=BC*BC;A[2][2]= 0;A[2][3]=OC*OC;A[2][4]=1;\n A[3][0]=OA*OA;A[3][1]=OB*OB;A[3][2]=OC*OC;A[3][3]= 0;A[3][4]=1;\n A[4][0]= 1;A[4][1]= 1;A[4][2]= 1;A[4][3]= 1;A[4][4]=0;\n //cout<<\"det(A):\"<<det(A)<<endl;\n if(det(A)<=0) return 0; \n return sqrt(det(A)/288.0);\n}\nsigned main(){\n int n;\n while(cin>>n,n){\n double a[n];\n double ans=0;\n for(int i=0;i<n;i++) cin>>a[i];\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n\tfor(int k=j+1;k<n;k++){\n\t for(int x=0;x<n;x++){\n\t if(i==x||j==x||k==x) continue;\n\t for(int y=0;y<n;y++){\n\t if(i==y||j==y||k==y||x==y) continue;\n\t for(int z=0;z<n;z++){\n\t\tif(i==z||j==z||k==z||x==z||y==z) continue;\n\t\tans=max(ans,tetrahedra(a[i],a[j],a[k],a[x],a[y],a[z]));\n\t }\n\t }\n\t }\n\t}\n }\n }\n printf(\"%.10f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 3256, "score_of_the_acc": -0.3061, "final_rank": 8 }, { "submission_id": "aoj_2060_2276503", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef complex<double> P;\ntypedef pair<P,double> C;\ntypedef pair<P,P> L;\n \nconst double eps=1e-8;\n \nbool eq(double a,double b){\n return -eps<a-b&&a-b<eps;\n}\n \ndouble cross(P a,P b){\n return imag(b*conj(a));\n}\n \nbool isCrossCC(C a,C b){\n double ar=a.second, br=b.second;\n double dist=abs(a.first-b.first);\n if( dist > ar+br+eps)return false;\n if( dist > ar+br-eps )return true;\n if( dist > abs(ar-br)+eps)return true;\n if( dist > abs(ar-br)-eps)return true;\n return false;\n}\n \ndouble Sqrt(double x){\n if(x<0)return 0;\n return sqrt(x);\n}\n \nP reflect(P a,P b,P c){\n b-=a;\n c-=a;\n return a+ conj(c/b)*b;\n}\n \nP getCrossCC(C a,C b){\n P p1=a.first,p2=b.first;\n double r1=a.second, r2=b.second;\n double cA = (r1*r1+norm(p1-p2)-r2*r2) / (2.0*r1*abs(p1-p2));\n return p1+(p2-p1)/abs(p1-p2)*r1*P(cA, Sqrt(1.0-cA*cA) );\n}\n \nint n;\nint t[15];\nint u[6];\nbool used[15];\ndouble ans=-1;\n \ndouble calcArea(double a,double b,double c){\n P p1=P(0,0);\n P p2=P(b,0);\n P p3=getCrossCC( C(p1,a) , C(p2,c) );\n return abs( cross( p1-p3 , p2-p3 )*0.5 );\n}\n \nP getCrossLL(L line0,L line1){\n P a=line0.first, b=line0.second;\n P c=line1.first, d=line1.second;\n a-=d;b-=d;c-=d;\n return d+a+(b-a)*imag(a/c)/imag(a/c-b/c);\n}\n \nbool check(int a,int b,int c){\n if(a<b)swap(a,b);\n if(b<c)swap(b,c);\n if(a<b)swap(a,b);\n return ( a < b + c );\n}\n \n \nvoid check(){\n \n \n double S=calcArea(u[0],u[1],u[2]);\n \n double h=1e9;\n \n C ca = C( P(0,0) , u[4] );\n C cb = C( P(u[1],0) , u[5] );\n C cc = C( getCrossCC( C(ca.first,u[0]) , C(cb.first,u[2]) ) , u[3] );\n \n if( isCrossCC(ca,cb) == false )return;\n if( isCrossCC(cc,cb) == false )return;\n if( isCrossCC(ca,cc) == false )return;\n \n L fi = L( getCrossCC(ca,cb) , getCrossCC(cb,ca) );\n if( abs(fi.second-fi.first ) < eps )return;\n \n L se = L( getCrossCC(cc,ca) , getCrossCC(ca,cc) );\n if( abs(se.second-se.first ) < eps )return;\n \n \n L th = L( getCrossCC(cc,cb) , getCrossCC(cb,cc) );\n \n P target=getCrossLL(fi,se);\n \n P target2 = getCrossLL( th,se);\n if( abs(target-target2) > eps )return;\n \n double di;\n \n di=abs(P(0,0)-target);\n double Ah=min(h, Sqrt(u[4]*u[4]-di*di) );\n \n di=abs(P(u[1],0)-target);\n double Bh=min(h, Sqrt(u[5]*u[5]-di*di) );\n \n di=abs(cc.first-target);\n double Ch=min(h, Sqrt(cc.second*cc.second-di*di) );\n if( !eq(Ah,Ch) )return;\n if( !eq(Ah,Bh) )return;\n if( !eq(Bh,Ch) )return;\n h=min( Ah, Bh);\n h=min( h, Ah);\n \n ans=max(ans, h*S/3.0);\n}\n \nvoid dfs(int cnt){\n if(cnt>=3 && !check(u[0],u[1],u[2]))return;\n if(cnt>=5 && !check(u[0],u[3],u[4]))return;\n if(cnt>=6 && !check(u[1],u[4],u[5]))return;\n if(cnt>=6 && !check(u[2],u[3],u[5]))return;\n \n if(cnt==6){\n check();\n }else{\n for(int i=0;i<n;i++){\n if(used[i])continue;\n used[i]=true;\n u[cnt]=t[i];\n dfs(cnt+1);\n used[i]=false;\n }\n }\n}\n \nint main(){\n while(1){\n scanf(\"%d\",&n);\n if(n==0)break;\n \n for(int i=0;i<n;i++){\n scanf(\"%d\",&t[i]);\n }\n \n sort(t,t+n);\n \n memset(used,false,sizeof(used));\n ans=-1;\n dfs(0);\n printf(\"%.10f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6070, "memory_kb": 3304, "score_of_the_acc": -1.026, "final_rank": 18 }, { "submission_id": "aoj_2060_2275914", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef complex<double> P;\ntypedef pair<P,double> C;\ntypedef pair<P,P> L;\n\nconst double eps=1e-8;\n\nbool eq(double a,double b){\n return -eps<a-b&&a-b<eps;\n}\n\ndouble cross(P a,P b){\n return imag(b*conj(a));\n}\n\nbool isCrossCC(C a,C b){\n double ar=a.second, br=b.second;\n double dist=abs(a.first-b.first);\n if( dist > ar+br+eps)return false;\n if( dist > ar+br-eps )return true;\n if( dist > abs(ar-br)+eps)return true;\n if( dist > abs(ar-br)-eps)return true;\n return false;\n}\n\ndouble Sqrt(double x){\n if(x<0)return 0;\n return sqrt(x);\n}\n\nP reflect(P a,P b,P c){\n b-=a;\n c-=a;\n return a+ conj(c/b)*b;\n}\n\nP getCrossCC(C a,C b){\n P p1=a.first,p2=b.first;\n double r1=a.second, r2=b.second;\n double cA = (r1*r1+norm(p1-p2)-r2*r2) / (2.0*r1*abs(p1-p2));\n return p1+(p2-p1)/abs(p1-p2)*r1*P(cA, Sqrt(1.0-cA*cA) );\n}\n\nint n;\nint t[15];\nint u[6];\nbool used[15];\ndouble ans=-1;\n\ndouble calcArea(double a,double b,double c){\n P p1=P(0,0);\n P p2=P(b,0);\n P p3=getCrossCC( C(p1,a) , C(p2,c) );\n return abs( cross( p1-p3 , p2-p3 )*0.5 );\n}\n\nP getCrossLL(L line0,L line1){\n P a=line0.first, b=line0.second;\n P c=line1.first, d=line1.second;\n a-=d;b-=d;c-=d;\n return d+a+(b-a)*imag(a/c)/imag(a/c-b/c);\n}\n\nbool check(int a,int b,int c){\n if(a<b)swap(a,b);\n if(b<c)swap(b,c);\n if(a<b)swap(a,b);\n return ( a < b + c );\n}\n\n\nvoid check(){\n\n \n double S=calcArea(u[0],u[1],u[2]);\n\n double h=1e9;\n \n C ca = C( P(0,0) , u[4] );\n C cb = C( P(u[1],0) , u[5] );\n C cc = C( getCrossCC( C(ca.first,u[0]) , C(cb.first,u[2]) ) , u[3] );\n\n if( isCrossCC(ca,cb) == false )return;\n if( isCrossCC(cc,cb) == false )return;\n if( isCrossCC(ca,cc) == false )return;\n \n L fi = L( getCrossCC(ca,cb) , getCrossCC(cb,ca) );\n if( abs(fi.second-fi.first ) < eps )return;\n \n L se = L( getCrossCC(cc,ca) , getCrossCC(ca,cc) );\n if( abs(se.second-se.first ) < eps )return;\n \n\n L th = L( getCrossCC(cc,cb) , getCrossCC(cb,cc) );\n \n P target=getCrossLL(fi,se);\n\n P target2 = getCrossLL( th,se);\n if( abs(target-target2) > eps )return;\n \n double di;\n \n di=abs(P(0,0)-target);\n double Ah=min(h, Sqrt(u[4]*u[4]-di*di) );\n\n di=abs(P(u[1],0)-target);\n double Bh=min(h, Sqrt(u[5]*u[5]-di*di) );\n\n di=abs(cc.first-target);\n double Ch=min(h, Sqrt(cc.second*cc.second-di*di) );\n if( !eq(Ah,Ch) )return;\n if( !eq(Ah,Bh) )return;\n if( !eq(Bh,Ch) )return;\n h=min( Ah, Bh);\n h=min( h, Ah);\n\n ans=max(ans, h*S/3.0);\n}\n\nvoid dfs(int cnt){\n if(cnt>=3 && !check(u[0],u[1],u[2]))return;\n if(cnt>=5 && !check(u[0],u[3],u[4]))return;\n if(cnt>=6 && !check(u[1],u[4],u[5]))return;\n if(cnt>=6 && !check(u[2],u[3],u[5]))return;\n \n if(cnt==6){\n check();\n }else{\n for(int i=0;i<n;i++){\n if(used[i])continue;\n used[i]=true;\n u[cnt]=t[i];\n dfs(cnt+1);\n used[i]=false;\n }\n }\n}\n\nint main(){\n while(1){\n scanf(\"%d\",&n);\n if(n==0)break;\n \n for(int i=0;i<n;i++){\n scanf(\"%d\",&t[i]);\n }\n\n sort(t,t+n);\n \n memset(used,false,sizeof(used));\n ans=-1;\n dfs(0);\n printf(\"%.10f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6070, "memory_kb": 3324, "score_of_the_acc": -1.0262, "final_rank": 19 }, { "submission_id": "aoj_2060_1882430", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX_N 15\n \nbool isTri(double a,double b,double c){\n if(c < a+b && a < b+c && b < c+a){\n\treturn true;\n }\n return false;\n}\n \ndouble getArea(double a,double b,double c){\n double s = (a+b+c)/2.0;\n return sqrt(s*(s-a)*(s-b)*(s-c)); \n}\n \ndouble S(double x){\n return x*x;\n}\n \ndouble calc(vector<double> &v){\n double a = S(v[0])*S(v[4])*(S(v[1])+S(v[2])+S(v[3])+S(v[5])-S(v[0])-S(v[4]));\n double b = S(v[1])*S(v[5])*(S(v[0])+S(v[2])+S(v[3])+S(v[4])-S(v[1])-S(v[5]));\n double c = S(v[2])*S(v[3])*(S(v[0])+S(v[1])+S(v[4])+S(v[5])-S(v[2])-S(v[3]));\n double d = -S(v[0])*S(v[1])*S(v[3])-(S(v[1])*S(v[2]))*S(v[4])-\n\tS(v[0])*S(v[2])*S(v[5])-S(v[3])*S(v[4])*S(v[5]);\n return sqrt((a+b+c+d)/144.0);\n}\n \ndouble getVolume(vector<double> &v){\n if(!isTri(v[0],v[1],v[3])) return 0.0; \n if(!isTri(v[0],v[2],v[5])) return 0.0; \n if(!isTri(v[1],v[2],v[4])) return 0.0; \n if(!isTri(v[3],v[4],v[5])) return 0.0; \n return calc(v);\n}\n \ndouble solve(int S,vector<double> &v,vector<double> &a){\n if(__builtin_popcount(S) == 6){\n\treturn getVolume(v);\n }\n double res = 0;\n for(int i = 0 ; i < (int)a.size() ; i++){\n\tif((S >> i) & 1) continue;\n\tv.push_back(a[i]);\n\tres = max(res,solve(S|(1<<i),v,a));\n\tv.pop_back();\n }\n return res;\n}\n \nint main(){\n int N;\n while(cin >> N,N){\n\tvector<double> a(N),v;\n\tfor(int i = 0 ; i < N ; i++){\n\t cin >> a[i];\n\t}\n\tprintf(\"%.10f\\n\",solve(0,v,a));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 3200, "score_of_the_acc": -0.2651, "final_rank": 6 }, { "submission_id": "aoj_2060_1255269", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX_N 15\n \nbool isTri(double a,double b,double c){\n if(c < a+b && a < b+c && b < c+a){\n return true;\n }\n return false;\n}\n \ndouble getArea(double a,double b,double c){\n double s = (a+b+c)/2.0;\n return sqrt(s*(s-a)*(s-b)*(s-c)); \n}\n \ndouble S(double x){\n return x*x;\n}\n \ndouble calc(vector<double> &v){\n double a = S(v[0])*S(v[4])*(S(v[1])+S(v[2])+S(v[3])+S(v[5])-S(v[0])-S(v[4]));\n double b = S(v[1])*S(v[5])*(S(v[0])+S(v[2])+S(v[3])+S(v[4])-S(v[1])-S(v[5]));\n double c = S(v[2])*S(v[3])*(S(v[0])+S(v[1])+S(v[4])+S(v[5])-S(v[2])-S(v[3]));\n double d = -S(v[0])*S(v[1])*S(v[3])-(S(v[1])*S(v[2]))*S(v[4])-\n S(v[0])*S(v[2])*S(v[5])-S(v[3])*S(v[4])*S(v[5]);\n return sqrt((a+b+c+d)/144.0);\n}\n \ndouble getVolume(vector<double> &v){\n if(!isTri(v[0],v[1],v[3])){ return 0.0; }\n if(!isTri(v[0],v[2],v[5])){ return 0.0; }\n if(!isTri(v[1],v[2],v[4])){ return 0.0; }\n if(!isTri(v[3],v[4],v[5])){ return 0.0; }\n return calc(v);\n}\n \ndouble solve(int S,vector<double> &v,vector<double> &a){\n if(__builtin_popcount(S) == 6){\n return getVolume(v);\n }\n double res = 0;\n for(int i = 0 ; i < (int)a.size() ; i++){\n if((S >> i) & 1){ continue; }\n v.push_back(a[i]);\n res = max(res,solve(S|(1<<i),v,a));\n v.pop_back();\n }\n return res;\n}\n \nint main(){\n int N;\n while(cin >> N,N){\n vector<double> a(N),v;\n for(int i = 0 ; i < N ; i++){\n cin >> a[i];\n }\n printf(\"%.10f\\n\",solve(0,v,a));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2440, "memory_kb": 1280, "score_of_the_acc": -0.3925, "final_rank": 11 }, { "submission_id": "aoj_2060_1155568", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\nbool tri(int a, int b, int c) {\n return a <= b + c && b <= c + a && c <= a + b;\n}\n\ndouble calc(double a, double b, double c, double l, double m, double n) {\n a *= a;\n b *= b;\n c *= c;\n l *= l;\n m *= m;\n n *= n;\n double tmp =\n +a*l*(-a+b+c-l+m+n)\n +b*m*(a-b+c+l-m+n)\n +c*n*(a+b-c+l+m-n)\n -l*b*c-a*m*c-a*b*n-l*m*n;\n if(tmp < 0) return 0;\n return sqrt(tmp) / 12;\n}\n\nint main(){\n int num;\n while(cin >> num && num > 0) {\n vector<int> v(num);\n REP(i, num) cin >> v[i];\n double ans = 0.0;\n REP(i, num) REP(j, i) REP(k, j) {\n int l = v[i], m = v[j], n = v[k];\n if(!tri(l, m, n)) continue;\n REP(p, num) if(p != i && p != j && p != k) \n REP(q, num) if(q != p && q != i && q != j && q != k)\n REP(r, num) if(r != q && r != p && r != i && r != j && r != k) {\n int a = v[p], b = v[q], c = v[r];\n if(!tri(a, b, n)) continue;\n if(!tri(b, c, l)) continue;\n if(!tri(c, a, m)) continue;\n ans = max(ans, calc(a, b, c, l, m, n));\n }\n }\n printf(\"%.12f\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 1220, "score_of_the_acc": -0.0019, "final_rank": 1 }, { "submission_id": "aoj_2060_1123787", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <complex>\n#include <cstdio>\nusing namespace std;\n\ndouble EPS = 1e-8;\ntypedef complex<double> P,point;\n\nstruct C {P p; double r; C(const P &p,double r) : p(p), r(r){} C(){} };\n\n\ntypedef double number;\nconst number eps = 1e-8;\ntypedef vector<number> Array;\ntypedef vector<Array> matrix;\n\n// O( n )\nmatrix identity(int n) {\n matrix A(n, Array(n));\n for (int i = 0; i < n; ++i) A[i][i] = 1;\n return A;\n}\n// O( n )\nnumber inner_product(const Array &a, const Array &b) {\n number ans = 0;\n for (int i = 0; i < a.size(); ++i)\n ans += a[i] * b[i];\n return ans;\n}\n// O( n^2 )\nArray mul(const matrix &A, const Array &x) {\n Array y(A.size());\n for (int i = 0; i < A.size(); ++i)\n for (int j = 0; j < A[0].size(); ++j)\n y[i] = A[i][j] * x[j];\n return y;\n}\n// O( n^3 )\nmatrix mul(const matrix &A, const matrix &B) {\n matrix C(A.size(), Array(B[0].size()));\n for (int i = 0; i < C.size(); ++i)\n for (int j = 0; j < C[i].size(); ++j)\n for (int k = 0; k < A[i].size(); ++k)\n C[i][j] += A[i][k] * B[k][j];\n return C;\n}\n// O( n^3 log e )\nmatrix pow(const matrix &A, int e) {\n return e == 0 ? identity(A.size()) :\n e % 2 == 0 ? pow(mul(A, A), e/2) : mul(A, pow(A, e-1));\n}\n// O( n^3 )\nnumber det(matrix A) {\n const int n = A.size();\n number D = 1;\n for (int i = 0; i < n; ++i) {\n int pivot = i;\n for (int j = i+1; j < n; ++j)\n if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;\n swap(A[pivot], A[i]);\n D *= A[i][i] * (i != pivot ? -1 : 1);\n if (abs(A[i][i]) < eps) break;\n for(int j = i+1; j < n; ++j)\n for(int k = n-1; k >= i; --k)\n A[j][k] -= A[i][k] * A[j][i] / A[i][i];\n }\n return D;\n}\n// O(n)\nnumber tr(const matrix &A) {\n number ans = 0;\n for (int i = 0; i < A.size(); ++i)\n ans += A[i][i];\n return ans;\n}\n// O( n^3 ).\nint rank(matrix A) {\n const int n = A.size(), m = A[0].size();\n int r = 0;\n for (int i = 0; r < n && i < m; ++i) {\n int pivot = r;\n for (int j = r+1; j < n; ++j)\n if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;\n swap(A[pivot], A[r]);\n if (abs(A[r][i]) < eps) continue;\n for (int k = m-1; k >= i; --k)\n A[r][k] /= A[r][i];\n for(int j = r+1; j < n; ++j)\n for(int k = i; k < m; ++k)\n A[j][k] -= A[r][k] * A[j][i];\n ++r;\n }\n return r;\n}\n\ndouble chk(double a,double b,double c){\n\tif( a > b ) swap(a,b);\n\tif( b > c ) swap(b,c);\n\tif( a > b ) swap(a,b);\n\treturn a + b >= c;\n}\ndouble calc(double a,double b,double c,double d,double e,double f){\n\tdouble x = a*a*d*d*(b*b+c*c+e*e+f*f-a*a-d*d)+b*b*e*e*(c*c+a*a+f*f+d*d-b*b-e*e)+c*c*f*f*(a*a+b*b+d*d+e*e-c*c-f*f)-a*a*b*b*c*c-a*a*e*e*f*f-d*d*b*b*f*f-d*d*e*e*c*c;\n\treturn x>0?sqrt(x / 144):0;\n}\nint main(){\n\tint N;\n\twhile(cin >> N && N){\n\t\tvector<int> t(N);\n\t\tdouble ans = 0;\n\t\tvector<int> reflesh(6,0);\n\t\t\n\t\tint cnt = 0;\n\t\t//cout << calc(P(0,0),P(0,1),P(1,1),1,1,1) << endl;\n\t\tfor(int i = 0 ; i < N ; i++) cin >> t[i];\n\t\tsort(t.begin(),t.end());\n\t\tfor(int i = 0 ; i < (1<<N) ; i++){\n\t\t\tif( __builtin_popcount(i) == 6 ){\n\t\t\t\tvector<int> v;\n\t\t\t\tfor(int j = 0 ; j < N ; j++) if(i>>j&1)v.push_back(t[j]);\n\t\t\t\tdo{\n\t\t\t\t\tdouble a,b,c,d,e,f;\n\t\t\t\t\ttie(a,b,c,d,e,f) = tuple<double,double,double,double,double,double>{v[0],v[1],v[2],v[3],v[4],v[5]};\n\t\t\t\t\tif( chk(a,e,f) && chk(a,b,c) && chk(e,d,c) && chk(d,b,f) ){\n\t\t\t\t\t\tans = max(ans,calc(v[0],v[1],v[2],v[3],v[4],v[5]));\n\t\t\t\t\t}\n\t\t\t\t}while( next_permutation(v.begin(),v.end()) );\n\t\t\t}\n\t\t}\n\t\t//cout << cnt << endl;\n\t\tif(ans == 0){\n\t\t\tfor(int i = 0 ; i < N ; i++) cout << t[i] << \" \"; cout << endl;\n\t\t}\n\t\tprintf(\"%.10lf%c\",ans,10);\n\t}\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 1232, "score_of_the_acc": -0.2424, "final_rank": 5 }, { "submission_id": "aoj_2060_1002134", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nusing namespace std;\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\nconst int IINF = INT_MAX;\nconst int inf = INT_MAX;\n//\nclass Point{\npublic:\n double x,y;\n\n Point(double x = -IINF,double y = -IINF): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:y<p.y; }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return p.p1 == p1 && p.p2 == p2; }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev から cur へ行って next へ行く際の角度を求める\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n//\n\n// CCW\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n// CCW END\n\n// intersect\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\n/*\nsame lineの時注意\nもしm.p1が中心でなかったときに正しい答えとならない\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]);\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n*/\nPoint crosspoint(Line l, Line m) {\n double A = cross(l.p2 - l.p1, m.p2 - m.p1);\n double B = cross(l.p2 - l.p1, l.p2 - m.p1);\n if (abs(A) < EPS && abs(B) < EPS) return m.p1; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m.p1 + (m.p2 - m.p1) * (B / A);\n}\n\n// intersect END\n\nint array[6000];\n\ndouble areaT(double a,double b,double c){\n double s = (a+b+c)/2.0;\n return sqrt(s*(s-a)*(s-b)*(s-c));\n}\n\ndouble areaH(double h,double *a){ return ( areaT(a[0],a[1],a[2]) * h) / 3.0; }\n\nbool LTE(double a,double b){ return equals(a,b) || a < b; }\n\nbool LT(double a,double b){ return !equals(a,b) && a < b; }\n\nbool GTE(double a,double b){ return equals(a,b) || a > b; }\n\nbool check1(double a,double b,double c){ return a + b >= c; }\n\nbool checkT(double a,double b,double c){ return LT(a,b+c) && LT(b,a+c) && LT(c,a+b); }\n\nbool check2(double h,double *a){\n if( LTE(a[3]*a[3]-h*h,0) || LTE(a[4]*a[4]-h*h,0) || LTE(a[5]*a[5]-h*h,0) ) return false;\n double L1 = sqrt(a[3]*a[3]-h*h);\n double L2 = sqrt(a[4]*a[4]-h*h);\n double L3 = sqrt(a[5]*a[5]-h*h);\n if( LTE(L1,0) || LTE(L2,0) || LTE(L3,0) ) return false;\n if( !checkT(L1,L2,a[0]) || !checkT(L2,L3,a[2]) || !checkT(L2,L3,a[2]) ) return false;\n cout << endl;\n cout << L1 << \" \" << L2 << \" \" << L3 << endl;\n double tmp_area = areaT(L1,L2,a[0]) + areaT(L2,L3,a[2]) + areaT(L1,L3,a[1]);\n cout << tmp_area << \" (\" << areaT(L1,L2,a[0]) << \"+\" << areaT(L2,L3,a[2]) << \"+\" << areaT(L1,L3,a[1]) << \") \"<< \" ?= \" << areaT(a[0],a[1],a[2]) << endl;\n return equals(tmp_area,areaT(a[0],a[1],a[2]));\n}\n\nSegment gen(Point p1,Point p2,double a,double b,double c,bool &error){\n double S = areaT(a,b,c);\n cout << \"S = \" << S << endl;\n double h = 2 * S / a;\n if( LTE(h,0) ) error = true;\n if( LTE(b*b-h*h,0) ) error = true;\n if( error ) return Segment();\n cout << c * c << \" - \" << h*h << endl; \ndouble x = sqrt(c*c-h*h);\n cout << h << \",\" << x << endl;\n Segment seg;\n seg.p1 = p1;\n Vector v = p2 - p1;\n Vector ve = v / abs(v);\n seg.p2 = p1 + ve * x;\n cout << v << endl;\n v = rotate(v,toRad(90));\n cout << v << endl;\n ve = v / abs(v);\n seg.p2 = seg.p2 + ve * h;\n return seg;\n}\n\n//\n typedef vector<int> VI;\n typedef vector<VI> VVI;\n typedef VVI matrix;\n\n\n\nmatrix identity(int n)\n{\n matrix A(n,vector<int>(n));\n for(int i=0;i<n;i++) A[i][i] = 1;\n return A;\n}\n\n\nvector<int> multi(const matrix& A,const vector<int>& B)\n{\n vector<int> ret(A.size());\n for(int i=0;i<A.size();++i)\n for(int j=0;j<A[0].size();++j)\n ret[i] += A[i][j]*B[j];\n return ret;\n}\n\n\nmatrix multi(const matrix& A,const matrix& B)\n{\n matrix C(A.size(),vector<int>(B[0].size()));\n for(int i=0;i<C.size();++i)\n for(int j=0;j<C[i].size();++j)\n for(int k=0;k<A[i].size();++k)\n\tC[i][j] += A[i][k]*B[k][j];\n return C;\n}\n\n\n\nmatrix pow(const matrix& A,int e)\n{\n return !e?identity(A.size()):\n (e%2?multi(A,pow(A,e-1)):pow(multi(A,A),e/2));\n}\n/*\n冪乗に関するちょいと解説\n行列をAする\nA^2 = A*A, A^4 = (A^2)*(A^2), A^8 = (A^4)*(A^4) ・・・・と計算している\n行列に限ったことでなく、普通の値の冪乗でも同じように考えることができる\n*/\n\n\nint extgcd(int a,int b,int& x,int& y)\n{\n int d = a;\n if(b != 0){\n d = extgcd(b,a%b,y,x);\n y -= (a/b)*x;\n }\n else\n x = 1,y = 0;\n return d;\n}\n\nint mod_inv(int a,int m)\n{\n int x,y;\n extgcd(a,m,x,y);\n return (m+x%m)%m;\n}\n\n\nint rank(vector<vector<int> > A,int mod)\n{\n int H = A.size(), W = A[0].size();\n int r = 0;\n rep(x,W)\n { \n int pivot = inf;\n REP(y,r,H)\n\tif(A[y][x])\n\t {\n\t pivot = y;\n\t break;\n\t }\n if(pivot == inf)continue;\n swap(A[r],A[pivot]);\n\n REP(y,r+1,H)\n\t{\n\t int value = A[y][x] * mod_inv(A[r][x],mod) % mod;\n\t if(value)REP(x2,x,W)\n\t\t A[y][x2] = ((A[y][x2] - A[r][x2] * value)%mod + mod ) % mod; \n\t}\n r++;\n }\n return r;\n}\n\n\n/*\n\nAx=bが解を持つ必要十分条件は、\nrank(A) = rank(A|b)\nであり、\nAx=bが一意の解を持つ必要十分条件は、\nrank(A) = rank(A|b) = n\n*/\n\nbool modGaussElimination(vector<vector<int> > &A, vector<int> &b, int m, vector<int> &res) {\n int n = A.size();\n vector<vector<int> > B(n, vector<int>(n+1));\n rep(i,n) rep(j,n)\n B[i][j] = A[i][j];\n rep(i, n) B[i][n] = b[i];\n \n int nowy = 0;\n rep(x, n) {\n int pivot = -1;\n for (int j=nowy; j<n; ++j)\n if (B[j][x]) {\n pivot = j;break;\n }\n if (pivot == -1) continue;\n swap(B[nowy], B[pivot]);\n\n for (int j=nowy+1; j<n; ++j) {\n int t = B[j][x] * mod_inv(B[nowy][x], m) % m;\n if (t)\n for (int k=x; k<=n; ++k)\n B[j][k] = ((B[j][k] - B[nowy][k] * t)%m + m )%m;\n }\n nowy++;\n \n }\n\n res.clear();\n for (int y=nowy; y<n; ++y)\n if (B[y][n]) // rank(A) != rank(A|b)\n return 0;\n if (nowy != n) { // rank(A) == rank(A|b) != n\n // 解が一意でない。ひとつ求める\n res = vector<int>(n,inf); // INFは任意を表す\n for (int y=n-1; y>=0; --y) {\n int x;\n for (x=y; x<n; ++x) {\n if (B[y][x])\n break;\n }\n if (x==n) continue;\n int sum = B[y][n];\n \n for (int i=x+1; i<n; ++i) {\n if (res[i] == inf) res[i] = 0; // この時点でres[i]が決まってなかったら0とする\n sum = ((sum - res[i] * B[y][i])%m + m)%m;\n }\n res[x] = sum * mod_inv(B[y][x], m) % m;\n }\n rep(i, n) if (res[i]==inf) res[i] = 0;\n return 0;\n }\n // 解が一意に決まる。普通に後退代入\n res.resize(n);\n for (int x=n-1; x>=0; --x) {\n int sum = B[x][n];\n for (int i=n-1; i>x; --i) {\n sum = ((sum - res[i] * B[x][i])%m + m)%m; \n }\n res[x] = sum * mod_inv(B[x][x], m) % m;\n }\n return 1;\n}\n\n//double det(vector<vector<double> > &A) {\ndouble det(double A[5][5]){\n const int n = 5;\n double D = 1;\n for (int i = 0; i < n; ++i) {\n int pivot = i;\n for (int j = i+1; j < n; ++j)\n if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;\n swap(A[pivot], A[i]);\n D *= A[i][i] * (i != pivot ? -1 : 1);\n if (abs(A[i][i]) < EPS) break;\n for(int j = i+1; j < n; ++j)\n for(int k = n-1; k >= i; --k)\n A[j][k] -= A[i][k] * A[j][i] / A[i][i];\n }\n return D;\n}\n\n\n//\n\ndouble getAreaTetrahedra(double *A){\n double a = A[3], b = A[1], c = A[0], p = A[5], q = A[4], r = A[2];\n\n /*\n vector<vector<double> > mat(5,vector<double>(5,0));\n mat[0][1] = p*p, mat[0][2] = q*q, mat[0][3] = a*a, mat[0][4] = 1;\n mat[1][0] = p*p, mat[1][2] = r*r, mat[1][3] = b*b, mat[1][4] = 1;\n mat[2][0] = q*q, mat[2][1] = r*r, mat[2][3] = c*c, mat[2][4] = 1;\n mat[3][0] = a*a, mat[3][1] = b*b, mat[3][2] = c*c, mat[3][4] = 1;\n mat[4][0] = 1, mat[4][1] = 1, mat[4][2] = 1, mat[4][3] = 1;\n */\n double mat[5][5] = {{0,p*p,q*q,a*a,1},{p*p,0,r*r,b*b,1},{q*q,r*r,0,c*c,1},{a*a,b*b,c*c,0,1},{1,1,1,1,0}};\n return sqrt(det(mat)/288.0);\n}\n\ndouble calc(double *a){\n double ret = 0;\n do{\n if( !checkT(a[0],a[1],a[2]) || !checkT(a[0],a[3],a[4]) || !checkT(a[1],a[3],a[5]) || !checkT(a[2],a[4],a[5]) ) continue;\n /*\n Point p1(0,0);\n Point p2(a[1],0);\n double theta = acos( ( a[0]*a[0] + a[1]*a[1] - a[2]*a[2] ) / ( 2*a[0]*a[1] ) );\n Point p3(a[0]*cos(theta),a[0]*sin(theta));\n //cout << p1 << \" \" << p2 << \" \" << p3 << endl;\n double A = a[1], b =a[0], c = a[2];\n double x0 = (A*A-b*b-c*c)/(2*A);\n //cout << \"(\" << (A*A-b*b-c*c)/(2*A) << \",\" << sqrt(b*b-x0*x0)<<\")\"<<endl;\n */\n //if( error ) continue;\n //if( error ) continue;\n //if( LTE(a[3]*a[3]-x*x,0) ) continue;\n //if( LTE(h,0) ) continue;\n ret = max(ret,getAreaTetrahedra(a));\n }while(next_permutation(a,a+6));\n return ret;\n}\n\nint main(){\n /*\n double a[6] = {2,2,2,2,2,2};\n cout << calc(a) << endl;\n return 0;\n */\n int dex = 0;\n rep(i,(1<<15)) if( __builtin_popcount(i) == 6 ) array[dex++] = i;\n int N,input[16];\n while( scanf(\"%d\",&N), N ){\n rep(i,N) scanf(\"%d\",input+i);\n sort(input,input+N);\n double maxi = 0;\n rep(k,dex){\n int bitmask = array[k];\n if( bitmask >= (1<<N) ) break;\n double buf[6];\n int cur = 0;\n rep(j,N) if( (bitmask>>j) & 1 ) buf[cur++] = input[j];\n maxi = max(maxi,calc(buf));\n }\n printf(\"%.10f\\n\",maxi);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5320, "memory_kb": 1288, "score_of_the_acc": -0.8767, "final_rank": 16 }, { "submission_id": "aoj_2060_1002131", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nusing namespace std;\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\nconst int IINF = INT_MAX;\nconst int inf = INT_MAX;\n//\nclass Point{\npublic:\n double x,y;\n\n Point(double x = -IINF,double y = -IINF): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:y<p.y; }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator == (const Segment& p)const { return p.p1 == p1 && p.p2 == p2; }\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad は角度をラジアンで持たせること\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// 度をラジアンに変換\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev から cur へ行って next へ行く際の角度を求める\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n//\n\n// CCW\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n// CCW END\n\n// intersect\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\n/*\nsame lineの時注意\nもしm.p1が中心でなかったときに正しい答えとならない\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]);\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n*/\nPoint crosspoint(Line l, Line m) {\n double A = cross(l.p2 - l.p1, m.p2 - m.p1);\n double B = cross(l.p2 - l.p1, l.p2 - m.p1);\n if (abs(A) < EPS && abs(B) < EPS) return m.p1; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m.p1 + (m.p2 - m.p1) * (B / A);\n}\n\n// intersect END\n\nint array[6000];\n\ndouble areaT(double a,double b,double c){\n double s = (a+b+c)/2.0;\n return sqrt(s*(s-a)*(s-b)*(s-c));\n}\n\ndouble areaH(double h,double *a){ return ( areaT(a[0],a[1],a[2]) * h) / 3.0; }\n\nbool LTE(double a,double b){ return equals(a,b) || a < b; }\n\nbool LT(double a,double b){ return !equals(a,b) && a < b; }\n\nbool GTE(double a,double b){ return equals(a,b) || a > b; }\n\nbool check1(double a,double b,double c){ return a + b >= c; }\n\nbool checkT(double a,double b,double c){ return LT(a,b+c) && LT(b,a+c) && LT(c,a+b); }\n\nbool check2(double h,double *a){\n if( LTE(a[3]*a[3]-h*h,0) || LTE(a[4]*a[4]-h*h,0) || LTE(a[5]*a[5]-h*h,0) ) return false;\n double L1 = sqrt(a[3]*a[3]-h*h);\n double L2 = sqrt(a[4]*a[4]-h*h);\n double L3 = sqrt(a[5]*a[5]-h*h);\n if( LTE(L1,0) || LTE(L2,0) || LTE(L3,0) ) return false;\n if( !checkT(L1,L2,a[0]) || !checkT(L2,L3,a[2]) || !checkT(L2,L3,a[2]) ) return false;\n cout << endl;\n cout << L1 << \" \" << L2 << \" \" << L3 << endl;\n double tmp_area = areaT(L1,L2,a[0]) + areaT(L2,L3,a[2]) + areaT(L1,L3,a[1]);\n cout << tmp_area << \" (\" << areaT(L1,L2,a[0]) << \"+\" << areaT(L2,L3,a[2]) << \"+\" << areaT(L1,L3,a[1]) << \") \"<< \" ?= \" << areaT(a[0],a[1],a[2]) << endl;\n return equals(tmp_area,areaT(a[0],a[1],a[2]));\n}\n\nSegment gen(Point p1,Point p2,double a,double b,double c,bool &error){\n double S = areaT(a,b,c);\n cout << \"S = \" << S << endl;\n double h = 2 * S / a;\n if( LTE(h,0) ) error = true;\n if( LTE(b*b-h*h,0) ) error = true;\n if( error ) return Segment();\n cout << c * c << \" - \" << h*h << endl; \ndouble x = sqrt(c*c-h*h);\n cout << h << \",\" << x << endl;\n Segment seg;\n seg.p1 = p1;\n Vector v = p2 - p1;\n Vector ve = v / abs(v);\n seg.p2 = p1 + ve * x;\n cout << v << endl;\n v = rotate(v,toRad(90));\n cout << v << endl;\n ve = v / abs(v);\n seg.p2 = seg.p2 + ve * h;\n return seg;\n}\n\n//\n typedef vector<int> VI;\n typedef vector<VI> VVI;\n typedef VVI matrix;\n\n\n\nmatrix identity(int n)\n{\n matrix A(n,vector<int>(n));\n for(int i=0;i<n;i++) A[i][i] = 1;\n return A;\n}\n\n\nvector<int> multi(const matrix& A,const vector<int>& B)\n{\n vector<int> ret(A.size());\n for(int i=0;i<A.size();++i)\n for(int j=0;j<A[0].size();++j)\n ret[i] += A[i][j]*B[j];\n return ret;\n}\n\n\nmatrix multi(const matrix& A,const matrix& B)\n{\n matrix C(A.size(),vector<int>(B[0].size()));\n for(int i=0;i<C.size();++i)\n for(int j=0;j<C[i].size();++j)\n for(int k=0;k<A[i].size();++k)\n\tC[i][j] += A[i][k]*B[k][j];\n return C;\n}\n\n\n\nmatrix pow(const matrix& A,int e)\n{\n return !e?identity(A.size()):\n (e%2?multi(A,pow(A,e-1)):pow(multi(A,A),e/2));\n}\n/*\n冪乗に関するちょいと解説\n行列をAする\nA^2 = A*A, A^4 = (A^2)*(A^2), A^8 = (A^4)*(A^4) ・・・・と計算している\n行列に限ったことでなく、普通の値の冪乗でも同じように考えることができる\n*/\n\n\nint extgcd(int a,int b,int& x,int& y)\n{\n int d = a;\n if(b != 0){\n d = extgcd(b,a%b,y,x);\n y -= (a/b)*x;\n }\n else\n x = 1,y = 0;\n return d;\n}\n\nint mod_inv(int a,int m)\n{\n int x,y;\n extgcd(a,m,x,y);\n return (m+x%m)%m;\n}\n\n\nint rank(vector<vector<int> > A,int mod)\n{\n int H = A.size(), W = A[0].size();\n int r = 0;\n rep(x,W)\n { \n int pivot = inf;\n REP(y,r,H)\n\tif(A[y][x])\n\t {\n\t pivot = y;\n\t break;\n\t }\n if(pivot == inf)continue;\n swap(A[r],A[pivot]);\n\n REP(y,r+1,H)\n\t{\n\t int value = A[y][x] * mod_inv(A[r][x],mod) % mod;\n\t if(value)REP(x2,x,W)\n\t\t A[y][x2] = ((A[y][x2] - A[r][x2] * value)%mod + mod ) % mod; \n\t}\n r++;\n }\n return r;\n}\n\n\n/*\n\nAx=bが解を持つ必要十分条件は、\nrank(A) = rank(A|b)\nであり、\nAx=bが一意の解を持つ必要十分条件は、\nrank(A) = rank(A|b) = n\n*/\n\nbool modGaussElimination(vector<vector<int> > &A, vector<int> &b, int m, vector<int> &res) {\n int n = A.size();\n vector<vector<int> > B(n, vector<int>(n+1));\n rep(i,n) rep(j,n)\n B[i][j] = A[i][j];\n rep(i, n) B[i][n] = b[i];\n \n int nowy = 0;\n rep(x, n) {\n int pivot = -1;\n for (int j=nowy; j<n; ++j)\n if (B[j][x]) {\n pivot = j;break;\n }\n if (pivot == -1) continue;\n swap(B[nowy], B[pivot]);\n\n for (int j=nowy+1; j<n; ++j) {\n int t = B[j][x] * mod_inv(B[nowy][x], m) % m;\n if (t)\n for (int k=x; k<=n; ++k)\n B[j][k] = ((B[j][k] - B[nowy][k] * t)%m + m )%m;\n }\n nowy++;\n \n }\n\n res.clear();\n for (int y=nowy; y<n; ++y)\n if (B[y][n]) // rank(A) != rank(A|b)\n return 0;\n if (nowy != n) { // rank(A) == rank(A|b) != n\n // 解が一意でない。ひとつ求める\n res = vector<int>(n,inf); // INFは任意を表す\n for (int y=n-1; y>=0; --y) {\n int x;\n for (x=y; x<n; ++x) {\n if (B[y][x])\n break;\n }\n if (x==n) continue;\n int sum = B[y][n];\n \n for (int i=x+1; i<n; ++i) {\n if (res[i] == inf) res[i] = 0; // この時点でres[i]が決まってなかったら0とする\n sum = ((sum - res[i] * B[y][i])%m + m)%m;\n }\n res[x] = sum * mod_inv(B[y][x], m) % m;\n }\n rep(i, n) if (res[i]==inf) res[i] = 0;\n return 0;\n }\n // 解が一意に決まる。普通に後退代入\n res.resize(n);\n for (int x=n-1; x>=0; --x) {\n int sum = B[x][n];\n for (int i=n-1; i>x; --i) {\n sum = ((sum - res[i] * B[x][i])%m + m)%m; \n }\n res[x] = sum * mod_inv(B[x][x], m) % m;\n }\n return 1;\n}\n\n//double det(vector<vector<double> > &A) {\ndouble det(double A[5][5]) {\n const int n = 5;\n double D = 1;\n for (int i = 0; i < n; ++i) {\n int pivot = i;\n for (int j = i+1; j < n; ++j)\n if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;\n swap(A[pivot], A[i]);\n D *= A[i][i] * (i != pivot ? -1 : 1);\n if (abs(A[i][i]) < EPS) break;\n for(int j = i+1; j < n; ++j)\n for(int k = n-1; k >= i; --k)\n A[j][k] -= A[i][k] * A[j][i] / A[i][i];\n }\n return D;\n}\n\n\n//\n\ndouble getAreaTetrahedra(double *A){\n double a = A[3], b = A[1], c = A[0], p = A[5], q = A[4], r = A[2];\n\n /*\n vector<vector<double> > mat(5,vector<double>(5,0));\n mat[0][1] = p*p, mat[0][2] = q*q, mat[0][3] = a*a, mat[0][4] = 1;\n mat[1][0] = p*p, mat[1][2] = r*r, mat[1][3] = b*b, mat[1][4] = 1;\n mat[2][0] = q*q, mat[2][1] = r*r, mat[2][3] = c*c, mat[2][4] = 1;\n mat[3][0] = a*a, mat[3][1] = b*b, mat[3][2] = c*c, mat[3][4] = 1;\n mat[4][0] = 1, mat[4][1] = 1, mat[4][2] = 1, mat[4][3] = 1;\n */\n double mat[5][5] = {{0,p*p,q*q,a*a,1},{p*p,0,r*r,b*b,1},{q*q,r*r,0,c*c,1},{a*a,b*b,c*c,0,1},{1,1,1,1,0}};\n return det(mat)/288.0;\n}\n\ndouble calc(double *a){\n double ret = 0;\n do{\n if( !checkT(a[0],a[1],a[2]) || !checkT(a[0],a[3],a[4]) || !checkT(a[1],a[3],a[5]) || !checkT(a[2],a[4],a[5]) ) continue;\n /*\n Point p1(0,0);\n Point p2(a[1],0);\n double theta = acos( ( a[0]*a[0] + a[1]*a[1] - a[2]*a[2] ) / ( 2*a[0]*a[1] ) );\n Point p3(a[0]*cos(theta),a[0]*sin(theta));\n //cout << p1 << \" \" << p2 << \" \" << p3 << endl;\n double A = a[1], b =a[0], c = a[2];\n double x0 = (A*A-b*b-c*c)/(2*A);\n //cout << \"(\" << (A*A-b*b-c*c)/(2*A) << \",\" << sqrt(b*b-x0*x0)<<\")\"<<endl;\n */\n //if( error ) continue;\n //if( error ) continue;\n //if( LTE(a[3]*a[3]-x*x,0) ) continue;\n //if( LTE(h,0) ) continue;\n ret = max(ret,sqrt(getAreaTetrahedra(a)));\n }while(next_permutation(a,a+6));\n return ret;\n}\n\nint main(){\n /*\n double a[6] = {2,2,2,2,2,2};\n cout << calc(a) << endl;\n return 0;\n */\n int dex = 0;\n rep(i,(1<<15)) if( __builtin_popcount(i) == 6 ) array[dex++] = i;\n int N,input[16];\n while( scanf(\"%d\",&N), N ){\n rep(i,N) scanf(\"%d\",input+i);\n sort(input,input+N);\n double maxi = 0;\n rep(k,dex){\n int bitmask = array[k];\n if( bitmask >= (1<<N) ) break;\n double buf[6];\n int cur = 0;\n rep(j,N) if( (bitmask>>j) & 1 ) buf[cur++] = input[j];\n maxi = max(maxi,calc(buf));\n }\n printf(\"%.10f\\n\",maxi);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5250, "memory_kb": 1288, "score_of_the_acc": -0.8649, "final_rank": 15 }, { "submission_id": "aoj_2060_551697", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<cstdio>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n\ndouble a[20];\nint index[6], n;\n\nbool ng(double a, double b, double c) {\n if (a + b <= c) return true;\n if (b + c <= a) return true;\n if (c + a <= b) return true;\n return false;\n}\n\t\t\t\t \n\ndouble solve(int m) {\n if (m == 6) {\n if (ng(a[index[0]], a[index[1]], a[index[2]])) return 0;\n if (ng(a[index[0]], a[index[3]], a[index[4]])) return 0;\n if (ng(a[index[1]], a[index[4]], a[index[5]])) return 0;\n if (ng(a[index[2]], a[index[5]], a[index[3]])) return 0;\n double aa[3], bb[3], t, pi[4];\n aa[0] = a[index[0]];\n aa[1] = a[index[1]];\n aa[2] = a[index[2]];\n bb[0] = a[index[5]];\n bb[1] = a[index[3]];\n bb[2] = a[index[4]];\n t = 0;\n rep (i, 3) t += aa[i] * aa[i];\n rep (i, 3) t += bb[i] * bb[i];\n t = sqrt(max(t, 0.));\n pi[0] = aa[0] * aa[1] * aa[2];\n pi[1] = aa[0] * bb[1] * bb[2];\n pi[2] = aa[1] * bb[2] * bb[0];\n pi[3] = aa[2] * bb[0] * bb[1];\n double v = 0;\n rep (i, 3) v += aa[i] * aa[i] * bb[i] * bb[i] * (t * t- 2 * aa[i] * aa[i] - 2 * bb[i] * bb[i]);\n rep (i, 4) v -= pi[i] * pi[i];\n return sqrt(max(v, 0.)) / 12;\n }\n double res = 0;\n rep (i, n) {\n bool ok = true;\n rep (j, m) if (index[j] == i) ok = false;\n if (ok) {\n index[m] = i;\n res = max(solve(m + 1), res);\n }\n }\n return res;\n}\n\nint main() {\n while (true) {\n cin >> n;\n if (n == 0) break;\n rep (i, n) cin >> a[i];\n printf(\"%.12f\\n\", solve(0));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2390, "memory_kb": 1244, "score_of_the_acc": -0.3837, "final_rank": 10 }, { "submission_id": "aoj_2060_472504", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nconst double EPS = 1.0e-10;\n\ndouble determinant(vector<vector<double> > mat)\n{ \n const int n = mat.size();\n double ret = 1.0;\n for(int i=0; i<n; ++i){\n int tmp = i;\n for(int j=i+1; j<n; ++j){\n if(abs(mat[j][i]) > abs(mat[tmp][i]))\n tmp = j;\n }\n if(abs(mat[tmp][i]) < EPS)\n return 0.0;\n if(tmp != i){\n mat[i].swap(mat[tmp]);\n ret *= -1.0;\n }\n\n for(int j=i+1; j<n; ++j){\n for(int k=n-1; k>=i; --k){\n mat[j][k] -= mat[i][k] * (mat[j][i] / mat[i][i]);\n }\n }\n ret *= mat[i][i];\n }\n return ret;\n}\n\ndouble tetrahedronVolume(const vector<double>& len)\n{\n const int a[][3] = {{0,1,3}, {1,2,4}, {0,2,5}, {3,4,5}};\n for(int i=0; i<4; ++i){\n vector<double> b(3);\n for(int j=0; j<3; ++j)\n b[j] = len[a[i][j]];\n sort(b.begin(), b.end());\n if(b[2] > b[0] + b[1] - EPS)\n return 0.0;\n }\n\n const int y[] = {0, 3, 1, 3, 2, 3, 0, 1, 1, 2, 0, 2};\n const int x[] = {3, 0, 3, 1, 3, 2, 1, 0, 2, 1, 2, 0};\n\n vector<vector<double> > mat(5, vector<double>(5, 1.0));\n for(int i=0; i<12; ++i)\n mat[y[i]][x[i]] = pow(len[i/2], 2.0);\n for(int i=0; i<5; ++i)\n mat[i][i] = 0.0;\n\n return sqrt(determinant(mat) / 288.0);\n}\n\ntemplate <size_t T>\nbool nextBitset(bitset<T> &bs, int digit)\n{\n if(bs.none())\n return false;\n bitset<T> x, y, z;\n x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);\n y = bs.to_ulong() + x.to_ulong() + 0ULL;\n z = bs & ~y;\n if(bs[digit-1] && bs == z)\n return false;\n bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;\n bs |= y;\n return true;\n}\n\nint main()\n{\n for(;;){\n int n;\n cin >> n;\n if(n == 0)\n return 0;\n\n vector<double> len(n);\n for(int i=0; i<n; ++i)\n cin >> len[i];\n\n double ret = 0.0;\n bitset<15> used;\n for(int i=0; i<n; ++i){\n for(int j=0; j<i; ++j){\n for(int k=0; k<j; ++k){\n used[i] = used[j] = used[k] = true;\n\n for(int a=0; a<n; ++a){\n for(int b=0; b<n; ++b){\n for(int c=0; c<n; ++c){\n if(a == b || b == c || c == a)\n continue;\n if(used[a] || used[b] || used[c])\n continue;\n\n vector<double> len2(6);\n len2[0] = len[a];\n len2[1] = len[b];\n len2[2] = len[c];\n len2[3] = len[i];\n len2[4] = len[j];\n len2[5] = len[k];\n ret = max(ret, tetrahedronVolume(len2));\n ;\n }\n }\n }\n\n used[i] = used[j] = used[k] = false;\n }\n }\n }\n\n printf(\"%.10f\\n\", ret);\n }\n}", "accuracy": 1, "time_ms": 3930, "memory_kb": 1272, "score_of_the_acc": -0.6429, "final_rank": 14 }, { "submission_id": "aoj_2060_418972", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\ntypedef long long ll;\n\nint n;\nint t[20];\nvector<int> v[20];\n\nvoid makePattern(int idx, int cnt, int bit){\n if(cnt > 6) return;\n if(idx == n){\n if(cnt == 6){\n v[n].push_back(bit);\n }\n return;\n }\n\n makePattern(idx + 1, cnt, bit);\n makePattern(idx + 1, cnt + 1, bit | (1 << idx));\n}\n\nbool check(ll x, ll y, ll z){\n return x + y > z && x + z > y && y + z > x;\n}\n\nvoid solve(){\n double ans = 0;\n\n for(int i = 0; i < v[n].size(); i++){\n vector<int> a;\n\n for(int j = 0; j < n; j++){\n if(v[n][i] & (1 << j)){\n a.push_back(t[j]);\n }\n }\n\n sort(a.begin(), a.end());\n\n do{\n ll A = a[0];\n ll B = a[1];\n ll C = a[2];\n ll X = a[3];\n ll Y = a[4];\n ll Z = a[5];\n\n if(!check(A, B, C) || !check(A, Y, Z) ||\n !check(B, X, Z) || !check(C, Y, Z)){\n continue;\n }\n\n A *= A;\n B *= B;\n C *= C;\n X *= X;\n Y *= Y;\n Z *= Z;\n\n ll V =\n A * X * (B + C - A + Y + Z - X) +\n B * Y * (C + A - B + Z + X - Y) +\n C * Z * (A + B - C + X + Y - Z) -\n A * Y * Z -\n B * Z * X -\n C * X * Y -\n A * B * C;\n\n ans = max(ans, sqrt((double)V / 144));\n }while(next_permutation(a.begin(), a.end()));\n }\n\n printf(\"%.6f\\n\", ans);\n}\n\nint main(){\n for(n = 6; n <= 15; n++){\n makePattern(0, 0, 0);\n }\n\n while(cin >> n, n){\n for(int i = 0; i < n; i++){\n cin >> t[i];\n }\n solve();\n }\n}", "accuracy": 1, "time_ms": 5420, "memory_kb": 1052, "score_of_the_acc": -0.8908, "final_rank": 17 } ]
aoj_2064_cpp
Problem G: Make Friendships Isaac H. Ives attended an international student party and made a lot of girl friends (as many other persons expected). To strike up a good friendship with them, he decided to have dates with them. However, it is hard for him to schedule dates because he made so many friends. Thus he decided to find the best schedule using a computer program. The most important criterion in scheduling is how many different girl friends he will date. Of course, the more friends he will date, the better the schedule is. However, though he has the ability to write a program finding the best schedule, he doesn’t have enough time to write it. Your task is to write a program to find the best schedule instead of him. Input The input consists of a series of data sets. The first line of each data set contains a single positive integer N ( N ≤ 1,000) that represents the number of persons Isaac made friends with in the party. The next line gives Isaac’s schedule, followed by N lines that give his new friends’ schedules. Each schedule consists of a positive integer M that represents the number of available days followed by M positive integers each of which represents an available day. The input is terminated by a line that contains a single zero. This is not part of data sets and should not be processed. Output For each data set, print a line that contains the maximum number of girl friends Isaac can have dates with. Sample Input 3 3 1 3 5 2 1 4 4 1 2 3 6 1 3 0 Output for the Sample Input 2
[ { "submission_id": "aoj_2064_11055694", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<pair<ll ,ll>>> vvpll;\ntypedef vector<vector<ll>> vvll;\ntypedef vector<vector<vector<ll>>> vvvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\ntypedef tuple<ll , ll , ll , ll> tpl4;\ntypedef tuple<ll , ll , ll , ll , ll> tpl5;\ntypedef tuple<ll , ll , ll , ll , ll , ll> tpl6;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nstruct Edge {\n ll to,cap,rev;\n};\n\nclass MaximumFlow{\n public:\n ll siz = 0;\n vector<bool> used;\n vector<vector<Edge>> G;\n MaximumFlow(ll n){\n siz = n;\n used.resize(n+1);\n G.resize(n+1);\n }\n\n // a からbにcリットル流せる辺を追加\n void add_edge(ll a,ll b , ll c){\n ll ga_size = G[a].size();\n ll gb_size = G[b].size();\n G[a].push_back({b,c,gb_size });\n G[b].push_back({a,0,ga_size });\n }\n\n //Fはスタートからposまでの残余グラフの辺の容量の最小値\n //返り値は流したフローの量\n ll dfs(ll pos,ll goal,ll F){\n // ゴールに到着\n if(pos == goal) return F;\n used[pos] = true;\n for(ll i = 0; i < (ll)G[pos].size();i++){\n // 容量0の辺は使えない\n if(G[pos][i].cap == 0)continue;\n //探索済みだった\n if(used[G[pos][i].to] == true) continue;\n\n ll flow = dfs(G[pos][i].to,goal,min(F,G[pos][i].cap));\n\n // フローを流せるとき、残余グラフの容量をflowだけ増減\n if(flow > 0){\n G[pos][i].cap -= flow;\n G[G[pos][i].to][G[pos][i].rev].cap += flow;\n return flow;\n }\n }\n // 見つからず\n return 0;\n }\n\n ll max_flow(ll s,ll t){\n ll Total_Flow = 0;\n while(true){\n for(ll i = 0;i < siz;i++)used[i] = false;\n ll F = dfs(s,t,INF);\n\n //流せなくなった\n if(F== 0)break;\n Total_Flow += F;\n }\n return Total_Flow;\n }\n};\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n);\n if(n == 0)break;\n LL(m);\n map<ll,ll> mp;\n rep(i,m){\n LL(u);\n ll siz = sz(mp);\n if(!mp.contains(u)){\n mp[u] = siz;\n }\n }\n MaximumFlow mf(n+m+2);\n rep(i,m){\n mf.add_edge(n+m,i,1);\n }\n rep(i,n){\n LL(x);\n rep(j,x){\n LL(v);\n if(mp.contains(v)){\n mf.add_edge(mp[v],i+m,1);\n }\n }\n mf.add_edge(i+m,n+m+1,1);\n }\n cout << mf.max_flow(n+m,n+m+1) << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4096, "score_of_the_acc": -0.5697, "final_rank": 12 }, { "submission_id": "aoj_2064_11055691", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<pair<ll ,ll>>> vvpll;\ntypedef vector<vector<ll>> vvll;\ntypedef vector<vector<vector<ll>>> vvvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\ntypedef tuple<ll , ll , ll , ll> tpl4;\ntypedef tuple<ll , ll , ll , ll , ll> tpl5;\ntypedef tuple<ll , ll , ll , ll , ll , ll> tpl6;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nstruct Edge {\n ll to,cap,rev;\n};\n\nclass MaximumFlow{\n public:\n ll siz = 0;\n vector<bool> used;\n vector<vector<Edge>> G;\n MaximumFlow(ll n){\n siz = n;\n used.resize(n+1);\n G.resize(n+1);\n }\n\n // a からbにcリットル流せる辺を追加\n void add_edge(ll a,ll b , ll c){\n ll ga_size = G[a].size();\n ll gb_size = G[b].size();\n G[a].push_back({b,c,gb_size });\n G[b].push_back({a,0,ga_size });\n }\n\n //Fはスタートからposまでの残余グラフの辺の容量の最小値\n //返り値は流したフローの量\n ll dfs(ll pos,ll goal,ll F){\n // ゴールに到着\n if(pos == goal) return F;\n used[pos] = true;\n for(ll i = 0; i < (ll)G[pos].size();i++){\n // 容量0の辺は使えない\n if(G[pos][i].cap == 0)continue;\n //探索済みだった\n if(used[G[pos][i].to] == true) continue;\n\n ll flow = dfs(G[pos][i].to,goal,min(F,G[pos][i].cap));\n\n // フローを流せるとき、残余グラフの容量をflowだけ増減\n if(flow > 0){\n G[pos][i].cap -= flow;\n G[G[pos][i].to][G[pos][i].rev].cap += flow;\n return flow;\n }\n }\n // 見つからず\n return 0;\n }\n\n ll max_flow(ll s,ll t){\n ll Total_Flow = 0;\n while(true){\n for(ll i = 0;i < siz;i++)used[i] = false;\n ll F = dfs(s,t,INF);\n\n //流せなくなった\n if(F== 0)break;\n Total_Flow += F;\n }\n return Total_Flow;\n }\n};\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n);\n if(n == 0)break;\n LL(m);\n map<ll,ll> mp;\n rep(i,m){\n LL(u);\n ll siz = sz(mp);\n mp[u];\n }\n ll idx = 0;\n for (auto& elem : mp) elem.second = idx++;\n MaximumFlow mf(n+m+2);\n rep(i,m){\n mf.add_edge(n+m,i,1);\n }\n rep(i,n){\n LL(x);\n rep(j,x){\n LL(v);\n if(mp.contains(v)){\n mf.add_edge(mp[v],i+m,1);\n }\n }\n mf.add_edge(i+m,n+m+1,1);\n }\n cout << mf.max_flow(n+m,n+m+1) << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4096, "score_of_the_acc": -0.5697, "final_rank": 12 }, { "submission_id": "aoj_2064_9341738", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\n\nint main()\n{ \n int n;\n while (cin >> n, n) {\n int m;\n cin >> m;\n map<int, int> mp;\n rep(i, m) {\n int x;\n cin >> x;\n mp[x];\n }\n int idx = 0;\n for (auto& elem : mp) elem.second = idx++;\n\n Dinic dn(m + n + 2);\n int S = m + n, T = m + n + 1;\n rep(i, m) dn.addedge(S, i, 1);\n rep(i, n) dn.addedge(i + m, T, 1);\n\n rep(i, n) {\n int mg = 0;\n cin >> mg;\n rep(j, mg) {\n int x;\n cin >> x;\n if (mp.count(x)) dn.addedge(mp[x], i + m, 1);\n }\n }\n\n int flow = dn.maxflow(S, T);\n cout << flow << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3696, "score_of_the_acc": -0.6677, "final_rank": 14 }, { "submission_id": "aoj_2064_2743724", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n \nusing namespace std;\n \n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n \ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n \ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n \nstruct edge { int to, cap, rev; };\nconst int MAX_V = 100000;\n \nvector<edge> G[MAX_V];\nint level[MAX_V];\nint iter[MAX_V];\n \nvoid add_edge(int from, int to, int cap){\n //printf(\"add %d -> %d (%d)\\n\", from, to, cap);\n G[from].push_back((edge){to, cap, G[to].size()});\n G[to].push_back((edge){from, 0, G[from].size()-1});\n}\nvoid bfs(int s){\n memset(level, -1, sizeof(level));\n queue<int> que;\n level[s] = 0;\n que.push(s);\n while(!que.empty()){\n int v = que.front(); que.pop();\n for(int i = 0; i < G[v].size(); i++){\n edge &e = G[v][i];\n if(e.cap > 0 && level[e.to] < 0){\n level[e.to] = level[v] + 1;\n que.push(e.to);\n }\n }\n }\n}\n \nint dfs(int v, int t, int f){\n if(v == t) return f;\n for (int &i = iter[v]; i < G[v].size(); i++){\n edge &e = G[v][i];\n if(e.cap > 0 && level[v] < level[e.to]){\n int d = dfs(e.to, t, min(f, e.cap));\n if(d > 0){\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint Denic(int s, int t){\n int flow = 0;\n while(true){\n bfs(s);\n if(level[t] < 0) break;;\n memset(iter, 0, sizeof(iter));\n int f;\n while((f = dfs(s, t, INF)) > 0){\n flow += f;\n }\n }\n return flow;\n}\nint main(){\n int N;\n while(cin>>N && N){\n REP(i, MAX_V) G[i].clear();\n int M; cin>>M;\n vector<int> myday;\n REP(i, M){\n int t; cin>>t;\n myday.push_back(t);\n }\n sort(myday.begin(), myday.end());\n REP(i, N){\n int M2;\n cin>>M2;\n while(M2--){\n int t; cin>>t;\n if(binary_search(myday.begin(), myday.end(), t)){\n int idx = lower_bound(myday.begin(), myday.end(), t) - myday.begin();\n add_edge(i, N + idx, 1);\n }\n }\n }\n const int S = N + M;\n const int G = S + 1;\n REP(i, N){\n add_edge(S, i, 1);\n }\n REP(i, M){\n add_edge(N + i, G, 1);\n }\n cout<<Denic(S, G)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 6232, "score_of_the_acc": -1.2857, "final_rank": 19 }, { "submission_id": "aoj_2064_2741021", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n#define NUM 3000\n\n\n//辺を表す構造体(行先、容量、逆辺のインデックス)\nstruct Edge{\n\tEdge(int arg_to,int arg_capacity,int arg_rev_index){\n\t\tto = arg_to;\n\t\tcapacity = arg_capacity;\n\t\trev_index = arg_rev_index;\n\t}\n\tint to,capacity,rev_index;\n};\n\nint V,E;\nint N;\nmap<int,int> MAP;\nvector<Edge> G[NUM]; //グラフの隣接リスト表現\nint dist[NUM]; //sourceからの距離\nint cheked_index[NUM]; //どこまで調べ終わったか\n\n//fromからtoへ向かう容量capacityの辺をグラフに追加する\nvoid add_edge(int from,int to,int capacity){\n\tG[from].push_back(Edge(to,capacity,G[to].size()));\n\tG[to].push_back(Edge(from,0,G[from].size()-1)); //逆辺の、初期容量は0\n}\n\n//sourceからの最短距離をBFSで計算する\nvoid bfs(int source){\n\tfor(int i = 0; i < V; i++)dist[i] = -1;\n\tqueue<int> Q;\n\tdist[source] = 0;\n\tQ.push(source);\n\n\twhile(!Q.empty()){\n\t\tint node_id = Q.front();\n\t\tQ.pop();\n\t\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\t\tEdge &e = G[node_id][i];\n\t\t\tif(e.capacity > 0 && dist[e.to] < 0){ //辺の容量が正で、かつエッジの行先に未訪問の場合\n\t\t\t\tdist[e.to] = dist[node_id]+1;\n\t\t\t\tQ.push(e.to);\n\t\t\t}\n\t\t}\n\t}\n}\n\n//増加パスをDFSで探す\nint dfs(int node_id,int sink,int flow){\n\tif(node_id == sink)return flow; //終点についたらflowをreturn\n\n\tfor(int &i = cheked_index[node_id]; i < G[node_id].size(); i++){ //node_idから出ているエッジを調査\n\t\tEdge &e = G[node_id][i];\n\t\tif(e.capacity > 0 && dist[node_id] < dist[e.to]){ //流せる余裕があり、かつsourceからの距離が増加する方法である場合\n\t\t\tint tmp_flow = dfs(e.to,sink,min(flow,e.capacity)); //流せるだけ流す\n\t\t\tif(tmp_flow > 0){ //流せた場合\n\t\t\t\te.capacity -= tmp_flow; //流した分、エッジの容量を削減する\n\t\t\t\tG[e.to][e.rev_index].capacity += tmp_flow; //逆辺の容量を、流した分だけ増加させる\n\t\t\t\treturn tmp_flow;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}\n\n\n//sourceからsinkへの最大流を求める\nint max_flow(int source,int sink){ //source:始点 sink:終点\n\tint flow = 0,add;\n\twhile(true){ //増加パスが存在する限り、流量を追加し続ける\n\t\tbfs(source);\n\t\tif(dist[sink] < 0)break; //sourceからsinkへと辿り着く残余グラフがない、つまり増加パスが無くなった場合、break\n\t\tfor(int i = 0; i < V; i++)cheked_index[i] = 0;\n\t\twhile((add = dfs(source,sink,BIG_NUM)) > 0){ //増加パスが見つかる間、加算\n\t\t\tflow += add;\n\t\t}\n\t}\n\treturn flow;\n}\n\nvoid func(){\n\n\tMAP.clear();\n\tfor(int i = 0; i < NUM; i++)G[i].clear();\n\n\tint num,tmp,index = 2;\n\tscanf(\"%d\",&num);\n\n\tint source = 0,sink = 1;\n\n\tfor(int loop = 0; loop < num; loop++){\n\t\tscanf(\"%d\",&tmp);\n\t\tMAP[tmp] = index;\n\t\tadd_edge(source,index,1); //sourceからindexに、容量1の辺を張る\n\t\tindex++;\n\t}\n\n\tint integrate;\n\n\tfor(int loop = 0; loop < N; loop++){\n\t\tscanf(\"%d\",&num);\n\t\tadd_edge(index,sink,1); //1人の人物につき、最大流量1の集約路を張る\n\t\tintegrate = index;\n\t\tindex++;\n\n\t\tfor(int i = 0; i < num; i++){\n\t\t\tscanf(\"%d\",&tmp);\n\t\t\tauto at = MAP.find(tmp);\n\t\t\tif(at == MAP.end())continue; //Isaccの都合の良い日でなければ無視\n\t\t\tadd_edge(MAP[tmp],integrate,1); //Isaccの都合の良い日から、\n\t\t}\n\t}\n\n\tV = index;\n\n\tprintf(\"%d\\n\", max_flow(source,sink));\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3476, "score_of_the_acc": -0.4805, "final_rank": 7 }, { "submission_id": "aoj_2064_2729687", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nstruct Dinic{\n const int INF=1<<28;\n \n struct edge {\n int to,cap,rev;\n edge(){}\n edge(int to,int cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n int n;\n vector<vector<edge> > G;\n vector<map<int,int> > M;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int sz):n(sz),G(n),M(n),level(n),iter(n){}\n \n void add_edge(int from,int to,int cap){\n M[from][to]=G[from].size();\n M[to][from]=G[to].size();\n G[from].push_back(edge(to,cap,G[to].size()));\n // undirected\n //G[to].push_back(edge(from,cap,G[from].size()-1));\n // directed\n G[to].push_back(edge(from,0,G[from].size()-1));\n }\n \n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.push(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e = G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.push(e.to);\n }\n }\n }\n }\n \n int dfs(int v,int t,int f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n int d = dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n }\n \n int flow(int s,int t,int lim){\n int fl=0;\n for(;;){\n bfs(s);\n if(level[t]<0||lim==0) return fl;\n fill(iter.begin(),iter.end(),0);\n int f;\n while((f=dfs(s,t,lim))>0){\n fl+=f;\n lim-=f;\n }\n }\n }\n\n int flow(int s,int t){\n return flow(s,t,INF);\n }\n\n //cap==1 only\n bool back_edge(int s,int t,int from, int to){\n for(int i=0;i<(int)G[from].size();i++) {\n edge& e=G[from][i];\n if(e.to==to) {\n if(e.cap==0&&flow(from,to,1)==0) {\n flow(from,s,1);\n flow(t,to,1);\n return 1;\n }\n }\n }\n return 0;\n }\n};\nint main(){\n int n,m,x,N;\n while(cin>>n,n){\n map<int,int>M;\n cin>>N;\n r(i,N){\n cin>>x;\n M[x]=i;\n }\n Dinic d(N+n+2);\n r(i,N)d.add_edge(N+n,i,1);\n r(i,n)d.add_edge(N+i,N+n+1,1);\n r(i,n){\n cin>>m;\n r(j,m){\n cin>>x;\n if(M.count(x))d.add_edge(M[x],N+i,1);\n }\n }\n cout<<d.flow(N+n,N+n+1,1e8)<<endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3676, "score_of_the_acc": -0.7708, "final_rank": 16 }, { "submission_id": "aoj_2064_2488730", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n#ifdef _DEBUG\n#include \"dump.hpp\"\n#else\n#define dump(...)\n#endif\n\n//#define int long long\n#define rep(i,a,b) for(int i=(a);i<(b);i++)\n#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)\n#define all(c) begin(c),end(c)\nconst int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;\nconst int MOD = (int)(1e9) + 7;\ntemplate<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }\n\nstruct Dinic {\n\tusing Flow = int;\n\tstruct Edge {\n\t\tint to, rev;\n\t\tFlow cap;\n\t\tEdge() {}\n\t\tEdge(int to, int rev, Flow cap) :to(to), rev(rev), cap(cap) {}\n\t};\n\tint n;\n\tvector<vector<Edge>> g;\n\tvector<bool> used;\n\tvector<int> level;\n\tvector<int> iter;\n\tDinic(int n) :n(n), g(n), used(n), level(n), iter(n) {};\n\tvoid addArc(int from, int to, Flow cap) {\n\t\tg[from].emplace_back(to, (int)g[to].size(), cap);\n\t\tg[to].emplace_back(from, (int)g[from].size() - 1, 0);\n\t}\n\tvoid addEdge(int a, int b, Flow cap) {\n\t\tg[a].emplace_back(b, (int)g[b].size(), cap);\n\t\tg[b].emplace_back(a, (int)g[a].size() - 1, cap);\n\t}\n\tFlow maximumFlow(int s, int t) {\n\t\tFlow total = 0;\n\t\twhile (true) {\n\t\t\tlevelize(s);\n\t\t\tif (level[t] < 0)return total;\n\t\t\tfill(iter.begin(), iter.end(), 0);\n\t\t\tFlow f;\n\t\t\twhile (true) {\n\t\t\t\tf = augment(s, t, INF);\n\t\t\t\tif (f == 0)break;\n\t\t\t\ttotal += f;\n\t\t\t}\n\t\t}\n\t}\n\tFlow augment(int v, int t, Flow f) {\n\t\tif (v == t)return f;\n\t\tfor (int &i = iter[v]; i < g[v].size(); i++) {\n\t\t\tEdge &e = g[v][i];\n\t\t\tif (e.cap > 0 && level[v] < level[e.to]) {\n\t\t\t\tFlow d = augment(e.to, t, min(f, e.cap));\n\t\t\t\tif (d > 0) {\n\t\t\t\t\te.cap -= d;\n\t\t\t\t\tg[e.to][e.rev].cap += d;\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\tvoid levelize(int s) {\n\t\tfill(level.begin(), level.end(), -1);\n\t\tqueue<int> q;\n\t\tlevel[s] = 0;\n\t\tq.push(s);\n\t\twhile (q.size()) {\n\t\t\tint v = q.front(); q.pop();\n\t\t\tfor (int i = 0; i < g[v].size(); i++) {\n\t\t\t\tEdge &e = g[v][i];\n\t\t\t\tif (e.cap > 0 && level[e.to] < 0) {\n\t\t\t\t\tlevel[e.to] = level[v] + 1;\n\t\t\t\t\tq.push(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n};\n\nsigned main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tfor (int N; cin >> N&&N;) {\n\t\tint M; cin >> M;\n\t\tDinic dinic(M + N + 2);\n\t\tint s = dinic.n - 2, t = s + 1;\n\t\tunordered_map<int, int> mp;\n\t\trep(i, 0, M) { int d; cin >> d; mp[d] = i; }\n\t\trep(i, 0, N) {\n\t\t\tint m; cin >> m;\n\t\t\trep(j, 0, m) {\n\t\t\t\tint d; cin >> d;\n\t\t\t\tif (mp.count(d))\n\t\t\t\t\tdinic.addArc(mp[d], M + i, 1);\n\t\t\t}\n\t\t}\n\t\trep(i, 0, M)dinic.addArc(s, i, 1);\n\t\trep(i, 0, N)dinic.addArc(M + i, t, 1);\n\t\tcout << dinic.maximumFlow(s, t) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3288, "score_of_the_acc": -0.4069, "final_rank": 2 }, { "submission_id": "aoj_2064_2414678", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct edge{int to,cap,rev;};\n\nconst int MAX_V = 2000;\nconst int F_INF = 19191919;\nvector<edge> G[MAX_V];\nint lv[MAX_V], it[MAX_V];\n\nvoid add_edge(int from, int to, int cap){\n G[from].pb({to,cap,(int)G[to].size()});\n G[to].pb({from,0,(int)G[from].size()-1});\n}\n\nvoid dinic_bfs(int s){\n fill(lv,lv+MAX_V,-1);\n queue<int> que;\n lv[s]=0;\n que.push(s);\n while(!que.empty()){\n int v = que.front(); que.pop();\n rep(i,G[v].size()){\n edge &e = G[v][i];\n if(e.cap>0 && lv[e.to]<0){\n lv[e.to] = lv[v]+1;\n que.push(e.to);\n }\n }\n }\n}\n\nint dinic_dfs(int v, int t, int f){\n if(v==t) return f;\n for(int &i=it[v]; i<G[v].size(); ++i){\n edge &e = G[v][i];\n if(e.cap>0 && lv[v]<lv[e.to]){\n int d = dinic_dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n}\n\nint max_flow(int s, int t){\n int flow = 0;\n while(1){\n dinic_bfs(s);\n if(lv[t]<0) return flow;\n fill(it,it+MAX_V,0);\n int f;\n while((f=dinic_dfs(s,t,F_INF))>0) flow+=f;\n }\n}\n\nint main()\n{\n int n;\n while(scanf(\" %d\", &n),n)\n {\n rep(i,MAX_V) G[i].clear();\n\n unordered_map<int,int> m;\n int M = 0;\n\n int ISSAC;\n scanf(\" %d\", &ISSAC);\n while(ISSAC--)\n {\n int day;\n scanf(\" %d\", &day);\n if(!m.count(day)) m[day] = M++;\n }\n\n vector<int> k(n);\n vector<vector<int>> s(n);\n\n rep(i,n)\n {\n scanf(\" %d\", &k[i]);\n rep(j,k[i])\n {\n int day;\n scanf(\" %d\", &day);\n if(m.count(day)) s[i].pb(m[day]);\n }\n k[i] = s[i].size();\n }\n\n int S = n+M, T = S+1;\n rep(i,n)\n {\n add_edge(S,i,1);\n rep(j,k[i]) add_edge(i,n+s[i][j],1);\n }\n rep(i,M) add_edge(n+i,T,1);\n\n printf(\"%d\\n\", max_flow(S,T));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3688, "score_of_the_acc": -0.5232, "final_rank": 11 }, { "submission_id": "aoj_2064_2198699", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)\n\nconst int inf = 1e9;\n\n#define MAX_V 3000\n\nstruct edge {\n int to, cap, rev;\n edge(){}\n edge(int to, int cap, int rev):to(to), cap(cap), rev(rev){}\n};\nvector<edge> graph[MAX_V];\nint level[MAX_V], iter[MAX_V];\nvoid add_edge(int from, int to, int cap) {\n graph[from].emplace_back(to, cap, (int)graph[to].size());\n graph[to].emplace_back(from, 0, (int)graph[from].size()-1);\n}\nvoid bfs(int s) {\n memset(level, -1, sizeof(level));\n level[s] = 0;\n queue<int> que;\n que.push(s);\n while(que.size()) {\n int v = que.front(); que.pop();\n for(edge& e : graph[v]) {\n if(e.cap > 0 && level[e.to] < 0) {\n\tlevel[e.to] = level[v] + 1;\n\tque.push(e.to);\n }\n }\n }\n}\nint dfs(int v, int t, int f) {\n if(v == t) return f;\n for(int& i = iter[v]; i < (int)graph[v].size(); i++) {\n edge& e = graph[v][i];\n if(e.cap > 0 && level[v] < level[e.to]) {\n int d = dfs(e.to, t, min(e.cap, f));\n if(d > 0) {\n\te.cap -= d;\n\tgraph[e.to][e.rev].cap += d;\n\treturn d;\n }\n }\n }\n return 0;\n}\nint max_flow(int s, int t) {\n int flow = 0;\n while(1) {\n bfs(s);\n if(level[t] < 0) return flow;\n memset(iter, 0, sizeof(iter));\n int f; while((f = dfs(s, t, inf)) > 0) flow += f;\n }\n}\n\n\nint N, M;\nmap<int, int> mp;\n\nsigned main()\n{\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n while(cin >> N, N) {\n int s = 0, t = 1, V = 2;\n rep(i, MAX_V) graph[i].clear();\n mp.clear();\n cin >> M;\n rep(i, M) {\n int d; cin >> d;\n mp[d] = V++;\n }\n for(auto p : mp) add_edge(s, p.second, 1);\n rep(i, N) {\n cin >> M;\n rep(j, M) {\n\tint d; cin >> d;\n\tif(mp.count(d)) add_edge(mp[d], V+i, 1);\n }\n add_edge(V+i, t, 1);\n }\n cout << max_flow(s, t) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3268, "score_of_the_acc": -0.4743, "final_rank": 6 }, { "submission_id": "aoj_2064_2173833", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nstruct BipartiteMatching {\n\tint V;\n\tvector<vector<bool> > G;\n\tvector<int> match;\n\tvector<bool> used;\n\n\tBipartiteMatching(int v) {\n\t\tV = v;\n\t\tG = vector<vector<bool> >(v, vector<bool>(v));\n\t\tmatch = vector<int>(v);\n\t\tused = vector<bool>(v);\n\t}\n\n\tvoid add_edge(int v, int u) {\n\t\tG[v][u] = G[u][v] = true;\n\t}\n\n\tbool dfs(int v) {\n\t\tused[v] = true;\n\t\tfor(int i = 0; i < V; i++) {\n\t\t\tif(!G[v][i]) continue;\n\t\t\tint u = i, w = match[u];\n\t\t\tif(w < 0 || (!used[w] && dfs(w))) {\n\t\t\t\tmatch[v] = u;\n\t\t\t\tmatch[u] = v;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\tint calc() {\n\t\tint res = 0;\n\t\tfill(match.begin(), match.end(), -1);\n\t\tfor(int v = 0; v < V; v++) {\n\t\t\tif(match[v] < 0) {\n\t\t\t\tfill(used.begin(), used.end(), false);\n\t\t\t\tif(dfs(v)) {\n\t\t\t\t\tres++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};\n\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tint N;\n\twhile(cin >> N, N) {\n\t\tint M;\n\t\tcin >> M;\n\t\tmap<int, int> id;\n\t\tfor(int i = 0; i < M; i++) {\n\t\t\tint a;\n\t\t\tcin >> a;\n\t\t\tid[a] = -1;\n\t\t}\n\t\tint cnt = 0;\n\t\tfor(auto v : id) {\n\t\t\tid[v.first] = cnt++;\n\t\t}\n\n\t\tBipartiteMatching bm(cnt + N);\n\t\tfor(int i = 0; i < N; i++) {\n\t\t\tcin >> M;\n\t\t\tfor(int j = 0; j < M; j++) {\n\t\t\t\tint a;\n\t\t\t\tcin >> a;\n\t\t\t\tif(id.count(a) == 0) continue;\n\t\t\t\tbm.add_edge(id[a], cnt + i);\n\t\t\t}\n\t\t}\n\n\t\tcout << bm.calc() << endl;\n\t}\n\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3256, "score_of_the_acc": -1.1862, "final_rank": 18 }, { "submission_id": "aoj_2064_2040185", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\n#define watch(a) { cout << #a << \" = \" << a << endl; }\ntemplate<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n \ntypedef long long ll;\nint const inf = 1<<29;\n \nnamespace flow {\n \nclass bipartite_matching {\nprivate:\n \n int V;\n vector<vector<int>> G;\n vector<int> match;\n vector<bool> used;\n \n bool dfs(int v) {\n used[v] = 1;\n for(auto& u: G[v]) {\n int w = match[u];\n if(w < 0 || (!used[w] && dfs(w))) {\n match[v] = u;\n match[u] = v;\n return true;\n }\n }\n return false;\n }\n \n \npublic:\n \n bipartite_matching(int V): V(V) {\n G.resize(V), match.resize(V), used.resize(V);\n }\n \n void add_edge(int u, int v) {\n G[u].push_back(v);\n G[v].push_back(u);\n }\n \n int maximum_matching() {\n int ret = 0;\n match.clear(), match.resize(V, -1);\n rep(v, V) {\n if(match[v] < 0) {\n used.clear(), used.resize(V, 0);\n if(dfs(v)) { ret ++; }\n }\n }\n return ret;\n }\n};\n \n}\n \nint main() {\n \n for(int N; cin >> N && N;) {\n int M; cin >> M;\n \n int dcnt = 0;\n unordered_map<int, int> dayidx;\n rep(i, M) {\n int x; cin >> x;\n if(dayidx.find(x) == dayidx.end()) {\n dayidx[x] = dcnt++;\n }\n }\n \n flow::bipartite_matching bp(dcnt + N);\n \n rep(girl, N) {\n cin >> M;\n rep(i, M) {\n int k; cin >> k;\n if(dayidx.find(k) != dayidx.end())\n bp.add_edge(dayidx[k], dcnt + girl);\n }\n }\n \n cout << bp.maximum_matching() << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3212, "score_of_the_acc": -0.6773, "final_rank": 15 }, { "submission_id": "aoj_2064_2040159", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\n#define watch(a) { cout << #a << \" = \" << a << endl; }\ntemplate<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n\ntypedef long long ll;\nint const inf = 1<<29;\n\nnamespace flow {\n\nclass bipartite_matching {\nprivate:\n\n int V;\n vector<vector<int>> G;\n vector<int> match;\n vector<bool> used;\n\n bool dfs(int v) {\n used[v] = 1;\n for(auto& u: G[v]) {\n int w = match[u];\n if(w < 0 || (!used[w] && dfs(w))) {\n match[v] = u;\n match[u] = v;\n return true;\n }\n }\n return false;\n }\n\n\npublic:\n\n bipartite_matching(int V): V(V) {\n G.resize(V), match.resize(V), used.resize(V);\n }\n\n void add_edge(int u, int v) {\n G[u].push_back(v);\n G[v].push_back(u);\n }\n\n int maximum_matching() {\n int ret = 0;\n match.clear(), match.resize(V, -1);\n rep(v, V) {\n if(match[v] < 0) {\n used.clear(), used.resize(V, 0);\n if(dfs(v)) { ret ++; }\n }\n }\n return ret;\n }\n};\n\n}\n\nint main() {\n\n for(int N; cin >> N && N;) {\n int M; cin >> M;\n\n int dcnt = 0;\n map<int, int> dayidx;\n rep(i, M) {\n int x; cin >> x;\n if(dayidx.find(x) == dayidx.end()) {\n dayidx[x] = dcnt++;\n }\n }\n\n flow::bipartite_matching bp(dcnt + N);\n\n rep(girl, N) {\n cin >> M;\n rep(i, M) {\n int k; cin >> k;\n if(dayidx.find(k) != dayidx.end())\n bp.add_edge(dayidx[k], dcnt + girl);\n }\n }\n\n cout << bp.maximum_matching() << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3232, "score_of_the_acc": -0.7885, "final_rank": 17 }, { "submission_id": "aoj_2064_1914970", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX_V 3000\n#define INF 1e9\n \nstruct edge{ int to,cap,rev; };\n \nvector<edge> G[MAX_V];\nint level[MAX_V],iter[MAX_V];\n \nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,G[to].size()});\n G[to].push_back((edge){from,0,G[from].size()-1});\n}\n \nvoid bfs(int s){\n memset(level,-1,sizeof(level));\n queue<int> Q;\n level[s] = 0;\n Q.push(s);\n while(!Q.empty()){\n\tint v = Q.front(); Q.pop();\n\tfor(int i = 0 ; i < (int)G[v].size() ; i++){\n\t edge &e = G[v][i];\n\t if(e.cap > 0 && level[e.to] < 0){\n\t\tlevel[e.to] = level[v] + 1;\n\t\tQ.push(e.to);\n\t }\n\t}\n }\n}\n \nint dfs(int v,int t,int f){\n if(v == t) return f;\n for(int &i = iter[v] ; i < (int)G[v].size() ; i++){\n\tedge &e = G[v][i];\n\tif(e.cap > 0 && level[v] < level[e.to]){\n\t int d = dfs(e.to,t,min(f,e.cap));\n\t if(d > 0){\n\t\te.cap -= d;\n\t\tG[e.to][e.rev].cap += d;\n\t\treturn d;\n\t }\n\t}\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow = 0;\n for(;;){\n\tbfs(s);\n\tif(level[t] < 0) return flow;\n\tmemset(iter,0,sizeof(iter));\n\tint f;\n\twhile((f = dfs(s,t,INF)) > 0){\n\t flow += f;\n\t}\n }\n}\n \nint main(){\n int N;\n while(cin >> N,N){\n\tfor(int i = 0 ; i < MAX_V ; i++){\n\t G[i].clear();\n\t}\n\tint k,x,n;\n\tcin >> n;\n\tint S = N+n,T = S+1;\n\tmap<int,int> mp;\n\tfor(int i = 0 ; i < n ; i++){\n\t cin >> x;\n\t mp[x] = i;\n\t}\n\tmap<int,int>::iterator it;\n\tfor(it = mp.begin() ; it != mp.end() ; ++it){\n\t add_edge(S,it->second,1);\n\t}\n\tfor(int i = 0 ; i < N ; i++){\n\t cin >> k;\n\t for(int j = 0 ; j < k ; j++){\n\t\tcin >> x;\n\t\tif(mp.find(x) != mp.end()){\n\t\t add_edge(mp[x],n+i,1);\n\t\t}\n\t }\n\t add_edge(n+i,T,1);\n\t}\n\tcout << max_flow(S,T) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1532, "score_of_the_acc": -0.446, "final_rank": 3 }, { "submission_id": "aoj_2064_1331970", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX_V 3000\n#define INF 1e9\n\nstruct edge{ int to,cap,rev; };\n\nvector<edge> G[MAX_V];\nint level[MAX_V],iter[MAX_V];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,G[to].size()});\n G[to].push_back((edge){from,0,G[from].size()-1});\n}\n\nvoid bfs(int s){\n memset(level,-1,sizeof(level));\n queue<int> Q;\n level[s] = 0;\n Q.push(s);\n while(!Q.empty()){\n int v = Q.front(); Q.pop();\n for(int i = 0 ; i < (int)G[v].size() ; i++){\n edge &e = G[v][i];\n if(e.cap > 0 && level[e.to] < 0){\n level[e.to] = level[v] + 1;\n Q.push(e.to);\n }\n }\n }\n}\n\nint dfs(int v,int t,int f){\n if(v == t) return f;\n for(int &i = iter[v] ; i < (int)G[v].size() ; i++){\n edge &e = G[v][i];\n if(e.cap > 0 && level[v] < level[e.to]){\n int d = dfs(e.to,t,min(f,e.cap));\n if(d > 0){\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n}\n\nint max_flow(int s,int t){\n int flow = 0;\n for(;;){\n bfs(s);\n if(level[t] < 0) return flow;\n memset(iter,0,sizeof(iter));\n int f;\n while((f = dfs(s,t,INF)) > 0){\n flow += f;\n }\n }\n}\n\nint main(){\n int N;\n while(cin >> N,N){\n for(int i = 0 ; i < MAX_V ; i++){\n G[i].clear();\n }\n int k,x,n;\n cin >> n;\n int S = N+n,T = S+1;\n map<int,int> mp;\n for(int i = 0 ; i < n ; i++){\n cin >> x;\n mp[x] = i;\n }\n map<int,int>::iterator it;\n for(it = mp.begin() ; it != mp.end() ; ++it){\n add_edge(S,it->second,1);\n }\n for(int i = 0 ; i < N ; i++){\n cin >> k;\n for(int j = 0 ; j < k ; j++){\n cin >> x;\n if(mp.find(x) != mp.end()){\n add_edge(mp[x],n+i,1);\n }\n }\n add_edge(n+i,T,1);\n }\n cout << max_flow(S,T) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1532, "score_of_the_acc": -0.446, "final_rank": 3 }, { "submission_id": "aoj_2064_1150189", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\nusing namespace std;\nconst int D_MAX_V=2102;\nconst int D_v_size=2102;\nstruct D_wolf{\n\tint t,c,r;\n\tD_wolf(){t=c=r=0;}\n\tD_wolf(int t1,int c1,int r1){\n\t\tt=t1;c=c1;r=r1;\n\t}\n};\nvector<D_wolf>D_G[D_MAX_V];\nint D_level[D_MAX_V];\nint D_iter[D_MAX_V];\n\nvoid add_edge(int from,int to,int cap){\n\tD_G[from].push_back(D_wolf(to,cap,D_G[to].size()));\n\tD_G[to].push_back(D_wolf(from,0,D_G[from].size()-1));\n}\nvoid D_bfs(int s){\n\tfor(int i=0;i<D_v_size;i++)D_level[i]=-1;\n\tqueue<int> Q;\n\tD_level[s]=0;\n\tQ.push(s);\n\twhile(Q.size()){\n\t\tint v=Q.front();\n\t\tQ.pop();\n\t\tfor(int i=0;i<D_G[v].size();i++){\n\t\t\tif(D_G[v][i].c>0&&D_level[D_G[v][i].t]<0){\n\t\t\t\tD_level[D_G[v][i].t]=D_level[v]+1;\n\t\t\t\tQ.push(D_G[v][i].t);\n\t\t\t}\n\t\t}\n\t}\n}\nint D_dfs(int v,int t,int f){\n\tif(v==t)return f;\n\tfor(;D_iter[v]<D_G[v].size();D_iter[v]++){\n\t\tint i=D_iter[v];\n\t\tif(D_G[v][i].c>0&&D_level[v]<D_level[D_G[v][i].t]){\n\t\t\tint d=D_dfs(D_G[v][i].t,t,min(f,D_G[v][i].c));\n\t\t\tif(d>0){\n\t\t\t\tD_G[v][i].c-=d;\n\t\t\t\tD_G[D_G[v][i].t][D_G[v][i].r].c+=d;\n\t\t\t\treturn d;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}\nint max_flow(int s,int t){\n\tint flow=0;\n\tfor(;;){\n\t\tD_bfs(s);\n\t\tif(D_level[t]<0)return flow;\n\t\tfor(int i=0;i<D_v_size;i++)D_iter[i]=0;\n\t\tint f;\n\t\twhile((f=D_dfs(s,t,99999999))>0){flow+=f;}\n\t}\n\treturn 0;\n}\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<2102;i++){\n\t\t\tD_G[i].clear();\n\t\t\tD_iter[i]=D_level[i]=0;\n\t\t}\n\t\tint n;scanf(\"%d\",&n);\n\t\tmap<int,int>c;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tint b;scanf(\"%d\",&b);\n\t\t\tc[b]=i;\n\t\t}\n\t\tint s=2100;\n\t\tint t=2101;\n\t\tfor(int i=0;i<n;i++)add_edge(s,i,1);\n\t\tfor(int i=0;i<a;i++){\n\t\t\tint m;scanf(\"%d\",&m);\n\t\t\tfor(int j=0;j<m;j++){\n\t\t\t\tint p;scanf(\"%d\",&p);\n\t\t\t\tif(c.count(p)){\n\t\t\t\t\tadd_edge(c[p],i+n,1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<a;i++)add_edge(i+n,t,1);\n\t\tprintf(\"%d\\n\",max_flow(s,t));\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 1268, "score_of_the_acc": -0.1786, "final_rank": 1 }, { "submission_id": "aoj_2064_1124230", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <cassert>\n#include <queue>\n#include <cstring>\nusing namespace std;\nstatic const double EPS = 1e-9;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rev(i,n) for(int i=n-1;i>=0;i--)\n#define sz(a) a.size()\n#define all(a) a.begin(),a.end()\n#define mp(a,b) make_pair(a,b)\n#define pb(a) push_back(a)\n#define SS stringstream\n#define DBG1(a) rep(_X,sz(a)){printf(\"%d \",a[_X]);}puts(\"\");\n#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf(\"%d \",a[_X][_Y]);puts(\"\");}\n#define bitcount(b) __builtin_popcount(b)\n#define fi first\n#define se second\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n\ntypedef int Weight;\nstruct Edge {\n int src, dst;\n Weight weight;\n Edge(int src, int dst, Weight weight) :\n src(src), dst(dst), weight(weight) { }\n Edge(int src, int dst) :\n src(src), dst(dst), weight(0) { }\n};\nbool operator < (const Edge &e, const Edge &f) {\n return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!\n e.src != f.src ? e.src < f.src : e.dst < f.dst;\n}\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nbool augment(const Graph& g, int u,\n vector<int>& matchTo, vector<bool>& visited) {\n if (u < 0) return true;\n FOR(e, g[u]) if (!visited[e->dst]) {\n visited[e->dst] = true;\n if (augment(g, matchTo[e->dst], matchTo, visited)) {\n matchTo[e->src] = e->dst;\n matchTo[e->dst] = e->src;\n return true;\n }\n }\n return false;\n}\nint bipartiteMatching(const Graph& g, int L, Edges& matching) {\n const int n = g.size();\n vector<int> matchTo(n, -1);\n int match = 0;\n REP(u, L) {\n vector<bool> visited(n);\n if (augment(g, u, matchTo, visited)) ++match;\n }\n REP(u, L) if (matchTo[u] >= 0) // make explicit matching\n matching.push_back( Edge(u, matchTo[u]) );\n return match;\n}\n\nint main(){\n\tint N;\n\twhile(cin >> N && N){\n\t\tset<int> w;\n\t\tint a,b;\n\t\tcin >> a;\n\t\tvector<int> day;\n\t\tfor(int i = 0 ; i < a ; i++){\n\t\t\tcin >> b;\n\t\t\tb--;\n\t\t\tw.insert(b);\n\t\t\tday.push_back(b);\n\t\t}\n\t\tEdges edge;\n\t\tfor(int i = 0 ; i < N ; i++){\n\t\t\tint t;\n\t\t\tcin >> t;\n\t\t\tfor(int j = 0 ; j < t ; j++){\n\t\t\t\tint s;\n\t\t\t\tcin >> s;\n\t\t\t\ts--;\n\t\t\t\tif(w.count(s)) edge.push_back(Edge(i,s));\n\t\t\t\tday.push_back(s);\n\t\t\t}\n\t\t}\n\t\tsort(day.begin(),day.end());\n\t\tday.erase(unique(day.begin(),day.end()),day.end());\n\t\tint ub = max(N,(int)day.size());\n\t\tGraph g(2*ub);\n\t\tfor(int i = 0 ; i < edge.size() ; i++){\n\t\t\tint a = edge[i].src;\n\t\t\tint b = ub + lower_bound(day.begin(),day.end(),edge[i].dst) - day.begin();\n\t\t\tg[a].push_back(Edge(a,b));\n\t\t\tg[b].push_back(Edge(b,a));\n\t\t\t//cout << a << \" \" << b << endl;\n\t\t}\n\t\tEdges tmp;\n\t\tcout << bipartiteMatching(g,ub,tmp) << endl;\n\t\t\n\t\t\n\t}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 4820, "score_of_the_acc": -1.7156, "final_rank": 20 }, { "submission_id": "aoj_2064_799240", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<map>\n#include<cstring>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define inf (1<<29) \n#define all(n) (n).begin(),(n).end()\n#define MAX_V 1100\nusing namespace std;\n \n//follow\nstruct edge\n{\n int to,cap,rev;\n edge(int to=inf,int cap=inf,int rev=inf):to(to),cap(cap),rev(rev){}\n};\n \nvector<edge> G[MAX_V];\nint level[MAX_V];\nint iter[MAX_V];\nbool used[MAX_V];\n \nvoid init(int V)\n{\n rep(i,V)G[i].clear();\n}\n \nvoid add_edge(int from,int to,int cap)\n{\n G[from].push_back(edge(to,cap,G[to].size()));\n G[to].push_back(edge(from,0,G[from].size()-1));\n}\n \nint dfs(int v,int t,int f)\n{\n if(v == t)return f;\n used[v] = true;\n for(int i=0;i<G[v].size();i++)\n {\n edge &e = G[v][i];\n if(e.cap > 0 && !used[e.to])\n {\n int d = dfs(e.to,t,min(f,e.cap));\n if(d > 0)\n {\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t)\n{\n int flow = 0;\n for(;;)\n {\n memset(used,0,sizeof(used));\n int f = dfs(s,t,inf);\n if(f == 0)\n {\n return flow;\n }\n flow += f;\n }\n}\n \n//\n \nint main()\n{\n int N,k,K,s,t,day;\n while(cin >> N,N)\n {\n cin >> k;\n K = k;\n s = k + N;\n t = s + 1;\n init(t+1);\n map<int,int> days;\n vector<int> vday;\n rep(i,k)\n {\n cin >> day;\n vday.push_back(day);\n }\n sort(all(vday));\n vday.erase(unique(all(vday)),vday.end());\n rep(i,vday.size())\n {\n days[vday[i]] = i;\n add_edge(s,i,1);\n }\n \n rep(i,N)\n {\n cin >> k;\n rep(j,k)\n {\n cin >> day;\n if(days.find(day) != days.end())\n {\n add_edge(days[day],K+i,1);\n }\n }\n add_edge(K+i,t,1);\n }\n \n cout << max_flow(s,t) << endl;\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1536, "score_of_the_acc": -0.4826, "final_rank": 10 }, { "submission_id": "aoj_2064_798627", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cassert>\n#include<map>\n\n#define MAX_V 5000\n#define INF (1<<29)\n\nusing namespace std;\n\nstruct edge{int to,cap,rev;};\n\nvector<edge>g[MAX_V];\nbool used[MAX_V];\n\nvoid add_edge(int from,int to,int cap){\n g[from].push_back((edge){to,cap,g[to].size()});\n g[to].push_back((edge){from,0,g[from].size()-1});\n}\n\nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<g[v].size();i++){\n edge &e=g[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n g[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n\nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n fill(used,used+MAX_V,false);\n int f=dfs(s,t,INF);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nint main(void){\n\n int n;\n while(cin >> n,n){\n\n for(int i=0;i<MAX_V;i++)g[i].clear();\n\n vector<int>a;\n map<int,int>mp;\n int m,in,tmp;\n cin >> tmp;\n for(int i=0;i<tmp;i++){\n cin >> in;\n add_edge(n+tmp,i,1);\n a.push_back(in);\n mp[in]=i;\n }\n\n for(int i=0;i<n;i++){\n cin >> m;\n vector<int>b(m);\n for(int j=0;j<m;j++)cin >> b[j];\n for(int j=0;j<m;j++){\n if(mp.count(b[j])){\n add_edge(mp[b[j]],i+tmp,1);\n }\n }\n }\n\n for(int i=0;i<n;i++){\n add_edge(i+tmp,n+tmp+1,1);\n }\n cout << max_flow(n+tmp,n+tmp+1) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1532, "score_of_the_acc": -0.4818, "final_rank": 8 }, { "submission_id": "aoj_2064_798621", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cassert>\n#include<map>\n\n#define MAX_V 5000\n#define INF (1<<29)\n\nusing namespace std;\n\nstruct edge{int to,cap,rev;};\n\nvector<edge>g[MAX_V];\nbool used[MAX_V];\n\nvoid add_edge(int from,int to,int cap){\n g[from].push_back((edge){to,cap,g[to].size()});\n g[to].push_back((edge){from,0,g[from].size()-1});\n}\n\nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<g[v].size();i++){\n edge &e=g[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n g[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n\nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n fill(used,used+MAX_V,false);\n int f=dfs(s,t,INF);\n if(f==0)return flow;\n flow+=f;\n }\n}\nint main(void){\n\n int n;\n while(cin >> n,n){\n\n for(int i=0;i<MAX_V;i++)g[i].clear();\n\n vector<int>a;\n map<int,int>mp;\n int m,in,tmp;\n cin >> tmp;\n for(int i=0;i<tmp;i++){\n cin >> in;\n add_edge(n+tmp,i,1);\n a.push_back(in);\n mp[in]=i;\n }\n\n for(int i=0;i<n;i++){\n cin >> m;\n vector<int>b(m);\n for(int j=0;j<m;j++)cin >> b[j];\n for(int j=0;j<m;j++){\n if(mp.count(b[j])){\n add_edge(mp[b[j]],i+tmp,1);\n }\n }\n }\n\n for(int i=0;i<n;i++){\n add_edge(i+tmp,n+tmp+1,1);\n }\n cout << max_flow(n+tmp,n+tmp+1) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1532, "score_of_the_acc": -0.4818, "final_rank": 8 }, { "submission_id": "aoj_2064_798518", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<map>\n#include<cstring>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define inf (1<<29) \n#define all(n) (n).begin(),(n).end()\n#define MAX_V 1100\nusing namespace std;\n\n//follow\nstruct edge\n{\n int to,cap,rev;\n edge(int to=inf,int cap=inf,int rev=inf):to(to),cap(cap),rev(rev){}\n};\n \nvector<edge> G[MAX_V];\nint level[MAX_V];\nint iter[MAX_V];\nbool used[MAX_V];\n \nvoid init(int V)\n{\n rep(i,V)G[i].clear();\n}\n \nvoid add_edge(int from,int to,int cap)\n{\n G[from].push_back(edge(to,cap,G[to].size()));\n G[to].push_back(edge(from,0,G[from].size()-1));\n}\n\nint dfs(int v,int t,int f)\n{\n if(v == t)return f;\n used[v] = true;\n for(int i=0;i<G[v].size();i++)\n {\n edge &e = G[v][i];\n if(e.cap > 0 && !used[e.to])\n\t{\n\t int d = dfs(e.to,t,min(f,e.cap));\n\t if(d > 0)\n\t {\n\t e.cap -= d;\n\t G[e.to][e.rev].cap += d;\n\t return d;\n\t }\n\t}\n }\n return 0;\n}\n \nint max_flow(int s,int t)\n{\n int flow = 0;\n for(;;)\n {\n memset(used,0,sizeof(used));\n int f = dfs(s,t,inf);\n if(f == 0)\n\t{\n\t return flow;\n\t}\n flow += f;\n }\n}\n \n//\n\nint main()\n{\n int N,k,K,s,t,day;\n while(cin >> N,N)\n {\n cin >> k;\n K = k;\n s = k + N;\n t = s + 1;\n init(t+1);\n map<int,int> days;\n vector<int> vday;\n rep(i,k)\n\t{\n\t cin >> day;\n\t vday.push_back(day);\n\t}\n sort(all(vday));\n vday.erase(unique(all(vday)),vday.end());\n rep(i,vday.size())\n\t{\n\t days[vday[i]] = i;\n\t add_edge(s,i,1);\n\t}\n\n rep(i,N)\n\t{\n\t cin >> k;\n\t rep(j,k)\n\t {\n\t cin >> day;\n\t if(days.find(day) != days.end())\n\t\t{\n\t\t add_edge(days[day],K+i,1);\n\t\t}\n\t }\n\t add_edge(K+i,t,1);\n\t}\n\n cout << max_flow(s,t) << endl;\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1536, "score_of_the_acc": -0.4468, "final_rank": 5 } ]
aoj_2067_cpp
Reading Brackets in English Shun and his professor are studying Lisp and S-expressions. Shun is in Tokyo in order to make a presen- tation of their research. His professor cannot go with him because of another work today. He was making final checks for his slides an hour ago. Then, unfortunately, he found some serious mistakes! He called his professor immediately since he did not have enough data in his hand to fix the mistakes. Their discussion is still going on now. The discussion looks proceeding with difficulty. Most of their data are written in S-expressions, so they have to exchange S-expressions via telephone. Your task is to write a program that outputs S-expressions represented by a given English phrase. Input The first line of the input contains a single positive integer N , which represents the number of test cases. Then N test cases follow. Each case consists of a line. The line contains an English phrase that represents an S-expression. The length of the phrase is up to 1000 characters. The rules to translate an S-expression are as follows. An S-expression which has one element is translated into “a list of < the element of the S-expression >”. (e.g. “(A)” will be “a list of A”) An S-expression which has two elements is translated into “a list of < the first element of the S-expression > and < the second element of the S-expression >”. (e.g. “(A B)” will be “a list of A and B”) An S-expression which has more than three elements is translated into “a list of < the first element of the S-expression >, < the second element of the S-expression >, . . . and < the last element of the S-expression >”. (e.g. “(A B C D)” will be “a list of A, B, C and D”) The above rules are applied recursively. (e.g. “(A (P Q) B (X Y Z) C)” will be “a list of A, a list of P and Q, B, a list of X, Y and Z and C”) Each atomic element of an S-expression is a string made of less than 10 capital letters. All words (“a”, “list”, “of” and “and”) and atomic elements of S-expressions are separated by a single space character, but no space character is inserted before comma (”,”). No phrases for empty S-expressions occur in the input. You can assume that all test cases can be translated into S-expressions, but the possible expressions may not be unique. Output For each test case, output the corresponding S-expression in a separate line. If the given phrase involves two or more different S-expressions, output “AMBIGUOUS” (without quotes). A single space character should be inserted between the elements of the S-expression, while no space character should be inserted after open brackets (“(”) and before closing brackets (“)”). Sample Input 4 a list of A, B, C and D a list of A, a list of P and Q, B, a list of X, Y and Z and C a list of A a list of a list of A and B Output for the Sample Input (A B C D) (A (P Q) B (X Y Z) C) (A) AMBIGUOUS
[ { "submission_id": "aoj_2067_10313005", "code_snippet": "// AOJ #2067 Reading Brackets in English\n// 2025.3.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Ans {\n bool valid, amb;\n string s;\n};\n\nAns mergeAns(const Ans &a, const Ans &b) {\n if(!a.valid) return b;\n if(!b.valid) return a;\n Ans ret;\n ret.valid = true;\n if(a.amb || b.amb || a.s != b.s) ret.amb = true;\n else {\n ret.amb = false;\n ret.s = a.s;\n }\n return ret;\n}\n\ntypedef unordered_map<int, Ans> MapAns;\n\nvoid unionMap(MapAns &dest, const MapAns &src) {\n for(auto &pr : src) {\n int pos = pr.first;\n if(dest.find(pos) == dest.end()) dest[pos] = pr.second;\n else dest[pos] = mergeAns(dest[pos], pr.second);\n }\n}\n\nvector<string> T;\n\nunordered_map<int, MapAns> memoS, memoE, memoL, memoList;\n\nMapAns parseS(int);\nMapAns parseE(int);\nMapAns parseL(int);\nMapAns parseList(int);\n\nMapAns parseS(int i) {\n if(memoS.count(i)) return memoS[i];\n MapAns res;\n if(i+3 <= (int)T.size() && T[i]==\"a\" && T[i+1]==\"list\" && T[i+2]==\"of\") {\n MapAns mL = parseL(i+3);\n for(auto &pr : mL) {\n int pos = pr.first;\n Ans an;\n an.valid = pr.second.valid;\n an.amb = pr.second.amb;\n an.s = \"(\" + pr.second.s + \")\";\n res[pos] = an;\n }\n }\n memoS[i] = res;\n return res;\n}\n\nMapAns parseE(int i) {\n if(memoE.count(i)) return memoE[i];\n MapAns res;\n MapAns mS = parseS(i);\n unionMap(res, mS);\n if(i+1 <= (int)T.size()) {\n string tok = T[i];\n bool isAtom = true;\n for(char c : tok) {\n if(!isupper(c)) { isAtom = false; break; }\n }\n if(isAtom) {\n Ans an;\n an.valid = true;\n an.amb = false;\n an.s = tok;\n res[i+1] = an;\n }\n }\n memoE[i] = res;\n return res;\n}\n\nMapAns parseList(int i) {\n if(memoList.count(i)) return memoList[i];\n MapAns res;\n MapAns mE = parseE(i);\n for(auto &pr : mE) {\n int pos = pr.first;\n res[pos] = mergeAns(res[pos], pr.second);\n if(pos < (int)T.size() && T[pos] == \",\") {\n MapAns mList = parseList(pos+1);\n for(auto &pr2 : mList) {\n int pos2 = pr2.first;\n Ans an;\n if(pr.second.valid && pr2.second.valid) {\n an.valid = true;\n string cand = pr.second.s + \" \" + pr2.second.s;\n an.amb = pr.second.amb || pr2.second.amb;\n an.s = cand;\n } else an.valid = false;\n res[pos2] = mergeAns(res[pos2], an);\n }\n }\n }\n memoList[i] = res;\n return res;\n}\n\nMapAns parseL(int i) {\n if(memoL.count(i)) return memoL[i];\n MapAns res;\n MapAns mE = parseE(i);\n unionMap(res, mE);\n MapAns mList = parseList(i);\n for(auto &pr : mList) {\n int pos = pr.first;\n if(pos < (int)T.size() && T[pos]==\"and\") {\n MapAns mE2 = parseE(pos+1);\n for(auto &pr2 : mE2) {\n int pos2 = pr2.first;\n Ans an;\n if(pr.second.valid && pr2.second.valid) {\n an.valid = true;\n string cand = pr.second.s + \" \" + pr2.second.s;\n an.s = cand;\n an.amb = pr.second.amb || pr2.second.amb;\n } else an.valid = false;\n res[pos2] = mergeAns(res[pos2], an);\n }\n }\n }\n memoL[i] = res;\n return res;\n}\n\nvector<string> tokenize(const string &s) {\n vector<string> ret;\n istringstream iss(s);\n string token;\n while(iss >> token) {\n while(!token.empty() && token.back()==','){\n token.pop_back();\n ret.push_back(token);\n ret.push_back(\",\");\n token = \"\";\n }\n if(!token.empty()) ret.push_back(token);\n }\n return ret;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int N; cin >> N;\n cin.ignore();\n for(int tc=0; tc<N; tc++){\n string line;\n getline(cin, line);\n T = tokenize(line);\n memoS.clear(); memoE.clear(); memoL.clear(); memoList.clear();\n MapAns mS = parseS(0);\n Ans finalAns; finalAns.valid = false;\n for(auto &pr : mS) {\n if(pr.first == (int)T.size() && pr.second.valid) {\n finalAns = mergeAns(finalAns, pr.second);\n }\n }\n if(finalAns.valid && !finalAns.amb) cout << finalAns.s << endl;\n else cout << \"AMBIGUOUS\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 19940, "score_of_the_acc": -0.4811, "final_rank": 4 }, { "submission_id": "aoj_2067_3037167", "code_snippet": "#include <cstdio>\n#include <cctype>\n#include <cassert>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <functional>\n\nvoid preprocess(const std::string &s, std::vector<std::string> &t) {\n t.clear();\n for (size_t i=0; i<s.length(); ++i) {\n if (s[i] == 'a') {\n if (s[i+1] == ' ') {\n // \"a list of \"\n t.push_back(\"(\");\n i += 9;\n } else if (s[i+1] == 'n') {\n // \"and \"\n t.push_back(\"&\");\n i += 3;\n } else {\n assert(false);\n }\n continue;\n }\n\n std::string word(1, s[i]);\n while (++i < s.length() && isupper(s[i]))\n word += s[i];\n\n t.push_back(word);\n if (i < s.length() && s[i] == ',') {\n ++i;\n }\n }\n}\n\nvoid debug(const std::vector<std::string> &s) {\n fprintf(stderr, \"> \");\n for (size_t i=0; i<s.size(); ++i)\n fprintf(stderr, \"%s%c\", s[i].c_str(), i+1<s.size()? ' ':'\\n');\n}\n\nstd::string cyk(const std::vector<std::string> &s) {\n /*\n S-Expr -> \"(\" Element\n | \"(\" Elements\n Element -> Upper\n | S-Expr\n Elements -> Element \"and\" Element\n | Element Elements\n ---\n\n S-Expr -> \"(\" Elements\n | \"(\" Upper\n | \"(\" S-Expr\n Elements -> Upper Elements\n | Upper AndElement\n | S-Expr Elements\n | S-Expr AndElement\n AndElement -> \"and\" Upper\n | \"and\" S-Expr\n */ \n enum Symbol {\n INVALID,\n // non-terminals\n S_EXPR, ELEMS, AND_ELEM,\n // terminals\n PAREN=9, AND, UPPER,\n };\n std::vector<std::string> types={\n \"0\", \"S-expr\", \"Elems\", \"AndElem\",\n \"0\", \"0\", \"0\", \"0\", \"0\", \"\\\"(\\\"\", \"\\\"and\\\"\", \"[A-Z]+\"\n };\n \n auto category=[](const std::string &token) {\n if (token == \"(\") return PAREN;\n if (token == \"&\") return AND;\n if (isupper(token[0])) return UPPER;\n return INVALID;\n };\n\n std::vector<Symbol> term={PAREN, AND, UPPER};\n std::vector<std::vector<Symbol>> prod={\n {S_EXPR, PAREN, ELEMS},\n {S_EXPR, PAREN, UPPER},\n {S_EXPR, PAREN, S_EXPR},\n {ELEMS, UPPER, ELEMS},\n {ELEMS, UPPER, AND_ELEM},\n {ELEMS, S_EXPR, ELEMS},\n {ELEMS, S_EXPR, AND_ELEM},\n {AND_ELEM, AND, UPPER},\n {AND_ELEM, AND, S_EXPR},\n };\n\n std::vector<std::vector<std::vector<int>>> dp(\n s.size(), std::vector<std::vector<int>>(\n s.size(), std::vector<int>(prod.size()+term.size())));\n\n for (size_t i=0; i<s.size(); ++i) {\n Symbol cat=category(s[i]);\n if (cat != INVALID) {\n dp[0][i][cat] = 1;\n }\n }\n\n struct Left {\n size_t length, pos;\n Symbol symb;\n Left(size_t i, size_t j, Symbol a): length(i), pos(j), symb(a) {}\n bool operator <(const Left &oth) const {\n if (length != oth.length) return length < oth.length;\n if (pos != oth.pos) return pos < oth.pos;\n return symb < oth.symb;\n }\n };\n\n struct Right {\n size_t part;\n Symbol symb1, symb2;\n Right(size_t k, Symbol b, Symbol c): part(k), symb1(b), symb2(c) {}\n };\n\n std::map<Left, Right> via;\n size_t n=s.size();\n for (size_t i=1; i<n; ++i) {\n for (size_t j=0; j<n-i; ++j) {\n for (size_t k=1; k<=i; ++k) {\n for (const auto &pp: prod) {\n Symbol a=pp[0], b=pp[1], c=pp[2];\n if (dp[k-1][j][b] && dp[i-k][j+k][c]) {\n dp[i][j][a] += std::max(dp[k-1][j][b], dp[i-k][j+k][c]);\n if (dp[i][j][a] > 2) dp[i][j][a] = 2;\n via.emplace(Left(i, j, a), Right(k, b, c));\n }\n }\n }\n }\n }\n if (dp[n-1][0][1] > 1) return \"AMBIGUOUS\";\n\n assert(dp[n-1][0][1]);\n std::function<std::string (Left)> dfs=[&](const Left &left) {\n if (left.length == 0) return s[left.pos];\n Right right=via.at(left);\n size_t i=left.length, j=left.pos, k=right.part;\n Symbol a=left.symb, b=right.symb1, c=right.symb2;\n\n std::string sb=dfs(Left(k-1, j, b)), sc=dfs(Left(i-k, j+k, c));\n\n if (a == S_EXPR) {\n return sb + sc + \")\";\n } else if (a == ELEMS) {\n return sb + \" \" + sc;\n } else if (a == AND_ELEM) {\n assert(sb == \"&\");\n return sc;\n } else {\n assert(false);\n }\n };\n return dfs(Left(n-1, 0, S_EXPR));\n}\n\nvoid solve_testcase() {\n char buf[1024];\n scanf(\"\\n%[^\\n]\", buf);\n std::string s=buf;\n std::vector<std::string> t;\n preprocess(s, t);\n printf(\"%s\\n\", cyk(t).c_str());\n}\n\nint main() {\n int N;\n scanf(\"%d\", &N);\n for (int i=0; i<N; ++i)\n solve_testcase();\n}", "accuracy": 1, "time_ms": 6450, "memory_kb": 12072, "score_of_the_acc": -1.2589, "final_rank": 6 }, { "submission_id": "aoj_2067_2131263", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define pb push_back\n#define vi vector<int>\n#define vvi vector<vi>\n#define va vector<array<int,6>>\n#define vva vector<va>\n\nvector<string> toVs(string s){\n\tvector<string> ret;\n\tstring buf = \"\";\n\t\n\trep(i,s.size()){\n\t\tif(i+9<=s.size()&&s.substr(i,9)==\"a list of\"){\n\t\t\tbuf+=\"<\";\n\t\t\ti+=8;\n\t\t}\n\t\telse if(i+3<=s.size()&&s.substr(i,3)==\"and\"){\n\t\t\tbuf+=\"&\";\n\t\t\ti+=2;\n\t\t}\n\t\telse if(isalpha(s[i]))buf+=s[i];\n\t\telse if(s[i]==' '){\n\t\t\tret.pb(buf);\n\t\t\tbuf = \"\";\n\t\t}\n\t}\n\tif(buf.size())ret.pb(buf);\n\treturn ret;\n}\n\n\nvector<char> toWordClass(vector<string> vs){\n\tvector<char> ret;\n\trep(i,vs.size()){\n\t\tstring s = vs[i];\n\t\tif(s==\"<\")ret.pb('L');\n\t\telse if(s==\"&\")ret.pb('A');\n\t\telse ret.pb('T');\n\t}\n\treturn ret;\n}\n\n\ndeque<string> dfs(int a,int b,int c,vector<vva> &bef,vector<string> &vs){\n\tif(a==b){\n\t\tif(vs[a]==\"<\"||vs[a]==\"&\")return deque<string>();\n\t\tdeque<string> ret;\n\t\tret.push_back(vs[a]);\n\t\treturn ret;\n\t}\n\t\n\tarray<int,3> l,r;\n\trep(i,3)l[i] = bef[a][b][c][i];\n\trep(i,3)r[i] = bef[a][b][c][i+3];\n\t\n\tdeque<string> ret,y;\n\tret = dfs(l[0],l[1],l[2],bef,vs);\n\ty = dfs(r[0],r[1],r[2],bef,vs);\n\t\n\tfor(auto &e:y){\n\t\tret.push_back(e);\n\t}\n\t\n\tif(c==0){\n\t\tret.push_front(\"(\");\n\t\tret.push_back(\")\");\n\t}\n\t\n\treturn ret;\n}\n\n\nint main(){\n\tvector<pii> rule[3] = {\n\t\t{pii(4,3),pii(4,0),pii(4,1)},\t\t\t//S\n\t\t{pii(3,2),pii(0,2),pii(3,1),pii(0,1)},\t//P\n\t\t{pii(5,3),pii(5,0)}\t\t\t\t\t\t//Q\n\t};\n\t\n\tint n;\n\tcin>>n;\n\tcin.ignore();\n\twhile(n--){\n\t\tstring s;\n\t\tgetline(cin,s);\n\t\tvector<string> vs = toVs(s);\n\t\tint N = vs.size();\n\t\tvector<char> wc = toWordClass(vs);\n\t\t\n\t\tvector<vvi> dp(N,vvi(N,vi(6,0)));\n\t\tvector<vva> bef(N,vva(N,va(6,array<int,6>{-1,-1,-1,-1,-1,-1})));\n\t\t\n\t\trep(i,N){\n\t\t\tif(wc[i]=='T')dp[i][i][3]=1;\n\t\t\tif(wc[i]=='L')dp[i][i][4]=1;\n\t\t\tif(wc[i]=='A')dp[i][i][5]=1;\n\t\t}\n\t\t\n\t\tfor(int wid = 2;wid<=N;wid++){\n\t\t\tfor(int l = 0;l<N;l++){\n\t\t\t\tint r = l+wid-1;\n\t\t\t\tif(r>=N)continue;\n\t\t\t\trep(i,3){\n\t\t\t\t\tfor(int p = l;p<=r-1;p++){\n\t\t\t\t\t\trep(j,rule[i].size()){\n\t\t\t\t\t\t\tint x,y;\n\t\t\t\t\t\t\ttie(x,y) = rule[i][j];\n\t\t\t\t\t\t\tif(dp[l][p][x]!=0 && dp[p+1][r][y]!=0){\n\t\t\t\t\t\t\t\tdp[l][r][i]+=dp[l][p][x]*dp[p+1][r][y];\n\t\t\t\t\t\t\t\tif(dp[l][r][i]>2)dp[l][r][i] = 2;\n\t\t\t\t\t\t\t\tbef[l][r][i] = array<int,6>{l,p,x,p+1,r,y};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tif(dp[0][N-1][0]>=2){\n\t\t\tcout<<\"AMBIGUOUS\"<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tassert(dp[0][N-1][0]!=0);\n\t\t\n\t\t\n\t\tdeque<string> ans = dfs(0,N-1,0,bef,vs);\n\t\t\n\t\tvector<string> a;\n\t\tfor(auto &e:ans)a.pb(e);\n\t\t\n\t\trep(i,a.size()){\n\t\t\tif(a[i]==\"(\"||(i+1<a.size()&&a[i+1]==\")\")||i==a.size()-1)cout<<a[i];\n\t\t\telse cout<<a[i]<<\" \";\n\t\t}\n\t\tcout<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 5110, "memory_kb": 29164, "score_of_the_acc": -1.5118, "final_rank": 7 }, { "submission_id": "aoj_2067_2093243", "code_snippet": "#include <vector>\n#include <iostream>\n#include <utility>\n#include <algorithm>\n#include <string>\n#include <deque>\n#include <tuple>\n#include <queue>\n#include <functional>\n#include <cmath>\n#include <iomanip>\n#include <map>\n#include <numeric>\n#include <unordered_map>\n#include <unordered_set>\n#include <complex>\n#include <iterator>\n#include <array>\n#include <chrono>\n//cin.sync_with_stdio(false);\n//streambuf\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef pair<double, double> pdd;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vpii = vector<pii>;\ntemplate<class T, int s>using va = vector<array<T, s>>;\ntemplate<class T, class T2> using umap = unordered_map<T, T2>;\ntemplate<class T> using uset = unordered_set<T>;\ntemplate<class T, class S> void cmin(T &a, const S&&b) { if (a > b)a = b; }\ntemplate<class T, class S> void cmax(T &a, const S&&b) { if (a < b)a = b; }\n#define ALL(a) a.begin(),a.end()\n#define rep(i,a) for(int i=0;i<a;i++)\n#define rep1(i,a) for(int i=1;i<=a;i++)\n#define rrep(i,a) for(int i=a-1;i>=0;i--)\n#define rrep1(i,a) for(int i=a;i;i--)\nconst ll mod = 1000000007;\n#ifndef INT_MAX\nconst int INT_MAX = numeric_limits<signed>().max();\n#endif\ntemplate<class T>using heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T>using pque = priority_queue<T, vector<T>, function<T(T, T)>>;\ntemplate <class T>\ninline void hash_combine(size_t & seed, const T & v) {\n\thash<T> hasher;\n\tseed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n}\nnamespace std {\n\n\ttemplate<typename S, typename T> struct hash<pair<S, T>> {\n\t\tinline size_t operator()(const pair<S, T> & v) const {\n\t\t\tsize_t seed = 0;\n\t\t\thash_combine(seed, v.first);\n\t\t\thash_combine(seed, v.second);\n\t\t\treturn seed;\n\t\t}\n\t};\n\t// Recursive template code derived from Matthieu M.\n\ttemplate <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>\n\tstruct HashValueImpl\n\t{\n\t\tstatic void apply(size_t& seed, Tuple const& tuple)\n\t\t{\n\t\t\tHashValueImpl<Tuple, Index - 1>::apply(seed, tuple);\n\t\t\thash_combine(seed, std::get<Index>(tuple));\n\t\t}\n\t};\n\n\ttemplate <class Tuple>\n\tstruct HashValueImpl<Tuple, 0>\n\t{\n\t\tstatic void apply(size_t& seed, Tuple const& tuple)\n\t\t{\n\t\t\thash_combine(seed, std::get<0>(tuple));\n\t\t}\n\t};\n\n\ttemplate <typename ... TT>\n\tstruct hash<std::tuple<TT...>>\n\t{\n\t\tsize_t\n\t\t\toperator()(std::tuple<TT...> const& tt) const\n\t\t{\n\t\t\tsize_t seed = 0;\n\t\t\tHashValueImpl<std::tuple<TT...> >::apply(seed, tt);\n\t\t\treturn seed;\n\t\t}\n\n\t};\n}\nll pow(ll base, ll i, ll mod) {\n\tll a = 1;\n\twhile (i) {\n\t\tif (i & 1) {\n\t\t\ta *= base;\n\t\t\ta %= mod;\n\t\t}\n\t\tbase *= base;\n\t\tbase %= mod;\n\t\ti /= 2;\n\t}\n\treturn a;\n}\nclass unionfind {\n\tvector<int> par, rank, size_;//????????§??????????????¢???????????????????????????rank???????????????size?????????\npublic:\n\tunionfind(int n) :par(n), rank(n), size_(n, 1) {\n\t\tiota(ALL(par), 0);\n\t}\n\tint find(int x) {\n\t\tif (par[x] == x)return x;\n\t\treturn par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y) {\n\t\tx = find(x), y = find(y);\n\t\tif (x == y)return;\n\t\tif (rank[x] < rank[y])swap(x, y);\n\t\tpar[y] = x;\n\t\tsize_[x] += size_[y];\n\t\tif (rank[x] == rank[y])rank[x]++;\n\t}\n\tbool same(int x, int y) {\n\t\treturn find(x) == find(y);\n\t}\n\tint size(int x) {\n\t\treturn size_[find(x)];\n\t}\n};\nll gcd(ll a, ll b) {\n\twhile (b) {\n\t\tll c = a%b;\n\t\ta = b;\n\t\tb = c;\n\t}\n\treturn a;\n}\nll lcm(ll a, ll b) {\n\treturn a / gcd(a, b)*b;\n}\nint popcnt(unsigned long long a) {\n\ta = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555);\n\ta = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333);\n\ta = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f);\n\ta = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff);\n\ta = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff);\n\treturn (a & 0xffffffff) + (a >> 32);\n}\ntemplate<class T, class Func = function<T(T, T)>>\nclass segtree {\n\tvector<T> obj;\n\tint offset;\n\tFunc updater;\n\tT e;\n\tint bufsize(int num) {\n\t\tint i = 1;\n\t\tfor (; num >i; i <<= 1);\n\t\toffset = i - 1;\n\t\treturn (i << 1) - 1;\n\t}\n\tT query(int a, int b, int k, int l, int r) {\n\t\tif (r <= a || b <= l)return e;\n\t\tif (a <= l && r <= b)return obj[k];\n\t\telse return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r));\n\t}\npublic:\n\tT query(int a, int b) {//[a,b)\n\t\treturn query(a, b, 0, 0, offset + 1);\n\t}\n\tvoid updateall(int l = 0, int r = -1) {\n\t\tif (r < 0)r = offset + 1;\n\t\tl += offset, r += offset;\n\t\tdo {\n\t\t\tl = l - 1 >> 1, r = r - 1 >> 1;\n\t\t\tfor (int i = l; i < r; i++)obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]);\n\t\t} while (l);\n\t}\n\tvoid update(int k, T &a) {\n\t\tk += offset;\n\t\tobj[k] = a;\n\t\twhile (k) {\n\t\t\tk = k - 1 >> 1;\n\t\t\tobj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]);\n\t\t}\n\t}\n\tsegtree(int n, T e, const Func &updater_ = Func()) :obj(bufsize(n), e), e(e), updater(updater_) {}\n\tsegtree(vector<T> &vec, T e, const Func &updater = Func()) :obj(bufsize(vec.size()), e), e(e), updater(updater) {\n\t\tcopy(vec.begin(), vec.end(), obj.begin() + offset);\n\t\tupdateall();\n\t}\n\ttypename vector<T>::reference operator[](int n) {\n\t\treturn obj[n + offset];\n\t}\n};\ntemplate<class T>\nclass matrix {\n\tvector<vector<T>> obj;\n\tpair<int, int> s;\npublic:\n\tmatrix(pair<int, int> size, T e = 0) :matrix(size.first, size.second, e) {}\n\tmatrix(int n, int m = -1, T e = 0) :obj(n, vector<T>(m == -1 ? n : m, e)), s(n, m == -1 ? n : m) {}\n\tstatic matrix e(int n) {\n\t\tmatrix a = (n);\n\t\tfor (int i = 0; i < n; i++)a[i][i] = 1;\n\t\treturn a;\n\t}\n\tmatrix& operator+=(const matrix &p) {\n\t\tif (s != p.s)throw runtime_error(\"matrix error\");\n\t\tfor (int i = 0; i < s.first; i++)\n\t\t\tfor (int j = 0; j < s.second; j++)obj[i][j] += p.obj[i][j];\n\t\treturn *this;\n\t}\n\tmatrix operator+(const matrix &p) {\n\t\tmatrix res(*this);\n\t\treturn res += p;\n\t}\n\tmatrix& operator-=(const matrix &p) {\n\t\tif (s != p.s)throw runtime_error(\"matrix error\");\n\t\tfor (int i = 0; i < s.first; i++)\n\t\t\tfor (int j = 0; j < s.second; j++)obj[i][j] -= p.obj[i][j];\n\t\treturn *this;\n\t}\n\tmatrix operator-(const matrix &p) {\n\t\tmatrix res(*this);\n\t\treturn res -= p;\n\t}\n\tmatrix& operator*=(T p) {\n\t\tfor (auto &a : obj)\n\t\t\tfor (auto &b : a)b *= p;\n\t\treturn *this;\n\t}\n\tmatrix operator*(T p) {\n\t\tmatrix res(*this);\n\t\treturn res *= p;\n\t}\n\tmatrix operator*(const matrix &p) {\n\t\tif (s.second != p.s.first)throw runtime_error(\"matrix error\");\n\t\tmatrix ret(s.first, p.s.second);\n\t\tfor (int i = 0; i < s.first; i++)\n\t\t\tfor (int j = 0; j < s.second; j++)\n\t\t\t\tfor (int k = 0; k < p.s.second; k++)ret[i][k] += obj[i][j] * p.obj[j][k];\n\t\treturn ret;\n\t}\n\tmatrix &operator*=(const matrix &p) {\n\t\treturn *this = *this*p;\n\t}\n\tpair<int, int> size() const {\n\t\treturn s;\n\t}\n\tmatrix &mod(T m) {\n\t\tfor (auto &a : obj)\n\t\t\tfor (auto &b : a)b %= m;\n\t\treturn *this;\n\t}\n\ttypename vector<vector<T>>::reference operator[](int t) {\n\t\treturn obj[t];\n\t}\n};\ntemplate<class T> inline\nmatrix<T> pow(matrix<T> &base, unsigned exp) {\n\tauto base_(base);\n\tif (base_.size().first != base_.size().second)throw runtime_error(\"matrix error\");\n\tmatrix<T> res = matrix<T>::e(base_.size().first);\n\tfor (;;) {\n\t\tif (exp & 1)res *= base_;\n\t\tif (!(exp /= 2))break;\n\t\tbase_ *= base_;\n\t}\n\treturn res;\n}\ntemplate<class T> inline\nmatrix<T> modpow(matrix<T> &base, unsigned exp, T m) {\n\tauto base_(base);\n\tif (base.size().first != base_.size().second)throw runtime_error(\"matrix error\");\n\tmatrix<T> res = matrix<T>::e(base_.size().first);\n\tfor (;;) {\n\t\tif (exp & 1)(res *= base_).mod(m);\n\t\tif (!(exp /= 2))break;\n\t\t(base_ *= base_).mod(m);\n\t}\n\treturn res;\n}\ntemplate<class T>int id(vector<T> &a, T b) {\n\treturn lower_bound(ALL(a), b) - a.begin();\n}\nclass Flow {\n\tint V;\n\tstruct edge { int to, cap, rev, cost; };\n\tvector<vector<edge>> G;\n\tvector<int> level, iter, h, dist, prevv, preve;\n\tFlow(int size) :G(size + 1), V(size + 1) {\n\t}\n\tvoid add_edge(int from, int to, int cap, int cost = 0) {\n\t\tG[from].push_back(edge{ to, cap, (int)G[to].size(),cost });\n\t\tG[to].push_back(edge{ from,0,(int)G[from].size() - 1,-cost });\n\t}\n\tvoid bfs(int s) {\n\t\tfill(level.begin(), level.end(), -1);\n\t\tqueue<int> que;\n\t\tlevel[s] = 0;\n\t\tque.push(s);\n\t\twhile (!que.empty()) {\n\t\t\tint v = que.front(); que.pop();\n\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\tedge &e = G[v][i];\n\t\t\t\tif (e.cap > 0 && level[e.to] < 0) {\n\t\t\t\t\tlevel[e.to] = level[v] + 1;\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint dfs(int v, int t, int f) {\n\t\tif (v == t)return f;\n\t\tfor (int &i = iter[v]; i < G[v].size(); i++) {\n\t\t\tedge &e = G[v][i];\n\t\t\tif (e.cap > 0 && level[v] < level[e.to]) {\n\t\t\t\tint d = dfs(e.to, t, min(f, e.cap));\n\t\t\t\tif (d > 0) {\n\t\t\t\t\te.cap -= d;\n\t\t\t\t\tG[e.to][e.rev].cap += d;\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\tint max_flow(int s, int t) {\n\t\tlevel.resize(V);\n\t\titer.resize(V);\n\t\tint flow = 0;\n\t\tfor (;;) {\n\t\t\tbfs(s);\n\t\t\tif (level[t] < 0)return flow;\n\t\t\tfill(iter.begin(), iter.end(), 0);\n\t\t\tint f;\n\t\t\twhile ((f = dfs(s, t, numeric_limits<int>::max()))>0) {\n\t\t\t\tflow += f;\n\t\t\t}\n\t\t}\n\t}\n\ttypedef pair<int, int> P;\n\tint min_cost_flow(int s, int t, int f) {\n\t\tint res = 0;\n\t\th.resize(V);\n\t\tdist.resize(V);\n\t\tprevv.resize(V);\n\t\tpreve.resize(V);\n\t\tfill(h.begin(), h.end(), 0);\n\t\twhile (f > 0) {\n\t\t\tpriority_queue<P, vector<P>, greater<P>> que;\n\t\t\tfill(dist.begin(), dist.end(), numeric_limits<int>::max());\n\t\t\tdist[s] = 0;\n\t\t\tque.push({ 0,s });\n\t\t\twhile (!que.empty()) {\n\t\t\t\tP p = que.top(); que.pop();\n\t\t\t\tint v = p.second;\n\t\t\t\tif (dist[v] < p.first)continue;\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge &e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to]>dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tque.push({ dist[e.to],e.to });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (dist[t] == numeric_limits<int>::max()) {\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tfor (int v = 0; v < V; v++)h[v] += dist[v];\n\n\t\t\tint d = f;\n\t\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t\t}\n\t\t\tf -= d;\n\t\t\tres += d*h[t];\n\t\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\t\tedge &e = G[prevv[v]][preve[v]];\n\t\t\t\te.cap -= d;\n\t\t\t\tG[v][e.rev].cap += d;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};\nconst ld eps = 1e-11, pi = acos(-1.0);\ntypedef complex<ld> P;\ntypedef vector<P> VP;\nld dot(P a, P b) { return real(conj(a) * b); }\nld cross(P a, P b) { return imag(conj(a) * b); }\n\nnamespace std {\n\tbool operator<(const P &a, const P &b) {\n\t\treturn abs(a.real() - b.real()) < eps ? a.imag() < b.imag() : a.real() < b.real();\n\t}\n}\n\nstruct L { P a, b; };//line->l,segment->s\nstruct C { P p; ld r; };\n\nint ccw(P a, P b, P c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps) return 1; // counter clockwise\n\tif (cross(b, c) < -eps) return -1; // clockwise\n\tif (dot(b, c) < 0) return 2; // c--a--b on line\n\tif (norm(b) < norm(c)) return -2; // a--b--c on line\n\treturn 0; // a--c--b on line\n}\n\nbool isis_ll(L l, L m) {//is intersect\n\treturn abs(cross(l.b - l.a, m.b - m.a)) > eps;\n}\n\nbool isis_ls(L l, L s) {\n\tld a = cross(l.b - l.a, s.a - l.a);\n\tld b = cross(l.b - l.a, s.b - l.a);\n\treturn (a * b < eps);\n}\n\nbool isis_lp(L l, P p) {\n\treturn abs(cross(l.b - p, l.a - p)) < eps;\n}\n\nbool isis_ss(L s, L t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nP is_ll(L s, L t) { //intersect\n\tP sv = s.b - s.a, tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nbool isis_sp(L s, P p) {\n\treturn abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;\n}\n\nP proj(L l, P p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\nld dist_lp(L l, P p) {\n\treturn abs(p - proj(l, p));\n}\n\nld dist_ll(L l, L m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\nld dist_ls(L l, L s) {\n\tif (isis_ls(l, s)) return 0;\n\treturn min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\nld dist_sp(L s, P p) {\n\tP r = proj(s, p);\n\tif (isis_sp(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\nld dist_ss(L s, L t) {\n\tif (isis_ss(s, t)) return 0;\n\tld a = min(dist_sp(s, t.a), dist_sp(t, s.a));\n\tld b = min(dist_sp(s, t.b), dist_sp(t, s.b));\n\treturn min(a, b);\n}\n\nVP is_cc(C c1, C c2) {\n\tVP res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tP diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * P(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs));\n\treturn res;\n}\n\nbool isis_vc(vector<C> vc) {\n\tVP crs;\n\tint n = vc.size();\n\trep(i, n)rep(j, i)\n\t\tfor (P p : is_cc(vc[i], vc[j]))\n\t\t\tcrs.push_back(p);\n\trep(i, n)\n\t\tcrs.push_back(vc[i].p);\n\tfor (P p : crs) {\n\t\tbool valid = true;\n\t\trep(i, n)\n\t\t\tif (abs(p - vc[i].p)>vc[i].r + eps)\n\t\t\t\tvalid = false;\n\t\tif (valid) return true;\n\t}\n\treturn false;\n}\n\nVP is_lc(C c, L l) {\n\tVP res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tP nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\nVP is_sc(C c, L l) {\n\tVP v = is_lc(c, l), res;\n\tfor (P p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\nvector<L> tangent_cp(C c, P p) {//????????\\????\n\tvector<L> ret;\n\tP v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tP v1 = v * P(l / d, c.r / d);\n\tP v2 = v * P(l / d, -c.r / d);\n\tret.push_back(L{ p, p + v1 });\n\tif (l < eps) return ret;\n\tret.push_back(L{ p, p + v2 });\n\treturn ret;\n}\n\nvector<L> tangent_cc(C c1, C c2) {\n\tvector<L> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tP center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tP out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<L> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tP v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tP q1 = c1.p + v * P(0, 1) * c1.r;\n\t\tP q2 = c1.p + v * P(0, -1) * c1.r;\n\t\tret.push_back(L{ q1, q1 + v });\n\t\tret.push_back(L{ q2, q2 + v });\n\t}\n\treturn ret;\n}\n\nld area(const VP &p) {//??¢?????\n\tld res = 0;\n\tint n = p.size();\n\trep(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\nbool is_polygon(L l, VP &g) {\n\tint n = g.size();\n\tfor (int i = 0; i < n; i++) {\n\t\tP a = g[i];\n\t\tP b = g[(i + 1) % n];\n\t\tif (isis_ss(l, L{ a, b })) return true;\n\t}\n\treturn false;\n}\n\nint is_in_Polygon(const VP &g, P p) {\n\tbool in = false;\n\tint n = g.size();\n\tfor (int i = 0; i < n; i++) {\n\t\tP a = g[i] - p, b = g[(i + 1) % n] - p;\n\t\tif (imag(a) > imag(b)) swap(a, b);\n\t\tif (imag(a) <= 0 && 0 < imag(b))\n\t\t\tif (cross(a, b) < 0) in = !in;\n\t\tif (abs(cross(a, b)) < eps && dot(a, b) < eps) return 0; // on\n\t}\n\tif (in) return 1; // in\n\treturn -1; // out\n}\n\nVP ConvexHull(VP ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tVP ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\nVP ConvexCut(const VP &ps, L l) {\n\tVP Q;\n\tfor (int i = 0; i < (int)ps.size(); i++) {\n\t\tP A = ps[i], B = ps[(i + 1) % ps.size()];\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0)\n\t\t\tQ.push_back(is_ll(L{ A, B }, l));\n\t}\n\treturn Q;\n}\ndouble dist2(P a) {\n\treturn real(a)*real(a) + imag(a)*imag(a);\n}\n// Suffix Array\t?????????O(|S|log^2|S|), ????´¢O(|T|log|S|), ?????????????§????O(|S|)\nclass StringSearch {\n\tconst int n;\n\tstring S;\npublic:\n\tvector<int> sa, rank;\n\tStringSearch(const string &S_) :n(S_.size()), S(S_), sa(n + 1), rank(n + 1) {\n\t\tfor (int i = 0; i <= n; i++) {\n\t\t\tsa[i] = i;\n\t\t\trank[i] = i < n ? S[i] : -1;\n\t\t}\n\t\tvector<int> tmp(n + 1);\n\t\tfor (int k = 1; k <= n; k *= 2) {\n\t\t\tauto Compare_SA = [=](int i, int j) {\n\t\t\t\tif (this->rank[i] != this->rank[j]) return this->rank[i] < this->rank[j];\n\n\t\t\t\tint ri = i + k <= n ? this->rank[i + k] : -1;\n\t\t\t\tint rj = j + k <= n ? this->rank[j + k] : -1;\n\t\t\t\treturn ri < rj;\n\t\t\t};\n\t\t\tsort(sa.begin(), sa.end(), Compare_SA);\n\t\t\ttmp[sa[0]] = 0;\n\t\t\tfor (int i = 1; i <= n; i++) {\n\t\t\t\ttmp[sa[i]] = tmp[sa[i - 1]] + (Compare_SA(sa[i - 1], sa[i]) ? 1 : 0);\n\t\t\t}\n\t\t\tfor (int i = 0; i <= n; i++) {\n\t\t\t\tthis->rank[i] = tmp[i];\n\t\t\t}\n\t\t}\n\t}\n\tbool Contain(const string &T) {\n\t\tint a = 0, b = n;\n\t\twhile (b - a > 1) {\n\t\t\tint c = (a + b) / 2;\n\t\t\tif (S.compare(sa[c], T.length(), T) < 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t\treturn S.compare(sa[b], T.length(), T) == 0;\n\t}\n\tvector<int> LCPArray() {\n\t\tfor (int i = 0; i <= n; i++) rank[sa[i]] = i;\n\t\tint h = 0;\n\t\tvector<int> lcp(n + 1);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint j = sa[rank[i] - 1];\n\t\t\tif (h > 0) h--;\n\t\t\tfor (; j + h < n && i + h < n; h++) {\n\t\t\t\tif (S[j + h] != S[i + h]) break;\n\t\t\t}\n\t\t\tlcp[rank[i] - 1] = h;\n\t\t}\n\t\treturn lcp;\n\t}\n};\n//0l \n//1A\n//141\n//101\n//2a\n//301\n//331\n//432\nint dp[340][340][5];\nvector<string> t;\nstring f4(int i, int j);\nstring f1(int i, int j) {\n\tif (i == j)return t[i];\n\trep(k, j - i) {\n\t\tif (dp[i][k][4] && dp[i + k + 1][j - k - 1 - i][1])return f4(i, i + k) + f1(i + k + 1, j) + ')';\n\t\tif (dp[i][k][0] && dp[i + k + 1][j - k - 1 - i][1])return '(' + f1(i + k + 1, j) + ')';\n\t}\n\treturn \"\";\n}\nstring f3(int i, int j) {\n\trep(k, j - i) {\n\t\tif (dp[i][k][0] && dp[i + k + 1][j - k - 1-i][1])return '(' + f1(i + k + 1, j);\n\t\tif (dp[i][k][3] && dp[i + k + 1][j - k - 1-i][1])return f3(i, i + k) + \" \" + f1(i + k + 1, j);\n\t}\n\treturn \"\";\n}\nstring f4(int i, int j) {\n\trep(k, j - i) {\n\t\tif (dp[i][k][3] && dp[i + k + 1][j - k - 1-i][2])return f3(i, i + k) + \" \";\n\t}\n}\nint main() {\n\tint n;\n\tcin >> n;\n\tstring tmp;\n\tgetline(cin, tmp);\n\twhile (n--) {\n\t\tstring s;\n\t\tgetline(cin, s);\n\t\tt.clear();\n\t\trep(i, s.size()) {\n\t\t\tif (s[i] >= 'A'&&s[i] <= 'Z') {\n\t\t\t\ttmp = \"\";\n\t\t\t\twhile (s[i] >= 'A'&&s[i] <= 'Z')tmp += s[i++];\n\t\t\t\tt.push_back(tmp);\n\t\t\t}\n\t\t\tif (s[i] == 'l'&&s[i + 1] == 'i')t.push_back(\"l\");\n\t\t\tif (s[i] == 'a'&&s[i + 1] == 'n')t.push_back(\"a\");\n\t\t}\n\t\tfill(dp[0][0], dp[340][0], 0);\n\t\trep(i, t.size())dp[i][0][(t[i] == \"l\" ? 0 : t[i] == \"a\" ? 2 : 1)] = 1;\n\t\trep1(i, t.size() - 1) {\n\t\t\trep(j, t.size() - i) {\n\t\t\t\trep(k, i) {\n\t\t\t\t\tdp[j][i][1] = min(dp[j][i][1] + dp[j][k][4] * dp[j + k + 1][i - k - 1][1] + dp[j][k][0] * dp[j + k + 1][i - k - 1][1], 20000);\n\t\t\t\t\tdp[j][i][4] = min(dp[j][i][4] + dp[j][k][3] * dp[j + k + 1][i - k - 1][2], 20000);\n\t\t\t\t\tdp[j][i][3] = min(dp[j][i][3] + dp[j][k][0] * dp[j + k + 1][i - k - 1][1] + dp[j][k][3] * dp[j+k+1][i - k - 1][1], 20000);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp[0][t.size() - 1][1] == 1) {\n\t\t\tcout << f1(0, t.size() - 1) << endl;\n\t\t}\n\t\telse cout << \"AMBIGUOUS\" << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1990, "memory_kb": 5344, "score_of_the_acc": -0.3563, "final_rank": 3 }, { "submission_id": "aoj_2067_1464103", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\ntypedef pair<int,int> ii;\n\nconst string FAIL = \"AMBIGUOUS\";\nint dp[1000][1000][6]; // dp[][][0] -> the number of L, dp[][][1] -> A, dp[][][2] -> T, dp[][][3] -> S, dp[][][4] -> P, dp[][][5] -> Q\nint path[1000][1000][6]; // 2??\\????????´???????????????????????????????????????????????????????????????AMBIGUOUS????????????????????????\nint topv[1000][1000][6];\nint rigv[1000][1000][6];\n\n/*\n ????§??¨???? S\n L ::= \"list\"\n A ::= \"and\"\n T ::= \"A\" | \"B\" | ... | \"Z\"\n S ::= L E | L P\n E ::= T | S\n P ::= E A E | E P\n \n -> Chomsky Normal Form\n L ::= \"list\"\n A ::= \"and\"\n T ::= \"A\" | \"B\" | ... | \"Z\" <- ??¬??\\??§????????°?????\\???????????????????????????????????????????????????????????\\??????????????????????????§?????????????????????\n S ::= L P | L T | L S\n P ::= T P | S P | T Q | S Q\n Q ::= A T | A S \n \n */\n\nvector<string> alphas;\nvector<char> lexer(string context){\n vector<char> vec;\n rep(i,(int)context.size()) if( context[i] == ',' ) context[i] = ' ';\n stringstream ss;\n ss << context;\n string token;\n while( !( ss >> token ).fail() ){\n if( token == \"a\" || token[0] == 'o' ) continue;\n alphas.push_back(\"$\");\n if( token[0] == 'l' ) vec.push_back('L'); // List\n else if( token[0] == 'a' ) vec.push_back('A'); // And\n else {\n vec.push_back('T'); // Terminal\n alphas.back() = token;\n }\n }\n return vec;\n}\n\ninline int getType(char c) { \n return ( c == 'L' ) ? 0 : ( ( c == 'A' ) ? 1 : ( ( 'A' <= c && c <= 'Z' ) ? 2 : ( ( c == 'S' ) ? 3 : ( ( c == 'P' ) ? 4 : 5 ) ) ) );\n}\n\ninline int reduce(int a,int b) {\n if( a == 0 && b == 4 ) return 3; // S ::= L P\n if( a == 0 && b == 2 ) return 3; // S ::= L T\n if( a == 0 && b == 3 ) return 3; // S ::= L S\n if( a == 2 && b == 4 ) return 4; // P ::= T P\n if( a == 3 && b == 4 ) return 4; // P ::= S P\n if( a == 2 && b == 5 ) return 4; // P ::= T Q\n if( a == 3 && b == 5 ) return 4; // P ::= S Q\n if( a == 1 && b == 2 ) return 5; // Q ::= A T\n if( a == 1 && b == 3 ) return 5; // Q ::= A S\n return -1;\n}\n\nvoid dfs(int L,int R,int type,vector<ii> &buf){\n if( L == R ) return;\n if( type == 3 ) buf.push_back(ii(L,R));\n dfs(L,path[L][R][type],topv[L][R][type],buf);\n dfs(path[L][R][type]+1,R,rigv[L][R][type],buf);\n}\n\nconst int LIMIT = 100;\nvoid compute(string context){\n alphas.clear();\n vector<char> tokens = lexer(context);\n int n = tokens.size();\n rep(i,n) rep(j,n) rep(k,6) dp[i][j][k] = 0;\n rep(i,n) dp[i][i][getType(tokens[i])] = 1;\n for(int l=1;l<n;l++){\n for(int i=0;i+l<n;i++){\n int j = i + l;\n for(int k=i;k<j;k++){\n\t//dp[i][j] = dp[i][k] + dp[k+1][j]\n\trep(tpv,6) if( dp[i][k][tpv] ) {\n\t rep(riv,6) if( dp[k+1][j][riv] ) {\n\t int response = reduce(tpv,riv);\n\t if( response == -1 ) continue; // no such rule\n\t dp[i][j][response] += dp[i][k][tpv] * dp[k+1][j][riv];\n\t dp[i][j][response] = min(dp[i][j][response],LIMIT);\n\t path[i][j][response] = k;\n\t topv[i][j][response] = tpv;\n\t rigv[i][j][response] = riv;\n\t }\n\t}\n }\n }\n }\n \n assert( dp[0][n-1][3] >= 0LL );\n if( dp[0][n-1][3] > 1LL ) puts(FAIL.c_str());\n else {\n vector<ii> buf;\n dfs(0,n-1,3,buf);\n string answer = \"\";\n vector<int> pR(n,0),pL(n,0);\n rep(i,(int)buf.size()) ++pR[buf[i].second], ++pL[buf[i].first];\n rep(i,(int)alphas.size()) {\n answer += string(pL[i],'(');\n if( alphas[i] != \"$\" ) answer += \"!\"+alphas[i]+\"?\";\n answer += string(pR[i],')');\n }\n string new_answer = \"\";\n int p = 0;\n rep(i,(int)answer.size()) {\n if( answer[i] == '(' ) ++p;\n if( answer[i] == ')' ) --p;\n new_answer += answer[i];\n if( answer[i] == ')' || answer[i] == '?' ) {\n\tif( i+1 < (int)answer.size() && answer[i+1] == ')' ) continue;\n\tnew_answer += \" \";\n }\n }\n string new_answer3 = \"\";\n rep(i,(int)new_answer.size()-1) {\n if( new_answer[i] == '!' || new_answer[i] == '?' ) continue;\n if( i+1 < (int)new_answer.size() && new_answer[i] == '(' && new_answer[i+1] == ' ' ) continue;\n if( i+1 < (int)new_answer.size() && new_answer[i] == ' ' && new_answer[i+1] == ')' ) continue;\n new_answer3 += new_answer[i];\n }\n cout << new_answer3 << endl;\n }\n}\n\nint main(){\n /*\n int n = 5;\n for(int l=1;l<n;l++){\n for(int i=0;i+l<n;i++){\n int j = i + l;\n for(int k=i;k<j;k++){\n\t//dp[i][j] = dp[i][k] + dp[k+1][j]\n\t cout << \"[\" << i << \",\" << j << \"] = [\" << i << \"][\" << k << \"][\" << k+1 << \"][\" << j << \"]\" << endl;\n }\n }\n }\n cout << \"final => [\" << 0 << \",\" << n-1 << \"]\" << endl;\n return 0;\n */\n int T;\n cin >> T;\n cin.ignore();\n while( T-- ) {\n string context;\n getline(cin,context);\n compute(context);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3840, "memory_kb": 39100, "score_of_the_acc": -1.5797, "final_rank": 8 }, { "submission_id": "aoj_2067_605267", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <deque>\n#include <string>\n#include <algorithm>\n#include <cctype>\n#include <cstring>\nusing namespace std;\n\nint tbl[6][6] = {\n\t{-1, 1, 1, -1, -1, -1},\n\t{-1, -1, -1, -1, -1, -1},\n\t{-1, -1, -1, -1, -1, -1},\n\t{0, 0, -1, -1, -1, 0},\n\t{2, -1, -1, -1, -1, 2},\n\t{-1, 1, 1, -1, -1, -1},\n};\n\nint cnt[500][500][6];\nint dat[500][500][6];\nint lft[500][500][6];\nint rgt[500][500][6];\nvector<string> name;\n\n\nvoid tostr(int i, int j, int t, bool space, string &res){\n\tif(cnt[i][j][t] != 1){ throw 0; }\n\n\tint k = dat[i][j][t];\n\tint x = lft[i][j][t];\n\tint y = rgt[i][j][t];\n\n\tif(t == 0){\n\t\ttostr(i, k, x, false, res);\n\t\tif(space){ res += ' '; }\n\t\tres += '(';\n\t\ttostr(k, j, y, false, res);\n\t\tres += ')';\n\t}\n\telse if(t == 1 || t == 2){\n\t\ttostr(i, k, x, space, res);\n\t\ttostr(k, j, y, true, res);\n\t}\n\telse if(t == 5){\n\t\tif(space){ res += ' '; }\n\t\tres += name[k];\n\t}\n}\n\n\nstring solve(const string &s){\n\tmemset(cnt, 0, sizeof cnt);\n\tname.clear();\n\n\tint n = 0;\n\tchar buf[32] = {};\n\tfor(int i = 0; i < s.size(); ++i){\n\t\tif(s[i] == 'l'){\n\t\t\tcnt[n][n+1][3] = 1;\n\t\t\t++n;\n\t\t}\n\t\telse if(s[i] == 'n'){\n\t\t\tcnt[n][n+1][4] = 1;\n\t\t\t++n;\n\t\t}\n\t\telse if(isupper(s[i])){\n\t\t\tsscanf(s.c_str() + i, \"%[A-Z]\", buf);\n\t\t\tcnt[n][n+1][5] = 1;\n\t\t\tdat[n][n+1][5] = name.size();\n\t\t\tname.push_back(buf);\n\t\t\ti += name.back().size() - 1;\n\t\t\t++n;\n\t\t}\n\t}\n\n\tfor(int len = 2; len <= n; ++len){\n\t\tfor(int i = 0; i + len <= n; ++i){\n\t\t\tint j = i + len;\n\t\t\tfor(int k = i; k < i + len; ++k){\n\t\t\t\tfor(int x = 0; x < 6; ++x){\n\t\t\t\t\tif(cnt[i][k][x] == 0){ continue; }\n\t\t\t\t\tfor(int y = 0; y < 6; ++y){\n\t\t\t\t\t\tint t = tbl[x][y];\n\t\t\t\t\t\tif(cnt[k][j][y] == 0 || t == -1){ continue; }\n\t\t\t\t\t\tif(++cnt[i][j][t] == 1){\n\t\t\t\t\t\t\tdat[i][j][t] = k;\n\t\t\t\t\t\t\tlft[i][j][t] = x;\n\t\t\t\t\t\t\trgt[i][j][t] = y;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(cnt[0][n][0] == 1){\n\t\ttry{\n\t\t\tstring ans;\n\t\t\ttostr(0, n, 0, false, ans);\n\t\t\treturn ans;\n\t\t}catch(...){}\n\t}\n\n\treturn \"AMBIGUOUS\";\n}\n\n\nint main(){\n\tname.reserve(500);\n\n\tint n;\n\tcin >> n;\n\tcin.get();\n\tstring s;\n\tfor(int i = 0; i < n; ++i){\n\t\tgetline(cin, s);\n\t\tcout << solve(s) << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 3320, "memory_kb": 24416, "score_of_the_acc": -1.0934, "final_rank": 5 }, { "submission_id": "aoj_2067_604448", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nvoid replace(string& s, const string& from, const string& to){\n string::size_type pos = s.find(from);\n while(pos != string::npos){\n s.replace(pos, from.size(), to);\n pos = s.find(from, pos + to.size());\n }\n}\nstruct Result{\n string value;\n int p;\n Result(string s, int p) : value(s), p(p){}\n};\nmap<int, vector<Result> > memo[1000];\nbool used[1000];\nmap<int, vector<Result> > expr(const string& s, int p){\n int P = p; \n assert(P >= 0 && P < 1000);\n if(used[P]) return memo[P];\n assert(s[p] == '[');\n p++;\n queue<Result> que;\n map<int, vector<Result> >& res = memo[P];\n vector<int> cnt(s.size() + 1);\n if(isupper(s[p])){\n string word;\n while(isupper(s[p])) word += s[p++];\n Result r(\"(\" + word, p);\n que.push(r);\n res[r.p].push_back(Result(r.value + \")\", r.p));\n }else{\n map<int, vector<Result> > rvm = expr(s, p);\n FORIT(it_, rvm)FORIT(it, it_->second){\n que.push(Result(\"(\" + it->value, it->p));\n res[it->p].push_back(Result(\"(\" + it->value + \")\", it->p));\n }\n }\n while(!que.empty()){\n Result r = que.front(); que.pop();\n if(s[r.p] == ','){\n if(s[r.p + 1] == '['){\n map<int, vector<Result> > rvm = expr(s, r.p + 1);\n FORIT(it_, rvm) FORIT(it, it_->second) if(cnt[it->p] < 2){\n cnt[it->p]++;\n que.push(Result(r.value + \" \" + it->value, it->p));\n }\n }else if(isupper(s[r.p + 1])){\n string word;\n while(isupper(s[r.p + 1])) word += s[++r.p];\n if(cnt[r.p + 1] < 2){\n cnt[r.p + 1]++;\n que.push(Result(r.value + \" \" + word, r.p + 1));\n }\n }else{\n assert(false);\n }\n }else if(s[r.p] == '&'){\n if(s[r.p + 1] == '['){\n map<int, vector<Result> > rvm = expr(s, r.p + 1);\n FORIT(it_, rvm) FORIT(it, (it_->second)) if(res[it->p].size() < 2){\n res[it->p].push_back(Result(r.value + \" \" + it->value + \")\", it->p));\n }\n }else if(isupper(s[r.p + 1])){\n string word;\n while(isupper(s[r.p + 1])) word += s[++r.p];\n if(res[r.p + 1].size() < 2){\n res[r.p + 1].push_back(Result(r.value + \" \" + word + \")\", r.p + 1));\n }\n }else{\n assert(false);\n }\n }else {\n assert(r.p == (int)s.size());\n }\n }\n used[P] = true;\n return res;\n}\nint main(){\n int N;\n cin >> N;\n cin.ignore();\n while(N--){\n string s;\n REP(i, 1000) memo[i].clear();\n REP(i, 1000) used[i] = false;\n getline(cin, s);\n replace(s, \"a list of\", \"[\");\n replace(s, \"and\", \"&\");\n replace(s, \" \", \"\");\n map<int, vector<Result> > rvm = expr(s, 0);\n if(rvm[s.size()].size() == 1){\n cout << rvm[s.size()][0].value << endl;\n }else if(rvm[s.size()].size() > 1){\n cout << \"AMBIGUOUS\" << endl;\n }else{\n cout << \"NULL\"<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 2628, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2067_604446", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nvoid replace(string& s, const string& from, const string& to){\n string::size_type pos = s.find(from);\n while(pos != string::npos){\n s.replace(pos, from.size(), to);\n pos = s.find(from, pos + to.size());\n }\n}\nstruct Result{\n string value;\n int p;\n Result(string s, int p) : value(s), p(p){}\n};\nmap<int, vector<Result> > memo[1000];\nbool used[1000];\nmap<int, vector<Result> > expr(const string& s, int p){\n int P = p; \n assert(P >= 0 && P < 1000);\n if(used[P]) return memo[P];\n //printf(\"expr %d %s\\n\", p, (s.substr(0, p) + \"*\" + s.substr(p + 1)).c_str());\n assert(s[p] == '[');\n p++;\n queue<Result> que;\n map<int, vector<Result> >& res = memo[P];\n vector<int> cnt(s.size() + 1);\n if(isupper(s[p])){\n string word;\n while(isupper(s[p])) word += s[p++];\n Result r(\"(\" + word, p);\n que.push(r);\n //printf(\"\\t(expr %d) push_back R(%s, %d)\\n\", P, (r.value + \")\").c_str(), r.p);\n res[r.p].push_back(Result(r.value + \")\", r.p));\n }else{\n //printf(\"\\t(expr %d) call1 expr %d\\n\", P, p);\n map<int, vector<Result> > rvm = expr(s, p);\n FORIT(it_, rvm)FORIT(it, it_->second){\n que.push(Result(\"(\" + it->value, it->p));\n //printf(\"\\t(expr %d) push_back R(%s, %d)\\n\", P, (\"(\" + it->value + \")\").c_str(), it->p);\n res[it->p].push_back(Result(\"(\" + it->value + \")\", it->p));\n }\n }\n while(!que.empty()){\n Result r = que.front(); que.pop();\n //printf(\"\\tque R(%s, %d)\\n\", r.value.c_str(), r.p);\n if(s[r.p] == ','){\n if(s[r.p + 1] == '['){\n //printf(\"\\t(expr %d) call2 expr %d\\n\", P, r.p + 1);\n map<int, vector<Result> > rvm = expr(s, r.p + 1);\n FORIT(it_, rvm) FORIT(it, it_->second) if(cnt[it->p] < 2){\n cnt[it->p]++;\n que.push(Result(r.value + \" \" + it->value, it->p));\n //res[it->p].push_back(Result(r.value + \" \" + it->value + \")\", it->p));\n }\n }else if(isupper(s[r.p + 1])){\n string word;\n while(isupper(s[r.p + 1])) word += s[++r.p];\n if(cnt[r.p + 1] < 2){\n cnt[r.p + 1]++;\n que.push(Result(r.value + \" \" + word, r.p + 1));\n //res[r.p + 1].push_back(Result(r.value + \" \" + word + \")\", r.p + 1));\n }\n }else{\n assert(false);\n }\n }else if(s[r.p] == '&'){\n if(s[r.p + 1] == '['){\n //printf(\"\\tcall3 expr %d\\n\", r.p + 1);\n map<int, vector<Result> > rvm = expr(s, r.p + 1);\n FORIT(it_, rvm) FORIT(it, (it_->second)) if(res[it->p].size() < 2){\n res[it->p].push_back(Result(r.value + \" \" + it->value + \")\", it->p));\n //printf(\"\\t(expr %d) push_back R(%s, %d)\\n\", P, (\"(\" + it->value + \")\").c_str(), it->p);\n }\n }else if(isupper(s[r.p + 1])){\n string word;\n while(isupper(s[r.p + 1])) word += s[++r.p];\n if(res[r.p + 1].size() < 2){\n res[r.p + 1].push_back(Result(r.value + \" \" + word + \")\", r.p + 1));\n //printf(\"\\t(expr %d) push_back R(%s, %d)\\n\", P, (r.value + \" \" + word + \")\").c_str(), r.p + 1);\n }\n }else{\n assert(false);\n }\n }else {\n assert(r.p == (int)s.size());\n }\n }\n used[P] = true;\n //printf(\"(expr %d) return\\n\", P);\n return res;\n}\nint main(){\n int N;\n cin >> N;\n cin.ignore();\n while(N--){\n string s;\n REP(i, 1000) memo[i].clear();\n REP(i, 1000) used[i] = false;\n getline(cin, s);\n //cout << \"#\" << s << endl;\n replace(s, \"a list of\", \"[\");\n replace(s, \"and\", \"&\");\n replace(s, \" \", \"\");\n map<int, vector<Result> > rvm = expr(s, 0);\n if(rvm[s.size()].size() == 1){\n cout << rvm[s.size()][0].value << endl;\n }else if(rvm[s.size()].size() > 1){\n cout << \"AMBIGUOUS\" << endl;\n }else{\n cout << \"NULL\"<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 2628, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2073_cpp
The Phantom Mr. Hoge is in trouble. He just bought a new mansion, but it’s haunted by a phantom. He asked a famous conjurer Dr. Huga to get rid of the phantom. Dr. Huga went to see the mansion, and found that the phantom is scared by its own mirror images. Dr. Huga set two flat mirrors in order to get rid of the phantom. As you may know, you can make many mirror images even with only two mirrors. You are to count up the number of the images as his assistant. Given the coordinates of the mirrors and the phantom, show the number of images of the phantom which can be seen through the mirrors. Input The input consists of multiple test cases. Each test cases starts with a line which contains two positive integers, p x and p y (1 ≤ p x , p y ≤ 50), which are the x - and y -coordinate of the phantom. In the next two lines, each line contains four positive integers u x , u y , v x and v y (1 ≤ u x , u y , v x , v y ≤ 50), which are the coordinates of the mirror. Each mirror can be seen as a line segment, and its thickness is negligible. No mirror touches or crosses with another mirror. Also, you can assume that no images will appear within the distance of 10 -5 from the endpoints of the mirrors. The input ends with a line which contains two zeros. Output For each case, your program should output one line to the standard output, which contains the number of images recognized by the phantom. If the number is equal to or greater than 100, print “TOO MANY” instead. Sample Input 4 4 3 3 5 3 3 5 5 6 4 4 3 3 5 3 3 5 5 5 0 0 Output for the Sample Input 4 TOO MANY
[ { "submission_id": "aoj_2073_3224441", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\n#include <utility>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\n\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L& l, const P& p) {\n return p + 2.0*(projection(l, p) -p);\n}\nL reflection(const L& l, const L& m){\n return L(reflection(l, m[0]), reflection(l, m[1]));\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nconst int maxrep = 1300;\n\nint main(){\n while(1){\n int sx,sy;\n cin >> sx >> sy;\n if(sx == 0) break;\n\n P s(sx, sy);\n vector<L> mirror(2);\n for(int d=0; d<2; d++){\n int xs,ys,xt,yt;\n cin >> xs >> ys >> xt >> yt;\n mirror[d] = L(P(xs, ys), P(xt, yt));\n }\n\n //反射後の鏡・幽霊の位置\n vector<vector<L> > w(2, vector<L>(maxrep));\n vector<vector<P> > p(2, vector<P>(maxrep));\n for(int d=0; d<2; d++){\n w[d][0] = mirror[1-d];\n w[d][1] = mirror[d];\n p[d][0] = s;\n p[d][1] = reflection(w[d][1], p[d][0]);\n for(int i=2; i<maxrep; i++){\n w[d][i] = reflection(w[d][i-1], w[d][i-2]);\n p[d][i] = reflection(w[d][i], p[d][i-1]);\n }\n }\n \n int ans = 0;\n for(int d=0; d<2; d++){\n for(int i=1; i<maxrep && ans<100; i++){\n L ray(p[d][0], p[d][i]);\n //コーナーケース:別の鏡に遮られる\n if(!isParallel(ray, w[d][0]) && intersectSS(ray, w[d][0])){\n L ray2(crosspointLL(ray, w[d][0]), ray[1]);\n if(intersectSS(ray2, w[d][1])) continue;\n }\n //順番に通過していればよい\n bool ok = true;\n for(int j=1; j<=i; j++){\n if(isParallel(ray, w[d][j]) || !intersectSS(ray, w[d][j])){\n ok = false;\n break;\n }\n ray[0] = crosspointLL(ray, w[d][j]);\n }\n if(ok){\n ans++;\n }\n }\n }\n if(ans >= 100){\n cout << \"TOO MANY\" << endl;\n }else{\n cout << ans << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3280, "score_of_the_acc": -1, "final_rank": 1 }, { "submission_id": "aoj_2073_1469066", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\n\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\n\nbool intersectLP(Line l,Point p) { return abs(cross(l.p2-p, l.p1-p)) < EPS; }\n\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\n\nbool intersectSP(Line s, Point p) { return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; }\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\n\nPoint reflection(Line l,Point p) { return p + (projection(l, p) - p) * 2; }\n\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //???????????°??????????????????\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n// --------\n\ninline bool LT(double a,double b) { return !equals(a,b) && a < b; }\ninline bool LTE(double a,double b) { return equals(a,b) || a < b; }\n\nconst double PI2 = M_PI * 2.0;\nstruct AngleRange {\n Point o; // ?????????, ???????????????????§???? ????????¨???????????????????????????,????????????\n double mini,maxi; // mini <= maxi | 0 <= mini,maxi <= 720\n // ?????°?????????????????§abs(maxi-mini)???180??????\n\n AngleRange( Point p, Segment seg ) {\n o = p;\n seg.p1 = seg.p1 - o, seg.p2 = seg.p2 - o;\n complex<double> p1(seg.p1.x,seg.p1.y);\n complex<double> p2(seg.p2.x,seg.p2.y);\n double tmp = arg( p1 / p2 ); // ???????????? p1 ??¨ p2 ??????????§? arg( p1 / p2 )\n mini = LT(0.0,tmp) ? arg(p2) : arg(p1);\n if( LT(mini,0) ) mini += PI2;\n maxi = abs(tmp) + mini;\n }\n\n bool fail(){ return equals(mini,maxi); }\n\n // mini < a < maxi\n bool contains(double a) const { \n return ( LT(mini,a-PI2) && LT(a-PI2,maxi) ) || ( LT(mini,a) && LT(a,maxi) ) || ( LT(mini,a+PI2) && LT(a+PI2,maxi) );\n }\n\n bool contains(Point p) const {\n p = p - o;\n complex<double> cp(p.x,p.y);\n return contains(arg(cp));\n }\n\n void update( AngleRange ar ) {\n if( contains(ar.mini) ) {\n if( contains(ar.maxi) ) mini = ar.mini, maxi = ar.maxi;\n else mini = ar.mini;\n } else { \n if( contains(ar.maxi) ) maxi = ar.maxi;\n else if( !ar.contains(mini) && !equals(mini,ar.mini) ) maxi = mini;\n }\n if( LT(maxi-mini,0) ) maxi += PI2;\n if( LTE(PI2,maxi-mini) ) maxi -= PI2;\n }\n\n};\n\nostream& operator << (ostream& os,const AngleRange& a){ os << \"[ \" << a.mini*180.0/M_PI << \" , \" << a.maxi*180.0/M_PI << \" ]\"; }\n\nSegment reflection(Line line, Segment seg){\n return Segment(reflection(line,seg.p1),reflection(line,seg.p2));\n}\n\ndouble distance(Segment s,Point p1,Point p2){\n Segment t = Segment(p1,p2);\n Point cp = crosspoint(s,Line(p1,p2));\n return abs(p1-cp);\n}\n\nPoint vp;\ndouble distance2(Segment s,double rad){\n Vector e = Point(10,0);\n e = rotate(e,rad);\n Point cp = crosspoint(s,Line(vp,vp+e));\n return abs(cp-vp);\n}\n\nconst int loop_limit = 100000;\nvoid compute(Point phantom,Segment mirror1,Segment mirror2){\n \n vp = phantom;\n\n Point iPhantom[2] = { reflection(mirror1,phantom), reflection(mirror2,phantom) }; \n Segment nmirror[2] = { mirror1, mirror2 };\n AngleRange range[2] = { AngleRange(phantom,mirror1), AngleRange(phantom,mirror2) };\n int cnt = 0;\n int i = 0;\n while( i < loop_limit && cnt <= 100 && !( range[0].fail() && range[1].fail() ) ){\n if( !range[0].fail() )\n if( !intersectSS(Segment(phantom,iPhantom[0]),mirror2) || !LT(distance(mirror2,phantom,iPhantom[0]),distance(mirror1,phantom,iPhantom[0])) ) {\n if( range[0].contains(iPhantom[0]) ) ++cnt;\n }\n\n if( !range[1].fail() )\n if( !intersectSS(Segment(phantom,iPhantom[1]),mirror1) || !LT(distance(mirror1,phantom,iPhantom[1]),distance(mirror2,phantom,iPhantom[1])) ) {\n if( range[1].contains(iPhantom[1]) ) ++cnt;\n }\n \n Segment tmp1 = reflection(mirror1,nmirror[1]);\n Segment tmp2 = reflection(mirror2,nmirror[0]);\n Point tmp3 = reflection(mirror1,iPhantom[1]);\n Point tmp4 = reflection(mirror2,iPhantom[0]);\n\n AngleRange tmpR(phantom,tmp1);\n range[0].update(tmpR);\n tmpR = AngleRange(phantom,tmp2);\n range[1].update(tmpR);\n\n double mid = ( range[0].mini + range[0].maxi ) / 2;\n if( !range[0].fail() && LT(distance2(tmp1,mid),distance2(nmirror[0],mid)) ) range[0].mini = range[0].maxi = 0;\n mid = ( range[1].mini + range[1].maxi ) / 2;\n if( !range[1].fail() && LT(distance2(tmp2,mid),distance2(nmirror[1],mid)) ) range[1].mini = range[1].maxi = 0;\n\n nmirror[0] = tmp1;\n nmirror[1] = tmp2;\n iPhantom[0] = tmp3;\n iPhantom[1] = tmp4;\n\n ++i;\n \n }\n\n if( cnt > 100 ) puts(\"TOO MANY\");\n else cout << cnt << endl;\n}\n\nint main(){\n int sx,sy;\n while( cin >> sx >> sy, sx | sy ){\n Point sp = Point(sx,sy);\n Segment segs[2];\n rep(i,2) cin >> segs[i].p1.x >> segs[i].p1.y >> segs[i].p2.x >> segs[i].p2.y;\n compute(sp,segs[0],segs[1]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1336, "score_of_the_acc": -1, "final_rank": 1 }, { "submission_id": "aoj_2073_1468969", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev ?????? cur ????????£??? next ????????????????§????????±???????\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //???????????°??????????????????\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n\n//cross product of pq and pr\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\n \n//returns true if point r is on the same line as the line pq\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \n//returns true if point t is on the left side of line pq\nbool ccwtest(Point p,Point q,Point r){\n return cross3p(p,q,r) > 0; //can be modified to accept collinear points\n}\n \nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n\n// --------\n\ninline bool LT(double a,double b) { return !equals(a,b) && a < b; }\ninline bool LTE(double a,double b) { return equals(a,b) || a < b; }\n\n\nconst double PI2 = M_PI * 2.0;\nstruct AngleRange {\n Point o; // ?????????, ???????????????????§???? ????????¨???????????????????????????,????????????\n double mini,maxi; // mini <= maxi | 0 <= mini,maxi <= 720\n // ?????°?????????????????§abs(maxi-mini)???180??????\n\n AngleRange( Point p, Segment seg ) {\n o = p;\n seg.p1 = seg.p1 - o, seg.p2 = seg.p2 - o;\n complex<double> p1(seg.p1.x,seg.p1.y);\n complex<double> p2(seg.p2.x,seg.p2.y);\n double tmp = arg( p1 / p2 ); // ???????????? p1 ??¨ p2 ??????????§? arg( p1 / p2 )\n mini = LT(0.0,tmp) ? arg(p2) : arg(p1);\n if( LT(mini,0) ) mini += PI2;\n maxi = abs(tmp) + mini;\n }\n\n bool fail(){ return equals(mini,maxi); }\n\n // mini < a < maxi\n bool contains(double a) const { \n return ( LT(mini,a-PI2) && LT(a-PI2,maxi) ) || ( LT(mini,a) && LT(a,maxi) ) || ( LT(mini,a+PI2) && LT(a+PI2,maxi) );\n }\n\n bool contains(Point p) const {\n p = p - o;\n complex<double> cp(p.x,p.y);\n return contains(arg(cp));\n }\n\n void update( AngleRange ar ) {\n if( contains(ar.mini) ) {\n if( contains(ar.maxi) ) mini = ar.mini, maxi = ar.maxi;\n else mini = ar.mini;\n } else { \n if( contains(ar.maxi) ) maxi = ar.maxi;\n else if( !ar.contains(mini) && !equals(mini,ar.mini) ) maxi = mini;\n }\n if( LT(maxi-mini,0) ) maxi += PI2;\n if( LTE(PI2,maxi-mini) ) maxi -= PI2;\n }\n\n};\n\n\nostream& operator << (ostream& os,const AngleRange& a){ os << \"[ \" << a.mini*180.0/M_PI << \" , \" << a.maxi*180.0/M_PI << \" ]\"; }\n\nSegment reflection(Line line, Segment seg){\n return Segment(reflection(line,seg.p1),reflection(line,seg.p2));\n}\n\ndouble distance(Segment s,Point p1,Point p2){\n Segment t = Segment(p1,p2);\n Point cp = crosspoint(s,Line(p1,p2));\n return abs(p1-cp);\n}\n\nPoint vp;\ndouble distance2(Segment s,double rad){\n Vector e = Point(10,0);\n e = rotate(e,rad);\n Point cp = crosspoint(s,Line(vp,vp+e));\n return abs(cp-vp);\n}\n\nconst int loop_limit = 100000;\nvoid compute(Point phantom,Segment mirror1,Segment mirror2){\n \n\n vp = phantom;\n\n Point iPhantom[2] = { reflection(mirror1,phantom), reflection(mirror2,phantom) }; \n Segment nmirror[2] = { mirror1, mirror2 };\n AngleRange range[2] = { AngleRange(phantom,mirror1), AngleRange(phantom,mirror2) };\n int cnt = 0;\n /*\n puts(\"------------\");\n cout << \"mirror1 = \" << mirror1 << endl;\n cout << \"nmirror[0] = \" << nmirror[0] << endl;\n cout << \"iPhantom[0] = \" << iPhantom[0] << endl;\n cout << \"range[0] = \" << range[0] << endl;\n cout << \"mirror2 = \" << mirror2 << endl;\n cout << \"nmirror[1] = \" << nmirror[1] << endl;\n cout << \"iPhantom[1] = \" << iPhantom[1] << endl;\n cout << \"range[1] = \" << range[1] << endl;\n */\n int i = 0;\n while( i < loop_limit && cnt <= 100 && !( range[0].fail() && range[1].fail() ) ){\n //cout << \"nmirror[0] = \" << nmirror[0] << endl;\n //cout << \"nmirror[1] = \" << nmirror[1] << endl;\n //puts(\"\");\n if( !range[0].fail() )\n if( !intersectSS(Segment(phantom,iPhantom[0]),mirror2) || !LT(distance(mirror2,phantom,iPhantom[0]),distance(mirror1,phantom,iPhantom[0])) ) {\n //if( !intersectSS(Segment(phantom,iPhantom[0]),mirror2) ) {\n if( range[0].contains(iPhantom[0]) ) {\n\t//cout << \"iPhantom[0] success!!!\" << endl;\n\t++cnt;\n }\n }\n\n if( !range[1].fail() )\n if( !intersectSS(Segment(phantom,iPhantom[1]),mirror1) || !LT(distance(mirror1,phantom,iPhantom[1]),distance(mirror2,phantom,iPhantom[1])) ) {\n //if( !intersectSS(Segment(phantom,iPhantom[1]),mirror1) ) {\n if( range[1].contains(iPhantom[1]) ) {\n\t//cout << \"iPhantom[1] success!!!\" << endl;\n\t++cnt;\n }\n }\n \n Segment tmp1 = reflection(mirror1,nmirror[1]);\n Segment tmp2 = reflection(mirror2,nmirror[0]);\n Point tmp3 = reflection(mirror1,iPhantom[1]);\n Point tmp4 = reflection(mirror2,iPhantom[0]);\n\n AngleRange tmpR(phantom,tmp1);\n range[0].update(tmpR);\n tmpR = AngleRange(phantom,tmp2);\n range[1].update(tmpR);\n\n double mid = ( range[0].mini + range[0].maxi ) / 2;\n /*\n cout << \"tmp = \" << tmp1 << endl;\n cout << \"mir = \" << nmirror[0] << endl;\n cout << distance2(tmp1,mid) << \" and \" << distance2(nmirror[0],mid) << endl;\n */\n if( !range[0].fail() && LT(distance2(tmp1,mid),distance2(nmirror[0],mid)) ) range[0].mini = range[0].maxi = 0;\n mid = ( range[1].mini + range[1].maxi ) / 2;\n if( !range[1].fail() && LT(distance2(tmp2,mid),distance2(nmirror[1],mid)) ) range[1].mini = range[1].maxi = 0;\n\n nmirror[0] = tmp1;\n nmirror[1] = tmp2;\n iPhantom[0] = tmp3;\n iPhantom[1] = tmp4;\n /*\n AngleRange tmpR(phantom,nmirror[0]);\n range[0].update(tmpR);\n tmpR = AngleRange(phantom,nmirror[1]);\n range[1].update(tmpR);\n */\n \n\n ++i;\n \n }\n //cout << \"cnt = \" << cnt << endl;\n if( cnt > 100 ) puts(\"TOO MANY\");\n else cout << cnt << endl;\n}\n\nint main(){\n /*\n Segment s;\n cin >> s.p1.x >> s.p1.y >> s.p2.x >>s.p2.y;\n AngleRange range(Point(0,0),s);\n cout << \"[\" << range.mini*180.0/M_PI << \",\" << range.maxi*180.0/M_PI << \"]\" << endl;\n while(1){\n Segment t;\n cin >> t.p1.x >> t.p1.y >> t.p2.x >> t.p2.y;\n AngleRange trange(Point(0,0),t);\n cout << \"* [\" << trange.mini*180.0/M_PI << \",\" << trange.maxi*180.0/M_PI << \"]\" << endl;\n range.update(trange);\n cout << \"= [\" << range.mini*180.0/M_PI << \",\" << range.maxi*180.0/M_PI << \"]\" << endl;\n }\n return 0;\n */\n int sx,sy;\n while( cin >> sx >> sy, sx | sy ){\n Point sp = Point(sx,sy);\n Segment segs[2];\n rep(i,2) cin >> segs[i].p1.x >> segs[i].p1.y >> segs[i].p2.x >> segs[i].p2.y;\n compute(sp,segs[0],segs[1]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1336, "score_of_the_acc": -1, "final_rank": 1 } ]
aoj_2075_cpp
Dock to the Future You had long wanted a spaceship, and finally you bought a used one yesterday! You have heard that the most difficult thing on spaceship driving is to stop your ship at the right position in the dock. Of course you are no exception. After a dozen of failures, you gave up doing all the docking process manually. You began to write a simple program that helps you to stop a spaceship. First, you somehow put the spaceship on the straight course to the dock manually. Let the distance to the limit line be x [m], and the speed against the dock be v [m/s]. Now you turn on the decelerating rocket. Then, your program will control the rocket to stop the spaceship at the best position. Your spaceship is equipped with a decelerating rocket with n modes. When the spaceship is in mode- i (0 ≤ i < n ), the deceleration rate is a i [m/s 2 ]. You cannot re-accelerate the spaceship. The accelerating rocket is too powerful to be used during docking. Also, you cannot turn off-and-on the decelerating rocket, because your spaceship is a used and old one, once you stopped the rocket, it is less certain whether you can turn it on again. In other words, the time you turn off the rocket is the time you stop your spaceship at the right position. After turning on the deceleration rocket, your program can change the mode or stop the rocket at every sec- ond, starting at the very moment the deceleration began. Given x and v , your program have to make a plan of deceleration. The purpose and the priority of your program is as follows: Stop the spaceship exactly at the limit line. If this is possible, print “perfect”. If it is impossible, then stop the spaceship at the position nearest possible to the limit line, but before the line. In this case, print “good d ”, where d is the distance between the limit line and the stopped position. Print three digits after the decimal point. If it is impossible again, decelerate the spaceship to have negative speed, and print “try again”. If all of these three cases are impossible, then the spaceship cannot avoid overrunning the limit line. In this case, print “crash”. Input The first line of the input consists of a single integer c , the number of test cases. Each test case begins with a single integer n (1 ≤ n ≤ 10), the number of deceleration modes. The following line contains n positive integers a 0 , . . . , a n-1 (1 ≤ a i ≤ 100), each denoting the deceleration rate of each mode. The next line contains a single integer q (1 ≤ q ≤ 20), and then q lines follow. Each of them contains two positive integers x and v (1 ≤ x , v ≤ 100) defined in the problem statement. Output For each pair of x and v , print the result in one line. A blank line should be inserted between the test cases. Sample Input 1 3 2 4 6 4 10 100 2 3 10 6 7 6 Output for the Sample Input crash try again good 1.000 perfect
[ { "submission_id": "aoj_2075_9659676", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\nusing namespace std;\nvoid solve(){\n int n, q;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n cin >> q;\n while (q--){\n int x, v;\n cin >> x >> v;\n vector<vector<int> > dp(2 * x + 1, vector<int> (v + 1, 0));\n dp[2 * x][v] = 1;\n for (int i = 2 * x; i >= 0; i--){\n for (int j = v; j >= 0; j--){\n if (!dp[i][j]) continue;\n for (int p = 0; p < n; p++){\n if (i - 2 * j + a[p] < 0 || j - a[p] < 0) continue;\n dp[i - 2 * j + a[p]][j - a[p]] = 1;\n }\n }\n }\n int mx = 0;\n for (int i = 0; i < n; i++) mx = max(mx, a[i]);\n if (v * v > 2 * mx * x){\n cout << \"crash\" << endl;\n continue;\n }\n \n if (dp[0][0]){\n cout << \"perfect\" << endl;\n continue;\n }\n double ans = -1;\n for (int i = 1; i <= 2 * x; i++){\n if (dp[i][0]){\n ans = i;\n break;\n }\n }\n if (ans != -1){\n cout << \"good \" << ans / 2 << endl;\n continue;\n }\n cout << \"try again\" << endl;\n }\n}\nint main() {\n cout << fixed << setprecision(3);\n int t;\n cin >> t;\n while (t--){\n solve();\n if (t > 0) cout << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3304, "score_of_the_acc": -1.2086, "final_rank": 3 }, { "submission_id": "aoj_2075_9659666", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\nvoid solve(){\n int n, q;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n int mx = 0;\n for (int i = 0; i < n; i++) mx = max(mx, a[i]);\n cin >> q;\n while (q--){\n int x, v;\n cin >> x >> v;\n vector<vector<int> > dp(2 * x + 1, vector<int> (v + 1, 0));\n dp[2 * x][v] = 1;\n for (int i = 2 * x; i >= 0; i--){\n for (int j = v; j >= 0; j--){\n if (!dp[i][j]) continue;\n for (int p = 0; p < n; p++){\n if (i - 2 * j + a[p] < 0 || j - a[p] < 0) continue;\n dp[i - 2 * j + a[p]][j - a[p]] = 1;\n }\n }\n }\n if (v * v > 2 * mx * x){\n cout << \"crash\" << endl;\n continue;\n }\n if (dp[0][0]){\n cout << \"perfect\" << endl;\n continue;\n }\n double ans = -1;\n for (int i = 1; i <= 2 * x; i++){\n if (dp[i][0]){\n ans = i;\n break;\n }\n }\n if (ans != -1){\n cout << \"good \" << ans / 2 << endl;\n continue;\n }\n cout << \"try again\" << endl;\n }\n}\nint main() {\n cout << fixed << setprecision(3);\n int t;\n cin >> t;\n while (t--){\n solve();\n if (t > 0) cout << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3392, "score_of_the_acc": -1.25, "final_rank": 5 }, { "submission_id": "aoj_2075_9659652", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\nvoid solve(){\n int n, q;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n sort(a.begin(), a.end());\n cin >> q;\n while (q--){\n int x, v;\n cin >> x >> v;\n vector<vector<int> > dp(2 * x + 1, vector<int> (v + 1, 0));\n dp[2 * x][v] = 1;\n for (int i = 2 * x; i >= 0; i--){\n for (int j = v; j >= 0; j--){\n if (!dp[i][j]) continue;\n for (int p = 0; p < n; p++){\n if (i - 2 * j + a[p] < 0 || j - a[p] < 0) continue;\n dp[i - 2 * j + a[p]][j - a[p]] = 1;\n }\n }\n }\n if (v * v > 2 * a[n - 1] * x){\n cout << \"crash\" << endl;\n continue;\n }\n if (dp[0][0]){\n cout << \"perfect\" << endl;\n continue;\n }\n double ans = -1;\n for (int i = 1; i <= 2 * x; i++){\n if (dp[i][0]){\n ans = i;\n break;\n }\n }\n if (ans != -1){\n cout << \"good \" << ans / 2 << endl;\n continue;\n }\n cout << \"try again\" << endl;\n }\n}\nint main() {\n cout << fixed << setprecision(3);\n int t;\n cin >> t;\n while (t--){\n solve();\n if (t > 0) cout << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3312, "score_of_the_acc": -1.2123, "final_rank": 4 }, { "submission_id": "aoj_2075_9659649", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\nvoid solve(){\n int n, q;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n sort(a.begin(), a.end());\n cin >> q;\n while (q--){\n int x, v;\n cin >> x >> v;\n vector<vector<int> > dp(2 * x + 1, vector<int> (v + 1, 0));\n if (v * v > 2 * a[n - 1] * x){\n cout << \"crash\" << endl;\n continue;\n }\n dp[2 * x][v] = 1;\n for (int i = 2 * x; i >= 0; i--){\n for (int j = v; j >= 0; j--){\n if (!dp[i][j]) continue;\n for (int p = 0; p < n; p++){\n if (i - 2 * j + a[p] < 0 || j - a[p] < 0) continue;\n dp[i - 2 * j + a[p]][j - a[p]] = 1;\n }\n }\n }\n if (dp[0][0]){\n cout << \"perfect\" << endl;\n continue;\n }\n double ans = -1;\n for (int i = 1; i <= 2 * x; i++){\n if (dp[i][0]){\n ans = i;\n break;\n }\n }\n if (ans != -1){\n cout << \"good \" << ans / 2 << endl;\n continue;\n }\n cout << \"try again\" << endl;\n }\n}\nint main() {\n cout << fixed << setprecision(3);\n int t;\n cin >> t;\n while (t--){\n solve();\n if (t > 0) cout << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3392, "score_of_the_acc": -1.25, "final_rank": 5 }, { "submission_id": "aoj_2075_1667651", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#pragma warning(disable:4996)\nusing namespace std;\n\ndouble eps = 1e-9;\nint main() {\n int A; cin >> A;\n while (A--) {\n int N; cin >> N;\n vector<int>as;\n for (int i = 0; i < N; ++i) {\n int a; cin >> a;\n as.push_back(a);\n }\n sort(as.begin(), as.end());\n int Q; cin >> Q;\n for (int t = 0; t< Q; ++t) {\n int x, v; cin >> x >> v;\n double stoptime = double(v) / as.back();\n if (x + eps < stoptime*v / 2) {\n cout << \"crash\" << endl;\n }\n else {\n bool dp[201][101];\n memset(dp, false, sizeof(dp));\n dp[2 * x][v] = true;\n for (int i = 2 * x; i >0; --i) {\n for (int j = 1; j <= v; ++j) {\n if (dp[i][j]) {\n for (int k = 0; k < as.size(); ++k) {\n int nextspeed = j - as[k];\n if (nextspeed >= 0) {\n if (i - j - nextspeed >= 0) {\n dp[i - j - nextspeed][nextspeed] = true;\n }\n }\n }\n }\n \n }\n }\n int dis = 9999;\n for (int i = 0; i <= 2 * x; ++i) {\n if (dp[i][0]) {\n dis = min(dis, i);\n }\n }\n if (dis == 9999) {\n cout << \"try again\" << endl;\n }\n else if (dis > 0) {\n cout << setprecision(3) << fixed << \"good \";\n printf(\"%.3f\", double(dis)/2);\n cout << endl;\n } \n else {\n cout << \"perfect\" << endl;\n }\n }\n }\n if (A)cout << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.8832, "final_rank": 1 }, { "submission_id": "aoj_2075_474165", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint n;\nvector<int> a;\n\nstring solve(int x, int v)\n{\n if(v * v > x * (2 * a[n-1]))\n return \"crash\";\n\n x *= 2;\n vector<vector<bool> > dp(x+1, vector<bool>(v+1, false));\n dp[x][v] = true;\n\n for(int i=x; i>=0; --i){\n for(int j=v; j>=0; --j){\n if(!dp[i][j])\n continue;\n for(int k=0; k<n; ++k){\n int i2 = i - 2 * j + a[k];\n int j2 = j - a[k];\n if(i2 >= 0 && j2 >= 0)\n dp[i2][j2] = true;\n }\n }\n }\n\n if(dp[0][0])\n return \"perfect\";\n\n for(int i=0; i<x; ++i){\n if(dp[i][0]){\n ostringstream oss;\n oss << \"good \" << (i / 2);\n if(i % 2 == 0)\n oss << \".000\";\n else\n oss << \".500\";\n return oss.str();\n }\n }\n\n return \"try again\";\n}\n\nint main()\n{\n int c;\n cin >> c;\n\n while(--c >= 0){\n cin >> n;\n a.resize(n);\n for(int i=0; i<n; ++i)\n cin >> a[i];\n sort(a.begin(), a.end());\n\n int q;\n cin >> q;\n while(--q >= 0){\n int x, v;\n cin >> x >> v;\n cout << solve(x, v) << endl;\n }\n\n if(c > 0)\n cout << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1268, "score_of_the_acc": -1, "final_rank": 2 } ]
aoj_2071_cpp
Web 0.5 In a forest, there lived a spider named Tim. Tim was so smart that he created a huge, well-structured web. Surprisingly, his web forms a set of equilateral and concentric N -sided polygons whose edge lengths form an arithmetic sequence. Figure 1: An example web of Tim Corresponding vertices of the N-sided polygons lie on a straight line, each apart from neighboring ver- tices by distance 1. Neighboring vertices are connected by a thread (shown in the figure as solid lines), on which Tim can walk (you may regard Tim as a point). Radial axes are numbered from 1 to N in a rotational order, so you can denote by ( r , i ) the vertex on axis i and apart from the center of the web by distance r . You may consider the web as infinitely large, i.e. consisting of infinite number of N -sided polygons, since the web is so gigantic. Tim’s web is thus marvelous, but currently has one problem: due to rainstorms, some of the threads are damaged and Tim can’t walk on those parts. Now, a bug has been caught on the web. Thanks to the web having so organized a structure, Tim accu- rately figures out where the bug is, and heads out for that position. You, as a biologist, are interested in whether Tim is smart enough to move through the shortest possible route. To judge that, however, you need to know the length of the shortest route yourself, and thereby decided to write a program which calculates that length. Input The input consists of multiple test cases. Each test case begins with a line containing two integers N (3 ≤ N ≤ 100) and X (0 ≤ X ≤ 100), where N is as described above and X denotes the number of damaged threads. Next line contains four integers r S , i S , r T , and i T , meaning that Tim is starting at ( r S , i S ) and the bug is at ( r T , i T ). This line is followed by X lines, each containing four integers r A , i A , r B , and i B , meaning that a thread connecting ( r A , i A ) and ( r B , i B ) is damaged. Here ( r A , i A ) and ( r B , i B ) will be always neighboring to each other. Also, for all vertices ( r , i ) given above, you may assume 1 ≤ r ≤ 10 7 and 1 ≤ i ≤ N . There will be at most 200 test cases. You may also assume that Tim is always able to reach where the bug is. The input is terminated by a line containing two zeros. Output For each test case, print the length of the shortest route in a line. You may print any number of digits after the decimal point, but the error must not be greater than 0.01. Sample Input 5 1 2 1 3 2 2 1 2 2 0 0 Output for the Sample Input 4.18
[ { "submission_id": "aoj_2071_4823005", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 605\n\nint N,X;\n\nstruct Info{\n\tInfo(int arg_from_dist,int arg_from_line,int arg_to_dist,int arg_to_line){\n\t\tfrom_dist = arg_from_dist;\n\t\tfrom_line = arg_from_line;\n\t\tto_dist = arg_to_dist;\n\t\tto_line = arg_to_line;\n\t}\n\tint from_dist,from_line,to_dist,to_line;\n};\n\nstruct Info2{\n\tInfo2(int arg_to_index,int arg_to_line){\n\t\tto_index = arg_to_index;\n\t\tto_line = arg_to_line;\n\t}\n\tint to_index,to_line;\n};\n\nstruct Edge{\n\tEdge(int arg_to_index,int arg_to_line,double arg_cost){\n\t\tto_index = arg_to_index;\n\t\tto_line = arg_to_line;\n\t\tcost = arg_cost;\n\t}\n\tint to_index,to_line;\n\tdouble cost;\n};\n\nstruct State{\n\tState(int arg_layer_index,int arg_line,double arg_sum_dist){\n\t\tlayer_index = arg_layer_index;\n\t\tline = arg_line;\n\t\tsum_dist = arg_sum_dist;\n\t}\n\tbool operator<(const struct State &arg) const{\n\n\t\treturn sum_dist > arg.sum_dist; //総距離の昇順\n\t}\n\n\tint layer_index,line;\n\tdouble sum_dist;\n};\n\nint dist_table[SIZE];\nint start_dist,start_line;\nint goal_dist,goal_line;\nmap<int,int> MAP;\nvector<int> V;\ndouble min_dist[SIZE][105];\nvector<Edge> G[SIZE][105];\nvector<Info> info;\nvector<Info2> INFO[SIZE][105];\n\n\n//前後のレイヤを追加\nvoid add_dist(int dist){\n\n\tV.push_back(dist+1);\n\tif(dist > 1){\n\t\tV.push_back(dist-1);\n\t}\n}\n\n\nvoid func(){\n\n\tfor(int i = 0; i < SIZE; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tG[i][k].clear();\n\t\t\tINFO[i][k].clear();\n\t\t}\n\t}\n\tMAP.clear();\n\tV.clear();\n\tinfo.clear();\n\n\tV.push_back(1); //★★一番内側★★\n\n\tscanf(\"%d %d\",&start_dist,&start_line);\n\tstart_line--;\n\tscanf(\"%d %d\",&goal_dist,&goal_line);\n\tgoal_line--;\n\n\tV.push_back(start_dist);\n\tadd_dist(start_dist);\n\n\tV.push_back(goal_dist);\n\tadd_dist(goal_dist);\n\n\tint dist1,line1,dist2,line2;\n\n\tfor(int i = 0; i < X; i++){\n\n\t\tscanf(\"%d %d %d %d\",&dist1,&line1,&dist2,&line2);\n\t\tline1--;\n\t\tline2--;\n\n\t\tinfo.push_back(Info(dist1,line1,dist2,line2));\n\n\t\tif(dist1 == dist2){ //横\n\n\t\t\tV.push_back(dist1);\n\t\t\tadd_dist(dist1);\n\n\t\t}else{ //縦\n\n\t\t\tV.push_back(dist1);\n\t\t\tadd_dist(dist1);\n\n\t\t\tV.push_back(dist2);\n\t\t\tadd_dist(dist2);\n\t\t}\n\t}\n\n\tsort(V.begin(),V.end());\n\tV.erase(unique(V.begin(),V.end()),V.end());\n\n\tint num_layer = 0;\n\tint index;\n\n\tfor(int i = 0; i < V.size(); i++){\n\n\t\tMAP[V[i]] = num_layer;\n\t\tdist_table[num_layer++] = V[i];\n\t}\n\n\t//infoの仕分け\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tint from_index = MAP[info[i].from_dist];\n\t\tint to_index = MAP[info[i].to_dist];\n\t\tINFO[from_index][info[i].from_line].push_back(Info2(to_index,info[i].to_line));\n\t\tINFO[to_index][info[i].to_line].push_back(Info2(from_index,info[i].from_line));\n\t}\n\n\n\tint start_index = -1,goal_index = -1;\n\tfor(int i = 0; i < num_layer; i++){\n\t\tif(dist_table[i] == start_dist){\n\n\t\t\tstart_index = i;\n\t\t}\n\t\tif(dist_table[i] == goal_dist){\n\n\t\t\tgoal_index = i;\n\t\t}\n\t}\n\n\tdouble center_rad = 2*M_PI/(double)N; //多角形の中心角\n\n\t//隣接するノードにのみエッジを張る\n\tfor(int i = 0; i < num_layer; i++){\n\n\t\tdouble a = dist_table[i];\n\t\tdouble len = sqrt(2*a*a-2*a*a*cos(center_rad));\n\n\t\tint tmp_dist = dist_table[i];\n\n\t\t//同レイヤ\n\t\tfor(int k = 0; k < N; k++){\n\n\t\t\tint from = k;\n\t\t\tint to = (k+1)%N;\n\n\t\t\tbool FLG = true;\n\n\t\t\tfor(int p = 0; p < INFO[i][from].size(); p++){\n\t\t\t\tif(INFO[i][from][p].to_index == i && INFO[i][from][p].to_line == to){\n\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!FLG)continue;\n\n\t\t\tG[i][from].push_back(Edge(i,to,len));\n\t\t\tG[i][to].push_back(Edge(i,from,len));\n\t\t}\n\n\t\tif(i == num_layer-1)continue;\n\n\t\t//次のレイヤと\n\t\tfor(int k = 0; k < N; k++){\n\n\t\t\tbool FLG = true;\n\n\t\t\tfor(int p = 0; p < INFO[i][k].size(); p++){\n\t\t\t\tif(INFO[i][k][p].to_index == i+1 && INFO[i][k][p].to_line == k){\n\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!FLG)continue;\n\n\t\t\tint diff = dist_table[i+1]-dist_table[i];\n\n\t\t\tG[i][k].push_back(Edge(i+1,k,diff));\n\t\t\tG[i+1][k].push_back(Edge(i,k,diff));\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_layer; i++){\n\t\tfor(int k = 0; k < N; k++){\n\n\t\t\tmin_dist[i][k] = BIG_NUM;\n\t\t}\n\t}\n\n\tpriority_queue<State> Q;\n\tmin_dist[start_index][start_line] = 0;\n\tQ.push(State(start_index,start_line,0));\n\n\n\tint next_index,next_line;\n\tdouble next_cost;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_dist > min_dist[Q.top().layer_index][Q.top().line]+EPS){\n\n\t\t\tQ.pop();\n\n\t\t}else if(Q.top().layer_index == goal_index && Q.top().line == goal_line){\n\n\t\t\tprintf(\"%.10lf\\n\",Q.top().sum_dist);\n\t\t\treturn;\n\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < G[Q.top().layer_index][Q.top().line].size(); i++){\n\n\t\t\t\tnext_index = G[Q.top().layer_index][Q.top().line][i].to_index;\n\t\t\t\tnext_line = G[Q.top().layer_index][Q.top().line][i].to_line;\n\t\t\t\tnext_cost = Q.top().sum_dist+ G[Q.top().layer_index][Q.top().line][i].cost;\n\n\t\t\t\tif(min_dist[next_index][next_line] > next_cost+EPS){\n\n\t\t\t\t\tmin_dist[next_index][next_line] = next_cost+EPS;\n\t\t\t\t\tQ.push(State(next_index,next_line,next_cost));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d\",&N,&X);\n\t\tif(N == 0 && X == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 9564, "score_of_the_acc": -0.9568, "final_rank": 7 }, { "submission_id": "aoj_2071_3117720", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <map>\n#include <set>\n#include <utility>\n#include <queue>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\ntypedef complex<double> P;\n\nstruct Pos{\n int r,i;\n Pos(int r, int i):r(r),i(i){}\n Pos(){}\n bool operator<(const Pos &a) const{\n return (r!=a.r)? r<a.r: i<a.i;\n }\n bool operator==(const Pos &a) const{\n return r==a.r && i==a.i;\n }\n};\n\nP convert(const Pos &pos, int n){\n double angle = 2*PI*pos.i/n;\n return P(pos.r, 0) *P(cos(angle), sin(angle));\n}\n\nint main(){\n while(1){\n int n,x;\n cin >> n >> x;\n if(n==0) break;\n\n Pos s,t;\n cin >> s.r >> s.i >> t.r >> t.i;\n s.i--; t.i--;\n set<pair<Pos, Pos> > ng;\n for(int i=0; i<x; i++){\n int rs,is,rt,it;\n cin >> rs >> is >> rt >> it;\n is--; it--;\n pair<Pos, Pos> edge(Pos(rs, is), Pos(rt, it));\n if(edge.second < edge.first){\n swap(edge.first, edge.second);\n }\n ng.insert(edge);\n }\n\n vector<int> rlist;\n rlist.push_back(s.r);\n rlist.push_back(t.r);\n rlist.push_back(1);\n for(const pair<Pos, Pos> &e: ng){\n rlist.push_back(e.first.r);\n rlist.push_back(e.second.r);\n }\n for(int i=rlist.size()-1; i>=0; i--){\n rlist.push_back(rlist[i]-1);\n rlist.push_back(rlist[i]+1);\n }\n sort(rlist.begin(), rlist.end());\n rlist.erase(unique(rlist.begin(), rlist.end()), rlist.end());\n rlist.erase(rlist.begin());\n rlist.push_back(1e8);\n\n map<Pos, vector<pair<Pos, double> > > adj;\n for(int i=0; i<(int)rlist.size()-1; i++){\n for(int j=0; j<n; j++){\n Pos curr(rlist[i], j);\n Pos next[2];\n next[0] = Pos(rlist[i], (j+1)%n);\n next[1] = Pos(rlist[i+1], j);\n for(int d=0; d<2; d++){\n pair<Pos, Pos> edge(curr, next[d]);\n if(edge.second < edge.first) swap(edge.first, edge.second);\n if(ng.find(edge) == ng.end()){\n double dist = abs(convert(curr, n) -convert(next[d], n));\n adj[curr].emplace_back(next[d], dist);\n adj[next[d]].emplace_back(curr, dist);\n }\n }\n }\n }\n\n priority_queue<pair<double, Pos> > wait;\n wait.push(make_pair(0, s));\n map<Pos, double> mincost;\n for(const auto &key: adj){\n mincost[key.first] = INF;\n }\n mincost[s] = 0;\n while(!wait.empty()){\n double dist = -wait.top().first;\n Pos pos = wait.top().second;\n wait.pop();\n if(dist > mincost[pos] +EPS) continue;\n if(pos == t) break;\n for(const auto &next: adj[pos]){\n double ndist = dist +next.second;\n Pos npos = next.first;\n if(ndist +EPS < mincost[npos]){\n mincost[npos] = ndist;\n wait.push(make_pair(-ndist, npos));\n }\n }\n }\n cout << fixed << setprecision(5);\n cout << mincost[t] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 9932, "score_of_the_acc": -2, "final_rank": 8 }, { "submission_id": "aoj_2071_2289642", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) begin(v), end(v)\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nusing pint = pair<int, int>;\nusing tint = tuple<int, int, int>;\nusing vint = vector<int>;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nint N, X;\nint sr, sc, tr, tc;\nint ar[101], ac[101];\nint br[101], bc[101];\nvint R; // compress\nmap<int, int> mp; // uncompress\nset< pair<pint, pint> > st; // non-exist edge\n\ndouble base;\n\ndouble distance(int ar, int br) {\n return (ar == br ? (R[ar]+1)*base : abs(R[ar] - R[br]));\n}\n\ndouble mincost[808][101];\n\nusing State = tuple<double, int, int>;\n\nconst int dr[] = {0, 1, 0, -1};\nconst int dc[] = {1, 0, -1, 0};\n\ndouble dijkstra() {\n priority_queue<State, vector<State>, greater<State> > que;\n fill(mincost[0], mincost[808], DBL_MAX/2);\n que.emplace(0, sr, sc);\n mincost[sr][sc] = 0;\n while(!que.empty()) {\n double cost; int r, c;\n tie(cost, r, c) = que.top(); que.pop();\n if(mincost[r][c] < cost) continue;\n if(r == tr && c == tc) return cost;\n rep(i, 4) {\n int nr = r + dr[i], nc = (c+dc[i]+N)%N;\n if(nr < 0 || R.size() <= nr) continue;\n auto u = make_pair(r, c);\n auto v = make_pair(nr, nc);\n if(st.count(make_pair(u, v))) continue;\n double dist = cost + distance(r, nr);\n if(dist < mincost[nr][nc]) {\n\tmincost[nr][nc] = dist;\n\tque.emplace(dist, nr, nc);\n }\n }\n }\n return -1;\n}\n\nsigned main()\n{\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n while(cin >> N >> X, N) {\n cin >> sr >> sc >> tr >> tc;\n --sr, --sc, --tr, --tc;\n R.clear();\n R.push_back(0);\n R.push_back(sr);\n R.push_back(tr);\n rep(i, X) {\n cin >> ar[i] >> ac[i] >> br[i] >> bc[i];\n --ar[i], --ac[i], --br[i], --bc[i];\n if(ar[i]) R.push_back(ar[i]-1);\n R.push_back(ar[i]);\n R.push_back(ar[i]+1);\n if(br[i]) R.push_back(br[i]-1);\n R.push_back(br[i]);\n R.push_back(br[i]+1);\n }\n sort(all(R));\n R.erase(unique(all(R)), R.end());\n mp.clear();\n st.clear();\n rep(i, R.size()) mp[R[i]] = i;\n sr = mp[sr], tr = mp[tr];\n rep(i, X) {\n ar[i] = mp[ar[i]], br[i] = mp[br[i]];\n auto u = make_pair(ar[i], ac[i]);\n auto v = make_pair(br[i], bc[i]);\n st.emplace(u, v);\n st.emplace(v, u);\n }\n base = sqrt(2.0-2.0*cos(2.0*acos(-1)/(double)N));\n cout << dijkstra() << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4364, "score_of_the_acc": -0.3459, "final_rank": 5 }, { "submission_id": "aoj_2071_800006", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <queue>\n#include <set>\n#include <cmath>\n#include <cstdio>\nusing namespace std;\n \nconst int MAXH = 400;\nconst int MAXW = 105;\nconst int MAXX = 101;\nconst double inf = 1e100;\nconst int di[] = {0,1,0,-1};\nconst int dj[] = {1,0,-1,0};\n \nint N, X;\nint si, sj, ti, tj;\nint pi[MAXX], pj[MAXX], qi[MAXX], qj[MAXX];\nvector<int> vi;\nmap<int,int> mi, rmi;\nint H;\nset<pair<pair<int,int>, pair<int,int> > > wall;\ndouble x;\n \nstruct State {\n int pi, pj;\n double cost;\n bool operator < (const State &s) const {\n return cost > s.cost;\n }\n};\ndouble cost[MAXH][MAXW];\n \ndouble getDist(int si, int sj, int ti, int tj) {\n if(si == ti) {\n return rmi[si] * x;\n } else {\n return abs(rmi[si]-rmi[ti]);\n }\n}\n \ndouble dijkstra(int si, int sj, int ti, int tj) {\n priority_queue<State> que;\n fill(cost[0], cost[MAXH], inf);\n que.push((State){si,sj,0.0});\n cost[si][sj] = 0.0;\n while(!que.empty()) {\n const State s = que.top();\n que.pop();\n if(cost[s.pi][s.pj] < s.cost) continue;\n if(s.pi == ti && s.pj == tj) return s.cost;\n for(int k = 0; k < 4; ++k) {\n State t = s;\n t.pi += di[k];\n t.pj = (t.pj + dj[k] + N) % N;\n if(t.pi >= H) continue;\n if(t.pi < 0) continue;\n if(wall.count(make_pair(make_pair(s.pi,s.pj),make_pair(t.pi,t.pj)))) continue;\n t.cost += getDist(s.pi, s.pj, t.pi, t.pj);\n if(cost[t.pi][t.pj] <= t.cost) continue;\n que.push(t);\n cost[t.pi][t.pj] = t.cost;\n }\n }\n return inf;\n}\n \nint main() {\n while(cin >> N >> X && (N|X)) {\n cin >> si >> sj >> ti >> tj;\n --sj; --tj;\n for(int i = 0; i < X; ++i) {\n cin >> pi[i] >> pj[i] >> qi[i] >> qj[i];\n --pj[i]; --qj[i];\n }\n\n vi.clear();\n vi.push_back(1);\n vi.push_back(si);\n vi.push_back(ti);\n for(int i = 0; i < X; ++i) {\n if(pi[i]-1 >= 1) vi.push_back(pi[i]-1);\n vi.push_back(pi[i]);\n vi.push_back(pi[i]+1);\n if(qi[i]-1 >= 1) vi.push_back(qi[i]-1);\n vi.push_back(qi[i]);\n vi.push_back(qi[i]+1);\n }\n sort(vi.begin(), vi.end());\n vi.erase(unique(vi.begin(), vi.end()), vi.end());\n \n mi.clear();\n rmi.clear();\n for(int i = 0; i < vi.size(); ++i) {\n mi[vi[i]] = i;\n rmi[i] = vi[i];\n }\n \n H = vi.size();\n si = mi[si];\n ti = mi[ti];\n for(int i = 0; i < X; ++i) {\n pi[i] = mi[pi[i]];\n qi[i] = mi[qi[i]];\n }\n \n wall.clear();\n for(int i = 0; i < X; ++i) {\n pair<pair<int,int>, pair<int,int> > p = make_pair(make_pair(pi[i],pj[i]),make_pair(qi[i],qj[i]));\n wall.insert(p);\n swap(p.first, p.second);\n wall.insert(p);\n }\n \n x = sqrt(2.0 - 2.0*cos(M_PI*2.0/(double)N));\n \n printf(\"%.2f\\n\", dijkstra(si, sj, ti, tj));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1704, "score_of_the_acc": -0.1023, "final_rank": 4 }, { "submission_id": "aoj_2071_538586", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\ntypedef pair<int, int> P;\ntypedef pair<P, P> S;\ntypedef pair<double, P> state;\n\nint main(){\n int N, X;\n while(cin>>N>>X && N){\n set<int> r_set;\n r_set.insert(1); r_set.insert(2);\n set<S> damage;\n int sr, sp, gr, gp;\n cin>>sr>>sp>>gr>>gp;\n sp--; gp--;\n r_set.insert(sr);\n r_set.insert(gr);\n REP(i, X){\n int r1, p1, r2, p2;\n cin>>r1>>p1>>r2>>p2;\n p1--; p2--;\n damage.insert(S(P(r1, p1), P(r2, p2)));\n damage.insert(S(P(r2, p2), P(r1, p1)));\n r_set.insert(r1);\n r_set.insert(r2);\n r_set.insert(r1 + 1);\n r_set.insert(r2 + 1);\n if(r1 >= 2) r_set.insert(r1 - 1);\n else r_set.insert(2);\n if(r2 >= 2) r_set.insert(r2 - 1);\n else r_set.insert(2);\n }\n vector<int> rs(r_set.begin(), r_set.end());\n int R = rs.size();\n //double dist[1000][100] = {};\n //REP(i, R)REP(j, N) dist[i][j] = 1e16;\n bool dist[1000][100] = {};\n int s_ridx = lower_bound(rs.begin(), rs.end(), sr) - rs.begin();\n priority_queue<state, vector<state>, greater<state> > que;\n que.push(state(0, P(s_ridx, sp)));\n while(!que.empty()){\n state s = que.top(); que.pop();\n int r_idx = s.second.first;\n int p = s.second.second;\n double d = s.first;\n //printf(\"(rs[%d]=%d, %d) = %lf\\n\", r_idx, rs[r_idx], p, d);\n if(rs[r_idx] == gr && p == gp){\n printf(\"%.2lf\\n\", d);\n break;\n }\n //if(dist[r_idx][p] < d) continue;\n if(dist[r_idx][p]) continue; dist[r_idx][p] = true;\n REP(r, 4){\n if(r % 2){\n int nr_idx = r_idx + r - 2;\n double nd = d + abs(rs[nr_idx] - rs[r_idx]);\n /*\n if(nr_idx >= 0 && nr_idx < R && dist[nr_idx][p] > nd && !damage.count(S(P(rs[r_idx], p), P(rs[nr_idx], p)))){\n */\n if(nr_idx >= 0 && nr_idx < R && !dist[nr_idx][p] && !damage.count(S(P(rs[r_idx], p), P(rs[nr_idx], p)))){\n //dist[r_idx][np] = nd;\n que.push(state(nd, P(nr_idx, p)));\n }\n }else{\n int np = (p + r - 1 + N) % N;\n double nd = (double)d + 2.0 * rs[r_idx] * sin(M_PI/N);\n /*\n if(dist[r_idx][np] > nd && !damage.count(S(P(rs[r_idx], p), P(rs[r_idx], np)))){\n */\n if(!dist[r_idx][np] && !damage.count(S(P(rs[r_idx], p), P(rs[r_idx], np)))){\n //dist[r_idx][np] = nd;\n que.push(state(nd, P(r_idx, np)));\n }\n }\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1420, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2071_538585", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\ntypedef pair<int, int> P;\ntypedef pair<P, P> S;\ntypedef pair<double, P> state;\n\nint main(){\n int N, X;\n while(cin>>N>>X && N){\n set<int> r_set;\n r_set.insert(1); r_set.insert(2);\n set<S> damage;\n int sr, sp, gr, gp;\n cin>>sr>>sp>>gr>>gp;\n sp--; gp--;\n r_set.insert(sr);\n r_set.insert(gr);\n r_set.insert(sr + 1);\n r_set.insert(gr + 1);\n if(sr != 1) r_set.insert(sr);\n if(gr != 1) r_set.insert(gr);\n REP(i, X){\n int r1, p1, r2, p2;\n cin>>r1>>p1>>r2>>p2;\n p1--; p2--;\n damage.insert(S(P(r1, p1), P(r2, p2)));\n damage.insert(S(P(r2, p2), P(r1, p1)));\n r_set.insert(r1);\n r_set.insert(r2);\n r_set.insert(r1 + 1);\n r_set.insert(r2 + 1);\n if(r1 >= 2) r_set.insert(r1 - 1);\n else r_set.insert(2);\n if(r2 >= 2) r_set.insert(r2 - 1);\n else r_set.insert(2);\n }\n vector<int> rs(r_set.begin(), r_set.end());\n int R = rs.size();\n //double dist[1000][100] = {};\n //REP(i, R)REP(j, N) dist[i][j] = 1e16;\n bool dist[1000][100] = {};\n int s_ridx = lower_bound(rs.begin(), rs.end(), sr) - rs.begin();\n priority_queue<state, vector<state>, greater<state> > que;\n que.push(state(0, P(s_ridx, sp)));\n while(!que.empty()){\n state s = que.top(); que.pop();\n int r_idx = s.second.first;\n int p = s.second.second;\n double d = s.first;\n //printf(\"(rs[%d]=%d, %d) = %lf\\n\", r_idx, rs[r_idx], p, d);\n if(rs[r_idx] == gr && p == gp){\n printf(\"%.2lf\\n\", d);\n break;\n }\n //if(dist[r_idx][p] < d) continue;\n if(dist[r_idx][p]) continue; dist[r_idx][p] = true;\n REP(r, 4){\n if(r % 2){\n int nr_idx = r_idx + r - 2;\n double nd = d + abs(rs[nr_idx] - rs[r_idx]);\n /*\n if(nr_idx >= 0 && nr_idx < R && dist[nr_idx][p] > nd && !damage.count(S(P(rs[r_idx], p), P(rs[nr_idx], p)))){\n */\n if(nr_idx >= 0 && nr_idx < R && !dist[nr_idx][p] && !damage.count(S(P(rs[r_idx], p), P(rs[nr_idx], p)))){\n //dist[r_idx][np] = nd;\n que.push(state(nd, P(nr_idx, p)));\n }\n }else{\n int np = (p + r - 1 + N) % N;\n double nd = (double)d + 2.0 * rs[r_idx] * sin(M_PI/N);\n /*\n if(dist[r_idx][np] > nd && !damage.count(S(P(rs[r_idx], p), P(rs[r_idx], np)))){\n */\n if(!dist[r_idx][np] && !damage.count(S(P(rs[r_idx], p), P(rs[r_idx], np)))){\n //dist[r_idx][np] = nd;\n que.push(state(nd, P(r_idx, np)));\n }\n }\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1420, "score_of_the_acc": -0.0345, "final_rank": 2 }, { "submission_id": "aoj_2071_493711", "code_snippet": "#include<cmath>\n#include<queue>\n#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double INF=1e77;\nconst double PI=acos(-1);\n\nstruct point{\n\tint r,i;\n\tbool operator==(const point &p)const{ return r==p.r && i==p.i; }\n};\n\nstruct edge{ int v; double cost; };\n\nint main(){\n\tfor(int n,m;scanf(\"%d%d\",&n,&m),n||m;){\n\t\tconst double COST=2*sin(PI/n); // 正 n 角形の一辺の長さ\n\n\t\tpoint s,t;\n\t\tscanf(\"%d%d%d%d\",&s.r,&s.i,&t.r,&t.i); s.i--; t.i--;\n\t\tpoint bad[100][2];\n\t\trep(i,m) rep(j,2) scanf(\"%d%d\",&bad[i][j].r,&bad[i][j].i), bad[i][j].i--;\n\n\t\tvector<int> R; // 候補となる頂点の r の値\n\t\tR.push_back(1);\n\t\tR.push_back(s.r);\n\t\tR.push_back(t.r);\n\t\trep(i,m) rep(j,2) {\n\t\t\tR.push_back(bad[i][j].r);\n\t\t\tR.push_back(bad[i][j].r+1);\n\t\t\tR.push_back(max(bad[i][j].r-1,1));\n\t\t}\n\t\tsort(R.begin(),R.end());\n\t\tR.erase(unique(R.begin(),R.end()),R.end());\n\n\t\tint N=n*R.size();\n\t\tstatic vector<edge> G[60300];\n\t\trep(u,N) G[u].clear();\n\t\trep(i,R.size()){\n\t\t\tint r=R[i];\n\t\t\trep(j,n){\n\t\t\t\t// 左\n\t\t\t\tbool ok=true;\n\t\t\t\trep(k,m){\n\t\t\t\t\tif(bad[k][0]==(point){r,j} && bad[k][1]==(point){r,(j-1+n)%n}\n\t\t\t\t\t|| bad[k][1]==(point){r,j} && bad[k][0]==(point){r,(j-1+n)%n}) ok=false;\n\t\t\t\t}\n\t\t\t\tif(ok){\n\t\t\t\t\tint u=n*i+j,v=n*i+(j-1+n)%n;\n\t\t\t\t\tG[u].push_back((edge){v,r*COST});\n\t\t\t\t\tG[v].push_back((edge){u,r*COST});\n\t\t\t\t}\n\n\t\t\t\t// 上\n\t\t\t\tif(i+1<R.size()){\n\t\t\t\t\tok=true;\n\t\t\t\t\tint r2=R[i+1];\n\t\t\t\t\trep(k,m){\n\t\t\t\t\t\tif(bad[k][0]==(point){r,j} && bad[k][1]==(point){r2,j}\n\t\t\t\t\t\t|| bad[k][1]==(point){r,j} && bad[k][0]==(point){r2,j}) ok=false;\n\t\t\t\t\t}\n\t\t\t\t\tif(ok){\n\t\t\t\t\t\tint u=n*i+j,v=n*(i+1)+j;\n\t\t\t\t\t\tG[u].push_back((edge){v,r2-r});\n\t\t\t\t\t\tG[v].push_back((edge){u,r2-r});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Dijkstra\n\t\tint s_id=n*(find(R.begin(),R.end(),s.r)-R.begin())+s.i;\n\t\tint t_id=n*(find(R.begin(),R.end(),t.r)-R.begin())+t.i;\n\t\tstatic double d[60300];\n\t\trep(u,N){\n\t\t\td[u]=(u==s_id?0:INF);\n\t\t}\n\t\tpriority_queue< pair<double,int> > Q; Q.push(make_pair(0,s_id));\n\t\twhile(!Q.empty()){\n\t\t\tdouble d_now=-Q.top().first;\n\t\t\tint u=Q.top().second; Q.pop();\n\n\t\t\tif(d_now>d[u]) continue;\n\n\t\t\trep(i,G[u].size()){\n\t\t\t\tedge e=G[u][i];\n\t\t\t\tif(d[e.v]>d[u]+e.cost){\n\t\t\t\t\td[e.v]=d[u]+e.cost;\n\t\t\t\t\tQ.push(make_pair(-d[e.v],e.v));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tprintf(\"%.9f\\n\",d[t_id]);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 5068, "score_of_the_acc": -0.7044, "final_rank": 6 }, { "submission_id": "aoj_2071_473189", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nconst double PI = acos(-1.0);\nconst double EPS = 1.0e-10;\nconst double INF = DBL_MAX / 1000;\n\nint main()\n{\n int _tmp = 0;\n\n for(;;){\n int n, x;\n cin >> n >> x;\n if(n == 0)\n return 0;\n\n int rStart, iStart, rGoal, iGoal;\n cin >> rStart >> iStart >> rGoal >> iGoal;\n \n vector<int> r1(x), i1(x), r2(x), i2(x);\n for(int j=0; j<x; ++j)\n cin >> r1[j] >> i1[j] >> r2[j] >> i2[j];\n\n // スタート、ゴール、失われた辺が位置する多角形と、その前後の多角形のみに注目\n // また、一番中央にある多角形にも注目する\n map<int, int> rIndex;\n rIndex[1];\n for(int j=0; j<3; ++j){\n rIndex[rStart - 1 + j];\n rIndex[rGoal - 1 + j];\n }\n for(int j=0; j<x; ++j){\n for(int k=0; k<3; ++k){\n rIndex[r1[j] - 1 + k];\n rIndex[r2[j] - 1 + k];\n }\n }\n if(rIndex.begin()->first == 0)\n rIndex.erase(rIndex.begin());\n\n int m = 0; // 注目する多角形の数\n vector<int> rPos; // 注目する多角形の位置\n for(map<int, int>::iterator it=rIndex.begin(); it!=rIndex.end(); ++it){\n it->second = m;\n ++ m;\n rPos.push_back(it->first);\n }\n\n set<pair<pair<int, int>, pair<int, int> > > damage;\n for(int j=0; j<x; ++j){\n damage.insert(make_pair(make_pair(rIndex[r1[j]], i1[j]), make_pair(rIndex[r2[j]], i2[j])));\n damage.insert(make_pair(make_pair(rIndex[r2[j]], i2[j]), make_pair(rIndex[r1[j]], i1[j])));\n }\n\n vector<vector<double> > minDist(m, vector<double>(n+1, INF));\n minDist[rIndex[rStart]][iStart] = 0.0;\n multimap<double, pair<int, int> > mm;\n mm.insert(make_pair(0.0, make_pair(rIndex[rStart], iStart)));\n\n for(;;){\n double dist0 = mm.begin()->first;\n int r0 = mm.begin()->second.first;\n int i0 = mm.begin()->second.second;\n mm.erase(mm.begin());\n if(dist0 > minDist[r0][i0] + EPS)\n continue;\n\n if(r0 == rIndex[rGoal] && i0 == iGoal){\n printf(\"%.10f\\n\", dist0);\n break;\n }\n\n for(int j=0; j<4; ++j){\n double dist = dist0;\n int r = r0;\n int i = i0;\n if(j % 2 == 0){\n r = r - 1 + j;\n if(r == -1 || r == m)\n continue;\n dist += abs(rPos[r] - rPos[r0]);\n }else{\n i = (i - 3 + j + n) % n + 1;\n dist += rPos[r] * sin(PI / n) * 2.0;\n }\n\n if(damage.find(make_pair(make_pair(r0, i0), make_pair(r, i))) != damage.end())\n continue;\n\n if(dist < minDist[r][i] - EPS){\n minDist[r][i] = dist;\n mm.insert(make_pair(dist, make_pair(r, i)));\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1588, "score_of_the_acc": -0.0542, "final_rank": 3 } ]