(博客搬迁)hdu1069 Monkey and Banana(最长不下降子序列)

思想:如序列{4,6,5,7,3},则其最长不下降子序列为{4,6,7};

算法实现:时间复杂度(n^2)

 int Lc(int num[], int n)
 {
      int i,j, max = 0;
       int list[n];
       for(i = 0; i < n; i++)
        {
              list[i] = 1;
               for(j = 0; j < i; j++)
               {
                    if(num[i] > num[j] && list[j] >= list[i])
                   {
                         list[i] = list[j] + 1;
                   }
               }
        }
 return max;
}

hdu 1069:这题可以通过数据的预处理,把它转化为最长不下降子序列问题。

code:

#include<stdio.h>
#include<stdlib.h>
#define N 35

struct Node
{
 int x, y, z;
 int max;
}node[N*6];

int cmp(const void* a, const void* b)
{
 struct Node *c = (Node*)a;
 struct Node *d = (Node*)b;
 if(c->x != d->x)return c->x - d->x;
 else if(c->y != d->y)return c->y - d->y;
 else return c->z - d->z;
}
int main()
{
 int xi[N], yi[N], zi[N];
 int n;
 int i, j, k;
 int max = 0;
 int cas = 1;
 while(scanf("%d", &n ) && n)
 {
  k = 0;
  for(i = 0; i < n; i++)
  {
      scanf("%d %d %d", &xi[i], &yi[i], &zi[i]);
   node[k].x = xi[i];
   node[k].y = yi[i];
   node[k++].z = zi[i];
   
   node[k].x = xi[i];
   node[k].y = zi[i];
   node[k++].z = yi[i];

   node[k].x = zi[i];
   node[k].y = yi[i];
   node[k++].z = xi[i];
   
   node[k].x = yi[i];
   node[k].y = zi[i];
   node[k++].z = xi[i];
   
   node[k].x = yi[i];
   node[k].y = xi[i];
   node[k++].z = zi[i];
   
   node[k].x = zi[i];
   node[k].y = xi[i];
   node[k++].z = yi[i];
   }
  qsort(node, k, sizeof(node[0]), cmp);
  max = 0;
  for(i = 0; i < k; i++)
  {
            node[i].max = node[i].z;
   for(j = i-1; j >= 0; j--)
   {
    if(node[i].x > node[j].x && node[i].y > node[j].y && node[i].max < (node[i].z + node[j].max))
     node[i].max = node[i].z + node[j].max;
   }
   max = max > node[i].max ? max : node[i].max;
  }
  
  printf("Case %d: maximum height = %d\n", cas++, max);
 }
 return 0;
}

你可能感兴趣的:((博客搬迁)hdu1069 Monkey and Banana(最长不下降子序列))