当前位置: 首页 > 工具软件 > tale-iiice > 使用案例 >

Anton and Fairy Tale--二分,因为手误没写对

柴宏浚
2023-12-01

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale

大概题意,有一个容量为 n 的粮仓,初始是满的,每天早上补充 m 个单位粮食,不能超出粮仓的容量,多出的部分不管,从第一天晚上开始来对应天数的小鸟,每只偷一个单位粮食(第一天来一只,第二天来两只,以此类推),问粮仓第一次空的天数,例如n=1,m=5,初始为满,第一天的补充没用用,第一天晚上来了一只小鸟偷一个单位粮食,所以第一天就空了。注意是每天早上补充,晚上偷,所以每天晚上都不是满的)

分析:
因为每天都能补充m,所以第m+1天之前都是满的,从第m+1天开始出现真正的盈亏1,2,3,4,….,直到某天x,真正的盈亏加上m==n,就为空了。
另外,到了第n天晚上是一定会变成空的

1<=n,m<=10的18次方
这种这么大规模的数,肯定用二分而不是轮询遍历的,可惜我把边界弄错了,我以为是(1,n),其实不是的

贴代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int ll;
int main()
{
    ll n,m;
    scanf("%lld %lld",&n,&m);
    ll ans = n;
    ll left = m + 1,right = m + 2e9;
    while(left < right)
    {
        ll mid = (right-left)/2 + left;
        ll xday = mid-m;            //x days after the m th day;
        ll tot = m + (1LL + xday) * xday / 2LL; //remember to add m

        if(tot < n)left = mid + 1;          //not empty yet
        else right = mid;           //already empty
    }
    ans = min(ans,left);
    printf("%lld\n",ans);
    return 0;
}
 类似资料: