文章标题 SPOJ PHT : Pigeonhole Tower(二分)

原创 2017年01月04日 00:05:11

Pigeonhole Tower

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

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

3 level 4level
3 Level Tower 4 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
题意: 给你n个木棒,然后看能堆成几层。
分析:通过看规律可知每层的数目是一个等差数列,所以对于所给数目n可以通过二分查找n所在的区间,通过所给数据可知,层数在1~1*10^6之间。
代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue> 
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
long long n;
int bs(){
    long long lo=1,hi=10e6;
    long long mid;
    while (lo<hi){
        mid=(hi+lo)/2;
        //如果找到区间就直接返回
        if (n<mid*(mid+2)&&n>=(mid-1)*(mid+1)) return mid-1;
        if (n>=mid*(mid+2))lo=mid;
        else hi=mid;
    }
}
int main ()
{
    int t;
    scanf ("%d",&t);
    int cnt=1;
    while (t--){
        scanf ("%lld",&n);
        if (n<3){
            printf ("Case %d: 0\n",cnt++);
            continue;
        }
        int ans=bs();
        printf ("Case %d: %d\n",cnt++,ans);
    }   
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

SPOJ - PHT【二分+预处理】

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

文章标题 codeforces 75C:Modified GCD (二分+最大公因数)

Modified GCDDescriptionWell, here is another math class task. In mathematics, GCD is the greatest co...

文章标题POJ 2785:4 Values whose Sum is 0?(二分)

4 Values whose Sum is 0DescriptionThe SUM problem can be formulated as follows: given four lists A, ...

文章标题 POJ 3272 : Monthly Expense (二分)

Monthly ExpenseDescription Farmer John is an astounding accounting wizard and has realized he might...

Problem D. Ice Cream Tower(2016 China-Final)【二分答案+贪心检验】

题意:这是2016 ACM-ICPC China-Final的D题,一共有N个冰淇淋球,做一个冰淇淋需要K个球,并且由于稳定性,这K个球还必须满足上下相邻的下面比上面大至少两倍。先给出N个球的质量,问...

文章标题自动标注程序

  • 2015-06-25 17:04
  • 6.93MB
  • 下载

文章标题

SLAM学习资料整理个人能力有限,若有错误请批评指正! 转载请标明出处:http://www.cnblogs.com/wenhust/书籍: 1.必读经典 Thrun S, Burgard W,...

文章标题

折腾:调整自己网站网页的css格式事情由来  最近看到java web这是一大块。正好也没有个人网站,买个云服务器给自己搭个网站也不错。整个网站现在还不算网站,没有后端操作,最多算网页吧。现在写的博客...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)