[POJ 1952] BUY LOW,BUY LOWER

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 1952] BUY LOW,BUY LOWER相关的知识,希望对你有一定的参考价值。

[题目链接]

         http://poj.org/problem?id=1952

[算法]

        DP

[代码]

        

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 5010

int i,j,n,ans1,ans2;
int a[MAXN],f[MAXN],g[MAXN];

int main() 
{
        
        scanf("%d",&n);
        for (i = 1; i <= n; i++) 
        {
                scanf("%d",&a[i]);
                f[i] = g[i] = 1;
        }
        for (i = 1; i <= n; i++)
        {
                for (j = i - 1; j >= 1; j--)
                {
                        if (a[j] > a[i])
                        {
                                if (f[j] + 1 > f[i])
                                {
                                        f[i] = f[j] + 1;
                                        g[i] = g[j];    
                                }    else if (f[j] + 1 == f[i]) g[i] += g[j];
                        }    else if (a[i] == a[j])
                        {
                                if (f[i] == 1)
                                        g[i] = 0;
                                break;
                        }
                }    
        }
        for (i = 1; i <= n; i++)
        {
                if (f[i] > ans1)
                {
                        ans1 = f[i];
                        ans2 = g[i];    
                }    else if (f[i] == ans1) ans2 += g[i];
        }
        printf("%d %d
",ans1,ans2);
        
        return 0;
    
}

 

以上是关于[POJ 1952] BUY LOW,BUY LOWER的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1952 -- BUY LOW, BUY LOWER

POJ 1952 BUY LOW, BUY LOWER

[poj 1952]buy low,by lower

dpPKU 1952 buy low,buy lower

BUY LOW, BUY LOWER_最长下降子序列

usaco 4.3 Buy Low, Buy Lower