Test instructions: Given some intervals, the length of a set requires that there are at least two sets in each interval.
Solution: Greedy or differential constraint. Greedy idea is very simple, as long as the interval by the right edge of the order, if the last two elements in the collection is not within the current interval, the number of the last two in the interval is added to the set, if only one element in the interval to add one, if the two elements are not added in the interval.
The differential constraint system is used to solve an inequality group, as long as the inequalities in this inequality group such as X1-X2 <= C,c as constants can be used to solve inequalities with difference constraints, each variable as a point, each inequality as a side, to find the shortest, then dis[i] is an inequality of a set of solutions, The main use of the principle is in the shortest way problem: Dis[v] <= Dis[u] + edge[u][v], and x1-x2 <= C to move the item can get the above form, because of negative rights, so generally with bellmanford or SPFA ... But I will not write SPFA ... If a negative ring is produced, no solution is indicated.
For the subject, each interval endpoint as a point, sum[i] represents the number of the set is less than equals I, then for an interval [L, R] an inequality Sum[r]-sum[l-1] >= 2, converted to the above form is SUM[L-1]-sum[r] <= -2, can be seen as a point R to the l-1 of a weight of 2 of the edge. In addition to the conditions given in the title, there is an implied condition of 0 <= Sum[i]-sum[i-1] <= 1, with the above inequalities to build the shortest way to run, Dis[n]-dis[0] is the answer.
Differential constraints are more complex ... But the main want to practice the difference constraint to do this problem = = But the data range is slightly larger ... It's a bit hard to run Bellmanford ... The last 1000ms grazing ...
Code:
Greedy:
#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #include <iomanip> #define LL Long Long#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1using namespace std;struct node{int L, R; BOOL operator < (const node &TMP) Const {if (R = = TMP.R) return L < TMP.L; return R < TMP.R; }}interval[10005];int Main () {int n; while (~SCANF ("%d", &n)) {for (int i = 0; i < n; i++) scanf ("%d%d", &INTERVAL[I].L, &in TERVAL[I].R); Sort (interval, interval + N); int x =-1, y =-1; int ans = 0; for (int i = 0; i < n; i++) {if (INTERVAL[I].L <= x) continue; if (interval[i].l <= y) {x = y; y = INTERVAL[I].R; ans++; Continue } x = Interval[i].r-1; y = INTERVAL[I].R; Ans + = 2; } cout << ans << endl; } return 0;}
Differential constraint system:
#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #include <iomanip> #define LL Long Long#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1using namespace Std;struct node{int u, V, value; node (int u, int v, int value): U (U), V (v), value (value) {} node () {}}edge[30005];int minn = 10000000, MAXN, M, Cnt;int dis[10005];const int inf = 0x3f3f3f3f;void Bellmanford ()//Because there is no solvable data, no negative ring {memset (dis, 0, sizeof dis) is required; Dis[minn] = 0; BOOL flag = TRUE; while (flag)//pruning, if this time there is no relaxation, then stop relaxation {flag = false; for (int j = 0; J < CNT; J + +) if (DIS[EDGE[J].V] > dis[edge[j].u] + edge[j].value) { Flag = true; DIS[EDGE[J].V] = Dis[edgE[J].U] + edge[j].value; }}}int Main () {while (~SCANF ("%d", &m) {cnt = 0; for (int i = 0; i < m; i++) {int A, B; scanf ("%d%d", &a, &b); Minn = MIN (minn, min (A, B + 1));//record minimum point MAXN = max (MAXN, Max (A, B + 1));//record maximum point edge[cnt++] = node (b + 1, A,-2); } for (int i = minn + 1; I <= maxn; i++) {edge[cnt++] = node (i-1, I, 1); edge[cnt++] = node (i, i-1, 0); } bellmanford (); cout << DIS[MAXN]-Dis[minn] << Endl; } return 0;}
POJ 1716 Integer Intervals