swhsz @ 2018-07-04 20:04:13
我第二遍dinic的时候给ecnt赋值是0,用的++ecnt,为什么能A。。。很奇怪,求解答。。
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::memset;
using std::max;
using std::queue;
using std::memcpy;
const int inf=0x3f3f3f3f;
const int N=3005,S=0,T=1001;
int n,a[N],f[N],ecnt=1,head[N],cur[N];
struct Edge {
int to,nxt,val;
} e[N<<2];
void add(int bg,int ed,int val) {
e[++ecnt].nxt=head[bg];
e[ecnt].to=ed;
e[ecnt].val=val;
head[bg]=ecnt;
}
void insert(int a,int b,int v) {
add(a,b,v);
add(b,a,0);
}
int h[N];
queue<int>q;
bool bfs() {
memset(h,-1,sizeof h);
q.push(S);
h[S]=0;
while(!q.empty()) {
int x=q.front();
q.pop();
for(int i=head[x]; i; i=e[i].nxt) {
if(e[i].val&&h[e[i].to]==-1) {
h[e[i].to]=h[x]+1;
q.push(e[i].to);
}
}
}
return h[T]!=-1;
}
int dfs(int x,int f) {
if(x==T)return f;
int tp,used=0;
for(int i=cur[x]; i; i=e[i].nxt) {
if(e[i].val&&h[e[i].to]==h[x]+1) {
tp=dfs(e[i].to,std::min(e[i].val,f-used));
e[i].val-=tp;
if(e[i].val)cur[x]=i;
e[i^1].val+=tp;
used+=tp;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int maxflow;
void dinic() {
maxflow=0;
while(bfs()) {
memcpy(cur,head,sizeof head);
maxflow+=dfs(S,inf);
}
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
for(int i=1; i<=n; i++) {
f[i]=1;
for(int j=1; j<i; j++)
if(a[i]>=a[j]) {
insert(j+500,i,1);
f[i]=max(f[i],f[j]+1);
f[n+1]=max(f[n+1],f[i]);
}
}
f[n+1]=max(f[n+1],1);
printf("%d\n",f[n+1]);
if(f[n+1]==1){
printf("%d\n%d",n,n);return 0;
}
for(int i=1; i<=n; i++) {
insert(i,i+500,1);
if(f[i]==f[n+1]) insert(i+500,T,1);
if(f[i]==1) insert(0,i,1);
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++) {
if(a[j]>=a[i]&&f[j]==f[i]+1) insert(i+500,j,1);
}
dinic();
printf("%d\n",maxflow);
memset(head,0,sizeof head);ecnt=0;
for(int i=1,v;i<=n;i++) {
v=1;
if(i==1||i==n) v=inf;
insert(i,i+500,v);
if(f[i]==f[n+1]) insert(i+500,T,v);
if(f[i]==1) insert(0,i,v);
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(a[j]>=a[i]&&f[j]==f[i]+1) insert(i+500,j,1);
dinic();
printf("%d",maxflow);
}
by swhsz @ 2018-07-06 17:25:00
@GhostCai
by GhostCai @ 2018-07-06 18:34:53
@Menteur_Hxy
by 醉语梦 @ 2018-07-06 18:40:58
@妖怪吧
by Monster_Qi @ 2018-07-06 19:19:45
@radish布団
by radish布団 @ 2018-07-06 21:33:02
@蒟蒻hsz
by GhostCai @ 2018-07-07 15:23:35
by swhsz @ 2018-07-07 15:23:50
@GhostCai Orz CZQ
by 四方契 @ 2018-07-08 16:42:49
@ GhostCai Orz CZQ