AcWing 295. Cleanup flights

This problem is clearly with the interval coverage is the same, and range in \ (1000000 \) or less, not discrete, can be solved directly greedy.

In particular: Let \ (nxt [i] \) is the range from \ (I \) starting point can reach the furthest right.

A section of the jump, jump until the end \ (T \) or jump no.

\ (Tips \) : Note that this is the point coverage and range of coverage is the side of the cover, pay attention to details jump.

#include <iostream>
using namespace std;
const int N = 1000001;
int n, m, p = 1, ans = 0, nxt[N];
int main() {
    scanf("%d%d", &m, &n);
    for (int i = 0, l, r; i < m; i++) 
        scanf("%d%d", &l, &r), nxt[l] = max(nxt[l], r);
    for (int i = 1; i <= n; i++) nxt[i] = max(nxt[i], nxt[i - 1]);
    while(p <= n && nxt[p] >= p) p = nxt[p] + 1, ans++;
    printf("%d\n", p <= n ? -1 : ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/dmoransky/p/11960714.html