Submission #28418529


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks
{
    public class D
    {
        public static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var (N, K) = Scanner.Scan<int, int>();
            var P = Scanner.ScanEnumerable<int>().ToArray();
            var queue = new PriorityQueue<int>();
            for (var i = 0; i < K; i++)
            {
                queue.Enqueue(P[i]);
            }

            Console.WriteLine(queue.Peek());

            for (var i = K; i < N; i++)
            {
                if (queue.Peek() < P[i])
                {
                    queue.Dequeue();
                    queue.Enqueue(P[i]);
                }

                var answer = queue.Peek();
                Console.WriteLine(answer);
            }
        }
        public class PriorityQueue<T> : IReadOnlyCollection<T>
        {
            private readonly Comparison<T> _comparison;
            private readonly List<T> _heap;
            public PriorityQueue(IComparer<T> comparer) : this(null, comparer) { }
            public PriorityQueue(Comparison<T> comparison) : this(null, comparison) { }
            public PriorityQueue(IEnumerable<T> items = null, IComparer<T> comparer = null)
                : this(items, (comparer ?? Comparer<T>.Default).Compare) { }
            public PriorityQueue(IEnumerable<T> items, Comparison<T> comparison)
            {
                _heap = new List<T>();
                _comparison = comparison;
                if (items == null) return;
                foreach (var item in items) Enqueue(item);
            }
            public int Count => _heap.Count;
            public IEnumerator<T> GetEnumerator() => _heap.GetEnumerator();
            IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
            public void Enqueue(T item)
            {
                var child = Count;
                _heap.Add(item);
                while (child > 0)
                {
                    var parent = (child - 1) / 2;
                    if (_comparison(_heap[parent], _heap[child]) <= 0) break;
                    (_heap[parent], _heap[child]) = (_heap[child], _heap[parent]);
                    child = parent;
                }
            }
            public T Dequeue()
            {
                if (Count == 0) throw new InvalidOperationException();
                var ret = _heap[0];
                _heap[0] = _heap[Count - 1];
                _heap.RemoveAt(Count - 1);
                var parent = 0;
                while (parent * 2 + 1 < Count)
                {
                    var left = parent * 2 + 1;
                    var right = parent * 2 + 2;
                    if (right < Count && _comparison(_heap[left], _heap[right]) > 0)
                        left = right;
                    if (_comparison(_heap[parent], _heap[left]) <= 0) break;
                    (_heap[parent], _heap[left]) = (_heap[left], _heap[parent]);
                    parent = left;
                }
                return ret;
            }
            public T Peek()
            {
                if (Count == 0) throw new InvalidOperationException();
                return _heap[0];
            }
            public bool TryDequeue(out T result)
            {
                if (Count > 0)
                {
                    result = Dequeue();
                    return true;
                }
                result = default;
                return false;
            }
            public bool TryPeek(out T result)
            {
                if (Count > 0)
                {
                    result = Peek();
                    return true;
                }
                result = default;
                return false;
            }
            public void Clear() => _heap.Clear();
            public bool Contains(T item) => _heap.Contains(item);
        }


        public static class Scanner
        {
            public static T Scan<T>() where T : IConvertible => Convert<T>(ScanLine()[0]);
            public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
            {
                var line = ScanLine();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]));
            }
            public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
            {
                var line = ScanLine();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]));
            }
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
            {
                var line = ScanLine();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]));
            }
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
            {
                var line = ScanLine();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]), Convert<T5>(line[4]));
            }
            public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
            {
                var line = ScanLine();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]), Convert<T5>(line[4]), Convert<T6>(line[5]));
            }
            public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => ScanLine().Select(Convert<T>);
            private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
            private static string[] ScanLine() => Console.ReadLine()?.Trim().Split(' ') ?? Array.Empty<string>();
        }
    }
}

Submission Info

Submission Time
Task D - Prefix K-th Max
User AconCavy
Language C# (.NET Core 3.1.201)
Score 400
Code Size 6585 Byte
Status AC
Exec Time 350 ms
Memory 91176 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 28
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 93 ms 28116 KiB
001.txt AC 291 ms 90844 KiB
002.txt AC 193 ms 81028 KiB
003.txt AC 314 ms 85192 KiB
004.txt AC 327 ms 85820 KiB
005.txt AC 334 ms 83224 KiB
006.txt AC 162 ms 60280 KiB
007.txt AC 297 ms 86728 KiB
008.txt AC 171 ms 52172 KiB
009.txt AC 174 ms 54696 KiB
010.txt AC 265 ms 90160 KiB
011.txt AC 242 ms 90668 KiB
012.txt AC 179 ms 81404 KiB
013.txt AC 296 ms 81576 KiB
014.txt AC 349 ms 85008 KiB
015.txt AC 277 ms 84996 KiB
016.txt AC 350 ms 85544 KiB
017.txt AC 273 ms 85188 KiB
018.txt AC 246 ms 91176 KiB
019.txt AC 254 ms 90920 KiB
020.txt AC 186 ms 81272 KiB
021.txt AC 228 ms 81316 KiB
022.txt AC 336 ms 85536 KiB
023.txt AC 311 ms 85000 KiB
024.txt AC 331 ms 85028 KiB
025.txt AC 320 ms 85020 KiB
example0.txt AC 82 ms 27948 KiB
example1.txt AC 73 ms 27916 KiB