#include #include #include #include #include #include using namespace std;const int maxn = 1000;const int INF = 0x3f3f3f3f;struct Edge { int from, to, cap, flow;};struct Dinic { int s, t; vector edges; vector G[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init() { edges.clear(); for (int i = 0; i < maxn; i++) G[i].clear(); } void addedge(int from, int to, int cap) { edges.push_back(Edge{from, to, cap, 0}); edges.push_back(Edge{to, from, 0, 0}); int x = edges.size(); G[from].push_back(x-2); G[to].push_back(x-1); } bool bfs() { memset(vis, 0, sizeof(vis)); queue q; q.push(s); d[s] = 0; vis[s] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[x] + 1; q.push(e.to); } } } 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 < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i]^1].flow -= f; 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()) { memset(cur, 0, sizeof(cur)); flow += dfs(s, INF); } return flow; }}ac, ac2;int n, a[maxn], dp[maxn];int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) dp[i] = 1; int ans = 1; int s = 0, t = 2 * n + 1; for (int i = n; i >= 1; i--) { for (int j = n; j > i; j--) { if (a[i] < a[j]) dp[i] = max(dp[i], dp[j] + 1); } ans = max(ans, dp[i]); } printf("%d\n", ans); if (ans == 1) { printf("%d\n%d\n", n, n); return 0; } ac.init(); for (int i = 1; i <= n; i++) ac.addedge(i, i+n, 1); for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (a[i] < a[j] && dp[i] == dp[j] + 1) { ac.addedge(i+n, j, 1); } } } for (int i = 1; i <= n; i++) { if (dp[i] == ans) ac.addedge(s, i, 1); } for (int i = 1; i <= n; i++) { if (dp[i] == 1) ac.addedge(i+n, t, 1); } printf("%d\n", ac.maxflow(s, t)); ac2.init(); ac2.addedge(1, 1+n, INF); for (int i = 2; i <= n-1; i++) ac2.addedge(i, i+n, 1); ac2.addedge(n, n+n, INF); for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (a[i] < a[j] && dp[i] == dp[j] + 1) { ac2.addedge(i+n, j, 1); } } } for (int i = 1; i <= n; i++) { if (dp[i] == ans) { if (i == 1) ac2.addedge(s, 1, INF); else ac2.addedge(s, i, 1); } } for (int i = 1; i <= n; i++) { if (dp[i] == 1) { if (i == n) ac2.addedge(n+n, t, INF); else ac2.addedge(i+n, t, 1); } } printf("%d\n", ac2.maxflow(s, t)); return 0;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~