HDU 6154.CaoHaha's staff

504


题目



点击显/隐题目

"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.


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).


Out put T integer in each line ,the least time CaoHaha can send the toy.


5
1
2
3
4
5


4
4
6
6
7




题解



给你一个面积,在平面坐标系中最少用多少长度为1√2的边能围出来


这种题基本上就是找规律的问题,首先画出来前几种的形状
首先要明确的原则是:
  • 周长越大围的面积也越大,也即尽可能多用√2
  • 周长相同的情况下越圆面积越大

如下图:

很容易发现,在7条边向8条边转换的时候,虽然正八边形更接近圆形,但是围出来正方形(菱形)面积更大

那么就可以看出来,其实只要是正好是4的倍数条边的时候能围成正方形面积最大
剩下只剩下3种情况,分别是向外拓展1、2、3条边,也是有规律的

打表记录有i个边能最大围出来多大,然后二分查找即可

最大面积是1e9 围成正方形需要的边是 4*√(1e9/2)=1.2e5
表只需要打到 1.5e5 即可


代码


点击显/隐代码
#include <algorithm>
#include <cstdio>
using namespace std;

const int maxn = 150000;

long long f[maxn];
void calc() {
    f[0] = f[1] = f[2] = f[3] = 0;
    f[4] = 2;
    f[5] = 2;
    f[6] = 4;
    f[7] = 5;
    for (long long i = 2; i < (maxn / 4) - 1; ++i) {
        f[i * 4] = i * i * 2;
        f[i * 4 + 1] = f[i * 4] + (i + i - 1) / 2;
        f[i * 4 + 2] = f[i * 4] + i * 2;
        f[i * 4 + 3] = f[i * 4 + 2] + (i + i + 1) / 2;
    }
}

int main() {
    // freopen("out.txt","w",stdout);
    int T;
    scanf("%d", &T);
    calc();
    
    /*
    for (int i = 1; i < 100; ++i) {
        printf("%d %d\n", i, lower_bound(f, f + maxn, i) - f);
    }
    */
    
    while (T--) {
        long long n;
        scanf("%I64d", &n);
        printf("%d\n", lower_bound(f, f + maxn, n) - f);
    }
    
    return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论