1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>
#include <utility>
#include <algorithm>

#define MAX 100010

std::pair<int, int> seg[MAX], sel[MAX];

int main() {
     int T, n, m, i, j, k, cov, use;
     scanf("%d", &T);
     while (T--) {
          scanf("%d", &m);
          n = 0;
          while (scanf("%d%d", &i, &j) && (i || j) ) {
               if (j<0 || i>m) continue;
               seg[n].first = i;
               seg[n].second = j;
               n++;
          }
          std::sort(seg, seg+n);
          use = 0;
          for (i=cov=0; i<n && cov<m; i=j) {
               for (j=i,k=-1; j<n && seg[j].first<=cov; j++)
                    if (k<0 || seg[j].second>seg[k].second) k = j;
               if (k < 0) break;
               sel[use++] = seg[k];
               cov = seg[k].second;
          }
          if (cov >= m) {
               printf("%d\n", use);
               for (i=0; i<use; i++)
                    printf("%d %d\n", sel[i].first, sel[i].second);
          } else puts("0");
          if ( T ) putchar('\n');
     }
     return 0;
}