Rectangle
Time limit1sMemory limit1024 MB
Given N unit squares on distinct grid cells, find the smallest possible perimeter of an axis-parallel rectangle that contains all of them, for each N up to 1e9.
- Level
Medium4 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
There is an infinitely large square grid. You want to place squares of size , each on a different cell of this grid.
Call a shape a minimum bounding rectangle if it satisfies the following conditions and has the shortest perimeter among such shapes.
- The shape is a rectangle whose sides are all parallel to the grid lines.
- The shape contains all of the placed squares.
Among all ways to place squares, in which case is the perimeter of the minimum bounding rectangle the shortest?
Input
The first line contains the number of test cases ().
Each of the next lines contains one integer , the number of squares to place ().
Output
For each test case, print the shortest possible perimeter of the minimum bounding rectangle.
Hint
This problem has a large input, so you may need to use fast I/O to avoid a time limit error.
-
If you use C++ and want to use
cin/cout:- Put
cin.tie(nullptr)andios::sync_with_stdio(false)at the top ofmain. - Use the newline character (
\n) instead ofendl. - In this case, you must not use C I/O such as
scanf/printf/puts/getchar/putchar.
- Put
-
If you use Java:
- Use
BufferedReaderandBufferedWriterinstead ofScannerandSystem.out.println. - Call
BufferedWriter.flushonce at the end.
- Use
-
If you use Python:
- Use
sys.stdin.readlineinstead ofinput. Note that this function also reads the trailing newline. - It is a good idea to add
.rstrip()if you want to store a string without the newline.
- Use