题目链接:http://codeforces.com/problemset/problem/589/F
题意:以[L, R]的形式给出N个区间,在每个区间内需要选出相同长度的子区间,子区间可以不连续且每段只能被选一次,求该长度与N的乘积
思路:考虑二分该长度,先将所给出的区间端点离散并编号作为二分图的右部,然后源点到左部N个点连边,容量为二分值mid,然后对于左部每个点,如果它所代表的区间在右部点所代表的线段内,那就连边容量为INF,右部点到汇点连边,容量为该线段的长度,最后判断是否满流
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#include <string>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
const int MAXN = 1100;
const int MAXM = 1000100;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, next, cap, flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
void init()
{
tol = 2;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int rw = 0)
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
int dep[MAXN], cur[MAXN], sta[MAXN];
bool bfs(int s, int t, int n)
{
int front = 0, tail = 0;
memset(dep, -1, sizeof(dep[0]) * (n + 1));
dep[s] = 0;
Q[tail++] = s;
while (front < tail) {
int u = Q[front++];
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dep[v] == -1) {
dep[v] = dep[u] + 1;
if (v == t)return true;
Q[tail++] = v;
}
}
}
return false;
}
int dinic(int s, int t, int n)
{
int maxflow = 0;
while (bfs(s, t, n)) {
for (int i = 0; i < n; i++)cur[i] = head[i];
int u = s, tail = 0;
while (cur[s] != -1) {
if (u == t) {
int tp = INF;
for (int i = tail - 1; i >= 0; i--)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
maxflow += tp;
for (int i = tail - 1; i >= 0; i--) {
edge[sta[i]].flow += tp;
edge[sta[i] ^ 1].flow -= tp;
if (edge[sta[i]].cap - edge[sta[i]].flow == 0)
tail = i;
}
u = edge[sta[tail] ^ 1].to;
}
else if (cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) {
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else {
while (u != s && cur[u] == -1)
u = edge[sta[--tail] ^ 1].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
}
vector <int> s;
set <int> ss;
int n;
struct node
{
int s, t;
} a[MAXN];
bool ok(int mid)
{
int m = s.size();
int S = 0, T = n + m + 1;
init();
for (int i = 1; i <= n; i++) addedge(S, i, mid);
for (int i = 1; i <= n; i++)
for (int j = 0; j < m - 1; j++)
{
if (a[i].s <= s[j] && s[j + 1] <= a[i].t)
addedge(i, n + j + 1, INF);
}
for (int j = 0; j < m - 1; j++) addedge(n + j + 1, T, s[j + 1] - s[j]);
int res = dinic(S, T, T + 1);
return res == mid * n;
}
int main()
{
while (cin >> n)
{
ss.clear();
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].s, &a[i].t);
ss.insert(a[i].s), ss.insert(a[i].t);
}
set <int> :: iterator it;
for (it = ss.begin(); it != ss.end(); it++) s.push_back(*it);
int l = 0, r = 10000;
while (l <= r)
{
int mid = (l + r) >> 1;
if (ok(mid))
l = mid + 1;
else
r = mid - 1;
}
cout << r * n << endl;
}
return 0;
}