zoukankan      html  css  js  c++  java
  • PAT 顶级 1017 The Best Peak Shape (35分)(最长上升子序列)

    题目链接:

    1017 The Best Peak Shape (35分)

    思路:

    1.LIS(Longest Increasing Subsequence)是动态规划里的一个基本类型,我们需要掌握它的O(nlogn)O(nlog n)的算法;(不会的朋友自行学习~)
    2.其基本操作就是每次二分dp数组,往里面写值,如果a[i]被写到dp[k],就说明以这个值为结尾的LIS最长是k+1
    3.我们则需要计算出对于每个点,以它为末尾的正向LIS和反向LIS的长度为多少即可;

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10005;
    const int INF=1<<30;
    int n,a[maxn],dp[maxn],l[maxn],r[maxn];
    void solve(){
    	fill(dp,dp+n,INF);
    	for(int i=0;i<n;i++){
    		l[i]=lower_bound(dp,dp+n,a[i])-dp;
    		dp[l[i]]=a[i];
    	}
    	fill(dp,dp+n,INF);
    	for(int i=n-1;i>=0;i--){
    		r[i]=lower_bound(dp,dp+n,a[i])-dp;
    		dp[r[i]]=a[i];
    	}
    	int ans=0,index,diff=INF;
    	for(int i=0;i<n;i++) if(l[i]&&r[i]){
    			int num=l[i]+r[i]+1;
    			if(num>ans||(num==ans&&abs(l[i]-r[i])<diff)) ans=num,index=i;
    		}
    	if(ans) cout<<ans<<' '<<index+1<<' '<<a[index];
    	else cout<<"No peak shape";
    }
    int main(){
    	cin>>n;
    	for(int i=0;i<n;i++) cin>>a[i];
    	solve();
    	return 0;
    }
    
  • 相关阅读:
    基于Diff机制的多个状态合并
    do_mmap解读
    Linux对用户态的动态内存管理
    我的WordPress站点
    使用Bochs学习硬件原理
    inode的若干锚
    Use sed and awk to prettify json
    IO完成端口
    如何使用iText制作中文PDF
    Font and PDF
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308725.html
Copyright © 2011-2022 走看看