Submission

Status:

[PPPPP][PPPPP][PPPPPPPPPP]

Subtask/Task Score:

{20/20}{30/30}{50/50}

Score: 100

User: someone

Problemset: ห้องสมุดเมือง 3M

Language: cpp

Time: 0.008 second

Submitted On: 2026-03-06 11:05:33

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, maxSize = 0;
    cin >> n;
    map<int, int> book;
    int first = 0;
    // vector<int> temp;
    for (int i = 0 ; i < n ; i++) {
        int x, y;
        cin >> x >> y;
        book[x]++;
        book[y]--;
        maxSize += y-x;
        first = x;
        // for (int j = x ; j < y ; j++) temp.push_back(j);
    }
    if (maxSize == 1) {
        cout << first;
        return 0;
    }
    // sort(temp.begin(), temp.end());
    // cout << "maxSize : " << maxSize << endl;
    int prev = 0;
    int cntBook = 0;
    int cnt = 0;
    // for (int i = 0 ; i < maxSize ; i++) cout << temp[i] << ' ';
    // cout << endl;
    for (auto p : book) {
        int index = p.first;
        // cout << index << ' ' << cnt << endl;
        cntBook += (index-prev)*cnt;
        // cout << "cntBook : " << cntBook << endl;
        if (cntBook >= maxSize/2) {
            cntBook -= (index-prev)*cnt;
            for (int i = prev ; i < index ; i++) {
                cntBook += cnt;
                if (cntBook >= maxSize/2) {
                    cout << i;
                    return 0;
                }
            }
        }
        // cout << cntBook << endl;
        cnt += p.second;
        prev = index;
    }
}