E. Santa Claus and Tangerines 二分答案 + 记忆化搜索
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 二分答案 + 记忆化搜索的更多相关文章
- E. Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Santa Claus and Tangerines
Santa Claus and Tangerines 题目链接:http://codeforces.com/contest/752/problem/E 二分 显然直接求答案并不是很容易,于是我们将其转 ...
- 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 ...
- 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 ...
- 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】
Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...
- bzoj4562: [Haoi2016]食物链--记忆化搜索
这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...
- 数位dp/记忆化搜索
一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an ...
- 最长不下降子序列 O(nlogn) || 记忆化搜索
#include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...
随机推荐
- wireshark如何抓取别人电脑的数据包
抓取别人的数据包有几种办法,第一种是你和别人共同使用的那个交换机有镜像端口的功能,这样你就可以把交换机上任意一个人的数据端口做镜像,然后你在镜像端口上插根网线连到你的网卡上,你就可以抓取别人的数据了: ...
- Qt之QComboBox(基本应用、代理设置)(转)
QComboBox下拉列表比较常用,用户可以通过选择不同的选项来实现不同的操作,如何实现自己的下拉列表呢? 很多人在问QComboBox如何设置选项的高度.代理等一些问题!今天就在此分享一下自己的一些 ...
- 基于MVC4+EasyUI的Web开发框架形成之旅--基类控制器CRUD的操作
在上一篇随笔中,我对Web开发框架的总体界面进行了介绍,其中并提到了我的<Web开发框架>的控制器的设计关系,Web开发框架沿用了我的<Winform开发框架>的很多架构设计思 ...
- C#访问非托管内存
示例1:分配一个新的内存地址给新变量 Point p; // Initialize unmanged memory to hold the struct. IntPtr pnt = Marshal.A ...
- javascript 中的 true 或 false
JavaScript中奇葩的假值 通常在以下语句结构中需要判断真假 if分支语句 while循环语句 for里的第二个语句 如 1 2 3 4 5 6 7 if (boo) { // do somet ...
- tomcat配置及优化
jdk安装 su - root mkdir jdk cd jdk wget https://mirror.its.sfu.ca/mirror/CentOS-Third-Party/NSG/common ...
- [Spring] - Spring + Hibernate
Spring整合Hibernate,花了很长时间研究,其中碰到的比较多问题. 使用的是Spring3.0+Hibernate4.1.6,Spring整合最新版本的Hibernate4.5,会抛些奇奇怪 ...
- 12 Linux下crontab详解
1. 概述: crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进 ...
- WebApi:过滤器的种类
WebApi:筛选器的种类 授权筛选器:这些筛选器用于实现IAuthorizationFilter和做出关于是否执行操作方法(如执行身份验证或验证请求的属性)的安全决策.AuthorizeAttrib ...
- UITapGestureRecognizer响应顺序是怎么样的
一个scrollview上有几个按钮在scrollview上add 了一个单击事件 singletap = [[UITapGestureRecognizer alloc] initWithTarget ...