[USACO14JAN] Recording the Moolympics S

给定一些区间,求最大可二重区间集。(n leq 150)

Solution

贪心,将区间按照右端点排序,依次处理,若可以放入则必定放入,并且尽可能放在当前结束时间最晚的那一组

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

const int N = 205;

struct range {
    int l,r;
    bool operator < (const range &b) {
        return r < b.r;
    }
} a[N];

int n,p1,p2,ans;

signed main() {
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i].l>>a[i].r;
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++) {
        if(a[i].l>=p1 && a[i].l>=p2) {
            if(p1>p2) p1=a[i].r;
            else p2=a[i].r;
            ++ans;
        }
        else {
            if(a[i].l>=p1) p1=a[i].r, ++ans;
            if(a[i].l>=p2) p2=a[i].r, ++ans;
        }
    }
    cout<<ans;
}

原文地址:https://www.cnblogs.com/mollnn/p/12446632.html