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]

——————————————————————————————题解

这个的话我根本没有算我的算法的复杂度……

但是最慢是0.163

好像nocow里众说纷纭……我也因为复杂度算不好迟不迟不敢开敲

后来瞅了一眼他们说n^4加上几个优化就过了

那我怕个毛啊

我的思路是这样的,既然作曲家喜欢奇怪的转调,那么其实这些主旋律的差值都是一样的,这样处理成差分就可以了,为了让差分都是正值我们可以都加上88

然后hash存起来,类似Rabin-Karp字符串匹配的哈希函数

然后用200个vector来记录每个值在的位置,n^2的枚举然后二分长度更新ans

二分时的优化,如果r<=ans 跳出

 /*
ID: ivorysi
LANG: C++
TASK: theme
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int h[];
int e[];
int num[],n,lis[],f[],ans;
vector<int> v[];
bool check(int s1,int s2,int len) {
if(s1+len > n+ || s2+len > n+) return false;
return ((ll)e[MAXN-s1]*(h[s1+len-]-h[s1-])-(ll)e[MAXN-s2]*(h[s2+len-]-h[s2-]))%mo==;
}
void init() {
scanf("%d",&n);
siji(i,,n) {
scanf("%d",&num[i]);
}
--n;
siji(i,,n) {
lis[i]=num[i+]-num[i]+;
v[lis[i]].push_back(i);
}
siji(i,,) {
if(!v[i].empty()) {
sort(v[i].begin(),v[i].end());
}
}
e[]=;
siji(i,,) {
e[i]=(ll)e[i-]*base%mo;
}
siji(i,,n) {
h[i]=((ll)h[i-]+(ll)lis[i]*e[i]%mo)%mo;
}
}
int binary(int s1,int s2,int minz,int maxz) {
if(maxz<=minz) return -;
if(maxz<=ans) return -;
int l=minz,r=maxz;
while(l<r) {
int mid=(l+r+)>>;
if(check(s1,s2,mid)) l=mid;
else {
r=mid-;
if(r<=ans) return -;
}
}
return l;
}
void solve() {
init();
siji(i,,) {
if(v[i].empty() || v[i].size()==) continue;
xiaosiji(j,,v[i].size()) {
if(f[v[i][j]-]>=) f[v[i][j]]=f[v[i][j]-]-;
else f[v[i][j]]=;
xiaosiji(k,j+,v[i].size()) {
int x=binary(v[i][j],v[i][k],f[v[i][j]],v[i][k]-v[i][j]-);
f[v[i][j]]=max(f[v[i][j]],x);
f[v[i][k]]=max(f[v[i][k]],x);
}
ans=max(ans,f[v[i][j]]);
}
}
++ans;
if(ans<) puts("");
else {
printf("%d\n",ans);
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("theme.in","r",stdin);
freopen("theme.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}

USACO 5.1 Musical Themes(哈希+二分)的更多相关文章

  1. USACO Section 5.1 Musical Themes(枚举)

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

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

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

  3. 【CodeForces】961 F. k-substrings 字符串哈希+二分

    [题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...

  4. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  5. Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...

  6. LOJ.6066.[2017山东一轮集训Day3]第二题(树哈希 二分)

    LOJ 被一件不愉快的小事浪费了一个小时= =. 表示自己(OI方面的)智商没救了=-= 比较显然 二分+树哈希.考虑对树的括号序列进行哈希. 那么每个点的\(k\)子树的括号序列,就是一段区间去掉距 ...

  7. [BZOJ]4650 优秀的拆分(Noi2016)(哈希+二分)

    传送门   题解 听说大佬们这题都是用SA秒掉的 然而SA的时间复杂度的确很优秀,缺点就是看不太懂…… 然后发现一位大佬用哈希华丽的过了此题,而且讲的特别清楚->这里 我们只要考虑以每一个点结尾 ...

  8. URAL - 1486 Equal Squares 二维哈希+二分

    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...

  9. BZOJ 3796 Mushroom追妹纸 哈希+二分(+KMP)

    先把两个串能匹配模式串的位置找出来,然后标记为$1$(标记在开头或末尾都行),然后对标记数组求一个前缀和,这样可以快速查到区间内是否有完整的一个模式串. 然后二分子串(答案)的长度,每次把长度为$md ...

随机推荐

  1. Spark记录-Scala数据类型

    Scala与Java具有相同的数据类型,具有相同的内存占用和精度.以下是提供Scala中可用的所有数据类型的详细信息的表格: 序号 数据类型 说明 1 Byte 8位有符号值,范围从-128至127 ...

  2. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  3. 2017 清北济南考前刷题Day 2 afternoon

    期望得分:100+60+70=230 实际得分:0+60+0=60 T1 可以证明如果一对括号原本就匹配,那么这对括号在最优解中一定不会被分开 所以用栈记录下没有匹配的括号 最后栈中一定是 一堆右括号 ...

  4. opencv 图像处理函数大全

    .cvLoadImage:将图像文件加载至内存: .cvNamedWindow:在屏幕上创建一个窗口: .cvShowImage:在一个已创建好的窗口中显示图像: .cvWaitKey:使程序暂停,等 ...

  5. 【转】Elastic日报 第576期 (2019-04-05)

    1.Elasticsearch性能测试实践http://t.cn/EiRzFiI2.监控Elasticsearch的插件推荐http://t.cn/EiRzFix3.支持机器数据的可扩展Elastic ...

  6. Spring容器事件、自定义事件

    Spring容器内置事件,如容器的启动.停止.关闭.销毁等事件 <bean name="contextStartedHandler" class="com.nuts ...

  7. 谁说码农不懂浪漫?js写的'老婆生日快乐'特效

    一直被老婆抱怨不懂浪漫,老婆的生日又来了,老婆指着闺蜜空间上贴的老公做的胡萝卜心形浪漫晚餐告诉我:必须送她一份用心的礼物.我绞尽脑汁想出这么一法子,还是得用我们码农的独特方式,经过一天多的努力,终于做 ...

  8. mysql 距离函数

    要有超级权限 SET GLOBAL log_bin_trust_function_creators = 1;DELIMITER $$CREATE DEFINER=`root`@`localhost` ...

  9. Window Batch编程示例

    日期时间相关示例 将下面的代码保存为批处理文件 ,命名为GetDate.bat 可以在另外的批处理文件中call GetDate.bat,并直接使用GetDate.bat里面定义的变量,如下图所示: ...

  10. 用jsx语法写iview事件

    普通的vue事件,在jsx中写法为 on+方法名(首字母大写) . 如:onClick={....}.onChange={....}.onBlur={....} iview中的事件,在vue中默认是 ...