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


题目链接:http://codeforces.com/contest/785/problem/C

题意:给两个数n,m,n表示仓库的最大容量(也是初始容量),m表示每天开始的时候给仓库加m粮(当然不能草果仓库的最大容量)。第 i 天吃掉 粮食 i,求第几天粮食被吃完。

可以分为两种情况:

     1.n<=m  该种情况表示只要当天没有吃完,第二天都会将仓库填满,也就是只有在当天吃完所有粮食才算结束。 也就是想要n天。

     2. n>m  该种情况在前m天,每天吃的粮食都小于等于m,都可以在第二天补满,故结束时间大于m。 

                   在m+1天的时结束的时候粮食剩下  n-(m+1)

                   在m+2天的时结束的时候粮食剩下  n-1-(m+2)=n-(m+1+2)

                   在m+3天的时结束的时候粮食剩下 n-1-2-(m+3)=n-(m+1+2+3)

                   在m+i天的时结束的时候粮食剩下 n-1-2-...-(m+i)=n-(m+1+2+3+...+i) = n-m-(1+i)*i/2

                   在第i天开始有  n-m-(1+i)*i/2<=0 的时候表示改天粮食被吃完


原本可以写一个

int main()
{
    int n,m;
    cin>>n>>m;
    int temp=n;
    int day=0;
    while(temp>0)
    {
        day++;
        temp+=m;
        if(temp>=n) temp=n;
        temp-=day;
    }
    cout<<day<<endl;
    return 0;
}
(要是真的写成这个就ac的话,那就太水了。。。),这个题数据比较大 (1 ≤  n ,  m  ≤ 10 18 (所以考虑用二分写),  



代码:
#include <iostream>
#include <cstdio>
#include <string>
#include<algorithm>
using namespace std;

typedef long long ll;
ll n,m;
ll binary(int s,int e)
{
    ll low,high,mid;
    low=s,high=e;
    ll i=1e18;
    while(low<=high)
    {
        mid=(low+high)/2;
        long long temp=(1+mid)*mid/2;
       if(temp>=n-m)
       {
           high=mid-1;
            i=min(i,mid);
       }
       else low=mid+1;
    }
    return i;
}

int main()
{

    cin>>n>>m;
     if(n<=m)
     {
         cout<<n<<endl;
     }
     else
     {
         cout<<m+binary(1,1e18)<<endl;
     }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值