【題解】Codeforces 1262A. Math Problem

Problem: http://codeforces.com/contest/1262/problem/A
Solution: Greedy

  • While reading input data, compare and record the maximum value (mxl) of the left bound and the minimum value (mnr) of the right bound of each segment.
  • [mnr, mxl], as a result, is the required segment.
  • Output 0 (zero) if mxl is less than mnr.
#include <iostream>
using namespace std;

int main() {
    int m, n, mxl, mnr, l, r;
    cin >> m;
    while (m--){
        while (cin >> n){
            mxl = 0;
            mnr = 0x7FFFFFFF;
            while (n--){
                cin >> l >> r;
                mxl = max(mxl, l);
                mnr = min(mnr, r);
            }
            if ((mxl - mnr) > 0) cout << mxl - mnr << "\n";
            else cout << 0 << "\n";
        }
    }
}
分享本文 Share with friends