zoukankan      html  css  js  c++  java
  • poj 1887 Testing the CATCHER

    题目链接:http://poj.org/problem?id=1887

    题目大意:求最长非增子序列的长度

    解题思路:简单 dp, 时间复杂度 O(n2), 另有时间复杂度为 O(n·logn) 的算法,读者可自行思考。

     1 ///////////////////////////////////////////////////////////////////////////
     2 //problem_id: poj 1887
     3 //user_id: SCNU20102200088
     4 ///////////////////////////////////////////////////////////////////////////
     5 
     6 #include <algorithm>
     7 #include <iostream>
     8 #include <iterator>
     9 #include <iomanip>
    10 #include <cstring>
    11 #include <cstdlib>
    12 #include <string>
    13 #include <vector>
    14 #include <cstdio>
    15 #include <cctype>
    16 #include <cmath>
    17 #include <queue>
    18 #include <stack>
    19 #include <list>
    20 #include <set>
    21 #include <map>
    22 using namespace std;
    23 
    24 ///////////////////////////////////////////////////////////////////////////
    25 typedef long long LL;
    26 const double PI=acos(-1.0);
    27 
    28 const int x4[]={-1,0,1,0};
    29 const int y4[]={0,1,0,-1};
    30 const int x8[]={-1,-1,0,1,1,1,0,-1};
    31 const int y8[]={0,1,1,1,0,-1,-1,-1};
    32 
    33 typedef int T;
    34 T max(T a,T b){ return a>b? a:b; }
    35 T min(T a,T b){ return a<b? a:b; }
    36 ///////////////////////////////////////////////////////////////////////////
    37 
    38 ///////////////////////////////////////////////////////////////////////////
    39 //Add Code:
    40 ///////////////////////////////////////////////////////////////////////////
    41 
    42 int main(){
    43     ///////////////////////////////////////////////////////////////////////
    44     //Add code:
    45     int x,i,j,Case=1,a[50005],dp[50005];
    46     while(scanf("%d",&x)!=EOF){
    47         if(x==-1) break;
    48         int n=0;
    49         a[++n]=x;
    50         while(scanf("%d",&x)!=EOF){
    51             if(x==-1) break;
    52             a[++n]=x;
    53         }
    54         for(i=1;i<=n;i++) dp[i]=1;
    55         int Max=1;
    56         for(i=2;i<=n;i++){
    57             for(j=1;j<i;j++){
    58                 if(a[j]>=a[i]) dp[i]=max(dp[i],dp[j]+1);
    59             }
    60             Max=max(Max,dp[i]);
    61         }
    62         if(Case>1) printf("
    ");
    63         printf("Test #%d:
    ",Case++);
    64         printf("  maximum possible interceptions: %d
    ",Max);
    65     }
    66     ///////////////////////////////////////////////////////////////////////
    67     return 0;
    68 }
    69 
    70 ///////////////////////////////////////////////////////////////////////////
    71 /*
    72 Testcase:
    73 Input:
    74 389
    75 207
    76 155
    77 300
    78 299
    79 170
    80 158
    81 65
    82 -1
    83 23
    84 34
    85 21
    86 -1
    87 -1
    88 Output:
    89 Test #1:
    90   maximum possible interceptions: 6
    91 
    92 Test #2:
    93   maximum possible interceptions: 2
    94 */
    95 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    比较器
    堆排序
    快速排序
    荷兰国旗问题
    python查漏补缺 --- 基础概念及控制结构
    redis 交集、并集、差集
    spark问题
    IDEA写scala简单操作
    hive 中遇到的正则
    scala模式匹配
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3279486.html
Copyright © 2011-2022 走看看