Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
Intervals Time limit: Seconds Memory Limit: 32768 KB
You are given n closed, integer intervals [AI, bi] and n integers c1, ..., CN.
Write a program:
> reads the number of intervals, their endpoints and integers C1, ..., CN from the standard input,
> computes the minimal size of a set Z of integers which have at least CI common elements with interval [AI, bi], for EA ch i = 1, 2, ..., N,
> writes the answer to the standard output.
Input
The first line of the input contains an integer n (1 <= n <=)-the number of intervals. The following n lines describe the intervals. The i+1-th line of the "input contains three integers ai, bi and CI separated by" spaces and such that 0 <= AI < = Bi <= and 1 <= ci <= bi-ai + 1.
Process to the end of file.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least CI elements with interval [AI, BI], for each i = 1, 2, ..., N.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Test instructions
For a sequence, there are n descriptions, [Ai,bi,ci], respectively, in the interval [Ai,bi], at least the number of CI belongs to the sequence. Outputs the shortest sequence that satisfies these n conditions (that is, the minimum number of digits included)
Analysis:
S[i] indicates that the number of s[i] from 0 to I is within this sequence.
So, for each description, you can get an equation s[bi]-s[ai-1]>=ci
At the same time, because each number is taken at most once, then there is s[i]-s[i-1]<=1, that is s[i-1]-s[i]>=-1;
Besides, there's s[i]-s[i-1]>=0;.
These three equations are used to construct graphs, which can be obtained by the longest road.
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%d",&u,&v,&d); theAdd_edge (u,v+1,-d); 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
poj1201/zoj1508/hdu1384 intervals (differential constraint)