Submission
Status:
[PP-SS][SSSSS][SSSSSSSSSS]
Score: 0
User: dwad2
Problemset: ห้องสมุดเมือง 3M
Language: cpp
Time: 0.002 second
Submitted On: 2025-03-20 20:30:25
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int>> libraries(n);
for (int i = 0; i < n; i++) {
cin >> libraries[i].first >> libraries[i].second;
}
sort(libraries.begin(), libraries.end());
vector<pair<int, int>> merged;
for (auto [x, y] : libraries) {
if (merged.empty() || merged.back().second < x) {
merged.push_back({x, y});
} else {
merged.back().second = max(merged.back().second, y);
}
}
long long total_books = 0;
for (auto [x, y] : merged) {
total_books += (y - x);
}
long long medianIndex = total_books / 2;
long long count = 0;
for (auto [x, y] : merged) {
long long books_in_range = y - x;
if (count + books_in_range > medianIndex) {
cout << x + (medianIndex - count) - 1 << endl;
return 0;
}
count += books_in_range;
}
return 0;
}