0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[paiza レベルアップ問題集] 日付セット 連休を伸ばす2 (paizaランク A 相当)

Posted at

paizaのレベルアップ問題集に挑戦。

考え方

連休の初日を1日ずつずらしていく。
前のループにおける①連休の初日が休日か営業日か、②連休の最終日がいつか、③何連休であったかを利用する。


    class Program
    {
        static int howManyHolidays(int firstDay,int N, int L, int[] list)
        {
            int counter = 0;
            for(int i = firstDay; i < N; i++)
            {
                if(list[i] == 1)
                {
                    counter++;
                }
                else if(L > 0)
                {
                    counter++;
                    L--;
                }
                else
                {
                    break;
                }
            }
            return counter;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int N = int.Parse(input[0]), L = int.Parse(input[1]);
            input = Console.ReadLine().Trim().Split(' ');
            int[] list = new int[N]; //1 -> 休業日、0 -> 営業日
            for(int i = 0; i < N; i++)
            {
                list[i] = int.Parse(input[i]);
            }
            //連休の初日が1日目の場合求める
            int firstDay = 0;
            int temp = howManyHolidays(firstDay, N, L, list);
            int max = temp;
            int lastDay = firstDay + temp - 1; //連休の初日がfirstDayの場合の連休の最終日
            //連休の初日を一日ずつずらす
            for(int i = 1; i < N; i++)
            {
                temp--;
                //連休の初日の前日営業日の場合、(lastDay+1)日目から後1回年休が使える
                int paidDay; 
                if(list[i - 1] == 0)
                {
                    paidDay = 1;
                }
                else
                {
                    paidDay = 0;
                }
                for(int j = lastDay + 1; j < N; j++)
                {
                    if(list[j] == 1)
                    {
                        temp++;
                    }else if(paidDay == 1)
                    {
                        temp++;
                        paidDay--;
                    }
                    else
                    {
                        lastDay = j - 1;
                        break;
                    }
                }
            if(temp > max)
                {
                    max = temp;
                }
            }
            Console.WriteLine(max);
        }
    }

youtubeの解説動画を視聴後に書いたコード


    class Program
    {
        static int howManyHolidays(int firstDay,int N,int L,int[] list)
        {
            //L日は休める -> 0の日を1に書き換えられる × L回
            int one = 0; //休業日を数える変数
            int zero = 0; //年休を使った日数を数える変数
            while(firstDay + one + zero < N)
            {
                if(list[firstDay] == 1)
                {
                    one++;
                }
                else
                {
                    if(zero == L)
                    {
                        break;
                    }
                    zero++;
                }
            }
            return one + zero;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int N = int.Parse(input[0]), L = int.Parse(input[1]);
            input = Console.ReadLine().Trim().Split(' ');
            int[] list = new int[N]; //1 -> 休業日、0 -> 営業日
            for (int i = 0; i < N; i++)
            {
                list[i] = int.Parse(input[i]);
            }
            int result = 0;
            int one = 0;
            int zero = 0;
            for (int left = 0, right = 0; right < N; right++)
            {
                if(list[right] == 1)
                {
                    one++;
                }
                else
                {
                    zero++;
                    while(zero > L)
                    {
                        //leftを増やす、oneとzeroを調整
                        if(list[left] == 1)
                        {
                            one--;
                        }
                        else
                        {
                            zero--;
                        }
                        left++;
                    }
                }
                if(one + zero > result)
                {
                    result = one + zero;
                }
            }
            Console.WriteLine(result);
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?