結果

問題 No.198 キャンディー・ボックス2
ユーザー sekiya9311sekiya9311
提出日時 2017-10-07 02:15:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 61 ms / 1,000 ms
コード長 3,784 bytes
コンパイル時間 3,745 ms
コンパイル使用メモリ 108,520 KB
実行使用メモリ 23,732 KB
最終ジャッジ日時 2023-08-10 08:34:44
合計ジャッジ時間 6,306 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
21,548 KB
testcase_01 AC 60 ms
23,544 KB
testcase_02 AC 58 ms
21,504 KB
testcase_03 AC 58 ms
19,480 KB
testcase_04 AC 59 ms
23,476 KB
testcase_05 AC 59 ms
21,560 KB
testcase_06 AC 60 ms
23,600 KB
testcase_07 AC 57 ms
21,552 KB
testcase_08 AC 57 ms
21,508 KB
testcase_09 AC 59 ms
23,580 KB
testcase_10 AC 59 ms
19,540 KB
testcase_11 AC 59 ms
21,552 KB
testcase_12 AC 57 ms
21,556 KB
testcase_13 AC 61 ms
21,760 KB
testcase_14 AC 58 ms
21,464 KB
testcase_15 AC 58 ms
21,568 KB
testcase_16 AC 57 ms
21,552 KB
testcase_17 AC 58 ms
23,560 KB
testcase_18 AC 59 ms
21,596 KB
testcase_19 AC 58 ms
21,540 KB
testcase_20 AC 57 ms
21,572 KB
testcase_21 AC 57 ms
21,428 KB
testcase_22 AC 56 ms
23,544 KB
testcase_23 AC 59 ms
21,496 KB
testcase_24 AC 59 ms
21,772 KB
testcase_25 AC 57 ms
23,584 KB
testcase_26 AC 56 ms
21,672 KB
testcase_27 AC 56 ms
23,732 KB
testcase_28 AC 57 ms
21,600 KB
testcase_29 AC 59 ms
21,636 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;
using System.Text;
using System.Threading.Tasks;

namespace ProgrammingContest
{
    class MainClass
    {
        Scanner sc;
        static void Main(string[] args)
        {
            new MainClass().Solve();
        }

        const bool DEBUG = false;   // TODO: must check!!
        void Solve()
        {
            if (DEBUG)
            {
                string backPath = "..";
                char dirSep = System.IO.Path.DirectorySeparatorChar;
                string inFilePath = backPath + dirSep + backPath + dirSep + "in.txt";
                sc = new Scanner(new System.IO.StreamReader(inFilePath));
            }
            else
            {
                sc = new Scanner();
            }
            long B = sc.NextInt;
            int N = sc.NextInt;
            long[] C = new long[N];
            for (int i = 0; i < N; i++)
            {
                C[i] = sc.NextInt;
            }
            Array.Sort(C);
            Array.Reverse(C);
            long high = (long) 1e18;
            long low = 0;
            while (high - low > 10)
            {
                long mid1 = (high + low * 2) / 3;
                long mid2 = (high * 2 + low) / 3;
                if (calc(B, C, mid1) > calc(B, C, mid2))
                {
                    low = mid1;
                }
                else
                {
                    high = mid2;
                }
            }
            long ans = (long)1e18;
            for (long i = Math.Max(0, low); i <= Math.Min(high + 5, (C.Sum() + B) / N); i++)
            {
                ans = Math.Min(ans, calc(B, C, i));
            }
            Console.WriteLine(ans);
        }
        long calc(long B, long[] C, long need)
        {
            long res = 0;
            long have = B;
            foreach(var c in C)
            {
                if (need > c)
                {
                    have -= need - c;
                    res += need - c;
                }
                else if (need < c)
                {
                    have += c - need;
                    res += c - need;
                }
                if (have < 0)
                {
                    return (long)1e18;
                }
            }
            return res;
        }
    }
    class Scanner
    {
        Queue<String> buffer;
        char[] sep;
        System.IO.TextReader reader;
        public Scanner(System.IO.TextReader reader = null)
        {
            this.buffer = new Queue<string>();
            this.sep = new char[] { ' ' };
            this.reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.buffer.Count == 0)
            {
                String[] sreArray = this.reader.ReadLine().Split(this.sep);
                foreach (String elStr in sreArray)
                {
                    this.buffer.Enqueue(elStr);
                }
            }
        }

        public String Next
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Dequeue();
            }
        }

        public int NextInt
        {
            get
            {
                return int.Parse(this.Next);
            }
        }
        public double NextDouble
        {
            get
            {
                return double.Parse(this.Next);
            }
        }

        public long NextLong
        {
            get
            {
                return long.Parse(this.Next);
            }
        }
        public bool IsEmpty
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Count == 0;
            }
        }
    }
}
0