#P15433. [蓝桥杯 2025 国 Python B] 灯塔

    ID: 17453 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>动态规划 DP贪心2025排序蓝桥杯国赛

[蓝桥杯 2025 国 Python B] 灯塔

Problem Description

On a coastline, there are nn lighthouses in a row, numbered from 11 to nn from left to right. You need to light up some of them to guide ships. You are given a lighting sequence a1,a2,,ama_1, a_2, \dots, a_m, where the ii-th operation means trying to light the lighthouse numbered aia_i. However, to save energy, if lighthouse ai1a_i - 1 or lighthouse ai+1a_i + 1 has already been lit, then lighthouse aia_i cannot be lit and this operation will be skipped. Of course, the same lighthouse will be lit at most once.

You may choose a subsequence from the given lighting sequence (keeping the relative order of operations), and execute the lighting operations one by one in the order of the subsequence. What is the maximum number of lighthouses that can be successfully lit?

Input Format

The first line contains two positive integers n,mn, m, separated by a space.

The second line contains mm positive integers a1,a2,,ama_1, a_2, \dots, a_m, with a space between adjacent integers.

Output Format

Output one line containing one integer, which is the answer.

10 9
1 3 2 5 2 10 9 6 5
4

Hint

Sample Explanation

One possible plan: keep the subsequence 1,3,5,91, 3, 5, 9, which can light up 44 lighthouses.

Testdata Scale and Constraints

For 40%40\% of the test cases, 1n,m50001 \le n, m \le 5000.

For all test cases, 1n,m1061 \le n, m \le 10^6, and 1ain1 \le a_i \le n.

Translated by ChatGPT 5