题目链接: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;
}