1. 程式人生 > >CF1101C Division and Union

CF1101C Division and Union

題目地址:洛谷CF1101C

很easy的貪心,比賽時題目讀錯了QWQ

不想說什麼,直接上程式碼:

#include <bits/stdc++.h>
using namespace std;
const int N = 100006;
struct P {
    int l, r, i;
    bool operator < (const P p) const {
        return l < p.l || (l == p.l && (r < p.r || (r == p.r && i < p.i)));
    }
} a[N];
int ans[N];

void work() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d %d", &a[i].l, &a[i].r);
        a[i].i = i;
    }
    sort(a + 1, a + n + 1);
    int x = 0, t = 0;
    for (int i = 1; i <= n; i++)
        if (x == 2) ans[a[i].i] = 2;
        else if (t < a[i].l) {
            ans[a[i].i] = ++x;
            t = a[i].r;
        } else {
            ans[a[i].i] = x;
            t = max(t, a[i].r);
        }
    if (x == 1) {
        puts("-1");
        return;
    }
    for (int i = 1; i <= n; i++)
        printf("%d ", ans[i]);
    puts("");
}

int main() {
    int t;
    cin >> t;
    while (t--) work();
    return 0;
}