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. 视音频数据处理入门:FLV封装格式解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  2. 戴尔R720xd服务器系统安装前期环境实现

    型号:R720xd 开启服务器,Ctrl+R进入raid配置 配置完raid后F2对硬盘进行格式化 保存并重启 F11进入BIOS选项设置U盘启动 选择U盘启动 开始进行系统安装!

  3. 【官方文档】Nginx负载均衡学习笔记(二)负载均衡基本概念介绍

    简介 负载均衡(Server Load Balancer)是将访问流量根据转发策略分发到后端多台 ECS 的流量分发控制服务.负载均衡可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应 ...

  4. Intellij IDEA设置及快捷键使用总结

    1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...

  5. 【实操笔记】MySQL主从同步功能实现

    写在前边: 这两天来了个需求,配置部署两台服务器的MySQL数据同步,折腾了两天查了很多相关资料,一直连不上,后来发现其实是数据库授权的ip有问题,我们用的服务器是机房中的虚拟机加上反向代理出来的,坑 ...

  6. 网站监控系统安装部署(zabbix,nagios)

    zabbix分布式监控系统安装部署 官方网站链接 https://www.zabbix.com/documentation/2.0/manual/installation 安装环境说明 参考地址 ht ...

  7. 15个超级实用的jQuery插件

    jQuery是一个可订制的.轻量级的前端开发框架,它会让你的前端开发拥有无限的可能性.它会在敏捷Web开发中帮你做很多事情,比如简化HTML文档的解析.事件处理.动画效果和Ajax交互.实践上jQue ...

  8. HDFS的java接口——简化HDFS文件系统操作

    今天闲来无事,于是把HDFS的基本操作用java写出简化程序出来给大家一些小小帮助! package com.quanttech; import org.apache.hadoop.conf.Conf ...

  9. 数位DP入门(A - 不要62 HDU - 2089 &&B - Bomb HDU - 3555 )

    题目链接:https://cn.vjudge.net/contest/278036#problem/A 具体思路:对于给定的数,我们按照位数进行运算,枚举每一位上可能的数,在枚举的时候需要注意几个条件 ...

  10. 01.Web基础和HTML初始

    1.1 上网就是请求数据 我们先不直接解决这个问题,我们做一个小实验.我们每个人的电脑里面,都有一个神秘的文件夹: C:\Users\Weiheng\AppData\Local\Microsoft\W ...