public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { string[] exp = fs.ReadLine().Split('e'); string[] num = exp[0].Split('.'); int b = int.Parse(exp[1]); StringBuilder sb = new StringBuilder(200); sb.Append(num[0]); int i = 0; for (i = 0; i < b; i++) { if (i >= num[1].Length) { sb.Append("0"); } else { sb.Append(num[1][i]); } } if (b < num[1].Length && num[1] != "0") { sb.Append("."); for (; i < num[1].Length; i++) { sb.Append(num[1][i]); } } writer.WriteLine(sb); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); HashSet <int> set = new HashSet <int>(); for (int i = 0; i < n; i++) { int x = fs.NextInt(); set.Add(x); } if (set.Count < 3) { writer.WriteLine("YES"); } else if (set.Count > 3) { writer.WriteLine("NO"); } else { int[] a = set.ToArray(); Array.Sort(a); writer.WriteLine(a[1] - a[0] == a[2] - a[1] ? "YES" : "NO"); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int t = fs.NextInt(); for (int i = 1; i <= t; i++) { long s = fs.NextLong(); long upper = (long)Math.Ceiling(Math.Sqrt(s)); long lower = upper - 1; long lt = s - lower * lower, x, y; if ((upper & 1) == 0) { x = Math.Min(upper, lt); y = upper - (lt > upper ? (lt - upper) : 0); } else { x = upper - (lt > upper ? (lt - upper) : 0); y = Math.Min(upper, lt); } writer.WriteLine("Case " + i + ": " + x + " " + y); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), c = fs.NextInt(); int[] p = new int[n], t = new int[n]; for (int i = 0; i < n; i++) { p[i] = fs.NextInt(); } for (int i = 0; i < n; i++) { t[i] = fs.NextInt(); } int limak = 0, radewoosh = 0, time = 0; for (int i = 0; i < n; i++) { time += t[i]; limak += Math.Max(0, p[i] - c * time); } time = 0; for (int i = n - 1; i >= 0; i--) { time += t[i]; radewoosh += Math.Max(0, p[i] - c * time); } writer.WriteLine(limak > radewoosh ? "Limak" : radewoosh > limak ? "Radewoosh" : "Tie"); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { string str = fs.ReadLine(); List <int> list = new List <int>(str.Length); int first = -1; for (int i = 0; i < str.Length; i++) { if (str[i] == 'F') { if (first == -1 && list.Count != i) { first = list.Count; } list.Add(i); } } if (list.Count == 0 || list.Count == str.Length || first == -1) { writer.WriteLine(0); return; } int t = 0; for (int i = first + 1; i < list.Count; i++) { t = Math.Max(0, t - list[i] + list[i - 1] + 2); } writer.WriteLine(t + list.Last() - list.Count + 1); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); int[] male = new int[367], fem = new int[367]; for (int i = 0; i < n; i++) { string[] line = fs.ReadLine().Split(' '); if (line[0] == "M") { int l = Convert.ToInt32(line[1]), r = Convert.ToInt32(line[2]); for (int j = l; j <= r; j++) { male[j]++; } } else if (line[0] == "F") { int l = Convert.ToInt32(line[1]), r = Convert.ToInt32(line[2]); for (int j = l; j <= r; j++) { fem[j]++; } } } int ans = 0; for (int i = 1; i < 367; i++) { ans = Math.Max(ans, Math.Min(male[i], fem[i]) * 2); } writer.WriteLine(ans); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); AdjList = new LinkedList <int> [n]; Sizes = new int[n]; StartTimes = new double[n]; for (int i = 0; i < n; i++) { AdjList[i] = new LinkedList <int>(); } for (int i = 1; i < n; i++) { int p = fs.NextInt() - 1; AdjList[p].AddLast(i); } EvalSize(0); StartTimes[0] = 1; EvalStartTimes(0, 0); for (int i = 0; i < n; i++) { writer.Write(StartTimes[i].ToString().Replace(",", ".") + " "); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); LinkedList <int>[] adjList = new LinkedList <int> [n]; for (int i = 0; i < n; i++) { adjList[i] = new LinkedList <int>(); } for (int i = 0; i < n; i++) { int v = fs.NextInt() - 1; if (v != i) { adjList[i].AddLast(v); } if (i > 0) { adjList[i].AddLast(i - 1); adjList[i - 1].AddLast(i); } } int[] dist = Enumerable.Repeat(int.MaxValue, n).ToArray(); RunDijkstra(adjList, dist); foreach (int x in dist) { writer.Write(x + " "); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), m = fs.NextInt(); Player[] p = new Player[n]; string[] ans = Enumerable.Repeat("", m).ToArray(); for (int i = 0; i < n; i++) { string[] s = fs.ReadLine().Split(); p[i] = new Player { Name = s[0], Region = Convert.ToInt32(s[1]) - 1, Score = Convert.ToInt32(s[2]) }; } p = p.OrderBy(x => x.Region).ThenByDescending(x => x.Score).ToArray(); int j = 0, lastRegion = -1; for (int i = 0; i < n; i++) { if (p[i].Region != lastRegion) { j = 0; lastRegion = p[i].Region; } if (j == 0) ans[p[i].Region] += p[i].Name + " "; else if (j == 1) ans[p[i].Region] += p[i].Name; else if (j == 2 && p[i].Score == p[i - 1].Score) ans[p[i].Region] = "?"; j++; } for (int i = 0; i < m; i++) { writer.WriteLine(ans[i]); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); long[] a = new long[n], b = new long[n]; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); } for (int i = 0; i < n; i++) { b[i] = fs.NextInt(); } long maxVal = a[0] + b[0]; for (int i = 0; i < n; i++) { long aor = a[i], bor = b[i]; maxVal = Math.Max(aor + bor, maxVal); for (int j = i + 1; j < n; j++) { aor |= a[j]; bor |= b[j]; maxVal = Math.Max(aor + bor, maxVal); } } writer.WriteLine(maxVal); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), m = fs.NextInt(); LinkedList <int>[] adj = new LinkedList <int> [n]; for (int i = 0; i < n; i++) { adj[i] = new LinkedList <int>(); } for (int i = 0; i < m; i++) { int u = fs.NextInt() - 1, v = fs.NextInt() - 1; adj[u].AddLast(v); adj[v].AddLast(u); } bool[] visited = new bool[n]; int ans = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { ans += DFS(i, visited, adj, -1); } } writer.WriteLine(ans); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); long x = fs.NextInt(); int sad = 0; for (int i = 0; i < n; i++) { string[] line = fs.ReadLine().Split(); if (line[0] == "+") { x += Convert.ToInt32(line[1]); } else { int d = Convert.ToInt32(line[1]); if (x < d) { sad++; } else { x -= d; } } } writer.WriteLine(x + " " + sad); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { long A = fs.NextLong(), B = fs.NextLong(), n = fs.NextLong(), x = fs.NextLong(); if (n == 0) { writer.WriteLine(x); } else if (n == 1) { writer.WriteLine((A * x + B) % _mod); } else { long[,] initial = new long[, ] { { A *x + B, 1 } }; long[,] factor = new long[, ] { { A, 0 }, { B, 1 } }; long[,] res = MultiplyMatricesMod(initial, BinPowMatrixMod(factor, n - 1, _mod), _mod); writer.WriteLine(res[0, 0]); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), p = fs.NextInt(); string[] buyers = new string[n]; for (int i = n - 1; i >= 0; i--) { buyers[i] = fs.ReadLine(); } long revenue = p / 2; double prevVal = 1; for (int i = 1; i < n; i++) { if (buyers[i] == "half") { prevVal *= 2; } else { prevVal = prevVal * 2 + 1; } revenue += (long)((prevVal / 2) * p); } writer.WriteLine(revenue); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); int[] a = new int[n]; for (int i = 0; i < n - 1; i++) { a[i] = fs.NextInt() - 1; } a[n - 1] = n - 1; long[] dp = new long[n]; MaxSegmentTree tree = new MaxSegmentTree(a); long sum = 1; dp[n - 2] = 1; for (int i = n - 3; i >= 0; i--) { int m = tree.QueryMax(i + 1, a[i]); dp[i] = dp[m] - a[i] + m + n - i - 1; sum += dp[i]; } writer.WriteLine(sum); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), k = fs.NextInt(); int a = fs.NextInt(), b = fs.NextInt(), c = fs.NextInt(), d = fs.NextInt(); if (n == 4 || k <= n) { writer.WriteLine(-1); } else { writer.Write(a + " " + c + " "); for (int i = 1; i <= n; i++) { if (i != a && i != b && i != c && i != d) { writer.Write(i + " "); } } writer.WriteLine(d + " " + b); writer.Write(c + " " + a + " "); for (int i = 1; i <= n; i++) { if (i != a && i != b && i != c && i != d) { writer.Write(i + " "); } } writer.Write(b + " " + d); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = 100; int[] a = new int[n]; Random rand = new Random(); for (int i = 0; i < n; i++) { a[i] = rand.Next(); writer.Write(a[i] + " "); } writer.WriteLine(); SegmentTree tree = new SegmentTree(a); bool isEqual = true; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int aa = tree.QueryMax(i, j), bb = FindMax(a, i, j); writer.WriteLine(aa + " " + bb); if (aa != bb) { isEqual = false; } } } writer.WriteLine(isEqual); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int t = fs.NextInt(); for (int i = 1; i <= t; i++) { long w = fs.NextLong(); if ((w & 1) == 0) { long m = 1; while ((w & 1) == 0) { m <<= 1; w >>= 1; } writer.WriteLine("Case " + i + ": " + w + " " + m); } else { writer.WriteLine("Case " + i + ": Impossible"); } } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { string s = fs.ReadLine(); long ans = 0; for (int i = 0; i < s.Length; i++) { int d = Convert.ToInt32("" + s[i]); if (i < s.Length - 1) { int dd = Convert.ToInt32("" + s[i] + s[i + 1]); if (dd % 4 == 0) { ans += i + 1; } } if (d % 4 == 0) { ans++; } } writer.WriteLine(ans); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), m = fs.NextInt(); int[] a = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32); int[] b = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32); int max = -1; for (int i = 0; i < n; i++) { int ind = Find(b, a[i]); int min; if (ind != -1) { min = 0; } else { int l = FindLowerBound(b, a[i]); int u = FindUpperBound(b, a[i]); min = Math.Min(l != -1 ? a[i] - b[l] : int.MaxValue, u != -1 ? b[u] - a[i] : int.MaxValue); } max = Math.Max(max, min); } writer.WriteLine(max); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); int[] indices = new int[n]; long[] a = new long[n], b = new long[n]; for (int i = 0; i < n; i++) { int r = fs.NextInt(), h = fs.NextInt(); a[i] = (long)r * r * h; indices[i] = i; } Array.Copy(a, b, n); Array.Sort(b); for (int i = 0; i < n; i++) { indices[i] = findValue(b, 0, n - 1, a[i]); } MaxSegmentTree tree = new MaxSegmentTree(new long[n]); for (int i = 0; i < n; i++) { int index = indices[i]; long value = tree.QueryMax(0, indices[i] - 1) + a[i]; tree.Update(indices[i], indices[i], value); } writer.WriteLine((tree.QueryMax(0, n - 1) * Math.PI).ToString().Replace(',', '.')); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); } for (int i = 0; i < n; i++) { for (int j = 1; j < n - i; j++) { if (a[j] < a[j - 1]) { int t = a[j]; a[j] = a[j - 1]; a[j - 1] = t; writer.WriteLine(j + " " + (j + 1)); } } } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int t = fs.NextInt(); Dictionary <string, int> count = new Dictionary <string, int>(); while (t-- > 0) { string[] cmd = fs.ReadLine().Split(); if (cmd[0] == "+") { long a = Convert.ToInt64(cmd[1]); string pattern = NumToPattern(a); if (!count.ContainsKey(pattern)) { count.Add(pattern, 0); } count[pattern]++; } else if (cmd[0] == "-") { long a = Convert.ToInt64(cmd[1]); string pattern = NumToPattern(a); count[pattern]--; } else { string pattern = AppendZeros(cmd[1]); writer.WriteLine(count.ContainsKey(pattern) ? count[pattern] : 0); } } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); Segment[] segms = new Segment[n]; int[] ans = new int[n]; for (int i = 0; i < n; i++) { segms[i] = new Segment { Id = i, Left = fs.NextInt(), Right = fs.NextInt() }; } segms = segms.OrderBy(s => s.Left).ToArray(); Segment[] t = segms.OrderBy(s => s.Right).ToArray(); int[] ind = new int[n]; for (int i = 0; i < n; i++) { ind[i] = findItem(t, 0, n - 1, segms[i].Right); } SegmentTree tree = new SegmentTree(new int[n]); for (int i = n - 1; i >= 0; i--) { ans[segms[i].Id] = tree.QuerySum(0, ind[i] - 1); tree.Update(ind[i], ind[i], 1); } for (int i = 0; i < n; i++) { writer.WriteLine(ans[i]); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); int[] a = new int[n], b = new int[n]; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); } for (int i = 0; i < n; i++) { b[i] = fs.NextInt(); } int l = 0, j = 0; int max = 0, min = 0; long ans = 0; while (j < n) { if (a[j] <= b[j]) { if (a[max] <= a[j]) { max = j; if (a[max] > b[min]) { l = min + 1; max = l; min = l; j = l; } } if (b[min] >= b[j]) { min = j; if (b[min] < a[max]) { l = max + 1; min = l; max = l; j = l; } } if (a[max] == b[min]) { ans += Math.Min(max, min) + 1 - l; } } else { l = j + 1; min = l; max = l; } j++; } writer.WriteLine(ans); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { N = fs.NextInt(); AdjList = new LinkedList <int> [N]; for (int i = 0; i < N; i++) { AdjList[i] = new LinkedList <int>(); } for (int i = 0; i < N - 1; i++) { int u = fs.NextInt() - 1, v = fs.NextInt() - 1; AdjList[u].AddLast(v); AdjList[v].AddLast(u); } int max = 0; foreach (int v in AdjList[0]) { List <int> times = new List <int>(); DFS(times, v, 0, 1); int[] temp = times.OrderBy(i => i).ToArray(); for (int i = 1; i < temp.Length; i++) { if (temp[i] <= temp[i - 1]) { temp[i] = temp[i - 1] + 1; } } max = Math.Max(max, temp[temp.Length - 1]); } writer.WriteLine(max); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); a = new int[n]; adjList = new LinkedList <Edge1> [n]; for (int i = 0; i < n; i++) { adjList[i] = new LinkedList <Edge1>(); a[i] = fs.NextInt(); } for (int i = 0; i < n - 1; i++) { int p = fs.NextInt() - 1, c = fs.NextInt(); adjList[i + 1].AddLast(new Edge1 { v = p, w = c }); adjList[p].AddLast(new Edge1 { v = i + 1, w = c }); } childNum = new int[n]; GetChildNum(0, new bool[n]); long ans = VerticesToRemove(0, 0, new bool[n]); writer.WriteLine(ans); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int t = fs.NextInt(); for (int i = 1; i <= t; i++) { int n = fs.NextInt(); int[] count = new int[(int)1e6 + 2]; for (int j = 0; j < n; j++) { int x = fs.NextInt() + 1; count[x]++; } int ans = 0; for (int j = 1; j < count.Length; j++) { if (count[j] > 0) { ans += (int)(Math.Ceiling(count[j] / (j * 1.0)) * j); } } writer.WriteLine("Case " + i + ": " + ans); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { if (i % 2 == 0) { sb.Append("I hate "); } else { sb.Append("I love "); } if (i < n - 1) { sb.Append("that "); } } sb.Append("it"); writer.WriteLine(sb); } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { bool[] met = new bool[26]; int n = fs.NextInt(); string str = fs.ReadLine(); int dup = 0, unused = 0; foreach (char c in str) { if (met[c - 'a']) { dup++; } else { met[c - 'a'] = true; } } foreach (bool b in met) { if (!b) { unused++; } } writer.WriteLine(dup > unused ? -1 : dup); } }