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:
- x[i1] < x[i2],…,<x[ik];
- 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.
- A = a1, a2, a3. B = b1, b2, b3.
- Each ai or bj(i,j = 1,2,3) can only be chose once at most.
Now, the question is:
- Find the maximum length of increasing subsequence of X(i.e. s).
- 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
因为每个点只能用一次,
每个点拆为入点和出点,之间容量为1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e4+5;
namespace NetFlow{
const int MAXN=2e5+5,MAXM=2e5+5,INF=0x3f3f3f3f;//点数/边数
struct Edge{
int to,next,cap,flow;
}edge[MAXM];
int tol,head[MAXN],gap[MAXN],dep[MAXN],cur[MAXN],Q[MAXN],S[MAXN];
void init(){tol=0;memset(head,-1,sizeof(head));}
void link(int u,int v,int w){
edge[tol]={v,head[u],w,0},head[u]=tol++;
edge[tol]={u,head[v],0,0},head[v]=tol++;
}
void BFS(int start,int end){
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=1,dep[end]=0;
int front=0,rear=0;
Q[rear++]=end;
while(front!=rear){
int u=Q[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dep[v]!=-1)continue;
Q[rear++]=v,dep[v]=dep[u]+1;
++gap[dep[v]];
}
}
}
int ISAP(int start,int end,int N){//源点/汇点/点数
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top=0,u=start,maxflow=0;
while(dep[start]<N){
if(u==end){
int Min=INF,inser;
for(int i=0;i<top;++i)if(Min>edge[S[i]].cap-edge[S[i]].flow)
Min=edge[S[i]].cap-edge[S[i]].flow,inser=i;
for(int i=0;i<top;++i)edge[S[i]].flow+=Min,edge[S[i]^1].flow-=Min;
maxflow+=Min,top=inser,u=edge[S[top]^1].to;
continue;
}
bool flag=false;int v;
for(int i=cur[u];i!=-1;i=edge[i].next){
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
flag=true,cur[u]=i;break;
}
}if(flag){
S[top++]=cur[u];
u=v;continue;
}
int Min=N;
for(int i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
Min=dep[edge[i].to],cur[u]=i;
--gap[dep[u]];
if(!gap[dep[u]])return maxflow;
dep[u]=Min+1;++gap[dep[u]];
if(u!=start)u=edge[S[--top]^1].to;
}
return maxflow;
}
}using namespace NetFlow;
int n,a[N],dp[N],len;
int main(){
while(~scanf("%d",&n)){
init();len=0;
int T=n<<1|1;
for(int i=1;i<=n;++i)scanf("%d",a+i),link(i,i+n,1),dp[i]=1;
for(int i=1;i<=n;len=max(len,dp[i++]))
for(int j=1;j<i;++j)if(a[j]<a[i])dp[i]=max(dp[i],dp[j]+1);
for(int i=1;i<=n;++i){
if(dp[i]==1)link(0,i,1);
if(dp[i]==len)link(i+n,T,1);
for(int j=i+1;j<=n;++j)if(dp[j]==dp[i]+1&&a[j]>a[i])link(i+n,j,1);
}
printf("%d\n%d\n",len,ISAP(0,T,T+1));
}
return 0;
}