[HDU 1160 DP|sort &LIS] J - FatMouse‘s Speed

这题坑点在于对于dp数组的初始化~,切记不能再犯同样的错误了!

#include<bits/stdc++.h>
#define debug(x) cout<<#x<<" is "<<x<<endl
#define pb push_back
#define se second
#define fi first
#define mp make_pair
using namespace std;
const int N = 1e3 + 5;
struct mice{
    
    
	int w,s,id;
	mice(int _w,int _s,int _id):w(_w),s(_s),id(_id){
    
    
	}
	bool operator<(mice & m){
    
    
		if(s != m.s)return s > m.s;
		return w< m.w;
	}
};
vector<mice> v;
int pre[N],dp[N];
void show(int index){
    
    
	if(index == -1)return;
	show(pre[index]);
	cout<<v[index].id + 1<<endl;
}
int main(){
    
    
	//freopen("input.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	memset(pre,-1,sizeof(pre));
	int w,s,n = 0,ans = 1,max_index = 0;
	while(scanf("%d%d",&w,&s) != EOF){
    
    
		v.pb(mice(w,s,n++));
	}
	sort(v.begin(),v.end());
	for(int i = 0;i < n;i++)dp[i] = 1;//若只令dp[0] = 1会报错,因为上升序列的开始并不一定为dp[0]
	for(int i = 0;i < n;i++){
    
    
		for(int j = 0;j < i;j++){
    
    
			if(v[i].s < v[j].s && v[i].w > v[j].w && (dp[j] + 1 ) > dp[i]){
    
    
				pre[i] = j;
				dp[i] = dp[j] + 1;
				if(dp[i] > ans){
    
    
					ans = dp[i];
					max_index = i;
				}
			}
		}
	}
	cout<<ans<<endl;
	show(max_index);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_20252251/article/details/108371498