zoukankan      html  css  js  c++  java
  • HDU-5532(LIS-nlogn)


    思路:

    解法一:

    新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个关系的同时也会出现一个新的关系

    解法二:找到非递减和非递增LIS中数量较大的一个,只要它大于等于n-1,答案就是YES,不然就是NO,由于卡时间,故要用nlogn算法


    方法一:
    #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int T; int n,num[100007]; int big,small; int flag; int judge(int x) { int tsmall = small; int tbig = big; if(x == 1) { if(num[x] < num[x+1]) tsmall--; if(num[x] > num[x+1]) tbig--; if(!tbig||!tsmall) return 1; else return 0; } else if(x == n) { if(num[x-1] < num[x]) tsmall--; if(num[x-1] > num[x]) tbig--; if(!tbig||!tsmall) return 1; else return 0; } else { if(num[x-1]>num[x]&&num[x]>num[x+1]) tbig -= 2; if(num[x-1]<num[x]&&num[x]<num[x+1]) tsmall -= 2; if(num[x-1]>num[x]&&num[x]<num[x+1]||num[x-1]<num[x]&&num[x]>num[x+1]) { tbig --; tsmall --; } if(num[x-1] < num[x+1]) tsmall++; if(num[x-1] > num[x+1]) tbig++; if(!tbig||!tsmall) returl 1; else return 0; } } int main() { scanf("%d",&T); while(T--) { big = small = 0; flag = 0; scanf("%d",&n); for(int i = 1;i <= n;i++) { scanf("%d",&num[i]); if(i >= 2) { if(num[i] > num[i-1]) small++; if(num[i] < num[i-1]) big++; } } if(n==2||!big||!small) { printf("YES "); continue; } else { for(int i = 1;i <= n;i++) if(judge(i)){ flag = 1; break; } if(flag) printf("YES "); else printf("NO "); } } return 0; }

    方法二:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define INF 0x7fffffff
    using namespace std;
    
    int T;
    int n,num[100007];
    int dp1[100007],dp2[100007];
    int B1[100007],B2[100007];
    int flag;
    
    int max(int a,int b) {
        return a>b?a:b;
    }
    
    int find(int* B,int goal,int r)
    {
        int l = 1;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(B[mid] < goal)
                l = mid+1;
            else if(B[mid] > goal) 
                r = mid-1;
            else return mid;
        }
        return l;
    }
    
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            int len1,len2;
            int ans1 = 0,ans = 0,ans2 = 0;
            scanf("%d",&n);
            for(int i = 1;i <= n;i++) {
                scanf("%d",&num[i]);
                dp1[i] = dp2[i] = 1;
            }
            B1[1] = B2[1] = num[1];
            len1 = len2 = 1;
            for(int i = 2;i <= n;i++) {
                if(num[i] >= B1[len1]) B1[++len1] = num[i];
                else {
                    int pos = find(B1,num[i],len1);
                    B1[pos] = num[i];
                }
                if(num[i] <= B2[len2]) B2[++len2] = num[i];
                else {
                    int pos = find(B2,num[i],len2);
                    B2[pos] = num[i];
                }
            }
            ans = max(len1,len2);
            if(ans >= n-1) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
    Problem Description
    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.

    We say an array is sorted if its elements are in non-decreasing order or non-increasing order. We say an array is almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now you are given an array a1,a2,,an, is it almost sorted?
     
    Input
    The first line contains an integer T indicating the total number of test cases. Each test case starts with an integer n in one line, then one line with n integers a1,a2,,an.

    1T2000
    2n105
    1ai105
    There are at most 20 test cases with n>1000.
     
    Output
    For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
     
    Sample Input
    3 3 2 1 7 3 3 2 1 5 3 1 4 1 5
     
    Sample Output
    YES YES NO
  • 相关阅读:
    201720181 JaWorld 第三周作业
    201720181 Java小组1623 第一周作业
    201720181 JaWorld 第四、五周作业
    201720181 Java小组1623 第二周作业
    无法打开用户默认数据库的解决方法
    技术网站/博客网址收藏
    .net中防止用户多次登录的方法
    asp.net导出excel方法总结
    面向对象设计模式之Bridge桥接模式(结构型)
    面向对象设计模式之Composite组合模式(结构型)
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/4960153.html
Copyright © 2011-2022 走看看