标签:space ++i href tps opera const 序列 ++ 选择
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b)
{
ll res = 1;
for (; b; b >>= 1)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
struct graph
{
int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz;
void init()
{
memset(head, -1, sizeof(head));
sz = 0;
}
graph() { init(); }
void push(int a, int b, int c) { nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++; }
int &operator[](const int a) { return to[a]; }
} g;
int d[1005], now[1005];
int s, t;
bool bfs()
{
memset(d, 0, sizeof(d));
queue<int> q;
q.push(s);
d[s] = 1;
now[s] = g.head[s];
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = g.head[x]; ~i; i = g.nxt[i])
{
if (!d[g[i]] && g.w[i])
{
d[g[i]] = d[x] + 1;
now[g[i]] = g.head[g[i]];
q.push(g[i]);
if (g[i] == t)
return 1;
}
}
}
return 0;
}
int dinic(int x, int flow)
{
if (x == t)
return flow;
int res = flow, i, k;
for (i = now[x]; ~i && res; i = g.nxt[i])
{
if (d[g[i]] == d[x] + 1 && g.w[i])
{
k = dinic(g[i], min(res, g.w[i]));
if (!k)
d[g[i]] = 0;
res -= k;
g.w[i] -= k;
g.w[i ^ 1] += k;
}
}
now[x] = i;
return flow - res;
}
int dp[505], a[505];
int main()
{
#ifndef ONLINE_JUDGE
freopen("simple.in", "r", stdin);
freopen("simple.out", "w", stdout);
#endif
int n, k = 0;
scanf("%d", &n);
s = 0, t = n * 2 + 1;
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i)
{
dp[i] = 1;
for (int j = 1; j < i; ++j)
{
if (a[i] >= a[j])
{
dp[i] = max(dp[i], dp[j] + 1);
}
}
k = max(k, dp[i]);
}
if (k == 1)
{
printf("%d\n%d\n%d\n", k, n, n);
return 0;
}
for (int i = 1; i <= n; ++i)
{
g.push(i, i + n, 1), g.push(i + n, i, 0);
if (dp[i] == 1)
{
g.push(s, i, 1), g.push(i, s, 0);
}
if (dp[i] == k)
{
g.push(i + n, t, 1), g.push(t, i + n, 0);
}
for (int j = 1; j < i; ++j)
{
if (a[i] >= a[j] && dp[i] == dp[j] + 1)
{
g.push(n + j, i, 1), g.push(i, n + j, 0);
}
}
}
int flow, ans2 = 0;
while (bfs())
{
while (flow = dinic(s, 1e9))
ans2 += flow;
}
g.init();
for (int i = 1; i <= n; ++i)
{
int w = (i == 1 || i == n) ? 1e9 : 1;
g.push(i, i + n, w), g.push(i + n, i, 0);
if (dp[i] == 1)
{
g.push(s, i, w), g.push(i, s, 0);
}
if (dp[i] == k)
{
g.push(i + n, t, w), g.push(t, i + n, 0);
}
for (int j = 1; j < i; ++j)
{
if (a[i] >= a[j] && dp[i] == dp[j] + 1)
{
g.push(n + j, i, 1), g.push(i, n + j, 0);
}
}
}
int f, ans3 = 0;
while (bfs())
{
while (f = dinic(s, 1e9))
ans3 += f;
}
cout << k << endl
<< ans2 << endl
<< ans3 << endl;
return 0;
}
标签:space ++i href tps opera const 序列 ++ 选择
原文地址:https://www.cnblogs.com/aaddvvaanntteezz/p/13034756.html