先用 dp 求得最长上升子序列 解决题(1)
然后每个数拆点,连一个流量为1 的边, 对于 a[i] <= a[j] 且 i < j,且 dp[i] + 1 = dp[j] 连一条 i - j+n 流量为 1 的边,
对于 dp[i] = 1, s -----INF-->i 对于dp[i] = LIS i+n ----INF---> t, ans = dinic(s,t); 解决题(2)
对于题(3) 将 1 和 n 拆点自边 的流量从1 改成 INF 就可以了, ans += dinic(s,t); 解决题(3)
#include
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1500 + 10;
const int maxm = 1000000 + 10;
int n,m,k;
int l[maxn];//记录层数
int h[maxn];//链式前向星
int cur[maxn];
int tot = 0;
struct edge
{
int to;
int c;
int next;
edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
}es[maxm*2];//记录边 注意是2倍
void add_edge(int u, int v, int c)
{
es[tot] = edge(v,c,h[u]);
h[u] = tot++;
es[tot] = edge(u,0,h[v]);
h[v] = tot++;
//cout << u <<" " < q;
q.push(s);
while(!q.empty())
{
int u = q.front();
//cout << u <= a[j]) dp[i] = max(dp[i],dp[j]+1);
maxa = max(maxa,dp[i]);
}
tot = 0;
memset(h,-1,sizeof(h));
int s = 0, t = 2*n+1;
add_edge(1,1+n,1);
add_edge(n,n+n,1);
for(int i = 1; i <= n; i++)
{
if(i != 1 && i != n) add_edge(i,i+n,1);
if(dp[i] == 1) add_edge(s,i,INF);
if(dp[i] == maxa) add_edge(i+n,t,INF);
for(int j = i+1; j <= n; j++)
if(dp[j] == dp[i]+1 &&a[i] <= a[j]) add_edge(i+n,j,1);
}
if(n == 1) {printf("1\n1\n1\n");return 0;}
if(maxa == 1) {printf("%d\n%d\n%d\n",1,n,n);return 0;}
printf("%d\n",maxa);
int res = dinic(s,t);
printf("%d\n",res);
es[0].c = INF;
es[2].c = INF;
res += dinic(s,t);
printf("%d\n",res);
return 0;
}