第一问可以直接DP来做,联想上一题,线性规划都可以化为网络流?我们可以借助第一问的DP数组,来建立第二问第三问的网络流图,考虑每一种可能,都是dp数组中满足num[i]>=num[j]&&dp[i]=dp[j]+1(i>j),每一种可能都是从dp为1的点递增到dp为第一问的值的点,那么我们就设一个源点一个汇点,每个源点向dp为1的点连capacity为1的边,每个dp为第一问答案的点向汇点连capacity为1的边,每一个满足dp条件,即num[i]>=num[j]&&dp[i]=dp[j]+1(i>j),从j向i连一条capacity为1的边,跑最大流即可,但是,我们注意到,题目要求是不同的,不重复的,而我们的做法无法考虑一个点是否重复使用,举个例子(丑图上
在这种情况下,第一个节点重复使用了,显然不满足题意,那我们怎么做呢,要满足不重复的条件,可以把每个点拆成入点和出点,入点向出点连一条capacity为1的边,就能完美的保证每个点只使用一次啦,相同情况如下,能保证只使用一次
#include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x)&(-x)) typedef long long LL; const int maxm = 3e3+5; const int INF = 0x3f3f3f3f; struct edge{ int u, v, cap, flow, nex; } edges[maxm]; int head[maxm], cur[maxm], cnt, level[1005], buf[505], dp[505]; void init() { memset(head, -1, sizeof(head));cnt = 0; } void add(int u, int v, int cap) { edges[cnt] = edge{u, v, cap, 0, head[u]}; head[u] = cnt++; } void addedge(int u, int v, int cap) { add(u, v, cap), add(v, u, 0); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> q; level[s] = 0; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edges[i].nex) { edge& now = edges[i]; if(now.cap > now.flow && level[now.v] < 0) { level[now.v] = level[u] + 1; q.push(now.v); } } } } int dfs(int u, int t, int f) { if(u == t) return f; for(int& i = cur[u]; i != -1; i = edges[i].nex) { edge& now = edges[i]; if(now.cap > now.flow && level[u] < level[now.v]) { int d = dfs(now.v, t, min(f, now.cap - now.flow)); if(d > 0) { now.flow += d; edges[i^1].flow -= d; return d; } } } return 0; } int dinic(int s, int t) { int maxflow = 0; for(;;) { bfs(s); if(level[t] < 0) break; memcpy(cur, head, sizeof(head)); int f; while((f = dfs(s, t, INF)) > 0) maxflow += f; } return maxflow; } void run_case() { int n; init(); cin >> n; int s = 0, t = (n<<1)+2; for(int i = 1; i <= n; ++i) { cin >> buf[i]; dp[i] = 1; } for(int i = 1; i <= n; ++i) for(int j = 1; j < i; ++j) if(buf[i] >= buf[j]) dp[i] = max(dp[i], dp[j] + 1); int ans = 0; for(int i = 1; i <= n; ++i) ans = max(ans, dp[i]); cout << ans << "\n"; for(int i = 1; i <= n; ++i) { for(int j = 1; j < i; ++j) { if(buf[i] >= buf[j] && dp[i] == dp[j]+1) addedge((j<<1)|1, i<<1, 1); } addedge(i<<1, (i<<1)|1, 1); if(dp[i] == 1) addedge(s, i<<1, 1); if(dp[i] == ans) addedge((i<<1)|1, t, 1); } int sum = dinic(s, t); cout << sum << "\n"; addedge(2, 3, INF), addedge(n<<1, (n<<1)|1, INF); if(dp[1] == 1) addedge(s, 2, INF); if(dp[n] == ans) addedge((n<<1)|1, t, INF); int threequestion = dinic(s, t); sum += threequestion==INF?0:threequestion; cout << sum << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(0); run_case(); cout.flush(); return 0; }
来源:https://www.cnblogs.com/GRedComeT/p/12285559.html