Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. class Program
  4. {
  5.     static int[] grapes;
  6.     static void Main()
  7.     {
  8.         grapes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  9.         int n = int.Parse(Console.ReadLine());
  10.         do
  11.         {
  12.             for (int cnt = 0; cnt < n; cnt++)
  13.             {
  14.                 BloomGrapes();
  15.             }
  16.             KillGrapesWithPowerLesserThan(n);
  17.         } while (StrongGrapesAreGreaterOrEqualThan(n));
  18.         PrintLiveGrapes(n);
  19.     }
  20.     static bool StrongGrapesAreGreaterOrEqualThan(int threshold)
  21.     {
  22.         int strongGrapesCnt = 0;
  23.         for (int cnt = 0; cnt < grapes.Length; cnt++)
  24.         {
  25.             if (grapes[cnt] > threshold)
  26.             {
  27.                 strongGrapesCnt++;
  28.             }
  29.         }
  30.         return strongGrapesCnt >= threshold;
  31.     }
  32.  
  33.     static void BloomGrapes()
  34.     {
  35.         for (int index = 0; index < grapes.Length; index++)
  36.         {
  37.          
  38.             if (!IsAlive(index) || IsLesserGrape(index))
  39.             {
  40.                 continue;
  41.             }
  42.             if (IsGreaterGrape(index))
  43.             {
  44.                 grapes[index]++;
  45.                 if (grapes[index - 1] > 0)
  46.                 {
  47.                     grapes[index - 1]--;
  48.                     grapes[index]++;
  49.                 }
  50.                 if (grapes[index + 1] > 0)
  51.                 {
  52.                     grapes[index + 1]--;
  53.                     grapes[index]++;
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 grapes[index]++;
  59.             }
  60.         }
  61.     }
  62.     static bool IsAlive(int index)
  63.     {
  64.         if (index < 0 || index > grapes.Length - 1 || grapes[index] <= 0)// <=
  65.         {
  66.             return false;
  67.         }
  68.         return grapes[index] > 0;
  69.     }
  70.     static bool IsGreaterGrape(int index)
  71.     {
  72.         if (index <= 0 || index >= grapes.Length - 1)
  73.         {
  74.             return false;
  75.         }
  76.         return grapes[index] > grapes[index - 1] && grapes[index] > grapes[index + 1];
  77.     }
  78.  
  79.     static bool IsLesserGrape(int index)
  80.     {
  81.         return IsGreaterGrape(index - 1) || IsGreaterGrape(index + 1);
  82.     }
  83.  
  84.     static void KillGrapesWithPowerLesserThan(int threshfold)
  85.     {
  86.         for (int cnt = 0; cnt < grapes.Length; cnt++)
  87.         {
  88.             if (grapes[cnt] <= threshfold)
  89.             {
  90.                 grapes[cnt] = 0;
  91.             }
  92.         }
  93.     }
  94.    
  95.     static void PrintLiveGrapes(int n)
  96.     {
  97.         for (int index = 0; index < grapes.Length; index++)
  98.         {
  99.             if (grapes[index] > n)
  100.             {
  101.                 Console.Write(grapes[index] + " ");
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement