zoukankan      html  css  js  c++  java
  • 动态规划|蒜头君跳木桩-最长下降子序列

    蒜头君跳木桩

    蒜头君面前有一排 nn 个木桩,木桩的高度分别是h_1,h_2,h_3cdots h_nh1​,h2​,h3​⋯hn​。蒜头第一步可以跳到任意一个木桩,接下来的每一步蒜头不能往回跳只能往前跳,并且跳下一个木桩的高度 不大于 当前木桩。蒜头君希望能踩到尽量多的木桩,请你帮蒜头计算,最多能踩到多少个木桩。

    输入格式
    第一行输入一个整数 nn 代表木桩个数。第二行输入 nn 个整数 h_1,h_2,h_3cdots h_nh1​,h2​,h3​⋯hn​,分别代表 nn 个木桩的高度。(1 leq n leq 1000,1 leq h_i leq 1000001≤n≤1000,1≤hi​≤100000)

    输出格式
    输出一个整数,代表最多能踩到的木桩个数,占一行。

    样例输入
    6
    3 6 4 1 4 2
    样例输出
    4

    思路:最长下降子序列

    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    /*最长下降子序列*/
    
    const int inf = 0x3f3f3f3f;
    const int MAX_N = 1010;
    int n;
    int a[MAX_N];
    int dp[MAX_N];
    int ans = -inf;
    
    int main(){
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>a[i];
    	}
    	
    	for(int i=1;i<=n;i++){
    		dp[i] = 1;
    		for(int j=1;j<i;j++){
    			if(a[j]>=a[i]){
    				dp[i] = max(dp[i],dp[j] + 1);
    			}
    		}
    		ans = max(ans,dp[i]);
    	}
    	if(ans == -inf){
    		cout<<0<<endl;
    	}else{
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    codeforces C. No to Palindromes!
    codeforces D. Pashmak and Parmida's problem
    codeforces C. Little Pony and Expected Maximum
    codeforces D. Count Good Substrings
    codeforces C. Jzzhu and Chocolate
    codeforces C. DZY Loves Sequences
    codeforces D. Multiplication Table
    codeforces C. Painting Fence
    hdu 5067 Harry And Dig Machine
    POJ 1159 Palindrome
  • 原文地址:https://www.cnblogs.com/fisherss/p/10316392.html
Copyright © 2011-2022 走看看