QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446820 | #6308. Magic | james1BadCreeper | TL | 0ms | 0kb | C++17 | 1.4kb | 2024-06-17 16:37:51 | 2024-06-17 16:37:51 |
answer
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 1e4 + 5;
int n, S, T, d[N];
bitset<N> g[N];
int bfs(void) {
memset(d, -1, sizeof d); d[S] = 1;
queue<int> q; q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v = g[u]._Find_first(); v != g[u].size(); v = g[u]._Find_next(v))
if (d[v] == -1) d[v] = d[u] + 1, q.push(v);
}
return d[T];
}
int dinic(int u, int res) {
if (u == T) return res;
int flow = 0;
for (int v = g[u]._Find_first(); v != g[u].size() && res; v = g[u]._Find_next(v)) {
if (d[v] == d[u] + 1) {
int k = dinic(v, min(res, 1));
flow += k, res -= k;
if (k) g[u][v] = 0, g[v][u] = 1;
}
}
if (!flow) d[u] = -1;
return flow;
}
int l[N], r[N], pos[N];
int main(void) {
ios::sync_with_stdio(0);
cin >> n; S = 0, T = 2 * n + 1;
for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i], pos[l[i]] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (l[i] < l[j] && l[j] < r[i] && r[i] < r[j])
g[l[j]][r[i]] = 1;
for (int i = 1; i <= 2 * n; ++i)
if (pos[i] == 1) g[S][i] = 1;
else g[i][T] = 1;
int ans = 2 * n;
while (bfs()) ans -= dinic(S, 1e9);
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
5 2 3 6 7 1 9 5 10 4 8