Use array dist[i] to indicate the number of key points contained in point 0 to I-1;
Input data can be converted to: dist[b+1]-dist[a]>=2 = dist[a]-dist[b+1]<=-2
There are also two constraints, depending on the actual conditions:
Dist[i+1]-dist[i]<=1
Dist[i+1]-dist[i]>=0 = dist[i]-dist[i]+1<=0
So you can build a picture, SPFA.
Last output DIST[MAX]-DIST[MIN] (max and Min are the maximum and minimum points that occur for the input data)
/* id:sdj22251 prog:subset lang:c++ * * #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitse t> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> # Include <cctype> #include <map> #include <string> #include <cstring> #include <cmath> #
Include <ctime> #define LOCA #define MAXN 50500 #define INF 1000000000 #define EPS 1e-7 using namespace std; struct Node {int V, W, next;}
EDGE[MAXN * 10];
int HEAD[MAXN], E, MX, MI, D[MAXN], q[120 * MAXN], VIS[MAXN];
void spfa (int src) {for (int i = mi; i <= mx; i++) d[i] = INF;
int h = 0, t = 1;
D[SRC] = 0;
Q[0] = src;
VIS[SRC] = 1;
while (H < T) { int u = q[h++];
Vis[u] = 0;
for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;
int w = EDGE[I].W;
if (D[v] > D[u] + W) {D[v] = D[u] + W;
if (!vis[v]) {q[t++] = V;
VIS[V] = 1;
}}}}} void insert (const int &X, const int &y, const int &w) {edge[e].v = y;
EDGE[E].W = W;
Edge[e].next = Head[x];
HEAD[X] = e++;
} int main () {int n, u, v;
scanf ("%d", &n);
Memset (Head,-1, sizeof (head));
for (int i = 1; I <= n; i++) {scanf ("%d%d", &u, &v);
if (v + 1 > mx) mx = v + 1;
if (Mi > U) mi = u;
Insert (v + 1, u,-2);
Insert (U, v + 1, v-u + 1);
} for (int i = 0; i < mx; i++) {Insert (I, i + 1, 1);
Insert (i + 1, I, 0); } SPFA (MX);
printf ("%d\n", D[mx]-d[mi]);
return 0;
}