PKU 1631 Bridging signals 二分查找

来源:互联网 发布:数据库系统工程师真题 编辑:程序博客网 时间:2024/06/04 19:40
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;int res[40005] ;  //保存非线性递增队列的结果int n, resLen ;void Insert (int low, int high, int val){    int mid ;    while (low <= high)    {        mid = (low+high) / 2 ;        if (res[mid] <= val)            low = mid + 1 ;        else            high = mid - 1;    }    res[low] = val ;}int main (){    int i, tcase, val;    scanf ("%d", &tcase) ;    while (tcase --)    {        resLen = -1 ;        scanf ("%d", &n) ;        for (i = 0; i < n; i ++)        {            scanf ("%d", &val) ;            if (resLen == -1)  //如果数组元素为空,将当前值入数组            {                resLen ++ ;                res[resLen] = val ;                continue ;            }            else            {                if (val > res[resLen]) //如果当前值大于数组的栈顶元素,                {                      //直接入数组,长度加1                    resLen ++ ;                    res[resLen] = val ;                    continue ;                }                else if (val == res[resLen]) //如果出现相等                    continue ;                else  //否则替换掉当前数组中比当前值大的元素                    Insert (0, resLen, val) ;  //二分            }        }        cout << resLen+1 << endl ;    }    return 0 ;}