[Question solution] Luogu P1108 low price purchase

Go to: My own blog

topic

Luogu P1108 low price purchase

answer

The key to this question is to calculate the number of plans. If the longest sequence length of the i-th position is updated from the j-th position, the method for updating the number of plans is the same. As long as the number is the same, the duplicate information in one position is cleared.

Code

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
int n;
int a[maxn],f[maxn],cnt[maxn]; 
//f[i]表示以a[i]结尾的单调数列的最长长度,cnt[i]表示以a[i]结尾的长度为f[i]的数列的个数 
inline void dp()
{
	int ans1=0;
	for(int i=1;i<=n;i++)
	{
		f[i]=1; //初始化 
		for(int j=1;j<i;j++)
			if(a[i]<a[j]) f[i]=max(f[i],f[j]+1);
		ans1=max(ans1,f[i]);
	}
	printf("%d ",ans1);
	for(int i=1;i<=n;i++)
	{
		if(f[i]==1) cnt[i]=1; //初始化 
		for(int j=1;j<i;j++) 
		{
			if(a[i]==a[j]) cnt[i]=0; //去重 
			cnt[i]+=cnt[j]*(f[i]==f[j]+1&&a[i]<a[j]);
		}
	}
	int ans2=0; for(int i=1;i<=n;i++) ans2+=cnt[i]*(f[i]==ans1);
	printf("%d\n",ans2);
}

int main()
{
	scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	dp();
	
	return 0;
}

Guess you like

Origin blog.csdn.net/zjgmartin/article/details/108561291