結果

問題 No.59 鉄道の旅
ユーザー nanophoto12nanophoto12
提出日時 2014-11-07 01:17:12
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 525 ms / 5,000 ms
コード長 2,724 bytes
コンパイル時間 2,137 ms
コンパイル使用メモリ 105,184 KB
実行使用メモリ 29,076 KB
最終ジャッジ日時 2023-08-26 05:29:06
合計ジャッジ時間 4,833 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,928 KB
testcase_01 AC 63 ms
23,840 KB
testcase_02 AC 62 ms
23,796 KB
testcase_03 AC 63 ms
21,828 KB
testcase_04 AC 160 ms
26,224 KB
testcase_05 AC 61 ms
21,876 KB
testcase_06 AC 63 ms
23,892 KB
testcase_07 AC 63 ms
21,912 KB
testcase_08 AC 70 ms
23,900 KB
testcase_09 AC 69 ms
23,928 KB
testcase_10 AC 69 ms
21,816 KB
testcase_11 AC 65 ms
21,812 KB
testcase_12 AC 90 ms
25,988 KB
testcase_13 AC 220 ms
26,396 KB
testcase_14 AC 525 ms
29,076 KB
testcase_15 AC 61 ms
21,924 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Collections.Generic;
using System.Linq;

internal class Program
{
    private static void Push(List<int> list, int element)
    {
        var left = 0;
        var right = list.Count - 1;
        while (right - left > 1)
        {
            int middle = (right + left) / 2;
            if (list[middle] == element)
            {
                list.Insert(middle, element);
                return;
            }
            if (list[middle] > element)
            {
                right = middle;
            }
            else
            {
                left = middle;
            }
        }
        for (int i = left; i <= right; i++)
        {
            if (list[i] == element)
            {
                list.Insert(i, element);
                return;
            }
            if (list[i] > element)
            {
                list.Insert(i, element);
                return;
            }
        }
        list.Add(element);
    }

    private static void Remove(List<int> list, int element)
    {
        var left = 0;
        var right = list.Count - 1;
        while (right - left > 1)
        {
            int middle = (right + left) / 2;
            if (list[middle] == element)
            {
                list.RemoveAt(middle);
                return;
            }
            if (list[middle] > element)
            {
                right = middle;
            }
            else
            {
                left = middle;
            }
        }
        for (int i = left; i <= right; i++)
        {
            if (list[i] == element)
            {
                list.RemoveAt(i);
                return;
            }
        }
    }

    public static void Main(string[] args)
    {
        var nAndK = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var n = nAndK[0];
        var k = nAndK[1];
        var w = new int[n];
        for (int i = 0; i < n; i++)
        {
            w[i] = int.Parse(Console.ReadLine());
        }
        var list = new List<int>();

        for (int i = 0; i < n; i++)
        {
            if (w[i] < 0)
            {
                Remove(list, -w[i]);
            }
            else
            {
                int index = list.Count - k;
                if (index < 0)
                {
                    Push(list, w[i]);
                    continue;
                }
                if (list[index] >= w[i])
                {
                    continue;
                }
                Push(list, w[i]);
            }
        }
        Console.WriteLine(list.Count);
    }
}
0