結果

問題 No.1199 お菓子配り-2
ユーザー terry_u16terry_u16
提出日時 2020-08-28 23:17:43
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 478 ms / 1,000 ms
コード長 13,308 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 125,892 KB
実行使用メモリ 51,052 KB
最終ジャッジ日時 2024-11-14 03:47:35
合計ジャッジ時間 18,410 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,392 KB
testcase_01 AC 31 ms
25,088 KB
testcase_02 AC 32 ms
27,252 KB
testcase_03 AC 126 ms
38,120 KB
testcase_04 AC 262 ms
49,388 KB
testcase_05 AC 43 ms
25,908 KB
testcase_06 AC 48 ms
27,992 KB
testcase_07 AC 144 ms
43,804 KB
testcase_08 AC 187 ms
48,580 KB
testcase_09 AC 128 ms
41,728 KB
testcase_10 AC 59 ms
30,232 KB
testcase_11 AC 107 ms
37,604 KB
testcase_12 AC 107 ms
39,368 KB
testcase_13 AC 62 ms
34,168 KB
testcase_14 AC 54 ms
29,748 KB
testcase_15 AC 52 ms
27,576 KB
testcase_16 AC 183 ms
47,604 KB
testcase_17 AC 106 ms
36,040 KB
testcase_18 AC 190 ms
44,048 KB
testcase_19 AC 73 ms
29,616 KB
testcase_20 AC 311 ms
48,864 KB
testcase_21 AC 203 ms
45,216 KB
testcase_22 AC 118 ms
32,052 KB
testcase_23 AC 159 ms
40,696 KB
testcase_24 AC 206 ms
45,204 KB
testcase_25 AC 63 ms
33,924 KB
testcase_26 AC 231 ms
46,824 KB
testcase_27 AC 196 ms
47,196 KB
testcase_28 AC 474 ms
50,724 KB
testcase_29 AC 473 ms
48,756 KB
testcase_30 AC 472 ms
48,808 KB
testcase_31 AC 472 ms
50,976 KB
testcase_32 AC 478 ms
49,080 KB
testcase_33 AC 471 ms
48,724 KB
testcase_34 AC 473 ms
48,776 KB
testcase_35 AC 468 ms
49,080 KB
testcase_36 AC 474 ms
50,688 KB
testcase_37 AC 473 ms
50,892 KB
testcase_38 AC 472 ms
51,052 KB
testcase_39 AC 476 ms
46,796 KB
testcase_40 AC 475 ms
50,864 KB
testcase_41 AC 474 ms
46,836 KB
testcase_42 AC 475 ms
51,024 KB
testcase_43 AC 473 ms
48,872 KB
testcase_44 AC 472 ms
48,872 KB
testcase_45 AC 470 ms
46,800 KB
testcase_46 AC 472 ms
48,952 KB
testcase_47 AC 477 ms
48,692 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.IO;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Yukicoder263.Extensions;
using Yukicoder263.Questions;

namespace Yukicoder263.Questions
{
    public class QuestionB : AtCoderQuestionBase
    {
        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var (snackCount, _) = inputStream.ReadValue<int, int>();
            var snacks = new long[snackCount];
            for (int i = 0; i < snacks.Length; i++)
            {
                snacks[i] = inputStream.ReadLongArray().Sum();
            }

            var dp = new long[snackCount + 1, 2];

            for (int i = 0; i < snacks.Length; i++)
            {
                dp[i + 1, 1] = Math.Max(dp[i, 1], dp[i, 0] + snacks[i]);
                dp[i + 1, 0] = Math.Max(dp[i, 0], dp[i, 1] - snacks[i]);
            }

            yield return Math.Max(dp[snackCount, 0], dp[snackCount, 1]);
        }

        public class Deque<T> : IReadOnlyCollection<T>
        {
            public int Count { get; private set; }
            private T[] _data;
            private int _first;
            private int _mask;

            public Deque() : this(4) { }

            public Deque(int minCapacity)
            {
                if (minCapacity <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(minCapacity), $"{nameof(minCapacity)}は0より大きい値でなければなりません。");
                }
                var capacity = GetPow2Over(minCapacity);
                _data = new T[capacity];
                _first = 0;
                _mask = capacity - 1;
            }

            public Deque(IEnumerable<T> collection)
            {
                var dataArray = collection.ToArray();
                var capacity = GetPow2Over(dataArray.Length);
                _data = new T[capacity];
                _first = 0;
                _mask = capacity - 1;

                for (int i = 0; i < dataArray.Length; i++)
                {
                    _data[i] = dataArray[i];
                    Count++;
                }
            }

            public T this[Index index]
            {
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                get
                {
                    var offset = index.GetOffset(Count);
                    if (unchecked((uint)offset) >= Count)
                    {
                        ThrowArgumentOutOfRangeException(nameof(index), $"{nameof(index)}がコレクションの範囲外です。");
                    }
                    return _data[(_first + offset) & _mask];
                }
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public void EnqueueFirst(T item)
            {
                if (_data.Length == Count)
                {
                    Resize();
                }

                _first = (_first - 1) & _mask;
                _data[_first] = item;
                Count++;
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public void EnqueueLast(T item)
            {
                if (_data.Length == Count)
                {
                    Resize();
                }

                _data[(_first + Count++) & _mask] = item;
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public T DequeueFirst()
            {
                if (Count == 0)
                {
                    ThrowInvalidOperationException("Queueが空です。");
                }

                var value = _data[_first];
                _data[_first++] = default;
                _first &= _mask;
                return value;
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public T DequeueLast()
            {
                if (Count == 0)
                {
                    ThrowInvalidOperationException("Queueが空です。");
                }

                var index = (_first + --Count) & _mask;
                var value = _data[index];
                _data[index] = default;
                return value;
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public T PeekFirst()
            {
                if (Count == 0)
                {
                    ThrowInvalidOperationException("Queueが空です。");
                }

                return _data[_first];
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public T PeekLast()
            {
                if (Count == 0)
                {
                    ThrowInvalidOperationException("Queueが空です。");
                }

                return _data[(_first + Count - 1) & _mask];
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            private void Resize()
            {
                var newArray = new T[_data.Length << 1];
                var span = _data.AsSpan();
                var firstHalf = span[_first..];
                var lastHalf = span[.._first];
                firstHalf.CopyTo(newArray);
                lastHalf.CopyTo(newArray.AsSpan(firstHalf.Length));
                _data = newArray;
                _first = 0;
                _mask = _data.Length - 1;
            }

            private void ThrowArgumentOutOfRangeException(string paramName, string message) => throw new ArgumentOutOfRangeException(paramName, message);
            private void ThrowInvalidOperationException(string message) => throw new InvalidOperationException(message);

            private int GetPow2Over(int n)
            {
                n--;
                var result = 1;
                while (n != 0)
                {
                    n >>= 1;
                    result <<= 1;
                }
                return result;
            }

            public IEnumerator<T> GetEnumerator()
            {
                for (int i = 0; i < Count; i++)
                {
                    var offset = (_first + i) & _mask;
                    yield return _data[offset];
                }
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
        }

    }
}

namespace Yukicoder263
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionB();
            var answers = question.Solve(Console.In);

            var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(writer);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            Console.Out.Flush();
        }
    }
}

#region Base Class

namespace Yukicoder263.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }
}

#endregion

#region Extensions

namespace Yukicoder263.Extensions
{
    public static class StringExtensions
    {
        public static string Join<T>(this IEnumerable<T> source) => string.Concat(source);
        public static string Join<T>(this IEnumerable<T> source, char separator) => string.Join(separator, source);
        public static string Join<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source);
    }

    public static class TextReaderExtensions
    {
        public static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        public static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        public static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        public static string ReadString(this TextReader reader) => reader.ReadLine();

        public static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        public static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        public static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        public static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);

        // Supports primitive type only.
        public static T1 ReadValue<T1>(this TextReader reader) => (T1)Convert.ChangeType(reader.ReadLine(), typeof(T1));

        public static (T1, T2) ReadValue<T1, T2>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            return (v1, v2);
        }

        public static (T1, T2, T3) ReadValue<T1, T2, T3>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            return (v1, v2, v3);
        }

        public static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            return (v1, v2, v3, v4);
        }

        public static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            return (v1, v2, v3, v4, v5);
        }

        public static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            return (v1, v2, v3, v4, v5, v6);
        }

        public static (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            return (v1, v2, v3, v4, v5, v6, v7);
        }

        public static (T1, T2, T3, T4, T5, T6, T7, T8) ReadValue<T1, T2, T3, T4, T5, T6, T7, T8>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            var v8 = (T8)Convert.ChangeType(inputs[7], typeof(T8));
            return (v1, v2, v3, v4, v5, v6, v7, v8);
        }
    }
}

#endregion
0