Codeforces 633D fibonacci-ish Violence

Source: Internet
Author: User

Test instructions: 1000 elements, the size of each element -1e9<=a[i]<=1e9, and then lets you rearrange the position of these elements

Get the longest prefix Fibonacci sequence

Parse: Enumerates the first and second elements, because within the range of the topic element, the Fibonacci sequence of up to 90 is formed

This will reach a length of 1000 unless there is a full 0 situation.

So this is a special case (record the number of 0 elements on the line)

Then the enumeration is n^2.

Find the element is 90, and then look for when I use the map to find

So the complexity of time is slightly larger is O (90N^2LOGN)

So because it is not possible to find 90 each time, so the same is relatively small, the time limit is 3s, I ran 2012ms

Mostly I'm too weak (this is just the puzzle after finishing the game)

#include <cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<vector>#include<string>#include<algorithm>#include<map>using namespaceStd;typedefLong LongLL;Const intn=1000000+5; Map<ll,int>MP; LL a[1005];intMain () {intN; LL Res=0, mx=-1; scanf ("%d",&N);  for(intI=1; i<=n;++i) {scanf ("%i64d",&A[i]); MX=Max (A[I],MX); if(a[i]==0)++Res; Mp[a[i]]++; } LL ans=2;  for(intI=1; i<=n;++i) { for(intj=1; j<=n;++j) {if(j==i)Continue; if(a[i]==0&&a[j]==0)Continue; LL x=a[i],y=a[j],cnt=2; Vector<LL>T;           T.clear ();           T.push_back (x);           T.push_back (y); MP[X]--; Mp[y]--;  while(x+y<=mx&&mp[x+y]>0) {LL tmp=x+y; MP[TMP]--;              T.push_back (TMP); X=y; Y=tmp; ++CNT; }            for(intk=0; K<t.size (); k++) Mp[t[k]]++; Ans=Max (ans,cnt); }} printf ("%i64d\n", Max (ans,res)); return 0;}
View Code

Then I wrote a copy of the map, because the range of elements is large

So you can use sort hash, with lower_bound to find, so the complexity of the above and the complexity of the principle of the same as above

But because of the implementation of the array, is certainly faster than the STL, written in such a way to run 826ms

So you can use the STL, or not.

#include <cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<vector>#include<string>#include<algorithm>#include<map>using namespaceStd;typedefLong LongLL;Const intn=1000000+5;inta[1005],b[1005],sum[1005],pos[1005];intMain () {intres=0, mx=-1, n,l=0; scanf ("%d",&N);  for(intI=1; i<=n; ++i) {scanf ("%d",&A[i]); MX=Max (A[I],MX); if(a[i]==0)++Res; } sort (A+1, A +1+N); b[++l]=a[1];  for(intI=2; i<=n; ++i)if(a[i]!=a[i-1]) b[++l]=A[i];  for(intI=1; i<=n; ++i) {pos[i]=lower_bound (b +1, B +1+l,a[i])-b; ++Sum[pos[i]]; }    intans=2;  for(intI=1; i<=n; ++i) { for(intj=1; j<=n; ++j) {if(j==i)Continue; if(a[i]==0&&a[j]==0)Continue; intX=a[i],y=a[j],cnt=2; 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) {intZ=lower_bound (b +1, B +1+l,x+y)-b; if(z==l+1|| b[z]!=x+y| |! SUM[Z]) Break; SUM[Z]--;                T.push_back (z); inttmp=x+y; X=y; Y=tmp; ++CNT; }             for(intk=0; K<t.size (); k++) Sum[t[k]]++; Ans=Max (ans,cnt); }} printf ("%d\n", Max (ans,res)); return 0;}
View Code

Codeforces 633D fibonacci-ish Violence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.