Sequence
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 889 Accepted Submission(s): 308
Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X as x[i1], x[i2],...,x[ik], which satisfies follow conditions: 1) x[i1] < x[i2],...,<x[ik]; 2) 1<=i1 < i2,...,<ik<=n
As an excellent program designer, you must know how to find the maximum length of the increasing sequense, which is defined as s. Now, the next question is how many increasing subsequence with s-length can you find out from the sequence X.
For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X. 1) A = a1, a2, a3. B = b1, b2, b3. 2) Each ai or bj(i,j = 1,2,3) can only be chose once at most.
Now, the question is: 1) Find the maximum length of increasing subsequence of X(i.e. s). 2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).
Input
The input file have many cases. Each case will give a integer number n.The next line will have n numbers.
Output
The output have two line. The first line is s and second line is num.
Sample Input
4 3 6 2 5
Sample Output
2 2
思路:思路跟最短路径+最大流是一样的,用最大流去限定,求出最短路径的个数。同样,求出最长上升子序列,再用最大流去限定,求出的结果就是最长上升子序列的个数。求LCIS则用常规方法,既定义数组dp[i]是表示以 i 结尾的最长上升子序列的长度,求出最长序列ans。由于数列中的每一个数只能使用一次,构图的时候需要拆点。若有n个数,则拆成2 * n个点,构造源点和汇点,将每个点都和自己拆出来的点连边,将源点和最长序列为1的点连边,将最长序列为ans的点与汇点连边,最后若dp[j] = dp[i] + 1,则将i + n和 j连边。所有边的流量都是1,这样便可以限制每个点只使用一次。其实连边的顺序就是最长序列为1,2,3,...,ans。可以理解为从最长序列为1(该数本身)一直流到数列中的最长上升序列。画出图来就好理解了。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define SIZE 8888 6 #define inf 0xfffffff 7 8 using namespace std; 9 10 struct node 11 { 12 int to,val,next; 13 }; 14 15 node edge[SIZE*88]; 16 int head[SIZE],idx; 17 18 int dp[SIZE]; 19 int X[SIZE]; 20 int n,source,sink,point; 21 int dis[SIZE],gap[SIZE]; 22 23 void addNode(int from,int to,int val) 24 { 25 edge[idx].to = to; 26 edge[idx].val = val; 27 edge[idx].next = head[from]; 28 head[from] = idx ++; 29 edge[idx].to = from; 30 edge[idx].val = 0; 31 edge[idx].next = head[to]; 32 head[to] = idx ++; 33 } 34 35 void init() 36 { 37 memset(head,-1,sizeof(head)); 38 idx = 0; 39 } 40 41 int lcis() 42 { 43 for(int i=1; i<=n; i++) 44 dp[i] = 1; 45 int ans = 0; 46 for(int i=2; i<=n; i++) 47 for(int j=1; j<i; j++) 48 if(X[i] > X[j] && dp[j] + 1 > dp[i]) 49 dp[i] = dp[j] + 1; 50 for(int i=1; i<=n; i++) 51 ans = max(ans,dp[i]); 52 return ans; 53 } 54 55 int dfs(int cur,int cur_val) 56 { 57 if(cur == sink) 58 return cur_val; 59 int min_dis = point - 1, temp_val = cur_val; 60 for(int i=head[cur]; i!=-1; i=edge[i].next) 61 { 62 int to = edge[i].to, to_val = edge[i].val; 63 if(to_val > 0) 64 { 65 if(dis[cur] == dis[to] + 1) 66 { 67 int val = min(edge[i].val,temp_val); 68 val = dfs(to,val); 69 temp_val -= val; 70 edge[i].val -= val; 71 edge[i^1].val += val; 72 if(dis[source] >= point) 73 return cur_val - temp_val; 74 if(temp_val == 0) 75 break; 76 } 77 if(min_dis > dis[to]) 78 min_dis = dis[to]; 79 } 80 } 81 if(cur_val == temp_val) 82 { 83 --gap[dis[cur]]; 84 if(gap[dis[cur]] == 0) 85 dis[source] = point; 86 dis[cur] = min_dis + 1; 87 ++gap[dis[cur]]; 88 } 89 return cur_val - temp_val; 90 } 91 92 int sap() 93 { 94 memset(gap,0,sizeof(gap)); 95 memset(dis,0,sizeof(dis)); 96 gap[source] = point; 97 int ret = 0; 98 while(dis[source] < point) 99 ret += dfs(source,inf); 100 return ret; 101 } 102 103 int main() 104 { 105 while(~scanf("%d",&n)) 106 { 107 for(int i=1; i<=n; i++) 108 scanf("%d",&X[i]); 109 int ans = lcis(); 110 init(); 111 source = 0; 112 sink = 2 * n + 1; 113 point = sink + 1; 114 for(int i=1; i<=n; i++) 115 { 116 addNode(i,i+n,1); 117 if(dp[i] == 1) 118 addNode(source,i,1); 119 if(dp[i] == ans) 120 addNode(i+n,sink,1); 121 for(int j=i+1; j<=n; j++) 122 if(dp[j] == dp[i] + 1) 123 addNode(i+n,j,1); 124 } 125 int num = sap(); 126 printf("%d\n%d\n",ans,num); 127 } 128 return 0; 129 }