您的位置:首页 > 其它

POJ 1887 Testing the CATCHER

2012-12-09 18:07 399 查看
最长下降子序列,单纯的DP算法复杂度是O(n^2),现在先放一放,等下我去弄O(nlogn)的算法。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

const int MAXN = 10010;

int A[MAXN];
int d[MAXN]; //以i为终点的最长下降序列 

int n;
int tot;
int times;

void dp()
{
	int MAX = 0;
	for(int i = 1; i <= tot; i++)
	{
		d[i] = 1;
		for(int j = 1; j <= i-1; j++) if(A[i] < A[j])
		{
			d[i] = max(d[i], d[j]+1);
		}
		MAX = max(MAX, d[i]);
	}
	printf("  maximum possible interceptions: %d\n\n", MAX);
}

void read_case()
{
	tot = 1;
	while(scanf("%d", &n) && n != -1)
	{
		A[++tot] = n;
	}
}

void solve()
{
	read_case();
	printf("Test #%d:\n", ++times);
	dp();
}

int main()
{
	times = 0;
	while(scanf("%d", &n) && n != -1)
	{
		A[1] = n;
		solve();
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: