题意:给一个数n,我们可以沿着横线和对角线上化一条线,求:画出来的面积大于等于N的最小笔画是多少。
思路:贪心的选择斜线画法,然后我们可以知道当前最大能构成的矩形的长和宽,假设我们可以再加大,自然是高加1,那么面积就会扩大 (长 * 1),然后再,考虑5出现6次的原因,是在长的那边在4的画法的情况下只需增加一个笔画,而面积可以增加很多,所以,求出这个面积再特判,贪心加斜线,然后再考虑加1的情况。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define LL long long
#define siz 1000005
using namespace std;
const int mod = 1e9+7;
int n;
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
if(n==1||n==2){
puts("4");continue;
}
if(n==3){
puts("6");continue;
}
int L,wide;
L = 2,wide = 1;
while(L*wide*2<=n){
if(L*(wide+1)*2>n) break;
wide = wide + 1;
if(wide>L) swap(L,wide);
}
int ans;
// cout<<L<<" "<<wide<<endl;
if(L*wide*2 == n){
ans = (L + wide)*2;
}
else{
int m = n - L * wide *2;
//cout<<2*l
int zx = ((L * 2) - 1) / 2;
//if(L%2==0) --zx;
if(zx >= m){
ans = 2*(L + wide) +1;
}
else{
ans = (L + wide)*2 + 2;
}
}
printf("%d\n",ans);
}
return 0;
}