Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 2,458 Bytes
d3f4f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <algorithm>
#include <iostream>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

const int LIM = 800008;

int N;
int A[LIM], B[LIM];

void solve() {
  vector<tuple<int, bool, int>> E;
  set<pair<int, int>> S;
  // Input, while finding tallest grounded ladder.
  int ng = 0, mg = -1;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (!A[i]) {
      ng++;
      if (mg < 0 || B[i] > B[mg]) {
        mg = i;
      }
    } else {
      E.emplace_back(A[i], 0, i);
      E.emplace_back(B[i], 1, i);
    }
  }
  // None grounded?
  if (ng == 0) {
    cout << "0 0" << endl;
    return;
  }
  // Sort endpoints (including tallest grounded ladder).
  E.emplace_back(B[mg], 1, mg);
  sort(E.begin(), E.end());

  // Line sweep.
  int ans1 = ng, ans2 = 0;
  int a = mg, b = mg;
  S.clear();
  for (auto e : E) {
    int i = get<2>(e);
    if (!get<1>(e)) {
      // Bottom of ladder i.
      S.emplace(B[i], i);
      continue;
    }
    // Top of ladder i.
    if (B[a] > B[b]) {
      swap(a, b);
    }
    auto it = S.find(make_pair(B[i], i));
    if (it != S.end()) {
      // Ladder i not used yet, so jump from ladder a to it.
      S.erase(it);
      ans1++;
      ans2++;
      a = i;
    } else if (i == b) {
      // Consider ladder i to be ladder a rather than b.
      swap(a, b);
    } else if (i != a) {
      // Ladder i is irrelevant.
      continue;
    }
    // Top of ladder a, so need to jump off of it.
    if (!S.empty()) {
      // There are other unused ladders, so jump from ladder a to the one with
      // lowest top.
      bool isB = a == b;
      a = S.begin()->second;
      S.erase(make_pair(B[a], a));
      ans1++;
      ans2++;
      if (isB) {
        // This is also ladder b, so repeat for it.
        if (!S.empty()) {
          // Jump from ladder b to another unused ladder.
          b = S.begin()->second;
          S.erase(make_pair(B[b], b));
          ans1++;
          ans2++;
        } else {
          // Jump from ladder b to the new ladder a.
          ans2++;
          b = a;
        }
      }
    } else if (a != b) {
      // Jump from ladder a to ladder b.
      ans2++;
      a = b;
    } else {
      // Nowhere else to go, so stop.
      break;
    }
  }
  cout << ans1 << " " << ans2 << endl;
}

int main() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cout << "Case #" << t << ": ";
    solve();
  }
  return 0;
}