Exemple #1
0
        static void Main()
        {
            var N       = int.Parse(Console.ReadLine());
            var heights = Console.ReadLine().Split(' ').Select(int.Parse).ToList();

            var ob           = new OrderedBag <int>();
            var lastInserted = 0;
            var memo         = new int[heights.Count];

            for (int i = heights.Count - 1; i >= 0; --i)
            {
                var current = heights[i];

                if (i == heights.Count - 1 || current > ob.GetLast())
                {
                    memo[i] = 0;
                    ob.Add(current);
                    lastInserted = current;
                    continue;
                }

                if (current < lastInserted)
                {
                    memo[i] = memo[i + 1] + 1;
                }
                else
                {
                    var currentIndex = -1;

                    for (int j = i + 1; j < heights.Count; j++)
                    {
                        var next = heights[j];
                        if (current < next)
                        {
                            currentIndex = j;
                            break;
                        }
                    }

                    if (currentIndex == -1)
                    {
                        memo[i] = 0;
                    }
                    else
                    {
                        memo[i] = memo[currentIndex] + 1;
                    }
                }

                ob.Add(current);
                lastInserted = current;
            }

            var longest = memo.ToList().Max();

            Console.WriteLine(longest);
            Console.WriteLine(string.Join(" ", memo));
        }
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());

            var points = new SweepPoint[n * 2];

            for (int i = 0; i < n; ++i)
            {
                var strs = Console.ReadLine().Split(' ');
                var l    = int.Parse(strs[0]);
                var r    = int.Parse(strs[1]);
                var h    = int.Parse(strs[2]);

                points[i * 2] = new SweepPoint
                {
                    X      = l,
                    Height = h,
                    IsLeft = true
                };
                points[i * 2 + 1] = new SweepPoint
                {
                    X      = r,
                    Height = h,
                    IsLeft = false
                };
            }

            Array.Sort(points, (x, y) => x.X - y.X);

            var result = new List <Point>();

            var heights = new OrderedBag <int>();

            heights.Add(0);

            foreach (var p in points)
            {
                if (p.IsLeft)
                {
                    heights.Add(p.Height);
                }
                else
                {
                    heights.Remove(p.Height);
                }

                var newPoint = new Point {
                    X = p.X, Y = heights.GetLast()
                };
                result.Add(newPoint);
            }

            var lastHeight = 0;

            for (int i = 0; i < result.Count; ++i)
            {
                var p = result[i];

                if (p.Y == lastHeight)
                {
                    continue;
                }

                if (i + 1 < result.Count && p.X == result[i + 1].X)
                {
                    continue;
                }

                Console.WriteLine("{0} {1}", p.X, p.Y);
                lastHeight = p.Y;
            }
        }
        static void Main(string[] args)
        {
            string        input  = Console.ReadLine();
            StringBuilder result = new StringBuilder();

            while (input != "EXIT")
            {
                string[] inputSplit = input.Split();
                string   command    = inputSplit[0];

                if (command == "ADD")
                {
                    short number = short.Parse(inputSplit[1]);

                    if (set2.Count == 0)
                    {
                        set2.Enqueue(number);
                        totalSoFar++;
                        input = Console.ReadLine();
                        continue;
                    }

                    if (number > set2.Peek())
                    {
                        set2.Enqueue(number);
                        totalSoFar++;
                    }
                    else
                    {
                        set1.Add(number);
                        totalSoFar++;
                    }

                    if (set2.Count > set1.Count + 1)
                    {
                        short min = set2.Dequeue();

                        set1.Add(min);
                    }
                    else if (set2.Count < set1.Count)
                    {
                        short max = set1.GetLast();
                        set1.RemoveLast();
                        set2.Enqueue(max);
                    }
                }
                else if (command == "FIND")
                {
                    if (totalSoFar % 2 == 0)
                    {
                        sum  = 0;
                        sum += set1.GetLast() + set2.Peek();
                        sum  = sum / 2;
                        result.AppendLine(sum.ToString());
                    }
                    else if (totalSoFar % 2 == 1)
                    {
                        result.AppendLine(set2.Peek().ToString());
                    }
                }
                input = Console.ReadLine();
            }
            Console.WriteLine(result);
        }