HDU3998 Sequence 动态规划+最大流2011 Multi-University Training Contest 16 - Host by TJU

 

Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 138 Accepted Submission(s): 43


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

Source
2011 Multi-University Training Contest 16 - Host by TJU

Recommend
lcy
 
第一问经典的LIS就不说了
第二问LIS不重复计数,用最大流解决
建图:
把每个数当做一个点
d[i]=1则s到i连边
d[i]=ans则i到t连边
如果d[j]=d[i]+1&&a[i]<a[j]则i到j连边
所有的边权都为1
 
代码:

#include<cstdio> #include<cstring> #include<vector> #include<algorithm> #define N 1005 #define M 100005 #define inf 999999999 using namespace std;

int n,m,s,t,num,adj[N],dis[N],q[N],a[N],d[N]; struct edge {  int v,w,pre; }e[M]; void insert(int u,int v,int w) {  e[num]=(edge){v,w,adj[u]};  adj[u]=num++;  e[num]=(edge){u,0,adj[v]};  adj[v]=num++; } int bfs() {  int i,x,v,head=0,tail=0;  memset(dis,0,sizeof(dis));  dis[s]=1;  q[++tail]=s;  while(head!=tail)  {   x=q[head=(head+1)%N];   for(i=adj[x];~i;i=e[i].pre)    if(e[i].w&&!dis[v=e[i].v])    {     dis[v]=dis[x]+1;     if(v==t)      return 1;     q[tail=(tail+1)%N]=v;    }  }  return 0; } int dfs(int x,int limit) {  if(x==t)   return limit;  int i,v,tmp,cost=0;  for(i=adj[x];~i&&cost<limit;i=e[i].pre)   if(e[i].w&&dis[x]==dis[v=e[i].v]-1)   {    tmp=dfs(v,min(limit-cost,e[i].w));    if(tmp)    {     e[i].w-=tmp;     e[i^1].w+=tmp;     cost+=tmp;    }    else     dis[v]=-1;   }  return cost; } int Dinic() {  int ans=0;  while(bfs())   ans+=dfs(s,inf);  return ans; } int main() {  while(~scanf("%d",&n))  {   int i,j,ans=0;   memset(d,0,sizeof(d));   memset(adj,-1,sizeof(adj));   num=0;   for(i=1;i<=n;i++)    scanf("%d",&a[i]);   for(i=1;i<=n;i++)    for(j=0;j<i;j++)     if(a[i]>a[j])     {      d[i]=max(d[i],d[j]+1);      ans=max(ans,d[i]);     }   s=0;   t=n+1;     for(i=1;i<=n;i++)   {    if(d[i]==1)     insert(s,i,1);    if(d[i]==ans)     insert(i,t,1);    for(j=i+1;j<=n;j++)     if(d[j]==d[i]+1&&a[i]<a[j])      insert(i,j,1);   }   printf("%d\n%d\n",ans,Dinic());  } }


你可能感兴趣的:(HDU3998 Sequence 动态规划+最大流2011 Multi-University Training Contest 16 - Host by TJU)