usaco training 5.1.3 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] 

【译题】

描述

我们用N(1 <= N <=5000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,每个数表示钢琴上的一个键。很不幸这种表示旋律的方法忽略了音符的时值,但这项编程任务是关于音高的,与时值无关。

许多作曲家围绕一个重复出现的“主题”来构建乐曲。在我们的乐曲表示法中,“主题”是整个音符序列的一个子串,它需要满足如下条件:

⒈长度至少为5个音符

⒉在乐曲中重复出现(可能经过转调,见下)

⒊重复出现的同一主题不能有公共部分。

“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值。 给定一段乐曲,计算其中最长主题的长度(即音符数)。

本题时限为1秒钟!

[编辑]格式

PROGRAM NAME: theme

INPUT FORMAT

输入文件的第一行包含整数N。下面的每一行(最后一行可能除外)包含20个整数,表示音符序列。最后一行可能少于20个音符。

OUTPUT FORMAT

输出文件应只含一个整数,即最长主题的长度。如果乐曲中没有主题,那么输出0。

[编辑]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


[编辑]SAMPLE OUTPUT (file theme.out)

5

(这个长度为5的主题是输入文件中第一行的最后5个音符和第二行开头5个音符)


【分析】太强了!原来想用二分枚举加验证,但是对于验证,我只能想出n^3的暴力判断,根本就过不了的。打开NOCWO的题解,一篇有关于最长公共子串的DP方法跳入了我的眼帘。这个方法不错!

【代码1】

#include<stdio.h>
#include<iostream>
using namespace std;
int a[5005],f[5005][5005],i,j,n,ans;
int main()
{
  freopen("theme.in","r",stdin);
  freopen("theme.out","w",stdout);
  scanf("%ld",&n);
  for (i=1;i<=n;i++)
    scanf("%ld",&a[i]);
  for (i=1;i<=n;i++)
    for (j=i;j<=n;j++) 
    {
      if (a[i]-a[i-1]==a[j]-a[j-1]) 
        f[i][j]=max(f[i][j],f[i-1][j-1]+1);
      if (f[i][j]>ans) 
        ans=f[i][j];
    }
  if (ans<4) ans=-1;printf("%ld",ans+1);
  return 0;
}

【注释】在比较的时候有点巧妙,因为音调是可以变化的,所以用作差法可以避免这个问题。因为是作差嘛,结果是要加1的。但是很快我就发现这个程序的漏洞了:主题音调是不能重叠的!

【深入分析】避免重复其实也很简单,就是f[i-1][j-1]<j-i-1。只要在画图上随便比划一下就能发现。哦,还有最重要的一点,USACO有坑爹的内存限制,所以还要开滚存!

【代码2(AC)】

/*
PROG:theme
ID:juan1973
LANG:C++
*/
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
int a[5005],f[2][5005],i,j,n,ans,now;
int main()
{
  freopen("theme.in","r",stdin);
  freopen("theme.out","w",stdout);
  scanf("%ld",&n);
  for (i=1;i<=n;i++)
    scanf("%ld",&a[i]);
  for (i=1;i<=n;i++)
  {
	now=i%2;
	memset(f[now],0,sizeof(f[now]));
	for (j=i;j<=n;j++) 
    {
	  if (a[i]-a[i-1]==a[j]-a[j-1]&&f[now^1][j-1]<j-i-1) 
        f[now][j]=max(f[now][j],f[now^1][j-1]+1);
      if (f[now][j]>ans) 
        ans=f[now][j];
    }
  }
  if (ans<4) ans=-1;printf("%ld",ans+1);
  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
USACO2022金组是国际在线判题系统USACO的最高级别,题目难度较高,在该比赛中取得好成绩是一项巨大的成就。以下是对该比赛的一些题目解析。 第一题:“交通计划” 题目要求:给定一个n个节点的有向图,每条边有一个长度,希望添加最少的边使得所有节点连通,求最小生成树的权值和。 解析:该题可以使用Kruskal算法求解,将每条边按权值从小到大排序,再依次加入,判断加入的边是否会形成环,若形成则不加入,直到所有节点连通为止。此时Kruskal算法得到的最小生成树的权值和即为所求。 第二题:“点火计划” 题目要求:给定一个n个节点的有向图,每条边有一个权值和一个点火时长,每个节点有一个点火启动时刻和时刻结束时刻,希望从其中选出一些边点火,使得所有节点都可从点火的边出发到达,且所选点火边的总点火时长最小。 解析:该题可以使用最小费用最大流算法求解。将每条边看做一个容量为1,费用为点火时长的边,源点向节点的点火边容量为1,费用为0的边,节点的点火边向汇点的容量为1,费用为0的边,对这个网络进行最小费用最大流即可得到所选边的总点火时长最小。 第三题:“美味佳肴” 题目要求:给定n个菜品,每个菜品有它的权值和两个类别,希望选出k个菜品,使得选出的菜品数量在每个类别中都不超过$\frac{k}{3}$个,且所选菜品的权值和最大。 解析:该题可以使用动态规划求解。设$f[i][j][k]$表示前i个菜品中,选择j个一类菜品,选择k个二类菜品的最大权值和,状态转移方程为$f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][k]+a[i],f[i-1][j][k-1]+b[i])$,其中a[i]为i号菜品的权值,若为一类则为该权值,否则为0,b[i]为i号菜品的权值,若为二类则为该权值,否则为0。最终答案为$f[n][$k/3$][$k/3$]。 以上是对USACO2022金组的部分题目的解析,USACO比赛是全球范围内的计算机竞赛,竞争非常激烈,能够在该比赛中脱颖而出是一项非常棒的成就。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值