"The main topic"
Given n intervals, ask how many intervals are selected so that they do not overlap one another.
Ideas
Water problem quq improve mood. Sort by the right endpoint size, updating the last right endpoint at a time, if the current left endpoint is greater than the last right endpoint is preferable.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxn=50000+ -;7 structnode8 {9 intL,r;Ten BOOL operator< (ConstNode &x)Const One { A return(r<X.R); - } - }COW[MAXN]; the intN; - - intMain () - { +scanf"%d",&n); - for(intI=0; i<n;i++) scanf ("%d%d",&cow[i].l,&COW[I].R); +Sort (cow,cow+n); A intr=-1, ans=0; at for(intI=0; i<n;i++) - { - if(cow[i].l>=R) - { -R=COW[I].R; -ans++; in } - } tocout<<ans<<Endl; + return 0; -}
"Greedy flood problem" bzoj3410-[usaco2009 dec]selfish grazing selfish grass eater