No.3542 Progression (Python)
タグ : / 解いたユーザー数 24
作問者 :
Rho
/ テスター :
Problem Statement
You are given a sequence of positive integers $A = (A_1, A_2, \ldots, A_N)$ of length $N$.
Among the contiguous subarrays of $A$ that are arithmetic progressions, find the length of the longest one.
Input
$N$ $A_1\ A_2\ \ldots\ A_N$
- All input values are integers.
- $1 \leq N \leq 2000$
- $1 \leq A_i \leq 10^9$
Output
Print the answer on one line.
A trailing newline or trailing whitespace may be present or absent.
Score
Intended solution: $88$ bytes
Score formula
If the code length of a submitted source code is $x$ and the code length of the intended solution of this problem is $y$, the score of that source code is $\max(500 - x + y, 100)$ points.The score of this problem is the maximum score among the source codes submitted to this problem.
Sample Program (Python)
An example solution in Python is shown below. By submitting this code, you can obtain $266$ points.
N = int(input())
A = list(map(int, input().split()))
ans = 1
for left_idx in range(N - 1):
diff = A[left_idx + 1] - A[left_idx]
for right_idx in range(left_idx + 1, N):
if A[right_idx] - A[right_idx - 1] != diff:
break
if ans < right_idx - left_idx + 1:
ans = right_idx - left_idx + 1
print(ans)
Sample
Sample 1
Input
5 3 1 4 7 5
Output
3
The contiguous subarray $(A_2, A_3, A_4)=(1, 4, 7)$ of $A$ is an arithmetic progression, and its length is $3$.
No contiguous subarray of $A$ that is an arithmetic progression has length greater than $3$, so print $3$.
Sample 2
Input
5 3 1 4 1 5
Output
2
Sample 3
Input
1 1000000000
Output
1
Sample 4
Input
10 10 9 8 7 6 5 4 3 2 1
Output
10
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。