链接:http://poj.org/problem?id=1201
Intervals
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 23123 |
|
Accepted: 8728 |
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
题意:
给你n个闭区间
【a,b】 及c
要求区间内的点数必须要大于等于c
点只存在于区间内。
然后问负无穷到正无穷最多有多少点。
做法:
我们从最左边到最右边。
区间内最少有c个点,说明点(a-1)到b点 最少会增加c个点。
也就是说 b-(a-1)>=c 转换下 (a-1)-b<=-c 就是差分约束的公式了。
然后就是建边 建一条权值为-c 的b到(a-1)的边就行了。
然后取最小的坐标x,取最大的坐标d。
因为求最大值 所以得公式 d-(x-1)>=?
加个负号, (x-1)-d<=-? ,所以最后最短路 计算 d点到(x-1)的最短距离,然后取负就是答案了。
但是这样条件还不够。
还有关键的是相邻点之间的建边,设相邻两点 (i) 和(i+1)
走到(i+1)数到的点数 肯定是比左边的(i)要大的 , 所以 (i+1)-(i)>=0 加个负号, (i)-(i+1)<=0
而从(i)走到(i+1)最多只增加了一个点,所以(i+1)-(i)<=1 ,
所以还要建这些边,才能跑出最后的答案。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#include
#include
#include
#include
#include
#include