【題解】TIOJ 2054 . 最大矩形涵蓋(cover)

【題目敘述】https://tioj.ck.tp.edu.tw/problems/2054

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n, L, W, ans;
vector <pair<int, int> > v;

bool cmp(pair<int, int> x, pair<int, int> y){
    return x.first < y.first;
}

int main(int argc, const char * argv[]) {
    cin >> n >> L >> W;
    for (int i = 0, x, y; i < n; i++){
        cin >> x >> y;
        v.push_back({x, y});
    }
    sort(v.begin(), v.end(), cmp);
    int l = 0;
    for (int i = 0; i < n; i++){
        while (v[i].first-v[l].first > W) l++;
        vector <int> tmp;
        for (int j = l; j <= i; j++){
            tmp.push_back(v[j].second);
        }
        sort(tmp.begin(), tmp.end());
        int l2 = 0;
        for (int j = 0; j < (int)tmp.size(); j++){
            while (tmp[j]-tmp[l2] > L) l2++;
            ans = max(ans, j-l2+1);
        }
    }
    cout << ans << "\n";
}
分享本文 Share with friends