2017CCPC网络选拔赛1005CaoHaha's staff(规律贪心)

时间:2022-12-20 23:26:07

2017CCPC网络资格赛1005


描述

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

输入

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)

样例输入

5
1
2
3
4
5

样例输出

4
4
6
6
7

题意:

  • 意思是在坐标系中,输入给我们一个面积s1,我们需要围出一个面积s2,(s2>=s1)。

  • 如何围出面积,有一定的约束方法。此坐标系平面由无数个单位面积为1的正方形组成,我们每一次可以沿着这个单位的边画一条线,或者是在沿着单位的对角线画一条线。

  • 现在要求最少需要画几次。

思路

  • 其实这个题好像有什么皮克定理,但是不会用。我就按照我的思路讲一下。

  • 首先我们转变一下思路,我们要求得某面积的画的最少的次数,可以转变为某些次数可以得到的最大的面积。那么我们从4条边一直画,最多画到10条的时候,我们可以发现。当画的次数为4,6,8,10,12的时候,面积是1*1*2,2*1*2,2*2*2,3*2*2,3*3*2,也就是说有数m,n就是我们画的面积的长和宽(对于偶数面积来说,一定是一个矩形)。也就是说对于偶数面积,它所需要的最小次数的增长是有一定规律的,总结为,当m==n时,面积为(++m)*n ;当m!=n时,面积为m*(++n).

  • 那画的次数为奇数所能得到的最大面积怎么确定呢?我们可以发现当画的次数为4的时候,长和宽都是1个单位(对角线和沿着边画都算一个单位),那么从4拓展到5,也就是1次画的次数拓展为2次,面积增加0.5;当画的次数为6的时候,长和宽分别是2个和1个单位,那么就是从max(2,1)扩展为1次,也就是2次扩展为3次,面积增加1.5.好了规律出来了,从偶数到奇数的变换,就是偶数的面积加上能够拓展出来的面积,拓展出来的面积又与max(长,宽)有关。

  • 然后打表,读入面积后,就可以二分查找即可。

AC代码


#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6+5;
double area[maxn];

int main()
{
area[4] = 2;
int m = 2,n = 1;
double temp;
for(int i = 5;i<=maxn;i++)
{
if(i%2 != 0)
{
temp = (double)((i+1)/4 - 0.5);
area[i] = area[i-1] + temp;
}

else
{
area[i] = m*n*2;
if(m == n)
m++;

else n++;
}
}

// cout<<area[maxn];

int T;
scanf("%d",&T);
while(T--)
{
double s;
cin>>s;
long long ans = lower_bound(area,area+99999,s) - area;
printf("%d\n",ans);

}

return 0;
}