forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
62 lines (54 loc) · 1.08 KB
/
C.cpp
File metadata and controls
62 lines (54 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
int n, a[3333];
short f[3333][3333], tr[3333][3333];
unordered_map <int,int> id;
int main()
{
ios::sync_with_stdio(0);
freopen("fibsubseq.in", "r", stdin);
freopen("fibsubseq.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
if (n == 1)
{
cout << 1 << endl << a[1] << endl;
return 0;
}
int ans = 0, ri, rj;
for (int i = 1; i < n; i++)
{
for (int j = i + 1; j <= n; j++)
{
f[i][j] = 2;
if (id.count(a[j] - a[i]))
{
int k = id[a[j] - a[i]];
if (f[k][i] + 1 > f[i][j])
{
f[i][j] = f[k][i] + 1;
tr[i][j] = k;
}
}
if (f[i][j] > ans)
{
ans = f[i][j];
ri = i;
rj = j;
}
}
id[a[i]] = i;
}
vector <int> seq(1, a[rj]);
while (ri)
{
seq.push_back(a[ri]);
int tmp = ri;
ri = tr[ri][rj];
rj = tmp;
}
cout << ans << endl;
for (int i = ans - 1; i >= 0; i--)
cout << seq[i] << ' ';
}