POJ 1887

一道world final,描述巨长,读完发现,就是最长下降序列,就变成很简单的DP了,不过为了学习那个O(nlogn)算法,再去仔细折腾了下,有两个觉得说的很好,一个纸牌比喻,一个对于这类问题的介绍

这里注意,因为一般问题是最长上升子序列,所以这里利用upper_bound, lower_bound这类二分查找的算法就需要一些技巧了,可以详见代码

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <deque>
using namespace std;

const int maxn= (1<<15)+3;

int a[maxn], dv[maxn];

int main(int argc, char const *argv[])
{
	int kase= 0;
	while (1== scanf("%d", &a[0]) && ~a[0]){
		int l= 1;
		while (1== scanf("%d", a+l) && ~a[l]){
			++l;
		}

		memset(dv, -1, (l+2)*sizeof(int));
		for (int i= 0; i< l; ++i){
			*upper_bound(dv, dv+l, a[i], greater<int>())= a[i];
		}
		if (kase){
			putchar('\n');
		}
		printf("Test #%d:\n  maximum possible interceptions: %d\n", ++kase, (int)(lower_bound(dv, dv+l, -1, greater<int>())-dv));
	}
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章