TJU Antimonotonicity

来源:互联网 发布:未央歌 鹿桥 知乎 编辑:程序博客网 时间:2024/04/30 18:48
I have a sequence Fred of length n comprised of integers between 1 and n inclusive. The elements of Fred are pairwise distinct. I want to find a subsequence Mary of Fred that is as long as possible and has the property that: 
   Mary[0] > Mary[1] < Mary[2] > Mary[3] < ...

Input

The first line of input will contain a single integer T expressed in decimal with no leading zeroes. T will be at most 50. T test cases will follow. 
Each test case is contained on a single line. A line describing a test case is formatted as follows: 

   n Fred[0] Fred[1] Fred[2] ... Fred[n-1].
where n and each element of Fred is an integer expressed in decimal with no leading zeroes. No line will have leading or trailing whitespace, and two adjacent integers on the same line will be separated by a single space. n will be at most 30000. 

Output

For each test case, output a single integer followed by a newline --- the length of the longest subsequence Mary of Fred with the desired properties. 

Sample Input

45 1 2 3 4 55 5 4 3 2 15 5 1 4 2 35 2 4 1 3 5

Sample Output

1253



题意概述:在给定的整数串中,找到最长的满足条件的子串,子序列可以不连续,我刚开始就理解错题意了。

源代码:

#include <cstdio>  
#include <queue>  
using namespace std ;  
#define maxn 30009                           //宏定义两个常量
#define max(a,b) a>b?a:b                  //用宏定义的方式定义  取最大值函数
  
int t,n;  
int dp[maxn] , a[maxn]  ;    
int main () 
{  
    scanf ( "%d" , &t ) ;  
    while ( t-- ) 
    {  
        scanf ( "%d" , &n ) ;  
        for ( int i = 1 ; i <= n ; ++i ) scanf ( "%d" , &a[i] ) ;  
        
        dp[1] = 1 ;                                                              //记录当前瞒住条件的字串的长度
        for ( int i = 2 ; i <= n ; ++i )
        {  
            dp[i] = dp[i-1] ;                                                  //记录当前满足条件的子串的长度
            if ( a[i] > a[i-1] )                                                 //如果长度为偶数,并且满足a[i] > a[i-1],就让dp[i]+1,表示满足条件的子串长度加1
            {  
                if ( dp[i-1] % 2 == 0 ) ++dp[i] ;  
            }  
            else if ( a[i] < a[i-1] )                                         //如果长度为奇数,并且满足a[i] <a[i-1],就让dp[i]+1,表示满足条件的子串长度加1
            {  
                if ( dp[i-1] % 2 == 1 ) ++dp[i] ;  
            }  
        }  
        int ans = 0 ;  
        for ( int i = 1 ; i <= n ; ++i ) {  
            ans = max ( ans , dp[i] ) ;  
        }  
        printf ( "%d\n" , ans ) ;  
    }  
    return 0 ;  
}  


我的代码:(有错误,但还没找到,比起上面这位,我的代码差远了~~~~~~自愧不如啊~~~~)

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;


int big(int a , int b)
{
    if(a>b)return a;
    else return b;
}


int main()
{
    int T,N,temp;
    deque<int>len;
    cin>>T;
    while(T--)
    {
          cin>>N;
          for(int i=0;i<N;++i)
          {
                cin>>temp;
                len.push_back(temp); 
          } 
          
          int max=0;
          deque<int>::iterator pos1,pos2;
          pos1=len.begin();
          while( pos1!=len.end() )
          {
               int tempmax=1,flag=0,L=distance(pos1,len.end());
               if( L==1 ){ tempmax=1;max=big(max,tempmax);break;}
               else if( L==2 && *pos1 > *(pos1+1)){tempmax=2;max=big(max,tempmax);break;}
               else if( L==2 && *pos1 <= *(pos1+1)){tempmax=1;max=big(max,tempmax);break;}
               else if( L>2 )
               {
                    for( pos2=pos1+1; pos2!=len.end(); )
                         if( flag%2==0 && *(pos2-1) > *pos2 ){ ++tempmax; ++flag; ++pos2; pos1=pos2;max=big(max,tempmax); continue; }
                         else if( flag%2==0 && *(pos2-1) <= *pos2){ pos1=pos2; max=big(max,tempmax); break; }
                         else if( flag%2==1 && *(pos2-1) < *pos2 ){ ++tempmax; ++flag; ++pos2; pos1=pos2; max=big(max,tempmax); continue; }
                         else if( flag%2==1 && *(pos2-1) >= *pos2 ){ pos1=pos2-1; max=big(max,tempmax); break; }
               }
          }
          len.clear();
          cout<<max<<endl;
    }
    return 0;
}
原创粉丝点击