結果

問題 No.649 ここでちょっとQK!
ユーザー バカらっくバカらっく
提出日時 2018-02-10 00:50:50
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,345 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 114,616 KB
実行使用メモリ 40,600 KB
最終ジャッジ日時 2024-04-17 15:07:27
合計ジャッジ時間 6,341 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
26,496 KB
testcase_01 AC 30 ms
20,864 KB
testcase_02 AC 34 ms
20,864 KB
testcase_03 AC 120 ms
26,496 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

public class Program
{

    public void Proc()
    {
        int[] qk = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        int q = qk[0];
        int k = qk[1];
        PriorityQueue first = new PriorityQueue();
        PriorityQueue second = new PriorityQueue();
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < q; i++) {
            long[] inpt = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();
            if(inpt[0]==1) {
                long num = inpt[1];
                if(first.Count<k) {
                    first.Push(num);
                } else {
                    if(first.PeekLast()>num) {
                        first.Push(num);
                        second.Push(first.PopLast());
                    } else {
                        second.Push(num);
                    }
                }
            } else {
                if(first.Count<k) {
                    ans.AppendLine("-1");
                } else {
                    ans.AppendLine(first.PopLast().ToString());
                    if(second.Count>0) {
                        first.Push(second.PopFirst());
                    }
                }
            }
        }
        Console.Write(ans.ToString());
    }

    private class PriorityQueue
    {
        private SortedDictionary<long, int> dic = new SortedDictionary<long, int>();
        private int ItemCount = 0;

        public int Count
        {
            get
            {
                return ItemCount;
            }
        }
        public long PopFirst()
        {
            long num = dic.First().Key;
            Remove(num);
            return num;
        }

        public long PeekLast() {
            return dic.Last().Key;
        }
    
        public long PopLast() {
            long num = dic.Last().Key;
            Remove(num);
            return num;
        }

        public void Push(long num) {
            Add(num);
        }

        private void Remove(long key) {
            if(!dic.ContainsKey(key)) {
                return;
            }
            if(dic[key]<=1) {
                dic.Remove(key);
            } else {
                dic[key]--;
            }
            ItemCount--;
        }

        private void Add(long key) {
            if(dic.ContainsKey(key)) {
                dic[key]++; 
            } else {
                dic.Add(key, 1);
            }
            ItemCount++;
        }
    }

    public class Reader
    {
        private static StringReader sr;
        public static bool IsDebug = false;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (sr == null)
                {
                    sr = new StringReader(InputText.Trim());
                }
                return sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        private static string InputText = @"



15 3
1 3
1 4
1 5
2
2
1 10
1 10
1 1
2
1 3
2
1 1000
2
1 0
2


";
    }

    public static void Main(string[] args)
    {
#if DEBUG
        Reader.IsDebug = true;
#endif
        Program prg = new Program();
        prg.Proc();
    }
}
0