結果

問題 No.884 Eat and Add
コンテスト
ユーザー kakel-san
提出日時 2026-02-19 00:49:28
言語 C#
(.NET 10.0.102)
結果
AC  
実行時間 58 ms / 1,000 ms
コード長 1,551 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 28,284 ms
コンパイル使用メモリ 173,616 KB
実行使用メモリ 35,712 KB
最終ジャッジ日時 2026-02-19 00:49:59
合計ジャッジ時間 12,880 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (218 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net10.0/main.dll
  main -> /home/judge/data/code/bin/Release/net10.0/publish/

ソースコード

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