2017中国大学生程序设计竞赛(网络选拔赛) HDU 6154 CaoHaha's staff

来源:互联网 发布:mac下制作u盘linux 编辑:程序博客网 时间:2024/06/05 08:51

CaoHaha’s staff

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

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

512345

Sample Output

44667


题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6154

分析

题意:给出T组数据,每组包含一个正整数S表示最小面积,现在要求在一个矩阵中,每一笔只能画一条边或一条对角线,求出最少需要多少笔能画出面积大于等于S的区域
这道题刚开始的思路放在了测试数据上,发现7笔画不出面积为5的区域,画了好久才发现7笔可以画出面积5.5,如图
这里写图片描述
后来摆脱原本的跟着测试数据去尝试用答案画出样例的错误思想,试着用n条边来画出最大面积才成功找出规律AC了这道题
其实当每条边处于对角线组成正方形时,边长为n倍的根号2,而边落在格子的边上时边长为n
这里写图片描述
同样的4n条边显然落在对角线上围成的面积更大
这里写图片描述
所以我们可以将可以组成上图这种形状的边数找出来,很显然就是刚刚所说的4n条边,其围成的最大面积就是n倍根号2乘以n倍根号2,也就是2 *n *n,然后就是找4(n-1)到4n之间的3条边的规律
n=8时,如上图,n=9时相当于8上加1条边,如下图蓝色线段是比原本多加了1条边,此时面积最大,相当于加了1.5的面积
这里写图片描述
而n=10是多加了2条边,如图,蓝色部分再次突出去,而此时两条蓝色边在同一条直线上时,像黄色边那样可以多加1的面积,此时的外轮廓是最大面积,相当于加了4的面积
这里写图片描述
而n=10也可以如下图,不过两种画法所加的面积是相同的,而代码中的思路用的是下图,把左右分开,加上的是n/2的面积,也就是4
这里写图片描述
而n=11时可以突出去两部分,面积加了6.5
这里写图片描述
而n=12就是另一个正方形了
我们可以将数据记录下,很容易发现规律,边为奇数相当于上一项加了x.5,这个x是每用2次加1,偶数的话是每用两次加2,最开始的数据已给出,所以前两个都只用1次
这里写图片描述
这就是规律了,不懂得对照代码看一下再理解理解

用word画了1个多小时的图,转载记得标明出处:http://blog.csdn.net/qq_38576126/article/details/77446840
老铁们,顶一下呗
这里写图片描述

代码

#include <bits/stdc++.h>using namespace std;const int MAX=90000;///90000条边围成的面积超过1e9,所以开90000就够了///按斜边的正方形来算4乘以根号下(1e9/2)是8W多,9W数组就够了double n[MAX],f=1.5,p=4;void prit(){    memset(n,0,sizeof(n));    n[4]=2;    n[5]=2.5;    n[6]=4;    for(int i=7; i<=MAX; i++)///根据上述的规律将每种边数可以构成的最大面积存放起来    {        int y=i%4;        if(y==1)        {            n[i]=n[i-1]+f;            f+=1.0;        }        if(y==2)        {            n[i]=n[i-2]+p;            p+=2.0;        }        if(y==3)        {            n[i]=n[i-1]+f;        }        if(y==0)        {            n[i]=n[i-2]+p;        }    }}int main(){    prit();    int T;    scanf("%d",&T);    while(T--)    {        double N;        scanf("%lf",&N);        for(int i=4; i<=MAX; i++)        {            if(n[i]>=N)///判断多少条边满足面积N            {                printf("%d\n",i);                break;            }        }    }}
阅读全文
2 0
原创粉丝点击