[Greedy] poj 2376 cleaning Shifts

Source: Internet
Author: User

Test instructions

For n segments, ask you to cover every point in [1,t], with a minimum number of segments.


Greedy, ascending in the x-coordinate, descending in the y-coordinate.

The first one is first placed by default.

It then continues to place the line segments based on the dependencies.

Note that [3,4] and [the] can be covered full [1,4].

Then notice that because the default is placed first, the state of the first is to be given a special sentence.

Including the first one if [1,t] Output 1.


#include "cstdlib" #include "Cstdio" #include "CString" #include "Cmath" #include "queue" #include "algorithm" #include " iostream "#include" map "using namespace std;struct node{int x, y;}        P[26000];int CMP (node A,node b) {if (a.x==b.x) {if (A.Y&GT;B.Y) return 1;    return 0;    } if (a.x<b.x) return 1; return 0;}    int main () {int n,m;        while (scanf ("%d%d", &n,&m)!=-1) {for (int i=0;i<n;i++) scanf ("%d%d", &p[i].x,&p[i].y);        Sort (p,p+n,cmp);            if (p[0].x!=1) {puts ("-1");        Continue        } int xx=0;        int yy=p[0].y;        int Ans=1;            for (int i=1;i<n;i++) {if (P[I].Y&LT;YY) continue;            if (yy>=m) break;                if (p[i].x>yy+1) {ans=-1;            Break                } if (p[i].x>xx+1) {ans++;                Xx=yy;            YY=P[I].Y;       } else {         YY=P[I].Y;        }} if (Yy<m) ans=-1;    printf ("%d\n", ans); } return 0;}

[Greedy] poj 2376 cleaning Shifts

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.