HDU6197 最长有序子序列 DP+二分查找

来源:互联网 发布:淘宝嘻哈店铺 编辑:程序博客网 时间:2024/06/14 08:00

**思路**


利用DP+二分查找依次找出最长递增子序列和最长递减子序列,取二者中较大值。
如果结果 ans>=n-k 那么这样的序列满足条件是一个魔法数组。


**代码**

#include <iostream>#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #include <math.h>#include <algorithm>using namespace std;#define INT 0x7fffffff  int main()  {      //freopen("0.0.txt", "r", stdin);    int a[100010], n, k, top1, top2, T;    int arr[100010], a1[100010];    scanf("%d", &T);      while(T--)      {          memset(a, 0, sizeof(a));        memset(a1, 0, sizeof(a1));        scanf("%d%d",&n, &k);        int i;          top1 = 0;         top2 = 0;         a[0] = INT;         a1[0] = -INT;         for(i = 0; i < n; i++)        {            scanf("%d", &arr[i]);        }        for(i = 0; i < n; i++)          {               if(arr[i] < a[top1])              {                  top1++;                  a[top1] = arr[i];              }//发现递增数据进栈              else              {                  int low = 1, high = top1;                  while(low <= high)                  {//二分查找                       int mid = (low + high)/2;                      if(arr[i] < a[mid])                          low = mid + 1;                      else                          high = mid - 1;                  }//while                  a[low]=arr[i];              }//else             if(arr[i]>a1[top2])              {                  top2++;                  a1[top2]=arr[i];              }//发现递增数据进栈              else              {                  int low=1,high=top2;                  while(low<=high)                  {//二分查找                       int mid=(low+high)/2;                      if(arr[i]>a1[mid])                          low=mid+1;                      else                          high=mid-1;                  }//while                  a1[low]=arr[i];              }//else          }//for          int ans = top1 > top2 ? top1 : top2;        if(ans >= n-k) printf("A is a magic array.\n");        else printf("A is not a magic array.\n");             }//while      //system("pause");      return 0;  }