zoukankan      html  css  js  c++  java
  • 最长上升子序列

    Almost Sorted Array

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 7686    Accepted Submission(s): 1800


    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
     
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <string>
     7 #include <deque>
     8 #include <map>
     9 #include <vector>
    10 using namespace std;
    11 #define  ll long long 
    12 #define  N   100009
    13 #define  M 1000000000
    14 #define  gep(i,a,b)  for(int  i=a;i<=b;i++)
    15 #define  gepp(i,a,b) for(int  i=a;i>=b;i--)
    16 #define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
    17 #define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
    18 #define  mem(a,b)  memset(a,b,sizeof(a))
    19 #define  ph  push_back
    20 int t,n;
    21 int a[N],dp[N];
    22 int main()
    23 {
    24     scanf("%d",&t);
    25     while(t--)
    26     {
    27         scanf("%d",&n);
    28         gep(i,1,n) scanf("%d",&a[i]);
    29         int l1=1,l2=1l;
    30         dp[l1]=a[1];
    31         gep(i,2,n){
    32             if(a[i]>=dp[l1]){
    33                 l1++;
    34                 dp[l1]=a[i];
    35             }
    36             else{
    37                 int pos=upper_bound(dp+1,dp+1+l1,a[i])-dp;
    38                 dp[pos]=a[i];
    39             }
    40         }
    41         dp[l2]=a[n];
    42         gepp(i,n-1,1){
    43             if(a[i]>=dp[l2]){
    44                 l2++;
    45                 dp[l2]=a[i];
    46             }
    47             else{
    48                 int pos=upper_bound(dp+1,dp+1+l2,a[i])-dp;
    49                 dp[pos]=a[i];
    50             }
    51         }
    52         if(l1>=n-1||l2>=n-1){
    53             printf("YES
    ");
    54         }
    55         else{
    56             printf("NO
    ");
    57         }
    58     }
    59     return 0;
    60 }
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <string>
     7 #include <deque>
     8 #include <map>
     9 #include <vector>
    10 #include <stack>
    11 using namespace std;
    12 #define  ll long long 
    13 #define  N   100009
    14 #define  M 1000000000
    15 #define  gep(i,a,b)  for(int  i=a;i<=b;i++)
    16 #define  gepp(i,a,b) for(int  i=a;i>=b;i--)
    17 #define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
    18 #define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
    19 #define  mem(a,b)  memset(a,b,sizeof(a))
    20 #define  ph  push_back
    21 int n,a[N],dp[N];
    22 int pre[N];
    23 stack<int>s;
    24 vector<int>ve;
    25 int main()
    26 {
    27     scanf("%d",&n);
    28     gep(i,1,n) scanf("%d",&a[i]);
    29     gep(i,1,n) dp[i]=1;
    30     mem(pre,-1);
    31     //递增
    32     gep(i,2,n){
    33         gep(j,1,i-1){
    34             if(a[i]>a[j]&&dp[i]<dp[j]+1){
    35                 dp[i]=dp[j]+1;//1~i
    36                 pre[i]=j;
    37             }
    38         }
    39     }
    40     while(j!=-1){
    41         s.push(a[j]);
    42         j=pre[j];
    43     }
    44     int v=s.top();
    45         s.pop();
    46         printf("%d",v);
    47     while(!s.empty()){
    48         int v=s.top();
    49         s.pop();
    50         printf(" %d",v);
    51     }
    52     printf("
    ");
    53     /*
    54     5
    55     6 3 5 2 9
    56     3 5 9
    57     */
    58     
    59     //递减
    60     gepp(i,n-1,1){
    61         gep(j,i+1,n){
    62             if(a[i]>a[j]&&dp[i]<dp[j]+1){
    63                 dp[i]=dp[j]+1;//i~n
    64                 pre[i]=j;
    65             }
    66         }
    67     }
    68     int MAX=0,j=1;
    69     gep(i,1,n){
    70         if(MAX<dp[i]){
    71             MAX=dp[i];
    72             j=i;
    73         }
    74     }
    75     while(j!=-1){
    76         ve.ph(a[j]);
    77         j=pre[j];
    78     }
    79     gep(i,0,ve.size()-1){
    80         printf("%d%c",ve[i],i==ve.size()-1?'
    ':' ');
    81     }
    82     /*
    83     5
    84     5 1 3 2 6
    85     5 3 2
    86     */
    87 
    88     return 0;
    89 }

    //ACM训练联盟周赛

    •  65536K
     

    Teemo starts to do homework everyday. Today, he meets a hard problem when doing his homework.

    There's an array A which contains n integers(for every 1<=i<=n, A[i] = 1 or A[i]= 2), you can choose an interval [l,r](1<=l<=r<=n), then reverse it so that the length of the longest non-decreasing subsequence of the new sequence is maximum.

    Input Format

    • The first line of the input contains an integer T(1=<T<=10), giving the number of test cases.
    • For every test case, the first line contains an integer n(1<=n<=2000). The second line contains n integers. The i th integer represents A[i](1<=A[i]<=2). 

    Output Format

    Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.

    样例输入

    1
    4
    1 2 1 2

    样例输出

    4


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <string>
     7 #include <deque>
     8 #include <map>
     9 #include <vector>
    10 #include <stack>
    11 using namespace std;
    12 #define  ll long long 
    13 #define  N   2009
    14 #define  M 1000000000
    15 #define  gep(i,a,b)  for(int  i=a;i<=b;i++)
    16 #define  gepp(i,a,b) for(int  i=a;i>=b;i--)
    17 #define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
    18 #define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
    19 #define  mem(a,b)  memset(a,b,sizeof(a))
    20 #define  ph  push_back
    21 int t,n,dpx[N][N],dpy[N][N];
    22 int dp[N],a[N];
    23 int l;
    24 int main()
    25 {
    26     scanf("%d",&t);
    27     
    28     while(t--)
    29     {
    30         scanf("%d",&n);
    31         gep(i,1,n) scanf("%d",&a[i]);
    32         mem(dpx,0);mem(dpy,0);
    33         gep(i,1,n){
    34             l=0;
    35             mem(dp,0);
    36             gep(j,i,n){
    37                 if(a[j]>=dp[l]){
    38                     l++;    
    39                     dp[l]=a[j];
    40                 }
    41                 else{
    42                     int pos=upper_bound(dp+1,dp+1+l,a[j])-dp;
    43                     dp[pos]=a[j];
    44                 }
    45                 dpx[i][j]=l;//i~j的最长上升子序列
    46                 }
    47         }
    48         gepp(i,n,1){
    49             l=0;
    50             mem(dp,0);
    51             gepp(j,i,1){
    52                 if(a[j]>=dp[l]){
    53                     l++;    
    54                     dp[l]=a[j];
    55                 }
    56                 else{
    57                     int pos=upper_bound(dp+1,dp+1+l,a[j])-dp;
    58                     dp[pos]=a[j];
    59                 }
    60                 dpy[j][i]=l;//j~i的最长下降子序列
    61             }
    62         }
    63         int MAX=0;
    64         gep(i,1,n){
    65             gep(j,1,n){
    66                 MAX=max(MAX,dpx[1][n]-dpx[i][j]+dpy[i][j]);//reverse
    67             }
    68         }
    69         printf("%d
    ",MAX);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    ERP Odoo开发--Getting started with Odoo development --openerp
    5 Tips for Building a Winning DevOps Culture
    android stream media
    LDAP summary-- Python ldap
    android call and audio
    android bluetooth
    .windows安装使用这些偏底层的Python扩展太
    经典的DOS小命令 for 网络 nbtstat
    js和jquery设置disabled属性为true使按钮失效
    Several ports (8005, 8080, 8009)被占用
  • 原文地址:https://www.cnblogs.com/tingtin/p/9492612.html
Copyright © 2011-2022 走看看