本文主要是介绍HDU 6154 CaoHaha's staff,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
2017中国大学生程序设计竞赛 - 网络选拔赛
题目大意
给你一个大小为n的玩具,若想邮寄出去它,必须画出面积不小于它的多边形(只能沿着格子边缘或者对角线),一个单位的格子或者一个格子的对角线为一步,问最少需要多少步。
解题思路
根据贪心策略,为了使结果最小,我们要尽可能多的走对角线。通过分析见下图
当步数为偶数步时,我们可以把图形画成 n2√∗m2√ (为使面积最大,n、m的差值应最小)的形状,而当步数为奇数步时,我们可以把上一步中较长的那条边拆开(即t=max(n,m))然后分别向正右和正上画一步,再走对角线,这时候面积增加了 (t∗2−1)∗0.5 步,这便是最佳的策略。那么我们根据步数打表表示面积最后遍历求解就可以了。
代码实现
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100007
int area[maxn];
void init()
{memset(area,0,sizeof(area));for(int i=4;i<maxn;i++){if(i%2){area[i]=int(area[i-1]+((((i-1)/2)%2+(i-1)/2/2)*2-1)*0.5);}else{int t=i/2;int a=t/2;int b=t%2+t/2;area[i]=a*b*2;}}
}
int main()
{init();int T;int n;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=4;i<maxn;i++){if(area[i]>=n){printf("%d\n",i);break;}}}return 0;
}
这篇关于HDU 6154 CaoHaha's staff的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!