F(k)<(维护+枚举)\(找规律+递推+枚举)>
题意
小明有一个不降序列(f(1),f(2),f(3),……),f(k)代表在这个序列中大小是k的有f(k)个。我们规定f(n)的前12项如下图。
n 1 2 3 4 5 6 7 8 9 10 11 12
f(n) 1 2 2 3 3 4 4 4 5 5 5 6
现在给你一个n,你知道f(n)是多少吗?
多组测试数据
每组一个n(1<=n<=2000,000,000)。
###法一:正常情况下想的到。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const long long maxn=20000000;
int f[maxn];
int main ()
{
int nn,i;
long long j;
f[1]=1;
f[2]=2;
f[3]=2;
j=3;
for(i=3;j<=maxn-3;i++)
{
nn=f[i];
while(nn--&&j<=maxn)
{
f[++j]=i;
}
}
int n;
while(~scanf("%d",&n))
{
int ans=0,i;
for(i=1;ans<n;i++)
{
ans+=f[i];
}
printf("%d\n",i-1);
}
return 0;
}
法二:正常情况下想不到
因为n的最大范围是20亿,显然不能数组保存,而且时间也不允许,也很难发现什么规律。我们可以换个角度,既然要找的是f[n]的值,那么我们把f[x]=i时的最大x记录为 d[i] = x;
d[1] = 1
d[2] = 3
d[3] = 5
d[4] = 8
d[5] = 11
仔细推敲不难发现规律
从3起,d[i] = d[i-1] + find(i); find(i) = min(k) 当d[k]>=i时
find(i)也就是d数组中大于等于i的一项的最小值的下标。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=680000;
LL fuck[maxn];
int i;
int Find(int l,int r,int key)
{
int mid;
while(l<r)
{
mid=(l+r)/2;
if(fuck[mid]<key)
l=mid+1;
else
r=mid;
}
return l;
}
void init()
{
fuck[1]=1;fuck[2]=3;
for(i=3;i<=673365;i++){
fuck[i]=fuck[i-1]+Find(1,i-1,i);
}
}
int main ()
{
int n;init();
while(~scanf("%d",&n))
printf("%d\n",Find(1,i,n));
return 0;
}
STL的魅力
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=680000;
LL fuck[maxn];
int i;
void init()
{
fuck[1]=1;fuck[2]=3;
for(i=3;i<=673365;i++){
fuck[i]=fuck[i-1]+(lower_bound(fuck+1, fuck+i-1,i)-fuck);
}
}
int main ()
{
int n;init();
while(~scanf("%d",&n))
printf("%ld\n",(lower_bound(fuck+1, fuck+i,n)-fuck));
return 0;
}
F(k)<(维护+枚举)\(找规律+递推+枚举)>的更多相关文章
- codeforces D. Queue 找规律+递推
题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...
- BZOJ1002:[FJOI2007]轮状病毒(找规律,递推)
Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子 和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下 ...
- HDU-2045 不容易系列之(3)—— LELE的RPG难题 找规律&递推
题目链接:https://cn.vjudge.net/problem/HDU-2045 找规律 代码 #include <cstdio> long long num[51][2]; int ...
- 2018南京区域赛G题 Pyramid——找规律&&递推
先手动推出前10项,再上BM板子求出递推式 $A_n = 5A_{n-1} - 10A_{n-2} + 10A_{n-3} - 5A_{n-4} + A_{n-5}$,根据特征根理论可求出特征方程 $ ...
- HDU-2050 折线分割平面 找规律&递推
题目链接:https://cn.vjudge.net/problem/HDU-2050 题意 算了吧,中文题不解释了 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线 ...
- ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 【规律递推】
任意门:https://nanti.jisuanke.com/t/31453 A.Hard to prepare After Incident, a feast is usually held in ...
- POJ 2229 sumset ( 完全背包 || 规律递推DP )
题意 : 给出一个数 n ,问如果使用 2 的幂的和来组成这个数 n 有多少种不同的方案? 分析 : 完全背包解法 将问题抽象==>有重量分别为 2^0.2^1.2^2…2^k 的物品且每种物 ...
- 【bzoj4318】【OSU!】期望dp——维护多个期望值递推
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62369739 Description osu 是 ...
- ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推)
Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...
随机推荐
- brctl 的使用
brctl 作用: 用来进行以太网桥接(bridge)的管理 主要用法如下: root@hbg:/# brctl --helpBusyBox v1.22.1 (2016-02-24 11:41:04 ...
- 小心DLL链接静态库时的内存错误
本文转自http://www.bennychen.cn/2010/09/%E5%B0%8F%E5%BF%83dll%E9%93%BE%E6%8E%A5%E9%9D%99%E6%80%81%E5%BA% ...
- linux ssh连接不自动断开
修改linux服务器ssh配置文件: vim /etc/ssh/ssh_config 修改两处的值为: ClientAliveInterval ClientAliveCountMax 使修改的ssh配 ...
- 2015 Multi-University Training Contest 3
1001 Magician 线段树.根据奇偶性分成4个区间.维护子列和最大值. 想法很简单.但是并不好写. 首先初始化的时候对于不存在的点要写成-INF. 然后pushup的时候.对于每个区间要考虑四 ...
- js中的随机数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ubuntu iptalbes 保存配置
ubuntu 保存防火墙命令,iptables方式:1.iptables 配置好策略2.iptables-save > /etc/network/iptables.up.rules ,配置的策 ...
- CentOS安装配置Tomcat7
1.下载apache-tomcat-7.0.62.tar.gz 2.解压:tar -zxvf apache-tomcat-7.0.62.tar.gz 3.配置环境变量: 进入安装目录:(/usr/lo ...
- (译)UEFI 启动:实际工作原理
本文是我翻译自国外技术博客的一篇文章,其中讲述了 UEFI 的一些基本概念和细节. 本文的原始链接位于: https://www.happyassassin.net/2014/01/25/uefi-b ...
- 学习笔记——迭代器模式Iterator
迭代器模式,使用很多,但是很少实现.常用的集合都支持迭代器. 集合中的CreateIterator()可用于创建自己的迭代器,在里面通过调用迭代器的构造函数Iterator(Aggregate)来绑定 ...
- URL scheme添加以及查找方式
2.1.1 添加URL Types URL Scheme是通过系统找到并跳转对应app的一类设置,通过向项目中的info.plist文件中加入URL types可使用第三方平台所注册的appkey信 ...