HDU 6154 CaoHaha's staff(【數學規律題】)

題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=6154

【中文題意】給你一個面積,問你最少可以用多少條邊圍成一個圖形的面積大於或等於給出的面積。邊可以是1cm,也可以是sqrt(2)cm。
【思路分析】畫圖畫了N久,網絡賽的時候和餅乾手推推到了20多,最後發現了一個規律。
4 2
5 2.5 x1=0.5
6 4 x1=1.5
7 5.5 x2=1.5
8 8 x2=2.5
9 9.5 x1=1.5
10 12 x1=2.5
11 14.5 x2=2.5
12 18 x2=3.5
前面是邊數,後面是可以組成的最大的面積,你會發現4個數裏面前兩個有規律,後兩個有規律。然後打表邊數就好了,最後暴力查找面積,當然可以使用lower_bound進行二分查找,因爲面積是一直增加的….
【AC代碼】

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
typedef long long LL;
double re[100005];
void init()
{
    double x1=0.5,x2=1.5;
    re[4]=2;
    for(LL i=5; i<100005; i++)
    {
        if(i%4==1)
        {
            re[i]=re[i-1]+x1;
        }
        if(i%4==2)
        {
            x1+=1;
            re[i]=re[i-1]+x1;
        }
        if(i%4==3)
        {
            re[i]=re[i-1]+x2;
        }
        if(i%4==0)
        {
            x2+=1;
            re[i]=re[i-1]+x2;
        }
    } /*for(int i=4;i<=12;i++)
    {
        printf("%d %lf\n",i,re[i]);
    }*/
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=4; i<100005; i++)
        {
            if(re[i]>=n)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
} /*
4 2
5 2.5 x1=0.5
6 4 x1=1.5
7 5.5 x2=1.5
8 8 x2=2.5
9 9.5 x1=1.5
10 12 x1=2.5
11 14.5 x2=2.5
12 18 x2=3.5

*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章