Test instructions: There are n intervals [a, b], and each interval has a value of C. Find the elements in a set so that each interval has at least C elements in this set, asking the smallest set size.
Idea: Set d[i+1] means 0 to i how many number in this set, obviously for each interval, d[b+1]-d[a]>=c, in order to meet the requirements of the topic. But this does not make all the collections connected. Continue to excavate the condition, according to d[] 's definition can get, 0<=d[i+1]-d[i]<=1.
Thus three inequalities can be derived:
D[b+1]-d[a]>=c
D[i+1]-d[i]>=0
D[i]-d[i+1]>=-1
It is obvious that this is a differential constraint system, but it is the longest road to be obtained. The longest way to find the minimum to maximum value after building a map is the answer.
1#include <stdio.h>2#include <string.h>3 Const intn=50000+111, m=200000, inf=0x3f3f3f3f;4 structnode{5 intV,w,next;6 }e[m];7 intHead[n],p[n],d[n];8 intq[m<<2],l,r,js;9 voidAddintUintVintW)Ten { Onee[js].v=v,e[js].w=W; Ae[js].next=head[u],head[u]=js++; - } - voidSPFA (intSintt) the { -L=r=0;intu,v,w,i; - for(i=s;i<=t;i++) d[i]=-INF; -Memset (P,0,sizeof(P)); +q[++r]=s;d[s]=0; - while(l<R) + { Ap[u=q[++l]]=0; at for(i=head[u];i!=-1; i=e[i].next) - { -v=e[i].v,w=E[I].W; - if(d[v]<d[u]+W) - { -d[v]=d[u]+W; in if(!P[v]) - { top[v]=1; +q[++r]=v; - } the } * } $ }Panax Notoginsengprintf"%d\n", D[t]); - } the intMain () + { A intN,u,v,w,i,ma,mi; the while(SCANF ("%d", &n)! =EOF) + { -memset (head,-1,sizeof(head)); $Js=ma=0; mi=INF; $ while(n--) - { -scanf"%d%d%d",&u,&v,&W); theAdd (u,v+1, W); - if(U<MI) mi=u;Wuyi if(v+1>MA) ma=v+1; the } - for(i=mi;i<ma;i++) Wu { -Add (i,i+1,0); AboutAdd (i+1, i,-1); $ } - SPFA (mi,ma); - } - return 0; A}
POJ 1201 Intervals