【CF MEMSQL 3.0 B. Lazy Security Guard】
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Your security guard friend recently got a new job at a new security company. The company requires him to patrol an area of the city encompassing exactly N city blocks, but they let him choose which blocks. That is, your friend must walk the perimeter of a region whose area is exactly N blocks. Your friend is quite lazy and would like your help to find the shortest possible route that meets the requirements. The city is laid out in a square grid pattern, and is large enough that for the sake of the problem it can be considered infinite.
Input
Input will consist of a single integer N (1 ≤ N ≤ 106), the number of city blocks that must be enclosed by the route.
Output
Print the minimum perimeter that can be achieved.
Examples
input
4
output
8
input
11
output
14
input
22
output
20
Note
Here are some possible shapes for the examples:
【翻译】有n个长度为1的正方形块拼在一起,求最小周长。
题解:
①先对n开根的出近似边长x。
②尝试周长:x*x ,x*(x+1), (x+1)*(x+1),只要块数大于n,输出最小值就可以了。
#include<stdio.h> #include<math.h> using namespace std; int n,a; int main() { scanf("%d",&n); a=floor(sqrt(1.0*n)+0.0001); if(a*a>=n)printf("%d\n",4*a); else if(a*(a+1)>=n)printf("%d\n",4*a+2); else if((a+1)*(a+1)>=n)printf("%d\n",4*a+4);return 0; }//Paul_Guderian