📌  相关文章
📜  最大化可以用坐标平面中的 N 个任意点形成的唯一正方形的数量

📅  最后修改于: 2022-05-13 01:56:08.500000             🧑  作者: Mango

最大化可以用坐标平面中的 N 个任意点形成的唯一正方形的数量

给定一个正整数N ,任务是找出在坐标平面上可以由N个任意点组成的唯一正方形的最大数量。

注意:任何两个不重叠的正方形都被认为是唯一的。

例子:

方法:这个问题可以根据以下观察来解决:

  • 观察如果N 是一个完美的正方形,那么当sqrt(N)*sqrt(N)个点形成一个sqrt(N)*sqrt(N)的网格并且它们都是相等的空间时,将形成最大数量的正方形。
  • 但是当N 不是一个完美的正方形时,它仍然形成一个网格,但是数量最多的是一个小于N的完美正方形。
  • 剩余的坐标可以放置在网格的边缘周围,这将导致最大可能的正方形。

请按照以下步骤解决问题:

  1. 初始化一个变量,比如ans ,它存储所形成的平方数。
  2. 找到最大可能的网格大小为sqrt(N)和所有可能的正方形的计数,直到长度为len的变量ans可以通过以下方式计算\sum_{i = 1}^{i = len} i*i          .
  3. N的值减少len*len
  4. 如果N的值至少为 len ,则所有其他正方形都可以通过将它们放在另一个点簇中来形成。求步骤 2中计算的len值的平方数。
  5. 完成上述步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum number
// of unique squares that can be formed
// from the given N points
int maximumUniqueSquares(int N)
{
    // Stores the resultant count of
    // squares formed
    int ans = 0;
 
    // Base Case
    if (N < 4) {
        return 0;
    }
 
    // Subtract the maximum possible
    // grid size as sqrt(N)
    int len = (sqrt(double(N)));
    N -= len * len;
 
    // Find the total squares till now
    // for the maximum grid
    for (int i = 1; i < len; i++) {
 
        // A i*i grid contains (i-1)*(i-1)
        // + (i-2)*(i-2) + ... + 1*1 squares
        ans += i * i;
    }
 
    // When N >= len then more squares
    // will be counted
    if (N >= len) {
        N -= len;
        for (int i = 1; i < len; i++) {
            ans += i;
        }
    }
 
    for (int i = 1; i < N; i++) {
        ans += i;
    }
 
    // Return total count of squares
    return ans;
}
 
// Driver Code
int main()
{
    int N = 9;
    cout << maximumUniqueSquares(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to find the maximum number
// of unique squares that can be formed
// from the given N points
static int maximumUniqueSquares(int N)
{
    // Stores the resultant count of
    // squares formed
    int ans = 0;
 
    // Base Case
    if (N < 4) {
        return 0;
    }
 
    // Subtract the maximum possible
    // grid size as sqrt(N)
    int len = (int)(Math.sqrt(N));
    N -= len * len;
 
    // Find the total squares till now
    // for the maximum grid
    for (int i = 1; i < len; i++) {
 
        // A i*i grid contains (i-1)*(i-1)
        // + (i-2)*(i-2) + ... + 1*1 squares
        ans += i * i;
    }
 
    // When N >= len then more squares
    // will be counted
    if (N >= len) {
        N -= len;
        for (int i = 1; i < len; i++) {
            ans += i;
        }
    }
 
    for (int i = 1; i < N; i++) {
        ans += i;
    }
 
    // Return total count of squares
    return ans;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 9;
    System.out.println( maximumUniqueSquares(N));
 
}
}
 
// This code is contributed by shivanisinghss2110.


Python3
# Python program for the above approach
 
# for math function
import math
 
# Function to find the maximum number
# of unique squares that can be formed
# from the given N points
def maximumUniqueSquares(N):
   
    # Stores the resultant count of
    # squares formed
    ans = 0
     
    # Base Case
    if N < 4:
        return 0
 
    # Subtract the maximum possible
    # grid size as sqrt(N)
    len = int(math.sqrt(N))
 
    N -= len * len
 
    # Find the total squares till now
    # for the maximum grid
    for i in range(1, len):
 
        # A i*i grid contains (i-1)*(i-1)
        # + (i-2)*(i-2) + ... + 1*1 squares
        ans += i * i
 
    # When N >= len then more squares
    # will be counted
    if (N >= len):
        N -= len
        for i in range(1, len):
            ans += i
 
    for i in range(1, N):
        ans += i
 
    # Return total count of squares
    return ans
 
# Driver Code
if __name__ == "__main__":
    N = 9
    print(maximumUniqueSquares(N))
     
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
    // Function to find the maximum number
    // of unique squares that can be formed
    // from the given N points
    static int maximumUniqueSquares(int N)
    {
       
        // Stores the resultant count of
        // squares formed
        int ans = 0;
     
        // Base Case
        if (N < 4) {
            return 0;
        }
     
        // Subtract the maximum possible
        // grid size as sqrt(N)
        int len = (int)(Math.Sqrt(N));
        N -= len * len;
     
        // Find the total squares till now
        // for the maximum grid
        for (int i = 1; i < len; i++) {
     
            // A i*i grid contains (i-1)*(i-1)
            // + (i-2)*(i-2) + ... + 1*1 squares
            ans += i * i;
        }
     
        // When N >= len then more squares
        // will be counted
        if (N >= len) {
            N -= len;
            for (int i = 1; i < len; i++) {
                ans += i;
            }
        }
     
        for (int i = 1; i < N; i++) {
            ans += i;
        }
     
        // Return total count of squares
        return ans;
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int N = 9;
        Console.WriteLine( maximumUniqueSquares(N));
     
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
5

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