题意描述
嗯...。
Problem 3: Arithmetic Progressions [Brian Dean, 2004]
FJ has implanted a new machine-readable RFID chip inside each of his cows so he can monitor them to ensure they all come home after a hard day feeding in the pastures.
He was intrigued to notice one night that the cows often lined up with many of their serial numbers in an arithmetic progression. One after he saw five cows go by with these serial numbers:
1 4 3 5 7
A quick inspection shows that cows numbered 1, 3, 5, and 7 were in the order of an arithmetic progression (each member of the progression differs from the previous progression's member by the same integer).
Help FJ learn if this is coincidence. Given a list of N (1 <= N <= 2,000) serial numbers (nonnegative integers less than one billion), find the longest subsequence of this list that forms an arithmetic progression (progressions can run from large to small or small to large) and report its length.
PROBLEM NAME: arithprg
INPUT FORMAT:
Line 1: A single line with the integer N.
Lines 2..N+1: N lines, each with a single integer. The first line represents the first cow's serial number, etc.
SAMPLE INPUT (file arithprg.in):
5
1
4
3
5
7OUTPUT FORMAT:
- Line 1: A single integer that is the length of the longest progression.
SAMPLE OUTPUT (file arithprg.out):
4
给定一个含有 \(N\) 个数的数列,求出其中最长的等差数列的长度。
算法分析
貌似暴力可以过,不过这就留给神犇实现吧
本着拒绝暴力踩标算水过的理念,蒟蒻认为这题可以 DP。
设 \(a(i)\) 表示数列中第 \(i\) 个数的值,\(f(i,a(j))\) 表示最后一个数是 \(i\),前一个数的值是 \(a(j)\) 的最长个数。
显然前面的前面的数(不是口吃)就是:\(a_j-(a_i-a_j)=2\times a_j-a_i\)。
\(f(i,a_j)=max_{1\leq j
预处理:\(f(1,0)=1,f(2,a_1)=2\)。
但是据观察发现 \(a_i\leq 10^9\),所以直接开数组会 MLE 爆炸。
由此可见 STL 大法好,用 map 映射即可。(但是 STL 过慢,需要常数优化,所以读者若有思路蒟蒻虚心求教)
代码实现
#include
#include
#include
#include
#include
#include
完结撒花。