POJ 1952 BUY LOW, BUY LOWER DP

BUY LOW , BUY LOWER
Time Limit: 1000MSMemory Limit: 30000KB

先来看看description:

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. 


看到这儿之后好像就是个简单的DP。博主当时看到这儿之后就直接去码代码了,随后WA掉。
这题很坑的,要看Output的说明:

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

大致思路:在用正常方法求最大序列的时候,要想办法判断是否有重复。和求最长序列一样,这一操作仍然使用一个数组tim[]来进行动态规划。tim[i]表示从i开始有几种最长序列。具体实现方法看代码里面的注释。把最长的那一段注释看懂,这道题就没有什么问题了。

这题的思路很巧妙,值得借鉴。如果代码搞不明白,可以自己写几个数据推一下。

代码是从右向左进行搜索的,从左往右道理相同。

792KB  94MS
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=5050;

int a[inf];
int len[inf];			//len[i]表示从i开始的最大长度,
long long tim[inf];		//tim[i]表示从i开始有几种方法 
int n,maxn=-1,tot=0;

int main(){
	cin>>n;
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	
	for (int i=1;i<=n;i++) len[i]=1,tim[i]=1;	//初始化 
	
	for (int i=n;i;i--){						//从右向左依次搜索 
		
		for (int j=i+1;j<=n;j++){				//从当前点向右逐个讨论 
			
			if (a[i] > a[j]){					//满足递减
			
				if (len[i] < len[j]+1){			//接上j的话可以变长
					len[i]=len[j]+1;
					tim[i]=tim[j];				//这时i只接了j,故这一步成立 
				} 
				
				else if (len[i] == len[j]+1)	//i接上j和i已有的接法一样长 
					tim[i]+=tim[j];				//这一步一定成立。有重复的情况在下文会排除 				
			}			
			
			else if (a[i]==a[j]){		//出现了重复
				if (len[i]==1)			/*重要:由于j的坐标是从i开始向右,所以len[i]=1意味着i在遇到与它重复的j之前没有能接上任何点;
										 那么在i左边的点z接上i时,z一定会与稍后要接上的j重复。
										 综上,要将i忽略不计。这里照应了上文的“tim[i]+=tim[j];”*/ 
										 
					tim[i]=0;			//避免之后重复讨论 
				break;			//不再讨论i点					
			}
		}		
	}
	
	for(int i=1;i<=n;i++) maxn=max(len[i],maxn);
	
    for(int i=1;i<=n;i++)
    	if(len[i]==maxn)
         	tot+=tim[i];
         	
    cout<<maxn<<' '<<tot<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值