HDU 6154 CaoHaha's staff

本篇介绍了在限定条件下,如何使用最少的线段绘制出足够大的魔法阵以传送特定大小的物品。通过分析不同面积下所需线段数量的变化规律,给出了具体的解题思路与实现代码。

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

CaoHaha's staff

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 337    Accepted Submission(s): 197

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

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

题意:在一个全由边长为1的正方形组成的平面内,输入一个面积S,如果每次只能取正方形的边或者对角线,需要多少线段才能围成一个面积不小于输入面积的多边形。

解题思路:昨天CCPC比赛的第5题,怎么说呢,最开始题目都没看懂,而且那时候好像大家都没怎么做这个题目,想着应该会很难就没再接着看,直到最后一个多小时的时候看见这个题的AC率慢慢往上涨就仔细看了题目,那时候看完题目就开始找规律,在面积为5只要7条边的时候卡了一会但是后来画着画着就出来了,因为心急就想着只要每次都取由对角线组成的正方形就好了,结果显然是不对的,所以昨天没有把题目做出来,刚刚A的时候有点心疼啊。找由对角线组成的正方形没错,但是在当面积不是正方形的整数倍的时候不应该往直接加正方形的方向想,其实在面积为5的时候就要想到,加一条对角线可以将面积增大1.5,所以呢主要是画图发现规律,多画几个就好了。

AC代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main(void)
{
	int T;//测试组数 
	int n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);//物品的大小 
		int m=sqrt(n/2);
		int sum=m*4;//找到由对角线组成的最小的正方形 
		if(n==m*m*2) ;//画图可以一下这样的结论
		else if(n>=m*m*2+1 && n<=m*m*2+m-1) sum+=1; 
		else if(n>=m*m*2+m && n<=m*m*2+2*m) sum+=2;
		else if(n>m*m*2+2*m && n<=m*m*2+3*m) sum+=3;
		else sum+=4;
		printf("%d\n",sum);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值