Test instructions
Select as few intervals as possible to cover the 1~t interval for n intervals.
If you cannot overwrite the output-1
Ideas:
Classic Greedy Range Overlay
Sort all the intervals from the starting point to the big one and take the end to the right-most interval.
Code
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < vector> #include <string> #include <queue> #include <map> #include <set> #include <cmath >using namespace std; #define INF 0x3f3f3f3f#define PI acos ( -1.0) #define MEM (A, B) memset (A, B, sizeof (a)) typedef PAIR&L T;int,int> pii;typedef Long Long ll;//------------------------------const int MAXN = 25005;int N, t;struct node{int S,e; BOOL operator < (const node NT) const{return s < Nt.s; }}cow[maxn];void Init () {for (int i = 0; i < n; i++) {scanf ("%d%d", &COW[I].S,&COW[I].E); } sort (cow, cow + N);} void Solve () {if (Cow[0].s > 1) {printf (" -1\n"); Return } int start_ = 1, End_ = 1, cnt = 1; for (int i = 0; i < n; i++) {if (Cow[i].s <= start_) end_ = max (End_, COW[I].E); else{cnt++; Start_ = End_ + 1; if (Cow[i].s <= Start_) ENd_ = Max (End_, COW[I].E); else{printf (" -1\n"); return;} } if (End_ >= T) break; } if (End_ >= T) printf ("%d\n", CNT); else printf (" -1\n");} int main () {scanf ("%d%d", &n,&t); Init (); Solve (); return 0;}
Was wrong once t_t because the solve function in the last few judgment of a condition end >= T......sigh ...
POJ 2376 greedy interval coverage problem