https://vjudge.net/problem/UVA-10131
直接暴力N^2dp就好了,最后找一下路径输出,很经典的DAG题目。
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<vector> 6 #include<stack> 7 using namespace std; 8 #define inf 0x3f3f3f3f 9 struct node{int w,s,u;}P[1005]; 10 bool cmp(node A,node B){return A.w<B.w;} 11 int f[1005]; 12 int main() 13 { 14 int N=1,M,i,j,k; 15 while(cin>>P[N].w){cin>>P[N].s;P[N].u=N;N++;}N--; 16 sort(P+1,P+1+N,cmp); 17 memset(f,0,sizeof(f)); 18 int ans=0; 19 for(i=1;i<=N;++i) 20 { 21 int maxn=0; 22 for(j=1;j<i;++j) 23 { 24 if(P[j].w<P[i].w&&P[j].s>P[i].s&&f[j]>maxn){ 25 maxn=f[j]; 26 } 27 } 28 f[i]=maxn+1; 29 if(f[i]>ans) ans=f[i]; 30 } 31 printf("%d\n",ans); 32 stack<int>s; 33 for(i=N;i&&ans;--i) 34 { 35 if(f[i]==ans){ans--;s.push(i);} 36 } 37 while(!s.empty()) {printf("%d\n",P[s.top()].u);s.pop();} 38 return 0; 39 }
时间: 2024-10-10 11:05:10