📜  浇水到矩形公园所需的最小洒水装置

📅  最后修改于: 2021-05-08 18:42:12             🧑  作者: Mango

给定具有N行和M列的N * M矩形停车库,停车库的每个单元都是单位面积的正方形,单元之间的边界称为十六进制,并且可以在十六进制的中间放置一个洒水器。任务是找到为整个公园浇水所需的最少洒水装置。

例子:

方法:

  1. 经过观察后,可以指出一件事,即对于每列,需要N喷淋头,因为我们可以将它们放置在两列之间。
  2. 如果M是偶数,则显然需要N *(M / 2)个喷头。
  3. 但是,如果M为奇数,则可以使用偶数列公式计算M – 1列的解,对于最后一列,无论N是奇数还是偶数,都向最后一列添加(N +1)/ 2洒水。

      这是上述方法的实现:

      C++
      // C++ program to find the
      // minimum number sprinklers
      // reqired to water the park.
        
      #include 
      using namespace std;
      typedef long long int ll;
        
      // Function to find the
      // minimum number sprinklers
      // required to water the park.
      void solve(int N, int M)
      {
        
          // General requirements of
          // sprinklers
          ll ans = (N) * (M / 2);
        
          // if M is odd then add
          // one additional sprinklers
          if (M % 2 == 1) {
              ans += (N + 1) / 2;
          }
        
          cout << ans << endl;
      }
        
      // Driver code
      int main()
      {
          int N, M;
          N = 5;
          M = 3;
          solve(N, M);
      }


      Java
      // Java program to find minimum 
      // number sprinklers required 
      // to cover the park
      class GFG{ 
            
      // Function to find the minimum 
      // number sprinklers reqired 
      // to water the park. 
      public static int solve(int n, int m)
      {
            
          // General requirements of sprinklers
          int ans = n * (m / 2);
                
          // If M is odd then add one
          // additional sprinklers 
          if (m % 2 == 1)
          {
              ans += (n + 1) / 2;
          } 
          return ans;
      }
        
      // Driver code
      public static void main(String args[]) 
      { 
          int N = 5;
          int M = 3;
            
          System.out.println(solve(N, M)); 
      } 
      } 
        
      // This code is contributed by grand_master


      Python3
      # Python3 program to find the 
      # minimum number sprinklers 
      # required to water the park. 
        
      # Function to find the 
      # minimum number sprinklers 
      # reqired to water the park. 
      def solve(N, M) :
            
          # General requirements of 
          # sprinklers 
          ans = int((N) * int(M / 2))
        
          # if M is odd then add 
          # one additional sprinklers 
          if (M % 2 == 1):
              ans += int((N + 1) / 2)
        
          print(ans)
        
      # Driver code 
      N = 5
      M = 3
      solve(N, M)
        
      # This code is contributed by yatinagg


      C#
      // C# program to find minimum 
      // number sprinklers required 
      // to cover the park
      using System;
        
      class GFG{ 
            
      // Function to find the minimum 
      // number sprinklers reqired 
      // to water the park. 
      public static int solve(int n, int m)
      {
            
          // General requirements of sprinklers
          int ans = n * (m / 2);
                
          // If M is odd then add one
          // additional sprinklers 
          if (m % 2 == 1)
          {
              ans += (n + 1) / 2;
          } 
          return ans;
      }
        
      // Driver code
      public static void Main(String []args) 
      { 
          int N = 5;
          int M = 3;
            
          Console.WriteLine(solve(N, M)); 
      } 
      } 
        
      // This code is contributed by 29AjayKumar


      输出:
      8
      

      时间复杂度: O(1)
      辅助空间: O(1)