注意:这里要求值不同。
先去重,去掉连续一段相同的数字,只保留一个,保存在数组 a[] 里,并用数组 c[i] 表示a[i] 在原始数据中连续出现的个数。
用 f[i] 表示数组 a[] 中以a[i] 为结尾的最长上升子序列的长度是多少;用g[i] 表示数组a[]中以a[i]为结尾的最长上升子序列的个数。
在处理 f[i] 的时候,如果满足 j<i, a[j]<a[i], f[j]+1和当前最大值相等,那么g[i] += g[j]*c[i],如果满足j<i, a[j]<a[i], f[j]+1大于当前最大值,那么g[i] = g[j]*c[i]。然后把所有与最长上升子序列长度相等的g[i]累加起来就可以了。
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
int tot = 0;
int last = -1;
int a[1010] = {0};
int c[1010] = {0};
cin>>n;
n++;
a[1] = (1<<31);c[1] = 1; tot = 1;
for (int i=2; i<=n; i++) //输入,同时去重
{
int x;
cin>>x;
if (x != last)
{
a[++tot] = x;
c[tot] = 1;
last = x;
}
else
{
c[tot]++;
}
}
int f[1010] = {0};
int g[1010] = {0};
int pos = 0;
int Max = 0;
int ans = 0;
int s = 0;
f[1] = 1; g[1] = 1;
for (int i=2; i<=n; i++)
{
pos = i;
Max = 0;
for (int j=1; j<i; j++)
if (a[j] < a[i] && f[j]+1 >= Max)
{
if (f[j]+1 == Max)
{
g[i] += g[j]*c[i];
}
if (f[j]+1 > Max)
{
Max = f[j]+1;
g[i] = g[j]*c[i];
}
}
f[i] = Max;
if (f[i] > ans)
{
ans = f[i];
s = g[i];
}
else if (f[i] == ans)
{
s += g[i];
}
}
cout<<s<<endl;
}
return 0;
}