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();
             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();
             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();
             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(), 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();
             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");
             }
         }
 }
Пример #7
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();
             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(), 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();
             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);
         }
 }
Пример #11
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();
             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(',', '.'));
         }
 }
Пример #12
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();
             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);
         }
 }
Пример #13
0
 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);
             }
         }
 }
Пример #14
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();
                    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[] 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(), 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");
         }
 }
Пример #17
0
 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);
         }
 }
Пример #18
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();
             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(), 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);
         }
 }
Пример #20
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(), k = fs.NextInt(), mod = (int)1e9 + 7;
                    List <SegmentEnd> points = new List <SegmentEnd>(n << 1);
                    for (int i = 0; i < n; i++)
                    {
                        points.Add(new SegmentEnd {
                            Id = i, X = fs.NextInt(), IsLeft = true
                        });
                        points.Add(new SegmentEnd {
                            Id = i, X = fs.NextInt() + 1, IsLeft = false
                        });
                    }
                    points = points.OrderBy(p => p.X).ThenBy(p => p.IsLeft).ToList();

                    factMod    = new long[n + 1];
                    factMod[0] = 1;
                    factMod[1] = 1;
                    for (int i = 2; i <= n; i++)
                    {
                        factMod[i] = factMod[i - 1] * i % mod;
                    }

                    long[]     crossCount    = new long[n + 1];
                    int        cross         = 0;
                    SegmentEnd prev          = null;
                    foreach (SegmentEnd curr in points)
                    {
                        if (curr.IsLeft)
                        {
                            if (prev != null)
                            {
                                crossCount[cross] += curr.X - prev.X;
                            }
                            cross++;
                        }
                        else
                        {
                            crossCount[cross] += curr.X - prev.X;
                            cross--;
                        }
                        prev = curr;
                    }

                    long ans = 0;
                    for (int i = k; i <= n; i++)
                    {
                        if (crossCount[i] == 0)
                        {
                            continue;
                        }
                        ans = (ans + Combination(i, k, mod) * crossCount[i]) % mod;
                    }
                    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())))
         {
             try
             {
                 int   F = fs.NextInt(), I = fs.NextInt(), T = fs.NextInt();
                 int[] likes = new int[I];
                 if (F <= 10 && F >= 1 && I <= 10 && I >= 1 && T <= F && T >= 1)
                 {
                     for (int i = 0; i < F; i++)
                     {
                         string str = fs.ReadLine();
                         if (str.Length != I)
                         {
                             writer.WriteLine(0);
                             return;
                         }
                         for (int j = 0; j < I; j++)
                         {
                             if (str[j] == 'Y')
                             {
                                 likes[j]++;
                             }
                             else if (str[j] != 'Y' && str[j] != 'N')
                             {
                                 writer.WriteLine(0);
                                 return;
                             }
                         }
                     }
                     if (!String.IsNullOrEmpty(fs.ReadLine()))
                     {
                         writer.WriteLine(0);
                         return;
                     }
                     int ans = 0;
                     for (int i = 0; i < I; i++)
                     {
                         if (likes[i] >= T)
                         {
                             ans++;
                         }
                     }
                     writer.WriteLine(ans);
                 }
                 else
                 {
                     writer.WriteLine(0);
                 }
             }
             catch (Exception e)
             {
                 writer.WriteLine(0);
                 return;
             }
         }
 }
 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(), d = fs.NextInt(), h = fs.NextInt();
             if (d > 2 * h)
             {
                 writer.WriteLine(-1);
                 return;
             }
             if (h == 1)
             {
                 if (d == 1)
                 {
                     if (n != 2)
                     {
                         writer.WriteLine(-1);
                     }
                     else
                     {
                         writer.WriteLine(1 + " " + 2);
                     }
                 }
                 else // d = 2
                 {
                     for (int i = 2; i <= n; i++)
                     {
                         writer.WriteLine(1 + " " + i);
                     }
                 }
                 return;
             }
             StringBuilder sb  = new StringBuilder();
             int           cur = 0;
             for (int i = 1; i <= h; i++)
             {
                 sb.Append((cur + 1) + " " + (i + 1) + "\n");
                 cur = i;
             }
             int cur2 = 0;
             if (d == h)
             {
                 cur2 = cur;
             }
             for (int i = cur + 1; i < cur + d - h + 1; i++)
             {
                 sb.Append((cur2 + 1) + " " + (i + 1) + "\n");
                 cur2 = i;
             }
             while (cur2 < n - 1)
             {
                 cur2++;
                 sb.Append(2 + " " + (cur2 + 1) + "\n");
             }
             writer.WriteLine(sb);
         }
 }
Пример #23
0
 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]];
                     }
                 }
             }
         }
 }
        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 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(), x = fs.NextInt();
                    int    mod = (int)1e9 + 7;
                    long[] a   = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt64);

                    long sum = a.Sum();
                    SortedDictionary <long, int> count = new SortedDictionary <long, int>();
                    for (int i = 0; i < n; i++)
                    {
                        long pow = sum - a[i];
                        if (!count.ContainsKey(pow))
                        {
                            count[pow] = 0;
                        }
                        count[pow]++;
                    }
                    LinkedList <Frequency> freqs = new LinkedList <Frequency>();
                    foreach (var pair in count)
                    {
                        freqs.AddLast(new Frequency {
                            Pow = pair.Key, Value = pair.Value
                        });
                    }
                    long minPow = 0;
                    while (freqs.Count > 0)
                    {
                        var node = freqs.First;
                        freqs.RemoveFirst();
                        var  next = freqs.First;
                        long pow  = node.Value.Pow;
                        int  freq = node.Value.Value;
                        if (freq % x == 0)
                        {
                            if (next != null && next.Value.Pow == pow + 1)
                            {
                                next.Value.Value += freq / x;
                            }
                            else
                            {
                                freqs.AddFirst(new Frequency {
                                    Pow = pow + 1, Value = freq / x
                                });
                            }
                        }
                        else
                        {
                            minPow = pow;
                            break;
                        }
                    }
                    writer.WriteLine(BinPowMod(x, Math.Min(minPow, sum), mod));
                }
        }
Пример #27
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();
             LinkedList <int>[] adjList = new LinkedList <int> [n];
             int[] p = new int[n];
             for (int i = 0; i < n; i++)
             {
                 adjList[i] = new LinkedList <int>();
             }
             for (int u = 0; u < n; u++)
             {
                 int v = fs.NextInt() - 1;
                 p[u] = v;
                 adjList[u].AddLast(v);
                 if (u != v)
                 {
                     adjList[v].AddLast(u);
                 }
             }
             LinkedList <int> comps   = new LinkedList <int>();
             bool[]           visited = new bool[n];
             for (int u = 0; u < n; u++)
             {
                 if (!visited[u])
                 {
                     int r = -1;
                     DoDfs(u, adjList, visited, p, -1, ref r);
                     comps.AddLast(r);
                 }
             }
             int k    = comps.Count - 1;
             int root = comps.Where(u => u == p[u]).DefaultIfEmpty(-1).First();
             if (root == -1)
             {
                 root    = comps.First();
                 p[root] = root;
                 k++;
             }
             foreach (int u in comps)
             {
                 if (u != root)
                 {
                     p[u] = root;
                 }
             }
             writer.WriteLine(k);
             for (int i = 0; i < n; i++)
             {
                 writer.Write(p[i] + 1 + " ");
             }
         }
 }
Пример #28
0
        public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
                using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
                {
                    int maxFloors    = fs.NextInt() + 1;
                    int initialFloor = fs.NextInt();
                    int secretFloor  = fs.NextInt();
                    int trips        = fs.NextInt() + 1;
                    int[,] dp = new int[maxFloors, trips];
                    int[] sum = new int[maxFloors];

                    for (int i = 0; i < maxFloors; i++)
                    {
                        if (i == secretFloor)
                        {
                            dp[i, 0] = 0;
                        }
                        else
                        {
                            dp[i, 0] = 1;
                        }
                        if (i == 0)
                        {
                            sum[i] = 1;
                        }
                        else
                        {
                            sum[i] = sum[i - 1] + dp[i, 0];
                        }
                    }
                    for (int j = 1; j < trips; j++)
                    {
                        for (int i = 1; i < maxFloors; i++)
                        {
                            int delta = Math.Abs(i - secretFloor) - 1;
                            int min = Math.Max(1, i - delta), max = Math.Min(maxFloors - 1, i + delta);
                            dp[i, j] = SubtractMod(sum[max], sum[min - 1]);
                            dp[i, j] = SubtractMod(dp[i, j], dp[i, j - 1]);
                        }
                        for (int i = 0; i < maxFloors; i++)
                        {
                            if (i == 0)
                            {
                                sum[i] = dp[i, j];
                            }
                            else
                            {
                                sum[i] = (sum[i - 1] + dp[i, j]) % mod;
                            }
                        }
                    }
                    writer.WriteLine(dp[initialFloor, trips - 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(), m = fs.NextInt(), bsize = (int)Math.Ceiling(Math.Sqrt(n));
             int[] a = new int[n];
             for (int i = 0; i < n; i++)
             {
                 a[i] = fs.NextInt();
             }
             Query[] queries = new Query[m];
             for (int i = 0; i < m; i++)
             {
                 int left = fs.NextInt() - 1, right = fs.NextInt() - 1;
                 queries[i] = new Query {
                     Id = i, Left = left, Right = right, Block = left / bsize
                 };
             }
             queries = queries.OrderBy(q => q.Block).ThenBy(q => q.Right).ToArray();
             counts  = new Dictionary <int, int>();
             int l = 0, r = 0;
             Add(a[0]);
             int[] answers = new int[m];
             foreach (Query q in queries)
             {
                 while (r < q.Right)
                 {
                     r++;
                     Add(a[r]);
                 }
                 while (r > q.Right)
                 {
                     Remove(a[r]);
                     r--;
                 }
                 while (l < q.Left)
                 {
                     Remove(a[l]);
                     l++;
                 }
                 while (l > q.Left)
                 {
                     l--;
                     Add(a[l]);
                 }
                 answers[q.Id] = answer;
             }
             for (int i = 0; i < m; i++)
             {
                 writer.WriteLine(answers[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(), l = fs.NextInt(), v1 = fs.NextInt(), v2 = fs.NextInt(), k = fs.NextInt();
             int    g    = (int)Math.Ceiling(n * 1.0 / k);
             double lbus = l * (1.0 * v1 + v2) / (v2 * 1.0 + (2 * g - 1) * v1);
             double time = (l - lbus) / v1 + lbus / v2;
             writer.WriteLine(time.ToString().Replace(",", "."));
         }
 }