結果

問題 No.910 素数部分列
ユーザー sekiya9311sekiya9311
提出日時 2019-10-19 16:15:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 84 ms / 1,000 ms
コード長 6,576 bytes
コンパイル時間 2,993 ms
コンパイル使用メモリ 110,048 KB
実行使用メモリ 25,892 KB
最終ジャッジ日時 2023-09-09 13:31:29
合計ジャッジ時間 9,313 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
22,004 KB
testcase_01 AC 68 ms
23,948 KB
testcase_02 AC 69 ms
21,888 KB
testcase_03 AC 68 ms
25,892 KB
testcase_04 AC 68 ms
21,784 KB
testcase_05 AC 68 ms
21,828 KB
testcase_06 AC 68 ms
23,988 KB
testcase_07 AC 66 ms
21,972 KB
testcase_08 AC 67 ms
21,792 KB
testcase_09 AC 68 ms
23,888 KB
testcase_10 AC 68 ms
23,840 KB
testcase_11 AC 69 ms
21,812 KB
testcase_12 AC 67 ms
21,760 KB
testcase_13 AC 69 ms
23,984 KB
testcase_14 AC 68 ms
23,992 KB
testcase_15 AC 69 ms
19,920 KB
testcase_16 AC 68 ms
23,836 KB
testcase_17 AC 68 ms
21,948 KB
testcase_18 AC 67 ms
21,932 KB
testcase_19 AC 67 ms
21,884 KB
testcase_20 AC 69 ms
21,856 KB
testcase_21 AC 67 ms
21,860 KB
testcase_22 AC 69 ms
21,932 KB
testcase_23 AC 78 ms
24,476 KB
testcase_24 AC 80 ms
24,432 KB
testcase_25 AC 81 ms
24,408 KB
testcase_26 AC 81 ms
20,384 KB
testcase_27 AC 80 ms
22,408 KB
testcase_28 AC 80 ms
22,456 KB
testcase_29 AC 83 ms
24,908 KB
testcase_30 AC 83 ms
22,920 KB
testcase_31 AC 84 ms
22,724 KB
testcase_32 AC 83 ms
24,860 KB
testcase_33 AC 83 ms
22,820 KB
testcase_34 AC 84 ms
20,724 KB
testcase_35 AC 84 ms
20,956 KB
testcase_36 AC 82 ms
22,780 KB
testcase_37 AC 83 ms
22,896 KB
testcase_38 AC 82 ms
22,880 KB
testcase_39 AC 82 ms
24,872 KB
testcase_40 AC 82 ms
22,904 KB
testcase_41 AC 82 ms
24,824 KB
testcase_42 AC 81 ms
22,908 KB
testcase_43 AC 80 ms
22,920 KB
testcase_44 AC 81 ms
24,884 KB
testcase_45 AC 82 ms
22,952 KB
testcase_46 AC 81 ms
22,868 KB
testcase_47 AC 80 ms
25,012 KB
testcase_48 AC 77 ms
22,832 KB
testcase_49 AC 77 ms
22,820 KB
testcase_50 AC 78 ms
24,832 KB
testcase_51 AC 77 ms
24,152 KB
testcase_52 AC 80 ms
24,860 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.Numerics;
using System.Text;

namespace ProgrammingContestTemplateByDotNetCore
{

    #region "Input and Output Classes"

    class Scanner : IDisposable
    {
        private readonly Queue<string> _buffer;
        private readonly char[] _sep;
        private readonly TextReader _reader;

        public Scanner(TextReader reader = null, char[] sep = null)
        {
            _buffer = new Queue<string>();
            _sep = sep ?? new[] { ' ' };
            _reader = reader ?? Console.In;
        }

        public Scanner(string path, char[] sep = null)
            : this(new StreamReader(path), sep) { }

        private void CheckBuffer()
        {
            if (_buffer.Any() || _reader.Peek() == -1)
                return;

            string str = string.Empty;
            for (;
                string.IsNullOrWhiteSpace(str);
                str = _reader.ReadLine()) { }

            var values = str.Split(_sep).Where(s => !string.IsNullOrWhiteSpace(s));
            foreach (var item in values)
            {
                _buffer.Enqueue(item);
            }
        }

        public string Next()
        {
            CheckBuffer();
            return _buffer.Dequeue();
        }
        public string[] GetStringArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => Next())
                .ToArray();

        public int NextInt() => int.Parse(Next());
        public int[] GetIntArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextInt())
                .ToArray();

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

        public double NextDouble() => double.Parse(Next());
        public double[] GetDoubleArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextDouble())
                .ToArray();

        public decimal NextDecimal() => decimal.Parse(Next());
        public decimal[] GetDecimalArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextDecimal())
                .ToArray();

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

        public bool IsEnd
        {
            get
            {
                CheckBuffer();
                return !_buffer.Any();
            }
        }

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

    class Writer : IDisposable
    {
        private readonly TextWriter _writer;
        private readonly StringBuilder _cache;
        private readonly bool _isReactive;

        public Writer(TextWriter writer = null, bool isReactive = false)
        {
            _writer = writer ?? Console.Out;
            _isReactive = isReactive;
            if (!_isReactive)
                _cache = new StringBuilder();
        }

        public Writer(string path)
            : this(new StreamWriter(path)) { }

        public Writer(bool isReactive)
            : this(null, isReactive) { }

        public void Write(object value)
        {
            if (_isReactive)
            {
                _writer.Write(value);
                _writer.Flush();
            }
            else
            {
                _cache.Append(value);
            }
        }

        public void WriteFormat(string format, params object[] values)
        {
            var value = string.Format(format, values);
            Write(value);
        }

        public void WriteLine(object value = null)
        {
            Write($"{value}{Environment.NewLine}");
        }

        public void WriteLine(string format, params object[] values)
        {
            WriteFormat($"{format}{Environment.NewLine}", values);
        }

        public void Dispose()
        {
            if (!_isReactive)
            {
                _writer.Write(_cache);
                _writer.Flush();
            }
            if (!_writer.Equals(Console.Out))
            {
                _writer.Dispose();
            }
        }
    }

#endregion

    class MainClass : IDisposable
    {
#region "Template"

        /// <summary> 入力を受け取る </summary>
        private readonly Scanner sc;
        /// <summary> 出力を行う </summary>
        private readonly Writer wr;
        private const string _inputFile = "input.txt";
        private const string _outFile = "output.txt";

        static void Main(string[] args)
        {
            using (new MainClass()) { }
        }

        public MainClass()
        {
            wr = new Writer(_isReactive);
            // TODO: ファイルに出力したい場合はこっち → wr = new Writer(_outFile);

#if DEBUG
            // 手元では、ファイルから入力を受け取る
            sc = new Scanner(_inputFile);
#else

            // 提出時、標準入力から受け取る
            sc = new Scanner();
#endif

            Solve();
        }

        public void Dispose()
        {
            sc?.Dispose();
            wr?.Dispose();
        }

        #endregion

        void Solve()
        {
            var N = sc.NextInt();
            var S = sc.Next();

            int ans = S.Count(c => c != '1' && c != '9');
            S = string.Concat(S.Where(c => c == '1' || c == '9'));
            int one = 0, nine = 0;
            for (int i = 0; i < S.Length; i++)
            {
                if (S[i] == '1')
                {
                    one++;
                }
                else
                {
                    if (one > 0)
                    {
                        one--;
                        ans++;
                    }
                    else
                    {
                        nine++;
                    }
                }
            }

            int tmp = Math.Min(nine / 2, one);
            ans += tmp;
            one -= tmp;
            ans += one / 2;

            wr.WriteLine(ans);
        }

        /// <summary>
        /// TODO: リアクティブ問題か否かを指定してね♪
        /// </summary>
        private const bool _isReactive = false;
    }
}
0