Reprint Please specify Source: http://www.cnblogs.com/fraud/ --by Fraud
Integer intervals
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
Description
An integer interval [A, a,], a < B, was a set of all consecutive integers beginning with a and ending with B.
Write a program that:finds the minimal number of elements in a set containing at least II different integers from each i Nterval.
Input
The first line of the input contains the number of intervals N, 1 <= n <= 10000. Each of the following n lines contains and integers a, b separated by a single space, 0 <= a < b <= 10000. They is the beginning and the end of an interval.
Output
Output the minimal number of elements in a set containing at least, different integers from each interval.
Sample Input
43 62 40) 24 7
Sample Output
4
The castrated version of the previous differential constraint (portal)
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cstdlib>5#include <algorithm>6#include <queue>7 using namespacestd;8typedefLong Longll;9typedef pair<int,int>PII;Ten #defineMAXN 50010 One #defineREP (a,x) for (int a=0; a<x; a++) A #defineINF 0x7fffffff - #defineCLR (a,x) memset (a,x,sizeof (A)) - structNode { the intV,d,next; -}edge[3*MAXN]; - intHEAD[MAXN]; - intE=0; + voidInit () - { +REP (I,MAXN) head[i]=-1; A } at voidAdd_edge (intUintVintd) - { -edge[e].v=v; -Edge[e].d=D; -edge[e].next=Head[u]; -head[u]=e; ine++; - } to BOOLVIS[MAXN]; + intDIS[MAXN]; - voidSPFA (ints) { theCLR (Vis,0); *REP (I,MAXN) dis[i]=i==s?0: INF; $queue<int>Q;Panax Notoginseng Q.push (s); -vis[s]=1; the while(!q.empty ()) + { A intx=Q.front (); the Q.pop (); + intt=Head[x]; - while(t!=-1) $ { $ inty=edge[t].v; - intD=edge[t].d; -t=Edge[t].next; the if(dis[y]>dis[x]+d) - {Wuyidis[y]=dis[x]+D; the if(Vis[y])Continue; -vis[y]=1; Wu Q.push (y); - } About } $vis[x]=0; - } - } - intMain () A { +Ios::sync_with_stdio (false); the intN; - intu,v,d; $ intans=0; the intmaxn=0; the intminn=MAXN; the while(SCANF ("%d", &n)! = eof&&N) the { -E=0; in init (); the REP (i,n) the { Aboutscanf"%d%d",&u,&v); theAdd_edge (u,v+1,-2); theMaxn=max (maxn,v+1); theminn=min (u,minn); + } - for(inti=minn;i<maxn;i++){ theAdd_edge (i+1I1);BayiAdd_edge (i,i+1,0); the } the SPFA (Minn); -cout<<-dis[maxn]<<Endl; - } the return 0; the}code June
poj1716 Integer intervals (differential constraint)