【POJ 1201 Intervals】
Time Limit: 2000MS
Meamory Limit: 65536K
Total Submissions: 27949
Accepted: 10764
Description
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
reads the number of intervals, their end points and integers c1, ..., cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,
writes the answer to the standard output.
Input
The first line of the input contains an integer n (1 <= n <= 50000) -- 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 single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.
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
Source
【题解】
①这是一个差分约束模型。
②本题约束条件基于前缀和sum数组。
③外加相邻点的前缀和关系(最多相差1)。
#include<queue> #include<stdio.h> #include<algorithm> #define inf 1000000007 #define go(i,a,b) for(int i=a;i<=b;i++) #define fo(i,a,x) for(int i=a[x],v=e[i].v;i;i=e[i].next,v=e[i].v) const int N=50010; using namespace std; bool inq[N];queue<int>q; struct E{int v,next,w;}e[N<<1]; int n,a[N],b[N],c[N],head[N],k=1,S=1e9,T,d[N]; void ADD(int u,int v,int w){e[k]=(E){v,head[u],w};head[u]=k++;} void Build() { go(i,1,n)scanf("%d %d %d",a+i,b+i,c+i); go(i,1,n)S=min(S,a[i]-1),T=max(T,b[i]); go(i,1,n)ADD(a[i]-1,b[i],c[i]); go(i,S+1,T)ADD(i,i-1,-1); go(i,S,T-1)ADD(i,i+1,0); } void SPFA() { go(i,S,T)d[i]=-inf;d[S]=0;q.push(S);int u; while(!q.empty()){inq[u=q.front()]=0;q.pop();fo(i,head,u) if(d[u]+e[i].w>d[v])d[v]=d[u]+e[i].w,!inq[v]?q.push(v),inq[v]=1:1;} } int main() { scanf("%d",&n);Build(); SPFA();printf("%d\n",d[T]); return 0; }//Paul_Guderian
.