您的位置:首页 > 其它

poj 1887 Testing the CATCHER

2013-08-24 16:40 323 查看
题目链接:http://poj.org/problem?id=1887

题目大意:求最长非增子序列的长度

解题思路:简单 dp, 时间复杂度 O(n2), 另有时间复杂度为 O(n·logn) 的算法,读者可自行思考。

///////////////////////////////////////////////////////////////////////////
//problem_id: poj 1887
//user_id: SCNU20102200088
///////////////////////////////////////////////////////////////////////////

#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

///////////////////////////////////////////////////////////////////////////
typedef long long LL;
const double PI=acos(-1.0);

const int x4[]={-1,0,1,0};
const int y4[]={0,1,0,-1};
const int x8[]={-1,-1,0,1,1,1,0,-1};
const int y8[]={0,1,1,1,0,-1,-1,-1};

typedef int T;
T max(T a,T b){ return a>b? a:b; }
T min(T a,T b){ return a<b? a:b; }
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//Add Code:
///////////////////////////////////////////////////////////////////////////

int main(){
///////////////////////////////////////////////////////////////////////
//Add code:
int x,i,j,Case=1,a[50005],dp[50005];
while(scanf("%d",&x)!=EOF){
if(x==-1) break;
int n=0;
a[++n]=x;
while(scanf("%d",&x)!=EOF){
if(x==-1) break;
a[++n]=x;
}
for(i=1;i<=n;i++) dp[i]=1;
int Max=1;
for(i=2;i<=n;i++){
for(j=1;j<i;j++){
if(a[j]>=a[i]) dp[i]=max(dp[i],dp[j]+1);
}
Max=max(Max,dp[i]);
}
if(Case>1) printf("\n");
printf("Test #%d:\n",Case++);
printf("  maximum possible interceptions: %d\n",Max);
}
///////////////////////////////////////////////////////////////////////
return 0;
}

///////////////////////////////////////////////////////////////////////////
/*
Testcase:
Input:
389
207
155
300
299
170
158
65
-1
23
34
21
-1
-1
Output:
Test #1:
maximum possible interceptions: 6

Test #2:
maximum possible interceptions: 2
*/
///////////////////////////////////////////////////////////////////////////
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: