POJ1952总结

BUY LOW, BUY LOWER

题目来源

Description

The advice to “buy low” is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems’ advice:

                    "Buy low; buy lower"

Each time you buy a stock, you must purchase it at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices.

You will be given the daily selling prices of a stock (positive 16-bit integers) over a period of time. You can choose to buy stock on any of the days. Each time you choose to buy, the price must be strictly lower than the previous time you bought stock. Write a program which identifies which days you should buy stock in order to maximize the number of times you buy.

Here is a list of stock prices:

 Day   1  2  3  4  5  6  7  8  9 10 11 12

Price 68 69 54 64 68 64 70 67 78 62 98 87

The best investor (by this problem, anyway) can buy at most four times if each purchase is lower then the previous purchase. One four day sequence (there might be others) of acceptable buys is:

Day    2  5  6 10

Price 69 68 64 62

Input

* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given

* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.

Output

Two integers on a single line:
* The length of the longest sequence of decreasing prices
* The number of sequences that have this length (guaranteed to fit in 31 bits)

In counting the number of solutions, two potential solutions are considered the same (and would only count as one solution) if they repeat the same string of decreasing prices, that is, if they “look the same” when the successive prices are compared. Thus, two different sequence of “buy” days could produce the same string of decreasing prices and be counted as only a single solution.

Sample Input

12
68 69 54 64 68 64 70 67 78 62
98 87

Sample Output

4 2

我的代码

这题依旧是求最长下降子序列,但是要记录路径;不仅要记录路径,还要计数;不仅要计数,还要去重。

原本记录路径时只要记一个前驱的结点就行了,现在要计数,就要把所有前去结点记载下来。但是现在有一个问题是要去重,如果一个个列下来,那么可能最多有2的32次方减一个,所以枚举肯定不可以。仔细思索一番发现,如果有两个前驱结点的数字相同,只需要记录后一个结点就可以了,因为前一个结点的可能情况后一个结点肯定能达到。

因此计数只需要不断回溯路径,直到出口为止,用一个深搜就可以解决。但是深搜会超时,还需要用上记忆化搜索。

#include<stdio.h>

int dp[5010];
int stocks[5010];
int dfsdp[5010]={0};
int base;

struct node
{
	int num;
	int pre[200];
	node(){
		num=0;
	}
}pres[5010],last;

//记忆化搜索 
int dfs(int now)
{
	if (pres[now].num==0) 
	{
		dfsdp[now]=1;
	}
	else
	{
		for (int i=0;i<pres[now].num;i++)
		{
			if (dfsdp[pres[now].pre[i]]!=0) dfsdp[now]+=dfsdp[pres[now].pre[i]];
			else dfsdp[now]+=dfs(pres[now].pre[i]);
		}
	}		
	return dfsdp[now];
}

int main()
{
	int n,max=-1,cnt=0;
	scanf("%d",&n);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&stocks[i]);
	}
	for (int i=0;i<n;i++)
	{
		dp[i]=1;
		for (int j=0;j<i;j++)
		{
			if (stocks[j]>stocks[i])
			{
				if (dp[j]+1>dp[i])
				{
					pres[i].num=0;
					pres[i].pre[pres[i].num++]=j;
					dp[i]=dp[j]+1;
				}
				else if(dp[j]+1==dp[i])
				{
					int flag=0;
					for (int k=0;k<pres[i].num;k++)
					{
						if (stocks[pres[i].pre[k]]==stocks[j])
						{
							pres[i].pre[k]=j;
							flag=1;
							break;
						}
					}
					if (flag==0) pres[i].pre[pres[i].num++]=j;
				}
			}
		}
		if (dp[i]>max)
		{
			max=dp[i];
			last.num=0;
			last.pre[last.num++]=i;
		}
		else if (dp[i]==max)
		{
			int flag=0;
			for (int k=0;k<last.num;k++)
			{
				if (stocks[last.pre[k]]==stocks[i])
				{
					last.pre[k]=i;
					flag=1;break;
				}
			}
			if (flag==0) last.pre[last.num++]=i;
		}
	}
	for (int i=0;i<last.num;i++)
	{
		cnt+=dfs(last.pre[i]);
	}
	printf("%d %d",max,cnt);
	return 0;
}

反思

终于不是模板题了。

发表评论