public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int T = sc.NextInt(); while (T-- > 0) { int N = sc.NextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = sc.NextInt(); } int min = a.Min(); int[] solutions = new int[5]; for (int i = 0; i < N; i++) { int diff = a[i] - min; solutions[0] += diff / 5 + (diff % 5) / 2 + (diff % 5) % 2; solutions[1] += (diff + 1) / 5 + ((diff + 1) % 5) / 2 + ((diff + 1) % 5) % 2; solutions[2] += (diff + 2) / 5 + ((diff + 2) % 5) / 2 + ((diff + 2) % 5) % 2; solutions[3] += (diff + 3) / 5 + ((diff + 3) % 5) / 2 + ((diff + 3) % 5) % 2; solutions[4] += (diff + 4) / 5 + ((diff + 4) % 5) / 2 + ((diff + 4) % 5) % 2; } Console.WriteLine(solutions.Min()); } }
// N ^ 2 public static void RunDP() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); int[] a = new int[n]; int[] dp = new int[n]; dp[0] = 1; for (int i = 0; i < n; i++) { a[i] = sc.NextInt(); if (i > 0) { int ind = -1, len = 0; for (int j = i - 1; j >= 0; j--) { if (a[j] < a[i] && dp[j] > len) { len = dp[j]; ind = j; } } dp[i] = (ind == -1) ? 1 : dp[ind] + 1; } } Console.WriteLine(dp.Max()); }
public static void Run() { ConsoleScanner cs = new ConsoleScanner(); int T = cs.NextInt(); while (T-- > 0) { int N = cs.NextInt(); long[] sum = new long[N], a = new long[N]; int[] index = new int[N]; for (int i = 0; i < N; i++) { a[i] = cs.NextInt(); index[i] = i; if (i > 0) sum[i] = sum[i - 1] + a[i]; else sum[0] = a[0]; } Array.Sort(a, index); long res = 0; int left = 0; for (int i = N - 1; i >= 0; i--) { int maxIndex = index[i]; if (maxIndex >= left) { res += a[i] * (maxIndex - left) - (maxIndex > 0 ? sum[maxIndex - 1] : 0) + (left > 0 ? sum[left - 1] : 0); left = maxIndex + 1; } } Console.WriteLine(res); } }
public static void Run() { ConsoleScanner cs = new ConsoleScanner(); int n = cs.NextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = cs.NextInt(); } a = a.OrderBy(x => x).ToArray(); long len = 0; int prev = int.MaxValue; for (int i = n - 1; i >= 0; i--) { if (a[i] < prev) { len += a[i]; prev = a[i]; if (prev == 1) break; } else { len += prev - 1; prev--; if (prev == 1) break; } } Console.WriteLine(len); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); Solve(n); Console.WriteLine(_sb); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); int min = int.MaxValue, oddCount = 0; long sum = 0; while (n-- > 0) { int t = sc.NextInt(); if (t % 2 == 1) { oddCount++; min = Math.Min(min, t); } sum += t; } if (oddCount % 2 == 1) sum -= min; Console.WriteLine(sum); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); long[] a = new long[1999], b = new long[1999]; while (n-- > 0) { int i = sc.NextInt(), j = sc.NextInt(); a[(j - i) + 999]++; b[(1001 - j) - i + 999]++; } long sum = 0; for (int i = 0; i < 1999; i++) { sum += (a[i] * (a[i] - 1)) / 2; sum += (b[i] * (b[i] - 1)) / 2; } Console.WriteLine(sum); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(), p = sc.NextInt(); long[] l = new long[n], r = new long[n]; for (int i = 0; i < n; i++) { l[i] = sc.NextInt(); r[i] = sc.NextInt(); } double expected = 0; for (int i = 0; i < n; i++) { int j = (i + 1) % n; long pi = r[i] / p - (l[i] - 1) / p, pj = r[j] / p - (l[j] - 1) / p; double e = 1 - ((r[i] - l[i] + 1 - pi) * (r[j] - l[j] + 1 - pj) * 1.0) / ((r[i] - l[i] + 1) * (r[j] - l[j] + 1) * 1.0); expected += e * 2e3; } Console.WriteLine(expected.ToString().Replace(",", ".")); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(), m = sc.NextInt(), k = sc.NextInt(), blockSize = (int)Math.Ceiling(Math.Sqrt(n)); int[] a = new int[n], pref = new int[n], count = new int[(int)2e6]; Query[] queries = new Query[m]; long[] offlineAnswers = new long[m]; for (int i = 0; i < n; i++) { a[i] = sc.NextInt(); if (i == 0) pref[i] = a[i]; else pref[i] = a[i] ^ pref[i - 1]; } for (int i = 0; i < m; i++) { queries[i] = new Query { Id = i, Left = sc.NextInt() - 1, Right = sc.NextInt() - 1 }; queries[i].Block = queries[i].Left / blockSize; } queries = queries.OrderBy(q => q.Block).ThenBy(q => q.Right).ToArray(); int wL = 0, wR = 1; count[pref[0]]++; long ans = count[pref[1] ^ k]; count[pref[1]]++; // Mo's Algorithm foreach (Query query in queries) { while (wR < query.Right) { wR++; ans += count[pref[wR] ^ k]; count[pref[wR]]++; } while (wR > query.Right) { count[pref[wR]]--; ans -= count[pref[wR] ^ k]; wR--; } while (wL < query.Left) { count[pref[wL]]--; ans -= count[pref[wL] ^ k]; wL++; } while (wL > query.Left) { wL--; ans += count[pref[wL] ^ k]; count[pref[wL]]++; } offlineAnswers[query.Id] = ans + count[(wL > 0 ? pref[wL - 1] : 0) ^ k]; } foreach (long res in offlineAnswers) { Console.WriteLine(res); } }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); int[,] a = new int[n, n]; int[] p = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i, j] = sc.NextInt(); } } int[] rem = new int[2]; int c = 0; for (int i = 0; i < n; i++) { int[] count = new int[n + 1]; bool found = false; for (int j = 0; j < n; j++) { if (i == j) continue; count[a[i, j]]++; if (count[a[i, j]] == 2) { p[i] = a[i, j]; found = true; break; } } if (!found) rem[c++] = i; } p[rem[0]] = n - 1; p[rem[1]] = n; foreach (int i in p) { Console.Write(i + " "); } }
// NlogN public static void RunNlogN() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); int[] a = new int[n], tails = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.NextInt(); } tails[0] = a[0]; int spot = 1; for (int i = 1; i < n; i++) { if (tails[0] >= a[i]) tails[0] = a[i]; else if (tails[spot - 1] <= a[i]) { if (tails[spot - 1] < a[i]) tails[spot++] = a[i]; } else tails[searchSpot(tails, 0, spot - 1, a[i])] = a[i]; } Console.WriteLine(spot); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(), k = sc.NextInt(); int left = 1, right = n * (k - 1) + 1, ksum = 0; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < k - 1) sb.Append(left++ + " "); else { if (j == k - 1) ksum += right; sb.Append(right++ + " "); } } sb.Append("\n"); } Console.WriteLine(ksum); Console.Write(sb); }
// Not solved yet! Should throw OutOfMemoryException public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(), m = sc.NextInt(); int[] a = new int[n]; int[] pref = new int[(int)3e6]; for (int i = 0; i < n; i++) { a[i] = sc.NextInt(); } for (int i = 1; i < pref.Length; i++) { pref[i] = i ^ pref[i - 1]; } int[,] dp = new int[n, n]; for (int r = 0; r < n; r++) { for (int l = r; l >= 0; l--) { if (l == r) dp[l, r] = a[l]; else { int xor1 = 0; if (a[l] <= a[r]) xor1 = pref[a[r]] ^ (a[l] > 0 ? pref[a[l] - 1] : 0); int xor2 = 0; if (a[r] < a[l]) xor2 = pref[a[l]] ^ (a[r] > 0 ? pref[a[r] - 1] : 0); dp[l, r] = Math.Max(dp[l, r - 1], Math.Max(dp[l + 1, r], Math.Max(xor1, xor2))); } } } StringBuilder sb = new StringBuilder(); while (m-- > 0) { int l = sc.NextInt() - 1, r = sc.NextInt() - 1; sb.Append(dp[l, r] + "\n"); } Console.WriteLine(sb); }
public static void Run() { ConsoleScanner cs = new ConsoleScanner(); int n = cs.NextInt(); int[] ratings = new int[n]; int[] candies = new int[n]; for (int i = 0; i < n; i++) { ratings[i] = cs.NextInt(); } for (int i = 0; i < n; i++) { if (i > 0 && i < n - 1) { if (ratings[i] <= ratings[i - 1] && ratings[i] <= ratings[i + 1]) candies[i] = 1; } else if (n > 1) { if (i == 0 && ratings[i] <= ratings[i + 1] || i == n - 1 && ratings[i] <= ratings[i - 1]) candies[i] = 1; } else candies[i] = 1; } for (int i = 1; i < n; i++) { if (ratings[i] > ratings[i - 1]) candies[i] = candies[i - 1] + 1; } for (int i = n - 2; i >= 0; i--) { if (ratings[i] > ratings[i + 1]) candies[i] = Math.Max(candies[i], candies[i + 1] + 1); } long sum = 0; for (int i = 0; i < n; i++) { sum += candies[i]; } Console.WriteLine(sum); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); string str = sc.ReadLine(); int[] altSeq = new int[n], revAltSeq = new int[n]; char current = str[0]; int index = 0; altSeq[0] = index; for (int i = 1; i < n; i++) { if (str[i] == current) altSeq[i] = index; else { current = str[i]; index++; altSeq[i] = index; } } current = str[n - 1]; index = 0; revAltSeq[n - 1] = index; for (int i = n - 2; i >= 0; i--) { if (str[i] == current) revAltSeq[i] = index; else { current = str[i]; index++; revAltSeq[i] = index; } } int[] dp = new int[n]; dp[0] = 1; for (int i = 1; i < n; i++) { int merged = str[i] == str[i - 1] ? dp[i - 1] : dp[i - 1] + 1; int newOne = str[i] == str[i - 1] ? altSeq[i - 1] + 2 : altSeq[i - 1] + 1; dp[i] = Math.Max(merged, newOne); } int answer = altSeq[n - 1] + 1; for (int i = 0; i < n - 1; i++) { answer = Math.Max(answer, dp[i] + (str[i] == str[i + 1] ? revAltSeq[i + 1] + 1 : revAltSeq[i + 1])); } answer = Math.Max(answer, dp[n - 1]); Console.WriteLine(answer); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); fact[0] = 1; for (int i = 1; i < fact.Length; i++) { fact[i] = (i * fact[i - 1]) % Mod; } int A = sc.NextInt(), B = sc.NextInt(), D = sc.NextInt(), C = sc.NextInt(); long ans = 0; if (A == 0 && B == 0 && C == 0 && D == 0) ans = 2; else if (((A != 0 && D == 0) || (A == 0 && D != 0)) && B == 0 && C == 0) ans = 1; if (B == C && B != 0) { ans = ((GetCombination(A + B, B) * GetCombination(D + B - 1, B - 1)) % Mod + (GetCombination(D + B, B) * GetCombination(A + B - 1, B - 1)) % Mod) % Mod; } else if (Math.Abs(B - C) == 1) { int M = Math.Max(B, C); ans = (GetCombination(M + A - 1, M - 1) * GetCombination(M + D - 1, M - 1)) % Mod; } Console.WriteLine(ans); }
public static void Run() { ConsoleScanner sc = new ConsoleScanner(); int n = sc.NextInt(); List<Point> points = new List<Point>(); Point f = new Point { x = sc.NextInt(), y = sc.NextInt(), i = 1 }; for (int i = 1; i < n; i++) { Point p = new Point { x = sc.NextInt(), y = sc.NextInt(), i = i + 1 }; p.dist = p.DistanceSqr(f); points.Add(p); } points = points.OrderBy(p => p.dist).ToList(); Point s = points.First(); points.RemoveAt(0); foreach (Point t in points) { if (!Collinear(f.x, f.y, s.x, s.y, t.x, t.y)) { Console.WriteLine(f.i + " " + s.i + " " + t.i); return; } } }
public static void Run() { ConsoleScanner cs = new ConsoleScanner(); int n = cs.NextInt(), m = cs.NextInt(), acount = 0, bcount = 0, ccount = 0; int[,] graph = new int[n, n]; char[] letter = Enumerable.Repeat(' ', n).ToArray(); LinkedList<int> nodes = new LinkedList<int>(); for (int i = 0; i < n; i++) nodes.AddLast(i); for (int i = 0; i < m; i++) { int u = cs.NextInt() - 1, v = cs.NextInt() - 1; graph[u, v] = 1; graph[v, u] = 1; } for (int i = 0; i < n; i++) { int adj = 0; for (int j = 0; j < n; j++) { if (graph[i, j] == 1) adj++; } if (adj == n - 1) { nodes.Remove(i); letter[i] = 'b'; bcount++; } } if (nodes.Count != 0) { int a = nodes.First(); nodes.RemoveFirst(); letter[a] = 'a'; acount++; for (int i = 0; i < n; i++) { if (graph[a, i] == 1 && letter[i] == ' ') { letter[i] = 'a'; nodes.Remove(i); acount++; } } if (nodes.Count != 0) { int c = nodes.First(); nodes.RemoveFirst(); letter[c] = 'c'; ccount++; for (int i = 0; i < n; i++) { if (graph[c, i] == 1) { if (letter[i] == ' ') { letter[i] = 'c'; nodes.Remove(i); ccount++; } else if (letter[i] == ' ') { Console.WriteLine("No"); return; } } } if (nodes.Count > 0) { Console.WriteLine("No"); return; } for (int i = 0; i < n; i++) { if (letter[i] == 'b') continue; int adj = 0; for (int j = 0; j < n; j++) { if (graph[i, j] == 1) { if (letter[j] != 'b') { if (letter[i] != letter[j]) { Console.WriteLine("No"); return; } else adj++; } } } if ((letter[i] == 'a' && adj < acount - 1) || (letter[i] == 'c' && adj < ccount - 1)) { Console.WriteLine("No"); return; } } } } Console.WriteLine("Yes"); for (int i = 0; i < n; i++) Console.Write(letter[i]); }
public static void Run() { ConsoleScanner cs = new ConsoleScanner(); double d = cs.NextInt(), L = cs.NextInt(), v1 = cs.NextInt(), v2 = cs.NextInt(); Console.WriteLine(((L - d) / (v1 + v2)).ToString().Replace(",", ".")); }