input:
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
output:
4
4
5
9
7
题目大意:
有cnt组数据,每组有体重和速度两个值,求其最长上升子序列并打印出他的路径。
分析:
LIS。先将数组按照体重从小到大,速度从大到小排序,之后就是LIS。在判断dp[i]<dp[j]+1是并记录下路径。
code:
#define frp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 1000;
const int MAXN = 10000;
int dp[MAXN];
int pre[MAXN];
struct node{
int weight,speed,num;
bool operator <(const node &a)const{
return weight==a.weight?speed>a.speed:weight<a.weight;
}
void pf(){
cout<<this->weight<<" "<<this->speed<<endl;
}
}mice[MAXN];
void print(int x){
if(x==-1)return;
print(pre[x]);
cout<<mice[x].num<<endl;
}
void solve() {
int speed,weight,cnt=0;
while(cin>>weight>>speed){
mice[cnt++]={weight,speed,cnt};
}
sort(mice,mice+cnt);
memset(pre,-1,sizeof(pre));
int ans=-1,last;
for(int i=0;i<cnt;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(mice[i].speed<mice[j].speed&&mice[i].weight>mice[j].weight&&dp[i]<dp[j]+1){
pre[i]=j;
dp[i]=dp[j]+1;
}
}
// cout<<dp[i]<<endl;
if(ans<dp[i]){
ans=dp[i];
last=i;
}
}
cout<<ans<<endl;
print(last);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef frp
freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
// freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
#endif
solve();
return 0;
}