码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P1108 低价购买

时间:2018-02-10 21:54:50      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:class   post   out   std   题目   ++i   链接   cpp   code   

题目链接

luogu P1108 低价购买

题解

朴素n^2dp,对于方案的转移利用加法原理,dp每一种最有解的方案

代码

#include<iostream>  
#include<cstdio>  
#include<algorithm>  
#include<cmath>  
using namespace std;  
const int maxn = 60007;
int f[maxn],c[maxn],a[maxn];  
int cnt=0;  
int n;
int main() {  
    scanf("%d",&n);  
    int i,j;  
    for(i=1;i<=n;i++)
        scanf("%d",a+i);  
    int ans=0;
    for(int i=1;i<=n;++i) {
        f[i]=1;
        for(int j=1;j<i;++j) {
            if(a[i]<a[j])
                f[i]=max(f[i],f[j]+1);
            ans=std::max(ans,f[i]);
        }
    }
    for(int i=1;i<=n;++i) {
        if(f[i]==1)c[i]=1;
        for(int j=1;j<i;++j) {
            if(f[i]==f[j]&&a[i]==a[j])c[j]=0;
            else if(f[j]+1==f[i]&&a[i]<a[j])c[i]+=c[j];
        }
    }
    int cnt=0;
    for(int i=1;i<=n;++i) {
        if(f[i]==ans)cnt+=c[i];
    }
    std::cout<<ans<<" "<<cnt<<std::endl;
    return 0;  
}  
/*
12
68 69 54 64 68 64 70 67 78 62
98 87
 */ 

luogu P1108 低价购买

标签:class   post   out   std   题目   ++i   链接   cpp   code   

原文地址:https://www.cnblogs.com/sssy/p/8439763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!