DP+贪心哪里有问题?
查看原帖
DP+贪心哪里有问题?
153590
bolerat楼主2020/10/4 10:11
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int h[100001],f[100001],w[100001];
int t=0,p;
int n=1;
int ans=0;

int main(){
	while(cin>>h[n])n++;
	n--;
	for(int i=1;i<=n;i++){
		f[i]=1;
		for(int j=1;j<i;j++){
			if(h[i]<=h[j]&&f[i]<f[j]+1)f[i]=f[j]+1;
		}
		ans=max(ans,f[i]);
	}
	w[t]=h[0];
	for(int i=1;i<n;i++){
		p=0;
		for(int j=1;j<=t;j++){
			if(w[j]>=h[i]){
				if(p==0)p=j;
				if(w[p]>w[j])p=j;//择优 
			}
		}
		if(p==0){
			t++;
			w[t]=h[i];
		}
		else {
			w[p]=h[i];
		} 
	}
	cout<<ans<<endl;
	cout<<t;
	return 0;
}
2020/10/4 10:11
加载中...