题目大意:n只大象 每只有重量与IQ两个属性 找出满足重量递减 IQ递增的最多个数(要求严格递增递减);
解题思路:递增子序列
#include <stdio.h>
#include <string.h>
#include<functional>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1005;
const int inf = 1<<30;
int dp[maxn],Path[maxn];
struct node
{
int id,w,s;
}ele[maxn];
bool cmp( node a,node b )
{
if( a.w == b.w ) return a.s > b.s;
else return a.w < b.w;
}
void dfs( int x )
{
if( x == -1 ) return;
dfs(Path[x]);
printf("%d\n",ele[x].id);
}
void GetDp( int n )
{
int MaxLen = -1,p = -1;
memset( Path,-1,sizeof(Path) );
for( int i = 0; i < n; i ++ ){
dp[i] = 1;
for( int j = i-1; j >= 0; j -- ){
if( ele[i].w > ele[j].w && ele[i].s < ele[j].s && dp[i] < dp[j] + 1 ){
dp[i] = dp[j] + 1;
Path[i] = j;
}
}
if( MaxLen < dp[i] ){
MaxLen = dp[p = i];
}
}
printf("%d\n",MaxLen);
dfs(p);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cnt = 0;
int w,s;
while( scanf("%d%d",&w,&s) != EOF )
{
ele[cnt].id = cnt+1;
ele[cnt].w = w; ele[cnt++].s = s;
}
sort( ele,ele+cnt,cmp );
GetDp( cnt );
return 0;
}