CCPC网赛 CaoHaha's staff(找规律+模拟扩展正方形)

本文探讨了一种特殊的数学问题,即如何利用单位长度的边和根号2长度的斜边构建一个面积不小于给定值的图形,并尽可能减少所需的边数。通过规律寻找和公式推导,给出了最优解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"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


题解:

比赛当时没想出来。。后来学长说了是找规律四次扩展以后就想了一会,推出了公式。。。这题还真有意思

题意:

输入一个面积,你只能用单位长1或者是单位格子的斜边根号2的边来围成这个图形,问最短为多少,注意围成的面积可以大于输入的值

思路:

我们可以每一条直边的贡献是1,而每一条斜边的贡献是根号2,所以越多斜边越好,而这些斜边如何摆放使得面积最大呢,就是用这些斜边组成一个正方形,那其他情况呢,根据找规律我们可以得出有四次扩展,每次扩展用的边数+1,但是得到新增的面积是最大化的,请看图


我们把里面的正方形进行扩展,共扩展4次得到一个新的正方形,我用红白相间标出了每次扩展的部分,然后根据平行关系推一下公式就好了,看他要求的面积在哪个区间里面,输出边数就是了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
using namespace std;
#define lson k*2
#define rson k*2+1
#define M (t[k].l+t[k].r)/2
#define INF 1008611111
#define ll long long
#define eps 1e-15
int main()
{
    ll i,j,test,n;
    scanf("%lld",&test);
    while(test--)
    {
        scanf("%lld",&n);
        if(n==1)//特殊处理下1
        {
            printf("4\n");
            continue;
        }
        int d=sqrt(n*1.0)/sqrt(2.0);//最初的正方形斜边长度
        if(d*d*2>=n)//没进行扩展的情况
        {
            printf("%lld\n",4*d);
            continue;
        }
        if(d*d*2+(2*d-1)/2>=n)//扩展1次的情况,后面以此类推
        {
            printf("%lld\n",4*d+1);
            continue;
        }
        if(d*d*2+2*d>=n)
        {
            printf("%lld\n",4*d+2);
            continue;
        }
        if(d*d*2+3*d+0.5>=n)
        {
            printf("%lld\n",4*d+3);
            continue;
        }
        printf("%lld\n",(d+1)*4);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值