CodeForces 991C Candies(二分答案)
http://codeforces.com/problemset/problem/991/C
题意:
给出一个数n,表示有n块蛋糕,有两个人a,b。a每次可以取k块蛋糕(如果剩下的蛋糕不足k,则一次性取完),
b每次取当前蛋糕的十分之一(如果不是整数的话,向下取整,例如,有9块蛋糕的话,那么b取0块)
求一个最小的k,使得a所取的蛋糕至少大于n的一半。
思路:
二分枚举k然后判断是否满足条件即可。
#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <math.h> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <sstream> const int INF=0x3f3f3f3f; typedef long long LL; #define Bug cout<<"---------------------"<<endl const int mod=1e9+7; const int maxn=2e5+10; using namespace std; int judge(LL n,LL k) { LL t=n; LL sum1=0; LL sum2=0; while(t) { if(t>=k) { t-=k; sum1+=k; } else { sum1+=t; t=0; } sum2+=t/10; t-=t/10; } if(sum1>=sum2) return 1; else return 0; } int main() { LL n; scanf("%lld",&n); LL l=1; LL r=n; LL mid; LL ans; while(l<=r) { mid=(l+r)>>1; if(judge(n,mid)) { ans=mid; r=mid-1; } else l=mid+1; } printf("%lld\n",ans); return 0; }
标签:二分,Candies,LL,mid,CodeForces,991C,蛋糕,ans,include From: https://www.cnblogs.com/jiamian/p/17023828.html