Advertisement
Statev

Tubes

Feb 9th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _3.Tubes
  7. {
  8.     class Tubes
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.             int m = int.Parse(Console.ReadLine());
  14.  
  15.             int[] tubes = new int[n];
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 tubes[i] = int.Parse(Console.ReadLine());
  19.             }
  20.  
  21.             bool haveSolution = false;
  22.             int solutionLength = -1;
  23.             int cutDownBorder = 0;
  24.             int cutUpBorder = tubes.Max();
  25.             int cutLength = cutUpBorder + cutDownBorder / 2;
  26.  
  27.             while (cutUpBorder - cutDownBorder > 1)
  28.             {
  29.                 haveSolution = false;
  30.                 int tubeItems = 0;
  31.  
  32.                 for (int j = 0; j < n; j++)
  33.                 {
  34.                     tubeItems += tubes[j] / cutLength;
  35.                     if (tubeItems >= m)
  36.                     {
  37.                         solutionLength = cutLength;
  38.                         haveSolution = true;
  39.                         break;
  40.                     }
  41.                 }
  42.                 if (haveSolution)
  43.                 {
  44.                     cutDownBorder = cutLength;
  45.                 }
  46.                 else
  47.                 {
  48.                     cutUpBorder = cutLength;
  49.                 }
  50.                 cutLength = (cutUpBorder + cutDownBorder) / 2;
  51.             }
  52.  
  53.             Console.WriteLine(solutionLength);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement