直接枚举O(n^3)会TLE,只要稍微加点优化,在不可能得到更优解时及时退出。其实就是道水题,虽说我提交了6次才过= =..我还太弱了

--------------------------------------------------------------------------

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cctype>
#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
const int maxn=5000;
int x[maxn];
inline int read() {
    
    char c=getchar();
    while(!isdigit(c)) c=getchar();
    
    int x=0;
    while(isdigit(c)) {
        x=x*10+c-'0';
        c=getchar();
    }
    
    return x;
}
int main()
{
    freopen("theme.in","r",stdin);
    freopen("theme.out","w",stdout);
    
    
    int n=read();
    rep(i,n) x[i]=read();
    int ans=0;
    
    rep(i,n) if(i+4+max(5,ans)<n)
        Rep(j,i+max(5,ans),n) if(j+max(5,ans)<n) {
            int a=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);
        }
    
    cout<<ans<<endl;
    
    return 0;
}

--------------------------------------------------------------------------

Musical Themes
Brian Dean

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:

  • 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 its other appearance(s)

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!

PROGRAM NAME: theme

INPUT FORMAT

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.

SAMPLE INPUT (file theme.in)

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

OUTPUT FORMAT

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.

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(枚举)的更多相关文章

  1. USACO 5.1 Musical Themes(哈希+二分)

    Musical ThemesBrian Dean A musical melody is represented as a sequence of N (1 <= N <= 5000) n ...

  2. P2743(poj1743) Musical Themes[差分+后缀数组]

    P2743 乐曲主题Musical Themes(poj1743) 然后呢这题思路其实还是蛮简单的,只是细节特别多比较恶心,忘记了差分带来的若干疏漏.因为转调的话要保证找到相同主题,只要保证一段内相对 ...

  3. USACO Section 4.3 Street Race(图的连通性+枚举)

    虽说是IOI'95,但是也是挺水的..for 第一问,n最大为50,所以可以直接枚举起点和终点之外的所有点,然后dfs判断是否连通:for 第二问,易知答案一定是第一问的子集,所以从第一问中的答案中枚 ...

  4. USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)

    usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...

  5. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  6. USACO Section 1.3 Prime Cryptarithm 解题报告

    题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...

  7. USACO 玛丽卡(最短路+枚举)

    USACO 玛丽卡 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市 ...

  8. USACO 5.4 Telecowmunication(最大流+枚举)

    面对最小割之类的题目,完全木想法... 枚举+最大流..复杂度很大了...居然很快的就过了.. /* ID: cuizhe LANG: C++ TASK: telecow */ #include &l ...

  9. USACO Section 4.4 追查坏牛奶Pollutant Control

    http://www.luogu.org/problem/show?pid=1344 题目描述 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件 ...

随机推荐

  1. gdb调试python

    一.概述 有时我们会想调试一个正在运行的Python进程,或者一个Python进程的coredump.例如现在遇到一个mod_wsgi的进程僵死了,不接受请求,想看看究竟是运行到哪行Python代码呢 ...

  2. 我终于解决UM编辑器了 泪......

    气死我了..... 好不容易测试好了....更显得我笨了..... 原来....什么都不用改   只改了2个小位置....真的是.....回首自己 不敢看 0.0 OK  记下步骤  以免以后忘记 将 ...

  3. javascrip格式

    1:若Button_Search_onclick()方法不存则在页面在火狐浏览器是可以正常运行,但是在IE浏览器就会出现问题 建议删除没用到的方法

  4. 关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器

    这篇文章主要介绍了关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器,需要的朋友可以参考下.希望对大家有所帮助   Firefox 和 IE 的浏览器各自实现了input历史记录的功能 ...

  5. Java学习之Java中常用对象

    java的几种对象(PO,VO,DAO,BO,POJO)解释     一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中 ...

  6. 出现"无法连接synaptics定点装置驱动程序"

    "开始“--”运行“--regedit(打开注册表)--依次打开 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVERSION/R ...

  7. jquery写的tab切换效果 非常简单

    自己写的一款 tab切换效果,比较简单,适合新手 <style type="text/css">*{margin:0; padding:0; font-size:12p ...

  8. org.gradle.process.internal.ExecException:

    com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ...

  9. 关于require,require_once,include和include_once的区别

    一.定义 require,require_once,include,include_once都属于PHP的关键字,也就是说它们实际都是PHP的语句,而不是函数,类似于print,echo一样,也是PH ...

  10. 简洁的PHP操作SQLite类

    SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了. ...