CF

来源:互联网 发布:soundtrack pro mac 编辑:程序博客网 时间:2024/05/05 04:01

1.题目描述:

D. Two Melodies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time!

Alice has a sheet with n notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

Subsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7.

You should write a program which will calculate maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.

Input

The first line contains one integer number n (2 ≤ n ≤ 5000).

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.

Output

Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.

Examples
input
41 2 4 5
output
4
input
662 22 60 61 48 49
output
5
Note

In the first example subsequences [1, 2] and [4, 5] give length 4 in total.

In the second example subsequences [62, 48, 49] and [60, 61] give length 5 in total. If you choose subsequence [62, 61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal.


2.题意概述:

定义一段弦乐是好听的,当且仅当相邻元素要么相差1要么相差7的倍数。给你一段乐谱,问其中两段最长的不相交好听的子段和是多少?

3.解题思路:

子段而且不连续,不相交问题,而且n较小,可以尝试O(n^2)的动态规划。定义dp[i][j]表示这两段分别以i结尾和以j结尾且不相交组成的最大子段和。那么我们一种方法是枚举i的同时枚举j。但是相交问题,显然i==j时候值为0,为了防止冲突,我们可以只枚举i<j,情况,因为i>j不能保证他们之间一定不相交。边界条件就是dp[i][j]=dp[i][0]+1;那么转移肯定是从之前的最大值转移,如果每一次都去找最大值未免浪费时间,考虑怎么优化:我们设maxmod[a[j]%7]表示所有dp[i][1...i]中与dp[i][j]中a[j]%7同余的dp的最大值,maxnum[a[j]]表示所有dp[i][1...i]中与a[j]相等数的dp最大值。

那么转移方程也容易归纳出来就是:

dp[i][j]=max(dp[i][0], maxmod[a[j] % 7], maxnum[a[j] - 1], maxnum[a[j] + 1]) + 1

需要注意的是每更新一个dp[i][j]别忘了更新maxmod和maxnum的值。

4.AC代码:

#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define maxn 100010#define lson root << 1#define rson root << 1 | 1#define lent (t[root].r - t[root].l + 1)#define lenl (t[lson].r - t[lson].l + 1)#define lenr (t[rson].r - t[rson].l + 1)#define N 5005#define eps 1e-6#define pi acos(-1.0)#define e exp(1.0)using namespace std;const int mod = 1e9 + 7;typedef long long ll;typedef unsigned long long ull;int a[N], dp[N][N], maxmod[8], maxnum[maxn];int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();#endifint n;scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]);memset(dp, 0, sizeof dp);int ans = 0;for (int i = 0; i <= n; i++){memset(maxmod, 0, sizeof maxmod);memset(maxnum, 0, sizeof maxnum);for (int j = 1; j <= i; j++){maxmod[a[j] % 7] = max(maxmod[a[j] % 7], dp[i][j]);//all a[j] % 7maxnum[a[j]] = max(maxnum[a[j]], dp[i][j]);//all a[j]}for (int j = i + 1; j <= n; j++){dp[i][j] = max(dp[i][j], dp[i][0] + 1);dp[i][j] = max(dp[i][j], maxmod[a[j] % 7] + 1);dp[i][j] = max(dp[i][j], maxnum[a[j] - 1] + 1);dp[i][j] = max(dp[i][j], maxnum[a[j] + 1] + 1);maxmod[a[j] % 7] = max(maxmod[a[j] % 7], dp[i][j]);maxnum[a[j]] = max(maxnum[a[j]], dp[i][j]);dp[j][i] = dp[i][j];ans = max(ans, dp[i][j]);}}printf("%d\n", ans);#ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);#endifreturn 0;}

原创粉丝点击