• CodeForces 748E Santa Claus and Tangerines(二分)


    题意:将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。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 1e6 + 10;
    const int MAXT = 1000000 + 10;
    using namespace std;
    int n;
    LL k;
    int a[MAXN];
    int vis[MAXN * 10];
    int dfs(int x, int t){
        if(vis[x] != -1) return vis[x];
        if(x == 1) return vis[x] = 1;
        if(x >= 2 * t){
            if(x & 1){
                return vis[x] = ((vis[x / 2 + 1] = dfs(x / 2 + 1, t)) + (vis[x / 2] = dfs(x / 2, t)));
            }
            else{
                return vis[x] = (vis[x / 2] = dfs(x / 2, t)) * 2;
            }
        }
        else if(x >= t){
            return vis[x] = 1;
        }
    }
    bool judge(int x){
        memset(vis, -1, sizeof vis);
        LL cnt = 0;
        for(int i = 0; i < n; ++i){
            if(a[i] >= x){
                cnt += (LL)dfs(a[i],  x);
            }
            else return false;
            if(cnt >= k){
               return true;
            }
        }
        return false;
    }
    int solve(){
        int l = 1, r = 1e7;
        while(l < r){
            int mid = l + (r - l + 1) / 2;
            if(judge(mid)) l = mid;
           else r = mid - 1;
        }
        return l;
    }
    int main(){
        while(scanf("%d%I64d", &n, &k) == 2){
            LL sum = 0;
            for(int i = 0; i < n; ++i){
                scanf("%d", &a[i]);
                sum += LL(a[i]);
            }
            if(sum < k){
                printf("-1\n");
                continue;
            }
            sort(a, a + n, greater<int>());
            printf("%d\n", solve());
        }
        return 0;
    }
    

      

  • 相关阅读:
    array与xml转换实现(转)
    设计模式之: 策略模式
    设计模式之: 代理模式
    设计模式之: 状态模式
    dedecms分页
    dedecms导出csv文件
    假如项目中使用到了多 表查询,怎么办?
    git忽略某个文件夹
    git忽略某个文件
    无极限分类
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6501120.html
Copyright © 2020-2023  润新知