您的位置:首页 > 理论基础 > 计算机网络

简单规律 HDU - 6154 CaoHaha's staff[2017 CCPC网络选拔赛]

2017-08-21 20:28 459 查看
CaoHaha's staff


题目链接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 667 Accepted Submission(s): 393

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 t
4000
o 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

2017中国大学生程序设计竞赛 - 网络选拔赛

Recommend

liuyiding



辛辛苦苦写的题解因为乱码,编辑了一遍不小心删了。。。

重写一遍吧。

找规律可以发现当n(边数)%4==0时,围成面积很好求得。

以图中最小的正方形为例,边数为8,边长为 (8/4) * 根2, 边长的平方即为面积。

故(n%4==0)时, 面积 = ((n/4) * 根2)的平方

然后当拓展一条边(n%4==1)时,沿着这个已得的正方形拓展,可拓展出一个等腰梯形的面积(即途中描黑的梯形)。可根据梯形的面积公式得出

当前面积 = [根2*((n-1)/4) + 根2*(n/4)] * (根2/2) * (1/2)

—————— 上底 ———– + ——— 下底 — × — 高 ——– (/2)

整理得小梯形的面积 t = (n-1)/4

当拓展出两条边(n%4==2)时,可拓展出两个全等的等腰梯形加上上边合计面积为1的两个小三角。 2*t+1;

当拓展出三条边(n%4==3)时,拓展出的部分即途中阴影部分,即三个等腰梯形加合计面积为2的四个小三角形。3*t+2;

规律已得出。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define PI acos(-1.0)
#define mod 1e9+7
#define inf 0x3f3f3f3f
using namespace std;

int main()
{

int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d", &n);
int ans;
if(n==1||n==2) ans = 4;
else if(n==3||n==4) ans = 6;
else if(n==5) ans = 7;
else
{
int i = 7;
double ts = 0, s = 0, t=0;
while(s <  n)
{
i++;
if(i%4 == 0)
{
ts = (sqrt(2.0) * i/4)*(sqrt(2.0) * i/4);
t= (2*i-2)/8;
}
s = ts;
if(i%4 == 1)
{
s = ts + t;
}
if(i%4 == 2)
{
s = ts + 2*t+1;
}
if(i%4 == 3)
{
s = ts + 3*t+2;
}
}
ans = i;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: