-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path859B Lazy Security Guard.cpp
51 lines (46 loc) · 1.45 KB
/
859B Lazy Security Guard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*B. Lazy Security Guard
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.*/
#include<bits/stdc++.h>
#include<cmath>
#define ll long long
using namespace std;
bool isPerfect(ll N)
{
if ((sqrt(N) - floor(sqrt(N))) != 0)
return false;
return true;
}
ll findsqrt(ll n)
{
while(n)
{
if(isPerfect(n))
return n;
n--;
}
return 1;
}
int main()
{
ll n;
cin>>n;
ll x = sqrt(findsqrt(n));
ll ans = x*4;
ll extra = n - (x*x);
if(extra>0)
{
if(extra<=x)
ans+=2;
else
ans+=4;
}
cout<<ans<<endl;
}