A typical greedy
Abstract look, in fact, in the interval, the interval can form a disjoint chain, demand chain of the minimum number of
Solution
Proof is actually very simple, you just find a usable throw into the like, because no matter where a fence lost, the last are still considered to be the last time, so in fact there is no difference
#include <. bits / STDC ++ H> the using namespace STD; int NUM = . 1 , n-; struct Node { int ST, ED, IND, Used;} A [ 50005 ]; BOOL CMP (Node A, Node B) { return A .st <b.st;} // sorted by start time BOOL CMP2 (Node A, Node B) { return a.ind <b.ind;} // a numbered ordering The priority_queue <pair < int , int >> Q; // priority queue int main () { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].st,&a[i].ed),a[i].ind=i;//记录序号 sort(a+1,a+n+1,cmp); A [ . 1 ] = .used . 1 ; q.push (the make_pair (-a [ . 1 ] .ed, . 1 )); // initialize a cowshed for ( int I = 2 ; I <= n-; I ++ ) { int X = - q.top () First;. int tmp = q.top () SECOND;. IF (X <A [I] .st) { // can be put into the opinions q.pop (); q.push(make_pair(-a[i].ed,tmp)); a[i].used=tmp; } the else { // can not be put on to open a q.push (the make_pair (-a [I] .ed, ++ NUM)); A [I] .used = NUM; // record } } sort(a+1,a+n+1,cmp2); printf("%d\n",num); for(int i=1;i<=n;i++) printf("%d\n",a[i].used);//输出 }