Language: Default
Intervals
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
Southwestern Europe 2002
|
首先讲一下什么是差分约束:
源自:http://www.cnblogs.com/void/archive/2011/08/26/2153928.html
比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图
由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了
根据以上的解法,我们可能会猜到求解过程实际就是求从a到c的最短路径,没错的....简单的说就是从a到c沿着某条路径后把所有权值和k求出就是c -a<=k的一个
推广的不等式约束,既然这样,满足题目的肯定是最小的k,也就是从a到c最短距离...
总结一下:
//差分约束
//求最大值:变为x-y<=k的标准形式,求出最短路径
//求最小值:变为x-y>=k的标准形式,求出最长路径
回到本题:
大致题意:
给出数轴上的n个区间[ai,bi],每个区间都是连续的int区间。
现在要在数轴上任意取一堆元素,构成一个元素集合V
要求每个区间[ai,bi]和元素集合V的交集至少有ci不同的元素
求集合V最小的元素个数。
显然我们可以令S[i]为start->i区间与V交集的不同元素个数,
那么我们可以知道S[b] - S[a-1] >= c,又0<=S[i]-S[i-1]<=1;
所以得到以下三个约束关系:
// S[b] - S[a-1] >= c
// S[i] - S[i-1] >= 0
// S[i-1] - S[i] >= -1
建图,题意求最小值,则求出最长路径即可。
CODE:
#include <iostream> #include <cstdio> #include <cstring> #include <set> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define bug cout<<"bug\n" const int MAXN = 50007; const int MAXM = 200007; int head[MAXN],index; struct node { int v,next,cost; } edge[MAXM]; void add_edge(int u,int v,int cost) { edge[index].v=v; edge[index].cost=cost; edge[index].next=head[u]; head[u]=index++; } bool vis[MAXN]; int dist[MAXN]; int SPFA(int s,int t) { memset(vis,0,sizeof(vis)); for(int i=s; i<=t; ++i)dist[i]=-INF; dist[s]=0; queue<int> q; while(!q.empty())q.pop(); q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i+1; i=edge[i].next) { int v=edge[i].v; if(dist[v]<dist[u]+edge[i].cost) { dist[v]=dist[u]+edge[i].cost; if(!vis[v]) { vis[v]=1; q.push(v); } } } } return dist[t]; } //差分约束:- - //求最大值:变为x-y<=k的标准形式, 求出最短路径 //求最小值:变为x-y>=k的标准形式,求出最长路径 //S[b] - S[a-1] >= c //S[i] - S[i-1] >= 0 //S[i-1] - S[i] >= -1 int main() { int n,a,b,c; while(scanf("%d",&n)!=EOF) { index=0; memset(head,-1,sizeof(head)); int maxx=0,minx=INF; for(int i=0; i<n; ++i) { scanf("%d%d%d",&a,&b,&c); add_edge(a,b+1,c); minx=min(minx,a); maxx=max(b+1,maxx); } for(int i=minx; i<maxx; ++i) { add_edge(i,i+1,0); add_edge(i+1,i,-1); } cout<<SPFA(minx,maxx)<<endl; } return 0; }