Description
在方格区域,只能选取方格边(长度为1)或对角边(长度为
Input
第一行一整数
Output
对于每组用例,输出最少需要的边数
Sample Input
5
1
2
3
4
5
Sample Output
4
4
6
6
7
Solution
考虑m条边可以组成的最大面积,对于每组查询二分查找即可
当
当m为奇数时,类似偶数的取法,但是多出一条边,多出的一条边可以使得该矩形的一条边凸出去形成一个梯形,为使多出来的面积尽可能大,当然选取边长较长的一条边,令
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
ll a[maxn];
int main()
{
for(int i=4;i<maxn;i++)
{
int j=i/2;
if(i&1)a[i]=2ll*(j-j/2)*(2*(j/2)+1)-1;
else a[i]=4ll*(j/2)*(j-j/2);
}
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
n*=2;
int ans=lower_bound(a,a+maxn,n)-a;
printf("%d\n",ans);
}
return 0;
}