Esempio n. 1
0
    static object Solve()
    {
        var(n, k) = Read2();
        var s = Console.ReadLine();

        var cs = new char[k];

        var q = new KeyedPriorityQueue <int, char>(i => s[i]);

        for (int i = 0; i < n - k; i++)
        {
            q.Push(i);
        }

        var t = -1;

        for (int i = n - k; i < n; i++)
        {
            q.Push(i);
            while (q.Peek() < t)
            {
                q.Pop();
            }

            t             = q.Pop();
            cs[i - n + k] = s[t];
        }

        return(new string(cs));
    }
        public void Keyed_1()
        {
            var n = 100000;
            var a = RandomHelper.CreateData(n);

            var actual = new List <int>();
            var q      = new KeyedPriorityQueue <int, int>(x => x / 10);

            foreach (var x in a)
            {
                q.Push(x);
            }
            while (q.Count > 0)
            {
                actual.Add(q.Pop());
            }

            var expected = a.OrderBy(x => x / 10).ToArray();

            CollectionAssert.AreEqual(expected, actual);

            actual.Clear();
            q = new KeyedPriorityQueue <int, int>(x => x / 10, ComparerHelper <int> .Create(true));
            foreach (var x in a)
            {
                q.Push(x);
            }
            while (q.Count > 0)
            {
                actual.Add(q.Pop());
            }

            expected = a.OrderByDescending(x => x / 10).ToArray();
            CollectionAssert.AreEqual(expected, actual);
        }