本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15585466.html

题目链接

首先第一问很好求,就是求最长下降子序列,n5000n\leqslant 5000O(n2)O(n^2) 暴力转移就行。

而这道题的难点就在于去重。

对于 iijji>ji>j),如果 ai=aja_i=a_jdpi=dpjdp_i=dp_j,说明他们是相同的,ii 的方案要清0,但是这里不能break

因为对于 kk 满足 j<k<ij<k<i,我们 ii 的方案也可能从 kk 转移过了。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Problem: P1108 低价购买
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1108
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 5010
int n, m, i, j, k;
int dp[N], s[N], a[N];
int cnt, ans;

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read();
for(i=1; i<=n; ++i)
{
a[i]=read();
dp[i]=1;
for(j=1; j<i; ++j)
if(a[i]<a[j])
dp[i]=max(dp[i], dp[j]+1);
if(dp[i]==1) s[i]=1;
for(j=1; j<i; ++j)
{
if(dp[j]==dp[i]&&a[i]==a[j]) s[i]=0;
else if(a[i]<a[j]&&dp[j]+1==dp[i]) s[i]+=s[j];
}
cnt=max(cnt, dp[i]);
// printf("%lld:%lld %lld\n", i, dp[i], s[i]);
}
for(i=1; i<=n; ++i)
if(dp[i]==cnt) ans+=s[i];
printf("%lld %lld\n", cnt, ans);
return 0;
}