| #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; |
| |
| 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); |
| } |
| } |
| |
| if (ng == 0) { |
| cout << "0 0" << endl; |
| return; |
| } |
| |
| E.emplace_back(B[mg], 1, mg); |
| sort(E.begin(), E.end()); |
|
|
| |
| 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)) { |
| |
| S.emplace(B[i], i); |
| continue; |
| } |
| |
| if (B[a] > B[b]) { |
| swap(a, b); |
| } |
| auto it = S.find(make_pair(B[i], i)); |
| if (it != S.end()) { |
| |
| S.erase(it); |
| ans1++; |
| ans2++; |
| a = i; |
| } else if (i == b) { |
| |
| swap(a, b); |
| } else if (i != a) { |
| |
| continue; |
| } |
| |
| if (!S.empty()) { |
| |
| |
| bool isB = a == b; |
| a = S.begin()->second; |
| S.erase(make_pair(B[a], a)); |
| ans1++; |
| ans2++; |
| if (isB) { |
| |
| if (!S.empty()) { |
| |
| b = S.begin()->second; |
| S.erase(make_pair(B[b], b)); |
| ans1++; |
| ans2++; |
| } else { |
| |
| ans2++; |
| b = a; |
| } |
| } |
| } else if (a != b) { |
| |
| ans2++; |
| a = b; |
| } else { |
| |
| break; |
| } |
| } |
| cout << ans1 << " " << ans2 << endl; |
| } |
|
|
| int main() { |
| int T; |
| cin >> T; |
| for (int t = 1; t <= T; t++) { |
| cout << "Case #" << t << ": "; |
| solve(); |
| } |
| return 0; |
| } |
|
|