B - Pigeonhole Tower

Pigeon SSNA want to build a tower with some wood walls. Let's describe the tower they want to make:

  1. A Tower can consist of different number of level.
  2. If a tower contain levels then 1st level must contain  holes , 2nd level L-1 , 3rd level L-2 ….. L level contain 1 hole .
  3. Each room contain 3 wood walls.

See the picture below:

3 level4level
3 Level Tower4 Level tower

Now pigeon SSNA has n wood walls. What is maximum number of level he can made.

Input

Input starts with an integer T (≤ 100000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1012)

Output

For each case of input you have to print the case number and expected answer.

Sample Input

Output for Sample Input

2
15
24
Case 1: 3
Case 2: 4

题意:给出一个数,代表边的条数,问能够搭建几层(要能完整的搭建,比如输入为4,那输出只能是1,不能是2),1,2层输出0.    二分查找

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
#define LL long long
LL f[1000001];
int main()
{
    int t,l,r;
    LL n;
    f[0]=0;
    for(int i=1; i<=1000000; i++)
        f[i]=f[i-1]+1+2*i;
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        l=1,r=1000000;
        scanf("%lld",&n);
        if(n<3)
            printf("Case %d: 0\n",cas++);
        else
        {
            int mid=(l+r)/2;
            while(l<r&&f[mid]!=n)
            {
                if(f[mid]>n)
                    r=mid-1;
                else if(f[mid]<n)
                    l=mid+1;
                mid=(l+r)/2;
            }
            printf("Case %d: %d\n",cas++,f[mid]<=n?mid:mid-1);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值