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. C++访问托管类(C#类库)

    1.新建C# 类库项目Airth,新建类 using System; using System.Collections.Generic; using System.Linq; using System ...

  2. VC_MFC水波纹控件,开源

    代码和效果图: https://github.com/wjx0912/MfcWaterEffect 集成以下5个文件即可: watereffect\DIB.hwatereffect\DIB.cppwa ...

  3. Docker SSH+NGHINX+MYSQL

    1.添加创建镜像的配置文件      vim Dockerfile      输入以下内容: FROM jdeathe/centos-sshMAINTAINER baxk"xxx@hongh ...

  4. Angular2.0-组件

    截止到目前为止,Angular2.0完成了其alpha-32版本的开发,新的版本还在迭代开发当中,这其中有个问题,就是每个版本相比于以前的版本都会有一些改动,包括API方面的修改,这会导致很多基于以前 ...

  5. System.Windows.Media.Imageing.BItmapImage 这么用才不会占用文件

    // Read byte[] from png file BinaryReader binReader = new BinaryReader(File.Open(filepath, FileMode. ...

  6. php返回json,xml,JSONP等格式的数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  7. python学习-day14:集合,函数,格式化

    一.集合 定义:由不同元素组成的集合.集合是一组无序排列的可hash值, 可以作为字典的key.元素必须是不可变类型:只能存放数字,字符串,字典 特性:集合的目的是将不同的值放在一起,不同的集合之间可 ...

  8. Windows下使用scapy+python2.7实现对pcap文件的读写操作

    scapy在linux环境中对pcap文件进行操作非常方便,但在windows下,特别是在python2.7环境下却会碰到各种各样的依赖包无法使用的问题,最明显的可能就属dnet和pcap的pytho ...

  9. bootstrap-导航条

    <body style="padding-top:50px"> <!-- navbar 导航条的基础样式 nav navbar-nav 导航条里菜单的固定样式组合 ...

  10. 为什么在注册和注销的时候intent要改成隐式调用

    显式意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件. 隐式意图: ...