Direct enumeration O (n^3) will be tle, as long as a little bit of optimization, when it is impossible to get a better solution in time to exit. Actually is the water problem, although I submitted 6 times before = =. I'm too weak.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <cctype>6 7 #defineRep (i,r) for (int i=0;i<r;i++)8 #defineCLR (x,c) memset (x,c,sizeof (x))9 #defineREP (I,L,R) for (int i=l;i<=r;i++)Ten One using namespacestd; A - Const intmaxn= the; - the intX[MAXN]; - - -InlineintRead () { + - CharC=GetChar (); + while(!isdigit (c)) c=GetChar (); A at intx=0; - while(IsDigit (c)) { -x=x*Ten+c-'0'; -C=GetChar (); - } - in returnx; - } to + - intMain () the { *Freopen ("theme.in","R", stdin); $Freopen ("Theme.out","W", stdout);Panax Notoginseng - the intn=read (); +Rep (I,n) x[i]=read (); A intans=0; the +Rep (I,n)if(i+4+max (5, ans) <N) -Rep (J,i+max (5, ans), N)if(J+max (5, ans) <N) { $ inta=i,b=j,d=x[i]-X[j]; $ while(x[a+1]-x[b+1]==d && B +1<n && A +1<J) {a++; b++; } - if(a-i+1>=5) Ans=max (a-i+1, ans); - } the -cout<<ans<<Endl;Wuyi the return 0; -}
View Code
Musical Themes
Brian Dean
A musical melody is represented as a sequence of n (1 <= n <=) Notes this is integers in the range 1..88, each Representing a key on the piano. It is unfortunate, but true, this representation of melodies ignores the notion of musical timing; But, the this programming task was about notes and not timings.
Many composers structure their music around a repeating "theme", which, being a subsequence of an entire melody, is a sequ ence of integers in our representation. A subsequence of a melody is a theme if it:
- is at least five notes long
- Appears (potentially transposed--see below) again somewhere else in the piece of music
- is disjoint from (i.e., non-overlapping with) at least one of their other appearance (s)
Transposed means that a constant positive or negative value are added to every note value in the theme subsequence.
Given a melody, compute the length (number of notes) of the longest theme.
One second time limit for this problem ' s solutions!
Program Name:themeinput FORMAT
The first line of the input file contains the integer N. Each subsequent line (except potentially, the last) contains integers representing the sequence of notes. The last line contains the remainder of the notes, potentially fewer than 20.
SAMPLE INPUT (file theme.in)
3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 80
OUTPUT FORMAT
The output file should contain a single line with a single integer that represents the length of the longest theme. If There is no themes, output 0.
SAMPLE OUTPUT (file theme.out)
5
[The Five-long theme is the last five notes of the first line and the first five notes of the second]
Usaco Section 5.1 musical Themes (enumeration)