Codeforces 633D Fibonacci-ish 暴力
题意: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 暴力的更多相关文章
- 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 ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- Codeforces A. Playlist(暴力剪枝)
题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- [codeforces 200 A Cinema]暴力,优化
题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...
- Codeforces 193E - Fibonacci Number(打表找规律+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...
- Codeforces 633D
题意: 给定n,和一个长度为n的序列. 让你在这n个数中找长度尽可能长的fib数列. 思路: 这题的数字是在1e9范围内的,所以最长的可能存在的fib数列官方的解释是90左右.有一种情况除外,就是0的 ...
- Codeforces Gym 100531D Digits 暴力
Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...
- CodeForces 589B Layer Cake (暴力)
题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...
随机推荐
- MBProgressHUD ---
1,MBProgressHUD常用属性和用法Demo - (void)testMBProgressHUD { NSLog(@"test MBProgressHUD "); /* 要 ...
- (转)[C++语法] 关键字typedef用法
转自http://www.cnblogs.com/SweetDream/archive/2006/05/10/395921.html C/C++语言中的typedef相信大家已经不陌生,本文对C/C+ ...
- dictionary 和 hashtable 区别
区别:1,Dictionary支持泛型,而Hashtable不支持. 2,Dictionary没有装填因子(Load Facto)概念,当容量不够时才扩容(扩容跟Hashtable一样,也是两倍于当前 ...
- 1034: [ZJOI2008]泡泡堂BNB - BZOJ
Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵 ...
- Linux环境变量的设置和查看方法
Linux环境变量的设置和查看方法 1. 显示环境变量HOME [root@AY1404171530212980a0Z ~]# echo $HOME /root 2. ...
- log4j示例
示例代码:此示例从控制台输入日志,设置了输出等级. # # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The f ...
- PAT-乙级-1029. 旧键盘(20)
1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...
- 转Spring+Hibernate+EHcache配置(三)
配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cac ...
- oracle-number(5,2)
insert into emp values(70000.123); 只能存储 整数的前3位, 小数点后面的2位
- hdu 1851 A Simple Game 博弈论
简单博弈问题(巴什博弈-Bash Game) 巴什博弈:只有一堆n个物品,两个人轮流从这对物品中取物,规定每次至少取一个,最多取m个,最后取光着得胜. 很容易想到当n%(m+1)!=0时,先取者必胜, ...