题意
给定正整数序列 x 1 ∼ x n x_1 \sim x_n x1∼xn,以下递增子序列均为非严格递增。
- 计算其最长递增子序列的长度 s s s。
- 计算从给定的序列中最多可取出多少个长度为 s s s 的递增子序列。
- 如果允许在取出的序列中多次使用 x 1 x_1 x1 和 x n x_n xn,则从给定序列中最多可取出多少个长度为 s s s 的递增子序列。
题解
如果每个点只能用一次,考虑拆点,变成前点和后点,前点向后点连一条容量为一条的边即可。
第一问直接dp。
第二问考虑,按照上面的方法拆点,如果
d
p
[
i
]
=
=
1
dp[i] == 1
dp[i]==1,则
s
s
s向
i
i
i连容量为1的边,若
d
p
[
i
]
=
=
m
x
l
e
n
dp[i] == mxlen
dp[i]==mxlen,那么
i
+
n
i +n
i+n向
t
t
t连容量为1的边。并且每个
i
i
i连
i
i
i到
i
+
n
i+n
i+n容量为1的边。跑最大流。
第三问把有条件的边容量改为INF即可。
代码
#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
const int nmax = 1e6+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const ull p = 67;
const ull MOD = 1610612741;
int n;
int a[nmax], dp[nmax];
int getdp() {
dp[1] = 1;
for(int i = 1; i <= n; ++i) {
int tmp = 0;
for(int j = 1; j < i; ++j) {
if(a[j] <= a[i]) {
tmp = max(tmp, dp[j]);
}
}
dp[i] = tmp + 1;
}
int ans = 0;
for(int i = 1; i <= n; ++i) {
ans = max(ans, dp[i]);
}
return ans;
}
struct Dinic {
int head[nmax], cur[nmax], d[nmax];
bool vis[nmax];
int tot, n, m, s, t, front, tail;
int qqq[nmax];
struct edge {
int nxt, to, w, cap, flow;
} e[nmax<<1];
void init(int n) {
this->n = n;
memset(head, -1, sizeof head);
memset(cur, 0, sizeof cur);
memset(e,0,sizeof e);
this->tot = 0;
}
int add_edge(int u, int v, int c) {
int temp = tot;
e[tot].to = v, e[tot].cap = c, e[tot].flow = 0;
e[tot].nxt = head[u];
head[u] = tot++;
e[tot].to = u, e[tot].cap = c, e[tot].flow = c;
e[tot].nxt = head[v];
head[v] = tot++;
return temp;
}
bool BFS() {
for(int i = 0; i <= n; ++i) vis[i] = false;
front = tail = 0;
vis[s] = 1; d[s] = 0;
qqq[tail++] = s;
while (front < tail) {
int u = qqq[front++];
for (int i = head[u]; i != -1; i = e[i].nxt) {
int v = e[i].to;
if (!vis[v] && e[i].cap > e[i].flow) {
vis[v] = 1;
d[v] = d[u] + 1;
qqq[tail++] = v;
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0) return a;
int Flow = 0, f;
for (int& i = cur[x]; i != -1; i = e[i].nxt) {
int v = e[i].to;
if (d[v] == d[x] + 1 && (f = DFS(v, min(a, e[i].cap - e[i].flow))) > 0) {
Flow += f;
e[i].flow += f;
e[i ^ 1].flow -= f;
a -= f;
if (a == 0) break;
}
}
return Flow;
}
int Maxflow(int s, int t) {
this->s = s, this->t = t;
int Flow = 0;
while (BFS()) {
for (int i = 0; i <= n; i++) cur[i] = head[i];
Flow += DFS(s,INF);
}
return Flow;
}
} dinic;
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
int s = 0, t = 2 * n + 1;
int mxlen = getdp();
printf("%d\n", mxlen);
dinic.init(t);
for(int i = 1; i <= n; ++i) {
dinic.add_edge(i, i + n, 1);
if(dp[i] == 1)
dinic.add_edge(s, i, 1);
if(dp[i] == mxlen)
dinic.add_edge(i + n, t, 1);
for(int j = i + 1; j <= n; ++j) {
if(dp[j] == dp[i] + 1 && a[j] >= a[i])
dinic.add_edge(i + n, j, 1);
}
}
int mxflow = dinic.Maxflow(s, t);
printf("%d\n", mxflow);
dinic.init(t);
for(int i = 1; i <= n; ++i) {
if(dp[i] == 1) dinic.add_edge(s, i, i == 1? INF : 1);
if(i == 1 || i == n) dinic.add_edge(i, i + n, INF);
else dinic.add_edge(i, i + n, 1);
if(dp[i] == mxlen) dinic.add_edge(i + n, t, i == n ? INF : 1);
for(int j = 1; j <= n; ++j) {
if(dp[j] == dp[i] + 1 && a[j] >= a[i])
dinic.add_edge(i + n, j, 1);
}
}
mxflow = dinic.Maxflow(s, t);
printf("%d\n", mxflow);
return 0;
}