题目:给你一些老鼠的体重,和速度,求出体重递增时速度变慢的最大集合。
分析:dp,LIS。最大不下降子序列。
说明:看到这个很久以前写得程序才发现,很多风格都变了 。(2011-09-21 11:34)
#include<iostream>
#include<cstdlib>
using namespace std;
struct mice
{
int weith;
int speed;
int order;
}mouse[ 1001 ];
int length[ 1001 ];
int front[ 1001 ];
int fun( const void *a , const void *b )
{
mice *p = (mice *)a,*q = (mice *)b;
return p->weith - q->weith;
}
int main()
{
int t = 0;
while ( cin >> mouse[ t ].weith >> mouse[ t ].speed )
mouse[ t ].order = t ++;
for ( int i = 0 ; i < t ; ++ i )
length[ i ] = 1;
qsort( mouse , t , sizeof( mouse[0] ) , fun );
for ( int i = 1 ; i < t ; ++ i )
{
int max = 0;
for ( int j = 0 ; j < i ; ++ j )
if ( mouse[j].speed > mouse[i].speed && length[j] > max ) {
max = length[j];
front[ i ] = j ;
}
length[ i ] = max + 1;
}
int max_length = 0,max_space = 0;
for ( int i = 0 ; i < t ; ++ i )
if ( length[ i ] > max_length ) {
max_length = length[ i ];
max_space = i;
}
int a[1001],con = 0;
while ( length[ max_space ] > 1 ) {
a[ con ++ ] = mouse[ max_space ].order;
max_space = front[ max_space ];
}
a[ con ++ ] = mouse[ max_space ].order;
cout << con << endl;
for ( int i = con-1 ; i >= 0 ; -- i )
cout << a[ i ]+1 << endl;
return 0;
}