zoukankan      html  css  js  c++  java
  • UVA 103 Stacking Boxes n维最长上升子序列

    题目链接:UVA - 103

    题意:现有k个箱子,每个箱子可以用n维向量表示。如果一个箱子的n维向量均比另一个箱子的n维向量大,那么它们可以套接在一起,每个箱子的n维向量可以互相交换值,如箱子(2,6)可以和箱子(7,3)套接在一起。求出套接的箱子最多的个数前提下任意一种解决方案。

    算法:抛开n维不看,本题就是一个DP的最长上升子序列问题,现在加上了n维的限制,想想也不是很难吧,在DP过程中判断每一维都满足条件即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define inf 0x7fffffff
     8 using namespace std;
     9 typedef long long LL;
    10 
    11 int k,n;
    12 int dp[33],pre[33];
    13 struct node
    14 {
    15     int an[13];
    16     int id;
    17     friend bool operator < (node a,node b)
    18     {
    19         for (int i=0 ;i<n ;i++)
    20         {
    21             if (a.an[i] != b.an[i]) return a.an[i] <  b.an[i];
    22         }
    23     }
    24 }arr[33];
    25 
    26 void printOut(int u)
    27 {
    28     if (pre[u]!=-1) printOut(pre[u]);
    29     if (pre[u]==-1) printf("%d",arr[u].id+1 );
    30     else printf(" %d",arr[u].id+1 );
    31 }
    32 
    33 int main()
    34 {
    35     while (scanf("%d%d",&k,&n)!=EOF)
    36     {
    37         memset(dp,0,sizeof(dp));
    38         memset(pre,-1,sizeof(pre));
    39         for (int i=0 ;i<k ;i++)
    40         {
    41             for (int j=0 ;j<n ;j++)
    42                 scanf("%d",&arr[i].an[j]);
    43             arr[i].id=i;
    44             sort(arr[i].an,arr[i].an+n);
    45         }
    46         sort(arr,arr+k);
    47 //        for (int i=0 ;i<k ;i++)
    48 //        {
    49 //            for (int j=0 ;j<n ;j++)
    50 //                cout<<arr[i].an[j]<<" ";
    51 //            cout<<endl;
    52 //        }
    53         for (int i=0 ;i<k ;i++)
    54         {
    55             int temp=0;
    56             for (int j=0 ;j<i ;j++)
    57             {
    58                 int flag=0;
    59                 for (int u=0 ;u<n ;u++)
    60                     if (arr[i].an[u]<=arr[j].an[u]) {flag=1;break; }
    61                 if (!flag && dp[j]>temp)
    62                 {
    63                     temp=dp[j];
    64                     pre[i]=j;
    65                 }
    66             }
    67             dp[i]=temp+1;
    68         }
    69         int maxlen=-1,num=0;
    70         for (int i=0 ;i<k ;i++)
    71         {
    72             if (dp[i]>maxlen)
    73             {
    74                 maxlen=dp[i];
    75                 num=i;
    76             }
    77         }
    78         printf("%d
    ",maxlen);
    79         printOut(num);
    80         printf("
    ");
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    正则匹配整数或小数
    文本超出点点点
    订单页面布局
    数据库连接池 maxActive,maxIdle,maxWait参数
    dll静态调用和动态调用
    Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnection
    sql server调优
    AdPlus
    010 Editor
    WinDBG相关
  • 原文地址:https://www.cnblogs.com/huangxf/p/4457505.html
Copyright © 2011-2022 走看看