类似最长递增子序,记忆化DP—— Codeforces Beta Round #4 (Div. 2 Only)D - Mysterious Present

题意主要是寻找最长递增子序,每个元素有两个属性,H,W,下一个一定要比上一个的H,W大

先排序,

再类似最长递增子序的做法,

两个for 语句

注意点:就是每个元素H,W要比开始给定的大

记忆路径的过程就是pre记录法+stack还原

类似最长递增子序,记忆化DP—— Codeforces Beta Round #4 (Div. 2 Only)D - Mysterious Present View Code
   
     
#include < iostream >
#include
< algorithm >
#include
< stdio.h >
#include
< stack >
using namespace std;

struct data
{
int no;
int w;
int h;
int add;
int pre;
}ss[
59 ]; /// /

int cmp(data a,data b)
{
if (a.w == b.w)
return a.h < b.h;
return a.w < b.w;
}

int main ()
{
int n,w,h;
scanf(
" %d%d%d " , & n, & w, & h);

int i,j;
for (i = 0 ;i < n;i ++ )
{
scanf(
" %d%d " , & ss[i].w, & ss[i].h);
if (ss[i].w > w && ss[i].h > h)
ss[i].add
= 1 ;
else
ss[i].add
= 0 ;
ss[i].pre
=- 1 ;
ss[i].no
= i;
}

sort(
& ss[ 0 ], & ss[n],cmp);

int ri;

for (i = 0 ;i < n;i ++ )
{
if (ss[i].add >= 1 )
{
for (j = i + 1 ;j < n;j ++ )
{
if (ss[j].add >= 1 )

if (ss[i].w < ss[j].w && ss[i].h < ss[j].h)
{
if (ss[j].add < ss[i].add + 1 )
{
ss[j].add
= ss[i].add + 1 ;
ss[j].pre
= i;
}
}
}
}
}

int max =- 1 ,kai;
for (i = 0 ;i < n;i ++ )
{
if (max < ss[i].add)
{
max
= ss[i].add;
kai
= i;
}
}

printf(
" %d\n " ,max);
if (max == 0 ) return 0 ;

stack
< int > st1;
int point = ss[kai].pre;
st1.push(ss[kai].no);
while (point !=- 1 )
{
st1.push(ss[point].no);
point
= ss[point].pre;
}
while ( ! st1.empty())
{
printf(
" %d " ,st1.top() + 1 );
st1.pop();
}

printf(
" \n " );
}

你可能感兴趣的:(codeforces)