2015EC-final A - Boxes and Balls(二分查找)

1 篇文章 0 订阅
1 篇文章 0 订阅

声明:题目来源于2015年EC-final A题。

题目描述:
Little Tom’s friend Jack just showed him a great magic trick. At the beginning of the trick, there is one box on the ground with some number of balls in it. Jack then performs this operation over and over again:
1. put a new empty box down on the ground
2. move one ball from each other box into that new empty box
3. remove any boxes that are now empty
4. sort the boxes in nondecreasing order by the number of balls in them
Tom noticed that it is possible for this operation to leave the state of the boxes and balls unchanged! For example:
• Suppose that at the beginning of the trick, the one box contains 3 balls.
• In the first operation, Jack adds a new empty box, puts 1 ball from the existing box into it, and sorts the boxes, so after that operation, there will be 2 boxes on the ground, one with 1 ball and one with 2 balls.
• In the second operation, Jack adds a new empty box and puts 1 ball from each of the existing 2 boxes into it; this creates one empty box, which Jack removes, and then he sorts the boxes. So there are 2 boxes on the ground, one with 1 ball and one with 2 balls. But this is exactly the state that was present before the second operation!
Tom thought about the trick some more, and realized that for some numbers of balls, it is not possible for the operation to leave the state unchanged. For example, if there are 2 balls at the beginning, then after one operation, there will be two boxes with 1 ball each, and after 2 operations, there will be one box with 2 balls, and so on, alternating between these two states forever.
Tom looked around in his room and found infinitely many empty boxes, but only N balls. What is the maximum number of those balls that he could use to perform this trick, such that one operation leaves the state unchanged?

Input
The first line of the input gives the number of test cases, T. T lines follow. Each line consist of one integer N, the number of balls Tom could find. Output For each test case, output one line containing ‘Case #x: y’, where x is the test case number (starting from 1) and y is the maximum number of balls that Tom could use to perform the trick, as described above.
Limits:
• 1 ≤ T ≤ 100.
• 1 ≤ N ≤ 10^18 .
Note: The trick can be performed with 1 ball or 3 balls, but not with 2 balls. So, for Case #2, Tom can use at most 1 of the 2 balls. Sample

Input
3
1
2
3
Sample Output

Case #1: 1
Case #2: 1
Case #3: 3

题意:
有n个球,无数个盒子,最初所有的球在一个盒子里面,每一次添加一个新盒子,并把所有盒子里的球把其中一个移动到新盒子里,并把空盒子移除掉。
在刚开始有3个球时;一次操作后有两个盒子,其中一个盒子有一个球,另一个盒子有2个球;再次进行一次操作,依然是有两个盒子,其中一个盒子有一个球,另一个盒子有2个球。前后操作后球的分布是一样的(不考虑顺序问题)。
那么在有n个球的情况下,最多用多少球可以完成在每完成一次操作后球的分布情况不变(球的分布状态稳定后)。

观察一下规律的话,在球的分布是:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
的情况下满足,它们球的总数是1,3,6,10,15。emmm…,与等差数列有关,化简一下后,相当于求m属于整数情况下m*(m-1)/2小于n的最大值。
n的最大值能达到10^18,emm…暴力肯定超时,用二分查找的话完全可以。
二分查找只能在有序数列中查找,每次查找中间的值与目标值进行比较,然后缩小范围继续查找,每次范围可以缩小一半,所以时间复杂度O(ln N)。

#include<iostream>
using namespace std;
int main()
{
    int T;cin>>T;
    for(int t=1;t<=T;++t)
    {
        long long n;cin>>n;
        long long l=1,r=2e9;  //在n等于10^18次方时,m的值是1.4*10^9多点,所以查找范围在1- 2*10^9即可。
        while(l<r)    //二分查找
        {
            long long m=l+r+1>>1;
            long long t=m*(m+1)>>1;
            if(t<=n)
                l=m;
            else
                r=m-1;
        }
        cout<<"Case #"<<t<<": "<<l*(l+1)/2<<endl;  //注意输出格式
    }
    return 0;
}

制作人:刘洋

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值