POJ 2376 greedy interval coverage problem

Source: Internet
Author: User

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", &AMP;COW[I].S,&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.