Test instructions: There are n lines of length 1, to determine their starting point, so that the first segment in the [Ri,di], the output of the minimum number of voids.
Analysis: Very obvious greedy problem, greedy strategy is like this, first put all the interval sort, the principle is to sort by the right end point, if equal again by the left end row, and then sweep again, if the right end of the first interval and the second right end of the same endpoint,
Must be adjacent, if not equal, and then see whether it is the size of the left end of the relationship, if less than the left end, then it will certainly produce voids, if not less than, you can, then the end of the right to move a unit, the other kind of judgment.
The code is as follows:
#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream > #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector > #include <map> #include <cctype>using namespace std; typedef long Long Ll;typedef pair<int, int> p;c onst int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f3f;const double EPS = 1e-8;const int maxn = 1e5 + 5;const int D R[] = {0, 0,-1, 1};const int dc[] = {-1, 1, 0, 0};int N, m;inline bool is_in (int r, int c) {return R >= 0 && ; R < n && C >= 0 && C < m;} struct node{int x, y; BOOL operator < (const node &p) const{return y < P.Y | | (y = = p.y && x < p.x); }};node A[maxn];int Main () {int T; Cin >> T; while (t--) {scanf ("%d", &n); for (int i = 0; i < n; ++i) scanf ("%d%d", &a[i].x, &A[I].Y); Sort (A, a+n); int ans= 0; int r = A[0].Y; for (int i = 1; i < n; ++i) {if (R = = A[I].Y) continue; if (R < a[i].x) {++ans; R = a[i].y; } else ++r; } printf ("%d\n", ans); } return 0;}
UVa 1617 Laptop (greedy)