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 ...
随机推荐
- 服务器IP安全策略限制网络访问
https://jingyan.baidu.com/article/3c343ff714d4890d377963cd.html https://www.icbase.com/TestGetUrl.as ...
- (转) linux下vim和bash配置文件
1.注释版 ~/.vimrc "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限 set nocompatible set autoread " 文件修改之后自动载入 ...
- 51nod 1181 质数中的质数
1181 质数中的质数(质数筛法) 题目来源: Sgu 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 如果一个质数,在质数列表中的编号也是质数,那么就称 ...
- SQL语句(二十)—— 数据库安全性
数据库安全性 1. SQL Server 配置管理器 => 网络配置 MSSQLSERVER 协议,如果应用程序和SQL Server 在同一机器上,仅使用 Shared Memory (共享 ...
- android 低功耗蓝牙使用
参考链接:http://blog.csdn.net/xubin341719/article/details/38584469 1.android 手机的低功耗蓝牙,又称BLE :BLE在andriod ...
- Vue 的style绑定显示background-image
data () { return { img: require('你的json资源路径') } } :style="{backgroundImage: 'url(' + img + ')'} ...
- 简易jQuery插件
之前写过jQuery插件的笔记 如何用jQuery封装插件 我一直觉得前面讲了一大堆闭包和三种插件封装模式有点冗余,那篇笔记我直到记录到后面才发现这事情很简单,想来想去还是觉得网上的一些文章把事情搞复 ...
- [转载]strtok函数和strtok_r函数
1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{ char name[25]; char sex[1 ...
- SHA-1(安全哈希算法实现)
如题,不知道sha-1的自己百度吧. #include <iostream> #include <vector> //定义vector数组 #include <strin ...
- jQuery技巧笔记
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...