CaoHaha's staff
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
"You shall not pass!"
After shouted out that,the Force Staff appered in CaoHaha's hand.
As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.
But now,his new owner,CaoHaha,is a sorcerers apprentice.He can only use that staff to send things to other place.
Today,Dreamwyy come to CaoHaha.Requesting him send a toy to his new girl friend.It was so far that Dreamwyy can only resort to CaoHaha.
The first step to send something is draw a Magic array on a Magic place.The magic place looks like a coordinate system,and each time you can draw a segments either on cell sides or on cell diagonals.In additional,you need 1 minutes to draw a segments.
If you want to send something ,you need to draw a Magic array which is not smaller than the that.You can make it any deformation,so what really matters is the size of the object.
CaoHaha want to help dreamwyy but his time is valuable(to learn to be just like you),so he want to draw least segments.However,because of his bad math,he needs your help.
After shouted out that,the Force Staff appered in CaoHaha's hand.
As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.
But now,his new owner,CaoHaha,is a sorcerers apprentice.He can only use that staff to send things to other place.
Today,Dreamwyy come to CaoHaha.Requesting him send a toy to his new girl friend.It was so far that Dreamwyy can only resort to CaoHaha.
The first step to send something is draw a Magic array on a Magic place.The magic place looks like a coordinate system,and each time you can draw a segments either on cell sides or on cell diagonals.In additional,you need 1 minutes to draw a segments.
If you want to send something ,you need to draw a Magic array which is not smaller than the that.You can make it any deformation,so what really matters is the size of the object.
CaoHaha want to help dreamwyy but his time is valuable(to learn to be just like you),so he want to draw least segments.However,because of his bad math,he needs your help.
Input
The first line contains one integer T(T<=300).The number of toys.
Then T lines each contains one intetger S.The size of the toy(N<=1e9).
Then T lines each contains one intetger S.The size of the toy(N<=1e9).
Output
Out put T integer in each line ,the least time CaoHaha can send the toy.
Sample Input
5 1 2 3 4 5
Sample Output
4 4 6 6 7
题意:
给出面积s,画出一个图形的面积大于等于s至少需要多少条边(可沿着小正方形的边或者对角线画)。
解题思路:
先找到面积最接近(可以等于)s的菱形(正方形),然后在这个菱形(正方形)的基础上添加边,
然后找面积增加的规律,计算s在那个区间。
1,添加一条边的情况:
增加的面积为:边长-0.5;
2,添加2条边的情况:
增加的面积为:边长*2;
往后同理,添加3条边 面积增加:边长*3+0.5 等等;
判断题目中给出的面积在哪个范围之内,就可以得出所需要的边数(最多添加4条)
如果一个斜着的矩形长宽分别是 a,b
,那么它的面积是 2a*b
。最优解肯定是离 sqrt(n/2)很近的位置。
据此算出在多大的菱形(正方形)上添加边
代码:
C++ Code
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 |
#include <bits/stdc++.h>
#define ll long long using namespace std; int main() { int t; scanf( "%d", &t); while(t--) { long long n; scanf( "%lld", &n); if(n == 1 || n == 2) puts( "4"); else if(n == 3 || n == 4) puts( "6"); else if(n == 5) puts( "7"); else if(n >= 6 && n <= 8) puts( "8"); else { ll m = floor(sqrt(n / 2)); ll tmp = m * 4; ll x = m * m * 2; if(n == x) cout << tmp << endl; else if(n <= x + m - 0. 5) cout << tmp + 1 << endl; else if(n <= x + 2 * m) cout << tmp + 2 << endl; else if(n <= 3 * m + 0. 5 + x) cout << tmp + 3 << endl; else cout << tmp + 4 << endl; } } return 0; } |