[CF752E]Santa Claus and Tangerines(二分答案,dp)
题目链接:http://codeforces.com/contest/752/problem/E
题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片。每个橘子每次对半分,偶数的话对半,奇数的话有一半会多一片。
二分答案,拿答案去判断。判断时记录dp(i)为橘子为i片的时候,最多分给多少人。枚举的时候从二分到的答案开始,由当前i的一半相加即可。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10001000; 5 typedef long long LL; 6 int n; 7 LL k; 8 int a[maxn]; 9 LL dp[maxn]; 10 11 bool ok(int each) { 12 memset(dp, 0, sizeof(dp)); 13 for(int i = each; i < maxn; i++) { 14 int lo = i / 2; 15 int hi = i - lo; 16 dp[i] = max(dp[lo] + dp[hi], 1LL); 17 } 18 LL ret = 0; 19 for(int i = 1; i <= n; i++) ret += dp[a[i]]; 20 return ret >= k; 21 } 22 23 int main() { 24 // freopen("in", "r", stdin); 25 while(cin >> n >> k) { 26 for(int i = 1; i <= n; i++) scanf("%d", &a[i]); 27 int ret = -1; 28 int lo = 0, hi = maxn - 1; 29 while(lo <= hi) { 30 int mid = (lo + hi) >> 1; 31 if(ok(mid)) { 32 lo = mid + 1; 33 ret = max(ret, mid); 34 } 35 else hi = mid - 1; 36 } 37 printf("%d\n", ret); 38 } 39 return 0; 40 }