G. Four Melodies

Codeforces
IDCF818G
Time5000ms
Memory1024MB
Difficulty
flowsgraphs
English · Original
Chinese · Translation
Formal · Original
_Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!_ Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. This time Alice wants to form four melodies for her tracks. Alice has a sheet with _n_ notes written on it. She wants to take four such non-empty non-intersecting subsequences that all 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 differ by _1_ or are congruent modulo _7_. You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody. ## Input The first line contains one integer number _n_ (4 ≤ _n_ ≤ 3000). The second line contains _n_ integer numbers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 105) — notes written on a sheet. ## Output Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody. [samples] ## Note In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note). In the second example it is possible to compose one melody with 2 notes — {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
_作者注:我相信你们中的一些人可能还记得 Educational Codeforces Round 22 中的题目 "Two Melodies"。现在是时候让它变得更难一些了!_ Alice 是一位作曲家,最近她录制了两首非常受欢迎的曲目。现在她拥有了大量等待新曲目的粉丝。 这次,Alice 想为她的曲目创作四个旋律。 Alice 的乐谱上写有 #cf_span[n] 个音符。她希望从中选出四个非空且互不相交的子序列,使得每个子序列都构成一个 _旋律_,且它们的长度总和最大。 _子序列是从另一个序列中删除某些元素而不改变剩余元素顺序而得到的序列。_ 当一个子序列中每两个相邻的音符要么相差 _1_,要么模 _7_ 同余时,该子序列构成一个 _旋律_。 你需要编写一个程序,计算满足上述条件的四个非空、互不相交子序列的最大长度总和。 第一行包含一个整数 #cf_span[n](#cf_span[4 ≤ n ≤ 3000])。 第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an](#cf_span[1 ≤ ai ≤ 105])——写在乐谱上的音符。 请输出满足条件的四个非空、互不相交子序列的最大长度总和。 在第一个示例中,可以通过选择任意 #cf_span[4] 个音符来组成 #cf_span[4] 个旋律(每个旋律仅包含一个音符)。 在第二个示例中,可以组成一个包含 #cf_span[2] 个音符的旋律 —— #cf_span[{1, 2}]。其余音符分别用于另外三个旋律(每个旋律一个音符)。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[4 ≤ n ≤ 3000])。第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an](#cf_span[1 ≤ ai ≤ 105])——写在乐谱上的音符。 ## Output 请输出满足条件的四个非空、互不相交子序列的最大长度总和。 [samples] ## Note 在第一个示例中,可以通过选择任意 #cf_span[4] 个音符来组成 #cf_span[4] 个旋律(每个旋律仅包含一个音符)。在第二个示例中,可以组成一个包含 #cf_span[2] 个音符的旋律 —— #cf_span[{1, 2}]。其余音符分别用于另外三个旋律(每个旋律一个音符)。
**Definitions** Let $ n \in \mathbb{Z} $ with $ 4 \leq n \leq 3000 $. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of integers with $ 1 \leq a_i \leq 10^5 $. A subsequence $ S = (s_1, s_2, \dots, s_k) $ forms a *melody* if for all $ 1 \leq i < k $, $$ |s_i - s_{i+1}| = 1 \quad \text{or} \quad s_i \equiv s_{i+1} \pmod{7}. $$ **Constraints** We seek four non-empty, pairwise disjoint subsequences $ S_1, S_2, S_3, S_4 $ of $ A $, such that each $ S_j $ forms a melody. **Objective** Maximize the total length: $$ \max \sum_{j=1}^{4} |S_j| $$ subject to the above constraints.
Samples
Input #1
5
1 3 5 7 9
Output #1
4
Input #2
5
1 3 5 7 2
Output #2
5
API Response (JSON)
{
  "problem": {
    "name": "G. Four Melodies",
    "description": {
      "content": "_Author note: I think some of you might remember the problem \"Two Melodies\" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!_ Alice is a composer, and recently sh",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 1048576
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF818G"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "_Author note: I think some of you might remember the problem \"Two Melodies\" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!_\n\nAlice is a composer, and recently sh...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "_作者注:我相信你们中的一些人可能还记得 Educational Codeforces Round 22 中的题目 \"Two Melodies\"。现在是时候让它变得更难一些了!_\n\nAlice 是一位作曲家,最近她录制了两首非常受欢迎的曲目。现在她拥有了大量等待新曲目的粉丝。\n\n这次,Alice 想为她的曲目创作四个旋律。\n\nAlice 的乐谱上写有 #cf_span[n] 个音符。她希望从中选出...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ with $ 4 \\leq n \\leq 3000 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of integers with $ 1 \\leq a_i \\leq 10^5 $.  \n\nA subsequence $ S = (s_1, s_2, ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments