Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2766 最长不下降子序列问题

P2766 最长不下降子序列问题

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define INF INT_MAX
using namespace std;
const int N = 1e3 + 10, M = 2.5e5 + 10;
int n, st, ed, k[N], f[N], d[N], cur[N];
int idx = -1, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
for (int i = st; i <= ed; i++)
d[i] = -1;
d[st] = 0;
cur[st] = hd[st];
queue <int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
{
int ver = edg[i];
if (d[ver] == -1 && wt[i])
{
cur[ver] = hd[ver];
d[ver] = d[t] + 1;
if (ver == ed)
return true;
q.push(ver);
}
}
}
return false;
}
int find(int x, int limit)
{
if (x == ed)
return limit;
int res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
int ver = edg[i];
if (d[ver] == d[x] + 1 && wt[i])
{
int t = find(ver, min(wt[i], limit - res));
if (!t)
d[ver] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = find(st, INF))
res += flow;
return res;
}
void add(int x, int y, int z)
{
nxt[++idx] = hd[x];
hd[x] = idx;
edg[idx] = y;
wt[idx] = z;
}
int main()
{
cin >> n;
st = 0;
ed = n << 1 | 1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1; i <= n; i++)
cin >> k[i];
int s = 0;
for (int i = 1; i <= n; i++)
{
add(i, i + n, 1);
add(i + n, i, 0);
f[i] = 1;
for (int j = 1; j < i; j++)
if (k[j] <= k[i])
f[i] = max(f[i], f[j] + 1);
for (int j = 1; j < i; j++)
if (k[j] <= k[i] && f[j] + 1 == f[i])
{
add(j + n, i, 1);
add(i, j + n, 0);
}
s = max(s, f[i]);
if (f[i] == 1)
{
add(st, i, 1);
add(i, st, 0);
}
}
for (int i = 1; i <= n; i++)
if (f[i] == s)
{
add(n + i, ed, 1);
add(ed, n + i, 1);
}
cout << s << endl;
if (s == 1)
{
cout << n << endl << n;
return 0;
}
int res = dinic();
cout << res << endl;
for (int i = 0; i <= idx; i += 2)
if ((edg[i ^ 1] == st && edg[i] == 1)
|| (edg[i ^ 1] == 1 && edg[i] == n + 1)
|| (edg[i ^ 1] == n && edg[i] == n + n)
|| (edg[i ^ 1] == n + n && edg[i] == ed))
wt[i] = INF;
cout << res + dinic();
return 0;
}