HDU 6154 CaoHaha's staff【找规律】【思维题】【好题】

CaoHaha's staff

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 297    Accepted Submission(s): 165


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.
 

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).
 

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
 

Source

题意:在一个二维坐标系中,想要得到一块为n的面积,问最少在坐标轴上面画多少笔画可以得到这样的面积,笔画可以是上、下、左、右、两种对角线共六种。


如果边为4k则面积为2*k*k,在加一条边,面积增加(k+k-1)/2,加两条则直接多一行,加三条面积增加(k+k+1)/2 【相当于在变最长的一条上往外延伸】,加4条又回到最初情况。

ps(注意输入可能为小数,还要注意计算面积的时候可能会爆int)

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;

typedef long long ll;

const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 100000;

double a[100000] = { 0 };

void solve()
{
	for (ll i = 4; i <= maxn; i++)
	{
		if (i % 4 == 0)
		{
			a[i] = i*i / 8.0;
		}
		else
		{
			ll p = i % 4;
			ll q = i / 4;
			if (p == 1)
			{
				a[i] = q*q * 2.0 + (2.0 * q - 1.0) / 2.0;
			}
			else if (p == 2)
			{
				a[i] = q*(q + 1.0) * 2.0;
			}
			else if (p == 3)
			{
				a[i] = a[i-1] + (2.0 * q + 1.0) / 2.0;
			}
		}
	}
}


int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out1.txt","w",stdout);
	solve();
	int t;
	scanf("%d", &t);
	while (t--)
	{
		double n;
		scanf("%lf", &n);
		int ans = lower_bound(a + 4, a + maxn, n) - a;
		printf("%d
", ans);
	}
}
Fighting~
原文地址:https://www.cnblogs.com/Archger/p/8451570.html