結果

問題 No.281 門松と魔法(1)
ユーザー 14番14番
提出日時 2016-05-14 17:37:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 22 ms / 1,000 ms
コード長 3,991 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 106,368 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-04-24 12:41:15
合計ジャッジ時間 3,210 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
17,536 KB
testcase_01 AC 21 ms
17,536 KB
testcase_02 AC 20 ms
17,536 KB
testcase_03 AC 21 ms
17,536 KB
testcase_04 AC 21 ms
17,664 KB
testcase_05 AC 20 ms
17,536 KB
testcase_06 AC 21 ms
17,536 KB
testcase_07 AC 20 ms
17,408 KB
testcase_08 AC 20 ms
17,792 KB
testcase_09 AC 20 ms
17,536 KB
testcase_10 AC 20 ms
17,536 KB
testcase_11 AC 21 ms
17,664 KB
testcase_12 AC 21 ms
17,536 KB
testcase_13 AC 19 ms
17,536 KB
testcase_14 AC 20 ms
17,536 KB
testcase_15 AC 20 ms
17,536 KB
testcase_16 AC 19 ms
17,536 KB
testcase_17 AC 20 ms
17,536 KB
testcase_18 AC 20 ms
17,536 KB
testcase_19 AC 21 ms
17,408 KB
testcase_20 AC 21 ms
17,536 KB
testcase_21 AC 20 ms
17,536 KB
testcase_22 AC 20 ms
17,408 KB
testcase_23 AC 20 ms
17,408 KB
testcase_24 AC 19 ms
17,664 KB
testcase_25 AC 19 ms
17,664 KB
testcase_26 AC 19 ms
17,536 KB
testcase_27 AC 19 ms
17,536 KB
testcase_28 AC 20 ms
17,792 KB
testcase_29 AC 21 ms
17,536 KB
testcase_30 AC 20 ms
17,408 KB
testcase_31 AC 20 ms
17,536 KB
testcase_32 AC 20 ms
17,792 KB
testcase_33 AC 20 ms
17,664 KB
testcase_34 AC 21 ms
17,536 KB
testcase_35 AC 21 ms
17,536 KB
testcase_36 AC 22 ms
17,664 KB
testcase_37 AC 22 ms
17,664 KB
testcase_38 AC 22 ms
17,664 KB
testcase_39 AC 21 ms
17,536 KB
testcase_40 AC 21 ms
17,664 KB
testcase_41 AC 21 ms
17,408 KB
testcase_42 AC 21 ms
17,792 KB
testcase_43 AC 21 ms
17,664 KB
testcase_44 AC 19 ms
17,664 KB
testcase_45 AC 20 ms
17,536 KB
testcase_46 AC 20 ms
17,408 KB
testcase_47 AC 19 ms
17,536 KB
testcase_48 AC 19 ms
17,536 KB
testcase_49 AC 19 ms
17,664 KB
testcase_50 AC 21 ms
17,280 KB
testcase_51 AC 22 ms
17,536 KB
testcase_52 AC 21 ms
17,536 KB
testcase_53 AC 21 ms
17,536 KB
testcase_54 AC 21 ms
17,408 KB
testcase_55 AC 20 ms
17,536 KB
testcase_56 AC 21 ms
17,536 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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        long minus = long.Parse(Reader.ReadLine());
        long take1 = long.Parse(Reader.ReadLine());
        long take2 = long.Parse(Reader.ReadLine());
        long take3 = long.Parse(Reader.ReadLine());
        
        long ans = -1;
        // 真ん中を減らす場合
        long ret = this.GetAns1(take1, take2, take3, minus);
        if(ret >= 0) {
            ans = ret;
        }
        
        ret = this.GetAns2(take1, take2, take3, minus);
        if(ret >= 0) {
            if(ans == -1) {
                ans = ret;
            } else {
                ans = Math.Min(ans, ret);
            }
        }
        
        Console.WriteLine(ans);

    }
    
    private long GetAns2(long take1, long take2, long take3, long minus) {
        // 真ん中が引っ込む場合
        long tmp1 = take1;
        long tmp2 = take2;
        long tmp3 = take3;
        long ret = 0;
        
        if(minus == 0) {
            return -1;
        }
        
        if(tmp1 == tmp3) {
            if(tmp1 == 0) {
                return -1;
            }
            tmp1 = Math.Max(0, tmp1 - minus);
            ret++;
        }
        long times = (tmp2 - Math.Min(tmp1,tmp3)) / minus + 1;
        tmp2 = Math.Max(0, tmp2 - times * minus);
        ret += times;
        if(tmp2 == tmp1 || tmp2 == tmp3) {
            if(tmp2 == 0) {
                return -1;
            }
             else
             {
                 tmp2 = Math.Max(0, tmp2 - minus);
                 ret++;
             }
        }
        return ret;
        
    }
    private long GetAns1(long take1, long take2, long take3, long minus) {
        // 最初から揃っている場合
        if(take1 != take2 && take2 != take3 && take3 != take1) {
            if(take2 > Math.Max(take1, take3)) {
                return 0;
            }
            if(take2 < Math.Min(take1, take3)) {
                return 0;
            }
        }
        if(minus == 0) {
            return -1;
        }
        
        // 真ん中が出る場合
        long tmp1 = take1;
        long tmp2 = take2;
        long tmp3 = take3;
        long ret = 0;
        if(tmp1 >= tmp2) {
            long times = (take1 - take2) / minus + 1;
            ret += times;
            tmp1 = Math.Max(0, tmp1 - (times * minus));
        }
        if(tmp3 >= tmp2) {
            long times = (take3 - take2) / minus + 1;
            ret += times;
            tmp3 = Math.Max(0, tmp3 - (times * minus));
        }
        if(tmp1 == tmp3) {
            if(tmp1 == 0) {
                return -1;
            } else
            {
                ret += 1;
            }
        }
        return ret;
    }



    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


0
0
1
2


 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0