Codeforces 1099 B. Squares and Segments-思維(Codeforces Round #530 (Div. 2))
B. Squares and Segments
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputLittle Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home, she decided to draw
In order to draw a segment, Sofia proceeds as follows. If she wants to draw a vertical segment with the coordinates of the ends
For example, if Sofia needs to draw one square, she will have to draw two segments using a ruler:
After that, she can draw the remaining two segments, using the first two as a guide:
If Sofia needs to draw two squares, she will have to draw three segments using a ruler:
After that, she can draw the remaining four segments, using the first three as a guide:
Sofia is in a hurry, so she wants to minimize the number of segments that she will have to draw with a ruler without a guide. Help her find this minimum number.
InputThe only line of input contains a single integer nn (1≤n≤1091≤n≤109), the number of squares that Sofia wants to draw.
OutputPrint single integer, the minimum number of segments that Sofia will have to draw with a ruler without a guide in order to draw nn squares in the manner described above.
Examples input Copy1output Copy
2input Copy
2output Copy
3input Copy
4output Copy
4
題意就是找最接近當前數的一個數的兩個因數,比如41,就是6*7,就是6+7的結果。如果按照41來算,那麽需要41+1=42,如果是按照42來算,就是6+7,還有10,最接近的是3+4,是12的,就是這樣的題目。
代碼:
1 //B 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #include<bitset> 7 #include<cassert> 8 #include<cctype> 9 #include<cmath> 10 #include<cstdlib> 11 #include<ctime> 12 #include<deque> 13 #include<iomanip> 14 #include<list> 15 #include<map> 16 #include<queue> 17 #include<set> 18 #include<stack> 19 #include<vector> 20 using namespace std; 21 typedef long long ll; 22 23 const double PI=acos(-1.0); 24 const double eps=1e-6; 25 const ll mod=1e9+7; 26 const int inf=0x3f3f3f3f; 27 const int maxn=2e7+10; 28 const int maxm=100+10; 29 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 30 31 int main() 32 { 33 int n; 34 cin>>n; 35 int p=sqrt(n); 36 int ans=inf,flag=0; 37 if(n==1){ 38 ans=2; 39 cout<<ans<<endl; 40 return 0; 41 } 42 if(p*p==n)ans=min(2*p,ans); 43 else if(p*(p+1)>=n) ans=min(p+p+1,ans); 44 else ans=min(2*(p+1),ans); 45 cout<<ans<<endl; 46 }
Codeforces 1099 B. Squares and Segments-思維(Codeforces Round #530 (Div. 2))