题意

 小明有一个不降序列(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)<(维护+枚举)\(找规律+递推+枚举)>的更多相关文章

  1. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  2. BZOJ1002:[FJOI2007]轮状病毒(找规律,递推)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子 和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下 ...

  3. HDU-2045 不容易系列之(3)—— LELE的RPG难题 找规律&递推

    题目链接:https://cn.vjudge.net/problem/HDU-2045 找规律 代码 #include <cstdio> long long num[51][2]; int ...

  4. 2018南京区域赛G题 Pyramid——找规律&&递推

    先手动推出前10项,再上BM板子求出递推式 $A_n = 5A_{n-1} - 10A_{n-2} + 10A_{n-3} - 5A_{n-4} + A_{n-5}$,根据特征根理论可求出特征方程 $ ...

  5. HDU-2050 折线分割平面 找规律&递推

    题目链接:https://cn.vjudge.net/problem/HDU-2050 题意 算了吧,中文题不解释了 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线 ...

  6. 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 ...

  7. POJ 2229 sumset ( 完全背包 || 规律递推DP )

    题意 : 给出一个数 n ,问如果使用 2 的幂的和来组成这个数 n 有多少种不同的方案? 分析 :  完全背包解法 将问题抽象==>有重量分别为 2^0.2^1.2^2…2^k 的物品且每种物 ...

  8. 【bzoj4318】【OSU!】期望dp——维护多个期望值递推

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62369739 Description osu 是 ...

  9. ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推)

    Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...

随机推荐

  1. hdu_4283_You Are the One(区间DP)

    题目链接:hdu_4283_You Are the One 题意: 有n个人,每个人有个屌丝值,如果果他是第K个上场,不开心指数就为(K-1)*D,然后有个小黑屋,可以调整他们的出场顺序,现在让你调整 ...

  2. DIV层漂浮居中

    <style type="text/css" title="currentStyle" media="screen" mce_bogu ...

  3. SQL函数学习(二):DATEADD() 函数

    DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的间隔数:对于 ...

  4. 生成SQL脚本的方法

    2点需要注意的关键: (1)选择特定数据库对象不包含用户选项: (2)要编写脚本的数据的类型选择"架构和数据".

  5. sql or 与and同时有时要注意

    如果是一天sql语句中有or和and同时在 正确:where xxx=xx and (xxx=xx or xxx=xx) 错误:where xxx=xx and xxx=xx or xxx=xx (这 ...

  6. 实现apk 调用framework java JNI中方法

    首先整个实现需要有Android源码编译环境.这里我用的是froyo2.2. 1.JNI层--C++代码部分 在目录frameworks/base/core/jni 下创建android_jnidem ...

  7. UVALive 7077 Little Zu Chongzhi's Triangles (有序序列和三角形的关系)

    这个题--我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了--结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小, ...

  8. Servie之前台Service

    public class MyService extends Service { public static final String TAG = "MyService"; pri ...

  9. HDU 2732 Leapin' Lizards

    网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...

  10. Entity Framework技巧系列之十三 - Tip 51 - 55

    提示51. 怎样由任意形式的流中加载EF元数据 在提示45中我展示了怎样在运行时生成一个连接字符串,这相当漂亮. 其问题在于它依赖于元数据文件(.csdl .ssdl .msl)存在于本地磁盘上. 但 ...