USACO 5.1 Musical Themes(哈希+二分)
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(哈希+二分)的更多相关文章
- USACO Section 5.1 Musical Themes(枚举)
直接枚举O(n^3)会TLE,只要稍微加点优化,在不可能得到更优解时及时退出.其实就是道水题,虽说我提交了6次才过= =..我还太弱了 -------------------------------- ...
- P2743(poj1743) Musical Themes[差分+后缀数组]
P2743 乐曲主题Musical Themes(poj1743) 然后呢这题思路其实还是蛮简单的,只是细节特别多比较恶心,忘记了差分带来的若干疏漏.因为转调的话要保证找到相同主题,只要保证一段内相对 ...
- 【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} ...
- POJ1743 Musical Theme(后缀数组 二分)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 33462 Accepted: 11124 Description A m ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- LOJ.6066.[2017山东一轮集训Day3]第二题(树哈希 二分)
LOJ 被一件不愉快的小事浪费了一个小时= =. 表示自己(OI方面的)智商没救了=-= 比较显然 二分+树哈希.考虑对树的括号序列进行哈希. 那么每个点的\(k\)子树的括号序列,就是一段区间去掉距 ...
- [BZOJ]4650 优秀的拆分(Noi2016)(哈希+二分)
传送门 题解 听说大佬们这题都是用SA秒掉的 然而SA的时间复杂度的确很优秀,缺点就是看不太懂…… 然后发现一位大佬用哈希华丽的过了此题,而且讲的特别清楚->这里 我们只要考虑以每一个点结尾 ...
- URAL - 1486 Equal Squares 二维哈希+二分
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...
- BZOJ 3796 Mushroom追妹纸 哈希+二分(+KMP)
先把两个串能匹配模式串的位置找出来,然后标记为$1$(标记在开头或末尾都行),然后对标记数组求一个前缀和,这样可以快速查到区间内是否有完整的一个模式串. 然后二分子串(答案)的长度,每次把长度为$md ...
随机推荐
- Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)
Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑配置文件(pml.xml)(我 ...
- Java基础-面向对象第二特征之继承(Inheritance)
Java基础-面向对象第二特征之继承(Inheritance) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.继承的概述 在现实生活中,继承一般指的是子女继承父辈的财产.在程序 ...
- eclipse下new server时不可创建的解决方法
在eclipse里新建server时会在工作目录下\.metadata\.plugins\org.eclipse.core.runtime\.settings下自动生成org.eclipse.wst. ...
- openstack虚拟机启动过程源码分析
源码版本:H版 以nova-api为起点开始分析! 一.在nova-api进程中进行处理 根据对nova api的分析,当请求发过来的时候,由相应的Controller进行处理,此处如下: nova/ ...
- 2015/11/5用Python写游戏,pygame入门(5):面向对象的游戏设计
昨天的内容里有了运动的子弹,虽然我们只添加了一个子弹,但你可以看到我们需要记录子弹的x,y坐标,每次要更新它的坐标.如果我们想要有多颗子弹,就需要存储多个坐标.那时候处理起来就不显得那么简单,也许我们 ...
- Java并发编程原理与实战二十六:闭锁 CountDownLatch
关于闭锁 CountDownLatch 之前在网上看到过一篇举例非常形象的例子,但不记得是出自哪里了,所以这里就当自己再重新写一篇吧: 例子如下: 我们每天起早贪黑的上班,父母每天也要上班,有一天定了 ...
- VBS 重启 TP-Link 路由器
分享一个自己用的小工具,重启TP-Link路由器的,好像还是大学时候写的,献丑了. 其他路由器可能有些不同,但是思路都是差不多的. user = "admin" '路由器帐号 pa ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- 【译】第十篇 SQL Server代理使用代理帐户
本篇文章是SQL Server代理系列的第十篇,详细内容请参考原文 在这一系列的上一篇,你查看了msdb库下用于授权访问SQL Server代理的安全角色.这些角色包括SQLAgentUserRole ...
- 2016.5.57—— Remove Duplicates from Sorted List
Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...