标签:
直接枚举O(n^3)会TLE,只要稍微加点优化,在不可能得到更优解时及时退出。其实就是道水题,虽说我提交了6次才过= =..我还太弱了
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cctype> 6 7 #define rep(i,r) for(int i=0;i<r;i++) 8 #define clr(x,c) memset(x,c,sizeof(x)) 9 #define Rep(i,l,r) for(int i=l;i<=r;i++) 10 11 using namespace std; 12 13 const int maxn=5000; 14 15 int x[maxn]; 16 17 18 inline int read() { 19 20 char c=getchar(); 21 while(!isdigit(c)) c=getchar(); 22 23 int x=0; 24 while(isdigit(c)) { 25 x=x*10+c-‘0‘; 26 c=getchar(); 27 } 28 29 return x; 30 } 31 32 33 int main() 34 { 35 freopen("theme.in","r",stdin); 36 freopen("theme.out","w",stdout); 37 38 39 int n=read(); 40 rep(i,n) x[i]=read(); 41 int ans=0; 42 43 rep(i,n) if(i+4+max(5,ans)<n) 44 Rep(j,i+max(5,ans),n) if(j+max(5,ans)<n) { 45 int a=i,b=j,d=x[i]-x[j]; 46 while(x[a+1]-x[b+1]==d && b+1<n && a+1<j) { a++; b++; } 47 if(a-i+1>=5) ans=max(a-i+1,ans); 48 } 49 50 cout<<ans<<endl; 51 52 return 0; 53 }
A musical melody is represented as a sequence of N (1 <= N <= 5000) notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings.
Many composers structure their music around a repeating "theme", which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it:
Transposed means that a constant positive or negative value is 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!
The first line of the input file contains the integer N. Each subsequent line (except potentially the last) contains 20 integers representing the sequence of notes. The last line contains the remainder of the notes, potentially fewer than 20.
30 25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18 82 78 74 70 66 67 64 60 65 80
The output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.
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(枚举)
标签:
原文地址:http://www.cnblogs.com/JSZX11556/p/4338073.html