我采用的是简单的方式, 转换成的是DAG上的最长路径,还可以改进,采用其它方法。
#include <iostream>
#include <limits>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
const int size = 1000 + 10;
int a[size][2];
int d[size] , f[size];
int G[size][size];
int total = 0;
int dp(int x)
{
if(d[x] >= 0) return d[x];
int m = 0;
for(int i = 0 ; i < total ; ++i) if(G[x][i])
{
int r = dp(i);
if(r > m) { m = r; f[x] = i; }
}
return d[x] = m + 1;
}
int main()
{
while(scanf("%d%d" , a[total] , a[total]+1) == 2)
{
++total;
}
memset(G , 0 , sizeof(G));
for(int i = 0 ; i < total ; ++i)
{
for(int j = i + 1 ; j < total ; ++j)
{
if(a[i][0] < a[j][0] && a[i][1] > a[j][1]) G[i][j] = 1;
else if(a[j][0] < a[i][0] && a[j][1] > a[i][1]) G[j][i] = 1;
}
}
memset(d , -1 , sizeof(d));
memset(f , -1 , sizeof(f));
for(int i = 0 ; i < total ; ++i) dp(i);
int cur = max_element(d , d + total) - d;
printf("%d\n" , d[cur]);
while(cur != -1)
{
printf("%d\n" , cur + 1);
cur = f[cur];
}
return 0;
}