結果

問題 No.59 鉄道の旅
ユーザー mbanmban
提出日時 2017-07-25 11:11:21
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 147 ms / 5,000 ms
コード長 2,278 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 107,468 KB
実行使用メモリ 30,620 KB
最終ジャッジ日時 2023-07-30 22:07:24
合計ジャッジ時間 4,318 ms
ジャッジサーバーID
(参考情報)
judge13 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,840 KB
testcase_01 AC 57 ms
20,796 KB
testcase_02 AC 58 ms
20,856 KB
testcase_03 AC 57 ms
20,748 KB
testcase_04 AC 140 ms
30,620 KB
testcase_05 AC 58 ms
24,328 KB
testcase_06 AC 58 ms
20,748 KB
testcase_07 AC 58 ms
24,496 KB
testcase_08 AC 76 ms
27,956 KB
testcase_09 AC 70 ms
28,128 KB
testcase_10 AC 73 ms
27,396 KB
testcase_11 AC 65 ms
20,748 KB
testcase_12 AC 133 ms
24,852 KB
testcase_13 AC 124 ms
25,632 KB
testcase_14 AC 147 ms
29,368 KB
testcase_15 AC 59 ms
24,296 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;

class Program
{
    static void Main()
    {
        new Magatro().Solve();
    }
}

class Magatro
{
    private int N, K;
    private int[] W;
    private void Scan()
    {
        var line = Console.ReadLine().Split(' ');
        N = int.Parse(line[0]);
        K = int.Parse(line[1]);
        W = new int[N];
        for (int i = 0; i < N; i++)
        {
            W[i] = int.Parse(Console.ReadLine());
        }
    }

    public void Solve()
    {
        Scan();
        var segtree = new SegmentTree(1000001);
        foreach (int i in W)
        {
            if (i > 0)
            {
                if (segtree.Query(i, 1000001) < K)
                {
                    segtree.Update(i, segtree.Get(i) + 1);
                }
            }
            else
            {
                int j = -i;
                segtree.Update(j, Math.Max(0, segtree.Get(j) - 1));
            }
        }
        Console.WriteLine(segtree.Tree[0]);
    }
}

class SegmentTree
{
    private int N;
    public int[] Tree;

    public SegmentTree(int length)
    {
        for (N = 1; N <= length; N *= 2) ;
        Tree = (new int[2 * N - 1]);
    }
    /// <summary>
    /// index(0_indexed)をvに更新
    /// </summary>
    /// <param name="index"></param>
    /// <param name="v"></param>
    public void Update(int index, int v)
    {
        int i = index + N - 1;
        Tree[i] = v;
        while (i > 0)
        {
            i = (i - 1) / 2;
            Tree[i] = Tree[i * 2 + 1] + Tree[i * 2 + 2];
        }
    }

    public int Get(int n)
    {
        return Tree[n + N - 1];
    }

    public int Query(int left, int right)
    {
        return Query(left, right, 0, 0, N);
    }

    private int Query(int a, int b, int k, int l, int r)
    {
        if (r <= a || b <= l) return 0;
        if (a <= l && r <= b)
        {
            return Tree[k];
        }
        else
        {
            int vl = Query(a, b, k * 2 + 1, l, (l + r) / 2);
            int vr = Query(a, b, k * 2 + 2, (l + r) / 2, r);
            return vl + vr; ;
        }
    }
}
0