Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 138 Accepted Submission(s): 43
#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()); } }