Gym - 100283F F. Bakkar In The Army 二分

本文介绍了一个复杂的惩罚计算模型,该模型通过三次二分查找算法来确定完成特定数量俯卧撑所需的最少重复次数。此过程涉及数据预处理二分查找、求和处理及分类讨论的二分查找,最终目的是找到达到指定惩罚次数的最有效方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Statements

Bakkar is now a senior college student studying computer science. And as many students; Bakkar fell in love with one of his finest colleagues Maymona. And as Bakkar has no brothers he is counting on getting an exemption from the military service after graduation. He got engaged to Maymona in their senior year counting on the exemption and a job he will get after graduation at the same place where he was interning last summer.

Well, man does not always get what he wants; the neither planned nor expected happened. Bakkar’s mother is pregnant and will give birth to Hareedy before Bakkar can get his exemption.

Hareedy is now born and unfortunately Bakkar will have to postpone his job and marriage plans for a year as he will serve as a military soldier for one year.

On the first 45 days, soldiers are trained in the military training center. They have to do a variety of exercises daily. One day Bakkar woke up late and didn't appear in the morning lineup at time. His commander is now angry and is going to punish him.

Bakkar is required to perform push-ups (the push-up position is called 6 esta'ed). His commander tells him to do them in reps (consecutive times) and then rest in between them. The commander wants him to follow a strict pattern. Given an upper limit, he will perform reps with increasing number of push-ups (1, 2, 3, ...) to warm up, until he reaches the upper limit. After that, he starts decreasing the number of push-ups per rep until he stops completely (..., 3, 2, 1). After resting, he will repeat the process again but with a higher upper limit. The upper limit starts with 1, and increases each time by a value of 1.

Here are the first 16 reps:

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1 ....

The total number of push-ups he does is the sum of all the reps has has done so far. So for example, the total number of push-ups after completing 4 reps = 1+1+2+1 = 5, and after completing 7 reps = 1+1+2+1+1+2+3 = 11.

Bakkar now has to do at least N push-ups. This is very exhausting so he needs to know the minimum number of reps to complete using this pattern to reach his punishment reps.

Input

Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1  ≤  T  ≤  100,000). Followed by T test cases, each test case will be a single integer N, the number of push-ups Bakkar wants to perform (1  ≤  N  ≤  1018).

Output

For each test case print a single line containing "Case n:" (without the quotes) where n is the test case number (starting from 1) followed by a single space, then a single integer representing the minimum number of reps needed as described above.

Examples
Input
5
6
9
11
21
35
Output
Case 1: 5
Case 2: 7
Case 3: 7
Case 4: 13
Case 5: 19

题目比较好懂,但是码起来比较难受,因为这道题包含三个二分,数据处理二分+分类讨论2种二分,思路是有的,不过二分处理起来很难受,首先我们对数据处理二分,我一开始以为数据直接暴力就可以了,没想到也要二分,所以我们要测出它的边界值,也就是当它输出的是10^18时候,应该到达的右边界应该是多少,我测试了150w是可以到达这个值,我们观察那个图形,我们可以发现它类似杨辉三角,不过这里是求和问题

    1        1^2      1

  1 2 1     2^2      3

1 2 3 2 1   3^3     5

我们可以发现每一段的所有数字加起来是n^2,所有从1-n段的数字全部加起来就是sum= n*(n+1)*(2*n+1),而那个1 3 5就是每一段的项数,是个等差数列 求和 n*n 所以我们二分找到我们所输入的  m<=sum的那一段k,因为是<=因此我们二分时候应该注意了,要使得二分出来的数字一定大于m那么我们直接返回l;   要使得二分出来的数字等于m,那么我们就应该有这个条件sum<n

ll binary()
{
    ll l=1,r=2e6,mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=mid*(mid+1)*(2*mid+1)/6;
        if(sum<n)
        {
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return l;
}

二分应该这样做

那么接着是求和处理

我一开始我以为是左边界应该为0的问题,但是后来测试数据发现是第三个二分写错了

第一个我们先比较最后那个值是在二分一的左边还是右边,左右两种情况考虑就可以了,仔细码!!!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
typedef long long ll;
using namespace std;
ll n;
ll bs(ll l,ll r)
{
    ll mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=(mid+1)*mid/2;
       // if(n==sum) return mid;
        if(n>sum) l=mid+1;
        else r=mid-1;
    }
    return l;
}
ll Bs(ll l,ll r,ll i,ll m)
{
    ll mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=(2*i+1-mid)*mid/2;
         //if(n==sum) return mid;
        if(sum<m) l=mid+1;
        else r=mid-1;
    }
    return l;
}
ll binary()
{
    ll l=1,r=2e6,mid,sum;
    while(l<=r)
    {
        mid=(l+r)/2;
        sum=mid*(mid+1)*(2*mid+1)/6;
        if(sum<n)
        {
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return l;
}
int main()
{
    freopen("army.in","r",stdin);
    ll t,Count=1,l;
    scanf("%lld",&t);
    while(t--)
    {
        ll i,k,temp,sum,mid;
        scanf("%lld",&n);
        l=binary();//找到那一行
        printf("Case %d: ",Count++);
        temp=(l-1)*l*(2*(l-1)+1)/6;//前面l-1行的总和
        n-=temp;//直接在第l行做操作
        sum=l*(l+1)/2;//第l行的中间值
       // printf("%lld\n",l);
        if(n<=sum)
        {
            k=bs(1,l);
            //printf("<= n==%lld l==%lld  k==%lld\n",n,l,k);
            printf("%lld\n",(l-1)*(l-1)+k);
        }
        else
        {
            k=Bs(1,l-1,l-1,n-sum);//去掉中间项只有l-1项
           //  printf("> n==%lld l==%lld  k==%lld\n",n,l,k);
            printf("%lld\n",(l-1)*(l-1)+l+k);
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值