結果

問題 No.634 硬貨の枚数1
ユーザー バカらっく
提出日時 2018-01-19 22:20:59
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 2,853 bytes
コンパイル時間 2,776 ms
コンパイル使用メモリ 107,392 KB
実行使用メモリ 60,160 KB
最終ジャッジ日時 2024-12-25 19:17:46
合計ジャッジ時間 160,524 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27 TLE * 48
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 num = int.Parse(Reader.ReadLine());
        this.SetSankakuList(num);
        int ans = GetAns(0, num, 0);
        Console.WriteLine(ans);
    }

    private Dictionary<int, Dictionary<int, int>> dic = new Dictionary<int, Dictionary<int, int>>();

    private int Ans = int.MaxValue;
    private int GetAns(int idx, int remain, int subTotal) {
        if(subTotal >= Ans) {
            return remain;
        }
        if(remain<=0) {
            Ans = Math.Min(Ans, subTotal);
            return 0;
        }
        if(SankakuList[idx]==1) {
            Ans = Math.Min(Ans, remain)+subTotal;
            return remain;
        }
        if(remain%SankakuList[idx]==0) {
            Ans = Math.Min(Ans, remain / SankakuList[idx] + subTotal);
            return remain / SankakuList[idx];
        }
        if(SankakuList[idx]>remain) {
            int tmp = SankakuList.FindIndex(a => a <= remain);
            return GetAns(tmp, remain, subTotal);
        }
        if(SankakuList.Contains(remain)) {
            Ans = Math.Min(Ans, subTotal + 1);
            return 1;
        }


        if(!dic.ContainsKey(idx)) {
            dic.Add(idx, new Dictionary<int, int>());
        }
        if(dic[idx].ContainsKey(remain)) {
            return dic[idx][remain];
        }

        int ans = remain;
        for (int i = 0; i * SankakuList[idx] <= remain; i++) {
            int ret = GetAns(idx + 1, remain - (i * SankakuList[idx]), subTotal+i) + i;
            ans = Math.Min(ans, ret);
        }

        dic[idx][remain] = ans;
        if(idx == 0) {
            this.Ans = Math.Min(ans, this.Ans);
        }
        return ans;
    }

    private List<int> SankakuList = new List<int>();
    private void SetSankakuList(int max) {
        
        for (int i = 1; i <= max; i++) {
            int num = i * (i + 1) / 2;
            if(num>max) {
                break;
            }
            SankakuList.Add(num);
        }
        SankakuList.Reverse();
    }



    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 = @"



15415



";
    }

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