1. 程式人生 > >poj1952求最長遞減子序列和最長子序列的種數

poj1952求最長遞減子序列和最長子序列的種數

求最長遞減子序列和最長子序列的種數。難在求種數

不需要使用高精度或者int64

BUY LOW, BUY LOWER
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5490 Accepted: 1848

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
#include <iostream>
#define N 5005
using namespace std;
int a[N];
int dp[N];
int times[N];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            dp[i]=times[i]=1;
        }
        for(int i=1;i<=n;i++)//求以a[i]為終止時子序列的長度
        {
            for(int j=i-1;j>0;j--)
            {
                if(a[i]<a[j])
                {
                    if(dp[i]<dp[j]+1)
                    {
                        dp[i]=dp[j]+1;
                        times[i]=times[j];
                    }
                    else if(dp[i]==dp[j]+1)
                    {
                        times[i]+=times[j];
                    }
                }
                if(a[i]==a[j])
                {
                    if(dp[i]==1)//防止重複
                    //dp[i]==1,a[i],a[j]前面接的一樣,只能算作一次
                    times[i]=0;
                    break;
                }
            }
        }
            int len=0,t=0;
            for(int i=1;i<=n;i++)
                if(dp[i]>len)
                len=dp[i];
            for(int i=1;i<=n;i++)
                if(dp[i]==len)//統計次數
                t+=times[i];
              cout<<len<<" "<<t<<endl;
    }
}


相關推薦

重複 不重複串 思路

題目: 求任意一個字串中的所有最長重複字串和所有最長不重複子串 最長不重複子串的解法:        設定一個輔助資料結構(如map)記錄每個字元最後一次出現的位置;遍歷字串中的每個字元,如果在map

公共公共序列之Python實現

動態規劃(dynamic programming)是運籌學的一個分支,是求解決策過程(decision process)最優化的數學方法。簡單的理解為:是將一個棘手的問題,分成一個個小問題,先著手解決

動態規劃法之公共優二叉查詢樹

1. 筆試常考的題型,最長公共子串問題:給定兩個字串str1和str2,返回兩個字串的最長公共子串(連續)和長度。 舉例: str1 = "abc" str2="caba" 它們的最長公共子串是 "ab"。 此題可用暴力法進行求解,求解的時間複雜度較高。現用動態規劃法進

poj1952遞減序列長子序列種數

求最長遞減子序列和最長子序列的種數。難在求種數 不需要使用高精度或者int64 BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5490 Accepted:

算法 - 一個數組的遞減序列(C++)

str log bst article subst else from return ear //************************************************************************************

演算法 一個數組的遞減序列 C

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

一個數組的遞減序列比如{9,4,3,2,5,4,3,2}的遞減序列為{9,5, 4,3,2}

程式碼如下:<pre name="code" class="java"> public class Decrease { /** * @param PLA * */ /*演算法描述: * 用動態規劃解決此問題,設A為原陣列,另設陣列B(

計蒜客-陣列的遞減序列

給定一個整數序列,輸出它的最長遞減(注意不是“不遞增”)子序列。 輸入包括兩行,第一行包括一個正整數N(N<=1000),表示輸入的整數序列的長度。第二行包括用空格分隔開的N個整數,整數範圍區間為[-30000,30000]。 輸出為一行,最長遞減

一個數組的遞減序列 比如{9,4,3,2,5,4,3,2}的遞減序列為{9,5,4,3,2}

分析: 用動態規劃解決,dp[i]表示a[0..i]的最長遞減子序列,dp滿足: 對於任意k,  0<=k<i dp[i] = max{dp[k]+1, a[k]>a[i]} 如果對於任意 0<=k<i   a[k] <= a[i] dp

陣列的遞減序列

給定一個整數序列,輸出它的最長遞減(注意不是“不遞增”)子序列。 輸入包括兩行,第一行包括一個正整數N(N<=1000),表示輸入的整數序列的長度。第二行包括用空格分隔開的N個整數,整數範圍區間為[-30000,30000]。 輸出為一行,最長遞減子序

公共序列的Python實現,帶圖示。

code mage 數字 實現 max 記錄 子串和 abc 使用 使用矩陣來記錄兩個子串之間各個字符之間的對應關系。 最長子串:矩陣中數字最大的就是最長子串的長度。若對應位置字符相同,則c[i][j] = c[i-1][j-1] + 1 1 def longSu

HRBUST 2010【簡單dp+遞減序列

amp stdio.h 遞增 scan ace name 多少 scanf ring 題目:所謂二等隊形就是從大到小依次排列,即對於數列a,二等隊形為任意a【i】滿足:a【i】>a【i+1】。現在給出一個長度為n的數列,從中最少去除多少個數可使數列變成二等隊形數列。

hdoj1160:FatMouse's Speed(dp+遞減序列思想+陣列巧妙記錄輸出)

目錄 FatMouse's Speed 解題思路: ac程式碼: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276

poj1887(遞減序列)

      題意:題目很長,但內容很簡單,就是求一串數字的最長遞減子序列的長度。 程式碼: #include<stdio.h> int arr[10010]; int dp[10010]; int main() { int ncase=1; while

動態規劃--尋找遞減序列

昨天C++課上留了三道題,除了C語言本身外都涉及了一些演算法。其中第二個問題是這樣的: 攔截導彈 某國為了防禦敵國的導彈襲擊,開發出一種導彈攔截系統。但是這種導彈攔截系統有一個缺陷:雖然它的第一發炮彈能夠到達任意的高度,但是以後每一發炮彈都不能高於前一發的高度。某天,雷達

遞減序列[轉載]

Q:例如:有一個序列,例如 9 8 2 1 7 5 3 4 3 2 1.      求出最長的遞減子序列。如本例的結果就是:9 8 7 5 4 3 2 1。 分析:         可採用動態規劃的思想進行解答,時間複雜度為O(n^2).        設原陣列為a[1....n]。另設一陣列d[1....

遞增序列(兩種時間複雜度演算法及其區別)+遞減序列(reverse)

O(n*n)//LIS+路徑回溯 O(n*n) #include <iostream> #include<cstdio> #include<stack> #i

] 找工作知識儲備(2)---陣列字串那些經典演算法:大子序列遞增序列公共串,公共序列,字串編輯距離,不重複串,迴文

作者:寒小陽 時間:2013年9月。 0、前言         這一部分的內容原本是打算在之後的字串或者陣列專題裡面寫的,但看著目前火熱進行的各家網際網路公司筆試面試中,出現了其中的一兩個內容,就隨即將這些經典問題整理整理,單寫一

遞減序列--動態規劃

例如:有一個序列,例如 9 8 2 1 7 5 3 4 3 2 1.      求出最長的遞減子序列。如本例的結果就是:9 8 7 5 4 3 2 1。 分析:         可採用動態規劃的思想進行解答,時間複雜度為O(n^2).        設原陣列為a[1

UESTC-1006 上升序列(遞減序列做法+貪心策略)

題意:求數列中最長最小子序列,所謂最小類似於字典序最小。 思路:最長遞減子序列動態更新,令通過一個nex陣列貪心更新字典序最小序列。 Code: #include <string.h>