题意
有N个糖果,Vasya每天早起吃k个,Petya每天晚上吃%10。Vasya至少要吃一半,最小的k为多少?
思路
二分答案,注意开long long,奇数的情况,n=1的时候。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
typedef long long ll;
ll n, ans;
bool check(ll k)
{
ll x = n, cnt = 0;
while (x > 0)
{
cnt += min(x, k);//x<k不够吃,此时吃了x个
x -= k;
x -= x/10;
}
return cnt >= (n+1)/2;//注意奇数
}
int main()
{
scanf("%lld", &n);
ll l = 1, r = (n+1)/2, m = (l+r)/2;
while (l < r)
{
if (check(m)) ans = r = m;
else l = m+1;
m = (l+r)/2;
}
if (ans == 0) ans = 1;//n = 1的特殊情况
printf("%lld\n", ans);
return 0;
}
/*
68
*/