I did not expect any good algorithm ...... feeling very violent way ......
The general idea is to first find a large square blocks just really out of bounds, and then add up the wall one by one.
First, consider the relationship between the number of blocks and fences:
- The number of blocks is the \ (1 ^ 2 \)
the number of fence is \ (2 \ times 1 \ times (1 + 1) \)
0 |
---|
- The number of blocks is the \ (2 ^ 2 \)
the number of fence is \ (2 \ times 2 \ times (1 + 2) \)
0 | 0 |
---|---|
0 | 0 |
- Block number is \ (2 ^ 3 \)
number of walls is \ (2 \ times 3 \ times (1 + 3) \)
0 | 0 | 0 |
---|---|---|
0 | 0 | 0 |
0 | 0 | 0 |
For \ (n-\) case of the block, the number of walls is \ (m = 2n (1 + n) \)
Now given that the number of walls \ (m \) , thus solving the equation \ (m = 2N (n-+. 1) \) .
Roots by the formula:
\[ n=\lfloor \frac{ \sqrt{4+8m}-2}{4} \rfloor \]
In addition to the large square operator how much is left fence:
\ [REST 2N-m = (n-+. 1) \]
Address remaining walls can enclose several blocks
\ [RestBlock = \ left \ { \ begin {aligned} & \ lfloor (rest-1) / 2 \ rfloor, \ quad \ quad rest \ leq 2n + 1 \\ & \ lfloor (rest-1) / 2 \ rfloor + n, \ rest> 2n + 1 \\ \ end {aligned} \ right. \]
Output \ (n-2 + RestBlock ^ \) .
Variable code is not the same with the topic, please pay attention.
code:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=105,INF=0x3f3f3f3f;
inline ll read()
{
char c=getchar();ll x=0;
for(;!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())
x=x*10+c-'0';
return x;
}
void pr(ll x)
{
if(x/10)pr(x/10);
putchar(x%10+'0');
}
int main()
{
//for(int i=1;i<100;i++)cout<<i<<' '<<((int)(sqrt(4+8*i)-2)/4)<<endl;
ll T=read();
while(T--)
{
ll n=read();
// cout<<T<<' ';
//ll n=T;
ll k= (sqrt(4+8*n)-2)/4;
ll rest=n-2*k*(k+1);
if(rest<=2*k+1)
{
ll dlt=(rest-1)/2;
pr(dlt+k*k);
putchar('\n');
}
else
{
rest-=2*k+1;
ll dlt= (rest-1)/2;
pr(k*k+k+dlt);
putchar('\n');
}
}
return 0;
}