zoukankan      html  css  js  c++  java
  • [POJ1952] BUY LOW,BUY LOWER

    问题描述

    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
    

    输入格式

    • 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.

    输出格式

    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.

    样例输入

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

    样例输出

    4 2

    题目大意

    求一个序列中的最长下降子序列长度并且输出方案种数。如果两个最长下降子序列的每个数字都相同则算同一种方案。

    题解

    首先只要把原序列倒过来就可以转化为求最长上升子序列了。第一问可以直接用动态规划求解,下面着重讨论第二问。

    首先不考虑重复的情况。先按照同样的框架写一遍LIS,把以i结尾的最长上升子序列的长度(f[i])求出来。如果对两个元素i、j(j<i),有(f[j]+1=f[i]),设构成以i结尾、长度为(f[i])的上升子序列的方案数为(g[i]),则有(g[i]+=g[j])。但如果考虑重复情况,这样是有可能计算重复的。还是假设两点i、j(j<i),且(a[i]=a[j],f[i]=f[j]),那么在j计算过的方案在i中也一定计算过。所以可以让(g[i])减去(g[j]),就可以得到不重复的方案数了。最后答案即为所有最长上升子序列的结尾点的方案总和。

    代码

    #include <iostream>
    #include <cstdio>
    #define N 5002
    using namespace std;
    int n,i,j,f[N],g[N],a[N],sum,ans;
    int find(int x,int y)
    {
    	if(y==0) return 1;
    	int ans=0;
    	for(int i=x-1;i>=1;i--){
    		if(a[i]<a[x]&&f[i]==y) ans+=find(i,y-1);
    	}
    	return ans;
    }
    int main()
    {
    	cin>>n;
    	for(i=n;i>=1;i--){
    		cin>>a[i];
    		f[i]=1;
    	}
    	for(i=1;i<=n;i++){
    		for(j=1;j<i;j++){
    			if(a[j]<a[i]) f[i]=max(f[i],f[j]+1);
    		}
    	}
    	for(i=1;i<=n;i++) ans=max(ans,f[i]);
    	cout<<ans<<' ';
    	for(i=1;i<=n;i++){
    		if(f[i]==1) g[i]=1;
    	}
    	for(i=1;i<=n;i++){
    		for(j=1;j<i;j++){
    			if(a[j]<a[i]){
    				if(f[j]+1==f[i]) g[i]+=g[j];
    			}
    		}
    		for(j=1;j<i;j++){
    			if(a[j]==a[i]&&f[i]==f[j]) g[i]-=g[j];
    		}
    	}
    	for(i=1;i<=n;i++){
    		if(f[i]==ans) sum+=g[i];
    	}
    	cout<<sum<<endl;
    	return 0;
    }
    
  • 相关阅读:
    ADB 操作手机的粘贴板
    JSESSIONID的简单说明
    Android无线调试出现错误的解决方法
    adb shell input text 完美支持中文输入
    SQL Server 2022来了
    新技能GET!在前端表格中花式使用异步函数的奥义
    地表最强IDE ——Visual Studio 2022正式发布
    用前端表格技术构建医疗SaaS 解决方案
    JavaScript Sanitizer API:原生WEB安全API出现啦
    突破技术限制,实现Web端静默打印
  • 原文地址:https://www.cnblogs.com/LSlzf/p/10642376.html
Copyright © 2011-2022 走看看