题意:1000个元素,每个元素的大小-1e9<=a[i]<=1e9,然后让你重新安排这些元素的位置

获得最长的前缀斐波那契数列

分析:枚举第一个元素和第二个元素,因为在题目元素的范围内,最多形成长度为90的斐波那契数列

除非有全0的情况出现,这种情况会达到长度1000

所以这种情况特判一下(记录一下零元素的个数就行了)

然后枚举是n^2的

找元素是90,然后找的时候,我用的map来找

所以时间复杂度是略大是O(90n^2logn)

所以由于不可能每次都找到90,所以均摊比较小,这题时限是3s,我跑了2012ms

主要是我太弱(这是刚比完赛看题解写的)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int N=+;
map<LL,int>mp;
LL a[];
int main()
{
int n;
LL res=,mx=-;
scanf("%d",&n);
for(int i=;i<=n;++i)
{
scanf("%I64d",&a[i]);
mx=max(a[i],mx);
if(a[i]==)++res;
mp[a[i]]++;
}
LL ans=;
for(int i=;i<=n;++i)
{
for(int j=;j<=n;++j)
{
if(j==i)continue;
if(a[i]==&&a[j]==)continue;
LL x=a[i],y=a[j],cnt=;
vector<LL>t;
t.clear();
t.push_back(x);
t.push_back(y);
mp[x]--;
mp[y]--;
while(x+y<=mx&&mp[x+y]>)
{
LL tmp=x+y;
mp[tmp]--;
t.push_back(tmp);
x=y;
y=tmp;
++cnt;
}
for(int k=;k<t.size();k++)
mp[t[k]]++;
ans=max(ans,cnt);
}
}
printf("%I64d\n",max(ans,res));
return ;
}

然后后来我又写了一份,之所以用MAP找,是因为元素范围大

所以可以用排序hash,用lower_bound来找,这样复杂度其实和上面的复杂度原理上和上面一样

但是由于用数组实现,所肯定比STL要快,写成这样才跑了826ms

所以说能不用STL,还是不用吧

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int N=+;
int a[],b[],sum[],pos[];
int main()
{
int res=,mx=-,n,l=;
scanf("%d",&n);
for(int i=; i<=n; ++i)
{
scanf("%d",&a[i]);
mx=max(a[i],mx);
if(a[i]==)++res;
}
sort(a+,a++n);
b[++l]=a[];
for(int i=; i<=n; ++i)
if(a[i]!=a[i-])b[++l]=a[i];
for(int i=; i<=n; ++i)
{
pos[i]=lower_bound(b+,b++l,a[i])-b;
++sum[pos[i]];
}
int ans=;
for(int i=; i<=n; ++i)
{
for(int j=; j<=n; ++j)
{
if(j==i)continue;
if(a[i]==&&a[j]==)continue;
int x=a[i],y=a[j],cnt=;
vector<int>t;
t.clear();
sum[pos[i]]--;
sum[pos[j]]--;
t.push_back(pos[i]);
t.push_back(pos[j]);
while(x+y<=mx)
{
int z=lower_bound(b+,b++l,x+y)-b;
if(z==l+||b[z]!=x+y||!sum[z])break;
sum[z]--;
t.push_back(z);
int tmp=x+y;
x=y;
y=tmp;
++cnt;
}
for(int k=; k<t.size(); k++)
sum[t[k]]++;
ans=max(ans,cnt);
}
}
printf("%d\n",max(ans,res));
return ;
}

Codeforces 633D Fibonacci-ish 暴力的更多相关文章

  1. codeforces 633D D. Fibonacci-ish(dfs+暴力+map)

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  2. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

  3. codeforces 897A Scarborough Fair 暴力签到

    codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...

  4. Codeforces A. Playlist(暴力剪枝)

    题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. [codeforces 200 A Cinema]暴力,优化

    题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...

  6. Codeforces 193E - Fibonacci Number(打表找规律+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...

  7. Codeforces 633D

    题意: 给定n,和一个长度为n的序列. 让你在这n个数中找长度尽可能长的fib数列. 思路: 这题的数字是在1e9范围内的,所以最长的可能存在的fib数列官方的解释是90左右.有一种情况除外,就是0的 ...

  8. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  9. CodeForces 589B Layer Cake (暴力)

    题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...

随机推荐

  1. Python之print语句

    print语句可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' 注意: 1.当我们在Python交 ...

  2. Xcode常用快捷键及代码格式刷(缩进)方法-b

    Xcode版本:4.5.1 一.总结的常用命令: 隐藏xcode command+h 退出xcode command+q 关闭窗口 command+w 关闭所有窗口 command+option+w ...

  3. 微软Hololens学院教程-Hologram 230-空间场景建模(Spatial mapping )【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  4. HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)

    Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...

  5. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件

    路径说明: 一.加载类目录下的配置文件 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:ap ...

  6. win8 优化笔记

    win8可以关掉的服务: 以下是小编搜集的可以安全更改为手动启动的服务(按名称排序): Application Experience(启动时为程序处理应用程序兼容性缓存请求) ·Computer Br ...

  7. firefly 框架 结构图

    原地址:http://www.9miao.com/question-15-54838.html 系统结构:

  8. APP 上传之后出现"invalid binary" 问题解决汇总

    背景 5.1 号开始 App 审核开始强制支持 iPhone5,并禁止使用 UDID. 问题 上传 app 后一直处于 Invalid Binary 状态,并且收到一封邮件说 Non-public A ...

  9. C++: 单例模式和缺陷

    C++: 单例模式和缺陷 实现一个单例模式 1 class Singleton { 2     private: 3         Singleton() { cout << " ...

  10. HDU1180+BFS

    bfs思路:三维标记状态 && 处理好 - | 和时刻的关系即可 /* bfs 思路:三维标记状态 && 处理好 - | 和时刻的关系即可 */ #include< ...