diff --git a/1899/B.java b/1899/B.java new file mode 100644 index 0000000..60aa93f --- /dev/null +++ b/1899/B.java @@ -0,0 +1,187 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Random; +import java.util.StringTokenizer; + +class Solution { + void solve() { + var n = fs.nextInt(); + var a = fs.readArray(n); + + var prefix = new long[n + 2]; + for (int i = 0; i < n; ++i) { + prefix[i + 1] = prefix[i] + a[i]; + } + + long max = 0; + for (int k = 1; n / k > 1; ++k) { + if (n % k != 0) { + continue; + } + + long _min = Long.MAX_VALUE, _max = Long.MIN_VALUE; + for (int i = 0, count = n / k; i < count; ++i) { + long truck = prefix[(i + 1) * k] - prefix[i * k]; + + if (truck < _min) { + _min = truck; + } + if (truck > _max) { + _max = truck; + } + } + + if (_max - _min > max) { + max = _max - _min; + } + } + + answer(max); + } + + // region runner + private static final boolean SINGLE = false; + void run() { + if (SINGLE) { + solve(); + } else { + int N = fs.nextInt(); + for (int i = 0; i < N; ++i) { + solve(); + } + } + + out.close(); + } + + public static void main(String[] args) { + new Solution().run(); + } + // endregion runner + + // region math + static final Random random = new Random(); + static final int mod = 1_000_000_007; + + static long add(long a, long b) { + return (a + b) % mod; + } + + static long sub(long a, long b) { + return ((a - b) % mod + mod) % mod; + } + + static long mul(long a, long b) { + return (a * b) % mod; + } + + static long exp(long base, long exp) { + if (exp == 0) + return 1; + long half = exp(base, exp / 2); + if (exp % 2 == 0) + return mul(half, half); + return mul(half, mul(half, base)); + } + + static long[] factorials = new long[2_000_001]; + static long[] invFactorials = new long[2_000_001]; + + static void precompFacts() { + factorials[0] = invFactorials[0] = 1; + for (int i = 1; i < factorials.length; i++) + factorials[i] = mul(factorials[i - 1], i); + invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); + for (int i = invFactorials.length - 2; i >= 0; i--) + invFactorials[i] = mul(invFactorials[i + 1], i + 1); + } + + static long nCk(int n, int k) { + return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); + } + // endregion math + + // region sorts + static void ruffleSort(int[] a) { + int n = a.length;// shuffle, then sort + for (int i = 0; i < n; i++) { + int oi = random.nextInt(n), temp = a[oi]; + a[oi] = a[i]; + a[i] = temp; + } + Arrays.sort(a); + } + + static void sort(int[] a) { + ArrayList l = new ArrayList<>(); + for (int i : a) + l.add(i); + Collections.sort(l); + for (int i = 0; i < a.length; i++) + a[i] = l.get(i); + } + // endregion sorts + + // region output + private PrintWriter out = new PrintWriter(System.out); + + private void yes() { + out.println("YES"); + } + + private void no() { + out.println("NO"); + } + + private void yesno(boolean ans) { + out.println(ans ? "YES" : "NO"); + } + + private void answer(boolean ans) { + yesno(ans); + } + + private void answer(T ans) { + out.println(ans); + } + // endregion output + + // region input + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + private FastScanner fs = new FastScanner(); + // endregion input + +} diff --git a/1899/E.java b/1899/E.java new file mode 100644 index 0000000..ed10944 --- /dev/null +++ b/1899/E.java @@ -0,0 +1,175 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Random; +import java.util.StringTokenizer; + +class Solution { + void solve() { + var n = fs.nextInt(); + var a = fs.readArray(n); + + // find the minimum + int min_idx = 0; + for (int i = 1; i < a.length; ++i) { + if (a[i] < a[min_idx]) { + min_idx = i; + } + } + + // check the rest of the array + for (int i = min_idx + 1; i < a.length; ++i) { + if (a[i - 1] > a[i]) { + answer(-1); + return; + } + } + + answer(min_idx); + } + + // region runner + private static final boolean SINGLE = false; + void run() { + if (SINGLE) { + solve(); + } else { + int N = fs.nextInt(); + for (int i = 0; i < N; ++i) { + solve(); + } + } + + out.close(); + } + + public static void main(String[] args) { + new Solution().run(); + } + // endregion runner + + // region math + static final Random random = new Random(); + static final int mod = 1_000_000_007; + + static long add(long a, long b) { + return (a + b) % mod; + } + + static long sub(long a, long b) { + return ((a - b) % mod + mod) % mod; + } + + static long mul(long a, long b) { + return (a * b) % mod; + } + + static long exp(long base, long exp) { + if (exp == 0) + return 1; + long half = exp(base, exp / 2); + if (exp % 2 == 0) + return mul(half, half); + return mul(half, mul(half, base)); + } + + static long[] factorials = new long[2_000_001]; + static long[] invFactorials = new long[2_000_001]; + + static void precompFacts() { + factorials[0] = invFactorials[0] = 1; + for (int i = 1; i < factorials.length; i++) + factorials[i] = mul(factorials[i - 1], i); + invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); + for (int i = invFactorials.length - 2; i >= 0; i--) + invFactorials[i] = mul(invFactorials[i + 1], i + 1); + } + + static long nCk(int n, int k) { + return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); + } + // endregion math + + // region sorts + static void ruffleSort(int[] a) { + int n = a.length;// shuffle, then sort + for (int i = 0; i < n; i++) { + int oi = random.nextInt(n), temp = a[oi]; + a[oi] = a[i]; + a[i] = temp; + } + Arrays.sort(a); + } + + static void sort(int[] a) { + ArrayList l = new ArrayList<>(); + for (int i : a) + l.add(i); + Collections.sort(l); + for (int i = 0; i < a.length; i++) + a[i] = l.get(i); + } + // endregion sorts + + // region output + private PrintWriter out = new PrintWriter(System.out); + + private void yes() { + out.println("YES"); + } + + private void no() { + out.println("NO"); + } + + private void yesno(boolean ans) { + out.println(ans ? "YES" : "NO"); + } + + private void answer(boolean ans) { + yesno(ans); + } + + private void answer(T ans) { + out.println(ans); + } + // endregion output + + // region input + static class FastScanner { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(""); + + String next() { + while (!st.hasMoreTokens()) + try { + st = new StringTokenizer(br.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + return st.nextToken(); + } + + int nextInt() { + return Integer.parseInt(next()); + } + + int[] readArray(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) + a[i] = nextInt(); + return a; + } + + long nextLong() { + return Long.parseLong(next()); + } + } + private FastScanner fs = new FastScanner(); + // endregion input + +} diff --git a/1899/a.cpp b/1899/a.cpp new file mode 100644 index 0000000..61b0a7f --- /dev/null +++ b/1899/a.cpp @@ -0,0 +1,318 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma region helpers + +#pragma region aliases +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +template +using V = std::vector; + +template +using M = std::map; + +template +using S = std::set; + +template +using Q = std::deque; + +using i8 = std::int8_t; +using u8 = std::uint8_t; +using i16 = std::int16_t; +using u16 = std::uint16_t; +using i32 = std::int32_t; +using u32 = std::uint32_t; +using i64 = std::int64_t; +using u64 = std::uint64_t; +#pragma endregion /* aliases */ + +#pragma region data structures +template +struct max_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end()); + } + + T pop() { + std::pop_heap(h.begin(), h.end()); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; + +template +struct min_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end(), std::greater<>{}); + } + + T pop() { + std::pop_heap(h.begin(), h.end(), std::greater<>{}); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; +#pragma endregion /* data structures */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#pragma region input +template +void collect(Container &c, std::size_t size) { + auto it = std::inserter(c, c.begin()); + + for (auto i = 0u; i < size; ++i) { + typename Container::value_type x; + std::cin >> x; + it = std::move(x); + } +} + +template +Container collect(std::size_t size) { + Container c{}; + collect(c, size); + return c; +} +#pragma endregion /* input */ + +#pragma region math +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} + +template +T isqrt(T x) { + assert(x >= 0); + + auto max_shift = 8 * sizeof(T) - 1; + auto shift = (max_shift - std::countl_zero(x)) & ~1; + auto bit = 1 << shift; + + T result = 0; + while (bit != 0) { + if (x >= (result + bit)) { + x -= result + bit; + result = (result >> 1) + bit; + } else { + result = (result >> 1); + } + bit = bit >> 2; + } + + return result; +} + +template +struct Z { + Z(std::int64_t x = 0) : x(x % MODULO) {} + + Z pow(std::uint32_t exp) const { + auto ans = 1; + auto base = x; + + for (; exp > 0; exp >>= 1) { + if (exp % 2 == 1) { + ans *= base; + } + + base *= base; + } + + return ans; + } + + Z inv() const { + assert(x != 0); + return pow(MODULO - 2); + } + + Z operator-() const { return {-x}; } + Z operator+=(const Z &rhs) { x = (x + rhs.x) % MODULO; } + Z operator-=(const Z &rhs) { x = (x - rhs.x) % MODULO; } + Z operator*=(const Z &rhs) { x = (x * rhs.x) % MODULO; } + Z operator/=(const Z &rhs) { x = (x * rhs.inv().x) % MODULO; } + + friend Z operator+(Z lhs, const Z &rhs) { return lhs += rhs; } + friend Z operator-(Z lhs, const Z &rhs) { return lhs -= rhs; } + friend Z operator*(Z lhs, const Z &rhs) { return lhs *= rhs; } + friend Z operator/(Z lhs, const Z &rhs) { return lhs /= rhs; } + + friend std::istream &operator>>(std::istream &is, Z &z) { + is >> z.x; + z.x %= MODULO; + return is; + } + + friend std::ostream &operator<<(std::ostream &os, const Z &z) { + return os << z.x; + } + + private: + std::int64_t x; +}; +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { + std::cout << ans << "\n"; +} + +inline void yes() { std::cout << "YES\n"; } +inline void no() { std::cout << "NO\n"; } +inline void yesno(bool ans) { std::cout << (ans ? "YES" : "NO") << "\n"; } +#pragma endregion /* output */ + +#pragma region rng +std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); +#pragma endregion /* rng */ + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +void solve() { + int x; + cin >> x; + + if (x % 3 != 0) { + answer("First"); + } else { + answer("Second"); + } +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif diff --git a/1899/c.cpp b/1899/c.cpp new file mode 100644 index 0000000..cb97a24 --- /dev/null +++ b/1899/c.cpp @@ -0,0 +1,329 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma region helpers + +#pragma region aliases +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +template +using V = std::vector; + +template +using M = std::map; + +template +using S = std::set; + +template +using Q = std::deque; + +using i8 = std::int8_t; +using u8 = std::uint8_t; +using i16 = std::int16_t; +using u16 = std::uint16_t; +using i32 = std::int32_t; +using u32 = std::uint32_t; +using i64 = std::int64_t; +using u64 = std::uint64_t; +#pragma endregion /* aliases */ + +#pragma region data structures +template +struct max_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end()); + } + + T pop() { + std::pop_heap(h.begin(), h.end()); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; + +template +struct min_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end(), std::greater<>{}); + } + + T pop() { + std::pop_heap(h.begin(), h.end(), std::greater<>{}); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; +#pragma endregion /* data structures */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#pragma region input +template +void collect(Container &c, std::size_t size) { + auto it = std::inserter(c, c.begin()); + + for (auto i = 0u; i < size; ++i) { + typename Container::value_type x; + std::cin >> x; + it = std::move(x); + } +} + +template +Container collect(std::size_t size) { + Container c{}; + collect(c, size); + return c; +} +#pragma endregion /* input */ + +#pragma region math +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} + +template +T isqrt(T x) { + assert(x >= 0); + + auto max_shift = 8 * sizeof(T) - 1; + auto shift = (max_shift - std::countl_zero(x)) & ~1; + auto bit = 1 << shift; + + T result = 0; + while (bit != 0) { + if (x >= (result + bit)) { + x -= result + bit; + result = (result >> 1) + bit; + } else { + result = (result >> 1); + } + bit = bit >> 2; + } + + return result; +} + +template +struct Z { + Z(std::int64_t x = 0) : x(x % MODULO) {} + + Z pow(std::uint32_t exp) const { + auto ans = 1; + auto base = x; + + for (; exp > 0; exp >>= 1) { + if (exp % 2 == 1) { + ans *= base; + } + + base *= base; + } + + return ans; + } + + Z inv() const { + assert(x != 0); + return pow(MODULO - 2); + } + + Z operator-() const { return {-x}; } + Z operator+=(const Z &rhs) { x = (x + rhs.x) % MODULO; } + Z operator-=(const Z &rhs) { x = (x - rhs.x) % MODULO; } + Z operator*=(const Z &rhs) { x = (x * rhs.x) % MODULO; } + Z operator/=(const Z &rhs) { x = (x * rhs.inv().x) % MODULO; } + + friend Z operator+(Z lhs, const Z &rhs) { return lhs += rhs; } + friend Z operator-(Z lhs, const Z &rhs) { return lhs -= rhs; } + friend Z operator*(Z lhs, const Z &rhs) { return lhs *= rhs; } + friend Z operator/(Z lhs, const Z &rhs) { return lhs /= rhs; } + + friend std::istream &operator>>(std::istream &is, Z &z) { + is >> z.x; + z.x %= MODULO; + return is; + } + + friend std::ostream &operator<<(std::ostream &os, const Z &z) { + return os << z.x; + } + + private: + std::int64_t x; +}; +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { + std::cout << ans << "\n"; +} + +inline void yes() { std::cout << "YES\n"; } +inline void no() { std::cout << "NO\n"; } +inline void yesno(bool ans) { std::cout << (ans ? "YES" : "NO") << "\n"; } +#pragma endregion /* output */ + +#pragma region rng +std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); +#pragma endregion /* rng */ + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +void solve() { + size_t n; + cin >> n; + + auto a = collect>(n); + + auto m = *max_element(a.begin(), a.end()); + auto k = a[0]; + + for (size_t i = 1; i < n; ++i) { + if (abs(a[i - 1] % 2) == abs(a[i] % 2)) { + k = a[i]; + } else { + k = max(k + a[i], a[i]); + } + + m = max(m, k); + } + + answer(m); +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif