标签:codeforces dp
给出n个信封,这n个信封有长和宽,给出卡片的尺寸,求取能够装入卡片的最长的序列,序列满足后一个的长和宽一定大于前一个,求最长的这个序列的长度,并且给出一组可行解。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 5007
using namespace std;
int n,w,h;
int dp[MAX];
int back[MAX];
struct Node
{
int w,h,x;
bool operator < ( const Node & a ) const
{
if ( w == a.w ) return h < a.h;
return w < a.w;
}
}a[MAX];
void print ( int x )
{
if ( a[x]. w <= w || a[x].h <= h ) return;
print ( back[x] );
printf ( "%d " , a[x].x );
}
int main ( )
{
while ( ~scanf ( "%d%d%d" , &n , &w , &h ))
{
for ( int i = 1 ; i <= n ; i++ )
{
scanf ( "%d%d" , &a[i].w , &a[i].h );
a[i].x = i;
}
dp[0] = -1;
sort ( a+1 , a+n+1 );
for ( int i = 1 ; i <= n ; i++ )
{
int temp = -1,id = 0;
for ( int j = 0 ; j < i ; j++ )
if ( a[j].w < a[i].w && a[j].h < a[i].h && a[j].w > w && a[j].h > h )
if ( temp < dp[j] )
{
temp = dp[j];
id = j;
}
if ( a[i].w > w && a[i].h > h ) dp[i] = 1 , back[i] = 0;
else dp[i] = -1;
if ( temp+1 > dp[i] )
{
dp[i] = temp+1;
back[i] = id;
}
}
int maxn = 0;
for ( int i = 1 ; i <= n ; i++ )
maxn = max ( maxn , dp[i] );
if ( maxn == 0 )
{
puts ( "0" );
continue;
}
printf ( "%d\n" , maxn );
for ( int i = 1 ; i <= n ; i++ )
if ( maxn == dp[i] )
{
print ( i );
puts ("");
break;
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 4D D. Mysterious Present(dp)
标签:codeforces dp
原文地址:http://blog.csdn.net/qq_24451605/article/details/48463889