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
分析:
差分约束系统模版题。
d[i]表示左边界到i的个数。
设读入x,y,w。
关系为y-x>=w,也就是说x-y<=-w,x到y有一条-w的边。
然后我们知道,
0<=d[i]-d[i-1]<=1(自己脑补)
也就是
d[i]-d[i-1]<=1,d[i-1]-d[i]<=0
就是说i和i-1有一条1的边,i-1和i有一条0的边,跑最短路。(这题本来为最长路,强行写成最短路的话要输出相反数)。
代码:
const
MaxE=500001;
MaxV=150001;
type
rec=record
x,y,w,next:longint;
end;
var
n,m,c,qx,i,x,y,w,o,q,maxx,maxy:longint;
g:array [0..Maxv] of rec;
ls:array [0..Maxe] of longint;
v,d,list,sum:array [0..maxe] of longint;
procedure spfa(first:longint);
var
head,tail,t,i,qe:longint;
begin
tail:=1;
list[1]:=first;
for i:=maxx-1 to maxy do
d[i]:=maxlongint;
d[first]:=0;
v[first]:=1;
while tail<>0 do
begin
t:=ls[list[tail]];
qe:=list[tail];
tail:=tail-1;
while t>0 do
with g[t] do
begin
if d[x]+w<d[y] then
begin
d[y]:=d[x]+w;
if v[y]=0 then
begin
v[y]:=1;
tail:=tail+1;
list[tail]:=y;
inc(sum[y]);
if (sum[y]>=n) then
begin
writeln('No');
halt;
end;
end;
end;
t:=next;
end;
v[qe]:=0;
end;
end;
procedure add(x,y,w:longint);
begin
inc(o);
g[o].x:=x;
g[o].y:=y;
g[o].w:=w;
g[o].next:=ls[x];
ls[x]:=o;
end;
begin
read(n);
maxx:=maxlongint;
for i:=1 to n do
begin
readln(x,y,w);
add(x-1,y,-w);
if y>maxy then maxy:=y;
if x<maxx then maxx:=x;
end;
for i:=maxx to maxy do
begin
add(i,i-1,1);
add(i-1,i,0);
end;
spfa(maxx-1);
writeln(-d[maxy]);
end.