http://codeforces.com/contest/752/problem/E

首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5.

14 = 7 + 7.但是按照平均分的话,它是不能产生5的,那就把那两个7当成是两个5,因为7比5还大,对min(b[i])是没有影响的。

可以思考下样例2.

那么二分答案mid,设dp[val][x]表示val这个数字能产生多少个x。dp[val][x] = dp[val / 2][x] + dp[(val + 1) / 2][x]

那么dp数组开不了那么大,可以考虑记忆化搜索。用另外一个数组vis[]标记是否已经搜索过即可。因为dp数组需要重复使用,已经有值了。vis[]可以用DFN简单代替memset

判断每个数字能拆成多少个x,复杂度是logn的,所以复杂度是nlognlogn

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
int a[maxn];
int n, k;
int dp[maxn * ];
int vis[maxn * ];
int DFN;
int func(int val, int x) {
if (vis[val] == DFN) return dp[val];
if (val < x) return ;
if (val / < x) { // 6也算可以生成5
vis[val] = DFN;
dp[val] = ;
return ;
}
if (val & ) {
int ans = func(val / , x) + func((val + ) / , x);
vis[val] = DFN;
dp[val] = ans;
return ans;
} else {
int ans = * func(val / , x);
vis[val] = DFN;
dp[val] = ans;
return ans;
}
}
bool check(LL val) {
int ans = ;
++DFN;
for (int i = ; i <= n; ++i) {
if (a[i] < val) break;
ans += func(a[i], val);
if (ans >= k) return true;
}
return false;
}
void work() {
scanf("%d%d", &n, &k);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
sort(a + , a + + n, greater<int>());
// for (int i = 1; i <= n; ++i) {
// cout << a[i] << " ";
// }
// cout << endl;
LL be = , en = 1e14L;
while (be <= en) {
LL mid = (be + en) >> ;
if (check(mid)) {
be = mid + ;
} else en = mid - ;
}
if (en == ) {
printf("-1\n");
} else printf("%I64d\n", en);
// cout << func(20, 5) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

E. Santa Claus and Tangerines 二分答案 + 记忆化搜索的更多相关文章

  1. E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  3. Santa Claus and Tangerines

    Santa Claus and Tangerines 题目链接:http://codeforces.com/contest/752/problem/E 二分 显然直接求答案并不是很容易,于是我们将其转 ...

  4. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索

    Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...

  6. 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】

    Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...

  7. bzoj4562: [Haoi2016]食物链--记忆化搜索

    这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...

  8. 数位dp/记忆化搜索

    一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an  ...

  9. 最长不下降子序列 O(nlogn) || 记忆化搜索

    #include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...

随机推荐

  1. PhpStorm (强大的PHP开发环境)2016.2.1 附注册方法

    最新版PhpStorm 2016正式版改进了PHP 7支持,改进代码完成功能. PhpStorm 是最好的PHP开发工具,使用它进行PHP开发将会让你感觉到编程的乐趣. 快乐无极终于从oschina看 ...

  2. 利用Jquery实现页面上div的拖动及位置保存

    <script src="js/jquery.js.js" type="text/javascript"></script> <s ...

  3. 安全快速修改Mysql数据库名的5种方法

    1. RENAME DATABASE db_name TO new_db_name这个..这个语法在mysql 5.1.7中被添加进来,到了5.1.23又去掉了.据说有可能丢失数据.还是不要用的好.详 ...

  4. 利用docker搭建rtmp服务器(1)

    以后的项目里面可能需要用到直播,所以就先看看 本来想在自己MAC上搭建nginx的,后来怕把自己的机子搞乱,刚好就学习了下docker,感觉docker强大就在于是一个操作系统软件的版本管理系统,可以 ...

  5. [经验交流] Apache Mesos Docker集群初探

    前言 因工作需要,我对基于Apache Mesos 的 Docker 集群作了一点研究,并搭建了一套环境,以下是资料分享. 1. Apache Mesos概述 Apache Mesos是一款开源群集管 ...

  6. SQLLite 学习笔记

    1.SQLLite 简介 2.命令行使用 3.常用GUI管理工具

  7. java语言实现堆排序

    package secondChapter; import java.util.Random; public class HeapSort { private static int AHeapSize ...

  8. angularjs jquery thinkPHP3.2.3 相结合小实例

    angular1.5 与 jquery想结合一个小应用 index.html <html> <head> <meta charset="utf-8"& ...

  9. 随便2--struct pointer

    同为struct,如果struct中没有指针, C 和C++可以用等号赋值,但是一旦里面涉及到指针,就不能用等号,要用memcpy struct A{char v1[20];int v2;} a,b; ...

  10. KT vs SKT [20160816]

    KT:索尔 SKT:茂凯,塔里克,卡西奥佩娅 普朗克+烬,大招开团. 塔里克保护,眩晕.