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:
Each test case is contained on a single line. A line describing a test case is formatted as follows:
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 <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;
}
#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;
}
- TJU Antimonotonicity
- Antimonotonicity
- Antimonotonicity
- Antimonotonicity
- JZOJ1728. Antimonotonicity
- poj 3298 Antimonotonicity DP
- Sicily 1448. Antimonotonicity
- poj 3298 Antimonotonicity 贪心
- Sicily 1448. Antimonotonicity
- JZOJ.1728. Antimonotonicity
- Antimonotonicity (Standard IO)
- ZCMU-1231-Antimonotonicity
- TJU 3474
- TJU Sequence
- TJU Vacation
- TJU Change
- TJU Price
- TJU 4087
- 获取c:foreach循环中的row对象
- JavaScript中的if...else if...语句
- Altium Designer规则
- android 屏幕分辨率与密度,dip与px的区别(整合自网络资源)
- SPOJ DQUERY 求区间内不同数的个数 主席树
- TJU Antimonotonicity
- CODE 71: Unique Paths
- HUNNU--湖师大--11407--It Is Cold
- linux输入子系统
- Stanford公开课《Programming Methodology》里面的Karel机器人的配置方法
- 【leetcode】Valid Palindrome
- 多线程与多进程的区别(4)
- 布线技巧
- Java安全_使用JavaAPI完成条目的管理