日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)

發(fā)布時(shí)間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an?almost arithmetical progression, if its elements can be represented as:

  • a1?=?p, where?p?is some integer;
  • ai?=?ai?-?1?+?(?-?1)i?+?1·q?(i?>?1), where?q?is some integer.

Right now Gena has a piece of paper with sequence?b, consisting of?n?integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.

Sequence?s1,??s2,??...,??sk?is a subsequence of sequence?b1,??b2,??...,??bn, if there is such increasing sequence of indexes?i1,?i2,?...,?ik?(1??≤??i1??<??i2??<?... ??<??ik??≤??n), that?bij??=??sj. In other words, sequence?s?can be obtained from?b?by crossing out some elements.

Input

The first line contains integer?n?(1?≤?n?≤?4000). The next line contains?n?integers?b1,?b2,?...,?bn?(1?≤?bi?≤?106).

Output

Print a single integer — the length of the required longest subsequence.

Examples

Input

2 3 5

Output

2

Input

4 10 20 10 30

Output

3

Note

In the first test the sequence actually is the suitable subsequence.

In the second test the following subsequence fits:?10,?20,?10.

題目大意:

? ?給出一個(gè)序列,求最長的子序列,滿足隔位的兩個(gè)數(shù)相等,問這個(gè)最長的子序列的長度是多少。

解題報(bào)告:

? 想了好久想到一個(gè)dp方程,抱著冒險(xiǎn)的心態(tài)o(n^2)交一發(fā),TLE41了,,,把離散化的數(shù)組提前預(yù)處理了一下,,,又莽了一發(fā),,AC了。。小驚喜。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 1e6+5; int dp[4004][4004]; int a[4004],b[4004]; int cnt[MAX]; int pos[MAX]; int len,n; int get(int x) {return lower_bound(b+1,b+len+1,x) - b; } int main() {cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i],cnt[a[i]]++;sort(b+1,b+n+1);len = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) pos[a[i]] = get(a[i]);for(int i = 1; i<=n; i++) for(int j = 1; j<=n; j++)dp[pos[a[i]]][pos[a[j]]] = 1;for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i]!=a[j])dp[pos[a[i]]][pos[a[j]]] = dp[pos[a[j]]][pos[a[i]]]+1;}}int maxx = -1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {maxx = max(maxx,dp[pos[a[i]]][pos[a[j]]]);}}for(int i = 1; i<=n; i++) {maxx = max(maxx,cnt[a[i]]);}printf("%d\n",maxx);return 0 ;}

貼一個(gè)題解

再貼一個(gè)題解

總結(jié):

? ?思維過程是這樣的,,剛開始想暴力(用vector存權(quán)值啊之類的亂搞),,一想肯定不行,,有太多重復(fù)操作了。。然后想各種優(yōu)化后來發(fā)現(xiàn)就是要求個(gè)序列,,所以跟最長上升子序列聯(lián)系起來了,,后來發(fā)現(xiàn)還真可以寫出來個(gè)遞推式,,但是是o(n^2)的不知道會(huì)不會(huì)T,,而且1600W的數(shù)組大小不知道會(huì)不會(huì)MLE,,交一發(fā),WA,,造樣例,,發(fā)現(xiàn)全是同一個(gè)數(shù)的時(shí)候就會(huì)爆炸,,我就想,,再多也不可能超過數(shù)組長度啊,,所以就像把元素相等的時(shí)候特判一下(并沒想過正確性),改代碼,,測(cè)樣例,,沒啥問題,,再交一發(fā),TLE41,,,失望(但是還是有點(diǎn)小驚喜的因?yàn)榕芷饋砹俗钇鸫a說明算法的問題不大)。再想優(yōu)化,開個(gè)數(shù)組預(yù)處理一波,去掉了時(shí)間復(fù)雜度的log,再交,AC。

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。