Advertisement
stanevplamen

03.02.04.Guitar

Jul 26th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2.  
  3. class Guitar
  4. {
  5.     static int maxVolume;
  6.     static int minVolume;
  7.     static int startVolume;
  8.  
  9.     static void Main()
  10.     {
  11.         string notUsed = Console.ReadLine();                  
  12.         string firstLineInput =  Console.ReadLine();                
  13.         startVolume = int.Parse(Console.ReadLine());              
  14.         maxVolume = int.Parse(Console.ReadLine());                
  15.         minVolume = 0;
  16.  
  17.         string[] numbsStr = firstLineInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  18.         int[] numbs = ConvertToInt(numbsStr);
  19.  
  20.         DynamicProgrammingSolution(numbs);
  21.     }
  22.  
  23.     private static void DynamicProgrammingSolution(int[] numbs)
  24.     {
  25.         int[,] possibleResults = new int[numbs.Length + 1, maxVolume + 1];
  26.         possibleResults[0, startVolume] = 1;
  27.  
  28.         for (int row = 1; row < possibleResults.GetLength(0); row++)
  29.         {
  30.             //int lowerBoargerCol = -1;
  31.             //int biggerBoarderCol = maxVolume;
  32.             for (int col = 0; col < possibleResults.GetLength(1); col++)
  33.             {
  34.                 if (possibleResults[row - 1, col] == 1)
  35.                 {
  36.                     if (col + numbs[row - 1] <= maxVolume)
  37.                     {
  38.                         possibleResults[row, col + numbs[row - 1]] = 1;
  39.                     }
  40.                     if (col - numbs[row - 1] >= minVolume)
  41.                     {
  42.                         possibleResults[row, col - numbs[row - 1]] = 1;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.  
  48.         int biggerCurrenVolume = -1;
  49.         for (int col = 0; col < possibleResults.GetLength(1); col++)
  50.         {
  51.             if (possibleResults[possibleResults.GetLength(0) - 1, col] == 1)
  52.             {
  53.                 if (biggerCurrenVolume < col)
  54.                 {
  55.                     biggerCurrenVolume = col;
  56.                 }
  57.             }
  58.         }
  59.         Console.WriteLine(biggerCurrenVolume);
  60.     }
  61.  
  62.     private static int[] ConvertToInt(string[] numbsStr)
  63.     {
  64.         int[] numbs = new int[numbsStr.Length];
  65.         for (int i = 0; i < numbsStr.Length; i++)
  66.         {
  67.             numbs[i] = int.Parse(numbsStr[i].Trim());
  68.         }
  69.         return numbs;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement