http://acm.hdu.edu.cn/showproblem.php?pid=1384
Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4622 Accepted Submission(s): 1762
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
Sample Output
6
Author
1384
Recommend
Eddy | We have carefully selected several similar problems for you: 1529 1531 1548 1534 1317
题意:给你几个区间,几个值。表示每个区间至少选择c个数字来使用,要求满足所有区间的最低要求。输出最少要选择几个点来满足这所有的要求。
差分约束的经典题目
分析:题目中相当于给出了这样的条件 s[R]−s[L−1]>=c s [ R ] − s [ L − 1 ] >= c ;对于每一个要求我们都可以写作这样,如果说有这样两个条件 s[R]−s[L−1]>=c1,s[R]−s[L−1]>=c2 s [ R ] − s [ L − 1 ] >= c 1 , s [ R ] − s [ L − 1 ] >= c 2 , c1<c2 c 1 < c 2 ;那么我们肯定优先选择c2,这里是不是就有些许感觉了!这简直就是最长路的松弛操作!
所以这个题目也就可以转换成一个求解最长路的题目了。
试想 如果 s[R]−s[L−1]<=c1 s [ R ] − s [ L − 1 ] <= c 1 这样的题目,感觉就可以用最短路解决了!
spfa实现:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
#define Irish_Moonshine main
const int maxn = 1e5 + 2333;
int n; int sz;
int head[maxn];
int dis[maxn];
int vis[maxn];
int L, R;
struct node {
int u, v, cs, nx;
}edg[maxn];
void add(int u, int v, int c) {
++sz;
int tmp = head[u];
head[u] = sz;
edg[sz].nx = tmp;
edg[sz].u = u;
edg[sz].v = v;
edg[sz].cs = c;
}
void spfa() {
memset(dis, 0, sizeof dis);
memset(vis, 0, sizeof vis);
queue<int> q;
for (int i = L; i <= R; i++) { q.push(i); vis[i] = 1; }
dis[L] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = head[x]; i; i = edg[i].nx) {
if (dis[edg[i].v] < dis[edg[i].u] + edg[i].cs) {//最长路松弛
dis[edg[i].v] = dis[edg[i].u] + edg[i].cs;
if (!vis[edg[i].v]) {
vis[edg[i].v] = 1;
q.push(edg[i].v);
}
}
}
if (x > L&&dis[x - 1] < dis[x] - 1) {
dis[x - 1] = dis[x] - 1;
if (!vis[x - 1]) {
vis[x - 1] = 1;
q.push(x - 1);
}
}
if (x < R&&dis[x + 1] < dis[x]) {
dis[x + 1] = dis[x];
if (!vis[x + 1]) {
vis[x + 1] = 1;
q.push(x + 1);
}
}
}
}
int Irish_Moonshine()
{
while (~scanf("%d", &n)) {
L = INF, R = 0;
memset(head, -1, sizeof head);
sz = 0;
int u, v, cst;
for (int i = 1; i <= n; i++) {
scanf("%d %d %d", &u, &v, &cst);
add(u, ++v, cst);
L = min(L, u);
R = max(R, v);
}
spfa();
printf("%d\n", dis[R]);
}
return 0;
}
//_CRT_SECURE_NO_WARNINGS