POJ 3903 Stock Exchange

来源:互联网 发布:软件著作权转让协议 编辑:程序博客网 时间:2024/06/05 12:44

最长上升子序列转移方程:d[i] = max(d[i], d[j]+1) (1<j <= i-1)。

一开始直接用O(n^2)的算法超时了,后来百度发现有一种O(nlogn)的贪心算法,而且可以输出方案,至于最长下降序列的话也可以有O(nlogn)的贪心实现,目前我先把输出方案弄清楚再去搞最长下降序列的O(nlogn)算法。

具体的方法请见:http://www.matrix67.com/blog/archives/112,http://www.cnblogs.com/celia01/archive/2012/07/27/2611043.html

#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;const int MAXN = 101000;const int INF = 0x3f3f3f3f;int A[MAXN];int S[MAXN], d[MAXN];int n;void init(){for(int i = 1; i <= n; i++) S[i] = INF;S[0] = -INF;d[0] = 0;}int BSearch(int x, int y, int q){while(x <= y){int mid = x+(y-x)/2;if(S[mid] >= q) y = mid-1;else x = mid+1;}return x;}void dp(){init();int ans = 0;for(int i = 1; i <= n; i++){int x = 0, y = i;int pos = BSearch(x, y, A[i]);d[i] = pos;S[d[i]] = min(S[d[i]], A[i]);ans = max(ans, d[i]);}printf("%d\n", ans);}void read_case(){for(int i = 1; i <= n; i++) scanf("%d", &A[i]);}void solve(){read_case();dp();}int main(){while(~scanf("%d", &n)){solve();}return 0;}