Advertisement
Guest User

Untitled

a guest
Aug 26th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P10.PoisonousPlants
  6. {
  7.     internal class PoisonousPlants
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int plantsCount = int.Parse(Console.ReadLine());
  12.             int[] pesticidesOfEachPlant = Console.ReadLine()
  13.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             Stack<int> stack = new Stack<int>(pesticidesOfEachPlant);
  18.             Stack<int> tempStack = new Stack<int>();
  19.             int savedNumber = stack.Pop();
  20.  
  21.             bool isDayWithoutDying = true;
  22.             int dayWithoutDyingCount = 0;
  23.  
  24.             while (true)
  25.             {
  26.                 if (savedNumber <= stack.Peek())
  27.                 {
  28.                     tempStack.Push(savedNumber);
  29.                 }
  30.                 else
  31.                 {
  32.                     isDayWithoutDying = false;
  33.                 }
  34.  
  35.                 savedNumber = stack.Pop();
  36.  
  37.                 if (stack.Count == 0)
  38.                 {
  39.                     if (isDayWithoutDying || tempStack.Count == 0)
  40.                     {
  41.                         Console.WriteLine(dayWithoutDyingCount);
  42.                         break;
  43.                     }
  44.  
  45.                     tempStack.Push(savedNumber);
  46.                     dayWithoutDyingCount++;
  47.                     isDayWithoutDying = true;
  48.  
  49.                     stack = new Stack<int>(tempStack);
  50.                     tempStack.Clear();
  51.                     savedNumber = stack.Pop();
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement