日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > Codeforces Round #389 (Div. 2) E. Santa Claus and Tangerines 二分+貪心+記憶化搜索

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

來源:程序員人生   發(fā)布時(shí)間:2017-02-24 11:08:35 閱讀次數(shù):2978次

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個(gè)橘子,每一個(gè)橘子可以分成ai瓣,但每次只能把 1個(gè)完全的橘子或由1些把構(gòu)成的部份橘子 分成盡量相等的兩部份,即如果瓣數(shù)是偶數(shù)則當(dāng)前只能分成相等的2部份,如果是奇數(shù)則分成2部份其中1部份比另外一部份多1,然后把這些得到的橘子或瓣分給k個(gè)小朋友,其中小朋友們得到瓣數(shù)中的最小值是答案,要求這個(gè)答案盡量大


2分+貪心+記憶化搜索

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

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

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

然后還是TLE了,斟酌到優(yōu)先對ai大的進(jìn)行計(jì)算可以記錄盡量多的數(shù)據(jù),故把a(bǔ)i排序,然后貪心,從大的開始掃,這樣可以減少大量的重復(fù)計(jì)算

通過記憶化搜索+貪心的優(yōu)化, < 2000ms

此時(shí)復(fù)雜度大概略大于 O(nlogn) 遠(yuǎn)小于 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--){  //排序以后從大的開始記憶化搜索,不然會(huì)超時(shí)
        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

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 一级性黄色片 | 99精品欧美一区二区蜜桃免费 | 成人高清视频免费观看 | 久久视频一区二区 | 精品中文字幕一区 | 中文字幕免费在线观看 | 亚洲国产成人精品久久久国产成人一区 | 精品国产乱码久久久久久久软件 | 亚洲成人精品久久久 | 中文字幕福利片 | 一级欧美黄色片 | 日本免费大全免费网站视频 | 欧美亚洲福利 | 欧美日韩电影 | 欧美日韩在线播放 | 成人久久久精品乱码一区二区三区 | 国产黄色大片 | a毛片免费 | 91精品国产91综合久久蜜臀 | 久久久亚洲欧洲 | 日本一区二区三区免费观看 | 天天综合网91 | 国产精品永久 | 亚洲精品乱码久久久久v最新版 | a级毛片免费高清视频 | 爱情岛论坛av | 国产成人精品一区 | 久久网亚洲 | 97国产在线视频 | 成人免费在线电影 | 一二三在线视频 | 69亚洲视频 | 精品亚洲一区二区三区 | 国产精品久久久久久久久久久久午夜 | 国产一级一区二区 | www.色婷婷 | 国产综合视频 | 欧美日韩精品一区二区三区四区 | av久久久 | 中文字幕偷拍 | 亚洲欧洲精品一区二区 |