zoukankan      html  css  js  c++  java
  • poj 1631 Bridging signals

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

    题目大意:求最长上升子序列的长度

    解题思路:dp+二分搜索,时间复杂度为 O(n·logn). 直接 dp 时间复杂度 O(n2),  会 TLE.

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: poj 1631
      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 Case,n,i,a[40005],b[40005];
     46     scanf("%d",&Case);
     47     while(Case--){
     48         scanf("%d",&n);
     49         for(i=1;i<=n;i++) scanf("%d",&a[i]);
     50         int Max=0;
     51         for(i=1;i<=n;i++){
     52             int L=1,R=Max;
     53             while(L<=R){
     54                 int M=(L+R)/2;
     55                 if(b[M]<a[i]) L=M+1;
     56                 else R=M-1;
     57             }
     58             b[L]=a[i];
     59             Max=max(Max,L);
     60         }
     61         printf("%d
    ",Max);
     62     }
     63     ///////////////////////////////////////////////////////////////////////
     64     return 0;
     65 }
     66 
     67 ///////////////////////////////////////////////////////////////////////////
     68 /*
     69 Testcase:
     70 Input:
     71 4
     72 6
     73 4
     74 2
     75 6
     76 3
     77 1
     78 5
     79 10
     80 2
     81 3
     82 4
     83 5
     84 6
     85 7
     86 8
     87 9
     88 10
     89 1
     90 8
     91 8
     92 7
     93 6
     94 5
     95 4
     96 3
     97 2
     98 1
     99 9
    100 5
    101 8
    102 9
    103 2
    104 3
    105 1
    106 7
    107 4
    108 6
    109 Output:
    110 3
    111 9
    112 1
    113 4
    114 */
    115 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    java的类继承(与c++对比)
    java的数据类型、自动拆装箱、字面量
    java中关键字static和final
    JVM之JIT
    java之JIT(Just in time)
    栈和堆
    C++中vector的使用
    canvas
    ajax笔记
    CSS笔记
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3279711.html
Copyright © 2011-2022 走看看