http://codeforces.com/problemset/problem/4/D
偷偷的说这是我第一次写最长上升子序列
题意大概就是几个信封 大小有别,一个套一个 最多能套多少层。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define MAXN 5555
using namespace std;
struct Node
{
int w, h, n;
};
Node matrix[MAXN];
int n, dp[MAXN], pre[MAXN], flag[MAXN]={0};
bool cmp(const Node& a, const Node& b)
{
if(a.w<b.w) return true;
if(a.w==b.w && a.h<b.h) return true;
return false;
}
bool able(int a, int b)
{
if(matrix[a].w<matrix[b].w && matrix[a].h<matrix[b].h) return true;
return false;
}
void print(int n)
{
if(n==0) return ;
print(pre[n]);
cout<<matrix[n].n<<" ";
}
int main()
{
cin>>n;
for(int i=0; i<=n; i++) cin>>matrix[i].w>>matrix[i].h, matrix[i].n=i, dp[i]=0, pre[i]=-1;
sort(matrix+1, matrix+n+1, cmp);
dp[0]=1; flag[0]=1;
for(int i=1; i<=n; i++)
for(int j=0; j<i; j++)
{
if(flag[j]==0 || !able(j, i)) continue;
if(dp[i]<=dp[j]) dp[i]=dp[j]+1, pre[i]=j, flag[i]=1;
}
int max=0;
for(int i=0; i<=n; i++)
if(dp[max]<dp[i]) max=i;
cout<<dp[max]-1<<endl;
print(max);
cout<<endl;
return 0;
}