2017ccpc网络赛——CaoHaha's staff

"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




题意:给一个面积n,要求在网格中画出一个多边形使得这个多边形的面积不小于n,每一步只能画单元格的对角线或边(单元格的边长为1),求最少需要画多少次才能画出满足题意的多边形。


思路:
参考学长和同学的做法之后得出的奇妙解法。。。
因为只有对角线和边长选,那么优先选对角线来画,但是相同面积下,圆的周长最小,然而根本画不出圆(/(ㄒoㄒ)/~~),只能退而求其次画正方形。
假设正方形的边需要画x次那么这个正方形的面积就是S=2*x*x,如果S能尽量接近n那么画的最小次数就是4*x,但不是每个n都能十分接近S,这样就有了四次向外扩张的操作(嗯对,这就是最新的操作!)

直接上图:


每两个正方形之间夹着黑灰红白四种颜色,分别代表第一、二、三、四次扩张,至于为什么只有四次扩张可以想一想两个正方形所需要画的次数之差是多少(4次)
所以每扩张一次就是次数加一保证在获得面积最大时,画的次数最少,那么可以计算出每次扩张之后的面积:
第一次:2*x*x+(2*x-1)*0.5 画的次数是4*x+1
第二次:2*x*x+2*x 画的次数是4*x+2
第三次:2*x*x+3*x+0.5 画的次数是4*x+3
第四次:2*(x+1)*(x+1) 画的次数是4*(x+1)

分别与n比较之后输出次数即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        int x=sqrt(n*0.5);    //计算x,即小于n的最大正方形的边长次数
        if(2*x*x>=n)        //不用扩张
        {
            printf("%d\n", 4*x);
            continue;
        }
        if(2*x*x+(2*x-1)*0.5>=n)    //第一次扩张
        {
            printf("%d\n", 4*x+1);
            continue;
        }
        if(2*x*x+2*x>=n)        //第二次扩张
        {
            printf("%d\n", 4*x+2);
            continue;
        }
        if(2*x*x+3*x+0.5>=n)     //第三次扩张
        {
            printf("%d\n", 4*x+3);
            continue;
        }
        printf("%d\n", (x+1)*4);    //第四次扩张
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值