提出 #28404691


ソースコード 拡げる

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 - 1; i++)
            {
                queue.Enqueue(P[i]);
            }

            var answer = new List<int>();
            for (var i = K - 1; i < N; i++)
            {
                queue.Enqueue(P[i]);
                while (queue.Count > K)
                {
                    queue.Dequeue();
                }

                var min = queue.Dequeue();
                answer.Add(min);
                queue.Enqueue(min);
            }
            Console.WriteLine(string.Join("\n", 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>();
        }
    }
}

提出情報

提出日時
問題 D - Prefix K-th Max
ユーザ AconCavy
言語 C# (.NET Core 3.1.201)
得点 400
コード長 6669 Byte
結果 AC
実行時間 809 ms
メモリ 111684 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 2
AC × 28
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
000.txt AC 111 ms 28248 KiB
001.txt AC 289 ms 105996 KiB
002.txt AC 209 ms 81556 KiB
003.txt AC 568 ms 97236 KiB
004.txt AC 676 ms 103444 KiB
005.txt AC 636 ms 96608 KiB
006.txt AC 371 ms 72388 KiB
007.txt AC 809 ms 109184 KiB
008.txt AC 261 ms 55908 KiB
009.txt AC 336 ms 59120 KiB
010.txt AC 282 ms 111684 KiB
011.txt AC 281 ms 105504 KiB
012.txt AC 175 ms 81344 KiB
013.txt AC 305 ms 81068 KiB
014.txt AC 624 ms 97036 KiB
015.txt AC 631 ms 97716 KiB
016.txt AC 529 ms 96488 KiB
017.txt AC 719 ms 97992 KiB
018.txt AC 282 ms 105524 KiB
019.txt AC 280 ms 105712 KiB
020.txt AC 177 ms 81560 KiB
021.txt AC 239 ms 81092 KiB
022.txt AC 522 ms 96876 KiB
023.txt AC 636 ms 98016 KiB
024.txt AC 528 ms 96872 KiB
025.txt AC 628 ms 97968 KiB
example0.txt AC 86 ms 28324 KiB
example1.txt AC 78 ms 28336 KiB