結果

問題 No.555 世界史のレポート
ユーザー RisenRisen
提出日時 2017-08-12 00:07:17
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,278 ms / 2,000 ms
コード長 2,679 bytes
コンパイル時間 3,556 ms
コンパイル使用メモリ 105,756 KB
実行使用メモリ 34,832 KB
最終ジャッジ日時 2023-08-03 00:16:27
合計ジャッジ時間 6,725 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
23,200 KB
testcase_01 AC 72 ms
23,144 KB
testcase_02 AC 72 ms
23,308 KB
testcase_03 AC 75 ms
23,200 KB
testcase_04 AC 75 ms
23,364 KB
testcase_05 AC 76 ms
23,312 KB
testcase_06 AC 74 ms
23,148 KB
testcase_07 AC 75 ms
25,192 KB
testcase_08 AC 74 ms
23,332 KB
testcase_09 AC 77 ms
25,368 KB
testcase_10 AC 133 ms
29,752 KB
testcase_11 AC 114 ms
27,572 KB
testcase_12 AC 168 ms
27,928 KB
testcase_13 AC 247 ms
30,732 KB
testcase_14 AC 1,278 ms
34,832 KB
testcase_15 AC 89 ms
27,332 KB
testcase_16 AC 117 ms
29,396 KB
testcase_17 AC 91 ms
29,376 KB
testcase_18 AC 118 ms
29,392 KB
testcase_19 AC 117 ms
27,472 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;

public class PriorityQueue<TKey, TValue>
{
    SortedDictionary<TKey, HashSet<TValue>> dict = new SortedDictionary<TKey, HashSet<TValue>>();
    int count = 0;

    public int Count
    {
        get
        {
            return count;
        }
    }

    public void Enqueue(TKey key, TValue value)
    {
        if (!dict.ContainsKey(key))
        {
            dict[key] = new HashSet<TValue>();
        }

        dict[key].Add(value);
        count++;
    }

    public KeyValuePair<TKey, TValue> Dequeue()
    {
        var queue = dict.First();
        if (queue.Value.Count <= 1)
        {
            dict.Remove(queue.Key);
        }

        var val = queue.Value.First();
        queue.Value.Remove(val);
        count--;
        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }

    public KeyValuePair<TKey, TValue> Peek()
    {
        var queue = dict.First();
        var val = queue.Value.First();
        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }

    public void ChangePriority(TKey prevKey, TKey newKey, TValue value)
    {
        var set = dict[prevKey];
        set.Remove(value);
        if (set.Count == 0)
        {
            dict.Remove(prevKey);
        }
        count--;

        Enqueue(newKey, value);
    }
}

class Solution
{
    internal struct State
    {
        internal int Cost;
        internal int Length;
        internal int CopiedLength;

        internal State(int c, int l, int cl)
        {
            Cost = c;
            Length = l;
            CopiedLength = cl;
        }
    }

    static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var c = vals[0];
        var v = vals[1];

        // cost -> length, copied length
        var states = new PriorityQueue<int, State>();
        states.Enqueue(0, new State(c, 1, 1) );

        State best = states.Peek().Value;
        while (states.Count > 0)
        {
            var cur = states.Dequeue().Value;

            if (cur.Length >= n)
            {
                best = cur;
                break;
            }

            // copy
            if (cur.Length != cur.CopiedLength)
            {
                var copied = new State(cur.Cost + c, cur.Length, cur.Length);
                states.Enqueue(copied.Cost, copied);
            }
            // paste
            var paste = new State(cur.Cost + v, cur.Length + cur.CopiedLength, cur.CopiedLength);
            states.Enqueue(paste.Cost, paste);
        }

        Console.WriteLine(best.Cost);
    }
}
0