题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切,

1、若蛋糕值为偶数,那一次可切成对等的两块。

2、若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1。

3、若蛋糕值为1,则不可切。

问每个人拥有的蛋糕中最小蛋糕值可能的最大值是多少。

分析:

1、若每个蛋糕都分成蛋糕值为1,即n个蛋糕的蛋糕值总和<k,则输出-1。

2、验证mid是否成立,若某蛋糕值x大于mid,则可切,并将该蛋糕可切成的块数保存在vis[x]中。

3、若mid为5,而x为11,则有必要切蛋糕x,若x为8,则没必要切蛋糕x。

  1. #pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cctype>
  6. #include<cmath>
  7. #include<iostream>
  8. #include<sstream>
  9. #include<iterator>
  10. #include<algorithm>
  11. #include<string>
  12. #include<vector>
  13. #include<set>
  14. #include<map>
  15. #include<stack>
  16. #include<deque>
  17. #include<queue>
  18. #include<list>
  19. #define Min(a, b) ((a < b) ? a : b)
  20. #define Max(a, b) ((a < b) ? b : a)
  21. const double eps = 1e-8;
  22. inline int dcmp(double a, double b){
  23. if(fabs(a - b) < eps) return 0;
  24. return a > b ? 1 : -1;
  25. }
  26. typedef long long LL;
  27. typedef unsigned long long ULL;
  28. const int INT_INF = 0x3f3f3f3f;
  29. const int INT_M_INF = 0x7f7f7f7f;
  30. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  31. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  32. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  33. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  34. const int MOD = 1e9 + 7;
  35. const double pi = acos(-1.0);
  36. const int MAXN = 1e6 + 10;
  37. const int MAXT = 1000000 + 10;
  38. using namespace std;
  39. int n;
  40. LL k;
  41. int a[MAXN];
  42. int vis[MAXN * 10];
  43. int dfs(int x, int t){
  44. if(vis[x] != -1) return vis[x];
  45. if(x == 1) return vis[x] = 1;
  46. if(x >= 2 * t){
  47. if(x & 1){
  48. return vis[x] = ((vis[x / 2 + 1] = dfs(x / 2 + 1, t)) + (vis[x / 2] = dfs(x / 2, t)));
  49. }
  50. else{
  51. return vis[x] = (vis[x / 2] = dfs(x / 2, t)) * 2;
  52. }
  53. }
  54. else if(x >= t){
  55. return vis[x] = 1;
  56. }
  57. }
  58. bool judge(int x){
  59. memset(vis, -1, sizeof vis);
  60. LL cnt = 0;
  61. for(int i = 0; i < n; ++i){
  62. if(a[i] >= x){
  63. cnt += (LL)dfs(a[i], x);
  64. }
  65. else return false;
  66. if(cnt >= k){
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. int solve(){
  73. int l = 1, r = 1e7;
  74. while(l < r){
  75. int mid = l + (r - l + 1) / 2;
  76. if(judge(mid)) l = mid;
  77. else r = mid - 1;
  78. }
  79. return l;
  80. }
  81. int main(){
  82. while(scanf("%d%I64d", &n, &k) == 2){
  83. LL sum = 0;
  84. for(int i = 0; i < n; ++i){
  85. scanf("%d", &a[i]);
  86. sum += LL(a[i]);
  87. }
  88. if(sum < k){
  89. printf("-1\n");
  90. continue;
  91. }
  92. sort(a, a + n, greater<int>());
  93. printf("%d\n", solve());
  94. }
  95. return 0;
  96. }

  

CodeForces - 748E Santa Claus and Tangerines(二分)的更多相关文章

  1. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. E. Santa Claus and Tangerines 二分答案 + 记忆化搜索

    http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...

  3. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. Santa Claus and Tangerines

    Santa Claus and Tangerines 题目链接:http://codeforces.com/contest/752/problem/E 二分 显然直接求答案并不是很容易,于是我们将其转 ...

  5. E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. Codeforces Round #389 Div.2 E. Santa Claus and Tangerines

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. Codeforces 752C - Santa Claus and Robot - [简单思维题]

    题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...

  8. Codeforces 784B Santa Claus and Keyboard Check

    题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...

  9. [CF752E]Santa Claus and Tangerines(二分答案,dp)

    题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...

随机推荐

  1. 51nod 1433:0和5

    1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  取消关注 小K手中有n张牌,每张牌上有一个一位数的数, ...

  2. Day6 - B - 采花 HYSBZ - 2743

    萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公主采花.公 ...

  3. MySQL之表、列别名及各种JOIN连接详解

    MySQL在SQL中,合理的别名可以让SQL更容易以及可读性更高.别名使用as来表示,可以分为表别名和列别名,别名应该是先定义后使用才对,所以首先要了解sql的执行顺序(1) from(2) on(3 ...

  4. TFIDF介绍

    简介 全称: Term Frequency-inverse document frequency(文本频率与逆文档频率指数) 目的: 表征一个token(可以是一个字或者一个词)的重要程度 是Elas ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-inbox

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. TS文件极简合并

    TS文件是可以直接通过二进制拷贝连接的方式进行合并的,一般采用如下的命令行参数:copy /b 1.ts+2.ts+3.ts new.ts 这个例子就是将1.ts.2.ts.3.ts三个文件按顺序连接 ...

  7. 让vscode使用Pipenv工作环境

    1.查看Pipenv的位置 # 先激活Pipenv环境 pipenv shell # 获取当前虚拟环境的位置 pipenv --venv 2.打开setting.json配置文件 Ctrl+Shift ...

  8. 四十六、SAP的Message中E和W区别

    一.如果写的是E,则报红色的信息,如图 效果如下 二.如果是写的是W,则报黄色的信息 效果如下

  9. Power BI Premium

    Power BI Premium的设计是为了满足拥有大量数据的大公司的需求.微软已经重新构建了Power BI的架构,以允许大量的“只读”用户.Premium用户还可以利用很多新功能. Power B ...

  10. hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...