結果

問題 No.837 Noelちゃんと星々2
ユーザー sekiya9311sekiya9311
提出日時 2019-06-14 22:41:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 9,482 bytes
コンパイル時間 2,188 ms
コンパイル使用メモリ 112,128 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2024-06-11 14:16:51
合計ジャッジ時間 6,317 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 244 ms
40,192 KB
testcase_01 AC 241 ms
40,192 KB
testcase_02 AC 242 ms
40,064 KB
testcase_03 AC 244 ms
40,064 KB
testcase_04 AC 245 ms
40,192 KB
testcase_05 AC 240 ms
40,192 KB
testcase_06 AC 242 ms
40,192 KB
testcase_07 AC 242 ms
40,448 KB
testcase_08 AC 246 ms
40,192 KB
testcase_09 AC 33 ms
20,352 KB
testcase_10 AC 32 ms
20,352 KB
testcase_11 AC 31 ms
20,736 KB
testcase_12 AC 31 ms
20,480 KB
testcase_13 AC 32 ms
20,736 KB
testcase_14 AC 32 ms
20,224 KB
testcase_15 AC 32 ms
20,096 KB
testcase_16 AC 31 ms
20,480 KB
testcase_17 AC 32 ms
20,352 KB
testcase_18 AC 32 ms
20,480 KB
testcase_19 AC 30 ms
20,864 KB
testcase_20 AC 31 ms
20,736 KB
testcase_21 AC 31 ms
20,480 KB
testcase_22 AC 31 ms
20,608 KB
testcase_23 AC 37 ms
20,352 KB
testcase_24 AC 33 ms
20,864 KB
testcase_25 AC 32 ms
20,352 KB
testcase_26 AC 31 ms
20,480 KB
testcase_27 AC 31 ms
20,608 KB
testcase_28 AC 31 ms
20,480 KB
testcase_29 AC 32 ms
20,352 KB
testcase_30 AC 31 ms
20,736 KB
testcase_31 AC 31 ms
20,224 KB
testcase_32 AC 30 ms
20,352 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.Linq;
using System.Text;
using System.Numerics;

namespace ProgrammingContest
{
    class Writer : IDisposable
    {
        private System.IO.TextWriter Out { get; }
        private StringBuilder Sb { get; }
        private bool IsReactive { get; }
        public Writer(string path)
            : this(new System.IO.StreamWriter(path))
        {
        }
        public Writer(bool isReactive)
            : this(null, isReactive)
        {
        }
        public Writer(System.IO.TextWriter writer = null, bool isReactive = false)
        {
            this.Out = (writer ?? Console.Out);
            this.IsReactive = isReactive;
            if (!this.IsReactive)
            {
                this.Sb = new StringBuilder();
            }
        }
        public void Dispose()
        {
            if (!this.IsReactive)
            {
                this.Out.Write(Sb.ToString());
            }
            if (!this.Out.Equals(Console.Out))
            {
                this.Out.Dispose();
            }
        }
        public void Write(object val)
        {
            if (this.IsReactive)
            {
                this.Out.Write(val.ToString());
                this.Out.Flush();
            }
            else
            {
                this.Sb.Append(val.ToString());
            }
        }
        public void WriteFormat(string format, params object[] vals)
        {
            if (this.IsReactive)
            {
                this.Out.Write(format, vals);
                this.Out.Flush();
            }
            else
            {
                this.Sb.AppendFormat(format, vals);
            }
        }

        public void WriteLine(object val = null)
            => this.WriteFormat((val?.ToString() ?? string.Empty) + Environment.NewLine);

        public void WriteLine(int val)
            => this.WriteLine(val.ToString());

        public void WriteLine(long val)
            => this.WriteLine(val.ToString());

        public void WriteLine(string val)
            => this.WriteLine((object)val);

        public void WriteLine(string format, params object[] vals)
            => this.WriteFormat(format + Environment.NewLine, vals);
    }

    class Scanner : IDisposable
    {
        private Queue<string> Buffer { get; }
        private char[] Sep { get; }
        private System.IO.TextReader Reader { get; }
        public Scanner(string path, char[] sep = null)
            : this(new System.IO.StreamReader(path), sep)
        {
        }
        public Scanner(System.IO.TextReader reader = null,
                        char[] sep = null)
        {
            this.Buffer = new Queue<string>();
            this.Sep = (sep ?? new char[] { ' ' });
            this.Reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.Buffer.Count == 0 && this.Reader.Peek() != -1)
            {
                string str = string.Empty;
                for (; string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str);
                str = this.Reader.ReadLine()) ;

                var strs = str.Split(this.Sep)
                    .Where(el => !(string.IsNullOrEmpty(el) || string.IsNullOrWhiteSpace(el)));
                foreach (var el in strs)
                {
                    this.Buffer.Enqueue(el);
                }
            }
        }

        public void Dispose()
        {
            if (!this.Reader.Equals(Console.In))
            {
                this.Reader.Dispose();
            }
        }

        public string Next()
        {
            this.CheckBuffer();
            return this.Buffer.Dequeue();
        }

        public string[] GetStringArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.Next())
                         .ToArray();

        public int NextInt()
            => int.Parse(this.Next());

        public int[] GetIntArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextInt())
                         .ToArray();

        public double NextDouble()
            => double.Parse(this.Next());

        public double[] GetdoubleArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextDouble())
                         .ToArray();

        public long NextLong()
            => long.Parse(this.Next());

        public long[] GetLongArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextLong())
                         .ToArray();

        public BigInteger NextBigInt()
            => BigInteger.Parse(this.Next());

        public BigInteger[] GetBigIntArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextBigInt())
                         .ToArray();

        public bool IsEnd
        {
            get
            {
                this.CheckBuffer();
                return this.Buffer.Count == 0;
            }
        }
    }

    class MainClass : IDisposable
    {
        private Scanner Sc { get; }
        private Writer Wr { get; }
        private string InFilePath => "in.txt";
        private string OutFilePath => "out.txt";
        public MainClass()
        {
            this.Wr = new Writer(this.IsReactive);
            //this.Wr = new Writer(this.OutFilePath);
#if DEBUG
            if (!this.IsReactive)
            {
                this.Sc = new Scanner(this.InFilePath);
            }
            else
            {
                this.Sc = new Scanner();
            }
#else
            this.Sc = new Scanner();
#endif
            this.Solve();
        }
        static void Main(string[] args)
        {
            using (new MainClass()) { }
        }

        public void Dispose()
        {
            this.Sc?.Dispose();
            this.Wr?.Dispose();
#if DEBUG
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
#endif
        }

        void Solve()
        {
            int N = Sc.NextInt();
            var Y = Sc.GetLongArray(N);

            Array.Sort(Y);
            var dCnt = new HashSet<long>(Y).Count;
            if (dCnt == 1)
            {
                Wr.WriteLine(1);
                return;
            }
            else if (dCnt == 2)
            {
                Wr.WriteLine(0);
                return;
            }

            long[] acc = new long[N + 1];
            for (int i = 0; i < N; i++)
            {
                acc[i + 1] = acc[i] + Y[i];
            }
            Func<int, int, long> CalcSum = (l, r) => acc[r] - acc[l];

            long ans = (long)1e18;
            for (int i = 1; i < N - 1; i++)
            {
                // [l_l, l_r), [r_l, r_r)
                int l_l = 0;
                int l_r = i;
                int r_l = i;
                int r_r = N;

                long left = (long)1e18;
                {
                    int low = l_l, high = l_r;
                    while (high - low > 10)
                    {
                        int mid1 = (low + low + high) / 3;
                        int mid2 = (low + high + high) / 3;

                        var sum1 = Math.Abs(CalcSum(l_l, mid1) - Y[mid1] * (mid1 - l_l)) + Math.Abs(CalcSum(mid1, l_r) - Y[mid1] * (l_r - mid1));
                        var sum2 = Math.Abs(CalcSum(l_l, mid2) - Y[mid2] * (mid2 - l_l)) + Math.Abs(CalcSum(mid2, l_r) - Y[mid2] * (l_r - mid2));
                        if (sum1 <= sum2)
                        {
                            high = mid2;
                        }
                        else
                        {
                            low = mid1;
                        }
                    }

                    for (int j = low; j < high; j++)
                    {
                        left = Math.Min(left, Math.Abs(CalcSum(l_l, j) - Y[j] * (j - l_l)) + Math.Abs(CalcSum(j, l_r) - Y[j] * (l_r - j)));
                    }
                }

                long right = (long)1e18;
                {
                    int low = r_l, high = r_r;
                    while (high - low > 10)
                    {
                        int mid1 = (low + low + high) / 3;
                        int mid2 = (low + high + high) / 3;

                        var sum1 = Math.Abs(CalcSum(r_l, mid1) - Y[mid1] * (mid1 - r_l)) + Math.Abs(CalcSum(mid1, r_r) - Y[mid1] * (r_r - mid1));
                        var sum2 = Math.Abs(CalcSum(r_l, mid2) - Y[mid2] * (mid2 - r_l)) + Math.Abs(CalcSum(mid2, r_r) - Y[mid2] * (r_r - mid2));
                        if (sum1 <= sum2)
                        {
                            high = mid2;
                        }
                        else
                        {
                            low = mid1;
                        }
                    }

                    for (int j = low; j < high; j++)
                    {
                        right = Math.Min(right, Math.Abs(CalcSum(r_l, j) - Y[j] * (j - r_l)) + Math.Abs(CalcSum(j, r_r) - Y[j] * (r_r - j)));
                    }
                }

                //Wr.WriteLine($"{Y[(l_r - l_l)/2]}, {Y[(r_r - r_l) / 2 + r_l - 1]}");
                ans = Math.Min(ans, left + right);
            }

            Wr.WriteLine(ans);
        }

        private bool IsReactive => false; // TODO: reactive check !!
    }
}
0