poj 1952 BUY LOW, BUY LOWER (最長遞減子序列+不同子序列計數)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   strong   

BUY LOW, BUY LOWER
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8327   Accepted: 2888

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


題意:

最長遞減子序列+最長的不同子序列計數。


思路:(貼別人的)

注意:重複的只算一次
如何去掉一些重複的是本題的關鍵
去重思路:
7
5 3 7 6 3 2 1
6
5 3 7 3 1
5
5 3 2 1 3
第一組在推到 數字 2 的時候有 3會出現重複, 顯然前面一個3是可有可無的。
第二組也一樣,前面一個3是可有可無的。

1.如果最長下降序列中有後面一個3, 如第一二組資料,那麼前面一個3是無用的,在推好後面一個3之後把之前的所用重複的計數數組清零

2.如果最長下降序列中只有前面一個3,如第三組資料, 做情況1的操作也是不會影響結果的,因為第三組的前面一個3的狀態已經推到2後才被清零的。


代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 35#define MAXN 100005#define mod 100000000#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,ans,cnt,tot,k;int dp[5005],num[5005],a[5005];int main(){    int i,j,t;    while(~scanf("%d",&n))    {        for(i=1; i<=n; i++)        {            scanf("%d",&a[i]);            a[i]=-a[i];        }        ans=0;        for(i=1;i<=n;i++)        {            t=0;            for(j=1;j<i;j++)            {                if(a[i]>a[j])                {                    if(t<dp[j])                    {                        t=dp[j];                        num[i]=num[j];                    }                    else if(t==dp[j])                    {                        num[i]+=num[j];                    }                }                else if(a[i]==a[j])                {                    num[j]=0;                }            }            if(t==0) num[i]=1;            dp[i]=t+1;            if(ans<dp[i]) ans=dp[i];        }        int res=0;        for(i=1;i<=n;i++)        {            if(dp[i]==ans) res+=num[i];        }        printf("%d %d\n",ans,res);    }    return 0;}/*44 1 3 1352 52 5164 3 4 1 3 142 2 2 2*/



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.