public static void Solve() { var(H, W) = Scanner.Scan <int, int>(); var A = new int[H][]; var B = new int[H][]; for (var i = 0; i < 2; i++) { var C = i % 2 == 0 ? A : B; for (var j = 0; j < H; j++) { C[j] = Scanner.ScanEnumerable <int>().ToArray(); } } var answer = 0L; for (var i = 0; i + 1 < H; i++) { for (var j = 0; j + 1 < W; j++) { var d = B[i][j] - A[i][j]; A[i][j] += d; A[i + 1][j] += d; A[i][j + 1] += d; A[i + 1][j + 1] += d; answer += Math.Abs(d); } } var ok = true; for (var i = 0; i < H; i++) { ok &= A[i][W - 1] == B[i][W - 1]; } for (var i = 0; i < W; i++) { ok &= A[H - 1][i] == B[H - 1][i]; } if (ok) { Console.WriteLine("Yes"); Console.WriteLine(answer); } else { Console.WriteLine("No"); } }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); var answer = new long[N]; answer[0] = A.Select((x, i) => i % 2 == 0 ? x : -x).Sum(); for (var i = 1; i < N; i++) { answer[i] = A[i - 1] * 2 - answer[i - 1]; } Console.WriteLine(string.Join(" ", answer)); }
public static void Solve() { var(N, Z, W) = Scanner.Scan <int, int, int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); if (N == 1) { Console.WriteLine(Math.Abs(A[0] - W)); } else { Console.WriteLine(Math.Max(Math.Abs(A[N - 1] - W), Math.Abs(A[N - 2] - A[N - 1]))); } }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); Array.Sort(A, (x, y) => y.CompareTo(x)); var answer = 0L; for (int i = 0; i < N; i += 2) { answer += A[i]; } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); var xor = A.Aggregate((current, item) => current ^ item); var answer = new long[N]; for (var i = 0; i < N; i++) { answer[i] = xor ^ A[i]; } Console.WriteLine(string.Join(" ", answer)); }
public static void Solve() { var(N, K) = Scanner.Scan <int, int>(); var P = Scanner.ScanEnumerable <double>().ToArray(); P = CumulativeItems(P.Select(x => x + 1)).ToArray(); var answer = 0.0; for (var i = 0; i <= N - K; i++) { answer = Math.Max(answer, (P[i + K] - P[i]) / 2); } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = new int[N][].Select(_ => Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray()).ToArray(); var Q = new Queue <int> [N]; for (var i = 0; i < N; i++) { Q[i] = new Queue <int>(A[i]); } var answer = 0; var prev = new HashSet <int>(Enumerable.Range(0, N)); while (true) { var current = new HashSet <int>(); foreach (var x in prev) { if (!Q[x].Any()) { continue; } var y = Q[x].Peek(); if (current.Contains(x) || current.Contains(y)) { continue; } if (Q[y].Peek() != x) { continue; } Q[x].Dequeue(); Q[y].Dequeue(); current.Add(x); current.Add(y); } prev = current; if (!current.Any()) { break; } answer++; } if (Q.Any(x => x.Any())) { answer = -1; } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var S = Scanner.ScanEnumerable <int>().ToArray(); var A = Scanner.ScanEnumerable <int>().ToArray(); var answer = 0; for (var i = 0; i < N; i++) { answer = Math.Max(answer, S[i] * A[i]); } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); var X = Scanner.Scan <long>(); var cum = new long[N + 1]; for (var i = 0; i < N; i++) { cum[i + 1] = cum[i] + A[i]; } var d = X / cum[^ 1]; var rem = X - d * cum[^ 1];
public static void Solve() { var(N, T) = Scanner.Scan <int, int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); for (var i = 1; i < N; i++) { while (A[i] <= A[i - 1]) { A[i] += T; } } var answer = A[^ 1];
public static void Solve() { var A = Scanner.ScanEnumerable <int>().ToArray(); var mid = A.Sum() - A.Min() - A.Max(); for (var i = 0; i < A.Length; i++) { if (mid == A[i]) { Console.WriteLine((char)('A' + i)); return; } } }
public static void Solve() { var N = Scanner.Scan <int>(); var P = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray(); var Q = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray(); var dsu = new DisjointSetUnion(N); foreach (var(p, q) in P.Zip(Q)) { dsu.Merge(p, q); } var f = new Dictionary <int, mint>(); f[1] = 2; f[2] = 3; mint F(int n) { if (f.ContainsKey(n)) { return(f[n]); } return(f[n] = F(n - 1) + F(n - 2)); } var g = new Dictionary <int, mint>(); g[1] = 1; g[2] = 3; g[3] = 4; mint G(int n) { if (g.ContainsKey(n)) { return(g[n]); } return(g[n] = F(n - 1) + F(n - 3)); } mint answer = 1; foreach (var group in dsu.GetGroups()) { answer *= G(group.Count); } Console.WriteLine(answer); }
public static void Solve() { var(N, K) = Scanner.Scan <int, int>(); var G = new int[K, N]; for (var i = 0; i < N; i++) { var A = Scanner.ScanEnumerable <int>().ToArray(); for (var j = 0; j < K; j++) { G[j, i] = A[j]; } } }
public static void Solve() { var N = Scanner.Scan <int>(); var A = new int[N * 2, N * 2]; for (var i = 0; i < N * 2 - 1; i++) { var AA = Scanner.ScanEnumerable <int>().ToArray(); for (var j = 0; j < AA.Length; j++) { A[i, j + i + 1] = A[j + i + 1, i] = AA[j]; } } var answer = 0; var used = new bool[N * 2]; void Dfs(int curr, int xor) { if (curr >= N * 2) { if (used.All(x => x)) { answer = Math.Max(answer, xor); } return; } if (!used[curr]) { for (var next = curr + 1; next < N * 2; next++) { if (!used[next]) { used[curr] = used[next] = true; Dfs(curr + 1, xor ^ A[curr, next]); used[curr] = used[next] = false; } } } else { Dfs(curr + 1, xor); } } Dfs(0, 0); Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var C = Scanner.ScanEnumerable <int>().OrderByDescending(x => x).Select(x => (ModuloInteger)x).ToArray(); ModuloInteger answer = 0; for (var i = 1; i <= N; i++) { answer += C[i - 1] * (i + 1); } answer *= ModuloInteger.Power(4, N - 1); Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); var min = 1001; var max = 0; for (var i = 0; i < A.Length; i++) { max = Math.Max(max, A[i]); min = Math.Min(min, A[i]); } Console.WriteLine(max - min); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = new long[N][]; mint answer = 1; for (var i = 0; i < N; i++) { A[i] = Scanner.ScanEnumerable <long>().ToArray(); answer *= A[i].Sum(); } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); var answer = 0; var counts = new[] { 0, 0, 1, 0, 1, 2, 3, 0, 1, 0 }; foreach (var x in A) { answer += counts[x]; } Console.WriteLine(answer); }
public static void Solve() { var(N, M) = Scanner.Scan <int, int>(); var S = Scanner.ScanEnumerable <string>().ToArray(); var T = Scanner.ScanEnumerable <string>().ToArray(); var queue = new Queue <string>(T); foreach (var s in S) { if (queue.TryPeek(out var t) && s == t) { queue.Dequeue(); Console.WriteLine("Yes"); }
public static void Solve() { var G = new char[4][]; for (var i = 0; i < 4; i++) { G[i] = Scanner.ScanEnumerable <char>().ToArray();; } for (var i = 0; i < 4; i++) { Console.WriteLine(string.Join(" ", (G[3 - i].Reverse().ToArray()))); } }
public static void Solve() { var P = Scanner.ScanEnumerable <int>().ToArray(); var builder = new StringBuilder(); foreach (var x in P) { builder.Append((char)('a' + x - 1)); } var answer = builder.ToString(); Console.WriteLine(answer); }
public static void Solve() { var(N, Q) = Scanner.Scan <int, int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); Array.Sort(A); while (Q-- > 0) { var x = Scanner.Scan <long>(); var answer = LowerBound(A, x); Console.WriteLine(N - answer); } }
public static void Solve() { var(N, L) = Scanner.Scan <int, long>(); var K = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <long>().ToList(); A.Insert(0, 0); A.Add(L); bool Check(long m) { var(l, r) = (0, 0); var list = new List <long>(); while (l < A.Count && r < A.Count) { while (r < A.Count && A[r] - A[l] < m) { r++; } if (r < A.Count && A[r] - A[l] >= m) { list.Add(A[r] - A[l]); } l = r; } return(list.Count > K); } var(l, r) = (0L, L); while (r - l > 1) { var m = (l + r) / 2; if (Check(m)) { l = m; } else { r = m; } } var answer = l; Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); var dict = new Dictionary <long, long>(); foreach (var a in A) { if (!dict.ContainsKey(a)) { dict[a] = 0; } dict[a]++; } Array.Sort(A, (x, y) => y.CompareTo(x)); var answer = 0L; foreach (var a in A) { for (var t = 32; t >= 1; t--) { var b = (1L << t) - a; if (b <= 0 || !dict.ContainsKey(b)) { continue; } if (b == a) { if (dict[b] > 1) { dict[b] -= 2; answer++; } } else { if (dict[a] > 0 && dict[b] > 0) { dict[a]--; dict[b]--; answer++; } } } } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var C = Scanner.ScanEnumerable <int>().ToArray(); var G = new List <int> [N].Select(x => new List <int>()).ToArray(); for (var i = 0; i < N - 1; i++) { var(a, b) = Scanner.Scan <int, int>(); a--; b--; G[a].Add(b); G[b].Add(a); } var ok = new bool[N]; Array.Fill(ok, true); var used = new int[(int)1e5 + 1]; void Dfs(int u, int p) { if (used[C[u]] > 0) { ok[u] = false; } used[C[u]]++; foreach (var v in G[u]) { if (v == p) { continue; } Dfs(v, u); } used[C[u]]--; } Dfs(0, -1); for (var i = 0; i < N; i++) { if (ok[i]) { Console.WriteLine(i + 1); } } }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); var B = new List <int>(); var prev = 0; foreach (var a in A) { if (a == prev) { continue; } B.Add(a); prev = a; } N = B.Count; var answer = 1; var isInc = N <= 1 || B[1] >= B[0]; for (var i = 1; i < N; i++) { if (isInc) { if (B[i] < B[i - 1]) { if (i + 1 < N) { isInc = B[i + 1] >= B[i]; } answer++; } } else { if (B[i] > B[i - 1]) { if (i + 1 < N) { isInc = B[i + 1] >= B[i]; } answer++; } } } Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <int>().ToArray(); var ok = true; foreach (var a in A.Where(x => x % 2 == 0)) { if (a % 3 != 0 && a % 5 != 0) { ok = false; } } Console.WriteLine(ok ? "APPROVED" : "DENIED"); }
public static void Solve() { var N = Scanner.Scan <int>(); var F = Scanner.ScanEnumerable <int>().ToArray(); var dsu = new DisjointSetUnion(N); for (var i = 0; i < N; i++) { dsu.Merge(i, F[i] - 1); } var answer = mint.Power(2, dsu.GetGroups().Count()) - 1; Console.WriteLine(answer); }
public static void Solve() { var N = Scanner.Scan <int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); var sum = 0L; for (var i = 1; i < N; i++) { sum += A[i] - A[i - 1]; } var answer = (decimal)sum / (N - 1); Console.WriteLine(answer); }
public static void Solve() { var(N, K) = Scanner.Scan <int, int>(); var A = Scanner.ScanEnumerable <long>().ToArray(); Array.Sort(A); var answer = 0L; for (var i = 0; i < K; i++) { answer += A[i] + i; } Console.WriteLine(answer); }