third

CodeForces - 991C div-2

题意:

有N个糖果,Vasya每天早起吃k个,Petya每天晚上吃10%。Vasya至少要吃一半,最小的k为多少?

思路:

二分找最优解,注意的点:long long ,奇数的情况 , n == 1的情况

首先是两个二分模板:

注意:关于上取整还是下取整:
如果题目中要求最大值,就用上取整,如果要求最小值,就用下取整

 

 

然后本题的分析过程就是要求k的最小值,所以过程为向下取整

 

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 long long n, ans;
 8 
 9 bool check(long long mid)//模拟过程
10 {
11     long long x = n,cnt = 0;
12     while(x > 0)
13     {
14         cnt += min(x , mid);//x<mid不够吃,此时吃了x个
15         x -= mid;
16         x -= x / 10;
17     }
18     return cnt >= (n + 1) / 2;//注意奇数 
19 }
20 
21 int main()
22 {
23     cin >> n;
24     long long l = 1, r = n;
25     while(l < r)
26     {
27         long long mid = l + r >> 1;
28         if(check(mid)) ans = r = mid;
29         else l = mid + 1;
30     }
31     if(n == 1) ans = 1;//n=1时因为l和r相同,所以ans最后为0,但ans实际上显然为最小为1,所以要特判
32     cout << ans << endl; //或者ans初始值就设为1就不需要特判
33     return 0;
34 }

 

posted @ 2021-02-01 22:06  彦辰kkkkk  阅读(174)  评论(0)    收藏  举报