因为有重复的数,就让hi[]记录其值。go[]记录值对应的其中一个值的序列号。这样就把
具有相同值的数统一起来了。
昨天都是对的,今天发现自己提交的又被判成错误了,看来自己随便想的东东还是很容易有各种漏洞啊。
自己找了一下,也不知道有什么例子不对的。真不好意思
仅供小小的参考
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int go[1111111]={0};
int b[5000];
int main()
{
int i,j,k;
int n;
cin>>n;
int hi[5000];
for (i=0;i<n;i++)
{
cin>>hi[i];
go[hi[i]]=i;
}
memset(b,false,sizeof(b));
int dp[5000];
int ans=0;
for (i=0;i<n;i++)
{
int cur=i;
for (j=0;j<n;j++)
dp[j]=0,b[j]=-1;
for (j=i+1;j<n;j++)
{
//cout<<b[go[hi[j]]]<<' '<<cur<<endl;
if (hi[i]==hi[j])
{
cur=j;
}
else if (b[go[hi[j]]]<cur)
{
//cout<<i<<j<<"FUck";
b[go[hi[j]]]=j;
dp[go[hi[j]]]+=2;
//cout<<hi[j]<<' '<<dp[go[hi[j]]]<<endl;
}
}
for (j=0;j<n;j++)
{
if (cur>b[go[hi[j]]] && !(dp[go[hi[j]]]&1))
dp[go[hi[j]]]++;
//cout<<j<<' '<<b[go[hi[j]]]<<endl;
ans=max(ans,dp[go[hi[j]]]);
//cout<<" "<<ans<<" ";
}
}
cout<<ans<<endl;
return 0;
}
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is analmost arithmetical progression, if its elements can be represented as:
- a1 = p, wherep is some integer;
- ai = ai - 1 + ( - 1)i + 1·q(i > 1), whereq is some integer.
Right now Gena has a piece of paper with sequence b, consisting ofn integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.
Sequence s1, s2, ..., sk is a subsequence of sequenceb1, b2, ..., bn, if there is such increasing sequence of indexesi1, i2, ..., ik(1 ≤ i1 < i2 < ... < ik ≤ n), thatbij = sj. In other words, sequences can be obtained fromb by crossing out some elements.
The first line contains integer n (1 ≤ n ≤ 4000). The next line contains n integersb1, b2, ..., bn(1 ≤ bi ≤ 106).
Print a single integer — the length of the required longest subsequence.
2 3 5
2
4 10 20 10 30
3
In the first test the sequence actually is the suitable subsequence.
In the second test the following subsequence fits: 10, 20, 10.