C++ Solution
\nclass ExamRoom {\npublic:\n    int num;\n    set<int> sec;\n    ExamRoom(int N) {\n        num = N;\n    }\n    \n    int seat() {\n        if (sec.size() == 
```\nclass ExamRoom {\npublic:\n    int num;\n    set<int> sec;\n    ExamRoom(int N) {\n        num = N;\n    }\n    \n    int seat() {\n        if (sec.size() == 0){\n            sec.insert(0);\n            return 0;\n        } else if (sec.size() == 1) {\n            set<int> :: iterator it = sec.begin();\n            int res = 0;\n            if (*it >= (num - *it)) {\n                res = 0;\n            } else {\n                res = num - 1;\n            }\n            sec.insert(res);\n            return (res);\n        } \n        \n        int max_gap, min_pos, zero_gap, end_gap;\n        max_gap = min_pos = zero_gap = end_gap = 0;\n        bool zero_flag, end_flag;\n        zero_flag = end_flag = false;\n        if (sec.find(0) == sec.end()) {\n            zero_flag  = true;\n            zero_gap = *(sec.begin());\n        } \n        if (sec.find(num - 1) == sec.end()) {\n            end_flag = true;\n            end_gap = (num - 1) - (*(sec.rbegin()));\n        }\n        if (zero_flag || end_flag) {\n            if (end_gap > zero_gap) {\n                zero_flag = false;\n            } else if (end_gap <= zero_gap) {\n                end_flag = false;\n            } \n        }\n        \n        for (set<int> :: iterator it = sec.begin(); it != sec.end(); it++) {\n            set<int> :: iterator iter = it;\n            if (++iter == sec.end())\n                break;\n            if ((abs(*iter - *it) / 2) > max_gap) {\n                max_gap = (abs(*iter - *it) / 2);\n                min_pos = *it + max_gap;\n            } else if ((abs(*iter - *it) / 2) == max_gap) {\n                if (min_pos > (*it + max_gap)) {\n                    min_pos = *it + max_gap;\n                }\n            }\n        }\n        int res = min_pos;\n        if (zero_flag) {\n            if (zero_gap >= max_gap) {\n                res = 0;\n            } else {\n                res = min_pos;\n            }\n        } else if (end_flag) {\n            if (end_gap > max_gap) {\n                res = num - 1;\n            } else {\n                res = min_pos;\n            }\n        }\n        sec.insert(res);\n        return res;\n    }\n    \n    void leave(int p) {\n        sec.erase(p);\n    }\n};\n\n```
[]