codeforces 633D D. Fibonacci-ish(dfs+暴力+map)
3 seconds
512 megabytes
standard input
standard output
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if
- the sequence consists of at least two elements
- f0 and f1 are arbitrary
- fn + 2 = fn + 1 + fn for all n ≥ 0.
You are given some sequence of integers a1, a2, ..., an. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence.
The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence ai.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 109).
Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.
- 3
1 2 -1
- 3
- 5
28 35 7 14 21
- 4
In the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence ai would be Fibonacci-ish.
In the second sample, the optimal way to rearrange elements is ,
,
,
, 28.
题意:给一个数组,问能组成斐波拉契数列的最大长度是多少;
思路:用map记录这个数是否出现以及出现了几次,用过一次-1,dfs寻找最大长度,注意开始的两个都为0的情况,否则会超时,还有我把b开成全局变量一直wa,wa到哭啊啊啊,最后改成局部变量就过了;
AC代码:
- #include <bits/stdc++.h>
- using namespace std;
- long long a[1005];
- int n,vis[1005];
- map<long long,int>mp;
- int ans=2;
- int dfs(long long x,long long y,int num)
- {
- long long b=x+y;
- if(mp[b]){
- mp[b]--;
- dfs(y,b,num+1);
- mp[b]++;
- return 0;
- }
- ans=max(ans,num);
- return 0;
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- scanf("%I64d",&a[i]);
- mp[a[i]]++;
- }
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- {
- if(i!=j)
- {
- if(a[i]==0&&a[j]==0)
- {
- ans=max(ans,mp[0]);
- }
- else {
- mp[a[i]]--;
- mp[a[j]]--;
- dfs(a[i],a[j],2);
- mp[a[j]]++;
- mp[a[i]]++;
- }
- }
- }
- }
- cout<<ans<<"\n";
- return 0;
- }
codeforces 633D D. Fibonacci-ish(dfs+暴力+map)的更多相关文章
- ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
FZU 2107 Hua Rong Dao Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- hdu 1010 Tempter of the Bone(dfs暴力)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- NOIP 2002提高组 选数 dfs/暴力
1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…, ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- Codeforces 600 E. Lomsat gelral (dfs启发式合并map)
题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...
- Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力
B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
随机推荐
- mysql 与mongodb的特点与优劣
首先我们来分析下mysql 与mongodb的特点与优劣.下面是我以前做的ppt的部分截图. 再来分析下应用场景,a.如果需要将mongodb作为后端db来代替mysql使用,即这里mysql与mon ...
- 删除路径下的文件以及子路径。(范例“E:/www/”)
/// <summary> /// 删除路径下的文件以及子路径. /// </summary> /// <param name="dir">目标 ...
- 卸载系统自带libevent
rpm -qa|grep libevent yum remove libevent* 或 rpm -e --nodeps --allmatches libevent*
- mxnet编译问题手记
MXNet在64位Win7下的编译安装:https://www.cnblogs.com/noahzn/p/5506086.html http://blog.csdn.net/Jarvis_wxy/ar ...
- vue2 本地安装
- 在Linux中显示日历(cal)
cal 2013 显示2013年整年日历 cal 7 2013 显示2013年 7 月 日历
- MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现
原文链接:http://hi.baidu.com/wangzhiqing999/item/7ca215d8ec9823ee785daa2b MySQL 下 ROW_NUMBER / DENSE_RAN ...
- Android代码绘制虚线、圆角、渐变效果图
drawable文件夹放置动画/形状/选择器等属性文件,唯一的drawable文件名,不允许写错和拼错,否则运行报错.drawable文件夹底下的xml文件可以包括的标签共18个:animation- ...
- PHP网站在Linux服务器上面的安全配置
本文详细总结了PHP网站在Linux服务器上面的安全配置,包含PHP安全.mysql数据库安全.web服务器安全.木马查杀和防范等,很好很强大很安全. PHP安全配置 1. 确保运行php的用户为一般 ...
- 设置某个字段为null
update 表名 set 字段名=null WHERE 条件如下例子 update dwtz set ggid=null WHERE name_of_invested_company='浙江海翔药业 ...