参考:https://blog.csdn.net/u012655441/article/details/64920825
https://blog.csdn.net/wy19910326/article/details/7232308
https://blog.csdn.net/weizhuwyzc000/article/details/45823031
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 #include <algorithm> 6 using namespace std; 7 const int N=1e3+10; 8 int dp[N]; 9 struct node 10 { 11 int w,s,n; 12 }m[N]; 13 int cmp(struct node x,struct node y) 14 { 15 if (x.w==y.w) 16 { 17 return x.s>y.s; 18 } 19 else 20 { 21 return x.w<y.w;//注意排序要求! 22 } 23 } 24 int main() 25 { 26 int w,s,i=1; 27 // freopen("bin.txt","r",stdin); 28 while (cin>>w>>s) 29 { 30 m[i].w=w,m[i].s=s,m[i].n=i; 31 dp[i]=1; 32 i++; 33 sort(m+1,m+i,cmp); 34 } 35 int maxn,maxd=1; 36 for (int j=2;j<i;j++)//更新dp数组 37 { 38 for (int k=1;k<j;k++) 39 { 40 if (m[k].w<m[j].w&&m[k].s>m[j].s) 41 { 42 dp[j]=max(dp[j],dp[k]+1); 43 if (dp[j]>maxd) 44 { 45 maxd=dp[j]; 46 maxn=j; 47 } 48 } 49 } 50 } 51 stack<int> st; 52 st.push(m[maxn].n); 53 for (int j=maxn-1;j>=1;j--)//用栈存路径 54 { 55 if (j<maxn&&dp[j]==dp[maxn]-1) 56 { 57 st.push(m[j].n); 58 maxn=j; 59 } 60 } 61 cout<<st.size()<<endl; 62 while (!st.empty()) 63 { 64 cout<<st.top()<<endl; 65 st.pop(); 66 } 67 68 return 0; 69 }