摘要: http://poj.org/problem?id=1631DP最长上升子序列最长上升子序列,因为n很大(n<40000),我开始写的O(n^2)的算法超时了。。。学了一下O(n*logn)的方法,dp[i]表示当前状态下,所有长度为i的子序列中,末尾元素最小的那个子序列,的末尾元素值。例如序列:a[6]:(1,3,5,4,6,2)考虑到前6个元素的时候长度为3的子序列有:(1,3,5) (1,3,4) (1,3,6) (3,5,6) (3,4,6) 那么dp[3] = 4;长度为4的子序列有:(1,3,5,6) (1,3,4,6) 那么dp[4] = 6;开始时dp数组为空,依次遍历原 阅读全文
posted @ 2012-12-06 21:22 Yuan1991 阅读(138) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=1659贪心自己YY了一个贪心,AC了。。。后来才知道,竟然还是个定理。。。“Havel-Hakimi定理” 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct A 6 { 7 int n, x; 8 }a[12]; 9 10 int map[12][12];11 12 int cmp(const void *a1, const void *b1)13 {14 return (*(struct A *)b1) 阅读全文
posted @ 2012-12-06 13:55 Yuan1991 阅读(158) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=1363数据结构 栈用2个栈模拟,复杂度O(n) (每个元素最多进栈(s2)一次,出栈一次)s1弹出的元素压入s2,s2再弹出来一定要按照顺序n,n-1,n-2...4,3,2,1想办法让s2按此顺序弹出,如果不能,则输出"No" 1 #include <stdio.h> 2 #include <stack> 3 4 using namespace std; 5 6 stack<int> s1, s2; 7 8 void clear() 9 {10 while(!s1.empty())1 阅读全文
posted @ 2012-12-06 13:17 Yuan1991 阅读(200) 评论(0) 推荐(0)