cf255C Almost Arithmetical Progression
C. Almost Arithmetical Progressiontime limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
InputThe first line contains integer n (1 ≤ n ≤ 4000). The next line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106).
OutputPrint a single integer — the length of the required longest subsequence.
Examplesinput2
3 5output2input4
10 20 10 30output3NoteIn the first test the sequence actually is the suitable subsequence.
In the second test the following subsequence fits: 10, 20, 10.
dp[i][j]表示以a[i]为尾,a[j]是这个序列前一个数的最长长度
dp[i][j]=max(dp[j][k]+1){0<=k<j&&a[k]==a[i]}但这样复杂度是O^3
但是dp[j][k]中的k肯定是选择最靠近j的那个,假如有k1<k2<……<kx<j,dp[j][kx]>=dp[j][k(1~x-1)]
这样的话,复杂度就变成O^2了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
typedef long long ll;
#define X first
#define Y second
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define sd(x) scanf("%d",&(x))
#define Pi acos(-1.0)
#define sf(x) scanf("%lf",&(x))
#define ss(x) scanf("%s",(x))
#define maxn 10000000
#include <ctime>
const int inf=0x3f3f3f3f;
const long long mod=;
using namespace std;
int dp[][];
int num[];
int main()
{
#ifdef local
freopen("in","r",stdin);
//freopen("data.txt","w",stdout);
int _time=clock();
#endif
int n;
cin>>n;
for(int i=;i<=n;i++)
sd(num[i]);
int ans=;
int la;
for(int i=;i<=n;i++)
{
la=;
for(int j=;j<i;j++)
{
dp[i][j]=dp[j][la]+;
if(num[i]==num[j])la=j;
ans=max(dp[i][j],ans);
}
}
cout<<ans<<endl;
#ifdef local
printf("time: %d\n",int(clock()-_time));
#endif
}
cf255C Almost Arithmetical Progression的更多相关文章
- Codeforces Round #156 (Div. 2)
A. Greg's Workout 模3求和,算最大值. B. Code Parsing 最后左半部分为x,右半部分为y,那么从中间不断去掉xy,直到其中一种全部消去. C. Almost Arith ...
- OUC_OptKernel_oshixiaoxiliu_好题推荐
poj1112 Team Them Up! 补图二分图+dp记录路径codeforces 256A Almost Arithmetical Progression dp或暴力 dp[i][j] = d ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- CodeForces 567C Geometric Progression
Geometric Progression Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- hdu 5278 Geometric Progression 高精度
Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...
- hdu 5429 Geometric Progression(存个大数模板)
Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...
- 文献导读 | Single-Cell Sequencing of iPSC-Dopamine Neurons Reconstructs Disease Progression and Identifies HDAC4 as a Regulator of Parkinson Cell Phenotypes
文献编号:19Mar - 11 2019年04月23日三读,会其精髓: 相信这种方法的话,那么它的精髓是什么,如何整合出这个core gene set. 首先要考虑样本的选择,样本里是否存在明显的分层 ...
- Codeforces 758F Geometrical Progression
Geometrical Progression n == 1的时候答案为区间长度, n == 2的时候每两个数字都可能成为答案, 我们只需要考虑 n == 3的情况, 我们可以枚举公差, 其分子分母都 ...
- The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)
The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE) Today I tried ...
随机推荐
- Hibernate基础学习(七)—检索方式
一.概述 Hibernate有五种检索方式. 1.导航对象图检索方式 根据已经加载的对象,导航到其他对象. Order order = (Order)session.get(Ord ...
- Liferay中利用URL传参数
业务场景:现在有一个新闻系统,有两个页面,A是新闻列表页面/web/guest/home,B是新闻的详情页面/web/guest/newsview. 业务逻辑为:在A页面中,点击新闻的标题进入B页面, ...
- app专项测试自动化测试方法思路与实现
秉着个人意愿打算把python+rf接口自动进行彻底结束再做些其它方面的输出~但事与愿违,但领导目前注重先把专项测试方面完成,借此,先暂停python+rf(主要是与Jenkins集成+导入DB+微信 ...
- java 客户端发起http请求
package com.mall.core.utils.http; import org.apache.commons.lang.StringUtils; import org.apache.http ...
- tmux鼠标配置出现错误unknown option: mode-mouse
setw -g mode-mouse on set -g mouse-select-pane on set -g mouse-resize-pane on set -g mouse-select-wi ...
- .NET Core中文分词组件jieba.NET Core
.NET Core中文分词组件jieba.NET Core,由于实际的一些需求,需要做中文分词. 找到了一个jieba.NET https://github.com/anderscui/jieba.N ...
- knockout中viewmodel跟子model相互调用
用knockout写前端复杂js逻辑的确很方便,而且html界面也很清爽. 在ko中对于复杂的业务逻辑我会给viewmodel创建一些子model对象,但是viewmodel跟子model怎样相互调用 ...
- Swift try try! try?使用和区别
Swift try try! try?使用和区别 一.异常处理try catch的使用 1. swift异常处理 历史由来 Swift1.0版本 Cocoa Touch 的 NSError ,Swif ...
- vscode同步设置&扩展插件
首先安装同步插件: Settings Sync 第二部进入你的github如图: 打开设置选项: 新建一个token: 如图: 记住这个token值 转到vscode 按shift+alt +u ...
- python自动化测试应用-番外篇--接口测试1
篇1 book-python-auto-test-番外篇--接口测试1 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),至今<安卓a ...