Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)

Posted AC_Arthur

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

思路:

我们二分答案, 那么这就变成了一个二分求下界的问题了。   关于判定我采用了一种记忆化搜索的递归方式, 简单证明了一下应该可以达到log的复杂度。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e6 + 10;
const int maxv = 1e7 + 10;
int n, a[maxn], d[maxv], vis[maxv], kase = 0;
ll k;
int dp(int cur, int mid) 
    if(vis[cur] == kase) return d[cur];
    vis[cur] = kase;
    if(cur&1) 
        if(cur/2 < mid) return d[cur] = 1;
        else return d[cur] = dp(cur/2, mid) + dp(cur/2+1, mid);
    
    else 
        if(cur/2 < mid) return d[cur] = 1;
        else return d[cur] = 2*dp(cur/2, mid);
    

bool ok(int mid) 
    ll cnt = 0;
    ++kase;
    for(int i = 1; i <= n; i++) 
        if(a[i] < mid) continue;
        cnt += dp(a[i], mid);
    
    return cnt >= k;

int main() 
    scanf("%d%I64d", &n, &k);
    for(int i = 1; i <= n; i++) 
        scanf("%d", &a[i]);
    
    sort(a+1, a+n+1);
    int l = 1, r = a[n], ans = -1;
    while(r >= l) 
        int mid = (l + r) >> 1;
        if(ok(mid)) ans = max(ans, mid), l = mid+1;
        else r = mid-1;
    
    printf("%d\\n", ans);
    return 0;



以上是关于Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #389 (Div. 2) 752F(树的权值重心)

Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)

Codeforces Global Round 19

Codeforces Educational Codeforces Round 67

Codeforces Round 1132Educational Round 61

codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Techn