Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4181 Accepted Submission(s): 1577
Problem 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 endpoints 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 <= 50 000) - 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 <= 50 000 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
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Author
1384
一道很明显的差分约束题。
对于每个[l,r]对应的k可以列出方程 f[r]-f[l-1]>=k;
同时区间之间 f[i]-f[i-1]>=0; f[i-1]-f[i]>=-1;
然后就是拿所有区间的最左端点作源点跑一遍spfa();
结束。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<set>
#define INF 1000000000
#define clr(x) memset(x,0,sizeof(x))
#define clrmin(x) memset(x,-1,sizeof(x))
#define clrmax(x) memset(x,0x3f3f3f3f,sizeof(x))
using namespace std;
struct node
{
int to,val,next;
}edge[*];
int head[];
int dis[];
int n,maxx,minx,maxans,cnt,l,r,k;
set<int> Q;
void addedge(int l,int r,int k);
int min(int a,int b);
int max(int a,int b);
void spfa(int s);
int main()
{
while(scanf("%d",&n)!=EOF)
{
clrmin(head);
clrmin(dis);
Q.clear();
maxx=;
cnt=;
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&l,&r,&k);
addedge(l,r+,k);
maxx=max(r+,maxx);
minx=min(l,minx);
}
for(int i=minx;i<maxx;i++)
{
addedge(i,i+,);
addedge(i+,i,-);
};
spfa(minx);
printf("%d\n",dis[maxx]);
}
return ;
}
void addedge(int l,int r,int k)
{
edge[++cnt].to=r;
edge[cnt].val=k;
edge[cnt].next=head[l];
head[l]=cnt;
return;
}
int min(int a,int b)
{
return a<b?a:b;
}
int max(int a,int b)
{
return a>b?a:b;
}
void spfa(int s)
{
dis[s]=;
Q.insert(s);
int v,k;
while(!Q.empty())
{
v=*Q.begin();
Q.erase(v);
k=head[v];
while(k!=-)
{
if(dis[v]+edge[k].val>dis[edge[k].to])
{
dis[edge[k].to]=dis[v]+edge[k].val;
Q.insert(edge[k].to);
}
k=edge[k].next;
}
}
return ;
}