zoukankan      html  css  js  c++  java
  • 【codeforces 752E】Santa Claus and Tangerines

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Santa Claus has n tangerines, and the i-th of them consists of exactly ai slices. Santa Claus came to a school which has k pupils. Santa decided to treat them with tangerines.

    However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to divide tangerines into parts so that no one will be offended. In order to do this, he can divide a tangerine or any existing part into two smaller equal parts. If the number of slices in the part he wants to split is odd, then one of the resulting parts will have one slice more than the other. It’s forbidden to divide a part consisting of only one slice.

    Santa Claus wants to present to everyone either a whole tangerine or exactly one part of it (that also means that everyone must get a positive number of slices). One or several tangerines or their parts may stay with Santa.

    Let bi be the number of slices the i-th pupil has in the end. Let Santa’s joy be the minimum among all bi’s.

    Your task is to find the maximum possible joy Santa can have after he treats everyone with tangerines (or their parts).

    Input
    The first line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 2·109) denoting the number of tangerines and the number of pupils, respectively.

    The second line consists of n positive integers a1, a2, …, an (1 ≤ ai ≤ 107), where ai stands for the number of slices the i-th tangerine consists of.

    Output
    If there’s no way to present a tangerine or a part of tangerine to everyone, print -1. Otherwise, print the maximum possible joy that Santa can have.

    Examples
    input
    3 2
    5 9 3
    output
    5
    input
    2 4
    12 14
    output
    6
    input
    2 3
    1 1
    output
    -1
    Note
    In the first example Santa should divide the second tangerine into two parts with 5 and 4 slices. After that he can present the part with 5 slices to the first pupil and the whole first tangerine (with 5 slices, too) to the second pupil.

    In the second example Santa should divide both tangerines, so that he’ll be able to present two parts with 6 slices and two parts with 7 slices.

    In the third example Santa Claus can’t present 2 slices to 3 pupils in such a way that everyone will have anything.

    【题目链接】:http://codeforces.com/contest/752/problem/E

    【题解】

    二分圣诞老人的joy值就好;
    看看每个橘子能够分成多少片(最后的大小大于等于二分的joy的情况下尽可能多);
    这个可以用一个递推来搞;
    即记录1..1e7里面每个数字有多少个;
    然后逆序循环从1e7到1即可,按照i的奇偶性把i的值传给i/2或i/2和i/2+1;
    然后计算这种情况下能够给多少个pupil吃;
    如果够吃就让joy变大、否则减小joy;
    感觉时间复杂度有点紧张;
    log2(1e7)≈23
    23*1e7≈2e9…

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 1e6+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n;
    LL k,l,r,tjoy;
    int a[MAXN];
    LL bo[10000009];
    
    bool ok(LL joy)
    {
        rep1(i,1,10000000)
            bo[i] = 0;
        rep1(i,1,n)
            bo[a[i]]++;
        LL num = 0;
        rep2(i,10000000,joy)
            {
                if (i/2>=joy)
                {
                    if (i&1)
                    {
                        bo[i/2]+=bo[i];
                        bo[i/2+1]+=bo[i];
                    }
                    else
                        bo[i/2]+=2*bo[i];
                }
                else
                    num+=bo[i];
            }
        if (num >= k)
            return true;
        else
            return false;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rel(k);
        rep1(i,1,n)
            rei(a[i]);
        l = 1,r = 1e7;
        LL ans =-1;
        while (l <= r)
        {
            LL m = (l+r)>>1;
            if (ok(m))
                {
                    ans = m;
                    l = m+1;
                }
                else
                    r = m-1;
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    js json string 互转
    更新内置flash方法[转]
    CSS设置滚动条样式[转]
    ArcGIS JavaScript API本地部署离线开发环境[转]
    正则匹配整数和小数
    把数字字符串转换为日期
    获取滚动条高度的兼容问题
    angular实现多个div的展开和折叠
    IE浏览器overflow:srcoll兼容问题记录
    input验证,光标左右移动问题
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626767.html
Copyright © 2011-2022 走看看