【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)
題干:
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if
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.
Input
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).
Output
Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.
Examples
Input
3 1 2 -1Output
3Input
5 28 35 7 14 21Output
4Note
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.
題目大意:
? ? ?定義,fib數(shù)列:f[0]、f[1]任意,對(duì)于n > 1,f[n] = f[n-1] + f[n-2]。給定n個(gè)元素,構(gòu)造最長(zhǎng)的fib數(shù)列,輸出長(zhǎng)度。
解題報(bào)告:
? ? ?由于斐波那契數(shù)列數(shù)值成指數(shù)型增長(zhǎng)(稱為斐波那契數(shù)列的收斂性),所以實(shí)際可選的長(zhǎng)度不會(huì)超過(guò)100(實(shí)際上增長(zhǎng)速度僅僅比2的冪次方要慢一點(diǎn)點(diǎn)。若是正整數(shù)的斐波那契數(shù)列,要從0 1增長(zhǎng)到1e9只需40位左右,所以題中樣例給出的長(zhǎng)度最大也是接近90的)。那么只需離散出全部的不同的數(shù)值及其個(gè)數(shù),枚舉前兩位即可。有個(gè)特例,如果前兩位為0,這時(shí)可以造一個(gè)樣例長(zhǎng)度可能達(dá)到1000,但只會(huì)出現(xiàn)一次。。。所以無(wú)傷大雅。(好像cf里沒(méi)有這個(gè)樣例)
? ? 時(shí)間復(fù)雜度o(100 * n^2logn)左右。
TLE3代碼:
#include<bits/stdc++.h> #define ll long long using namespace std; ll a[2005]; int n; ll max(ll x,ll y) {if(x > y) return x;return y; } ll solve(int p1,int p2) {ll res = 2,tmp = 0;int pos = 1;ll r[3] = {a[p1],a[p2]};while(1) {if(binary_search(a+1,a+n+1,r[0] + r[1]) == 0) break;res++;r[2] = r[0]+r[1];r[0]=r[1]; r[1]=r[2];} return res; } int main() {ll maxx = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);sort(a+1,a+n+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(j == i) continue;maxx = max(maxx,solve(i,j));}}printf("%lld\n",maxx);return 0 ;}究其原因,,貌似是因?yàn)闆](méi)有離散化?但是貌似離散化也離散不了哪里去吧,也就是可以下標(biāo)存下這個(gè)元素了,但是查找的時(shí)候還是log的復(fù)雜度啊。。
?
AC代碼:(700ms左右)
離散化后用map存出現(xiàn)次數(shù),但是其實(shí)因?yàn)槎茧x散化了,就不需要map了啊(因?yàn)橄聵?biāo)已經(jīng)是<1000的了),所以直接二分找位置然后開(kāi)num數(shù)組存離散化后值出現(xiàn)的次數(shù)就好了呀。
#include<bits/stdc++.h> #define ll long long using namespace std; ll a[2005]; int n,len; map<ll,ll> mp; ll max(ll x,ll y) {if(x > y) return x;return y; } ll solve(int p1,int p2) {ll res = 2,tmp = 0;int pos = 1;ll r[3] = {a[p1],a[p2]};mp[r[0]]--;mp[r[1]]--;while(1) {if(mp[r[0]+r[1]] == 0) break;res++;r[2] = r[0]+r[1];r[0]=r[1]; r[1]=r[2];mp[r[1]]--;//mp[r[0]]--;} mp[r[1]]++;mp[r[0]]++;while(1) {if(r[0] == a[p1] && r[1] == a[p2]) break;r[2] = r[1]-r[0];r[1] = r[0];r[0] = r[2];mp[r[0]]++;}return res; }int main() {ll maxx = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),mp[a[i]]++;sort(a+1,a+n+1);len = unique(a+1,a+n+1) - a - 1;for(int i = 1; i<=len; i++) {for(int j = 1; j<=len; j++) {if(j == i && mp[a[i]] <= 1) continue;maxx = max(maxx,solve(i,j));}}printf("%lld\n",maxx);return 0 ;}另:這題solve函數(shù)中完全沒(méi)有必要兩邊while,其實(shí)一遍就可以,順便記錄下用到了哪些值,最后直接遍歷這些值然后num[]++不就好了。
AC代碼2:(網(wǎng)上大部分都是給的遞歸的形式,但是有什么卵用呢、、)
#include<bits/stdc++.h> #define ll long long using namespace std; ll a[2005]; int n,len; map<ll,ll> mp; ll max(ll x,ll y) {if(x > y) return x;return y; } int f(ll a,ll b) {ll ans=0;if(mp[a+b]) {mp[a+b]--;ans=f(b,a+b)+1;mp[a+b]++;}return ans; }int main() {ll maxx = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),mp[a[i]]++;sort(a+1,a+n+1);len = unique(a+1,a+n+1) - a - 1;for(int i = 1; i<=len; i++) {for(int j = 1; j<=len; j++) {if(j == i && mp[a[i]] <= 1) continue;mp[a[i]]--;mp[a[j]]--;maxx = max(maxx,f(a[i],a[j]));mp[a[i]]++;mp[a[j]]++;}}printf("%lld\n",maxx+2);return 0 ;}?
附一個(gè)網(wǎng)絡(luò)AC代碼沒(méi)用map的:
#include<iostream> #include<cstdio> #include<cstring> #include<string.h> #include<algorithm> #include<vector> #include<cmath> #include<stdlib.h> #include<time.h> #include<stack> #include<set> #include<map> #include<queue> using namespace std; #define rep0(i,l,r) for(int i = (l);i < (r);i++) #define rep1(i,l,r) for(int i = (l);i <= (r);i++) #define rep_0(i,r,l) for(int i = (r);i > (l);i--) #define rep_1(i,r,l) for(int i = (r);i >= (l);i--) #define MS0(a) memset(a,0,sizeof(a)) #define MS1(a) memset(a,-1,sizeof(a)) #define MSi(a) memset(a,0x3f,sizeof(a)) #define inf 0x3f3f3f3f #define lson l, m, rt << 1 #define rson m+1, r, rt << 1|1 typedef __int64 ll; void out(T a) {if(a>9) out(a/10);putchar(a%10+'0'); } int v[1010],num[1010],stk[1010];//stk長(zhǎng)度也要為1000,因?yàn)榍懊鎯蓚€(gè)為0時(shí),len可能為最大值 int main() {int n;read1(n);rep0(i,0,n) read1(v[i]);sort(v,v + n);int ans = 0,cnt = 0;rep0(i,0,n){int t = i;while(i < n - 1 && v[i] == v[i + 1]) i++;num[cnt] = i - t + 1;v[cnt++] = v[i];//壓縮}v[cnt] = 2e9;rep0(i,0,cnt){rep0(j,0,cnt){if(i != j || num[j] > 1){stk[1] = i;stk[2] = j;int len = 2,val = v[i] + v[j];num[i]--,num[j]--;int down = lower_bound(v,v+cnt,val) - v;while(v[down] == val && num[down]){num[down]--;val -= v[stk[len-1]];val += v[down];stk[++len] = down;down = lower_bound(v,v+cnt,val) - v;}ans = max(ans,len);rep1(i,1,len) num[stk[i]]++;}}}out(ans);return 0; }?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最受欢迎信用卡排名 看看2017最受欢迎
- 下一篇: 人民币连续7个月升值,创两年多新高,我们