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 q = fs.NextInt(); long u, v, w; List<Edge> path; while (q-- > 0) { int t = fs.NextInt(); if (t == 1) { u = fs.NextLong(); v = fs.NextLong(); w = fs.NextLong(); path = GetPath(u, v); foreach (var edge in path) { if (!costs.ContainsKey(edge)) costs.Add(edge, 0); costs[edge] += w; } } else { u = fs.NextLong(); v = fs.NextLong(); path = GetPath(u, v); writer.WriteLine(GetCost(path)); } } } }
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(), 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 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 a1 = fs.NextInt(), an = fs.NextInt(), d = fs.NextInt(); if (d == 0) { if (a1 == an) writer.WriteLine("YES"); else writer.WriteLine("NO"); } else { if ((an - a1) % d == 0) { if ((an - a1) / d + 1 >= 1) writer.WriteLine("YES"); else writer.WriteLine("NO"); } else { writer.WriteLine("NO"); } } } }
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(); 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()))) { int n = fs.NextInt(), k = fs.NextInt(); long[] c = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt64); bool[] isCap = new bool[n]; long sum = c.Sum(), res = 0, caps = 0; for (int i = 0; i < k; i++) { int ind = fs.NextInt() - 1; isCap[ind] = true; caps += c[ind]; } for (int i = 0; i < n; i++) { int l = i - 1, r = (i + 1) % n; if (l < 0) l = n - 1; if (isCap[i]) res += c[i] * (sum - c[i]); else { res += c[i] * caps; if (!isCap[l]) res += c[i] * c[l]; if (!isCap[r]) res += c[i] * c[r]; } } writer.WriteLine(res / 2); } }
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[] ids = new int[n]; for (int i = 0; i < n; i++) { ids[i] = fs.NextInt(); } long sum = 0; long r = 0; for (int i = 1; i <= n; i++) { sum += i; if (k <= sum) { r = i; break; } } int index = (int)(k - (r * (r - 1)) / 2) - 1; writer.WriteLine(ids[index]); } }
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]; int min = n + 1, max = 0, l = -1, r = -1; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); if (a[i] < min) { min = a[i]; l = i; } if (a[i] > max) { max = a[i]; r = i; } } if (l > r) { int t = l; l = r; r = t; } if (r > n - l - 1) writer.WriteLine(r); else writer.WriteLine(n - l - 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[] 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 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(), h = fs.NextInt(), k = fs.NextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); } int sum = 0, j = 0; long sec = 0; while (j < n) { while (j < n && sum + a[j] <= h) sum += a[j++]; if (j < n) { int need = a[j] - (h - sum); int s = (int)Math.Ceiling(need * 1.0 / k); sec += s; sum -= s * k; if (sum < 0) sum = 0; } else { sec += (int)Math.Ceiling(sum * 1.0 / k); } } writer.WriteLine(sec); } }
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()))) { int t = fs.NextInt(); while (t-- > 0) { int n = fs.NextInt(); dp = Enumerable.Repeat<long>(-1, n).ToArray(); a = new int[n]; sum = new long[n]; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); } sum[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { sum[i] = a[i] + sum[i + 1]; } for (int i = Math.Max(n - 3, 0); i < n; i++) { for (int j = i; j < n; j++) { if (dp[j] == -1) dp[j] = 0; dp[i] += a[j]; } } writer.WriteLine(Solve(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(); 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 n = fs.NextInt(), m = fs.NextInt(); int[] p = new int[n]; DisjointSet dsu = new DisjointSet(); for (int i = 0; i < n; i++) { p[i] = fs.NextInt() - 1; dsu.MakeSet(i); } for (int i = 0; i < m; i++) { int u = fs.NextInt() - 1, v = fs.NextInt() - 1; dsu.Union(p[u], p[v]); } LinkedList<int>[] lists = new LinkedList<int>[n]; for (int i = n - 1; i >= 0; i--) { int set = dsu.FindSet(i); if (lists[set] == null) lists[set] = new LinkedList<int>(); lists[set].AddLast(i); } for (int i = 0; i < n; i++) { int set = dsu.FindSet(p[i]); writer.Write(lists[set].First() + 1 + " "); lists[set].RemoveFirst(); } } }
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(); 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(); 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[] m = new int[n], r = new int[n]; for (int i = 0; i < n; i++) m[i] = fs.NextInt(); for (int i = 0; i < n; i++) r[i] = fs.NextInt(); int size = (int)1e6; bool[] onDuty = new bool[size]; for (int i = 0; i < size; i++) { for (int j = 0; j < n; j++) { if (i % m[j] == r[j]) { onDuty[i] = true; break; } } } int count = 0; for (int i = 0; i < size; i++) { if (onDuty[i]) count++; } writer.WriteLine(((count * 1.0) / size).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(), m = fs.NextInt(); int[] a = new int[n]; int[] accuses = new int[n], defenses = new int[n]; bool[] isAccusing = new bool[n]; for (int i = 0; i < n; i++) { a[i] = fs.NextInt(); if (a[i] > 0) { a[i] = a[i] - 1; accuses[a[i]]++; isAccusing[i] = true; } else { a[i] = -a[i] - 1; defenses[a[i]]++; isAccusing[i] = false; } } bool[] isSuspect = new bool[n]; int totalAccuses = accuses.Sum(), totalDefenses = defenses.Sum(); int suspectCount = 0; for (int i = 0; i < n; i++) { if (accuses[i] + (totalDefenses - defenses[i]) == m) { isSuspect[i] = true; suspectCount++; } } string[] verdicts = new string[n]; if (suspectCount == 1) { for (int i = 0; i < n; i++) { if (isAccusing[i] && isSuspect[a[i]] || !isAccusing[i] && !isSuspect[a[i]]) verdicts[i] = "Truth"; else verdicts[i] = "Lie"; } } else { for (int i = 0; i < n; i++) { if (!isAccusing[i] && !isSuspect[a[i]]) verdicts[i] = "Truth"; else if (isAccusing[i] && !isSuspect[a[i]]) verdicts[i] = "Lie"; else verdicts[i] = "Not defined"; } } for (int i = 0; i < n; i++) { writer.WriteLine(verdicts[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(), 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()))) { int n = fs.NextInt(), a = fs.NextInt(), b = fs.NextInt(); int[] p = new int[n]; Dictionary<int, int> pos = new Dictionary<int, int>(); Node[] nodes = new Node[n]; for (int i = 0; i < n; i++) { p[i] = fs.NextInt(); nodes[i] = new Node { Value = p[i] }; pos.Add(p[i], i); } for (int i = 0; i < n; i++) { int adiff = a - nodes[i].Value; int bdiff = b - nodes[i].Value; if (pos.ContainsKey(adiff)) { nodes[i].Adiff = nodes[pos[adiff]]; nodes[pos[adiff]].Adiff = nodes[i]; } if (pos.ContainsKey(bdiff) && a != b) { nodes[i].Bdiff = nodes[pos[bdiff]]; nodes[pos[bdiff]].Bdiff = nodes[i]; } } int[] ans = Enumerable.Repeat(-1, n).ToArray(); for (int i = 0; i < n; i++) { Node current = nodes[i]; if (!Traverse(nodes, current, pos, ans)) { writer.WriteLine("NO"); return; } } for (int i = 0; i < n; i++) { Node current = nodes[i]; if (current.Adiff == current || current.Bdiff == current) { if (current.Adiff == current && current.Bdiff == null) ans[i] = 0; else if (current.Adiff == current && current.Bdiff != null) current.Adiff = null; else if (current.Bdiff == current && current.Adiff == null) ans[i] = 1; else if (current.Bdiff == current && current.Adiff != null) current.Bdiff = null; } Traverse(nodes, current, pos, ans); } writer.WriteLine("YES"); for (int i = 0; i < n; i++) { writer.Write(ans[i] + " "); } } }
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(); DisjointSet dsu = new DisjointSet(); List<string> cycles = new List<string>(); for (int i = 1; i <= n; i++) { dsu.MakeSet(i); } for (int i = 0; i < n - 1; i++) { int a = fs.NextInt(), b = fs.NextInt(); if (dsu.FindSet(a) == dsu.FindSet(b)) cycles.Add(a + " " + b); else dsu.Union(a, b); } HashSet<int> hashSet = new HashSet<int>(); for (int i = 1; i <= n; i++) { hashSet.Add(dsu.FindSet(i)); } int[] leaders = hashSet.ToArray(); writer.WriteLine(leaders.Length - 1); int j = 0; for (int i = 0; i < leaders.Length - 1; i++) { writer.WriteLine(cycles[j++] + " " + leaders[i] + " " + leaders[i + 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(), s = fs.NextInt(), x = fs.NextInt(); writer.WriteLine((x >= t && (x - t) % s == 0) || (x >= t + 1 && (x - t - 1) % s == 0 && (x - t - 1) / s >= 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 n = fs.NextInt(), b = fs.NextInt(); Query[] queries = new Query[n]; long[] finishingTimes = new long[n]; for (int i = 0; i < n; i++) { queries[i] = new Query { Id = i, Time = fs.NextInt(), Duration = fs.NextInt() }; } Queue<Query> queryQueue = new Queue<Query>(); long freeTime = queries[0].Time; for (int i = 0; i < n; i++) { if (freeTime <= queries[i].Time) { if (queryQueue.Count > 0) { Query query = queryQueue.Dequeue(); freeTime = freeTime + query.Duration; finishingTimes[query.Id] = freeTime; queryQueue.Enqueue(queries[i]); } else { freeTime = queries[i].Time + queries[i].Duration; finishingTimes[queries[i].Id] = freeTime; } } else if (freeTime > queries[i].Time) { if (queryQueue.Count < b) { queryQueue.Enqueue(queries[i]); } else { finishingTimes[i] = -1; } } } while (queryQueue.Count > 0) { Query query = queryQueue.Dequeue(); freeTime = freeTime + query.Duration; finishingTimes[query.Id] = freeTime; } for (int i = 0; i < n; i++) { writer.Write(finishingTimes[i] + " "); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int totalCount = fs.NextInt(); List<int> a = new List<int>(totalCount); Dictionary<int, int> count = new Dictionary<int, int>(); if (totalCount % 2 == 1) { writer.WriteLine("NO"); return; } for (int i = 0; i < totalCount; i++) { int x = fs.NextInt(); if (!count.ContainsKey(x)) { count.Add(x, 1); a.Add(x); } else count[x]++; } int n = a.Count; a = a.OrderByDescending(x => x).ToList(); int temp = count[a[0]]; totalCount -= temp; for (int i = 1; i < n; i++) { if (a[i - 1] - a[i] > 1 || count[a[i]] < temp) { writer.WriteLine("NO"); break; } if (count[a[i]] == temp) { temp = 0; writer.WriteLine(totalCount - count[a[i]] == 0 ? "YES" : "NO"); break; } else if (count[a[i]] > temp) { if (i == n - 1) { writer.WriteLine("NO"); } else { temp = count[a[i]] - temp; totalCount -= count[a[i]]; } } } } }