多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Codeforces Round #389 (Div. 2) E. Santa Claus and Tangerines 二分+貪心+記憶化搜索

Codeforces Round #389 (Div. 2) E. Santa Claus and Tangerines 二分+貪心+記憶化搜索

來源:程序員人生   發布時間:2017-02-24 11:08:35 閱讀次數:2971次

E. Santa Claus and Tangerines
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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?≤?1061?≤?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 . 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
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 5slices 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 7slices.

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




Source

Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3)


My Solution

題意:有n個橘子,每一個橘子可以分成ai瓣,但每次只能把 1個完全的橘子或由1些把構成的部份橘子 分成盡量相等的兩部份,即如果瓣數是偶數則當前只能分成相等的2部份,如果是奇數則分成2部份其中1部份比另外一部份多1,然后把這些得到的橘子或瓣分給k個小朋友,其中小朋友們得到瓣數中的最小值是答案,要求這個答案盡量大


2分+貪心+記憶化搜索

由于每一個小朋友只能得到1個橘子或1些由瓣構成的部份橘子,所答案最大為max{ai} 這里ans 屬于[1, 1e7],故在(0, 1e7 + 1)內]對答案進行2分(不會取到左右端點),

每次2分掃1遍ai數組,對每一個數在跑1個logai的遞歸求出這個ai可以弄出多少個瓣數大于等于u的有瓣構成的部份橘子。

這是算法是 O(nlognlogn) 顯示會超時的,故對求ai可以弄出多少個u時把普通的遞歸改成記憶化搜索,

然后還是TLE了,斟酌到優先對ai大的進行計算可以記錄盡量多的數據,故把ai排序,然后貪心,從大的開始掃,這樣可以減少大量的重復計算

通過記憶化搜索+貪心的優化, < 2000ms

此時復雜度大概略大于 O(nlogn) 遠小于 O(nlognlogn).


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL maxn = 1e6 + 8;

int a[maxn], n, k, dp[10*maxn];

inline int calc(int x, const int& u)
{
    if(x < u) return 0;
    if(dp[x] != 0) return dp[x];
    if(x / 2 < u){
        return dp[x] = 1;
    }
    else if(x & 1){
        dp[x] = calc(x / 2, u);
        return dp[x] += calc(x / 2 + 1, u);
    }
    else{
        return dp[x] = 2 * calc(x / 2, u);
    }
}

bool check(const int& u)
{
    LL s = 0;
    for(int i = n - 1; i >= 0; i--){  //排序以后從大的開始記憶化搜索,不然會超時
        s += calc(a[i], u);
    }
    //cout << u << " : " << s << " " << k << endl;
    if(s >= k) return true;
    else return false;
}

int main()
{
    #ifdef LOCAL
    freopen("e.txt", "r", stdin);
    //freopen("e.out", "w", stdout);
    LL T = 4;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    int ans = ⑴;
    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    sort(a, a + n);
    int l = 0, r = 1e7 + 1, mid;
    while(l + 1 < r){
        mid = (l + r) >> 1;
        memset(dp, 0, sizeof dp);
        if(check(mid)){
            ans = max(ans, mid);
            l = mid;
        }
        else r = mid;
    }

    cout << ans << endl;


    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



  Thank you!

                                                                                                                                               ------from ProLights

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 在线九色| 中国国产一级毛片 | 午夜a级片 | free性欧美xxx狂欢 | 91九色精品国产免费 | 日本337p| 香蕉伊 | 日本中文字幕在线视频 | 亚洲人成综合网站在线 | 日本动漫免费看 | 中文在线1区二区六区 | 亚洲男人的天堂久久无 | 亚洲精品在线观看视频 | 在线亚洲自拍 | 日本高清免费视频www | a毛片a毛片a视频 | 天堂mv亚洲mv在线播放9蜜 | 亚洲免费人成在线视频观看 | 精品一区二区三区自拍图片区 | 国产精品国产三级国产爱网 | 亚洲综合一区二区 | 特级做a爰片毛片免费看一区 | 中文字幕最新中文字幕中文字幕 | 国产日韩欧美久久久 | 91精品日韩| 国产l精品国产亚洲区久久 国产mv在线观看 | 91精品国产闺蜜国产在线闺蜜 | 欧美激情免费a视频 | 国产成人精品免费视频大 | 欧美色频 | 日本成本人在线观看免费视频 | 日本欧美一区二区免费视 | 欧美亚洲高清 | 欧美另类精品xxxx人妖换性 | 日韩 欧美 国产 亚洲 中文 | 中文字幕激情视频 | 用劲好爽再深点视频 | 无人精品乱码一区二区三区 | 国产精品久久久久久久久久免费 | 九九九精品午夜在线观看 | 黄伊人|