题意: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. 格式化说明符定义、转义字符、枚举、结构体、typedef

    1.格式化说明符定义: %i,%d:输出十进制整型数 %6d:输出十进制整型数,至少6个字符宽 %li,%ld:输出长整数 %u:输出无符号整数 %lu:输出无符号长整数(相当于:unsigned l ...

  2. MS SQL Server时间常用函数

    SQLServer时间日期函数详解,SQLServer,时间日期, 1.      当前系统日期.时间 select getdate() 2. dateadd      在向指定日期加上一段时间的基础 ...

  3. 实时数据处理环境搭建flume+kafka+storm:3.kafka安装

    1.  解压  tar -zxvf   2.配置/app/kafka_2.9.2-0.8.1.1/config/server.properties     #标识--     broker.id=0 ...

  4. 18:字符串-char型字符串

    1 什么是字符串? 字符串是以空字符(\)结尾的字符数组.空字符的assii码为:0, 空格的ascii码为322 \0的作用'\0'是一个空字符标志,它的ASSII码为0,C++有好多处理字符串的函 ...

  5. Searching in a rotated and sorted array

    Given a sorted array that has been rotated serveral times. Write code to find an element in this arr ...

  6. A geometric interpretation of the covariance matrix

    A geometric interpretation of the covariance matrix Contents [hide] 1 Introduction 2 Eigendecomposit ...

  7. PAT-乙级-1035. 插入与归并(25)

    1035. 插入与归并(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 根据维基百科的定义: 插入排序是迭 ...

  8. hdu 4657 Find Permutation

    思路:用一个数组index[]存放a的下标,初始化令a[i]=c[i]=index[i]=i; 假设当前处理的i,初始时令cur=i:j为大于i的任意值.每次操作找a[l]=c[cur]-b[cur] ...

  9. linux 配置java环境变量

    修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题. ·用文本编辑器打开/etc/profi ...

  10. navicat 导入sql文件乱码问题解决

    一,右键数据库链接,点击链接属性,修改以下信息,如图: 选择高级选项页签==>去掉使用MySQL字符集复选框==>选择GB2312字符编码==>点击确定 三,控制台后,show va ...