首页 > 其他分享 >Codeforces Round #156 (Div. 2) C. Almost Arithmetical Progression dp

Codeforces Round #156 (Div. 2) C. Almost Arithmetical Progression dp

时间:2023-04-24 22:39:25浏览次数:44  
标签:last Progression 156 Almost ll sequence int include dp


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
inputCopy
2
3 5
outputCopy
2
inputCopy
4
10 20 10 30
outputCopy
3
Note
In the first test the sequence actually is the suitable subsequence.

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

题意很明确:
看满足p,p-q,p,p-q……的最长子序列

考虑dp[ i ][ j ]:
表示最后最后两个数字为 ai,aj 时的序列长度:
那么 dp[ i ][ j ]=dp[ last ][ i ]+1;
last 表示离 i 最近且 a[ last ]= a[ j ] 的位置

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<set>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long int ull;
#define eps 1e-5
typedef pair<int,int> pii;
const ll mod=1e9+7;
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b){
        if(b%2)ans=ans*a%mod;
        b=b/2;
        a=a*a%mod;
    }
    return ans;
}

int a[maxn];
int dp[5004][5004];
//dp[i][j]=dp[last][i]+1;
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int i,j;
    for(i=1;i<=n;i++)cin>>a[i];
    int maxx=0;
    dp[0][0]=0;
    int last=0;
    for(j=1;j<=n;j++){
        for(i=0,last=0;i<j;i++){
            dp[i][j]=dp[last][i]+1;
            if(a[i]==a[j]){
                last=i;
            }
            maxx=max(maxx,dp[i][j]);
        }

    }
    cout<<maxx<<endl;
}


标签:last,Progression,156,Almost,ll,sequence,int,include,dp
From: https://blog.51cto.com/u_15657999/6221925

相关文章

  • [AGC061D] Almost Multiplication Table
    人类智慧。答案显然具有可二分性,考虑如何check。我们使用调整法,不妨设\(x_n<y_m\)(反着做同理),一开始我们令\(x_i=1,y_i=+\infty\)。每次我们期望让\(x\)不断变大,\(y\)不断变小,不断将它们调整到当前的上下界。具体的,每次令\(x_i=\max\{x_i,\max\lceil{a_{i,j}-k\overy......
  • C - Almost Sorted
    https://atcoder.jp/contests/arc132/tasks/arc132_c很难想到的动态规划,优化空间的思路非常巧妙用相对位置来转移f[i][j]表示i之前,放置数字的压缩情况为j,的所有方案数**f[i+1][(j|(1<<k))>>1]+=f[i][j]**k表示i放的数字的相对位置具体转移还是看代码#include<b......
  • 解决 ubuntu 无法关机 Dell Studio 1569 Cannot Shutdown in Ubuntu 11.10 or 12.04
    ShutdowncomputerusingterminalinUbuntufyouwanttoshutdownyourcomputerwhatdoyoudo?Simplygotoshutdownbuttonandclickshutdownisn’t? Haveyoueverwonderedhowwouldyoushutdownyourpcifyourgdm(GraphicalUserInterface)isnotwork......
  • 【蓝牙音频SoC】BES2700YP、BES2600YP、AB1565A、QCC3056助力TWS耳机实现更高音质效果
    1、高通QCC3056是一种超低功耗、单芯片解决方案,经过优化,可用于无线耳塞和耳戴式设备。它支持TrueWirelessMirroring技术,并具有广泛的差异化功能。QCC3056集成了1x80MHz3......
  • P4156 [WC2016]论战捆竹竿 题解
    题目链接题意描述给定一个字符串\(S\),你初始拥有一个空串\(T\),每次可以选择这个字符串的一个Border,去掉它后接在\(T\)的后面,操作后\(S\)不变,给出一个上限\(w\),求......
  • Oracle问题:ORA-01565
    问题oracle启动时报错,找不到spfile文件。ORA-01078:failureinprocessingsystemparametersORA-01565:errorinidentifyingfile'+datadg/prod/parameterfile/spf......
  • E - Geometric Progression
    E-GeometricProgressionhttps://atcoder.jp/contests/abc293/tasks/abc293_e 思路根据矩阵递推式找出转移矩阵的幂形式。利用矩阵快速幂计算。     ......
  • 题解 ABC293E【Geometric Progression】
    由于模数不一定是大质数,我们不能直接套等比数列求和公式。换一种思路,数列\(\langle1,A,A^2,\cdots,A^{X-1}\rangle\)可以看做线性递推,因此设计矩阵:\[\boldsymbolT=\b......
  • [ABC293E] Geometric Progression 题解
    [ABC293E]GeometricProgression题解神中神数论题目描述给定整数\(A,X,M\),求\[\sum_{i=0}^{X-1}A^i\bmodM\]\(1\leA,M\le10^9\)\(1\leX\le10^......
  • CF888D Almost Identity Permutations 题解
    CF链接:AlmostIdentityPermutationsLuogu链接:AlmostIdentityPermutations${\scr\color{Aquamarine}{\text{Solution}}}$前言这好像是一道能用数学秒掉的题目但......