pku 1952 BUY LOW, BUY LOWER(DP)

来源:互联网 发布:大魔王名器淘宝旗舰店 编辑:程序博客网 时间:2024/04/30 07:00
BUY LOW, BUY LOWER
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 7483 Accepted: 2609

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 12Price 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 10Price 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

1268 69 54 64 68 64 70 67 78 6298 87

Sample Output

4 2

Source

USACO 2002 February


题意:求最长下降子序列和该长度的子序列的数目

题解:求最长下降子序列好简单啊,有木有,求该长度的子序列的数目好难啊,有木有,不会做啊,囧么办,看别人代码啦,blabla,别人方法挺巧妙的啊,有木有,为了美观还竟然将2个都合为一体来写啊。我就随便解释一下就算啦,dp数组是求最长下降子序列的,这个无视吧。cou数组是求出现次数的啊,由于不能有相同的元素啊对不对,所以第二重循环扫描的时候顺便扫描相同的元素啦,你看是不是,a【i】回扫到a【j】之间啊,明显如果a【i】==a【j】而且a【i】又不能合j+1~i之间的元素组成的下降子序列的话,明显a【i】这个元素就废了啊,所以这时候cou【i】就等于0吧。再说一下怎么刷新cou的长度吧,好不好,如果dp【j】+1==dp【i】&&a【i】大于a【j】的话,明显这也是一个以a【i】结尾的dp【i】这么这么长下降子序列对不对,所以就cou【i】+=cou【j】吧,如果dp【j】+1大于dp【i】,哇哈哈哈,那么dp【i】=dp【j】+1吧。。。真不好意思,倒着说了,不过这说明我真的懂了- - .....看着办吧


#include<stdio.h>#include<string.h>int cou[5005],dp[5005],c[5005];int main(){    int n,i,j,sum1,sum2;    while(scanf("%d",&n)>0)    {        for(i=0;i<n;i++) scanf("%d",c+i);        for(i=0;i<n;i++) dp[i]=cou[i]=1;        for(i=0;i<n;i++)        {            for(j=i-1;j>=0;j--)            {                if(c[j]>c[i])                {                    if(dp[j]+1>dp[i])                    {                        dp[i]=dp[j]+1;                        cou[i]=cou[j];                    }                    else if(dp[j]+1==dp[i]) cou[i]+=cou[j];                }                else if(c[j]==c[i])                {                    if(dp[i]==1) cou[i]=0;                    break;                }            }        }        for(sum1=i=0;i<n;i++)            if(dp[i]>sum1) sum1=dp[i];        for(sum2=i=0;i<n;i++)            if(dp[i]==sum1) sum2+=cou[i];        printf("%d %d\n",sum1,sum2);    }    return 0;}

原创粉丝点击