Coderforces 633D:Fibonacci-ish(map+暴力枚举)
http://codeforces.com/problemset/problem/633/D
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.
题意:给出 n个数,求出他们任意排列之后最长的斐波那契数列是多长。
思路:这题也是听了师兄的讲解之后才会做的。因为斐波那契数的递增是巨大的,1e9里面的斐波那契数是比较少的,
因此暴力枚举,比较巧妙的是用map来存一个数有没有出现过,并且如果是很多个零的话要加一个特判,不然的话可能会超时。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define N 1010 map <int, int> mp;
int a[N];
int temp[N]; int main()
{
int n ;
cin >> n;
mp.clear();
for(int i = ; i < n; i++){
scanf("%d", a + i);
mp[a[i]]++;
//该数出现的次数
}
int ans = , now = , cnt, x, y, z;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i == j) continue;
x = a[i], y = a[j];
if( x == && y == ){
//两个都是0的话,直接判断0的个数,因为0+0=0
if(mp[] > ans) ans = mp[];
continue;
}
cnt = ;
mp[x]--; mp[y]--;
temp[cnt++] = x;
temp[cnt++] = y;
//先把要加的数从map里面删除,避免重复,用temp存放,之后再放回去
while(mp[x+y] > ){
z = x + y;
mp[z]--;
temp[cnt++] = z;
x = y;
y = z;
}
for(int i = ; i < cnt; i++)
mp[temp[i]]++;
if(cnt > ans) ans = cnt;
}
}
printf("%d\n",ans);
return ;
}
Coderforces 633D:Fibonacci-ish(map+暴力枚举)的更多相关文章
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- POJ-3187 Backward Digit Sums (暴力枚举)
http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- HDU - 1248 寒冰王座 数学or暴力枚举
思路: 1.暴力枚举每种面值的张数,将可以花光的钱记录下来.每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的.时间复杂度O(n) 2.350 = 200 + 150,买350的 ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- こだわり者いろはちゃん / Iroha's Obsession (暴力枚举)
题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_a Time limit : 2sec / Memory limit : 256MB Score ...
- Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举
B. Covered Path Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...
- Codeforces Round #266 (Div. 2)B(暴力枚举)
很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找 ...
随机推荐
- sql server 查询存储过程指令
Sp_helptext PROCEDUREName 例子 创建存储过程 CREATE PROCEDURE SelectFromBitTable AS BEGIN select pkid,isdelet ...
- WPF 使用Propereties:Resources.resx里面的资源
<Window x:Class="Wpf180706.Window7" xmlns="http://schemas.microsoft.com/win ...
- WPF绑定到linq表达式
using ClassLibrary;using System;using System.Collections.Generic;using System.Collections.ObjectMode ...
- MVC 创建强类型视图
•在ViewModel中创建一个类型 •在Action中为ViewData.Model赋值 •在View中使用"@model类型"设置 14 手动创建强类型视图 •在ViewMod ...
- 为什么腾讯总能做出好产品?(在互联网行业,往往仅凭一个关键产品就足以改变整个公司的格局)MSN失败在不以用户体验为中心
投递人 itwriter 发布于 2017-07-10 11:16 评论(36) 有3401人阅读 原文链接 [收藏] « » 本文来自微信公众号“郑志昊 Peter”,作者李翔.郑志昊:博客园经授权 ...
- 在 Excel 中如何使用宏示例删除列表中的重复项
概要:在 Microsoft Excel 中,可以创建宏来删除列表中的重复项.也可以创建宏来比较两个列表,并删除第二个列表中那些也出现在第一个(主)列表中的项目.如果您想将两个列表合并在一起,或者如果 ...
- discuz电脑访问手机版域名怎么跳转到电脑版本
用discuz论坛访问手机版本的域名不会自动跳转到电脑版本,而是会跳转到域名+misc.php?mod=mobile体验很不好.现提供修改方法:打开论坛根目录找到文件./source/class/di ...
- .NET 图片像素比较
/// <summary> /// 图像比较.用于找出两副图片之间的差异位置 /// </summary> public class ImageComparer { /// & ...
- 【全面解禁!真正的Expression Blend实战开发技巧】十一章 全面解析布局(Grid & Canvas &StackPanel &Wrappanel)
原文:[全面解禁!真正的Expression Blend实战开发技巧]十一章 全面解析布局(Grid & Canvas &StackPanel &Wrappanel) 写这篇文 ...
- WPF应用程序如何重启当前的Application
// Restart current process Method 1 System.Windows.Forms.Application.Restart(); Application.Current. ...