zoukankan      html  css  js  c++  java
  • HLG1116-选美大赛

    Description

    一年一度的哈理工选美大赛开始了.来自各个院系的N个美女们都在一起排成一排,然后从左到右给他们标号(1-N),评委叫兽开始观摩,由于身高高低都不同, 叫兽想从中选出尽可能多的人使得他们的身高从左到右依次递增,你能帮助叫兽吗?

    Input


    输入数据第一行一个数据表示美女的个数N(0<N<100)

    接下来有N个数据表示1-N标号的美女的身高,身高范围都在0-180之内

    当N=0时候输入结束




    Output



    按照样例输出,首先The number is N:N是选出最多美女个数,然后后面输出N个数,代表选出美女的标号,从左到右依次输出.

    题目保证答案唯一





    Sample Input

    3

    2 1 2

    3

    1 2 3

    0

    Sample Output

    The number is 2: 2 3

    The number is 3: 1 2 3

    想说,这道题目,求LIS不是问题,纠结在于记录路径,搞这个搞了半天,结果还是去看了大牛的博客T T,只能说,学习了。。。

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <stack>
     6 
     7 using namespace std;
     8 const int MAX_SIZE = 1000;
     9 int dp[MAX_SIZE];
    10 int vis[MAX_SIZE];
    11 int strat;
    12 
    13 int LIS(int arr[], int n)
    14 {
    15     memset(vis, 0, sizeof(vis));
    16     for(int i = 1; i <= n; ++i)
    17     {
    18         dp[i] = 0;
    19     }
    20 
    21     int ans = 0;
    22     dp[1] = 1;
    23 
    24     for(int i = 2; i <= n; ++i)
    25     {
    26         ans = dp[i];
    27         for(int j = 1; j < i; ++j)
    28         {
    29             if(arr[i] > arr[j] && dp[j] > ans)
    30             {
    31                 ans = dp[j];
    32                 vis[i] = j;
    33             }
    34         }
    35         dp[i] = ans + 1;
    36     }
    37     ans = 0;
    38     for(int i = 1; i <= n; ++i)
    39     {
    40         if(dp[i] > ans)
    41         {
    42             ans = dp[i];
    43             strat = i;
    44         }
    45     }
    46 
    47     return ans;
    48 }
    49 
    50 ///dfs去找路径
    51 void path(int strat)
    52 {
    53     if(strat != 0)
    54     {
    55         path(vis[strat]);
    56         printf(" %d", strat);
    57     }
    58 }
    59 
    60 int main()
    61 {
    62     int n;
    63     int arr[MAX_SIZE];
    64     stack<int > s;
    65 
    66     while(~scanf("%d", &n) && n)
    67     {
    68         for(int i = 1; i <= n; ++i)
    69         {
    70             scanf("%d", arr + i);
    71         }
    72         int ans = LIS(arr, n);
    73         printf("The number is %d:",ans);
    74 
    75         path(strat);
    76         puts("");
    77     }
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    练习5.6.3节
    size_t
    练习3.43
    use include to read a file
    ACM数学(转)
    POJ 2039 To and Fro
    poj 1716 差分约束
    poj 3159 差分约束
    hdu 4571 floyd+动态规划
    poj 1364 差分约束
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4440135.html
Copyright © 2011-2022 走看看