結果

問題 No.884 Eat and Add
コンテスト
ユーザー 👑 kakel-san
提出日時 2026-02-19 00:49:28
言語 C#
(.NET 10.0.402 + ACL)
コンパイル:
dotnet_c
実行:
/usr/bin/dotnet_wrap
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 1,551 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-07-21 09:12:41
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = ReadLine();
        WriteLine(Eat(n));
    }
    static int Eat(string n)
    {
        var last = '+';
        var count = 0;
        var list = new List<(char c, int count)>();
        for (var i = 0; i < n.Length; ++i)
        {
            if (n[i] == last)
            {
                ++count;
            }
            else
            {
                if (count > 0) list.Add((last, count));
                last = n[i];
                count = 1;
            }
            if (i + 1 == n.Length)
            {
                list.Add((last, count));
            }
        }
        var ans = 0;
        for (var i = list.Count - 1; i >= 0; --i)
        {
            if (list[i].c == '1')
            {
                if (list[i].count == 1)
                {
                    ++ans;
                }
                else if (i > 1 && list[i - 1].count == 1)
                {
                    ++ans;
                    list[i - 2] = (list[i - 2].c, list[i - 2].count + 1);
                }
                else
                {
                    ans += 2;
                }
            }
        }
        return ans;
    }
}
0