Submission #48379127


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks;

public class C
{
    public static void Main()
    {
        using var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }

    public static void Solve()
    {
        var (N, M) = Scanner.Scan<int, int>();
        var S = Scanner.Scan<string>();

        bool F(int x)
        {
            var a = M;
            var b = x;

            for (var i = 0; i < N; i++)
            {
                if (S[i] == '0')
                {
                    a = M;
                    b = x;
                }
                else if (S[i] == '1')
                {
                    if (a > 0) a--;
                    else if (b > 0) b--;
                    else return false;
                }
                else
                {
                    if (b > 0) b--;
                    else return false;
                }
            }

            return true;
        }

        var answer = BinarySearch<int>(-1, N, F);
        Console.WriteLine(answer);

    }

    public static T BinarySearch<T>(T ng, T ok, Func<T, bool> f) where T : INumber<T> => BinarySearch(ng, ok, f, T.One);
    public static T BinarySearch<T>(T ng, T ok, Func<T, bool> f, T eps) where T : INumber<T>
    {
        var one = T.One;
        var two = one + one;
        while (T.Abs(ok - ng) > eps)
        {
            var m = ng + (ok - ng) / two;
            if (f(m)) ok = m;
            else ng = m;
        }
        return ok;
    }

    public static class Scanner
    {
        public static T Scan<T>() where T : IConvertible => Convert<T>(ScanStringArray()[0]);
        public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]));
        }
        public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]));
        }
        public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]));
        }
        public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]));
        }
        public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]), Convert<T6>(input[5]));
        }
        public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => ScanStringArray().Select(Convert<T>);
        private static string[] ScanStringArray()
        {
            var line = Console.ReadLine()?.Trim() ?? string.Empty;
            return string.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split(' ');
        }
        private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
    }
}

Submission Info

Submission Time
Task C - T-shirts
User AconCavy
Language C# 11.0 (.NET 7.0.7)
Score 300
Code Size 4069 Byte
Status AC
Exec Time 47 ms
Memory 25164 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 43
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 43 ms 24624 KiB
example_01.txt AC 39 ms 24876 KiB
example_02.txt AC 42 ms 24856 KiB
hand_00.txt AC 46 ms 24856 KiB
hand_01.txt AC 42 ms 24888 KiB
hand_02.txt AC 40 ms 24860 KiB
hand_03.txt AC 35 ms 24632 KiB
hand_04.txt AC 35 ms 24852 KiB
hand_05.txt AC 38 ms 24692 KiB
hand_06.txt AC 33 ms 24856 KiB
hand_07.txt AC 40 ms 24876 KiB
hand_08.txt AC 42 ms 24696 KiB
hand_09.txt AC 47 ms 24696 KiB
random_00.txt AC 36 ms 24864 KiB
random_01.txt AC 32 ms 24856 KiB
random_02.txt AC 34 ms 25164 KiB
random_03.txt AC 33 ms 24780 KiB
random_04.txt AC 38 ms 24700 KiB
random_05.txt AC 34 ms 25000 KiB
random_06.txt AC 42 ms 24856 KiB
random_07.txt AC 46 ms 24856 KiB
random_08.txt AC 41 ms 24688 KiB
random_09.txt AC 36 ms 24692 KiB
random_10.txt AC 37 ms 24996 KiB
random_11.txt AC 33 ms 24652 KiB
random_12.txt AC 33 ms 24844 KiB
random_13.txt AC 42 ms 24688 KiB
random_14.txt AC 42 ms 24944 KiB
random_15.txt AC 47 ms 24848 KiB
random_16.txt AC 41 ms 24860 KiB
random_17.txt AC 36 ms 24976 KiB
random_18.txt AC 36 ms 24996 KiB
random_19.txt AC 43 ms 24724 KiB
random_20.txt AC 41 ms 24716 KiB
random_21.txt AC 42 ms 25136 KiB
random_22.txt AC 35 ms 24680 KiB
random_23.txt AC 39 ms 24860 KiB
random_24.txt AC 43 ms 25020 KiB
random_25.txt AC 35 ms 24696 KiB
random_26.txt AC 46 ms 25008 KiB
random_27.txt AC 42 ms 24840 KiB
random_28.txt AC 44 ms 24852 KiB
random_29.txt AC 34 ms 25016 KiB