Test instructions
For n segments, ask you to cover every point in [1,t], with a minimum number of segments.
Ideas:
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.
Code:
#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>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<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